mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] btrfs: zoned: wait for the relocating repair kthread during unmount
@ 2026-11-18  0:48 Miroslav Cimerman
  2026-11-18 12:58 ` [PATCH] btrfs: wait for block group relocation on remount read-only Miroslav Cimerman
  0 siblings, 1 reply; 3+ messages in thread
From: Miroslav Cimerman @ 2026-11-18  0:48 UTC (permalink / raw)
  To: linux-btrfs
  Cc: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	linux-kernel

On a zoned filesystem, a read error with another good copy makes
btrfs_repair_one_zone() start a "btrfs-relocating-repair" kthread that
relocates the whole block group, and return. Nothing tracks that
kthread. It takes sb_start_write() and the BALANCE exclusive operation,
but neither holds off an unmount: freeze protection only excludes
freezing, and close_ctree() only pauses a balance that has a
balance_ctl. If the filesystem is unmounted during the relocation,
close_ctree() tears it down under the kthread:

  assertion failed: list_empty(&fs_info->delalloc_roots), in fs/btrfs/disk-io.c:4608
  kernel BUG at fs/btrfs/disk-io.c:4608!
  RIP: 0010:close_ctree+0x67f/0x9b8
  Call Trace:
   generic_shutdown_super+0x14d/0x540
   kill_anon_super+0x35/0x90
   btrfs_kill_super+0x39/0x60
   deactivate_locked_super+0x9f/0x120
   cleanup_mnt+0x9a/0x130

Without the assertion, the relocation goes on to use the fs_info and
the roots that the unmount freed (KASAN: slab-use-after-free in
wait_subpage_spinlock(), null-ptr-deref in btrfs_root_node()).

Do as for the uuid rescan kthread: hold a semaphore from the start of a
repair until its kthread ends, and take and release it in close_ctree()
where the block group reclaim work, which does the same relocation, is
cancelled, before the cleaner is parked. btrfs_repair_one_zone() checks
BTRFS_FS_CLOSING_START after taking the semaphore, so no repair starts
once close_ctree() has waited. Unmount now waits for the relocation of
that one block group, as it already does for the reclaim work. The
kthread is split in two so that its guard(super_write) is released
before the up(), which must be its last access to fs_info.

A repair started while mounting has the same problem if the mount then
fails, as most error paths of open_ctree() (the fail_* labels) free
fs_info without calling close_ctree(). Don't start one before
BTRFS_FS_OPEN is set, as btrfs_should_reclaim() does.

Only one repair runs at a time now. Before, a second repair started
while one was running failed the exclusive operation with -EBUSY but
left BLOCK_GROUP_FLAG_RELOCATING_REPAIR set, so its block group was not
repaired again until the next mount. Now it returns before setting the
flag, and the next read error retries it.

Fixes: f7ef5287a63d ("btrfs: zoned: relocate block group to repair IO failure in zoned filesystems")
CC: stable@vger.kernel.org # 6.19+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
Found with FuzzyRAID, a fault-injection tester for RAID that is not
public at the moment. The analysis, the fix, the tests and the commit
message were prepared with the help of an LLM. I reviewed everything.

Tested on btrfs for-next (6f4bbde7e036) in QEMU with KASAN, two zoned
null_blk devices with RAID1 and the raid stripe tree, and read errors
on one of them, so that reading a file starts the repair:
- unmount while the repair relocates: without the patch 4 of 4 crash as
  above, with it 6 of 6 unmount cleanly after the relocation
- unmount after the repair has finished: works with and without the
  patch
- mount with read errors in one device's metadata: without the patch
  the repair starts during the mount, btrfs_start_pre_rw_mount() then
  fails ("failed to recover relocation: -ENOENT") and close_ctree()
  crashes on the repair's uncommitted transaction (ASSERT(!found) in
  warn_about_uncommitted_trans()). With it the mount works and a scrub
  repairs the errors.

Remount read-only has the same problem: it doesn't wait for the repair,
and outside the part of the repair where the remount is refused with
-EBUSY it succeeds with the kthread still running. The next unmount
then finds the repair's transaction not committed (ASSERT(!found) in
warn_about_uncommitted_trans(), 3 of 4 runs). I'll send a separate fix
for that.

 fs/btrfs/disk-io.c |  8 ++++++++
 fs/btrfs/fs.h      |  6 ++++++
 fs/btrfs/volumes.c | 49 +++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 7775fc768f7e..6af173e65c77 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -2965,6 +2965,7 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
 	init_rwsem(&fs_info->cleanup_work_sem);
 	init_rwsem(&fs_info->subvol_sem);
 	sema_init(&fs_info->uuid_tree_rescan_sem, 1);
+	sema_init(&fs_info->zone_repair_sem, 1);
 
 	btrfs_init_dev_replace_locks(fs_info);
 	btrfs_init_qgroup(fs_info);
@@ -4405,6 +4406,13 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)
 	 * trying to stop the async reclaim task.
 	 */
 	cancel_work_sync(&fs_info->reclaim_bgs_work);
+	/*
+	 * Same for a zoned repair relocating a block group. Wait for it, no new
+	 * one can start after this since BTRFS_FS_CLOSING_START is set, see
+	 * btrfs_repair_one_zone().
+	 */
+	down(&fs_info->zone_repair_sem);
+	up(&fs_info->zone_repair_sem);
 	/*
 	 * We don't want the cleaner to start new transactions, add more delayed
 	 * iputs, etc. while we're closing. We can't use kthread_stop() yet
diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
index 2dd167c32d61..3abbe4aa85e4 100644
--- a/fs/btrfs/fs.h
+++ b/fs/btrfs/fs.h
@@ -862,6 +862,12 @@ struct btrfs_fs_info {
 
 	struct semaphore uuid_tree_rescan_sem;
 
+	/*
+	 * Held while a zoned repair kthread relocates a block group, see
+	 * btrfs_repair_one_zone(). close_ctree() waits for it.
+	 */
+	struct semaphore zone_repair_sem;
+
 	/* Used to reclaim the metadata space in the background. */
 	struct work_struct async_reclaim_work;
 	struct work_struct async_data_reclaim_work;
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index f27c0c400a20..4630053d5cea 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -9053,9 +9053,8 @@ bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
 	return node != NULL;
 }
 
-static int relocating_repair_kthread(void *data)
+static int relocate_repair_block_group(struct btrfs_block_group *cache)
 {
-	struct btrfs_block_group *cache = data;
 	struct btrfs_fs_info *fs_info = cache->fs_info;
 	u64 target;
 	int ret = 0;
@@ -9100,6 +9099,22 @@ static int relocating_repair_kthread(void *data)
 	return ret;
 }
 
+static int relocating_repair_kthread(void *data)
+{
+	struct btrfs_block_group *cache = data;
+	struct btrfs_fs_info *fs_info = cache->fs_info;
+	int ret;
+
+	ret = relocate_repair_block_group(cache);
+	/*
+	 * Pairs with close_ctree(). Must be the last access to fs_info, the
+	 * unmount may free it as soon as the semaphore is released.
+	 */
+	up(&fs_info->zone_repair_sem);
+
+	return ret;
+}
+
 bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical)
 {
 	struct btrfs_block_group *cache;
@@ -9112,19 +9127,43 @@ bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical)
 	if (btrfs_test_opt(fs_info, DEGRADED))
 		return true;
 
+	/*
+	 * Don't start one during mount either: most error paths of
+	 * open_ctree() free fs_info without waiting for the repair in
+	 * close_ctree().
+	 */
+	if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
+		return true;
+
+	/*
+	 * One repair at a time, the kthread releases the semaphore when it
+	 * ends. close_ctree() takes it after setting BTRFS_FS_CLOSING_START,
+	 * so either it waits for the kthread or we see the flag here.
+	 */
+	if (down_trylock(&fs_info->zone_repair_sem))
+		return true;
+	if (btrfs_fs_closing(fs_info))
+		goto out_up;
+
 	cache = btrfs_lookup_block_group(fs_info, logical);
 	if (!cache)
-		return true;
+		goto out_up;
 
 	if (test_and_set_bit(BLOCK_GROUP_FLAG_RELOCATING_REPAIR, &cache->runtime_flags)) {
 		btrfs_put_block_group(cache);
-		return true;
+		goto out_up;
 	}
 
 	task = kthread_run(relocating_repair_kthread, cache, "btrfs-relocating-repair");
-	if (IS_ERR(task))
+	if (IS_ERR(task)) {
 		btrfs_put_block_group(cache);
+		goto out_up;
+	}
+
+	return true;
 
+out_up:
+	up(&fs_info->zone_repair_sem);
 	return true;
 }
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] btrfs: wait for block group relocation on remount read-only
  2026-11-18  0:48 [PATCH] btrfs: zoned: wait for the relocating repair kthread during unmount Miroslav Cimerman
@ 2026-11-18 12:58 ` Miroslav Cimerman
  2026-11-18 20:25   ` [PATCH v2] " Miroslav Cimerman
  0 siblings, 1 reply; 3+ messages in thread
From: Miroslav Cimerman @ 2026-11-18 12:58 UTC (permalink / raw)
  To: linux-btrfs
  Cc: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	linux-kernel

Nothing holds off a remount read-only while the reclaim work or a zoned
repair relocates a block group. Both take the super write lock, and the
reclaim work counts on it ("As we did take the super write lock, "mount
-o remount,ro" won't happen"), but sb_prepare_remount_readonly() doesn't
look at it. Without the remap tree, the remount is refused with -EBUSY
while the relocation's orphan inode exists, as it counts in
s_remove_count. Before the inode is created and after it is dropped, and
always for a forced remount, the remount goes through. The relocation
then keeps writing to the read-only filesystem, and its last transaction
is still open at unmount, as close_ctree() doesn't commit a read-only
filesystem:

  BTRFS warning (device nb2 state I): transaction 65 (with 98304 dirty metadata bytes) is not committed
  assertion failed: !found, in fs/btrfs/disk-io.c:4378
  kernel BUG at fs/btrfs/disk-io.c:4378!

Every relocation runs under reclaim_bgs_lock, btrfs_relocate_chunk()
asserts it. Take it in btrfs_remount_ro() around setting the filesystem
read-only, so the remount waits for the block group being relocated.
The reclaim work checks for read-only under the lock before each block
group and skips the remaining ones, add the same check to the zoned
repair kthread. Take the lock before setting the filesystem read-only,
as a relocation may wait for unfinished snapshot drops, which the
cleaner stops working on once the filesystem is read-only.

While it skips them, the reclaim work still calls
btrfs_delete_unused_bgs() between the block groups, which starts
transactions, also after btrfs_commit_super() has committed the last
one, and the cleaner may have queued it again. Stop it once the cleaner
is done, before that commit.

Balance, resize and device remove hold off a regular remount read-only
with mnt_want_write_file(), a resumed balance is paused by
btrfs_pause_balance(), and none of them is changed here.

Fixes: 18bb8bbf13c1 ("btrfs: zoned: automatically reclaim zones")
Fixes: f7ef5287a63d ("btrfs: zoned: relocate block group to repair IO failure in zoned filesystems")
CC: stable@vger.kernel.org
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
Found by an LLM while testing "btrfs: zoned: wait for the relocating
repair kthread during unmount", which noted that remount read-only had
the same problem for the repair. It turned out to be about any
relocation, so this is separate and applies with or without that patch.
The fix and the commit message were prepared with the help of an LLM,
which also ran the tests below. I reviewed everything.

Tested on btrfs for-next (6f4bbde7e036) in QEMU with KASAN:
- reclaim: one zoned null_blk device, 75% of several block groups
  freed, bg_reclaim_threshold lowered, and remount read-only retried in
  a loop while the reclaim work relocates. Without the patch the
  relocation goes on after the remount (1560 to 5536 sectors written to
  the device afterwards, 4 of 4 runs) and an unmount right after the
  remount crashes as above (6 of 6). With it nothing is written after
  the remount (7 of 7) and the unmount is clean (7 of 7).
- zoned repair: RAID1 on two zoned null_blk devices with read errors on
  one, remount read-only in a loop while the repair relocates, then
  unmount. Without the patch 3 of 4 crash as above, with it 7 of 7 are
  clean.
- unused block groups: as in the reclaim test, with the remaining files
  deleted while the reclaim work relocates, and with delays added for
  the test only (after setting read-only, before the reclaim work
  deletes unused block groups, and per deleted block group). Without
  the cancel_work_sync() the reclaim work deleted block groups in a
  transaction after the remount's last commit in 6 of 8 runs, and 2 of
  8 unmounts crashed as above. With it, 0 of 8.

With vm.dirty_bytes at 2 MiB, a remount read-only or an unmount during
the reclaim hangs, with and without this patch: the relocation waits
in balance_dirty_pages() for the flusher, which skips the filesystem
while the remount or unmount holds s_umount. "btrfs: start writeback
of relocated data before dirty throttling" fixes that separately.

When the repair is skipped because of the remount, the block group
keeps its repair flag, as with the other early exits of the kthread.
Kernels before 6.8 will need a different backport.

 fs/btrfs/block-group.c |  9 +++++----
 fs/btrfs/super.c       | 20 ++++++++++++++++++++
 fs/btrfs/volumes.c     |  4 ++++
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index a40eedc65dd2..9d78ea7d5370 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -2021,10 +2021,11 @@ static int btrfs_reclaim_block_group(struct btrfs_block_group *bg, int *reclaime
 	/*
 	 * Get out fast, in case we're read-only or unmounting the
 	 * filesystem. It is OK to drop block groups from the list even
-	 * for the read-only case. As we did take the super write lock,
-	 * "mount -o remount,ro" won't happen and read-only filesystem
-	 * means it is forced read-only due to a fatal error. So, it
-	 * never gets back to read-write to let us reclaim again.
+	 * for the read-only case, after a remount read-write they come
+	 * back when they are marked for reclaim again. The super write
+	 * lock doesn't hold off "mount -o remount,ro", but
+	 * btrfs_remount_ro() sets read-only under reclaim_bgs_lock, which
+	 * we hold, so a remount waits for the block group we relocate.
 	 */
 	if (btrfs_need_cleaner_sleep(fs_info)) {
 		up_write(&space_info->groups_sem);
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index b424cc836bd4..e696d1133055 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -1342,7 +1342,19 @@ static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)
 	/* Avoid complains from lockdep et al. */
 	up(&fs_info->uuid_tree_rescan_sem);
 
+	/*
+	 * Nothing holds off a remount read-only while the reclaim work or a
+	 * zoned repair relocates a block group, not even the super write lock
+	 * they take. Every relocation runs under reclaim_bgs_lock, so wait for
+	 * the one in progress and set read-only under the lock. The reclaim
+	 * work and the zoned repair check for read-only under it before they
+	 * relocate. Do this before setting read-only, as a relocation may wait
+	 * for unfinished snapshot drops, which the cleaner stops working on
+	 * once the filesystem is read-only.
+	 */
+	mutex_lock(&fs_info->reclaim_bgs_lock);
 	btrfs_set_sb_rdonly(fs_info->sb);
+	mutex_unlock(&fs_info->reclaim_bgs_lock);
 
 	/*
 	 * Setting SB_RDONLY will put the cleaner thread to sleep at the next
@@ -1364,6 +1376,14 @@ static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)
 	 */
 	wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING, TASK_UNINTERRUPTIBLE);
 
+	/*
+	 * The reclaim work doesn't relocate on a read-only filesystem, but
+	 * it still deletes unused block groups between the ones it skips, and
+	 * the cleaner may have queued it again before going to sleep. Stop
+	 * it, so it can't start a transaction after btrfs_commit_super().
+	 */
+	cancel_work_sync(&fs_info->reclaim_bgs_work);
+
 	/*
 	 * We've set the superblock to RO mode, so we might have made the
 	 * cleaner task sleep without running all pending delayed iputs. Go
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 4630053d5cea..49988f4f8018 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -9078,6 +9078,10 @@ static int relocate_repair_block_group(struct btrfs_block_group *cache)
 	if (!cache)
 		goto out;
 
+	/* Don't relocate after a remount read-only, see btrfs_remount_ro(). */
+	if (btrfs_need_cleaner_sleep(fs_info))
+		goto out;
+
 	if (!test_bit(BLOCK_GROUP_FLAG_RELOCATING_REPAIR, &cache->runtime_flags))
 		goto out;
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2] btrfs: wait for block group relocation on remount read-only
  2026-11-18 12:58 ` [PATCH] btrfs: wait for block group relocation on remount read-only Miroslav Cimerman
