From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.70.43.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796410823; b=4nA01zKVW7QplXGUK24pDdyddFpOBVHgMGEZb6v4yAtw4P3u8G9V8yxz5BphTDieIdj7 S90QfsJkITD1ix5IlykhpcIDhDc/loa6rI9KWvNCT9p055jX7wYg2cs7k+4H+fkcCG1Ab +ZfxwDMbUioTxI/cklr0XgSuTG6iVcoPK2zmHTYCB0Z3UyVAXB+ufSCagigPKU0sdSaWR RUeHzwx55aRiNfx+rT2WeXWew8+LXq3PWOHw2XhZfGJfFVkkjU74gTemeZMnAI7yvCdpQ 86xfl1W1PcgopcP7dVtlbtPi6UeWWzkv5mSu/T2XkuCJIFLe4PqpNVL+qQYDvVc95cg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796410823; h=DMARC-Filter:DKIM-Signature:X-Pm-Submission-Id:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=GITuQHH/ly0lZsVbwNOiqs7Htu3yE6FfOxMpkf3Qaq4=; b=UqN0dqglC1SnzqOi8bgAmNBTOGS83RrjkF8AsPQX76sk/H1J+79utyfGzyL6AmVUPcjE eXH2jRe9essNHJhILnjR2cjUlTQ8Eyk3fUKV4FbAy3f0RqL77mqJM3wEBrpJBghACkWgV SlPjAfcNlUmQaoVgMgCYwcGmVwnF5QUPQm25sqnr6Z+a12hefVoJ3J5PcLwUxLVmP3iRS UOzH4gjKLIlz6D7cFMhnlVRoJ+b0dcvVqmPLRqY8UzZ+Ac9+CixsMyG7BXFSjQab1kOXp ru6sPtXkL69REXJ772DPcnhLa/GH6vW4uDmC2i1BN4cYFO+JHrIY9WN4au5UBiuV21Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=raidlab.org; spf=pass smtp.mailfrom=raidlab.org; dkim=pass header.d=raidlab.org header.i=@raidlab.org header.a=rsa-sha256 header.s=protonmail header.b=JLLNdEFs; arc=none smtp.remote-ip=185.70.43.172 Received: from mail-43172.protonmail.ch (mail-43172.protonmail.ch [185.70.43.172]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BB7A606A330 for ; Fri, 4 Dec 2026 19:00:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1BB7A606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=raidlab.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=raidlab.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=raidlab.org header.i=@raidlab.org header.a=rsa-sha256 header.s=protonmail header.b=JLLNdEFs DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raidlab.org; s=protonmail; t=1796410820; x=1796670020; bh=GITuQHH/ly0lZsVbwNOiqs7Htu3yE6FfOxMpkf3Qaq4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:From:To: Cc:Date:Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=JLLNdEFsu6cz4UG4OtcB0ulxpgAMKxTDHB2oIgM1fQ0H4p6yrgKPkdXHRMY62Zb+R S9JUtDcN7V2BbKdrCWdhfY+5U0Pfq5U6ctSU5qX84blytXjGWJAk+zBGicqplyg2UT kIM06rkEbkBK5LIv+IVl+DPBzrFXYR/0Z/PJUJTELBZI0apzvLdi1e7mNUr8m0zP1j lUJPm8DnYY6fbrkJ672anR3mSe9zqAH58Lt0n59/QM1HquozDMPpuO4v7/qTx9RQJL 56SLJpwcEhi3DKY5MRtISfuymluS7S7HRREaIVc072Mx90yAZbaWwYnF7Tg2sEGAV/ eKFtL65ng1j/g== X-Pm-Submission-Id: 4jh15r0hdQz2ScP0 From: Miroslav Cimerman To: linux-btrfs@vger.kernel.org Cc: David Sterba , Chris Mason , Johannes Thumshirn , Naohiro Aota Subject: [PATCH v3 3/3] btrfs: zoned: finish raid stripe tree zones once all data is written Date: Fri, 4 Dec 2026 18:59:57 +0000 Message-ID: <20261204185957.313982-4-mc@raidlab.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261204185957.313982-1-mc@raidlab.org> References: <20261204185957.313982-1-mc@raidlab.org> X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 its ordered extent finishes or it is freed, and no more data can be allocated from the block group. Wake the work when delalloc_bytes drops to zero. An underflow would skip zero, and the work would wait forever. Assert before both decrements that it does not happen. 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 --- v3: - Split the assertion into ASSERT()s with messages, and assert that delalloc_bytes does not underflow (Johannes). - The assertion relies on alloc_offset of a full block group not moving back. [1] fixes the allocator, which could move it back (Sashiko). - The data relocation block group on RAID0/RAID10 has an older problem of its own (Sashiko). I will send a fix. - Rebased on for-next. 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(). [1] https://lore.kernel.org/linux-btrfs/20261130163800.175298-1-mc@raidlab.org/ fs/btrfs/block-group.c | 23 ++++++++++++++++--- fs/btrfs/inode.c | 6 +++++ fs/btrfs/zoned.c | 52 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 76 insertions(+), 5 deletions(-) diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index e217500c618d..4665999ec072 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -419,13 +419,23 @@ 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. + */ + if (!bg->ro) { + ASSERT(btrfs_is_zoned(bg->fs_info), "bg=%llu", bg->start); + ASSERT(bg->alloc_offset + bg->fs_info->sectorsize > bg->zone_capacity, + "bg=%llu alloc_offset=%llu sectorsize=%u zone_capacity=%llu", + bg->start, 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). @@ -3737,8 +3747,15 @@ 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) { + ASSERT(cache->delalloc_bytes >= num_bytes, + "bg=%llu delalloc_bytes=%llu num_bytes=%llu", + cache->start, cache->delalloc_bytes, num_bytes); 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 906b1cf38607..fc596c31d1c3 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -3134,7 +3134,13 @@ static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, ASSERT(cache); spin_lock(&cache->lock); + ASSERT(cache->delalloc_bytes >= len, + "bg=%llu delalloc_bytes=%llu len=%llu", + cache->start, cache->delalloc_bytes, len); 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 27c304429b6e..42954bc3884d 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 @@ -2599,7 +2600,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)) { @@ -2857,6 +2859,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 its ordered extent finishes or it is freed. + * Waiting with btrfs_wait_ordered_roots() 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; @@ -2881,6 +2908,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. @@ -3333,7 +3375,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