btrfs-progs: check: introduce function to check dir_item
[platform/upstream/btrfs-progs.git] / chunk-recover.c
index 4a081db..e6b26ac 100644 (file)
@@ -968,7 +968,7 @@ static int build_device_map_by_chunk_record(struct btrfs_root *root,
                map->stripes[i].dev = btrfs_find_device(root, devid,
                                                        uuid, NULL);
                if (!map->stripes[i].dev) {
-                       kfree(map);
+                       free(map);
                        return -EIO;
                }
        }
@@ -1398,26 +1398,24 @@ static int rebuild_block_group(struct btrfs_trans_handle *trans,
 {
        struct chunk_record *chunk_rec;
        struct btrfs_key search_key;
-       struct btrfs_path *path;
+       struct btrfs_path path;
        u64 used = 0;
        int ret = 0;
 
        if (list_empty(&rc->rebuild_chunks))
                return 0;
 
-       path = btrfs_alloc_path();
-       if (!path)
-               return -ENOMEM;
+       btrfs_init_path(&path);
        list_for_each_entry(chunk_rec, &rc->rebuild_chunks, list) {
                search_key.objectid = chunk_rec->offset;
                search_key.type = BTRFS_EXTENT_ITEM_KEY;
                search_key.offset = 0;
                ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
-                                       &search_key, path, 0, 0);
+                                       &search_key, &path, 0, 0);
                if (ret < 0)
                        goto out;
                ret = calculate_bg_used(root->fs_info->extent_root,
-                                       chunk_rec, path, &used);
+                                       chunk_rec, &path, &used);
                /*
                 * Extent tree is damaged, better to rebuild the whole extent
                 * tree. Currently, change the used to chunk's len to prevent
@@ -1432,7 +1430,7 @@ static int rebuild_block_group(struct btrfs_trans_handle *trans,
                                "Mark the block group full to prevent block rsv problems\n");
                        used = chunk_rec->length;
                }
-               btrfs_release_path(path);
+               btrfs_release_path(&path);
                ret = __insert_block_group(trans, chunk_rec,
                                           root->fs_info->extent_root,
                                           used);
@@ -1440,7 +1438,7 @@ static int rebuild_block_group(struct btrfs_trans_handle *trans,
                        goto out;
        }
 out:
-       btrfs_free_path(path);
+       btrfs_release_path(&path);
        return ret;
 }
 
@@ -1479,7 +1477,7 @@ open_ctree_with_broken_chunk(struct recover_control *rc)
 
        memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
 
-       ret = btrfs_check_fs_compatibility(disk_super, 1);
+       ret = btrfs_check_fs_compatibility(disk_super, OPEN_CTREE_WRITES);
        if (ret)
                goto out_devices;
 
@@ -1488,7 +1486,7 @@ open_ctree_with_broken_chunk(struct recover_control *rc)
        sectorsize = btrfs_super_sectorsize(disk_super);
        stripesize = btrfs_super_stripesize(disk_super);
 
-       __setup_root(nodesize, leafsize, sectorsize, stripesize,
+       btrfs_setup_root(nodesize, leafsize, sectorsize, stripesize,
                     fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
 
        ret = build_device_maps_by_chunk_records(rc, fs_info->chunk_root);
@@ -1914,7 +1912,7 @@ static int check_one_csum(int fd, u64 start, u32 len, u32 tree_csum)
        }
        ret = 0;
        csum_result = btrfs_csum_data(NULL, data, csum_result, len);
-       btrfs_csum_final(csum_result, (char *)&csum_result);
+       btrfs_csum_final(csum_result, (u8 *)&csum_result);
        if (csum_result != tree_csum)
                ret = 1;
 out:
@@ -1947,9 +1945,12 @@ static int insert_stripe(struct list_head *devexts,
        dev = btrfs_find_device_by_devid(rc->fs_devices, devext->objectid,
                                        0);
        if (!dev)
-               return 1;
-       BUG_ON(btrfs_find_device_by_devid(rc->fs_devices, devext->objectid,
-                                       1));
+               return -ENOENT;
+       if (btrfs_find_device_by_devid(rc->fs_devices, devext->objectid, 1)) {
+               error("unexpected: found another device with id %llu",
+                               (unsigned long long)devext->objectid);
+               return -EINVAL;
+       }
 
        chunk->stripes[index].devid = devext->objectid;
        chunk->stripes[index].offset = devext->offset;
@@ -2247,6 +2248,13 @@ static int btrfs_recover_chunks(struct recover_control *rc)
                chunk->sub_stripes = calc_sub_nstripes(bg->flags);
 
                ret = insert_cache_extent(&rc->chunk, &chunk->cache);
+               if (ret == -EEXIST) {
+                       error("duplicate entry in cache start %llu size %llu",
+                                       (unsigned long long)chunk->cache.start,
+                                       (unsigned long long)chunk->cache.size);
+                       free(chunk);
+                       return ret;
+               }
                BUG_ON(ret);
 
                list_del_init(&bg->list);