Skip to contentSkip to conversation

btrfs: replace manual next item search with btrfs_get_next_valid_item()

PATCH In review Sun YangKai 2 participants 3 messages Jul 30, 2025 – Jul 31, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: replace manual next item search with btrfs_get_next_valid_item()

The function btrfs_get_next_valid_item() was introduced in commit 62142be363ae9("btrfs: introduce btrfs_for_each_slot iterator macro") and has never been used in other places other than
the btrfs_for_each_slot macro.

However, in multiple locations, we implement the same pattern for advancing to the next valid item in a btrfs path:
1. Increment path->slots[0]
2. Check if beyond last item in leaf
3. If so, call btrfs_next_leaf()
4. Check return value and handle errors
5. Update the key

This pattern can be replaced by the helper function btrfs_get_next_valid_item() which encapsulates step 2-5. The change simplifies the code by:
* Reducing code duplication
* Making control flow easier to follow
* Centralizing error handling logic
* Improving maintainability

The functionality remains identical as the helper implements the exact same logic sequence.

There are still some places that have a similar pattern but with more complex logic. These parts are left untouched. One example: in file.c: btrfs_drop_extents(), the pattern after the label next_slot is left untouched.

---
 fs/btrfs/disk-io.c    |  15 +++---
 fs/btrfs/file-item.c  |  28 ++++-------
 fs/btrfs/file.c       |  18 +++-----
 fs/btrfs/inode.c      |  31 +++++--------
 fs/btrfs/ioctl.c      |  49 ++++++++++----------
 fs/btrfs/props.c      |  15 ++----
 fs/btrfs/reflink.c    |  16 ++-----
 fs/btrfs/relocation.c |  12 ++---
 fs/btrfs/send.c       |  40 ++++++++--------
 fs/btrfs/tree-log.c   | 105 +++++++++++++++---------------------------
 fs/btrfs/verity.c     |  21 +++------
 fs/btrfs/volumes.c    |  15 ++----
 fs/btrfs/zoned.c      |  15 +++---
 13 files changed, 145 insertions(+), 235 deletions(-)
fs/btrfs/disk-io.c +6 -9
index 70fc4e7cc5a0..69f5af9d5e92 100644
@@ -2130,17 +2130,14 @@ static int load_global_roots_objectid(struct btrfs_root *tree_root,
 		if (ret < 0)
 			break;
 
-		if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
-			ret = btrfs_next_leaf(tree_root, path);
-			if (ret) {
-				if (ret > 0)
-					ret = 0;
-				break;
-			}
+		ret = btrfs_get_next_valid_item(root, &key, path);
+		if (ret > 0) {
+			ret = 0;
+			break;
 		}
-		ret = 0;
+		if (ret < 0)
+			break;
 
-		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
 		if (key.objectid != objectid)
 			break;
 		btrfs_release_path(path);
fs/btrfs/file-item.c +10 -18
index c09fbc257634..4612eecade52 100644
@@ -517,17 +517,13 @@ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end,
 	while (start <= end) {
 		u64 csum_end;
 
+		ret = btrfs_get_next_valid_item(root, &key, path);
+		if (ret < 0)
+			goto out;
+		if (ret > 0)
+			break;
 		leaf = path->nodes[0];
-		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
-			ret = btrfs_next_leaf(root, path);
-			if (ret < 0)
-				goto out;
-			if (ret > 0)
-				break;
-			leaf = path->nodes[0];
-		}
 
-		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
 		    key.type != BTRFS_EXTENT_CSUM_KEY ||
 		    key.offset > end)
@@ -674,17 +670,13 @@ int btrfs_lookup_csums_bitmap(struct btrfs_root *root, struct btrfs_path *path,
 	while (start <= end) {
 		u64 csum_end;
 
+		ret = btrfs_get_next_valid_item(root, &key, path);
+		if (ret < 0)
+			goto fail;
+		if (ret > 0)
+			break;
 		leaf = path->nodes[0];
-		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
-			ret = btrfs_next_leaf(root, path);
-			if (ret < 0)
-				goto fail;
-			if (ret > 0)
-				break;
-			leaf = path->nodes[0];
-		}
 
-		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
 		    key.type != BTRFS_EXTENT_CSUM_KEY ||
 		    key.offset > end)
