The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2 0/3] btrfs: zoned: unwritten copies of mirrored data writes
@ 2026-11-29 23:52 Miroslav Cimerman
  2026-11-29 23:52 ` [PATCH v2 1/3] btrfs: zoned: use zone append for every copy of a mirrored write Miroslav Cimerman
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Miroslav Cimerman @ 2026-11-29 23:52 UTC (permalink / raw)
  To: linux-btrfs
  Cc: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	Qu Wenruo, linux-kernel

On a zoned filesystem, a mirrored data write can lose a copy, or fail,
without any problem on the devices. The bugs fixed by 1/3 and 3/3 need
writes to one zone to arrive out of allocation order, which parallel
writers do.

1/3: since 7.0, every copy but the last is a regular write. A regular
write that arrives before an earlier one fails.

2/3: since 5.19, unmount does not wait for the work that finishes the
zones of a full metadata block group. 3/3 uses that work for data too.

3/3: since 6.7, with the raid stripe tree, the zones of a block group
are finished when the extent at its end completes. Zone append writes of
other extents that are still queued then fail.

Changes in v2:
- 1/3: Reviewed-by from Johannes. Stable tag 6.18+: the fixed commit
  is in 6.18.y too. Rebased on for-next.
- 2/3: new.
- 3/3: no new field in btrfs_block_group (Johannes). zone_finish_work
  finishes the zones once every extent that zone append writes to the
  block group is written or freed. Rebased on for-next.

Tested on for-next 1adb6f14ca90 as in v1 [1], with 8 active zones per
device. With 1/3 alone, zone append writes failed in 10 of 10 runs with
8 direct writers. With the series, 31 runs showed no failed write. A
test-only print showed the work waiting for other writes before 82 of
160 zone finishes. With a test-only delay in the work, an unmount right
after the writes hit an assertion in 9 of 9 runs without 2/3 and in
none of 6 runs with it.

Found by an LLM while testing a fix on zoned RAID1. The analysis, the
fixes and the tests were done with the help of an LLM. I reviewed
everything.

[1] https://lore.kernel.org/linux-btrfs/20261122034826.2333981-1-mc@raidlab.org/

Miroslav Cimerman (3):
  btrfs: zoned: use zone append for every copy of a mirrored write
  btrfs: zoned: wait for zone finish works at unmount
  btrfs: zoned: finish raid stripe tree zones once all data is written

 fs/btrfs/bio.c         |  1 +
 fs/btrfs/block-group.c | 16 ++++++++--
 fs/btrfs/disk-io.c     |  7 +++++
 fs/btrfs/inode.c       |  3 ++
 fs/btrfs/zoned.c       | 71 ++++++++++++++++++++++++++++++++++++++++--
 fs/btrfs/zoned.h       |  3 ++
 6 files changed, 96 insertions(+), 5 deletions(-)


base-commit: 1adb6f14ca90851ae4a8279ee90442589c312ba3
-- 
2.53.0


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

* [PATCH v2 1/3] btrfs: zoned: use zone append for every copy of a mirrored write
  2026-11-29 23:52 [PATCH v2 0/3] btrfs: zoned: unwritten copies of mirrored data writes Miroslav Cimerman
@ 2026-11-29 23:52 ` Miroslav Cimerman
  2026-11-29 23:52 ` [PATCH v2 2/3] btrfs: zoned: wait for zone finish works at unmount Miroslav Cimerman
  2026-11-29 23:52 ` [PATCH v2 3/3] btrfs: zoned: finish raid stripe tree zones once all data is written Miroslav Cimerman
  2 siblings, 0 replies; 4+ messages in thread
From: Miroslav Cimerman @ 2026-11-29 23:52 UTC (permalink / raw)
  To: linux-btrfs
  Cc: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	Qu Wenruo, linux-kernel

