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 follow the straight-forward way to fix it, causing
the final dev extents layout to match with chunk tree, and make btrfs
check happy.

Signed-off-by: Qu Wenruo <w...@suse.com>
---
 image/main.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 102 insertions(+)

diff --git a/image/main.c b/image/main.c
index 9187de34f34a..8f756689a1fa 100644
--- a/image/main.c
+++ b/image/main.c
@@ -2209,6 +2209,104 @@ 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) {
+               error("failed to search dev tree: %s", strerror(-ret));
+               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) {
+                               error("failed to search dev tree: %s",
+                                       strerror(-ret));
+                               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) {
+                       error("failed to delete dev extent %llu, %llu: %s",
+                               key.objectid, key.offset, strerror(-ret));
+                       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)
+               error("failed to remove all existing dev extents: %s",
+                       strerror(-ret));
+
+       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) {
+                               error(
+                               "failed to insert dev extent %llu %llu: %s",
+                                       devid, map->stripes[i].physical,
+                                       strerror(-ret));
+                               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 +2324,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;
-- 
2.19.2

Reply via email to