X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=btrfs-corrupt-block.c;h=71b4d7711451637d72bfb82e8bb20569ae492075;hb=c1c987503412a8e98e40da2548c6b78888535641;hp=fb6fcaadfe297ea34d140b3d2692c66eb1a26f50;hpb=ee0908ee81e55d730402cd4931513579b34ea792;p=platform%2Fupstream%2Fbtrfs-progs.git diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c index fb6fcaa..71b4d77 100644 --- a/btrfs-corrupt-block.c +++ b/btrfs-corrupt-block.c @@ -31,30 +31,32 @@ #include "transaction.h" #include "list.h" #include "utils.h" +#include "help.h" #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++; @@ -64,14 +66,26 @@ static struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, "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); } @@ -84,37 +98,30 @@ 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) { - 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"); + 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); } @@ -131,8 +138,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; @@ -172,7 +179,7 @@ static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr) } 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; @@ -181,7 +188,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) @@ -258,13 +265,13 @@ 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, @@ -282,7 +289,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; } @@ -302,6 +309,13 @@ 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, }; @@ -340,6 +354,20 @@ static enum btrfs_inode_field convert_inode_field(char *field) 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; } @@ -391,7 +419,7 @@ static u64 generate_u64(u64 orig) { u64 ret; do { - ret = rand(); + ret = rand_u64(); } while (ret == orig); return ret; } @@ -400,7 +428,7 @@ static u32 generate_u32(u32 orig) { u32 ret; do { - ret = rand(); + ret = rand_u32(); } while (ret == orig); return ret; } @@ -409,7 +437,7 @@ static u8 generate_u8(u8 orig) { u8 ret; do { - ret = rand(); + ret = rand_u8(); } while (ret == orig); return ret; } @@ -597,6 +625,41 @@ static int corrupt_inode(struct btrfs_trans_handle *trans, 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; @@ -893,7 +956,11 @@ static 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", @@ -945,7 +1012,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; @@ -972,7 +1039,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) @@ -1018,7 +1085,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; @@ -1041,7 +1107,6 @@ int main(int argc, char **argv) char field[FIELD_BUF_LEN]; field[0] = '\0'; - srand(128); memset(&key, 0, sizeof(key)); while(1) { @@ -1161,7 +1226,7 @@ int main(int argc, char **argv) if (logical == (u64)-1) print_usage(1); trans = btrfs_start_transaction(root, 1); - ret = corrupt_extent (trans, root, logical, 0); + ret = corrupt_extent(trans, root, logical); btrfs_commit_transaction(trans, root); goto out_close; } @@ -1180,7 +1245,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"); @@ -1296,8 +1361,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;