Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH 0/3] btrfs: some cleanups for btrfs_lookup_extent_info()
@ 2024-06-19 11:06 fdmanana
  2024-06-19 11:06 ` [PATCH 1/3] btrfs: remove superfluous metadata check at btrfs_lookup_extent_info() fdmanana
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: fdmanana @ 2024-06-19 11:06 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

Details in the change logs. Trivial changes.

Filipe Manana (3):
  btrfs: remove superfluous metadata check at btrfs_lookup_extent_info()
  btrfs: reduce nesting for extent processing at btrfs_lookup_extent_info()
  btrfs: don't BUG_ON() when 0 reference count at btrfs_lookup_extent_info()

 fs/btrfs/extent-tree.c | 48 ++++++++++++++++++++++++++----------------
 1 file changed, 30 insertions(+), 18 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/3] btrfs: remove superfluous metadata check at btrfs_lookup_extent_info()
  2024-06-19 11:06 [PATCH 0/3] btrfs: some cleanups for btrfs_lookup_extent_info() fdmanana
@ 2024-06-19 11:06 ` fdmanana
  2024-06-19 11:06 ` [PATCH 2/3] btrfs: reduce nesting for extent processing " fdmanana
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: fdmanana @ 2024-06-19 11:06 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

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 <fdmanana@suse.com>
---
 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,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/3] btrfs: reduce nesting for extent processing at btrfs_lookup_extent_info()
  2024-06-19 11:06 [PATCH 0/3] btrfs: some cleanups for btrfs_lookup_extent_info() fdmanana
  2024-06-19 11:06 ` [PATCH 1/3] btrfs: remove superfluous metadata check at btrfs_lookup_extent_info() fdmanana
@ 2024-06-19 11:06 ` fdmanana
  2024-06-19 11:06 ` [PATCH 3/3] btrfs: don't BUG_ON() when 0 reference count " fdmanana
  2024-06-19 23:24 ` [PATCH 0/3] btrfs: some cleanups for btrfs_lookup_extent_info() Qu Wenruo
  3 siblings, 0 replies; 5+ messages in thread
From: fdmanana @ 2024-06-19 11:06 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

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 <fdmanana@suse.com>
---
 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;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/3] btrfs: don't BUG_ON() when 0 reference count at btrfs_lookup_extent_info()
  2024-06-19 11:06 [PATCH 0/3] btrfs: some cleanups for btrfs_lookup_extent_info() fdmanana
  2024-06-19 11:06 ` [PATCH 1/3] btrfs: remove superfluous metadata check at btrfs_lookup_extent_info() fdmanana
  2024-06-19 11:06 ` [PATCH 2/3] btrfs: reduce nesting for extent processing " fdmanana
@ 2024-06-19 11:06 ` fdmanana
  2024-06-19 23:24 ` [PATCH 0/3] btrfs: some cleanups for btrfs_lookup_extent_info() Qu Wenruo
  3 siblings, 0 replies; 5+ messages in thread
From: fdmanana @ 2024-06-19 11:06 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

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 <fdmanana@suse.com>
---
 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);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/3] btrfs: some cleanups for btrfs_lookup_extent_info()
  2024-06-19 11:06 [PATCH 0/3] btrfs: some cleanups for btrfs_lookup_extent_info() fdmanana
                   ` (2 preceding siblings ...)
  2024-06-19 11:06 ` [PATCH 3/3] btrfs: don't BUG_ON() when 0 reference count " fdmanana
@ 2024-06-19 23:24 ` Qu Wenruo
  3 siblings, 0 replies; 5+ messages in thread
From: Qu Wenruo @ 2024-06-19 23:24 UTC (permalink / raw)
  To: fdmanana, linux-btrfs



在 2024/6/19 20:36, fdmanana@kernel.org 写道:
> From: Filipe Manana <fdmanana@suse.com>
> 
> Details in the change logs. Trivial changes.

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu
> 
> Filipe Manana (3):
>    btrfs: remove superfluous metadata check at btrfs_lookup_extent_info()
>    btrfs: reduce nesting for extent processing at btrfs_lookup_extent_info()
>    btrfs: don't BUG_ON() when 0 reference count at btrfs_lookup_extent_info()
> 
>   fs/btrfs/extent-tree.c | 48 ++++++++++++++++++++++++++----------------
>   1 file changed, 30 insertions(+), 18 deletions(-)
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-06-19 23:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-19 11:06 [PATCH 0/3] btrfs: some cleanups for btrfs_lookup_extent_info() fdmanana
2024-06-19 11:06 ` [PATCH 1/3] btrfs: remove superfluous metadata check at btrfs_lookup_extent_info() fdmanana
2024-06-19 11:06 ` [PATCH 2/3] btrfs: reduce nesting for extent processing " fdmanana
2024-06-19 11:06 ` [PATCH 3/3] btrfs: don't BUG_ON() when 0 reference count " fdmanana
2024-06-19 23:24 ` [PATCH 0/3] btrfs: some cleanups for btrfs_lookup_extent_info() Qu Wenruo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox