* [PATCH v7 00/27] btrfs: zoned: fixes for generic/747
@ 2026-11-17 18:58 Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 05/27] btrfs: zoned: skip the append only metadata writeback path for conventional block groups Johannes Thumshirn
` (17 more replies)
0 siblings, 18 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
At the moment, zoned btrfs doesn't distinguish between DATA and
METADATA/SYSTEM block-groups, when allocating a new chunk. Therefore
METADATA and SYSTEM block-groups can end up on sequential write only
zones of a drive. This adds a lot of complexity, as we can only append
to these zones (and not perform random writes) and for METADATA we need
to write with a queue-depth of 1 using the btrfs_zoned_meta_io_lock().
Also the write stream for METADATA is not allowed to have any "holes" in
it and must be aligned to the write pointer, which was a source of hard
to fix bugs in the past.
This series introduces a split in the block-group allocation path on
zoned filesystems. For METADATA and SYSTEM block-groups try to place
them on conventional zones if the device has them and enough are free.
Then change the allocation in these block-groups from the zoned
allocator to the unclustered allocator, which is less complex than
setting up a cluster for them only. The reverse is done for DATA
block-groups, which can be trivially written on sequential write
required block-groups as we use REQ_OP_ZONE_APPEND for writing DATA with
a high queue-depth.
In addition to this, this series reworks how garbage collection works in
zoned btrfs by tackling the remaining accounting problems and relocation
still being triggered not timely enough to evacuate the space the user
write needs.
The first two patches are fixes that are independent of the rest. The
first one stops allocations from waiting forever for a zone finish after
a transaction abort or shutdown, which hangs generic/388 and is much
easier to hit once metadata lives on conventional zones. As whether a
block-group is append only is derived from the zone types of its stripes,
dev-replace must not change them, so the second one rejects a replace
onto a device with a different conventional/sequential layout.
Changes to v6:
- Rebase onto current btrfs/for-next
- New patch: don't wait for zone finish after fs error
- New patch: reject dev-replace on zone type mismatch
- Rename "don't activate conventional zones of mixed block groups"
- Drop its reservation hunk, already upstream
- Clarify its active state invariants in the changelog
- Keep upstream zone finish in btrfs_relocate_chunk_finish()
- Explain lockless check in "never activate conventional metadata block groups"
- ASSERT zoned and data space-info in btrfs_zoned_calc_inflight()
- Add Boris' Reviewed-by tags
- Mention why the unclustered allocator is used
Changes to v5:
- Don't overwrite ffe_ctl->policy, pick the allocator per block-group
- Retry without empty_size in LOOP_NO_EMPTY_SIZE on zoned as well, so non
append only block-groups don't hit a premature ENOSPC
- Perform space_info migration only on ro block-groups
- Only set data_reloc_bg under relocation_bg_lock if it isn't set yet and
undo the space_info migration otherwise
- Skip a block-group in do_allocation_zoned() instead of ASSERT()ing when
data_reloc_bg points to a different block-group, and only clear
data_reloc_bg if it points to the current block-group
- Don't account bytes_super twice when moving a block-group between
space_infos
- Return the free space of a reset block-group to its current space_info
if promoting it to the relocation target fails
- Reword commit messages of "use the regular allocator for conventional
metadata block groups" and "return finished relocation block groups to
the data space_info"
Changes to v4:
- Fixup broken rebase
Changes to v3:
- Fix the active-zone reservation race
- defer RAID activity publication until all stripe write pointers and
capacities have been validated.
Johannes Thumshirn (27):
btrfs: zoned: don't wait for a zone finish after a filesystem error
btrfs: zoned: reject dev-replace onto a target with different zone
types
btrfs: zoned: fix possible race for reserved_active_zones
btrfs: zoned: don't use active zones for conventional stripes
btrfs: zoned: skip the append only metadata writeback path for
conventional block groups
btrfs: zoned: never activate conventional metadata block groups
btrfs: zoned: use the regular allocator for conventional metadata
block groups
btrfs: zoned: only take the metadata IO lock for append only block
groups
btrfs: zoned: allow restricting the dev extent search to a zone type
btrfs: zoned: prefer conventional zones for metadata chunks
btrfs: zoned: use plain mutex lock in check_bg_is_active
btrfs: zoned: use plain mutex lock in
btrfs_zoned_release_dirty_metadata
btrfs: zoned: remove btrfs_zoned_meta_io_(un)lock
btrfs: zoned: prefer sequential zones for data chunks
btrfs: zoned: wait for a running block group reclaim in RECLAIM_ZONES
btrfs: zoned: don't count in-flight data as available in statfs
btrfs: zoned: don't recheck the reclaim threshold of full zoned
block-groups
btrfs: zoned: don't account superblock zones as available data space
btrfs: zoned: factor out resetting a block group in place
btrfs: zoned: prepare reserving the data relocation block group at
runtime
btrfs: zoned: promote reset data block groups to the relocation target
btrfs: zoned: return finished relocation block groups to the data
space_info
btrfs: zoned: reuse block groups emptied by reclaim as relocation
target
btrfs: add btrfs_bg_type_to_num_stripes
btrfs: zoned: keep a zone free for relocation when allocating data
chunks
btrfs: zoned: don't count the relocation zone reserve as available in
statfs
btrfs: zoned: provide zoned specific reclaim sweep
fs/btrfs/block-group.c | 56 ++-
fs/btrfs/block-group.h | 2 +
fs/btrfs/dev-replace.c | 9 +
fs/btrfs/direct-io.c | 4 +-
fs/btrfs/extent-tree.c | 50 +-
fs/btrfs/extent_io.c | 84 ++--
fs/btrfs/extent_io.h | 2 +
fs/btrfs/free-space-cache.c | 23 +-
fs/btrfs/fs.h | 3 +
fs/btrfs/inode.c | 3 +
fs/btrfs/messages.c | 1 +
fs/btrfs/space-info.c | 3 +-
fs/btrfs/super.c | 33 +-
fs/btrfs/tests/zoned-tests.c | 16 +
fs/btrfs/volumes.c | 107 ++++-
fs/btrfs/volumes.h | 1 +
fs/btrfs/zoned.c | 893 ++++++++++++++++++++++++++++-------
fs/btrfs/zoned.h | 118 ++++-
18 files changed, 1136 insertions(+), 272 deletions(-)
--
2.56.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v7 05/27] btrfs: zoned: skip the append only metadata writeback path for conventional block groups
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 06/27] btrfs: zoned: never activate conventional metadata " Johannes Thumshirn
` (16 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
Metadata living in a block group backed by conventional zones only can
be overwritten in place and in arbitrary order. There is no write
pointer to honour, so there is no need to serialize the extent buffer
writeback against block_group::meta_write_pointer, to keep a zone
active, or to write out a freed tree block as zeros just to advance the
write pointer.
Add btrfs_zoned_bg_is_append_only() and use it to bypass all of that
for such block groups, which lets metadata writeback proceed out of
order and with a queue depth larger than one.
Freeing a dirty tree block needs to know whether its block group is
append only. Count metadata block groups with and without sequential
zones, so the block group lookup is only needed if both kinds exist.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/block-group.c | 7 ++++---
fs/btrfs/extent_io.c | 10 +++++-----
fs/btrfs/fs.h | 2 ++
fs/btrfs/zoned.c | 29 ++++++++++++++++++++++++++++-
fs/btrfs/zoned.h | 34 ++++++++++++++++++++++++++++++++++
5 files changed, 73 insertions(+), 9 deletions(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index a40eedc65dd2..7853d227a204 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -64,11 +64,11 @@ int btrfs_should_fragment_free_space(const struct btrfs_block_group *block_group
static inline bool has_unwritten_metadata(struct btrfs_block_group *block_group)
{
- /* The meta_write_pointer is available only on the zoned setup. */
- if (!btrfs_is_zoned(block_group->fs_info))
+ if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
return false;
- if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
+ /* The meta_write_pointer is tracked for append only block groups only. */
+ if (!btrfs_zoned_bg_is_append_only(block_group))
return false;
return block_group->start + block_group->alloc_offset >
@@ -1204,6 +1204,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
rb_erase_cached(&block_group->cache_node,
&fs_info->block_group_cache_tree);
RB_CLEAR_NODE(&block_group->cache_node);
+ btrfs_zoned_account_meta_bg(block_group, -1);
/* Once for the block groups rbtree */
btrfs_put_block_group(block_group);
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index b2739838c646..6ba202b312b5 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -2535,8 +2535,7 @@ static int write_meta_extent_buffer(struct btrfs_eb_write_context *ctx,
if (!lock_extent_buffer_for_io(eb, wbc))
return 0;
- /* Implies write in zoned mode. */
- if (ctx->zoned_bg) {
+ if (ctx->zoned_bg && btrfs_zoned_bg_is_append_only(ctx->zoned_bg)) {
/* Mark the last eb in the block group. */
btrfs_schedule_zone_finish_bg(ctx->zoned_bg, eb);
ctx->zoned_bg->meta_write_pointer += eb->len;
@@ -4169,13 +4168,14 @@ void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans,
/*
* Instead of clearing the dirty flag off of the buffer, mark it as
* EXTENT_BUFFER_ZONED_ZEROOUT. This allows us to preserve
- * write-ordering in zoned mode, without the need to later re-dirty
- * the extent_buffer.
+ * write-ordering in append only block groups, without the need to
+ * later re-dirty the extent_buffer.
*
* The actual zeroout of the buffer will happen later in
* btree_csum_one_bio.
*/
- if (btrfs_is_zoned(fs_info) && test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
+ if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags) &&
+ btrfs_zoned_meta_is_append_only(fs_info, eb->start)) {
set_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &eb->bflags);
return;
}
diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
index 84a6eb2ebf83..401a5fcf45d5 100644
--- a/fs/btrfs/fs.h
+++ b/fs/btrfs/fs.h
@@ -919,6 +919,8 @@ struct btrfs_fs_info {
u64 max_zone_append_size;
struct mutex zoned_meta_io_lock;
+ atomic_t zoned_seq_meta_bgs;
+ atomic_t zoned_conv_meta_bgs;
spinlock_t treelog_bg_lock;
u64 treelog_bg;
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 169afbcd8be7..a8762033e44d 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2019,6 +2019,7 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
}
if (!ret) {
+ btrfs_zoned_account_meta_bg(cache, 1);
cache->meta_write_pointer = cache->alloc_offset + cache->start;
if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags)) {
spin_lock(&fs_info->zone_active_bgs_lock);
@@ -2300,6 +2301,9 @@ int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
ctx->zoned_bg = block_group;
}
+ if (!btrfs_zoned_bg_is_append_only(block_group))
+ return 0;
+
if (block_group->meta_write_pointer == eb->start) {
struct btrfs_block_group **tgt;
@@ -2327,6 +2331,29 @@ int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
return -EBUSY;
}
+bool btrfs_zoned_meta_is_append_only(struct btrfs_fs_info *fs_info, u64 logical)
+{
+ struct btrfs_block_group *bg;
+ bool ret;
+
+ if (!btrfs_is_zoned(fs_info))
+ return false;
+
+ if (!atomic_read(&fs_info->zoned_conv_meta_bgs))
+ return true;
+ if (!atomic_read(&fs_info->zoned_seq_meta_bgs))
+ return false;
+
+ bg = btrfs_lookup_block_group(fs_info, logical);
+ if (!bg)
+ return false;
+
+ ret = btrfs_zoned_bg_is_append_only(bg);
+ btrfs_put_block_group(bg);
+
+ return ret;
+}
+
int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
{
if (!btrfs_dev_is_sequential(device, physical))
@@ -2594,7 +2621,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
}
/* Check if we have unwritten allocated space */
- if (is_metadata &&
+ if (is_metadata && btrfs_zoned_bg_is_append_only(block_group) &&
block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
spin_unlock(&block_group->lock);
return -EAGAIN;
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 2225090839f3..3c561f6ad228 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -81,6 +81,7 @@ bool btrfs_use_zone_append(struct btrfs_bio *bbio);
void btrfs_record_physical_zoned(struct btrfs_bio *bbio);
int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
struct btrfs_eb_write_context *ctx);
+bool btrfs_zoned_meta_is_append_only(struct btrfs_fs_info *fs_info, u64 logical);
int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length);
int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
u64 physical_start, u64 physical_pos);
@@ -220,6 +221,12 @@ static inline int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
return 0;
}
+static inline bool btrfs_zoned_meta_is_append_only(struct btrfs_fs_info *fs_info,
+ u64 logical)
+{
+ return false;
+}
+
static inline int btrfs_zoned_issue_zeroout(struct btrfs_device *device,
u64 physical, u64 length)
{
@@ -439,4 +446,31 @@ static inline bool btrfs_zoned_bg_is_full(const struct btrfs_block_group *bg)
return (bg->alloc_offset == bg->zone_capacity);
}
+/*
+ * Data is always allocated append only, even on conventional zones. Metadata
+ * only has to be, if the block group has at least one sequential zone.
+ */
+static inline bool btrfs_zoned_bg_is_append_only(const struct btrfs_block_group *bg)
+{
+ if (!btrfs_is_zoned(bg->fs_info))
+ return false;
+ if (bg->flags & BTRFS_BLOCK_GROUP_DATA)
+ return true;
+
+ return test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &bg->runtime_flags);
+}
+
+static inline void btrfs_zoned_account_meta_bg(struct btrfs_block_group *bg, int delta)
+{
+ struct btrfs_fs_info *fs_info = bg->fs_info;
+
+ if (!btrfs_is_zoned(fs_info) || (bg->flags & BTRFS_BLOCK_GROUP_DATA))
+ return;
+
+ if (test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &bg->runtime_flags))
+ atomic_add(delta, &fs_info->zoned_seq_meta_bgs);
+ else
+ atomic_add(delta, &fs_info->zoned_conv_meta_bgs);
+}
+
#endif
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 06/27] btrfs: zoned: never activate conventional metadata block groups
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 05/27] btrfs: zoned: skip the append only metadata writeback path for conventional block groups Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 07/27] btrfs: zoned: use the regular allocator for " Johannes Thumshirn
` (15 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
Metadata and system block groups backed by conventional zones only are
written in place, so they do not need a zone to be active and they do
not consume any active zone resource on the device.
Make btrfs_zone_activate() a no-op for them and skip them in
btrfs_zoned_activate_one_bg(), which would otherwise report progress
without having activated anything. The check doesn't need the
block-group lock: the block-group type and
BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE are set before the block-group is
visible and never change afterwards.
Such block groups are still marked active when they are loaded. That is
dropped once they stop using the append only allocator.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/zoned.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index a8762033e44d..bc83f4a2577b 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2463,6 +2463,9 @@ bool btrfs_zone_activate(struct btrfs_block_group *block_group)
if (unlikely(btrfs_is_testing(fs_info)))
return true;
+ if (!btrfs_zoned_bg_is_append_only(block_group))
+ return true;
+
map = block_group->physical_map;
spin_lock(&fs_info->zone_active_bgs_lock);
@@ -3127,6 +3130,8 @@ int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_fin
for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
list_for_each_entry(bg, &space_info->block_groups[index],
list) {
+ if (!btrfs_zoned_bg_is_append_only(bg))
+ continue;
if (!spin_trylock(&bg->lock))
continue;
if (btrfs_zoned_bg_is_full(bg) ||
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 07/27] btrfs: zoned: use the regular allocator for conventional metadata block groups
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 05/27] btrfs: zoned: skip the append only metadata writeback path for conventional block groups Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 06/27] btrfs: zoned: never activate conventional metadata " Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 08/27] btrfs: zoned: only take the metadata IO lock for append only " Johannes Thumshirn
` (14 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
The zoned allocator hands out space strictly sequentially from
block_group::alloc_offset and accounts everything freed below it as
zone_unusable, so freed tree blocks can only be reclaimed by relocating
the whole block group.
A metadata or system block group backed by conventional zones only can
be overwritten in place, so it can use the regular free space cache and
the free space cache based (unclustered) allocator instead, and freed
tree blocks become immediately reusable.
Pick the allocation scheme per block group rather than per filesystem,
replace the filesystem wide zoned tests in the free space tracking, the
space accounting and the extent allocator with a check if the
block-group is append only.
Such block groups no longer need an allocation pointer either, so skip
calculate_alloc_pointer() for them when loading the zone info. As a
consequence they are no longer marked active and put on
fs_info::zone_active_bgs.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/block-group.c | 16 ++++++++--------
fs/btrfs/extent-tree.c | 38 +++++++++++++++++++++++++++++--------
fs/btrfs/free-space-cache.c | 23 +++++++++++-----------
fs/btrfs/zoned.c | 6 ++++++
4 files changed, 56 insertions(+), 27 deletions(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 7853d227a204..625f3533ec09 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -953,8 +953,8 @@ int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait)
struct btrfs_caching_control *caching_ctl = NULL;
int ret = 0;
- /* Allocator for zoned filesystems does not use the cache at all */
- if (btrfs_is_zoned(fs_info))
+ /* The append only allocator does not use the cache at all */
+ if (btrfs_zoned_bg_is_append_only(cache))
return 0;
/*
@@ -1455,7 +1455,7 @@ static int inc_block_group_ro(struct btrfs_block_group *cache, bool force)
if (!ret) {
sinfo->bytes_readonly += num_bytes;
- if (btrfs_is_zoned(cache->fs_info)) {
+ if (btrfs_zoned_bg_is_append_only(cache)) {
/* Migrate zone_unusable bytes to readonly */
sinfo->bytes_readonly += cache->zone_unusable;
btrfs_space_info_update_bytes_zone_unusable(sinfo, -cache->zone_unusable);
@@ -2556,8 +2556,8 @@ static int read_one_block_group(struct btrfs_fs_info *info,
}
/*
- * For zoned filesystem, space after the allocation offset is the only
- * free space for a block group. So, we don't need any caching work.
+ * For an append only block group, space after the allocation offset is
+ * the only free space. So, we don't need any caching work.
* btrfs_calc_zone_unusable() will set the amount of free space and
* zone_unusable space.
*
@@ -2567,7 +2567,7 @@ static int read_one_block_group(struct btrfs_fs_info *info,
* in and be done with it. This saves us _a_lot_ of time, particularly
* in the full case.
*/
- if (btrfs_is_zoned(info)) {
+ if (btrfs_zoned_bg_is_append_only(cache)) {
btrfs_calc_zone_unusable(cache);
/* Should not have any excluded extents. Just in case, though. */
btrfs_free_excluded_extents(cache);
@@ -3249,7 +3249,7 @@ void btrfs_dec_block_group_ro(struct btrfs_block_group *cache)
spin_lock(&sinfo->lock);
spin_lock(&cache->lock);
if (!--cache->ro) {
- if (btrfs_is_zoned(cache->fs_info)) {
+ if (btrfs_zoned_bg_is_append_only(cache)) {
/* Migrate zone_unusable bytes back */
cache->zone_unusable =
(cache->alloc_offset - cache->used - cache->pinned -
@@ -3731,7 +3731,7 @@ void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, u64 num_bytes,
if (bg_ro)
space_info->bytes_readonly += num_bytes;
- else if (btrfs_is_zoned(cache->fs_info))
+ else if (btrfs_zoned_bg_is_append_only(cache))
btrfs_space_info_update_bytes_zone_unusable(space_info, num_bytes);
space_info->bytes_reserved -= num_bytes;
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 0008cbc12412..b01b3ccc944a 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -2896,7 +2896,7 @@ static int unpin_extent_range(struct btrfs_fs_info *fs_info,
if (readonly) {
space_info->bytes_readonly += len;
- } else if (btrfs_is_zoned(fs_info)) {
+ } else if (btrfs_zoned_bg_is_append_only(cache)) {
/* Need reset before reusing in a zoned block group */
btrfs_space_info_update_bytes_zone_unusable(space_info, len);
} else if (return_free_space) {
@@ -3685,8 +3685,8 @@ int btrfs_free_tree_block(struct btrfs_trans_handle *trans,
* contend with.
*/
- if (test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags)
- || btrfs_is_zoned(fs_info)) {
+ if (test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags) ||
+ btrfs_zoned_bg_is_append_only(bg)) {
pin_down_extent(trans, bg, buf->start, buf->len, true);
btrfs_put_block_group(bg);
return 0;
@@ -4037,7 +4037,7 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,
int ret = 0;
bool skip = false;
- ASSERT(btrfs_is_zoned(block_group->fs_info));
+ ASSERT(btrfs_zoned_bg_is_append_only(block_group));
/*
* Do not allow non-tree-log blocks in the dedicated tree-log block
@@ -4191,11 +4191,28 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,
return ret;
}
+/*
+ * On a zoned filesystem ffe_ctl->policy is BTRFS_EXTENT_ALLOC_ZONED, but block
+ * groups that are not append only are allocated from with the regular free
+ * space cache. As prepare_allocation_zoned() does not set up a cluster, this
+ * always ends up in find_free_extent_unclustered().
+ */
+static enum btrfs_extent_allocation_policy
+bg_alloc_policy(const struct find_free_extent_ctl *ffe_ctl,
+ const struct btrfs_block_group *block_group)
+{
+ if (ffe_ctl->policy == BTRFS_EXTENT_ALLOC_ZONED &&
+ !btrfs_zoned_bg_is_append_only(block_group))
+ return BTRFS_EXTENT_ALLOC_CLUSTERED;
+
+ return ffe_ctl->policy;
+}
+
static int do_allocation(struct btrfs_block_group *block_group,
struct find_free_extent_ctl *ffe_ctl,
struct btrfs_block_group **bg_ret)
{
- switch (ffe_ctl->policy) {
+ switch (bg_alloc_policy(ffe_ctl, block_group)) {
case BTRFS_EXTENT_ALLOC_CLUSTERED:
return do_allocation_clustered(block_group, ffe_ctl, bg_ret);
case BTRFS_EXTENT_ALLOC_ZONED:
@@ -4209,7 +4226,7 @@ static void release_block_group(struct btrfs_block_group *block_group,
struct find_free_extent_ctl *ffe_ctl,
bool delalloc)
{
- switch (ffe_ctl->policy) {
+ switch (bg_alloc_policy(ffe_ctl, block_group)) {
case BTRFS_EXTENT_ALLOC_CLUSTERED:
ffe_ctl->retry_uncached = false;
break;
@@ -4349,8 +4366,13 @@ static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info,
return 1;
/* See the comments for btrfs_loop_type for an explanation of the phases. */
- if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE)
- return -ENOSPC;
+ if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) {
+ if (ffe_ctl->empty_size == 0 && ffe_ctl->empty_cluster == 0)
+ return -ENOSPC;
+
+ ffe_ctl->empty_size = 0;
+ ffe_ctl->empty_cluster = 0;
+ }
ffe_ctl->index = 0;
/*
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index c90ba5f6a8bb..095784f0f6fe 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -26,6 +26,7 @@
#include "accessors.h"
#include "file-item.h"
#include "file.h"
+#include "zoned.h"
#include "super.h"
#include "relocation.h"
@@ -1370,7 +1371,7 @@ static int __btrfs_add_free_space(struct btrfs_block_group *block_group,
int ret = 0;
u64 filter_bytes = bytes;
- ASSERT(!btrfs_is_zoned(fs_info));
+ ASSERT(!btrfs_zoned_bg_is_append_only(block_group));
info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
if (!info)
@@ -1499,7 +1500,7 @@ int btrfs_add_free_space(struct btrfs_block_group *block_group,
if (block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED)
return 0;
- if (btrfs_is_zoned(block_group->fs_info))
+ if (btrfs_zoned_bg_is_append_only(block_group))
return __btrfs_add_free_space_zoned(block_group, bytenr, size,
true);
@@ -1512,7 +1513,7 @@ int btrfs_add_free_space(struct btrfs_block_group *block_group,
int btrfs_add_free_space_unused(struct btrfs_block_group *block_group,
u64 bytenr, u64 size)
{
- if (btrfs_is_zoned(block_group->fs_info))
+ if (btrfs_zoned_bg_is_append_only(block_group))
return __btrfs_add_free_space_zoned(block_group, bytenr, size,
false);
@@ -1529,7 +1530,7 @@ int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group,
{
enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
- if (btrfs_is_zoned(block_group->fs_info))
+ if (btrfs_zoned_bg_is_append_only(block_group))
return __btrfs_add_free_space_zoned(block_group, bytenr, size,
true);
@@ -1548,7 +1549,7 @@ int btrfs_remove_free_space(struct btrfs_block_group *block_group,
int ret;
bool re_search = false;
- if (btrfs_is_zoned(block_group->fs_info)) {
+ if (btrfs_zoned_bg_is_append_only(block_group)) {
/*
* This can happen with conventional zones when replaying log.
* Since the allocation info of tree-log nodes are not recorded
@@ -1663,10 +1664,10 @@ void btrfs_dump_free_space(struct btrfs_block_group *block_group,
int count = 0;
/*
- * Zoned btrfs does not use free space tree and cluster. Just print
- * out the free space after the allocation offset.
+ * Append only block groups do not use free space tree and cluster.
+ * Just print out the free space after the allocation offset.
*/
- if (btrfs_is_zoned(fs_info)) {
+ if (btrfs_zoned_bg_is_append_only(block_group)) {
btrfs_info(fs_info, "free space %llu active %d",
block_group->zone_capacity - block_group->alloc_offset,
test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
@@ -1838,7 +1839,7 @@ u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
enum btrfs_trim_state align_gap_trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
bool use_bytes_index = (offset == block_group->start);
- ASSERT(!btrfs_is_zoned(block_group->fs_info));
+ ASSERT(!btrfs_zoned_bg_is_append_only(block_group));
spin_lock(&ctl->tree_lock);
entry = find_free_space(ctl, &offset, &bytes_search,
@@ -1971,7 +1972,7 @@ u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
struct rb_node *node;
u64 ret = 0;
- ASSERT(!btrfs_is_zoned(block_group->fs_info));
+ ASSERT(!btrfs_zoned_bg_is_append_only(block_group));
spin_lock(&cluster->lock);
if (bytes > cluster->max_size)
@@ -2789,7 +2790,7 @@ int btrfs_trim_block_group(struct btrfs_block_group *block_group,
int ret;
u64 rem = 0;
- ASSERT(!btrfs_is_zoned(block_group->fs_info));
+ ASSERT(!btrfs_zoned_bg_is_append_only(block_group));
*trimmed = 0;
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index bc83f4a2577b..9a617842574f 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -1969,6 +1969,12 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
}
if (num_conventional > 0) {
+ if (map->num_stripes == num_conventional &&
+ !btrfs_zoned_bg_is_append_only(cache)) {
+ cache->zone_capacity = cache->length;
+ goto out;
+ }
+
ret = calculate_alloc_pointer(cache, &last_alloc, new);
if (ret) {
btrfs_err(fs_info,
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 08/27] btrfs: zoned: only take the metadata IO lock for append only block groups
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
` (2 preceding siblings ...)
2026-11-17 18:58 ` [PATCH v7 07/27] btrfs: zoned: use the regular allocator for " Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 09/27] btrfs: zoned: allow restricting the dev extent search to a zone type Johannes Thumshirn
` (13 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
fs_info::zoned_meta_io_lock serializes the metadata writeback against
the zone write pointers and protects fs_info::active_{meta,system}_bg.
Metadata in a block group backed by conventional zones only is written
in place and in arbitrary order, so none of that applies to it.
Acquire the lock lazily instead of around the whole btree_writepages()
walk: take it on the first extent buffer that lives in an append only
block group and drop it when the walk is done. A filesystem whose
metadata is entirely on conventional zones never takes it at all.
To do so, split the block group lookup out of
btrfs_check_meta_write_pointer() into
btrfs_zoned_eb_write_is_append_only(), so btree_writepages() can decide
whether it needs the lock before writing an extent buffer.
btrfs_check_meta_write_pointer() now expects the lock to be held for
append only block groups.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/extent_io.c | 70 +++++++++++++++++++++++++++++---------------
fs/btrfs/extent_io.h | 2 ++
fs/btrfs/zoned.c | 40 +++++++++++++++++--------
fs/btrfs/zoned.h | 8 +++++
4 files changed, 85 insertions(+), 35 deletions(-)
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 6ba202b312b5..8b58482e62aa 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -2547,16 +2547,15 @@ static int write_meta_extent_buffer(struct btrfs_eb_write_context *ctx,
/*
* On a zoned filesystem, write out the currently dirty metadata extent buffers
* of @bg. Used to flush the active metadata/system block group before the
- * ascending-address walk in btree_writepages(), so that walk can pivot the
- * active block group away (finishing it) instead of aborting the commit; see
- * the caller for details.
+ * ascending-address walk in btree_writepages() writes its first append only
+ * extent buffer, so that walk can pivot the active block group away (finishing
+ * it) instead of aborting the commit; see lock_zoned_meta_io().
*/
-static void flush_active_meta_bg(struct address_space *mapping,
+static void flush_active_meta_bg(struct btrfs_fs_info *fs_info,
struct writeback_control *wbc,
struct btrfs_eb_write_context *ctx,
struct btrfs_block_group *bg)
{
- struct btrfs_fs_info *fs_info = inode_to_fs_info(mapping->host);
unsigned long index = bg->start >> fs_info->nodesize_bits;
unsigned long end = (btrfs_block_group_end(bg) - 1) >> fs_info->nodesize_bits;
struct eb_batch batch;
@@ -2591,6 +2590,43 @@ static void flush_active_meta_bg(struct address_space *mapping,
}
}
+static void flush_active_meta_bgs(struct btrfs_fs_info *fs_info,
+ struct writeback_control *wbc)
+{
+ struct btrfs_eb_write_context ctx = { .wbc = wbc };
+
+ if (fs_info->active_meta_bg)
+ flush_active_meta_bg(fs_info, wbc, &ctx, fs_info->active_meta_bg);
+ if (fs_info->active_system_bg)
+ flush_active_meta_bg(fs_info, wbc, &ctx, fs_info->active_system_bg);
+
+ if (ctx.zoned_bg)
+ btrfs_put_block_group(ctx.zoned_bg);
+}
+
+/*
+ * Take fs_info::zoned_meta_io_lock once the walk in btree_writepages() hits the
+ * first extent buffer of an append only block group, and keep it for the rest
+ * of the walk. Metadata in other block groups is written in place and in any
+ * order, so it does not need the lock.
+ *
+ * When writing back for a transaction commit, flush the currently active
+ * metadata/system block group(s) first, under this same lock, so the walk can
+ * pivot the active block group instead of aborting the transaction commit with
+ * -EAGAIN.
+ */
+static void lock_zoned_meta_io(struct btrfs_fs_info *fs_info,
+ struct btrfs_eb_write_context *ctx)
+{
+ struct writeback_control *wbc = ctx->wbc;
+
+ mutex_lock(&fs_info->zoned_meta_io_lock);
+ ctx->meta_io_locked = true;
+
+ if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
+ flush_active_meta_bgs(fs_info, wbc);
+}
+
int btree_writepages(struct address_space *mapping, struct writeback_control *wbc)
{
struct btrfs_eb_write_context ctx = { .wbc = wbc };
@@ -2625,23 +2661,6 @@ int btree_writepages(struct address_space *mapping, struct writeback_control *wb
tag = PAGECACHE_TAG_TOWRITE;
else
tag = PAGECACHE_TAG_DIRTY;
- btrfs_zoned_meta_io_lock(fs_info);
-
- /*
- * On a zoned filesystem, flush the currently active metadata/system
- * block group(s) first, under this same lock, so the ascending-address
- * walk below can pivot the active block group instead of aborting the
- * transaction commit with -EAGAIN.
- */
- if (btrfs_is_zoned(fs_info) && wbc->sync_mode == WB_SYNC_ALL &&
- !wbc->for_sync) {
- if (fs_info->active_meta_bg)
- flush_active_meta_bg(mapping, wbc, &ctx,
- fs_info->active_meta_bg);
- if (fs_info->active_system_bg)
- flush_active_meta_bg(mapping, wbc, &ctx,
- fs_info->active_system_bg);
- }
retry:
if (wbc->sync_mode == WB_SYNC_ALL)
buffer_tree_tag_for_writeback(fs_info, index, end);
@@ -2652,6 +2671,10 @@ int btree_writepages(struct address_space *mapping, struct writeback_control *wb
while ((eb = eb_batch_next(&batch)) != NULL) {
ctx.eb = eb;
+ if (!ctx.meta_io_locked &&
+ btrfs_zoned_eb_write_is_append_only(fs_info, &ctx))
+ lock_zoned_meta_io(fs_info, &ctx);
+
ret = write_meta_extent_buffer(&ctx, wbc);
if (ret == -EBUSY) {
ret = 0;
@@ -2684,7 +2707,8 @@ int btree_writepages(struct address_space *mapping, struct writeback_control *wb
if (ctx.zoned_bg)
btrfs_put_block_group(ctx.zoned_bg);
- btrfs_zoned_meta_io_unlock(fs_info);
+ if (ctx.meta_io_locked)
+ mutex_unlock(&fs_info->zoned_meta_io_lock);
return ret;
}
diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index c55b37cb24b7..d7e34930a43c 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -143,6 +143,8 @@ struct btrfs_eb_write_context {
struct extent_buffer *eb;
/* Block group @eb resides in. Only used for zoned mode. */
struct btrfs_block_group *zoned_bg;
+ /* Is fs_info::zoned_meta_io_lock held for this writeback? */
+ bool meta_io_locked;
};
static inline unsigned long offset_in_eb_folio(const struct extent_buffer *eb,
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 9a617842574f..b1a364030fe6 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2274,22 +2274,17 @@ static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
}
/*
- * Check if @ctx->eb is aligned to the write pointer.
- *
- * Return:
- * 0: @ctx->eb is at the write pointer. You can write it.
- * -EAGAIN: There is a hole. The caller should handle the case.
- * -EBUSY: There is a hole, but the caller can just bail out.
+ * Look up the block group of @ctx->eb, cache it in @ctx->zoned_bg and return
+ * whether @ctx->eb has to be written in write pointer order.
*/
-int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
- struct btrfs_eb_write_context *ctx)
+bool btrfs_zoned_eb_write_is_append_only(struct btrfs_fs_info *fs_info,
+ struct btrfs_eb_write_context *ctx)
{
- const struct writeback_control *wbc = ctx->wbc;
const struct extent_buffer *eb = ctx->eb;
struct btrfs_block_group *block_group = ctx->zoned_bg;
if (!btrfs_is_zoned(fs_info))
- return 0;
+ return false;
if (block_group) {
if (block_group->start > eb->start ||
@@ -2303,13 +2298,34 @@ int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
if (!block_group) {
block_group = btrfs_lookup_block_group(fs_info, eb->start);
if (!block_group)
- return 0;
+ return false;
ctx->zoned_bg = block_group;
}
- if (!btrfs_zoned_bg_is_append_only(block_group))
+ return btrfs_zoned_bg_is_append_only(block_group);
+}
+
+/*
+ * Check if @ctx->eb is aligned to the write pointer.
+ *
+ * Return:
+ * 0: @ctx->eb is at the write pointer. You can write it.
+ * -EAGAIN: There is a hole. The caller should handle the case.
+ * -EBUSY: There is a hole, but the caller can just bail out.
+ */
+int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
+ struct btrfs_eb_write_context *ctx)
+{
+ const struct writeback_control *wbc = ctx->wbc;
+ const struct extent_buffer *eb = ctx->eb;
+ struct btrfs_block_group *block_group;
+
+ if (!btrfs_zoned_eb_write_is_append_only(fs_info, ctx))
return 0;
+ block_group = ctx->zoned_bg;
+ lockdep_assert_held(&fs_info->zoned_meta_io_lock);
+
if (block_group->meta_write_pointer == eb->start) {
struct btrfs_block_group **tgt;
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 3c561f6ad228..5195d3d6f541 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -81,6 +81,8 @@ bool btrfs_use_zone_append(struct btrfs_bio *bbio);
void btrfs_record_physical_zoned(struct btrfs_bio *bbio);
int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
struct btrfs_eb_write_context *ctx);
+bool btrfs_zoned_eb_write_is_append_only(struct btrfs_fs_info *fs_info,
+ struct btrfs_eb_write_context *ctx);
bool btrfs_zoned_meta_is_append_only(struct btrfs_fs_info *fs_info, u64 logical);
int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length);
int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
@@ -221,6 +223,12 @@ static inline int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
return 0;
}
+static inline bool btrfs_zoned_eb_write_is_append_only(struct btrfs_fs_info *fs_info,
+ struct btrfs_eb_write_context *ctx)
+{
+ return false;
+}
+
static inline bool btrfs_zoned_meta_is_append_only(struct btrfs_fs_info *fs_info,
u64 logical)
{
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 09/27] btrfs: zoned: allow restricting the dev extent search to a zone type
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
` (3 preceding siblings ...)
2026-11-17 18:58 ` [PATCH v7 08/27] btrfs: zoned: only take the metadata IO lock for append only " Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 10/27] btrfs: zoned: prefer conventional zones for metadata chunks Johannes Thumshirn
` (12 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
Add a zone type filter argument to btrfs_find_allocatable_zones() and
plumb it through the device extent hole search. When set, only regions
consisting purely of conventional or purely of sequential zones are
considered.
A caller expresses a preference for one zone type by first searching
with the corresponding filter and retrying with BTRFS_ZONE_FILTER_NONE
if that does not yield enough space.
No functional change, all callers pass BTRFS_ZONE_FILTER_NONE for now.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/volumes.c | 24 ++++++++++++++++--------
fs/btrfs/zoned.c | 25 ++++++++++++++++++++++++-
fs/btrfs/zoned.h | 17 +++++++++++++++--
3 files changed, 55 insertions(+), 11 deletions(-)
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 08a579e142ae..4b6bafd144b9 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -1704,7 +1704,8 @@ static u64 dev_extent_search_start(struct btrfs_device *device)
static bool dev_extent_hole_check_zoned(struct btrfs_device *device,
u64 *hole_start, u64 *hole_size,
- u64 num_bytes)
+ u64 num_bytes,
+ enum btrfs_zone_filter filter)
{
u64 zone_size = device->zone_info->zone_size;
u64 pos;
@@ -1717,7 +1718,7 @@ static bool dev_extent_hole_check_zoned(struct btrfs_device *device,
while (*hole_size > 0) {
pos = btrfs_find_allocatable_zones(device, *hole_start,
*hole_start + *hole_size,
- num_bytes);
+ num_bytes, filter);
if (pos != *hole_start) {
*hole_size = *hole_start + *hole_size - pos;
*hole_start = pos;
@@ -1754,6 +1755,7 @@ static bool dev_extent_hole_check_zoned(struct btrfs_device *device,
* @hole_start: input/output pointer for the hole start position
* @hole_size: input/output pointer for the hole size
* @num_bytes: minimum allocation size required
+ * @filter: on a zoned filesystem, restrict to a given zone type
*
* Check if the specified hole is suitable for allocation and adjust it if
* necessary. The hole may be modified to skip over pending chunk allocations
@@ -1772,7 +1774,8 @@ static bool dev_extent_hole_check_zoned(struct btrfs_device *device,
* and *hole_size is set to 0.
*/
static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start,
- u64 *hole_size, u64 num_bytes)
+ u64 *hole_size, u64 num_bytes,
+ enum btrfs_zone_filter filter)
{
bool found = false;
const u64 hole_end = *hole_start + *hole_size - 1;
@@ -1793,7 +1796,8 @@ static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start,
case BTRFS_CHUNK_ALLOC_REGULAR:
return found;
case BTRFS_CHUNK_ALLOC_ZONED:
- if (dev_extent_hole_check_zoned(device, hole_start, hole_size, num_bytes))
+ if (dev_extent_hole_check_zoned(device, hole_start, hole_size,
+ num_bytes, filter))
goto again;
break;
}
@@ -1810,6 +1814,8 @@ static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start,
* @start: store the start of the free space.
* @len: the size of the free space. that we find, or the size
* of the max free space if we don't find suitable free space
+ * @filter: on a zoned filesystem, restrict the search to a given
+ * zone type
*
* This does a pretty simple search, the expectation is that it is called very
* infrequently and that a given device has a small number of extents.
@@ -1829,7 +1835,8 @@ static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start,
* is not reported as available.
*/
static int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
- u64 *start, u64 *len)
+ u64 *start, u64 *len,
+ enum btrfs_zone_filter filter)
{
struct btrfs_fs_info *fs_info = device->fs_info;
struct btrfs_root *root = fs_info->dev_root;
@@ -1905,7 +1912,7 @@ static int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
if (key.offset > search_start) {
hole_size = key.offset - search_start;
dev_extent_hole_check(device, &search_start, &hole_size,
- num_bytes);
+ num_bytes, filter);
if (hole_size > max_hole_size) {
max_hole_start = search_start;
@@ -1944,7 +1951,8 @@ static int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
*/
if (search_end > search_start) {
hole_size = search_end - search_start;
- dev_extent_hole_check(device, &search_start, &hole_size, num_bytes);
+ dev_extent_hole_check(device, &search_start, &hole_size, num_bytes,
+ filter);
if (hole_size > max_hole_size) {
max_hole_start = search_start;
@@ -5941,7 +5949,7 @@ static int gather_device_info(struct btrfs_fs_devices *fs_devices,
continue;
ret = find_free_dev_extent(device, dev_extent_want, &dev_offset,
- &max_avail);
+ &max_avail, BTRFS_ZONE_FILTER_NONE);
if (ret && ret != -ENOSPC)
return ret;
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index b1a364030fe6..8794c3c74c49 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -1050,6 +1050,22 @@ int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
return ret;
}
+static bool zone_filter_match(const struct btrfs_zoned_device_info *zinfo,
+ unsigned long begin, unsigned long nzones,
+ enum btrfs_zone_filter filter)
+{
+ switch (filter) {
+ case BTRFS_ZONE_FILTER_CONV:
+ return bitmap_test_range_all_zero(zinfo->seq_zones, begin, nzones);
+ case BTRFS_ZONE_FILTER_SEQ:
+ return bitmap_test_range_all_set(zinfo->seq_zones, begin, nzones);
+ case BTRFS_ZONE_FILTER_NONE:
+ return true;
+ }
+
+ return true;
+}
+
/*
* Find allocatable zones within a given region.
*
@@ -1057,12 +1073,14 @@ int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
* @hole_start: the position of the hole to allocate the region
* @num_bytes: size of wanted region
* @hole_end: the end of the hole
+ * @filter: restrict the search to a given zone type
* @return: position of allocatable zones
*
* Allocatable region should not contain any superblock locations.
*/
u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
- u64 hole_end, u64 num_bytes)
+ u64 hole_end, u64 num_bytes,
+ enum btrfs_zone_filter filter)
{
struct btrfs_zoned_device_info *zinfo = device->zone_info;
const u8 shift = zinfo->zone_size_shift;
@@ -1084,6 +1102,11 @@ u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
if (end > zinfo->nr_zones)
return hole_end;
+ if (!zone_filter_match(zinfo, begin, nzones, filter)) {
+ pos += zinfo->zone_size;
+ continue;
+ }
+
/* Check if zones in the region are all empty */
if (btrfs_dev_is_sequential(device, pos) &&
!bitmap_test_range_all_set(zinfo->empty_zones, begin, nzones)) {
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 5195d3d6f541..174528e2c312 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -28,6 +28,17 @@ struct btrfs_fs_devices;
#define BTRFS_DEFAULT_RECLAIM_THRESH (75)
+/*
+ * Zone type restriction for the device extent search. A caller expresses a
+ * preference by first searching with a restriction and retrying with
+ * BTRFS_ZONE_FILTER_NONE if that does not yield enough space.
+ */
+enum btrfs_zone_filter {
+ BTRFS_ZONE_FILTER_NONE,
+ BTRFS_ZONE_FILTER_CONV,
+ BTRFS_ZONE_FILTER_SEQ,
+};
+
struct btrfs_zoned_device_info {
/*
* Number of zones, zone size and types of zones if bdev is a
@@ -71,7 +82,8 @@ int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
int btrfs_advance_sb_log(struct btrfs_device *device, int mirror);
int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror);
u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
- u64 hole_end, u64 num_bytes);
+ u64 hole_end, u64 num_bytes,
+ enum btrfs_zone_filter filter);
int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
u64 length, u64 *bytes);
int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size);
@@ -182,7 +194,8 @@ static inline int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror
static inline u64 btrfs_find_allocatable_zones(struct btrfs_device *device,
u64 hole_start, u64 hole_end,
- u64 num_bytes)
+ u64 num_bytes,
+ enum btrfs_zone_filter filter)
{
return hole_start;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 10/27] btrfs: zoned: prefer conventional zones for metadata chunks
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
` (4 preceding siblings ...)
2026-11-17 18:58 ` [PATCH v7 09/27] btrfs: zoned: allow restricting the dev extent search to a zone type Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 11/27] btrfs: zoned: use plain mutex lock in check_bg_is_active Johannes Thumshirn
` (11 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
On a zoned filesystem metadata and system chunks are allocated from
both conventional and sequential zones. Metadata landing on a
sequential zone has to be written append only, in strict order and with
a queue depth of one, and freed tree blocks cannot be reused until the
whole block group is reclaimed.
Try to place metadata and system chunks on conventional zones instead.
The device extent search is first run with the conventional-only
restriction. If that does not yield enough devices for the requested
allocation profile, the search is repeated over all zones, keeping the
previous behaviour as the fallback.
Skip the restricted pass if none of the allocatable devices has any
conventional zones, e.g. on ZNS SSDs, so the device extent search is not
done twice for every metadata chunk.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/volumes.c | 41 +++++++++++++++++++++++++++++++++++++++--
fs/btrfs/zoned.h | 20 ++++++++++++++++++++
2 files changed, 59 insertions(+), 2 deletions(-)
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 4b6bafd144b9..4c37fd93604e 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -5910,6 +5910,29 @@ static void init_alloc_chunk_ctl(struct btrfs_fs_devices *fs_devices,
}
}
+static enum btrfs_zone_filter chunk_zone_filter(const struct btrfs_fs_devices *fs_devices,
+ const struct alloc_chunk_ctl *ctl)
+{
+ enum btrfs_zone_filter filter;
+ struct btrfs_device *device;
+
+ if (fs_devices->chunk_alloc_policy != BTRFS_CHUNK_ALLOC_ZONED)
+ return BTRFS_ZONE_FILTER_NONE;
+
+ if (ctl->type & BTRFS_BLOCK_GROUP_DATA)
+ return BTRFS_ZONE_FILTER_NONE;
+
+ filter = BTRFS_ZONE_FILTER_CONV;
+
+ /* Avoid a pointless first pass if no device has such zones at all. */
+ list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
+ if (btrfs_dev_has_zone_type(device, filter))
+ return filter;
+ }
+
+ return BTRFS_ZONE_FILTER_NONE;
+}
+
static int gather_device_info(struct btrfs_fs_devices *fs_devices,
struct alloc_chunk_ctl *ctl,
struct btrfs_device_info *devices_info)
@@ -5918,11 +5941,14 @@ static int gather_device_info(struct btrfs_fs_devices *fs_devices,
struct btrfs_device *device;
u64 total_avail;
u64 dev_extent_want = ctl->dev_extent_max * ctl->dev_stripes;
+ enum btrfs_zone_filter filter = chunk_zone_filter(fs_devices, ctl);
int ret;
- int ndevs = 0;
+ int ndevs;
u64 max_avail;
u64 dev_offset;
+again:
+ ndevs = 0;
/*
* in the first pass through the devices list, we gather information
* about the available holes on each device.
@@ -5949,7 +5975,7 @@ static int gather_device_info(struct btrfs_fs_devices *fs_devices,
continue;
ret = find_free_dev_extent(device, dev_extent_want, &dev_offset,
- &max_avail, BTRFS_ZONE_FILTER_NONE);
+ &max_avail, filter);
if (ret && ret != -ENOSPC)
return ret;
@@ -5976,6 +6002,17 @@ static int gather_device_info(struct btrfs_fs_devices *fs_devices,
devices_info[ndevs].dev = device;
++ndevs;
}
+
+ /*
+ * Not enough free zones of the preferred type to satisfy the allocation
+ * profile, fall back to searching all zones.
+ */
+ if (filter != BTRFS_ZONE_FILTER_NONE &&
+ rounddown(ndevs, ctl->devs_increment) < ctl->devs_min) {
+ filter = BTRFS_ZONE_FILTER_NONE;
+ goto again;
+ }
+
ctl->ndevs = ndevs;
/*
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 174528e2c312..ecce74f67a5d 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -339,6 +339,26 @@ static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
return test_bit(pos >> zone_info->zone_size_shift, zone_info->seq_zones);
}
+static inline bool btrfs_dev_has_zone_type(const struct btrfs_device *device,
+ enum btrfs_zone_filter filter)
+{
+ const struct btrfs_zoned_device_info *zone_info = device->zone_info;
+
+ if (!zone_info)
+ return false;
+
+ switch (filter) {
+ case BTRFS_ZONE_FILTER_CONV:
+ return !bitmap_full(zone_info->seq_zones, zone_info->nr_zones);
+ case BTRFS_ZONE_FILTER_SEQ:
+ return !bitmap_empty(zone_info->seq_zones, zone_info->nr_zones);
+ case BTRFS_ZONE_FILTER_NONE:
+ return true;
+ }
+
+ return true;
+}
+
static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos)
{
struct btrfs_zoned_device_info *zone_info = device->zone_info;
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 11/27] btrfs: zoned: use plain mutex lock in check_bg_is_active
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
` (5 preceding siblings ...)
2026-11-17 18:58 ` [PATCH v7 10/27] btrfs: zoned: prefer conventional zones for metadata chunks Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 20/27] btrfs: zoned: prepare reserving the data relocation block group at runtime Johannes Thumshirn
` (10 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
check_bg_is_active() is only called for zoned filesystems, so there is
no need in using the wrapper for taking fs_info::zoned_meta_io_lock.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/zoned.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 8794c3c74c49..c4dcd8d1bdd2 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2250,9 +2250,9 @@ static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
if (!btrfs_zone_activate(block_group)) {
int ret_fin;
- btrfs_zoned_meta_io_unlock(fs_info);
+ mutex_unlock(&fs_info->zoned_meta_io_lock);
ret_fin = btrfs_zone_finish_one_bg(fs_info);
- btrfs_zoned_meta_io_lock(fs_info);
+ mutex_lock(&fs_info->zoned_meta_io_lock);
if (ret_fin != 1 || !btrfs_zone_activate(block_group))
return false;
@@ -2275,10 +2275,10 @@ static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
}
/* Pivot active metadata/system block group. */
- btrfs_zoned_meta_io_unlock(fs_info);
+ mutex_unlock(&fs_info->zoned_meta_io_lock);
wait_eb_writebacks(tgt);
do_zone_finish(tgt, true);
- btrfs_zoned_meta_io_lock(fs_info);
+ mutex_lock(&fs_info->zoned_meta_io_lock);
if (*active_bg == tgt) {
btrfs_put_block_group(tgt);
*active_bg = NULL;
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 20/27] btrfs: zoned: prepare reserving the data relocation block group at runtime
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
` (6 preceding siblings ...)
2026-11-17 18:58 ` [PATCH v7 11/27] btrfs: zoned: use plain mutex lock in check_bg_is_active Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 21/27] btrfs: zoned: promote reset data block groups to the relocation target Johannes Thumshirn
` (9 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
btrfs_zoned_reserve_data_reloc_bg() is only called at mount time, so it
walks the data space_info's block group list without locking and does not
expect the candidate block group to change underneath it.
Prepare it for being called at runtime, concurrently with allocations:
walk the block group list under groups_sem, pin the candidate with a
reference and recheck that it is still empty under the write lock before
moving it to the data relocation sub space_info.
Factor out setting the data relocation block group into
btrfs_set_data_reloc_bg() and transfer the complete accounting of the
block group, including its zone_unusable bytes, to the data relocation
sub space_info.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/block-group.c | 11 +++
fs/btrfs/block-group.h | 1 +
fs/btrfs/extent-tree.c | 12 ++-
fs/btrfs/zoned.c | 201 ++++++++++++++++++++++++++++++++---------
4 files changed, 180 insertions(+), 45 deletions(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 6697eaf995cf..88ed3609fe50 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -3245,6 +3245,17 @@ int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
return ret;
}
+/*
+ * Unlike btrfs_inc_block_group_ro() this neither joins a transaction nor
+ * allocates a chunk, so it can be used from space flushing context. Currently
+ * only used in zoned mode.
+ */
+int btrfs_inc_block_group_ro_notrans(struct btrfs_block_group *cache)
+{
+ ASSERT(btrfs_is_zoned(cache->fs_info));
+ return inc_block_group_ro(cache, false);
+}
+
void btrfs_dec_block_group_ro(struct btrfs_block_group *cache)
{
struct btrfs_space_info *sinfo = cache->space_info;
diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h
index d567ed822e55..baf8e41ce8d3 100644
--- a/fs/btrfs/block-group.h
+++ b/fs/btrfs/block-group.h
@@ -353,6 +353,7 @@ void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans);
int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
bool do_chunk_alloc);
void btrfs_dec_block_group_ro(struct btrfs_block_group *cache);
+int btrfs_inc_block_group_ro_notrans(struct btrfs_block_group *cache);
int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans);
int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans);
int btrfs_update_block_group(struct btrfs_trans_handle *trans,
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index b01b3ccc944a..7ae03168ed91 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -4097,9 +4097,12 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,
ASSERT(!ffe_ctl->for_treelog ||
block_group->start == fs_info->treelog_bg ||
fs_info->treelog_bg == 0);
- ASSERT(!ffe_ctl->for_data_reloc ||
- block_group->start == fs_info->data_reloc_bg ||
- fs_info->data_reloc_bg == 0);
+
+ if (ffe_ctl->for_data_reloc && fs_info->data_reloc_bg &&
+ fs_info->data_reloc_bg != block_group->start) {
+ ret = 1;
+ goto out;
+ }
if (block_group->ro ||
(!ffe_ctl->for_data_reloc &&
@@ -4183,7 +4186,8 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,
out:
if (ret && ffe_ctl->for_treelog)
fs_info->treelog_bg = 0;
- if (ret && ffe_ctl->for_data_reloc)
+ if (ret && ffe_ctl->for_data_reloc &&
+ fs_info->data_reloc_bg == block_group->start)
fs_info->data_reloc_bg = 0;
spin_unlock(&fs_info->relocation_bg_lock);
spin_unlock(&fs_info->treelog_bg_lock);
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 8532ef57db48..5840c97aa9a3 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2972,6 +2972,131 @@ void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
spin_unlock(&fs_info->relocation_bg_lock);
}
+/*
+ * Move the empty block group @bg from @from to @to, which are the data
+ * space_info and its relocation sub-group.
+ *
+ * The caller holds the only read-only reference on @bg, so nothing can be
+ * allocated from it and its free space is accounted as readonly, not as
+ * available to reservations. As it has no used, pinned or reserved bytes,
+ * no accounting path can update its counters while it changes owner.
+ */
+static bool zoned_move_bg_to_space_info(struct btrfs_fs_info *fs_info,
+ struct btrfs_block_group *bg,
+ struct btrfs_space_info *from,
+ struct btrfs_space_info *to)
+{
+ struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
+ struct btrfs_space_info *reloc_sinfo = data_sinfo->sub_group[0];
+ const int factor = btrfs_bg_type_to_factor(bg->flags);
+ const int index = btrfs_bg_flags_to_raid_index(bg->flags);
+ bool moved = false;
+ u64 readonly;
+
+ ASSERT((from == data_sinfo && to == reloc_sinfo) ||
+ (from == reloc_sinfo && to == data_sinfo));
+
+ down_write(&data_sinfo->groups_sem);
+ down_write_nested(&reloc_sinfo->groups_sem, SINGLE_DEPTH_NESTING);
+ spin_lock(&data_sinfo->lock);
+ spin_lock_nested(&reloc_sinfo->lock, SINGLE_DEPTH_NESTING);
+ spin_lock(&bg->lock);
+
+ if (bg->space_info != from || bg->ro != 1 || btrfs_is_block_group_used(bg))
+ goto out;
+ if (to == reloc_sinfo && bg->alloc_offset)
+ goto out;
+
+ readonly = btrfs_block_group_available_space(bg);
+
+ from->total_bytes -= bg->length;
+ from->disk_total -= bg->length * factor;
+ from->bytes_readonly -= readonly;
+ btrfs_space_info_update_bytes_zone_unusable(from, -(s64)bg->zone_unusable);
+ from->max_extent_size = 0;
+
+ to->total_bytes += bg->length;
+ to->disk_total += bg->length * factor;
+ to->bytes_readonly += readonly;
+ btrfs_space_info_update_bytes_zone_unusable(to, bg->zone_unusable);
+ to->full = false;
+
+ bg->space_info = to;
+ list_move_tail(&bg->list, &to->block_groups[index]);
+ list_move_tail(&bg->ro_list, &to->ro_bgs);
+ moved = true;
+out:
+ spin_unlock(&bg->lock);
+ spin_unlock(&reloc_sinfo->lock);
+ spin_unlock(&data_sinfo->lock);
+ up_write(&reloc_sinfo->groups_sem);
+ up_write(&data_sinfo->groups_sem);
+
+ if (moved && !to->block_group_kobjs[index])
+ btrfs_sysfs_add_block_group_type(bg);
+
+ return moved;
+}
+
+static void zoned_dec_bg_ro(struct btrfs_block_group *bg)
+{
+ struct btrfs_space_info *space_info = bg->space_info;
+
+ btrfs_dec_block_group_ro(bg);
+ spin_lock(&space_info->lock);
+ btrfs_try_granting_tickets(space_info);
+ spin_unlock(&space_info->lock);
+}
+
+static bool btrfs_set_data_reloc_bg(struct btrfs_fs_info *fs_info,
+ struct btrfs_block_group *bg)
+{
+ struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
+ struct btrfs_space_info *reloc_sinfo = data_sinfo->sub_group[0];
+ const bool migrate = (bg->space_info == data_sinfo);
+
+ ASSERT(reloc_sinfo->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC,
+ "subgroup_id=%d", reloc_sinfo->subgroup_id);
+
+ if (migrate) {
+ /*
+ * Fails if the free space of @bg already backs reservations
+ * in the data space_info.
+ */
+ if (btrfs_inc_block_group_ro_notrans(bg))
+ return false;
+
+ if (!zoned_move_bg_to_space_info(fs_info, bg, data_sinfo, reloc_sinfo)) {
+ zoned_dec_bg_ro(bg);
+ return false;
+ }
+ ASSERT(bg->bytes_super == 0, "bg->bytes_super=%llu", bg->bytes_super);
+ }
+
+ spin_lock(&fs_info->relocation_bg_lock);
+ if (fs_info->data_reloc_bg) {
+ spin_unlock(&fs_info->relocation_bg_lock);
+
+ if (migrate) {
+ zoned_move_bg_to_space_info(fs_info, bg, reloc_sinfo,
+ data_sinfo);
+ zoned_dec_bg_ro(bg);
+ }
+
+ return false;
+ }
+ fs_info->data_reloc_bg = bg->start;
+ spin_unlock(&fs_info->relocation_bg_lock);
+ set_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &bg->runtime_flags);
+
+ if (migrate)
+ zoned_dec_bg_ro(bg);
+
+ btrfs_zone_activate(bg);
+
+ return true;
+}
+
static int zoned_reset_bg_in_place(struct btrfs_block_group *bg, u64 *reclaimed)
{
struct btrfs_fs_info *fs_info = bg->fs_info;
@@ -3004,7 +3129,7 @@ static int zoned_reset_bg_in_place(struct btrfs_block_group *bg, u64 *reclaimed)
spin_lock(&space_info->lock);
spin_lock(&bg->lock);
ASSERT(!btrfs_is_block_group_used(bg));
- if (bg->ro) {
+ if (bg->ro || bg->space_info != space_info) {
spin_unlock(&bg->lock);
spin_unlock(&space_info->lock);
return 0;
@@ -3043,7 +3168,7 @@ void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)
struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
struct btrfs_space_info *space_info = data_sinfo;
struct btrfs_trans_handle *trans;
- struct btrfs_block_group *bg;
+ struct btrfs_block_group *bg, *tmp;
struct list_head *bg_list;
u64 alloc_flags;
bool did_chunk_alloc = false;
@@ -3053,9 +3178,6 @@ void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)
if (!btrfs_is_zoned(fs_info))
return;
- if (fs_info->data_reloc_bg)
- return;
-
if (sb_rdonly(fs_info->sb))
return;
@@ -3063,46 +3185,43 @@ void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)
index = btrfs_bg_flags_to_raid_index(alloc_flags);
again:
- bg_list = &space_info->block_groups[index];
- list_for_each_entry(bg, bg_list, list) {
+ /*
+ * It's OK to check without taking fs_info::relocation_bg_lock here, it
+ * is only to bail out if we have a relocation block-group.
+ */
+ if (data_race(fs_info->data_reloc_bg))
+ return;
- if (bg->alloc_offset != 0)
+ /*
+ * Find a completely empty block group. Unlike at mount time, this can
+ * now run concurrently with allocations, so iterate under groups_sem
+ * and pin a candidate with a reference before dropping the lock and
+ * operating on it.
+ */
+ bg = NULL;
+ bg_list = &space_info->block_groups[index];
+ down_read(&space_info->groups_sem);
+ list_for_each_entry(tmp, bg_list, list) {
+ bool usable;
+
+ spin_lock(&tmp->lock);
+ usable = (tmp->alloc_offset == 0 && tmp->used == 0 &&
+ tmp->reserved == 0 && !tmp->ro);
+ spin_unlock(&tmp->lock);
+ if (!usable)
continue;
+ bg = tmp;
+ btrfs_get_block_group(bg);
+ break;
+ }
+ up_read(&space_info->groups_sem);
- if (space_info == data_sinfo) {
- /* Migrate the block group to the data relocation space_info. */
- struct btrfs_space_info *reloc_sinfo = data_sinfo->sub_group[0];
- int factor;
-
- ASSERT(reloc_sinfo->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC,
- "reloc_sinfo->subgroup_id=%d", reloc_sinfo->subgroup_id);
- factor = btrfs_bg_type_to_factor(bg->flags);
-
- down_write(&space_info->groups_sem);
- list_del_init(&bg->list);
- up_write(&space_info->groups_sem);
-
- spin_lock(&space_info->lock);
- space_info->total_bytes -= bg->length;
- space_info->disk_total -= bg->length * factor;
- space_info->disk_total -= bg->zone_unusable;
- /* There is no allocation ever happened. */
- ASSERT(bg->used == 0, "bg->used=%llu", bg->used);
- /* No super block in a block group on the zoned setup. */
- ASSERT(bg->bytes_super == 0, "bg->bytes_super=%llu", bg->bytes_super);
- spin_unlock(&space_info->lock);
-
- bg->space_info = reloc_sinfo;
- if (reloc_sinfo->block_group_kobjs[index] == NULL)
- btrfs_sysfs_add_block_group_type(bg);
-
- btrfs_add_bg_to_space_info(bg);
- }
-
- fs_info->data_reloc_bg = bg->start;
- set_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &bg->runtime_flags);
- btrfs_zone_activate(bg);
+ if (bg) {
+ bool set = btrfs_set_data_reloc_bg(fs_info, bg);
+ btrfs_put_block_group(bg);
+ if (!set)
+ goto again;
return;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 21/27] btrfs: zoned: promote reset data block groups to the relocation target
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
` (7 preceding siblings ...)
2026-11-17 18:58 ` [PATCH v7 20/27] btrfs: zoned: prepare reserving the data relocation block group at runtime Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 22/27] btrfs: zoned: return finished relocation block groups to the data space_info Johannes Thumshirn
` (8 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
When btrfs_reset_unused_block_groups() resets a data block group in place
and there is currently no data relocation block group, use the reset
block group as the new data relocation block group, instead of returning
its space to the data space_info.
Returning the space first would let a waiting data write take it, only
to have the block group moved into the data relocation sub space_info
afterwards.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/zoned.c | 27 ++++++++++++++++++++++++---
1 file changed, 24 insertions(+), 3 deletions(-)
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 5840c97aa9a3..dd05687f398b 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -3097,7 +3097,8 @@ static bool btrfs_set_data_reloc_bg(struct btrfs_fs_info *fs_info,
return true;
}
-static int zoned_reset_bg_in_place(struct btrfs_block_group *bg, u64 *reclaimed)
+static int zoned_reset_bg_in_place(struct btrfs_block_group *bg, bool promote,
+ u64 *reclaimed)
{
struct btrfs_fs_info *fs_info = bg->fs_info;
struct btrfs_space_info *space_info = bg->space_info;
@@ -3157,9 +3158,28 @@ static int zoned_reset_bg_in_place(struct btrfs_block_group *bg, u64 *reclaimed)
bg->free_space_ctl->free_space += *reclaimed;
btrfs_space_info_update_bytes_zone_unusable(space_info, -*reclaimed);
spin_unlock(&bg->lock);
- btrfs_return_free_space(space_info, *reclaimed);
+
+ /*
+ * If we need a data relocation target, promote this just
+ * evacuated and reset zone to be it directly, instead of
+ * returning its free space to the data space_info. Otherwise the
+ * returned space could be handed out to a waiting data write and
+ * then yanked away by the migration into the relocation
+ * space_info.
+ */
+ promote = promote && (space_info->flags & BTRFS_BLOCK_GROUP_DATA);
+ if (!promote)
+ btrfs_return_free_space(space_info, *reclaimed);
spin_unlock(&space_info->lock);
+ if (promote && !btrfs_set_data_reloc_bg(fs_info, bg)) {
+ struct btrfs_space_info *space_info = bg->space_info;
+
+ spin_lock(&space_info->lock);
+ btrfs_return_free_space(space_info, *reclaimed);
+ spin_unlock(&space_info->lock);
+ }
+
return 0;
}
@@ -3542,7 +3562,8 @@ int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num
list_del_init(&bg->bg_list);
spin_unlock(&fs_info->unused_bgs_lock);
- ret = zoned_reset_bg_in_place(bg, &reclaimed);
+ ret = zoned_reset_bg_in_place(bg, data_race(!fs_info->data_reloc_bg),
+ &reclaimed);
if (ret || !reclaimed)
goto requeue;
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 22/27] btrfs: zoned: return finished relocation block groups to the data space_info
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
` (8 preceding siblings ...)
2026-11-17 18:58 ` [PATCH v7 21/27] btrfs: zoned: promote reset data block groups to the relocation target Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 23/27] btrfs: zoned: reuse block groups emptied by reclaim as relocation target Johannes Thumshirn
` (7 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
On a zoned filesystem, the dedicated data relocation block-group lives
in the data relocation sub space_info. Once it is no longer the
relocation target, it stays there, so filled relocation block-groups
accumulate and their space is never available to regular data again.
After each block-group reclaim run, move the relocation block-groups
that are no longer the target back to the primary data space-info, and
reserve a new data relocation block group if needed.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/block-group.c | 4 +++
fs/btrfs/zoned.c | 57 ++++++++++++++++++++++++++++++++++++++++++
fs/btrfs/zoned.h | 3 +++
3 files changed, 64 insertions(+)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 88ed3609fe50..562ec70ff0a5 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -2164,6 +2164,10 @@ void btrfs_reclaim_block_groups(struct btrfs_fs_info *fs_info, unsigned int limi
spin_lock(&fs_info->unused_bgs_lock);
list_splice_tail(&retry_list, &fs_info->reclaim_bgs);
spin_unlock(&fs_info->unused_bgs_lock);
+
+ btrfs_zoned_return_data_reloc_bgs(fs_info);
+ btrfs_zoned_reserve_data_reloc_bg(fs_info);
+
btrfs_exclop_finish(fs_info);
}
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index dd05687f398b..027aa78cbb3a 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -3006,6 +3006,11 @@ static bool zoned_move_bg_to_space_info(struct btrfs_fs_info *fs_info,
goto out;
if (to == reloc_sinfo && bg->alloc_offset)
goto out;
+ if (to == data_sinfo) {
+ if (bg->start == READ_ONCE(fs_info->data_reloc_bg))
+ goto out;
+ clear_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &bg->runtime_flags);
+ }
readonly = btrfs_block_group_available_space(bg);
@@ -3268,6 +3273,58 @@ void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)
}
}
+void btrfs_zoned_return_data_reloc_bgs(struct btrfs_fs_info *fs_info)
+{
+ struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
+ struct btrfs_space_info *reloc_sinfo;
+
+ if (!btrfs_is_zoned(fs_info) || !data_sinfo)
+ return;
+
+ reloc_sinfo = data_sinfo->sub_group[0];
+ if (!reloc_sinfo || reloc_sinfo->subgroup_id != BTRFS_SUB_GROUP_DATA_RELOC)
+ return;
+
+again:
+ for (int raid = 0; raid < BTRFS_NR_RAID_TYPES; raid++) {
+ struct btrfs_block_group *bg;
+ struct btrfs_block_group *found = NULL;
+ bool moved;
+
+ down_read(&reloc_sinfo->groups_sem);
+ list_for_each_entry(bg, &reloc_sinfo->block_groups[raid], list) {
+ spin_lock(&bg->lock);
+ /*
+ * Only return written but now empty block groups that are
+ * no longer the active relocation destination.
+ */
+ if (!bg->ro && bg->alloc_offset && !btrfs_is_block_group_used(bg) &&
+ bg->start != READ_ONCE(fs_info->data_reloc_bg)) {
+ found = bg;
+ btrfs_get_block_group(bg);
+ spin_unlock(&bg->lock);
+ break;
+ }
+ spin_unlock(&bg->lock);
+ }
+ up_read(&reloc_sinfo->groups_sem);
+
+ if (!found)
+ continue;
+
+ if (btrfs_inc_block_group_ro_notrans(found)) {
+ btrfs_put_block_group(found);
+ return;
+ }
+ moved = zoned_move_bg_to_space_info(fs_info, found, reloc_sinfo, data_sinfo);
+ zoned_dec_bg_ro(found);
+ btrfs_put_block_group(found);
+ if (!moved)
+ return;
+ goto again;
+ }
+}
+
void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
{
struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 02298f408102..1f5f62f7590b 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -110,6 +110,7 @@ void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
struct extent_buffer *eb);
void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg);
void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info);
+void btrfs_zoned_return_data_reloc_bgs(struct btrfs_fs_info *fs_info);
void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info);
bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info *fs_info);
void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
@@ -298,6 +299,8 @@ static inline void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) { }
static inline void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info) { }
+static inline void btrfs_zoned_return_data_reloc_bgs(struct btrfs_fs_info *fs_info) { }
+
static inline void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info) { }
static inline bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info *fs_info)
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 23/27] btrfs: zoned: reuse block groups emptied by reclaim as relocation target
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
` (9 preceding siblings ...)
2026-11-17 18:58 ` [PATCH v7 22/27] btrfs: zoned: return finished relocation block groups to the data space_info Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 24/27] btrfs: add btrfs_bg_type_to_num_stripes Johannes Thumshirn
` (6 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
When the block group reclaim relocates a data block group, the emptied
block group is removed and its zones are reset. The next relocation then
needs a new data relocation block group, and if none can be allocated the
relocation run has to be aborted.
After a successful relocation there is a free data block group anyway.
Instead of removing it, reset its zones in place and use it as the new
data relocation block group, so the reclaim always has a zone to relocate
into.
Mark block groups that are relocated by the reclaim with
BLOCK_GROUP_FLAG_ZONED_RECLAIM, so only those are reused.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/block-group.c | 4 +++
fs/btrfs/block-group.h | 1 +
fs/btrfs/volumes.c | 22 ++++++++-----
fs/btrfs/zoned.c | 70 ++++++++++++++++++++++++++++++++++++++++++
fs/btrfs/zoned.h | 6 ++++
5 files changed, 96 insertions(+), 7 deletions(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 562ec70ff0a5..6801687ce029 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -2065,7 +2065,11 @@ static int btrfs_reclaim_block_group(struct btrfs_block_group *bg, int *reclaime
spin_unlock(&bg->lock);
trace_btrfs_reclaim_block_group(bg);
+ if (btrfs_is_zoned(fs_info))
+ set_bit(BLOCK_GROUP_FLAG_ZONED_RECLAIM, &bg->runtime_flags);
ret = btrfs_relocate_chunk(fs_info, bg->start, false);
+ if (btrfs_is_zoned(fs_info))
+ clear_bit(BLOCK_GROUP_FLAG_ZONED_RECLAIM, &bg->runtime_flags);
if (btrfs_is_zoned(fs_info) && ret == -EAGAIN) {
btrfs_dec_block_group_ro(bg);
btrfs_debug(fs_info, "deferring reclaim of chunk %llu", bg->start);
diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h
index baf8e41ce8d3..9c385c02c1f5 100644
--- a/fs/btrfs/block-group.h
+++ b/fs/btrfs/block-group.h
@@ -88,6 +88,7 @@ enum btrfs_block_group_flags {
BLOCK_GROUP_FLAG_NEW,
BLOCK_GROUP_FLAG_FULLY_REMAPPED,
BLOCK_GROUP_FLAG_STRIPE_REMOVAL_PENDING,
+ BLOCK_GROUP_FLAG_ZONED_RECLAIM,
};
enum btrfs_caching_type {
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index bf6dacdb2571..72768f29b8f4 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -3723,28 +3723,36 @@ static int btrfs_relocate_chunk_finish(struct btrfs_fs_info *fs_info,
{
struct btrfs_root *root = fs_info->chunk_root;
struct btrfs_trans_handle *trans;
- u64 length;
+ const u64 start = bg->start;
+ const u64 length = bg->length;
int ret;
btrfs_discard_cancel_work(&fs_info->discard_ctl, bg);
- length = bg->length;
btrfs_zone_finish_relocated(bg);
- btrfs_put_block_group(bg);
/*
* On a zoned file system, discard the whole block group, this will
* trigger a REQ_OP_ZONE_RESET operation on the device zone. If
* resetting the zone fails, don't treat it as a fatal problem from the
* filesystem's point of view.
+ *
+ * A block group emptied by GC is reset in place and becomes the next
+ * data relocation target instead of being removed, so GC always has a
+ * zone to relocate into.
*/
if (btrfs_is_zoned(fs_info)) {
- ret = btrfs_discard_extent(fs_info, bg->start, length, NULL, true);
+ if (btrfs_zoned_reuse_reclaimed_bg(bg)) {
+ btrfs_put_block_group(bg);
+ return 0;
+ }
+ ret = btrfs_discard_extent(fs_info, start, length, NULL, true);
if (ret)
btrfs_info(fs_info, "failed to reset zone %llu after relocation",
- bg->start);
+ start);
}
+ btrfs_put_block_group(bg);
- trans = btrfs_start_trans_remove_block_group(root->fs_info, bg->start);
+ trans = btrfs_start_trans_remove_block_group(root->fs_info, start);
if (IS_ERR(trans)) {
ret = PTR_ERR(trans);
btrfs_handle_fs_error(root->fs_info, ret, NULL);
@@ -3752,7 +3760,7 @@ static int btrfs_relocate_chunk_finish(struct btrfs_fs_info *fs_info,
}
/* Step two, delete the device extents and the chunk tree entries. */
- ret = btrfs_remove_chunk(trans, bg->start);
+ ret = btrfs_remove_chunk(trans, start);
btrfs_end_transaction(trans);
return ret;
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 027aa78cbb3a..67b440670125 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -3188,6 +3188,76 @@ static int zoned_reset_bg_in_place(struct btrfs_block_group *bg, bool promote,
return 0;
}
+static bool btrfs_zoned_can_reuse_bg(struct btrfs_block_group *bg)
+{
+ if (!btrfs_zoned_bg_is_append_only(bg))
+ return false;
+ /*
+ * Only the read-only references taken by btrfs_reclaim_block_group()
+ * and btrfs_relocate_block_group(), nobody else (scrub, balance).
+ */
+ if (bg->ro != 2)
+ return false;
+ if (btrfs_is_block_group_used(bg))
+ return false;
+ if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags))
+ return false;
+ /* Once read-write again, nothing must be allocatable before the reset. */
+ if (!btrfs_zoned_bg_is_full(bg))
+ return false;
+ return true;
+}
+
+bool btrfs_zoned_reuse_reclaimed_bg(struct btrfs_block_group *bg)
+{
+ struct btrfs_fs_info *fs_info = bg->fs_info;
+ struct btrfs_space_info *space_info = bg->space_info;
+ u64 reclaimed;
+ bool reuse;
+ int ret;
+
+ if (!test_bit(BLOCK_GROUP_FLAG_ZONED_RECLAIM, &bg->runtime_flags))
+ return false;
+
+ if (space_info != fs_info->data_sinfo)
+ return false;
+
+ if ((bg->flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) !=
+ (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_PROFILE_MASK))
+ return false;
+
+ spin_lock(&space_info->lock);
+ spin_lock(&bg->lock);
+ reuse = btrfs_zoned_can_reuse_bg(bg);
+ spin_unlock(&bg->lock);
+ spin_unlock(&space_info->lock);
+ if (!reuse)
+ return false;
+
+ spin_lock(&fs_info->unused_bgs_lock);
+ if (!list_empty(&bg->bg_list)) {
+ list_del_init(&bg->bg_list);
+ btrfs_put_block_group(bg);
+ }
+ spin_unlock(&fs_info->unused_bgs_lock);
+
+ /*
+ * Drop the read-only references taken by reclaim and relocation. The
+ * block-group is full and empty, so this leaves it entirely
+ * zone_unusable, just like the block groups that
+ * btrfs_reset_unused_block_groups() resets in place.
+ */
+ btrfs_dec_block_group_ro(bg);
+ btrfs_dec_block_group_ro(bg);
+
+ ret = zoned_reset_bg_in_place(bg, data_race(!fs_info->data_reloc_bg),
+ &reclaimed);
+ if (ret || !reclaimed)
+ btrfs_mark_bg_unused(bg);
+
+ return true;
+}
+
void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)
{
struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 1f5f62f7590b..d2011f764f87 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -111,6 +111,7 @@ void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg);
void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info);
void btrfs_zoned_return_data_reloc_bgs(struct btrfs_fs_info *fs_info);
+bool btrfs_zoned_reuse_reclaimed_bg(struct btrfs_block_group *bg);
void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info);
bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info *fs_info);
void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
@@ -301,6 +302,11 @@ static inline void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_in
static inline void btrfs_zoned_return_data_reloc_bgs(struct btrfs_fs_info *fs_info) { }
+static inline bool btrfs_zoned_reuse_reclaimed_bg(struct btrfs_block_group *bg)
+{
+ return false;
+}
+
static inline void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info) { }
static inline bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info *fs_info)
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 24/27] btrfs: add btrfs_bg_type_to_num_stripes
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
` (10 preceding siblings ...)
2026-11-17 18:58 ` [PATCH v7 23/27] btrfs: zoned: reuse block groups emptied by reclaim as relocation target Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 25/27] btrfs: zoned: keep a zone free for relocation when allocating data chunks Johannes Thumshirn
` (5 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
Add btrfs_bg_type_to_num_stripes() a helper to get the number of device
stripes a new chunk with a given allocation profile takes. Profiles
without a device limit span all writebale devices.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/volumes.c | 13 +++++++++++++
fs/btrfs/volumes.h | 1 +
2 files changed, 14 insertions(+)
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 72768f29b8f4..2090687966ca 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -195,6 +195,19 @@ const char *btrfs_bg_type_to_raid_name(u64 flags)
return btrfs_raid_array[index].raid_name;
}
+int btrfs_bg_type_to_num_stripes(struct btrfs_fs_info *fs_info, u64 flags)
+{
+ const int index = btrfs_bg_flags_to_raid_index(flags);
+ int ndevs;
+
+ if (btrfs_raid_array[index].devs_max)
+ ndevs = btrfs_raid_array[index].devs_max;
+ else
+ ndevs = fs_info->fs_devices->rw_devices;
+
+ return btrfs_raid_array[index].dev_stripes * ndevs;
+}
+
int btrfs_nr_parity_stripes(u64 type)
{
enum btrfs_raid_types index = btrfs_bg_flags_to_raid_index(type);
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 891bc2432870..d48d4d1d9aff 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -913,6 +913,7 @@ void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, struct btrfs_devic
enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags);
int btrfs_bg_type_to_factor(u64 flags);
const char *btrfs_bg_type_to_raid_name(u64 flags);
+int btrfs_bg_type_to_num_stripes(struct btrfs_fs_info *fs_info, u64 flags);
int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
bool btrfs_verify_dev_items(const struct btrfs_fs_info *fs_info);
void btrfs_update_per_profile_avail(struct btrfs_fs_info *fs_info);
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 25/27] btrfs: zoned: keep a zone free for relocation when allocating data chunks
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
` (11 preceding siblings ...)
2026-11-17 18:58 ` [PATCH v7 24/27] btrfs: add btrfs_bg_type_to_num_stripes Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 26/27] btrfs: zoned: don't count the relocation zone reserve as available in statfs Johannes Thumshirn
` (4 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
When allocating new data chunks, make sure there is one zone free as a
relocation target.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/block-group.c | 7 ++++++-
fs/btrfs/zoned.c | 32 ++++++++++++++++++++++++++++++++
fs/btrfs/zoned.h | 7 +++++++
3 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 6801687ce029..40b8f5499007 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -4132,7 +4132,12 @@ int btrfs_chunk_alloc(struct btrfs_trans_handle *trans,
force_metadata_allocation(fs_info);
}
- ret_bg = do_chunk_alloc(trans, space_info, flags);
+ if (!btrfs_is_zoned(fs_info) ||
+ btrfs_zoned_data_chunk_allowed(space_info, flags))
+ ret_bg = do_chunk_alloc(trans, space_info, flags);
+ else
+ ret_bg = ERR_PTR(-ENOSPC);
+
trans->allocating_chunk = false;
if (IS_ERR(ret_bg)) {
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 67b440670125..5ca1a37fe9ee 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -3772,3 +3772,35 @@ void btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct seq_file *seq)
}
spin_unlock(&fs_info->zone_active_bgs_lock);
}
+
+bool btrfs_zoned_data_chunk_allowed(struct btrfs_space_info *space_info, u64 flags)
+{
+ struct btrfs_fs_info *fs_info = space_info->fs_info;
+ const u64 chunk_bytes = fs_info->zone_size *
+ btrfs_bg_type_to_num_stripes(fs_info, flags);
+ struct btrfs_device *device;
+ u64 avail = 0;
+
+ ASSERT(btrfs_is_zoned(fs_info));
+
+ if (space_info != fs_info->data_sinfo)
+ return true;
+
+ lockdep_assert_held(&fs_info->chunk_mutex);
+
+ list_for_each_entry(device, &fs_info->fs_devices->alloc_list, dev_alloc_list) {
+ u64 total;
+ u64 used;
+
+ if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state) ||
+ test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
+ continue;
+
+ total = btrfs_device_get_total_bytes(device);
+ used = btrfs_device_get_bytes_used(device);
+ if (total > used)
+ avail += btrfs_zoned_dev_avail_bytes(device, total - used);
+ }
+
+ return avail >= 2 * chunk_bytes;
+}
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index d2011f764f87..ddbc93d0b8ad 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -121,6 +121,7 @@ int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_fin
void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info);
int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num_bytes);
void btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct seq_file *seq);
+bool btrfs_zoned_data_chunk_allowed(struct btrfs_space_info *space_info, u64 flags);
#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
struct zone_info;
@@ -342,6 +343,12 @@ static inline int btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct s
return 0;
}
+static inline bool btrfs_zoned_data_chunk_allowed(struct btrfs_space_info *space_info,
+ u64 flags)
+{
+ return true;
+}
+
#endif
static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 26/27] btrfs: zoned: don't count the relocation zone reserve as available in statfs
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
` (12 preceding siblings ...)
2026-11-17 18:58 ` [PATCH v7 25/27] btrfs: zoned: keep a zone free for relocation when allocating data chunks Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 27/27] btrfs: zoned: provide zoned specific reclaim sweep Johannes Thumshirn
` (3 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
Don't account the space reserved as a relocation target as user
available space in btrfs_statfs().
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/super.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 99cb01788315..10280863b3da 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -1734,6 +1734,7 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
u32 bits = fs_info->sectorsize_bits;
__be32 *fsid;
unsigned factor = 1;
+ u64 data_alloc_profile = btrfs_data_alloc_profile(fs_info);
struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
int ret;
u64 thresh = 0;
@@ -1793,6 +1794,11 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
if (ret)
return ret;
+
+ if (btrfs_is_zoned(fs_info))
+ total_free_data -= min(total_free_data, fs_info->zone_size *
+ btrfs_bg_type_to_num_stripes(fs_info, data_alloc_profile));
+
buf->f_bavail += div_u64(total_free_data, factor);
buf->f_bavail = buf->f_bavail >> bits;
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v7 27/27] btrfs: zoned: provide zoned specific reclaim sweep
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
` (13 preceding siblings ...)
2026-11-17 18:58 ` [PATCH v7 26/27] btrfs: zoned: don't count the relocation zone reserve as available in statfs Johannes Thumshirn
@ 2026-11-17 18:58 ` Johannes Thumshirn
[not found] ` <20261117185850.4128157-2-johannes.thumshirn@wdc.com>
` (2 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 18:58 UTC (permalink / raw)
To: linux-btrfs
Cc: Damien Le Moal, Naohiro Aota, David Sterba, Boris Burkov,
Johannes Thumshirn
The RECLAIM_ZONES flush state calls btrfs_reclaim_sweep() to find
victims for garbage collection. That sweep does nothing unless periodic
reclaim is enabled for the space_info, which it isn't by default. And
even when it is enabled, it only queues a block-group the second time a
sweep sees it below the reclaim threshold.
Apart from that, block-groups only get queued for reclaim when their
usage drops below the threshold while space is freed. Block-groups that
were already below it, or whose garbage comes from many small frees
spread over the filesystem, are never picked. With data tickets waiting
for space, RECLAIM_ZONES then relocates nothing and fails the tickets,
although relocation could recover zone_unusable space.
Replace the call in RECLAIM_ZONES with btrfs_zoned_reclaim_sweep(),
which:
- queues every full data block-group below the reclaim threshold,
- then adds the full block-groups with the most garbage, until the
queued garbage adds up to two zones. As soon as the data relocation
block-group fills up, relocation uses the zone kept free for it, and
the waiting writer needs another zone on top before a new data chunk
may be allocated,
- also considers the block-groups of the data relocation sub-group, as
relocated data turns into garbage there as well.
The periodic sweep keeps running from the cleaner, adding an additional
layer of garbage collection.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/space-info.c | 2 +-
fs/btrfs/zoned.c | 125 ++++++++++++++++++++++++++++++++++++++++++
fs/btrfs/zoned.h | 3 +
3 files changed, 129 insertions(+), 1 deletion(-)
diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c
index bb08294cbf9a..22b5ecd8b464 100644
--- a/fs/btrfs/space-info.c
+++ b/fs/btrfs/space-info.c
@@ -914,7 +914,7 @@ static void flush_space(struct btrfs_space_info *space_info, u64 num_bytes,
case RECLAIM_ZONES:
if (btrfs_is_zoned(fs_info)) {
flush_work(&fs_info->reclaim_bgs_work);
- btrfs_reclaim_sweep(fs_info);
+ btrfs_zoned_reclaim_sweep(fs_info);
btrfs_delete_unused_bgs(fs_info);
btrfs_reclaim_block_groups(fs_info,
BTRFS_ZONED_SYNC_RECLAIM_BATCH);
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 5ca1a37fe9ee..ef2cc51be7bc 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -6,6 +6,7 @@
#include <linux/sched/mm.h>
#include <linux/atomic.h>
#include <linux/vmalloc.h>
+#include <linux/sort.h>
#include "ctree.h"
#include "volumes.h"
#include "zoned.h"
@@ -3773,6 +3774,130 @@ void btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct seq_file *seq)
spin_unlock(&fs_info->zone_active_bgs_lock);
}
+static u64 zoned_bg_garbage(const struct btrfs_block_group *bg)
+{
+ const u64 slack = bg->length - bg->zone_capacity;
+
+ if (bg->zone_unusable <= slack)
+ return 0;
+
+ return bg->zone_unusable - slack;
+}
+
+static bool zoned_bg_reclaimable(const struct btrfs_block_group *bg)
+{
+ if (bg->ro)
+ return false;
+
+ if (!btrfs_zoned_bg_is_full(bg))
+ return false;
+
+ if (test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &bg->runtime_flags))
+ return false;
+
+ return zoned_bg_garbage(bg) > 0;
+}
+
+struct zoned_gc_candidate {
+ struct btrfs_block_group *bg;
+ u64 garbage;
+};
+
+static int zoned_gc_candidate_cmp(const void *a, const void *b)
+{
+ const struct zoned_gc_candidate *ca = a;
+ const struct zoned_gc_candidate *cb = b;
+
+ if (ca->garbage > cb->garbage)
+ return -1;
+ if (ca->garbage < cb->garbage)
+ return 1;
+ return 0;
+}
+
+static u64 zoned_sweep_space_info(struct btrfs_space_info *sinfo, int thresh_pct,
+ struct zoned_gc_candidate *cands, size_t *nr,
+ size_t max)
+{
+ struct btrfs_block_group *bg;
+ u64 garbage = 0;
+
+ down_read(&sinfo->groups_sem);
+ for (int i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
+ list_for_each_entry(bg, &sinfo->block_groups[i], list) {
+ u64 bg_garbage;
+ bool below;
+
+ spin_lock(&bg->lock);
+ if (!zoned_bg_reclaimable(bg)) {
+ spin_unlock(&bg->lock);
+ continue;
+ }
+ bg_garbage = zoned_bg_garbage(bg);
+ below = bg->used < mult_perc(bg->length, thresh_pct);
+ if (below)
+ btrfs_mark_bg_to_reclaim(bg);
+ spin_unlock(&bg->lock);
+
+ if (below) {
+ garbage += bg_garbage;
+ } else if (*nr < max) {
+ btrfs_get_block_group(bg);
+ cands[*nr].bg = bg;
+ cands[*nr].garbage = bg_garbage;
+ (*nr)++;
+ }
+ }
+ }
+ up_read(&sinfo->groups_sem);
+
+ return garbage;
+}
+
+void btrfs_zoned_reclaim_sweep(struct btrfs_fs_info *fs_info)
+{
+ struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
+ struct btrfs_space_info *sinfos[] = {
+ data_sinfo,
+ data_sinfo->sub_group[0],
+ };
+ struct zoned_gc_candidate *cands;
+ const u64 target = 2 * fs_info->zone_size;
+ u64 total_bytes = 0;
+ u64 garbage = 0;
+ size_t nr = 0;
+ size_t max;
+ int thresh_pct;
+
+ spin_lock(&data_sinfo->lock);
+ thresh_pct = btrfs_calc_reclaim_threshold(data_sinfo);
+ spin_unlock(&data_sinfo->lock);
+ if (!thresh_pct)
+ return;
+
+ for (int s = 0; s < ARRAY_SIZE(sinfos); s++)
+ total_bytes += READ_ONCE(sinfos[s]->total_bytes);
+
+ max = div64_u64(total_bytes, fs_info->zone_size);
+ cands = kvmalloc_array(max, sizeof(*cands), GFP_NOFS);
+ if (!cands)
+ max = 0;
+
+ for (int s = 0; s < ARRAY_SIZE(sinfos); s++)
+ garbage += zoned_sweep_space_info(sinfos[s], thresh_pct, cands, &nr, max);
+
+ sort(cands, nr, sizeof(*cands), zoned_gc_candidate_cmp, NULL);
+
+ for (size_t i = 0; i < nr; i++) {
+ if (garbage < target) {
+ btrfs_mark_bg_to_reclaim(cands[i].bg);
+ garbage += cands[i].garbage;
+ }
+ btrfs_put_block_group(cands[i].bg);
+ }
+ kvfree(cands);
+}
+
bool btrfs_zoned_data_chunk_allowed(struct btrfs_space_info *space_info, u64 flags)
{
struct btrfs_fs_info *fs_info = space_info->fs_info;
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index ddbc93d0b8ad..7837dcc91684 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -121,6 +121,7 @@ int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_fin
void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info);
int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num_bytes);
void btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct seq_file *seq);
+void btrfs_zoned_reclaim_sweep(struct btrfs_fs_info *fs_info);
bool btrfs_zoned_data_chunk_allowed(struct btrfs_space_info *space_info, u64 flags);
#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
@@ -343,6 +344,8 @@ static inline int btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct s
return 0;
}
+static inline void btrfs_zoned_reclaim_sweep(struct btrfs_fs_info *fs_info) { }
+
static inline bool btrfs_zoned_data_chunk_allowed(struct btrfs_space_info *space_info,
u64 flags)
{
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* Re: [PATCH v7 01/27] btrfs: zoned: don't wait for a zone finish after a filesystem error
[not found] ` <20261117185850.4128157-2-johannes.thumshirn@wdc.com>
@ 2026-11-17 21:07 ` Damien Le Moal
0 siblings, 0 replies; 19+ messages in thread
From: Damien Le Moal @ 2026-11-17 21:07 UTC (permalink / raw)
To: Johannes Thumshirn, linux-btrfs; +Cc: Naohiro Aota, David Sterba, Boris Burkov
On 11/17/26 10:58, Johannes Thumshirn wrote:
> When no zone can be activated, buffered writeback in cow_file_range() and
> direct IO in btrfs_new_extent_direct() wait uninterruptibly for
> BTRFS_FS_NEED_ZONE_FINISH to be cleared, which only happens once a zone
> is finished.
>
> After a transaction abort or an emergency shutdown no zone gets finished
> anymore, so the waiters sleep forever. With writeback holding locked
> folios, btrfs_cleanup_transaction() then blocks in
> invalidate_inode_pages2_range() as well. generic/388 hangs this way.
>
> Clear the bit and wake the waiters when the filesystem error is set,
> clear it again if it gets set after an error was recorded, and let the
> waiters bail out with the filesystem error.
>
> Fixes: 2ce543f47843 ("btrfs: zoned: wait until zone is finished when allocation didn't progress")
> Fixes: 776a838f1fa9 ("btrfs: zoned: wait for data BG to be finished on direct IO allocation")
> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
> ---
> fs/btrfs/direct-io.c | 4 +++-
> fs/btrfs/fs.h | 1 +
> fs/btrfs/inode.c | 3 +++
> fs/btrfs/messages.c | 1 +
> fs/btrfs/zoned.c | 10 +++++++++-
> 5 files changed, 17 insertions(+), 2 deletions(-)
>
> diff --git a/fs/btrfs/direct-io.c b/fs/btrfs/direct-io.c
> index 2cad9f048f4a..7bf048dfced6 100644
> --- a/fs/btrfs/direct-io.c
> +++ b/fs/btrfs/direct-io.c
> @@ -191,7 +191,9 @@ static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
> ASSERT(btrfs_is_zoned(fs_info));
> wait_on_bit_io(&inode->root->fs_info->flags, BTRFS_FS_NEED_ZONE_FINISH,
> TASK_UNINTERRUPTIBLE);
> - goto again;
> + ret = BTRFS_FS_ERROR(fs_info);
> + if (likely(!ret))
> + goto again;
> }
> if (ret)
> return ERR_PTR(ret);
> diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
> index 2dd167c32d61..84a6eb2ebf83 100644
> --- a/fs/btrfs/fs.h
> +++ b/fs/btrfs/fs.h
> @@ -1232,6 +1232,7 @@ static inline void btrfs_force_shutdown(struct btrfs_fs_info *fs_info)
> * So here we only mark the fs error without flipping it RO.
> */
> WRITE_ONCE(fs_info->fs_error, -EIO);
> + clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
> if (!test_and_set_bit(BTRFS_FS_STATE_EMERGENCY_SHUTDOWN, &fs_info->fs_state)) {
> btrfs_crit(fs_info, "emergency shutdown");
> fserror_report_shutdown(fs_info->sb, GFP_KERNEL);
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 4b29b82eb180..319488794320 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -1409,6 +1409,9 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
> wait_on_bit_io(&inode->root->fs_info->flags,
> BTRFS_FS_NEED_ZONE_FINISH,
> TASK_UNINTERRUPTIBLE);
> + ret = BTRFS_FS_ERROR(fs_info);
> + if (unlikely(ret))
> + goto out_unlock;
> continue;
> }
> if (done_offset) {
> diff --git a/fs/btrfs/messages.c b/fs/btrfs/messages.c
> index 73acb69c3849..2722751401b0 100644
> --- a/fs/btrfs/messages.c
> +++ b/fs/btrfs/messages.c
> @@ -163,6 +163,7 @@ void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function
> * send it down to the disk.
> */
> WRITE_ONCE(fs_info->fs_error, error);
> + clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
>
> /* Don't go through full error handling during mount. */
> if (!(sb->s_flags & SB_BORN))
> diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
> index b1be02eeba68..ca0aedb916bf 100644
> --- a/fs/btrfs/zoned.c
> +++ b/fs/btrfs/zoned.c
> @@ -2773,8 +2773,16 @@ bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
> spin_unlock(&fs_info->zone_active_bgs_lock);
> mutex_unlock(&fs_info->chunk_mutex);
>
> - if (!ret)
> + if (!ret) {
> set_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
> + /*
> + * No zone gets finished after a filesystem error, don't let
> + * anybody wait for it. Pairs with the clear in the error paths.
> + */
> + smp_mb__after_atomic();
Looks good to me, but inserting the comment block here between the set_bit() and
smp_mb__after_atomic() is odd looking. I would keep these together as they
should be.
The comment describes why the "if" is here, so let' move it right before it.
Also, the comment would be nicer as:
/*
* No zone gets finished after a filesystem error, so do not let
* anybody wait for it. This pairs with the clear in the error
* path.
*/
s/error, don't let/error, so do not let
> + if (unlikely(BTRFS_FS_ERROR(fs_info)))
> + clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
> + }
>
> return ret;
> }
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH v7 02/27] btrfs: zoned: reject dev-replace onto a target with different zone types
[not found] ` <20261117185850.4128157-3-johannes.thumshirn@wdc.com>
@ 2026-11-17 21:08 ` Damien Le Moal
0 siblings, 0 replies; 19+ messages in thread
From: Damien Le Moal @ 2026-11-17 21:08 UTC (permalink / raw)
To: Johannes Thumshirn, linux-btrfs; +Cc: Naohiro Aota, David Sterba, Boris Burkov
On 11/17/26 10:58, Johannes Thumshirn wrote:
> BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE is set when a block-group is loaded and
> never updated. Replacing a device whose chunks live on conventional zones
> with one that has sequential zones at the same offsets, or vice versa,
> leaves the flag stale. The block-group then keeps being written in place
> on sequential zones, or written with zone append on conventional ones.
>
> Reject the replace if the conventional/sequential layout of the target
> device doesn't match the source device. Missing source devices have no
> zone information and can't be checked.
>
> Fixes: 7db1c5d14dcd ("btrfs: zoned: support dev-replace in zoned filesystems")
> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Looks OK to me.
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH v7 03/27] btrfs: zoned: fix possible race for reserved_active_zones
[not found] ` <20261117185850.4128157-4-johannes.thumshirn@wdc.com>
@ 2026-11-17 21:09 ` Damien Le Moal
0 siblings, 0 replies; 19+ messages in thread
From: Damien Le Moal @ 2026-11-17 21:09 UTC (permalink / raw)
To: Johannes Thumshirn, linux-btrfs
Cc: Naohiro Aota, David Sterba, Boris Burkov, Sashiko
On 11/17/26 10:58, Johannes Thumshirn wrote:
> In call_zone_finish() the counter for reserved active zones is
> incremented without holding the per filesystem zone_active_bgs_lock
> potentially leading to a race for the active zone count.
>
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Fixes: a7e1ac7bdc5a ("btrfs: zoned: reserve zones for an active metadata/system block group")
> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Looks OK to me.
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 19+ messages in thread
end of thread, other threads:[~2026-11-17 21:09 UTC | newest]
Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 18:58 [PATCH v7 00/27] btrfs: zoned: fixes for generic/747 Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 05/27] btrfs: zoned: skip the append only metadata writeback path for conventional block groups Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 06/27] btrfs: zoned: never activate conventional metadata " Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 07/27] btrfs: zoned: use the regular allocator for " Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 08/27] btrfs: zoned: only take the metadata IO lock for append only " Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 09/27] btrfs: zoned: allow restricting the dev extent search to a zone type Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 10/27] btrfs: zoned: prefer conventional zones for metadata chunks Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 11/27] btrfs: zoned: use plain mutex lock in check_bg_is_active Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 20/27] btrfs: zoned: prepare reserving the data relocation block group at runtime Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 21/27] btrfs: zoned: promote reset data block groups to the relocation target Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 22/27] btrfs: zoned: return finished relocation block groups to the data space_info Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 23/27] btrfs: zoned: reuse block groups emptied by reclaim as relocation target Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 24/27] btrfs: add btrfs_bg_type_to_num_stripes Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 25/27] btrfs: zoned: keep a zone free for relocation when allocating data chunks Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 26/27] btrfs: zoned: don't count the relocation zone reserve as available in statfs Johannes Thumshirn
2026-11-17 18:58 ` [PATCH v7 27/27] btrfs: zoned: provide zoned specific reclaim sweep Johannes Thumshirn
[not found] ` <20261117185850.4128157-2-johannes.thumshirn@wdc.com>
2026-11-17 21:07 ` [PATCH v7 01/27] btrfs: zoned: don't wait for a zone finish after a filesystem error Damien Le Moal
[not found] ` <20261117185850.4128157-3-johannes.thumshirn@wdc.com>
2026-11-17 21:08 ` [PATCH v7 02/27] btrfs: zoned: reject dev-replace onto a target with different zone types Damien Le Moal
[not found] ` <20261117185850.4128157-4-johannes.thumshirn@wdc.com>
2026-11-17 21:09 ` [PATCH v7 03/27] btrfs: zoned: fix possible race for reserved_active_zones Damien Le Moal
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.