fs/btrfs/file.c +7 -11
index 204674934795..24eb3285c01f 100644
@@ -3623,22 +3623,18 @@ static loff_t find_desired_extent(struct file *file, loff_t offset, int whence)
 	}
 
 	while (start < i_size) {
-		struct extent_buffer *leaf = path->nodes[0];
+		struct extent_buffer *leaf;
 		struct btrfs_file_extent_item *extent;
 		u64 extent_end;
 		u8 type;
 
-		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
-			ret = btrfs_next_leaf(root, path);
-			if (ret < 0)
-				goto out;
-			else if (ret > 0)
-				break;
-
-			leaf = path->nodes[0];
-		}
+		ret = btrfs_get_next_valid_item(root, &key, path);
+		if (ret < 0)
+			goto out;
+		if (ret > 0)
+			break;
+		leaf = path->nodes[0];
 
-		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
 		if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
 			break;
 
fs/btrfs/inode.c +12 -19
index b77dd22b8cdb..92531693a627 100644
@@ -2089,17 +2089,12 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
 		check_prev = false;
 next_slot:
 		/* Go to next leaf if we have exhausted the current one */
+		ret = btrfs_get_next_valid_item(root, &found_key, path);
+		if (ret < 0)
+			goto error;
+		if (ret > 0)
+			break;
 		leaf = path->nodes[0];
-		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
-			ret = btrfs_next_leaf(root, path);
-			if (ret < 0)
-				goto error;
-			if (ret > 0)
-				break;
-			leaf = path->nodes[0];
-		}
-
-		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
 
 		/* Didn't find anything for our INO */
 		if (found_key.objectid > ino)
@@ -7074,16 +7069,14 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 next:
 	if (start >= extent_end) {
 		path->slots[0]++;
-		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
-			ret = btrfs_next_leaf(root, path);
-			if (ret < 0)
-				goto out;
-			else if (ret > 0)
-				goto not_found;
+		ret = btrfs_get_next_valid_item(root, &found_key, path);
+		if (ret < 0)
+			goto out;
+		if (ret > 0)
+			goto not_found;
+
+		leaf = path->nodes[0];
 
-			leaf = path->nodes[0];
-		}
-		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
 		if (found_key.objectid != objectid ||
 		    found_key.type != BTRFS_EXTENT_DATA_KEY)
 			goto not_found;
fs/btrfs/ioctl.c +24 -25

75 lines not on this page · raw message

fs/btrfs/props.c +5 -10
index adc956432d2f..79b4ab5f51f7 100644
@@ -166,19 +166,14 @@ static int iterate_object_props(struct btrfs_root *root,
 		int slot;
 		const struct hlist_head *handlers;
 
+		ret = btrfs_get_next_valid_item(root, &key, path);
+		if (ret < 0)
+			goto out;
+		else if (ret > 0)
+			break;
 		slot = path->slots[0];
 		leaf = path->nodes[0];
 
-		if (slot >= btrfs_header_nritems(leaf)) {
-			ret = btrfs_next_leaf(root, path);
-			if (ret < 0)
-				goto out;
-			else if (ret > 0)
-				break;
-			continue;
-		}
-
-		btrfs_item_key_to_cpu(leaf, &key, slot);
 		if (key.objectid != objectid)
 			break;
 		if (key.type != BTRFS_XATTR_ITEM_KEY)
fs/btrfs/reflink.c +5 -11
index ce25ab7f0e99..c70940c15d6b 100644
@@ -345,7 +345,6 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
 	struct btrfs_trans_handle *trans;
 	char *buf = NULL;
 	struct btrfs_key key;
-	u32 nritems;
 	int slot;
 	int ret;
 	const u64 len = olen_aligned;
@@ -397,20 +396,15 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
 				path->slots[0]--;
 		}
 
