]> git.hungrycats.org Git - linux/commitdiff
btrfs: add a new 'stripe_len' parameter to btrfs_stripe_nr_to_offset()
authorQu Wenruo <wqu@suse.com>
Wed, 21 Oct 2026 03:39:39 +0000 (14:09 +1030)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:18 +0000 (21:36 +0100)
That helper is currently using a fixed BTRFS_STRIPE_LEN, change it to
accept a 'u32 stripe_len'.

Also add an ASSERT() to make sure the @stripe_len passed in is power of
2, and no larger than BTRFS_STRIPE_LEN.

There is a special case where we still pass BTRFS_STRIPE_LEN into
btrfs_stripe_nr_to_offset(), that's for chunk allocation to determine
the minimal device extent size.

In that case, since our future planned map->stripe_len is always power
of 2 and no larger than BTRFS_STRIPE_LEN, we are totally fine to use the
old BTRFS_STRIPE_LEN.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/block-group.c
fs/btrfs/scrub.c
fs/btrfs/volumes.c
fs/btrfs/volumes.h
fs/btrfs/zoned.c

index f1c663e81d9d4310f026e108ecd797916576c688..45d99669c5591943fb56feb68b2fb58aa9f4dbbf 100644 (file)
@@ -2302,7 +2302,8 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
 
        /* For RAID5/6 adjust to a full IO stripe length */
        if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
-               io_stripe_size = btrfs_stripe_nr_to_offset(nr_data_stripes(map));
+               io_stripe_size = btrfs_stripe_nr_to_offset(nr_data_stripes(map),
+                                                          map->stripe_len);
 
        buf = kzalloc_objs(u64, map->num_stripes, GFP_NOFS);
        if (!buf) {
index c3387e10977936bdd9eca13d148a28aeb048c1d6..98362c91bce43729aa6967b8edcff7a58b37748e 100644 (file)
@@ -1425,7 +1425,8 @@ static int get_raid56_logic_offset(u64 physical, int num,
                u32 stripe_index;
                u32 rot;
 
-               *offset = last_offset + btrfs_stripe_nr_to_offset(i);
+               *offset = last_offset + btrfs_stripe_nr_to_offset(i,
+                                                       map->stripe_len);
 
                stripe_nr = (u32)(*offset >> map->stripe_shift) / data_stripes;
 
@@ -1439,7 +1440,7 @@ static int get_raid56_logic_offset(u64 physical, int num,
                if (stripe_index < num)
                        j++;
        }
-       *offset = last_offset + btrfs_stripe_nr_to_offset(j);
+       *offset = last_offset + btrfs_stripe_nr_to_offset(j, map->stripe_len);
        return 1;
 }
 
@@ -1924,7 +1925,8 @@ static void submit_initial_group_read(struct scrub_ctx *sctx,
        ASSERT(first_slot + nr_stripes <= SCRUB_TOTAL_STRIPES);
 
        scrub_throttle_dev_io(sctx, sctx->stripes[0].dev,
-                             btrfs_stripe_nr_to_offset(nr_stripes));
+                             btrfs_stripe_nr_to_offset(nr_stripes,
+                                               sctx->stripes[0].stripe_len));
        blk_start_plug(&plug);
        for (int i = 0; i < nr_stripes; i++) {
                struct scrub_stripe *stripe = &sctx->stripes[first_slot + i];
@@ -2102,7 +2104,7 @@ static int scrub_raid56_cached_parity(struct scrub_ctx *sctx,
        struct btrfs_raid_bio *rbio;
        struct bio bio;
        const int data_stripes = nr_data_stripes(map);
-       u64 length = btrfs_stripe_nr_to_offset(data_stripes);
+       u64 length = btrfs_stripe_nr_to_offset(data_stripes, map->stripe_len);
        int ret;
 
        bio_init(&bio, NULL, NULL, 0, REQ_OP_READ);
@@ -2151,6 +2153,7 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
        BTRFS_PATH_AUTO_RELEASE(csum_path);
        struct scrub_stripe *stripe;
        bool all_empty = true;
+       const u32 stripe_len = map->stripe_len;
        const int data_stripes = nr_data_stripes(map);
        unsigned long extent_bitmap = 0;
        int ret;
@@ -2191,14 +2194,15 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
                              data_stripes) >> map->stripe_shift;
                stripe_index = (i + rot) % map->num_stripes;
                physical = map->stripes[stripe_index].physical +
-                          btrfs_stripe_nr_to_offset(rot);
+                          btrfs_stripe_nr_to_offset(rot, stripe_len);
 
                scrub_reset_stripe(stripe);
                set_bit(SCRUB_STRIPE_FLAG_NO_REPORT, &stripe->state);
                ret = scrub_find_fill_first_stripe(bg, &extent_path, &csum_path,
                                map->stripes[stripe_index].dev, physical, 1,
-                               full_stripe_start + btrfs_stripe_nr_to_offset(i),
-                               map->stripe_len, stripe);
+                               full_stripe_start +
+                               btrfs_stripe_nr_to_offset(i, stripe_len),
+                               stripe_len, stripe);
                if (ret < 0)
                        return ret;
                /*
@@ -2207,7 +2211,7 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
                 */
                if (ret > 0) {
                        stripe->logical = full_stripe_start +
-                                         btrfs_stripe_nr_to_offset(i);
+                                         btrfs_stripe_nr_to_offset(i, stripe_len);
                        stripe->dev = map->stripes[stripe_index].dev;
                        stripe->mirror_num = 1;
                        set_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state);
@@ -2348,7 +2352,8 @@ static u64 simple_stripe_full_stripe_len(const struct btrfs_chunk_map *map)
        ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
                            BTRFS_BLOCK_GROUP_RAID10));
 
