From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=109.224.244.107 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795996546; b=473FuGPD1G9CEqneHsgjX6XutnIzZDyoSAgFADw9Ha0RS9hKADFWdz9nWFOt5EiMTaMx a3x+0v13gpESmt8PuT0S93dbnFWftbV4+Mk9u92xmRcimdSoIk59CXtsj9Jg27dthuiv1 0/y3YzP1h42gPtsHABiQ8BxJNsZs3u5hQwC7oTA0cM4K2bjyOa+C6zpWKS1mvw1EL/g7y vE/rYtGFYOZVEl7V2wMuUqXNU/F/OqCySggCih/c2kKage2WZlElq/tT1abIraRkKQ4KK Uyyy8EKZqL9fiYqK0Q9hBTRyF6HoFlGKulhzGnd+SggW/7sHCKXYt9iycQyaT9QCg/w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795996546; 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=Fu2XKvbeweJIwUI+3Oh5N8DJEomwQfJi4lsci7BRYpQ=; b=augVcKiwAQ6nFzhwpb70A+xdN9xKH4/cyJJcaF3TkLXc2UUVFxheL6CsnjWJHJ37FPsp B1GATJUA/ehtidgMBFx8fQNkr/+6qFfq//RY7eqQkSUHqHGH3vPUDDwABa/kxDDe3NSwK Y5pwmYGAZBD0Hg7tqT5M7/KHfhbfE/fK+TcIrT4jHKOpoA69pWM+6Ja8KmuhE4JIMiUPW Cn4E7oNtceSlWJCdCbFOpKDTSeIVmlaxP9oUMtgisGqFRhNmFcB4kCyyX+Xw6Wl6kifap EQi92mkJDP0AwGzFNCe48XCR5DTj3kMOGQPSzbL11qOmSwfyS+hODjKSXetzMVG+HOA== 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=MeJuF2N2; arc=none smtp.remote-ip=109.224.244.107 Received: from mail-244107.protonmail.ch (mail-244107.protonmail.ch [109.224.244.107]) (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 85B76606A680 for ; Sun, 29 Nov 2026 23:55:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 85B76606A680 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=MeJuF2N2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raidlab.org; s=protonmail; t=1795996543; x=1796255743; bh=Fu2XKvbeweJIwUI+3Oh5N8DJEomwQfJi4lsci7BRYpQ=; 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=MeJuF2N2fCPotR+6ccIGTj43J+yKsoh/Agn5ifinxw5nhhjFm3i4t63NmxzQV/dUd HoPZ0o8IehqYgDfd3yVq+lKay8B2q4vrnxr2XSbMKMMKBs8OnZeMCMU/p9QRdoLMB2 j/LJAk7tUm1EYX7JTG779QtL8yHIGM6316hIwaR7nJiTl7medpPukRvcbY8HbnaA5t fKOqhcPK7qdHJp9eMfyRITU3FtM5t9q6Gq79/l43fhTTzHwpoiFab8xqQibZU74miy AqtHanjx4sDJr8S0rqN8L6w5lT9Q7taMW8V8PTFQLTsU3v/u012utoOWYnq8Mahxcf aZoeEpFldi1OA== X-Pm-Submission-Id: 4jd3v11L2Kz2ScWP From: Miroslav Cimerman To: linux-btrfs@vger.kernel.org Cc: David Sterba , Chris Mason , Naohiro Aota , Johannes Thumshirn , Qu Wenruo , 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 Message-ID: <20261129235226.459661-4-mc@raidlab.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261129235226.459661-1-mc@raidlab.org> References: <20261129235226.459661-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 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 --- 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