Skip to content

Commit fe9d99f

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. Signed-off-by: Sun YangKai <sunk67188@gmail.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 494bf38 commit fe9d99f

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

fs/btrfs/ctree.c

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4853,34 +4853,23 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
48534853

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

48864875
while (level < BTRFS_MAX_LEVEL) {

0 commit comments

Comments
 (0)