From e74d5a2d0a4cbe60b92734b54c2a392e79ce2b4d Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Tue, 10 Nov 2026 22:12:36 -0500 Subject: [PATCH] btrfs: scrub: do not let a transaction commit pass under queued stripes Scrub and dev-replace find extents in the commit root of the extent tree, through paths kept in the scrub context for the whole block group (sctx->extent_path, sctx->csum_path), and fill up to SCRUB_TOTAL_STRIPES stripes before the last of them is read. A transaction commit waits in btrfs_scrub_pause() until the scrub reaches the top of the scrub_simple_mirror() loop, and then switches the commit roots and unpins the extents the transaction freed before btrfs_scrub_continue(). With discard=sync btrfs_finish_extent_commit() also discards them. Nothing on the scrub side notices. The stripes it queued before the pause are read afterwards, and the search continues in a leaf of the old commit root, so the scrub reads extents that are no longer allocated. Without discard that is harmless: the block group is read-only while it is scrubbed, so the freed range is not reused and still holds what the scrub expects. With discard=sync the range reads back zeros on every mirror: BTRFS warning (device loop0): scrub: tree block 65617920 mirror 1 has bad bytenr, has 0 want 65617920 BTRFS error (device loop0): scrub: unable to fixup (regular) error at logical 65617920 on dev /dev/loop0 physical 65617920 BTRFS error (device loop0): scrub: stripe 65601536 has unrepaired metadata sector at logical 65617920 BTRFS error (device loop0): btrfs_scrub_dev(/dev/loop0, 1, /dev/mapper/rddelay) failed -5 A scrub reports uncorrectable errors on a healthy filesystem, and a dev-replace cancels on the first such metadata sector. The freed blocks are not referenced by anything, so no reader of the filesystem sees them; btrfs check and a second scrub after the churn are clean. Before a pause, finish the queued stripes and release both paths, so that a commit can only pass with nothing of the scrub's still referring to the old commit root, and the next search starts from the new one. The parity scrub of RAID56 has its own paths, but its pause point is reached with data stripes queued too, so it uses the same helper. The stale path came with 1dc4888e725d ("btrfs: scrub: avoid unnecessary extent tree search preparing stripes"); the queued stripes are older. scrub-stale-extent-test.sh (raid56-wh-harness) fills a raid1 filesystem (nodesize 64K) with 100000 small files, mounts it with discard=sync, and runs a scrub or a replace while the files are rewritten, deleted and renamed and the filesystem is synced every 50 ms. On v7.3.3 10 of 10 replaces cancel and 5 of 10 scrubs report uncorrectable errors; with this patch 0 of 30 each. Without discard, or with discard=async, nothing shows. Fixes: 1dc4888e725d ("btrfs: scrub: avoid unnecessary extent tree search preparing stripes") Assisted-by: Claude:claude-opus-5-5 --- fs/btrfs/scrub.c | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c index 98362c91bce43..4f3c49ca36758 100644 --- a/fs/btrfs/scrub.c +++ b/fs/btrfs/scrub.c @@ -2142,6 +2142,37 @@ out: return ret; } +/* + * Let a waiting transaction commit go ahead, with nothing of ours left that + * the commit could invalidate. + * + * The extent and csum paths of @sctx hold leaves of the commit roots, and the + * queued stripes were filled from them. A transaction commit switches the + * commit roots and unpins the extents freed by the transaction (with + * discard=sync it also discards them) before it lets the scrub continue. A + * stripe read after that, or a search continued in a leaf of the old commit + * root, would find extents that are no longer allocated, and with discard read + * back zeros where it expects a tree block or data with a csum: bogus + * uncorrectable errors, and a dev-replace cancels on the first metadata one. + * + * So finish the queued stripes and drop the paths before pausing; the next + * search starts at the new commit root. + */ +static int scrub_pause_point(struct scrub_ctx *sctx) +{ + struct btrfs_fs_info *fs_info = sctx->fs_info; + int ret; + + if (!atomic_read(&fs_info->scrub_pause_req)) + return 0; + + ret = flush_scrub_stripes(sctx); + btrfs_release_path(&sctx->extent_path); + btrfs_release_path(&sctx->csum_path); + scrub_blocked_if_needed(fs_info); + return ret; +} + static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx, struct btrfs_device *scrub_dev, struct btrfs_block_group *bg, @@ -2164,8 +2195,9 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx, if (ret < 0) return ret; - if (atomic_read(&fs_info->scrub_pause_req)) - scrub_blocked_if_needed(fs_info); + ret = scrub_pause_point(sctx); + if (ret < 0) + return ret; spin_lock(&bg->lock); if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags)) { @@ -2294,7 +2326,6 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx, struct btrfs_device *device, u64 physical, int mirror_num) { - struct btrfs_fs_info *fs_info = sctx->fs_info; const u64 logical_end = logical_start + logical_length; u64 cur_logical = logical_start; int ret = 0; @@ -2311,8 +2342,9 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx, if (ret < 0) break; - if (atomic_read(&fs_info->scrub_pause_req)) - scrub_blocked_if_needed(fs_info); + ret = scrub_pause_point(sctx); + if (ret < 0) + break; spin_lock(&bg->lock); if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags)) { -- 2.53.0