]> git.hungrycats.org Git - linux/commit
btrfs: stripe_alloc: compact mostly live groups instead of skipping them
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 29 Sep 2026 09:29:38 +0000 (05:29 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:29 +0000 (21:16 -0500)
commitc03ff1b36c4e0ba23fde446649adf480dacd35f4
treeb39b1fe23e081ecfc88ddd16345fbfc2a5edcb7e
parentbc599fa1b8130e13bb3a50de8cb90d00fcdf3bfe
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".

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