]> git.hungrycats.org Git - linux/commit
btrfs: stripe_meta: hold back whole stripes so tree blocks always land and trapped...
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:18:12 +0000 (13:18 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:31 +0000 (21:16 -0500)
commitebd9dd7185342561496d5c57633401b36b44eb65
tree1ca8f7614eb6acb7d2f7e3566b9a5995b997cbeb
parent7bbd02b12369623c5d6702945f527862dac9be01
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.

Assisted-by: Claude:claude-fable-5
Assisted-by: Claude:claude-opus-5-5
Assisted-by: Claude:claude-fable-5-1
fs/btrfs/block-group.c
fs/btrfs/disk-io.c
fs/btrfs/free-space-cache.c
fs/btrfs/fs.h
fs/btrfs/relocation.c
fs/btrfs/space-info.c
fs/btrfs/space-info.h
fs/btrfs/sysfs.c