X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=btrfs-corrupt-block.c;h=da0ec8c51e5a1a175b766bd099e98ea7d5e7f8d8;hb=3d29a0e5d1c862bf2206b809feab8b07839c2094;hp=64f515ecd1c5616c9e432bbd12d92750e6ee15ee;hpb=2b7cdab42529bc4ed4c36a3659504e50f0ef700c;p=platform%2Fupstream%2Fbtrfs-progs.git diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c index 64f515e..da0ec8c 100644 --- a/btrfs-corrupt-block.c +++ b/btrfs-corrupt-block.c @@ -16,13 +16,13 @@ * Boston, MA 021110-1307, USA. */ -#define _XOPEN_SOURCE 500 -#define _GNU_SOURCE 1 #include #include #include #include #include +#include + #include "kerncompat.h" #include "ctree.h" #include "volumes.h" @@ -30,32 +30,32 @@ #include "print-tree.h" #include "transaction.h" #include "list.h" -#include "version.h" #include "utils.h" +#include "help.h" #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); + ret = btrfs_map_block(root->fs_info, READ, eb->start, &length, + &multi, mirror_num, NULL); + 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++; @@ -65,19 +65,31 @@ 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); } - num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, - eb->start, eb->len); + num_copies = btrfs_num_copies(root->fs_info, eb->start, + eb->len); if (num_copies == 1) break; @@ -85,38 +97,35 @@ 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"); - 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, - struct btrfs_root *root, + struct btrfs_fs_info *fs_info, struct extent_buffer *eb) { int slot; @@ -128,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; @@ -148,28 +157,28 @@ static void corrupt_keys(struct btrfs_trans_handle *trans, btrfs_mark_buffer_dirty(eb); if (!trans) { u16 csum_size = - btrfs_super_csum_size(root->fs_info->super_copy); + btrfs_super_csum_size(fs_info->super_copy); csum_tree_block_size(eb, csum_size, 0); write_extent_to_disk(eb); } } -static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr) +static int corrupt_keys_in_block(struct btrfs_fs_info *fs_info, u64 bytenr) { struct extent_buffer *eb; - eb = read_tree_block(root, bytenr, root->leafsize, 0); - if (!eb) + eb = read_tree_block(fs_info, bytenr, 0); + if (!extent_buffer_uptodate(eb)) return -EIO;; - corrupt_keys(NULL, root, eb); + corrupt_keys(NULL, fs_info, eb); free_extent_buffer(eb); return 0; } static int corrupt_extent(struct btrfs_trans_handle *trans, - struct btrfs_root *root, u64 bytenr, u64 copy) + struct btrfs_root *root, u64 bytenr) { struct btrfs_key key; struct extent_buffer *leaf; @@ -178,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) @@ -207,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 && @@ -254,19 +264,20 @@ 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; btrfs_item_key_to_cpu(eb, &key, victim); objectid = key.objectid; - corrupt_extent(trans, root, objectid, 1); + corrupt_extent(trans, root, objectid); } static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct extent_buffer *eb) { + struct btrfs_fs_info *fs_info = root->fs_info; int i; if (!eb) @@ -278,17 +289,16 @@ 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; } for (i = 0; i < btrfs_header_nritems(eb); i++) { struct extent_buffer *next; - next = read_tree_block(root, btrfs_node_blockptr(eb, i), - root->leafsize, + next = read_tree_block(fs_info, btrfs_node_blockptr(eb, i), btrfs_node_ptr_generation(eb, i)); - if (!next) + if (!extent_buffer_uptodate(next)) continue; btrfs_corrupt_extent_tree(trans, root, next); free_extent_buffer(next); @@ -297,6 +307,14 @@ 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_NLINK, + BTRFS_INODE_FIELD_GENERATION, + BTRFS_INODE_FIELD_TRANSID, + BTRFS_INODE_FIELD_BLOCK_GROUP, + BTRFS_INODE_FIELD_MODE, + BTRFS_INODE_FIELD_UID, + BTRFS_INODE_FIELD_GID, BTRFS_INODE_FIELD_BAD, }; @@ -333,6 +351,22 @@ 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; + if (!strncmp(field, "nlink", FIELD_BUF_LEN)) + return BTRFS_INODE_FIELD_NLINK; + if (!strncmp(field, "generation", FIELD_BUF_LEN)) + return BTRFS_INODE_FIELD_GENERATION; + if (!strncmp(field, "transid", FIELD_BUF_LEN)) + return BTRFS_INODE_FIELD_TRANSID; + if (!strncmp(field, "block_group", FIELD_BUF_LEN)) + return BTRFS_INODE_FIELD_BLOCK_GROUP; + if (!strncmp(field, "mode", FIELD_BUF_LEN)) + return BTRFS_INODE_FIELD_MODE; + if (!strncmp(field, "uid", FIELD_BUF_LEN)) + return BTRFS_INODE_FIELD_UID; + if (!strncmp(field, "gid", FIELD_BUF_LEN)) + return BTRFS_INODE_FIELD_GID; return BTRFS_INODE_FIELD_BAD; } @@ -384,7 +418,7 @@ static u64 generate_u64(u64 orig) { u64 ret; do { - ret = rand(); + ret = rand_u64(); } while (ret == orig); return ret; } @@ -393,7 +427,7 @@ static u32 generate_u32(u32 orig) { u32 ret; do { - ret = rand(); + ret = rand_u32(); } while (ret == orig); return ret; } @@ -402,7 +436,7 @@ static u8 generate_u8(u8 orig) { u8 ret; do { - ret = rand(); + ret = rand_u8(); } while (ret == orig); return ret; } @@ -470,7 +504,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; @@ -509,17 +543,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); @@ -591,6 +619,46 @@ 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; + case BTRFS_INODE_FIELD_NLINK: + orig = btrfs_inode_nlink(path->nodes[0], ei); + bogus = generate_u32(orig); + btrfs_set_inode_nlink(path->nodes[0], ei, bogus); + break; + case BTRFS_INODE_FIELD_GENERATION: + orig = btrfs_inode_generation(path->nodes[0], ei); + bogus = generate_u64(orig); + btrfs_set_inode_generation(path->nodes[0], ei, bogus); + break; + case BTRFS_INODE_FIELD_TRANSID: + orig = btrfs_inode_transid(path->nodes[0], ei); + bogus = generate_u64(orig); + btrfs_set_inode_transid(path->nodes[0], ei, bogus); + break; + case BTRFS_INODE_FIELD_BLOCK_GROUP: + orig = btrfs_inode_block_group(path->nodes[0], ei); + bogus = generate_u64(orig); + btrfs_set_inode_block_group(path->nodes[0], ei, bogus); + break; + case BTRFS_INODE_FIELD_MODE: + orig = btrfs_inode_mode(path->nodes[0], ei); + bogus = generate_u32(orig); + btrfs_set_inode_mode(path->nodes[0], ei, bogus); + break; + case BTRFS_INODE_FIELD_UID: + orig = btrfs_inode_uid(path->nodes[0], ei); + bogus = generate_u32(orig); + btrfs_set_inode_uid(path->nodes[0], ei, bogus); + break; + case BTRFS_INODE_FIELD_GID: + orig = btrfs_inode_gid(path->nodes[0], ei); + bogus = generate_u32(orig); + btrfs_set_inode_gid(path->nodes[0], ei, bogus); + break; default: ret = -EINVAL; break; @@ -658,7 +726,7 @@ out: static void shift_items(struct btrfs_root *root, struct extent_buffer *eb) { int nritems = btrfs_header_nritems(eb); - int shift_space = btrfs_leaf_free_space(root, eb) / 2; + int shift_space = btrfs_leaf_free_space(root->fs_info, eb) / 2; int slot = nritems / 2; int i = 0; unsigned int data_end = btrfs_item_offset_nr(eb, nritems - 1); @@ -676,10 +744,11 @@ static void shift_items(struct btrfs_root *root, struct extent_buffer *eb) } } -static int corrupt_metadata_block(struct btrfs_root *root, u64 block, +static int corrupt_metadata_block(struct btrfs_fs_info *fs_info, u64 block, char *field) { struct btrfs_trans_handle *trans; + struct btrfs_root *root; struct btrfs_path *path; struct extent_buffer *eb; struct btrfs_key key, root_key; @@ -695,8 +764,8 @@ static int corrupt_metadata_block(struct btrfs_root *root, u64 block, return -EINVAL; } - eb = read_tree_block(root, block, root->leafsize, 0); - if (!eb) { + eb = read_tree_block(fs_info, block, 0); + if (!extent_buffer_uptodate(eb)) { fprintf(stderr, "Couldn't read in tree block %s\n", field); return -EINVAL; } @@ -712,9 +781,9 @@ static int corrupt_metadata_block(struct btrfs_root *root, u64 block, root_key.type = BTRFS_ROOT_ITEM_KEY; root_key.offset = (u64)-1; - root = btrfs_read_fs_root(root->fs_info, &root_key); + root = btrfs_read_fs_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); } @@ -845,12 +914,32 @@ out: return ret; } +static int delete_csum(struct btrfs_root *root, u64 bytenr, u64 bytes) +{ + struct btrfs_trans_handle *trans; + int ret; + + root = root->fs_info->csum_root; + trans = btrfs_start_transaction(root, 1); + if (IS_ERR(trans)) { + fprintf(stderr, "Couldn't start transaction %ld\n", + PTR_ERR(trans)); + return PTR_ERR(trans); + } + + ret = btrfs_del_csums(trans, root, bytenr, bytes); + if (ret) + fprintf(stderr, "Error deleting csums %d\n", ret); + btrfs_commit_transaction(trans, root); + return ret; +} + /* 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) { @@ -867,7 +956,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", @@ -883,7 +976,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; @@ -919,7 +1012,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; @@ -946,7 +1039,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) @@ -956,7 +1049,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; @@ -980,19 +1073,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; @@ -1010,38 +1102,41 @@ int main(int ac, char **av) u64 metadata_block = 0; u64 inode = 0; u64 file_extent = (u64)-1; + u64 root_objectid = 0; + u64 csum_bytenr = 0; char field[FIELD_BUF_LEN]; field[0] = '\0'; - srand(128); memset(&key, 0, sizeof(key)); while(1) { int c; - int option_index = 0; static const struct option long_options[] = { /* { "byte-count", 1, NULL, 'b' }, */ - { "logical", 1, NULL, 'l' }, - { "copy", 1, NULL, 'c' }, - { "bytes", 1, NULL, 'b' }, - { "extent-record", 0, NULL, 'e' }, - { "extent-tree", 0, NULL, 'E' }, - { "keys", 0, NULL, 'k' }, - { "chunk-record", 0, NULL, 'u' }, - { "chunk-tree", 0, NULL, 'U' }, - { "inode", 1, NULL, 'i'}, - { "file-extent", 1, NULL, 'x'}, - { "metadata-block", 1, NULL, 'm'}, - { "field", 1, NULL, 'f'}, - { "key", 1, NULL, 'K'}, - { "item", 0, NULL, 'I'}, - { "dir-item", 0, NULL, 'D'}, - { "delete", 0, NULL, 'd'}, - { 0, 0, 0, 0} + { "logical", required_argument, NULL, 'l' }, + { "copy", required_argument, NULL, 'c' }, + { "bytes", required_argument, NULL, 'b' }, + { "extent-record", no_argument, NULL, 'e' }, + { "extent-tree", no_argument, NULL, 'E' }, + { "keys", no_argument, NULL, 'k' }, + { "chunk-record", no_argument, NULL, 'u' }, + { "chunk-tree", no_argument, NULL, 'U' }, + { "inode", required_argument, NULL, 'i'}, + { "file-extent", required_argument, NULL, 'x'}, + { "metadata-block", required_argument, NULL, 'm'}, + { "field", required_argument, NULL, 'f'}, + { "key", required_argument, NULL, 'K'}, + { "item", no_argument, NULL, 'I'}, + { "dir-item", no_argument, NULL, 'D'}, + { "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:IDd", long_options, - &option_index); + c = getopt_long(argc, argv, "l:c:b:eEkuUi:f:x:m:K:IDdr:C:", + long_options, NULL); if (c < 0) break; switch(c) { @@ -1089,7 +1184,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': @@ -1101,15 +1196,21 @@ int main(int ac, char **av) case 'd': delete = 1; break; + case 'r': + root_objectid = arg_strtou64(optarg); + break; + 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); @@ -1123,15 +1224,18 @@ 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); + BUG_ON(IS_ERR(trans)); + ret = corrupt_extent(trans, root, logical); btrfs_commit_transaction(trans, root); goto out_close; } if (extent_tree) { struct btrfs_trans_handle *trans; + trans = btrfs_start_transaction(root, 1); + BUG_ON(IS_ERR(trans)); btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root, root->fs_info->extent_root->node); btrfs_commit_transaction(trans, root); @@ -1143,8 +1247,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"); @@ -1157,6 +1261,7 @@ int main(int ac, char **av) goto out_close; } trans = btrfs_start_transaction(root, 1); + BUG_ON(IS_ERR(trans)); ret = corrupt_item_nocow(trans, root->fs_info->chunk_root, path, del); if (ret < 0) @@ -1166,7 +1271,9 @@ int main(int ac, char **av) } if (chunk_tree) { struct btrfs_trans_handle *trans; + trans = btrfs_start_transaction(root, 1); + BUG_ON(IS_ERR(trans)); ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root); if (ret < 0) fprintf(stderr, "Failed to corrupt chunk tree\n"); @@ -1176,10 +1283,11 @@ 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); + BUG_ON(IS_ERR(trans)); if (file_extent == (u64)-1) { printf("corrupting inode\n"); ret = corrupt_inode(trans, root, inode, field); @@ -1192,31 +1300,52 @@ int main(int ac, char **av) goto out_close; } if (metadata_block) { - if (!strlen(field)) - print_usage(); - ret = corrupt_metadata_block(root, metadata_block, field); + if (*field == 0) + print_usage(1); + ret = corrupt_metadata_block(root->fs_info, 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; } + if (csum_bytenr) { + ret = delete_csum(root, csum_bytenr, bytes); + goto out_close; + } 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(); - ret = delete_item(root, &key); + print_usage(1); + if (root_objectid) { + struct btrfs_key root_key; + + root_key.objectid = root_objectid; + root_key.type = BTRFS_ROOT_ITEM_KEY; + root_key.offset = (u64)-1; + + target = btrfs_read_fs_root(root->fs_info, &root_key); + if (IS_ERR(target)) { + fprintf(stderr, "Couldn't find root %llu\n", + (unsigned long long)root_objectid); + 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; } @@ -1225,27 +1354,38 @@ 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; + bytes = root->fs_info->sectorsize; - bytes = (bytes + root->sectorsize - 1) / root->sectorsize; - bytes *= root->sectorsize; + bytes = round_up(bytes, root->fs_info->sectorsize); while (bytes > 0) { if (corrupt_block_keys) { - corrupt_keys_in_block(root, logical); + corrupt_keys_in_block(root->fs_info, 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); + 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->fs_info->sectorsize, copy); free_extent_buffer(eb); } - logical += root->sectorsize; - bytes -= root->sectorsize; + logical += root->fs_info->sectorsize; + bytes -= root->fs_info->sectorsize; } return ret; out_close: