btrfs-progs: read stripe tree when mapping blocks

Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Johannes Thumshirn 2023-09-14 09:05:37 -07:00 committed by David Sterba
parent fff57d3774
commit a9fd50f85f
1 changed files with 103 additions and 3 deletions

View File

@ -1810,6 +1810,95 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
multi_ret, mirror_num, raid_map_ret);
}
static bool btrfs_need_stripe_tree_update(struct btrfs_fs_info *fs_info, u64 map_type)
{
#if EXPERIMENTAL
const bool is_data = (map_type & BTRFS_BLOCK_GROUP_DATA);
if (!btrfs_fs_incompat(fs_info, RAID_STRIPE_TREE))
return false;
if (!fs_info->stripe_root)
return false;
if (!is_data)
return false;
if (map_type & BTRFS_BLOCK_GROUP_DUP)
return true;
if (map_type & BTRFS_BLOCK_GROUP_RAID1_MASK)
return true;
if (map_type & BTRFS_BLOCK_GROUP_RAID0)
return true;
if (map_type & BTRFS_BLOCK_GROUP_RAID10)
return true;
#endif
return false;
}
static int btrfs_stripe_tree_logical_to_physical(struct btrfs_fs_info *fs_info,
u64 logical,
struct btrfs_bio_stripe *stripe)
{
struct btrfs_root *root = fs_info->stripe_root;
struct btrfs_path path = { 0 };
struct btrfs_key key;
struct extent_buffer *leaf;
int slot;
int ret;
key.objectid = logical;
key.type = BTRFS_RAID_STRIPE_KEY;
key.offset = 0;
ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
if (ret < 0)
return ret;
while (1) {
struct btrfs_key found_key;
struct btrfs_stripe_extent *extent;
int num_stripes;
u32 item_size;
leaf = path.nodes[0];
slot = path.slots[0];
if (slot >= btrfs_header_nritems(leaf)) {
ret = btrfs_next_leaf(root, &path);
if (ret == 0)
continue;
if (ret < 0)
goto error;
break;
}
btrfs_item_key_to_cpu(leaf, &found_key, slot);
if (found_key.type != BTRFS_RAID_STRIPE_KEY)
goto next;
extent = btrfs_item_ptr(leaf, slot, struct btrfs_stripe_extent);
item_size = btrfs_item_size(leaf, slot);
num_stripes = (item_size -
offsetof(struct btrfs_stripe_extent, strides)) /
sizeof(struct btrfs_raid_stride);
for (int i = 0; i < num_stripes; i++) {
if (stripe->dev->devid !=
btrfs_raid_stride_devid_nr(leaf, extent, i))
continue;
stripe->physical = btrfs_raid_stride_offset_nr(leaf, extent, i);
btrfs_release_path(&path);
return 0;
}
next:
path.slots[0]++;
}
btrfs_release_path(&path);
error:
return ret;
}
int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
u64 logical, u64 *length, u64 *type,
struct btrfs_multi_bio **multi_ret, int mirror_num,
@ -2002,10 +2091,21 @@ again:
BUG_ON(stripe_index >= map->num_stripes);
for (i = 0; i < multi->num_stripes; i++) {
multi->stripes[i].physical =
map->stripes[stripe_index].physical + stripe_offset +
stripe_nr * map->stripe_len;
multi->stripes[i].dev = map->stripes[stripe_index].dev;
if (stripes_allocated &&
btrfs_need_stripe_tree_update(fs_info, map->type)) {
int ret;
ret = btrfs_stripe_tree_logical_to_physical(fs_info, logical,
&multi->stripes[i]);
if (ret)
return ret;
} else {
multi->stripes[i].physical =
map->stripes[stripe_index].physical +
stripe_offset + stripe_nr * map->stripe_len;
}
stripe_index++;
}
*multi_ret = multi;