-       return btrfs_stripe_nr_to_offset(map->num_stripes / map->sub_stripes);
+       return btrfs_stripe_nr_to_offset(map->num_stripes / map->sub_stripes,
+                                        map->stripe_len);
 }
 
 /* Get the logical bytenr for the stripe */
@@ -2364,8 +2369,8 @@ static u64 simple_stripe_get_logical(struct btrfs_chunk_map *map,
         * (stripe_index / sub_stripes) gives how many data stripes we need to
         * skip.
         */
-       return btrfs_stripe_nr_to_offset(stripe_index / map->sub_stripes) +
-              bg->start;
+       return btrfs_stripe_nr_to_offset(stripe_index / map->sub_stripes,
+                                        map->stripe_len) + bg->start;
 }
 
 /* Get the mirror number for the stripe */
@@ -2504,7 +2509,8 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
        }
        if (profile & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
                ret = scrub_simple_stripe(sctx, bg, map, scrub_dev, stripe_index);
-               offset = btrfs_stripe_nr_to_offset(stripe_index / map->sub_stripes);
+               offset = btrfs_stripe_nr_to_offset(stripe_index / map->sub_stripes,
+                                                  stripe_len);
                goto out;
        }
 
@@ -2519,7 +2525,7 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
 
        /* Initialize @offset in case we need to go to out: label */
        get_raid56_logic_offset(physical, stripe_index, map, &offset, NULL);
-       increment = btrfs_stripe_nr_to_offset(nr_data_stripes(map));
+       increment = btrfs_stripe_nr_to_offset(nr_data_stripes(map), stripe_len);
 
        /*
         * Due to the rotation, for RAID56 it's better to iterate each stripe
index ff7189ac1c49112a6dbd0aab96b3139f3a873ef3..6a3ae8a081fee54c6bf440e930c783b22962a80a 100644 (file)
@@ -5823,7 +5823,8 @@ static void init_alloc_chunk_ctl_policy_regular(
        /* We don't want a chunk larger than 10% of writable space */
        ctl->max_chunk_size = min(mult_perc(fs_devices->total_rw_bytes, 10),
                                  ctl->max_chunk_size);
-       ctl->dev_extent_min = btrfs_stripe_nr_to_offset(ctl->dev_stripes);
+       ctl->dev_extent_min = btrfs_stripe_nr_to_offset(ctl->dev_stripes,
+                                                       BTRFS_STRIPE_LEN);
 }
 
 static void init_alloc_chunk_ctl_policy_zoned(
@@ -6587,7 +6588,8 @@ unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
 
        if (!WARN_ON(IS_ERR(map))) {
                if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
-                       len = btrfs_stripe_nr_to_offset(nr_data_stripes(map));
+                       len = btrfs_stripe_nr_to_offset(nr_data_stripes(map),
+                                                       map->stripe_len);
                btrfs_free_chunk_map(map);
        }
        return len;
@@ -6784,6 +6786,7 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
        u64 stripe_end_offset;
        u64 stripe_offset;
        u32 stripe_index;
+       u32 stripe_len;
        u32 factor = 0;
        u32 sub_stripes = 0;
        u32 stripes_per_dev = 0;
@@ -6820,6 +6823,7 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
                goto out_free_map;
        }
 
