]> git.hungrycats.org Git - linux/commit
btrfs: stripe_alloc: give each log commit its own metadata stripe run
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 5 Oct 2026 03:16:03 +0000 (23:16 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:31 +0000 (21:16 -0500)
commit751c8aa6c3f1e03a68bdd3aeeaf3b20f7332ed87
tree1de4c0b3941e932902a92072e915c3035d3a6197
parent068437143f26d8d6f5ba3240b1c5aab6484a59c6
btrfs: stripe_alloc: give each log commit its own metadata stripe run

A metadata stripe run is keyed by transaction id alone, so every fsync log
commit inside one transaction appends at the same frontier and into the same
full stripe.  A log commit's tree blocks are durable the instant fsync
returns, so a later writer sharing that stripe -- the next log commit, or the
transaction's ordinary metadata at commit -- has its sub-stripe write
recompute parity over metadata the user was already promised.  That is the
raid56 write hole, on data a user was told was safe.  Padding cannot cover it
either: the earlier blocks are live in the run's liveness map, so
rmw_try_pad_full() must decline.

Give the log tree its own class.  A BTRFS_STRIPE_RUN_META_LOG run is keyed by
the owning subvolume's root id and that subvolume's log_transid, and is
private like the per-inode runs: never in the shared band slots, found by
walking for its key, so nothing else can join it.  Each log commit therefore
gets its own whole stripes and the sharing cannot arise, rather than being
detected and worked around after the fact.

The space cost is temporary by construction, which is the point of keying on
log_transid rather than merely separating log blocks from other metadata.  The
log tree is freed at transaction commit, so a stripe holding only one log
commit's blocks is wholly free once that happens and the commit-time rescan
credits the whole stripe back.  Measured on a 4-device raid5 filesystem with
raid5 metadata and 16K nodes under six fsync writers, trapped space falls from
a 20.2 MiB loaded peak to 1.25 MiB once the load stops and three commits pass,
which is below the 23.7 MiB the plain allocator settles at.  Metadata
total_bytes settles at 2.15x the plain allocator's, and since allocated
metadata chunks are not available to data, that multiple is the real cost on a
small or full filesystem.  Filling to ENOSPC leaves 384 KiB of df-avail, the
same figure the plain allocator gives, so nothing is over-committed.

Both directions of the sharing go to zero, and so does the disk-side witness:
an audit counting uncovered tree blocks whose stripe holds log-tree headers
reads 0 under load, against 223847 without the change.  Padding declines fall
from 36732 uncovered to 1.

Retirement handles both metadata classes through
btrfs_stripe_run_class_is_meta(), since a log run must not be closed and
waited on in the commit-time drain that settles the data classes.  Drain is by
bytenr through end_bbio_meta_write() and clean_log_buffer(), so it is
class-agnostic and covers the log tree's private free path.  A sysfs knob,
stripe_log_run, turns the class off so the change can be measured against
itself on one boot.

log_transid starts over with every transaction's log tree, so a run also
carries its transaction id, and a run from an earlier transaction is
closed rather than joined even when its log_transid matches: that
transaction's commit retires the run and waits for its blocks, and
would wait out its timeout for a block only the later transaction
writes.  Commit-heavy load (relocation of metadata groups during an fsync
storm) hit that within minutes.

Metadata runs drain only when their tree blocks are written, in a commit,
so making a raid56 metadata group read-only (relocation, scrub) commits
the current transaction first when the group has metadata runs, instead
of waiting up to commit_interval for the next one while relocation holds
reclaim_bgs_lock.

The drain waits for metadata runs, at commit and when a group goes
read-only, stay unbounded: a run that never drains has a tree block freed
or allocated without a report, an accounting bug that should show as a
hung commit where it happens rather than as later symptoms.  After 60
seconds they warn once and dump the group's runs (every run class) and
keep waiting.  Only a filesystem error ends them.

Assisted-by: Claude:claude-opus-5
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/block-group.c
fs/btrfs/block-group.h
fs/btrfs/disk-io.c
fs/btrfs/extent-tree.c
fs/btrfs/extent-tree.h
fs/btrfs/fs.h
fs/btrfs/sysfs.c