btrfs-progs: fix btrfsck of space_cache=v2 bitmaps on big-endian
[platform/upstream/btrfs-progs.git] / btrfs-corrupt-block.c
index d331f96..a488e47 100644 (file)
 
 #define FIELD_BUF_LEN 80
 
-static struct extent_buffer *debug_corrupt_block(struct btrfs_root *root,
-               u64 bytenr, u32 blocksize, u64 copy)
+static int debug_corrupt_block(struct extent_buffer *eb,
+               struct btrfs_root *root, u64 bytenr, u32 blocksize, u64 copy)
 {
        int ret;
-       struct extent_buffer *eb;
        u64 length;
        struct btrfs_multi_bio *multi = NULL;
        struct btrfs_device *device;
        int num_copies;
        int mirror_num = 1;
 
-       eb = btrfs_find_create_tree_block(root->fs_info, bytenr, blocksize);
-       if (!eb)
-               return NULL;
-
        length = blocksize;
        while (1) {
                ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
                                      eb->start, &length, &multi,
                                      mirror_num, NULL);
-               BUG_ON(ret);
+               if (ret) {
+                       error("cannot map block %llu length %llu mirror %d: %d",
+                                       (unsigned long long)eb->start,
+                                       (unsigned long long)length,
+                                       mirror_num, ret);
+                       return ret;
+               }
                device = multi->stripes[0].dev;
                eb->fd = device->fd;
                device->total_ios++;
@@ -68,10 +69,22 @@ static struct extent_buffer *debug_corrupt_block(struct btrfs_root *root,
 
                if (!copy || mirror_num == copy) {
                        ret = read_extent_from_disk(eb, 0, eb->len);
+                       if (ret < 0) {
+                               error("cannot read eb bytenr %llu: %s",
+                                               (unsigned long long)eb->dev_bytenr,
+                                               strerror(-ret));
+                               return ret;
+                       }
                        printf("corrupting %llu copy %d\n", eb->start,
                               mirror_num);
                        memset(eb->data, 0, eb->len);
-                       write_extent_to_disk(eb);
+                       ret = write_extent_to_disk(eb);
+                       if (ret < 0) {
+                               error("cannot write eb bytenr %llu: %s",
+                                               (unsigned long long)eb->dev_bytenr,
+                                               strerror(-ret));
+                               return ret;
+                       }
                        fsync(eb->fd);
                }
 
@@ -84,7 +97,8 @@ static struct extent_buffer *debug_corrupt_block(struct btrfs_root *root,
                if (mirror_num > num_copies)
                        break;
        }
-       return eb;
+
+       return 0;
 }
 
 static void print_usage(int ret)
@@ -131,8 +145,8 @@ static void corrupt_keys(struct btrfs_trans_handle *trans,
        if (nr == 0)
                return;
 
-       slot = rand() % nr;
-       bad_slot = rand() % nr;
+       slot = rand_range(nr);
+       bad_slot = rand_range(nr);
 
        if (bad_slot == slot)
                return;
@@ -181,7 +195,7 @@ static int corrupt_extent(struct btrfs_trans_handle *trans,
        struct btrfs_path *path;
        int ret;
        int slot;
-       int should_del = rand() % 3;
+       int should_del = rand_range(3);
 
        path = btrfs_alloc_path();
        if (!path)
@@ -210,6 +224,7 @@ static int corrupt_extent(struct btrfs_trans_handle *trans,
                        break;
 
                if (key.type != BTRFS_EXTENT_ITEM_KEY &&
+                   key.type != BTRFS_METADATA_ITEM_KEY &&
                    key.type != BTRFS_TREE_BLOCK_REF_KEY &&
                    key.type != BTRFS_EXTENT_DATA_REF_KEY &&
                    key.type != BTRFS_EXTENT_REF_V0_KEY &&
@@ -257,7 +272,7 @@ static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
                                      struct extent_buffer *eb)
 {
        u32 nr = btrfs_header_nritems(eb);
-       u32 victim = rand() % nr;
+       u32 victim = rand_range(nr);
        u64 objectid;
        struct btrfs_key key;
 
@@ -281,7 +296,7 @@ static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
        }
 
        if (btrfs_header_level(eb) == 1 && eb != root->node) {
-               if (rand() % 5)
+               if (rand_range(5))
                        return;
        }
 
@@ -390,7 +405,7 @@ static u64 generate_u64(u64 orig)
 {
        u64 ret;
        do {
-               ret = rand();
+               ret = rand_u64();
        } while (ret == orig);
        return ret;
 }
@@ -399,7 +414,7 @@ static u32 generate_u32(u32 orig)
 {
        u32 ret;
        do {
-               ret = rand();
+               ret = rand_u32();
        } while (ret == orig);
        return ret;
 }
@@ -408,7 +423,7 @@ static u8 generate_u8(u8 orig)
 {
        u8 ret;
        do {
-               ret = rand();
+               ret = rand_u8();
        } while (ret == orig);
        return ret;
 }
@@ -944,7 +959,7 @@ static int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
        while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
                slot = path->slots[0];
                leaf = path->nodes[0];
-               del = rand() % 3;
+               del = rand_range(3);
                /* Never delete the first item to keep the leaf structure */
                if (path->slots[0] == 0)
                        del = 0;
@@ -971,7 +986,7 @@ static int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
        while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
                slot = path->slots[0];
                leaf = path->nodes[0];
-               del = rand() % 3;
+               del = rand_range(3);
                btrfs_item_key_to_cpu(leaf, &found_key, slot);
                ret = corrupt_item_nocow(trans, root, path, del);
                if (ret)
@@ -1017,7 +1032,6 @@ int main(int argc, char **argv)
        struct cache_tree root_cache;
        struct btrfs_key key;
        struct btrfs_root *root;
-       struct extent_buffer *eb;
        char *dev;
        /* chunk offset can be 0,so change to (u64)-1 */
        u64 logical = (u64)-1;
@@ -1040,7 +1054,6 @@ int main(int argc, char **argv)
        char field[FIELD_BUF_LEN];
 
        field[0] = '\0';
-       srand(128);
        memset(&key, 0, sizeof(key));
 
        while(1) {
@@ -1179,7 +1192,7 @@ int main(int argc, char **argv)
 
                if (logical == (u64)-1)
                        print_usage(1);
-               del = rand() % 3;
+               del = rand_range(3);
                path = btrfs_alloc_path();
                if (!path) {
                        fprintf(stderr, "path allocation failed\n");
@@ -1295,8 +1308,20 @@ int main(int argc, char **argv)
                if (corrupt_block_keys) {
                        corrupt_keys_in_block(root, logical);
                } else {
-                       eb = debug_corrupt_block(root, logical,
-                                                root->sectorsize, copy);
+                       struct extent_buffer *eb;
+
+                       eb = btrfs_find_create_tree_block(root->fs_info,
+                                       logical, root->sectorsize);
+                       if (!eb) {
+                               error(
+               "not enough memory to allocate extent buffer for bytenr %llu",
+                                       (unsigned long long)logical);
+                               ret = 1;
+                               goto out_close;
+                       }
+
+                       debug_corrupt_block(eb, root, logical, root->sectorsize,
+                                       copy);
                        free_extent_buffer(eb);
                }
                logical += root->sectorsize;