+       stripe_len = map->stripe_len;
        offset = logical - map->start;
        length = min_t(u64, map->start + map->chunk_len - logical, length);
        *length_ret = length;
@@ -6831,12 +6835,13 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
        stripe_nr = offset >> BTRFS_STRIPE_LEN_SHIFT;
 
        /* stripe_offset is the offset of this block in its stripe */
-       stripe_offset = offset - btrfs_stripe_nr_to_offset(stripe_nr);
+       stripe_offset = offset - btrfs_stripe_nr_to_offset(stripe_nr,
+                                                          stripe_len);
 
        stripe_nr_end = round_up(offset + length, BTRFS_STRIPE_LEN) >>
                        BTRFS_STRIPE_LEN_SHIFT;
        stripe_cnt = stripe_nr_end - stripe_nr;
-       stripe_end_offset = btrfs_stripe_nr_to_offset(stripe_nr_end) -
+       stripe_end_offset = btrfs_stripe_nr_to_offset(stripe_nr_end, stripe_len) -
                            (offset + length);
        /*
         * after this, stripe_nr is the number of stripes on this
@@ -6879,12 +6884,14 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
        for (i = 0; i < *num_stripes; i++) {
                stripes[i].physical =
                        map->stripes[stripe_index].physical +
-                       stripe_offset + btrfs_stripe_nr_to_offset(stripe_nr);
+                       stripe_offset + btrfs_stripe_nr_to_offset(stripe_nr,
+                                                                 stripe_len);
                stripes[i].dev = map->stripes[stripe_index].dev;
 
                if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
                                 BTRFS_BLOCK_GROUP_RAID10)) {
-                       stripes[i].length = btrfs_stripe_nr_to_offset(stripes_per_dev);
+                       stripes[i].length = btrfs_stripe_nr_to_offset(stripes_per_dev,
+                                                                     stripe_len);
 
                        if (i / sub_stripes < remaining_stripes)
                                stripes[i].length += BTRFS_STRIPE_LEN;
@@ -7022,6 +7029,8 @@ static void handle_ops_on_dev_replace(struct btrfs_io_context *bioc,
 static u64 btrfs_max_io_len(struct btrfs_chunk_map *map, u64 offset,
                            struct btrfs_io_geometry *io_geom)
 {
+       const u32 stripe_len = map->stripe_len;
+
        /*
         * Stripe_nr is the stripe where this block falls.  stripe_offset is
         * the offset of this block in its stripe.
@@ -7033,7 +7042,8 @@ static u64 btrfs_max_io_len(struct btrfs_chunk_map *map, u64 offset,
 
        if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
                unsigned long full_stripe_len =
-                       btrfs_stripe_nr_to_offset(nr_data_stripes(map));
+                       btrfs_stripe_nr_to_offset(nr_data_stripes(map),
+                                                 stripe_len);
 
                /*
                 * For full stripe start, we use previously calculated
@@ -7045,7 +7055,8 @@ static u64 btrfs_max_io_len(struct btrfs_chunk_map *map, u64 offset,
                 * not ensured to be power of 2.
                 */
                io_geom->raid56_full_stripe_start = btrfs_stripe_nr_to_offset(
-                       rounddown(io_geom->stripe_nr, nr_data_stripes(map)));
+                       rounddown(io_geom->stripe_nr, nr_data_stripes(map)),
+                       stripe_len);
 
                ASSERT(io_geom->raid56_full_stripe_start + full_stripe_len > offset,
                       "raid56_full_stripe_start=%llu full_stripe_len=%lu offset=%llu",
@@ -7084,7 +7095,8 @@ static int set_io_stripe(struct btrfs_fs_info *fs_info, u64 logical,
 
        dst->physical = map->stripes[io_geom->stripe_index].physical +
                        io_geom->stripe_offset +
-                       btrfs_stripe_nr_to_offset(io_geom->stripe_nr);
+                       btrfs_stripe_nr_to_offset(io_geom->stripe_nr,
+                                                 map->stripe_len);
        return 0;
 }
 
@@ -7207,7 +7219,7 @@ static void map_blocks_raid56_write(struct btrfs_chunk_map *map,
        /* Return the length to the full stripe end. */
        *length = min(logical + *length,
                      io_geom->raid56_full_stripe_start + map->start +
-                     btrfs_stripe_nr_to_offset(data_stripes)) -
+                     btrfs_stripe_nr_to_offset(data_stripes, map->stripe_len)) -
                logical;
        io_geom->stripe_index = 0;
        io_geom->stripe_offset = 0;
@@ -7434,7 +7446,8 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
                 */
                bioc->full_stripe_logical = map->start +
                        btrfs_stripe_nr_to_offset(io_geom.stripe_nr *
-                                                 nr_data_stripes(map));
+                                                 nr_data_stripes(map),
+                                                 map->stripe_len);
                for (int i = 0; i < io_geom.num_stripes; i++) {
                        struct btrfs_io_stripe *dst = &bioc->stripes[i];
                        u32 stripe_index;
@@ -7444,7 +7457,8 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
                        dst->physical =
                                map->stripes[stripe_index].physical +
                                io_geom.stripe_offset +
-                               btrfs_stripe_nr_to_offset(io_geom.stripe_nr);
+                               btrfs_stripe_nr_to_offset(io_geom.stripe_nr,
+                                                         map->stripe_len);
                }
        } else {
                /*
@@ -9070,7 +9084,7 @@ static void map_raid56_repair_block(struct btrfs_io_context *bioc,
 
        for (i = 0; i < data_stripes; i++) {
                u64 stripe_start = bioc->full_stripe_logical +
-                                  btrfs_stripe_nr_to_offset(i);
+                                  btrfs_stripe_nr_to_offset(i, bioc->stripe_len);
 
                if (logical >= stripe_start &&
                    logical < stripe_start + BTRFS_STRIPE_LEN)
index 9356cec00a90079091f67d2fabcf5b49366b895b..75b1179776c80a841967d2dba30656abe7316b68 100644 (file)
@@ -722,9 +722,10 @@ static inline unsigned long btrfs_chunk_item_size(int num_stripes)
  * @stripe_nr is u32, with left shift it can overflow u32 for chunks larger
  * than 4G.  This does the proper type cast to avoid overflow.
  */
-static inline u64 btrfs_stripe_nr_to_offset(u32 stripe_nr)
+static inline u64 btrfs_stripe_nr_to_offset(u32 stripe_nr, u32 stripe_len)
 {
-       return (u64)stripe_nr << BTRFS_STRIPE_LEN_SHIFT;
+       ASSERT(is_power_of_2(stripe_len) && stripe_len <= BTRFS_STRIPE_LEN);
+       return (u64)stripe_nr << ilog2(stripe_len);
 }
 
 void btrfs_get_bioc(struct btrfs_io_context *bioc);
index c0bd5e0868867f01d51be558956fd26dc9818af9..53c3768c682c5829681b278efdf220b753bfe021 100644 (file)
@@ -1638,7 +1638,9 @@ static int btrfs_load_block_group_raid0(struct btrfs_block_group *bg,
 
                if (zone_info[i].alloc_offset == WP_CONVENTIONAL) {
                        has_conventional = true;
-                       zone_info[i].alloc_offset = btrfs_stripe_nr_to_offset(stripe_nr);
+                       zone_info[i].alloc_offset =
+                               btrfs_stripe_nr_to_offset(stripe_nr,
+                                                         map->stripe_len);
 
                        if (stripe_index > i)
                                zone_info[i].alloc_offset += BTRFS_STRIPE_LEN;
@@ -1780,7 +1782,8 @@ static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg,
 
                if (raid0_allocs[idx] == WP_CONVENTIONAL) {
                        has_conventional = true;
-                       raid0_allocs[idx] = btrfs_stripe_nr_to_offset(stripe_nr);
+                       raid0_allocs[idx] = btrfs_stripe_nr_to_offset(stripe_nr,
+                                                               map->stripe_len);
 
                        if (stripe_index > idx)
                                raid0_allocs[idx] += BTRFS_STRIPE_LEN;