From: Sun YangKai <sunk67188@gmail.com>
To: linux-btrfs@vger.kernel.org
Cc: fdmanana@kernel.org, Sun YangKai <sunk67188@gmail.com>
Subject: [PATCH v2 3/4] btrfs: cleanup btrfs_search_slot_for_read()
Date: Thu, 11 Dec 2025 15:22:18 +0800 [thread overview]
Message-ID: <20251211072442.15920-5-sunk67188@gmail.com> (raw)
In-Reply-To: <20251211072442.15920-2-sunk67188@gmail.com>
- Now @return_any is not used by any caller, remove it and related logic.
- @for_read is used as boolean, so convert it from int to bool.
- This function is only meaningful when called with p->lowest_level == 0,
add assertion for that.
No functional change.
Signed-off-by: Sun YangKai <sunk67188@gmail.com>
---
fs/btrfs/ctree.c | 48 +++++++-------------------------------
fs/btrfs/ctree.h | 3 +--
fs/btrfs/free-space-tree.c | 2 +-
fs/btrfs/qgroup.c | 10 ++++----
fs/btrfs/send.c | 12 +++++-----
5 files changed, 21 insertions(+), 54 deletions(-)
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index a48b4befbee7..0a0157db0b0c 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -2450,22 +2450,15 @@ static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
* instead the next or previous item should be returned.
* When find_higher is true, the next higher item is returned, the next lower
* otherwise.
- * When return_any and find_higher are both true, and no higher item is found,
- * return the next lower instead.
- * When return_any is true and find_higher is false, and no lower item is found,
- * return the next higher instead.
- * It returns 0 if any item is found, 1 if none is found (tree empty), and
- * < 0 on error
+ * It returns 0 if any item is found, 1 if none is found and < 0 on error
*/
int btrfs_search_slot_for_read(struct btrfs_root *root,
const struct btrfs_key *key,
- struct btrfs_path *p, int find_higher,
- int return_any)
+ struct btrfs_path *p, bool find_higher)
{
int ret;
- struct extent_buffer *leaf;
-again:
+ ASSERT(p->lowest_level == 0);
ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
if (ret <= 0)
return ret;
@@ -2476,47 +2469,22 @@ int btrfs_search_slot_for_read(struct btrfs_root *root,
* to the first free slot in the previous leaf, i.e. at an invalid
* item.
*/
- leaf = p->nodes[0];
-
if (find_higher) {
- if (p->slots[0] >= btrfs_header_nritems(leaf)) {
- ret = btrfs_next_leaf(root, p);
- if (ret <= 0)
- return ret;
- if (!return_any)
- return 1;
- /*
- * no higher item found, return the next
- * lower instead
- */
- return_any = 0;
- find_higher = 0;
- btrfs_release_path(p);
- goto again;
- }
+ if (p->slots[0] >= btrfs_header_nritems(p->nodes[0]))
+ return btrfs_next_leaf(root, p);
} else {
if (p->slots[0] == 0) {
ret = btrfs_prev_leaf(root, p);
if (ret < 0)
return ret;
if (!ret) {
- leaf = p->nodes[0];
- if (p->slots[0] == btrfs_header_nritems(leaf))
+ if (p->slots[0] == btrfs_header_nritems(p->nodes[0]))
p->slots[0]--;
return 0;
}
- if (!return_any)
- return 1;
- /*
- * no lower item found, return the next
- * higher instead
- */
- return_any = 0;
- find_higher = 1;
- btrfs_release_path(p);
- goto again;
+ return 1;
} else {
- --p->slots[0];
+ p->slots[0]--;
}
}
return 0;
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 692370fc07b2..4b7b8ce7e211 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -595,8 +595,7 @@ int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
struct btrfs_path *p, u64 time_seq);
int btrfs_search_slot_for_read(struct btrfs_root *root,
const struct btrfs_key *key,
- struct btrfs_path *p, int find_higher,
- int return_any);
+ struct btrfs_path *p, bool find_higher);
void btrfs_release_path(struct btrfs_path *p);
struct btrfs_path *btrfs_alloc_path(void);
void btrfs_free_path(struct btrfs_path *p);
diff --git a/fs/btrfs/free-space-tree.c b/fs/btrfs/free-space-tree.c
index 1ad2ad384b9e..88c46950f5d2 100644
--- a/fs/btrfs/free-space-tree.c
+++ b/fs/btrfs/free-space-tree.c
@@ -1089,7 +1089,7 @@ static int populate_free_space_tree(struct btrfs_trans_handle *trans,
key.offset = 0;
extent_root = btrfs_extent_root(trans->fs_info, key.objectid);
- ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
+ ret = btrfs_search_slot_for_read(extent_root, &key, path, true);
if (ret < 0)
goto out_locked;
/*
diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
index d780980e6790..bd458ba537ba 100644
--- a/fs/btrfs/qgroup.c
+++ b/fs/btrfs/qgroup.c
@@ -415,7 +415,7 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
key.objectid = 0;
key.type = 0;
key.offset = 0;
- ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
+ ret = btrfs_search_slot_for_read(quota_root, &key, path, true);
if (ret)
goto out;
@@ -530,7 +530,7 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
key.objectid = 0;
key.type = BTRFS_QGROUP_RELATION_KEY;
key.offset = 0;
- ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
+ ret = btrfs_search_slot_for_read(quota_root, &key, path, true);
if (ret)
goto out;
while (1) {
@@ -1088,7 +1088,7 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
key.offset = 0;
btrfs_release_path(path);
- ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
+ ret = btrfs_search_slot_for_read(tree_root, &key, path, true);
if (ret > 0)
goto out_add_root;
if (unlikely(ret < 0)) {
@@ -1130,7 +1130,7 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
goto out_free_path;
}
ret = btrfs_search_slot_for_read(tree_root, &found_key,
- path, 1, 0);
+ path, true);
if (unlikely(ret < 0)) {
btrfs_abort_transaction(trans, ret);
goto out_free_path;
@@ -3692,7 +3692,7 @@ static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans,
fs_info->qgroup_rescan_progress.objectid);
ret = btrfs_search_slot_for_read(extent_root,
&fs_info->qgroup_rescan_progress,
- path, 1, 0);
+ path, true);
btrfs_debug(fs_info,
"current progress key " BTRFS_KEY_FMT ", search_slot ret %d",
diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
index eae596b80ec0..471e81a8e844 100644
--- a/fs/btrfs/send.c
+++ b/fs/btrfs/send.c
@@ -1234,7 +1234,7 @@ static int get_inode_path(struct btrfs_root *root,
key.type = BTRFS_INODE_REF_KEY;
key.offset = 0;
- ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
+ ret = btrfs_search_slot_for_read(root, &key, p, true);
if (ret < 0)
return ret;
if (ret)
@@ -1979,7 +1979,7 @@ static int get_first_ref(struct btrfs_root *root, u64 ino,
key.type = BTRFS_INODE_REF_KEY;
key.offset = 0;
- ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
+ ret = btrfs_search_slot_for_read(root, &key, path, true);
if (ret < 0)
return ret;
if (!ret)
@@ -2475,7 +2475,7 @@ static int send_subvol_begin(struct send_ctx *sctx)
key.offset = 0;
ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
- &key, path, 1, 0);
+ &key, path, true);
if (ret < 0)
return ret;
if (ret)
@@ -6195,7 +6195,7 @@ static int is_extent_unchanged(struct send_ctx *sctx,
key.objectid = ekey->objectid;
key.type = BTRFS_EXTENT_DATA_KEY;
key.offset = ekey->offset;
- ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
+ ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, false);
if (ret < 0)
return ret;
if (ret)
@@ -6320,7 +6320,7 @@ static int get_last_extent(struct send_ctx *sctx, u64 offset)
key.objectid = sctx->cur_ino;
key.type = BTRFS_EXTENT_DATA_KEY;
key.offset = offset;
- ret = btrfs_search_slot_for_read(root, &key, path, 0, 0);
+ ret = btrfs_search_slot_for_read(root, &key, path, false);
if (ret < 0)
return ret;
ASSERT(ret == 0);
@@ -7288,7 +7288,7 @@ static int full_send_tree(struct send_ctx *sctx)
sctx->last_reloc_trans = fs_info->last_reloc_trans;
up_read(&fs_info->commit_root_sem);
- ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
+ ret = btrfs_search_slot_for_read(send_root, &key, path, true);
if (ret < 0)
return ret;
if (ret)
--
2.51.2
next prev parent reply other threads:[~2025-12-11 7:25 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-11 7:22 [PATCH v2 0/4] btrfs: some cleanups for two ctree functions Sun YangKai
2025-12-11 7:22 ` [PATCH v2 1/4] btrfs: don't set @return_any for btrfs_search_slot_for_read in btrfs_read_qgroup_config Sun YangKai
2025-12-11 7:22 ` [PATCH v2 2/4] btrfs: don't set return_any @return_any for btrfs_search_slot_for_read in get_last_extent() Sun YangKai
2025-12-11 7:22 ` Sun YangKai [this message]
2025-12-11 7:22 ` [PATCH v2 4/4] btrfs: ctree: cleanup btrfs_prev_leaf() Sun YangKai
2026-02-07 23:09 ` Qu Wenruo
2026-02-08 2:42 ` Sun YangKai
2026-02-08 3:13 ` Qu Wenruo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251211072442.15920-5-sunk67188@gmail.com \
--to=sunk67188@gmail.com \
--cc=fdmanana@kernel.org \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox