]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
btrfs: remove partial support for lowest level from btrfs_search_forward()
authorSun YangKai <sunk67188@gmail.com>
Thu, 12 Jun 2025 08:32:23 +0000 (16:32 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 15 Aug 2025 14:38:23 +0000 (16:38 +0200)
[ Upstream commit 27260dd1904bb409cf84709928ba9bc5506fbe8e ]

Commit 323ac95bce44 ("Btrfs: don't read leaf blocks containing only
checksums during truncate") changed the condition from `level == 0` to
`level == path->lowest_level`, while its original purpose was just to do
some leaf node handling (calling btrfs_item_key_to_cpu()) and skip some
code that doesn't fit leaf nodes.

After changing the condition, the code path:

1. Also handles the non-leaf nodes when path->lowest_level is nonzero,
   which is wrong. However btrfs_search_forward() is never called with a
   nonzero path->lowest_level, which makes this bug not found before.

2. Makes the later if block with the same condition, which was originally
   used to handle non-leaf node (calling btrfs_node_key_to_cpu()) when
   lowest_level is not zero, dead code.

Since btrfs_search_forward() is never called for a path with a
lowest_level different from zero, just completely remove the partial
support for a non-zero lowest_level, simplifying a bit the code, and
assert that lowest_level is zero at the start of the function.

Suggested-by: Qu Wenruo <wqu@suse.com>
Fixes: 323ac95bce44 ("Btrfs: don't read leaf blocks containing only checksums during truncate")
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Sun YangKai <sunk67188@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
fs/btrfs/ctree.c

index a2e7979372ccd8fb56e53f438803ff36d6c11265..648531fe09002c0030357ae3943ae48e65bde695 100644 (file)
@@ -4585,16 +4585,13 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 
 /*
  * A helper function to walk down the tree starting at min_key, and looking
- * for nodes or leaves that are have a minimum transaction id.
+ * for leaves that have a minimum transaction id.
  * This is used by the btree defrag code, and tree logging
  *
  * This does not cow, but it does stuff the starting key it finds back
  * into min_key, so you can call btrfs_search_slot with cow=1 on the
  * key and get a writable path.
  *
- * This honors path->lowest_level to prevent descent past a given level
- * of the tree.
- *
  * min_trans indicates the oldest transaction that you are interested
  * in walking through.  Any nodes or leaves older than min_trans are
  * skipped over (without reading them).
@@ -4615,6 +4612,7 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
        int keep_locks = path->keep_locks;
 
        ASSERT(!path->nowait);
+       ASSERT(path->lowest_level == 0);
        path->keep_locks = 1;
 again:
        cur = btrfs_read_lock_root_node(root);
@@ -4636,8 +4634,8 @@ again:
                        goto out;
                }
 
-               /* at the lowest level, we're done, setup the path and exit */
-               if (level == path->lowest_level) {
+               /* At level 0 we're done, setup the path and exit. */
+               if (level == 0) {
                        if (slot >= nritems)
                                goto find_next_key;
                        ret = 0;
@@ -4678,12 +4676,6 @@ find_next_key:
                                goto out;
                        }
                }
-               if (level == path->lowest_level) {
-                       ret = 0;
-                       /* Save our key for returning back. */
-                       btrfs_node_key_to_cpu(cur, min_key, slot);
-                       goto out;
-               }
                cur = btrfs_read_node_slot(cur, slot);
                if (IS_ERR(cur)) {
                        ret = PTR_ERR(cur);
@@ -4699,7 +4691,7 @@ find_next_key:
 out:
        path->keep_locks = keep_locks;
        if (ret == 0)
-               btrfs_unlock_up_safe(path, path->lowest_level + 1);
+               btrfs_unlock_up_safe(path, 1);
        return ret;
 }