Skip to contentSkip to conversation

btrfs: some cleanups around read_block_for_search()

PATCH In review [email protected] 2 participants 8 messages Oct 16, 2024 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: some cleanups around read_block_for_search()

From: Filipe Manana <[email protected]>

A set of cleanups around read_block_for_search() and btrfs_verify_level_key(), mostly simplify arguments and remove redundant arguments and variables.

Filipe Manana (4):
btrfs: remove redundant level argument from read_block_for_search()
btrfs: simplify arguments for btrfs_verify_level_key()
btrfs: remove redundant initialization from btrfs_readahead_tree_block()
btrfs: remove local generation variable from read_block_for_search()

 fs/btrfs/ctree.c        | 29 ++++++++++++-----------------
 fs/btrfs/extent_io.c    |  1 -
 fs/btrfs/tree-checker.c | 16 ++++++++--------
 fs/btrfs/tree-checker.h |  4 ++--
 4 files changed, 22 insertions(+), 28 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/4 btrfs: remove redundant level argument from read_block_for_search()

From: Filipe Manana <[email protected]>

The level parameter passed to read_block_for_search() always matches the level of the extent buffer passed in the "eb_ret" parameter, which we are also extracting into the "parent_level" local variable.

So remove the level parameter and instead use the "parent_level" variable which in fact has a better (it's the level of the parent node from which we are reading a child node/leaf).

---
 fs/btrfs/ctree.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
fs/btrfs/ctree.c +9 -11
index 4f34208126f7..428c5650559a 100644
@@ -1508,7 +1508,7 @@ static noinline void unlock_up(struct btrfs_path *path, int level,
  */
 static int
 read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
-		      struct extent_buffer **eb_ret, int level, int slot,
+		      struct extent_buffer **eb_ret, int slot,
 		      const struct btrfs_key *key)
 {
 	struct btrfs_fs_info *fs_info = root->fs_info;
@@ -1542,7 +1542,7 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
 	tmp = find_extent_buffer(fs_info, blocknr);
 	if (tmp) {
 		if (p->reada == READA_FORWARD_ALWAYS)
-			reada_for_search(fs_info, p, level, slot, key->objectid);
+			reada_for_search(fs_info, p, parent_level, slot, key->objectid);
 
 		/* first we do an atomic uptodate check */
 		if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
@@ -1568,7 +1568,7 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
 		}
 
 		if (!p->skip_locking) {
-			btrfs_unlock_up_safe(p, level + 1);
+			btrfs_unlock_up_safe(p, parent_level + 1);
 			tmp_locked = true;
 			btrfs_tree_read_lock(tmp);
 			btrfs_release_path(p);
@@ -1595,12 +1595,12 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
 	}
 
 	if (!p->skip_locking) {
-		btrfs_unlock_up_safe(p, level + 1);
+		btrfs_unlock_up_safe(p, parent_level + 1);
 		ret = -EAGAIN;
 	}
 
 	if (p->reada != READA_NONE)
-		reada_for_search(fs_info, p, level, slot, key->objectid);
+		reada_for_search(fs_info, p, parent_level, slot, key->objectid);
 
 	tmp = btrfs_find_create_tree_block(fs_info, blocknr, check.owner_root, check.level);
 	if (IS_ERR(tmp)) {
@@ -2236,7 +2236,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 			goto done;
 		}
 
-		err = read_block_for_search(root, p, &b, level, slot, key);
+		err = read_block_for_search(root, p, &b, slot, key);
 		if (err == -EAGAIN && !p->nowait)
 			goto again;
 		if (err) {
@@ -2363,7 +2363,7 @@ int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
 			goto done;
 		}
 
-		err = read_block_for_search(root, p, &b, level, slot, key);
+		err = read_block_for_search(root, p, &b, slot, key);
 		if (err == -EAGAIN && !p->nowait)
 			goto again;
 		if (err) {
@@ -4969,8 +4969,7 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
 		}
 
 		next = c;
-		ret = read_block_for_search(root, path, &next, level,
-					    slot, &key);
+		ret = read_block_for_search(root, path, &next, slot, &key);
 		if (ret == -EAGAIN && !path->nowait)
 			goto again;
 
@@ -5013,8 +5012,7 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
 		if (!level)
 			break;
 
-		ret = read_block_for_search(root, path, &next, level,
-					    0, &key);
+		ret = read_block_for_search(root, path, &next, 0, &key);
 		if (ret == -EAGAIN && !path->nowait)
 			goto again;
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/4 btrfs: simplify arguments for btrfs_verify_level_key()

In reply to [email protected]

From: Filipe Manana <[email protected]>

The only caller of btrfs_verify_level_key() is read_block_for_search() and it's passing 3 arguments to it that can be extracted from its on stack variable of type struct btrfs_tree_parent_check.

So change btrfs_verify_level_key() to accept an argument of type struct btrfs_tree_parent_check instead of level, first key and parent transid arguments.

---
 fs/btrfs/ctree.c        |  3 +--
 fs/btrfs/tree-checker.c | 16 ++++++++--------
 fs/btrfs/tree-checker.h |  4 ++--
 3 files changed, 11 insertions(+), 12 deletions(-)
fs/btrfs/ctree.c +1 -2
index 428c5650559a..f68a9b586079 100644
@@ -1551,8 +1551,7 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
 			 * being cached, read from scrub, or have multiple
 			 * parents (shared tree blocks).
 			 */
-			if (btrfs_verify_level_key(tmp,
-					parent_level - 1, &check.first_key, gen)) {
+			if (btrfs_verify_level_key(tmp, &check)) {
 				ret = -EUCLEAN;
 				goto out;
 			}
fs/btrfs/tree-checker.c +8 -8
index 7b50263723bc..148d8cefa40e 100644
@@ -2183,8 +2183,8 @@ int btrfs_check_eb_owner(const struct extent_buffer *eb, u64 root_owner)
 	return 0;
 }
 
-int btrfs_verify_level_key(struct extent_buffer *eb, int level,
-			   struct btrfs_key *first_key, u64 parent_transid)
+int btrfs_verify_level_key(struct extent_buffer *eb,
+			   const struct btrfs_tree_parent_check *check)
 {
 	struct btrfs_fs_info *fs_info = eb->fs_info;
 	int found_level;
@@ -2192,16 +2192,16 @@ int btrfs_verify_level_key(struct extent_buffer *eb, int level,
 	int ret;
 
 	found_level = btrfs_header_level(eb);
-	if (found_level != level) {
+	if (found_level != check->level) {
 		WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
 		     KERN_ERR "BTRFS: tree level check failed\n");
 		btrfs_err(fs_info,
 "tree level mismatch detected, bytenr=%llu level expected=%u has=%u",
-			  eb->start, level, found_level);
+			  eb->start, check->level, found_level);
 		return -EIO;
 	}
 
-	if (!first_key)
+	if (!check->has_first_key)
 		return 0;
 
 	/*
@@ -2226,15 +2226,15 @@ int btrfs_verify_level_key(struct extent_buffer *eb, int level,
 		btrfs_node_key_to_cpu(eb, &found_key, 0);
 	else
 		btrfs_item_key_to_cpu(eb, &found_key, 0);
-	ret = btrfs_comp_cpu_keys(first_key, &found_key);
+	ret = btrfs_comp_cpu_keys(&check->first_key, &found_key);
 
 	if (ret) {
 		WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
 		     KERN_ERR "BTRFS: tree first key check failed\n");
 		btrfs_err(fs_info,
 "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)",
-			  eb->start, parent_transid, first_key->objectid,
-			  first_key->type, first_key->offset,
+			  eb->start, check->transid, check->first_key.objectid,
+			  check->first_key.type, check->first_key.offset,
 			  found_key.objectid, found_key.type,
 			  found_key.offset);
 	}
fs/btrfs/tree-checker.h +2 -2
index 01669cfa6578..db67f96cbe4b 100644
@@ -69,7 +69,7 @@ int btrfs_check_node(struct extent_buffer *node);
 int btrfs_check_chunk_valid(struct extent_buffer *leaf,
 			    struct btrfs_chunk *chunk, u64 logical);
 int btrfs_check_eb_owner(const struct extent_buffer *eb, u64 root_owner);
-int btrfs_verify_level_key(struct extent_buffer *eb, int level,
-			   struct btrfs_key *first_key, u64 parent_transid);
+int btrfs_verify_level_key(struct extent_buffer *eb,
+			   const struct btrfs_tree_parent_check *check);
 
 #endif
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/4 btrfs: remove redundant initialization from btrfs_readahead_tree_block()

In reply to [email protected]

From: Filipe Manana <[email protected]>

It's pointless to initialize the has_first_key field of the stack local btrfs_tree_parent_check structure since it all fields not explicitly initialized are zeroed out, plus it's a bit odd because the field is of type bool and we are assigning 0 instead of false to it (however it's not incorrect since 0 is converted to false). Just remove the explicit initialization due to its redundancy.

---
 fs/btrfs/extent_io.c | 1 -
 1 file changed, 1 deletion(-)
fs/btrfs/extent_io.c +0 -1
index 09c0d18a7b5a..0a0c84eb1c42 100644
@@ -4285,7 +4285,6 @@ void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info,
 				u64 bytenr, u64 owner_root, u64 gen, int level)
 {
 	struct btrfs_tree_parent_check check = {
-		.has_first_key = 0,
 		.level = level,
 		.transid = gen
 	};
Signature · 1 line
2.43.0
#6 lore raw

PATCH 4/4 btrfs: remove local generation variable from read_block_for_search()

In reply to [email protected]

From: Filipe Manana <[email protected]>

It's reduntant to have the 'gen' variable since we already have the same value in the local btrfs_tree_parent_check structure. So remove it and instead use the structure's field.

---
 fs/btrfs/ctree.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
fs/btrfs/ctree.c +2 -4
index f68a9b586079..148648ea1c8b 100644
@@ -1514,7 +1514,6 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
 	struct btrfs_fs_info *fs_info = root->fs_info;
 	struct btrfs_tree_parent_check check = { 0 };
 	u64 blocknr;
-	u64 gen;
 	struct extent_buffer *tmp = NULL;
 	int ret = 0;
 	int parent_level;
@@ -1524,12 +1523,11 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
 	bool path_released = false;
 
 	blocknr = btrfs_node_blockptr(*eb_ret, slot);
-	gen = btrfs_node_ptr_generation(*eb_ret, slot);
 	parent_level = btrfs_header_level(*eb_ret);
 	btrfs_node_key_to_cpu(*eb_ret, &check.first_key, slot);
 	check.has_first_key = true;
 	check.level = parent_level - 1;
-	check.transid = gen;
+	check.transid = btrfs_node_ptr_generation(*eb_ret, slot);
 	check.owner_root = btrfs_root_id(root);
 
 	/*
@@ -1545,7 +1543,7 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
 			reada_for_search(fs_info, p, parent_level, slot, key->objectid);
 
 		/* first we do an atomic uptodate check */
-		if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
+		if (btrfs_buffer_uptodate(tmp, check.transid, 1) > 0) {
 			/*
 			 * Do extra check for first_key, eb can be stale due to
 			 * being cached, read from scrub, or have multiple
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 3/4] btrfs: remove redundant initialization from btrfs_readahead_tree_block()

In reply to [email protected]
Quoted 8 lines · On Wed, Oct 16, 2024 at 03:20:22PM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

It's pointless to initialize the has_first_key field of the stack local btrfs_tree_parent_check structure since it all fields not explicitly initialized are zeroed out, plus it's a bit odd because the field is of type bool and we are assigning 0 instead of false to it (however it's not incorrect since 0 is converted to false). Just remove the explicit initialization due to its redundancy.

Makes sense, I've noticed there's one more to remove from btrfs_qgroup_trace_subtree() you can squash it to this patch too.

Reviewed-by on the v1 series
#7 lore raw
In reply to [email protected]
Quoted 10 lines · On Wed, Oct 16, 2024 at 03:20:19PM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

A set of cleanups around read_block_for_search() and btrfs_verify_level_key(), mostly simplify arguments and remove redundant arguments and variables.

Filipe Manana (4):
btrfs: remove redundant level argument from read_block_for_search()
btrfs: simplify arguments for btrfs_verify_level_key()
btrfs: remove redundant initialization from btrfs_readahead_tree_block()
btrfs: remove local generation variable from read_block_for_search()

#5 lore raw

Re: [PATCH 3/4] btrfs: remove redundant initialization from btrfs_readahead_tree_block()

In reply to David Sterba
Quoted 11 lines · David Sterba
Quoted 8 lines · On Wed, Oct 16, 2024 at 03:20:22PM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

It's pointless to initialize the has_first_key field of the stack local btrfs_tree_parent_check structure since it all fields not explicitly initialized are zeroed out, plus it's a bit odd because the field is of type bool and we are assigning 0 instead of false to it (however it's not incorrect since 0 is converted to false). Just remove the explicit initialization due to its redundancy.

Quoted 2 lines

Makes sense, I've noticed there's one more to remove from btrfs_qgroup_trace_subtree() you can squash it to this patch too.

Ah yes, done and added to for-next. Thanks.