Since commit b39b26e017c7 ("btrfs: zoned: don't zone append to
conventional zone"), btrfs_submit_dev_bio() uses zone append only if
can_use_append is set in the btrfs_bio. For a mirrored write,
btrfs_submit_mirrored_bio() sends the original bio to the last stripe
and a clone to every other stripe. The clone is set up with
btrfs_bio_init(), which clears can_use_append. So on a zoned filesystem,
every copy of a mirrored data write but the last is a regular write at
the allocated address.

A regular write to a sequential zone has to arrive at the zone's write
pointer. Writes to one zone can arrive in a different order than they
were allocated, for example from two writers. The copy that arrives too
early then fails:

  extent A at offset 0, extent B at offset 64K of one zone
  last stripe (zone append):  B, then A: both written
  stripe 0 (regular write):   B fails, the write pointer is still at 0

A RAID1 write succeeds with one failed copy. A buffered write then
completes with one copy less. Only the device's write error counter and
its "errs: wr" log line show it. The failed write also leaves the write
pointer of that zone behind, so every later regular write to the zone
fails too.

Copy can_use_append to the clone, as commit c757edbef980 ("btrfs: fix
copying the flags of btrfs_bio after split") does for split bios.

Fixes: b39b26e017c7 ("btrfs: zoned: don't zone append to conventional zone")
CC: stable@vger.kernel.org # 6.18+
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
 fs/btrfs/bio.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/fs/btrfs/bio.c b/fs/btrfs/bio.c
index b90ca97c9c4d..2573f980de99 100644
--- a/fs/btrfs/bio.c
+++ b/fs/btrfs/bio.c
@@ -521,6 +521,7 @@ static void btrfs_submit_mirrored_bio(struct btrfs_io_context *bioc, int dev_nr)
 		bio_inc_remaining(orig_bio);
 		btrfs_bio_init(btrfs_bio(bio), orig_bbio->inode,
 			       orig_bbio->file_offset, NULL, NULL);
+		btrfs_bio(bio)->can_use_append = orig_bbio->can_use_append;
 		bio->bi_end_io = btrfs_clone_write_end_io;
 	}
 
-- 
2.53.0


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

* [PATCH v2 2/3] btrfs: zoned: wait for zone finish works at unmount
  2026-11-29 23:52 [PATCH v2 0/3] btrfs: zoned: unwritten copies of mirrored data writes Miroslav Cimerman
  2026-11-29 23:52 ` [PATCH v2 1/3] btrfs: zoned: use zone append for every copy of a mirrored write Miroslav Cimerman
@ 2026-11-29 23:52 ` Miroslav Cimerman
  2026-11-29 23:52 ` [PATCH v2 3/3] btrfs: zoned: finish raid stripe tree zones once all data is written Miroslav Cimerman
  2 siblings, 0 replies; 4+ messages in thread
From: Miroslav Cimerman @ 2026-11-29 23:52 UTC (permalink / raw)
  To: linux-btrfs
  Cc: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	Qu Wenruo, linux-kernel

btrfs_schedule_zone_finish_bg() finishes the zones of a full metadata
block group from zone_finish_work. The work holds a reference to the
block group, and nothing waits for it at unmount. When unmount starts
shortly after a metadata block group became full, the work can still be
queued or running when close_ctree() frees the block groups:

  close_ctree()                     zone_finish_work
                                    waits for the last extent buffer
  btrfs_free_block_groups()
    empties zone_active_bgs
    ASSERT(refs == 1) fails
                                    do_zone_finish()
                                      drops the active_bg_list
                                      reference a second time

Without CONFIG_BTRFS_ASSERT, the work then drops its own reference on a
freed block group.

Wait for the zone finish works of all block groups once no metadata
write is left, in close_ctree() and in the error path of open_ctree().

Fixes: 56fbb0a4e8b3 ("btrfs: zoned: properly finish block group on metadata write")
CC: stable@vger.kernel.org # 5.19+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
Found while preparing v2 of 3/3. Tested with a test-only delay of 3 s in
the work and an unmount right after the writes. Without this patch, 4
of 4 runs on the metadata path hit ASSERT(refs == 1) in
btrfs_free_block_groups() and then
ASSERT(!list_empty(&block_group->active_bg_list)) in do_zone_finish().
With it, unmount waited for the works in 6 of 6 runs, metadata and data.

The flush runs after btrfs_zoned_release_dirty_metadata(), so no later
metadata write can queue a work. It runs before
invalidate_and_check_btree_folios(), since a queued metadata work holds
an extent buffer.

 fs/btrfs/disk-io.c |  7 +++++++
 fs/btrfs/zoned.c   | 19 +++++++++++++++++++
 fs/btrfs/zoned.h   |  3 +++
 3 files changed, 29 insertions(+)

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index fde9f4c4ea11..8f4f97e492cb 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -3867,6 +3867,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
 	 * kthreads
 	 */
 	filemap_write_and_wait(fs_info->btree_inode->i_mapping);
+	btrfs_zoned_flush_finish_works(fs_info);
 
 fail_sysfs:
 	btrfs_sysfs_remove_mounted(fs_info);
@@ -4635,6 +4636,12 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)
 	 */
 	btrfs_zoned_release_dirty_metadata(fs_info);
 
+	/*
+	 * No metadata write is left that could queue a zone finish work. Wait
+	 * for the queued ones, they still hold extent buffers and block groups.
+	 */
+	btrfs_zoned_flush_finish_works(fs_info);
+
 	/*
 	 * We must make sure there is not any read request to
 	 * submit after we stop all workers.
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 0277b2a48a73..a4a5231daf9c 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2892,6 +2892,25 @@ void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
 	queue_work(system_dfl_wq, &bg->zone_finish_work);
 }
 
+/*
+ * Wait for the zone finish works of all block groups. Unmount calls this before
+ * it stops the workers and frees the block groups.
+ */
+void btrfs_zoned_flush_finish_works(struct btrfs_fs_info *fs_info)
+{
+	struct btrfs_block_group *bg;
+
+	if (!btrfs_is_zoned(fs_info))
+		return;
+
+	for (bg = btrfs_lookup_first_block_group(fs_info, 0); bg;
+	     bg = btrfs_next_block_group(bg)) {
+		/* The work is initialized when it is queued the first time. */
+		if (bg->zone_finish_work.func)
+			flush_work(&bg->zone_finish_work);
+	}
+}
+
 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
 {
 	struct btrfs_fs_info *fs_info = bg->fs_info;
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 2225090839f3..e0934a93b06b 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -92,6 +92,7 @@ int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical,
 			     u64 length);
 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
 				   struct extent_buffer *eb);
+void btrfs_zoned_flush_finish_works(struct btrfs_fs_info *fs_info);
 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg);
 void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info);
 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info);
@@ -260,6 +261,8 @@ static inline int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info,
 static inline void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
 						 struct extent_buffer *eb) { }
 
+static inline void btrfs_zoned_flush_finish_works(struct btrfs_fs_info *fs_info) { }
+
 static inline void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) { }
 
 static inline void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info) { }
-- 
2.53.0


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

* [PATCH v2 3/3] btrfs: zoned: finish raid stripe tree zones once all data is written
  2026-11-29 23:52 [PATCH v2 0/3] btrfs: zoned: unwritten copies of mirrored data writes Miroslav Cimerman
  2026-11-29 23:52 ` [PATCH v2 1/3] btrfs: zoned: use zone append for every copy of a mirrored write Miroslav Cimerman
  2026-11-29 23:52 ` [PATCH v2 2/3] btrfs: zoned: wait for zone finish works at unmount Miroslav Cimerman
@ 2026-11-29 23:52 ` Miroslav Cimerman
  2 siblings, 0 replies; 4+ messages in thread
From: Miroslav Cimerman @ 2026-11-29 23:52 UTC (permalink / raw)
  To: linux-btrfs
  Cc: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	Qu Wenruo, linux-kernel

btrfs_zone_finish_endio() finishes the zones of a data block group when
the ordered extent that ends at its capacity completes. It assumes that
all other data of the block group is written by then. Without the raid
stripe tree that holds: btrfs_finish_ordered_zoned() moves an extent to
the address where zone append wrote it, so the extent at the end of the
block group is the last one the device wrote.

With the raid stripe tree, an extent keeps its allocated address, and
the stripe tree records where each copy was written. The extent at the
end can then complete while zone append writes of other extents are
still queued. On a device with a limit of active zones, they reach a
finished zone and fail:

  C allocated at 14M, D at 15M, the capacity is 16M
  D completes, btrfs_zone_finish_endio() finishes the zones
  C's zone append writes reach the full zones and fail

For such a block group, finish the zones from zone_finish_work, like the
metadata path does. The work waits until the allocators are done and
delalloc_bytes of the block group drops to zero, then finishes the
zones. delalloc_bytes counts every extent that zone append writes to the
block group until it is written or freed, and no more data can be
allocated from the block group. Wake the work when delalloc_bytes drops
to zero. Let btrfs_wait_block_group_reservations() accept a zoned block
group with less than a sector left. Like a read-only one, it takes no
new allocations.

The wait cannot run in btrfs_zone_finish_endio(). Its caller releases
the delalloc bytes of its own extent only afterwards. Waiting for the
ordered extents of the block group could hang. A writer that waits for a
zone finish can hold a folio locked. Writeback started by that wait, or
by sync while it holds the ordered extent mutexes the wait needs, would
wait for the folio.

Let btrfs_reset_unused_block_groups() skip a block group whose
zone_finish_work is still queued or running. The work would finish the
zones after the reset.

Fixes: 568220fa9657 ("btrfs: zoned: support RAID0/1/10 on top of raid stripe tree")
CC: stable@vger.kernel.org # 6.7+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
v2:
- No new field in btrfs_block_group (Johannes). Finish the zones from
  zone_finish_work, which waits until every extent that zone append
  writes to the block group is written or freed (delalloc_bytes drops
  to zero).
- Let btrfs_wait_block_group_reservations() accept a zoned block group
  with less than a sector left, and btrfs_reset_unused_block_groups()
  skip a block group with a queued or running zone_finish_work.
- Unmount waits for the work since 2/3.
- Rebased on for-next. 6.12 needs a backport: it lacks system_dfl_wq
  and wait_var_event_spinlock().

 fs/btrfs/block-group.c | 16 ++++++++++---
 fs/btrfs/inode.c       |  3 +++
 fs/btrfs/zoned.c       | 52 ++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 66 insertions(+), 5 deletions(-)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 0736132624f8..ce3f7b98f00d 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -419,13 +419,19 @@ void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg)
 {
 	struct btrfs_space_info *space_info = bg->space_info;
 
-	ASSERT(bg->ro);
+	/*
+	 * A zoned block group with less than a sector left takes no new
+	 * allocations either.
+	 */
+	ASSERT(bg->ro || (btrfs_is_zoned(bg->fs_info) &&
+			  bg->alloc_offset + bg->fs_info->sectorsize >
+			  bg->zone_capacity));
 
 	if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
 		return;
 
 	/*
-	 * Our block group is read only but before we set it to read only,
+	 * No more extents are allocated from our block group, but before that,
 	 * some task might have had allocated an extent from it already, but it
 	 * has not yet created a respective ordered extent (and added it to a
 	 * root's list of ordered extents).
@@ -3732,8 +3738,12 @@ void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, u64 num_bytes,
 	bg_ro = cache->ro;
 	cache->reserved -= num_bytes;
 	btrfs_maybe_reset_size_class(cache);
-	if (is_delalloc)
+	if (is_delalloc) {
 		cache->delalloc_bytes -= num_bytes;
+		/* See btrfs_zone_finish_rst_workfn(). */
+		if (!cache->delalloc_bytes)
+			wake_up_var_locked(&cache->delalloc_bytes, &cache->lock);
+	}
 	spin_unlock(&cache->lock);
 
 	if (bg_ro)
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 40dd87276cc9..ca04fb0e667c 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -3128,6 +3128,9 @@ static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
 
 	spin_lock(&cache->lock);
 	cache->delalloc_bytes -= len;
