]> git.hungrycats.org Git - linux/commit
btrfs: stripe_alloc: settle a logged extent's stripes at log commit
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:17:31 +0000 (13:17 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:29 +0000 (21:16 -0500)
commit78405553b7b530e7af5f906fab9239dafe074625
tree09ef94d7c510339f004b0c1e0d95c5768bb10bb8
parentc42db3e3fd32cba1496e91508bc185fc18cfcddd
btrfs: stripe_alloc: settle a logged extent's stripes at log commit

Stripe-exclusive allocation bounds degraded-crash damage to the current
transaction, but within that window 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 read-modify-write rewrites the parity that
protects fsync 1's data.  A degraded crash during the second write tears
the first -- the write hole's shape, confined to the log window, and the
reason the fsync guarantee has so far been "loss is detectable" rather
than "completed fsyncs survive".

Close it with a case analysis.  A logged extent in a full stripe is
already safe: nothing ever writes a full stripe again, since COW never
overwrites and there are no free sectors left to allocate.  A logged
extent in a partial stripe is exposed only to future writes into that
stripe's remaining sectors -- so at log commit, close the stripe's open
run (nothing further allocates into it), kick any parked partial writes
for it, and wait for its in-flight data IO before the log super is
written.  This is the log-window analogue of invariant I2, which the
commit-time retirement provides for full commits.  Both log paths are
hooked: the fast path per extent map in log_one_extent(), and the
full-sync path where copy_items() walks new data extents (old-transaction
extents are skipped there, and are exactly the ones already settled by
their own commit).

The drain is join-free: write_done reporting needs no transaction join,
so waiting under the inode log mutex and a running transaction handle is
safe.  It is not bounded: it has no timeout and waits for data IO to
complete.  Run inflight is counted from allocation, which happens during
writeback with submission following in the same pass, so in practice the
wait is bio flight time plus the parked-write deadline that the flush
short-circuits.  (A later patch shares LOG runs per log transaction and
makes that the default; its settle waits until every allocated sector in
the logged extent's stripes is written, other writers' sectors included,
and is not bounded either.)

The cost is spatial: every fsync that logs an extent in a partial stripe
retires that stripe early, trapping its unwritten tail like any other
partially filled stripe until it frees or balance reclaims it.
Fsync-heavy workloads therefore burn a stripe tail per touched stripe
per fsync; the follow-up per-inode log runs and copy-forward relocation
exist to reclaim exactly that cost, and are optimizations on top of the
guarantee this patch completes.

On a filesystem that has gone into error the settle wait does not sleep
for IO: the log commit fails anyway, and the ordered extents in flight are
only torn down at unmount.

Assisted-by: Claude:claude-fable-5
Assisted-by: Claude:claude-fable-5-1
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/block-group.c
fs/btrfs/block-group.h
fs/btrfs/tree-log.c