Skip to content

Commit bae7d4c

Browse files
SaltyKitkatkdave
authored andcommitted
btrfs: simplify leaf traversal after path release in btrfs_next_old_leaf()
After releasing the path in btrfs_next_old_leaf(), we need to re-check the leaf because a balance operation may have added items or removed the last item. The original code handled this with two separate conditional blocks, the second marked with a lengthy comment explaining a "missed case". Merge these two blocks into a single logical structure that handles both scenarios more clearly. Also update the comment to be more concise and accurate, incorporating the explanation directly into the main block rather than a separate annotation. No functional change. Signed-off-by: Sun YangKai <sunk67188@gmail.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 6d2ba15 commit bae7d4c

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

fs/btrfs/ctree.c

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4854,34 +4854,22 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
48544854

48554855
nritems = btrfs_header_nritems(path->nodes[0]);
48564856
/*
4857-
* by releasing the path above we dropped all our locks. A balance
4858-
* could have added more items next to the key that used to be
4859-
* at the very end of the block. So, check again here and
4860-
* advance the path if there are now more items available.
4861-
*/
4862-
if (nritems > 0 && path->slots[0] < nritems - 1) {
4863-
if (ret == 0)
4864-
path->slots[0]++;
4865-
ret = 0;
4866-
goto done;
4867-
}
4868-
/*
4869-
* So the above check misses one case:
4870-
* - after releasing the path above, someone has removed the item that
4871-
* used to be at the very end of the block, and balance between leafs
4872-
* gets another one with bigger key.offset to replace it.
4857+
* By releasing the path above we dropped all our locks. A balance
4858+
* could have happened and
48734859
*
4874-
* This one should be returned as well, or we can get leaf corruption
4875-
* later(esp. in __btrfs_drop_extents()).
4860+
* 1. added more items after the previous last item
4861+
* 2. deleted the previous last item
48764862
*
4877-
* And a bit more explanation about this check,
4878-
* with ret > 0, the key isn't found, the path points to the slot
4879-
* where it should be inserted, so the path->slots[0] item must be the
4880-
* bigger one.
4863+
* So, check again here and advance the path if there are now more items available.
48814864
*/
4882-
if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
4883-
ret = 0;
4884-
goto done;
4865+
if (nritems > 0 && path->slots[0] <= nritems - 1) {
4866+
if (ret == 0 && path->slots[0] != nritems - 1) {
4867+
path->slots[0]++;
4868+
goto done;
4869+
} else if (ret > 0) {
4870+
ret = 0;
4871+
goto done;
4872+
}
48854873
}
48864874

48874875
while (level < BTRFS_MAX_LEVEL) {

0 commit comments

Comments
 (0)