2 * Copyright (C) 2009 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #define _XOPEN_SOURCE 500
26 #include "kerncompat.h"
30 #include "print-tree.h"
31 #include "transaction.h"
36 #define FIELD_BUF_LEN 80
38 struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
39 u32 blocksize, int copy)
42 struct extent_buffer *eb;
44 struct btrfs_multi_bio *multi = NULL;
45 struct btrfs_device *device;
49 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
55 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
56 eb->start, &length, &multi,
59 device = multi->stripes[0].dev;
62 eb->dev_bytenr = multi->stripes[0].physical;
65 "mirror %d logical %llu physical %llu device %s\n",
66 mirror_num, (unsigned long long)bytenr,
67 (unsigned long long)eb->dev_bytenr, device->name);
70 if (!copy || mirror_num == copy) {
71 ret = read_extent_from_disk(eb, 0, eb->len);
72 printf("corrupting %llu copy %d\n", eb->start,
74 memset(eb->data, 0, eb->len);
75 write_extent_to_disk(eb);
79 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
85 if (mirror_num > num_copies)
91 static void print_usage(void)
93 fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n");
94 fprintf(stderr, "\t-l Logical extent to be corrupted\n");
95 fprintf(stderr, "\t-c Copy of the extent to be corrupted"
96 " (usually 1 or 2, default: 0)\n");
97 fprintf(stderr, "\t-b Number of bytes to be corrupted\n");
98 fprintf(stderr, "\t-e Extent to be corrupted\n");
99 fprintf(stderr, "\t-E The whole extent tree to be corrupted\n");
100 fprintf(stderr, "\t-u Given chunk item to be corrupted\n");
101 fprintf(stderr, "\t-U The whole chunk tree to be corrupted\n");
102 fprintf(stderr, "\t-i The inode item to corrupt (must also specify "
103 "the field to corrupt)\n");
104 fprintf(stderr, "\t-x The file extent item to corrupt (must also "
105 "specify -i for the inode and -f for the field to corrupt)\n");
106 fprintf(stderr, "\t-m The metadata block to corrupt (must also "
107 "specify -f for the field to corrupt)\n");
108 fprintf(stderr, "\t-K The key to corrupt in the format "
109 "<num>,<num>,<num> (must also specify -f for the field)\n");
110 fprintf(stderr, "\t-f The field in the item to corrupt\n");
114 static void corrupt_keys(struct btrfs_trans_handle *trans,
115 struct btrfs_root *root,
116 struct extent_buffer *eb)
121 struct btrfs_disk_key bad_key;;
123 nr = btrfs_header_nritems(eb);
128 bad_slot = rand() % nr;
130 if (bad_slot == slot)
134 "corrupting keys in block %llu slot %d swapping with %d\n",
135 (unsigned long long)eb->start, slot, bad_slot);
137 if (btrfs_header_level(eb) == 0) {
138 btrfs_item_key(eb, &bad_key, bad_slot);
139 btrfs_set_item_key(eb, &bad_key, slot);
141 btrfs_node_key(eb, &bad_key, bad_slot);
142 btrfs_set_node_key(eb, &bad_key, slot);
144 btrfs_mark_buffer_dirty(eb);
147 btrfs_super_csum_size(root->fs_info->super_copy);
148 csum_tree_block_size(eb, csum_size, 0);
149 write_extent_to_disk(eb);
154 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
156 struct extent_buffer *eb;
158 eb = read_tree_block(root, bytenr, root->leafsize, 0);
162 corrupt_keys(NULL, root, eb);
163 free_extent_buffer(eb);
167 static int corrupt_extent(struct btrfs_trans_handle *trans,
168 struct btrfs_root *root, u64 bytenr, int copy)
170 struct btrfs_key key;
171 struct extent_buffer *leaf;
174 struct btrfs_path *path;
177 int should_del = rand() % 3;
179 path = btrfs_alloc_path();
183 key.objectid = bytenr;
185 key.offset = (u64)-1;
188 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
194 if (path->slots[0] == 0)
199 leaf = path->nodes[0];
200 slot = path->slots[0];
201 btrfs_item_key_to_cpu(leaf, &key, slot);
202 if (key.objectid != bytenr)
205 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
206 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
207 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
208 key.type != BTRFS_EXTENT_REF_V0_KEY &&
209 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
210 key.type != BTRFS_SHARED_DATA_REF_KEY)
215 "deleting extent record: key %llu %u %llu\n",
216 key.objectid, key.type, key.offset);
218 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
219 /* make sure this extent doesn't get
220 * reused for other purposes */
221 btrfs_pin_extent(root->fs_info,
222 key.objectid, key.offset);
225 btrfs_del_item(trans, root, path);
228 "corrupting extent record: key %llu %u %llu\n",
229 key.objectid, key.type, key.offset);
230 ptr = btrfs_item_ptr_offset(leaf, slot);
231 item_size = btrfs_item_size_nr(leaf, slot);
232 memset_extent_buffer(leaf, 0, ptr, item_size);
233 btrfs_mark_buffer_dirty(leaf);
236 btrfs_release_path(path);
244 btrfs_free_path(path);
248 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
249 struct btrfs_root *root,
250 struct extent_buffer *eb)
252 u32 nr = btrfs_header_nritems(eb);
253 u32 victim = rand() % nr;
255 struct btrfs_key key;
257 btrfs_item_key_to_cpu(eb, &key, victim);
258 objectid = key.objectid;
259 corrupt_extent(trans, root, objectid, 1);
262 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
263 struct btrfs_root *root,
264 struct extent_buffer *eb)
272 nr = btrfs_header_nritems(eb);
273 if (btrfs_is_leaf(eb)) {
274 btrfs_corrupt_extent_leaf(trans, root, eb);
278 if (btrfs_header_level(eb) == 1 && eb != root->node) {
283 for (i = 0; i < nr; i++) {
284 struct extent_buffer *next;
286 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
288 btrfs_node_ptr_generation(eb, i));
291 btrfs_corrupt_extent_tree(trans, root, next);
292 free_extent_buffer(next);
296 enum btrfs_inode_field {
297 BTRFS_INODE_FIELD_ISIZE,
298 BTRFS_INODE_FIELD_BAD,
301 enum btrfs_file_extent_field {
302 BTRFS_FILE_EXTENT_DISK_BYTENR,
303 BTRFS_FILE_EXTENT_BAD,
306 enum btrfs_metadata_block_field {
307 BTRFS_METADATA_BLOCK_GENERATION,
308 BTRFS_METADATA_BLOCK_BAD,
311 enum btrfs_key_field {
318 static enum btrfs_inode_field convert_inode_field(char *field)
320 if (!strncmp(field, "isize", FIELD_BUF_LEN))
321 return BTRFS_INODE_FIELD_ISIZE;
322 return BTRFS_INODE_FIELD_BAD;
325 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
327 if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
328 return BTRFS_FILE_EXTENT_DISK_BYTENR;
329 return BTRFS_FILE_EXTENT_BAD;
332 static enum btrfs_metadata_block_field
333 convert_metadata_block_field(char *field)
335 if (!strncmp(field, "generation", FIELD_BUF_LEN))
336 return BTRFS_METADATA_BLOCK_GENERATION;
337 return BTRFS_METADATA_BLOCK_BAD;
340 static enum btrfs_key_field convert_key_field(char *field)
342 if (!strncmp(field, "objectid", FIELD_BUF_LEN))
343 return BTRFS_KEY_OBJECTID;
344 if (!strncmp(field, "type", FIELD_BUF_LEN))
345 return BTRFS_KEY_TYPE;
346 if (!strncmp(field, "offset", FIELD_BUF_LEN))
347 return BTRFS_KEY_OFFSET;
348 return BTRFS_KEY_BAD;
351 static u64 generate_u64(u64 orig)
356 } while (ret == orig);
360 static u8 generate_u8(u8 orig)
365 } while (ret == orig);
369 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
372 enum btrfs_key_field corrupt_field = convert_key_field(field);
373 struct btrfs_path *path;
374 struct btrfs_trans_handle *trans;
377 root = root->fs_info->fs_root;
378 if (corrupt_field == BTRFS_KEY_BAD) {
379 fprintf(stderr, "Invalid field %s\n", field);
383 path = btrfs_alloc_path();
387 trans = btrfs_start_transaction(root, 1);
389 btrfs_free_path(path);
390 return PTR_ERR(trans);
393 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
397 fprintf(stderr, "Couldn't find the key to corrupt\n");
402 switch (corrupt_field) {
403 case BTRFS_KEY_OBJECTID:
404 key->objectid = generate_u64(key->objectid);
407 key->type = generate_u8(key->type);
409 case BTRFS_KEY_OFFSET:
410 key->offset = generate_u64(key->objectid);
413 fprintf(stderr, "Invalid field %s, %d\n", field,
419 btrfs_set_item_key_unsafe(root, path, key);
421 btrfs_free_path(path);
422 btrfs_commit_transaction(trans, root);
427 static int corrupt_inode(struct btrfs_trans_handle *trans,
428 struct btrfs_root *root, u64 inode, char *field)
430 struct btrfs_inode_item *ei;
431 struct btrfs_path *path;
432 struct btrfs_key key;
433 enum btrfs_inode_field corrupt_field = convert_inode_field(field);
438 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
439 fprintf(stderr, "Invalid field %s\n", field);
443 key.objectid = inode;
444 key.type = BTRFS_INODE_ITEM_KEY;
445 key.offset = (u64)-1;
447 path = btrfs_alloc_path();
451 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
455 if (!path->slots[0]) {
456 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
464 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
465 if (key.objectid != inode) {
466 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
471 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
472 struct btrfs_inode_item);
473 switch (corrupt_field) {
474 case BTRFS_INODE_FIELD_ISIZE:
475 orig = btrfs_inode_size(path->nodes[0], ei);
476 bogus = generate_u64(orig);
477 btrfs_set_inode_size(path->nodes[0], ei, bogus);
483 btrfs_mark_buffer_dirty(path->nodes[0]);
485 btrfs_free_path(path);
489 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
490 struct btrfs_root *root, u64 inode, u64 extent,
493 struct btrfs_file_extent_item *fi;
494 struct btrfs_path *path;
495 struct btrfs_key key;
496 enum btrfs_file_extent_field corrupt_field;
501 corrupt_field = convert_file_extent_field(field);
502 if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
503 fprintf(stderr, "Invalid field %s\n", field);
507 key.objectid = inode;
508 key.type = BTRFS_EXTENT_DATA_KEY;
511 path = btrfs_alloc_path();
515 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
519 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
525 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
526 struct btrfs_file_extent_item);
527 switch (corrupt_field) {
528 case BTRFS_FILE_EXTENT_DISK_BYTENR:
529 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
530 bogus = generate_u64(orig);
531 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
537 btrfs_mark_buffer_dirty(path->nodes[0]);
539 btrfs_free_path(path);
543 static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
546 struct btrfs_trans_handle *trans;
547 struct btrfs_path *path;
548 struct extent_buffer *eb;
549 struct btrfs_key key, root_key;
550 enum btrfs_metadata_block_field corrupt_field;
556 corrupt_field = convert_metadata_block_field(field);
557 if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
558 fprintf(stderr, "Invalid field %s\n", field);
562 eb = read_tree_block(root, block, root->leafsize, 0);
564 fprintf(stderr, "Couldn't read in tree block %s\n", field);
567 root_objectid = btrfs_header_owner(eb);
568 level = btrfs_header_level(eb);
570 btrfs_node_key_to_cpu(eb, &key, 0);
572 btrfs_item_key_to_cpu(eb, &key, 0);
573 free_extent_buffer(eb);
575 root_key.objectid = root_objectid;
576 root_key.type = BTRFS_ROOT_ITEM_KEY;
577 root_key.offset = (u64)-1;
579 root = btrfs_read_fs_root(root->fs_info, &root_key);
581 fprintf(stderr, "Couldn't finde owner root %llu\n",
583 return PTR_ERR(root);
586 path = btrfs_alloc_path();
590 trans = btrfs_start_transaction(root, 1);
592 btrfs_free_path(path);
593 fprintf(stderr, "Couldn't start transaction %ld\n",
595 return PTR_ERR(trans);
598 path->lowest_level = level;
599 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
601 fprintf(stderr, "Error searching to node %d\n", ret);
604 eb = path->nodes[level];
607 switch (corrupt_field) {
608 case BTRFS_METADATA_BLOCK_GENERATION:
609 orig = btrfs_header_generation(eb);
610 bogus = generate_u64(orig);
611 btrfs_set_header_generation(eb, bogus);
617 btrfs_mark_buffer_dirty(path->nodes[level]);
619 btrfs_commit_transaction(trans, root);
620 btrfs_free_path(path);
624 static struct option long_options[] = {
625 /* { "byte-count", 1, NULL, 'b' }, */
626 { "logical", 1, NULL, 'l' },
627 { "copy", 1, NULL, 'c' },
628 { "bytes", 1, NULL, 'b' },
629 { "extent-record", 0, NULL, 'e' },
630 { "extent-tree", 0, NULL, 'E' },
631 { "keys", 0, NULL, 'k' },
632 { "chunk-record", 0, NULL, 'u' },
633 { "chunk-tree", 0, NULL, 'U' },
634 { "inode", 1, NULL, 'i'},
635 { "file-extent", 1, NULL, 'x'},
636 { "metadata-block", 1, NULL, 'm'},
637 { "field", 1, NULL, 'f'},
638 { "key", 1, NULL, 'K'},
642 /* corrupt item using NO cow.
643 * Because chunk recover will recover based on whole partition scaning,
644 * If using COW, chunk recover will use the old item to recover,
645 * which is still OK but we want to check the ability to rebuild chunk
646 * not only restore the old ones */
647 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
648 struct btrfs_root *root, struct btrfs_path *path,
652 struct btrfs_key key;
653 struct extent_buffer *leaf;
658 leaf = path->nodes[0];
659 slot = path->slots[0];
660 /* Not deleting the first item of a leaf to keep leaf structure */
663 /* Only accept valid eb */
664 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
665 btrfs_item_key_to_cpu(leaf, &key, slot);
667 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
668 key.objectid, key.type, key.offset);
669 btrfs_del_item(trans, root, path);
671 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
672 key.objectid, key.type, key.offset);
673 ptr = btrfs_item_ptr_offset(leaf, slot);
674 item_size = btrfs_item_size_nr(leaf, slot);
675 memset_extent_buffer(leaf, 0, ptr, item_size);
676 btrfs_mark_buffer_dirty(leaf);
680 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
681 struct btrfs_root *root)
686 struct btrfs_path *path;
687 struct btrfs_key key;
688 struct btrfs_key found_key;
689 struct extent_buffer *leaf;
691 path = btrfs_alloc_path();
695 key.objectid = (u64)-1;
696 key.offset = (u64)-1;
699 /* Here, cow and ins_len must equals 0 for the following reasons:
700 * 1) chunk recover is based on disk scanning, so COW should be
701 * disabled in case the original chunk being scanned and
702 * recovered using the old chunk.
703 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
706 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
709 fprintf(stderr, "Error searching tree\n");
712 /* corrupt/del dev_item first */
713 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
714 slot = path->slots[0];
715 leaf = path->nodes[0];
717 /* Never delete the first item to keep the leaf structure */
718 if (path->slots[0] == 0)
720 ret = corrupt_item_nocow(trans, root, path, del);
724 btrfs_release_path(path);
726 /* Here, cow and ins_len must equals 0 for the following reasons:
727 * 1) chunk recover is based on disk scanning, so COW should be
728 * disabled in case the original chunk being scanned and
729 * recovered using the old chunk.
730 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
733 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
736 fprintf(stderr, "Error searching tree\n");
739 /* corrupt/del chunk then*/
740 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
741 slot = path->slots[0];
742 leaf = path->nodes[0];
744 btrfs_item_key_to_cpu(leaf, &found_key, slot);
745 ret = corrupt_item_nocow(trans, root, path, del);
750 btrfs_free_path(path);
753 int find_chunk_offset(struct btrfs_root *root,
754 struct btrfs_path *path, u64 offset)
756 struct btrfs_key key;
759 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
760 key.type = BTRFS_CHUNK_ITEM_KEY;
763 /* Here, cow and ins_len must equals 0 for following reasons:
764 * 1) chunk recover is based on disk scanning, so COW should
765 * be disabled in case the original chunk being scanned
766 * and recovered using the old chunk.
767 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
770 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
772 fprintf(stderr, "Can't find chunk with given offset %llu\n",
777 fprintf(stderr, "Error searching chunk");
784 int main(int ac, char **av)
786 struct cache_tree root_cache;
787 struct btrfs_key key;
788 struct btrfs_root *root;
789 struct extent_buffer *eb;
791 /* chunk offset can be 0,so change to (u64)-1 */
792 u64 logical = (u64)-1;
794 int option_index = 0;
799 int corrupt_block_keys = 0;
802 u64 metadata_block = 0;
804 u64 file_extent = (u64)-1;
805 char field[FIELD_BUF_LEN];
809 memset(&key, 0, sizeof(key));
813 c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:", long_options,
819 logical = atoll(optarg);
825 "invalid copy number\n");
830 bytes = atoll(optarg);
833 "invalid byte count\n");
844 corrupt_block_keys = 1;
852 inode = atoll(optarg);
855 "invalid inode number\n");
860 strncpy(field, optarg, FIELD_BUF_LEN);
864 file_extent = atoll(optarg);
866 fprintf(stderr, "error converting "
873 metadata_block = atoll(optarg);
875 fprintf(stderr, "error converting "
881 ret = sscanf(optarg, "%llu,%u,%llu",
883 (unsigned int *)&key.type,
886 fprintf(stderr, "error reading key "
901 cache_tree_init(&root_cache);
903 root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
905 fprintf(stderr, "Open ctree failed\n");
909 struct btrfs_trans_handle *trans;
911 if (logical == (u64)-1)
913 trans = btrfs_start_transaction(root, 1);
914 ret = corrupt_extent (trans, root, logical, 0);
915 btrfs_commit_transaction(trans, root);
919 struct btrfs_trans_handle *trans;
920 trans = btrfs_start_transaction(root, 1);
921 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
922 root->fs_info->extent_root->node);
923 btrfs_commit_transaction(trans, root);
927 struct btrfs_trans_handle *trans;
928 struct btrfs_path *path;
931 if (logical == (u64)-1)
934 path = btrfs_alloc_path();
936 fprintf(stderr, "path allocation failed\n");
940 if (find_chunk_offset(root->fs_info->chunk_root, path,
942 btrfs_free_path(path);
945 trans = btrfs_start_transaction(root, 1);
946 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
949 fprintf(stderr, "Failed to corrupt chunk record\n");
950 btrfs_commit_transaction(trans, root);
954 struct btrfs_trans_handle *trans;
955 trans = btrfs_start_transaction(root, 1);
956 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
958 fprintf(stderr, "Failed to corrupt chunk tree\n");
959 btrfs_commit_transaction(trans, root);
963 struct btrfs_trans_handle *trans;
968 trans = btrfs_start_transaction(root, 1);
969 if (file_extent == (u64)-1) {
970 printf("corrupting inode\n");
971 ret = corrupt_inode(trans, root, inode, field);
973 printf("corrupting file extent\n");
974 ret = corrupt_file_extent(trans, root, inode,
977 btrfs_commit_transaction(trans, root);
980 if (metadata_block) {
983 ret = corrupt_metadata_block(root, metadata_block, field);
986 if (key.objectid || key.offset || key.type) {
989 ret = corrupt_key(root, &key, field);
993 * If we made it here and we have extent set then we didn't specify
994 * inode and we're screwed.
996 if (file_extent != (u64)-1)
999 if (logical == (u64)-1)
1003 bytes = root->sectorsize;
1005 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
1006 bytes *= root->sectorsize;
1009 if (corrupt_block_keys) {
1010 corrupt_keys_in_block(root, logical);
1012 eb = debug_corrupt_block(root, logical,
1013 root->sectorsize, copy);
1014 free_extent_buffer(eb);
1016 logical += root->sectorsize;
1017 bytes -= root->sectorsize;