]> git.hungrycats.org Git - linux/commit
btrfs: stripe_alloc: do not count a writeback ENOSPC drop's give-back as supply
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 25 Oct 2026 10:27:49 +0000 (06:27 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:54 +0000 (18:37 -0500)
commitfbb7ed9e85ef990244f0719c8b48862cef8b9cde
treed1aa4c2e79b4451c3b4993e3a9ad2eeac18cd921
parentb675889e733ccb1d93535b899a6131b9b59922fd
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]

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/block-group.c
fs/btrfs/extent-tree.c
fs/btrfs/free-space-cache.c
fs/btrfs/free-space-cache.h
fs/btrfs/inode.c
fs/btrfs/space-info.c
fs/btrfs/space-info.h
fs/btrfs/sysfs.c