[PATCH v2 1/4] btrfs: store stripe size and chunk size in space-info struct.
From: Stefan Roesch <hidden>
Date: 2021-10-27 20:16:30
Subsystem:
btrfs file system, filesystems (vfs and infrastructure), the rest · Maintainers:
Chris Mason, David Sterba, Alexander Viro, Christian Brauner, Linus Torvalds
The stripe size and chunk size are stored in the btrfs_space_info structure. They are initialized at the start and are then used. Two api's are added to get the current value and one to update the value. These api's will be used to be able to expose the stripe_size as a sysfs setting. Signed-off-by: Stefan Roesch <redacted> --- fs/btrfs/space-info.c | 72 +++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/space-info.h | 4 +++ fs/btrfs/volumes.c | 28 ++++++----------- 3 files changed, 85 insertions(+), 19 deletions(-)
diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c
index 48d77f360a24..570acfebeae4 100644
--- a/fs/btrfs/space-info.c
+++ b/fs/btrfs/space-info.c@@ -181,6 +181,74 @@ void btrfs_clear_space_info_full(struct btrfs_fs_info *info) found->full = 0; } +/* + * Compute stripe size depending on block type for regular volumes. + */ +static u64 compute_stripe_size_regular(struct btrfs_fs_info *info, u64 flags) +{ + ASSERT(flags & BTRFS_BLOCK_GROUP_TYPE_MASK); + + if (flags & BTRFS_BLOCK_GROUP_DATA) + return SZ_1G; + else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) + return SZ_32M; + + /* Handle BTRFS_BLOCK_GROUP_METADATA */ + if (info->fs_devices->total_rw_bytes > 50ULL * SZ_1G) + return SZ_1G; + + return SZ_256M; +} + +/* + * Compute stripe size for zoned volumes. + */ +static u64 compute_stripe_size_zoned(struct btrfs_fs_info *info) +{ + return info->zone_size; +} + +/* + * Compute stripe size depending on volume type. + */ +static u64 compute_stripe_size(struct btrfs_fs_info *info, u64 flags) +{ + if (btrfs_is_zoned(info)) + return compute_stripe_size_zoned(info); + + return compute_stripe_size_regular(info, flags); +} + +/* + * Compute chunk size depending on block type and stripe size. + */ +static u64 compute_chunk_size(u64 flags, u64 max_stripe_size) +{ + ASSERT(flags & BTRFS_BLOCK_GROUP_TYPE_MASK); + + if (flags & BTRFS_BLOCK_GROUP_DATA) + return BTRFS_MAX_DATA_CHUNK_SIZE; + else if (flags & BTRFS_BLOCK_GROUP_METADATA) + return max_stripe_size; + + /* Handle BTRFS_BLOCK_GROUP_SYSTEM */ + return 2 * max_stripe_size; +} + +/* + * Update maximum stripe size and chunk size. + * + */ +void btrfs_update_space_info_max_alloc_sizes(struct btrfs_space_info *space_info, + u64 flags, u64 max_stripe_size) +{ + spin_lock(&space_info->lock); + space_info->max_stripe_size = max_stripe_size; + space_info->max_chunk_size = compute_chunk_size(flags, + space_info->max_stripe_size); + spin_unlock(&space_info->lock); +} + static int create_space_info(struct btrfs_fs_info *info, u64 flags) {
@@ -203,6 +271,10 @@ static int create_space_info(struct btrfs_fs_info *info, u64 flags) INIT_LIST_HEAD(&space_info->priority_tickets); space_info->clamp = 1; + space_info->max_stripe_size = compute_stripe_size(info, flags); + space_info->max_chunk_size = compute_chunk_size(flags, + space_info->max_stripe_size); + ret = btrfs_sysfs_add_space_info_type(info, space_info); if (ret) return ret;
diff --git a/fs/btrfs/space-info.h b/fs/btrfs/space-info.h
index cb5056472e79..5ee3e381de38 100644
--- a/fs/btrfs/space-info.h
+++ b/fs/btrfs/space-info.h@@ -23,6 +23,8 @@ struct btrfs_space_info { u64 max_extent_size; /* This will hold the maximum extent size of the space info if we had an ENOSPC in the allocator. */ + u64 max_chunk_size; /* maximum chunk size in bytes */ + u64 max_stripe_size; /* maximum stripe size in bytes */ int clamp; /* Used to scale our threshold for preemptive flushing. The value is >> clamp, so turns
@@ -115,6 +117,8 @@ void btrfs_update_space_info(struct btrfs_fs_info *info, u64 flags, u64 total_bytes, u64 bytes_used, u64 bytes_readonly, u64 bytes_zone_unusable, struct btrfs_space_info **space_info); +void btrfs_update_space_info_max_alloc_sizes(struct btrfs_space_info *space_info, + u64 flags, u64 max_stripe_size); struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info, u64 flags); u64 __pure btrfs_space_info_used(struct btrfs_space_info *s_info,
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 6031e2f4c6bc..6408592d4461 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c@@ -5002,26 +5002,16 @@ static void init_alloc_chunk_ctl_policy_regular( struct btrfs_fs_devices *fs_devices, struct alloc_chunk_ctl *ctl) { - u64 type = ctl->type; + struct btrfs_space_info *space_info; - if (type & BTRFS_BLOCK_GROUP_DATA) { - ctl->max_stripe_size = SZ_1G; - ctl->max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE; - } else if (type & BTRFS_BLOCK_GROUP_METADATA) { - /* For larger filesystems, use larger metadata chunks */ - if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) - ctl->max_stripe_size = SZ_1G; - else - ctl->max_stripe_size = SZ_256M; - ctl->max_chunk_size = ctl->max_stripe_size; - } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { - ctl->max_stripe_size = SZ_32M; - ctl->max_chunk_size = 2 * ctl->max_stripe_size; - ctl->devs_max = min_t(int, ctl->devs_max, - BTRFS_MAX_DEVS_SYS_CHUNK); - } else { - BUG(); - } + space_info = btrfs_find_space_info(fs_devices->fs_info, ctl->type); + ASSERT(space_info); + + ctl->max_stripe_size = space_info->max_stripe_size; + ctl->max_chunk_size = space_info->max_chunk_size; + + if (ctl->type & BTRFS_BLOCK_GROUP_SYSTEM) + ctl->devs_max = min_t(int, ctl->devs_max, BTRFS_MAX_DEVS_SYS_CHUNK); /* We don't want a chunk larger than 10% of writable space */ ctl->max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
--
2.30.2