+	/* See btrfs_zone_finish_rst_workfn(). */
+	if (!cache->delalloc_bytes)
+		wake_up_var_locked(&cache->delalloc_bytes, &cache->lock);
 	spin_unlock(&cache->lock);
 
 	btrfs_put_block_group(cache);
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index a4a5231daf9c..ca064be9056d 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -18,6 +18,7 @@
 #include "bio.h"
 #include "transaction.h"
 #include "sysfs.h"
+#include "raid-stripe-tree.h"
 
 /* Maximum number of zones to report per blkdev_report_zones() call */
 #define BTRFS_REPORT_NR_ZONES   4096
@@ -2597,7 +2598,8 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
 	 * new allocation) and the IO for the last usable block is completed, we
 	 * don't need to wait for the other IOs. This holds because we ensure
 	 * the sequential IO submissions using the ZONE_APPEND command for data
-	 * and block_group->meta_write_pointer for metadata.
+	 * and block_group->meta_write_pointer for metadata. With the raid stripe
+	 * tree, zone_finish_work waits for the other IOs first.
 	 */
 	if (!fully_written) {
 		if (test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
@@ -2818,6 +2820,31 @@ bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
 	return ret;
 }
 
+static void btrfs_zone_finish_rst_workfn(struct work_struct *work)
+{
+	struct btrfs_block_group *bg =
+		container_of(work, struct btrfs_block_group, zone_finish_work);
+	int ret;
+
+	/*
+	 * No more data can be allocated here. Once the allocators are done,
+	 * delalloc_bytes counts every extent that zone append writes to the
+	 * block group until it is written or freed. Waiting for the ordered
+	 * extents instead could wait for a folio that a writer holds while it
+	 * waits for this zone finish.
+	 */
+	btrfs_wait_block_group_reservations(bg);
+	spin_lock(&bg->lock);
+	wait_var_event_spinlock(&bg->delalloc_bytes, !bg->delalloc_bytes, &bg->lock);
+	spin_unlock(&bg->lock);
+
+	ret = do_zone_finish(bg, true);
+	if (ret)
+		btrfs_warn(bg->fs_info, "zoned: failed to finish zones of block group %llu: %d",
+			   bg->start, ret);
+	btrfs_put_block_group(bg);
+}
+
 int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
 {
 	struct btrfs_block_group *block_group;
@@ -2842,6 +2869,21 @@ int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 leng
 	    block_group->start + block_group->zone_capacity)
 		goto out;
 
+	/*
+	 * With the raid stripe tree, an extent keeps the address it was
+	 * allocated at, and zone append writes it wherever the device puts it.
+	 * The extent at the end can complete while other extents of the block
+	 * group are still being written, so finish the zones from a work that
+	 * waits for them.
+	 */
+	if (btrfs_need_stripe_tree_update(fs_info, block_group->flags)) {
+		/* For the work */
+		btrfs_get_block_group(block_group);
+		INIT_WORK(&block_group->zone_finish_work, btrfs_zone_finish_rst_workfn);
+		queue_work(system_dfl_wq, &block_group->zone_finish_work);
+		goto out;
+	}
+
 	/*
 	 * The data of the ordered extent is written. A failure to finish the
 	 * zones does not change that, so only report it.
@@ -3294,7 +3336,13 @@ int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num
 			 */
 			if (!spin_trylock(&bg->lock))
 				continue;
-			if (btrfs_is_block_group_used(bg) || bg->zone_unusable < bg->length) {
+			/*
+			 * A queued or running zone_finish_work would finish the
+			 * zones after the reset. No new one can be queued: the
+			 * block group has no extent left to complete.
+			 */
+			if (btrfs_is_block_group_used(bg) || bg->zone_unusable < bg->length ||
+			    work_busy(&bg->zone_finish_work)) {
 				spin_unlock(&bg->lock);
 				continue;
 			}
-- 
2.53.0


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

end of thread, other threads:[~2026-11-29 23:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 23:52 [PATCH v2 0/3] btrfs: zoned: unwritten copies of mirrored data writes Miroslav Cimerman
2026-11-29 23:52 ` [PATCH v2 1/3] btrfs: zoned: use zone append for every copy of a mirrored write Miroslav Cimerman
2026-11-29 23:52 ` [PATCH v2 2/3] btrfs: zoned: wait for zone finish works at unmount Miroslav Cimerman
2026-11-29 23:52 ` [PATCH v2 3/3] btrfs: zoned: finish raid stripe tree zones once all data is written Miroslav Cimerman

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