* [PATCH 0/2] btrfs: zoned: unwritten copies of mirrored data writes
@ 2026-11-22 3:48 Miroslav Cimerman
2026-11-22 3:48 ` [PATCH 1/2] btrfs: zoned: use zone append for every copy of a mirrored write Miroslav Cimerman
2026-11-22 3:48 ` [PATCH 2/2] btrfs: zoned: finish raid stripe tree zones once all data is written Miroslav Cimerman
0 siblings, 2 replies; 3+ messages in thread
From: Miroslav Cimerman @ 2026-11-22 3:48 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. Two bugs cause it. Both need writes
to one zone to arrive in a different order than they were allocated,
which parallel writers do.
1/2: since 7.0, every copy but the last is a regular write. A regular
write that arrives before an earlier one fails.
2/2: 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.
Tested in QEMU with KASAN on for-next 184fa2c40f5c, with two zoned
null_blk devices (1 GiB, 16 MiB zones, 8 active zones) and RAID1 data
and metadata on the raid stripe tree. 8 direct writers each wrote 4
files of 8 MiB, and 16 buffered writers did the same with fsync. At
least two runs each:
direct, 8 writers buffered, 16 writers
for-next 5 to 8 of 32 writes writes succeed, but some
fail with EIO copies are not written
with 1/2 writes succeed, but some no errors
copies are not written
with 2/2 writes succeed, but some writes succeed, but some
copies are not written copies are not written
with both no errors no errors
A copy that is not written shows up as a write error in the device
statistics ("errs: wr").
The series is based on 6b72ea3e75f4, which differs from the tested
commit only in qgroup.c.
On for-next, the next mount failed in 3 of 4 runs with "write pointer
offset mismatch". With both patches, it worked in every run. With the
single data profile, for-next shows no failure in the same tests. A set
of degraded-mount tests and the btrfs self-tests show no new failure
with the patches, also on top of my RAID1 series [1].
Each patch alone leaves the failures of the other, so they are sent
together. They apply to for-next without [1].
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.
FuzzyRAID, a fault-injection tester for RAID that is not public at the
moment, reproduced both bugs on its own and found no error with the
patches. With 1/2 missing, a scrub found the copy on one device corrupt
after the writes had succeeded.
[1] https://lore.kernel.org/linux-btrfs/20261121134550.216217-1-mc@raidlab.org/
Miroslav Cimerman (2):
btrfs: zoned: use zone append for every copy of a mirrored write
btrfs: zoned: finish raid stripe tree zones once all data is written
fs/btrfs/bio.c | 1 +
fs/btrfs/block-group.h | 6 ++++++
fs/btrfs/zoned.c | 33 +++++++++++++++++++++++++++++----
3 files changed, 36 insertions(+), 4 deletions(-)
base-commit: 6b72ea3e75f4d20a0ad1e9c95d7f9f1b8f192b92
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH 1/2] btrfs: zoned: use zone append for every copy of a mirrored write
2026-11-22 3:48 [PATCH 0/2] btrfs: zoned: unwritten copies of mirrored data writes Miroslav Cimerman
@ 2026-11-22 3:48 ` Miroslav Cimerman
2026-11-22 3:48 ` [PATCH 2/2] btrfs: zoned: finish raid stripe tree zones once all data is written Miroslav Cimerman
1 sibling, 0 replies; 3+ messages in thread
From: Miroslav Cimerman @ 2026-11-22 3:48 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. At write time, only the device's write
error counter and its "errs: wr" log line show it. The failed write also
leaves a gap in that zone. Every regular write allocated behind the gap
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 # 7.0+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
During a device replace, writes to block groups that were already copied
are sent to the target through the same function. With this patch the
source copy is a zone append write again, as before 7.0. I did not test
a replace.
fs/btrfs/bio.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/fs/btrfs/bio.c b/fs/btrfs/bio.c
index efbce4d549ff..8aac4afa30ee 100644
--- a/fs/btrfs/bio.c
+++ b/fs/btrfs/bio.c
@@ -519,6 +519,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 [flat|nested] 3+ messages in thread* [PATCH 2/2] btrfs: zoned: finish raid stripe tree zones once all data is written
2026-11-22 3:48 [PATCH 0/2] btrfs: zoned: unwritten copies of mirrored data writes Miroslav Cimerman
2026-11-22 3:48 ` [PATCH 1/2] btrfs: zoned: use zone append for every copy of a mirrored write Miroslav Cimerman
@ 2026-11-22 3:48 ` Miroslav Cimerman
1 sibling, 0 replies; 3+ messages in thread
From: Miroslav Cimerman @ 2026-11-22 3:48 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. 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
Count the completed bytes of such a block group in zone_written. Finish
its zones only when no more data can be allocated from it and all
allocated data is written. A block group loaded at mount counts as
written, and btrfs_reset_unused_block_groups() resets the counter with
alloc_offset. Bytes of a failed ordered extent, or of an extent freed
before its ordered extent was created, are not counted. Such a block
group is not finished here. Before, this happened only when the failed
extent was the last one. btrfs_zone_finish_one_bg() finishes it later
when an active zone is needed.
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>
---
The reset of zone_written in btrfs_reset_unused_block_groups() was not
reached in my tests. Without it, a reused block group would be finished
too early. The function already resets meta_write_pointer there for the
same reason.
fs/btrfs/block-group.h | 6 ++++++
fs/btrfs/zoned.c | 33 +++++++++++++++++++++++++++++----
2 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h
index d567ed822e55..1f88ecbc0b59 100644
--- a/fs/btrfs/block-group.h
+++ b/fs/btrfs/block-group.h
@@ -266,6 +266,12 @@ struct btrfs_block_group {
u64 alloc_offset;
u64 zone_unusable;
u64 zone_capacity;
+ /*
+ * Bytes of allocated data whose write has completed. Only used for
+ * data block groups with the raid stripe tree on a zoned filesystem.
+ * Protected by the spinlock 'lock'.
+ */
+ u64 zone_written;
u64 meta_write_pointer;
struct btrfs_chunk_map *physical_map;
struct list_head active_bg_list;
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index e6ce6f05ad75..02baf1ecc55e 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
@@ -2015,6 +2016,8 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
if (!ret) {
cache->meta_write_pointer = cache->alloc_offset + cache->start;
+ /* Everything allocated at load time is written. */
+ cache->zone_written = cache->alloc_offset;
if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags)) {
spin_lock(&fs_info->zone_active_bgs_lock);
if (list_empty(&cache->active_bg_list)) {
@@ -2594,7 +2597,9 @@ 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, btrfs_zone_finish_endio() calls this only once all
+ * allocated data is written.
*/
if (!fully_written) {
if (test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
@@ -2798,10 +2803,29 @@ int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 leng
else
min_alloc_bytes = fs_info->nodesize;
- /* Bail out if we can allocate more data from this block group. */
- if (logical + length + min_alloc_bytes <=
- block_group->start + block_group->zone_capacity)
+ if (btrfs_need_stripe_tree_update(fs_info, block_group->flags)) {
+ bool written;
+
+ /*
+ * With the raid stripe tree, an extent keeps the address it was
+ * allocated at, and zone append places its stripes wherever the
+ * device writes them. The extent at the end of the block group
+ * can complete before other extents of it are written, so only
+ * finish the zones once all allocated data is written.
+ */
+ spin_lock(&block_group->lock);
+ block_group->zone_written += length;
+ written = (block_group->alloc_offset + min_alloc_bytes >
+ block_group->zone_capacity &&
+ block_group->zone_written >= block_group->alloc_offset);
+ spin_unlock(&block_group->lock);
+ if (!written)
+ goto out;
+ } else if (logical + length + min_alloc_bytes <=
+ block_group->start + block_group->zone_capacity) {
+ /* Bail out if we can allocate more data from this block group. */
goto out;
+ }
ret = do_zone_finish(block_group, true);
@@ -3282,6 +3306,7 @@ int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num
reclaimed = bg->alloc_offset;
bg->zone_unusable = bg->length - bg->zone_capacity;
bg->alloc_offset = 0;
+ bg->zone_written = 0;
/*
* The zone was just reset to empty, so alloc_offset went back to
* the start of the zone. For metadata/system block groups the
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-22 3:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-22 3:48 [PATCH 0/2] btrfs: zoned: unwritten copies of mirrored data writes Miroslav Cimerman
2026-11-22 3:48 ` [PATCH 1/2] btrfs: zoned: use zone append for every copy of a mirrored write Miroslav Cimerman
2026-11-22 3:48 ` [PATCH 2/2] 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
all inboxes | Powered by JetHome®