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.
24 #include "kerncompat.h"
28 #include "print-tree.h"
29 #include "transaction.h"
33 #define FIELD_BUF_LEN 80
35 struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
36 u32 blocksize, u64 copy)
39 struct extent_buffer *eb;
41 struct btrfs_multi_bio *multi = NULL;
42 struct btrfs_device *device;
46 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
52 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
53 eb->start, &length, &multi,
56 device = multi->stripes[0].dev;
59 eb->dev_bytenr = multi->stripes[0].physical;
62 "mirror %d logical %llu physical %llu device %s\n",
63 mirror_num, (unsigned long long)bytenr,
64 (unsigned long long)eb->dev_bytenr, device->name);
67 if (!copy || mirror_num == copy) {
68 ret = read_extent_from_disk(eb, 0, eb->len);
69 printf("corrupting %llu copy %d\n", eb->start,
71 memset(eb->data, 0, eb->len);
72 write_extent_to_disk(eb);
76 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
82 if (mirror_num > num_copies)
88 static void print_usage(void)
90 fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n");
91 fprintf(stderr, "\t-l Logical extent to be corrupted\n");
92 fprintf(stderr, "\t-c Copy of the extent to be corrupted"
93 " (usually 1 or 2, default: 0)\n");
94 fprintf(stderr, "\t-b Number of bytes to be corrupted\n");
95 fprintf(stderr, "\t-e Extent to be corrupted\n");
96 fprintf(stderr, "\t-E The whole extent tree to be corrupted\n");
97 fprintf(stderr, "\t-u Given chunk item to be corrupted\n");
98 fprintf(stderr, "\t-U The whole chunk tree to be corrupted\n");
99 fprintf(stderr, "\t-i The inode item to corrupt (must also specify "
100 "the field to corrupt)\n");
101 fprintf(stderr, "\t-x The file extent item to corrupt (must also "
102 "specify -i for the inode and -f for the field to corrupt)\n");
103 fprintf(stderr, "\t-m The metadata block to corrupt (must also "
104 "specify -f for the field to corrupt)\n");
105 fprintf(stderr, "\t-K The key to corrupt in the format "
106 "<num>,<num>,<num> (must also specify -f for the field)\n");
107 fprintf(stderr, "\t-f The field in the item to corrupt\n");
108 fprintf(stderr, "\t-I An item to corrupt (must also specify the field "
109 "to corrupt and a root+key for the item)\n");
110 fprintf(stderr, "\t-D Corrupt a dir item, must specify key and field\n");
111 fprintf(stderr, "\t-d Delete this item (must specify -K)\n");
112 fprintf(stderr, "\t-r Operate on this root (only works with -d)\n");
116 static void corrupt_keys(struct btrfs_trans_handle *trans,
117 struct btrfs_root *root,
118 struct extent_buffer *eb)
123 struct btrfs_disk_key bad_key;;
125 nr = btrfs_header_nritems(eb);
130 bad_slot = rand() % nr;
132 if (bad_slot == slot)
136 "corrupting keys in block %llu slot %d swapping with %d\n",
137 (unsigned long long)eb->start, slot, bad_slot);
139 if (btrfs_header_level(eb) == 0) {
140 btrfs_item_key(eb, &bad_key, bad_slot);
141 btrfs_set_item_key(eb, &bad_key, slot);
143 btrfs_node_key(eb, &bad_key, bad_slot);
144 btrfs_set_node_key(eb, &bad_key, slot);
146 btrfs_mark_buffer_dirty(eb);
149 btrfs_super_csum_size(root->fs_info->super_copy);
150 csum_tree_block_size(eb, csum_size, 0);
151 write_extent_to_disk(eb);
156 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
158 struct extent_buffer *eb;
160 eb = read_tree_block(root, bytenr, root->leafsize, 0);
161 if (!extent_buffer_uptodate(eb))
164 corrupt_keys(NULL, root, eb);
165 free_extent_buffer(eb);
169 static int corrupt_extent(struct btrfs_trans_handle *trans,
170 struct btrfs_root *root, u64 bytenr, u64 copy)
172 struct btrfs_key key;
173 struct extent_buffer *leaf;
176 struct btrfs_path *path;
179 int should_del = rand() % 3;
181 path = btrfs_alloc_path();
185 key.objectid = bytenr;
187 key.offset = (u64)-1;
190 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
196 if (path->slots[0] == 0)
201 leaf = path->nodes[0];
202 slot = path->slots[0];
203 btrfs_item_key_to_cpu(leaf, &key, slot);
204 if (key.objectid != bytenr)
207 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
208 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
209 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
210 key.type != BTRFS_EXTENT_REF_V0_KEY &&
211 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
212 key.type != BTRFS_SHARED_DATA_REF_KEY)
217 "deleting extent record: key %llu %u %llu\n",
218 key.objectid, key.type, key.offset);
220 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
221 /* make sure this extent doesn't get
222 * reused for other purposes */
223 btrfs_pin_extent(root->fs_info,
224 key.objectid, key.offset);
227 btrfs_del_item(trans, root, path);
230 "corrupting extent record: key %llu %u %llu\n",
231 key.objectid, key.type, key.offset);
232 ptr = btrfs_item_ptr_offset(leaf, slot);
233 item_size = btrfs_item_size_nr(leaf, slot);
234 memset_extent_buffer(leaf, 0, ptr, item_size);
235 btrfs_mark_buffer_dirty(leaf);
238 btrfs_release_path(path);
246 btrfs_free_path(path);
250 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
251 struct btrfs_root *root,
252 struct extent_buffer *eb)
254 u32 nr = btrfs_header_nritems(eb);
255 u32 victim = rand() % nr;
257 struct btrfs_key key;
259 btrfs_item_key_to_cpu(eb, &key, victim);
260 objectid = key.objectid;
261 corrupt_extent(trans, root, objectid, 1);
264 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
265 struct btrfs_root *root,
266 struct extent_buffer *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 < btrfs_header_nritems(eb); i++) {
284 struct extent_buffer *next;
286 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
288 btrfs_node_ptr_generation(eb, i));
289 if (!extent_buffer_uptodate(next))
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_dir_item_field {
308 BTRFS_DIR_ITEM_LOCATION_OBJECTID,
312 enum btrfs_metadata_block_field {
313 BTRFS_METADATA_BLOCK_GENERATION,
314 BTRFS_METADATA_BLOCK_SHIFT_ITEMS,
315 BTRFS_METADATA_BLOCK_BAD,
318 enum btrfs_item_field {
323 enum btrfs_key_field {
330 static enum btrfs_inode_field convert_inode_field(char *field)
332 if (!strncmp(field, "isize", FIELD_BUF_LEN))
333 return BTRFS_INODE_FIELD_ISIZE;
334 return BTRFS_INODE_FIELD_BAD;
337 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
339 if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
340 return BTRFS_FILE_EXTENT_DISK_BYTENR;
341 return BTRFS_FILE_EXTENT_BAD;
344 static enum btrfs_metadata_block_field
345 convert_metadata_block_field(char *field)
347 if (!strncmp(field, "generation", FIELD_BUF_LEN))
348 return BTRFS_METADATA_BLOCK_GENERATION;
349 if (!strncmp(field, "shift_items", FIELD_BUF_LEN))
350 return BTRFS_METADATA_BLOCK_SHIFT_ITEMS;
351 return BTRFS_METADATA_BLOCK_BAD;
354 static enum btrfs_key_field convert_key_field(char *field)
356 if (!strncmp(field, "objectid", FIELD_BUF_LEN))
357 return BTRFS_KEY_OBJECTID;
358 if (!strncmp(field, "type", FIELD_BUF_LEN))
359 return BTRFS_KEY_TYPE;
360 if (!strncmp(field, "offset", FIELD_BUF_LEN))
361 return BTRFS_KEY_OFFSET;
362 return BTRFS_KEY_BAD;
365 static enum btrfs_item_field convert_item_field(char *field)
367 if (!strncmp(field, "offset", FIELD_BUF_LEN))
368 return BTRFS_ITEM_OFFSET;
369 return BTRFS_ITEM_BAD;
372 static enum btrfs_dir_item_field convert_dir_item_field(char *field)
374 if (!strncmp(field, "name", FIELD_BUF_LEN))
375 return BTRFS_DIR_ITEM_NAME;
376 if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
377 return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
378 return BTRFS_DIR_ITEM_BAD;
381 static u64 generate_u64(u64 orig)
386 } while (ret == orig);
390 static u32 generate_u32(u32 orig)
395 } while (ret == orig);
399 static u8 generate_u8(u8 orig)
404 } while (ret == orig);
408 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
411 enum btrfs_key_field corrupt_field = convert_key_field(field);
412 struct btrfs_path *path;
413 struct btrfs_trans_handle *trans;
416 root = root->fs_info->fs_root;
417 if (corrupt_field == BTRFS_KEY_BAD) {
418 fprintf(stderr, "Invalid field %s\n", field);
422 path = btrfs_alloc_path();
426 trans = btrfs_start_transaction(root, 1);
428 btrfs_free_path(path);
429 return PTR_ERR(trans);
432 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
436 fprintf(stderr, "Couldn't find the key to corrupt\n");
441 switch (corrupt_field) {
442 case BTRFS_KEY_OBJECTID:
443 key->objectid = generate_u64(key->objectid);
446 key->type = generate_u8(key->type);
448 case BTRFS_KEY_OFFSET:
449 key->offset = generate_u64(key->objectid);
452 fprintf(stderr, "Invalid field %s, %d\n", field,
458 btrfs_set_item_key_unsafe(root, path, key);
460 btrfs_free_path(path);
461 btrfs_commit_transaction(trans, root);
465 static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
468 struct btrfs_trans_handle *trans;
469 struct btrfs_dir_item *di;
470 struct btrfs_path *path;
472 struct btrfs_key location;
473 struct btrfs_disk_key disk_key;
474 unsigned long name_ptr;
475 enum btrfs_dir_item_field corrupt_field =
476 convert_dir_item_field(field);
481 if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
482 fprintf(stderr, "Invalid field %s\n", field);
486 path = btrfs_alloc_path();
490 trans = btrfs_start_transaction(root, 1);
492 btrfs_free_path(path);
493 return PTR_ERR(trans);
496 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
500 fprintf(stderr, "Error searching for dir item %d\n", ret);
504 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
505 struct btrfs_dir_item);
507 switch (corrupt_field) {
508 case BTRFS_DIR_ITEM_NAME:
509 name_len = btrfs_dir_name_len(path->nodes[0], di);
510 name = malloc(name_len);
515 name_ptr = (unsigned long)(di + 1);
516 read_extent_buffer(path->nodes[0], name, name_ptr, name_len);
518 write_extent_buffer(path->nodes[0], name, name_ptr, name_len);
519 btrfs_mark_buffer_dirty(path->nodes[0]);
522 case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
523 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
524 bogus = generate_u64(location.objectid);
525 location.objectid = bogus;
526 btrfs_cpu_key_to_disk(&disk_key, &location);
527 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
528 btrfs_mark_buffer_dirty(path->nodes[0]);
535 btrfs_commit_transaction(trans, root);
536 btrfs_free_path(path);
540 static int corrupt_inode(struct btrfs_trans_handle *trans,
541 struct btrfs_root *root, u64 inode, char *field)
543 struct btrfs_inode_item *ei;
544 struct btrfs_path *path;
545 struct btrfs_key key;
546 enum btrfs_inode_field corrupt_field = convert_inode_field(field);
551 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
552 fprintf(stderr, "Invalid field %s\n", field);
556 key.objectid = inode;
557 key.type = BTRFS_INODE_ITEM_KEY;
558 key.offset = (u64)-1;
560 path = btrfs_alloc_path();
564 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
568 if (!path->slots[0]) {
569 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
577 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
578 if (key.objectid != inode) {
579 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
584 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
585 struct btrfs_inode_item);
586 switch (corrupt_field) {
587 case BTRFS_INODE_FIELD_ISIZE:
588 orig = btrfs_inode_size(path->nodes[0], ei);
589 bogus = generate_u64(orig);
590 btrfs_set_inode_size(path->nodes[0], ei, bogus);
596 btrfs_mark_buffer_dirty(path->nodes[0]);
598 btrfs_free_path(path);
602 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
603 struct btrfs_root *root, u64 inode, u64 extent,
606 struct btrfs_file_extent_item *fi;
607 struct btrfs_path *path;
608 struct btrfs_key key;
609 enum btrfs_file_extent_field corrupt_field;
614 corrupt_field = convert_file_extent_field(field);
615 if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
616 fprintf(stderr, "Invalid field %s\n", field);
620 key.objectid = inode;
621 key.type = BTRFS_EXTENT_DATA_KEY;
624 path = btrfs_alloc_path();
628 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
632 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
638 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
639 struct btrfs_file_extent_item);
640 switch (corrupt_field) {
641 case BTRFS_FILE_EXTENT_DISK_BYTENR:
642 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
643 bogus = generate_u64(orig);
644 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
650 btrfs_mark_buffer_dirty(path->nodes[0]);
652 btrfs_free_path(path);
656 static void shift_items(struct btrfs_root *root, struct extent_buffer *eb)
658 int nritems = btrfs_header_nritems(eb);
659 int shift_space = btrfs_leaf_free_space(root, eb) / 2;
660 int slot = nritems / 2;
662 unsigned int data_end = btrfs_item_offset_nr(eb, nritems - 1);
664 /* Shift the item data up to and including slot back by shift space */
665 memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end - shift_space,
666 btrfs_leaf_data(eb) + data_end,
667 btrfs_item_offset_nr(eb, slot - 1) - data_end);
669 /* Now update the item pointers. */
670 for (i = nritems - 1; i >= slot; i--) {
671 u32 offset = btrfs_item_offset_nr(eb, i);
672 offset -= shift_space;
673 btrfs_set_item_offset(eb, btrfs_item_nr(i), offset);
677 static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
680 struct btrfs_trans_handle *trans;
681 struct btrfs_path *path;
682 struct extent_buffer *eb;
683 struct btrfs_key key, root_key;
684 enum btrfs_metadata_block_field corrupt_field;
690 corrupt_field = convert_metadata_block_field(field);
691 if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
692 fprintf(stderr, "Invalid field %s\n", field);
696 eb = read_tree_block(root, block, root->leafsize, 0);
697 if (!extent_buffer_uptodate(eb)) {
698 fprintf(stderr, "Couldn't read in tree block %s\n", field);
701 root_objectid = btrfs_header_owner(eb);
702 level = btrfs_header_level(eb);
704 btrfs_node_key_to_cpu(eb, &key, 0);
706 btrfs_item_key_to_cpu(eb, &key, 0);
707 free_extent_buffer(eb);
709 root_key.objectid = root_objectid;
710 root_key.type = BTRFS_ROOT_ITEM_KEY;
711 root_key.offset = (u64)-1;
713 root = btrfs_read_fs_root(root->fs_info, &root_key);
715 fprintf(stderr, "Couldn't finde owner root %llu\n",
717 return PTR_ERR(root);
720 path = btrfs_alloc_path();
724 trans = btrfs_start_transaction(root, 1);
726 btrfs_free_path(path);
727 fprintf(stderr, "Couldn't start transaction %ld\n",
729 return PTR_ERR(trans);
732 path->lowest_level = level;
733 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
735 fprintf(stderr, "Error searching to node %d\n", ret);
738 eb = path->nodes[level];
741 switch (corrupt_field) {
742 case BTRFS_METADATA_BLOCK_GENERATION:
743 orig = btrfs_header_generation(eb);
744 bogus = generate_u64(orig);
745 btrfs_set_header_generation(eb, bogus);
747 case BTRFS_METADATA_BLOCK_SHIFT_ITEMS:
748 shift_items(root, path->nodes[level]);
754 btrfs_mark_buffer_dirty(path->nodes[level]);
756 btrfs_commit_transaction(trans, root);
757 btrfs_free_path(path);
761 static int corrupt_btrfs_item(struct btrfs_root *root, struct btrfs_key *key,
764 struct btrfs_trans_handle *trans;
765 struct btrfs_path *path;
766 enum btrfs_item_field corrupt_field;
770 corrupt_field = convert_item_field(field);
771 if (corrupt_field == BTRFS_ITEM_BAD) {
772 fprintf(stderr, "Invalid field %s\n", field);
776 path = btrfs_alloc_path();
780 trans = btrfs_start_transaction(root, 1);
782 btrfs_free_path(path);
783 fprintf(stderr, "Couldn't start transaction %ld\n",
785 return PTR_ERR(trans);
788 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
790 fprintf(stderr, "Error searching to node %d\n", ret);
795 switch (corrupt_field) {
796 case BTRFS_ITEM_OFFSET:
797 orig = btrfs_item_offset_nr(path->nodes[0], path->slots[0]);
798 bogus = generate_u32(orig);
799 btrfs_set_item_offset(path->nodes[0],
800 btrfs_item_nr(path->slots[0]), bogus);
806 btrfs_mark_buffer_dirty(path->nodes[0]);
808 btrfs_commit_transaction(trans, root);
809 btrfs_free_path(path);
813 static int delete_item(struct btrfs_root *root, struct btrfs_key *key)
815 struct btrfs_trans_handle *trans;
816 struct btrfs_path *path;
819 path = btrfs_alloc_path();
823 trans = btrfs_start_transaction(root, 1);
825 btrfs_free_path(path);
826 fprintf(stderr, "Couldn't start transaction %ld\n",
828 return PTR_ERR(trans);
831 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
835 fprintf(stderr, "Error searching to node %d\n", ret);
838 ret = btrfs_del_item(trans, root, path);
839 btrfs_mark_buffer_dirty(path->nodes[0]);
841 btrfs_commit_transaction(trans, root);
842 btrfs_free_path(path);
846 /* corrupt item using NO cow.
847 * Because chunk recover will recover based on whole partition scaning,
848 * If using COW, chunk recover will use the old item to recover,
849 * which is still OK but we want to check the ability to rebuild chunk
850 * not only restore the old ones */
851 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
852 struct btrfs_root *root, struct btrfs_path *path,
856 struct btrfs_key key;
857 struct extent_buffer *leaf;
862 leaf = path->nodes[0];
863 slot = path->slots[0];
864 /* Not deleting the first item of a leaf to keep leaf structure */
867 /* Only accept valid eb */
868 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
869 btrfs_item_key_to_cpu(leaf, &key, slot);
871 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
872 key.objectid, key.type, key.offset);
873 btrfs_del_item(trans, root, path);
875 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
876 key.objectid, key.type, key.offset);
877 ptr = btrfs_item_ptr_offset(leaf, slot);
878 item_size = btrfs_item_size_nr(leaf, slot);
879 memset_extent_buffer(leaf, 0, ptr, item_size);
880 btrfs_mark_buffer_dirty(leaf);
884 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
885 struct btrfs_root *root)
890 struct btrfs_path *path;
891 struct btrfs_key key;
892 struct btrfs_key found_key;
893 struct extent_buffer *leaf;
895 path = btrfs_alloc_path();
899 key.objectid = (u64)-1;
900 key.offset = (u64)-1;
903 /* Here, cow and ins_len must equals 0 for the following reasons:
904 * 1) chunk recover is based on disk scanning, so COW should be
905 * disabled in case the original chunk being scanned and
906 * recovered using the old chunk.
907 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
910 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
913 fprintf(stderr, "Error searching tree\n");
916 /* corrupt/del dev_item first */
917 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
918 slot = path->slots[0];
919 leaf = path->nodes[0];
921 /* Never delete the first item to keep the leaf structure */
922 if (path->slots[0] == 0)
924 ret = corrupt_item_nocow(trans, root, path, del);
928 btrfs_release_path(path);
930 /* Here, cow and ins_len must equals 0 for the following reasons:
931 * 1) chunk recover is based on disk scanning, so COW should be
932 * disabled in case the original chunk being scanned and
933 * recovered using the old chunk.
934 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
937 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
940 fprintf(stderr, "Error searching tree\n");
943 /* corrupt/del chunk then*/
944 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
945 slot = path->slots[0];
946 leaf = path->nodes[0];
948 btrfs_item_key_to_cpu(leaf, &found_key, slot);
949 ret = corrupt_item_nocow(trans, root, path, del);
954 btrfs_free_path(path);
957 int find_chunk_offset(struct btrfs_root *root,
958 struct btrfs_path *path, u64 offset)
960 struct btrfs_key key;
963 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
964 key.type = BTRFS_CHUNK_ITEM_KEY;
967 /* Here, cow and ins_len must equals 0 for following reasons:
968 * 1) chunk recover is based on disk scanning, so COW should
969 * be disabled in case the original chunk being scanned
970 * and recovered using the old chunk.
971 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
974 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
976 fprintf(stderr, "Can't find chunk with given offset %llu\n",
981 fprintf(stderr, "Error searching chunk");
988 int main(int ac, char **av)
990 struct cache_tree root_cache;
991 struct btrfs_key key;
992 struct btrfs_root *root;
993 struct extent_buffer *eb;
995 /* chunk offset can be 0,so change to (u64)-1 */
996 u64 logical = (u64)-1;
1001 int extent_tree = 0;
1002 int corrupt_block_keys = 0;
1005 int corrupt_item = 0;
1008 u64 metadata_block = 0;
1010 u64 file_extent = (u64)-1;
1011 u64 root_objectid = 0;
1012 char field[FIELD_BUF_LEN];
1016 memset(&key, 0, sizeof(key));
1020 static const struct option long_options[] = {
1021 /* { "byte-count", 1, NULL, 'b' }, */
1022 { "logical", required_argument, NULL, 'l' },
1023 { "copy", required_argument, NULL, 'c' },
1024 { "bytes", required_argument, NULL, 'b' },
1025 { "extent-record", no_argument, NULL, 'e' },
1026 { "extent-tree", no_argument, NULL, 'E' },
1027 { "keys", no_argument, NULL, 'k' },
1028 { "chunk-record", no_argument, NULL, 'u' },
1029 { "chunk-tree", no_argument, NULL, 'U' },
1030 { "inode", required_argument, NULL, 'i'},
1031 { "file-extent", required_argument, NULL, 'x'},
1032 { "metadata-block", required_argument, NULL, 'm'},
1033 { "field", required_argument, NULL, 'f'},
1034 { "key", required_argument, NULL, 'K'},
1035 { "item", no_argument, NULL, 'I'},
1036 { "dir-item", no_argument, NULL, 'D'},
1037 { "delete", no_argument, NULL, 'd'},
1038 { "root", no_argument, NULL, 'r'},
1039 { NULL, 0, NULL, 0 }
1042 c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:IDdr:",
1043 long_options, NULL);
1048 logical = arg_strtou64(optarg);
1051 copy = arg_strtou64(optarg);
1054 bytes = arg_strtou64(optarg);
1063 corrupt_block_keys = 1;
1072 inode = arg_strtou64(optarg);
1075 strncpy(field, optarg, FIELD_BUF_LEN);
1078 file_extent = arg_strtou64(optarg);
1081 metadata_block = arg_strtou64(optarg);
1084 ret = sscanf(optarg, "%llu,%u,%llu",
1086 (unsigned int *)&key.type,
1089 fprintf(stderr, "error reading key "
1104 root_objectid = arg_strtou64(optarg);
1112 if (check_argc_min(ac, 1))
1117 cache_tree_init(&root_cache);
1119 root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
1121 fprintf(stderr, "Open ctree failed\n");
1125 struct btrfs_trans_handle *trans;
1127 if (logical == (u64)-1)
1129 trans = btrfs_start_transaction(root, 1);
1130 ret = corrupt_extent (trans, root, logical, 0);
1131 btrfs_commit_transaction(trans, root);
1135 struct btrfs_trans_handle *trans;
1136 trans = btrfs_start_transaction(root, 1);
1137 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
1138 root->fs_info->extent_root->node);
1139 btrfs_commit_transaction(trans, root);
1143 struct btrfs_trans_handle *trans;
1144 struct btrfs_path *path;
1147 if (logical == (u64)-1)
1150 path = btrfs_alloc_path();
1152 fprintf(stderr, "path allocation failed\n");
1156 if (find_chunk_offset(root->fs_info->chunk_root, path,
1158 btrfs_free_path(path);
1161 trans = btrfs_start_transaction(root, 1);
1162 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
1165 fprintf(stderr, "Failed to corrupt chunk record\n");
1166 btrfs_commit_transaction(trans, root);
1170 struct btrfs_trans_handle *trans;
1171 trans = btrfs_start_transaction(root, 1);
1172 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
1174 fprintf(stderr, "Failed to corrupt chunk tree\n");
1175 btrfs_commit_transaction(trans, root);
1179 struct btrfs_trans_handle *trans;
1184 trans = btrfs_start_transaction(root, 1);
1185 if (file_extent == (u64)-1) {
1186 printf("corrupting inode\n");
1187 ret = corrupt_inode(trans, root, inode, field);
1189 printf("corrupting file extent\n");
1190 ret = corrupt_file_extent(trans, root, inode,
1191 file_extent, field);
1193 btrfs_commit_transaction(trans, root);
1196 if (metadata_block) {
1199 ret = corrupt_metadata_block(root, metadata_block, field);
1203 if (!key.objectid || !strlen(field))
1205 ret = corrupt_dir_item(root, &key, field);
1211 ret = corrupt_btrfs_item(root, &key, field);
1214 struct btrfs_root *target = root;
1218 if (root_objectid) {
1219 struct btrfs_key root_key;
1221 root_key.objectid = root_objectid;
1222 root_key.type = BTRFS_ROOT_ITEM_KEY;
1223 root_key.offset = (u64)-1;
1225 target = btrfs_read_fs_root(root->fs_info, &root_key);
1226 if (IS_ERR(target)) {
1227 fprintf(stderr, "Couldn't find root %llu\n",
1228 (unsigned long long)root_objectid);
1232 ret = delete_item(target, &key);
1235 if (key.objectid || key.offset || key.type) {
1238 ret = corrupt_key(root, &key, field);
1242 * If we made it here and we have extent set then we didn't specify
1243 * inode and we're screwed.
1245 if (file_extent != (u64)-1)
1248 if (logical == (u64)-1)
1252 bytes = root->sectorsize;
1254 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
1255 bytes *= root->sectorsize;
1258 if (corrupt_block_keys) {
1259 corrupt_keys_in_block(root, logical);
1261 eb = debug_corrupt_block(root, logical,
1262 root->sectorsize, copy);
1263 free_extent_buffer(eb);
1265 logical += root->sectorsize;
1266 bytes -= root->sectorsize;