]> git.hungrycats.org Git - linux/commit
btrfs: stripe_alloc: claim fully-free stripes that span free space entries
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:17:47 +0000 (13:17 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:46 +0000 (18:37 -0500)
commitf7af527cb633d2b8862092dabac7daa02a444a85
treefdfce5be022d24836862a4416f0e641c2d73315e
parentfa8e015c02b2d1d240bb8017456bc0add503a5ad
btrfs: stripe_alloc: claim fully-free stripes that span free space entries

The claim fast path searches single entries, so a fully-free full
stripe whose free space spans an entry boundary -- an extent entry
adjoining a bitmap, or two neighbouring bitmap windows -- was
unclaimable.  stripe_unusable accounting is entry-blind and counts
exactly those stripes as claimable, so admission reserves data against
them; at writeback the claim finds nothing, cow_file_range() gets
-ENOSPC, and the already-dirtied pages are dropped.  full_stripe_len
is not a power of two, so stripe boundaries drift through the fixed
128M bitmap windows and a straddling stripe is a certainty near full,
not a corner case.

Observed live at the raid6 fill edge (rolling-failure, then isolated
by fill-edge-debug with enospc_debug): two 384K stripes, each
straddling a bitmap window boundary, held the space_info accounting
786432 bytes above what the claim could reach, and every writeback
allocation against that phantom margin failed -ENOSPC while ~991MB of
genuinely trapped free space sat in the cache.  The reservation margin
cannot absorb this: the gap is per straddling stripe, not per
outstanding extent.

Add an entry-blind slow path: one offset-ordered walk accumulating
contiguous free coverage across entry boundaries, and a piecewise
removal that runs in the same tree_lock critical section as the find,
so a racing claimer cannot see a half-removed run.  The fast path is
unchanged and still serves the common case; the slow path runs only
after it fails, which is the near-full case where a stranded stripe
matters most.

A bitmap piece of a split run is taken only to the end of the set bits
that start at it, so that an extent entry starting inside the same bitmap
window is found and removed as the next piece.  A self-test covers that
layout.

On a large aged filesystem (85 TiB, mostly legacy data, heavily fragmented
free space) the slow path walked the whole group for every small
allocation and found nothing, at 100% CPU in the delalloc workers.  So a
failed exhaustive scan is remembered: bg->max_claimable_run is an upper
bound learned from the scans ((u64)-1 unknown, 0 after a scan proved no
claimable run), claims fail in O(1) while it is below one full stripe or
the group's free space is, and adding free space (frees, unpins, stripe
run returns, and a cluster returning its entries) resets it.  It is only
set and trusted once the group is fully cached, since cache loading links
entries without the reset.

When a claim splits one extent entry, the entry keeps the tail and the
head is linked back with a spare entry allocated before ctl->tree_lock is
taken, so a failed allocation cannot lose it from the cache; without a
spare it is added after the lock as before.

Assisted-by: Claude:claude-fable-5
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/block-group.c
fs/btrfs/block-group.h
fs/btrfs/free-space-cache.c
fs/btrfs/tests/free-space-tests.c