The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Miroslav Cimerman <mc@raidlab.org>
To: linux-btrfs@vger.kernel.org
Cc: David Sterba <dsterba@suse.com>, Chris Mason <mason@kernel.org>,
	Naohiro Aota <naohiro.aota@wdc.com>,
	Johannes Thumshirn <johannes.thumshirn@wdc.com>,
	Qu Wenruo <wqu@suse.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] btrfs: zoned: finish raid stripe tree zones once all data is written
Date: Sun, 22 Nov 2026 03:48:25 +0000	[thread overview]
Message-ID: <20261122034826.2333981-3-mc@raidlab.org> (raw)
In-Reply-To: <20261122034826.2333981-1-mc@raidlab.org>

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


      parent reply	other threads:[~2026-11-22  3:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261122034826.2333981-3-mc@raidlab.org \
    --to=mc@raidlab.org \
    --cc=dsterba@suse.com \
    --cc=johannes.thumshirn@wdc.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mason@kernel.org \
    --cc=naohiro.aota@wdc.com \
    --cc=wqu@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox