-From 57034df182f0238f0420d4b958e4f3d0b06f7959 Mon Sep 17 00:00:00 2001
+From 4f26c7c448962f551ae85038d4ca3971d109ec9b Mon Sep 17 00:00:00 2001
From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Sat, 17 Oct 2026 22:32:17 -0400
-Subject: [PATCH 0/2] btrfs: keep room to relocate on a full filesystem
+Date: Thu, 22 Oct 2026 05:12:25 -0400
+Subject: [PATCH 0/3] btrfs: keep room to relocate on a full filesystem
To: linux-btrfs@vger.kernel.org
A btrfs filesystem that fills up needs relocation to get out: automatic
System chunks are not covered: balance -m relocates them too, and a
full filesystem can still fail there.
-[Candidate patch 3, not yet in this series (refs/cand/m-reloc):
-"btrfs: relocation: flush fully before failing a merge reservation".
-merge_reloc_root() refills relocation's block reserve with
-BTRFS_RESERVE_FLUSH_LIMIT, so at the fill edge a merge step can fail
-with ENOSPC, which forces the filesystem read-only, while delayed refs,
-a commit or the global reserve could have served it. The patch uses
-BTRFS_RESERVE_FLUSH_EVICT. If promoted, this letter needs a paragraph
-on it and the subject becomes 0/3. It does not cover the other abort
-found at the same edge (a commit's delayed refs after a relocation frees
-a burst of tree blocks); see TESTING.md.]
-
-Zygo Blaxell (2):
+Patch 3 fixes a transaction abort in upstream relocation code that the
+same fill edge exposes, with or without the reserve. merge_reloc_root()
+refills relocation's block reserve before each merge step with
+BTRFS_RESERVE_FLUSH_LIMIT, which runs neither delayed refs nor a commit
+and never touches the global reserve. At the fill edge a refill then
+fails with ENOSPC and merge_reloc_roots() forces the filesystem
+read-only, although delayed refs, a commit (9 MiB was pinned) or the
+global reserve would have covered it. On a 6-device raid5 filesystem
+under fsstress with every device allocated, background reclaim aborted this way 3 times out of 3;
+with the patch, 6 runs had no merge failure, and every refill that
+needed flushing was served by the delayed-ref states. The patch
+refills with BTRFS_RESERVE_FLUSH_EVICT. Its message explains why
+FLUSH_LIMIT was chosen (killability, not a deadlock), why no other mode
+fits, and why flushing here cannot deadlock. A second abort at the same
+edge is not fixed here: a commit's delayed refs can still run out of
+metadata after a relocation pins a burst of freed tree blocks (see
+TESTING.md).
+
+Zygo Blaxell (3):
btrfs: print the error when reclaim fails to relocate a block group
btrfs: hold block groups free for relocation
+ btrfs: relocation: flush fully before failing a merge reservation
fs/btrfs/block-group.c | 366 ++++++++++++++++++++++++++++++++++++++++-
fs/btrfs/block-group.h | 23 +++
fs/btrfs/extent-tree.c | 34 +++-
fs/btrfs/extent-tree.h | 3 +
fs/btrfs/fs.h | 12 ++
- fs/btrfs/relocation.c | 2 +
+ fs/btrfs/relocation.c | 13 +-
fs/btrfs/space-info.c | 67 +++++++-
fs/btrfs/space-info.h | 19 ++-
fs/btrfs/super.c | 2 +
fs/btrfs/sysfs.c | 48 ++++++
fs/btrfs/volumes.c | 31 ++++
- 12 files changed, 604 insertions(+), 9 deletions(-)
+ 12 files changed, 614 insertions(+), 10 deletions(-)
base-commit: 0c48d26d46bdd6422bb91ba7f1c519a2993116dc
--- /dev/null
+From 4f26c7c448962f551ae85038d4ca3971d109ec9b Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Wed, 21 Oct 2026 12:48:13 -0400
+Subject: [PATCH 3/3] btrfs: relocation: flush fully before failing a merge
+ reservation
+To: linux-btrfs@vger.kernel.org
+
+merge_reloc_root() refills the relocation block reserve before each step
+of the merge. Since 44d354abf33e ("btrfs: relocation: review the call
+sites which can be interrupted by signal") it does so with
+BTRFS_RESERVE_FLUSH_LIMIT. FLUSH_LIMIT only flushes delayed items and
+tries to allocate a chunk. It does not run delayed refs, does not flush
+delalloc, does not commit the transaction to unpin what the merge and
+everybody else freed, and never falls back to the global reserve. A
+failure here is fatal: merge_reloc_roots() handles it with
+btrfs_handle_fs_error() and the filesystem goes read-only.
+
+The merge routinely needs more than prepare_to_merge() reserved for it,
+and refills from free space. On a filesystem with every device
+allocated, metadata free space is all in other tasks' reservations, and
+the refill loses:
+
+ BTRFS: error (device dm-6) in merge_reloc_roots:2053: errno=-28 No space left
+ BTRFS info (device dm-6 state E): forced readonly
+ BTRFS error (device dm-6 state EA): error relocating chunk 14022541312: -28
+
+This is the reclaim worker relocating a data block group of a 6-device
+raid5 filesystem (metadata raid1) with 6 MiB unallocated, under
+fsstress. A trace of the failing merge: prepare_to_merge() reserved 704
+KiB, the merge used it up and refilled 14 times (560 KiB more), and the
+15th refill, for 32 KiB, failed. At that moment the metadata space_info
+(256 MiB) had 160 MiB used, 9 MiB pinned and 86 MiB in may_use, of which
+27 MiB was the untouched global reserve: 16 KiB free. Background
+reclaim is optional work; it took the filesystem down.
+
+Refill with BTRFS_RESERVE_FLUSH_EVICT instead.
+
+Why FLUSH_LIMIT was chosen. This refill used FLUSH_ALL from
+08e007d2e577 ("Btrfs: improve the noflush reservation") until
+44d354abf33e. That commit changed it only so that a fatal signal cannot
+interrupt the merge: FLUSH_ALL waits for its ticket killably and
+returns -EINTR, which here would turn a killed balance into a read-only
+filesystem. Its message already notes that the change "can be
+ENOSPC-prone", and it shrank min_reserved to compensate. No deadlock
+was given as a reason.
+
+Why not another mode. No existing mode is "run delayed refs and commit,
+but neither flush delalloc nor steal, and do not wait killably":
+- FLUSH_LIMIT does not run delayed refs, which is what the merge refills
+ that needed flushing in testing needed.
+- FLUSH_ALL and FLUSH_ALL_STEAL flush delalloc and commit too, through
+ the async reclaim worker, and wait killably. That brings back the
+ -EINTR that 44d354abf33e removed.
+- FLUSH_EVICT is the priority flusher with the full list: delayed items,
+ delayed refs, delalloc, chunk allocation and a commit, in that order,
+ run in the caller's context. It stops at the first state that serves
+ the ticket, does not wait killably, and steals from the global reserve
+ only after all of them have failed.
+In testing, every merge refill that needed flushing was served by the
+delayed-ref states (5 of 6 tickets; the sixth needed only delayed
+items). None reached delalloc, the commit or the steal. Those states
+run only where the old code forced the filesystem read-only.
+
+Deadlock analysis. At the refill the merging task holds:
+- No transaction handle. The previous step ended its transaction with
+ btrfs_end_transaction_throttle(), and the first step has not started
+ one, so current->journal_info is NULL, as FLUSH_EVICT requires.
+- No tree locks. The path into the reloc tree holds references only
+ (the reloc tree is walked without locks, and btrfs_unlock_up_safe()
+ dropped the locks of the resume search). replace_path() unlocks the
+ subvolume tree before it returns.
+- Not reloc_mutex. merge_reloc_roots() takes it only around its list
+ splices.
+- These locks from its callers: cleaner_mutex (do_nonremap_reloc()),
+ reclaim_bgs_lock, the exclusive operation, and sb_start_write for a
+ data block group. Writeback, ordered extent completion and
+ transaction commit take none of them. The other takers of
+ reclaim_bgs_lock either use trylock (unused block group deletion, the
+ reclaim worker) or are relocation entry points (balance, system chunk
+ relocation, device shrink, zoned repair). Those take it with no
+ transaction handle held, and the exclusive operation serializes them
+ with this one anyway. The other takers of cleaner_mutex are the
+ cleaner (trylock), mount and unmount (btrfs_start_pre_rw_mount(),
+ btrfs_commit_super()) and the quota ioctl, which takes it before
+ starting a transaction. So no flush or commit can wait on a lock that
+ the merging task holds.
+
+The same task already does each of these things in the same lock
+context:
+- prepare_to_merge() reserves with FLUSH_ALL, waiting while the async
+ reclaim worker flushes delalloc and commits, and then commits itself.
+- relocate_block_group() commits again after the merge.
+- btrfs_end_transaction_throttle() at the end of every merge step runs
+ delayed iputs. An evicted inode reserves with FLUSH_EVICT from right
+ there.
+
+A commit in the middle of the merge is legal. The transaction kthread,
+sync and other tasks already commit between merge steps. Each step
+saves drop_progress and drop_level before it ends its transaction, and
+the commit writes them to the reloc root item through
+btrfs_update_reloc_root(). init_reloc_root() creates nothing while
+rc->merge_reloc_tree is set. btrfs_end_transaction_throttle() used to
+commit from this loop itself, until 302167c50b32 ("btrfs: don't end the
+transaction for delayed refs in throttle").
+
+DATA_RELOC inode. In the UPDATE_DATA_PTRS stage its delalloc was
+written back and waited for, and its page cache invalidated, before the
+stage started (do_nonremap_reloc()), so there is nothing to flush. In
+the MOVE_DATA_EXTENTS stage its dirty pages may still be unwritten
+during the merge. Flushing them from here is the same writeback that
+the flusher threads, sync, or another task's FLUSH_ALL or FLUSH_EVICT
+ticket may start at any time. It is also what do_nonremap_reloc() waits
+for right after relocate_block_group() returns. Completing its ordered
+extents joins a transaction on the data reloc root, which is not
+shareable, so it does not take reloc_mutex.
+
+The steal is bounded. steal_from_global_rsv() leaves 10% of the global
+reserve, and each steal is at most min_reserved, which is two nodes per
+level of the reloc root (64 KiB in the trace above). A merge that fails
+here takes the filesystem read-only. Keeping it writable is what the
+global reserve is for, as it is for eviction.
+
+If even the global reserve cannot cover the refill, the merge fails as
+before.
+
+Reproduced with reloc-merge-enospc-test.sh (raid56-wh-harness): the
+octopus P1 sequence on 6 x 3 GiB, fsstress until the reclaim worker's
+relocations, plain relocations or balances run at the fill edge. On the
+base, 3 of 3 reclaim runs went read-only in merge_reloc_roots(). With
+the patch, 6 runs had no merge failure.
+
+Fixes: 44d354abf33e ("btrfs: relocation: review the call sites which can be interrupted by signal")
+Assisted-by: Claude:claude-opus-5-5
+---
+ fs/btrfs/relocation.c | 11 ++++++++++-
+ 1 file changed, 10 insertions(+), 1 deletion(-)
+
+diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
+index 179bac3a2dccd..87e22737405b4 100644
+--- a/fs/btrfs/relocation.c
++++ b/fs/btrfs/relocation.c
+@@ -1675,8 +1675,17 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
+ memset(&next_key, 0, sizeof(next_key));
+
+ while (1) {
++ /*
++ * A failure here forces the filesystem read-only (see
++ * merge_reloc_roots()), so flush everything before giving up:
++ * delayed refs, delalloc, a transaction commit to unpin what
++ * the merge has freed so far, and the global reserve as the
++ * last resort. FLUSH_EVICT does that in our own context and,
++ * unlike FLUSH_ALL, cannot be interrupted by a signal. We hold
++ * no transaction handle and no tree lock here.
++ */
+ ret = btrfs_block_rsv_refill(rc->block_rsv, min_reserved,
+- BTRFS_RESERVE_FLUSH_LIMIT);
++ BTRFS_RESERVE_FLUSH_EVICT);
+ if (ret)
+ goto out;
+ trans = btrfs_start_transaction(root, 0);
+--
+2.47.3
+