[PATCH v2 1/2] btrfs: add tree parent check to btrfs_buffer_uptodate()

From: ZhengYuan Huang

Date: Fri Mar 13 2026 - 05:19:50 EST


btrfs_buffer_uptodate() only checks whether an extent buffer is uptodate
and whether its generation matches the expected parent transid.

For callers that also need tree-parent verification, this is not enough on
a cache hit, because a cached extent buffer can be reported uptodate
without being checked against the expected level/first_key constraints.

Extend btrfs_buffer_uptodate() to take an optional
btrfs_tree_parent_check so cached buffers can be verified before being
reported uptodate.

For now all callers pass NULL, so there is no functional change. A
follow-up patch will use the new argument on the relevant cached-hit path.

Signed-off-by: ZhengYuan Huang <gality369@xxxxxxxxx>
---
fs/btrfs/ctree.c | 2 +-
fs/btrfs/disk-io.c | 14 +++++++++++---
fs/btrfs/disk-io.h | 3 ++-
fs/btrfs/extent-tree.c | 2 +-
fs/btrfs/extent_io.c | 2 +-
fs/btrfs/tree-log.c | 2 +-
6 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 561658aca018..c008b847200a 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -1493,7 +1493,7 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
reada_for_search(fs_info, p, parent_level, slot, key->objectid);

/* first we do an atomic uptodate check */
- if (btrfs_buffer_uptodate(tmp, check.transid, true) > 0) {
+ if (btrfs_buffer_uptodate(tmp, check.transid, true, NULL) > 0) {
/*
* Do extra check for first_key, eb can be stale due to
* being cached, read from scrub, or have multiple
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 900e462d8ea1..8773f1f7ea46 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -116,13 +116,21 @@ static void csum_tree_block(struct extent_buffer *buf, u8 *result)
* detect blocks that either didn't get written at all or got written
* in the wrong place.
*/
-int btrfs_buffer_uptodate(struct extent_buffer *eb, u64 parent_transid, bool atomic)
+int btrfs_buffer_uptodate(struct extent_buffer *eb, u64 parent_transid, bool atomic,
+ const struct btrfs_tree_parent_check *check)
{
if (!extent_buffer_uptodate(eb))
return 0;

- if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
+ if (!parent_transid || btrfs_header_generation(eb) == parent_transid) {
+ /*
+ * On a cache hit, the caller may still need tree parent
+ * verification before reusing the buffer.
+ */
+ if (check && btrfs_verify_level_key(eb, check))
+ return -EUCLEAN;
return 1;
+ }

if (atomic)
return -EAGAIN;
@@ -1046,7 +1054,7 @@ static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root,
root->node = NULL;
goto fail;
}
- if (unlikely(!btrfs_buffer_uptodate(root->node, generation, false))) {
+ if (unlikely(!btrfs_buffer_uptodate(root->node, generation, false, NULL))) {
ret = -EIO;
goto fail;
}
diff --git a/fs/btrfs/disk-io.h b/fs/btrfs/disk-io.h
index 57920f2c6fe4..aef106484dbe 100644
--- a/fs/btrfs/disk-io.h
+++ b/fs/btrfs/disk-io.h
@@ -106,7 +106,8 @@ static inline struct btrfs_root *btrfs_grab_root(struct btrfs_root *root)
void btrfs_put_root(struct btrfs_root *root);
void btrfs_mark_buffer_dirty(struct btrfs_trans_handle *trans,
struct extent_buffer *buf);
-int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, bool atomic);
+int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, bool atomic,
+ const struct btrfs_tree_parent_check *check);
int btrfs_read_extent_buffer(struct extent_buffer *buf,
const struct btrfs_tree_parent_check *check);

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index dc4ca98c3780..54daa8672272 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -5584,7 +5584,7 @@ static int check_next_block_uptodate(struct btrfs_trans_handle *trans,

generation = btrfs_node_ptr_generation(path->nodes[level], path->slots[level]);

- if (btrfs_buffer_uptodate(next, generation, false))
+ if (btrfs_buffer_uptodate(next, generation, false, NULL))
return 0;

check.level = level - 1;
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 14da72a9a950..93eed1d3716c 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -4574,7 +4574,7 @@ void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info,
if (IS_ERR(eb))
return;

- if (btrfs_buffer_uptodate(eb, gen, true)) {
+ if (btrfs_buffer_uptodate(eb, gen, true, NULL)) {
free_extent_buffer(eb);
return;
}
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 30f3c3b849c1..ff1a83d26598 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -456,7 +456,7 @@ static int process_one_buffer(struct extent_buffer *eb,
return ret;
}

- if (btrfs_buffer_uptodate(eb, gen, false) && level == 0) {
+ if (btrfs_buffer_uptodate(eb, gen, false, NULL) && level == 0) {
ret = btrfs_exclude_logged_extents(eb);
if (ret)
btrfs_abort_transaction(trans, ret);
--
2.43.0