Subject: [RFC PATCH 000/114] btrfs: close the raid56 write hole for datacow with stripe-exclusive allocation
To: linux-btrfs@vger.kernel.org
-[STALE COVER -- DO NOT SEND AS IS. This letter was written when the
-series had 65 patches (roadmap 1-39, "Pending" 40-65). The branch now
-has 111: patches 66-111 are listed in the shortlog below but not
-described here, and patch numbers cited in the text may no longer
-match. It needs a rewrite before posting.]
-
-This series closes the raid56 write hole for datacow data by changing the
-allocator, with no on-disk format change and no journal. The same rule,
-applied at log commits, lets a crash with all devices present recover
-completed fsyncs intact, and bounds a degraded crash's fsync-window
-loss to detectable absence -- never silent, never damaging committed
-data (patches 13-15); patches 16-17 and 21-23 remove the latency and
-IO overheads this adds to fsync. Patches 18-20 restore
-write-in-place for nodatacow files and preallocated extents as an
-opt-in policy, with the write hole those writes reintroduce provably
-confined to the writing file alone. Patch 38 extends the same
-allocation rule to raid56 metadata, closing the write hole for tree
-blocks whenever the metadata profile is raid56. Patch 39 makes the
-policy safe to switch on at runtime on a filesystem the legacy
-allocator was writing a moment earlier.
-
-Relationship to the other mitigation strategies: the raid-stripe-tree
-addresses issues specific to zoned (ZNS) devices and remains the right
-tool there -- but this series may simplify the cases the stripe-tree
-work has to cope with, for example by eliminating mid-stripe update
-cases entirely. Journalling in any form (a parity log, a raid1 metadata
-update journal) is a complementary function: it can add crash-safe
-write-in-place for nodatacow and preallocated files, which this series
-deliberately does not attempt. By confining datacow writes to whole
-stripes, this series reduces and simplifies the work remaining for both.
-
-Patch roadmap
-=============
-
-Numbers may shift between revisions; titles are authoritative.
-
-Core allocator:
- 1 btrfs: add btrfs_claim_free_stripe_run() for stripe-exclusive allocation
- 2 btrfs: add open stripe run tracking for stripe-exclusive allocation
- 3 btrfs: add the stripe_alloc allocation policy for raid56 data
- 4 btrfs: report stripe run data IO through the ordered extent lifecycle
- 5 btrfs: retire stripe runs at commit and gate stripe_alloc
- 6 btrfs: add a write-hole invariant checker to the raid56 write path
- 7 btrfs: account stripe_alloc trapped free space for honest statfs
-
-Packing density:
- 8 btrfs: stripe_alloc: keep an open run per size band to trap less
- 9 btrfs: stripe_alloc: grow the frontier run instead of stranding its tail
-
-IO batching and robustness:
- 10 btrfs: raid56: batch stripe_alloc partial writes into full-stripe writes
- 11 btrfs: stripe_alloc: never claim stripes covered by a live run
-
-Interface:
- 12 btrfs: stripe_alloc: control the policy with a filesystem property
-
-fsync guarantee:
- 13 btrfs: stripe_alloc: settle a logged extent's stripes at log commit
- 14 btrfs: stripe_alloc: private per-inode stripe runs for log-active inodes
- 15 btrfs: stripe_alloc: carry an inode's log tail forward at fsync
-
-fsync latency:
- 16 btrfs: stripe_alloc: kick parked rbios before the fast fsync's writeback wait
- 17 btrfs: raid56: pad sub-stripe writes to full stripes in open runs
-
-nodatacow/prealloc write-in-place:
- 18 btrfs: stripe_alloc: isolate nodatacow and preallocated extents by stripe
- 19 btrfs: stripe_alloc: allow-rmw policy, and write-in-place for isolated nocow extents
- 20 btrfs: stripe_alloc: persist nocow runs across commits and remounts
-
-Parking edges and observability:
- 21 btrfs: raid56: do not park sync writes into nocow runs
- 22 btrfs: raid56: expose parking statistics in sysfs
- 23 btrfs: raid56: skip parking for sync rbios finished by their unplug batch
-
-Reservation integrity:
- 24 btrfs: stripe_alloc: count open stripe run remainders against data reservations
- 25 btrfs: stripe_alloc: pessimistic data reservation margin
- 26 btrfs: stripe_alloc: claim fully-free stripes that span free space entries
-
-Coverage observability:
- 27 btrfs: stripe_alloc: warn at mount when raid56 metadata is not covered
- 28 btrfs: stripe_alloc: report read-modify-write of uncovered stripes
- 29 btrfs: raid56: say whether a metadata read-modify-write is a write hole
-
-Parking refinement:
- 30 btrfs: stripe_alloc: tunable park deadlines, and say why padding declined
- 31 btrfs: stripe_alloc: trace why padding declined, at the moment it declined
- 32 btrfs: stripe_alloc: complete parked writes on allocation coverage, not clocks
- 33 btrfs: stripe_alloc: track per-sector liveness and pad from the map
- 34 btrfs: stripe_alloc: count claimable whole-stripe supply directly
- 35 btrfs: raid56: adaptive park deadline from the parked-rbio backlog
-
-Admission and allocation cost:
- 36 btrfs: stripe_alloc: gate data admission on claimable whole-stripe supply
- 37 btrfs: stripe_alloc: cache a failed stripe run scan per block group
-
-Metadata coverage:
- 38 btrfs: stripe_meta: allocate tree blocks by whole stripes
-
-Runtime enable:
- 39 btrfs: stripe_alloc: drain the legacy allocator's writes before arming the write-hole check at a runtime enable
-
-A further 32 patches -- reservation accounting at the fill edge, the
-relocation and reclaim paths, the metadata reserve under raid56
-metadata, and six fixes from the first review round -- are complete,
-validated, and landed on the 6.18 lane's copy of this series; they are
-not yet in the misc-next roadmap above and will be folded in before it
-is posted. They are described under "Pending" at the end of this
-letter so that nothing measured on the current test kernels is
-attributed to a patch that is not in the list above.
-
-Prerequisites
-=============
-
-These are not part of this series -- they are independent raid56 and
-error-handling fixes, sent separately -- but the series is developed and
-tested on top of them, and the guarantees below are stated with them
-applied. Every one was found by this series' test harness.
-
-raid56 fixes (a separate series):
-
- btrfs: propagate a split bio's error when the failing split completes last
- Required for "detectable, never silent". Without it, a read of
- BTRFS_STRIPE_LEN or more over an unrecoverable sector on a degraded
- array returns zero-filled data and succeeds. Fixes: d48e1dea3931.
-
- btrfs: raid56: do not verify the content if there is no data checksum
- (Qu Wenruo, 69e02e32ff2e in btrfs/for-next, Cc stable; it replaced
- our equivalent "only verify recovered sectors that have a checksum"
- in the tree this series is tested on, 2026-10-01.)
- Required for degraded operation at all. verify_one_sector()
- checks rebuilt sectors against the stripe's csum array without
- testing the per-sector csum bitmap, so a rebuilt sector that
- legitimately has no csum -- free space, or the very sector a
- sub-stripe write is replacing, whose csums are not yet in the
- tree -- is compared against a zeroed slot and fails. Sub-stripe
- writes to a degraded raid5/6 then fail with EIO whenever the
- missing device's column is only partially covered by csums, and
- each failure silently drops the unwritten extent. Found by this
- series' degraded-write test on its first run; upstream since
- v6.2 (Fixes: 7a3150723061). stripe_alloc never RMWs and does
- not itself need the fix, but the legacy baselines below do, and
- so does any degraded array taking writes.
-
- btrfs: raid56: fix use-after-free of rbio in bio end_io wakeups
- A pre-existing raid56 UAF (a bare wait_event racing the bio-endio
- wake_up against the rbio free), not caused by this series, but this
- series keeps rbios alive across more waits and the soak loads below
- hit it readily. Every patched-kernel result quoted below was
- produced with it applied.
-
- btrfs: raid56: drop cached rbios when their stripes' extents are freed
- A pre-existing latent-corruption window: nothing invalidates the
- rbio stripe cache when extents are freed, so a later sub-stripe
- write to reused stripes can steal all-uptodate pages holding the
- stripe's previous life and generate parity from that memory
- instead of the disk. If the on-disk content diverged meanwhile
- (silent device corruption of a column holding only freed data is
- invisible -- no csum, nothing for scrub), the write bakes the
- divergence into parity: reads pass, scrub is clean, and the loss
- surfaces only when a device failure forces reconstruction. Found
- by this series' rolling-failure test as a deterministic
- unreconstructible stripe and proven byte-exactly (the parity
- solved uniquely as XOR including pre-corruption bytes that
- existed nowhere on disk). stripe_alloc additionally reorders its
- padding check ahead of the cached-pages shortcut (patch 17), so a
- claimed stripe's first partial write rewrites every column it
- does not cover.
-
- btrfs: raid56: keep a reference on a stolen cached rbio until it is dropped
- btrfs: raid56: do not unhash a cached rbio that has rbios plugged on it
- Two use-after-free races around the rbio cache, both reachable
- with the in-tree cache shrink and any concurrent lock_stripe_add():
- the steal path in lock_stripe_add() drops the hash reference
- before the cache is done with the rbio, and unlock_stripe()
- caches an rbio with RBIO_RMW_LOCKED_BIT still set so a
- lock_stripe_add() in that window can only plug onto it, after
- which a cache removal hits BUG_ON(!list_empty(&rbio->plug_list)).
- Both crashed the test VMs in the stock-allocator pass of the
- acceptance suite under a silently corrupting device; the previous
- patch's cache trimming only makes them frequent.
-
- btrfs: raid56: verify reconstructed tree blocks
- Reconstructed data sectors are csum-verified before use, but a
- reconstructed tree block was returned to the reader unchecked:
- nothing confirmed its csum, bytenr, fsid or generation before it
- entered the metadata cache. Adds the identity checks the normal
- metadata read path performs. Independent of this series; found
- while extending the harness to raid56 metadata.
-
- btrfs: don't abort raid56 data scrub on first uncorrectable sector
- Not required by the series. The harness scrubs deliberately damaged
- filesystems, which stock scrub abandons at the first uncorrectable
- stripe, so the verification below depends on it.
-
- btrfs: log a message when dropping an extent due to IO error
- Not required by the series; this is the patch referred to in "A
- second failure mode" that makes the silent extent drop visible.
-
-delalloc fixes (a separate series, both found running this series to
-ENOSPC under fsstress, neither specific to it):
-
- btrfs: release the space of delalloc ranges abandoned by a failed writeback
- writepage_delalloc()'s error branch only unlocks the delalloc
- ranges after the one that failed in a multi-range folio; the
- folio was already cleared dirty, so EXTENT_DELALLOC sits on a
- clean folio until reclaim clears it without a release, leaking
- the inode's reservation and the data space_info's bytes_may_use
- (WARN_ONs at eviction and unmount). Marked for stable.
-
- btrfs: drain pending NOCOW writes before making a block group read-only
- A nodatacow write() that finds no data space decides to write in
- place and holds nothing; scrub, balance and zoned reclaim make the
- group read-only without flushing, and the writeback then falls
- back to COW with a reservation that never went through admission
- -- dropped at ENOSPC. The fix drains the group before the flip.
-
-Deadlock fixes (a separate series; all three surfaced on the soak rig
-below, none involves raid56):
-
- btrfs: fix flushoncommit deadlock when cloning an inline extent inside i_size
- btrfs: reflink: never block on space reservations while the locked range has delalloc
- btrfs: relocation: reserve all of a folio's slices before dirtying any
- The same shape three times: a task blocks on a transaction join or
- a space reservation while holding a locked dirty folio or an
- extent lock over delalloc, the flusher needs that lock, and the
- commit (flushoncommit, or the reclaim commit that services the
- ticket) waits for the flusher. The first reproduces on a stock
- single-device 6.12 with flushoncommit plus dedupe of inline
- extents; the third is reachable on plain x86 because btrfs uses
- large data folios (up to 64 sectors per folio).
-
-lib/raid6 (sent to the md maintainers, Cc btrfs and stable):
-
- lib/raid6: fix the x1 SIMD gen_syndrome variants for a single data disk
- The sse1x1/sse2x1/avx2x1/avx512x1 gen_syndrome loops preload
- dptr[z0-1] before iterating from z0-2, so with one data disk
- (disks == 3, z0 == 0) they read pointers[-1]: a NULL oops or, if
- the neighbouring bytes happen to be a pointer, P and Q silently
- XORed with an unrelated page. md never builds a 3-disk raid6;
- btrfs does (devs_min = 3), and any raid6 array whose devices are
- uneven in size produces 3-wide chunks once the small devices fill.
- Found by the raid6 coverage run below as a "rare" panic that was
- in fact deterministic on every boot where the boot-time benchmark
- picked an x1 variant.
-
-Two more bugs found the same way were fixed upstream independently
-before this posting: btrfs_end_repair_bio() writing repaired sectors
-back on a read-only mount (upstream 8ceaad6cd6e7, v7.0; our guard is
-carried only on the 6.18 backport, where a stable backport is the right
-action) and defrag reserving with a flush while holding a locked cluster
-(upstream ba02eab28041f, v7.3, fixed more broadly than our patch, which
-the 6.18 lane dropped once that fix was backported to it).
+[RFC draft. The series is described by subtopic, in the order the
+branch will be rebuilt:
+
+ 1. Activation by property, basic no-RMW function
+ 2. A second property that allows RMW for some writes
+ 3. Stripe-only reclaim
+ 4. Competing fsync implementations
+ 5. stripe_meta (raid56 metadata)
+ 6. Debugging code, not for merging
+
+Until then, patch numbers are the current export numbers
+(NNNN-*.patch), and the shortlog below is in the old order. Subtopic 5
+may be dropped; its section says why. REORDER-PLAN.md has the rebuild
+plan.]
+
+This series closes the raid56 write hole for datacow data by changing
+the allocator. There is no on-disk format change and no journal. The
+policy is opt-in, per filesystem, through a property on the top-level
+subvolume's root directory.
The write hole
==============
-A raid56 stripe becomes unrecoverable in degraded mode when data blocks and
-the parity block of one stripe are modified in different transactions: a
-crash during the second modification tears the stripe against parity that
-already reflects committed data from the first. The classic RAID5 RMW hole.
-
-The core observation is that btrfs only tears a stripe this way because the
-allocator fills a partially used stripe across a transaction boundary. If a
-full stripe only ever receives writes between two commits of one transaction,
-then either the transaction commits (data already on disk, valid in degraded
-mode) or it is reverted (the stripe is unreferenced and its contents do not
-matter). Either way there is no hole.
+A raid56 stripe becomes unrecoverable in degraded mode when its data
+and its parity are modified in different transactions. A crash during
+the second modification tears the stripe against parity that already
+reflects committed data from the first. This is the classic RAID5
+read-modify-write (RMW) hole.
+
+btrfs tears a stripe this way only because the allocator fills a
+partially used stripe across a transaction boundary. If a full stripe
+only ever receives writes from one transaction, a crash either keeps
+that transaction (its data is on disk and valid in degraded mode) or
+reverts it (the stripe is unreferenced and its contents do not matter).
+Either way there is no hole.
+
+The exposure is not limited to the previous commit. Any committed
+extent that shares a stripe with a later sub-stripe allocation is at
+risk. On a filesystem that reuses freed space, the steady state of any
+aged filesystem, that is data of arbitrary age.
+
+Detection of the damage is not new. Data checksums flag it (read EIO
+and corruption_errs), and raid56 has reported it correctly since the
+read/repair rework in about v6.5. This series changes the blast
+radius, not the detectability.
The approach
============
-Add a data allocation policy ("stripe_alloc", raid56 data block groups
-only) that allocates strictly from "open stripe runs": contiguous,
-stripe-aligned regions of fully-free stripes, filled sequentially
-(patches 1-3 and 5-6). A run stops accepting allocations when it is exhausted
-or at transaction commit, and a closed run is never reopened -- its
-unallocated tail returns to the free space cache where, being part of a
-partially filled stripe, it cannot be claimed again until the whole
-stripe frees. No persistent state: the "fully free" claim rule
-excludes partially filled stripes statelessly, so retired stripes
-become unallocatable with nothing written to disk.
-
-Two allocator refinements keep the packing dense (patches 9-10). Open
-runs are kept per power-of-two band of remaining space (segregated best
-fit), so a small write lands in an already-open, nearly full stripe
-instead of opening a fresh one; and when an allocation outgrows every
-open run, the frontier run grows to absorb the adjacent fully-free
-stripes it claims, placing the extent across the stripe boundary
-instead of stranding the run's tail. Both preserve the claim rule -- a
-grown run's stripes were claimed fully free, and it still closes at
-commit and never reopens.
-
-The run guarantee also pays raid56 back at the IO layer. Because the
-rest of an open run's stripe is written before the run closes or not at
-all, a partial write rbio for such a stripe can be parked -- stripe
-lock held, RMW not started -- collecting later writes through the
-existing merge path until it goes down as one full-stripe write: one
-parity computation, no reads (patch 11). Parking is strictly
-edge-driven for anything with a waiter behind it: the fast fsync kicks
-the parked rbios covering its ordered extents before waiting for
-writeback (patch 16), sync writes skip parking entirely when their
-stripe belongs to a nodatacow file's private run (patch 21) or when
-their submission batch was already merged at unplug (patch 23), a
-parked write completes as soon as allocation covers its stripe (patch
-32), and a deadline (100ms, 3ms for sync writes) backstops the paths
-with no waiter at all. A flushed partial write whose uncovered sectors
-all lie at or past its run's allocation frontier is zero-padded into a
-single full-stripe write -- no read phase, one parity pass; the zeros
-are physically written, so scrub stays consistent (patch 17). Padding
-decides from a per-sector liveness map (patch 33), so it never
-zero-fills a sector an allocation already owns; the map is the one
-place where an under-report would be silent data loss rather than a
-forced RMW, and it is what the balance-reclaim regression test below
-guards.
-
-Invariants (stated so the design can be checked against them):
+The "stripe_alloc" policy applies to raid56 data block groups. It
+allocates only from "open stripe runs": contiguous, stripe-aligned
+regions of wholly free stripes, filled forward. A run stops accepting
+allocations when it is full or at transaction commit, and a closed run
+is never reopened. Its unallocated tail returns to the free space
+cache. Because the tail is part of a partially filled stripe, the
+claim rule ("only wholly free stripes") excludes it until the whole
+stripe frees. Nothing is written to disk to remember this.
+
+Invariants:
I1 Every extent in a full stripe was allocated within one open-run
- lifetime; stripes are never rejoined after a commit closes them.
- I2 No superblock referencing an extent in stripe S is written while any
- write to S is in flight.
- I3 No stripe contains extents of more than one allocation class
- (cow, relocation, log-active inode, nodatacow inode).
+ lifetime. A stripe is never rejoined after a commit closes its
+ run.
+ I2 No superblock that references an extent in stripe S is written
+ while any write to S is in flight.
+ I3 No stripe holds extents of more than one allocation class (cow,
+ relocation, log, nocow, and with subtopic 5 metadata and log-tree
+ metadata).
I4 Every raid56 data allocation goes through the policy.
-At commit, every open run is closed and the commit waits for all data IO
-into those stripes to reach disk (I2). This runs while the transaction
-still accepts joins, deliberately: the drain waits on data IO from tasks
+At commit, every open run is closed, and the commit waits for all data
+IO into those stripes to reach disk (I2). This runs while the
+transaction still accepts joins. The drain waits for data IO from tasks
that may themselves need a join to make progress (a chunk allocation in
the middle of a delalloc flush, for example), so retiring after joins
-stop would deadlock. It is a data-IO wait -- parked partial writes in
-the closed runs are kicked first, and the wait covers their completion
--- and never an ordered-extent wait, which needs a join too.
-
-A join that arrives between the retirement and the join barrier is safe
-by sequence numbering: the retirement atomically bumps a sequence and
-snapshots the run list under one lock, so any allocation made after it
-opens runs stamped with the new sequence. An ordered extent backed by
-such a run detects the stamp and defers its file extent insertion to the
-next transaction. The committing transaction therefore only ever
-references extents in stripes whose runs it retired and drained; writes
-from the join window land in stripes that belong, wholly, to the next
-window. The result: no flushoncommit requirement, no format change, no
-allocator rwsem.
-
-The defer wait is itself a lock-ordering hazard, found the hard way: a
-production raid5 host (balance plus steady write load, btrfs over
-dm-crypt) deadlocked with the commit parked in the retirement drain and
-every btrfs-endio-write worker parked in the defer wait -- each still
-holding the extent lock its finish had taken before joining. Buffered
-writers piled up on those locked ranges while holding their prepared
-locked folios, which stranded the flusher, kcompactd and page reclaim;
-kcryptd then could not allocate bounce pages for the very data bios the
-drain was waiting on, closing the cycle through the mm. The defer wait
-therefore drops the range's extent lock (and the
-EXTENT_FINISHING_ORDERED tag taken with it) before sleeping, and
-re-takes both before re-joining. The unlocked window is safe: the
-range is still covered by the pending ordered extent, so a writer that
-takes the lock finds the ordered extent, releases its folios and waits
-for it -- exactly the path that keeps writeback and reclaim flowing
-while the commit drains.
-
-Log commits get the same treatment at finer grain (patches 13-15).
-Before the log super is written, the log commit closes the open run
-covering every extent it logs and drains that stripe's data IO -- the
-log-window analogue of I2. A closed run is never extended, so once
-fsync returns, no later write can RMW the stripes holding the logged
-extents. So that one fsync-heavy inode does not keep closing runs it
-shares with other writers -- fragmenting their streams and trapping the
-shared tails -- a log-active inode (sticky, set at its first fsync)
-allocates from its own private runs (patch 14), and its log commits
-settle only its own stripes. The stripe tail trapped per log commit is
-then reclaimed by carry-forward (patch 15): at each log commit the
-settling walk records which of the inode's logged extents sit in the
-closed run's final partial stripe, and the inode's next fsync
-re-dirties those ranges (the defrag pattern) so its delalloc flush COWs
-carried and new data together into the current run -- everything
-downstream is the ordinary COW pipeline, and the emptied partial stripe
-frees whole. The private-run scoping makes this tractable: nobody else
-ever writes a log-active inode's stripes, and reflinked ranges just
-stay put (foreign referents keep the old extent alive in its sealed
-stripe). Crash safety does not regress: a carried extent's old copy is
-dropped through the normal paths, and a logged-but-uncommitted one has
-its ADD and DROP delayed refs cancelled in cleanup_ref_head(), which
-pins must_insert_reserved heads, so the old blocks cannot be
-reallocated before the log that references them is superseded. The
-re-dirty reserves with NO_FLUSH: it runs with the folios and the extent
-range locked, and a flushing reservation there is the deadlock shape
-described under Prerequisites; carrying is best effort and the carried
-data is already durable in its old stripe, so on ENOSPC the carry is
-skipped. Steady-state trapped space per fsync-active inode drops from
-one stripe tail per fsync to at most one partial stripe.
-
-In-place writes (nodatacow, and writes into preallocated extents) would
-reintroduce the hole for a whole stripe's worth of other files' committed
-data, so by default they are forced to COW while stripe_alloc is
-enabled. The data relocation inode is exempt: its extents live in
-relocation-class runs that never share a stripe with other data (I3),
-and relocation depends on in-place writes.
-
-Patches 18-20 make the forced COW a policy rather than a hard rule.
-Nodatacow files' extents and preallocated extents are placed in private
-per-inode runs of their own class (patch 18), so they never share a
-stripe with any other file's data; with the allow-rmw policy enabled,
-an extent whose full stripes are provably owned by the writing inode
-alone (its own run, or the committed extent tree shows sole ownership)
-is written in place (patch 19). Such a write can tear, on a degraded
-crash, only the writing file's own data -- which is the nodatacow
-contract, and no worse than stock raid5, where the same write endangers
-arbitrary neighbors. Anything that fails the ownership test -- extents
-from before stripe isolation, reflinked or snapshotted extents,
-relocated extents -- stays force-COWed, and since the rewrite lands in
-the file's private run, the next overwrite of the same data passes:
-legacy nocow files migrate themselves to isolation in one COW
-generation. The private runs persist across transaction commits
-(nothing in them needs invariant I2) and their partial-stripe tails are
-re-adopted after a remount, so a slowly appended nocow file packs
-sequentially instead of burning a stripe per commit (patch 20). (The
-pending patches narrow this: a private run is only worth keeping while
-the policy actually lets the file be written in place -- see Pending.)
-
-Patch 38 extends the same rule to raid56 metadata. When the metadata
-profile is raid56, tree blocks allocate from metadata-class stripe runs
-under the identical claim rule -- only fully-free stripes, filled
-forward, never revisited -- so no tree block write ever lands in a
-stripe holding committed blocks. Metadata needs its own completion
-accounting (there is no ordered extent): the extent buffer write endio
-reports written blocks back to their run, and the two paths that
-discard a tree block that will never be written report it abandoned
-instead -- a block freed in the same transaction that allocated it
-(including when its delayed ref head was already processed, so the
-free completes through the delayed-ref machinery rather than
-btrfs_free_tree_block()'s own cleanup), and an unwritten log tree
-block discarded at log teardown, which bypasses
-btrfs_free_tree_block() entirely. Miss either report and the run's
-inflight count never drains: the commit-time retirement stalls and the
-block group reference leaks. Both paths were found by the crash
-matrix below -- its recovery mounts drive both routinely -- and the
-reports are part of the patch. Runs of different classes never share
-a stripe, so data and metadata still never mix, and mixed
-data+metadata block groups are refused outright. Metadata retirement
-is two-phase: runs stay open through the commit's own tree building
-and close only after every tree block the transaction allocated is on
-disk -- closing earlier would strand blocks the commit has not
-allocated yet, draining earlier would wait on writes it has not
-issued. Coverage is driven purely by the block group profile: there
-is no separate option or property, raid1c3/c4 metadata is simply
-untouched, and system chunks are covered too from patch 65 on: the
-audit of patches 28-29 had measured real write holes there (parity
-rewritten over committed chunk tree blocks), and a log-writes tear test
-turned that into a lost chunk tree on a degraded mount; with the chunk
-tree placed by whole stripes as well, the same tear costs nothing.
-
-Driving that retirement to saturation -- a workload of
-multi-hundred-item fsyncs keeps log tree IO in flight at every
-commit, where ordinary load only occasionally does -- exposed three
-defects in it, all fixed in patch 38. First,
-the retirement wait was only ever woken when a run was freed, and
-metadata runs are not freed on drain (they are kept for padding and
-merging until the retirement collects them) -- so a run whose last
-block was reported through either discard path above never woke the
-waiter, and the retirement slept out its full timeout. The fix
-wakes at the drain transition itself. Second, retirement runs after
-the transaction unblocks, so the next transaction is already opening
-runs while it waits; counting those meant waiting on writes this
-commit never issued, which cannot settle before the next commit.
-Retirement now waits only on closed runs. Third and worst: the next
-transaction could join the committing transaction's still-open runs,
-putting two transactions' tree blocks into one stripe -- the later
-blocks' writeback then rewrites parity over tree blocks the earlier
-transaction has already committed, which for metadata is exactly the
-write hole this series exists to close. The uncovered-RMW reporter
-of patches 28-29 showed it directly under the saturating load: parity
-rewrites over blocks exactly one generation old, on every commit.
-(That adjacent-generation signature is the fingerprint of live
-sharing; the reporter peeks only a stale header's address and fsid,
-so on freshly reclaimed stripes it can also report long-freed blocks
-from earlier lives -- those ghosts carry large generation gaps and
-audit as dead in the extent tree.) The third fix stamps each
-metadata run with the transaction that opened it: allocation refuses
-and evicts other transactions' runs (closing them and returning
-their unused tails), and retirement sweeps and waits only on runs of the
-committing transaction and older. That restores the data-side
-invariant for trees -- a stripe only ever receives blocks from one
-transaction. Fixed, the same load produces zero adjacent-generation
-reports and zero retirement stalls, and the commit rate rises from
-5-8 commits per four-minute run (every commit eating the 60-second
-retirement timeout) to 427.
+stop would deadlock. It is a data-IO wait, never an ordered-extent
+wait.
+
+A join between the retirement and the join barrier is handled by
+sequence numbers. The retirement bumps a sequence and snapshots the run
+list under one lock, so a later allocation opens runs stamped with the
+new sequence. An ordered extent backed by such a run defers its file
+extent insertion to the next transaction. The committing transaction
+therefore references only extents in stripes whose runs it retired and
+drained. There is no flushoncommit requirement and no allocator rwsem.
+
+The defer wait drops the range's extent lock (and the
+EXTENT_FINISHING_ORDERED tag) before it sleeps, and retakes both before
+it rejoins. Holding the lock deadlocked a production raid5 host
+(balance plus steady writes, btrfs over dm-crypt): buffered writers
+blocked on the locked range while holding locked folios, which stopped
+the flusher and page reclaim, and kcryptd then could not allocate bounce
+pages for the data bios the drain was waiting on (patch 6).
+
+In-place writes (nodatacow files, writes into preallocated extents)
+would reintroduce the hole for other files' committed data, so they are
+forced to COW while stripe_alloc is on. The data relocation inode is
+exempt: its extents live in relocation-class runs that never share a
+stripe with other data (I3). Subtopic 2 turns the forced COW into a
+policy.
+
+Relationship to other strategies
+--------------------------------
+
+The raid-stripe-tree addresses problems specific to zoned devices and
+remains the right tool there. This series may simplify what it has to
+handle, for example by removing mid-stripe updates of datacow data.
+
+Journalling (a parity log, or a raid1 journal for metadata updates) is
+complementary. It could make in-place writes to nodatacow and
+preallocated files crash-safe, which this series does not attempt. By
+confining datacow writes to whole stripes, this series reduces the work
+left for either.
+
+Prerequisites and related series
+================================
+
+The series applies on its own to the base (0c48d26d46bd). It was
+developed and tested on top of these separate series, and the
+guarantees below are stated with them applied. Each fixes a
+pre-existing bug that this series' tests found:
+
+ raid56-fixes (11 patches for merging):
+ - "propagate a split bio's error when the failing split completes
+ last". Without it, a read of BTRFS_STRIPE_LEN or more over an
+ unrecoverable sector on a degraded array returns zeros and
+ succeeds. "Detectable, never silent" below depends on it.
+ - "log a message when dropping an extent due to IO error". It makes
+ the drop described under "A second failure mode" visible.
+ - Use-after-free and stale-cache fixes in the rbio cache (end_io
+ wakeups, stolen cached rbios, unhashing a plugged rbio, cached
+ rbios of freed extents, the pages of a failed write). This series
+ keeps rbios cached longer (patch 75), which makes some of these
+ races frequent.
+ - Verification of reconstructed tree blocks, and raid56 repair
+ fixes (rebuild from Q, device error stats, write-back of the
+ sectors an RMW repaired).
+ raid56-scrub-replace: the harness scrubs deliberately damaged
+ filesystems, so its results depend on scrub not giving up at the
+ first uncorrectable raid56 sector.
+ deadlock-fixes: three lock-ordering deadlocks found on the soak rig.
+ None involves raid56.
+ delalloc-fixes: "drain pending NOCOW writes before making a block
+ group read-only", found at the fill edge.
+
+low-space ("hold block groups free for relocation") conflicts with this
+series in the space accounting. The integration of the two exists only
+in the lane's merge (fixups/post-merge.patch in the export).
+
+Subtopic 1: activation by property, basic no-RMW function
+==========================================================
+
+Patches (61): 1-6, 8-12, 17, 23-27, 30, 32-37, 40, 41, 43-45, 54, 57,
+60, 62-64, 67-69, 73-77, 79, 82, 84, 86, 87, 89, 91, 92, 95-99, 101,
+109-112.
+
+Under stripe_alloc, raid56 data is placed so that no partial stripe
+write ever reaches a stripe holding committed data.
+
+Mechanisms:
+
+- The claim rule: allocation takes runs of wholly free stripes, including
+ runs that span several free space entries or bitmaps (1, 26, 89, 101).
+ A failed scan is cached per block group, so an aged group with no
+ claimable run costs O(1) per allocation instead of a full scan under
+ the free space lock (37).
+- Open runs: one per size band, so a small write lands in a nearly full
+ open stripe instead of a new one. When an allocation outgrows every
+ open run, the frontier run grows into the adjacent wholly free stripes
+ instead of stranding its tail (2, 9, 10). A stripe covered by a live
+ run is never claimed again (12).
+- Run IO is reported through the ordered-extent lifecycle (5, 87), and
+ commit retirement closes and drains the runs (6).
+- Parking: a partial write into an open run keeps its stripe lock and
+ waits for later writes to the same stripe, which merge into it through
+ the existing lock_stripe_add() path. It goes down as one full-stripe
+ write: one parity computation, no reads (11). Parks end when the
+ stripe is covered, when allocation covers it (32), when a waiter kicks
+ them, or at a deadline: 100 ms, 3 ms for sync writes, with a stuck cap
+ of 10 times the deadline (11, 23, 32, 82). An optional congestion
+ knob shortens the deadline while the parked backlog is large (35).
+- Padding: a partial write whose uncovered sectors hold nothing anyone
+ has written is zero-filled to a full stripe, with no read phase. The
+ zeros are written, so scrub stays consistent (17). A per-sector
+ liveness map decides what may be padded, so padding never zeroes a
+ sector an allocation owns (33). A written map, set as raid56 writes
+ complete, lets the first partial write also pad sectors whose write is
+ still to come; their own write is ordered after the pad by the stripe
+ lock (76).
+- Rbio pinning: a stripe stays in the rbio cache until its run has
+ written all of it, so a later write into the stripe takes the earlier
+ sectors from the cache instead of reading them back from the devices
+ before the csum tree covers them (75, 79). At most 256 stripes are
+ pinned (RBIO_PIN_MAX). Without the pin, a device that returns wrong
+ data lost a file on the strip-fault fixture.
+- Space accounting: free space in partially filled stripes is "trapped".
+ It is counted per block group as stripe_unusable, recomputed at commit
+ for groups whose free space changed, and subtracted from statfs
+ f_bavail (8, 54). The claimable whole-stripe supply is counted
+ directly (34).
+- Admission: data reservations are gated on claimable whole stripes,
+ with the open-run remainders, a per-extent margin and stripes stranded
+ behind live runs charged against them, so a write that cannot be
+ placed gets ENOSPC at write(2) instead of being dropped at writeback
+ (24, 25, 36, 57, 60, 110, 111, 112). A group may go read-only only
+ while the admitted bytes still fit the remaining supply (40).
+- Relocation (balance, device remove, shrink) works under the gate: it
+ never waits on the gate, it is charged a margin per reservation, it can
+ fall back to the relocation group, it may allocate data chunks, and its
+ runs are kept until relocation ends (41, 43-45, 73, 74, 84).
+- Lifecycle: runtime enable and disable, remount (99), the property
+ applied before tree-log replay (77), errored filesystems (68, 69, 96),
+ failed mounts and aborted unmounts (63), group deletion and teardown
+ (92, 95), and allocation clusters left by the legacy allocator (62,
+ 98).
+- raid5/6 groups with one data stripe, which upstream maps as
+ RAID1/RAID1C3 since 49ba3a3c0d67, are left to the normal allocator
+ (109), and their free space counts in admission (110).
+
+Interface:
+
+- The btrfs.stripe_alloc property on the top-level subvolume's root
+ directory: "setfattr -n btrfs.stripe_alloc -v 1 /mnt" (patch 4).
+ Removing the xattr turns the policy off. Copies on other inodes,
+ such as received or cloned subvolumes, are refused when set and
+ ignored when loaded.
+- The property is refused, with a message, without the free space tree,
+ with the remap-tree feature, on zoned filesystems and with mixed block
+ groups.
+- Mount prints "using stripe-exclusive allocation for raid56 data
+ (filesystem property)" when the policy is on, and nothing when it is
+ off (64). Log replay with the property set prints "stripe_alloc
+ property in force for tree-log replay" (77).
+- Without subtopic 5, mount warns that raid56 metadata is not covered
+ (27).
+- sysfs allocation/data/: bytes_stripe_unusable, bytes_stripe_claimable,
+ bytes_stripe_open, bytes_stripe_margin, bytes_stripe_stranded.
+- Park tunables in /sys/fs/btrfs/<uuid>/: stripe_park_timeout_ms (100),
+ stripe_park_sync_timeout_ms (3), stripe_park_congestion (0, off).
+- The enospc_debug space_info dump shows per-group trapped bytes (54).
+
+Subtopic 2: a property to allow RMW for some writes
+===================================================
+
+Patches (11): 18-21, 56, 58, 66, 85, 90, 100, 103.
+
+Without this subtopic, stripe_alloc forces COW on nodatacow files and
+preallocated extents. A second property lets the administrator allow
+in-place writes again, with the blast radius bounded: an in-place write
+is allowed only into stripes that belong to the writing inode alone. A
+degraded crash can then tear only that file's own data, which is the
+nodatacow contract. On stock raid5 the same write endangers any file
+that shares the stripe.
+
+Mechanisms:
+
+- While the policy allows in-place writes of a kind, those extents are
+ placed in per-inode NOCOW-class runs, keyed by subvolume and inode
+ (18, 58, 85).
+- Ownership check, per extent: the fast path accepts the inode's own
+ runs; the slow path accepts an extent that the committed extent tree
+ shows has a single plain ref from the writing inode (19, 56, 100).
+ Anything that fails (extents from before isolation, reflinked or
+ snapshotted extents, relocated extents) stays force-COWed. The COWed
+ rewrite lands in the inode's private run, so a legacy nocow file
+ migrates itself in one COW generation.
+- NOCOW runs survive commits and remounts while the policy allows
+ in-place writes, so a slowly appended nocow file packs sequentially
+ (20, 58). Dead NOCOW runs are closed at commit (66).
+- Sync writes into NOCOW runs are not parked (21).
+- 90, 103: a stripe tail claimed in one lock section, and a dead field
+ removed.
+
+Interface: btrfs.stripe_alloc_allow_rmw, on the same root directory, a
+word list separated by comma, space or colon. "nodatacow" and
+"prealloc" enable the confined in-place writes. The third word,
+"fsync", belongs to subtopic 4. Everything defaults off. The name is
+"allow_rmw", not "allow_overwrite", because every word permits a
+read-modify-write of a live stripe that a degraded crash can tear.
+
+Subtopic 3: stripe-only reclaim
+===============================
+
+Patches (6): 42, 46, 47, 70, 113, 114.
+
+Trapped stripe tails accumulate until something relocates their group.
+The stock reclaim trigger keys on used bytes, so it never sees them.
+Trapped space is real free space, and balance reclaims it; this
+subtopic lets the existing reclaim worker do it without an operator.
+
+Mechanisms:
+
+- A group whose trapped bytes reach bg_reclaim_threshold percent of its
+ free space is queued for the existing reclaim worker, the way zoned
+ filesystems recycle zone_unusable space. The trigger runs from the
+ commit rescan and from the cleaner, needs room for the group's live
+ data elsewhere, and has a five-minute hysteresis (46).
+- The worker relocates a queued group even while it has reserved or
+ pinned bytes (47), and allocates a chunk to relocate into when the
+ claimable supply is short (42).
+- Compaction: a mostly live group is not relocated whole. Only the
+ extents in its partially used stripes move, plus, for data, every
+ extent that shares a stripe with one of them, transitively. The group
+ stays, writable, with whole stripes. If that would move more than
+ half the group's live bytes, the group is relocated whole instead
+ (70).
+- Reclaim on demand (114).
+- The reclaim worker relocates with verbose=false and logs nothing.
+ Patch 113 logs the start and end of each reclaim with btrfs_debug(),
+ off unless enabled through dynamic debug. It was asked for as a
+ production diagnostic ("a log message for reclaim that can be enabled
+ through dynamic_printk"), so it is placed here, not in subtopic 6.
+
+These paths also serve subtopic 5's metadata groups.
+
+Interface:
+
+- bg_reclaim_threshold, the existing per-space_info knob. It defaults
+ to 0 on non-zoned filesystems, so automatic reclaim stays off until an
+ administrator sets it. The default is left for review.
+- stripe_compact: percent of a group's length that must be trapped
+ before compaction (default 10, 0 disables).
+- stripe_reclaim_now (write-only): "all" (or "1") queues every group the
+ reclaim predicate picks, ignoring the hysteresis; a block group's
+ logical start reclaims that group whatever its trapped space.
+- echo 'file fs/btrfs/block-group.c format "reclaim: " +p' \
+ > /sys/kernel/debug/dynamic_debug/control
+
+Subtopic 4: competing fsync implementations
+===========================================
+
+Patches (6): 13-16, 83, 88.
+
+Subtopic 1 confines the write hole to the running transaction. Inside
+it, two log commits can still share a stripe: fsync 1 writes the head
+of a stripe, the open run keeps filling it, and a later write's RMW
+rewrites the parity that protects fsync 1's data.
+
+The base (13) settles logged extents at each log commit. Before the
+log super is written, the log commit closes the stripe's run, kicks the
+parked writes for it, and waits for its data IO. Patch 16 kicks the
+parked rbios before the fast fsync's writeback wait, so fsync does not
+pay a park deadline. This closes the hole for completed fsyncs. The
+cost is a trapped tail per fsync per touched stripe. Two
+implementations pay that cost back:
+
+- Private runs with carry-forward (14, 15). Each log-active inode
+ (sticky from its first fsync) allocates from its own private LOG run,
+ and each of its log commits closes that run. The logged extents in
+ the closed run's final partial stripe are recorded, and the inode's
+ next fsync re-dirties them, defrag-style, so its flush rewrites them
+ into the new run and the old stripe frees whole. The re-dirty
+ reserves with NO_FLUSH, because it runs with the folios and the extent
+ range locked; on ENOSPC the carry is skipped, and the data stays
+ durable in its old stripe. Steady state is one partial stripe per
+ inode, and a back-to-back fsync loop rewrites its previous tail each
+ time.
+- Shared LOG runs per log transaction, the default (83, 88). LOG runs
+ are keyed by (subvolume, log_transid), so the inodes logging in one log
+ transaction fill the same stripes. An allocation of a full stripe or
+ more keeps a private run. A log commit does not close a shared run;
+ it seals the stripe holding the end of the logged extent (the frontier
+ moves to the stripe end, and the stripe's final write pads the skipped
+ sectors). The settle then waits, with no timeout, until every
+ allocated sector of the logged extent's stripes is written, using the
+ run's written map. Returning earlier would let a neighbour's later
+ partial write RMW the extent after the log made it durable. Nothing is
+ carried forward; leftover partial stripes wait for frees or
+ compaction.
+
+Shared runs are the default because they measured better. On 5 x 8 GiB
+raid5 data with raid1 and raid5 metadata, against private runs with
+carry-forward in the same rep (patch 83):
+
+- 16 and 64 writers, and one fsync per inode: 1.2-2.9x the fsyncs/s
+ (one 64-writer rep of eight: 0.8x), 3-8x fewer device bytes per fsync,
+ and less slab. A later run gave 1.3-3x the fsyncs/s at 4-64 writers,
+ with p99.9 latency at 0.1-0.7x (bench #280,
+ forensics/fsync-mode-bench-20261004).
+- One writer: parity on latency, 17-29% fewer device bytes.
+- The workload that can lose: two large streaming writers beside small
+ fsync writers, 0.5-1.9x the fsyncs/s. A small fsync waits for
+ neighbours that joined its stripe and sit behind the large IO. The
+ multi-second p99.9 in that workload is btrfs's log-commit tail, which
+ both modes and the stock allocator show (stock raid5 reached 17.1 s in
+ 2 of 4 reps).
+
+Private runs with carry-forward are kept, selectable at runtime, so the
+two can be compared on one boot. If review prefers one, the other can
+be dropped.
+
+Interface:
+
+- stripe_log_data_shared (sysfs, boolean, default 1; 0 selects private
+ runs with carry-forward).
+- The "fsync" word of btrfs.stripe_alloc_allow_rmw waives the log-commit
+ guarantee: no settle, no LOG steering, no carry. The fsync-window
+ exposure of subtopic 1 returns, detectably.
+- Counters in stripe_run_stats: log_settle_waits and log_settle_wait_ns
+ (both modes), log_carry_ranges and log_carry_bytes (private runs),
+ log_seals and log_seal_bytes (shared runs).
+
+Subtopic 5: stripe_meta
+=======================
+
+Patches (15): 38, 48-53, 61, 65, 81, 93, 102, 104, 105, 106. (104 and
+105 are comment cleanups of the whole series and stay whatever happens
+to stripe_meta.)
+
+raid56 metadata gets the data rule. When the metadata or system profile
+is raid56 and stripe_alloc is on, tree blocks come from metadata-class
+runs of wholly free stripes, and a stripe is never revisited once its
+transaction has written it. There is no separate switch. Other
+metadata profiles have no write hole and are untouched.
+
+Mechanisms:
+
+- Completion reports: the extent buffer write endio reports written
+ blocks to their run, and the two paths that discard a tree block that
+ will never be written report it abandoned (38, 102).
+- Two-phase retirement: metadata runs stay open through the commit's
+ tree building and are closed and drained after the tree blocks are on
+ disk. Each run is stamped with the transaction that opened it, so a
+ stripe only receives blocks of one transaction (38). The drain waits
+ without a timeout and dumps the runs once after 60 s (106).
+- Trapped-space accounting for metadata groups (48).
+- Fill edge: the global reserve is backed by usable stripes (49). Two
+ transactions' worth of whole-stripe claims are pre-paid, and a reserve
+ of whole stripes (the largest group, at most a quarter of the
+ space_info) is held back for relocation (50). The global reserve
+ steal refuses below that reserve (51), and the transaction machinery's
+ own reservations and the read-only flip for relocation may draw on it
+ (52, 53). A group made read-only commits first instead of waiting for
+ the periodic commit (93).
+- Runtime enable and disable (61), and raid56 system chunks (65).
+- Log-tree blocks: each log commit gets its own metadata run, keyed by
+ (root, log_transid, transid), so a later writer never RMWs log blocks
+ that an fsync made durable (81). The space returns when the log tree
+ is freed.
+
+Interface:
+
+- allocation/metadata/ shows the subtopic 1 counters, plus
+ bytes_stripe_reserve.
+- stripe_log_run (sysfs, default 1): 0 puts log blocks back in the
+ transaction's metadata runs, which reopens the log-tree hole. It
+ exists to measure patch 81 against itself.
+- Patch 38 narrows the mount warning of patch 27 to system chunks, and
+ patch 65 removes it.
+
+Why stripe_meta may be dropped
+------------------------------
+
+Four claims were checked against the measurements we have. No new
+tests were run for this.
+
+1. raid56 metadata is not faster than raid1. Holds in practice, and
+ partly in theory. On a 4-device raid5 data rig, raid5 metadata
+ against raid1: create+fsync 0.92-0.93x the files/s, fsync p50
+ 1.08-1.09x, fio 4K randwrite+fsync 0.94-0.95x the IOPS, rm -rf
+ 1.11x the time. The only win was cold stat at 0.82-0.88x the time.
+ That bench predates patch 81. With patch 81, device bytes per
+ fsync were 1.36x raid1's (geomean, 0.95-1.98x), with no consistent
+ fsync/s win. In theory, raid5 can win only for commits large enough
+ to fill stripes and perhaps for cold multi-device reads. It loses on
+ fsync (a 1-3 block log commit writes a whole padded stripe), on
+ small commits and on every degraded read. Not measured: physical
+ devices (every member was a loop file on one backing store, which
+ hides raid5's parallelism), degraded mode and raid6.
+
+2. The space saving is smaller than the raid5/raid1 efficiency
+ difference, because trapped blocks fill much of the metadata space.
+ Partly holds, and holds where it matters. raid5 saves raw space only
+ while the trapped fraction stays below 33% at 4 devices (raid6: 25%
+ at 6). On a fresh filesystem at moderate fill, trapping was 16-23%
+ and raid5 used half of raid1's raw space. On churned, near-full or
+ fsync-heavy filesystems trapping reached 51% (fill edge), 82%
+ (6-device raid6 fsstress) and 88% (raid6 COW churn), and with patch
+ 81 the settled metadata total was 2.15x the plain allocator's. The
+ saving disappears or reverses there. Compaction recovers some of it
+ (metadata 89 -> 24 MiB trapped in stripe-compact-test), but reclaim is
+ off by default. Not measured: a raid1-metadata arm on the same aged
+ churn workload.
+
+3. The complexity is high, and so is the risk of metadata stalls and
+ deadlocks. Holds. 13 patches are exclusively stripe_meta (+1150 /
+ -126 lines), in the reservation, overcommit, global reserve, commit,
+ log commit and relocation paths. Of the metadata fix patches, 6 fix
+ ENOSPC aborts or liveness (48-53) and 3 fix stalls or deadlocks (81,
+ 93, 94). Eight more shared patches were found through metadata, six
+ of them for hangs or aborts. At least 5 more defects were folded
+ away earlier. One ENOSPC abort class is still open: with a single
+ metadata group the reserve is 0, and a tree block that finds no
+ claimable stripe aborts the transaction (no acked data lost).
+
+4. It collides with Johannes Thumshirn's recent zoned ENOSPC work and
+ has the same issues. Partly holds. The problems are the same:
+ free-but-unusable metadata space, reservations against it,
+ relocation deadlocking on its own reservation, aborts on reservation
+ failure during reclaim, and metadata writeback order against commit.
+ stripe_meta solves them a second time with different mechanisms (a
+ hold-back reserve instead of a flush state, a steal refusal instead
+ of a retryable -EAGAIN), so every later change to those paths would
+ need both. There is no code collision: all of that work is merged
+ (v7.1-rc1 to v7.3-rc4) and in the base, and only btree_writepages()
+ is touched by both. Against the claim: zoned metadata already
+ accepts append-only placement and unusable-until-reclaim space, so
+ upstream has taken this kind of cost for metadata before.
+
+Dropping stripe_meta removes the 13 patches above (not 104/105), patch
+77 of subtopic 1 (its failure needs raid56 metadata; the raid1-metadata
+crash arm passed without it), about +1211 / -130 lines or 10% of the
+series' insertions, and the metadata hunks of about 9 shared patches.
+The series becomes data-only.
+
+What users lose: write-hole protection for raid56 metadata, raid56
+system chunks and log-tree blocks. A crash plus a lost member can then
+tear committed tree blocks that share a stripe with a later sub-stripe
+write. On stock 7.3, a torn system-chunk write made the chunk tree
+unreadable and the degraded mount was refused (sys-chunk-tear-test).
+With plain stripe_alloc and raid5 metadata, 174 of 249 sub-stripe
+metadata writes rewrote parity over committed blocks (patch 29).
+
+What users should do instead: use raid1 metadata with raid5 data, or
+raid1c3 metadata with raid6 data, which tolerate the same number of lost
+devices. mkfs already defaults to raid1 metadata on multiple devices.
+An existing filesystem converts online with "btrfs balance start
+-mconvert=raid1c3". raid6 data with raid1c3 metadata is the tested
+pairing (r6c3 below).
+
+Patch 27's mount warning then keeps its original scope, raid56 metadata
+or system chunks:
+
+ stripe_alloc does not cover raid56 metadata: the write hole remains
+ for metadata, consider converting it to raid1c3/raid1c4
+
+The rate-limited per-write warning ("read-modify-write of full stripe
+N: this block group is not covered by stripe_alloc, the raid56 write
+hole applies to it") is patch 28, which is in subtopic 6. If it should
+stay as a production warning for uncovered metadata, it moves to
+subtopic 1.
+
+Subtopic 6: debugging code, not for merging
+===========================================
+
+Patches (15): 7, 22, 28, 29, 31, 39, 55, 59, 71, 72, 78, 80, 94, 107,
+108.
+
+Carried for the test harness only:
+
+- The write-hole invariant checker in the raid56 write path
+ (CONFIG_BTRFS_DEBUG), and the drain at a runtime enable that keeps it
+ quiet for writes placed before the policy existed (7, 39).
+- The metadata RMW audit, which says whether a metadata RMW rewrites
+ parity over a committed tree block (29, 55, 94).
+- The data RMW audit against the committed extent tree
+ (debug/stripe_rmw_audit, debug/stripe_rmw_audit_stats; 72).
+- Attribution of each data RMW to the reason padding declined (71, 107),
+ and the pad-decline tracepoint (31).
+- The "write hole applies" warning for RMWs of uncovered groups (28).
+- Statistics: stripe_park_stats (22), stripe_run_stats (59), refused
+ pins and rbio-cache high water (78), the per-class breakdown (80), and
+ ordered extents that end with sectors unwritten, with a rate-limited
+ log line (108).
+- space_info/stripe_unusable_rescan (CONFIG_BTRFS_DEBUG), which is part
+ of patch 8 today and will be split out.
+
+Open for the rebuild: whether stripe_park_stats, stripe_run_stats and
+the pad-decline tracepoint (22, 59, 31) are observability that belongs
+in subtopic 1, or debug code for subtopic 6. Later subtopic 1 patches
+increment their counters, and the liveness map (33) and pad-unwritten
+(76) are written against the pad information structure that 31
+introduced. Keeping them in subtopic 1 is a reorder; moving them to 6
+means rewriting 33 and 76.
+
+Some of this code sits in production paths today: the checker's
+RELOC_USED and NOCOW_USED flags are set in the allocation path, 39's
+drain worker runs on every runtime enable, and 28's warning prints on
+production kernels. The rebuild moves those hunks with their patches.
Interface and compatibility
===========================
-The policy is controlled by a "stripe_alloc" filesystem property -- a
-btrfs. namespace xattr on the top-level subvolume's root directory,
-following the compression property precedent (patch 4). Set once
-(setfattr -n btrfs.stripe_alloc -v 1), it is persistent: the kernel
-applies it when the root directory inode loads its properties during
-mount, before any user IO, so protection cannot lapse through an fstab
-edit or a rescue mount -- exactly the moments a degraded raid56 is most
-likely to be written. Deleting the xattr disables the policy and open
-runs drain at the next commit. Stray copies on received or cloned
-subvolumes are ignored; only the top-level root carries the policy.
-There is no mount option: the property refuses every unsupported
-configuration itself (no free space tree, the remap-tree feature, zoned,
-mixed block groups), so a property set on such a filesystem is refused
-with a message rather than enabling a policy the series does not support
-there.
-
-Setting the property on a live filesystem is safe with respect to the
-write-hole checker (patch 39): the legacy allocator may still have
-writes in flight when the policy flips, and those extents sit in partly
-used stripes no run covers, so the first of them to reach the raid56
-write path would trip the invariant check although nothing violated
-the policy -- the data was placed before the policy existed. The
-checker therefore stays disarmed from the flip until a worker has
-started delalloc on every root and waited for every ordered extent
-that could predate it; a mount-time enable has nothing in flight and
-skips the drain.
-
-A second property, "stripe_alloc_allow_rmw", holds a word list naming
-the cases where stripe_alloc may permit the legacy unsafe RMW of live
-stripes (patch 19). It is "allow_rmw", not "allow_overwrite": the
-fsync case overwrites nothing, but all three words permit
-read-modify-write of stripes a degraded crash can then tear.
-"nodatacow" and "prealloc" enable the confined write-in-place above;
-"fsync" waives the close-at-log-commit guarantee of patches 13-15
-(none of its costs, and the fsync-window exposure of the allocator-only
-patches returns). Everything defaults off.
-
-There is no on-disk format change anywhere in the series, and that is
-the whole compatibility story: an older kernel mounts the filesystem
-read-write and uses the legacy allocator, fully compatibly, because the
-layout is identical -- it lists and reads the xattr but does not act on
-it. New kernels honor the property during the feature's long-tail
-testing period. If no use case surfaces where stripe-exclusive
-allocation is worse than the write hole it prevents, it can eventually
-become the only allocation mode and the property a no-op. The
-log-commit protection (patches 13-15) currently shares the stripe_alloc
-gate; whether it should be gated separately for its own long-tail
-period is an open question for this RFC. Later stages with different
-risk profiles (e.g. a global force-datacow policy) are planned as
-separately gated properties so their exposure can be controlled
-independently.
-
-Out of scope / gated off (for now):
- - zoned filesystems (own allocator, no hole);
- - the remap-tree feature (its relocation writes bypass ordered-extent
- accounting -- integration is future work);
- - filesystems without the free space tree: the claim rule reads free
- space from the tree, so nospace_cache mounts are refused (the v1
- space cache no longer exists as of v7.3; on kernels that still
- have it, v1 writes its cache data in place during commit and is
- refused as well);
- - system chunks: the metadata coverage keys on the METADATA block
- group flag, so SYSTEM chunks keep the hole (real: measured parity
- rewrites over committed chunk-tree blocks on raid5 system chunks;
- patch 27 warns at mount) -- extending the rule to them is future
- work. raid1c3/raid1c4 metadata remains a fine choice and is
- simply untouched by patch 38;
- - mixed data+metadata block groups: refused, permanently, on both
- the mount and the property path.
- fill_data_csums() decides by block group whether to verify csums,
- so in a mixed group it cannot tell a data stripe from a metadata
- stripe; and data and metadata sharing one space_info lets a data
- fill starve metadata reservations into a transaction abort;
- - nodatacow data itself still has the write hole when written in
- place (that is the point of the opt-in); a journal adding full
- crash-safety for in-place writes is separate future work.
+There is no on-disk format change. An older kernel mounts the
+filesystem read-write and uses the legacy allocator; the layout is the
+same, and it lists and reads the xattrs but does not act on them.
+
+The policy is a property, not a mount option. The kernel applies it
+when the root directory inode loads its properties during mount, before
+any user IO, and before tree-log replay when the log is replayed (77).
+So the protection cannot lapse through an fstab edit or a rescue mount,
+which are the times a degraded raid56 is most likely to be written. A
+remount does not revert it (99). Setting it on a live filesystem arms
+the policy at once; removing it disables the policy, and the open runs
+drain at the next commit.
+
+Not supported:
+
+- zoned filesystems (own allocator, no hole);
+- the remap-tree feature (its relocation writes bypass the
+ ordered-extent accounting);
+- filesystems without the free space tree;
+- mixed data+metadata block groups, permanently. fill_data_csums()
+ decides by block group whether to verify csums, so in a mixed group
+ it cannot tell a data stripe from a metadata stripe, and a data fill
+ in a shared space_info can starve metadata reservations into a
+ transaction abort (patch 6);
+- crash-safe in-place writes to nodatacow data (subtopic 2 only bounds
+ the damage).
+
+The log-commit protection (subtopic 4) shares the stripe_alloc
+property. Whether it should have its own property during its testing
+period is open. If no use case appears where stripe-exclusive
+allocation is worse than the hole it prevents, it could later become
+the only raid56 allocation mode.
Guarantee shape
===============
-Crash with all devices present: fully correct (cow + csums, unchanged).
-
-Crash plus one missing device: no data from any committed transaction
-can be harmed. The fsync window (data made durable by fsync since the
-last full commit) is not recovered on a degraded array -- the safe
-recovery below discards the log -- but its loss is detectable absence,
-never silent corruption and never collateral damage to committed data.
-A crash with all devices still present recovers the fsync window intact
-by normal log replay. This bound -- committed data untouchable,
-fsync-window loss detectable, degraded reconstruction never trusted --
-is the improvement. For raid6 the same statements hold with two
-missing devices, and the raid6 coverage below exercises exactly that.
-
-The end state is three operating models:
-
- legacy (stripe_alloc off): sub-stripe writes RMW shared stripes as
- they always have. A degraded crash tears other files' committed
- data (the write hole), and a degraded array cannot safely accept
- RMW writes at all.
-
- stripe_alloc, default policy: no RMW of any stripe holding earlier
- data, ever. Data from committed transactions and completed fsyncs
- survives a crash plus a missing device, and the array keeps taking
- new writes while degraded without endangering any of it, because no
- write ever lands in a stripe holding earlier data. nodatacow and
- prealloc writes are forced to COW. (Stated for raid56 data. With
- raid56 metadata, patch 38 applies the same rule to tree blocks and
- the committed-data half of the statement extends to the trees
- themselves; system chunks are the remaining uncovered case, warned
- at mount. The completed-fsync half does NOT yet extend to raid56
- metadata: see "Log tree under raid56 metadata" below.)
-
- stripe_alloc + allow_rmw: the named cases RMW as before and their
- write-hole exposure returns, confined to what each case's machinery
- can reach. nodatacow/prealloc in-place writes require per-extent
- stripe isolation as a precondition -- an extent that lands in a
- shared stripe stays force-COWed -- so a torn RMW can damage only
- the writing file's own data. allow_rmw=fsync re-exposes only
- just-fsynced data, detectably. Data written by every other path
- keeps the default-model guarantee.
-
-"No RMW of a stripe holding earlier data" is the precise claim, and
-for data written through a stripe run it now holds as "no RMW" at all.
-A stripe run's stripe is often written in pieces: a write that ends
-inside it leaves the rest to later writes, and writeback allocates a
-whole range before any of its bios complete. Each run therefore keeps
-a written map beside its live map, set as raid56 writes complete. The
-stripe's first partial write pads not only the sectors nobody has
-allocated but also those allocated to writes still to come -- they hold
-nothing anyone has written, and the stripe lock orders their own write
-after the pad, which it replaces. Every later write into the stripe
-finds sectors already written outside its range, and those it takes from
-the rbio cache, where the stripe pin keeps the stripe until its run has
-written all of it -- otherwise it would read that data back from the
-devices before the csum tree covers it, unverified (the strip-fault
-fixture lost a file exactly this way). Measured on the strip-fault
-fixture and the corrupt-churn test: zero data RMW reads in stripe_alloc
-groups. The "why padding declined" counters of patches 30-31 attribute
-any that remain; written_not_live and padded_unwritten in
-stripe_park_stats watch the written map itself.
-
-Log tree under raid56 metadata. Metadata stripe runs are stamped and
-retired per transaction, and nothing settles them at a log commit the
-way patch 13 settles the data stripes of a logged extent. So with
-raid56 metadata, fsync N's log-tree blocks share a stripe with the
-blocks a later log commit (or the transaction commit) writes, and
-that later write's read-modify-write rewrites the parity over blocks
-fsync N had already made durable; a crash plus a missing device can
-tear them. Found in review; then measured (Testing below): the loss
-is the log alone. The degraded mount refuses to replay ("Failed to
-recover log tree", -EIO); the recovery is a read-only, degraded
-rescue=nologreplay mount, which never writes and reads the
-authoritative committed tree (recovered intact in every measured
-case). Committed metadata is never touched -- across the whole
-characterization no torn stripe held a committed tree block, because
-every metadata run drains before the super that commits it -- and no
-torn stripe ever held both a log-tree block and another tree block of
-the crashed transaction, so the sharpest form of the hazard did not
-occur. The recommended pairing -- raid6 data with raid1c3 metadata --
-is not affected at all, since its log tree is mirrored. Whether to settle
-metadata runs at log commit (with a log-class metadata run to confine
-the trapped tails, as the per-inode data runs do) or to state the
-raid56-metadata fsync window as detectable loss is the open decision
-of the review; the measurement is what it is decided on.
-
-Today a sub-stripe write in one transaction shares a stripe with data
-committed by an earlier transaction, so a degraded crash tears that
-earlier, already-committed data. In the common case the allocator fills
-forward and the victim is the immediately preceding commit -- the
-harness below reproduces exactly that on stock kernels -- but the
-exposure is not limited to it: any committed extent that shares a
-stripe with a later sub-stripe allocation is at risk, which on a
-filesystem that reuses freed space (the steady state of any aged
-filesystem) is data of arbitrary age. Confining allocation to whole
-stripes removes the whole class: a committed extent's stripe is never
-extended by a later transaction, so the only stripes a degraded crash
-can tear are those still being filled in the running one.
-
-Detection of the damage that does occur is not new and not part of this
-series: btrfs data checksums flag it (read EIO and the per-device
-corruption_errs counter), and raid56 has surfaced these correctly since
-Qu Wenruo's read/repair rework in ~v6.5. What this series changes is the
-blast radius, not the detectability.
-
-Testing this series did expose one gap in that detectability, in the
-generic bio layer rather than in raid56. btrfs_bio_end_io() dropped a
-split bio's error whenever the failing split was the last to complete --
-which, for a checksum failure, is essentially always, since read repair
-walks every mirror before giving up while the healthy splits finish
-immediately. raid56 splits reads at each stripe, so any read of
-BTRFS_STRIPE_LEN or more over an unrecoverable sector on a degraded array
-returned zero-filled data with no error, while btrfs logged the checksum
-failure and counted the corruption; a 4 KiB read of the same sector
-returned EIO. Profiles that do not split at 64 KiB (single, dup, raid1)
-were unaffected, which is how it survived. It reproduces unchanged on
-stable 6.18.39 and on 7.2-rc, so it is neither new nor caused by this
-series; the fix is the first prerequisite above.
-
-The guaranteed recovery for the fsync window is the read-only
-rescue=nologreplay mount: the committed tree is authoritative -- no
-reconstruction of a torn log, no in-place replay writes -- and the
-bound above holds under any crash model; the fsync-window files are
-simply absent, which is detectable, not silent. Beyond it the
-harness drives the aggressive recovery as a probe of the default
-model's claims: replay the log onto the degraded array read-write,
-then keep going -- write a fresh workload phase while still degraded,
-sync it, and re-verify everything, old and new. Under a
-flush-consistent crash model (every member cut at the same barrier,
-then an independent unflushed suffix each: what a device can actually
-lose, since btrfs flushes every device and waits before writing the
-superblocks a commit depends on) the default policy passes all of it
-on every geometry. The degraded replay recovers what the all-present
-replay of the identical image recovers, compared case by case -- the
-torn images are the same whichever member is dropped, so the
-comparison is exact -- and the continued degraded writes damage
-nothing, before or after them (94 continuations across the sweep,
-zero damage to pre-crash or new data). The one permitted difference
-appeared once in the sweep: a degraded view may end its log earlier
-than the all-present view for a file whose fsync never completed, its
-log tail sitting in some members' unflushed suffixes -- detectable
-absence of data fsync never promised. Under the harness's harsher
-default model -- each member cut at an independent point, an image
-honest devices cannot present (btrfs's barriers order every
-superblock behind flushes on all members; in the failing cases the
-cuts sat 14 to 145 barriers apart) -- degraded replay does resurrect
-torn state, which is why nologreplay remains the guaranteed recovery
-and the aggressive one remains a probe. Committed data is never
-harmed either way, and with the split-bio error fix in the
-prerequisites any loss it does produce reads as EIO rather than as
-zeros.
+Crash with all devices present: unchanged. COW and csums give a
+consistent filesystem, and log replay recovers completed fsyncs.
+
+Crash plus up to P missing devices (one for raid5, two for raid6):
+
+- No data from a committed transaction can be harmed.
+- Completed fsyncs: subtopic 4 keeps the stripes of logged extents from
+ being written again after the log commit. The guaranteed recovery is
+ a read-only "rescue=nologreplay" degraded mount, which reads the
+ committed tree and never trusts a reconstruction of the log; the fsync
+ window is then absent, which is detectable. Degraded log replay is
+ driven by the tests as a probe. In the crash campaigns, under a
+ flush-consistent crash model, it recovered what the all-present replay
+ recovered. Under the harness's harsher per-member crash model (each
+ member cut at an independent point, which honest devices cannot
+ produce) it can return torn state, which is why it is a probe and not
+ the recovery.
+- Any loss reads as EIO, never as wrong data or zeros (with the split
+ bio fix from raid56-fixes).
+
+The three operating models:
+
+- Legacy (property unset): sub-stripe writes RMW shared stripes. A
+ degraded crash can tear other files' committed data, and a degraded
+ array cannot safely take sub-stripe writes at all.
+- stripe_alloc: no RMW of a stripe holding earlier data. The array
+ keeps taking writes while degraded without endangering earlier data.
+ nodatacow and prealloc writes are forced to COW. With subtopic 5 the
+ same holds for raid56 metadata, system chunks and log-tree blocks.
+- stripe_alloc with allow_rmw: the named cases RMW as before, and their
+ exposure returns, confined. nodatacow and prealloc in-place writes
+ can tear only the writing file's own data. "fsync" re-exposes only
+ just-fsynced data, detectably.
+
+For data written through a stripe run, the claim is in fact "no RMW".
+The first partial write into a stripe pads the sectors still to come
+(76), and later writes take the earlier sectors from the pinned rbio
+(75). corrupt-churn measures zero data RMWs in stripe_alloc groups.
A second failure mode, no crash required
========================================
-Sharing a stripe with older committed data breaks writes even without a
-crash. When a raid5 stripe already holds two unrecoverable corrupt blocks --
-bit rot on two devices, or an earlier torn write -- and a new datacow write
-is allocated into a free block of that same stripe, the sub-stripe write
-triggers a full read-modify-write. The RMW must read and csum-verify every
-data sector of the stripe to recompute parity; it finds two bad sectors,
-cannot recover them (raid5 tolerates one), and btrfs drops the *new* extent.
-A write with nothing wrong with it is lost because of pre-existing corruption
-in an unrelated file that happened to share its stripe. This has been
-reported by several users on 6.6 and 6.12. Upstream btrfs drops the new
-extent *silently*: a write IO error during flushing discards the extent
-without a message, and a recovery failure inside the RMW's read phase
-counts as exactly such a write error. The last patch under Prerequisites
-at least logs the drop; either way it remains a data-loss event.
-
-It is the same root cause as the write hole -- a new write sharing a stripe
-with older committed data -- and the same allocator change removes it: a
-stripe_alloc write only ever lands in a fully-free stripe, so it never reads,
-let alone fails to recover, another file's blocks. The write succeeds, or
-near-full returns a clean ENOSPC, but is never silently dropped.
-
-The degraded variant of the same collision is the second prerequisite
-above: there the RMW's recovery verification fails not on genuinely
-corrupt neighbors but on rebuilt sectors that never had csums at all,
-so a degraded array refuses valid writes outright. Same root cause --
-a sub-stripe write forced to read and verify a stripe it shares with
-unrelated state -- and the same allocator change removes the whole
-interaction.
+Sharing a stripe with older data breaks writes without any crash. When
+a raid5 stripe already holds two unrecoverable blocks (bit rot on two
+devices, or an earlier torn write), a new datacow write allocated into a
+free block of that stripe triggers an RMW. The RMW must read and verify
+every data sector to recompute parity, finds two bad sectors, cannot
+recover them, and btrfs drops the new extent. A write with nothing
+wrong with it is lost because of corruption in an unrelated file. Users
+have reported this on 6.6 and 6.12.
+
+The drop is silent upstream. A write error during writeback discards
+the extent with no message, and write(2) had already succeeded, so an
+application that does not fsync never learns of it. raid56-fixes patch
+1 logs the drop; it is still data loss.
+
+The cause is the same as the write hole's, and the same change removes
+it: a stripe_alloc write lands only in a wholly free stripe, so it never
+reads another file's blocks. The write succeeds, or the filesystem
+returns ENOSPC at write(2). Measured: stock 20 dropped extents, the
+series 0 (corrupt-devices); with fsstress and a churn writer on the
+damaged columns, stock 370, the series 0 (corrupt-churn). An earlier
+revision left a residual of 32-37 drops in the churn test; it is gone.
Testing
=======
-Everything below was run on two 8-core test VMs with root ssh and a
-serial console, on loop devices or LVM volumes, with deterministic
-workloads so patched and unpatched runs write byte-identical data.
-The harness is a git repository of its own (raid56-wh-harness) with a
-test catalog listing every campaign, what it proved and where its
-results live, and a revalidation driver that re-runs the
-regression-relevant subset after every rebase; the regression plan is
-described at the end of this section. Where a paragraph names a rig it
-is the rig that paragraph's numbers come from; the original one is a
-raid5 of four 700MiB devices (64KiB stripe elements, 192KiB of data
-per full stripe).
-
-A dm-log-writes crash harness records every raid5 member device, runs a
-phased small-file workload, then replays each member's log to a cut point
-plus a different torn tail per member, drops one device, mounts degraded and
-verifies every committed file against a sha256 manifest.
-
- stock kernel: 4/36 armed (torn+degraded) cases destroyed committed data
- (all csum-detected), 24/24 controls clean.
- with this series: 0/36 armed cases damaged, 24/24 controls clean.
-
-The matrix was repeated across a geometry sweep -- raid5 with 3..10
-devices (2..9 data stripes) and raid6 with 4..10 devices (2..8 data
-stripes), covering non-power-of-two data widths -- under three policy
-configurations: full protection, allow_rmw=nodatacow:prealloc with
-in-place churn, and allow_rmw=fsync. Every geometry in every
-configuration was clean on the load-bearing claims: all controls
-clean, zero armed committed-data damage, and under the safe recovery
-zero silent corruption anywhere. The sweep also drove degraded
-read-write log replay as a deliberate probe of the unsafe recovery
-above: on several wider geometries it returned wrong content for
-never-completed fsync-window files (never for committed data). Running
-that down is what produced the split-bio error-propagation fix in the
-prerequisites; with it applied the same probe returns EIO for those
-files instead, and no configuration in the sweep returns silently wrong
-content anywhere. (One caveat on the sweep's raid6 rows, discovered
-later: they ran over raid1 metadata, and a two-device drop can take
-both copies of a metadata chunk, so some raid6 cases died as
-"unmountable" instead of testing data recovery. The raid6 coverage
-described below uses raid1c3 metadata for that reason.)
-
-Repeated under a flush-consistent crash model -- every member cut at
-the same barrier, then an independent unflushed suffix each, which is
-what a device may actually lose -- all three configurations are clean
-on every geometry, and the full-protection configuration is
-additionally held to the degraded-replay parity and degraded-write
-criteria described under Guarantee shape: 130 mounted fsync-window
-cases with zero silent and zero csum-detected corruption, and 94
-degraded read-write continuations with zero damage to pre-crash or
-new data. That model reproduces the write hole on the legacy
-allocator at a lower rate (the honest unflushed window is narrow), so
-the harsher per-member-entry model is retained as the primary
-committed-data evidence.
-
-The degraded-write test is also what exposed the second prerequisite,
-on its first run: on the legacy allocator, 29 of 56 continuations on
-one geometry died with fsync EIO -- many on clean images, where
-degraded read-write operation is unambiguously supposed to work --
-with no device error and nothing logged. verify_one_sector() was
-failing every rebuilt sector that has no csum against a zeroed csum
-slot. With the fix, 55 of 56 complete with zero damage, and the one
-remaining failure is the case where the crash-time write hole had
-already destroyed committed data: the RMW's verification now refuses,
-loudly, to reconstruct through the genuinely torn stripe rather than
-bake garbage over it. The full-protection configuration passed the
-same test on the unfixed kernel, 56 of 56: it never RMWs a shared
-stripe, so it never enters the failing path.
-
-A separate fsync-window mode makes the torn phase's data durable by
-fsync only (log tree, no transaction commit) before the cut. On the
-allocator-only patches (1-12) it showed the expected residual exposure:
-committed data intact in every case, damage confined to just-fsynced
-files and csum-flagged. With the log-commit settling (patches 13-15)
-that residual loss goes to zero under the safe recovery: every mounted
-case verified all committed files intact with the fsync-window loss
-reduced to nothing beyond detectable absence -- zero silent
-corruption -- and the harness enforces this as a strict acceptance
-criterion.
-
-With the prerequisites applied the criterion is stricter still: silently
-wrong content is now forbidden in every mounted case, including the
-degraded-replay probe, which no longer gets an exemption. On raid5 with
-nine devices, all 19 mounted cases pass it -- 920 committed files intact
-in every one, no case returning wrong content -- where the same run
-without the split-bio fix had seven cases handing back zero-filled data
-for fsync-window files with no error.
-
-The allow-rmw policy was validated the same way: with four nodatacow
-files overwritten in place through every phase including the torn one,
-the full strict matrix stays clean -- the in-place RMW traffic, running
-to the moment of the crash, damaged nothing beyond its own expendable
-files (two of them ended the run as a single extent after 88 fsynced
-in-place overwrites each). A lifecycle smoke verifies
-overwrite-in-place, forced-COW control, one-generation self-heal of a
-deliberately shared legacy layout, writes into preallocated extents,
-and sequential packing of a nocow file through six commits, an inode
-eviction and a remount (one contiguous extent at the end), with clean
-scrubs throughout.
-
-The per-inode steering and the carry were verified byte-exact: a
-stream-writing file kept a single contiguous extent through five
-interleaved fsyncs of a second file; six consecutive 8K-write+fsync
-rounds ended with all 48K packed in one full stripe (one tail trapped
-instead of six), and the trapped-space counter decomposed exactly into
-the expected tails in every case.
-
-raid6 data with raid1c3 metadata -- the pairing this series recommends
-for production -- has its own coverage driver, run on both hosts after
-every candidate: the replay matrix on both arms (stock must be RED,
-stripe_alloc GREEN) with the two-device drop axis and the degraded
-read-write continuation, the strict fsync-window matrix under the
-flush-consistent model, within-tolerance whole-device corruption
-(detection must be visible in the csum counters, recovery must be
-byte-exact, scrub must repair, a cache-cold re-read must be clean,
-plus one-corrupt-plus-one-missing combinations), and two
-beyond-tolerance A/B stages -- two devices wiped, and stripes
-corrupted beyond parity's reach -- where stock drops writes and
-stripe_alloc must not. On the current candidate all seven stages pass
-on both hosts with the kernel untainted: 72 matrix rows per arm with
-27/27 controls clean, the write hole reproduced on the stock arm and
-absent on the stripe_alloc arm, 43/43 degraded continuations intact
-(stock: 12 incomplete), the strict fsync-window criterion met in all
-24 mounted cases, recovery 30/30, and on the beyond-tolerance stages
-570 (stripe corruption) and 10 (two devices wiped) writes dropped on
-the stock arm against 0 on stripe_alloc, from identical fixtures.
-raid1c3 is load-bearing here for the reason given above: raid1
-metadata tolerates one lost device and the raid6 drop axis removes
-two.
-
-The second failure mode was reproduced directly (no crash): 8 devices, six
-small and two several-GB-larger so metadata lands only on the large ones;
-fill to ENOSPC with 4K..512K files then delete half, leaving only sub-stripe
-holes in committed stripes; wipe two of the small devices from the 1M offset
-(destroying their data and parity but not the superblocks or the large-device
-metadata, so the array still mounts all-present); then write more.
-
- stock kernel: 20 new extents silently dropped (RMW recover-fail).
- with this series: 0 extents dropped.
-
-A harsher variant of the same test (the same wipe, then 180 s of
-fsstress plus a content-verifying churn writer on the two corrupted
-columns) does not reach zero: 360 dropped extents on stock against
-32-37 with the series, reproduced on both hosts on the current base.
-That is a ~91% reduction, not the elimination the fixture above shows,
-and the residual is not yet attributed; it is tracked as an open item
-(the dropped writes read back as zero-filled holes for a writer that
-did not fsync, and the "dropping unwritten extent" message it counts
-fires for any ordered extent finishing with an error, so the count is
-an upper bound on RMW-caused drops).
-
-Additionally: a multi-hour fsstress + balance + scrub soak (25 rounds, ~1.25M
-ops) with the in-kernel write-hole invariant checker (patch 7,
-CONFIG_BTRFS_DEBUG) armed -- zero violations, all scrubs clean. The
-full-stripe write batching was additionally soaked with concurrent
-fill-to-ENOSPC/delete storms, balance, and fsstress -- the pressure
-pattern that exposed a run accounting race (a stripe fully re-freed by
-discarded reservations could be re-claimed while its old run was still
-draining); the fix, run-range exclusivity at claim time (patch 12), ran
-the same load cleanly for ten hours where the race fired in minutes. A
-final twelve-round fsstress + balance + scrub soak and an xfstests
--g quick pass complete the gate; every xfstests failure was attributed
-(known pre-existing, load flakes passing on rerun, or failing
-identically on a plain mount of the same base kernel).
-
-The stripe_unusable statfs accounting was validated against an independent
-userspace computation of trapped space from a dump of each block group's
-free ranges: the two agree exactly, the reported value is identical live
-and after a umount/mount reload, deleting all data returns it to zero, and
-balance reclaims it -- across extent- and bitmap-backed free space, with an
-in-kernel assertion that every free byte is distributed to exactly one
-stripe.
-
-An 8-device rolling-failure scenario (devices pre-filled with random
-garbage so lost writes cannot masquerade as blank sectors; three
-successive device corruptions with continued operation, no unmounts,
-trees written between failures, sysadmin deletes the unrecoverable
-tree, two scrubs) runs fully clean under stripe_alloc: data written
-before a corruption reads correct-or-EIO with zero silent damage, data
-written after reads all-correct through the remaining failures, and
-both scrubs finish with no errors. The stock baseline on the same
-script loses multiple GiB of both trees silently.
-
-The same fill-to-ENOSPC generation initially turned its finding
-inward: byte-counted data reservations held no collateral against the
-stripe claim rule, so commits and mid-transaction free-space returns
-could trap the space out from under an admitted reservation and
-delalloc writeback dropped the pages (measured at its worst: a fill
-"wrote" 12.5 GiB into a 5 GiB filesystem). Patches 24-25 close this
-with the same shape metadata reservations use -- pessimistic at
-admission (claimed remainders and one full stripe width per
-outstanding delalloc extent are counted against reservations, probed
-through the ticketed data flush), optimistic at allocation -- plus
-immediate accounting of returning sub-stripe fragments. With them the
-fill edge is exact: zero dropped writebacks, zero allocation failures,
-capacity unchanged, and fullness surfaces only as ENOSPC at write(2).
-A raid6 run later found an ordering race in that accounting, fixed in
-patch 24: the per-block-group remainder deltas were applied to the
-space_info aggregate outside the lock that computed them, so a
-negative delta could overtake the positive one it trailed and
-underflow the aggregate -- a spurious warning, and a clamp that then
-inflated the total. The deltas now apply in the order they were
-computed. Patch 36 then gates admission itself on the counted
-whole-stripe supply, so write(2) returns ENOSPC where statfs reaches
-zero instead of writeback discovering the shortfall. The fill-edge
-campaign that followed -- fsstress with thousands of preallocating
-writers, background reclaim, relocation and scrub all at the edge at
-once, under a dead or silently corrupting device -- found the cases
-the gate still missed: they are the pending patches, and with them a
-healthy-filesystem fill to ENOSPC loses zero acknowledged writes
-(checksum-manifest verified) and reports zero writeback drops, the
-only allocator ENOSPC left at the edge being relocation refusing
-cleanly.
-
-Balance reclaims trapped space completely (stripe_unusable collapses
-to ~0 and a refill reuses the space), with the usual balance caveat
-that relocating a block group needs enough free space outside it:
-reserving headroom before filling -- shrink each device ~1G at mkfs
-time, grow back to max before balancing, the same technique admins
-already use against metadata ENOSPC -- lets balance complete on a
-fully aged filesystem. The liveness map of patch 33 has a regression
-test of its own here: a per-file-fsync fill followed by balance once
-found that the grown-frontier path did not mark its cross-stripe spill
-live, so padding zeroed it and computed parity over the zeros --
-committed data silently destroyed, in raid5 and raid6 alike, visible
-only when balance read it back. The fix is one line in patch 10; the
-test (balance-reclaim, both profiles, both allocators) is what would
-catch its return.
-
-The metadata coverage was validated with the same crash harness on a
-raid5 data + raid5 metadata rig: the full replay matrix -- 24
-controls, 36 armed torn+degraded cases, and the degraded read-write
-continuation probe -- runs green end to end on two independent hosts
-(zero committed-data damage, every continuation intact, zero silent
-corruption anywhere). The matrix's recovery mounts (log replay plus
-orphan cleanup in every case) are also what exposed the two metadata
-completion-report paths now handled in patch 38: each run drives both
-discard paths tens of times, and a single missed report reproduced as
-a leaked block group reference at unmount within minutes -- on both
-hosts, deterministically -- until both were reported.
-
-Saturating the same rig's log commits (eight writers issuing
-multi-hundred-item fsyncs against a 100ms sync loop, raid6 data +
-raid6 metadata) is what exposed the three retirement defects
-described under Metadata coverage: every commit stalled the
-retirement timeout, and the uncovered-RMW reporter showed the
-cross-transaction parity rewrites. With the fixes the same load
-runs at full commit rate with
-zero stalls and zero adjacent-generation reports, on two hosts. The
-armed replay matrix was then repeated on raid6 data + raid6 metadata
-with the fixes in place: all controls clean, zero committed-data
-damage, and 46 of 46 degraded read-write continuations intact.
-
-Regression plan and acceptance suite
-------------------------------------
-
-Everything above is a campaign that answered a question once. What
-runs after every rebase and on every candidate is a fixed plan with
-declared expected outcomes, so a green run means something and a
-surprise stops the loop:
-
- sanity -- environment and tooling preflight
- stage0 -- the dm-log-writes replay matrix, both crash models
- fsync -- the fsync-window and torn-log suite
- directed -- every single-purpose reproducer for a bug this series
- had (balance-reclaim liveness, the fill edge, scrub on a
- fragmented array, the block-group gate, nocow private
- runs, the runtime enable, the legacy-layout warnings);
- a script exits 77 when the kernel lacks the feature it
- tests, and an undeclared kernel WARN during any script
- fails the stage
- octopus -- the acceptance suite below, 4 h budget
- soak -- the deadlock rig below, 12 h
-
-The acceptance suite runs thirteen tests under a fault that is present
-for the whole run, on a 6-device raid5 with one spare: read-after-write
-with a cache drop, no IO errors visible to applications, scrub, balance,
-fsstress, ENOSPC without data loss (the live filesystem is shrunk with
-resize and filled to the edge), compression, continuous kernel health
-(dmesg and D-state watchdog), nodatacow, fsync durability, fsync-heavy
-log-tree stress with renames, device replace, and an offline check of
-the healed array. Each host presents a different fault model: one
-member returning EIO on every access (a dead disk, through dm-error),
-or one member silently dropping every write and returning rotating
-random garbage on every read (dm-flakey over a random backing whose
-offset changes every second, so no block ever reads the same twice and
-reconstruction is exercised on every read). Three passes per loop:
-stripe_alloc with the fault injected online, stripe_alloc mounted
-degraded from the start, and the stock allocator on the same kernel.
-The declared expectation is that both stripe_alloc passes pass all
-thirteen (a single fault is inside the redundancy, so the only
-acceptable application-visible error is ENOSPC at the fill edge), the
-stock pass may fail (marked as expected), and nodatacow is skipped
-under silent corruption where its loss is undetectable by design. A
-positive control -- the same suite on a pristine stable kernel with
-none of the prerequisites -- fails four of the tests (ENOSPC data loss
-and post-replace corruption under the dead disk; write errors reaching
-the application and fsynced data not durable under silent corruption),
-which is the evidence that the green results are not vacuous.
-
-On the current candidate: under the dead-disk model, three loops of
-all three passes in a 12-hour budget -- every stripe_alloc pass
-13/13, the stock pass 13/13 once and 12/13 twice, the one failure a
-debug-config artifact (a 20 ms expedited-RCU stall timeout tripping
-the health watchdog at unmount) -- no unexpected failure, kernel
-untainted. Under the silent-corruption model, sanity, stage0, fsync,
-directed and the full acceptance suite all pass (12/12 per pass plus
-the declared nodatacow skip). The raid6 coverage above is 7/7 on
-both hosts. The same suite on the 7.3-rc2 base gives 13/13 and 13/13
-for the stripe_alloc passes; its stock pass scored 7 passes and 6
-expected failures and ended in a relocation ENOSPC abort after the
-last test, the small-geometry metadata exhaustion the suite now
-provisions against. An earlier 24-hour
-endurance run of the suite (four loops per host, before the fill-edge
-campaign) was clean under both models.
-
-The soak is the deadlock rig: eight LVs with one removed (degraded),
-optionally dm-crypt underneath (the production shape), mounted
-degraded,compress=zstd:3,flushoncommit,stripe_alloc,discard=async, with
-churn writers, a deduper, a snapshot loop, a balance loop and mass
-deletes, a heartbeat watching commit progress and a capture (SysRq,
-stacks, ftrace ring, drgn probes) tripping on a genuine stall. It is
-the rig that reproduced the production retire/finish-ordered deadlock
-described under The approach, the carry-forward folio deadlock that
-made the carry reserve NO_FLUSH, and all three deadlock-fixes
-prerequisites, and it is where the 60-230 s self-resolving commit
-stalls of a large delayed-ref backlog were shown (NMI-sampled) to be a
-generic delayed-ref grind and not this series.
-
-Review round one (2026-09-15/16) added three directed tests and one
-characterization to the plan, all in the harness repository. A
-runtime enable/disable test for raid56 metadata (property set on a
-live filesystem that the legacy allocator had been using, with legacy
-data extents and, under ssd_spread, a data allocation cluster in
-place; a fill to the metadata edge; then a property-enabled
-filesystem disabled and unmounted) was RED on the unfixed series --
-the disable left the metadata counters stale, the metadata runs
-unretired, and the unmount asserted on their block group references
--- and found two more things on the way to green: the arming scan
-could not see free space the legacy allocator had parked in an
-allocation cluster (an assertion, then a panic, at the first enable on
-a metadata group), and the enable-time drain covered data only, so the
-enabling transaction's legacy tree blocks tripped the write-hole check
-at its commit. With the six round-one fixes it passes on raid5 and
-raid6 metadata, rotational, ssd and ssd_spread, on both hosts, with
-the enabling transaction's own read-modify-writes as the only
-remaining reports (inherent: its blocks were placed before the flip).
-A preallocation storm against manifest-verified buffered writers at
-the fill edge guards the atomic stripe hold (it did not reproduce the
-review's window before the fix and stays clean after). The directed
-regression set passes on the fixed candidate on both hosts.
-
-The raid56-metadata fsync-window characterization runs the replay
-matrix on raid5 data + raid5 metadata (5 devices) and raid6 + raid6
-(6 devices) in fsync-window mode under the flush-consistent model,
-with degraded log replay on, a rescue=nologreplay recovery when it is
-refused, the degraded read-write continuation, an offline check, and a
-scan of every metadata stripe the torn tail touched (which tree blocks
--- log or other, committed or not -- it holds, whether its parity
-still matches its data, and which members' loss would expose a durable
-log block). Two passes each: cuts spread over the fsync stream, and
-late cuts that land inside the transaction commit, where a log-tree
-block and another tree block of the same transaction could meet in a
-stripe. Across all four passes -- 162 armed torn-plus-degraded cases
--- zero committed-data damage, zero silent corruption, every degraded
-continuation intact, and no torn stripe held a committed tree block or
-mixed a log block with a non-log block of the crashed transaction.
-The degraded log replay was refused only in the two raid5 cases the
-scan had named (a torn log tree); every other case, including the
-late-cut passes and all of raid6, replayed cleanly. For the two
-refused cases the rescue=nologreplay recovery read the committed set
-back intact (825 files, both cases). The hazard scan
-over-approximates in the safe direction: several cases it flagged
-replayed without a refusal, because the "durable" log blocks it counts
-include dead nodes superseded by a later log commit.
-
-Fifteen pre-existing, unrelated bugs surfaced along the way. Thirteen
-are fixed by the standalone patches listed under Prerequisites: the
-soak exposed the raid56 rbio use-after-free, the fsync-window sweep
-exposed the split bio error propagation gap, the degraded-write test
-exposed the recovered-sector csum verification bug, the rolling-failure
-test exposed the stale rbio-cache parity window, extending the harness
-to metadata exposed that reconstructed tree blocks were returned to
-readers unverified, the acceptance suite's stock pass under silent
-corruption exposed the two rbio-cache use-after-free races, the fill
-edge exposed the abandoned-delalloc reservation leak and the
-nodatacow-versus-read-only race, the deadlock rig exposed the three
-lock-ordering deadlocks, and the raid6 coverage exposed the lib/raid6
-single-data-disk bug. Two more -- the read-only repair write-back and
-the defrag reservation deadlock -- were fixed upstream independently in
-the meantime. None is caused by this series and none is part of it.
+TESTING.md (in the export directory) lists every test with its last
+result, kernel, harness commit and evidence. The summary below is from
+it. The kernel under test for the current results is the misc-next
+lane's integration of all topics (7.3.0-rc7+ #5, KASAN, lockdep and
+BTRFS_DEBUG on), which includes the prerequisite series above.
+
+Write hole and damage to committed data:
+
+- dm-log-writes replay matrix (raid5, raid1 metadata): every
+ member is recorded, each replay cuts each member at a mark plus a
+ torn tail, drops a device, mounts degraded and verifies every
+ committed file. Stock: 34 of 36 armed cases damaged committed data.
+ stripe_alloc: 0 of 36. Controls clean on both (24/24).
+- raid6 data + raid1c3 metadata (r6c3, 7 stages, two-device drops):
+ stock 7 of 45 armed cases damaged, stripe_alloc 0 of 45; 43 degraded
+ read-write continuations intact; strict fsync-window criterion met;
+ beyond-tolerance stages: stock dropped 20 and 1900 writes, stripe_alloc
+ 0.
+- directed-writehole (one stripe, 3 devices), old-stripe (damage to old
+ data through a refill of holes), rolling-failure (three successive
+ device corruptions with writes in between): stock damages committed
+ data, stripe_alloc does not.
+- strip-fault: partial-stripe writes through a strip that returns
+ garbage. Both stripe_alloc arms read back every file.
+- The fsync stage of the regression driver (log-tree settle under the
+ safe recovery): PASS.
+
+Space, admission and relocation:
+
+- Fill to ENOSPC with manifest-verified writers (enospc-dataloss,
+ fill-edge-debug, prealloc-race): no acknowledged write lost, no
+ writeback dropped.
+- The incremental claimable count against a full rescan, under trim,
+ punch-hole churn and every discard mode: exact after quiesce.
+- balance-reclaim: balance reclaims trapped space (1458 -> 3 MiB) and
+ padding never zeroes live data.
+- stripe-reclaim: background reclaim took a filesystem from 53% trapped
+ at its peak to 0% with no operator; with reclaim off, 81% stayed.
+- stripe-compact: data trapped 300 -> 0 MiB, metadata 89 -> 24 MiB, with
+ every file verified and btrfs check clean.
+
+Lifecycle and policy: mount-state, property-remount, bg-gate,
+meta-crash-replay, stripe-meta-toggle (runtime enable and disable on
+raid56 metadata), retire-bail (an errored filesystem ends the drains),
+the nocow lifecycle tests and prealloc-inplace all pass.
+
+New directed tests each have a negative control: one kernel with
+patches 67, 99, 109, 110 and 111 reverted and the commit in 93 removed
+fails every test that guards one of them.
+
+Acceptance suite: 13 tests under a fault present for the whole run,
+on a 6-device raid5. In the dead-disk role it passed 13/13 on one host.
+On the other host it passed 13/13 in one loop of three, and the other
+two loops ran the filesystem out of space during the suite's
+time-bounded fsstress (ENOSPC from the test geometry, every acknowledged
+write verified, btrfs check clean). The harness now restores headroom
+after that test; the confirmation runs are pending.
+
+Not rerun on this kernel: the silent-corruption role of the acceptance
+suite, the 12-hour deadlock soak (last PASS on the 6.18 lane,
+2026-09-08), and the raid56-metadata fsync-window characterization
+(162 armed cases, no committed damage; it predates patch 81).
+
+Not covered by a test of their own: 86 and 101 (need allocation fault
+injection), 100 (a blocking search in a NOWAIT write), and 94, 95, 97
+(lock-order and teardown races, covered only by lockdep and KASAN runs
+of the whole set).
Performance
===========
-On small-file writeback, where the stock code produces 100% sub-stripe
-rbios and rewrites parity once per file, parking converts 86% of stripe
-traffic to full-stripe writes and cuts write rbios 4.6x; a mixed
-4K-512K workload goes from ~50% to 97% full-stripe writes. Full-stripe
-completion before the commit also collapses the trapped space of a
-mixed-size 800MiB fill from ~87 MiB to ~3 MiB.
-
-fsync latency (8K append+fsync stream, p50, same boot): full protection
-costs about +2ms over the legacy allocator; allow_rmw=fsync runs at or
-slightly below legacy latency (about 3ms under full protection); and an
-in-place nodatacow overwrite+fsync stream under the allow-rmw policy
-runs faster than stock nodatacow. Device read IO during a protected
-fsync stream is zero (every flush is a padded full-stripe write; on
-rotational media the eliminated RMW read round trip is the dominant
-cost). These fsync/sync latencies depend on parking being edge-driven for
-every waiter (patches 16, 21, 23 and 32): no measured waiter path pays a
-parking deadline. An async writeback with no waiter behind it can pay
-one; at a small-file fill to ENOSPC that becomes a throughput cost (see
-Known costs).
-
-The allocator's own cost showed up once, on a production array rather
-than a test rig: an aged 85 TiB raid5 with legacy-packed block groups,
-where every 4K allocation rescanned a whole block group's free space
-under the free-space lock looking for a run that did not exist -- a
-delalloc worker at 100% CPU and commits at KiB/s. Patch 37 caches the
-failed scan per block group (invalidated by any free-space return), and
-on a rig shaped like that array (60 GiB raid5 fragmented by
-delete-every-other) the average claim went from 1447 us to 0.8 us and a
-fsync write storm from 48 to 218 files/s, now fsync-bound.
-
-On filesystems that are not raid56 the series is dormant: its hooks on
-the buffered write path are early-outs measured at 0.2% of
-btrfs_buffered_write()'s CPU time (ftrace function profile, single
-profile, no stripe_alloc), and a quiet-host raw-LV A/B of the two most
-recent revisions shows no bandwidth change (sequential +2.2%,
-sequential with fsync -0.4%, direct IO -5.9% with fully overlapping
-per-run ranges, 4K random write 0.0%, ten runs each).
-
-A /sys/fs/btrfs/<uuid>/stripe_park_stats file (patch 22) reports how
-every park ended (filled to a full stripe, kicked by a waiter or a
-settle/retire flush, completed on allocation coverage, or expired at
-its deadline), how many flushed writes were padded to full stripes
-versus needing the RMW read phase, why each declined padding, and the
-metadata RMW audit's live/dead/free classification, so the deadline
-constants can be judged, and retuned if ever needed, from measurements
-rather than taste.
+Small-file writeback, which on stock is 100% sub-stripe RMW, goes to 86%
+full-stripe writes with parking, and write rbios drop from 1537 to 335.
+A mixed 4K-512K workload goes from 43% to 97% full-stripe writes (patch
+11). The congestion knob, on a throttled moderate stream, keeps three
+times as many merges and 38% fewer RMW reads than a flat 3 ms deadline
+(patch 35).
+
+Packing: on a 4-device raid5 mixed 4K-512K fill, usable capacity at
+ENOSPC is 1561 MiB against stock's 1569 MiB (1421 MiB without growing
+the frontier run). Trapped space at the end of the fill is 7.2 MiB
+(111.1 MiB without growing), and parking collapses the trapped space
+of a mixed forward fill from about 87 MiB to 3 MiB (patches 10, 11).
+
+fsync: see subtopic 4 for the two implementations against each other.
+The current default has not been re-measured against the stock
+allocator's latency since it became the default.
+
+Claim cost: an aged 85 TiB raid5 with legacy-packed groups spun its
+delalloc workers at 100% CPU, rescanning a whole group for a run that
+did not exist on every 4K allocation. Patch 37 caches the failed scan.
+[6.18 A/B of 2026-08-16, not in the forensics: average claim 1447 us ->
+0.8 us, fsync write storm 48 -> 218 files/s.]
+
+On filesystems that are not raid56, or without the property, the
+series' hooks are early returns.
Known costs
===========
-Free space in partially filled stripes is unallocatable until the whole
-stripe frees. With the banded cursors and the growable frontier this cost
-was measured against the stock allocator, filling byte-identical
-mixed-size (4K-512K) data and reading the trapped bytes from the
-stripe_unusable counter:
-
- - streaming/forward fill: usable capacity at ENOSPC within 0.5% of the
- stock allocator (1561 vs 1569 MiB usable; 1421 MiB
- without the growable frontier). Stock's density here comes from
- filling partial stripes across transactions -- exactly the behavior
- that is the write hole;
- - aged filesystem, age-correlated deletes (fill to ENOSPC, delete the
- oldest half, refill): ~5% of capacity trapped. Contiguous
- allocation-order packing lets whole stripes free together;
- - aged filesystem, adversarial scattered small-file deletes: ~14%
- trapped -- freed small files' stripes stay pinned by kept neighbors.
-
-For reference, stock refills a fragmented filesystem to exactly its fresh
-capacity (zero trapped) by reusing every sub-stripe hole -- each such
-reuse being a cross-transaction partial-stripe write. Trapped space is
-real free space and balance reclaims it; garbage collection on aged
-filesystems is expected and by design. The pending patches hand a
-block group whose trapped bytes cross the existing bg_reclaim_threshold
-to the existing reclaim worker, the way zoned filesystems recycle their
-unusable space, so an operator no longer has to run balance by hand.
-The threshold keeps its upstream default of 0 on non-zoned
-filesystems (a reviewer decision, deliberately not changed here), so
-automatic reclaim is off until an administrator sets it. With raid56
-data that only means balance does the reclaiming, as today. With
-raid56 metadata it matters more: every commit's COW turns freed tree
-blocks into trapped partial stripes, nothing recovers them, and the
-metadata reserve eventually refuses even unlinks with ENOSPC until
-the knob is set or a balance runs -- an operational requirement of
-the raid56-metadata configuration that this letter states rather
-than hides. The recommended raid1c3 metadata has none of this.
-
-statfs reports the trapped bytes honestly (patch 8): they are tracked
-per block group as stripe_unusable (summed into a space_info
-bytes_stripe_unusable, recomputed at commit for groups whose free space
-changed) and subtracted from f_bavail, so df no longer promises space the
-allocator will refuse. The counter is exposed at
-/sys/fs/btrfs/<uuid>/allocation/data/bytes_stripe_unusable. It is derived
-from the existing free space (no on-disk format change; the free space
-tree and cache are left untouched). Patches 24-25 make the reservation
-layer respect it as well: trapped fragments, claimed run remainders and
-a pessimistic per-extent margin are all counted against data admissions,
-so a writer that cannot be covered waits in the ticketed data flush and
-receives ENOSPC at write(2) -- a delalloc writeback is never left to
-discover the shortfall after the pages are dirty. Patch 36 gates
-admission on a directly counted claimable whole-stripe supply, so
-write(2) reaches ENOSPC exactly as statfs reaches zero, and patch 37
-caches a failed claim scan per block group. Balance -- or, with the
-pending patches, the reclaim worker -- remains the reclaim mechanism
-for trapped space.
-
-The admission gate is pessimistic by design (the direction chosen for
-this RFC: refuse space early rather than admit a write the allocator
-cannot place). Measured on a fragmented raid5 after a delete-half and
-balance, the refill accepts about 24% less than the pre-gate revision
-did -- but the pre-gate revision's extra came with dropped writebacks
-in the same run (26 in one boot against 0) and a statfs that promised
-1.9 GiB more than the allocator could place, so the 24% is the honest
-number. A big sequential file to ENOSPC is within 1.5% either way and
-a small-file fill to ENOSPC is equal.
-
-Async small-file fill to ENOSPC pays the parking deadline. When the
-workload is many sub-stripe files whose neighbours land in the next
-file's stripe rather than this one, a partial write's rbio never fills:
-it waits out its deadline -- and, still missing an allocated neighbour
-whose data never comes, its 10x stuck cap -- before padding to a full
-stripe and going down. Measured on an 8-device raid5 fill: a 4K-512K
-small-file fill writes 3.8 GiB in ~1600s with ~12000 parks reaching the
-stuck cap, while a large sequential fill of the same filesystem writes
-12 GiB in 34s with none -- sequential writes are already full stripes and
-never park. The cost is throughput only: every such write reaches disk
-correctly at any fill level (no path returns wrong or zero data). Patches 32-34 shrink the cost
-where it can be shrunk: a parked write completes as soon as allocation
-covers its stripe rather than when a clock expires, and padding
-decides from a per-sector liveness map so it never zero-fills a live
-sector. Patch 35 makes the deadline adaptive: a stripe_park_congestion knob (default off)
-shortens a park only while the parked-rbio backlog runs ahead of it, so a
-small-file flood -- which builds that backlog -- drains at the short
-deadline, while a moderate steady stream stays under it, keeps the full
-deadline, and keeps its merges (3x more full-stripe writes and 38% fewer
-RMW reads than a flat 3ms deadline on a throttled stream). The archetypal
-"operator fills the disk" accident is one large writer, which is already
-full stripes, parks nothing, and pays neither cost.
-
-Pending: reservation, relocation and reclaim hardening
-======================================================
-
-Twenty-six further patches on top of this series are complete and
-carried by every test kernel quoted above from the acceptance-suite
-paragraph on. They were landed on the 6.18 lane's copy of the series
-on 2026-09-16 after the first review round (whose findings and fixes
-are listed after them), and will be folded into this roadmap with the
-misc-next port before it is posted. They exist because the fill-edge campaign -- the
-acceptance suite's ENOSPC test under a dead or corrupting device, with
-fsstress preallocating from eight processes while background reclaim,
-relocation and scrub all reach the edge together -- found every case
-the admission gate of patch 36 still missed, and each was fixed as its
-own patch with the counter or dump that proved it. Titles:
-
- btrfs: stripe_alloc: do not count trapped free space twice when making a block group read-only
- btrfs: stripe_alloc: keep the admitted bytes placeable when a group goes read-only
- btrfs: stripe_alloc: never let relocation wait on the whole-stripe gate
- btrfs: stripe_alloc: give a queued stripe group a chunk to relocate into when the supply is short
- btrfs: stripe_alloc: charge relocation a margin per reservation, not per extent
- btrfs: stripe_alloc: keep the relocation group's stripes out of the admission supply
- btrfs: stripe_alloc: admit relocation against the whole claimable supply, not only its dedicated group
- btrfs: stripe_alloc: let a data write fall back to the relocation group rather than drop
- btrfs: stripe_alloc: hand groups full of trapped free space to the reclaim worker
- btrfs: stripe_alloc: relocate a queued stripe group even while it has reserved or pinned bytes
- btrfs: stripe_meta: maintain trapped free space for metadata groups like data
- btrfs: stripe_meta: back the global reserve with usable stripes, and say when a tree block finds none
- btrfs: stripe_meta: hold back whole stripes so tree blocks always land and trapped groups can be reclaimed
- btrfs: stripe_meta: do not steal from the global reserve below the relocation reserve
- btrfs: stripe_meta: let the transaction machinery's own reservations draw on the reserve
- btrfs: stripe_meta: let a group go read-only for relocation against the reserve
- btrfs: stripe_alloc: show per-group trapped bytes in the space info dump
- btrfs: stripe_alloc: tell a dead tree block from a live one in the metadata RMW audit
- btrfs: stripe_alloc: do not read an inline backref off a keyed-ref extent item
- btrfs: stripe_alloc: count whole stripes stranded behind live runs as trapped
- btrfs: stripe_alloc: give nocow extents a private run only while they may be written in place
- btrfs: stripe_alloc: count nocow run lifecycles and stranded stripes in sysfs
- btrfs: stripe_alloc: charge the held stripe margin against every admission
- btrfs: stripe_alloc: admit data reservations by whole stripes
- btrfs: stripe_alloc: hold a stripe of margin across a preallocation
- btrfs: stripe_alloc: hold the probe margin until the metadata reservation charges it
-
-What they change, in the order the campaign found it:
-
- - Scrub and replace on a fragmented array. inc_block_group_ro()
- asked the space_info to absorb the group's free space in bytes
- while the trapped part of that free space was already counted as
- used, so a churned raid56 data group could never go read-only and
- every device's scrub failed with "failed setting block group ro:
- -28" at 51% used. The read-only transition now also refuses while
- admitted bytes would no longer fit in the remaining whole-stripe
- supply (scrub, balance and reclaim retry), the fail-early direction.
-
- - Relocation at the fill edge. Relocation's cluster reservations
- went through the data gate and could wait as a ticket for the very
- stripes relocation was about to free, with the exclusive operation
- and the source group held: writers queued behind that ticket and
- unmount hung on their writeback. Relocation now never waits on the
- gate, is charged one stripe of collateral per reservation instead
- of one per extent (relocating a group of 16-48K files had inflated
- the margin to 150 MB), is admitted against the whole supply while
- its dedicated group's stripes stay out of everyone else's supply,
- can be handed a fresh chunk by the reclaim worker when the
- remaining groups are the trapped ones it was queued to move, and
- an ordinary write admitted a moment before a group became
- dedicated may fall back into it rather than be dropped.
-
- - Automatic reclaim of trapped space, keyed on bg_reclaim_threshold
- like zoned, with the reclaim worker taking a stripe group even
- while it has reserved or pinned bytes (a stripe group is queued
- for trapped space, which waiting does not free).
-
- - raid56 metadata at the fill edge. Trapped bytes in metadata groups
- are now maintained like data (they were frozen at their first
- value), and the global reserve is backed by whole stripes: a chunk
- is allocated while unallocated space still exists, two
- transactions' worth of whole-stripe claims are held back so a tree
- block admitted in bytes always finds a stripe, the global-reserve
- steal refuses below the relocation reserve so an unlink returns
- ENOSPC instead of the commit aborting, the transaction machinery's
- own NO_FLUSH reservations may draw on the reserve, and the
- read-only transition that starts a relocation may too. Each of
- those was a transaction abort in the tiny-file metadata fill test
- before its patch; with all of them the fill stops at a clean
- ENOSPC and the delete phase runs through.
-
- Two refinements came from raid6 metadata under the acceptance
- suite's full load (a dead member, fsstress, then the fill) and are
- folded into the reserve and steal patches. Relocation holds its
- claim on the reserve for the whole of btrfs_relocate_block_group(),
- not only while reloc_ctl is set: creating the data reloc inode
- reserves six items before the first stage, and made as an ordinary
- reservation it failed exactly when relocation was needed -- once
- claimable had fallen below the reserve -- so every reclaim of a
- metadata group failed and the trapped space kept growing (857
- failed relocations in one run; 12 with the change). And the steal
- refusal makes one exception, the mount-time orphan cleanup: an
- eviction it cannot complete leaves the orphan in place, cleanup
- stops, and the read-write mount fails, so a filesystem that
- reached the refusal with orphans on disk could never be mounted
- read-write to run the relocation the reserve is kept for. The
- cleanup's evictions are bounded by the orphans on disk; a directed
- reproduction that is refused (open_ctree -22) without the
- exception mounts with it. A third, found reading the code: when a
- relocation ticket was granted out of the reserve, the ticket loop
- left the reserve subtracted from its running total, so every
- ticket after it in the same pass saw the reserve as free. The
- subtraction is now per ticket.
-
- - A new last patch lets reclaim compact a group instead of skipping
- it. Reclaim only moved groups whose live data is below
- bg_reclaim_threshold, but trapped space also builds up in mostly
- live groups (steady COW churn leaves partially used stripes behind in
- every group). Compaction relocates only the extents in a group's
- partially used stripes -- plus, for data, every extent sharing a
- stripe with one of those, transitively -- and keeps the group,
- writable again with its stripes whole; when that would move more
- than half the group's live bytes the group is relocated whole
- instead. It reuses relocation (find_next_extent() skips unselected
- extents; replace_file_extents() rewrites only the selected ones'
- pointers). On a raid5 test filesystem with half-stripe files and
- every fourth deleted, reclaim compacted two data groups (about a
- third of their live bytes moved) and one metadata group, trapped
- space fell from 300M to 0 (data) and 80M to 22M (metadata), and
- every file and btrfs check stayed clean; stripe_compact=0 turns it
- off.
-
- - The metadata RMW audit distinguishes a dead block (freed with its
- stripe, harmless under a torn parity write) from a live one by
- asking the committed extent tree, which turned six hundred
- "write hole" reports per fill into zero live ones.
-
- - A keyed backref on a data extent item (the leaf had no room for an
- inline one) made the sole-ownership walk read the next item's bytes
- and WARN.
-
- - The admission gate's premise "claimable implies placeable" had
- three holes. Whole stripes freed inside a live run's range cannot
- be claimed (a second run over the same range would make the
- lookups ambiguous) but were counted claimable: on a raid56
- filesystem filled by fsstress, thousands of cached inodes each
- held a private nodatacow run over stripes that had since freed,
- the gate admitted ~100k writes per loop against them, and
- writeback dropped every one. They are now counted as trapped
- (bytes_stripe_stranded), a nodatacow private run only exists while
- the allow-rmw policy would actually let the file be written in
- place, and a stripe_run_stats file counts the lifecycles so a test
- can prove the paths ran. Then the held stripe margin was charged
- against total_bytes but not against the supply the gate checks;
- then preallocation was admitted for its bytes while it claims whole
- stripes, holds no margin, and eight of them at once at the edge
- each took a stripe the gate had counted for someone else; then the
- probe margin was released between the data check and the metadata
- reservation that re-charges it, and eight writers were admitted
- into each other's released margins. Fill-edge writeback failures
- per loop fell from ~100k to tens with the stranded-stripe and
- nocow-run patches, to 2 with the held probe margin, and to 0 on a
- healthy filesystem.
-
-The first review round of the folded result found five things, all
-confirmed and fixed as six further patches on the same lane:
-
- btrfs: stripe_alloc: saturate the read-only guard's claimable subtraction
- The read-only guard subtracted a group's claimable bytes from
- the aggregate without saturation; the two are updated under
- different locks, so a wrap waved the transition through.
- btrfs: stripe_meta: carry raid56 metadata groups through a runtime enable or disable
- The enable/disable sweep walked DATA space_infos only; metadata
- retirement returned at once with the policy off (runs left open
- until unmount, which asserted); the enable-time drain covered
- data only (the enabling transaction's legacy tree blocks
- tripped the write-hole check at its commit).
- btrfs: stripe_alloc: charge the preallocation's stripe hold under the reservation lock
- The one-stripe hold for a preallocation was added after the
- reservation dropped its lock; two sub-stripe fallocate()s could
- be admitted against one stripe.
- btrfs: stripe_alloc: return a group's allocation cluster before scanning its stripes
- The arming scan could not see free space the legacy allocator
- had moved into an allocation cluster (a metadata group's whole
- initial extent), and asserted at the first runtime enable.
- btrfs: stripe_meta: sync the open-run remainder when the commit retires metadata runs
- After a disable the last retired runs' remainder stayed counted
- as used metadata for the life of the mount.
- btrfs: raid56: do not call a covered stripe_meta group uncovered
- The per-write reporter called every raid56 metadata RMW
- uncovered, including the covered stripe_meta case.
-
-The sixth finding -- completed fsyncs are not protected under raid56
-metadata -- is confirmed and measured (Guarantee shape and Testing),
-and its fix or restatement is the open item of the round.
-
-Validation of the folded result is the acceptance-suite, raid6 and
-soak results quoted under Testing (every kernel there carries these
-patches), plus the round-one tests above. The directed evidence: on a healthy filesystem the
-fill-edge reproducer reports zero dropped writebacks, and four
-fill-to-ENOSPC runs of the manifest-verified data-loss writer (~5000
-files each, no fsync) lose zero acknowledged writes; the only allocator
-ENOSPC left at the edge is relocation's, which fails a balance or
-shrink cleanly rather than losing data. The scrub-on-a-fragmented-
-array reproducer passes at 88% used where it failed at 83%; the
-nodatacow-strand test shows the default policy stranding nothing and
-the waived policy refusing at write() rather than dropping, and
-recovering the space at eviction. Against the same base without these
-patches, on raid5 with stripe_alloc: sequential write +32% on a fresh
-and on a fragmented filesystem, 4K random write +12%, small files on a
-fragmented filesystem +86%, balance 24% faster, bytes_stripe_unusable
-14% lower; the one cost is the capacity pessimism quoted under Known
-costs, accepted for this RFC.
-
- fs/btrfs/... | 39 patches, 34 files changed, 6338 insertions(+), 49 deletions(-)
- harness, logs and the test catalog available on request.
-
-
-Patches 40-65, by area
-----------------------
-
-Metadata (48-53, 61, 65). Patch 38 allocates tree blocks by whole stripes;
-these make that survivable. Metadata groups maintain trapped free space
-like data groups (48), the global reserve is backed by usable stripes and
-says so when a tree block finds none (49), and whole stripes are held back
-so tree blocks always land and a trapped group can still be relocated
-(50-53). 61 carries metadata groups through a runtime enable or disable.
-65 extends the whole-stripe rule to raid56 system chunks: the chunk tree was
-the one tree the series still left in shared stripes, and a torn parity
-update there followed by a lost member costs the whole filesystem (a
-log-writes tear test demonstrates it, and that it no longer happens).
-
-Reclaim and relocation (40-47, 62). Relocation must never be refused by
-the gate that protects everyone else, or a full raid56 filesystem cannot be
-emptied: 41-45 exempt it and give it somewhere to go, 46-47 hand groups
-full of trapped space to the reclaim worker even when they hold reserved or
-pinned bytes, and 40 fixes the read-only path's accounting. 62 returns the
-allocation cluster before scanning the group, a throughput fix.
-
-Data reservation (57, 60). 60 holds the admission margin across a
-preallocation; 57 counts stripes stranded behind live runs as trapped.
-
-nodatacow (58, 59). 58 narrows patch 18's private run to the window in
-which the extent may actually be written in place, so a nocow file no
-longer strands a run for its lifetime. 59 counts run lifecycles and
-strandings in sysfs.
-
-Diagnostics (54-56, 63, 64). Per-group trapped bytes in the space info
-dump (54); a metadata RMW audit that tells a dead tree block from a live
-one (55) and reads backrefs correctly (56); runs released when a mount
-fails or an aborted filesystem unmounts (63); and a line at mount saying
-whether the policy is on (64).
-
-Since the previous posting, nine fixes to earlier patches have been folded
-into the patches they correct (the parked-rbio reference into 11, the two
-admission charges into 36, the read-only guard's saturation into 40, the
-probe margin's release into 25, the metadata remainder sync and the covered
-group report into 38, the double count into 8, the preallocation hold's
-locking into 60), the mount option has been removed from the series
-outright, and the property patch moved up to 4 so the policy has its
-interface from the start.
+- Trapped space. Free space in partially filled stripes cannot be
+ allocated until the whole stripe frees. With churn, patch 10 measured
+ usable capacity of 1486-1561 MiB against stock's 1569 MiB, depending
+ on the delete pattern. Stock reaches its full capacity by reusing
+ every sub-stripe hole, which is the write hole. Balance or subtopic
+ 3's reclaim gets the space back, and statfs reports it honestly.
+ Relocating a group needs free space outside it, as with any balance.
+- Admission is pessimistic by design: it refuses early rather than admit
+ a write the allocator cannot place, so a fragmented filesystem accepts
+ less than its free bytes suggest.
+- Async small-file fills to ENOSPC pay the park deadline. When each
+ file's neighbours land in the next file's stripe, a partial write
+ waits out its deadline and the stuck cap before it pads and goes down.
+ Every write still reaches disk correctly. stripe_park_congestion
+ shortens this; it is off by default. One large writer is already full
+ stripes and never parks.
+- Under raid56 metadata (subtopic 5): the metadata total settles at
+ about 2.15x the plain allocator's with patch 81, and without reclaim
+ the trapped metadata space grows until unlinks get ENOSPC. raid1 or
+ raid1c3 metadata has none of this.
+- Up to 256 pinned stripes keep their pages in the rbio cache until
+ their runs are fully written.
+
+Open questions for review
+=========================
+
+- Keep or drop subtopic 5 (above).
+- Which fsync implementation to keep, or both (subtopic 4).
+- Whether the log-commit protection gets its own property.
+- The bg_reclaim_threshold default on non-zoned filesystems.
+- Whether the statistics files and the tracepoint stay (subtopic 6).
+
+The harness, logs and test catalog are available on request.
Zygo Blaxell (114):
btrfs: add btrfs_claim_free_stripe_run() for stripe-exclusive