Skip to contentSkip to conversation

btrfs: zoned: reserve data_reloc block group on mount

PATCH Superseded Johannes Thumshirn 3 participants 4 messages Jun 2, 2025 Btrfs file system

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 btrfs: zoned: reserve data_reloc block group on mount

From: Johannes Thumshirn <[email protected]>

Create a block group dedicated for data relocation on mount of a zoned filesystem.

If there is already more than one empty DATA block group on mount, this one is picked for the data relocation block group, instead of a newly created one.

This is done to ensure, there is always space for performing garbage collection and the filesystem is not hitting ENOSPC under heavy overwrite workloads.

---
Changes to v2:
- Don't take references
- Use btrfs_chunk_alloc()
- Don't abort the transaction
- Decrease indendation

 fs/btrfs/disk-io.c |  1 +
 fs/btrfs/zoned.c   | 71 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/zoned.h   |  3 ++
 3 files changed, 75 insertions(+)
fs/btrfs/disk-io.c +1 -0
index 3def93016963..b211dc8cdb86 100644
@@ -3562,6 +3562,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
 		goto fail_sysfs;
 	}
 
+	btrfs_zoned_reserve_data_reloc_bg(fs_info);
 	btrfs_free_zone_cache(fs_info);
 
 	btrfs_check_active_zone_reservation(fs_info);
fs/btrfs/zoned.c +71 -0

83 lines not on this page · raw message

fs/btrfs/zoned.h +3 -0
index 9672bf4c3335..6e11533b8e14 100644
@@ -88,6 +88,7 @@ void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical,
 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_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,
@@ -241,6 +242,8 @@ static inline void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
 
 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_free_zone_cache(struct btrfs_fs_info *fs_info) { }
 
 static inline bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info *fs_info)
Signature · 1 line
2.49.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 111 lines · Johannes Thumshirn

From: Johannes Thumshirn <[email protected]>

Create a block group dedicated for data relocation on mount of a zoned filesystem.

If there is already more than one empty DATA block group on mount, this one is picked for the data relocation block group, instead of a newly created one.

This is done to ensure, there is always space for performing garbage collection and the filesystem is not hitting ENOSPC under heavy overwrite workloads.

---
Changes to v2:
- Don't take references
- Use btrfs_chunk_alloc()
- Don't abort the transaction
- Decrease indendation

 fs/btrfs/disk-io.c |  1 +
 fs/btrfs/zoned.c   | 71 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/zoned.h   |  3 ++
 3 files changed, 75 insertions(+)
fs/btrfs/disk-io.c +1 -0
index 3def93016963..b211dc8cdb86 100644
@@ -3562,6 +3562,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
                goto fail_sysfs;
        }
 
+       btrfs_zoned_reserve_data_reloc_bg(fs_info);
        btrfs_free_zone_cache(fs_info);
 
        btrfs_check_active_zone_reservation(fs_info);
fs/btrfs/zoned.c +56 -0
index 19710634d63f..663cd194522b 100644
@@ -17,6 +17,7 @@
 #include "fs.h"
 #include "accessors.h"
 #include "bio.h"
+#include "transaction.h"
 
 /* Maximum number of zones to report per blkdev_report_zones() call */
 #define BTRFS_REPORT_NR_ZONES   4096
@@ -2443,6 +2444,76 @@ void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
        spin_unlock(&fs_info->relocation_bg_lock);
 }
 
+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->sub_group[0];
+       struct btrfs_trans_handle *trans;
+       struct btrfs_block_group *bg;
+       u64 alloc_flags;
+       bool initial = false;
+       int index;
+       int ret;
+
+       if (!btrfs_is_zoned(fs_info))
+               return;
+
+       if (fs_info->data_reloc_bg)
+               return;
+
+       if (sb_rdonly(fs_info->sb))
+               return;
+
+       ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC);
+       alloc_flags = btrfs_get_alloc_profile(fs_info, space_info->flags);
+       index = btrfs_bg_flags_to_raid_index(alloc_flags);
+
+       list_for_each_entry(bg, &data_sinfo->block_groups[index], list) {
+               if (bg->used > 0)
+                       continue;
+
+               if (!initial) {
+                       initial = true;
+                       continue;
+               }
+
+               fs_info->data_reloc_bg = bg->start;
+               set_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
+                       &bg->runtime_flags);
+               btrfs_zone_activate(bg);
+
+               return;
+       }
+
+       trans = btrfs_join_transaction(fs_info->tree_root);
+       if (IS_ERR(trans))
+               return;
+
+       ret = btrfs_chunk_alloc(trans, space_info, alloc_flags, CHUNK_ALLOC_FORCE);
+       if (ret != 1)
+               goto out;
+
+       list_for_each_entry(bg, &space_info->block_groups[index], list) {
+               if (bg->used > 0)
+                       continue;
+
+               if (!initial) {
+                       initial = true;

fs/btrfs/zoned.c void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) · line 2511

Quoted 10 lines
fs/btrfs/zoned.c +10 -0
+                       continue;
+               }
+
+               fs_info->data_reloc_bg = bg->start;
+               set_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
+                       &bg->runtime_flags);
+               btrfs_zone_activate(bg);
+
+               break;
+       }

Instead of duplicating the iteration code, we could use a label and a boolean to track if we already allocated a chunk, something like this:

https://pastebin.com/raw/jrFtUVFj

Otherwise it looks good to me.
Thanks.

Quoted 33 lines
fs/btrfs/zoned.c +5 -0
+
+out:
+       btrfs_end_transaction(trans);
+}
+
 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
 {
        struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
fs/btrfs/zoned.h +3 -1
index 9672bf4c3335..6e11533b8e14 100644
@@ -88,6 +88,7 @@ void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical,
 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_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,
@@ -241,6 +242,8 @@ static inline void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
 
 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_free_zone_cache(struct btrfs_fs_info *fs_info) { }
 
 static inline bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info *fs_info)
--

2.49.0

#2 lore raw
Quoted 4 lines · Filipe Manana

Instead of duplicating the iteration code, we could use a label and a boolean to track if we already allocated a chunk, something like this:

https://pastebin.com/raw/jrFtUVFj

Not quite, the 1st one iterates over data_sinfo the 2nd over space_info which is data_sinfo->sub_spaceinfo[0].

I could've made a helper function, but that seemed a bit excessive for this.

Note: locally I've deleted the line breaks for the set_bit() calls as well. They fit in 80 chars now.

Thanks,
Johannes

#3 lore raw
Quoted 11 lines · Johannes Thumshirn
Quoted 4 lines · Filipe Manana

Instead of duplicating the iteration code, we could use a label and a boolean to track if we already allocated a chunk, something like this:

https://pastebin.com/raw/jrFtUVFj

Not quite, the 1st one iterates over data_sinfo the 2nd over space_info which is data_sinfo->sub_spaceinfo[0].

I could've made a helper function, but that seemed a bit excessive for this.

There's no need for that, the again label approach still works if we just used a local list_head pointer variable and use it in the list_for_each_entry() call, like this:

https://pastebin.com/raw/Aq2SVB4W

Quoted 3 lines

Note: locally I've deleted the line breaks for the set_bit() calls as well. They fit in 80 chars now.

Nice.

Thanks.

Quoted 3 lines

Thanks,
Johannes