From patchwork Wed Jun 19 11:06:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13703668 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5DC0135A6D for ; Wed, 19 Jun 2024 11:06:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718795208; cv=none; b=nhdBFBG/81udgyRLdfunt4tK3SpZpetNAqOYfpRRA00gcyPf05tiReqWZPXXQk4cyumpmcH+sw32UNqSXjYdQCxxdDzLurd6sa5Fz9qNLHMNsVPciF47ooHUjTBFzdiSULsNRMIjnkOBcXfH65etXw4X/YQ2LISjF+Biby/7Llo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718795208; c=relaxed/simple; bh=rR6DMQkCf2iqBmxgUqpf6uND0QhqDp9IavAFgv49zr8=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=l9w2L3hL2KvoGyWmIP/19Ww+T8jc36/6G/M6GI//sVshzngO6k4/O1AX28b/MemWgJ2iJYQEHnCYHZI5pyRhCnUlcoGMltdoRvmKuDoGF2BGUzwO/h5QjGmtI+jkVsY+6r4Nl3LAI2bu4hBWBw07jwiWuRV8UG1sdXpKrUsQrwg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hcecr0Hu; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hcecr0Hu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F366C4AF1A for ; Wed, 19 Jun 2024 11:06:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718795208; bh=rR6DMQkCf2iqBmxgUqpf6uND0QhqDp9IavAFgv49zr8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=hcecr0HurVZX59ivMin8axsiiBMz/4G8PRvQPnR46FuM9YSd+GRx+rQmByNJRGdqW sCoXqHrAiNtT/vwdFpA6nPLI4YbU2+7m2qfbxuGoZx0/E9oSQcefnqZY+SLjJNNAGW ZGoWKlGpMZ5acnR6fsBzPLGh0ck4uuWc6823NguI6JqJOFPTWXu/HQRWPDcANooXos Hgj6BxcJeeE/SpwVFagtxN3g0MUBIKhYu5gLqWvcejW5CrFbInmJkVtMx3aj8ydS15 zep4XGP9CGiZ2EgA12R16yflYwBnTb02kQucqhJCTLcmI6NL10ClzuC68xgAIL9cl9 vA5tGQwtYHeEw== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 1/3] btrfs: remove superfluous metadata check at btrfs_lookup_extent_info() Date: Wed, 19 Jun 2024 12:06:42 +0100 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana If we didn't found an extent item with the initial btrfs_search_slot() call, it's pointless to test if the "metadata" variable is "true", because right after we check if the key type is BTRFS_METADATA_ITEM_KEY and that is the case only when "metadata" is set to "true". So remove the redundant check. Signed-off-by: Filipe Manana --- fs/btrfs/extent-tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 21d123d392c0..a14d2a74d7fd 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -139,7 +139,7 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, if (ret < 0) goto out_free; - if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) { + if (ret > 0 && key.type == BTRFS_METADATA_ITEM_KEY) { if (path->slots[0]) { path->slots[0]--; btrfs_item_key_to_cpu(path->nodes[0], &key, From patchwork Wed Jun 19 11:06:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13703669 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ACA3A136E2C for ; Wed, 19 Jun 2024 11:06:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718795209; cv=none; b=brr12QHwGP0lZAl/7i1xSInpMGEo4rFNDacxJL9zrxmk9yRWGVLp/wIPFDoYqaoVcicyohRuwRZiYNoU8mGCTyPv2ozrw+oayvNqaBq/9bMfOPb/uuO1kyRtYC138VmqBFH7g7L6D6QNNm8RyOO5PRyNobK8OeGk9Ymx4NXB8mk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718795209; c=relaxed/simple; bh=xKlfs9QamTKzn2pmngIB+tTSvA0otkice90N/pdYA6I=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LkLSr2pFZSaBSR0lzySBi/0pzvH70XzB5zcx5GVdf487Mur8VZV67Pnmg+E4D1TUaZ7vdKkQKZlPrDllU7i6l6kGjqdL556iQ6bfe81ximPoSyPPpa2hbu+PdsW5yYZwILKufCA++CmMrxNq8NoxBO52sl9DLbANQySBNx90oic= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VQupMaHY; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VQupMaHY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0D4B2C2BBFC for ; Wed, 19 Jun 2024 11:06:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718795209; bh=xKlfs9QamTKzn2pmngIB+tTSvA0otkice90N/pdYA6I=; h=From:To:Subject:Date:In-Reply-To:References:From; b=VQupMaHY4uRRGecjDK6QC+54DPF0/DWLufchXNJOiUyGUsp/HhV965pO4rq6itiyV qd5y6gmfsRKn8sMtIA3tHdjQkXx8ojBi/uJBK1VmC9fUw9fBn4Oh63KCRzs3NXyhya ba3Ayktn18XN0gFWD4qDYa1WJ7A7YbQZp4VfjmmYgvrmpP9h0EayNThCne2+xqiJM2 wYeQin9pXAU6VSSbR/xLrn7uwMEW/OmuPCXY2GXAspwIHxQkGeJ6RO0zfwiyZpFxF+ IAjwHN3weFcvXX+mCbIi2hfDQ9zCoTUDHeEISzghEIT8a2+btfJacY9dqfOX6RFMua 6/2yC3OFmFoKg== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 2/3] btrfs: reduce nesting for extent processing at btrfs_lookup_extent_info() Date: Wed, 19 Jun 2024 12:06:43 +0100 Message-Id: <5be9e81e355e5e763a401ef38add72841bfdacd4.1718794792.git.fdmanana@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana Instead of using an if-else statement when processing the extent item at btrfs_lookup_extent_info(), use a single if statement for the error case since it does a goto at the end and leave the success (expected) case following the if statement, reducing indentation and making the logic a bit easier to follow. Also make the if statement's condition as unlikely since it's not expected to ever happen, as it signals some corruption, making it clear and hint the compiler to generate more efficient code. Signed-off-by: Filipe Manana --- fs/btrfs/extent-tree.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index a14d2a74d7fd..94dffe6b6252 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -104,10 +104,7 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, struct btrfs_delayed_ref_head *head; struct btrfs_delayed_ref_root *delayed_refs; struct btrfs_path *path; - struct btrfs_extent_item *ei; - struct extent_buffer *leaf; struct btrfs_key key; - u32 item_size; u64 num_refs; u64 extent_flags; u64 owner = 0; @@ -152,16 +149,11 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, } if (ret == 0) { - leaf = path->nodes[0]; - item_size = btrfs_item_size(leaf, path->slots[0]); - if (item_size >= sizeof(*ei)) { - ei = btrfs_item_ptr(leaf, path->slots[0], - struct btrfs_extent_item); - num_refs = btrfs_extent_refs(leaf, ei); - extent_flags = btrfs_extent_flags(leaf, ei); - owner = btrfs_get_extent_owner_root(fs_info, leaf, - path->slots[0]); - } else { + struct extent_buffer *leaf = path->nodes[0]; + struct btrfs_extent_item *ei; + const u32 item_size = btrfs_item_size(leaf, path->slots[0]); + + if (unlikely(item_size < sizeof(*ei))) { ret = -EUCLEAN; btrfs_err(fs_info, "unexpected extent item size, has %u expect >= %zu", @@ -170,6 +162,10 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, goto out_free; } + ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); + num_refs = btrfs_extent_refs(leaf, ei); + extent_flags = btrfs_extent_flags(leaf, ei); + owner = btrfs_get_extent_owner_root(fs_info, leaf, path->slots[0]); BUG_ON(num_refs == 0); } else { num_refs = 0; From patchwork Wed Jun 19 11:06:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13703670 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E4B96136E2C for ; Wed, 19 Jun 2024 11:06:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718795211; cv=none; b=WLA8fPBxsbHtF+7J8kSUdZudllRg9O97Qvflxsh1BAq8Cy+bziqlsfOGd7FW0yYEz221jk8Hols5cTZWhOQGziImXtXwTTSP2ORio9jRma5kVchoEL+rzb8KCxItvhJRmLWSzIhd6TMO4bXvMKMNRkCheq57osVGsN5a8KWLGXM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718795211; c=relaxed/simple; bh=5KntMGn2AISuksmrgBroZsIxyJJPaaz0bTZ8QzbofOY=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZyJCrcRjzqIn23xASPTLRDqBfACYRSR+GCJHNm0cfB8JPH2sXPreiDwqBIg5oaiIFXSh2adOY7SuRiSkJP9N9gROuc/kY3GDUWHmcqqr1bnx8C2MOBAOBiequLz8rsWbAiVOA1Sdvvq+bbM8ckJKA8zSQPXdJmiJb4CMz4/HHoU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZP1ow4c+; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZP1ow4c+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF6C1C2BBFC for ; Wed, 19 Jun 2024 11:06:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718795210; bh=5KntMGn2AISuksmrgBroZsIxyJJPaaz0bTZ8QzbofOY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ZP1ow4c+1jdbh/7V11bK07Lz4ZtN/qmoL96XdkuLjKagV8zwcbEKA+i4Ida6vHD2O OykvligJ/dE/iXD00nUdd3c5E8ZW6QdgibeDMQHxtL5tVbhQJY56GcCr7LG+iEvwIx yT0h+aejDKP5XaFzKHP1C4Yzq1iDiLBiM7ASNVm1qiCIImL4L5t63z92grG4GLCYP9 6L5unwXu31EF+A+1EoVM+yqQCRdokoe+PBz6zou3FF7uAM9n8r6ALcVVxXMXPkAVFo PZXbIFpSAN/NjXcSzfEdv2OT2ekHU3zUsBhFfOfiuOCAtA/yFw42jweQfANO45ALKa zlLg3S+ewIu1Q== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 3/3] btrfs: don't BUG_ON() when 0 reference count at btrfs_lookup_extent_info() Date: Wed, 19 Jun 2024 12:06:44 +0100 Message-Id: <7ebd3b8d03895dc1d9ad3bcdf12b954453237ccf.1718794792.git.fdmanana@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana Instead of doing a BUG_ON() handle the error by returning -EUCLEAN, aborting the transaction and logging an error message. Signed-off-by: Filipe Manana --- fs/btrfs/extent-tree.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 94dffe6b6252..23a7cac108eb 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -164,9 +164,16 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); num_refs = btrfs_extent_refs(leaf, ei); + if (unlikely(num_refs == 0)) { + ret = -EUCLEAN; + btrfs_err(fs_info, + "unexpected zero reference count for extent item (%llu %u %llu)", + key.objectid, key.type, key.offset); + btrfs_abort_transaction(trans, ret); + goto out_free; + } extent_flags = btrfs_extent_flags(leaf, ei); owner = btrfs_get_extent_owner_root(fs_info, leaf, path->slots[0]); - BUG_ON(num_refs == 0); } else { num_refs = 0; extent_flags = 0; @@ -193,10 +200,19 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, goto search_again; } spin_lock(&head->lock); - if (head->extent_op && head->extent_op->update_flags) + if (head->extent_op && head->extent_op->update_flags) { extent_flags |= head->extent_op->flags_to_set; - else - BUG_ON(num_refs == 0); + } else if (unlikely(num_refs == 0)) { + spin_unlock(&head->lock); + mutex_unlock(&head->mutex); + spin_unlock(&delayed_refs->lock); + ret = -EUCLEAN; + btrfs_err(fs_info, + "unexpected zero reference count for extent %llu (%s)", + bytenr, metadata ? "metadata" : "data"); + btrfs_abort_transaction(trans, ret); + goto out_free; + } num_refs += head->ref_mod; spin_unlock(&head->lock);