Zygo Blaxell [Wed, 11 Nov 2026 04:55:31 +0000 (23:55 -0500)]
Merge 23 topics into 7.3/testing
The 7.3 lane (2026-11-08): a test/staging lane on v7.3.3 for the
end-of-year production move; 6.18 stays production and misc-next stays
the submission lane. Every 7.3/topics/* branch is ported from its
misc-next/topics/* version (backports at the topic heads re-checked
against v7.3.3: those present dropped, the rest kept). The parent order
is 6.18/testing's.
The sequential octopus conflicts in the same six files as a fresh
misc-next/testing octopus does (raid56.c, raid56.h, block-group.c,
Kconfig, sysfs.c, volumes.c), and each is resolved exactly as
misc-next/testing (and 6.18/testing) resolves it:
- raid56.c: raid56-fixes' RMW repair write-back helpers
(rmw_repair_verified() .. rmw_finish_repair_bios()) and stripe-alloc's
RMW audit helpers (rmw_peek_eb_generation() .. report_uncovered_rmw())
are both kept, in that order;
- raid56.h: raid56-fixes' btrfs_raid56_uncache_range() and
stripe-alloc's parked-rbio declarations are both kept, then
test-knobs' btrfs_set_rbio_cache_size() under CONFIG_BTRFS_DEBUG;
- block-group.c btrfs_inc_block_group_ro(): stripe-alloc's
"if (!ret) btrfs_retire_block_group_stripes(cache);" followed by
delalloc-fixes' unblock as its else branch (the merge order puts the
two sides the other way round from misc-next; the result is the same);
- Kconfig / sysfs.c / volumes.c: allocator-hints and read-policies (and
dev-stats) blocks are kept one after the other, as on the other lanes.
2026-11-08 (Zygo, nc1108): "btrfs: free queued remap chunks when balance
bails out", an independent METADATA_REMAP fix, is split out of
topics/balance-resume into its own topic, 7.3/topics/remap-balance-leak,
standalone on the base and merged right after topics/balance-resume.
topics/balance-resume also gains "btrfs: write the remaining balance limits
back to the balance item" (Zygo 2026-11-08, bl1108).
New topic 7.3/topics/stable-cherry-picks, merged first: clean picks of
upstream fixes that v7.3.3 lacks, starting with 16b8f07d1ed50 ("btrfs: fix
chattr failing on nodatasum files that have the compress flag").
topics/balance-resume gains "btrfs: apply the balance limit_min filter only
to a limit range" as its first patch (Zygo 2026-11-09, lr1109).
Also merges '7.3/topics/replace-finish-deadlock' (topics/replace-finish-deadlock: two device replace
hangs at replace finishing, bio counter waits in the bio completion workers (read repair) and
across the data csum lookup; both since v6.3; stacked on 7.3/topics/read-repair-fixes).
Also merges '7.3/topics/scrub-commit-pause' (topics/scrub-commit-pause: scrub and dev-replace drain their queued stripes and drop their commit-root paths before letting a transaction commit pass; with discard=sync they read extents the commit freed (zeros), spurious scrub errors and a cancelled replace; since v6.6; independent of the other topics).
The merge conflicts in fs/btrfs/scrub.c: the new scrub_pause_point()
goes in front of scrub_raid56_parity_stripe(), where
topics/raid56-scrub-replace adds scrub_raid56_copy_parity_rows(); both
are kept, the copy helper first.
2026-11-11 (Zygo, rm1111): 7.3/topics/remap-balance-leak is merged into
topics/balance-resume and no longer exists: its three fixes (free the
queued remap chunks, give their limit back, count METADATA_REMAP chunks as
metadata in the limit_min check) follow the limit_min-range patch, and
"btrfs: do not write queued remap chunks as processed to the balance item"
stays its own commit after the limit write-back. The tree is identical
to the gated rm1111 candidate testing 23af6191ae76, which adds the give-back,
the limit_min and the on-disk remap fixes to the previous testing.
Zygo Blaxell [Sun, 18 Oct 2026 19:50:43 +0000 (15:50 -0400)]
btrfs: allocator hints: make the available space estimates honor the hints
The chunk allocator keeps data chunks off METADATA_ONLY and NONE_ONLY
devices and metadata and system chunks off DATA_ONLY and NONE_ONLY
devices (gather_device_info()). The available space estimates do not:
btrfs_update_per_profile_avail() simulates chunk allocation over every
writable device, and statfs adds up every device's unallocated space.
The space of a device a hint excludes is counted as available although
no chunk of that type can ever be made from it.
Metadata overcommit trusts that estimate (calc_available_free_space()),
so once the devices metadata may use are full, reservations are granted
against chunks that cannot be allocated, and the transactions that use
them abort. Reproducer (raid56-wh-harness alloc-hints-space-test.sh):
raid1 data and metadata on three devices of 6, 3 and 6 GiB, devid 3 set
to NONE_ONLY (5) and emptied with a full balance, then data written
until ENOSPC (devid 2 fills while devid 1 still has 3 GiB) and small
files created until ENOSPC:
BTRFS: error (device loop2 state A) in btrfs_finish_one_ordered:3410: errno=-28 No space left
BTRFS error (device loop2 state EA): Transaction 29 aborted (-ENOSPC)
BTRFS info (device loop2 state EA): forced readonly
and, after a remount, a second abort from __btrfs_update_delayed_inode()
while the files were read back. The files written since the last
commit were lost. statfs reported 3103 MiB available at the data ENOSPC.
The same filesystem without devid 3 (6 + 3 GiB, no hints) fills
metadata to a clean ENOSPC and reports 55 MiB.
Give the estimates the allocator's view of the hints:
- btrfs_dev_alloc_hint() decides whether a chunk type may use a device
and returns its tier; btrfs_select_hint_tiers() keeps the tiers a
chunk would use. gather_device_info() now uses both instead of its
open-coded copy, so the rules live in one place.
- The per-profile estimate is kept separately for data and for metadata
and system chunks; alloc_virtual_chunk() skips the devices the hints
exclude for the type and picks hint tiers the way the allocator does.
btrfs_get_per_profile_avail() takes the block group type. Mixed block
groups ignore the hints, as in the allocator.
- statfs leaves out devices that data chunks may not use. Only the
available space changes: f_bavail (df Avail) leaves out the
unallocated space of those devices, while f_blocks (df Size) and
f_bfree (and so df Used) still count every device, including the ones
the hints keep data off. On a filesystem with a METADATA_ONLY device,
for example, Size minus Used is larger than Avail by about that
device's unallocated space.
- A type change recomputes the estimate.
PREFERRED_NONE devices still count: the allocator uses them as the last
tier. Without CONFIG_BTRFS_ALLOCATOR_HINTS the data and metadata
estimates are identical and only one is computed.
The estimates have been hint-blind since "btrfs: add allocator_hint
mode", earlier in this series, taught the allocator the hints.
The fallback to fs_info->free_chunk_space (zoned filesystems, or a
failed estimate) and the reclaim thresholds that read it stay hint-blind.
With this change the reproducer fills metadata to a clean ENOSPC with no
abort and no lost files, and statfs reports the space data can really
use.
Zygo Blaxell [Sun, 4 Oct 2026 14:55:00 +0000 (10:55 -0400)]
btrfs: allocator hints: don't run a transaction from the type store
btrfs_devinfo_type_store() starts and commits a transaction while kernfs
holds the attribute active. btrfs_rm_device() removes the device's
sysfs directory, draining that attribute, while it holds its own
transaction handle and fs_devices->device_list_mutex. A type write
racing a device remove can deadlock:
rm_device: transaction handle, device_list_mutex -> drain devinfo/<id>/type
type store: devinfo/<id>/type active -> commit waits for rm_device's handle
Lockdep reports the cycle (kn->active -> btrfs_trans_num_writers ->
btrfs_trans_num_extwriters -> device_list_mutex) with a type writer
looping during 'btrfs device remove'. Joining the transaction without
committing does not help: a chunk allocation during the dev item update
makes the end of the transaction take device_list_mutex.
Do what the label and feature stores do and keep transactions out of
sysfs. The store sets device->type under chunk_mutex (which also orders
it against chunk allocation and against the end of a device replace),
marks the device BTRFS_DEV_STATE_TYPE_DIRTY and requests a commit.
btrfs_update_dirty_device_types() writes the dirty dev items at the start
of the next commit, under device_list_mutex, after the transaction
handle, the same order as btrfs_rm_device() and btrfs_run_dev_stats(), with the
chunk tree space reserved beforehand (btrfs_reserve_chunk_metadata(), as
btrfs_grow_device() does; it takes chunk_mutex, so before the mutex). A
dev item already deleted by a device remove is skipped. A dev item that
cannot be written stays dirty, with a rate-limited warning, and is retried
at the next commit: a hint is not worth aborting the transaction for. The
hint takes effect for new chunks at once and reaches the disk at the next
commit, sync or unmount.
Taking chunk_mutex in the store also closes the race at the end of a
device replace: the finish moves the hint and the dirty bit to the
target under the same mutex, so a hint change on the source either
reaches the target or, once the source has the replace devid, returns
-EBUSY. The replace target (devid 0, no dev item) still returns -EBUSY.
Also check sb_rdonly() before BTRFS_FS_OPEN, so a read-only mount
reports -EROFS rather than -EBUSY, and refuse seed devices, which are
read-only and not written by the commit-time update.
The store orders the new type before the dirty bit
(smp_mb__before_atomic(); the commit's test_and_clear_bit() is the other
half), and btrfs_update_device() reads the type with READ_ONCE(), so the
commit cannot write the old type and clear the bit on a weakly ordered
machine.
Zygo Blaxell [Sun, 4 Oct 2026 04:44:58 +0000 (00:44 -0400)]
btrfs: allocator hints: keep the hint across device replace
Device replace drops the allocation hint, and changing a hint during a
replace can abort the transaction.
The replace target takes over the source's dev item when the replace
finishes, but not the in-memory device->type. The on-disk item keeps the
hint only until the next btrfs_update_device() on the new device (any
chunk allocation on it), which writes type 0 over it. Copy the type
together with the devid, under the same locks.
During a replace, the target is visible as devinfo/0 and has no dev item.
After the finish, the source's devid is BTRFS_DEV_REPLACE_DEVID until its
sysfs directory goes away. A write to 'type' on either device makes
btrfs_update_device() miss the dev item (-ENOENT), and the store aborts
the transaction, forcing the filesystem read-only in the middle of the
replace. Device remove has the same window between deleting the dev
item and removing the sysfs directory.
Return -EBUSY for the replace target and for a device with the replace
devid. An -ENOENT that still gets past these checks (remove, or a
replace that finishes after them) ends the transaction without aborting
it and returns -ENODEV.
The source's hint can still be changed during a replace, and the target
inherits it at the finish, so a replace can move to a device of a
different type, e.g. SSD to HDD. A write that races the finish can be
lost: it misses the dev item and returns -ENODEV, or the finish has
already copied the old type. That is tolerable, and the next patch
closes the window.
Zygo Blaxell [Sun, 4 Oct 2026 04:44:02 +0000 (00:44 -0400)]
btrfs: allocator hints: define the hint values unconditionally in the UAPI header
The BTRFS_DEV_ALLOCATION_* values sit in include/uapi/linux/btrfs_tree.h
inside #ifdef CONFIG_BTRFS_ALLOCATOR_HINTS, to keep them away from
userspace until the interface is settled. A CONFIG_ symbol in a UAPI
header is rejected by scripts/headers_install.sh:
error: include/uapi/linux/btrfs_tree.h: leak CONFIG_BTRFS_ALLOCATOR_HINTS to user-space
so 'make headers_install' (and the deb/rpm package targets, which run
it) fail with the patch set applied.
The values are on-disk format: they give the meaning of the low bits of
dev_item.type, and userspace (btrfs-progs) needs them to read and set a
device's hint. Defining them changes no userspace-visible structure.
So drop the guard and always export the values. The code that acts on
the hints stays under CONFIG_BTRFS_ALLOCATOR_HINTS.
Zygo Blaxell [Wed, 11 Nov 2026 03:12:37 +0000 (22:12 -0500)]
btrfs: scrub: do not let a transaction commit pass under queued stripes
Scrub and dev-replace find extents in the commit root of the extent tree,
through paths kept in the scrub context for the whole block group
(sctx->extent_path, sctx->csum_path), and fill up to SCRUB_TOTAL_STRIPES
stripes before the last of them is read. A transaction commit waits in
btrfs_scrub_pause() until the scrub reaches the top of the
scrub_simple_mirror() loop, and then switches the commit roots and unpins
the extents the transaction freed before btrfs_scrub_continue(). With
discard=sync btrfs_finish_extent_commit() also discards them.
Nothing on the scrub side notices. The stripes it queued before the pause
are read afterwards, and the search continues in a leaf of the old commit
root, so the scrub reads extents that are no longer allocated. Without
discard that is harmless: the block group is read-only while it is
scrubbed, so the freed range is not reused and still holds what the scrub
expects. With discard=sync the range reads back zeros on every mirror:
BTRFS warning (device loop0): scrub: tree block 65617920 mirror 1 has bad bytenr, has 0 want 65617920
BTRFS error (device loop0): scrub: unable to fixup (regular) error at logical 65617920 on dev /dev/loop0 physical 65617920
BTRFS error (device loop0): scrub: stripe 65601536 has unrepaired metadata sector at logical 65617920
BTRFS error (device loop0): btrfs_scrub_dev(/dev/loop0, 1, /dev/mapper/rddelay) failed -5
A scrub reports uncorrectable errors on a healthy filesystem, and a
dev-replace cancels on the first such metadata sector. The freed blocks
are not referenced by anything, so no reader of the filesystem sees them;
btrfs check and a second scrub after the churn are clean.
Before a pause, finish the queued stripes and release both paths, so that
a commit can only pass with nothing of the scrub's still referring to the
old commit root, and the next search starts from the new one. The parity
scrub of RAID56 has its own paths, but its pause point is reached with data
stripes queued too, so it uses the same helper.
The stale path came with 1dc4888e725d ("btrfs: scrub: avoid unnecessary
extent tree search preparing stripes"); the queued stripes are older.
scrub-stale-extent-test.sh (raid56-wh-harness) fills a raid1 filesystem
(nodesize 64K) with 100000 small files, mounts it with discard=sync, and
runs a scrub or a replace while the files are rewritten, deleted and
renamed and the filesystem is synced every 50 ms. On v7.3.3 10 of 10
replaces cancel and 5 of 10 scrubs report uncorrectable errors; with this
patch 0 of 30 each. Without discard, or with discard=async, nothing shows.
Hengyu Liang [Sun, 11 Oct 2026 15:58:15 +0000 (11:58 -0400)]
btrfs: fix chattr failing on nodatasum files that have the compress flag
Commit cf1afec09e9f ("btrfs: validate properties before setting them")
made btrfs_set_prop() validate a property before setting it. For the
compression property, the validation fails with -EINVAL when the inode
has NODATASUM.
However, chattr writes back all the flags it read from the file, and
btrfs_fileattr_set() sets the compression property whenever the new
flags include FS_COMPR_FL, even when the compression flags do not
change. A file created on a filesystem mounted with nodatasum has
NODATASUM and still inherits the compress flag from its directory. As
of now, chattr fails on such a file when it changes any other flag.
Before commit cf1afec09e9f ("btrfs: validate properties before setting
them"), the last command succeeds.
After that commit, it fails with EINVAL.
This patch will make btrfs_fileattr_set() leave the compression flags
and the compression property alone when the call changes neither
FS_COMPR_FL nor FS_NOCOMP_FL.
Fixes: cf1afec09e9f ("btrfs: validate properties before setting them") CC: stable@vger.kernel.org # 7.2+ Suggested-by: Qu Wenruo <wqu@suse.com> Assisted-by: LLM Signed-off-by: Hengyu Liang <hengyul@cs.unc.edu> Reviewed-by: Qu Wenruo <wqu@suse.com> Reviewed-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
(cherry picked from commit 16b8f07d1ed50d4a8da468b56484873784dd8f4a)
Zygo Blaxell [Mon, 9 Nov 2026 01:40:09 +0000 (20:40 -0500)]
btrfs: do not hold the bio counter across the data csum lookup
Device replace can hang forever at the end, after "replace status"
already reports it finished, when data is read while it finishes.
This happens with any RAID profile:
btrfs_submit_chunk() takes the bio counter before mapping the bio, and
for a data read it then looks up the data checksums. When the csum tree
leaf is not cached, the lookup reads it, and that read goes through
btrfs_submit_chunk() and btrfs_bio_counter_inc_blocked() again. If
btrfs_dev_replace_finishing() has set BTRFS_FS_STATE_DEV_REPLACING in
between, the reader sleeps on the second reference while it holds the
first, and replace finishing waits for the first to be dropped. Every
other bio submission then blocks behind them.
It was reproduced on a 4-device RAID5 with one device missing,
mounted with thread_pool=64, 8 O_DIRECT readers and 2 writers:
replacing the missing device hung in round 7. The counter was 2, and
exactly two readers were sleeping in the nested
btrfs_bio_counter_inc_blocked() under btrfs_lookup_bio_sums().
The counter only keeps the result of btrfs_map_block() valid until the
bio is submitted. So drop it, and the mapping, for the csum lookup, take
it again afterwards and map the bio again. That costs one more
btrfs_map_block() call per data read bio. The mirror picked by the first
mapping is passed back in, so the bio is mapped to the same copy.
[zygo: 7.3 backport: clean pick on top of the previous patch.]
Fixes: 1c2b3ee3b0ec4 ("btrfs: pre-load data checksum for reads in btrfs_submit_bio") Assisted-by: Claude:claude-opus-5-5
Zygo Blaxell [Tue, 10 Nov 2026 04:47:27 +0000 (23:47 -0500)]
btrfs: do not wait for device replace in the bio completion workers
Device replace can hang forever at the end, after "replace status"
already reports it finished, when data that needs read repair is read
while it finishes:
and every other task submitting bios to the filesystem blocks behind
them.
btrfs_dev_replace_finishing() sets BTRFS_FS_STATE_DEV_REPLACING and
waits for the bio counter to drop to zero. Every bio that
btrfs_submit_chunk() counted keeps its reference until its completion
runs: a RAID56 bio until btrfs_raid56_end_io() runs in the btrfs-rmw
worker that completes its rbio, a mirrored write until
orig_write_end_io_work() runs in btrfs-endio. Both workqueues run at
most thread_pool workers at a time.
Read repair runs in those same workers: btrfs_check_read_bio() is called
by simple_end_io_work() in btrfs-endio and by btrfs_raid56_end_io() in
btrfs-rmw. It takes the bio counter when it submits a repair read to
the next mirror (repair_one_sector() and btrfs_end_repair_bio(), through
btrfs_submit_chunk()), and when it writes the good copy back
(btrfs_repair_bbio_failure()). While replace finishing has the flag
set, both wait for replace to finish. Once every worker of the pool
waits there, the counted bios queued behind them never complete, and
replace never finishes.
This is easy to hit when a missing device is replaced while its data is
read: each such read fails, is repaired by a rebuild read through
btrfs-rmw, and writes the rebuilt block back to the replace target. On
a 4-device RAID5 with one device missing, 8 O_DIRECT readers and 2
writers, it hangs in about half the replaces. At one hang the counter
was 15: 2 rebuild rbios queued on btrfs-rmw behind the 8 workers waiting
in the write-back, and 13 rebuild rbios waiting for the stripe locks
those 2 hold, one bio each. On RAID6, a repair read that rebuilds a
sector from P and fails its csum goes on to rebuild from Q, and that
submission waits the same way: with the P strips corrupted, the same
test hung in the first replace, with all 8 btrfs-rmw workers in
btrfs_end_repair_bio() -> btrfs_submit_bbio() and a counter of 68 = 23
rebuild rbios queued on btrfs-rmw + 45 waiting for their stripe locks.
Run the parts of read repair that take the bio counter in a new
workqueue, btrfs-repair, that no bio completion waits for. A worker
that waits there for replace to finish holds nothing replace waits for:
the repair bbio is not submitted yet, and the read being repaired is
held back by fbio->repair_count, which is not counted.
As before, the read being repaired completes once the write-back is
done; while replace finishes, that is once replace has finished.
[zygo: 7.3 backport: on 7.3/topics/read-repair-fixes, whose head backports
Qu Wenruo's 15ed4a938b30 (btrfs_repair_bbio_failure()); fs.h and disk-io.c
differ in context only (endio_freespace_worker).]
Fixes: 7609afac67754 ("btrfs: handle checksum validation and repair at the storage layer") Assisted-by: Claude:claude-opus-5-5
Zygo Blaxell [Tue, 13 Oct 2026 18:24:01 +0000 (14:24 -0400)]
btrfs: keep the old resume filters for balance items without a vrange
This patch is optional. It is offered so that reviewers can take it or
drop it; the author's preference is to drop it. The previous patches do
not depend on it.
A balance item written by a kernel without the vrange bound has no vrange
filter. With the previous patch alone, a balance that was paused or
interrupted on such a kernel and is resumed on a new one restarts from
the beginning: every block group the original request selects is
relocated again, including the ones the interrupted run created.
This patch restores the old resume adjustments for exactly those items.
When a chunk type has no vrange filter in the stored item, the resume
changes its arguments as before: it adds the soft filter if the type is
being converted, and usage=90 if it has no usage, usage range or convert
filter. This is decided when the item is recovered at mount, before
the previous patch adds the bound, and is written back with the bound.
A chunk type with a vrange, which is every balance started by a kernel
with the previous patch and any balance the user gave a vrange, relies
on the vrange and is resumed unchanged.
The trade-off: the legacy resume neither completes the original request
(block groups more than 90% full are skipped, and so are the ones being
converted that already had the target profile) nor avoids much work on a
filesystem that is more than 90% full, and keeping it adds a layer of
surprising behavior and code for a case that only arises once, when a
balance spans the kernel upgrade. Without it, such a balance repeats
work but skips nothing.
Zygo Blaxell [Wed, 11 Nov 2026 05:32:48 +0000 (00:32 -0500)]
btrfs: do not write queued remap chunks as processed to the balance item
The balance item gets the remaining limits after each processed chunk
(count_limited_chunk()). METADATA_REMAP chunks are not processed in the
chunk walk: __btrfs_balance() queues them and balances them after the
walk, but should_balance_chunk() has already counted them down from the
metadata limit when it selected them. Any limited chunk processed after
that writes the item with the queued chunks counted as done. If the
balance stops before the end of the walk, by a pause, an unmount or a
crash, the queue is gone, and the balance resumed from the item has too
little left of its metadata limit to balance the chunks it never
balanced: with -mlimit=1..1 and the remap chunk selected as the one
metadata chunk, the resumed balance balances no metadata chunk.
Write the item with the queued chunks added back to the metadata limit
(limit, or limit_max of a range, as should_balance_chunk() counted them).
The in-memory limit is not changed: the walk needs it to stop selecting
chunks once the limit is used up.
Zygo Blaxell [Mon, 9 Nov 2026 03:24:59 +0000 (22:24 -0500)]
btrfs: write the remaining balance limits back to the balance item
The limit filters are counted down in memory: should_balance_chunk()
decrements limit, or limit_max of a limit_min..limit_max range, for every
chunk that passes the other filters, and __btrfs_balance() restores them
after its counting pass. A balance resumed after a pause continues with
what is left. The balance item is never updated, so a balance resumed
after an unmount or a crash starts again from the limits that were given:
every resume can relocate up to limit more chunks. The minimum shifts
too: the counting pass of the resume sees only the chunks that are left,
which can be fewer than limit_min, and the resume then relocates nothing.
Count a chunk against both ends of the range, and write the remaining
limits to the balance item, once the chunk has been processed. limit and
limit_max are still decremented when the chunk is selected, as before,
and limit_min is now decremented (down to 0) when it has been processed,
after the limit_min check has let it through, so limit_min means the
remaining minimum, on disk and in memory. A resume after an unmount or a
crash then continues from the remaining limits, like a resume after a
pause, and its limit_min check compares the chunks that are left with the
minimum that is left. The values written are those of the real pass:
the counting pass returns to the loop before any chunk is processed, and
its decrements are undone before the real pass starts.
A chunk counts as processed when its relocation has returned for that
chunk: relocated, or left in place by ENOSPC or an active swapfile. This
is what the in-memory limit has always counted, so a resume after an
unmount or a crash now behaves exactly like a resume after a pause. A
pause cannot stop a chunk halfway: the pause request is checked between
chunks, so the chunk being relocated is processed and written first. A
cancel or a fatal signal can stop a relocation, but it also ends the
balance and deletes the item. If the item cannot be written, the balance
stops with the error, like any other error in the loop, and its item is
deleted; if the item survives that (an aborted transaction), it holds
the last committed limits, within the bound below.
The update takes its own handle on the running transaction once the
relocation has returned, and ends it without committing. The
transaction that removed the relocated chunk has ended by then, and is
usually still the running one, so both changes usually commit together,
and the update can never commit before the removal. A crash in between
loses the count of that one chunk, and the resumed balance can relocate
one chunk more than requested, never one fewer. Making the update part
of the removal's transaction would mean passing the balance into
btrfs_relocate_chunk(), which device shrink and removal also use.
Only chunk types with a limit filter write the item, once per processed
chunk; a balance without limits makes no more writes than before. The
remaining limits are visible: the BTRFS_IOC_BALANCE_PROGRESS ioctl
already returned them during the balance and now returns the same values
after a remount, and the "balance: resume" log line shows them.
btrfs: replace the balance resume heuristics with a vrange bound
When a balance is resumed after a pause or an unmount,
update_balance_args() adds a usage=90 filter to every chunk type that is
not being converted. That changes the set of block groups the original
request selected. It skips full block groups the interrupted balance had
not reached yet, so the resumed balance can finish without having done
what was asked, and it selects again any block group the interrupted
balance created that is less than 90% full, relocating the same data a
second time. For the chunk types that are being converted it adds the
soft filter instead, so the resume does not convert again the block
groups the interrupted balance converted, but it also skips the ones
that already had the target profile when the balance started, which the
original request selected.
Replace it with a bound fixed when the balance starts. Before the
balance item is written, limit the vrange filter of each chunk type to
the addresses below the next chunk to be allocated: add a 0..end range
when the request had no vrange filter, or lower the end of the one it
had. Every block group that existed when the balance started stays
selected until the balance relocates it.
New chunks are allocated after the last chunk, but relocation can remove
the highest chunks without allocating any (their data fits in free space
lower down), and the next chunk would then reuse their addresses, below
the bound. A resumed balance would select it again, although the
interrupted one, walking downward, never would have. So while a balance
item exists, find_next_chunk() never returns an address below the bound:
an in-memory floor, set when the balance starts and when the balance
item is recovered at mount, and cleared when the item is deleted. Every
chunk allocated in that time, relocation targets included, stays outside
the range, on the first run and after a resume. The gap the floor can
leave in the logical address space is harmless.
A resume now uses the arguments exactly as they were stored: the bound
already keeps out every block group the interrupted balance created,
converted ones included, so neither the usage filter nor the soft filter
is needed, and update_balance_args() goes away.
The bound does not move as the balance progresses. Block groups the
balance has relocated are gone, so on resume the walk only revisits the
chunks it left in place above the point where it stopped (filtered out,
ENOSPC, active swapfile) and checks them against the filters again.
Nothing is skipped.
Because the bound is written with the balance item and never changes,
there is no progress to persist: a resume after an unmount or a crash
needs no extra writes.
A balance item recovered at mount is limited in the same way before the
floor is set from it: a vrange 0..end is added to each chunk type that
has none, and a vrange that ends above the next chunk is lowered to it.
That covers items written by older kernels, which have no vrange or a
vrange the user gave, which can end anywhere (a 1 TiB end would put
every new chunk at 1 TiB and above). A bound written by this kernel
changes only when relocation removed the highest chunks and allocated
none above it; no chunk exists between the next chunk and the old end,
so lowering it drops nothing. When the recovered arguments change,
they are written back to the balance item once, when the balance
resumes, so a resume that is interrupted again resumes with the same
bound and not with a new one that includes the chunks the first resume
allocated. If that write fails, the balance is not resumed: as for the
other errors at that point, it ends with the error logged, and its item
is deleted. Nothing has been relocated yet, so if the item survives (the
delete fails too, or the system crashes first), the next mount bounds it
again to the same chunks.
A balance item written by an older kernel therefore resumes with the
bound and without the usage or soft filter: the resumed balance restarts
from the beginning, relocating again every block group the request
selects, including the ones the interrupted run created or converted,
but it skips none.
The bound is visible: `btrfs balance status -v`, the
BTRFS_IOC_BALANCE_PROGRESS ioctl and the "balance: start" and "balance:
resume" log lines show a vrange the user did not give.
Zygo Blaxell [Wed, 11 Nov 2026 05:32:48 +0000 (00:32 -0500)]
btrfs: count METADATA_REMAP chunks as metadata in the balance limit_min check
should_balance_chunk() already treats a METADATA_REMAP chunk as a
metadata chunk: the metadata type flag selects it, the metadata filters
apply to it, and it is charged against the metadata limit or limit_max
when it is selected, so the remap chunks queued by __btrfs_balance()
stay within the MAX of -mlimit=MIN..MAX. The counting pass of
__btrfs_balance() also counts it in count_meta.
The limit_min check in the second pass is the only place that tests the
raw METADATA bit, which a METADATA_REMAP chunk does not have. With
-mlimit=MIN..MAX and fewer than MIN matching metadata chunks, the
balance skips every METADATA chunk, as asked, but still relocates the
METADATA_REMAP chunk.
Test the METADATA_REMAP bit in that check as well, so that remap chunks
count as metadata in both passes and the limit bounds the total number
of block groups relocated.
Zygo Blaxell [Wed, 11 Nov 2026 05:32:47 +0000 (00:32 -0500)]
btrfs: give back the limit of queued remap chunks when balance bails out
__btrfs_balance() does not relocate METADATA_REMAP chunks in its chunk
walk: it queues them and balances them all after the walk, in
balance_remap_chunks(). should_balance_chunk() selects them as metadata
chunks, and a metadata limit filter counts them down when it selects
them, like any other chunk.
A pause request ends the walk at the error label, which drops the queue:
the queued chunks are not balanced, but the metadata limit still counts
them. The balance control stays in memory for the resume, which then
sees the chunks again and finds the limit already used up for them. For
example, with -mlimit=1 and the remap chunk selected as the one metadata
chunk, a balance paused while it relocates the data chunks below the
remap chunk relocates no metadata chunk at all after the resume.
Add the dropped chunks back to the metadata limit (limit, or limit_max of
a range, whichever should_balance_chunk() counted down) at the error
label. For the other ways out through the label (cancel, errors) the
balance ends and the count does not matter.
Jeff Layton [Thu, 5 Nov 2026 20:07:45 +0000 (15:07 -0500)]
btrfs: honor FMODE_NOCMTIME when cloning
btrfs_remap_file_range() calls generic_remap_file_range_prep() through
btrfs_remap_file_range_prep(). The file_modified() call in
generic_remap_file_range_prep() skips the c/mtime update for
FMODE_NOCMTIME files, but btrfs_clone_files() then always tells
btrfs_clone() to update the c/mtime of the destination.
nfsd sets FMODE_NOCMTIME while a client holds delegated timestamps. Thus
a CLONE from the holder advances the server's ctime, and the times that
the holder returns cannot move it back.
Pass no_time_update for FMODE_NOCMTIME destinations, as dedupe already
does. Make btrfs_should_update_cmtime() available to reflink.c.
Jeff Layton [Thu, 5 Nov 2026 20:07:44 +0000 (15:07 -0500)]
btrfs: honor FMODE_NOCMTIME in fallocate
btrfs_fallocate() calls file_modified(), which skips the c/mtime update
for FMODE_NOCMTIME files. But the helpers that btrfs_fallocate() calls
update the times anyway:
- btrfs_prealloc_file_range() sets ctime. Its
insert_prealloc_file_extent() tells btrfs_replace_file_extents() to
update c/mtime.
- btrfs_fallocate_update_isize() sets ctime.
btrfs_zero_range() also calls these helpers. nfsd sets FMODE_NOCMTIME
while a client holds delegated timestamps. Thus an ALLOCATE from the
holder advances the server's ctime, and the times that the holder
returns cannot move it back.
Pass an update_times flag from btrfs_fallocate() to these helpers. Set
the flag from btrfs_should_update_cmtime(). Relocation still updates the
times.
btrfs_fallocate_update_isize() never incremented i_version. When a size
extension stayed within existing preallocation, only the ctime update
changed the NFS change attribute. Increment i_version unconditionally
there, as btrfs_prealloc_file_range() does.
[zygo: 7.3 port: 7.3 still has the __btrfs_prealloc_file_range() wrapper
and btrfs_prealloc_file_range_trans() for the v1 space cache.
update_times goes through insert_prealloc_file_extent() and
__btrfs_prealloc_file_range(); btrfs_prealloc_file_range_trans() passes
true, so the v1 cache inode keeps updating its times as before]
Jeff Layton [Thu, 5 Nov 2026 20:07:43 +0000 (15:07 -0500)]
btrfs: honor FMODE_NOCMTIME when punching holes
btrfs_punch_hole() calls file_modified(), which skips the c/mtime update
for FMODE_NOCMTIME files. But btrfs_punch_hole() then updates c/mtime
itself in two places: after it replaces the extents, and in the
partial-block path. Also, when extent_info is NULL,
btrfs_replace_file_extents() updates c/mtime for each transaction.
nfsd sets FMODE_NOCMTIME while a client holds delegated timestamps. Thus
a DEALLOCATE from the holder advances the server's ctime, and the times
that the holder returns cannot move it back.
- Add btrfs_should_update_cmtime() and use it in btrfs_write_check() too.
- Replace btrfs_replace_extent_info::update_times with an update_times
argument to btrfs_replace_file_extents(). This lets the NULL
extent_info (hole punch) case skip the update.
- In btrfs_punch_hole(), skip the c/mtime updates for FMODE_NOCMTIME
files.
The reflink caller that passes a NULL extent_info for a trailing hole
now also honors no_time_update, as clone_finish_inode_update() does.
Jeff Layton [Thu, 5 Nov 2026 20:07:42 +0000 (15:07 -0500)]
btrfs: honor FMODE_NOCMTIME in btrfs_write_check()
btrfs_write_check() open-codes the c/mtime update and checks only
IS_NOCMTIME(inode). file_update_time() also skips the update when the
file has FMODE_NOCMTIME. nfsd sets this flag on files that have a
delegation with delegated timestamps.
Thus, on a btrfs export, WRITEs from the delegation holder update the
server's c/mtime. When the holder returns its times, nfsd restores the
mtime. nfsd cannot move ctime backward, so ctime stays newer than the
time that the holder reported. Over NFSv4.2, git test t1092 finds a
stat-dirty file.
Zygo Blaxell [Thu, 1 Oct 2026 07:54:55 +0000 (03:54 -0400)]
DO-NOT-UPSTREAM: btrfs: raid56: debug knob for the rbio stripe cache size
Test fixture, not for submission. A read-modify-write whose stripe is
still in the rbio stripe cache takes the other strips from memory instead
of reading them, so a test that injects faults into reads (dm-stripfault)
cannot reach the RMW read path for recently written stripes.
With CONFIG_BTRFS_DEBUG, /sys/fs/btrfs/<uuid>/debug/rbio_cache_size
replaces the RBIO_CACHE_SIZE limit (default unchanged, 1024); 0 disables
the cache, except for the stripes stripe_alloc pins there until their run
has written all of them. Lowering it empties the cache, and cache_rbio()
passes the limit to rbio_cache_prune(), which prunes as far as needed, so
a lowered limit holds.
It sits in topics/test-knobs, on top of topics/stripe-alloc, whose rbio
pins and cache prune it adapts to.
Zygo Blaxell [Thu, 22 Oct 2026 08:02:53 +0000 (04:02 -0400)]
DO-NOT-UPSTREAM: btrfs: let queued exclusive operations in between block group reclaims
DO-NOT-UPSTREAM: this works around the reclaim worker starving queued
exclusive operations. Whether upstream wants to do anything about that
starvation, and what, is upstream's decision; this stays on our lanes.
The block group reclaim worker runs under the balance exclusive operation.
At the fill edge, with writers active, each relocation takes tens of
seconds (2 to 61 s measured) and the list keeps refilling, so the
worker held the operation for most of the time writers ran: 84-90% of a
600 s churn in reclaim-futile-test.sh, and for an hour of fsstress in the
octopus P1 pass, where a resize and a replace could not start.
btrfs-progs makes this worse than a plain refusal: before it calls the
ioctl it reads sysfs exclusive_operation. Without --enqueue it gives up
there; with --enqueue it waits for the notification that
btrfs_exclop_finish() sends and then calls the ioctl. Either way the
kernel never sees a refusal, and the worker retakes the operation right
after each group, before a waiter can get in: on the lane a queued resize
waited the whole churn (0 of 40 one-shot resizes succeeded, the longest
refusal 601 s).
After each group that was relocated (or failed to be), release the
operation (btrfs_exclop_finish() sends the sysfs notification) and, if
more groups are queued, leave it free for BTRFS_RECLAIM_GAP (12 s)
before taking it back; if someone took it meanwhile, leave the rest of
the list for a later run. A group that was skipped or dropped (the
backoff, or the checks before relocation) held the operation only
briefly and gets no gap. Neither does a filesystem that is closing or
has failed: the worker only drops the rest of the list there, and
close_ctree() waits for it in cancel_work_sync(). Nor does the last
group a limited call may reclaim (the zoned RECLAIM_ZONES flush): the
loop ends there and releases the operation anyway. 12 s comes from
btrfs-progs: after the notification, check_running_fs_exclop() reopens
the file and sleeps in a second select() for (remaining % 10) + 1 s "to
randomize the order" before it re-reads the operation, so a waiter can
take up to 10 s, plus wakeup latency under load, to get to the ioctl;
with a 5 s gap about half the queued waiters woke after the worker had
retaken the operation and waited another group. A progs change that
re-reads right after the notification (and randomizes only when it loses
a race) would allow a much shorter gap. With nobody waiting the gap
costs only reclaim throughput: reclaim resumes after it.
A caller that does reach btrfs_exclop_start() while the worker holds
the operation (an ioctl user that does not pre-check sysfs) opens a
BTRFS_RECLAIM_YIELD_WINDOW (60 s, longer than such a caller's retry
interval and short next to the five-minute reclaim hysteresis) in which
the worker neither starts nor takes another group. Both are backoffs
only: no drain or accounting wait is bounded by them.
A group in progress is not interrupted, so a queued operation waits for
the rest of the current group plus at most one gap. Measured (before
the gap was limited to relocated groups) on a stripe_alloc filesystem,
whose trapped space keeps the reclaim list full at the fill edge, with
reclaim-futile-test.sh (6 x 1 GiB raid5 at the fill edge, 128 MiB per
member added, "all" written to stripe_reclaim_now, 600 s of fsstress, a
no-op `btrfs filesystem resize` every 15 s and a `--enqueue` one every
60 s), three traced runs per kernel:
without this patch: the worker held the operation for a whole sweep
(184, 176 and 616 s); the first queued resize waited 184, 176 and about
655 s, i.e. for every queued group; one-shot resizes 27/40, 28/40, 0/40.
with it: every queued resize got in at the first hand-off after it
arrived (one sat through three short groups, 23 s), 0.3-9.4 s after the
worker's finish; longest queued waits 82, 11 and 23 s (the 82 s one sat
behind a single 127 s group); one-shot resizes 12/40, 31/40, 27/40.
Reclaim kept its pace: 10, 10 and 14 groups per 10 minutes without the
patch, 13, 11 and 10 with it.
Zygo Blaxell [Wed, 21 Oct 2026 16:48:13 +0000 (12:48 -0400)]
btrfs: relocation: flush fully before failing a merge reservation
merge_reloc_root() refills the relocation block reserve before each step
of the merge. Since 44d354abf33e ("btrfs: relocation: review the call
sites which can be interrupted by signal") it does so with
BTRFS_RESERVE_FLUSH_LIMIT. FLUSH_LIMIT only flushes delayed items and
tries to allocate a chunk. It does not run delayed refs, does not flush
delalloc, does not commit the transaction to unpin what the merge and
everybody else freed, and never falls back to the global reserve. A
failure here is fatal: merge_reloc_roots() handles it with
btrfs_handle_fs_error() and the filesystem goes read-only.
The merge routinely needs more than prepare_to_merge() reserved for it,
and refills from free space. On a filesystem with every device
allocated, metadata free space is all in other tasks' reservations, and
the refill loses:
BTRFS: error (device dm-6) in merge_reloc_roots:2053: errno=-28 No space left
BTRFS info (device dm-6 state E): forced readonly
BTRFS error (device dm-6 state EA): error relocating chunk 14022541312: -28
This is the reclaim worker relocating a data block group of a 6-device
raid5 filesystem (metadata raid1) with 6 MiB unallocated, under
fsstress. A trace of the failing merge: prepare_to_merge() reserved 704
KiB, the merge used it up and refilled 14 times (560 KiB more), and the
15th refill, for 32 KiB, failed. At that moment the metadata space_info
(256 MiB) had 160 MiB used, 9 MiB pinned and 86 MiB in may_use, of which
27 MiB was the untouched global reserve: 16 KiB free. Background
reclaim is optional work; it took the filesystem down.
Refill with BTRFS_RESERVE_FLUSH_EVICT instead.
Why FLUSH_LIMIT was chosen. This refill used FLUSH_ALL from 08e007d2e577 ("Btrfs: improve the noflush reservation") until 44d354abf33e. That commit changed it only so that a fatal signal cannot
interrupt the merge: FLUSH_ALL waits for its ticket killably and
returns -EINTR, which here would turn a killed balance into a read-only
filesystem. Its message already notes that the change "can be
ENOSPC-prone", and it shrank min_reserved to compensate. No deadlock
was given as a reason.
Why not another mode. No existing mode is "run delayed refs and commit,
but neither flush delalloc nor steal, and do not wait killably":
- FLUSH_LIMIT does not run delayed refs, which is what the merge refills
that needed flushing in testing needed.
- FLUSH_ALL and FLUSH_ALL_STEAL flush delalloc and commit too, through
the async reclaim worker, and wait killably. That brings back the
-EINTR that 44d354abf33e removed.
- FLUSH_EVICT is the priority flusher with the full list: delayed items,
delayed refs, delalloc, chunk allocation and a commit, in that order,
run in the caller's context. It stops at the first state that serves
the ticket, does not wait killably, and steals from the global reserve
only after all of them have failed.
In testing, every merge refill that needed flushing was served by the
delayed-ref states (5 of 6 tickets; the sixth needed only delayed
items). None reached delalloc, the commit or the steal. Those states
run only where the old code forced the filesystem read-only.
Deadlock analysis. At the refill the merging task holds:
- No transaction handle. The previous step ended its transaction with
btrfs_end_transaction_throttle(), and the first step has not started
one, so current->journal_info is NULL, as FLUSH_EVICT requires.
- No tree locks. The path into the reloc tree holds references only
(the reloc tree is walked without locks, and btrfs_unlock_up_safe()
dropped the locks of the resume search). replace_path() unlocks the
subvolume tree before it returns.
- Not reloc_mutex. merge_reloc_roots() takes it only around its list
splices.
- These locks from its callers: cleaner_mutex (do_nonremap_reloc()),
reclaim_bgs_lock, the exclusive operation, and sb_start_write for a
data block group. Writeback, ordered extent completion and
transaction commit take none of them. The other takers of
reclaim_bgs_lock either use trylock (unused block group deletion, the
reclaim worker) or are relocation entry points (balance, system chunk
relocation, device shrink, zoned repair). Those take it with no
transaction handle held, and the exclusive operation serializes them
with this one anyway. The other takers of cleaner_mutex are the
cleaner (trylock), mount and unmount (btrfs_start_pre_rw_mount(),
btrfs_commit_super()) and the quota ioctl, which takes it before
starting a transaction. So no flush or commit can wait on a lock that
the merging task holds.
The same task already does each of these things in the same lock
context:
- prepare_to_merge() reserves with FLUSH_ALL, waiting while the async
reclaim worker flushes delalloc and commits, and then commits itself.
- relocate_block_group() commits again after the merge.
- btrfs_end_transaction_throttle() at the end of every merge step runs
delayed iputs. An evicted inode reserves with FLUSH_EVICT from right
there.
A commit in the middle of the merge is legal. The transaction kthread,
sync and other tasks already commit between merge steps. Each step
saves drop_progress and drop_level before it ends its transaction, and
the commit writes them to the reloc root item through
btrfs_update_reloc_root(). init_reloc_root() creates nothing while
rc->merge_reloc_tree is set. btrfs_end_transaction_throttle() used to
commit from this loop itself, until 302167c50b32 ("btrfs: don't end the
transaction for delayed refs in throttle").
DATA_RELOC inode. In the UPDATE_DATA_PTRS stage its delalloc was
written back and waited for, and its page cache invalidated, before the
stage started (do_nonremap_reloc()), so there is nothing to flush. In
the MOVE_DATA_EXTENTS stage its dirty pages may still be unwritten
during the merge. Flushing them from here is the same writeback that
the flusher threads, sync, or another task's FLUSH_ALL or FLUSH_EVICT
ticket may start at any time. It is also what do_nonremap_reloc() waits
for right after relocate_block_group() returns. Completing its ordered
extents joins a transaction on the data reloc root, which is not
shareable, so it does not take reloc_mutex.
The steal is bounded. steal_from_global_rsv() leaves 10% of the global
reserve, and each steal is at most min_reserved, which is two nodes per
level of the reloc root (64 KiB in the trace above). A merge that fails
here takes the filesystem read-only. Keeping it writable is what the
global reserve is for, as it is for eviction.
If even the global reserve cannot cover the refill, the merge fails as
before.
Reproduced with reloc-merge-enospc-test.sh (raid56-wh-harness): the
octopus P1 sequence on 6 x 3 GiB, fsstress until the reclaim worker's
relocations, plain relocations or balances run at the fill edge. On the
base, 3 of 3 reclaim runs went read-only in merge_reloc_roots(). With
the patch, 6 runs had no merge failure.
[zygo: 7.3 port: 7.3's btrfs_block_rsv_refill() still takes fs_info;
context adjusted (as on 6.18), the change itself is unchanged]
Fixes: 44d354abf33e ("btrfs: relocation: review the call sites which can be interrupted by signal") Assisted-by: Claude:claude-opus-5-5
Relocating a block group needs somewhere to write its live data, and a
filesystem short of free space is exactly when block groups need to be
relocated: automatic reclaim, balance, device shrink and removal. When
ordinary writers have taken every free byte, relocation fails with
-ENOSPC and the space it would have recovered stays out of reach, so a
full filesystem can stay full until an administrator frees space by
hand. Zoned filesystems already avoid this for data by dedicating a
block group to relocation (data_reloc_bg); regular filesystems have
nothing, for data or metadata.
Keep, per space_info, a set of block groups held free for relocation:
- Only relocation allocates from them: the data relocation inode's
extents, and tree blocks allocated by the task running
btrfs_relocate_block_group(). That task is the one stripe_alloc
already records as fs_info->reloc_owner for the whole relocation
(tree blocks COWed in the COW-only roots during relocation cannot be
told apart by their root); btrfs_is_reloc_task() tests it.
- Their free bytes are accounted in space_info->bytes_reloc_reserve,
which btrfs_space_info_used() counts as used, so admission,
overcommit, chunk allocation and ticket granting treat the reserve as
unavailable -- except for reservations and tickets made by the
relocating task (btrfs_is_reloc_task(); a ticket carries it as
reloc_reserve). stripe_alloc's reloc flag, which also admits the
transaction machinery's bounded reservations (NO_FLUSH, FLUSH_LIMIT,
EMERGENCY) to the stripe_meta reserve, does not extend to this one:
stripe_alloc's btrfs_can_overcommit() takes the relocation reserve
off "used", after the stripe_meta reserve, only when that flag is set
and the caller is the relocating task. The per-group share is
adjusted where bytes are reserved, freed or unpinned in a held group,
so allocations the reserve serves are not counted twice.
- statfs does not offer their space, the cleaner does not delete them
while they are empty, balance without a conversion filter skips them
(relocating an empty held group only deletes the reserve), and a held
group that goes read-only leaves the set.
- The relocating task keeps its exemption through the removal of the
emptied chunk after relocation, which sets reloc_owner again around
that transaction: it reserves metadata too, and refused on a full
filesystem it failed a relocation whose data had already moved, and
kept the raw space the reserve refills from.
- A tree block that finds no room outside the reserve takes it from the
reserve rather than fail. The reservation layer admitted it already,
a failed tree block allocation aborts the transaction, and relocating
a metadata group leaves delayed refs and commit work that tasks other
than the relocating one run; without this their tree blocks aborted
the transaction on a full filesystem. Data allocations have no such
fallback: a data writer gets ENOSPC.
- A metadata group may go read-only for relocation when the reserve can
hold its live and reserved bytes, even if the rest of the space_info
cannot: without that the first, non-forced read-only step of reclaim
fails on exactly the full filesystems reclaim is needed for. An
empty group, or a space_info without a reserve, gets no exemption,
and neither does data: there the ordinary test fails only while
outstanding reservations depend on the group's free space, and data
writeback has no reserve to fall back on.
- A held group that goes read-only (scrub, device replace) leaves the
set and rejoins it when writable again, or at once if the read-only
transition fails; otherwise every scrub of a full filesystem handed
the reserve to whoever wrote next.
- The refill after a relocation commits the removal of the source group
first: its device extents can be allocated again only then, and a
refill before that claimed a sliver of a chunk from the leftovers.
- While a device shrink or removal runs, the reserve takes only empty
groups: allocating chunks for it would take back the room the shrink
is freeing (a raid6 shrink failed in merge_reloc_roots with -ENOSPC).
With stripe_alloc:
- A held group counts as reserve only its claimable whole stripes:
reloc_reserve_resync(), called from btrfs_scan_stripe_unusable() at
each commit-time rescan, resyncs its share to them. Its trapped
free space is in bytes_stripe_unusable already, and counting it in
the reserve too made the metadata space_info read over-full on a
full raid6 filesystem while relocation found no stripe to claim.
Between rescans the share is capped at the claimable stripes when a
group joins the reserve while armed or is armed while held, and a
claimed stripe run's remainder (bytes_stripe_open) leaves the share
with the claim.
- stripe_claimable_admit() leaves the held groups' stripes out of
ordinary writers' whole-stripe supply; relocation still sees them.
A held group's stripes are not counted again as the dedicated data
relocation group's (bytes_stripe_claimable_reloc), which the same
check subtracts.
- stripe_alloc's whole-stripe check in inc_block_group_ro() is for
data groups, which have no reserve exception (above).
stripe_meta's bytes_stripe_reserve is not reduced by the held groups: it
is an admission margin that keeps tree blocks off the reserve's
emergency fallback, and without it a metadata fill consumed the reserve
before any relocation (dhive sresmn raid5/raid5, 2026-09-29).
Relocation moves at most one block group's live data, so the set holds
at least as much as the largest block group of the space_info. That is
the largest existing group rather than the chunk allocator's caps: a new
chunk can come out smaller than the caps when the devices are nearly
full, and a target no chunk can reach would allocate another chunk for
the reserve on every retry. A larger group appearing later raises the
target by itself. Block groups differ in size (on RAID5/6 the stripe
width follows the devices that have free space), so the reserve is a
set of groups whose free bytes add up to the target, not one group.
It is established at read-write mount after log replay and orphan
cleanup, from empty groups (largest first) or a new chunk while
unallocated space exists; refilled after every relocation from the raw
space the relocated group released, and by the cleaner; and groups
leave it, the least free first, only while the rest still covers the
target. If it cannot be established the filesystem says so once and
carries on without it.
The reserve is off by default: allocation/<type>/reloc_reserve turns it
on (empty groups are taken at once, a chunk for the rest at the
cleaner's next pass), and allocation/<type>/{bytes_reloc_reserve,
reloc_reserve_target} show its state. It stays off on zoned
filesystems, which have their own data relocation block group, and for
system, remap tree and mixed block groups.
The cost is capacity: the reserve holds one largest block group's worth
of space per space_info -- up to 10% of the writable space on small
filesystems, where the chunk allocator caps a chunk at that -- and a
filesystem needs room for the reserve beside its working block groups.
[zygo: 7.3 port: 7.3 predates upstream cd5e2d2f2ea2 (allocate additional
SYSTEM space earlier), so the unused-group check has no system space
target term next to the new test; context adjusted, the change itself is
unchanged]
btrfs: print the error when reclaim fails to relocate a block group
When the background reclaim of a block group fails, the error is logged
as just "error relocating chunk <start>". The reclaim worker retries
the group on its next pass, so a persistent failure shows up as the same
line every 30 seconds with nothing saying why: -ENOSPC from a
reservation, -EIO from a bad read, and -ETXTBSY from an active swapfile
all look alike, and telling them apart takes tracing the relocation
path.
Zygo Blaxell [Thu, 1 Oct 2026 16:14:49 +0000 (12:14 -0400)]
btrfs: stripe_alloc: keep a stripe in the rbio cache until its run has written all of it (debug_meta part)
The hunks of "btrfs: stripe_alloc: keep a stripe in the rbio cache until
its run has written all of it" (earlier in this series) that need code
from "btrfs: raid56: say whether a metadata read-modify-write is a write
hole", which comes after it in this series: they are applied here, once
"btrfs: raid56: say whether a metadata read-modify-write is a write
hole" is in, instead of with the rest of "btrfs: stripe_alloc: keep a
stripe in the rbio cache until its run has written all of it".
btrfs: raid56: adaptive park deadline from the parked-rbio backlog (debug_meta part)
The hunks of "btrfs: raid56: adaptive park deadline from the parked-rbio
backlog" (earlier in this series) that need code from "btrfs: raid56:
say whether a metadata read-modify-write is a write hole", which comes
after it in this series: they are applied here, once "btrfs: raid56: say
whether a metadata read-modify-write is a write hole" is in, instead of
with the rest of "btrfs: raid56: adaptive park deadline from the
parked-rbio backlog".
btrfs: stripe_alloc: attribute each data read-modify-write to why padding declined (debug_meta part)
The hunks of "btrfs: stripe_alloc: attribute each data read-modify-write
to why padding declined" (earlier in this series) that need code from
"btrfs: raid56: say whether a metadata read-modify-write is a write
hole", which comes after it in this series: they are applied here, once
"btrfs: raid56: say whether a metadata read-modify-write is a write
hole" is in, instead of with the rest of "btrfs: stripe_alloc: attribute
each data read-modify-write to why padding declined".
It also drops the pad_decline_live and pad_decline_unallocated counters:
these hunks remove their last readers.
btrfs: stripe_alloc: trace why padding declined, at the moment it declined (debug_meta part)
The hunks of "btrfs: stripe_alloc: trace why padding declined, at the
moment it declined" (earlier in this series) that need code from "btrfs:
raid56: say whether a metadata read-modify-write is a write hole", which
comes after it in this series: they are applied here, once "btrfs:
raid56: say whether a metadata read-modify-write is a write hole" is in,
instead of with the rest of "btrfs: stripe_alloc: trace why padding
declined, at the moment it declined".
btrfs: stripe_alloc: tunable park deadlines, and say why padding declined (debug_meta part)
The hunks of "btrfs: stripe_alloc: tunable park deadlines, and say why
padding declined" (earlier in this series) that need code from "btrfs:
raid56: say whether a metadata read-modify-write is a write hole", which
comes after it in this series: they are applied here, once "btrfs:
raid56: say whether a metadata read-modify-write is a write hole" is in,
instead of with the rest of "btrfs: stripe_alloc: tunable park
deadlines, and say why padding declined".
btrfs: raid56: say whether a metadata read-modify-write is a write hole
meta_rmw counts sub-stripe metadata writes, which is a proxy for exposure
rather than a measurement of it. A stripe modified in place is only a write
hole if it holds data some completed transaction is relying on; a stripe
that two writes of the *same* transaction happen to split costs an extra
read but risks nothing, because a tear loses that whole transaction anyway.
The counter cannot tell those apart, so it cannot answer the only question
that matters.
The rbio can. A read-modify-write has already read every column the write
does not cover, so at the point of the report it is holding the stripe's
on-disk contents. Walk the uncovered tree block positions and read their
headers: a block whose bytenr and fsid match belongs there, and its
generation says which transaction put it there. Compare that against the
generation of the blocks this write is carrying, taken from the same rbio,
so a transaction committing concurrently cannot skew the verdict:
meta_rmw_cur the same transaction's own blocks -- a cost
meta_rmw_old an earlier transaction's -- a write hole
meta_rmw_free no tree block there at all
System block groups are classified too, not just metadata ones. A system
chunk carries no METADATA bit, so a flags test excludes it, but it holds
the chunk tree: the same tree block header, the same generation field, and
a worse consequence if a degraded crash tears it, since without the chunk
tree no logical address can be mapped at all. meta_rmw already counted
system chunks; only the classification skipped them, so their read-modify-
writes were visible as a rate and never as a verdict. Whether a raid56
system chunk ever rewrites parity over a committed tree block is therefore
an open question that this answers by measurement rather than by reasoning
about how the chunk tree is laid out.
This trusts nothing the allocator says about itself. The stripe runs, the
liveness map and the drain accounting are all bookkeeping that could be
wrong in the same way twice; the header in the sector is what a degraded
read would actually have to reconstruct.
On a 3-device raid5 filesystem with raid5 metadata, 6000 small files with
periodic syncs and then a third rewritten, plain stripe_alloc reports 249
sub-stripe metadata writes of which 174 rewrite parity over committed tree
blocks. Adding stripe_meta leaves 37 sub-stripe writes and none of them.
An older header under the write is not yet a write hole: most belong to
blocks that died with their stripe, which a torn parity write cannot hurt.
So the committed extent tree, the witness a degraded read after a crash
would consult, is asked whether the block is still referenced, with an
extent item of the header's generation (a sector re-allocated in the
current transaction carries a new item while the old header is still on
disk). A live one is reported as before, with its bytenr and reference
count; a dead one is counted as meta_rmw_ghost without a warning.
The lookup runs in rmw_rbio() under the full-stripe lock, where reading a
tree block of the same stripe would queue a rebuild behind that lock, so
it does not block: commit_root_sem is taken with a trylock and the search
uses path->nowait. A block not in memory is counted as meta_rmw_unknown.
[zygo: 7.3 port: 7.3 has no per-chunk stripe length (map->stripe_len,
bioc->stripe_len, the two-argument btrfs_stripe_nr_to_offset()):
BTRFS_STRIPE_LEN is used instead; the change itself is unchanged]
Zygo Blaxell [Fri, 9 Oct 2026 23:13:11 +0000 (19:13 -0400)]
btrfs: stripe_alloc: count and log ordered extents that end with sectors unwritten (debug_meta part)
The hunks of "btrfs: stripe_alloc: count and log ordered extents that
end with sectors unwritten" (earlier in this series) that need code from
"btrfs: stripe_alloc: share LOG data runs per log transaction
(stripe_meta part)", which comes after it in this series: they are
applied here, once "btrfs: stripe_alloc: share LOG data runs per log
transaction (stripe_meta part)" is in, instead of with the rest of
"btrfs: stripe_alloc: count and log ordered extents that end with
sectors unwritten".
They append the unwritten_on_end counters to the stripe_run_stats list
after the log seal counters, which that patch adds to it.
Zygo Blaxell [Tue, 6 Oct 2026 04:11:38 +0000 (00:11 -0400)]
btrfs: stripe_alloc: share LOG data runs per log transaction (stripe_meta part)
The hunks of "btrfs: stripe_alloc: share LOG data runs per log
transaction" (earlier in this series) that need code from "btrfs:
stripe_alloc: give each log commit its own metadata stripe run", which
comes after it in this series: they are applied here, once "btrfs:
stripe_alloc: give each log commit its own metadata stripe run" is in,
instead of with the rest of "btrfs: stripe_alloc: share LOG data runs
per log transaction".
Zygo Blaxell [Mon, 5 Oct 2026 03:16:03 +0000 (23:16 -0400)]
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.
Zygo Blaxell [Sat, 10 Oct 2026 04:19:18 +0000 (00:19 -0400)]
btrfs: stripe_alloc: skip RAID5/6 groups that are mapped as RAID1/RAID1C3 (stripe_meta part 2)
The hunks of "btrfs: stripe_alloc: skip RAID5/6 groups that are mapped
as RAID1/RAID1C3" (earlier in this series) that need code from "btrfs:
stripe_meta: cover raid56 system chunks", which comes after it in this
series: they are applied here, once "btrfs: stripe_meta: cover raid56
system chunks" is in, instead of with the rest of "btrfs: stripe_alloc:
skip RAID5/6 groups that are mapped as RAID1/RAID1C3".
The chunk tree lives in system chunks, which carry no METADATA bit, so the
whole-stripe rule that stripe_meta applies to raid56 tree blocks never
applied to it: chunk tree blocks were placed by the clustered allocator,
sharing stripes with chunk tree blocks committed earlier, and every chunk
tree COW was a read-modify-write of such a stripe. The RMW audit measured
it (meta_rmw_old over system chunks during chunk churn), and the mount
warned about it. A torn parity rewrite there is the worst write hole the
filesystem can take: with a member then lost, the committed chunk tree
block that shared the stripe reconstructs wrong, the checksum rejects it,
and nothing maps -- the degraded mount fails, and there is no second copy.
Include system block groups in btrfs_is_stripe_meta_bg(). Everything
downstream already keys on that predicate: the whole-stripe allocation in
do_allocation_stripe(), the trapped-space accounting, the covered/uncovered
decision in the RMW report and the audit. The four walks that select
groups by space_info flags (the enable-time sweep, the unusable-space scan,
the reclaim tick, the commit-time metadata run retirement) gain SYSTEM
alongside DATA and METADATA, so system groups are armed, scanned, reclaimed
when trapped, and have their runs closed after the commit's tree writes
like metadata groups.
reserve_chunk_space() sizes its request in tree blocks, but a chunk tree
COW that needs a fresh run takes a whole stripe for its first block; the
trapped bytes and the open run's remainder are already counted as used, so
what is left is whole free stripes, and the check now demands one stripe
of headroom beyond the request -- deciding when to allocate the next
system chunk, without reserving the headroom -- or the chunk item for that
very allocation could find no stripe to land in.
The "does not cover raid56 system chunks" warning has nothing left to
warn about and goes.
[zygo: 7.3 port: 7.3 predates upstream cd5e2d2f2ea2 (allocate additional
SYSTEM space earlier), so reserve_chunk_space() takes the headroom as on
6.18: left < bytes + headroom decides both the ENOSPC_DEBUG report and
the allocation]
Zygo Blaxell [Sun, 25 Oct 2026 10:27:49 +0000 (06:27 -0400)]
btrfs: stripe_alloc: do not count a writeback ENOSPC drop's give-back as supply (stripe_meta part)
The hunks of "btrfs: stripe_alloc: do not count a writeback ENOSPC
drop's give-back as supply" (earlier in this series) that need code from
"btrfs: stripe_meta: hold back whole stripes so tree blocks always land
and trapped groups can be reclaimed", which comes after it in this
series: they are applied here, once "btrfs: stripe_meta: hold back whole
stripes so tree blocks always land and trapped groups can be reclaimed"
is in, instead of with the rest of "btrfs: stripe_alloc: do not count a
writeback ENOSPC drop's give-back as supply".
btrfs: stripe_alloc: count whole stripes stranded behind live runs as trapped (stripe_meta part)
The hunks of "btrfs: stripe_alloc: count whole stripes stranded behind
live runs as trapped" (earlier in this series) that need code from
"btrfs: stripe_meta: hold back whole stripes so tree blocks always land
and trapped groups can be reclaimed", which comes after it in this
series: they are applied here, once "btrfs: stripe_meta: hold back whole
stripes so tree blocks always land and trapped groups can be reclaimed"
is in, instead of with the rest of "btrfs: stripe_alloc: count whole
stripes stranded behind live runs as trapped".
btrfs: stripe_meta: hold back whole stripes so tree blocks always land and trapped groups can be reclaimed
Two failures at the metadata fill edge under stripe_meta, both seen
with the tiny-file fill test on raid5 metadata.
The first is a transaction abort. A tree block is reserved in bytes
and admitted while claimable whole stripes cover the outstanding
reservations, but it is placed in whole stripes: the open run it would
have joined is closed at the transaction boundary, so every group
claims at least one fresh full stripe per transaction. The last few
megabytes of claimable supply go to those claims, made for reservations
admitted earlier, and the next admitted tree block finds no stripe at
all ("stripe_meta: tree block allocation of 16384 bytes returned
ENOSPC: claimable 0 open 1622016 trapped 1614266368"), which aborts the
transaction. Pre-pay two transactions' worth of claims -- twice the
number of writable groups times the widest full stripe -- in the
space_info's stripe margin, recomputed at each commit scan. Best-fit
run selection opens a fresh run only when a group's runs are full, so
this is a few megabytes, not a fraction of the space_info.
The second is a filesystem that quietly runs out at half capacity.
COW frees trap stripes rather than free them: a stripe is claimable
again only once every tree block in it is dead, which random deaths in
a sixteen-block stripe essentially never achieve, so the METADATA
space_info settles around half used, half trapped and no claimable
stripe left (3.04 GiB metadata, 49% used, fs not writable, deletes
included). Only relocation packs live tree blocks back into whole
stripes, and it needs the moved group's live bytes claimable elsewhere
before it starts -- by the time the trigger fires there is nothing
left. Hold back a reserve of whole stripes -- the largest writable
group's length, capped at a quarter of the space_info, nothing with a
single group -- from every metadata reservation except those made by
the relocation task, identified by a task pointer that reloc_ctl
already implies. Reservations admitted against the reserve reach the
same tickets, so the flag travels with the ticket. The reserve is an
accounting hold-back, not a partition: a committing transaction still
allocates from those stripes when it must, and the next scan restates
the reserve from what remains. Both counters are cleared when
stripe_alloc is turned off, since the scan that maintains them stops
with it.
The reserve is reported through sysfs as bytes_stripe_reserve and in
the space_info dump, whose stripe_claimable and stripe_margin values
were printed under each other's label.
The reserve belongs to the whole of btrfs_relocate_block_group(), not
only the stretches where reloc_ctl is set: relocation reserves metadata
before its first stage (create_reloc_inode() starts a transaction for
six items), and that reservation, made as an ordinary user, fails with
-ENOSPC exactly when relocation is needed -- once the claimable supply
has fallen below the reserve. fs_info->reloc_owner records the task
for the whole run (reloc_chunk_start() serializes it) and counts as
relocation here.
Who may draw on the reserve:
- Relocation, for the whole of btrfs_relocate_block_group(), which
reloc_chunk_start() serializes: the reservation create_reloc_inode()
makes before the first stage must not stay behind the reserve, or every
metadata relocation fails exactly when the supply is below it (511 of
536 failed relocations of a raid6/raid6 octopus run ended so). Making a
group read-only, relocation's first step, draws on it too.
- The transaction machinery's own reservations, NO_FLUSH, FLUSH_LIMIT and
EMERGENCY: the reserve holds back user operations so relocation and the
commit after it have room, and must not hold back the commit (an
extent tree COW freeing deleted leaves aborted the transaction with
163 MiB of whole stripes unused). User reservations (FLUSH_ALL,
ALL_STEAL, EVICT, DATA) still stop at it.
- The global reserve steal is refused once the claimable supply is down
to the reserve plus the claim margin: an inline unlink frees no stripe
and each transaction of unlinks takes fresh ones, so stolen
reservations end in an abort. The unlink fails with ENOSPC instead.
The orphan cleanup at mount and ro->rw remount (the task is recorded in
btrfs_start_pre_rw_mount()) steals regardless: a failed eviction there
fails the read-write mount, and a filesystem that cannot be mounted
read-write can never run the relocation the reserve is for.
The reserve is a multiple of the metadata full stripe (stripe_round_up(),
not round_up(): the widths are not powers of two).
Raid56 metadata groups go through a runtime enable or disable too: the
arm and disarm sweep covers METADATA space_infos, the commit-time
metadata retirement runs whether or not the policy is on (so a disable
does not leave the running transaction's metadata runs and their group
references until unmount), and the enable drain commits the running
transaction before arming the write-hole check, since tree blocks the
legacy allocator placed in it are written by that commit into partly used
stripes. That one commit's exposure is inherent.
Zygo Blaxell [Thu, 22 Oct 2026 20:47:05 +0000 (16:47 -0400)]
btrfs: stripe_meta: extend stripe reclaim to metadata block groups
The stripe reclaim of the reclaim subtopic handles stripe_alloc data block
groups only. stripe_meta allocates tree blocks in whole stripes too, so
its block groups trap free space in partially used stripes the same way,
and that space comes back only by moving the live tree blocks out of those
stripes. Metadata needs it more than data: a metadata reservation that
finds no claimable stripe aborts the transaction instead of failing one
write, so trapped metadata space that is never reclaimed ends in a forced
read-only filesystem.
Extend each reclaim mechanism to stripe_meta block groups:
- btrfs_reclaim_block_group(): allocate a chunk to relocate into when the
other groups' claimable stripes cannot take the group's live bytes, and
relocate the group even while it has reserved or pinned bytes. Under
COW, a metadata group always has tree blocks reserved or pinned, so the
generic bail-out would defer it forever.
- The cleaner's periodic re-evaluation scans the metadata space_info as
well as the data one.
- Compaction counts metadata extent items: a tree block is nodesize long
and never spans a stripe, so for a metadata group the closure is the
partial stripes themselves.
- stripe_reclaim_now accepts stripe_meta block groups, for one group or
for all of them.
The comments that name the groups reclaim handles now name both.
Zygo Blaxell [Sat, 10 Oct 2026 04:19:18 +0000 (00:19 -0400)]
btrfs: stripe_alloc: skip RAID5/6 groups that are mapped as RAID1/RAID1C3 (stripe_meta part)
The hunks of "btrfs: stripe_alloc: skip RAID5/6 groups that are mapped
as RAID1/RAID1C3" (earlier in this series) that need code from "btrfs:
stripe_meta: allocate tree blocks by whole stripes", which comes after
it in this series: they are applied here, once "btrfs: stripe_meta:
allocate tree blocks by whole stripes" is in, instead of with the rest
of "btrfs: stripe_alloc: skip RAID5/6 groups that are mapped as
RAID1/RAID1C3".
Zygo Blaxell [Thu, 1 Oct 2026 16:14:49 +0000 (12:14 -0400)]
btrfs: stripe_alloc: keep a stripe in the rbio cache until its run has written all of it (stripe_meta part)
The hunks of "btrfs: stripe_alloc: keep a stripe in the rbio cache until
its run has written all of it" (earlier in this series) that need code
from "btrfs: stripe_meta: allocate tree blocks by whole stripes", which
comes after it in this series: they are applied here, once "btrfs:
stripe_meta: allocate tree blocks by whole stripes" is in, instead of
with the rest of "btrfs: stripe_alloc: keep a stripe in the rbio cache
until its run has written all of it".
btrfs: stripe_meta: allocate tree blocks by whole stripes
Metadata gets the placement rule data already has: tree blocks are handed
out from runs of fully free stripes, and a stripe is never revisited once
the transaction that filled it has written it. No separate mount option: the
chunk profile already decides it. raid56 metadata has the same write hole
as raid56 data, so it gets the same protection at the same time, and
metadata on any other profile has no write hole to close -- the policy's
RAID56_MASK test simply does not match it, so nothing changes there.
It does cost a stranded stripe tail per commit, and a metadata allocation
that fails aborts the transaction rather than failing one write. Those
are the price of covering raid56 metadata at all rather than a reason to
make it opt-in separately: a filesystem unwilling to pay them does not
want raid56 metadata, and converting it to raid1c3/raid1c4 removes both
the cost and the write hole.
Only system chunks are left uncovered: btrfs_is_stripe_meta_bg() tests the
METADATA flag and a system chunk does not carry it, so a raid56 system
chunk keeps the legacy read-modify-write. The mount-time warning is
narrowed to say exactly that instead of claiming metadata is uncovered,
which it no longer is.
The machinery is the data machinery, including the per-sector liveness
map the previous patch gave it. Metadata needed three things of its own:
- A completion report. A run drains when every byte allocated from it
has been accounted for, and metadata had no equivalent of ordered
extent completion, so its runs could never drain -- the deadlock that
mixed block groups hit. end_bbio_meta_write() now reports, and
btrfs_open_stripe_write_abandoned() covers a tree block freed before
it was ever written (COWed twice in one transaction, say).
- A two-phase closure. btrfs_retire_open_stripes() runs before the
commit writes tree blocks, so it skips metadata runs; they are closed
and drained afterwards by btrfs_retire_meta_stripes(), once every block
is on disk. Closing earlier would strand blocks the commit has not
allocated yet, draining earlier would wait for writes it has not
issued. Metadata runs are also freed there rather than when their
counter reaches zero: a stripe's columns can be written by separate
rbios, and freeing on the first completion leaves the second with no
run to pad or merge against.
- Whole stripes per writeback pass. The opportunistic writeback passes
do not know about stripes and will happily write three tree blocks now
and five later; btrfs_defer_stripe_meta_write() leaves the blocks
dirty for the commit (or a WB_SYNC_ALL waiter) instead, the same shape
as the zoned write-pointer deferral.
Two free paths bypass the usual completion reporting and must report the
block abandoned themselves: btrfs_free_tree_block() can send a
same-transaction unwritten block through the delayed-ref machinery when
check_ref_cleanup() finds its ref head already processed, and
clean_log_buffer() discards unwritten log tree blocks via
btrfs_pin_reserved_extent() without ever visiting btrfs_free_tree_block().
Either miss leaves the block counted as inflight forever: the run never
drains, the block group reference never drops, and unmount asserts. Both
were found by the dm-log-writes replay matrix, whose crash-recovery mounts
exercise both paths routinely.
Trapped free space is maintained for stripe_meta groups as for data: the
disarm, rescan and commit-time scan accept them, the commit scan sums the
METADATA space_info, and inc_block_group_ro() applies the trapped-bytes
subtraction to metadata groups too. btrfs_update_global_block_rsv()
compares the global reserve against the capacity minus the trapped bytes,
so a metadata chunk is allocated while unallocated space remains, and a
stripe_meta tree block allocation that fails with ENOSPC (which aborts the
transaction) prints the stripe counters.
btrfs_retire_meta_stripes() syncs the open-run remainder into the
space_info after it closes the runs, as the data retirement does.
report_uncovered_rmw() keeps its "not covered by stripe_alloc" message for
what it was written for, system chunks and mixed groups, and no longer
says it of a covered stripe_meta group. btrfs_free_tree_block() checks
its block group lookup.
A mount whose log replay fails after allocating tree blocks, and the
unmount of an aborted filesystem, never commit again, so the commit-time
retirement never releases their open runs and block group references:
both release every open run before the block groups are freed, reporting
any run that still counts bytes in flight.
Mark Korenberg [Fri, 6 Nov 2026 17:33:10 +0000 (22:33 +0500)]
btrfs: raid56: pass REQ_META to per-device bios
RAID5/6 writes, and reads that need a rebuild, go through an rbio,
which builds new per-device bios from a bare REQ_OP_READ or
REQ_OP_WRITE. So all tree block writes on RAID5/6 lose REQ_META, while
on the other profiles the per-device bios are clones and keep it. The
same goes for a tree block read rebuilt from the other stripes, for
example with a missing device.
Pass REQ_META on to all per-device bios of an rbio, data and P/Q alike,
if any bio it serves has it. Scrub and relocation do not set REQ_META,
so their rbios stay untagged, as do rbios that only serve file data.
[zygo: 7.3 port: 7.3 has no per-chunk stripe length (map->stripe_len,
map->stripe_shift, two-argument btrfs_stripe_nr_to_offset()), context
adjusted to BTRFS_STRIPE_LEN; the change itself is unchanged]
Zygo Blaxell [Sun, 25 Oct 2026 10:27:49 +0000 (06:27 -0400)]
btrfs: stripe_alloc: do not count a writeback ENOSPC drop's give-back as supply (debug part)
The hunks of "btrfs: stripe_alloc: do not count a writeback ENOSPC
drop's give-back as supply" (earlier in this series) that need code from
"btrfs: stripe_alloc: log the space counters when data writeback hits
ENOSPC", which comes after it in this series: they are applied here,
once "btrfs: stripe_alloc: log the space counters when data writeback
hits ENOSPC" is in, instead of with the rest of "btrfs: stripe_alloc: do
not count a writeback ENOSPC drop's give-back as supply".
Zygo Blaxell [Sun, 25 Oct 2026 08:48:50 +0000 (04:48 -0400)]
btrfs: stripe_alloc: log the space counters when data writeback hits ENOSPC
A data writeback allocation that fails with ENOSPC under stripe_alloc
drops the buffered data in its range. The reservation margin is meant to
make that impossible, so the warning is the only evidence that admission
and the allocator disagreed, and today it names only the allocation size.
A drop seen once in a long test cannot be explained afterwards.
Log what admission decided on in the same line: the whole-stripe supply
(claimable, the part dedicated to relocation, unmanaged free space), what
is held against it (may_use, the outstanding extents' margin), the open,
trapped and stranded bytes, reserved and total, and the margin unit. The
"despite reservation margin" wording is kept, so existing log scanners
still match it.
On CONFIG_BTRFS_DEBUG builds, also log one line per block group with its
full stripe length, claimable bytes, largest known claimable run, open
remainder and trapped and stranded bytes. Unlike btrfs_dump_space_info()
this walks no free space entries. It takes groups_sem for read and each
group's lock, as the ENOSPC_DEBUG dump a few lines above already does at
this call site, so it adds no lock ordering.
The line and the walk share one rate limit, so a burst of failures cannot
flood the log with group lines.
Zygo Blaxell [Fri, 9 Oct 2026 23:13:11 +0000 (19:13 -0400)]
btrfs: stripe_alloc: count and log ordered extents that end with sectors unwritten
When an ordered extent reports to its stripe run, sectors it allocated
that no write reached are marked written so nothing waits for them (a
failed write, truncated folios, a short direct write, error teardown).
Whether any of these paths can still leave a waiter stuck in practice is
not known: the log settle that motivated the marking seals and frees a
shared LOG run before it would wait.
Count such extents and their unwritten bytes in stripe_run_stats
(unwritten_on_end, unwritten_on_end_bytes) and log each one, rate
limited, with the extent's identity, truncated length and flags, to see
which paths produce them.
One class is harmless and is counted but not logged: an extent marked
TRUNCATED, with no IOERR and not compressed (disk_num_bytes == num_bytes),
whose unwritten sectors are exactly the part truncation cut off,
[disk_bytenr + ALIGN(truncated_len, sectorsize), disk_bytenr +
disk_num_bytes). It comes from a truncate or O_TRUNC racing writeback and
from a direct write whose bounce-buffer copy faults before copying
anything, and it would bury the lines the message is there to find.
btrfs_open_stripe_write_done_run() returns the lowest unwritten sector so
that the tail can be told from merely as many sectors. Those extents go to
unwritten_on_end_truncated and unwritten_on_end_truncated_bytes;
unwritten_on_end and unwritten_on_end_bytes count only the logged ones, so
any increase there is worth a look.
Zygo Blaxell [Sun, 4 Oct 2026 21:46:19 +0000 (17:46 -0400)]
btrfs: stripe_alloc: break rbio pins and open stripe runs down by run class
Under fsync load the rbio cache holds up to ~190 pins its runs still need,
against 35-55 with the same load on raid1 metadata, so most of them look
like metadata stripes: tree-log blocks share the transaction's META run
with the tree blocks COWed in it, a log commit writes its part of a stripe
and the rest waits for the transaction commit. Data runs are private per
fsyncing inode and closed at every log commit, so they should be many and
short. Say which, instead of inferring it.
stripe_park_stats gains rbio_pinned_live_<class> (live pins by the class of
the run that still needs the stripe) and open_runs_<class> (open runs by
class), for class cow, reloc, log, nocow and meta.
btrfs_stripe_run_incomplete() becomes a wrapper around
btrfs_stripe_run_incomplete_class(), which also says which run's class.
[zygo: 7.3 port: 7.3 has no per-chunk stripe length (map->stripe_len,
bioc->stripe_len, the two-argument btrfs_stripe_nr_to_offset()):
BTRFS_STRIPE_LEN is used instead; the change itself is unchanged]
btrfs: stripe_alloc: optionally audit data read-modify-writes against the committed extent tree
The attribution counters say which RMWs the allocator's design explains;
they do not show that an RMW put nothing at risk. That depends on what
the uncovered sectors hold: an RMW recomputes parity under them while
they stay put, so a crash mid-write can tear them, and that matters
only if a committed transaction references them and the owner is not
relocation's own orphaned copy.
Add an audit that answers it from the tree. For every data RMW, walk
the commit root of the extent tree (no locking, as
rmw_block_referenced() does; and like it, cached blocks only and
commit_root_sem only when it is free, because the full stripe lock is
held: a miss counts in audit_err, and mixed block groups, whose tree
blocks share the stripe, are not audited) for extents overlapping the
stripe's uncovered sectors, and read each one's data backrefs, inline
and keyed. An extent owned only by the data reloc tree counts as
relocation's (data_rmw_hole_reloc_only); any other owner, a shared ref
(owner unknown), or no ref at all is foreign and counts as a write hole
(data_rmw_hole_no_run / _uncovered / _other, by pad verdict). Log the
writer of every foreign hole and of every NO_RUN RMW, rate-limited from
separate call sites so a flood of the second cannot starve the first:
root, inode, file offset, ordered extent flags, disk bytenr, stripe run
sequence, and the covering run as the pad oracle saw it. The commit
root switches just before the super lands, so the check can report a
hole early, never hide one.
A tree search per RMW is not free (a stock raid5 filesystem does
thousands a minute), so the audit is built only with CONFIG_BTRFS_DEBUG
and runs only while /sys/fs/btrfs/<uuid>/debug/stripe_rmw_audit is set,
which it is not by default. Its counters are in
debug/stripe_rmw_audit_stats, with data_rmw_audited so a reader can
tell an audit that found nothing from one that never ran.
[zygo: 7.3 port: 7.3 has no per-chunk stripe length (map->stripe_len,
bioc->stripe_len, the two-argument btrfs_stripe_nr_to_offset()):
BTRFS_STRIPE_LEN is used instead; the change itself is unchanged]
btrfs: stripe_alloc: attribute each data read-modify-write to why padding declined
data_rmw counts read-modify-writes, and the pad_decline_* counters count
the pad oracle's refusals, but a refusal does not always lead to a read
(a full stripe or a cached one does not read), so the two cannot be
subtracted: nothing says which RMWs the allocator explains. With
allow_rmw empty, a data RMW in a stripe_alloc group is expected in two
places only:
- at the open run's own frontier (the pad verdict UNCOVERED): the
sectors it reads belong to the same run, in the same transaction,
and are not durable yet;
- from relocation writing into its own preallocated extents, which no
root but the data reloc tree references until relocation moves the
pointers after writeback.
Record the pad oracle's verdict on the rbio (rmw_try_pad_full()) and,
at the RMW site, split data_rmw by it (data_rmw_<reason>, with
data_rmw_not_tried when the oracle was not consulted and
data_rmw_pad_ok_nomem when it agreed but the zero pages could not be
allocated). NO_RUN is the oracle's catch-all exit, so split it further
by the exit taken (data_rmw_norun_*) and count those in a read-only
group (data_rmw_norun_bg_ro). Count the data RMWs written by the data
reloc inode outside the frontier (data_rmw_reloc_writer, disjoint from
data_rmw_uncovered). All in stripe_park_stats.
A test can then assert that data_rmw - data_rmw_uncovered -
data_rmw_reloc_writer is zero on a stripe_alloc filesystem, on a
production kernel, with reclaim relocating during the run -- which it
could not do from the totals alone.
The cost is a byte of state per rbio and a few counter increments per
RMW, plus one block group lookup for NO_RUN RMWs.
btrfs: stripe_alloc: allow-rmw policy, and write-in-place for isolated nocow extents (debug part)
The hunks of "btrfs: stripe_alloc: allow-rmw policy, and write-in-place
for isolated nocow extents" (earlier in this series) that need code from
"btrfs: add a write-hole invariant checker to the raid56 write path",
which comes after it in this series: they are applied here, once "btrfs:
add a write-hole invariant checker to the raid56 write path" is in,
instead of with the rest of "btrfs: stripe_alloc: allow-rmw policy, and
write-in-place for isolated nocow extents".
btrfs: stripe_alloc: say at mount whether the policy is on (debug part)
The hunks of "btrfs: stripe_alloc: say at mount whether the policy is
on" (earlier in this series) that need code from "btrfs: add a
write-hole invariant checker to the raid56 write path", which comes
after it in this series: they are applied here, once "btrfs: add a
write-hole invariant checker to the raid56 write path" is in, instead of
with the rest of "btrfs: stripe_alloc: say at mount whether the policy
is on".
btrfs: stripe_alloc: report read-modify-write of uncovered stripes (debug part)
The debug half of "btrfs: stripe_alloc: report read-modify-write of
uncovered stripes", earlier in this series: the meta_rmw counter in
stripe_park_stats, which goes with the series' other debug statistics.
The warning itself is in that patch.
btrfs: add a write-hole invariant checker to the raid56 write path
With stripe-exclusive allocation, a raid56 data stripe may only be
written while an open or draining stripe run covers it: after its run
retires and drains at a transaction commit, nothing may ever write to
it again, and a write outside any run means an allocation bypassed the
policy. Both cases are the write hole about to happen.
Check the invariant (under CONFIG_BTRFS_DEBUG) for every raid56 write
operation, full-stripe and sub-stripe alike, at rmw_rbio() time. This
turns every upstream violation -- a missed allocation path, a
retirement ordering bug, an accounting leak -- into a deterministic
WARN at the moment of the offending write, instead of silent damage
that needs a crash plus a device failure plus a scrub to observe. The
bios gathered in an rbio have not reported their IO done yet, so their
runs cannot drain under the check: no false positives from completion
races.
Block groups that ever hosted relocation-class runs are skipped
(sticky, debug-only flag): relocation legitimately overwrites its
preallocated extents in place after their runs drain, and the write
path cannot tell those writes from violations.
A runtime enable of the policy (the btrfs.stripe_alloc property set on a
live filesystem) flips it while the legacy allocator may still have writes
in flight, in partly used stripes that no run covers, and the check would
warn on the first of them. So a runtime enable holds the check off until
a worker has started delalloc on all roots and waited for every ordered
extent (the property is applied inside a transaction, where that cannot
wait); the same wait writes any delalloc tail a writeback racing the
enable left unsubmitted. A mount-time enable has nothing in flight.
close_ctree() flushes the worker.
Zygo Blaxell [Tue, 6 Oct 2026 04:11:38 +0000 (00:11 -0400)]
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.
btrfs: stripe_alloc: allow-rmw policy, and write-in-place for isolated nocow extents (fsync part 2)
The hunks of "btrfs: stripe_alloc: allow-rmw policy, and write-in-place
for isolated nocow extents" (earlier in this series) that need code from
"btrfs: stripe_alloc: carry an inode's log tail forward at fsync", which
comes after it in this series: they are applied here, once "btrfs:
stripe_alloc: carry an inode's log tail forward at fsync" is in, instead
of with the rest of "btrfs: stripe_alloc: allow-rmw policy, and
write-in-place for isolated nocow extents".
btrfs: stripe_alloc: carry an inode's log tail forward at fsync
Closing a log-active inode's private stripe run at each log commit
traps the run's final partial stripe tail every time the inode fsyncs:
a steady fsync stream burns one stripe per log commit until the stripes
free or balance runs.
Reclaim the tails by carrying the partial stripe's live data forward.
At each log commit, the settling walk now computes the closed run's
final partial stripe, and the logging paths record the file ranges of
the logged extents that live inside it on a small per-inode table
(bounded; overflow just means an extent is not carried). At the
inode's next fsync, before its delalloc flush, each recorded range that
still maps to the recorded disk bytenr is re-dirtied, defrag style
(reserve, reset delalloc state, mark the folios dirty; absent folios
are read back, which is a plain read of settled data). The flush then
COWs the carried ranges together with the new data into the inode's
current private run, and the old partial stripe empties and frees
whole.
Everything downstream is the ordinary COW pipeline: new extent maps,
ordered extents, checksums, file extent items and delayed refs, and
the log's modified-extents snapshot -- taken inside btrfs_log_inode
after the flush -- picks up the new addresses by itself. Reflinked
ranges need no special handling: foreign referents keep the old extent
alive in its closed (never again written) stripe, and only the space
reclaim degrades. A range that was rewritten, truncated, punched or
compressed simply fails the mapping check and stays put.
Crash safety does not regress. A carried extent's old copy is dropped
through the normal paths: if it was committed, the free pins until the
transaction commits; if it was logged but never committed, its ADD and
DROP delayed refs cancel in cleanup_ref_head(), which also pins
must_insert_reserved heads -- either way the old blocks cannot be
reallocated before the log that references them is superseded.
The settling walk also learned to report the partial stripe through a
widened btrfs_log_settle_stripes() signature (inode and file range
instead of fs_info); compressed extents pass a zero file length to opt
out of carrying while still settling.
The re-dirty reserves with NO_FLUSH. carry_one_range() takes the folio
locks and the extent range lock before reserving, and a flushing
(ticketed) reservation can sleep in wait_reserve_ticket() until the
flusher makes progress -- but every way forward needs the locks we
hold: FLUSH_DELALLOC has to write back the carried range, which blocks
in __folio_lock() on our locked folio, and a transaction commit (with
flushoncommit) waits for the same writeback. The whole filesystem then
wedges behind the stuck commit. Carrying is best effort by design and
the carried range's data is already durable in its old stripe, so on
ENOSPC just skip the carry and let the old stripe free the slow way.
The carry's reservation does not flush at all, including the qgroup
reserve: btrfs_check_data_free_space_noflush() reserves data space and
qgroup space without flushing, since try_flush_qgroup() would write back
the folios the carry holds locked. On EDQUOT, as on ENOSPC, the range is
just not carried. The carry also clears a stale EXTENT_NORESERVE (left by
a NOCOW write) with the other delalloc bits, so the data space it reserved
is released when the range is later written NOCOW.
btrfs: stripe_alloc: allow-rmw policy, and write-in-place for isolated nocow extents (fsync part)
The hunks of "btrfs: stripe_alloc: allow-rmw policy, and write-in-place
for isolated nocow extents" (earlier in this series) that need code from
"btrfs: stripe_alloc: settle a logged extent's stripes at log commit",
which comes after it in this series: they are applied here, once "btrfs:
stripe_alloc: settle a logged extent's stripes at log commit" is in,
instead of with the rest of "btrfs: stripe_alloc: allow-rmw policy, and
write-in-place for isolated nocow extents".
btrfs: stripe_alloc: count open stripe run remainders against data reservations (fsync part)
The hunks of "btrfs: stripe_alloc: count open stripe run remainders
against data reservations" (earlier in this series) that need code from
"btrfs: stripe_alloc: settle a logged extent's stripes at log commit",
which comes after it in this series: they are applied here, once "btrfs:
stripe_alloc: settle a logged extent's stripes at log commit" is in,
instead of with the rest of "btrfs: stripe_alloc: count open stripe run
remainders against data reservations".
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.
[zygo: 7.3 port: 7.3 has no stripe-tree force-commit check (upstream b9569eda415c came later) in front of the new block; context adjusted,
the change itself is unchanged]
Zygo Blaxell [Thu, 22 Oct 2026 08:02:52 +0000 (04:02 -0400)]
btrfs: stripe_alloc: back off stripe reclaim after repeated ENOSPC
The reclaim worker backs off a space_info after repeated -ENOSPC failures.
Under stripe_alloc two more paths queue trapped groups, the commit-time
rescan and the cleaner tick, every five minutes each; have them honour the
backoff too, and have the reclaim predicate require room for relocation's
up-front metadata reservation as well. A stripe_reclaim_now request is
still tried during a backoff.
It showed in two ways:
- The octopus P1 pass (raid5 6 x 3 GiB, misc-next, bhive 2026-10-20):
with the data space fully allocated and ~2 GiB trapped, reclaim held the
exclusive operation for over an hour of fsstress and long after it,
"error relocating chunk ...: -28" repeating (28 attempts, 13 errors in
one loop), so a resize could not start and the replace failed. The
worker sat in a metadata reservation (btrfs_block_rsv_refill under
relocate_block_group): metadata had no room either.
- reclaim-futile-test.sh (raid5 6 x 1 GiB at the edge, fsstress churn):
226 reclaim errors in 600 s. Of the -28 returns, relocation's data
reservation lost the claimable stripes the room check had counted to
the concurrent writers.
The room check (claimable stripes in the other groups) is an estimate
taken when the group is queued, and it did not cover metadata: it now
also requires room for relocation's up-front metadata reservation (512
tree blocks, twice RELOCATION_RESERVED_NODES), which the octopus failure
needed.
Tested: reclaim-futile-test.sh measured 226 reclaim errors in 600 s on the
lane without the backoff (the fill-edge shape that loops; it does not loop
on every run). With it no run logged more than 5 errors per 600 s; in one,
five relocations failed with -ENOSPC and reclaim backed off once
(reclaim_backoffs +1), then resumed. stripe-reclaim-test and
stripe-reclaim-now-test pass.
Zygo Blaxell [Tue, 20 Oct 2026 18:41:37 +0000 (14:41 -0400)]
btrfs: stripe_alloc: add stripe_reclaim_now to reclaim stripes on demand
Background reclaim of stripe_alloc raid56 groups runs when a
commit-time rescan, or the cleaner's tick at most every five minutes per
group, finds a group past bg_reclaim_threshold. Nothing lets an operator
say "reclaim now": during an idle period, before a large write, or for one
group seen trapped. The only tool is balance, which relocates whole groups
whatever their state and cannot compact.
echo all > stripe_reclaim_now (or "1")
Queue every stripe_alloc raid56 group that the
reclaim predicate picks: relocate below the threshold once enough
of its free space is trapped, compact above it once stripe_compact
percent of its length is trapped. The five-minute hysteresis is
ignored.
echo <start> > stripe_reclaim_now
Reclaim the group at that logical start (decimal or 0x hex, as
`btrfs inspect-internal dump-tree -t chunk` and the "reclaim: "
debug lines print it) whatever its trapped space:
compact it if it is above the threshold and has trapped stripes,
relocate it otherwise.
The threshold is the space_info's bg_reclaim_threshold, or 75
(BTRFS_DEFAULT_RECLAIM_THRESH) where that is 0, the non-zoned default,
since the request is explicit. Both forms keep the room check: the other
groups' claimable stripes, plus the one chunk the worker allocates when
those fall short, must take what moves. The write returns once the groups
are queued; it does not wait for the reclaim. The worker re-checks its
thresholds and would drop a group it was asked to reclaim, so a group
queued by this knob carries the kind of reclaim to do
(stripe_reclaim_forced), which the worker honours once and clears.
Errors: -ENOENT when no group starts there; -EINVAL for anything but a
stripe_alloc raid56 group (balance's vrange filter relocates
the others); -EBUSY when the group is read-only (a balance or scrub has
it); -EAGAIN when it is already on a list (queued, unused or new);
-ENOSPC when there is no room; -EROFS on a read-only or errored
filesystem. The worker takes the balance exclusive operation and gives the
list back when another holds it, so a request made while a balance,
replace, resize or device add/remove runs would wait unseen: it fails at
once with -EAGAIN and nothing is queued; retry when the operation is done.
The reclaim worker's own hold (a balance with no balance control) is not
refused, as the worker picks up the new entries.
Expected use: an idle-time job (cron) writes "all"; an operator writes the
start of one group taken from the "reclaim: " debug lines or from the
chunk tree. The queued groups are logged at debug level with the same
"reclaim: " prefix.
There is no in-tree btrfs sysfs documentation; the btrfs-progs sysfs
chapter should list the file.
Tested with stripe-reclaim-now-test.sh (raid56-wh-harness): with
background reclaim off nothing moves; one group is relocated alone, and a
group above the threshold with trapped stripes is compacted; "all"
reclaims and trapped space falls; no such group, not a number, a raid1
metadata group and a read-only mount fail with the codes above, and a
paused balance and a running replace fail with -EAGAIN; data verifies and
btrfs check is clean.
The stripe_meta block group part of this change moved to
"btrfs: stripe_meta: extend stripe reclaim to metadata block groups".
Zygo Blaxell [Tue, 20 Oct 2026 17:04:19 +0000 (13:04 -0400)]
btrfs: stripe_alloc: log compaction and trapped bytes in the reclaim log
Say in the reclaim debug lines whether a stripe group is being compacted
or relocated, with the trapped bytes the reclaim trigger counted, and
whether it was compacted or relocated. Without that, stripe-reclaim-test,
which counts reclaims in the log, could not tell automatic compaction from
relocation, and an octopus replace stalled behind a reclaim the log did not
explain.
The hunks of "btrfs: stripe_alloc: keep relocation's stripe runs until
relocation ends" (earlier in this series) that need code from "btrfs:
stripe_alloc: compact mostly live groups instead of skipping them",
which comes after it in this series: they are applied here, once "btrfs:
stripe_alloc: compact mostly live groups instead of skipping them" is
in, instead of with the rest of "btrfs: stripe_alloc: keep relocation's
stripe runs until relocation ends".
btrfs: stripe_alloc: compact mostly live groups instead of skipping them
Reclaim only takes a stripe_alloc group whose live data is below
bg_reclaim_threshold: moving everything out of a fuller group costs nearly
a group's worth of writes and needs that much claimable space elsewhere.
But trapped free space is not confined to emptyish groups. Under steady
COW churn a group can stay mostly live while every commit leaves
partially used stripes behind. Metadata under stripe_meta showed it
first: on a raid6 metadata fill the trapped share of the metadata free
space reached 88% with no group ever falling below the threshold:
reclaim never ran, and the space never came back.
A group like that does not need all of its data moved. The trapped space
is in its partially used stripes; moving only the extents in those
stripes makes them whole again. An extent spanning several stripes frees
its bytes in all of them when it moves, so the stripes it shares with
others must move too, transitively: the selection is the closure of the
partial stripes under "shares an extent with".
Compaction runs from the reclaim worker, for a group whose live data does
not fall below the threshold, once its trapped space reaches
stripe_compact percent of the whole group. (The test whole relocation
uses, trapped share of the FREE space, never admits a mostly live group:
its little free space is mostly whole stripes.)
- The group goes read-only as for relocation. After a commit, so the
free space cache and the extent tree's commit root agree, the partial
stripes are read from the free space cache and the closure is found
with a union-find over the group's stripes in one pass over its extent
items, each union carrying the live bytes of its extents.
- If the closure would move more than half the group's live bytes, the
group is relocated whole and removed instead: that costs about the
same and also gives back the group's raw space.
- Otherwise relocation runs with find_next_extent() skipping every
extent outside the selection, through both data stages, and the group
is committed (so what moved out is unpinned) and made writable again
instead of removed. replace_file_extents() rewrites only the pointers
to selected extents: the others did not move and have no new location
(a leaf referencing both kinds is common).
Balance, device shrink and removal still relocate whole groups
(btrfs_relocate_chunk() is unchanged; btrfs_compact_chunk() is the new
entry point). sysfs stripe_compact is the trapped percent of a group's
length that makes it a candidate (default 10; 0 turns compaction off),
and stripe_run_stats counts compaction passes, fallbacks to whole
relocation, stripes evacuated and bytes moved.
The destination is ordinary relocation's: the moved extents need whole
claimable stripes in other groups, and a commit during the pass can leave
a partial stripe at the tail of a relocation run there. The admission
test for compaction therefore asks for half the group's live bytes
claimable elsewhere, the most a compaction can move.
The stripe_meta block group part of this change moved to
"btrfs: stripe_meta: extend stripe reclaim to metadata block groups".
btrfs: stripe_alloc: count whole stripes stranded behind live runs as trapped (reclaim part)
The hunks of "btrfs: stripe_alloc: count whole stripes stranded behind
live runs as trapped" (earlier in this series) that need code from
"btrfs: stripe_alloc: hand groups full of trapped free space to the
reclaim worker", which comes after it in this series: they are applied
here, once "btrfs: stripe_alloc: hand groups full of trapped free space
to the reclaim worker" is in, instead of with the rest of "btrfs:
stripe_alloc: count whole stripes stranded behind live runs as trapped".
btrfs: stripe_alloc: hand groups full of trapped free space to the reclaim worker
Zoned filesystems mark a block group for reclaim as soon as its
unusable bytes reach bg_reclaim_threshold percent of its capacity, so
space that can no longer be written is recycled without an operator
running balance. stripe_alloc had no equivalent. The non-zoned
trigger consults the threshold only where a free drops a group's used
bytes across it, and the test hosts were already running every mount
with bg_reclaim_threshold=75 when this was measured: trapped tails
still accumulated until balance (1.86 GiB of 6.5 GiB after a few hours
of churn), while every byte-based check counted them as free, because
that predicate keys on used bytes only and fires only at the crossing
-- a group hollowed out after it was already below the threshold, or
one whose used bytes never moved while its stripes trapped, is never
queued, and a group at the same used fraction with nothing trapped is.
Mark a group for the existing reclaim worker when its trapped bytes
reach the threshold; the worker re-checks with
should_reclaim_block_group(). The predicate, factored out as
btrfs_stripe_bg_wants_reclaim(), compares the trapped bytes with the
group's free space (length minus used) rather than its length -- a
stripe_alloc group also holds claimable whole stripes, and in practice
fragmentation settles around two thirds of the group trapped with a
fifth used, nothing left worth writing into yet below any sensible
fraction of the length -- and requires room for the group's live data
in the rest of the claimable supply after the bytes already promised to
admitted writers, so a group that cannot be relocated is left alone
until space is freed instead of being retried every commit while the
read-only transitions starve writers.
The trigger runs from the cleaner, which the transaction thread wakes
every cycle whether or not a transaction exists, as well as at the
commit-time rescan: on an idle filesystem nothing rescans, and a group
already on another list (a chunk created in the running transaction is
still on trans->new_bgs) cannot be linked to the reclaim list, so the
five-minute hysteresis stamp is taken only when the mark actually
lands. The worker re-checks whatever it takes, so a stale read costs at
most one wasted attempt. bg_reclaim_threshold defaults to 0 on
non-zoned filesystems, so nothing changes until the sysfs knob is set;
the default is left for review. Relocation needs inc_block_group_ro()
to succeed, hence the earlier fix.
The worker does not defer a queued stripe group that has bytes reserved
or pinned: it was queued for space no waiting frees, and under COW churn
it can have blocks reserved or pinned at any moment (under stripe_meta,
a metadata group always does).
Relocation waits for in-flight allocations and writers on its own.
The stripe_meta block group part of this change moved to
"btrfs: stripe_meta: extend stripe reclaim to metadata block groups".
btrfs: stripe_alloc: count open stripe run remainders against data reservations (allow_rmw part)
The hunks of "btrfs: stripe_alloc: count open stripe run remainders
against data reservations" (earlier in this series) that need code from
"btrfs: stripe_alloc: persist nocow runs across commits and remounts",
which comes after it in this series: they are applied here, once "btrfs:
stripe_alloc: persist nocow runs across commits and remounts" is in,
instead of with the rest of "btrfs: stripe_alloc: count open stripe run
remainders against data reservations".
btrfs: stripe_alloc: persist nocow runs across commits and remounts
A nodatacow inode's private stripe run used to close at every
transaction commit like all runs, so a slowly appended nocow file burned
a fresh stripe per commit -- and after a remount its partial stripe's
free tail was abandoned outright. Neither cost buys anything: the run
machinery's commit-time closing exists for invariant I2, and a nocow
stripe holds only the owner's write-hole-waived data, so there is
nothing for I2 to protect.
Keep NOCOW-class runs open across commits: the commit-time retirement
and its drain predicate skip them (their extents insert without the
window-sequence deferral, which is fine -- the data is on disk when the
ordered extent finishes, and later same-stripe writes can tear only the
owner's own data). They still close on forced quiescing (read-only,
removal, unmount) and now on the owning inode's eviction, so a cached
but idle inode cannot pin a claimed tail forever.
Across remounts, re-adopt instead: when a nocow allocation's hint
points into a partial stripe that the committed extent tree proves is
wholly owned by the allocating inode, claim exactly the stripe's free
tail (a new exact-range claim that verifies every byte is free before
removing; nothing else can consume free space inside a partially used
stripe, so verify-then-remove cannot race) and continue the run at the
old frontier. Appends to a nocow file then pack sequentially through
commits, evictions and remounts alike.
The re-adoption lookup must not block. It runs from
btrfs_alloc_from_inode_stripe_run(), i.e. inside find_free_extent(),
which holds space_info->groups_sem for read, and stripe_extents_owned_by()
does a full btrfs_search_slot() on the extent tree. The reverse order is
longstanding upstream -- delayed ref processing holds extent tree locks
and then calls find_free_extent(), which takes groups_sem -- so waiting
here closes a cycle. Both sides only take groups_sem for read, which is
not enough: it is an rw_semaphore and block group creation and removal
take it for write, so a writer queued between the two readers blocks the
second one. The check cannot be hoisted above the lock, since it is a
question about the specific stripe the allocator has just settled on, but
it does not have to block: it is read-only and advisory, and every caller
already treats "not provably ours" as a reason to decline the
optimisation rather than as a fact about the extents. So set path->nowait
for the groups_sem caller and let -EAGAIN fall into the existing error
paths; the cost on contention is a re-adoption that does not happen.
btrfs_stripe_nocow_writable() keeps the blocking search -- it runs from
the nocow check with no groups_sem held, and a spurious "not ours" there
would force COW on an extent that does not need it.
Whether a nocow extent can be written in place at all is decided by the
stripe_alloc_allow_rmw policy, and with nothing waived btrfs_stripe_nocow_
writable() forces COW for every such write. A private run would then hold
only COWed extents and unwritten preallocation, and its survival would
only strand the whole stripes freed inside its range (an fsstress fill to
ENOSPC left hundreds of such runs in one group, holding 99% of its free
whole stripes). So prealloc and nodatacow extents are steered into a
NOCOW run only when the policy lets that kind of extent be written in
place, and a NOCOW run outlives commits only when the policy allowed
in-place writes when it was opened. The verdict is recorded in the run,
so the commit's close and settle predicates agree when the policy changes
underneath them.
full_stripe_len is a power of two only when the data stripe count is
(raid5 over 4 devices gives 192K), so stripes are rounded and tested for
alignment by division, in one helper each, never with round_down() or
IS_ALIGNED().
The cleaner does not delete an unused block group while a stripe run
still covers it (a persistent nocow run whose extents were all freed, or
the running transaction's metadata run): a run's unallocated remainder is
counted in none of used, reserved and pinned, and its tail would later be
added to the removed group's free space cache. The group stays read-only
and is left for a later pass; its runs close by the next commit.
The nocow check passes an IOCB_NOWAIT write's nowait down to its extent
tree search; when that cannot answer, the extent is treated as not the
inode's own and the write falls back to COW, which a NOWAIT write refuses
with -EAGAIN.
btrfs: stripe_alloc: track per-sector liveness and pad from the map (allow_rmw part)
The hunks of "btrfs: stripe_alloc: track per-sector liveness and pad
from the map" (earlier in this series) that need code from "btrfs:
stripe_alloc: allow-rmw policy, and write-in-place for isolated nocow
extents", which comes after it in this series: they are applied here,
once "btrfs: stripe_alloc: allow-rmw policy, and write-in-place for
isolated nocow extents" is in, instead of with the rest of "btrfs:
stripe_alloc: track per-sector liveness and pad from the map".
btrfs: stripe_alloc: tunable park deadlines, and say why padding declined (allow_rmw part)
The hunks of "btrfs: stripe_alloc: tunable park deadlines, and say why
padding declined" (earlier in this series) that need code from "btrfs:
stripe_alloc: allow-rmw policy, and write-in-place for isolated nocow
extents", which comes after it in this series: they are applied here,
once "btrfs: stripe_alloc: allow-rmw policy, and write-in-place for
isolated nocow extents" is in, instead of with the rest of "btrfs:
stripe_alloc: tunable park deadlines, and say why padding declined".
btrfs: stripe_alloc: allow-rmw policy, and write-in-place for isolated nocow extents
Re-enable write-in-place for nodatacow files and preallocated extents
under stripe-exclusive allocation, with the understood caveat that the
write hole cannot be prevented for data that opts out of COW: an
in-place write RMWs its stripe's parity, so a degraded crash can tear
the stripe. What CAN be guaranteed is the blast radius: in-place is
permitted only for extents whose full stripes are isolated to the
writing inode, so such a crash can tear only the writing file's own
data -- the nodatacow contract, no worse.
The gate is per extent, decided where nocow eligibility is already
checked: fast path, the stripe belongs to one of the inode's own
NOCOW-class runs (which only ever held its extents); slow path, the
committed extent tree proves sole ownership (single plain data ref,
count 1, matching root and objectid; anything shared, foreign or
metadata fails). The fully-free claim rule keeps uncommitted foreign
extents out of partially used stripes, so the committed tree is
authoritative. Extents that fail -- anything allocated before stripe
isolation existed, extents shared through reflink or snapshots,
relocated extents -- simply stay force-COWed, and because their rewrite
is steered into the inode's private NOCOW run, the next overwrite of
the same data passes: legacy nocow files migrate themselves to
isolation in one COW generation, with no tool and no flag day.
Log-commit settling skips NOCOW-class runs: nodatacow data gets no
fsync survival guarantee (its own later in-place writes can always
tear it), so closing the run would trap its tail for nothing. The
write-hole debug checker skips groups that hosted NOCOW-class runs,
like relocation-used groups, since isolated in-place writes land in
stripes whose runs have drained. Settling, the fsync steering and the
checker come later in this series, so these parts of the change are in
its "(fsync part)", "(fsync part 2)" and "(debug part)" patches.
The interface is a word-list policy naming the cases in which
stripe_alloc may permit the legacy unsafe RMW, each independently:
nodatacow in-place writes for nodatacow files' extents
prealloc in-place writes into preallocated extents
fsync waive the close-at-log-commit guarantee: no settling, no
per-inode LOG steering, no carry-forward; logged stripes
may be extended and RMWed as without stripe_alloc
It is "allow_rmw", not "allow_overwrite": the fsync case overwrites
nothing, but all three permit read-modify-write of stripes that a
degraded crash can then tear. The nodatacow and prealloc cases require
the per-extent stripe isolation above (the blast radius stays confined
to the writing file); fsync restores the exposure stock btrfs has,
where a degraded crash may cost just-fsynced data, detectably, in
exchange for none of the log-commit costs.
The policy is persistent as the btrfs.stripe_alloc_allow_rmw property
on the top-level root directory, following stripe_alloc's precedent
(applied when the root inode loads during mount, before any user IO).
Words are separated by comma, space or colon. Everything defaults off:
plain stripe_alloc keeps forcing COW and keeps the full fsync guarantee.
Sync writes into a NOCOW-class run do not park: in-place overwrites arrive
one fsync at a time with nothing to merge and, unlike the datacow fsync
path, nothing kicks them before the page writeback wait (the sync park
deadline was most of a 3.7ms per-fsync regression against stock
nodatacow). Async writeback still parks there.
The ownership walk checks the extent item's size before reading its
inline backref: a data extent whose only backref is a keyed
EXTENT_DATA_REF item (the leaf had no room to grow the item) has none, and
counts as not owned.
Zygo Blaxell [Fri, 2 Oct 2026 06:41:44 +0000 (02:41 -0400)]
btrfs: stripe_alloc: pad a stripe's allocated sectors whose write is still to come (allow_rmw part)
The hunks of "btrfs: stripe_alloc: pad a stripe's allocated sectors
whose write is still to come" (earlier in this series) that need code
from "btrfs: stripe_alloc: isolate nodatacow and preallocated extents by
stripe", which comes after it in this series: they are applied here,
once "btrfs: stripe_alloc: isolate nodatacow and preallocated extents by
stripe" is in, instead of with the rest of "btrfs: stripe_alloc: pad a
stripe's allocated sectors whose write is still to come".
Zygo Blaxell [Thu, 1 Oct 2026 16:14:49 +0000 (12:14 -0400)]
btrfs: stripe_alloc: keep a stripe in the rbio cache until its run has written all of it (allow_rmw part)
The hunks of "btrfs: stripe_alloc: keep a stripe in the rbio cache until
its run has written all of it" (earlier in this series) that need code
from "btrfs: stripe_alloc: isolate nodatacow and preallocated extents by
stripe", which comes after it in this series: they are applied here,
once "btrfs: stripe_alloc: isolate nodatacow and preallocated extents by
stripe" is in, instead of with the rest of "btrfs: stripe_alloc: keep a
stripe in the rbio cache until its run has written all of it".
btrfs: stripe_alloc: count nocow run lifecycles and stranded stripes in sysfs (allow_rmw part)
The hunks of "btrfs: stripe_alloc: count nocow run lifecycles and
stranded stripes in sysfs" (earlier in this series) that need code from
"btrfs: stripe_alloc: isolate nodatacow and preallocated extents by
stripe", which comes after it in this series: they are applied here,
once "btrfs: stripe_alloc: isolate nodatacow and preallocated extents by
stripe" is in, instead of with the rest of "btrfs: stripe_alloc: count
nocow run lifecycles and stranded stripes in sysfs".
btrfs: stripe_alloc: isolate nodatacow and preallocated extents by stripe
Preallocated extents and nodatacow files' extents are candidates for
write-in-place, which reintroduces the raid56 write hole for every
stripe such a write touches: the RMW recomputes parity that also covers
whatever else shares the stripe. Before write-in-place can be
re-enabled for them (a later change; stripe_alloc still forces COW
today), their placement must guarantee the blast radius: such an extent
must never share a stripe with any other file's data.
Steer them into private per-inode stripe runs of a new NOCOW class,
reusing the log-active inode machinery: runs owned by one inode, never
in the shared band slots, found by owner-and-class lookup. Successive
allocations of the same file pack sequentially into the file's own
stripes; different files, and the datacow/relocation/log classes, never
share a stripe with them. A preallocation signals itself through a new
btrfs_reserve_extent() parameter; nodatacow files are recognized by the
inode flag. Placement remains best effort: when no fully-free stripes
are left for a private run the allocation falls back to the shared
runs, which is safe -- an extent that lands in a shared stripe simply
stays force-COWed when write-in-place arrives.
Zygo Blaxell [Sun, 25 Oct 2026 10:27:49 +0000 (06:27 -0400)]
btrfs: stripe_alloc: do not count a writeback ENOSPC drop's give-back as supply
When buffered writeback fails with ENOSPC under stripe_alloc,
cow_file_range() drops the range: the folios end writeback unwritten, the
range's may_use and its margin are released, and the extents it had
already allocated are freed, so their stripes come back as claimable at
the next commit's rescan. The data was acknowledged by write(2), so it
is lost; that is the bug the earlier patches remove at its causes. This
patch is about what happens next.
The write admission gate ("btrfs: stripe_alloc: gate data admission on
claimable whole-stripe supply", earlier in this series) sees the
give-back as supply. The writer blocked on the FLUSH_DATA ticket whose
flush caused the drop is admitted when the same reclaim pass commits
(COMMIT_TRANS is one of the data flush states) and the rescan returns
the dropped extents' stripes. The supply was not enough for the range
that was just dropped, so the next range fails the same way. With split
pieces stranding stripes (as they did before "btrfs: stripe_alloc:
pessimistic data reservation margin", earlier in this series, trimmed
them to the group's stripes), one writer of 64 MiB got every write(2)
acknowledged and lost 62.8 MiB in five identical drop cycles of 12.6
MiB. Stock btrfs releases the same way after a cow_file_range() ENOSPC;
it just does not expect one, because its data reservation is exact.
Keep a quarantine, bytes_stripe_quarantine, that the gate adds to every
non-relocation data admission:
- On the drop, add the range length plus one full-stripe unit per extent
the range had allocated: each extent claimed at most its length rounded
up to its group's full stripe, so this bounds what the drop gives back.
The whole stripes covering the range plus one unit would not: in the
case above, the range was 13172736 bytes and its first 10747904 were
allocated as 448K pieces that each claimed 640K in 320K groups, so the
drop gives back about 15.3 MiB of stripes, where need(L) + unit is
13.6 MiB. Capped at the space info's size. Relocation's own drops do not count:
its data is never acknowledged to a writer.
- Release it only by claimable stripes that come back for real: whole
stripes completed by extents unpinned at a commit (deletes, overwrites,
relocation, compaction), measured with the rescan's own rule, and the
claimable of a new group (a new chunk, or a group whose free space only
now finished loading). Not a group back from read-only: a failed
relocation at the fill edge returns stripes the supply had before, and
reclaim's read-only cycling would drain the quarantine. The rescan
cannot be used either: it recomputes the total and cannot tell the
dropped extents' stripes from real frees.
- Relocation is not held back by it, since relocation is what frees
stripes. It is cleared when stripe_alloc is turned off at runtime, and
it is not persistent, so an unmount clears it.
The ticket that waited on the dropping flush now runs out of flush states
with no new supply and fails with ENOSPC, and so does every later write
until something is freed. The trade-off: ENOSPC in place of loss of
acknowledged data, and stripes that were only temporarily held at the
drop (run tails that a commit returns) stay quarantined until real frees
or a new group arrive.
The counter is in sysfs (bytes_stripe_quarantine), in the space info
dump, and in the data writeback ENOSPC line; each drop logs the new total.
[zygo: 7.3 port: 7.3's btrfs_add_bg_to_space_info() still takes fs_info,
context adjusted; the change itself is unchanged]
Zygo Blaxell [Sat, 10 Oct 2026 04:19:18 +0000 (00:19 -0400)]
btrfs: stripe_alloc: skip RAID5/6 groups that are mapped as RAID1/RAID1C3
Since commit 49ba3a3c0d67 ("btrfs: disguise single-data-RAID56 as
RAID1/RAID1C3"), a RAID5 chunk with one data stripe (two devices) or a
RAID6 chunk with one (three devices) keeps its RAID5/6 type on disk, in
the chunk map's on_disk_type and in the block group's flags, but is
mapped as RAID1/RAID1C3. Such chunks appear when only two or three
devices have unallocated space, e.g. on devices of unequal size.
stripe_alloc and stripe_meta decide by the block group's flags, so they
kept opening stripe runs in these groups. Their IO goes down the
mirrored path and never reaches raid56, so no write ever marked the
runs' sectors written: every ordered extent there ended with all its
sectors unwritten, and the groups paid stripe_alloc's per-allocation run
bookkeeping for a write hole they do not have. (Nothing was trapped there:
their full stripe is one sector, since btrfs_full_stripe_len() reads the
chunk map's type.)
Record at block group creation whether the chunk map's type differs from
its on_disk_type in this way (BLOCK_GROUP_FLAG_RAID56_MIRRORED), and
leave such groups to the normal allocator.
Their flags still match the RAID5/6 allocation profile, so writes land in
them, and with two of five raid6 devices missing every new data chunk is
one: the admission supply must include them, or every degraded write fails
with ENOSPC. Each space_info counts the free space of its writable
single-data-stripe RAID5/6 data groups, following ctl->free_space, which
every free space change goes through; btrfs_stripe_unmanaged_sync() runs
when a group joins its space_info and after each read-only transition, and
the read-only guard treats a counted group's free space as the supply it
takes away. Groups of other profiles are not counted: a RAID5/6 write
cannot use them. The full-stripe widths the admission charge is taken
over leave these groups out, as their full stripe is one sector.
[zygo: 7.3 port: 7.3's btrfs_add_bg_to_space_info() still takes fs_info;
7.3 still calls a chunk's device extent size stripe_size
(btrfs_calc_stripe_length()); context adjusted, the change itself is
unchanged]
Zygo Blaxell [Sun, 4 Oct 2026 16:26:45 +0000 (12:26 -0400)]
btrfs: stripe_alloc: count refused rbio pins and track rbio cache high water
The rbio cache keeps a stripe its run has not finished writing pinned, at
most RBIO_PIN_MAX (256) of them; the next pin is refused and that stripe
is cached evictable. If it is evicted before its run's next write into
it, that write reads back the sectors already written, unverified: the
case the pin exists to avoid. Nothing counted the refusals, so whether
the limit is ever reached was unknown.
stripe_park_stats gains:
rbio_pin_refused pins refused at RBIO_PIN_MAX (every attempt)
rbio_pinned_now pinned rbios now (nr_pinned: what the limit counts)
rbio_pinned_live of those, pins still needed: re-checked against the
runs at read time, as the prune does. A pin whose run
has finished the stripe stays counted until its entry
is evicted or cached again.
rbio_pinned_hwm high water of pinned rbios since mount
rbio_cache_now cached rbios now
rbio_cache_hwm high water of cached rbios since mount (above
RBIO_CACHE_SIZE only by live pins, which the prune
keeps)
meta_rmw_reads read-modify-writes of metadata/system stripes that had
to read (data_rmw counts these with the data ones).
The pin is what keeps a stripe of fsync log blocks
cached for the next log commit; a refused pin can bring
that read back.
The high water marks are only raised under the cache lock. Reading the
file walks the cache once under the cache lock (1024 entries at most).
nr_pinned counts a pin until its entry is evicted or cached again, also
after its run has finished the stripe, so the limit filled with stale pins
within seconds under any fsync-heavy load (13k-87k refusals a minute)
while buffered writeback needed 3-14. Two things release pins:
- A sweep, rbio_unpin_stale(), re-checks every pin against its run and
clears the stale ones under the cache lock. It runs when a pin is about
to be taken with RBIO_PIN_LOW (3/4 of the limit) or more counted, at
most once per RBIO_PIN_SWEEP_INTERVAL from that path, and at every
commit right after btrfs_retire_open_stripes(), which ends every run.
- If the pins left after a sweep (all live) are still RBIO_PIN_LOW or
more, the transaction kthread is asked to commit now, once per
transaction. After the commit a stripe's earlier writes are no longer
at risk from a read-back: data checksums are committed and the log
blocks are dead.
stripe_park_stats also has rbio_pin_swept and rbio_pin_commit_requests.
Zygo Blaxell [Fri, 2 Oct 2026 06:41:44 +0000 (02:41 -0400)]
btrfs: stripe_alloc: pad a stripe's allocated sectors whose write is still to come
A stripe run's first partial write into a stripe cannot pad today: the
stripe's other sectors are allocated (live) to writes that are still in
flight, so the pad oracle calls them live and the write goes down as a
read-modify-write of the whole stripe. With the stripe pin keeping the
stripe cached for every later write, this first write is the only data RMW
left in a stripe run (traced on the strip-fault fixture: every remaining
data RMW read was a first write), and the read it does is of sectors
nobody has written: it can only fetch garbage into the parity, and on a
device that returns wrong data it makes that parity disagree with what is
on the disk.
Use the run's written map: a live sector whose write has not completed
holds nothing anyone has written, so it is as paddable as a dead one.
Zeroing it is safe because its write is ordered after this one -- the
padding write holds the stripe lock, every raid56 write to the stripe
takes it, and rbio_orig_end_io() records a write's sectors in the written
map before it unlocks the stripe -- and nothing reads the sector before
its own write lands, since its extent is not committed until then. That
write replaces the zeros; it is a sub-stripe write into a stripe written
before, which the stripe pin serves from the cache.
A live sector that HAS been written still refuses the pad, as before:
that is now the precise meaning of the UNCOVERED decline. Nodatacow runs
keep no written map (their extents are rewritten in place for as long as
the inode lives) and are excluded. padded_unwritten in stripe_park_stats
counts the pads that zeroed such sectors.
Zygo Blaxell [Thu, 1 Oct 2026 16:14:49 +0000 (12:14 -0400)]
btrfs: stripe_alloc: keep a stripe in the rbio cache until its run has written all of it
A stripe run hands out its sectors in order, so a stripe it allocates is
often written in pieces: a write that ends inside the stripe leaves the
rest to later writes, and writeback allocates a whole range before any of
its bios complete, so even a stripe the frontier has already passed can
still have writes in flight. The later writes cannot pad (the earlier
sectors are live), so unless the stripe is still in the rbio cache each
is a read-modify-write that reads the earlier sectors back from the
devices. Those sectors are not yet covered by the csum tree (their
ordered extents have not finished), so what the devices return goes into
the new parity unverified. A device that returns wrong data there
destroys the earlier write, whose only intact copy at that point may be
the parity: the strip-fault fixture (a device that returns random bytes
for one strip per stripe) lost a file exactly this way, with every RMW of
the run in this same-run class.
The cache already prevents that read when the stripe is still cached: the
later write steals the earlier write's pages (lock_stripe_add()) and reads
nothing. But the cache is an LRU that any other stripe's write can push
such a stripe out of. Pin it instead. Each run gets a written map beside
its live map, set from each completed raid56 write's covered data sectors
(rbio_orig_end_io(), before the cache decision in unlock_stripe()).
cache_rbio() pins an rbio whose stripe is still waiting for writes
(btrfs_stripe_run_incomplete()): an open run's allocation frontier lies
inside it -- the rest is not even allocated yet, so every sector
allocated so far may already be written -- or a run covering it, open,
closed or kept for relocation, has live sectors there not yet written.
The prune skips live pins and re-checks each against the runs, so a pin
ends as soon as the stripe is complete or its run goes away. Nodatacow
runs are excluded from the written check: they are rewritten in place
for as long as the inode lives and are never complete. RBIO_PIN_MAX
bounds the pinned entries; no memory is allocated for this, the stripe's
pages are only released later. A completed write is marked in the run
that owns it, by [run->start, run->end): the maps reach past run->end
(they span at least BTRFS_STRIPE_RUN_LIVE_STRIPES stripes), so a write
must never be recorded in an earlier run whose map merely overlaps it.
written_not_live in stripe_park_stats counts completed-write sectors that
are not live in their run, which should stay near zero.
This removes the read rather than adding a recovery step for it: nothing
here depends on checksums, sectors without them, or the reconstruction
path. What remains is a stripe's first partial write, which reads only
sectors nobody has written yet (traced on the fixture: every remaining
data RMW read was of that kind). rbio_pinned in stripe_park_stats counts
the pins.
An ordered extent can end without writing all of its sectors (a write
error, truncation of folios never submitted, a short direct write, teardown
of an extent whose IO never started). When it reports to its run, its
whole range is marked written: no write of those sectors is still coming,
which is all the pin (and any other waiter on the written map) needs to
know. Marking them written rather than dead keeps them from being padded,
so nothing a truncated extent kept is zeroed. Waiters are woken while the
run lock is held, since the run and its group can be freed once it is
dropped.
[zygo: 7.3 port: 7.3 has no per-chunk stripe length (map->stripe_len,
bioc->stripe_len, the two-argument btrfs_stripe_nr_to_offset()):
BTRFS_STRIPE_LEN is used instead; the change itself is unchanged]
Zygo Blaxell [Thu, 1 Oct 2026 04:57:50 +0000 (00:57 -0400)]
btrfs: stripe_alloc: keep relocation's stripe runs until relocation ends
Relocation preallocates each destination extent and writes the data into
it later, after the commit that ends relocate_block_group(). Under
stripe_alloc the preallocation lands in a BTRFS_STRIPE_RUN_RELOC run,
which drains at once (a preallocation has no IO to report) and is freed
when that commit closes it. By the time the relocated data is written,
no run covers its stripes: a write that does not cover a whole stripe
can neither park to merge with its neighbour nor pad, and goes down as a
read-modify-write. Relocated extents rarely line up with stripes, so
that is roughly one RMW per relocated extent: an audited data balance on
a 4-device raid5 made 2247 of them, every one NO_RUN, every one written
by the data reloc inode into its own preallocation, and none over
another owner's committed data (the stripes hold only relocation's own
orphaned copies) -- harmless, but every one a stripe read.
Keep RELOC runs after they close and drain, as metadata runs already are
for the same reason, until relocation releases them from
btrfs_release_data_reloc_bg(), which it calls on its way out (after the
pointer update, before the next relocation can allocate). A retained
run that has nothing in flight is not waited for by the commit's drain,
and any forced quiesce of its block group (read-only, removal, unmount)
releases it first. While retained, its range stays unclaimable and its
stripes are relocation's alone, so the writes into them park and merge
or pad like any other run's.
reloc_run_retained / reloc_run_released in stripe_run_stats count the
runs kept and released.
The open stripe self-test frees the runs it leaves retained, as unmount
does.
btrfs: stripe_alloc: say at mount whether the policy is on
Nothing announced that stripe-exclusive allocation was on. The
btrfs.stripe_alloc property applies when btrfs_fill_super() reads the
root directory's inode and its properties, and btrfs_enable_stripe_alloc()
prints its line only for a set on a live filesystem, so mounting a
filesystem whose property was already set said nothing.
An absent line therefore meant "off" and "on" equally well, and the only
way to answer the question was to look at a live filesystem -- no use
when the filesystem is someone else's, or when all that is left is a log
from before a crash. Analysing a lockup from a filesystem whose
btrfs.stripe_alloc property was set, I read that silence as the policy
being off and said so, which was wrong.
State it once in btrfs_fill_super(), after the root inode has been read
so the property has had its say. The property path keeps its own line
for a set on a live filesystem, where there is no mount to report. At
mount that line still comes too, so a mount with the property set logs
the state twice, until "btrfs: stripe_alloc: say at mount whether the
policy is on (debug part)" later in this series quiets it there: it
needs the runtime enable worker of the write-hole checker.
Nothing is printed when the policy is off, which keeps the common mount
quiet and makes the absence of the line mean exactly one thing.
btrfs: stripe_alloc: count nocow run lifecycles and stranded stripes in sysfs
Add a stripe_run_stats file beside stripe_park_stats with lifetime
counters for the paths the previous two patches touched, so a test can
show that it exercised them rather than merely passed: private NOCOW
runs opened, claim candidates refused for overlapping a live run, bytes
of whole stripes the scans found stranded behind live runs, and rescans
requested by a freed run to credit its stripes back.
A persistent nocow run whose allocations have all been freed is dead: it
owns no data and only strands the whole stripes in its range (99% of the
free whole stripes of every read-write group sat inside such runs after an
fsstress fallocate load). btrfs_stripe_run_note_free(), from
btrfs_add_free_space(), clears the live bits of freed sectors inside a
persistent run, and close_block_group_stripe_runs() closes a persistent
run that handed out space, has nothing in flight and no live bit set, on
the normal commit path that frees it and marks its group for the rescan.
A run with any live byte stays open, so a slowly appended nocow file still
burns no stripe per commit; a run whose map was retired is never taken as
dead. The free path only clears bits under bg->stripe_run_lock.
nocow_run_dead_closed counts the closes.
btrfs: stripe_alloc: count whole stripes stranded behind live runs as trapped
The data admission gate admits a reservation while the whole-stripe
supply, bytes_stripe_claimable, covers it, and the per-commit scan
measures that supply as the block group's wholly free full stripes.
But a wholly free stripe inside a live stripe run's range cannot be
claimed: btrfs_stripe_run_range_usable() rejects any claim overlapping
a run object, because a second run over the same range would make the
range-to-run lookups ambiguous. The run was claimed over the stripe,
the extents allocated there were freed again, and the run has not been
freed yet. For a COW run that window closes at the next commit; a
nodatacow inode's private run deliberately survives commits and lives
until the inode is evicted.
On a raid56 filesystem filled to ENOSPC by fsstress (thousands of
fallocated files, so thousands of cached inodes each holding a private
NOCOW run) a live walk of the free space cache showed the accounting
exact -- the wholly free stripes summed to bytes_stripe_claimable to the
byte -- and 99% of them inside the ranges of open NOCOW runs with no
inflight IO: 22.8 MiB of 22.8 MiB behind 387 runs in one group, 73.4 MiB
of 73.4 MiB behind 976 runs in another. Every claim failed in O(1) on
the "no claimable run" verdict, every write() admitted against the 99 MiB
of phantom supply failed at writeback, and the data was dropped ("data
writeback allocation ... returned ENOSPC despite reservation margin"),
about 100,000 delalloc ranges per run of the degraded acceptance suite
where the stock allocator drops none.
Have the scan take such stripes out of the claimable supply: count them
in stripe_unusable, which btrfs_space_info_used() already charges so the
gate cannot admit against them, and record them separately as
stripe_stranded so that btrfs_stripe_bg_wants_reclaim() does not ask
relocation to recover space that comes back by itself. When a run is
freed, mark its group for rescan so the next commit credits the stripes
back. The space_info total is exposed as bytes_stripe_stranded in sysfs
and in the space info dump.
This makes the gate honest -- write() gets the ENOSPC instead of
writeback dropping the data -- but does not return the stranded space
while the runs live; that is the run lifecycle's business.
The space_info totals are kept equal to the sum of the armed groups:
claim, arm, disarm and rescan update a group's value and the total
together under sinfo->lock, and the commit scan sums the groups under
sinfo->lock while it holds groups_sem. Reading the groups unlocked and
assigning the totals later lost or doubled updates made in between, and a
group made read-only could have its whole claimable supply counted again
until the next commit, admitting writes that writeback could not place.
The scan does not restore the stripe_meta margin and reserve when
stripe_alloc was disabled after it started.
btrfs: stripe_alloc: show per-group trapped bytes in the space info dump
btrfs_dump_space_info() prints each block group's available bytes, and
under stripe_alloc those include the free space trapped in partly used
stripes, which no allocation can use until the group is reclaimed
(stripe_unusable). An ENOSPC dump then shows space in the groups that
cannot be had.
Print stripe_unusable next to zone_unusable and leave it out of the
available bytes. A group whose trapped bytes have not been counted yet
(stripe_unusable_ready not set) prints 0 and the old available figure.
btrfs: stripe_alloc: gate data admission on claimable whole-stripe supply
Wire stripe_claimable into the DATA reservation path so write() returns -ENOSPC
when there is no fully-free whole stripe to place the write crash-safely --
matching statfs f_bavail, which already subtracts the trapped partial-stripe
space. Add stripe_claimable_admit() and AND it into the two data-admit sites:
__reserve_bytes() and btrfs_try_granting_tickets() (the latter is required, or a
flushed data ticket would be granted by the plain used<=total rule, bypassing the
gate). Scoped to raid56 stripe_alloc DATA (stripe_margin_unit != 0); metadata,
non-raid56 and zoned space_infos are unaffected.
The gate refuses at reservation time, before the range becomes delalloc and later
parks at writeback with nowhere to land -- so ENOSPC is clean and there is no
fill-edge parking collapse.
Phase 1 is pessimistic: it gates on bytes_stripe_claimable alone, which errs LOW,
so it can refuse a write that would have landed in a partially-open stripe
(over-refuses by the open remainder, healed by the commit rescan raising claimable
and the FLUSH_DATA ticket retry). A later change adds bytes_stripe_open to the
bound for statfs-exact behavior.
What the gate charges, and what it holds against:
- A data reservation is charged need(L): the largest roundup(L, f) over
the full-stripe widths f of the filesystem's raid56 data groups, with L
its data bytes without the per-extent probe margin. The margin is
collateral that stays accounted (in bytes_may_use as the probe, then in
bytes_stripe_margin) until the extent is written, so later admissions
already see it; charging it inside the roundup would ask the supply for
it twice. Groups narrow at the fill edge, which the max over widths
covers. fs_info records the distinct widths next to stripe_margin_unit
and never forgets one; with more widths than it holds, the charge falls
back to L + unit - sectorsize. The probe margin reaches the gate through
btrfs_reserve_data_bytes(), __reserve_bytes() and the ticket. Rounding is
by division (stripe_round_up(), div64_u64()): the widths are not powers
of two, and round_up() would charge 512K for 384K of 192K stripes.
- The held margin, bytes_stripe_margin, is added to the admitted bytes:
it is owed to writers already admitted, and each commit shrinks the
supply by the tails it traps.
- The delalloc probe margin is not released until the metadata
reservation has charged it into bytes_stripe_margin (buffered write,
page_mkwrite, block truncation, direct IO, log-tail carry,
btrfs_delalloc_reserve_space()), so concurrent writers cannot be
admitted into each other's released margins. The v1 cache write-out,
which reserves no metadata, releases it at once.
- A preallocation has no outstanding extent, so it holds one stripe in
bytes_stripe_margin from the reservation until its allocations land.
The hold is charged inside __reserve_bytes(), in the section that grants
the bytes (directly or by btrfs_try_granting_tickets()), and handed back
through btrfs_alloc_data_chunk_ondemand_held() /
btrfs_reserve_data_bytes_held() to fallocate, zero-range, relocation's
cluster preallocation and the encoded write.
- Relocation is not charged the per-extent margin for extents already
allocated; its reservation is charged roundup(L, unit) plus one stripe,
what its own open runs can lose at the next commit. It has its own
flush type, BTRFS_RESERVE_FLUSH_DATA_RELOC, under the same bound but
never waiting: a relocation waiting on the gate held the exclusive
operation and the group it was emptying while the gate waited for the
stripes it was to free. Since a reservation that cannot ticket never
reaches the flush states that allocate data chunks, a refused relocation
reservation forces data chunk allocations while they succeed and
retries, and fails only when that does not help.
- The group dedicated to relocation (fs_info->data_reloc_bg) serves only
relocation, so its claimable bytes are tracked as
bytes_stripe_claimable_reloc and ordinary reservations are admitted
against the supply without them. Relocation is admitted against the
whole supply.
- inc_block_group_ro() refuses the read-only transition while the
admitted bytes would no longer fit in the claimable supply without the
group; scrub, balance and reclaim retry later. The subtraction
saturates, since the group's claimable value can be read ahead of the
space_info total.
- The reclaim worker allocates a chunk before it relocates a stripe group
whose live bytes exceed the claimable supply of the other groups (the
reservation path does not: a live device shrink must fail cleanly at
its reservation rather than grow the data space_info).
[zygo: 7.3 port: 7.3 still has BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE (v1
space cache): BTRFS_RESERVE_FLUSH_DATA_RELOC goes next to it, in the
6.18 order; btrfs_add_bg_to_space_info() still takes fs_info.]
btrfs: raid56: adaptive park deadline from the parked-rbio backlog
Full-stripe batching parks a sub-stripe write's rbio for a deadline so the
writes that fill the rest of its stripe can merge into it, turning a
read-modify-write into one full-stripe write. The deadline is a fixed
per-filesystem value. It is long enough to catch those merges, but at a
small-file fill to ENOSPC -- where a run's stripes never fill because each
file's neighbours land in the next file's stripe rather than this one --
every partial write waits the deadline out, and then its 10x stuck cap, on
an arrival that never comes before padding to a full stripe and going down.
Lowering the deadline globally fixes that fill but throws away the merges
the deadline exists to catch on ordinary moderate writes.
Feed the deadline back from the parking machinery's own backlog instead of
guessing at the workload. Track stripe_parked_now, the number of
currently-parked rbios, incremented as a partial write parks and
decremented as it unparks. When it runs ahead of the stripe_park_congestion
knob (0 disables it, the default), a new async partial write takes the short
sync deadline through the existing sync-park path rather than the full one.
This is negative feedback: the shorter deadline drains the backlog, so once
it falls back below the knob later writes regain the full deadline and full-
stripe batching. No space-state estimate is needed -- a small-file flood
builds the backlog directly, while a large sequential fill (already full
stripes, never parked) and a moderate steady stream do not.
Measured on an eight-device raid5. A small-file balance-reclaim soak that
times out at the default deadline completes at a congestion of 4; on a
throttled moderate stream, where the backlog stays low, a congestion of 32
never trips, keeps the full deadline, and merges three times as much with
38% fewer RMW reads as the same filesystem under a flat 3ms deadline. The
gauge and a congestion_short counter join the stripe_park_stats sysfs file
so the knob can be set from measurement. Off by default, and no data-path
change: a shortened park still pads or reads exactly as it would have, only
sooner.
Data reservations are admitted against arithmetic -- free space minus
trapped fragments minus open-run remainders minus a per-extent margin --
and under sustained near-full churn the arithmetic and the claim rule
disagree for long enough that admitted buffered writes reach writeback
with nothing claimable left: measured, ~5.2-5.5k reserved writebacks
dropped per fsstress churn run, silently for anything not waiting on
fsync. Stock refuses the same write()s up front. Nothing between
"durable by fsync" and "refused by write()" is acceptable when an
operator accident fills the disk.
First step, accounting only: measure the constraint instead of deriving
it. bg->stripe_claimable counts bytes of fully free whole stripes --
exactly what btrfs_claim_free_stripe_run() can take, the complement of
stripe_unusable within each stripe. The commit rescan derives it from
the same pass that computes trapped bytes (free minus trapped) and is
the sole upward correction; incremental maintenance under
ctl->tree_lock only ever DEBITS -- removals round their decrement OUT
to every touched stripe, claims subtract exactly what they took. The
counter can therefore only under-count between commits, never
over-count.
Crediting freed whole stripes incrementally on the add side is
deliberately not done, having been tried and dropped. Several add
paths re-add free space that a low-level remove never de-credited --
the async discard trim (unlink_free_space / bitmap_clear_bits, then
do_trimming's re-add through __btrfs_add_free_space) and
btrfs_remove_free_space's middle-split tail re-add -- so crediting on
add double-counts and drives the counter ABOVE the authoritative scan.
That is the dangerous direction: once admission gates on this counter,
an over-count admits reservations against phantom supply that writeback
then drops, while an under-count is an early, clean write()-time
ENOSPC. Debit-only makes over-counting structurally impossible,
whatever a future re-add path forgets to de-credit. (Reproduced with
compress+autodefrag+discard=async on a legacy-converted raid5
filesystem, incremental ~= 2x scanned; credit_return attribution
confirmed the phantom entered through do_trimming's re-add, not the
open-stripe allocator returns.)
A WARN_RATELIMIT at the rescan catches the dangerous direction anyway
(incremental above scanned), which under a debit-only rule means a
missed consumption site, and localizes it; the clamp to the scanned
value keeps every rescan authoritative regardless. Rate limited rather
than _ONCE because such a gap recurs on every commit, and reporting
only the first hit hides that it is ongoing.
The space_info aggregate bytes_stripe_claimable follows the
stripe_unusable pattern (incremental between commits, re-totaled from
armed groups at the rescan), shows in the ENOSPC dump and in sysfs.
No admission change yet; that comes once the counter proves accurate
under the fill, churn, balance and reclaim suites. The by-size
fast-fail in the claim keeps its early exit: the point of measuring is
to make such inputs trustworthy, not to search harder around them.
Data block groups only for now; raid56 metadata and mixed block groups
join when the data counter has settled.
A trim takes a free range out of the tree, drops tree_lock for the
discard and adds the range back, and a rescan in that window would find
fewer claimable stripes than the incremental count (seen as "incremental
stripe_claimable ... above scanned" with discard=async). Each step
changes exactly the stripes the range belongs to, so the unlink debits
every one of them that was claimable (wholly free, not inside a live run:
the scan's rule) and the re-add credits every one that is claimable now,
in the tree_lock section that unlinks or links the range. Rounding
instead would over-debit a stripe per trimmed sub-stripe hole, and async
discard trims thousands after a delete. The wholly-free test falls back
to the bitmap lookup at offset_to_bitmap() as the other callers do, since
an extent entry starting inside a bitmap's window hides the bitmap from a
non-fuzzy search.
The nocow tail claim, btrfs_claim_stripe_tail(), now counts and removes
the tail in one ctl->tree_lock section, with claim_stripe_run_pieces()
doing the removal as for any claimed run, and fails with -ENOSPC unless
the whole tail is in the cache, debiting the
claimable count for what it removes in the same lock section. It counted under the lock and then
called btrfs_remove_free_space(), which returns 0 when the range is not
in the cache at all: two allocations for the same inode (two direct
writers, writeback racing fallocate) or a trim holding an entry out could
both "remove" the same tail and hand out the same extent. A self-test
claims the same tail twice.
[zygo: 7.3 port: 7.3 still tracks trimming ranges under
cache_writeout_mutex (v1 space cache); kept around the converted calls,
as on 6.18]
btrfs: stripe_alloc: track per-sector liveness and pad from the map
Padding used to ask where the run's allocation frontier was, which
cannot see an allocation abandoned below it -- a reservation released, an
allocation the finder discarded, an extent freed before its write was
ever issued. Runs now carry a bit per sector, set at allocation and
cleared by the new btrfs_open_stripe_write_abandoned() report, and
padding fills exactly the sectors that are dead: this replaces
btrfs_stripe_run_pad_start() with btrfs_stripe_run_pad_mask(). The map
is sized once, with headroom for frontier growth, because the growth
path holds the run lock where nothing may sleep; a run that would
outgrow its map stops growing instead. A closed run's final stripe
overhangs its shrunk end by construction, and those sectors -- the
returned tail, unclaimable while the stripe holds live data -- are dead
and paddable too.
The abandonment report is what closes a writeback ENOSPC race the
fsync-heavy near-full workload hits: an allocation undone without a
report left phantom inflight bytes and phantom-live sectors, and
reservations admitted against that state failed at writeback
(cow_file_range -28) where stock refuses the write() up front. With
the report in place the same workload shows zero writeback failures at
every reservation margin setting, including margin disabled.
Claiming also learns an O(1) fast fail: the by-size index walk is
extracted into find_free_stripe_run(), stopping at the first entry whose
largest contiguous free run cannot hold a full stripe -- near-full, where
free space degenerates into many sub-stripe holes, this replaces a scan
of the whole tree on every allocation.
Moved out of the stripe_meta patch, which needs all of this for tree
blocks but introduced it tangled with the metadata machinery; data wants
it on its own.
A parked rbio does not wait for sectors whose write will never come: a
sector the liveness map has as dead is not counted as missing.
The O(1) fast fail extends to the failed-scan bound: claims stop at once
in a group whose last exhaustive scan proved there is no claimable run,
until free space is added.
With padding decided by the liveness map, the LIVE and UNALLOCATED pad
refusal reasons, which only the frontier oracle produced, go; padding now
declines only for NO_RUN and UNCOVERED.
[zygo: 7.3 port: 7.3 has no per-chunk stripe length (map->stripe_len,
map->stripe_shift, two-argument btrfs_stripe_nr_to_offset()), context
adjusted to BTRFS_STRIPE_LEN; the change itself is unchanged]
btrfs: stripe_alloc: complete parked writes on allocation coverage, not clocks
A parked partial-stripe write goes down its RMW path when its deadline
expires or a retirement flush kicks it, even when every byte it is
missing below the run's frontier belongs to an allocation whose data IO
is already in flight -- allocation happens at writeback submission, and
the commit's retirement drain waits for exactly those arrivals. Timing
out such a park buys nothing and costs a stripe read plus a second write
of the same stripe: measured on an 8-device raid5 buffered fill, the
100ms deadline turns ~2200 parked stripes per 2GB into read-modify-
writes (pad_decline_live), and stretching the clock 20x recovers only
14% -- the clock is the wrong instrument.
Replace the clock with a coverage test. A parked rbio is ready when its
gathered bios cover everything the covering run has allocated inside its
stripe (btrfs_stripe_run_alloc_ceiling); the remainder lies at or past
the frontier, so the existing pad turns it into a single full-stripe
write with no read phase. Merges check readiness as they land, the park
timer holds unready parks instead of expiring them, and run-retirement
flushes leave unready parks parked: the retirement has already closed
the run (freezing the allocated prefix) and its drain waits on the very
arrivals that will complete them. Waiter-driven flushes (fsync's
pre-writeback kick, ordered-extent waits) and sync parks keep today's
forced behaviour: a blocked waiter's latency beats a saved stripe read.
A stuck cap (10x the park deadline) bounds the wait when an arrival can
never come: a writeback error abandoned the allocation, or the stripe
was already written once by a forced sync park. Such parks are forced
down the old path and counted.
New sysfs stripe_park_stats counters: unparked_ready (parks completed by
the coverage test) and stuck (parks forced at the cap).
[zygo: 7.3 port: 7.3 has no per-chunk stripe length (map->stripe_len,
map->stripe_shift, two-argument btrfs_stripe_nr_to_offset()), context
adjusted to BTRFS_STRIPE_LEN; the change itself is unchanged]
btrfs: stripe_alloc: trace why padding declined, at the moment it declined
The counters say how often padding refused and broadly why, but not what
the run looked like when it happened, and reconstructing that from the
extent tree afterwards cannot distinguish "the stripe was already
allocated when this write arrived" from "it was allocated shortly after".
Those want different fixes, so record the decision where it is made.
Also count the second refusal, which had no counter at all: the oracle can
allow padding and the sector walk still refuse, because a sector below the
frontier is not covered by this write. That is the case where another
allocation shares the stripe and its data is not in this rbio -- a
different thing from the oracle finding the whole stripe allocated.
The tracepoint carries the stripe, how much of it this write covers, the
refusal reason, and the run's start/end/frontier/inflight, so a trace says
directly whether the frontier had already run past the stripe when the
write showed up.
[zygo: 7.3 port: 7.3 has no per-chunk stripe length (map->stripe_len,
map->stripe_shift, two-argument btrfs_stripe_nr_to_offset()), context
adjusted to BTRFS_STRIPE_LEN; the change itself is unchanged]
btrfs: stripe_alloc: tunable park deadlines, and say why padding declined
The deadline constants are meant to be judged "from measurements rather
than taste", but they are compile-time, so every data point costs a kernel
build and a reboot. Measuring a buffered fill -- dd, no fsync, so nothing
kicks the park and the backstop is the only exit -- found 1861 of 3984
parks expiring at the 100ms deadline, which is precisely the no-waiter case
the constant exists for and the one with no numbers behind it.
Expose both deadlines as writable sysfs files, clamped to 60s because a
parked rbio holds its stripe lock, with 0 disabling parking for that class
(useful as an experiment in itself):
Add the counters the stats file cannot supply. rmw_reads counts only
parked writes that still had to read, so it cannot answer "does this
workload read-modify-write at all"; data_rmw counts every data RMW, which
with allow_rmw empty on a covered block group is the number the
stripe-exclusive claim is about, and it should be zero.
And record why padding refused, because the reasons are different defects:
the rest of the stripe is allocated and its data has not arrived yet
(wait longer, or kick when the frontier advances); no open run covers the
stripe, so the run closed before its own write went down; or the frontier
never reached the stripe at all. Attributing them turns "some RMW
remains" into a specific thing to fix.
btrfs: stripe_alloc: report read-modify-write of uncovered stripes
The mount-time warning says which block groups stripe_alloc does not
cover. It cannot say whether anything is actually landing there, and on
a filesystem with raid56 metadata that is the interesting question: every
sub-stripe write to an uncovered stripe is a write hole window, where
parity and data reach the disk separately and a crash in between leaves
the stripe unreconstructible.
Report it from the one place that knows the write is going out as a
read-modify-write rather than as a full or padded stripe. Rate limited,
because a raid56-metadata filesystem does this continuously and the point
is to make the exposure visible rather than to fill the log. (A
meta_rmw counter in stripe_park_stats, to read the rate off without
grepping dmesg, is debug code and is added in the debug subtopic.)
Silent when stripe_alloc is off: there the whole filesystem works this
way and the user has asked for nothing else.
btrfs: stripe_alloc: warn when raid56 metadata is not covered
stripe_alloc closes the write hole for raid56 data. It does nothing for
raid56 metadata, which keeps read-modify-write and keeps the hole, and
on a mixed-block-group filesystem it now does nothing at all. A user
who enables stripe_alloc on -d raid5 -m raid5 has every reason to
believe the filesystem is covered, and finds out otherwise only after a
crash on a degraded array -- with metadata damage, which is worse than
the data damage they were protecting against.
Say it once at mount, after the block groups are read, so the message
reflects what is actually on disk rather than what was asked for. A
warning, not an error: the data guarantee is real and worth having on
its own, and a filesystem can be converted to raid1c3/raid1c4 metadata
without unmounting.
btrfs: stripe_alloc: claim fully-free stripes that span free space entries
The claim fast path searches single entries, so a fully-free full
stripe whose free space spans an entry boundary -- an extent entry
adjoining a bitmap, or two neighbouring bitmap windows -- was
unclaimable. stripe_unusable accounting is entry-blind and counts
exactly those stripes as claimable, so admission reserves data against
them; at writeback the claim finds nothing, cow_file_range() gets
-ENOSPC, and the already-dirtied pages are dropped. full_stripe_len
is not a power of two, so stripe boundaries drift through the fixed
128M bitmap windows and a straddling stripe is a certainty near full,
not a corner case.
Observed live at the raid6 fill edge (rolling-failure, then isolated
by fill-edge-debug with enospc_debug): two 384K stripes, each
straddling a bitmap window boundary, held the space_info accounting
786432 bytes above what the claim could reach, and every writeback
allocation against that phantom margin failed -ENOSPC while ~991MB of
genuinely trapped free space sat in the cache. The reservation margin
cannot absorb this: the gap is per straddling stripe, not per
outstanding extent.
Add an entry-blind slow path: one offset-ordered walk accumulating
contiguous free coverage across entry boundaries, and a piecewise
removal that runs in the same tree_lock critical section as the find,
so a racing claimer cannot see a half-removed run. The fast path is
unchanged and still serves the common case; the slow path runs only
after it fails, which is the near-full case where a stranded stripe
matters most.
A bitmap piece of a split run is taken only to the end of the set bits
that start at it, so that an extent entry starting inside the same bitmap
window is found and removed as the next piece. A self-test covers that
layout.
On a large aged filesystem (85 TiB, mostly legacy data, heavily fragmented
free space) the slow path walked the whole group for every small
allocation and found nothing, at 100% CPU in the delalloc workers. So a
failed exhaustive scan is remembered: bg->max_claimable_run is an upper
bound learned from the scans ((u64)-1 unknown, 0 after a scan proved no
claimable run), claims fail in O(1) while it is below one full stripe or
the group's free space is, and adding free space (frees, unpins, stripe
run returns, and a cluster returning its entries) resets it. It is only
set and trusted once the group is fully cached, since cache loading links
entries without the reset.
When a claim splits one extent entry, the entry keeps the tail and the
head is linked back with a spare entry allocated before ctl->tree_lock is
taken, so a failed allocation cannot lose it from the cache; without a
spare it is added after the lock as before.
btrfs: stripe_alloc: pessimistic data reservation margin
A byte-counted data reservation holds no collateral against the stripe
claim rule. Between admission at write() time and the allocation at
writeback, the trapped-space picture keeps moving: commits close open
stripe runs and trap their sub-stripe tails, and free space returning
mid-transaction (unpinned deletions, same-transaction frees, drained
run tails, freed reservations) lands in the free space cache as
fragments the claim rule can never hand out but that no counter yet
reflects. The counters were honest at every instant for NEW
admissions while OLD admissions were left holding air; delalloc
writeback gets no second chance, so the pages were dropped. Measured
at the fill edge: ~19 MiB of orphaned bytes_may_use with accounting
and allocator in perfect agreement, and in a later round the admitted
may_use+margin exactly equaled the phantom mid-transaction fragments.
Be pessimistic at reservation time and optimistic at allocation time,
the same shape metadata reservations already use:
- bytes_stripe_margin: one full stripe width (fs_info->
stripe_margin_unit, the widest raid56 data full stripe) per
outstanding delalloc extent, charged and released inside
btrfs_mod_outstanding_extents() so it stays exact across delalloc
merges and splits -- including async compression, where the io-tree
split hook grows the margin to one stripe per compressed piece,
matching the true worst case. Counted in btrfs_space_info_used()
so it holds back admissions. The margin exists only for in-flight
dirty data; steady-state capacity and statfs are untouched.
- Admission probes len + margin through the ticketed FLUSH_DATA path
(then immediately re-releases the probe, which the delalloc hooks
re-charge), so a writer that cannot be covered waits for the
flusher -- commits convert pinned deletions into claimable whole
stripes -- and receives an honest ENOSPC at write(2) if flushing
cannot produce cover.
- Every btrfs_add_free_space() into an armed group counts the added
range's sub-stripe head and tail fragments as trapped immediately;
whole stripes fully inside the range are trivially fully free and
stay claimable. A bounded overcount in the safe direction, never
an undercount; the commit rescan remains the authority and
reconciles (and now grants tickets when it lowers the counter,
since admission waiters may be blocked on exactly that headroom).
This closes the retire-to-rescan window the previous patch left
open, and closes it in the one place every returning range passes
through: counting at the individual close/drain call sites instead
misses paths (the drain returns have two) and gets the arithmetic
wrong (round_up() on the non-power-of-two 448K stripe width).
- When a margin-backed data allocation must split across free space
fragments, every non-final piece is trimmed to whole stripes of the
group it lands in, so at most one piece per delalloc extent can strand
a tail -- the margin pre-paid exactly one. The trim is done in
do_allocation_stripe() for the group being tried, not up front in
btrfs_reserve_extent(): groups narrow at the fill edge (8 devices of
which two are small give 448K and then 320K full stripes), and a piece
sized for the widest stripe strands a tail in every narrower group it
lands in. btrfs_reserve_extent() only marks the split
(ffe_ctl->split_bytes, the untrimmed length, so a group that undoes its
allocation does not pass a trimmed length on). A piece shorter than one
stripe of the group is left alone: that is the tail the margin pays
for.
- A data writeback allocation returning ENOSPC anyway is loudly
warned: the margin should make it impossible, and the pages are
dropped.
With this, the fill-to-ENOSPC harness goes fully clean for the first
time: zero writeback drops, zero warnings, zero allocation failures,
margin drains to zero at idle, and fill capacity is unchanged. A
writeback ENOSPC retry loop that this replaces is dropped entirely.
Known bounds, accepted: compressed writes are probed one unit per
128M range but charged per 128K piece (probe optimistic, charge
honest); direct IO is probed but carries no margin (its reserve-to-
allocate window is one syscall); clearing the property with dirty
delalloc drains the counter (charges are gated on the policy,
releases are not, and the counter clamps at zero).
[zygo: 7.3 port: 7.3's btrfs_add_bg_to_space_info() still takes fs_info;
context adjusted. 7.3 still has the v1 space cache, so
cache_save_setup()'s btrfs_check_data_free_space() call takes the new
stripe_held argument as NULL (the free space inode reserves with
FLUSH_FREE_SPACE_INODE, which carries no margin)]
btrfs: stripe_alloc: count open stripe run remainders against data reservations
Close the remaining reservation-vs-allocation windows the commit-time
stripe_unusable rescan cannot see:
- bytes_stripe_open (new): the sum of open runs' unallocated
remainders, maintained per block group under stripe_run_lock at
every open/alloc/grow/close, synced into the space_info after each
mutation, and counted in btrfs_space_info_used(). Claimed bytes are
invisible to the free space cache and will either be allocated or
become trapped tails at close, so reservations must not be admitted
against them.
A closed run's returned tail is still only counted as trapped by the
commit-time rescan, so reservations can race the retire-to-rescan window
inside a commit and be admitted against freshly trapped tails. The next
patch closes that window from the free space cache side, where every
returning range is seen and no call site can be missed.
Measured on the fill-to-ENOSPC rolling test before this change (with
only the bytes_stripe_unusable accounting): tree 'b' still lost 3.3%
(8532 blocks) and tree 'c' 30% (62371 blocks) to silent writeback
allocation failure as trapping compounded.
/sys/fs/btrfs/<uuid>/stripe_park_stats reports lifetime counters for
the parking machinery: rbios parked, bytes merged into parked rbios,
and how each park ended (filled to a full stripe, kicked by a waiter or
a settle/retire flush, or expired at its deadline), plus how many
flushed writes were padded to full stripes versus still needing the
RMW read phase. The filled/kicked/expired split shows directly whether
the deadlines are sized right for a given system -- expired parks that
later reappear as rmw_reads are the batching the window failed to
capture -- so any future retuning (or an adaptive deadline) can argue
from measurements instead of taste.
btrfs: raid56: pad sub-stripe writes to full stripes in open runs
A sub-stripe write does a full RMW: read every untouched data sector of
the stripe, recompute parity, write. Under stripe-exclusive allocation
the read phase is usually pointless: a stripe covered by a live stripe
run has never been written at or past the run's allocation frontier, so
the sectors being read contain nothing.
When every data sector the rbio does not cover lies at or past the
frontier, zero-fill those sectors instead of reading them and write
them out with the stripe -- the zeros must reach the disk, or the
parity (computed over them) would not match what scrub reads back. The
partial write becomes one full-stripe write: no read phase, one parity
pass. A frontier that grows during the attempt is safe: the newer
allocation's write serializes behind this rbio's stripe lock and lands
over the zeros. If any uncovered sector is below the frontier (already
allocated to someone else), fall back to the normal RMW.
Together with kicking parked rbios before the fast fsync's writeback
wait, this removes both stalls the raid56 layer added to fsync under
stripe_alloc: the park deadline and the RMW read round trip.
The pad sets the rbio's data bitmap under bio_list_lock: until
RBIO_RMW_LOCKED_BIT is set another writer can merge into the rbio and OR
its bits in under that lock, and a lost pad bit would leave rows out of
the write while the cached stripe marks their zero-filled pages up to date.
[zygo: 7.3 port: 7.3 has no per-chunk stripe length (map->stripe_len,
bioc->stripe_len, the two-argument btrfs_stripe_nr_to_offset()):
BTRFS_STRIPE_LEN is used instead; the change itself is unchanged]
btrfs: stripe_alloc: private per-inode stripe runs for log-active inodes
An fsync-heavy inode shares the open band runs with every other writer.
Closing the open stripe run covering every logged extent at each log
commit, which "btrfs: stripe_alloc: settle a logged extent's stripes at
log commit" does later in this series to make completed fsyncs
crash-safe, would then close a shared run at each of its log commits,
fragmenting concurrent write streams and trapping the shared run's tail.
Give log-active inodes their own runs instead. The first fsync of an
inode sets a sticky runtime flag (at btrfs_sync_file entry, before the
fsync flushes its own delalloc, so even that first fsync's allocations
are steered). Datacow allocations for a flagged inode come from a
private LOG-class run: claimed like any run but never placed in the
shared band slots, owned by the inode, and found by owner lookup under
the block group's stripe_run_lock. A per-inode hint seeds the
allocator's search with the run's location; the hint is advisory (a
stale hint costs a lookup miss, never a wrong run, since the owner
match is authoritative). When no fully-free stripes remain for a
private run the allocation falls back to the shared runs, restoring
the previous placement with unchanged safety.
One inode's log commit then settles only its own stripes: other
writers' runs stay open and their streams stay contiguous, and the
only trapped tails are the fsyncing inode's own. Private runs also
give each log-active inode exclusive stripes, which a later change
uses to copy live tails forward and reclaim them without touching
foreign extents.
The commit-time retirement closed runs by walking the band slots,
which private runs never occupy; walk the block group's run list
instead so every open run, slotted or private, is closed and drained
under invariant I2 (no open run survives a transaction commit).