]> git.hungrycats.org Git - linux/commit
btrfs: stripe_alloc: pessimistic data reservation margin
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:17:46 +0000 (13:17 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:45 +0000 (18:37 -0500)
commitfa8e015c02b2d1d240bb8017456bc0add503a5ad
tree50fcc0874052436d43cba72de1e7d1d675ddf57a
parenta1b2e28c996f11bfdc9579ecdbd33d2c1431a0ef
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)]

Assisted-by: Claude:claude-fable-5
Assisted-by: Claude:claude-opus-5-5
14 files changed:
fs/btrfs/block-group.c
fs/btrfs/btrfs_inode.h
fs/btrfs/delalloc-space.c
fs/btrfs/delalloc-space.h
fs/btrfs/direct-io.c
fs/btrfs/extent-tree.c
fs/btrfs/extent-tree.h
fs/btrfs/file.c
fs/btrfs/free-space-cache.c
fs/btrfs/fs.h
fs/btrfs/inode.c
fs/btrfs/space-info.c
fs/btrfs/space-info.h
fs/btrfs/sysfs.c