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.]