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