@ 2026-11-18 20:25   ` Miroslav Cimerman
  0 siblings, 0 replies; 3+ messages in thread
From: Miroslav Cimerman @ 2026-11-18 20:25 UTC (permalink / raw)
  To: linux-btrfs
  Cc: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	linux-kernel

Nothing holds off a remount read-only while the reclaim work or a zoned
repair relocates a block group. Both take the super write lock, and the
reclaim work counts on it ("As we did take the super write lock, "mount
-o remount,ro" won't happen"), but sb_prepare_remount_readonly() doesn't
look at it. Without the remap tree, the remount is refused with -EBUSY
while the relocation's orphan inode exists, as it counts in
s_remove_count. Before the inode is created and after it is dropped, and
always for a forced remount, the remount goes through:

 reclaim work                       remount read-only
 btrfs_reclaim_block_groups()
 |-btrfs_relocate_chunk()           btrfs_remount_ro()
 | Still relocating                 |-btrfs_set_sb_rdonly()
 |                                  \-btrfs_commit_super()
 |                                    Returns, read-only
 \-Keeps writing, leaves its last
   transaction open
                                    unmount
                                    close_ctree()
                                      No commit on a read-only fs

  BTRFS warning (device nb2 state I): transaction 65 (with 98304 dirty metadata bytes) is not committed
  assertion failed: !found, in fs/btrfs/disk-io.c:4378
  kernel BUG at fs/btrfs/disk-io.c:4378!

Every relocation runs under reclaim_bgs_lock, btrfs_relocate_chunk()
asserts it. Take it in btrfs_remount_ro() around setting the filesystem
read-only, so the remount waits for the block group being relocated.
The reclaim work checks for read-only under the lock before each block
group and skips the remaining ones, add the same check to the zoned
repair kthread. Take the lock before setting the filesystem read-only,
as a relocation may wait for unfinished snapshot drops, which the
cleaner stops working on once the filesystem is read-only.

While it skips them, the reclaim work still calls
btrfs_delete_unused_bgs() between the block groups, which starts
transactions, also after btrfs_commit_super() has committed the last
one, and the cleaner may have queued it again. Stop it once the cleaner
is done, before that commit. Cancel the space reclaim works last, right
before that commit, as what the remount waits for may queue them again,
and run the delayed iputs before, as a work may wait for them.

Balance, resize and device remove hold off a regular remount read-only
with mnt_want_write_file(), a resumed balance is paused by
btrfs_pause_balance(), and none of them is changed here.

Fixes: 18bb8bbf13c1 ("btrfs: zoned: automatically reclaim zones")
Fixes: f7ef5287a63d ("btrfs: zoned: relocate block group to repair IO failure in zoned filesystems")
CC: stable@vger.kernel.org # 6.8+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
v2:
- Cancel the space reclaim works right before the last commit instead of
  at the start of btrfs_remount_ro(), after running the delayed iputs,
  as in close_ctree(): what the remount waits for may queue them again
  (Sashiko).
- Add a chart of the race.

Found by an LLM while testing "btrfs: zoned: wait for the relocating
repair kthread during unmount", which noted that remount read-only had
the same problem for the repair. It turned out to be about any
relocation, so this is separate and applies with or without that patch.
The fix and the commit message were prepared with the help of an LLM,
which also ran the tests below. I reviewed everything.

Tested on btrfs for-next in QEMU with KASAN, remount read-only retried
while the reclaim work or a zoned repair relocates, then an unmount:
without the patch 6 of 6 reclaim and 3 of 4 repair runs crash as
above, with it 9 of 9 runs are clean.

The cancel_work_sync() under s_umount is the same as in close_ctree(),
and a remount can't run during a freeze.

A remount or unmount during a relocation at the dirty limit still hangs
without "btrfs: start writeback of relocated data before dirty
throttling".

When the repair is skipped because of the remount, the block group
keeps its repair flag, as with the other early exits of the kthread.
Kernels before 6.8 will need a different backport.

 fs/btrfs/block-group.c |  9 +++++----
 fs/btrfs/super.c       | 36 +++++++++++++++++++++++++++++-------
 fs/btrfs/volumes.c     |  4 ++++
 3 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index a40eedc65dd2..9d78ea7d5370 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -2021,10 +2021,11 @@ static int btrfs_reclaim_block_group(struct btrfs_block_group *bg, int *reclaime
 	/*
 	 * Get out fast, in case we're read-only or unmounting the
 	 * filesystem. It is OK to drop block groups from the list even
-	 * for the read-only case. As we did take the super write lock,
-	 * "mount -o remount,ro" won't happen and read-only filesystem
-	 * means it is forced read-only due to a fatal error. So, it
-	 * never gets back to read-write to let us reclaim again.
+	 * for the read-only case, after a remount read-write they come
+	 * back when they are marked for reclaim again. The super write
+	 * lock doesn't hold off "mount -o remount,ro", but
+	 * btrfs_remount_ro() sets read-only under reclaim_bgs_lock, which
+	 * we hold, so a remount waits for the block group we relocate.
 	 */
 	if (btrfs_need_cleaner_sleep(fs_info)) {
 		up_write(&space_info->groups_sem);
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index b424cc836bd4..e80df13d36e4 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -1328,13 +1328,6 @@ static int btrfs_remount_rw(struct btrfs_fs_info *fs_info)
 
 static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)
 {
-	/*
-	 * This also happens on 'umount -rf' or on shutdown, when the
-	 * filesystem is busy.
-	 */
-	cancel_work_sync(&fs_info->async_reclaim_work);
-	cancel_work_sync(&fs_info->async_data_reclaim_work);
-
 	btrfs_discard_cleanup(fs_info);
 
 	/* Wait for the uuid_scan task to finish */
@@ -1342,7 +1335,19 @@ static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)
 	/* Avoid complains from lockdep et al. */
 	up(&fs_info->uuid_tree_rescan_sem);
 
+	/*
+	 * Nothing holds off a remount read-only while the reclaim work or a
+	 * zoned repair relocates a block group, not even the super write lock
+	 * they take. Every relocation runs under reclaim_bgs_lock, so wait for
+	 * the one in progress and set read-only under the lock. The reclaim
+	 * work and the zoned repair check for read-only under it before they
+	 * relocate. Do this before setting read-only, as a relocation may wait
+	 * for unfinished snapshot drops, which the cleaner stops working on
+	 * once the filesystem is read-only.
+	 */
+	mutex_lock(&fs_info->reclaim_bgs_lock);
 	btrfs_set_sb_rdonly(fs_info->sb);
+	mutex_unlock(&fs_info->reclaim_bgs_lock);
 
 	/*
 	 * Setting SB_RDONLY will put the cleaner thread to sleep at the next
@@ -1364,6 +1369,14 @@ static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)
 	 */
 	wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING, TASK_UNINTERRUPTIBLE);
 
+	/*
+	 * The reclaim work doesn't relocate on a read-only filesystem, but
+	 * it still deletes unused block groups between the ones it skips, and
+	 * the cleaner may have queued it again before going to sleep. Stop
+	 * it, so it can't start a transaction after btrfs_commit_super().
+	 */
+	cancel_work_sync(&fs_info->reclaim_bgs_work);
+
 	/*
 	 * We've set the superblock to RO mode, so we might have made the
 	 * cleaner task sleep without running all pending delayed iputs. Go
@@ -1385,6 +1398,15 @@ static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)
 	 */
 	btrfs_qgroup_wait_for_completion(fs_info, false);
 
+	/*
+	 * Stop the space reclaim works last, as what we waited for above may
+	 * have queued them again. Run the delayed iputs before, as a work may
+	 * wait for them and the cleaner no longer runs them.
+	 */
+	btrfs_run_delayed_iputs(fs_info);
+	cancel_work_sync(&fs_info->async_reclaim_work);
+	cancel_work_sync(&fs_info->async_data_reclaim_work);
+
 	return btrfs_commit_super(fs_info);
 }
 
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 4630053d5cea..49988f4f8018 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -9078,6 +9078,10 @@ static int relocate_repair_block_group(struct btrfs_block_group *cache)
 	if (!cache)
 		goto out;
 
+	/* Don't relocate after a remount read-only, see btrfs_remount_ro(). */
+	if (btrfs_need_cleaner_sleep(fs_info))
+		goto out;
+
 	if (!test_bit(BLOCK_GROUP_FLAG_RELOCATING_REPAIR, &cache->runtime_flags))
 		goto out;
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-18 20:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18  0:48 [PATCH] btrfs: zoned: wait for the relocating repair kthread during unmount Miroslav Cimerman
2026-11-18 12:58 ` [PATCH] btrfs: wait for block group relocation on remount read-only Miroslav Cimerman
2026-11-18 20:25   ` [PATCH v2] " Miroslav Cimerman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®