mirror of
https://github.com/kdave/btrfs-progs
synced 2025-01-30 09:21:45 +00:00
btrfs-progs: image: Remove all existing dev extents for later rebuild
For multi-disk btrfs image recovered to single disk, the dev tree would look like: item 2 key (1 DEV_EXTENT 22020096) dev extent chunk_tree 3 chunk_objectid 256 chunk_offset 22020096 length 8388608 item 3 key (1 DEV_EXTENT 30408704) dev extent chunk_tree 3 chunk_objectid 256 chunk_offset 30408704 length 1073741824 item 4 key (1 DEV_EXTENT 1104150528) dev extent chunk_tree 3 chunk_objectid 256 chunk_offset 1104150528 length 536870912 item 5 key (2 DEV_EXTENT 1048576) dev extent chunk_tree 3 chunk_objectid 256 chunk_offset 22020096 length 8388608 item 6 key (2 DEV_EXTENT 9437184) dev extent chunk_tree 3 chunk_objectid 256 chunk_offset 30408704 length 1073741824 item 7 key (2 DEV_EXTENT 1083179008) dev extent chunk_tree 3 chunk_objectid 256 chunk_offset 1104150528 length 536870912 However in chunk tree, we only use devid 2, thus devid 1 is completely garbage: item 0 key (DEV_ITEMS DEV_ITEM 2) item 1 key (FIRST_CHUNK_TREE CHUNK_ITEM 22020096) itemoff 16105 itemsize 80 length 8388608 owner 2 stripe_len 65536 type SYSTEM num_stripes 1 sub_stripes 0 stripe 0 devid 2 offset 1048576 item 2 key (FIRST_CHUNK_TREE CHUNK_ITEM 30408704) itemoff 16025 itemsize 80 length 1073741824 owner 2 stripe_len 65536 type METADATA num_stripes 1 sub_stripes 0 stripe 0 devid 2 offset 9437184 item 3 key (FIRST_CHUNK_TREE CHUNK_ITEM 1104150528) itemoff 15945 itemsize 80 length 1073741824 owner 2 stripe_len 65536 type DATA num_stripes 1 sub_stripes 0 stripe 0 devid 2 offset 1083179008 To fix the problem, the most straight-forward way is to remove all existing dev extents, and then re-fill correct dev extents from chunk. So this patch just follows the straight-forward way to fix it, causing the final dev extents layout to match chunk tree, and make btrfs check happy. Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
parent
e6c1fa297a
commit
a1a98ee7a8
105
image/main.c
105
image/main.c
@ -2209,6 +2209,107 @@ static void fixup_block_groups(struct btrfs_fs_info *fs_info)
|
||||
}
|
||||
}
|
||||
|
||||
static int remove_all_dev_extents(struct btrfs_trans_handle *trans)
|
||||
{
|
||||
struct btrfs_fs_info *fs_info = trans->fs_info;
|
||||
struct btrfs_root *root = fs_info->dev_root;
|
||||
struct btrfs_path path;
|
||||
struct btrfs_key key;
|
||||
struct extent_buffer *leaf;
|
||||
int slot;
|
||||
int ret;
|
||||
|
||||
key.objectid = 1;
|
||||
key.type = BTRFS_DEV_EXTENT_KEY;
|
||||
key.offset = 0;
|
||||
btrfs_init_path(&path);
|
||||
|
||||
ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
|
||||
if (ret < 0) {
|
||||
errno = -ret;
|
||||
error("failed to search dev tree: %m");
|
||||
return ret;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
slot = path.slots[0];
|
||||
leaf = path.nodes[0];
|
||||
if (slot >= btrfs_header_nritems(leaf)) {
|
||||
ret = btrfs_next_leaf(root, &path);
|
||||
if (ret < 0) {
|
||||
errno = -ret;
|
||||
error("failed to search dev tree: %m");
|
||||
goto out;
|
||||
}
|
||||
if (ret > 0) {
|
||||
ret = 0;
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
btrfs_item_key_to_cpu(leaf, &key, slot);
|
||||
if (key.type != BTRFS_DEV_EXTENT_KEY)
|
||||
break;
|
||||
ret = btrfs_del_item(trans, root, &path);
|
||||
if (ret < 0) {
|
||||
errno = -ret;
|
||||
error("failed to delete dev extent %llu, %llu: %m",
|
||||
key.objectid, key.offset);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
out:
|
||||
btrfs_release_path(&path);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int fixup_dev_extents(struct btrfs_trans_handle *trans)
|
||||
{
|
||||
struct btrfs_fs_info *fs_info = trans->fs_info;
|
||||
struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
|
||||
struct btrfs_device *dev;
|
||||
struct cache_extent *ce;
|
||||
struct map_lookup *map;
|
||||
u64 devid = btrfs_stack_device_id(&fs_info->super_copy->dev_item);
|
||||
int i;
|
||||
int ret;
|
||||
|
||||
ret = remove_all_dev_extents(trans);
|
||||
if (ret < 0) {
|
||||
errno = -ret;
|
||||
error("failed to remove all existing dev extents: %m");
|
||||
}
|
||||
|
||||
dev = btrfs_find_device(fs_info, devid, NULL, NULL);
|
||||
if (!dev) {
|
||||
error("faild to find devid %llu", devid);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/* Rebuild all dev extents using chunk maps */
|
||||
for (ce = search_cache_extent(&map_tree->cache_tree, 0); ce;
|
||||
ce = next_cache_extent(ce)) {
|
||||
u64 stripe_len;
|
||||
|
||||
map = container_of(ce, struct map_lookup, ce);
|
||||
stripe_len = calc_stripe_length(map->type, ce->size,
|
||||
map->num_stripes);
|
||||
for (i = 0; i < map->num_stripes; i++) {
|
||||
ret = btrfs_insert_dev_extent(trans, dev, ce->start,
|
||||
stripe_len, map->stripes[i].physical);
|
||||
if (ret < 0) {
|
||||
errno = -ret;
|
||||
error(
|
||||
"failed to insert dev extent %llu %llu: %m",
|
||||
devid, map->stripes[i].physical);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
}
|
||||
out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int fixup_chunks_and_devices(struct btrfs_fs_info *fs_info,
|
||||
struct mdrestore_struct *mdres, off_t dev_size)
|
||||
{
|
||||
@ -2226,6 +2327,10 @@ static int fixup_chunks_and_devices(struct btrfs_fs_info *fs_info,
|
||||
}
|
||||
|
||||
fixup_block_groups(fs_info);
|
||||
ret = fixup_dev_extents(trans);
|
||||
if (ret < 0)
|
||||
goto error;
|
||||
|
||||
ret = fixup_device_size(trans, mdres, dev_size);
|
||||
if (ret < 0)
|
||||
goto error;
|
||||
|
Loading…
Reference in New Issue
Block a user