btrfs: stripe_alloc: share LOG data runs per log transaction
An fsyncing inode's data goes into a private LOG-class stripe run, which
each log commit settles: the run is closed, the commit waits for all of the
run's data IO, and the extents left in its final partial stripe are
recorded for carry-forward, so the next fsync re-dirties them and moves
them into the inode's new run. Every fsync therefore leaves one partial
stripe per inode, and a back-to-back fsync loop rewrites the previous
fsync's tail each time.
Share the runs instead:
- LOG-class runs are keyed by (subvolume, log_transid at allocation)
instead of the inode, so the inodes logging in one log transaction fill
the same stripes. An allocation of a full stripe or more keeps the
inode's private run, so small fsync extents never share the partial last
stripe of a large one and wait for its IO.
- A log commit does not close a shared run. It seals the stripe holding
the end of the logged extent: the run's frontier moves to that stripe's
end, the skipped sectors go back to the free space cache like a closed
run's tail (the stripe's final write pads them), and the run goes on in
the next stripe. Nothing allocates beside the extent after its log
commit.
- The settle kicks only the logged extent's stripes and waits until every
allocated sector in them is written (the run's written map), rather than
for the whole run to drain. Writers sharing those stripes park and merge
into the same full-stripe write, so the wait couples an fsync to them and
to nothing else. It has no timeout: returning while a neighbour's sector
is unwritten would let its later partial write RMW the extent after the
log made it durable. Completed writes wake the waiters.
- Nothing is carried forward: a final partial stripe stays until the
extents in it are freed or compaction moves them.
Measured on 5 x 8 GiB raid5 data with raid1 and raid5 metadata, against
private runs with carry-forward, same rep: at 16 and 64 writers and with
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; one writer is at
parity on latency with 17-29% fewer device bytes. The one workload that
can lose is two large streaming writers beside small fsync writers: a small
fsync waits for neighbours that joined its stripe and sit behind the large
IO (0.5-1.9x the fsyncs/s; the p99.9 there is dominated by a multi-second
log-commit tail that both modes, and stock raid5, show).
/sys/fs/btrfs/<uuid>/stripe_log_data_shared (default 1) switches a
filesystem back to private runs with carry-forward. stripe_run_stats gains
log_settle_waits and log_settle_wait_ns (both), log_carry_ranges and
log_carry_bytes (private runs), and log_seals and log_seal_bytes (shared
runs).
Private LOG and NOCOW runs are matched by (subvolume root id, inode
number), since inode numbers are unique only within a subvolume (every
subvolume's first file is inode 257): matching by inode alone let
same-numbered inodes of different subvolumes join each other's runs,
breaking NOCOW isolation, and let an eviction close other subvolumes'
runs and the META_LOG run of a subvolume whose id equals the inode number.
Eviction closes only the inode's own LOG and NOCOW runs, and the shared
LOG key uses the whole root id.
btrfs_log_settle_stripes() reads stripe_log_data_shared once per settle,
and every completed write wakes the settle waiters whatever the knob says,
so a sysfs change cannot leave an fsync waiting for a run only the commit
it blocks would provide.