]> git.hungrycats.org Git - linux/commit
btrfs: scrub: do not let a transaction commit pass under queued stripes 6.18/topics/scrub-commit-pause
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 11 Nov 2026 03:12:37 +0000 (22:12 -0500)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 11 Nov 2026 03:12:37 +0000 (22:12 -0500)
commitdff6f2a21e5f545815f7cd9f3804ff3d8eb93df2
tree8d659c1c12fe53be1666ccf66e10b2be7d15fe75
parentc3f63d70f236bb7685e6d11d1d9b5b351552e600
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 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.

[zygo: 6.18 backport: 6.18 has no pause point in scrub_raid56_parity_stripe(),
 so only the scrub_simple_mirror() one changes]

Fixes: 1dc4888e725d ("btrfs: scrub: avoid unnecessary extent tree search preparing stripes")
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/scrub.c