-		nritems = btrfs_header_nritems(path->nodes[0]);
 process_slot:
-		if (path->slots[0] >= nritems) {
-			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
-			if (ret < 0)
-				goto out;
-			if (ret > 0)
-				break;
-			nritems = btrfs_header_nritems(path->nodes[0]);
-		}
+		ret = btrfs_get_next_valid_item(BTRFS_I(src)->root, &key, path);
+		if (ret < 0)
+			goto out;
+		if (ret > 0)
+			break;
 		leaf = path->nodes[0];
 		slot = path->slots[0];
 
-		btrfs_item_key_to_cpu(leaf, &key, slot);
 		if (key.type > BTRFS_EXTENT_DATA_KEY ||
 		    key.objectid != btrfs_ino(BTRFS_I(src)))
 			break;
fs/btrfs/relocation.c +3 -9
index e58151933844..85af65a794c6 100644
@@ -3342,7 +3342,6 @@ int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
 {
 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
 	struct btrfs_key key;
-	struct extent_buffer *leaf;
 	u64 start, end, last;
 	int ret;
 
@@ -3367,15 +3366,10 @@ int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
 		if (ret < 0)
 			break;
 next:
-		leaf = path->nodes[0];
-		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
-			ret = btrfs_next_leaf(rc->extent_root, path);
-			if (ret != 0)
-				break;
-			leaf = path->nodes[0];
-		}
+		ret = btrfs_get_next_valid_item(rc->extent_root, &key, path);
+		if (ret != 0)
+			break;
 
