[PATCH 4/6] btrfs: scan the running transaction for data extent references

From: Ismael Luceno

Date: Sun Nov 29 2026 - 08:40:36 EST


The reference scan of a data extent walks the trees without a transaction
handle, so the backref walk reads the commit root. A reference made by
the running transaction, such as a reflink made during balance, is not
in the commit root, so the scan misses it, while the extent item already
counts it.

Run the scan under a joined transaction, so the walk sees the running
transaction and its pending delayed refs. The transaction is not flushed:
the walk accounts for pending refs itself.

Signed-off-by: Ismael Luceno <ismael@xxxxxxxxxxx>
---
fs/btrfs/relocation.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index a9da7a6468fa5..75300c21fe06e 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -3255,19 +3255,31 @@ static int collect_extent_segments(struct reloc_control *rc,
{
struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
struct btrfs_backref_walk_ctx ctx = { 0 };
+ struct btrfs_trans_handle *trans;
struct ulist_iterator leaf_uiter;
struct ulist_node *ref_node;
const u64 bytenr = extent_key->objectid;
const u64 num_bytes = extent_key->offset;
int ret;

+ /*
+ * Walk the running transaction, delayed refs included, so the scan sees
+ * the references made by it. They are not flushed.
+ */
+ trans = btrfs_join_transaction(rc->extent_root);
+ if (IS_ERR(trans))
+ return PTR_ERR(trans);
+
ctx.bytenr = bytenr;
ctx.skip_inode_ref_list = true;
ctx.fs_info = fs_info;
+ ctx.trans = trans;

ret = btrfs_find_all_leafs(&ctx);
- if (ret < 0)
+ if (ret < 0) {
+ btrfs_end_transaction(trans);
return ret;
+ }

ULIST_ITER_INIT(&leaf_uiter);
while ((ref_node = ulist_next(ctx.refs, &leaf_uiter))) {
@@ -3315,6 +3327,7 @@ static int collect_extent_segments(struct reloc_control *rc,
break;
}
ulist_free(ctx.refs);
+ btrfs_end_transaction(trans);
if (ret)
return ret;

--
2.56.0