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