[RFC PATCH 03/10] btrfs: allow remapped chunks to have zero stripes

Mark Harmstone
2025-05-15T16:37:00Z

When a chunk has been fully remapped, we are going to set its
num_stripes to 0, as it will no longer represent a physical location on
disk.

Change tree-checker to allow for this, and fix a couple of
divide-by-zeroes seen elsewhere.

Signed-off-by: Mark Harmstone <maharmstone@fb.com>
---
fs/btrfs/tree-checker.c | 16 +++++++++-------
fs/btrfs/volumes.c | 8 +++++++-
2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index 0505f8d76581..fd83df06e3fb 100644
--- a/fs/btrfs/tree-checker.c
+++ b/fs/btrfs/tree-checker.c
@@ -829,7 +829,7 @@ int btrfs_check_chunk_valid(const struct btrfs_fs_info *fs_info,
u64 type;
u64 features;
u32 chunk_sector_size;
- bool mixed = false;
+ bool mixed = false, remapped;
int raid_index;
int nparity;
int ncopies;
@@ -853,12 +853,14 @@ int btrfs_check_chunk_valid(const struct btrfs_fs_info *fs_info,
ncopies = btrfs_raid_array[raid_index].ncopies;
nparity = btrfs_raid_array[raid_index].nparity;

- if (unlikely(!num_stripes)) {
+ remapped = type & BTRFS_BLOCK_GROUP_REMAPPED;
+
+ if (unlikely(!remapped && !num_stripes)) {
chunk_err(fs_info, leaf, chunk, logical,
"invalid chunk num_stripes, have %u", num_stripes);
return -EUCLEAN;
}
- if (unlikely(num_stripes < ncopies)) {
+ if (unlikely(!remapped && num_stripes < ncopies)) {
chunk_err(fs_info, leaf, chunk, logical,
"invalid chunk num_stripes < ncopies, have %u < %d",
num_stripes, ncopies);
@@ -960,7 +962,7 @@ int btrfs_check_chunk_valid(const struct btrfs_fs_info *fs_info,
}
}

- if (unlikely((type & BTRFS_BLOCK_GROUP_RAID10 &&
+ if (unlikely(!remapped && ((type & BTRFS_BLOCK_GROUP_RAID10 &&
sub_stripes != btrfs_raid_array[BTRFS_RAID_RAID10].sub_stripes) ||
(type & BTRFS_BLOCK_GROUP_RAID1 &&
num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1].devs_min) ||
@@ -975,7 +977,7 @@ int btrfs_check_chunk_valid(const struct btrfs_fs_info *fs_info,
(type & BTRFS_BLOCK_GROUP_DUP &&
num_stripes != btrfs_raid_array[BTRFS_RAID_DUP].dev_stripes) ||
((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
- num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes))) {
+ num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes)))) {
chunk_err(fs_info, leaf, chunk, logical,
"invalid num_stripes:sub_stripes %u:%u for profile %llu",
num_stripes, sub_stripes,
@@ -999,11 +1001,11 @@ static int check_leaf_chunk_item(struct extent_buffer *leaf,
struct btrfs_fs_info *fs_info = leaf->fs_info;
int num_stripes;

- if (unlikely(btrfs_item_size(leaf, slot) < sizeof(struct btrfs_chunk))) {
+ if (unlikely(btrfs_item_size(leaf, slot) < offsetof(struct btrfs_chunk, stripe))) {
chunk_err(fs_info, leaf, chunk, key->offset,
"invalid chunk item size: have %u expect [%zu, %u)",
btrfs_item_size(leaf, slot),
- sizeof(struct btrfs_chunk),
+ offsetof(struct btrfs_chunk, stripe),
BTRFS_LEAF_DATA_SIZE(fs_info));
return -EUCLEAN;
}
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 0698613276d9..77194bb46b40 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -6133,6 +6133,12 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
goto out_free_map;
}

+ /* avoid divide by zero on fully-remapped chunks */
+ if (map->num_stripes == 0) {
+ ret = -EOPNOTSUPP;
+ goto out_free_map;
+ }
+
offset = logical - map->start;
length = min_t(u64, map->start + map->chunk_len - logical, length);
*length_ret = length;
@@ -6953,7 +6959,7 @@ u64 btrfs_calc_stripe_length(const struct btrfs_chunk_map *map)
{
const int data_stripes = calc_data_stripes(map->type, map->num_stripes);

- return div_u64(map->chunk_len, data_stripes);
+ return data_stripes ? div_u64(map->chunk_len, data_stripes) : 0;
}

#if BITS_PER_LONG == 32
--
2.49.0