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.
26 #include "kerncompat.h"
30 #include "print-tree.h"
31 #include "transaction.h"
35 #define FIELD_BUF_LEN 80
37 static struct extent_buffer *debug_corrupt_block(struct btrfs_root *root,
38 u64 bytenr, u32 blocksize, u64 copy)
41 struct extent_buffer *eb;
43 struct btrfs_multi_bio *multi = NULL;
44 struct btrfs_device *device;
48 eb = btrfs_find_create_tree_block(root->fs_info, bytenr, blocksize);
54 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
55 eb->start, &length, &multi,
58 device = multi->stripes[0].dev;
61 eb->dev_bytenr = multi->stripes[0].physical;
64 "mirror %d logical %llu physical %llu device %s\n",
65 mirror_num, (unsigned long long)bytenr,
66 (unsigned long long)eb->dev_bytenr, device->name);
69 if (!copy || mirror_num == copy) {
70 ret = read_extent_from_disk(eb, 0, eb->len);
71 printf("corrupting %llu copy %d\n", eb->start,
73 memset(eb->data, 0, eb->len);
74 write_extent_to_disk(eb);
78 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
84 if (mirror_num > num_copies)
90 static void print_usage(int ret)
92 fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n");
93 fprintf(stderr, "\t-l Logical extent to be corrupted\n");
94 fprintf(stderr, "\t-c Copy of the extent to be corrupted"
95 " (usually 1 or 2, default: 0)\n");
96 fprintf(stderr, "\t-b Number of bytes to be corrupted\n");
97 fprintf(stderr, "\t-e Extent to be corrupted\n");
98 fprintf(stderr, "\t-E The whole extent tree to be corrupted\n");
99 fprintf(stderr, "\t-u Given chunk item to be corrupted\n");
100 fprintf(stderr, "\t-U The whole chunk tree to be corrupted\n");
101 fprintf(stderr, "\t-i The inode item to corrupt (must also specify "
102 "the field to corrupt)\n");
103 fprintf(stderr, "\t-x The file extent item to corrupt (must also "
104 "specify -i for the inode and -f for the field to corrupt)\n");
105 fprintf(stderr, "\t-m The metadata block to corrupt (must also "
106 "specify -f for the field to corrupt)\n");
107 fprintf(stderr, "\t-K The key to corrupt in the format "
108 "<num>,<num>,<num> (must also specify -f for the field)\n");
109 fprintf(stderr, "\t-f The field in the item to corrupt\n");
110 fprintf(stderr, "\t-I An item to corrupt (must also specify the field "
111 "to corrupt and a root+key for the item)\n");
112 fprintf(stderr, "\t-D Corrupt a dir item, must specify key and field\n");
113 fprintf(stderr, "\t-d Delete this item (must specify -K)\n");
114 fprintf(stderr, "\t-r Operate on this root (only works with -d)\n");
115 fprintf(stderr, "\t-C Delete a csum for the specified bytenr. When "
116 "used with -b it'll delete that many bytes, otherwise it's "
117 "just sectorsize\n");
121 static void corrupt_keys(struct btrfs_trans_handle *trans,
122 struct btrfs_root *root,
123 struct extent_buffer *eb)
128 struct btrfs_disk_key bad_key;;
130 nr = btrfs_header_nritems(eb);
134 slot = rand_range(nr);
135 bad_slot = rand_range(nr);
137 if (bad_slot == slot)
141 "corrupting keys in block %llu slot %d swapping with %d\n",
142 (unsigned long long)eb->start, slot, bad_slot);
144 if (btrfs_header_level(eb) == 0) {
145 btrfs_item_key(eb, &bad_key, bad_slot);
146 btrfs_set_item_key(eb, &bad_key, slot);
148 btrfs_node_key(eb, &bad_key, bad_slot);
149 btrfs_set_node_key(eb, &bad_key, slot);
151 btrfs_mark_buffer_dirty(eb);
154 btrfs_super_csum_size(root->fs_info->super_copy);
155 csum_tree_block_size(eb, csum_size, 0);
156 write_extent_to_disk(eb);
161 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
163 struct extent_buffer *eb;
165 eb = read_tree_block(root, bytenr, root->nodesize, 0);
166 if (!extent_buffer_uptodate(eb))
169 corrupt_keys(NULL, root, eb);
170 free_extent_buffer(eb);
174 static int corrupt_extent(struct btrfs_trans_handle *trans,
175 struct btrfs_root *root, u64 bytenr, u64 copy)
177 struct btrfs_key key;
178 struct extent_buffer *leaf;
181 struct btrfs_path *path;
184 int should_del = rand_range(3);
186 path = btrfs_alloc_path();
190 key.objectid = bytenr;
192 key.offset = (u64)-1;
195 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
201 if (path->slots[0] == 0)
206 leaf = path->nodes[0];
207 slot = path->slots[0];
208 btrfs_item_key_to_cpu(leaf, &key, slot);
209 if (key.objectid != bytenr)
212 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
213 key.type != BTRFS_METADATA_ITEM_KEY &&
214 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
215 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
216 key.type != BTRFS_EXTENT_REF_V0_KEY &&
217 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
218 key.type != BTRFS_SHARED_DATA_REF_KEY)
223 "deleting extent record: key %llu %u %llu\n",
224 key.objectid, key.type, key.offset);
226 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
227 /* make sure this extent doesn't get
228 * reused for other purposes */
229 btrfs_pin_extent(root->fs_info,
230 key.objectid, key.offset);
233 btrfs_del_item(trans, root, path);
236 "corrupting extent record: key %llu %u %llu\n",
237 key.objectid, key.type, key.offset);
238 ptr = btrfs_item_ptr_offset(leaf, slot);
239 item_size = btrfs_item_size_nr(leaf, slot);
240 memset_extent_buffer(leaf, 0, ptr, item_size);
241 btrfs_mark_buffer_dirty(leaf);
244 btrfs_release_path(path);
252 btrfs_free_path(path);
256 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
257 struct btrfs_root *root,
258 struct extent_buffer *eb)
260 u32 nr = btrfs_header_nritems(eb);
261 u32 victim = rand_range(nr);
263 struct btrfs_key key;
265 btrfs_item_key_to_cpu(eb, &key, victim);
266 objectid = key.objectid;
267 corrupt_extent(trans, root, objectid, 1);
270 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
271 struct btrfs_root *root,
272 struct extent_buffer *eb)
279 if (btrfs_is_leaf(eb)) {
280 btrfs_corrupt_extent_leaf(trans, root, eb);
284 if (btrfs_header_level(eb) == 1 && eb != root->node) {
289 for (i = 0; i < btrfs_header_nritems(eb); i++) {
290 struct extent_buffer *next;
292 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
294 btrfs_node_ptr_generation(eb, i));
295 if (!extent_buffer_uptodate(next))
297 btrfs_corrupt_extent_tree(trans, root, next);
298 free_extent_buffer(next);
302 enum btrfs_inode_field {
303 BTRFS_INODE_FIELD_ISIZE,
304 BTRFS_INODE_FIELD_NBYTES,
305 BTRFS_INODE_FIELD_BAD,
308 enum btrfs_file_extent_field {
309 BTRFS_FILE_EXTENT_DISK_BYTENR,
310 BTRFS_FILE_EXTENT_BAD,
313 enum btrfs_dir_item_field {
315 BTRFS_DIR_ITEM_LOCATION_OBJECTID,
319 enum btrfs_metadata_block_field {
320 BTRFS_METADATA_BLOCK_GENERATION,
321 BTRFS_METADATA_BLOCK_SHIFT_ITEMS,
322 BTRFS_METADATA_BLOCK_BAD,
325 enum btrfs_item_field {
330 enum btrfs_key_field {
337 static enum btrfs_inode_field convert_inode_field(char *field)
339 if (!strncmp(field, "isize", FIELD_BUF_LEN))
340 return BTRFS_INODE_FIELD_ISIZE;
341 if (!strncmp(field, "nbytes", FIELD_BUF_LEN))
342 return BTRFS_INODE_FIELD_NBYTES;
343 return BTRFS_INODE_FIELD_BAD;
346 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
348 if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
349 return BTRFS_FILE_EXTENT_DISK_BYTENR;
350 return BTRFS_FILE_EXTENT_BAD;
353 static enum btrfs_metadata_block_field
354 convert_metadata_block_field(char *field)
356 if (!strncmp(field, "generation", FIELD_BUF_LEN))
357 return BTRFS_METADATA_BLOCK_GENERATION;
358 if (!strncmp(field, "shift_items", FIELD_BUF_LEN))
359 return BTRFS_METADATA_BLOCK_SHIFT_ITEMS;
360 return BTRFS_METADATA_BLOCK_BAD;
363 static enum btrfs_key_field convert_key_field(char *field)
365 if (!strncmp(field, "objectid", FIELD_BUF_LEN))
366 return BTRFS_KEY_OBJECTID;
367 if (!strncmp(field, "type", FIELD_BUF_LEN))
368 return BTRFS_KEY_TYPE;
369 if (!strncmp(field, "offset", FIELD_BUF_LEN))
370 return BTRFS_KEY_OFFSET;
371 return BTRFS_KEY_BAD;
374 static enum btrfs_item_field convert_item_field(char *field)
376 if (!strncmp(field, "offset", FIELD_BUF_LEN))
377 return BTRFS_ITEM_OFFSET;
378 return BTRFS_ITEM_BAD;
381 static enum btrfs_dir_item_field convert_dir_item_field(char *field)
383 if (!strncmp(field, "name", FIELD_BUF_LEN))
384 return BTRFS_DIR_ITEM_NAME;
385 if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
386 return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
387 return BTRFS_DIR_ITEM_BAD;
390 static u64 generate_u64(u64 orig)
395 } while (ret == orig);
399 static u32 generate_u32(u32 orig)
404 } while (ret == orig);
408 static u8 generate_u8(u8 orig)
413 } while (ret == orig);
417 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
420 enum btrfs_key_field corrupt_field = convert_key_field(field);
421 struct btrfs_path *path;
422 struct btrfs_trans_handle *trans;
425 root = root->fs_info->fs_root;
426 if (corrupt_field == BTRFS_KEY_BAD) {
427 fprintf(stderr, "Invalid field %s\n", field);
431 path = btrfs_alloc_path();
435 trans = btrfs_start_transaction(root, 1);
437 btrfs_free_path(path);
438 return PTR_ERR(trans);
441 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
445 fprintf(stderr, "Couldn't find the key to corrupt\n");
450 switch (corrupt_field) {
451 case BTRFS_KEY_OBJECTID:
452 key->objectid = generate_u64(key->objectid);
455 key->type = generate_u8(key->type);
457 case BTRFS_KEY_OFFSET:
458 key->offset = generate_u64(key->objectid);
461 fprintf(stderr, "Invalid field %s, %d\n", field,
467 btrfs_set_item_key_unsafe(root, path, key);
469 btrfs_free_path(path);
470 btrfs_commit_transaction(trans, root);
474 static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
477 struct btrfs_trans_handle *trans;
478 struct btrfs_dir_item *di;
479 struct btrfs_path *path;
481 struct btrfs_key location;
482 struct btrfs_disk_key disk_key;
483 unsigned long name_ptr;
484 enum btrfs_dir_item_field corrupt_field =
485 convert_dir_item_field(field);
490 if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
491 fprintf(stderr, "Invalid field %s\n", field);
495 path = btrfs_alloc_path();
499 trans = btrfs_start_transaction(root, 1);
501 btrfs_free_path(path);
502 return PTR_ERR(trans);
505 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
509 fprintf(stderr, "Error searching for dir item %d\n", ret);
513 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
514 struct btrfs_dir_item);
516 switch (corrupt_field) {
517 case BTRFS_DIR_ITEM_NAME:
518 name_len = btrfs_dir_name_len(path->nodes[0], di);
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]);
525 case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
526 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
527 bogus = generate_u64(location.objectid);
528 location.objectid = bogus;
529 btrfs_cpu_key_to_disk(&disk_key, &location);
530 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
531 btrfs_mark_buffer_dirty(path->nodes[0]);
538 btrfs_commit_transaction(trans, root);
539 btrfs_free_path(path);
543 static int corrupt_inode(struct btrfs_trans_handle *trans,
544 struct btrfs_root *root, u64 inode, char *field)
546 struct btrfs_inode_item *ei;
547 struct btrfs_path *path;
548 struct btrfs_key key;
549 enum btrfs_inode_field corrupt_field = convert_inode_field(field);
554 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
555 fprintf(stderr, "Invalid field %s\n", field);
559 key.objectid = inode;
560 key.type = BTRFS_INODE_ITEM_KEY;
561 key.offset = (u64)-1;
563 path = btrfs_alloc_path();
567 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
571 if (!path->slots[0]) {
572 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
580 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
581 if (key.objectid != inode) {
582 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
587 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
588 struct btrfs_inode_item);
589 switch (corrupt_field) {
590 case BTRFS_INODE_FIELD_ISIZE:
591 orig = btrfs_inode_size(path->nodes[0], ei);
592 bogus = generate_u64(orig);
593 btrfs_set_inode_size(path->nodes[0], ei, bogus);
595 case BTRFS_INODE_FIELD_NBYTES:
596 orig = btrfs_inode_nbytes(path->nodes[0], ei);
597 bogus = generate_u64(orig);
598 btrfs_set_inode_nbytes(path->nodes[0], ei, bogus);
604 btrfs_mark_buffer_dirty(path->nodes[0]);
606 btrfs_free_path(path);
610 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
611 struct btrfs_root *root, u64 inode, u64 extent,
614 struct btrfs_file_extent_item *fi;
615 struct btrfs_path *path;
616 struct btrfs_key key;
617 enum btrfs_file_extent_field corrupt_field;
622 corrupt_field = convert_file_extent_field(field);
623 if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
624 fprintf(stderr, "Invalid field %s\n", field);
628 key.objectid = inode;
629 key.type = BTRFS_EXTENT_DATA_KEY;
632 path = btrfs_alloc_path();
636 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
640 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
646 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
647 struct btrfs_file_extent_item);
648 switch (corrupt_field) {
649 case BTRFS_FILE_EXTENT_DISK_BYTENR:
650 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
651 bogus = generate_u64(orig);
652 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
658 btrfs_mark_buffer_dirty(path->nodes[0]);
660 btrfs_free_path(path);
664 static void shift_items(struct btrfs_root *root, struct extent_buffer *eb)
666 int nritems = btrfs_header_nritems(eb);
667 int shift_space = btrfs_leaf_free_space(root, eb) / 2;
668 int slot = nritems / 2;
670 unsigned int data_end = btrfs_item_offset_nr(eb, nritems - 1);
672 /* Shift the item data up to and including slot back by shift space */
673 memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end - shift_space,
674 btrfs_leaf_data(eb) + data_end,
675 btrfs_item_offset_nr(eb, slot - 1) - data_end);
677 /* Now update the item pointers. */
678 for (i = nritems - 1; i >= slot; i--) {
679 u32 offset = btrfs_item_offset_nr(eb, i);
680 offset -= shift_space;
681 btrfs_set_item_offset(eb, btrfs_item_nr(i), offset);
685 static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
688 struct btrfs_trans_handle *trans;
689 struct btrfs_path *path;
690 struct extent_buffer *eb;
691 struct btrfs_key key, root_key;
692 enum btrfs_metadata_block_field corrupt_field;
698 corrupt_field = convert_metadata_block_field(field);
699 if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
700 fprintf(stderr, "Invalid field %s\n", field);
704 eb = read_tree_block(root, block, root->nodesize, 0);
705 if (!extent_buffer_uptodate(eb)) {
706 fprintf(stderr, "Couldn't read in tree block %s\n", field);
709 root_objectid = btrfs_header_owner(eb);
710 level = btrfs_header_level(eb);
712 btrfs_node_key_to_cpu(eb, &key, 0);
714 btrfs_item_key_to_cpu(eb, &key, 0);
715 free_extent_buffer(eb);
717 root_key.objectid = root_objectid;
718 root_key.type = BTRFS_ROOT_ITEM_KEY;
719 root_key.offset = (u64)-1;
721 root = btrfs_read_fs_root(root->fs_info, &root_key);
723 fprintf(stderr, "Couldn't find owner root %llu\n",
725 return PTR_ERR(root);
728 path = btrfs_alloc_path();
732 trans = btrfs_start_transaction(root, 1);
734 btrfs_free_path(path);
735 fprintf(stderr, "Couldn't start transaction %ld\n",
737 return PTR_ERR(trans);
740 path->lowest_level = level;
741 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
743 fprintf(stderr, "Error searching to node %d\n", ret);
746 eb = path->nodes[level];
749 switch (corrupt_field) {
750 case BTRFS_METADATA_BLOCK_GENERATION:
751 orig = btrfs_header_generation(eb);
752 bogus = generate_u64(orig);
753 btrfs_set_header_generation(eb, bogus);
755 case BTRFS_METADATA_BLOCK_SHIFT_ITEMS:
756 shift_items(root, path->nodes[level]);
762 btrfs_mark_buffer_dirty(path->nodes[level]);
764 btrfs_commit_transaction(trans, root);
765 btrfs_free_path(path);
769 static int corrupt_btrfs_item(struct btrfs_root *root, struct btrfs_key *key,
772 struct btrfs_trans_handle *trans;
773 struct btrfs_path *path;
774 enum btrfs_item_field corrupt_field;
778 corrupt_field = convert_item_field(field);
779 if (corrupt_field == BTRFS_ITEM_BAD) {
780 fprintf(stderr, "Invalid field %s\n", field);
784 path = btrfs_alloc_path();
788 trans = btrfs_start_transaction(root, 1);
790 btrfs_free_path(path);
791 fprintf(stderr, "Couldn't start transaction %ld\n",
793 return PTR_ERR(trans);
796 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
798 fprintf(stderr, "Error searching to node %d\n", ret);
803 switch (corrupt_field) {
804 case BTRFS_ITEM_OFFSET:
805 orig = btrfs_item_offset_nr(path->nodes[0], path->slots[0]);
806 bogus = generate_u32(orig);
807 btrfs_set_item_offset(path->nodes[0],
808 btrfs_item_nr(path->slots[0]), bogus);
814 btrfs_mark_buffer_dirty(path->nodes[0]);
816 btrfs_commit_transaction(trans, root);
817 btrfs_free_path(path);
821 static int delete_item(struct btrfs_root *root, struct btrfs_key *key)
823 struct btrfs_trans_handle *trans;
824 struct btrfs_path *path;
827 path = btrfs_alloc_path();
831 trans = btrfs_start_transaction(root, 1);
833 btrfs_free_path(path);
834 fprintf(stderr, "Couldn't start transaction %ld\n",
836 return PTR_ERR(trans);
839 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
843 fprintf(stderr, "Error searching to node %d\n", ret);
846 ret = btrfs_del_item(trans, root, path);
847 btrfs_mark_buffer_dirty(path->nodes[0]);
849 btrfs_commit_transaction(trans, root);
850 btrfs_free_path(path);
854 static int delete_csum(struct btrfs_root *root, u64 bytenr, u64 bytes)
856 struct btrfs_trans_handle *trans;
859 root = root->fs_info->csum_root;
860 trans = btrfs_start_transaction(root, 1);
862 fprintf(stderr, "Couldn't start transaction %ld\n",
864 return PTR_ERR(trans);
867 ret = btrfs_del_csums(trans, root, bytenr, bytes);
869 fprintf(stderr, "Error deleting csums %d\n", ret);
870 btrfs_commit_transaction(trans, root);
874 /* corrupt item using NO cow.
875 * Because chunk recover will recover based on whole partition scanning,
876 * If using COW, chunk recover will use the old item to recover,
877 * which is still OK but we want to check the ability to rebuild chunk
878 * not only restore the old ones */
879 static int corrupt_item_nocow(struct btrfs_trans_handle *trans,
880 struct btrfs_root *root, struct btrfs_path *path,
884 struct btrfs_key key;
885 struct extent_buffer *leaf;
890 leaf = path->nodes[0];
891 slot = path->slots[0];
892 /* Not deleting the first item of a leaf to keep leaf structure */
895 /* Only accept valid eb */
896 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
897 btrfs_item_key_to_cpu(leaf, &key, slot);
899 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
900 key.objectid, key.type, key.offset);
901 btrfs_del_item(trans, root, path);
903 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
904 key.objectid, key.type, key.offset);
905 ptr = btrfs_item_ptr_offset(leaf, slot);
906 item_size = btrfs_item_size_nr(leaf, slot);
907 memset_extent_buffer(leaf, 0, ptr, item_size);
908 btrfs_mark_buffer_dirty(leaf);
912 static int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
913 struct btrfs_root *root)
918 struct btrfs_path *path;
919 struct btrfs_key key;
920 struct btrfs_key found_key;
921 struct extent_buffer *leaf;
923 path = btrfs_alloc_path();
927 key.objectid = (u64)-1;
928 key.offset = (u64)-1;
931 /* Here, cow and ins_len must equals 0 for the following reasons:
932 * 1) chunk recover is based on disk scanning, so COW should be
933 * disabled in case the original chunk being scanned and
934 * recovered using the old chunk.
935 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
938 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
941 fprintf(stderr, "Error searching tree\n");
944 /* corrupt/del dev_item first */
945 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
946 slot = path->slots[0];
947 leaf = path->nodes[0];
949 /* Never delete the first item to keep the leaf structure */
950 if (path->slots[0] == 0)
952 ret = corrupt_item_nocow(trans, root, path, del);
956 btrfs_release_path(path);
958 /* Here, cow and ins_len must equals 0 for the following reasons:
959 * 1) chunk recover is based on disk scanning, so COW should be
960 * disabled in case the original chunk being scanned and
961 * recovered using the old chunk.
962 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
965 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
968 fprintf(stderr, "Error searching tree\n");
971 /* corrupt/del chunk then*/
972 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
973 slot = path->slots[0];
974 leaf = path->nodes[0];
976 btrfs_item_key_to_cpu(leaf, &found_key, slot);
977 ret = corrupt_item_nocow(trans, root, path, del);
982 btrfs_free_path(path);
985 static int find_chunk_offset(struct btrfs_root *root,
986 struct btrfs_path *path, u64 offset)
988 struct btrfs_key key;
991 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
992 key.type = BTRFS_CHUNK_ITEM_KEY;
995 /* Here, cow and ins_len must equals 0 for following reasons:
996 * 1) chunk recover is based on disk scanning, so COW should
997 * be disabled in case the original chunk being scanned
998 * and recovered using the old chunk.
999 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
1000 * will be triggered.
1002 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1004 fprintf(stderr, "Can't find chunk with given offset %llu\n",
1009 fprintf(stderr, "Error searching chunk\n");
1016 int main(int argc, char **argv)
1018 struct cache_tree root_cache;
1019 struct btrfs_key key;
1020 struct btrfs_root *root;
1021 struct extent_buffer *eb;
1023 /* chunk offset can be 0,so change to (u64)-1 */
1024 u64 logical = (u64)-1;
1029 int extent_tree = 0;
1030 int corrupt_block_keys = 0;
1033 int corrupt_item = 0;
1036 u64 metadata_block = 0;
1038 u64 file_extent = (u64)-1;
1039 u64 root_objectid = 0;
1040 u64 csum_bytenr = 0;
1041 char field[FIELD_BUF_LEN];
1044 memset(&key, 0, sizeof(key));
1048 static const struct option long_options[] = {
1049 /* { "byte-count", 1, NULL, 'b' }, */
1050 { "logical", required_argument, NULL, 'l' },
1051 { "copy", required_argument, NULL, 'c' },
1052 { "bytes", required_argument, NULL, 'b' },
1053 { "extent-record", no_argument, NULL, 'e' },
1054 { "extent-tree", no_argument, NULL, 'E' },
1055 { "keys", no_argument, NULL, 'k' },
1056 { "chunk-record", no_argument, NULL, 'u' },
1057 { "chunk-tree", no_argument, NULL, 'U' },
1058 { "inode", required_argument, NULL, 'i'},
1059 { "file-extent", required_argument, NULL, 'x'},
1060 { "metadata-block", required_argument, NULL, 'm'},
1061 { "field", required_argument, NULL, 'f'},
1062 { "key", required_argument, NULL, 'K'},
1063 { "item", no_argument, NULL, 'I'},
1064 { "dir-item", no_argument, NULL, 'D'},
1065 { "delete", no_argument, NULL, 'd'},
1066 { "root", no_argument, NULL, 'r'},
1067 { "csum", required_argument, NULL, 'C'},
1068 { "help", no_argument, NULL, GETOPT_VAL_HELP},
1069 { NULL, 0, NULL, 0 }
1072 c = getopt_long(argc, argv, "l:c:b:eEkuUi:f:x:m:K:IDdr:C:",
1073 long_options, NULL);
1078 logical = arg_strtou64(optarg);
1081 copy = arg_strtou64(optarg);
1084 bytes = arg_strtou64(optarg);
1093 corrupt_block_keys = 1;
1102 inode = arg_strtou64(optarg);
1105 strncpy(field, optarg, FIELD_BUF_LEN);
1108 file_extent = arg_strtou64(optarg);
1111 metadata_block = arg_strtou64(optarg);
1114 ret = sscanf(optarg, "%llu,%u,%llu",
1116 (unsigned int *)&key.type,
1119 fprintf(stderr, "error reading key "
1134 root_objectid = arg_strtou64(optarg);
1137 csum_bytenr = arg_strtou64(optarg);
1139 case GETOPT_VAL_HELP:
1141 print_usage(c != GETOPT_VAL_HELP);
1145 if (check_argc_min(argc - optind, 1))
1150 cache_tree_init(&root_cache);
1152 root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
1154 fprintf(stderr, "Open ctree failed\n");
1158 struct btrfs_trans_handle *trans;
1160 if (logical == (u64)-1)
1162 trans = btrfs_start_transaction(root, 1);
1163 ret = corrupt_extent (trans, root, logical, 0);
1164 btrfs_commit_transaction(trans, root);
1168 struct btrfs_trans_handle *trans;
1169 trans = btrfs_start_transaction(root, 1);
1170 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
1171 root->fs_info->extent_root->node);
1172 btrfs_commit_transaction(trans, root);
1176 struct btrfs_trans_handle *trans;
1177 struct btrfs_path *path;
1180 if (logical == (u64)-1)
1182 del = rand_range(3);
1183 path = btrfs_alloc_path();
1185 fprintf(stderr, "path allocation failed\n");
1189 if (find_chunk_offset(root->fs_info->chunk_root, path,
1191 btrfs_free_path(path);
1194 trans = btrfs_start_transaction(root, 1);
1195 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
1198 fprintf(stderr, "Failed to corrupt chunk record\n");
1199 btrfs_commit_transaction(trans, root);
1203 struct btrfs_trans_handle *trans;
1204 trans = btrfs_start_transaction(root, 1);
1205 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
1207 fprintf(stderr, "Failed to corrupt chunk tree\n");
1208 btrfs_commit_transaction(trans, root);
1212 struct btrfs_trans_handle *trans;
1217 trans = btrfs_start_transaction(root, 1);
1218 if (file_extent == (u64)-1) {
1219 printf("corrupting inode\n");
1220 ret = corrupt_inode(trans, root, inode, field);
1222 printf("corrupting file extent\n");
1223 ret = corrupt_file_extent(trans, root, inode,
1224 file_extent, field);
1226 btrfs_commit_transaction(trans, root);
1229 if (metadata_block) {
1232 ret = corrupt_metadata_block(root, metadata_block, field);
1236 if (!key.objectid || *field == 0)
1238 ret = corrupt_dir_item(root, &key, field);
1242 ret = delete_csum(root, csum_bytenr, bytes);
1248 ret = corrupt_btrfs_item(root, &key, field);
1251 struct btrfs_root *target = root;
1255 if (root_objectid) {
1256 struct btrfs_key root_key;
1258 root_key.objectid = root_objectid;
1259 root_key.type = BTRFS_ROOT_ITEM_KEY;
1260 root_key.offset = (u64)-1;
1262 target = btrfs_read_fs_root(root->fs_info, &root_key);
1263 if (IS_ERR(target)) {
1264 fprintf(stderr, "Couldn't find root %llu\n",
1265 (unsigned long long)root_objectid);
1269 ret = delete_item(target, &key);
1272 if (key.objectid || key.offset || key.type) {
1275 ret = corrupt_key(root, &key, field);
1279 * If we made it here and we have extent set then we didn't specify
1280 * inode and we're screwed.
1282 if (file_extent != (u64)-1)
1285 if (logical == (u64)-1)
1289 bytes = root->sectorsize;
1291 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
1292 bytes *= root->sectorsize;
1295 if (corrupt_block_keys) {
1296 corrupt_keys_in_block(root, logical);
1298 eb = debug_corrupt_block(root, logical,
1299 root->sectorsize, copy);
1300 free_extent_buffer(eb);
1302 logical += root->sectorsize;
1303 bytes -= root->sectorsize;