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, u64 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, u64 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)
271 if (btrfs_is_leaf(eb)) {
272 btrfs_corrupt_extent_leaf(trans, root, eb);
276 if (btrfs_header_level(eb) == 1 && eb != root->node) {
281 for (i = 0; i < btrfs_header_nritems(eb); i++) {
282 struct extent_buffer *next;
284 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
286 btrfs_node_ptr_generation(eb, i));
289 btrfs_corrupt_extent_tree(trans, root, next);
290 free_extent_buffer(next);
294 enum btrfs_inode_field {
295 BTRFS_INODE_FIELD_ISIZE,
296 BTRFS_INODE_FIELD_BAD,
299 enum btrfs_file_extent_field {
300 BTRFS_FILE_EXTENT_DISK_BYTENR,
301 BTRFS_FILE_EXTENT_BAD,
304 enum btrfs_metadata_block_field {
305 BTRFS_METADATA_BLOCK_GENERATION,
306 BTRFS_METADATA_BLOCK_BAD,
309 enum btrfs_key_field {
316 static enum btrfs_inode_field convert_inode_field(char *field)
318 if (!strncmp(field, "isize", FIELD_BUF_LEN))
319 return BTRFS_INODE_FIELD_ISIZE;
320 return BTRFS_INODE_FIELD_BAD;
323 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
325 if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
326 return BTRFS_FILE_EXTENT_DISK_BYTENR;
327 return BTRFS_FILE_EXTENT_BAD;
330 static enum btrfs_metadata_block_field
331 convert_metadata_block_field(char *field)
333 if (!strncmp(field, "generation", FIELD_BUF_LEN))
334 return BTRFS_METADATA_BLOCK_GENERATION;
335 return BTRFS_METADATA_BLOCK_BAD;
338 static enum btrfs_key_field convert_key_field(char *field)
340 if (!strncmp(field, "objectid", FIELD_BUF_LEN))
341 return BTRFS_KEY_OBJECTID;
342 if (!strncmp(field, "type", FIELD_BUF_LEN))
343 return BTRFS_KEY_TYPE;
344 if (!strncmp(field, "offset", FIELD_BUF_LEN))
345 return BTRFS_KEY_OFFSET;
346 return BTRFS_KEY_BAD;
349 static u64 generate_u64(u64 orig)
354 } while (ret == orig);
358 static u8 generate_u8(u8 orig)
363 } while (ret == orig);
367 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
370 enum btrfs_key_field corrupt_field = convert_key_field(field);
371 struct btrfs_path *path;
372 struct btrfs_trans_handle *trans;
375 root = root->fs_info->fs_root;
376 if (corrupt_field == BTRFS_KEY_BAD) {
377 fprintf(stderr, "Invalid field %s\n", field);
381 path = btrfs_alloc_path();
385 trans = btrfs_start_transaction(root, 1);
387 btrfs_free_path(path);
388 return PTR_ERR(trans);
391 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
395 fprintf(stderr, "Couldn't find the key to corrupt\n");
400 switch (corrupt_field) {
401 case BTRFS_KEY_OBJECTID:
402 key->objectid = generate_u64(key->objectid);
405 key->type = generate_u8(key->type);
407 case BTRFS_KEY_OFFSET:
408 key->offset = generate_u64(key->objectid);
411 fprintf(stderr, "Invalid field %s, %d\n", field,
417 btrfs_set_item_key_unsafe(root, path, key);
419 btrfs_free_path(path);
420 btrfs_commit_transaction(trans, root);
425 static int corrupt_inode(struct btrfs_trans_handle *trans,
426 struct btrfs_root *root, u64 inode, char *field)
428 struct btrfs_inode_item *ei;
429 struct btrfs_path *path;
430 struct btrfs_key key;
431 enum btrfs_inode_field corrupt_field = convert_inode_field(field);
436 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
437 fprintf(stderr, "Invalid field %s\n", field);
441 key.objectid = inode;
442 key.type = BTRFS_INODE_ITEM_KEY;
443 key.offset = (u64)-1;
445 path = btrfs_alloc_path();
449 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
453 if (!path->slots[0]) {
454 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
462 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
463 if (key.objectid != inode) {
464 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
469 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
470 struct btrfs_inode_item);
471 switch (corrupt_field) {
472 case BTRFS_INODE_FIELD_ISIZE:
473 orig = btrfs_inode_size(path->nodes[0], ei);
474 bogus = generate_u64(orig);
475 btrfs_set_inode_size(path->nodes[0], ei, bogus);
481 btrfs_mark_buffer_dirty(path->nodes[0]);
483 btrfs_free_path(path);
487 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
488 struct btrfs_root *root, u64 inode, u64 extent,
491 struct btrfs_file_extent_item *fi;
492 struct btrfs_path *path;
493 struct btrfs_key key;
494 enum btrfs_file_extent_field corrupt_field;
499 corrupt_field = convert_file_extent_field(field);
500 if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
501 fprintf(stderr, "Invalid field %s\n", field);
505 key.objectid = inode;
506 key.type = BTRFS_EXTENT_DATA_KEY;
509 path = btrfs_alloc_path();
513 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
517 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
523 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
524 struct btrfs_file_extent_item);
525 switch (corrupt_field) {
526 case BTRFS_FILE_EXTENT_DISK_BYTENR:
527 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
528 bogus = generate_u64(orig);
529 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
535 btrfs_mark_buffer_dirty(path->nodes[0]);
537 btrfs_free_path(path);
541 static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
544 struct btrfs_trans_handle *trans;
545 struct btrfs_path *path;
546 struct extent_buffer *eb;
547 struct btrfs_key key, root_key;
548 enum btrfs_metadata_block_field corrupt_field;
554 corrupt_field = convert_metadata_block_field(field);
555 if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
556 fprintf(stderr, "Invalid field %s\n", field);
560 eb = read_tree_block(root, block, root->leafsize, 0);
562 fprintf(stderr, "Couldn't read in tree block %s\n", field);
565 root_objectid = btrfs_header_owner(eb);
566 level = btrfs_header_level(eb);
568 btrfs_node_key_to_cpu(eb, &key, 0);
570 btrfs_item_key_to_cpu(eb, &key, 0);
571 free_extent_buffer(eb);
573 root_key.objectid = root_objectid;
574 root_key.type = BTRFS_ROOT_ITEM_KEY;
575 root_key.offset = (u64)-1;
577 root = btrfs_read_fs_root(root->fs_info, &root_key);
579 fprintf(stderr, "Couldn't finde owner root %llu\n",
581 return PTR_ERR(root);
584 path = btrfs_alloc_path();
588 trans = btrfs_start_transaction(root, 1);
590 btrfs_free_path(path);
591 fprintf(stderr, "Couldn't start transaction %ld\n",
593 return PTR_ERR(trans);
596 path->lowest_level = level;
597 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
599 fprintf(stderr, "Error searching to node %d\n", ret);
602 eb = path->nodes[level];
605 switch (corrupt_field) {
606 case BTRFS_METADATA_BLOCK_GENERATION:
607 orig = btrfs_header_generation(eb);
608 bogus = generate_u64(orig);
609 btrfs_set_header_generation(eb, bogus);
615 btrfs_mark_buffer_dirty(path->nodes[level]);
617 btrfs_commit_transaction(trans, root);
618 btrfs_free_path(path);
622 static struct option long_options[] = {
623 /* { "byte-count", 1, NULL, 'b' }, */
624 { "logical", 1, NULL, 'l' },
625 { "copy", 1, NULL, 'c' },
626 { "bytes", 1, NULL, 'b' },
627 { "extent-record", 0, NULL, 'e' },
628 { "extent-tree", 0, NULL, 'E' },
629 { "keys", 0, NULL, 'k' },
630 { "chunk-record", 0, NULL, 'u' },
631 { "chunk-tree", 0, NULL, 'U' },
632 { "inode", 1, NULL, 'i'},
633 { "file-extent", 1, NULL, 'x'},
634 { "metadata-block", 1, NULL, 'm'},
635 { "field", 1, NULL, 'f'},
636 { "key", 1, NULL, 'K'},
640 /* corrupt item using NO cow.
641 * Because chunk recover will recover based on whole partition scaning,
642 * If using COW, chunk recover will use the old item to recover,
643 * which is still OK but we want to check the ability to rebuild chunk
644 * not only restore the old ones */
645 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
646 struct btrfs_root *root, struct btrfs_path *path,
650 struct btrfs_key key;
651 struct extent_buffer *leaf;
656 leaf = path->nodes[0];
657 slot = path->slots[0];
658 /* Not deleting the first item of a leaf to keep leaf structure */
661 /* Only accept valid eb */
662 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
663 btrfs_item_key_to_cpu(leaf, &key, slot);
665 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
666 key.objectid, key.type, key.offset);
667 btrfs_del_item(trans, root, path);
669 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
670 key.objectid, key.type, key.offset);
671 ptr = btrfs_item_ptr_offset(leaf, slot);
672 item_size = btrfs_item_size_nr(leaf, slot);
673 memset_extent_buffer(leaf, 0, ptr, item_size);
674 btrfs_mark_buffer_dirty(leaf);
678 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
679 struct btrfs_root *root)
684 struct btrfs_path *path;
685 struct btrfs_key key;
686 struct btrfs_key found_key;
687 struct extent_buffer *leaf;
689 path = btrfs_alloc_path();
693 key.objectid = (u64)-1;
694 key.offset = (u64)-1;
697 /* Here, cow and ins_len must equals 0 for the following reasons:
698 * 1) chunk recover is based on disk scanning, so COW should be
699 * disabled in case the original chunk being scanned and
700 * recovered using the old chunk.
701 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
704 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
707 fprintf(stderr, "Error searching tree\n");
710 /* corrupt/del dev_item first */
711 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
712 slot = path->slots[0];
713 leaf = path->nodes[0];
715 /* Never delete the first item to keep the leaf structure */
716 if (path->slots[0] == 0)
718 ret = corrupt_item_nocow(trans, root, path, del);
722 btrfs_release_path(path);
724 /* Here, cow and ins_len must equals 0 for the following reasons:
725 * 1) chunk recover is based on disk scanning, so COW should be
726 * disabled in case the original chunk being scanned and
727 * recovered using the old chunk.
728 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
731 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
734 fprintf(stderr, "Error searching tree\n");
737 /* corrupt/del chunk then*/
738 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
739 slot = path->slots[0];
740 leaf = path->nodes[0];
742 btrfs_item_key_to_cpu(leaf, &found_key, slot);
743 ret = corrupt_item_nocow(trans, root, path, del);
748 btrfs_free_path(path);
751 int find_chunk_offset(struct btrfs_root *root,
752 struct btrfs_path *path, u64 offset)
754 struct btrfs_key key;
757 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
758 key.type = BTRFS_CHUNK_ITEM_KEY;
761 /* Here, cow and ins_len must equals 0 for following reasons:
762 * 1) chunk recover is based on disk scanning, so COW should
763 * be disabled in case the original chunk being scanned
764 * and recovered using the old chunk.
765 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
768 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
770 fprintf(stderr, "Can't find chunk with given offset %llu\n",
775 fprintf(stderr, "Error searching chunk");
782 int main(int ac, char **av)
784 struct cache_tree root_cache;
785 struct btrfs_key key;
786 struct btrfs_root *root;
787 struct extent_buffer *eb;
789 /* chunk offset can be 0,so change to (u64)-1 */
790 u64 logical = (u64)-1;
792 int option_index = 0;
797 int corrupt_block_keys = 0;
800 u64 metadata_block = 0;
802 u64 file_extent = (u64)-1;
803 char field[FIELD_BUF_LEN];
807 memset(&key, 0, sizeof(key));
811 c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:", long_options,
817 logical = arg_strtou64(optarg);
820 copy = arg_strtou64(optarg);
823 bytes = arg_strtou64(optarg);
832 corrupt_block_keys = 1;
840 inode = arg_strtou64(optarg);
843 strncpy(field, optarg, FIELD_BUF_LEN);
846 file_extent = arg_strtou64(optarg);
849 metadata_block = arg_strtou64(optarg);
852 ret = sscanf(optarg, "%llu,%u,%llu",
854 (unsigned int *)&key.type,
857 fprintf(stderr, "error reading key "
868 if (check_argc_min(ac, 1))
873 cache_tree_init(&root_cache);
875 root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
877 fprintf(stderr, "Open ctree failed\n");
881 struct btrfs_trans_handle *trans;
883 if (logical == (u64)-1)
885 trans = btrfs_start_transaction(root, 1);
886 ret = corrupt_extent (trans, root, logical, 0);
887 btrfs_commit_transaction(trans, root);
891 struct btrfs_trans_handle *trans;
892 trans = btrfs_start_transaction(root, 1);
893 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
894 root->fs_info->extent_root->node);
895 btrfs_commit_transaction(trans, root);
899 struct btrfs_trans_handle *trans;
900 struct btrfs_path *path;
903 if (logical == (u64)-1)
906 path = btrfs_alloc_path();
908 fprintf(stderr, "path allocation failed\n");
912 if (find_chunk_offset(root->fs_info->chunk_root, path,
914 btrfs_free_path(path);
917 trans = btrfs_start_transaction(root, 1);
918 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
921 fprintf(stderr, "Failed to corrupt chunk record\n");
922 btrfs_commit_transaction(trans, root);
926 struct btrfs_trans_handle *trans;
927 trans = btrfs_start_transaction(root, 1);
928 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
930 fprintf(stderr, "Failed to corrupt chunk tree\n");
931 btrfs_commit_transaction(trans, root);
935 struct btrfs_trans_handle *trans;
940 trans = btrfs_start_transaction(root, 1);
941 if (file_extent == (u64)-1) {
942 printf("corrupting inode\n");
943 ret = corrupt_inode(trans, root, inode, field);
945 printf("corrupting file extent\n");
946 ret = corrupt_file_extent(trans, root, inode,
949 btrfs_commit_transaction(trans, root);
952 if (metadata_block) {
955 ret = corrupt_metadata_block(root, metadata_block, field);
958 if (key.objectid || key.offset || key.type) {
961 ret = corrupt_key(root, &key, field);
965 * If we made it here and we have extent set then we didn't specify
966 * inode and we're screwed.
968 if (file_extent != (u64)-1)
971 if (logical == (u64)-1)
975 bytes = root->sectorsize;
977 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
978 bytes *= root->sectorsize;
981 if (corrupt_block_keys) {
982 corrupt_keys_in_block(root, logical);
984 eb = debug_corrupt_block(root, logical,
985 root->sectorsize, copy);
986 free_extent_buffer(eb);
988 logical += root->sectorsize;
989 bytes -= root->sectorsize;