X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=btrfs-corrupt-block.c;h=16680df7e72162c19b7e3ef4913023a3d4dbcd82;hb=52bfe9ef78fdf6c1f1c5ceb3f7a8bf8bf2164775;hp=6c335b9558e458267189b05edd042b75d1e1ece7;hpb=af3b243358d3d352f59b4bcc38b695d5b4aa2644;p=platform%2Fupstream%2Fbtrfs-progs.git diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c index 6c335b9..16680df 100644 --- a/btrfs-corrupt-block.c +++ b/btrfs-corrupt-block.c @@ -21,6 +21,8 @@ #include #include #include +#include + #include "kerncompat.h" #include "ctree.h" #include "volumes.h" @@ -32,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++; @@ -62,14 +65,26 @@ struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr, "mirror %d logical %llu physical %llu device %s\n", mirror_num, (unsigned long long)bytenr, (unsigned long long)eb->dev_bytenr, device->name); - kfree(multi); + free(multi); 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); } @@ -82,38 +97,31 @@ 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(void) +static void print_usage(int ret) { - fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n"); - fprintf(stderr, "\t-l Logical extent to be corrupted\n"); - fprintf(stderr, "\t-c Copy of the extent to be corrupted" - " (usually 1 or 2, default: 0)\n"); - fprintf(stderr, "\t-b Number of bytes to be corrupted\n"); - fprintf(stderr, "\t-e Extent to be corrupted\n"); - fprintf(stderr, "\t-E The whole extent tree to be corrupted\n"); - fprintf(stderr, "\t-u Given chunk item to be corrupted\n"); - fprintf(stderr, "\t-U The whole chunk tree to be corrupted\n"); - fprintf(stderr, "\t-i The inode item to corrupt (must also specify " - "the field to corrupt)\n"); - fprintf(stderr, "\t-x The file extent item to corrupt (must also " - "specify -i for the inode and -f for the field to corrupt)\n"); - fprintf(stderr, "\t-m The metadata block to corrupt (must also " - "specify -f for the field to corrupt)\n"); - fprintf(stderr, "\t-K The key to corrupt in the format " - ",, (must also specify -f for the field)\n"); - fprintf(stderr, "\t-f The field in the item to corrupt\n"); - fprintf(stderr, "\t-I An item to corrupt (must also specify the field " - "to corrupt and a root+key for the item)\n"); - fprintf(stderr, "\t-D Corrupt a dir item, must specify key and field\n"); - fprintf(stderr, "\t-d Delete this item (must specify -K)\n"); - fprintf(stderr, "\t-r Operate on this root (only works with -d)\n"); - fprintf(stderr, "\t-C Delete a csum for the specified bytenr. When " - "used with -b it'll delete that many bytes, otherwise it's " - "just sectorsize\n"); - exit(1); + printf("usage: btrfs-corrupt-block [options] device\n"); + printf("\t-l Logical extent to be corrupted\n"); + printf("\t-c Copy of the extent to be corrupted (usually 1 or 2, default: 0)\n"); + printf("\t-b Number of bytes to be corrupted\n"); + printf("\t-e Extent to be corrupted\n"); + printf("\t-E The whole extent tree to be corrupted\n"); + printf("\t-u Given chunk item to be corrupted\n"); + printf("\t-U The whole chunk tree to be corrupted\n"); + printf("\t-i The inode item to corrupt (must also specify the field to corrupt)\n"); + printf("\t-x The file extent item to corrupt (must also specify -i for the inode and -f for the field to corrupt)\n"); + printf("\t-m The metadata block to corrupt (must also specify -f for the field to corrupt)\n"); + printf("\t-K The key to corrupt in the format ,, (must also specify -f for the field)\n"); + printf("\t-f The field in the item to corrupt\n"); + printf("\t-I An item to corrupt (must also specify the field to corrupt and a root+key for the item)\n"); + printf("\t-D Corrupt a dir item, must specify key and field\n"); + printf("\t-d Delete this item (must specify -K)\n"); + printf("\t-r Operate on this root (only works with -d)\n"); + printf("\t-C Delete a csum for the specified bytenr. When used with -b it'll delete that many bytes, otherwise it's just sectorsize\n"); + exit(ret); } static void corrupt_keys(struct btrfs_trans_handle *trans, @@ -129,8 +137,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; @@ -160,7 +168,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;; @@ -179,7 +187,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) @@ -208,6 +216,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 && @@ -255,7 +264,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; @@ -279,7 +288,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; } @@ -287,7 +296,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; @@ -298,6 +307,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, }; @@ -334,6 +344,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; } @@ -385,7 +397,7 @@ static u64 generate_u64(u64 orig) { u64 ret; do { - ret = rand(); + ret = rand_u64(); } while (ret == orig); return ret; } @@ -394,7 +406,7 @@ static u32 generate_u32(u32 orig) { u32 ret; do { - ret = rand(); + ret = rand_u32(); } while (ret == orig); return ret; } @@ -403,7 +415,7 @@ static u8 generate_u8(u8 orig) { u8 ret; do { - ret = rand(); + ret = rand_u8(); } while (ret == orig); return ret; } @@ -471,7 +483,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; @@ -510,17 +522,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); @@ -592,6 +598,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; @@ -696,7 +707,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; @@ -715,7 +726,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); } @@ -867,11 +878,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) { @@ -888,7 +899,11 @@ int corrupt_item_nocow(struct btrfs_trans_handle *trans, if (slot == 0) del = 0; /* Only accept valid eb */ - BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf)); + if (slot >= btrfs_header_nritems(leaf)) { + error("invalid eb: no data or slot out of range: %d >= %d", + slot, btrfs_header_nritems(leaf)); + return -EINVAL; + } btrfs_item_key_to_cpu(leaf, &key, slot); if (del) { fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n", @@ -904,7 +919,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; @@ -940,7 +955,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; @@ -967,7 +982,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) @@ -977,7 +992,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; @@ -1001,19 +1016,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; @@ -1036,7 +1050,6 @@ int main(int ac, char **av) char field[FIELD_BUF_LEN]; field[0] = '\0'; - srand(128); memset(&key, 0, sizeof(key)); while(1) { @@ -1061,10 +1074,11 @@ int main(int ac, char **av) { "delete", no_argument, NULL, 'd'}, { "root", no_argument, NULL, 'r'}, { "csum", required_argument, NULL, 'C'}, + { "help", no_argument, NULL, GETOPT_VAL_HELP}, { 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; @@ -1113,7 +1127,7 @@ int main(int ac, char **av) if (ret != 3) { fprintf(stderr, "error reading key " "%d\n", errno); - print_usage(); + print_usage(1); } break; case 'D': @@ -1131,15 +1145,15 @@ int main(int ac, char **av) case 'C': csum_bytenr = arg_strtou64(optarg); break; + case GETOPT_VAL_HELP: default: - print_usage(); + print_usage(c != GETOPT_VAL_HELP); } } - set_argv0(av); - ac = ac - optind; - if (check_argc_min(ac, 1)) - print_usage(); - dev = av[optind]; + set_argv0(argv); + if (check_argc_min(argc - optind, 1)) + print_usage(1); + dev = argv[optind]; radix_tree_init(); cache_tree_init(&root_cache); @@ -1153,7 +1167,7 @@ int main(int ac, char **av) struct btrfs_trans_handle *trans; if (logical == (u64)-1) - print_usage(); + print_usage(1); trans = btrfs_start_transaction(root, 1); ret = corrupt_extent (trans, root, logical, 0); btrfs_commit_transaction(trans, root); @@ -1173,8 +1187,8 @@ int main(int ac, char **av) int del; if (logical == (u64)-1) - print_usage(); - del = rand() % 3; + print_usage(1); + del = rand_range(3); path = btrfs_alloc_path(); if (!path) { fprintf(stderr, "path allocation failed\n"); @@ -1206,8 +1220,8 @@ int main(int ac, char **av) if (inode) { struct btrfs_trans_handle *trans; - if (!strlen(field)) - print_usage(); + if (*field == 0) + print_usage(1); trans = btrfs_start_transaction(root, 1); if (file_extent == (u64)-1) { @@ -1222,14 +1236,14 @@ int main(int ac, char **av) goto out_close; } if (metadata_block) { - if (!strlen(field)) - print_usage(); + 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)) - print_usage(); + if (!key.objectid || *field == 0) + print_usage(1); ret = corrupt_dir_item(root, &key, field); goto out_close; } @@ -1239,14 +1253,14 @@ int main(int ac, char **av) } if (corrupt_item) { if (!key.objectid) - print_usage(); + print_usage(1); ret = corrupt_btrfs_item(root, &key, field); } if (delete) { struct btrfs_root *target = root; if (!key.objectid) - print_usage(); + print_usage(1); if (root_objectid) { struct btrfs_key root_key; @@ -1258,15 +1272,15 @@ int main(int ac, char **av) if (IS_ERR(target)) { fprintf(stderr, "Couldn't find root %llu\n", (unsigned long long)root_objectid); - print_usage(); + print_usage(1); } } ret = delete_item(target, &key); goto out_close; } if (key.objectid || key.offset || key.type) { - if (!strlen(field)) - print_usage(); + if (*field == 0) + print_usage(1); ret = corrupt_key(root, &key, field); goto out_close; } @@ -1275,10 +1289,10 @@ int main(int ac, char **av) * inode and we're screwed. */ if (file_extent != (u64)-1) - print_usage(); + print_usage(1); if (logical == (u64)-1) - print_usage(); + print_usage(1); if (bytes == 0) bytes = root->sectorsize; @@ -1290,8 +1304,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;