* [PATCH v4 1/4] btrfs: zoned: track only sequential zones as active
2026-11-25 10:21 [PATCH v4 0/4] btrfs: zoned: fix active-zone accounting and transitions Dongjiang Zhu
@ 2026-11-25 10:21 ` Dongjiang Zhu
2026-11-25 10:21 ` [PATCH v4 2/4] btrfs: zoned: recover active non-data block group roles on mount Dongjiang Zhu
` (3 subsequent siblings)
4 siblings, 0 replies; 17+ messages in thread
From: Dongjiang Zhu @ 2026-11-25 10:21 UTC (permalink / raw)
To: linux-btrfs; +Cc: johannes.thumshirn, naohiro.aota, a2jphilipp
Conventional zones do not consume hardware active-zone slots, but
conventional stripes can still be included in runtime accounting.
This complicates active-zone state management and increases the
risk of accounting errors. Simplify the accounting by tracking
only sequential zones as active.
Assisted-by: LLM
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Tested-by: Jochen Philipp <a2jphilipp@gmail.com>
Signed-off-by: Dongjiang Zhu <zhudongjiang@fygo.io>
---
fs/btrfs/zoned.c | 48 +++++++++++++++++++++++++++++++++---------------
1 file changed, 33 insertions(+), 15 deletions(-)
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index e6ce6f05ad75..3475e7785f58 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -1980,7 +1980,6 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
} else if (map->num_stripes == num_conventional) {
cache->alloc_offset = last_alloc;
cache->zone_capacity = cache->length;
- set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
goto out;
}
}
@@ -2016,6 +2015,8 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
if (!ret) {
cache->meta_write_pointer = cache->alloc_offset + cache->start;
if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags)) {
+ ASSERT(test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE,
+ &cache->runtime_flags));
spin_lock(&fs_info->zone_active_bgs_lock);
if (list_empty(&cache->active_bg_list)) {
btrfs_get_block_group(cache);
@@ -2211,6 +2212,9 @@ static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags))
return true;
+ if (!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &block_group->runtime_flags))
+ return true;
+
if (fs_info->treelog_bg == block_group->start) {
if (!btrfs_zone_activate(block_group)) {
int ret_fin;
@@ -2407,6 +2411,18 @@ int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
}
+static inline bool btrfs_needs_active_zone_tracking(struct btrfs_device *dev,
+ u64 physical)
+{
+ if (!btrfs_dev_is_sequential(dev, physical))
+ return false;
+
+ if (dev->zone_info->max_active_zones == 0)
+ return false;
+
+ return true;
+}
+
/*
* Activate block group and underlying device zones
*
@@ -2431,6 +2447,9 @@ bool btrfs_zone_activate(struct btrfs_block_group *block_group)
if (unlikely(btrfs_is_testing(fs_info)))
return true;
+ if (!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &block_group->runtime_flags))
+ return true;
+
map = block_group->physical_map;
spin_lock(&fs_info->zone_active_bgs_lock);
@@ -2462,7 +2481,7 @@ bool btrfs_zone_activate(struct btrfs_block_group *block_group)
if (!device->bdev)
continue;
- if (zinfo->max_active_zones == 0)
+ if (!btrfs_needs_active_zone_tracking(device, physical))
continue;
/*
@@ -2537,27 +2556,23 @@ static int call_zone_finish(struct btrfs_block_group *block_group,
struct btrfs_device *device = stripe->dev;
const u64 physical = stripe->physical;
struct btrfs_zoned_device_info *zinfo = device->zone_info;
+ unsigned int nofs_flags;
bool cleared;
int ret;
if (!device->bdev)
return 0;
- if (zinfo->max_active_zones == 0)
+ if (!btrfs_needs_active_zone_tracking(device, physical))
return 0;
- if (btrfs_dev_is_sequential(device, physical)) {
- unsigned int nofs_flags;
-
- nofs_flags = memalloc_nofs_save();
- ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
- physical >> SECTOR_SHIFT,
- zinfo->zone_size >> SECTOR_SHIFT);
- memalloc_nofs_restore(nofs_flags);
-
- if (ret)
- return ret;
- }
+ nofs_flags = memalloc_nofs_save();
+ ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
+ physical >> SECTOR_SHIFT,
+ zinfo->zone_size >> SECTOR_SHIFT);
+ memalloc_nofs_restore(nofs_flags);
+ if (ret)
+ return ret;
cleared = btrfs_dev_clear_active_zone(device, physical);
if (!(block_group->flags & BTRFS_BLOCK_GROUP_DATA) && cleared)
@@ -3168,6 +3183,9 @@ void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
if (!device->bdev)
continue;
+ if (zinfo->max_active_zones == 0)
+ continue;
+
if (!test_bit(physical >> zinfo->zone_size_shift, zinfo->active_zones))
continue;
--
2.39.5
^ permalink raw reply related [flat|nested] 17+ messages in thread* [PATCH v4 2/4] btrfs: zoned: recover active non-data block group roles on mount
2026-11-25 10:21 [PATCH v4 0/4] btrfs: zoned: fix active-zone accounting and transitions Dongjiang Zhu
2026-11-25 10:21 ` [PATCH v4 1/4] btrfs: zoned: track only sequential zones as active Dongjiang Zhu
@ 2026-11-25 10:21 ` Dongjiang Zhu
2026-12-02 9:08 ` Johannes Thumshirn
2026-12-02 9:25 ` Johannes Thumshirn
2026-11-25 10:21 ` [PATCH v4 3/4] btrfs: zoned: remove obsolete non-data block group activation helper Dongjiang Zhu
` (2 subsequent siblings)
4 siblings, 2 replies; 17+ messages in thread
From: Dongjiang Zhu @ 2026-11-25 10:21 UTC (permalink / raw)
To: linux-btrfs; +Cc: johannes.thumshirn, naohiro.aota, a2jphilipp
Mount restores active sequential non-data block groups and their
reservations, but leaves active_meta_bg and active_system_bg unset.
Writing to a recovered block group does not set its role pointer:
check_bg_is_active() returns early because ZONE_IS_ACTIVE is already
set, skipping the role assignment.
These block groups can still be finished through other paths, but
metadata/system write-time switching cannot identify the previous
block group when the corresponding role pointer is NULL.
Restore these roles during mount so write-time switching can identify
and finish the previous non-data block group.
Keep the active block group with the largest writable tail for each
metadata/system role and finish the others. Since the tree-log role
is not persisted, leave a reservation for a new tree-log block group.
Account for the abandoned tails in do_zone_finish().
Assisted-by: LLM
Tested-by: Jochen Philipp <a2jphilipp@gmail.com>
Signed-off-by: Dongjiang Zhu <zhudongjiang@fygo.io>
---
fs/btrfs/disk-io.c | 7 +++-
fs/btrfs/zoned.c | 102 +++++++++++++++++++++++++++++++++++++++++----
fs/btrfs/zoned.h | 7 +++-
3 files changed, 106 insertions(+), 10 deletions(-)
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index fde9f4c4ea11..021dcd38b7be 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -3768,7 +3768,12 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
btrfs_free_zone_cache(fs_info);
- btrfs_check_active_zone_reservation(fs_info);
+ ret = btrfs_restore_active_nondata_bgs(fs_info);
+ if (ret) {
+ btrfs_err(fs_info, "failed to restore active non-data block groups: %pe",
+ ERR_PTR(ret));
+ goto fail_sysfs;
+ }
if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices &&
!btrfs_check_rw_degradable(fs_info, NULL)) {
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 3475e7785f58..3853bc23d9bd 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2584,6 +2584,7 @@ static int call_zone_finish(struct btrfs_block_group *block_group,
static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
{
struct btrfs_fs_info *fs_info = block_group->fs_info;
+ struct btrfs_space_info *space_info = block_group->space_info;
struct btrfs_chunk_map *map;
const bool is_metadata = (block_group->flags &
(BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
@@ -2591,9 +2592,11 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
int ret = 0;
int i;
+ spin_lock(&space_info->lock);
spin_lock(&block_group->lock);
if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
spin_unlock(&block_group->lock);
+ spin_unlock(&space_info->lock);
return 0;
}
@@ -2601,6 +2604,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
if (is_metadata &&
block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
spin_unlock(&block_group->lock);
+ spin_unlock(&space_info->lock);
return -EAGAIN;
}
@@ -2610,13 +2614,18 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
* don't need to wait for the other IOs. This holds because we ensure
* the sequential IO submissions using the ZONE_APPEND command for data
* and block_group->meta_write_pointer for metadata.
+ *
+ * Mount-time cleanup also skips this, as there are no pending writes
+ * or concurrent allocations.
*/
if (!fully_written) {
if (test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
spin_unlock(&block_group->lock);
+ spin_unlock(&space_info->lock);
return -EAGAIN;
}
spin_unlock(&block_group->lock);
+ spin_unlock(&space_info->lock);
ret = btrfs_inc_block_group_ro(block_group, false);
if (ret)
@@ -2631,6 +2640,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
if (is_metadata)
wait_eb_writebacks(block_group);
+ spin_lock(&space_info->lock);
spin_lock(&block_group->lock);
/*
@@ -2640,6 +2650,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
&block_group->runtime_flags)) {
spin_unlock(&block_group->lock);
+ spin_unlock(&space_info->lock);
btrfs_dec_block_group_ro(block_group);
return 0;
}
@@ -2648,11 +2659,23 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
&block_group->runtime_flags)) {
spin_unlock(&block_group->lock);
+ spin_unlock(&space_info->lock);
btrfs_dec_block_group_ro(block_group);
return -EAGAIN;
}
}
+ /*
+ * For read-only block groups, the tail is already accounted as
+ * bytes_readonly.
+ */
+ if (!block_group->ro) {
+ u64 tail_unusable = block_group->zone_capacity - block_group->alloc_offset;
+
+ block_group->zone_unusable += tail_unusable;
+ btrfs_space_info_update_bytes_zone_unusable(space_info, tail_unusable);
+ }
+
clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
block_group->alloc_offset = block_group->zone_capacity;
if (block_group->flags & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM))
@@ -2662,6 +2685,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
btrfs_clear_treelog_bg(block_group);
btrfs_clear_data_reloc_bg(block_group);
spin_unlock(&block_group->lock);
+ spin_unlock(&space_info->lock);
down_read(&dev_replace->rwsem);
map = block_group->physical_map;
@@ -3128,22 +3152,58 @@ int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_fin
return 0;
}
+static int finish_extra_active_nondata_bgs(struct btrfs_fs_info *fs_info)
+{
+ struct btrfs_block_group *block_group;
+ struct btrfs_block_group *next;
+ int ret;
+
+ list_for_each_entry_safe(block_group, next, &fs_info->zone_active_bgs,
+ active_bg_list) {
+ if (!(block_group->flags &
+ (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)))
+ continue;
+
+ if (block_group == fs_info->active_meta_bg ||
+ block_group == fs_info->active_system_bg)
+ continue;
+
+ ret = do_zone_finish(block_group, true);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
/*
- * Reserve zones for one metadata block group, one tree-log block group, and one
- * system block group.
+ * Restore one active block group for normal metadata and system writes.
+ *
+ * The tree-log block group identity is not persisted, so recovered active
+ * metadata block groups cannot be distinguished by their previous role. Keep
+ * one for the normal metadata role and leave one reservation for a new
+ * tree-log block group.
+ *
+ * Older kernels rebuilt zone_active_bgs at mount without recovering the role
+ * assignments, which could result in multiple active metadata or system block
+ * groups. Finish all but the selected block group for each role.
*/
-void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
+int btrfs_restore_active_nondata_bgs(struct btrfs_fs_info *fs_info)
{
struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
struct btrfs_block_group *block_group;
+ struct btrfs_block_group *active_meta_bg = NULL;
+ struct btrfs_block_group *active_system_bg = NULL;
struct btrfs_device *device;
+ u64 active_meta_avail = 0;
+ u64 active_system_avail = 0;
/* Reserve zones for normal SINGLE metadata and tree-log block group. */
unsigned int metadata_reserve = 2;
/* Reserve a zone for SINGLE system block group. */
unsigned int system_reserve = 1;
if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags))
- return;
+ return 0;
/*
* This function is called from the mount context. So, there is no
@@ -3165,14 +3225,38 @@ void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
}
mutex_unlock(&fs_devices->device_list_mutex);
- /* Release reservation for currently active block groups. */
+ /*
+ * Account all active non-data block groups before selecting one for each role.
+ * Finishing the extra block groups releases their reservations again.
+ */
spin_lock(&fs_info->zone_active_bgs_lock);
list_for_each_entry(block_group, &fs_info->zone_active_bgs, active_bg_list) {
struct btrfs_chunk_map *map = block_group->physical_map;
+ struct btrfs_block_group **active_bg;
+ u64 *active_avail;
+ u64 avail;
- if (!(block_group->flags &
- (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)))
+ if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
+ active_bg = &active_meta_bg;
+ active_avail = &active_meta_avail;
+ } else if (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
+ active_bg = &active_system_bg;
+ active_avail = &active_system_avail;
+ } else {
continue;
+ }
+
+ spin_lock(&block_group->lock);
+ avail = block_group->zone_capacity - block_group->alloc_offset;
+ spin_unlock(&block_group->lock);
+
+ if (!*active_bg || avail > *active_avail) {
+ if (*active_bg)
+ btrfs_put_block_group(*active_bg);
+ *active_bg = block_group;
+ *active_avail = avail;
+ btrfs_get_block_group(*active_bg);
+ }
for (int i = 0; i < map->num_stripes; i++) {
struct btrfs_device *device = map->stripes[i].dev;
@@ -3192,7 +3276,11 @@ void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
zinfo->reserved_active_zones--;
}
}
+ fs_info->active_meta_bg = active_meta_bg;
+ fs_info->active_system_bg = active_system_bg;
spin_unlock(&fs_info->zone_active_bgs_lock);
+
+ return finish_extra_active_nondata_bgs(fs_info);
}
/*
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 2225090839f3..8ed28e47d71b 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -100,7 +100,7 @@ void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logica
u64 length);
int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info);
int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_finish);
-void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info);
+int btrfs_restore_active_nondata_bgs(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);
@@ -286,7 +286,10 @@ static inline int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_inf
return 0;
}
-static inline void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info) { }
+static inline int btrfs_restore_active_nondata_bgs(struct btrfs_fs_info *fs_info)
+{
+ return 0;
+}
static inline int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info,
u64 num_bytes)
--
2.39.5
^ permalink raw reply related [flat|nested] 17+ messages in thread* Re: [PATCH v4 2/4] btrfs: zoned: recover active non-data block group roles on mount
2026-11-25 10:21 ` [PATCH v4 2/4] btrfs: zoned: recover active non-data block group roles on mount Dongjiang Zhu
@ 2026-12-02 9:08 ` Johannes Thumshirn
2026-12-02 9:25 ` Johannes Thumshirn
1 sibling, 0 replies; 17+ messages in thread
From: Johannes Thumshirn @ 2026-12-02 9:08 UTC (permalink / raw)
To: Dongjiang Zhu, linux-btrfs; +Cc: naohiro.aota, a2jphilipp
On 25.11.26 11:21, Dongjiang Zhu wrote:
One nit below, apart from that:
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
> +static int finish_extra_active_nondata_bgs(struct btrfs_fs_info *fs_info)
> +{
> + struct btrfs_block_group *block_group;
> + struct btrfs_block_group *next;
> + int ret;
> +
> + list_for_each_entry_safe(block_group, next, &fs_info->zone_active_bgs,
> + active_bg_list) {
> + if (!(block_group->flags &
> + (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)))
> + continue;
> +
Why not:
if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
continue;
^ permalink raw reply [flat|nested] 17+ messages in thread* Re: [PATCH v4 2/4] btrfs: zoned: recover active non-data block group roles on mount
2026-11-25 10:21 ` [PATCH v4 2/4] btrfs: zoned: recover active non-data block group roles on mount Dongjiang Zhu
2026-12-02 9:08 ` Johannes Thumshirn
@ 2026-12-02 9:25 ` Johannes Thumshirn
2026-12-02 10:16 ` Dongjiang Zhu
1 sibling, 1 reply; 17+ messages in thread
From: Johannes Thumshirn @ 2026-12-02 9:25 UTC (permalink / raw)
To: Dongjiang Zhu, linux-btrfs; +Cc: naohiro.aota, a2jphilipp
On 25.11.26 11:21, Dongjiang Zhu wrote:
> -void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
> +int btrfs_restore_active_nondata_bgs(struct btrfs_fs_info *fs_info)
> {
> struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
> struct btrfs_block_group *block_group;
> + struct btrfs_block_group *active_meta_bg = NULL;
> + struct btrfs_block_group *active_system_bg = NULL;
> struct btrfs_device *device;
> + u64 active_meta_avail = 0;
> + u64 active_system_avail = 0;
> /* Reserve zones for normal SINGLE metadata and tree-log block group. */
> unsigned int metadata_reserve = 2;
> /* Reserve a zone for SINGLE system block group. */
> unsigned int system_reserve = 1;
>
> if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags))
> - return;
> + return 0;
>
> /*
> * This function is called from the mount context. So, there is no
> @@ -3165,14 +3225,38 @@ void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
> }
> mutex_unlock(&fs_devices->device_list_mutex);
>
> - /* Release reservation for currently active block groups. */
> + /*
> + * Account all active non-data block groups before selecting one for each role.
> + * Finishing the extra block groups releases their reservations again.
> + */
> spin_lock(&fs_info->zone_active_bgs_lock);
> list_for_each_entry(block_group, &fs_info->zone_active_bgs, active_bg_list) {
> struct btrfs_chunk_map *map = block_group->physical_map;
> + struct btrfs_block_group **active_bg;
> + u64 *active_avail;
> + u64 avail;
>
> - if (!(block_group->flags &
> - (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)))
> + if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
> + active_bg = &active_meta_bg;
> + active_avail = &active_meta_avail;
> + } else if (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
> + active_bg = &active_system_bg;
> + active_avail = &active_system_avail;
> + } else {
> continue;
> + }
> +
> + spin_lock(&block_group->lock);
> + avail = block_group->zone_capacity - block_group->alloc_offset;
> + spin_unlock(&block_group->lock);
> +
> + if (!*active_bg || avail > *active_avail) {
> + if (*active_bg)
> + btrfs_put_block_group(*active_bg);
> + *active_bg = block_group;
> + *active_avail = avail;
> + btrfs_get_block_group(*active_bg);
> + }
>
> for (int i = 0; i < map->num_stripes; i++) {
> struct btrfs_device *device = map->stripes[i].dev;
> @@ -3192,7 +3276,11 @@ void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
> zinfo->reserved_active_zones--;
> }
> }
> + fs_info->active_meta_bg = active_meta_bg;
> + fs_info->active_system_bg = active_system_bg;
> spin_unlock(&fs_info->zone_active_bgs_lock);
Sorry for hopping in again, but can this be reached with a read only mount?
> +
> + return finish_extra_active_nondata_bgs(fs_info);
> }
^ permalink raw reply [flat|nested] 17+ messages in thread* Re: [PATCH v4 2/4] btrfs: zoned: recover active non-data block group roles on mount
2026-12-02 9:25 ` Johannes Thumshirn
@ 2026-12-02 10:16 ` Dongjiang Zhu
2026-12-02 11:25 ` Johannes Thumshirn
0 siblings, 1 reply; 17+ messages in thread
From: Dongjiang Zhu @ 2026-12-02 10:16 UTC (permalink / raw)
To: Johannes Thumshirn, linux-btrfs; +Cc: naohiro.aota, a2jphilipp
在 2026/12/2 17:25, Johannes Thumshirn 写道:
> On 25.11.26 11:21, Dongjiang Zhu wrote:
>> -void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
>> +int btrfs_restore_active_nondata_bgs(struct btrfs_fs_info *fs_info)
>> {
>> struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
>> struct btrfs_block_group *block_group;
>> + struct btrfs_block_group *active_meta_bg = NULL;
>> + struct btrfs_block_group *active_system_bg = NULL;
>> struct btrfs_device *device;
>> + u64 active_meta_avail = 0;
>> + u64 active_system_avail = 0;
>> /* Reserve zones for normal SINGLE metadata and tree-log block group. */
>> unsigned int metadata_reserve = 2;
>> /* Reserve a zone for SINGLE system block group. */
>> unsigned int system_reserve = 1;
>> if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags))
>> - return;
>> + return 0;
>> /*
>> * This function is called from the mount context. So, there is no
>> @@ -3165,14 +3225,38 @@ void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
>> }
>> mutex_unlock(&fs_devices->device_list_mutex);
>> - /* Release reservation for currently active block groups. */
>> + /*
>> + * Account all active non-data block groups before selecting one for each role.
>> + * Finishing the extra block groups releases their reservations again.
>> + */
>> spin_lock(&fs_info->zone_active_bgs_lock);
>> list_for_each_entry(block_group, &fs_info->zone_active_bgs, active_bg_list) {
>> struct btrfs_chunk_map *map = block_group->physical_map;
>> + struct btrfs_block_group **active_bg;
>> + u64 *active_avail;
>> + u64 avail;
>> - if (!(block_group->flags &
>> - (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)))
>> + if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
>> + active_bg = &active_meta_bg;
>> + active_avail = &active_meta_avail;
>> + } else if (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
>> + active_bg = &active_system_bg;
>> + active_avail = &active_system_avail;
>> + } else {
>> continue;
>> + }
>> +
>> + spin_lock(&block_group->lock);
>> + avail = block_group->zone_capacity - block_group->alloc_offset;
>> + spin_unlock(&block_group->lock);
>> +
>> + if (!*active_bg || avail > *active_avail) {
>> + if (*active_bg)
>> + btrfs_put_block_group(*active_bg);
>> + *active_bg = block_group;
>> + *active_avail = avail;
>> + btrfs_get_block_group(*active_bg);
>> + }
>> for (int i = 0; i < map->num_stripes; i++) {
>> struct btrfs_device *device = map->stripes[i].dev;
>> @@ -3192,7 +3276,11 @@ void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
>> zinfo->reserved_active_zones--;
>> }
>> }
>> + fs_info->active_meta_bg = active_meta_bg;
>> + fs_info->active_system_bg = active_system_bg;
>> spin_unlock(&fs_info->zone_active_bgs_lock);
>
> Sorry for hopping in again, but can this be reached with a read only mount?
Hi Johannes,
Many thanks for taking the time to review this. Yes, this path
is also reached on a read-only mount, so the unconditional zone
finishing is a problem.
Your recent work overlaps with patch 1, and I also saw your work
on offline recovery for extra active zones. If you're interested,
please feel free to address the issue described in patch 2 as
part of your work. I'd really appreciate it. Also, many thanks
for all your recent work on zoned support.
Would it make sense to submit patches 3 and 4 separately for now?
They address independent issues.
Thanks,
Dongjiang
^ permalink raw reply [flat|nested] 17+ messages in thread* Re: [PATCH v4 2/4] btrfs: zoned: recover active non-data block group roles on mount
2026-12-02 10:16 ` Dongjiang Zhu
@ 2026-12-02 11:25 ` Johannes Thumshirn
2026-12-03 10:21 ` Dongjiang Zhu
0 siblings, 1 reply; 17+ messages in thread
From: Johannes Thumshirn @ 2026-12-02 11:25 UTC (permalink / raw)
To: Dongjiang Zhu, linux-btrfs; +Cc: naohiro.aota, a2jphilipp
On 02.12.26 11:16, Dongjiang Zhu wrote:
>>
>> Sorry for hopping in again, but can this be reached with a read only mount?
>
> Hi Johannes,
>
> Many thanks for taking the time to review this. Yes, this path
> is also reached on a read-only mount, so the unconditional zone
> finishing is a problem.
Thanks for confirming it.
>
> Your recent work overlaps with patch 1, and I also saw your work
> on offline recovery for extra active zones. If you're interested,
> please feel free to address the issue described in patch 2 as
> part of your work. I'd really appreciate it. Also, many thanks
> for all your recent work on zoned support.
>
> Would it make sense to submit patches 3 and 4 separately for now?
> They address independent issues.
I think a v5 of the series will be sufficient. I'll review as soon as it
is out.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v4 2/4] btrfs: zoned: recover active non-data block group roles on mount
2026-12-02 11:25 ` Johannes Thumshirn
@ 2026-12-03 10:21 ` Dongjiang Zhu
0 siblings, 0 replies; 17+ messages in thread
From: Dongjiang Zhu @ 2026-12-03 10:21 UTC (permalink / raw)
To: Johannes Thumshirn, linux-btrfs; +Cc: naohiro.aota, a2jphilipp
在 2026/12/2 19:25, Johannes Thumshirn 写道:
> On 02.12.26 11:16, Dongjiang Zhu wrote:
>>>
>>> Sorry for hopping in again, but can this be reached with a read only mount?
>>
>> Hi Johannes,
>>
>> Many thanks for taking the time to review this. Yes, this path
>> is also reached on a read-only mount, so the unconditional zone
>> finishing is a problem.
>
> Thanks for confirming it.
>>
>> Your recent work overlaps with patch 1, and I also saw your work
>> on offline recovery for extra active zones. If you're interested,
>> please feel free to address the issue described in patch 2 as
>> part of your work. I'd really appreciate it. Also, many thanks
>> for all your recent work on zoned support.
>>
>> Would it make sense to submit patches 3 and 4 separately for now?
>> They address independent issues.
> I think a v5 of the series will be sufficient. I'll review as soon as it is out.
Hi Johannes,
Sorry for the change of plan. I've sent v5 with only patches 3
and 4 from v4, as they address independent issues.
I've deferred v4 patches 1 and 2 for now. Patch 1 overlaps with
your work, and Sashiko's review raised some challenging issues
with patch 2 that require rethinking its design.
Thanks for your review and patience.
Dongjiang
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v4 3/4] btrfs: zoned: remove obsolete non-data block group activation helper
2026-11-25 10:21 [PATCH v4 0/4] btrfs: zoned: fix active-zone accounting and transitions Dongjiang Zhu
2026-11-25 10:21 ` [PATCH v4 1/4] btrfs: zoned: track only sequential zones as active Dongjiang Zhu
2026-11-25 10:21 ` [PATCH v4 2/4] btrfs: zoned: recover active non-data block group roles on mount Dongjiang Zhu
@ 2026-11-25 10:21 ` Dongjiang Zhu
2026-11-25 10:21 ` [PATCH v4 4/4] btrfs: zoned: serialize zone finishing per block group Dongjiang Zhu
2026-11-27 13:34 ` [PATCH v4 0/4] btrfs: zoned: fix active-zone accounting and transitions Jochen Philipp
4 siblings, 0 replies; 17+ messages in thread
From: Dongjiang Zhu @ 2026-11-25 10:21 UTC (permalink / raw)
To: linux-btrfs; +Cc: johannes.thumshirn, naohiro.aota, a2jphilipp
On an HC620 with max_active_zones=128, file writes occupied all 128
active-zone slots. With four direct-I/O writers still running, starting
a balance with
btrfs balance start -f -dusage=0 -musage=0 -susage=100 <mountpoint>
left the balance task blocked in D state. SysRq-w reported:
task:btrfs state:D stack:0 pid:8145
Call Trace:
<TASK>
__schedule+0x50e/0xfe0
schedule+0x22/0xa0
schedule_preempt_disabled+0x10/0x20
__mutex_lock.constprop.0+0x380/0x920
btrfs_inc_block_group_ro+0x6a/0x270 [btrfs]
do_zone_finish+0x2b6/0x4c0 [btrfs]
btrfs_zone_finish_one_bg+0x184/0x1f0 [btrfs]
btrfs_zoned_activate_one_bg+0x189/0x1d0 [btrfs]
reserve_chunk_space+0xdb/0x180 [btrfs]
btrfs_inc_block_group_ro+0x252/0x270 [btrfs]
btrfs_relocate_block_group+0xc2/0x2260 [btrfs]
btrfs_relocate_chunk+0x39/0x1c0 [btrfs]
btrfs_balance+0x911/0x1810 [btrfs]
</TASK>
The blocked balance task also stalls transaction commit and data writers,
so the filesystem can no longer make write progress or unmount cleanly.
This happens because the outer btrfs_inc_block_group_ro() holds
ro_block_group_mutex while reserve_chunk_space() tries to activate a
system block group. When no active-zone slot is available,
btrfs_zoned_activate_one_bg() starts finishing a partially written data
block group.
do_zone_finish() then enters btrfs_inc_block_group_ro() again. The same
task waits for ro_block_group_mutex that it already holds and
self-deadlocks.
These allocation-time activation calls were needed for
active_total_bytes accounting, which has been removed. Metadata and
system block groups can instead be activated during writeback.
Remove both calls and the unused helper. This avoids recursively
entering btrfs_inc_block_group_ro() while holding ro_block_group_mutex
and leaves activation to the writeback path, where normal metadata
and system block group switching is handled.
Assisted-by: LLM
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Tested-by: Jochen Philipp <a2jphilipp@gmail.com>
Signed-off-by: Dongjiang Zhu <zhudongjiang@fygo.io>
---
fs/btrfs/block-group.c | 38 +++++++++-----------------------
fs/btrfs/zoned.c | 50 ------------------------------------------
fs/btrfs/zoned.h | 8 -------
3 files changed, 10 insertions(+), 86 deletions(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index a40eedc65dd2..ff108c5de8fa 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -3212,13 +3212,6 @@ int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
alloc_flags = btrfs_get_alloc_profile(fs_info, space_info->flags);
ret = btrfs_chunk_alloc(trans, space_info, alloc_flags, CHUNK_ALLOC_FORCE);
- if (ret < 0)
- goto out;
- /*
- * We have allocated a new chunk. We also need to activate that chunk to
- * grant metadata tickets for zoned filesystem.
- */
- ret = btrfs_zoned_activate_one_bg(space_info, true);
if (ret < 0)
goto out;
@@ -4201,29 +4194,18 @@ static void reserve_chunk_space(struct btrfs_trans_handle *trans,
if (IS_ERR(bg)) {
ret = PTR_ERR(bg);
} else {
- int activate_ret;
-
/*
- * We have a new chunk. We also need to activate it for
- * zoned filesystem.
+ * If we fail to add the chunk item here, we end
+ * up trying again at phase 2 of chunk allocation,
+ * at btrfs_create_pending_block_groups(). So
+ * ignore any error here. An ENOSPC here could
+ * happen, due to the cases described at
+ * do_chunk_alloc() - the system block group we
+ * just created was just turned into RO mode by a
+ * scrub for example, or a running discard
+ * temporarily removed its free space entries, etc.
*/
- activate_ret = btrfs_zoned_activate_one_bg(info, true);
- if (activate_ret < 0) {
- ret = activate_ret;
- } else {
- /*
- * If we fail to add the chunk item here, we end
- * up trying again at phase 2 of chunk allocation,
- * at btrfs_create_pending_block_groups(). So
- * ignore any error here. An ENOSPC here could
- * happen, due to the cases described at
- * do_chunk_alloc() - the system block group we
- * just created was just turned into RO mode by a
- * scrub for example, or a running discard
- * temporarily removed its free space entries, etc.
- */
- btrfs_chunk_alloc_add_chunk_item(trans, bg);
- }
+ btrfs_chunk_alloc_add_chunk_item(trans, bg);
}
}
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 3853bc23d9bd..2d02d6fd2526 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -3102,56 +3102,6 @@ int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
return ret < 0 ? ret : 1;
}
-int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_finish)
-{
- struct btrfs_fs_info *fs_info = space_info->fs_info;
- struct btrfs_block_group *bg;
- int index;
-
- if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
- return 0;
-
- for (;;) {
- int ret;
- bool need_finish = false;
-
- down_read(&space_info->groups_sem);
- for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
- list_for_each_entry(bg, &space_info->block_groups[index],
- list) {
- if (!spin_trylock(&bg->lock))
- continue;
- if (btrfs_zoned_bg_is_full(bg) ||
- test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
- &bg->runtime_flags)) {
- spin_unlock(&bg->lock);
- continue;
- }
- spin_unlock(&bg->lock);
-
- if (btrfs_zone_activate(bg)) {
- up_read(&space_info->groups_sem);
- return 1;
- }
-
- need_finish = true;
- }
- }
- up_read(&space_info->groups_sem);
-
- if (!do_finish || !need_finish)
- break;
-
- ret = btrfs_zone_finish_one_bg(fs_info);
- if (ret == 0)
- break;
- if (ret < 0)
- return ret;
- }
-
- return 0;
-}
-
static int finish_extra_active_nondata_bgs(struct btrfs_fs_info *fs_info)
{
struct btrfs_block_group *block_group;
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 8ed28e47d71b..7db5d5a250ed 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -99,7 +99,6 @@ 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,
u64 length);
int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info);
-int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_finish);
int btrfs_restore_active_nondata_bgs(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);
@@ -279,13 +278,6 @@ static inline int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
return 1;
}
-static inline int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info,
- bool do_finish)
-{
- /* Consider all the block groups are active */
- return 0;
-}
-
static inline int btrfs_restore_active_nondata_bgs(struct btrfs_fs_info *fs_info)
{
return 0;
--
2.39.5
^ permalink raw reply related [flat|nested] 17+ messages in thread* [PATCH v4 4/4] btrfs: zoned: serialize zone finishing per block group
2026-11-25 10:21 [PATCH v4 0/4] btrfs: zoned: fix active-zone accounting and transitions Dongjiang Zhu
` (2 preceding siblings ...)
2026-11-25 10:21 ` [PATCH v4 3/4] btrfs: zoned: remove obsolete non-data block group activation helper Dongjiang Zhu
@ 2026-11-25 10:21 ` Dongjiang Zhu
2026-12-02 11:26 ` Johannes Thumshirn
2026-11-27 13:34 ` [PATCH v4 0/4] btrfs: zoned: fix active-zone accounting and transitions Jochen Philipp
4 siblings, 1 reply; 17+ messages in thread
From: Dongjiang Zhu @ 2026-11-25 10:21 UTC (permalink / raw)
To: linux-btrfs; +Cc: johannes.thumshirn, naohiro.aota, a2jphilipp
On an HC620 with max_active_zones=128, buffered writes occupied all 128
active-zone slots. A metadata-heavy create/delete workload followed by
sync then forced a metadata block group switch.
A kernel with additional zone finish tracing reported:
zoned-debug: finish clear-runtime bg=140928614400 caller=btrfs_zone_finish_endio_workfn comm=kworker/u8:7
zoned-debug: finish early-inactive bg=140928614400 caller=btrfs_check_meta_write_pointer comm=sync
zoned-debug: pivot finish ret=0 old_bg=140928614400 new_bg=339839287296
zoned-debug: activate fail slot bg=339839287296 type=metadata left=0 max_active=128 comm=sync
zoned-debug: finish zone-mgmt bg=140928614400 ret=0 left=0 caller=btrfs_zone_finish_endio_workfn comm=kworker/u8:7
BTRFS error (device sdb): error while writing out transaction: -EAGAIN
BTRFS error (device sdb state A): Transaction 67 aborted (-EAGAIN)
BTRFS info (device sdb state EA): forced readonly
zoned-debug: clear-active dev=/dev/sdb pos=140928614400 left=1 max_active=128
The transaction was aborted before the old block group returned its
active-zone slot, forcing the filesystem read-only.
This happens because do_zone_finish() clears
BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE before issuing the zone finish operation.
This allows the following interleaving:
Thread A, endio worker Thread B, transaction writeback
clear ZONE_IS_ACTIVE
call_zone_finish(old)
observe old as inactive
return from do_zone_finish(old)
activate(new)
fail with left == 0
clear the device active bit
increment active_zones_left
Thread B treats the cleared active bit as a completed finish, although
thread A has not yet returned the active-zone slot.
Add ZONE_FINISHING to serialize finishers per block group. Keep
ZONE_IS_ACTIVE set until the device operations and active-list cleanup
are complete, then wake waiting finishers. Release any temporary
read-only reference acquired by a waiting caller.
Assisted-by: LLM
Tested-by: Jochen Philipp <a2jphilipp@gmail.com>
Signed-off-by: Dongjiang Zhu <zhudongjiang@fygo.io>
---
fs/btrfs/block-group.h | 1 +
fs/btrfs/zoned.c | 31 ++++++++++++++++++++++++++++++-
2 files changed, 31 insertions(+), 1 deletion(-)
diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h
index d567ed822e55..31aca6424d54 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_ZONE_FINISHING,
};
enum btrfs_caching_type {
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 2d02d6fd2526..14ea819831b6 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2589,6 +2589,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
const bool is_metadata = (block_group->flags &
(BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
+ bool bg_ro = false;
int ret = 0;
int i;
@@ -2600,6 +2601,12 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
return 0;
}
+ if (test_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING, &block_group->runtime_flags)) {
+ spin_unlock(&block_group->lock);
+ spin_unlock(&space_info->lock);
+ goto wait_finish;
+ }
+
/* Check if we have unwritten allocated space */
if (is_metadata &&
block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
@@ -2630,6 +2637,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
ret = btrfs_inc_block_group_ro(block_group, false);
if (ret)
return ret;
+ bg_ro = true;
/* Ensure all writes in this block group finish */
btrfs_wait_block_group_reservations(block_group);
@@ -2655,6 +2663,13 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
return 0;
}
+ if (test_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING,
+ &block_group->runtime_flags)) {
+ spin_unlock(&block_group->lock);
+ spin_unlock(&space_info->lock);
+ goto wait_finish;
+ }
+
if (block_group->reserved ||
test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
&block_group->runtime_flags)) {
@@ -2676,7 +2691,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
btrfs_space_info_update_bytes_zone_unusable(space_info, tail_unusable);
}
- clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
+ set_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING, &block_group->runtime_flags);
block_group->alloc_offset = block_group->zone_capacity;
if (block_group->flags & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM))
block_group->meta_write_pointer = block_group->start +
@@ -2700,8 +2715,13 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
btrfs_dec_block_group_ro(block_group);
spin_lock(&fs_info->zone_active_bgs_lock);
+ spin_lock(&block_group->lock);
+ clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
ASSERT(!list_empty(&block_group->active_bg_list));
list_del_init(&block_group->active_bg_list);
+ clear_and_wake_up_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING,
+ &block_group->runtime_flags);
+ spin_unlock(&block_group->lock);
spin_unlock(&fs_info->zone_active_bgs_lock);
/* For active_bg_list */
@@ -2710,6 +2730,15 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
return ret;
+
+wait_finish:
+ wait_on_bit_io(&block_group->runtime_flags,
+ BLOCK_GROUP_FLAG_ZONE_FINISHING,
+ TASK_UNINTERRUPTIBLE);
+ if (bg_ro)
+ btrfs_dec_block_group_ro(block_group);
+
+ return 0;
}
int btrfs_zone_finish(struct btrfs_block_group *block_group)
--
2.39.5
^ permalink raw reply related [flat|nested] 17+ messages in thread* Re: [PATCH v4 4/4] btrfs: zoned: serialize zone finishing per block group
2026-11-25 10:21 ` [PATCH v4 4/4] btrfs: zoned: serialize zone finishing per block group Dongjiang Zhu
@ 2026-12-02 11:26 ` Johannes Thumshirn
2026-12-02 11:46 ` Dongjiang Zhu
0 siblings, 1 reply; 17+ messages in thread
From: Johannes Thumshirn @ 2026-12-02 11:26 UTC (permalink / raw)
To: Dongjiang Zhu, linux-btrfs; +Cc: naohiro.aota, a2jphilipp
On 25.11.26 11:21, Dongjiang Zhu wrote:
> On an HC620 with max_active_zones=128, buffered writes occupied all 128
> active-zone slots. A metadata-heavy create/delete workload followed by
> sync then forced a metadata block group switch.
>
> A kernel with additional zone finish tracing reported:
>
> zoned-debug: finish clear-runtime bg=140928614400 caller=btrfs_zone_finish_endio_workfn comm=kworker/u8:7
> zoned-debug: finish early-inactive bg=140928614400 caller=btrfs_check_meta_write_pointer comm=sync
> zoned-debug: pivot finish ret=0 old_bg=140928614400 new_bg=339839287296
> zoned-debug: activate fail slot bg=339839287296 type=metadata left=0 max_active=128 comm=sync
> zoned-debug: finish zone-mgmt bg=140928614400 ret=0 left=0 caller=btrfs_zone_finish_endio_workfn comm=kworker/u8:7
> BTRFS error (device sdb): error while writing out transaction: -EAGAIN
> BTRFS error (device sdb state A): Transaction 67 aborted (-EAGAIN)
> BTRFS info (device sdb state EA): forced readonly
> zoned-debug: clear-active dev=/dev/sdb pos=140928614400 left=1 max_active=128
>
> The transaction was aborted before the old block group returned its
> active-zone slot, forcing the filesystem read-only.
>
> This happens because do_zone_finish() clears
> BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE before issuing the zone finish operation.
> This allows the following interleaving:
>
> Thread A, endio worker Thread B, transaction writeback
>
> clear ZONE_IS_ACTIVE
> call_zone_finish(old)
> observe old as inactive
> return from do_zone_finish(old)
> activate(new)
> fail with left == 0
> clear the device active bit
> increment active_zones_left
>
> Thread B treats the cleared active bit as a completed finish, although
> thread A has not yet returned the active-zone slot.
>
> Add ZONE_FINISHING to serialize finishers per block group. Keep
> ZONE_IS_ACTIVE set until the device operations and active-list cleanup
> are complete, then wake waiting finishers. Release any temporary
> read-only reference acquired by a waiting caller.
>
> Assisted-by: LLM
> Tested-by: Jochen Philipp <a2jphilipp@gmail.com>
> Signed-off-by: Dongjiang Zhu <zhudongjiang@fygo.io>
This is partially already covered in for-next with: "btrfs: zoned: don't
wait for a zone finish after a filesystem error".
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v4 4/4] btrfs: zoned: serialize zone finishing per block group
2026-12-02 11:26 ` Johannes Thumshirn
@ 2026-12-02 11:46 ` Dongjiang Zhu
2026-12-02 15:03 ` Johannes Thumshirn
0 siblings, 1 reply; 17+ messages in thread
From: Dongjiang Zhu @ 2026-12-02 11:46 UTC (permalink / raw)
To: Johannes Thumshirn, linux-btrfs; +Cc: naohiro.aota, a2jphilipp
在 2026/12/2 19:26, Johannes Thumshirn 写道:
> On 25.11.26 11:21, Dongjiang Zhu wrote:
>> On an HC620 with max_active_zones=128, buffered writes occupied all 128
>> active-zone slots. A metadata-heavy create/delete workload followed by
>> sync then forced a metadata block group switch.
>>
>> A kernel with additional zone finish tracing reported:
>>
>> zoned-debug: finish clear-runtime bg=140928614400 caller=btrfs_zone_finish_endio_workfn comm=kworker/u8:7
>> zoned-debug: finish early-inactive bg=140928614400 caller=btrfs_check_meta_write_pointer comm=sync
>> zoned-debug: pivot finish ret=0 old_bg=140928614400 new_bg=339839287296
>> zoned-debug: activate fail slot bg=339839287296 type=metadata left=0 max_active=128 comm=sync
>> zoned-debug: finish zone-mgmt bg=140928614400 ret=0 left=0 caller=btrfs_zone_finish_endio_workfn comm=kworker/u8:7
>> BTRFS error (device sdb): error while writing out transaction: -EAGAIN
>> BTRFS error (device sdb state A): Transaction 67 aborted (-EAGAIN)
>> BTRFS info (device sdb state EA): forced readonly
>> zoned-debug: clear-active dev=/dev/sdb pos=140928614400 left=1 max_active=128
>>
>> The transaction was aborted before the old block group returned its
>> active-zone slot, forcing the filesystem read-only.
>>
>> This happens because do_zone_finish() clears
>> BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE before issuing the zone finish operation.
>> This allows the following interleaving:
>>
>> Thread A, endio worker Thread B, transaction writeback
>>
>> clear ZONE_IS_ACTIVE
>> call_zone_finish(old)
>> observe old as inactive
>> return from do_zone_finish(old)
>> activate(new)
>> fail with left == 0
>> clear the device active bit
>> increment active_zones_left
>>
>> Thread B treats the cleared active bit as a completed finish, although
>> thread A has not yet returned the active-zone slot.
>>
>> Add ZONE_FINISHING to serialize finishers per block group. Keep
>> ZONE_IS_ACTIVE set until the device operations and active-list cleanup
>> are complete, then wake waiting finishers. Release any temporary
>> read-only reference acquired by a waiting caller.
>>
>> Assisted-by: LLM
>> Tested-by: Jochen Philipp <a2jphilipp@gmail.com>
>> Signed-off-by: Dongjiang Zhu <zhudongjiang@fygo.io>
> This is partially already covered in for-next with: "btrfs: zoned: don't wait for a zone finish after a filesystem error".
Hi Johannes,
Thanks for pointing this out. I think these address different issues:
your fix wakes waiters after a filesystem error, while patch 4
addresses a race that can cause the error in the first place.
Am I missing something?
Thanks,
Dongjiang
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v4 4/4] btrfs: zoned: serialize zone finishing per block group
2026-12-02 11:46 ` Dongjiang Zhu
@ 2026-12-02 15:03 ` Johannes Thumshirn
0 siblings, 0 replies; 17+ messages in thread
From: Johannes Thumshirn @ 2026-12-02 15:03 UTC (permalink / raw)
To: Dongjiang Zhu, linux-btrfs; +Cc: naohiro.aota, a2jphilipp
On 02.12.26 12:46, Dongjiang Zhu wrote:
>> This is partially already covered in for-next with: "btrfs: zoned: don't wait for a zone finish after a filesystem error".
>
> Hi Johannes,
>
> Thanks for pointing this out. I think these address different issues:
> your fix wakes waiters after a filesystem error, while patch 4
> addresses a race that can cause the error in the first place.
>
> Am I missing something?
No you're right, I'm sorry.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v4 0/4] btrfs: zoned: fix active-zone accounting and transitions
2026-11-25 10:21 [PATCH v4 0/4] btrfs: zoned: fix active-zone accounting and transitions Dongjiang Zhu
` (3 preceding siblings ...)
2026-11-25 10:21 ` [PATCH v4 4/4] btrfs: zoned: serialize zone finishing per block group Dongjiang Zhu
@ 2026-11-27 13:34 ` Jochen Philipp
2026-11-30 7:09 ` Dongjiang Zhu
4 siblings, 1 reply; 17+ messages in thread
From: Jochen Philipp @ 2026-11-27 13:34 UTC (permalink / raw)
To: Dongjiang Zhu; +Cc: linux-btrfs, johannes.thumshirn, naohiro.aota
Hi Dongjiang,
I wanted to test v4 as well, but I couldn't find a tree it
applies to:
- the base commit 7bfc2c517eac is no longer in btrfs for-next
(looks like it has been rebased since)
- on current btrfs for-next, patch 1/4 fails to apply in
fs/btrfs/zoned.c
- same on your for-next (df5b88a7914e)
Could you push v4 to a branch, or tell me which base to use?
Thanks,
Jochen
Am Mi., 25. Nov. 2026 um 11:21 Uhr schrieb Dongjiang Zhu <zhudongjiang@fygo.io>:
>
> This series addresses active-zone accounting and block group
> transitions on zoned devices. It simplifies conventional-zone handling,
> restores metadata/system write roles at mount, and addresses a
> balance self-deadlock and a race during zone finishing.
>
> The four patches:
>
> 1. Track only sequential zones as active.
> 2. Restore the normal metadata and system write roles at mount,
> keeping one active block group for each role and finishing the
> remaining active non-data block groups.
> 3. Remove obsolete allocation-time non-data activation, leaving
> activation and role switching to the writeback path.
> 4. Serialize concurrent finish operations on the same block group,
> keeping it active until the device operations and active-list
> cleanup complete.
>
> Jochen Philipp tested a rebased version of this series on his
> filesystem. He reported writing another 2.3 TB via rsync without
> Btrfs errors, a clean unmount, no errors from btrfs check --readonly,
> and a successful read-write mount afterwards. Many thanks to Jochen
> for the extensive testing.
>
> Changes in v4:
> - Rebased onto btrfs for-next at 7bfc2c517eac.
> - Retained the upstream active-zone bitmap check for mount-time
> reservation accounting and the conditional reservation updates
> during activation and finishing.
> - Moved abandoned-tail accounting into do_zone_finish(), alongside
> the alloc_offset update, preserving read-only block group accounting.
> - Removed the unnecessary temporary block group references in
> mount-time cleanup.
> - Updated the commit messages to reflect the current base and clarify
> the role-recovery and zone-finishing behavior.
>
> Changes in v3:
> - Dropped the block-group flag comment changes from patch 1; no
> functional changes.
>
> Changes in v2:
> - Removed the [BUG], [CAUSE] and [FIX] headings from the commit messages.
> - Added a helper for identifying zones that need active-zone tracking.
> - Added block group locking around alloc_offset and zone_unusable
> accesses during mount recovery.
> - Explained why moving the active-bit clear alone does not serialize
> concurrent zone finish operations.
>
> Previous versions:
> v3: https://lore.kernel.org/linux-btrfs/cover.1787887776.git.zhudongjiang@fygo.io/
> v2: https://lore.kernel.org/linux-btrfs/cover.1787717573.git.zhudongjiang@fygo.io/
> v1: https://lore.kernel.org/linux-btrfs/cover.1787551793.git.zhudongjiang@fygo.io/
>
> Dongjiang Zhu (4):
> btrfs: zoned: track only sequential zones as active
> btrfs: zoned: recover active non-data block group roles on mount
> btrfs: zoned: remove obsolete non-data block group activation helper
> btrfs: zoned: serialize zone finishing per block group
>
> fs/btrfs/block-group.c | 38 ++------
> fs/btrfs/block-group.h | 1 +
> fs/btrfs/disk-io.c | 7 +-
> fs/btrfs/zoned.c | 211 +++++++++++++++++++++++++++++------------
> fs/btrfs/zoned.h | 9 +-
> 5 files changed, 167 insertions(+), 99 deletions(-)
>
> --
> 2.39.5
^ permalink raw reply [flat|nested] 17+ messages in thread* Re: [PATCH v4 0/4] btrfs: zoned: fix active-zone accounting and transitions
2026-11-27 13:34 ` [PATCH v4 0/4] btrfs: zoned: fix active-zone accounting and transitions Jochen Philipp
@ 2026-11-30 7:09 ` Dongjiang Zhu
2026-11-30 15:00 ` Jochen Philipp
0 siblings, 1 reply; 17+ messages in thread
From: Dongjiang Zhu @ 2026-11-30 7:09 UTC (permalink / raw)
To: Jochen Philipp; +Cc: linux-btrfs, johannes.thumshirn, naohiro.aota
Hi Jochen,
Thank you very much for offering to do more testing.
I suspect the issue you encountered is the race addressed by
patch 4. Would you be willing to test the standalone diff below?
Some of Johannes's recent zoned work overlaps with patches 1 and 2
and may address the same issues. I may therefore submit patches 3
and 4 separately, as they address independent problems.
The diff is based on btrfs for-next at 80bef0a:
https://github.com/btrfs/linux/tree/for-next
I've checked that it builds.
Thanks,
Dongjiang
--------------------- git diff -----------------------------
diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h
index d567ed822e55..31aca6424d54 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_ZONE_FINISHING,
};
enum btrfs_caching_type {
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 0277b2a48a73..1eabd2a9b882 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2574,6 +2574,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
(BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
enum btrfs_raid_types index;
+ bool bg_ro = false;
bool zones_full;
int failed = 0;
int ret = 0;
@@ -2585,6 +2586,11 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
return 0;
}
+ if (test_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING, &block_group->runtime_flags)) {
+ spin_unlock(&block_group->lock);
+ goto wait_finish;
+ }
+
/* Check if we have unwritten allocated space */
if (is_metadata &&
block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
@@ -2609,6 +2615,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
ret = btrfs_inc_block_group_ro(block_group, false);
if (ret)
return ret;
+ bg_ro = true;
/* Ensure all writes in this block group finish */
btrfs_wait_block_group_reservations(block_group);
@@ -2632,6 +2639,12 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
return 0;
}
+ if (test_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING,
+ &block_group->runtime_flags)) {
+ spin_unlock(&block_group->lock);
+ goto wait_finish;
+ }
+
if (block_group->reserved ||
test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
&block_group->runtime_flags)) {
@@ -2641,7 +2654,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
}
}
- clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
+ set_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING, &block_group->runtime_flags);
/*
* If the block group is allocated up to the zone capacity,
* everything is written and the zones should be full already. A
@@ -2700,8 +2713,13 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
btrfs_dec_block_group_ro(block_group);
spin_lock(&fs_info->zone_active_bgs_lock);
+ spin_lock(&block_group->lock);
+ clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
ASSERT(!list_empty(&block_group->active_bg_list));
list_del_init(&block_group->active_bg_list);
+ clear_and_wake_up_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING,
+ &block_group->runtime_flags);
+ spin_unlock(&block_group->lock);
spin_unlock(&fs_info->zone_active_bgs_lock);
/* For active_bg_list */
@@ -2710,6 +2728,15 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
return ret;
+
+wait_finish:
+ wait_on_bit_io(&block_group->runtime_flags,
+ BLOCK_GROUP_FLAG_ZONE_FINISHING,
+ TASK_UNINTERRUPTIBLE);
+ if (bg_ro)
+ btrfs_dec_block_group_ro(block_group);
+
+ return 0;
}
int btrfs_zone_finish(struct btrfs_block_group *block_group)
^ permalink raw reply related [flat|nested] 17+ messages in thread* Re: [PATCH v4 0/4] btrfs: zoned: fix active-zone accounting and transitions
2026-11-30 7:09 ` Dongjiang Zhu
@ 2026-11-30 15:00 ` Jochen Philipp
2026-12-02 10:11 ` Dongjiang Zhu
0 siblings, 1 reply; 17+ messages in thread
From: Jochen Philipp @ 2026-11-30 15:00 UTC (permalink / raw)
To: Dongjiang Zhu; +Cc: linux-btrfs, johannes.thumshirn, naohiro.aota
Hi Dongjiang,
I tested your standalone patch 4 diff on btrfs for-next 80bef0a
(7.4.0-rc5-p4+, untainted). Note: the diff came through with
tabs converted to spaces, so I had to apply it with
git apply --ignore-whitespace.
Results:
- after reboot into this kernel and mount: 126 closed zones
(all previously open zones had become closed by the power cycle)
- copied ~317 GB via rsync from a USB disk, no btrfs messages
- afterwards: 5 closed + 121 implicitly open = 126 active zones,
i.e. it stayed below max_open_zones=128 and kept working
- umount, btrfs check --readonly: no error found
- mounted again, stays rw
One correction to my earlier reports: I only counted closed
zones back then, not open ones. So "closed zones dropped from 124
to 5" most likely meant the zones went from closed to open again,
not that they were finished. Sorry for the confusion.
The fs is now at ~88% full, so I can't do many more large write
tests on it.
If it helps:
Tested-by: Jochen Philipp <a2jphilipp@gmail.com>
Thanks for your work on this
Jochen
Am Mo., 30. Nov. 2026 um 08:09 Uhr schrieb Dongjiang Zhu <zhudongjiang@fygo.io>:
>
> Hi Jochen,
>
> Thank you very much for offering to do more testing.
>
> I suspect the issue you encountered is the race addressed by
> patch 4. Would you be willing to test the standalone diff below?
>
> Some of Johannes's recent zoned work overlaps with patches 1 and 2
> and may address the same issues. I may therefore submit patches 3
> and 4 separately, as they address independent problems.
>
> The diff is based on btrfs for-next at 80bef0a:
> https://github.com/btrfs/linux/tree/for-next
>
> I've checked that it builds.
>
> Thanks,
> Dongjiang
>
>
> --------------------- git diff -----------------------------
>
> diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h
> index d567ed822e55..31aca6424d54 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_ZONE_FINISHING,
> };
>
> enum btrfs_caching_type {
> diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
> index 0277b2a48a73..1eabd2a9b882 100644
> --- a/fs/btrfs/zoned.c
> +++ b/fs/btrfs/zoned.c
> @@ -2574,6 +2574,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
> (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
> struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
> enum btrfs_raid_types index;
> + bool bg_ro = false;
> bool zones_full;
> int failed = 0;
> int ret = 0;
> @@ -2585,6 +2586,11 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
> return 0;
> }
>
> + if (test_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING, &block_group->runtime_flags)) {
> + spin_unlock(&block_group->lock);
> + goto wait_finish;
> + }
> +
> /* Check if we have unwritten allocated space */
> if (is_metadata &&
> block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
> @@ -2609,6 +2615,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
> ret = btrfs_inc_block_group_ro(block_group, false);
> if (ret)
> return ret;
> + bg_ro = true;
>
> /* Ensure all writes in this block group finish */
> btrfs_wait_block_group_reservations(block_group);
> @@ -2632,6 +2639,12 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
> return 0;
> }
>
> + if (test_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING,
> + &block_group->runtime_flags)) {
> + spin_unlock(&block_group->lock);
> + goto wait_finish;
> + }
> +
> if (block_group->reserved ||
> test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
> &block_group->runtime_flags)) {
> @@ -2641,7 +2654,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
> }
> }
>
> - clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
> + set_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING, &block_group->runtime_flags);
> /*
> * If the block group is allocated up to the zone capacity,
> * everything is written and the zones should be full already. A
> @@ -2700,8 +2713,13 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
> btrfs_dec_block_group_ro(block_group);
>
> spin_lock(&fs_info->zone_active_bgs_lock);
> + spin_lock(&block_group->lock);
> + clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
> ASSERT(!list_empty(&block_group->active_bg_list));
> list_del_init(&block_group->active_bg_list);
> + clear_and_wake_up_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING,
> + &block_group->runtime_flags);
> + spin_unlock(&block_group->lock);
> spin_unlock(&fs_info->zone_active_bgs_lock);
>
> /* For active_bg_list */
> @@ -2710,6 +2728,15 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
> clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
>
> return ret;
> +
> +wait_finish:
> + wait_on_bit_io(&block_group->runtime_flags,
> + BLOCK_GROUP_FLAG_ZONE_FINISHING,
> + TASK_UNINTERRUPTIBLE);
> + if (bg_ro)
> + btrfs_dec_block_group_ro(block_group);
> +
> + return 0;
> }
>
> int btrfs_zone_finish(struct btrfs_block_group *block_group)
^ permalink raw reply [flat|nested] 17+ messages in thread* Re: [PATCH v4 0/4] btrfs: zoned: fix active-zone accounting and transitions
2026-11-30 15:00 ` Jochen Philipp
@ 2026-12-02 10:11 ` Dongjiang Zhu
0 siblings, 0 replies; 17+ messages in thread
From: Dongjiang Zhu @ 2026-12-02 10:11 UTC (permalink / raw)
To: Jochen Philipp; +Cc: linux-btrfs, johannes.thumshirn, naohiro.aota
在 2026/11/30 23:00, Jochen Philipp 写道:
> Hi Dongjiang,
>
> I tested your standalone patch 4 diff on btrfs for-next 80bef0a
> (7.4.0-rc5-p4+, untainted). Note: the diff came through with
> tabs converted to spaces, so I had to apply it with
> git apply --ignore-whitespace.
>
> Results:
> - after reboot into this kernel and mount: 126 closed zones
> (all previously open zones had become closed by the power cycle)
> - copied ~317 GB via rsync from a USB disk, no btrfs messages
> - afterwards: 5 closed + 121 implicitly open = 126 active zones,
> i.e. it stayed below max_open_zones=128 and kept working
> - umount, btrfs check --readonly: no error found
> - mounted again, stays rw
>
> One correction to my earlier reports: I only counted closed
> zones back then, not open ones. So "closed zones dropped from 124
> to 5" most likely meant the zones went from closed to open again,
> not that they were finished. Sorry for the confusion.
>
> The fs is now at ~88% full, so I can't do many more large write
> tests on it.
>
> If it helps:
> Tested-by: Jochen Philipp <a2jphilipp@gmail.com>
>
> Thanks for your work on this
> Jochen
>
Hi Jochen,
Many thanks for testing this and for the detailed results!
I'm considering submitting patches 3 and 4 separately from the
rest of the series. I'll prepare and send them out soon.
Thanks,
Dongjiang
> Am Mo., 30. Nov. 2026 um 08:09 Uhr schrieb Dongjiang Zhu <zhudongjiang@fygo.io>:
>>
>> Hi Jochen,
>>
>> Thank you very much for offering to do more testing.
>>
>> I suspect the issue you encountered is the race addressed by
>> patch 4. Would you be willing to test the standalone diff below?
>>
>> Some of Johannes's recent zoned work overlaps with patches 1 and 2
>> and may address the same issues. I may therefore submit patches 3
>> and 4 separately, as they address independent problems.
>>
>> The diff is based on btrfs for-next at 80bef0a:
>> https://github.com/btrfs/linux/tree/for-next
>>
>> I've checked that it builds.
>>
>> Thanks,
>> Dongjiang
>>
>>
>> --------------------- git diff -----------------------------
>>
>> diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h
>> index d567ed822e55..31aca6424d54 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_ZONE_FINISHING,
>> };
>>
>> enum btrfs_caching_type {
>> diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
>> index 0277b2a48a73..1eabd2a9b882 100644
>> --- a/fs/btrfs/zoned.c
>> +++ b/fs/btrfs/zoned.c
>> @@ -2574,6 +2574,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
>> (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
>> struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
>> enum btrfs_raid_types index;
>> + bool bg_ro = false;
>> bool zones_full;
>> int failed = 0;
>> int ret = 0;
>> @@ -2585,6 +2586,11 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
>> return 0;
>> }
>>
>> + if (test_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING, &block_group->runtime_flags)) {
>> + spin_unlock(&block_group->lock);
>> + goto wait_finish;
>> + }
>> +
>> /* Check if we have unwritten allocated space */
>> if (is_metadata &&
>> block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
>> @@ -2609,6 +2615,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
>> ret = btrfs_inc_block_group_ro(block_group, false);
>> if (ret)
>> return ret;
>> + bg_ro = true;
>>
>> /* Ensure all writes in this block group finish */
>> btrfs_wait_block_group_reservations(block_group);
>> @@ -2632,6 +2639,12 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
>> return 0;
>> }
>>
>> + if (test_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING,
>> + &block_group->runtime_flags)) {
>> + spin_unlock(&block_group->lock);
>> + goto wait_finish;
>> + }
>> +
>> if (block_group->reserved ||
>> test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
>> &block_group->runtime_flags)) {
>> @@ -2641,7 +2654,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
>> }
>> }
>>
>> - clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
>> + set_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING, &block_group->runtime_flags);
>> /*
>> * If the block group is allocated up to the zone capacity,
>> * everything is written and the zones should be full already. A
>> @@ -2700,8 +2713,13 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
>> btrfs_dec_block_group_ro(block_group);
>>
>> spin_lock(&fs_info->zone_active_bgs_lock);
>> + spin_lock(&block_group->lock);
>> + clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
>> ASSERT(!list_empty(&block_group->active_bg_list));
>> list_del_init(&block_group->active_bg_list);
>> + clear_and_wake_up_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING,
>> + &block_group->runtime_flags);
>> + spin_unlock(&block_group->lock);
>> spin_unlock(&fs_info->zone_active_bgs_lock);
>>
>> /* For active_bg_list */
>> @@ -2710,6 +2728,15 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
>> clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
>>
>> return ret;
>> +
>> +wait_finish:
>> + wait_on_bit_io(&block_group->runtime_flags,
>> + BLOCK_GROUP_FLAG_ZONE_FINISHING,
>> + TASK_UNINTERRUPTIBLE);
>> + if (bg_ro)
>> + btrfs_dec_block_group_ro(block_group);
>> +
>> + return 0;
>> }
>>
>> int btrfs_zone_finish(struct btrfs_block_group *block_group)
^ permalink raw reply [flat|nested] 17+ messages in thread