-		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
 		if (key.objectid >= last) {
 			ret = 1;
 			break;
fs/btrfs/send.c +18 -22
index 7664025a5af4..3c17f3b0794c 100644
@@ -5887,8 +5887,8 @@ static int clone_range(struct send_ctx *sctx, struct btrfs_path *dst_path,
 	}
 
 	while (true) {
-		struct extent_buffer *leaf = path->nodes[0];
-		int slot = path->slots[0];
+		struct extent_buffer *leaf;
+		int slot;
 		struct btrfs_file_extent_item *ei;
 		u8 type;
 		u64 ext_len;
@@ -5896,16 +5896,14 @@ static int clone_range(struct send_ctx *sctx, struct btrfs_path *dst_path,
 		u64 clone_data_offset;
 		bool crossed_src_i_size = false;
 
-		if (slot >= btrfs_header_nritems(leaf)) {
-			ret = btrfs_next_leaf(clone_root->root, path);
-			if (ret < 0)
-				goto out;
-			else if (ret > 0)
-				break;
-			continue;
-		}
+		ret = btrfs_get_next_valid_item(clone_root->root, &key, path);
+		if (ret < 0)
+			goto out;
+		if (ret > 0)
+			break;
 
-		btrfs_item_key_to_cpu(leaf, &key, slot);
+		leaf = path->nodes[0];
+		slot = path->slots[0];
 
 		/*
 		 * We might have an implicit trailing hole (NO_HOLES feature
@@ -6400,21 +6398,19 @@ static int range_is_hole_in_parent(struct send_ctx *sctx,
 		path->slots[0]--;
 
 	while (search_start < end) {
-		struct extent_buffer *leaf = path->nodes[0];
-		int slot = path->slots[0];
+		struct extent_buffer *leaf;
+		int slot;
 		struct btrfs_file_extent_item *fi;
 		u64 extent_end;
 
-		if (slot >= btrfs_header_nritems(leaf)) {
-			ret = btrfs_next_leaf(root, path);
-			if (ret < 0)
-				goto out;
-			else if (ret > 0)
-				break;
-			continue;
-		}
+		ret = btrfs_get_next_valid_item(root, &key, path);
+		if (ret < 0)
+			goto out;
+		if (ret > 0)
+			break;
 
-		btrfs_item_key_to_cpu(leaf, &key, slot);
+		leaf = path->nodes[0];
+		slot = path->slots[0];
 		if (key.objectid < sctx->cur_ino ||
 		    key.type < BTRFS_EXTENT_DATA_KEY)
 			goto next;
fs/btrfs/tree-log.c +36 -69

176 lines not on this page · raw message

fs/btrfs/verity.c +7 -14
index b7a96a005487..f517d1f7a1da 100644
@@ -332,8 +332,14 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
 	}
 
 	while (len > 0) {
+		ret = btrfs_get_next_valid_item(root, &key, path);
+		if (ret < 0)
+			break;
+		if (ret > 0) {
+			ret = 0;
+			break;
+		}
 		leaf = path->nodes[0];
-		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
 
 		if (key.objectid != btrfs_ino(inode) || key.type != key_type)
 			break;
@@ -389,19 +395,6 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
 		copied += copy_bytes;
 
 		path->slots[0]++;
-		if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
-			/*
-			 * We've reached the last slot in this leaf and we need
-			 * to go to the next leaf.
-			 */
-			ret = btrfs_next_leaf(root, path);
-			if (ret < 0) {
-				break;
-			} else if (ret > 0) {
-				ret = 0;
-				break;
-			}
-		}
 	}
 out:
 	btrfs_free_path(path);
fs/btrfs/volumes.c +5 -10
index fa7a929a0461..70e73cbf6b16 100644
@@ -1723,18 +1723,13 @@ static int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
 		goto out;
 
 	while (search_start < search_end) {
+		ret = btrfs_get_next_valid_item(root, &key, path);
+		if (ret < 0)
+			goto out;
+		if (ret > 0)
+			break;
 		l = path->nodes[0];
 		slot = path->slots[0];
-		if (slot >= btrfs_header_nritems(l)) {
-			ret = btrfs_next_leaf(root, path);
-			if (ret == 0)
-				continue;
-			if (ret < 0)
-				goto out;
-
-			break;
-		}
-		btrfs_item_key_to_cpu(l, &key, slot);
 
 		if (key.objectid < device->devid)
 			goto next;
fs/btrfs/zoned.c +7 -8
index 245e813ecd78..8fe5b7e362c0 100644
@@ -306,14 +306,13 @@ static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
 	if (ret < 0)
 		return ret;
 
-	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
-		ret = btrfs_next_leaf(root, path);
-		if (ret < 0)
-			return ret;
-		/* No dev extents at all? Not good */
-		if (ret > 0)
-			return -EUCLEAN;
-	}
+	ret = btrfs_get_next_valid_item(root, &key, path);
+	if (ret < 0)
+		return ret;
+	/* No dev extents at all? Not good */
+	if (ret > 0)
+		return -EUCLEAN;
+
 
 	leaf = path->nodes[0];
 	dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
Signature · 1 line
2.50.1
#1 lore raw
Quoted 4 lines · Sun YangKai

The function btrfs_get_next_valid_item() was introduced in commit 62142be363ae9("btrfs: introduce btrfs_for_each_slot iterator macro") and has never been used in other places other than
the btrfs_for_each_slot macro.

That's because the function name is not that straight-forward.

The name btrfs_get_"next"_valid_item() implies it would move to the next slot, but it's not the case.

Thus this can lead to quite some confusion and we're not that excited to use it.

One example inlined below:

[...]

fs/btrfs/volumes.c static int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, · line 1730

Quoted 13 lines
fs/btrfs/volumes.c +5 -0
index fa7a929a0461..70e73cbf6b16 100644
@@ -1723,18 +1723,13 @@ static int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
  		goto out;
 
  	while (search_start < search_end) {
+		ret = btrfs_get_next_valid_item(root, &key, path);
+		if (ret < 0)
+			goto out;
+		if (ret > 0)
+			break;

Here just by the name, it looks like it will move to the next slot thus can be problematic (as it will skip the current slot).

But it's not the case, as if the current slot is not beyond the current leaf, we just return with the current item key stored into @key.

Another thing is, we may not need to bother the @key, or the caller will manually check the item key anyway. Compared to the confusing name, this is just a minor problem though.

But still it modifies the @key, which may or may not be what we want.

That's why we are not excited to use the new helper.

Thanks,
Qu

Quoted 41 lines
fs/btrfs/volumes.c +0 -10
  		l = path->nodes[0];
  		slot = path->slots[0];
-		if (slot >= btrfs_header_nritems(l)) {
-			ret = btrfs_next_leaf(root, path);
-			if (ret == 0)
-				continue;
-			if (ret < 0)
-				goto out;
-
-			break;
-		}
-		btrfs_item_key_to_cpu(l, &key, slot);
 
  		if (key.objectid < device->devid)
  			goto next;
fs/btrfs/zoned.c +7 -8
index 245e813ecd78..8fe5b7e362c0 100644
@@ -306,14 +306,13 @@ static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
  	if (ret < 0)
  		return ret;
 
-	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
-		ret = btrfs_next_leaf(root, path);
-		if (ret < 0)
-			return ret;
-		/* No dev extents at all? Not good */
-		if (ret > 0)
-			return -EUCLEAN;
-	}
+	ret = btrfs_get_next_valid_item(root, &key, path);
+	if (ret < 0)
+		return ret;
+	/* No dev extents at all? Not good */
+	if (ret > 0)
+		return -EUCLEAN;
+
 
  	leaf = path->nodes[0];
  	dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
#2 lore raw
Quoted 7 lines · Sun YangKai
Quoted 5 lines

The function btrfs_get_next_valid_item() was introduced in commit 62142be363ae9("btrfs: introduce btrfs_for_each_slot iterator macro")
and has never been used in other places other than
the btrfs_for_each_slot macro.

Quoted 7 lines

That's because the function name is not that straight-forward.

The name btrfs_get_"next"_valid_item() implies it would move to the next slot, but it's not the case.

Thus this can lead to quite some confusion and we're not that excited to use it.

Great point, and I also find the name is kind of confusing. Maybe we can have a better name like btrfs_ensure_valid_item() or something similar.

Quoted 27 lines

One example inlined below:

[...]

Quoted 10 lines
fs/btrfs/volumes.c +0 -0
index fa7a929a0461..70e73cbf6b16 100644
@@ -1723,18 +1723,13 @@ static int find_free_dev_extent(struct

btrfs_device *device, u64 num_bytes,>
goto out;

  	while (search_start < search_end) {

fs/btrfs/volumes.c

Quoted 5 lines
fs/btrfs/volumes.c +5 -0
+		ret = btrfs_get_next_valid_item(root, &key, path);
+		if (ret < 0)
+			goto out;
+		if (ret > 0)
+			break;

Here just by the name, it looks like it will move to the next slot thus can be problematic (as it will skip the current slot).

But it's not the case, as if the current slot is not beyond the current leaf, we just return with the current item key stored into @key.

Quoted 5 lines

Another thing is, we may not need to bother the @key, or the caller will manually check the item key anyway. Compared to the confusing name, this is just a minor problem though.

But still it modifies the @key, which may or may not be what we want.

If I recall correctly, there's only one case that we don't want to bother with the @key in this patch, and I'll discard that change in the future versions if necessary. In all other cases, we just read the key according to the path, which is part of the pattern I want to replace with this helper.

Quoted 19 lines

That's why we are not excited to use the new helper.

Thanks,
Qu

Quoted 14 lines
  		l = path->nodes[0];
  		slot = path->slots[0];
fs/btrfs/volumes.c +0 -10
-		if (slot >= btrfs_header_nritems(l)) {
-			ret = btrfs_next_leaf(root, path);
-			if (ret == 0)
-				continue;
-			if (ret < 0)
-				goto out;
-
-			break;
-		}
-		btrfs_item_key_to_cpu(l, &key, slot);
Quoted 4 lines
Quoted 4 lines

if (key.objectid < device->devid)

goto next;

Thanks,
YangKai