btrfs: stripe_alloc: persist nocow runs across commits and remounts
A nodatacow inode's private stripe run used to close at every
transaction commit like all runs, so a slowly appended nocow file burned
a fresh stripe per commit -- and after a remount its partial stripe's
free tail was abandoned outright. Neither cost buys anything: the run
machinery's commit-time closing exists for invariant I2, and a nocow
stripe holds only the owner's write-hole-waived data, so there is
nothing for I2 to protect.
Keep NOCOW-class runs open across commits: the commit-time retirement
and its drain predicate skip them (their extents insert without the
window-sequence deferral, which is fine -- the data is on disk when the
ordered extent finishes, and later same-stripe writes can tear only the
owner's own data). They still close on forced quiescing (read-only,
removal, unmount) and now on the owning inode's eviction, so a cached
but idle inode cannot pin a claimed tail forever.
Across remounts, re-adopt instead: when a nocow allocation's hint
points into a partial stripe that the committed extent tree proves is
wholly owned by the allocating inode, claim exactly the stripe's free
tail (a new exact-range claim that verifies every byte is free before
removing; nothing else can consume free space inside a partially used
stripe, so verify-then-remove cannot race) and continue the run at the
old frontier. Appends to a nocow file then pack sequentially through
commits, evictions and remounts alike.
The re-adoption lookup must not block. It runs from
btrfs_alloc_from_inode_stripe_run(), i.e. inside find_free_extent(),
which holds space_info->groups_sem for read, and stripe_extents_owned_by()
does a full btrfs_search_slot() on the extent tree. The reverse order is
longstanding upstream -- delayed ref processing holds extent tree locks
and then calls find_free_extent(), which takes groups_sem -- so waiting
here closes a cycle. Both sides only take groups_sem for read, which is
not enough: it is an rw_semaphore and block group creation and removal
take it for write, so a writer queued between the two readers blocks the
second one. The check cannot be hoisted above the lock, since it is a
question about the specific stripe the allocator has just settled on, but
it does not have to block: it is read-only and advisory, and every caller
already treats "not provably ours" as a reason to decline the
optimisation rather than as a fact about the extents. So set path->nowait
for the groups_sem caller and let -EAGAIN fall into the existing error
paths; the cost on contention is a re-adoption that does not happen.
btrfs_stripe_nocow_writable() keeps the blocking search -- it runs from
the nocow check with no groups_sem held, and a spurious "not ours" there
would force COW on an extent that does not need it.
Whether a nocow extent can be written in place at all is decided by the
stripe_alloc_allow_rmw policy, and with nothing waived btrfs_stripe_nocow_
writable() forces COW for every such write. A private run would then hold
only COWed extents and unwritten preallocation, and its survival would
only strand the whole stripes freed inside its range (an fsstress fill to
ENOSPC left hundreds of such runs in one group, holding 99% of its free
whole stripes). So prealloc and nodatacow extents are steered into a
NOCOW run only when the policy lets that kind of extent be written in
place, and a NOCOW run outlives commits only when the policy allowed
in-place writes when it was opened. The verdict is recorded in the run,
so the commit's close and settle predicates agree when the policy changes
underneath them.
full_stripe_len is a power of two only when the data stripe count is
(raid5 over 4 devices gives 192K), so stripes are rounded and tested for
alignment by division, in one helper each, never with round_down() or
IS_ALIGNED().
The cleaner does not delete an unused block group while a stripe run
still covers it (a persistent nocow run whose extents were all freed, or
the running transaction's metadata run): a run's unallocated remainder is
counted in none of used, reserved and pinned, and its tail would later be
added to the removed group's free space cache. The group stays read-only
and is left for a later pass; its runs close by the next commit.
The nocow check passes an IOCB_NOWAIT write's nowait down to its extent
tree search; when that cannot answer, the extent is treated as not the
inode's own and the write falls back to COW, which a NOWAIT write refuses
with -EAGAIN.