Linux Btrfs filesystem development
 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 v2 3/3] btrfs: zoned: finish raid stripe tree zones once all data is written
Date: Sun, 29 Nov 2026 23:52:26 +0000	[thread overview]
Message-ID: <20261129235226.459661-4-mc@raidlab.org> (raw)
In-Reply-To: <20261129235226.459661-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. 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


      parent reply	other threads:[~2026-11-29 23:55 UTC|newest]

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