X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=btrfs-corrupt-block.c;h=a488e479f72e94509faf72b21f8587263b4dbb81;hb=bf5e74204e5d40545cc97a9a79d340b1615003a4;hp=6eb04a7fb9a2109c4c687991d48529cb2a9c108f;hpb=4647ab887a592516841f74fa47586771bbcad81b;p=platform%2Fupstream%2Fbtrfs-progs.git diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c index 6eb04a7..a488e47 100644 --- a/btrfs-corrupt-block.c +++ b/btrfs-corrupt-block.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "kerncompat.h" #include "ctree.h" @@ -33,27 +34,28 @@ #define FIELD_BUF_LEN 80 -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, 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++; @@ -67,10 +69,22 @@ struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr, 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); } @@ -83,7 +97,8 @@ struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr, if (mirror_num > num_copies) break; } - return eb; + + return 0; } static void print_usage(int ret) @@ -130,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; @@ -161,7 +176,7 @@ static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr) { struct extent_buffer *eb; - eb = read_tree_block(root, bytenr, root->leafsize, 0); + eb = read_tree_block(root, bytenr, root->nodesize, 0); if (!extent_buffer_uptodate(eb)) return -EIO;; @@ -180,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) @@ -209,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 && @@ -256,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; @@ -280,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; } @@ -288,7 +304,7 @@ static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans, struct extent_buffer *next; next = read_tree_block(root, btrfs_node_blockptr(eb, i), - root->leafsize, + root->nodesize, btrfs_node_ptr_generation(eb, i)); if (!extent_buffer_uptodate(next)) continue; @@ -299,6 +315,7 @@ static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans, enum btrfs_inode_field { BTRFS_INODE_FIELD_ISIZE, + BTRFS_INODE_FIELD_NBYTES, BTRFS_INODE_FIELD_BAD, }; @@ -335,6 +352,8 @@ static enum btrfs_inode_field convert_inode_field(char *field) { if (!strncmp(field, "isize", FIELD_BUF_LEN)) return BTRFS_INODE_FIELD_ISIZE; + if (!strncmp(field, "nbytes", FIELD_BUF_LEN)) + return BTRFS_INODE_FIELD_NBYTES; return BTRFS_INODE_FIELD_BAD; } @@ -386,7 +405,7 @@ static u64 generate_u64(u64 orig) { u64 ret; do { - ret = rand(); + ret = rand_u64(); } while (ret == orig); return ret; } @@ -395,7 +414,7 @@ static u32 generate_u32(u32 orig) { u32 ret; do { - ret = rand(); + ret = rand_u32(); } while (ret == orig); return ret; } @@ -404,7 +423,7 @@ static u8 generate_u8(u8 orig) { u8 ret; do { - ret = rand(); + ret = rand_u8(); } while (ret == orig); return ret; } @@ -472,7 +491,7 @@ static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key, struct btrfs_trans_handle *trans; struct btrfs_dir_item *di; struct btrfs_path *path; - char *name; + char name[PATH_MAX]; struct btrfs_key location; struct btrfs_disk_key disk_key; unsigned long name_ptr; @@ -511,17 +530,11 @@ static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key, switch (corrupt_field) { case BTRFS_DIR_ITEM_NAME: name_len = btrfs_dir_name_len(path->nodes[0], di); - name = malloc(name_len); - if (!name) { - ret = -ENOMEM; - goto out; - } name_ptr = (unsigned long)(di + 1); read_extent_buffer(path->nodes[0], name, name_ptr, name_len); name[0]++; write_extent_buffer(path->nodes[0], name, name_ptr, name_len); btrfs_mark_buffer_dirty(path->nodes[0]); - free(name); goto out; case BTRFS_DIR_ITEM_LOCATION_OBJECTID: btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); @@ -593,6 +606,11 @@ static int corrupt_inode(struct btrfs_trans_handle *trans, bogus = generate_u64(orig); btrfs_set_inode_size(path->nodes[0], ei, bogus); break; + case BTRFS_INODE_FIELD_NBYTES: + orig = btrfs_inode_nbytes(path->nodes[0], ei); + bogus = generate_u64(orig); + btrfs_set_inode_nbytes(path->nodes[0], ei, bogus); + break; default: ret = -EINVAL; break; @@ -697,7 +715,7 @@ static int corrupt_metadata_block(struct btrfs_root *root, u64 block, return -EINVAL; } - eb = read_tree_block(root, block, root->leafsize, 0); + eb = read_tree_block(root, block, root->nodesize, 0); if (!extent_buffer_uptodate(eb)) { fprintf(stderr, "Couldn't read in tree block %s\n", field); return -EINVAL; @@ -716,7 +734,7 @@ static int corrupt_metadata_block(struct btrfs_root *root, u64 block, root = btrfs_read_fs_root(root->fs_info, &root_key); if (IS_ERR(root)) { - fprintf(stderr, "Couldn't finde owner root %llu\n", + fprintf(stderr, "Couldn't find owner root %llu\n", key.objectid); return PTR_ERR(root); } @@ -868,11 +886,11 @@ static int delete_csum(struct btrfs_root *root, u64 bytenr, u64 bytes) } /* corrupt item using NO cow. - * Because chunk recover will recover based on whole partition scaning, + * Because chunk recover will recover based on whole partition scanning, * If using COW, chunk recover will use the old item to recover, * which is still OK but we want to check the ability to rebuild chunk * not only restore the old ones */ -int corrupt_item_nocow(struct btrfs_trans_handle *trans, +static int corrupt_item_nocow(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, int del) { @@ -905,7 +923,7 @@ int corrupt_item_nocow(struct btrfs_trans_handle *trans, } return ret; } -int corrupt_chunk_tree(struct btrfs_trans_handle *trans, +static int corrupt_chunk_tree(struct btrfs_trans_handle *trans, struct btrfs_root *root) { int ret; @@ -941,7 +959,7 @@ 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; @@ -968,7 +986,7 @@ 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) @@ -978,7 +996,7 @@ free_out: btrfs_free_path(path); return ret; } -int find_chunk_offset(struct btrfs_root *root, +static int find_chunk_offset(struct btrfs_root *root, struct btrfs_path *path, u64 offset) { struct btrfs_key key; @@ -1002,19 +1020,18 @@ int find_chunk_offset(struct btrfs_root *root, goto out; } if (ret < 0) { - fprintf(stderr, "Error searching chunk"); + fprintf(stderr, "Error searching chunk\n"); goto out; } out: return ret; } -int main(int ac, char **av) +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; @@ -1037,7 +1054,6 @@ int main(int ac, char **av) char field[FIELD_BUF_LEN]; field[0] = '\0'; - srand(128); memset(&key, 0, sizeof(key)); while(1) { @@ -1066,7 +1082,7 @@ int main(int ac, char **av) { NULL, 0, NULL, 0 } }; - c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:IDdr:C:", + c = getopt_long(argc, argv, "l:c:b:eEkuUi:f:x:m:K:IDdr:C:", long_options, NULL); if (c < 0) break; @@ -1138,11 +1154,10 @@ int main(int ac, char **av) print_usage(c != GETOPT_VAL_HELP); } } - set_argv0(av); - ac = ac - optind; - if (check_argc_min(ac, 1)) + set_argv0(argv); + if (check_argc_min(argc - optind, 1)) print_usage(1); - dev = av[optind]; + dev = argv[optind]; radix_tree_init(); cache_tree_init(&root_cache); @@ -1177,7 +1192,7 @@ int main(int ac, char **av) 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"); @@ -1209,7 +1224,7 @@ int main(int ac, char **av) if (inode) { struct btrfs_trans_handle *trans; - if (!strlen(field)) + if (*field == 0) print_usage(1); trans = btrfs_start_transaction(root, 1); @@ -1225,13 +1240,13 @@ int main(int ac, char **av) goto out_close; } if (metadata_block) { - if (!strlen(field)) + if (*field == 0) print_usage(1); ret = corrupt_metadata_block(root, metadata_block, field); goto out_close; } if (corrupt_di) { - if (!key.objectid || !strlen(field)) + if (!key.objectid || *field == 0) print_usage(1); ret = corrupt_dir_item(root, &key, field); goto out_close; @@ -1268,7 +1283,7 @@ int main(int ac, char **av) goto out_close; } if (key.objectid || key.offset || key.type) { - if (!strlen(field)) + if (*field == 0) print_usage(1); ret = corrupt_key(root, &key, field); goto out_close; @@ -1293,8 +1308,20 @@ int main(int ac, char **av) 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;