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");
115 static void corrupt_keys(struct btrfs_trans_handle *trans,
116 struct btrfs_root *root,
117 struct extent_buffer *eb)
122 struct btrfs_disk_key bad_key;;
124 nr = btrfs_header_nritems(eb);
129 bad_slot = rand() % nr;
131 if (bad_slot == slot)
135 "corrupting keys in block %llu slot %d swapping with %d\n",
136 (unsigned long long)eb->start, slot, bad_slot);
138 if (btrfs_header_level(eb) == 0) {
139 btrfs_item_key(eb, &bad_key, bad_slot);
140 btrfs_set_item_key(eb, &bad_key, slot);
142 btrfs_node_key(eb, &bad_key, bad_slot);
143 btrfs_set_node_key(eb, &bad_key, slot);
145 btrfs_mark_buffer_dirty(eb);
148 btrfs_super_csum_size(root->fs_info->super_copy);
149 csum_tree_block_size(eb, csum_size, 0);
150 write_extent_to_disk(eb);
155 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
157 struct extent_buffer *eb;
159 eb = read_tree_block(root, bytenr, root->leafsize, 0);
160 if (!extent_buffer_uptodate(eb))
163 corrupt_keys(NULL, root, eb);
164 free_extent_buffer(eb);
168 static int corrupt_extent(struct btrfs_trans_handle *trans,
169 struct btrfs_root *root, u64 bytenr, u64 copy)
171 struct btrfs_key key;
172 struct extent_buffer *leaf;
175 struct btrfs_path *path;
178 int should_del = rand() % 3;
180 path = btrfs_alloc_path();
184 key.objectid = bytenr;
186 key.offset = (u64)-1;
189 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
195 if (path->slots[0] == 0)
200 leaf = path->nodes[0];
201 slot = path->slots[0];
202 btrfs_item_key_to_cpu(leaf, &key, slot);
203 if (key.objectid != bytenr)
206 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
207 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
208 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
209 key.type != BTRFS_EXTENT_REF_V0_KEY &&
210 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
211 key.type != BTRFS_SHARED_DATA_REF_KEY)
216 "deleting extent record: key %llu %u %llu\n",
217 key.objectid, key.type, key.offset);
219 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
220 /* make sure this extent doesn't get
221 * reused for other purposes */
222 btrfs_pin_extent(root->fs_info,
223 key.objectid, key.offset);
226 btrfs_del_item(trans, root, path);
229 "corrupting extent record: key %llu %u %llu\n",
230 key.objectid, key.type, key.offset);
231 ptr = btrfs_item_ptr_offset(leaf, slot);
232 item_size = btrfs_item_size_nr(leaf, slot);
233 memset_extent_buffer(leaf, 0, ptr, item_size);
234 btrfs_mark_buffer_dirty(leaf);
237 btrfs_release_path(path);
245 btrfs_free_path(path);
249 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
250 struct btrfs_root *root,
251 struct extent_buffer *eb)
253 u32 nr = btrfs_header_nritems(eb);
254 u32 victim = rand() % nr;
256 struct btrfs_key key;
258 btrfs_item_key_to_cpu(eb, &key, victim);
259 objectid = key.objectid;
260 corrupt_extent(trans, root, objectid, 1);
263 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
264 struct btrfs_root *root,
265 struct extent_buffer *eb)
272 if (btrfs_is_leaf(eb)) {
273 btrfs_corrupt_extent_leaf(trans, root, eb);
277 if (btrfs_header_level(eb) == 1 && eb != root->node) {
282 for (i = 0; i < btrfs_header_nritems(eb); i++) {
283 struct extent_buffer *next;
285 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
287 btrfs_node_ptr_generation(eb, i));
288 if (!extent_buffer_uptodate(next))
290 btrfs_corrupt_extent_tree(trans, root, next);
291 free_extent_buffer(next);
295 enum btrfs_inode_field {
296 BTRFS_INODE_FIELD_ISIZE,
297 BTRFS_INODE_FIELD_BAD,
300 enum btrfs_file_extent_field {
301 BTRFS_FILE_EXTENT_DISK_BYTENR,
302 BTRFS_FILE_EXTENT_BAD,
305 enum btrfs_dir_item_field {
307 BTRFS_DIR_ITEM_LOCATION_OBJECTID,
311 enum btrfs_metadata_block_field {
312 BTRFS_METADATA_BLOCK_GENERATION,
313 BTRFS_METADATA_BLOCK_SHIFT_ITEMS,
314 BTRFS_METADATA_BLOCK_BAD,
317 enum btrfs_item_field {
322 enum btrfs_key_field {
329 static enum btrfs_inode_field convert_inode_field(char *field)
331 if (!strncmp(field, "isize", FIELD_BUF_LEN))
332 return BTRFS_INODE_FIELD_ISIZE;
333 return BTRFS_INODE_FIELD_BAD;
336 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
338 if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
339 return BTRFS_FILE_EXTENT_DISK_BYTENR;
340 return BTRFS_FILE_EXTENT_BAD;
343 static enum btrfs_metadata_block_field
344 convert_metadata_block_field(char *field)
346 if (!strncmp(field, "generation", FIELD_BUF_LEN))
347 return BTRFS_METADATA_BLOCK_GENERATION;
348 if (!strncmp(field, "shift_items", FIELD_BUF_LEN))
349 return BTRFS_METADATA_BLOCK_SHIFT_ITEMS;
350 return BTRFS_METADATA_BLOCK_BAD;
353 static enum btrfs_key_field convert_key_field(char *field)
355 if (!strncmp(field, "objectid", FIELD_BUF_LEN))
356 return BTRFS_KEY_OBJECTID;
357 if (!strncmp(field, "type", FIELD_BUF_LEN))
358 return BTRFS_KEY_TYPE;
359 if (!strncmp(field, "offset", FIELD_BUF_LEN))
360 return BTRFS_KEY_OFFSET;
361 return BTRFS_KEY_BAD;
364 static enum btrfs_item_field convert_item_field(char *field)
366 if (!strncmp(field, "offset", FIELD_BUF_LEN))
367 return BTRFS_ITEM_OFFSET;
368 return BTRFS_ITEM_BAD;
371 static enum btrfs_dir_item_field convert_dir_item_field(char *field)
373 if (!strncmp(field, "name", FIELD_BUF_LEN))
374 return BTRFS_DIR_ITEM_NAME;
375 if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
376 return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
377 return BTRFS_DIR_ITEM_BAD;
380 static u64 generate_u64(u64 orig)
385 } while (ret == orig);
389 static u32 generate_u32(u32 orig)
394 } while (ret == orig);
398 static u8 generate_u8(u8 orig)
403 } while (ret == orig);
407 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
410 enum btrfs_key_field corrupt_field = convert_key_field(field);
411 struct btrfs_path *path;
412 struct btrfs_trans_handle *trans;
415 root = root->fs_info->fs_root;
416 if (corrupt_field == BTRFS_KEY_BAD) {
417 fprintf(stderr, "Invalid field %s\n", field);
421 path = btrfs_alloc_path();
425 trans = btrfs_start_transaction(root, 1);
427 btrfs_free_path(path);
428 return PTR_ERR(trans);
431 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
435 fprintf(stderr, "Couldn't find the key to corrupt\n");
440 switch (corrupt_field) {
441 case BTRFS_KEY_OBJECTID:
442 key->objectid = generate_u64(key->objectid);
445 key->type = generate_u8(key->type);
447 case BTRFS_KEY_OFFSET:
448 key->offset = generate_u64(key->objectid);
451 fprintf(stderr, "Invalid field %s, %d\n", field,
457 btrfs_set_item_key_unsafe(root, path, key);
459 btrfs_free_path(path);
460 btrfs_commit_transaction(trans, root);
464 static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
467 struct btrfs_trans_handle *trans;
468 struct btrfs_dir_item *di;
469 struct btrfs_path *path;
471 struct btrfs_key location;
472 struct btrfs_disk_key disk_key;
473 unsigned long name_ptr;
474 enum btrfs_dir_item_field corrupt_field =
475 convert_dir_item_field(field);
480 if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
481 fprintf(stderr, "Invalid field %s\n", field);
485 path = btrfs_alloc_path();
489 trans = btrfs_start_transaction(root, 1);
491 btrfs_free_path(path);
492 return PTR_ERR(trans);
495 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
499 fprintf(stderr, "Error searching for dir item %d\n", ret);
503 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
504 struct btrfs_dir_item);
506 switch (corrupt_field) {
507 case BTRFS_DIR_ITEM_NAME:
508 name_len = btrfs_dir_name_len(path->nodes[0], di);
509 name = malloc(name_len);
514 name_ptr = (unsigned long)(di + 1);
515 read_extent_buffer(path->nodes[0], name, name_ptr, name_len);
517 write_extent_buffer(path->nodes[0], name, name_ptr, name_len);
518 btrfs_mark_buffer_dirty(path->nodes[0]);
521 case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
522 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
523 bogus = generate_u64(location.objectid);
524 location.objectid = bogus;
525 btrfs_cpu_key_to_disk(&disk_key, &location);
526 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
527 btrfs_mark_buffer_dirty(path->nodes[0]);
534 btrfs_commit_transaction(trans, root);
535 btrfs_free_path(path);
539 static int corrupt_inode(struct btrfs_trans_handle *trans,
540 struct btrfs_root *root, u64 inode, char *field)
542 struct btrfs_inode_item *ei;
543 struct btrfs_path *path;
544 struct btrfs_key key;
545 enum btrfs_inode_field corrupt_field = convert_inode_field(field);
550 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
551 fprintf(stderr, "Invalid field %s\n", field);
555 key.objectid = inode;
556 key.type = BTRFS_INODE_ITEM_KEY;
557 key.offset = (u64)-1;
559 path = btrfs_alloc_path();
563 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
567 if (!path->slots[0]) {
568 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
576 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
577 if (key.objectid != inode) {
578 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
583 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
584 struct btrfs_inode_item);
585 switch (corrupt_field) {
586 case BTRFS_INODE_FIELD_ISIZE:
587 orig = btrfs_inode_size(path->nodes[0], ei);
588 bogus = generate_u64(orig);
589 btrfs_set_inode_size(path->nodes[0], ei, bogus);
595 btrfs_mark_buffer_dirty(path->nodes[0]);
597 btrfs_free_path(path);
601 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
602 struct btrfs_root *root, u64 inode, u64 extent,
605 struct btrfs_file_extent_item *fi;
606 struct btrfs_path *path;
607 struct btrfs_key key;
608 enum btrfs_file_extent_field corrupt_field;
613 corrupt_field = convert_file_extent_field(field);
614 if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
615 fprintf(stderr, "Invalid field %s\n", field);
619 key.objectid = inode;
620 key.type = BTRFS_EXTENT_DATA_KEY;
623 path = btrfs_alloc_path();
627 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
631 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
637 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
638 struct btrfs_file_extent_item);
639 switch (corrupt_field) {
640 case BTRFS_FILE_EXTENT_DISK_BYTENR:
641 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
642 bogus = generate_u64(orig);
643 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
649 btrfs_mark_buffer_dirty(path->nodes[0]);
651 btrfs_free_path(path);
655 static void shift_items(struct btrfs_root *root, struct extent_buffer *eb)
657 int nritems = btrfs_header_nritems(eb);
658 int shift_space = btrfs_leaf_free_space(root, eb) / 2;
659 int slot = nritems / 2;
661 unsigned int data_end = btrfs_item_offset_nr(eb, nritems - 1);
663 /* Shift the item data up to and including slot back by shift space */
664 memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end - shift_space,
665 btrfs_leaf_data(eb) + data_end,
666 btrfs_item_offset_nr(eb, slot - 1) - data_end);
668 /* Now update the item pointers. */
669 for (i = nritems - 1; i >= slot; i--) {
670 u32 offset = btrfs_item_offset_nr(eb, i);
671 offset -= shift_space;
672 btrfs_set_item_offset(eb, btrfs_item_nr(i), offset);
676 static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
679 struct btrfs_trans_handle *trans;
680 struct btrfs_path *path;
681 struct extent_buffer *eb;
682 struct btrfs_key key, root_key;
683 enum btrfs_metadata_block_field corrupt_field;
689 corrupt_field = convert_metadata_block_field(field);
690 if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
691 fprintf(stderr, "Invalid field %s\n", field);
695 eb = read_tree_block(root, block, root->leafsize, 0);
696 if (!extent_buffer_uptodate(eb)) {
697 fprintf(stderr, "Couldn't read in tree block %s\n", field);
700 root_objectid = btrfs_header_owner(eb);
701 level = btrfs_header_level(eb);
703 btrfs_node_key_to_cpu(eb, &key, 0);
705 btrfs_item_key_to_cpu(eb, &key, 0);
706 free_extent_buffer(eb);
708 root_key.objectid = root_objectid;
709 root_key.type = BTRFS_ROOT_ITEM_KEY;
710 root_key.offset = (u64)-1;
712 root = btrfs_read_fs_root(root->fs_info, &root_key);
714 fprintf(stderr, "Couldn't finde owner root %llu\n",
716 return PTR_ERR(root);
719 path = btrfs_alloc_path();
723 trans = btrfs_start_transaction(root, 1);
725 btrfs_free_path(path);
726 fprintf(stderr, "Couldn't start transaction %ld\n",
728 return PTR_ERR(trans);
731 path->lowest_level = level;
732 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
734 fprintf(stderr, "Error searching to node %d\n", ret);
737 eb = path->nodes[level];
740 switch (corrupt_field) {
741 case BTRFS_METADATA_BLOCK_GENERATION:
742 orig = btrfs_header_generation(eb);
743 bogus = generate_u64(orig);
744 btrfs_set_header_generation(eb, bogus);
746 case BTRFS_METADATA_BLOCK_SHIFT_ITEMS:
747 shift_items(root, path->nodes[level]);
753 btrfs_mark_buffer_dirty(path->nodes[level]);
755 btrfs_commit_transaction(trans, root);
756 btrfs_free_path(path);
760 static int corrupt_btrfs_item(struct btrfs_root *root, struct btrfs_key *key,
763 struct btrfs_trans_handle *trans;
764 struct btrfs_path *path;
765 enum btrfs_item_field corrupt_field;
769 corrupt_field = convert_item_field(field);
770 if (corrupt_field == BTRFS_ITEM_BAD) {
771 fprintf(stderr, "Invalid field %s\n", field);
775 path = btrfs_alloc_path();
779 trans = btrfs_start_transaction(root, 1);
781 btrfs_free_path(path);
782 fprintf(stderr, "Couldn't start transaction %ld\n",
784 return PTR_ERR(trans);
787 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
789 fprintf(stderr, "Error searching to node %d\n", ret);
794 switch (corrupt_field) {
795 case BTRFS_ITEM_OFFSET:
796 orig = btrfs_item_offset_nr(path->nodes[0], path->slots[0]);
797 bogus = generate_u32(orig);
798 btrfs_set_item_offset(path->nodes[0],
799 btrfs_item_nr(path->slots[0]), bogus);
805 btrfs_mark_buffer_dirty(path->nodes[0]);
807 btrfs_commit_transaction(trans, root);
808 btrfs_free_path(path);
812 static int delete_item(struct btrfs_root *root, struct btrfs_key *key)
814 struct btrfs_trans_handle *trans;
815 struct btrfs_path *path;
818 path = btrfs_alloc_path();
822 trans = btrfs_start_transaction(root, 1);
824 btrfs_free_path(path);
825 fprintf(stderr, "Couldn't start transaction %ld\n",
827 return PTR_ERR(trans);
830 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
834 fprintf(stderr, "Error searching to node %d\n", ret);
837 ret = btrfs_del_item(trans, root, path);
838 btrfs_mark_buffer_dirty(path->nodes[0]);
840 btrfs_commit_transaction(trans, root);
841 btrfs_free_path(path);
845 /* corrupt item using NO cow.
846 * Because chunk recover will recover based on whole partition scaning,
847 * If using COW, chunk recover will use the old item to recover,
848 * which is still OK but we want to check the ability to rebuild chunk
849 * not only restore the old ones */
850 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
851 struct btrfs_root *root, struct btrfs_path *path,
855 struct btrfs_key key;
856 struct extent_buffer *leaf;
861 leaf = path->nodes[0];
862 slot = path->slots[0];
863 /* Not deleting the first item of a leaf to keep leaf structure */
866 /* Only accept valid eb */
867 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
868 btrfs_item_key_to_cpu(leaf, &key, slot);
870 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
871 key.objectid, key.type, key.offset);
872 btrfs_del_item(trans, root, path);
874 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
875 key.objectid, key.type, key.offset);
876 ptr = btrfs_item_ptr_offset(leaf, slot);
877 item_size = btrfs_item_size_nr(leaf, slot);
878 memset_extent_buffer(leaf, 0, ptr, item_size);
879 btrfs_mark_buffer_dirty(leaf);
883 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
884 struct btrfs_root *root)
889 struct btrfs_path *path;
890 struct btrfs_key key;
891 struct btrfs_key found_key;
892 struct extent_buffer *leaf;
894 path = btrfs_alloc_path();
898 key.objectid = (u64)-1;
899 key.offset = (u64)-1;
902 /* Here, cow and ins_len must equals 0 for the following reasons:
903 * 1) chunk recover is based on disk scanning, so COW should be
904 * disabled in case the original chunk being scanned and
905 * recovered using the old chunk.
906 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
909 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
912 fprintf(stderr, "Error searching tree\n");
915 /* corrupt/del dev_item first */
916 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
917 slot = path->slots[0];
918 leaf = path->nodes[0];
920 /* Never delete the first item to keep the leaf structure */
921 if (path->slots[0] == 0)
923 ret = corrupt_item_nocow(trans, root, path, del);
927 btrfs_release_path(path);
929 /* Here, cow and ins_len must equals 0 for the following reasons:
930 * 1) chunk recover is based on disk scanning, so COW should be
931 * disabled in case the original chunk being scanned and
932 * recovered using the old chunk.
933 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
936 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
939 fprintf(stderr, "Error searching tree\n");
942 /* corrupt/del chunk then*/
943 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
944 slot = path->slots[0];
945 leaf = path->nodes[0];
947 btrfs_item_key_to_cpu(leaf, &found_key, slot);
948 ret = corrupt_item_nocow(trans, root, path, del);
953 btrfs_free_path(path);
956 int find_chunk_offset(struct btrfs_root *root,
957 struct btrfs_path *path, u64 offset)
959 struct btrfs_key key;
962 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
963 key.type = BTRFS_CHUNK_ITEM_KEY;
966 /* Here, cow and ins_len must equals 0 for following reasons:
967 * 1) chunk recover is based on disk scanning, so COW should
968 * be disabled in case the original chunk being scanned
969 * and recovered using the old chunk.
970 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
973 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
975 fprintf(stderr, "Can't find chunk with given offset %llu\n",
980 fprintf(stderr, "Error searching chunk");
987 int main(int ac, char **av)
989 struct cache_tree root_cache;
990 struct btrfs_key key;
991 struct btrfs_root *root;
992 struct extent_buffer *eb;
994 /* chunk offset can be 0,so change to (u64)-1 */
995 u64 logical = (u64)-1;
1000 int extent_tree = 0;
1001 int corrupt_block_keys = 0;
1004 int corrupt_item = 0;
1007 u64 metadata_block = 0;
1009 u64 file_extent = (u64)-1;
1010 char field[FIELD_BUF_LEN];
1014 memset(&key, 0, sizeof(key));
1018 int option_index = 0;
1019 static const struct option long_options[] = {
1020 /* { "byte-count", 1, NULL, 'b' }, */
1021 { "logical", 1, NULL, 'l' },
1022 { "copy", 1, NULL, 'c' },
1023 { "bytes", 1, NULL, 'b' },
1024 { "extent-record", 0, NULL, 'e' },
1025 { "extent-tree", 0, NULL, 'E' },
1026 { "keys", 0, NULL, 'k' },
1027 { "chunk-record", 0, NULL, 'u' },
1028 { "chunk-tree", 0, NULL, 'U' },
1029 { "inode", 1, NULL, 'i'},
1030 { "file-extent", 1, NULL, 'x'},
1031 { "metadata-block", 1, NULL, 'm'},
1032 { "field", 1, NULL, 'f'},
1033 { "key", 1, NULL, 'K'},
1034 { "item", 0, NULL, 'I'},
1035 { "dir-item", 0, NULL, 'D'},
1036 { "delete", 0, NULL, 'd'},
1037 { NULL, 0, NULL, 0 }
1040 c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:IDd", long_options,
1046 logical = arg_strtou64(optarg);
1049 copy = arg_strtou64(optarg);
1052 bytes = arg_strtou64(optarg);
1061 corrupt_block_keys = 1;
1070 inode = arg_strtou64(optarg);
1073 strncpy(field, optarg, FIELD_BUF_LEN);
1076 file_extent = arg_strtou64(optarg);
1079 metadata_block = arg_strtou64(optarg);
1082 ret = sscanf(optarg, "%llu,%u,%llu",
1084 (unsigned int *)&key.type,
1087 fprintf(stderr, "error reading key "
1107 if (check_argc_min(ac, 1))
1112 cache_tree_init(&root_cache);
1114 root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
1116 fprintf(stderr, "Open ctree failed\n");
1120 struct btrfs_trans_handle *trans;
1122 if (logical == (u64)-1)
1124 trans = btrfs_start_transaction(root, 1);
1125 ret = corrupt_extent (trans, root, logical, 0);
1126 btrfs_commit_transaction(trans, root);
1130 struct btrfs_trans_handle *trans;
1131 trans = btrfs_start_transaction(root, 1);
1132 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
1133 root->fs_info->extent_root->node);
1134 btrfs_commit_transaction(trans, root);
1138 struct btrfs_trans_handle *trans;
1139 struct btrfs_path *path;
1142 if (logical == (u64)-1)
1145 path = btrfs_alloc_path();
1147 fprintf(stderr, "path allocation failed\n");
1151 if (find_chunk_offset(root->fs_info->chunk_root, path,
1153 btrfs_free_path(path);
1156 trans = btrfs_start_transaction(root, 1);
1157 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
1160 fprintf(stderr, "Failed to corrupt chunk record\n");
1161 btrfs_commit_transaction(trans, root);
1165 struct btrfs_trans_handle *trans;
1166 trans = btrfs_start_transaction(root, 1);
1167 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
1169 fprintf(stderr, "Failed to corrupt chunk tree\n");
1170 btrfs_commit_transaction(trans, root);
1174 struct btrfs_trans_handle *trans;
1179 trans = btrfs_start_transaction(root, 1);
1180 if (file_extent == (u64)-1) {
1181 printf("corrupting inode\n");
1182 ret = corrupt_inode(trans, root, inode, field);
1184 printf("corrupting file extent\n");
1185 ret = corrupt_file_extent(trans, root, inode,
1186 file_extent, field);
1188 btrfs_commit_transaction(trans, root);
1191 if (metadata_block) {
1194 ret = corrupt_metadata_block(root, metadata_block, field);
1198 if (!key.objectid || !strlen(field))
1200 ret = corrupt_dir_item(root, &key, field);
1206 ret = corrupt_btrfs_item(root, &key, field);
1211 ret = delete_item(root, &key);
1214 if (key.objectid || key.offset || key.type) {
1217 ret = corrupt_key(root, &key, field);
1221 * If we made it here and we have extent set then we didn't specify
1222 * inode and we're screwed.
1224 if (file_extent != (u64)-1)
1227 if (logical == (u64)-1)
1231 bytes = root->sectorsize;
1233 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
1234 bytes *= root->sectorsize;
1237 if (corrupt_block_keys) {
1238 corrupt_keys_in_block(root, logical);
1240 eb = debug_corrupt_block(root, logical,
1241 root->sectorsize, copy);
1242 free_extent_buffer(eb);
1244 logical += root->sectorsize;
1245 bytes -= root->sectorsize;