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.
25 #include "kerncompat.h"
29 #include "print-tree.h"
30 #include "transaction.h"
34 #define FIELD_BUF_LEN 80
36 struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
37 u32 blocksize, u64 copy)
40 struct extent_buffer *eb;
42 struct btrfs_multi_bio *multi = NULL;
43 struct btrfs_device *device;
47 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
53 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
54 eb->start, &length, &multi,
57 device = multi->stripes[0].dev;
60 eb->dev_bytenr = multi->stripes[0].physical;
63 "mirror %d logical %llu physical %llu device %s\n",
64 mirror_num, (unsigned long long)bytenr,
65 (unsigned long long)eb->dev_bytenr, device->name);
68 if (!copy || mirror_num == copy) {
69 ret = read_extent_from_disk(eb, 0, eb->len);
70 printf("corrupting %llu copy %d\n", eb->start,
72 memset(eb->data, 0, eb->len);
73 write_extent_to_disk(eb);
77 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
83 if (mirror_num > num_copies)
89 static void print_usage(int ret)
91 fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n");
92 fprintf(stderr, "\t-l Logical extent to be corrupted\n");
93 fprintf(stderr, "\t-c Copy of the extent to be corrupted"
94 " (usually 1 or 2, default: 0)\n");
95 fprintf(stderr, "\t-b Number of bytes to be corrupted\n");
96 fprintf(stderr, "\t-e Extent to be corrupted\n");
97 fprintf(stderr, "\t-E The whole extent tree to be corrupted\n");
98 fprintf(stderr, "\t-u Given chunk item to be corrupted\n");
99 fprintf(stderr, "\t-U The whole chunk tree to be corrupted\n");
100 fprintf(stderr, "\t-i The inode item to corrupt (must also specify "
101 "the field to corrupt)\n");
102 fprintf(stderr, "\t-x The file extent item to corrupt (must also "
103 "specify -i for the inode and -f for the field to corrupt)\n");
104 fprintf(stderr, "\t-m The metadata block to corrupt (must also "
105 "specify -f for the field to corrupt)\n");
106 fprintf(stderr, "\t-K The key to corrupt in the format "
107 "<num>,<num>,<num> (must also specify -f for the field)\n");
108 fprintf(stderr, "\t-f The field in the item to corrupt\n");
109 fprintf(stderr, "\t-I An item to corrupt (must also specify the field "
110 "to corrupt and a root+key for the item)\n");
111 fprintf(stderr, "\t-D Corrupt a dir item, must specify key and field\n");
112 fprintf(stderr, "\t-d Delete this item (must specify -K)\n");
113 fprintf(stderr, "\t-r Operate on this root (only works with -d)\n");
114 fprintf(stderr, "\t-C Delete a csum for the specified bytenr. When "
115 "used with -b it'll delete that many bytes, otherwise it's "
116 "just sectorsize\n");
120 static void corrupt_keys(struct btrfs_trans_handle *trans,
121 struct btrfs_root *root,
122 struct extent_buffer *eb)
127 struct btrfs_disk_key bad_key;;
129 nr = btrfs_header_nritems(eb);
134 bad_slot = rand() % nr;
136 if (bad_slot == slot)
140 "corrupting keys in block %llu slot %d swapping with %d\n",
141 (unsigned long long)eb->start, slot, bad_slot);
143 if (btrfs_header_level(eb) == 0) {
144 btrfs_item_key(eb, &bad_key, bad_slot);
145 btrfs_set_item_key(eb, &bad_key, slot);
147 btrfs_node_key(eb, &bad_key, bad_slot);
148 btrfs_set_node_key(eb, &bad_key, slot);
150 btrfs_mark_buffer_dirty(eb);
153 btrfs_super_csum_size(root->fs_info->super_copy);
154 csum_tree_block_size(eb, csum_size, 0);
155 write_extent_to_disk(eb);
160 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
162 struct extent_buffer *eb;
164 eb = read_tree_block(root, bytenr, root->leafsize, 0);
165 if (!extent_buffer_uptodate(eb))
168 corrupt_keys(NULL, root, eb);
169 free_extent_buffer(eb);
173 static int corrupt_extent(struct btrfs_trans_handle *trans,
174 struct btrfs_root *root, u64 bytenr, u64 copy)
176 struct btrfs_key key;
177 struct extent_buffer *leaf;
180 struct btrfs_path *path;
183 int should_del = rand() % 3;
185 path = btrfs_alloc_path();
189 key.objectid = bytenr;
191 key.offset = (u64)-1;
194 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
200 if (path->slots[0] == 0)
205 leaf = path->nodes[0];
206 slot = path->slots[0];
207 btrfs_item_key_to_cpu(leaf, &key, slot);
208 if (key.objectid != bytenr)
211 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
212 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
213 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
214 key.type != BTRFS_EXTENT_REF_V0_KEY &&
215 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
216 key.type != BTRFS_SHARED_DATA_REF_KEY)
221 "deleting extent record: key %llu %u %llu\n",
222 key.objectid, key.type, key.offset);
224 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
225 /* make sure this extent doesn't get
226 * reused for other purposes */
227 btrfs_pin_extent(root->fs_info,
228 key.objectid, key.offset);
231 btrfs_del_item(trans, root, path);
234 "corrupting extent record: key %llu %u %llu\n",
235 key.objectid, key.type, key.offset);
236 ptr = btrfs_item_ptr_offset(leaf, slot);
237 item_size = btrfs_item_size_nr(leaf, slot);
238 memset_extent_buffer(leaf, 0, ptr, item_size);
239 btrfs_mark_buffer_dirty(leaf);
242 btrfs_release_path(path);
250 btrfs_free_path(path);
254 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
255 struct btrfs_root *root,
256 struct extent_buffer *eb)
258 u32 nr = btrfs_header_nritems(eb);
259 u32 victim = rand() % nr;
261 struct btrfs_key key;
263 btrfs_item_key_to_cpu(eb, &key, victim);
264 objectid = key.objectid;
265 corrupt_extent(trans, root, objectid, 1);
268 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
269 struct btrfs_root *root,
270 struct extent_buffer *eb)
277 if (btrfs_is_leaf(eb)) {
278 btrfs_corrupt_extent_leaf(trans, root, eb);
282 if (btrfs_header_level(eb) == 1 && eb != root->node) {
287 for (i = 0; i < btrfs_header_nritems(eb); i++) {
288 struct extent_buffer *next;
290 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
292 btrfs_node_ptr_generation(eb, i));
293 if (!extent_buffer_uptodate(next))
295 btrfs_corrupt_extent_tree(trans, root, next);
296 free_extent_buffer(next);
300 enum btrfs_inode_field {
301 BTRFS_INODE_FIELD_ISIZE,
302 BTRFS_INODE_FIELD_BAD,
305 enum btrfs_file_extent_field {
306 BTRFS_FILE_EXTENT_DISK_BYTENR,
307 BTRFS_FILE_EXTENT_BAD,
310 enum btrfs_dir_item_field {
312 BTRFS_DIR_ITEM_LOCATION_OBJECTID,
316 enum btrfs_metadata_block_field {
317 BTRFS_METADATA_BLOCK_GENERATION,
318 BTRFS_METADATA_BLOCK_SHIFT_ITEMS,
319 BTRFS_METADATA_BLOCK_BAD,
322 enum btrfs_item_field {
327 enum btrfs_key_field {
334 static enum btrfs_inode_field convert_inode_field(char *field)
336 if (!strncmp(field, "isize", FIELD_BUF_LEN))
337 return BTRFS_INODE_FIELD_ISIZE;
338 return BTRFS_INODE_FIELD_BAD;
341 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
343 if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
344 return BTRFS_FILE_EXTENT_DISK_BYTENR;
345 return BTRFS_FILE_EXTENT_BAD;
348 static enum btrfs_metadata_block_field
349 convert_metadata_block_field(char *field)
351 if (!strncmp(field, "generation", FIELD_BUF_LEN))
352 return BTRFS_METADATA_BLOCK_GENERATION;
353 if (!strncmp(field, "shift_items", FIELD_BUF_LEN))
354 return BTRFS_METADATA_BLOCK_SHIFT_ITEMS;
355 return BTRFS_METADATA_BLOCK_BAD;
358 static enum btrfs_key_field convert_key_field(char *field)
360 if (!strncmp(field, "objectid", FIELD_BUF_LEN))
361 return BTRFS_KEY_OBJECTID;
362 if (!strncmp(field, "type", FIELD_BUF_LEN))
363 return BTRFS_KEY_TYPE;
364 if (!strncmp(field, "offset", FIELD_BUF_LEN))
365 return BTRFS_KEY_OFFSET;
366 return BTRFS_KEY_BAD;
369 static enum btrfs_item_field convert_item_field(char *field)
371 if (!strncmp(field, "offset", FIELD_BUF_LEN))
372 return BTRFS_ITEM_OFFSET;
373 return BTRFS_ITEM_BAD;
376 static enum btrfs_dir_item_field convert_dir_item_field(char *field)
378 if (!strncmp(field, "name", FIELD_BUF_LEN))
379 return BTRFS_DIR_ITEM_NAME;
380 if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
381 return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
382 return BTRFS_DIR_ITEM_BAD;
385 static u64 generate_u64(u64 orig)
390 } while (ret == orig);
394 static u32 generate_u32(u32 orig)
399 } while (ret == orig);
403 static u8 generate_u8(u8 orig)
408 } while (ret == orig);
412 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
415 enum btrfs_key_field corrupt_field = convert_key_field(field);
416 struct btrfs_path *path;
417 struct btrfs_trans_handle *trans;
420 root = root->fs_info->fs_root;
421 if (corrupt_field == BTRFS_KEY_BAD) {
422 fprintf(stderr, "Invalid field %s\n", field);
426 path = btrfs_alloc_path();
430 trans = btrfs_start_transaction(root, 1);
432 btrfs_free_path(path);
433 return PTR_ERR(trans);
436 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
440 fprintf(stderr, "Couldn't find the key to corrupt\n");
445 switch (corrupt_field) {
446 case BTRFS_KEY_OBJECTID:
447 key->objectid = generate_u64(key->objectid);
450 key->type = generate_u8(key->type);
452 case BTRFS_KEY_OFFSET:
453 key->offset = generate_u64(key->objectid);
456 fprintf(stderr, "Invalid field %s, %d\n", field,
462 btrfs_set_item_key_unsafe(root, path, key);
464 btrfs_free_path(path);
465 btrfs_commit_transaction(trans, root);
469 static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
472 struct btrfs_trans_handle *trans;
473 struct btrfs_dir_item *di;
474 struct btrfs_path *path;
476 struct btrfs_key location;
477 struct btrfs_disk_key disk_key;
478 unsigned long name_ptr;
479 enum btrfs_dir_item_field corrupt_field =
480 convert_dir_item_field(field);
485 if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
486 fprintf(stderr, "Invalid field %s\n", field);
490 path = btrfs_alloc_path();
494 trans = btrfs_start_transaction(root, 1);
496 btrfs_free_path(path);
497 return PTR_ERR(trans);
500 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
504 fprintf(stderr, "Error searching for dir item %d\n", ret);
508 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
509 struct btrfs_dir_item);
511 switch (corrupt_field) {
512 case BTRFS_DIR_ITEM_NAME:
513 name_len = btrfs_dir_name_len(path->nodes[0], di);
514 name = malloc(name_len);
519 name_ptr = (unsigned long)(di + 1);
520 read_extent_buffer(path->nodes[0], name, name_ptr, name_len);
522 write_extent_buffer(path->nodes[0], name, name_ptr, name_len);
523 btrfs_mark_buffer_dirty(path->nodes[0]);
526 case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
527 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
528 bogus = generate_u64(location.objectid);
529 location.objectid = bogus;
530 btrfs_cpu_key_to_disk(&disk_key, &location);
531 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
532 btrfs_mark_buffer_dirty(path->nodes[0]);
539 btrfs_commit_transaction(trans, root);
540 btrfs_free_path(path);
544 static int corrupt_inode(struct btrfs_trans_handle *trans,
545 struct btrfs_root *root, u64 inode, char *field)
547 struct btrfs_inode_item *ei;
548 struct btrfs_path *path;
549 struct btrfs_key key;
550 enum btrfs_inode_field corrupt_field = convert_inode_field(field);
555 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
556 fprintf(stderr, "Invalid field %s\n", field);
560 key.objectid = inode;
561 key.type = BTRFS_INODE_ITEM_KEY;
562 key.offset = (u64)-1;
564 path = btrfs_alloc_path();
568 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
572 if (!path->slots[0]) {
573 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
581 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
582 if (key.objectid != inode) {
583 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
588 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
589 struct btrfs_inode_item);
590 switch (corrupt_field) {
591 case BTRFS_INODE_FIELD_ISIZE:
592 orig = btrfs_inode_size(path->nodes[0], ei);
593 bogus = generate_u64(orig);
594 btrfs_set_inode_size(path->nodes[0], ei, bogus);
600 btrfs_mark_buffer_dirty(path->nodes[0]);
602 btrfs_free_path(path);
606 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
607 struct btrfs_root *root, u64 inode, u64 extent,
610 struct btrfs_file_extent_item *fi;
611 struct btrfs_path *path;
612 struct btrfs_key key;
613 enum btrfs_file_extent_field corrupt_field;
618 corrupt_field = convert_file_extent_field(field);
619 if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
620 fprintf(stderr, "Invalid field %s\n", field);
624 key.objectid = inode;
625 key.type = BTRFS_EXTENT_DATA_KEY;
628 path = btrfs_alloc_path();
632 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
636 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
642 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
643 struct btrfs_file_extent_item);
644 switch (corrupt_field) {
645 case BTRFS_FILE_EXTENT_DISK_BYTENR:
646 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
647 bogus = generate_u64(orig);
648 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
654 btrfs_mark_buffer_dirty(path->nodes[0]);
656 btrfs_free_path(path);
660 static void shift_items(struct btrfs_root *root, struct extent_buffer *eb)
662 int nritems = btrfs_header_nritems(eb);
663 int shift_space = btrfs_leaf_free_space(root, eb) / 2;
664 int slot = nritems / 2;
666 unsigned int data_end = btrfs_item_offset_nr(eb, nritems - 1);
668 /* Shift the item data up to and including slot back by shift space */
669 memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end - shift_space,
670 btrfs_leaf_data(eb) + data_end,
671 btrfs_item_offset_nr(eb, slot - 1) - data_end);
673 /* Now update the item pointers. */
674 for (i = nritems - 1; i >= slot; i--) {
675 u32 offset = btrfs_item_offset_nr(eb, i);
676 offset -= shift_space;
677 btrfs_set_item_offset(eb, btrfs_item_nr(i), offset);
681 static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
684 struct btrfs_trans_handle *trans;
685 struct btrfs_path *path;
686 struct extent_buffer *eb;
687 struct btrfs_key key, root_key;
688 enum btrfs_metadata_block_field corrupt_field;
694 corrupt_field = convert_metadata_block_field(field);
695 if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
696 fprintf(stderr, "Invalid field %s\n", field);
700 eb = read_tree_block(root, block, root->leafsize, 0);
701 if (!extent_buffer_uptodate(eb)) {
702 fprintf(stderr, "Couldn't read in tree block %s\n", field);
705 root_objectid = btrfs_header_owner(eb);
706 level = btrfs_header_level(eb);
708 btrfs_node_key_to_cpu(eb, &key, 0);
710 btrfs_item_key_to_cpu(eb, &key, 0);
711 free_extent_buffer(eb);
713 root_key.objectid = root_objectid;
714 root_key.type = BTRFS_ROOT_ITEM_KEY;
715 root_key.offset = (u64)-1;
717 root = btrfs_read_fs_root(root->fs_info, &root_key);
719 fprintf(stderr, "Couldn't finde owner root %llu\n",
721 return PTR_ERR(root);
724 path = btrfs_alloc_path();
728 trans = btrfs_start_transaction(root, 1);
730 btrfs_free_path(path);
731 fprintf(stderr, "Couldn't start transaction %ld\n",
733 return PTR_ERR(trans);
736 path->lowest_level = level;
737 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
739 fprintf(stderr, "Error searching to node %d\n", ret);
742 eb = path->nodes[level];
745 switch (corrupt_field) {
746 case BTRFS_METADATA_BLOCK_GENERATION:
747 orig = btrfs_header_generation(eb);
748 bogus = generate_u64(orig);
749 btrfs_set_header_generation(eb, bogus);
751 case BTRFS_METADATA_BLOCK_SHIFT_ITEMS:
752 shift_items(root, path->nodes[level]);
758 btrfs_mark_buffer_dirty(path->nodes[level]);
760 btrfs_commit_transaction(trans, root);
761 btrfs_free_path(path);
765 static int corrupt_btrfs_item(struct btrfs_root *root, struct btrfs_key *key,
768 struct btrfs_trans_handle *trans;
769 struct btrfs_path *path;
770 enum btrfs_item_field corrupt_field;
774 corrupt_field = convert_item_field(field);
775 if (corrupt_field == BTRFS_ITEM_BAD) {
776 fprintf(stderr, "Invalid field %s\n", field);
780 path = btrfs_alloc_path();
784 trans = btrfs_start_transaction(root, 1);
786 btrfs_free_path(path);
787 fprintf(stderr, "Couldn't start transaction %ld\n",
789 return PTR_ERR(trans);
792 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
794 fprintf(stderr, "Error searching to node %d\n", ret);
799 switch (corrupt_field) {
800 case BTRFS_ITEM_OFFSET:
801 orig = btrfs_item_offset_nr(path->nodes[0], path->slots[0]);
802 bogus = generate_u32(orig);
803 btrfs_set_item_offset(path->nodes[0],
804 btrfs_item_nr(path->slots[0]), bogus);
810 btrfs_mark_buffer_dirty(path->nodes[0]);
812 btrfs_commit_transaction(trans, root);
813 btrfs_free_path(path);
817 static int delete_item(struct btrfs_root *root, struct btrfs_key *key)
819 struct btrfs_trans_handle *trans;
820 struct btrfs_path *path;
823 path = btrfs_alloc_path();
827 trans = btrfs_start_transaction(root, 1);
829 btrfs_free_path(path);
830 fprintf(stderr, "Couldn't start transaction %ld\n",
832 return PTR_ERR(trans);
835 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
839 fprintf(stderr, "Error searching to node %d\n", ret);
842 ret = btrfs_del_item(trans, root, path);
843 btrfs_mark_buffer_dirty(path->nodes[0]);
845 btrfs_commit_transaction(trans, root);
846 btrfs_free_path(path);
850 static int delete_csum(struct btrfs_root *root, u64 bytenr, u64 bytes)
852 struct btrfs_trans_handle *trans;
855 root = root->fs_info->csum_root;
856 trans = btrfs_start_transaction(root, 1);
858 fprintf(stderr, "Couldn't start transaction %ld\n",
860 return PTR_ERR(trans);
863 ret = btrfs_del_csums(trans, root, bytenr, bytes);
865 fprintf(stderr, "Error deleting csums %d\n", ret);
866 btrfs_commit_transaction(trans, root);
870 /* corrupt item using NO cow.
871 * Because chunk recover will recover based on whole partition scaning,
872 * If using COW, chunk recover will use the old item to recover,
873 * which is still OK but we want to check the ability to rebuild chunk
874 * not only restore the old ones */
875 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
876 struct btrfs_root *root, struct btrfs_path *path,
880 struct btrfs_key key;
881 struct extent_buffer *leaf;
886 leaf = path->nodes[0];
887 slot = path->slots[0];
888 /* Not deleting the first item of a leaf to keep leaf structure */
891 /* Only accept valid eb */
892 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
893 btrfs_item_key_to_cpu(leaf, &key, slot);
895 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
896 key.objectid, key.type, key.offset);
897 btrfs_del_item(trans, root, path);
899 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
900 key.objectid, key.type, key.offset);
901 ptr = btrfs_item_ptr_offset(leaf, slot);
902 item_size = btrfs_item_size_nr(leaf, slot);
903 memset_extent_buffer(leaf, 0, ptr, item_size);
904 btrfs_mark_buffer_dirty(leaf);
908 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
909 struct btrfs_root *root)
914 struct btrfs_path *path;
915 struct btrfs_key key;
916 struct btrfs_key found_key;
917 struct extent_buffer *leaf;
919 path = btrfs_alloc_path();
923 key.objectid = (u64)-1;
924 key.offset = (u64)-1;
927 /* Here, cow and ins_len must equals 0 for the following reasons:
928 * 1) chunk recover is based on disk scanning, so COW should be
929 * disabled in case the original chunk being scanned and
930 * recovered using the old chunk.
931 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
934 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
937 fprintf(stderr, "Error searching tree\n");
940 /* corrupt/del dev_item first */
941 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
942 slot = path->slots[0];
943 leaf = path->nodes[0];
945 /* Never delete the first item to keep the leaf structure */
946 if (path->slots[0] == 0)
948 ret = corrupt_item_nocow(trans, root, path, del);
952 btrfs_release_path(path);
954 /* Here, cow and ins_len must equals 0 for the following reasons:
955 * 1) chunk recover is based on disk scanning, so COW should be
956 * disabled in case the original chunk being scanned and
957 * recovered using the old chunk.
958 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
961 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
964 fprintf(stderr, "Error searching tree\n");
967 /* corrupt/del chunk then*/
968 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
969 slot = path->slots[0];
970 leaf = path->nodes[0];
972 btrfs_item_key_to_cpu(leaf, &found_key, slot);
973 ret = corrupt_item_nocow(trans, root, path, del);
978 btrfs_free_path(path);
981 int find_chunk_offset(struct btrfs_root *root,
982 struct btrfs_path *path, u64 offset)
984 struct btrfs_key key;
987 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
988 key.type = BTRFS_CHUNK_ITEM_KEY;
991 /* Here, cow and ins_len must equals 0 for following reasons:
992 * 1) chunk recover is based on disk scanning, so COW should
993 * be disabled in case the original chunk being scanned
994 * and recovered using the old chunk.
995 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
998 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1000 fprintf(stderr, "Can't find chunk with given offset %llu\n",
1005 fprintf(stderr, "Error searching chunk");
1012 int main(int ac, char **av)
1014 struct cache_tree root_cache;
1015 struct btrfs_key key;
1016 struct btrfs_root *root;
1017 struct extent_buffer *eb;
1019 /* chunk offset can be 0,so change to (u64)-1 */
1020 u64 logical = (u64)-1;
1025 int extent_tree = 0;
1026 int corrupt_block_keys = 0;
1029 int corrupt_item = 0;
1032 u64 metadata_block = 0;
1034 u64 file_extent = (u64)-1;
1035 u64 root_objectid = 0;
1036 u64 csum_bytenr = 0;
1037 char field[FIELD_BUF_LEN];
1041 memset(&key, 0, sizeof(key));
1045 static const struct option long_options[] = {
1046 /* { "byte-count", 1, NULL, 'b' }, */
1047 { "logical", required_argument, NULL, 'l' },
1048 { "copy", required_argument, NULL, 'c' },
1049 { "bytes", required_argument, NULL, 'b' },
1050 { "extent-record", no_argument, NULL, 'e' },
1051 { "extent-tree", no_argument, NULL, 'E' },
1052 { "keys", no_argument, NULL, 'k' },
1053 { "chunk-record", no_argument, NULL, 'u' },
1054 { "chunk-tree", no_argument, NULL, 'U' },
1055 { "inode", required_argument, NULL, 'i'},
1056 { "file-extent", required_argument, NULL, 'x'},
1057 { "metadata-block", required_argument, NULL, 'm'},
1058 { "field", required_argument, NULL, 'f'},
1059 { "key", required_argument, NULL, 'K'},
1060 { "item", no_argument, NULL, 'I'},
1061 { "dir-item", no_argument, NULL, 'D'},
1062 { "delete", no_argument, NULL, 'd'},
1063 { "root", no_argument, NULL, 'r'},
1064 { "csum", required_argument, NULL, 'C'},
1065 { "help", no_argument, NULL, GETOPT_VAL_HELP},
1066 { NULL, 0, NULL, 0 }
1069 c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:IDdr:C:",
1070 long_options, NULL);
1075 logical = arg_strtou64(optarg);
1078 copy = arg_strtou64(optarg);
1081 bytes = arg_strtou64(optarg);
1090 corrupt_block_keys = 1;
1099 inode = arg_strtou64(optarg);
1102 strncpy(field, optarg, FIELD_BUF_LEN);
1105 file_extent = arg_strtou64(optarg);
1108 metadata_block = arg_strtou64(optarg);
1111 ret = sscanf(optarg, "%llu,%u,%llu",
1113 (unsigned int *)&key.type,
1116 fprintf(stderr, "error reading key "
1131 root_objectid = arg_strtou64(optarg);
1134 csum_bytenr = arg_strtou64(optarg);
1136 case GETOPT_VAL_HELP:
1138 print_usage(c != GETOPT_VAL_HELP);
1143 if (check_argc_min(ac, 1))
1148 cache_tree_init(&root_cache);
1150 root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
1152 fprintf(stderr, "Open ctree failed\n");
1156 struct btrfs_trans_handle *trans;
1158 if (logical == (u64)-1)
1160 trans = btrfs_start_transaction(root, 1);
1161 ret = corrupt_extent (trans, root, logical, 0);
1162 btrfs_commit_transaction(trans, root);
1166 struct btrfs_trans_handle *trans;
1167 trans = btrfs_start_transaction(root, 1);
1168 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
1169 root->fs_info->extent_root->node);
1170 btrfs_commit_transaction(trans, root);
1174 struct btrfs_trans_handle *trans;
1175 struct btrfs_path *path;
1178 if (logical == (u64)-1)
1181 path = btrfs_alloc_path();
1183 fprintf(stderr, "path allocation failed\n");
1187 if (find_chunk_offset(root->fs_info->chunk_root, path,
1189 btrfs_free_path(path);
1192 trans = btrfs_start_transaction(root, 1);
1193 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
1196 fprintf(stderr, "Failed to corrupt chunk record\n");
1197 btrfs_commit_transaction(trans, root);
1201 struct btrfs_trans_handle *trans;
1202 trans = btrfs_start_transaction(root, 1);
1203 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
1205 fprintf(stderr, "Failed to corrupt chunk tree\n");
1206 btrfs_commit_transaction(trans, root);
1210 struct btrfs_trans_handle *trans;
1215 trans = btrfs_start_transaction(root, 1);
1216 if (file_extent == (u64)-1) {
1217 printf("corrupting inode\n");
1218 ret = corrupt_inode(trans, root, inode, field);
1220 printf("corrupting file extent\n");
1221 ret = corrupt_file_extent(trans, root, inode,
1222 file_extent, field);
1224 btrfs_commit_transaction(trans, root);
1227 if (metadata_block) {
1230 ret = corrupt_metadata_block(root, metadata_block, field);
1234 if (!key.objectid || !strlen(field))
1236 ret = corrupt_dir_item(root, &key, field);
1240 ret = delete_csum(root, csum_bytenr, bytes);
1246 ret = corrupt_btrfs_item(root, &key, field);
1249 struct btrfs_root *target = root;
1253 if (root_objectid) {
1254 struct btrfs_key root_key;
1256 root_key.objectid = root_objectid;
1257 root_key.type = BTRFS_ROOT_ITEM_KEY;
1258 root_key.offset = (u64)-1;
1260 target = btrfs_read_fs_root(root->fs_info, &root_key);
1261 if (IS_ERR(target)) {
1262 fprintf(stderr, "Couldn't find root %llu\n",
1263 (unsigned long long)root_objectid);
1267 ret = delete_item(target, &key);
1270 if (key.objectid || key.offset || key.type) {
1273 ret = corrupt_key(root, &key, field);
1277 * If we made it here and we have extent set then we didn't specify
1278 * inode and we're screwed.
1280 if (file_extent != (u64)-1)
1283 if (logical == (u64)-1)
1287 bytes = root->sectorsize;
1289 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
1290 bytes *= root->sectorsize;
1293 if (corrupt_block_keys) {
1294 corrupt_keys_in_block(root, logical);
1296 eb = debug_corrupt_block(root, logical,
1297 root->sectorsize, copy);
1298 free_extent_buffer(eb);
1300 logical += root->sectorsize;
1301 bytes -= root->sectorsize;