btrfs: store chunk size in space-info struct
authorStefan Roesch <shr@fb.com>
Tue, 8 Feb 2022 19:31:20 +0000 (11:31 -0800)
committerDavid Sterba <dsterba@suse.com>
Mon, 25 Jul 2022 15:45:32 +0000 (17:45 +0200)
The chunk size is stored in the btrfs_space_info structure.  It is
initialized at the start and is then used.

A new API is added to update the current chunk size.  This API is used
to be able to expose the chunk_size as a sysfs setting.

Signed-off-by: Stefan Roesch <shr@fb.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ rename and merge helpers, switch atomic type to u64, style fixes ]
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/space-info.c
fs/btrfs/space-info.h
fs/btrfs/volumes.c

index 2cf8da1116eb280b4fc560b0cadd053626b9ebf7..62d25112310d96f426c76cc61d1fd69bd52de5b7 100644 (file)
@@ -187,6 +187,37 @@ void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
  */
 #define BTRFS_DEFAULT_ZONED_RECLAIM_THRESH                     (75)
 
+/*
+ * Calculate chunk size depending on volume type (regular or zoned).
+ */
+static u64 calc_chunk_size(const struct btrfs_fs_info *fs_info, u64 flags)
+{
+       if (btrfs_is_zoned(fs_info))
+               return fs_info->zone_size;
+
+       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 (fs_info->fs_devices->total_rw_bytes > 50ULL * SZ_1G)
+               return SZ_1G;
+
+       return SZ_256M;
+}
+
+/*
+ * Update default chunk size.
+ */
+void btrfs_update_space_info_chunk_size(struct btrfs_space_info *space_info,
+                                       u64 chunk_size)
+{
+       WRITE_ONCE(space_info->chunk_size, chunk_size);
+}
+
 static int create_space_info(struct btrfs_fs_info *info, u64 flags)
 {
 
@@ -208,6 +239,7 @@ static int create_space_info(struct btrfs_fs_info *info, u64 flags)
        INIT_LIST_HEAD(&space_info->tickets);
        INIT_LIST_HEAD(&space_info->priority_tickets);
        space_info->clamp = 1;
+       btrfs_update_space_info_chunk_size(space_info, calc_chunk_size(info, flags));
 
        if (btrfs_is_zoned(info))
                space_info->bg_reclaim_threshold = BTRFS_DEFAULT_ZONED_RECLAIM_THRESH;
index c096695598c1290a228e38c8845d21ec6cad05e3..e7de24a529cfb50534dcaaa4776f09ac32aaaa84 100644 (file)
@@ -25,6 +25,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. */
+       /* Chunk size in bytes */
+       u64 chunk_size;
 
        /*
         * Once a block group drops below this threshold (percents) we'll
@@ -123,6 +125,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_chunk_size(struct btrfs_space_info *space_info,
+                                       u64 chunk_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,
index a2bb0928dc066fa92a8112ac91378b9f208beb15..b8e40d55012f0b8c5439cf3a3f102101819a6d61 100644 (file)
@@ -5071,26 +5071,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_chunk_size = READ_ONCE(space_info->chunk_size);
+       ctl->max_stripe_size = ctl->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),