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 static struct extent_buffer *debug_corrupt_block(struct btrfs_root *root,
37 u64 bytenr, 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_NBYTES,
303 BTRFS_INODE_FIELD_BAD,
306 enum btrfs_file_extent_field {
307 BTRFS_FILE_EXTENT_DISK_BYTENR,
308 BTRFS_FILE_EXTENT_BAD,
311 enum btrfs_dir_item_field {
313 BTRFS_DIR_ITEM_LOCATION_OBJECTID,
317 enum btrfs_metadata_block_field {
318 BTRFS_METADATA_BLOCK_GENERATION,
319 BTRFS_METADATA_BLOCK_SHIFT_ITEMS,
320 BTRFS_METADATA_BLOCK_BAD,
323 enum btrfs_item_field {
328 enum btrfs_key_field {
335 static enum btrfs_inode_field convert_inode_field(char *field)
337 if (!strncmp(field, "isize", FIELD_BUF_LEN))
338 return BTRFS_INODE_FIELD_ISIZE;
339 if (!strncmp(field, "nbytes", FIELD_BUF_LEN))
340 return BTRFS_INODE_FIELD_NBYTES;
341 return BTRFS_INODE_FIELD_BAD;
344 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
346 if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
347 return BTRFS_FILE_EXTENT_DISK_BYTENR;
348 return BTRFS_FILE_EXTENT_BAD;
351 static enum btrfs_metadata_block_field
352 convert_metadata_block_field(char *field)
354 if (!strncmp(field, "generation", FIELD_BUF_LEN))
355 return BTRFS_METADATA_BLOCK_GENERATION;
356 if (!strncmp(field, "shift_items", FIELD_BUF_LEN))
357 return BTRFS_METADATA_BLOCK_SHIFT_ITEMS;
358 return BTRFS_METADATA_BLOCK_BAD;
361 static enum btrfs_key_field convert_key_field(char *field)
363 if (!strncmp(field, "objectid", FIELD_BUF_LEN))
364 return BTRFS_KEY_OBJECTID;
365 if (!strncmp(field, "type", FIELD_BUF_LEN))
366 return BTRFS_KEY_TYPE;
367 if (!strncmp(field, "offset", FIELD_BUF_LEN))
368 return BTRFS_KEY_OFFSET;
369 return BTRFS_KEY_BAD;
372 static enum btrfs_item_field convert_item_field(char *field)
374 if (!strncmp(field, "offset", FIELD_BUF_LEN))
375 return BTRFS_ITEM_OFFSET;
376 return BTRFS_ITEM_BAD;
379 static enum btrfs_dir_item_field convert_dir_item_field(char *field)
381 if (!strncmp(field, "name", FIELD_BUF_LEN))
382 return BTRFS_DIR_ITEM_NAME;
383 if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
384 return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
385 return BTRFS_DIR_ITEM_BAD;
388 static u64 generate_u64(u64 orig)
393 } while (ret == orig);
397 static u32 generate_u32(u32 orig)
402 } while (ret == orig);
406 static u8 generate_u8(u8 orig)
411 } while (ret == orig);
415 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
418 enum btrfs_key_field corrupt_field = convert_key_field(field);
419 struct btrfs_path *path;
420 struct btrfs_trans_handle *trans;
423 root = root->fs_info->fs_root;
424 if (corrupt_field == BTRFS_KEY_BAD) {
425 fprintf(stderr, "Invalid field %s\n", field);
429 path = btrfs_alloc_path();
433 trans = btrfs_start_transaction(root, 1);
435 btrfs_free_path(path);
436 return PTR_ERR(trans);
439 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
443 fprintf(stderr, "Couldn't find the key to corrupt\n");
448 switch (corrupt_field) {
449 case BTRFS_KEY_OBJECTID:
450 key->objectid = generate_u64(key->objectid);
453 key->type = generate_u8(key->type);
455 case BTRFS_KEY_OFFSET:
456 key->offset = generate_u64(key->objectid);
459 fprintf(stderr, "Invalid field %s, %d\n", field,
465 btrfs_set_item_key_unsafe(root, path, key);
467 btrfs_free_path(path);
468 btrfs_commit_transaction(trans, root);
472 static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
475 struct btrfs_trans_handle *trans;
476 struct btrfs_dir_item *di;
477 struct btrfs_path *path;
479 struct btrfs_key location;
480 struct btrfs_disk_key disk_key;
481 unsigned long name_ptr;
482 enum btrfs_dir_item_field corrupt_field =
483 convert_dir_item_field(field);
488 if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
489 fprintf(stderr, "Invalid field %s\n", field);
493 path = btrfs_alloc_path();
497 trans = btrfs_start_transaction(root, 1);
499 btrfs_free_path(path);
500 return PTR_ERR(trans);
503 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
507 fprintf(stderr, "Error searching for dir item %d\n", ret);
511 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
512 struct btrfs_dir_item);
514 switch (corrupt_field) {
515 case BTRFS_DIR_ITEM_NAME:
516 name_len = btrfs_dir_name_len(path->nodes[0], di);
517 name_ptr = (unsigned long)(di + 1);
518 read_extent_buffer(path->nodes[0], name, name_ptr, name_len);
520 write_extent_buffer(path->nodes[0], name, name_ptr, name_len);
521 btrfs_mark_buffer_dirty(path->nodes[0]);
523 case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
524 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
525 bogus = generate_u64(location.objectid);
526 location.objectid = bogus;
527 btrfs_cpu_key_to_disk(&disk_key, &location);
528 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
529 btrfs_mark_buffer_dirty(path->nodes[0]);
536 btrfs_commit_transaction(trans, root);
537 btrfs_free_path(path);
541 static int corrupt_inode(struct btrfs_trans_handle *trans,
542 struct btrfs_root *root, u64 inode, char *field)
544 struct btrfs_inode_item *ei;
545 struct btrfs_path *path;
546 struct btrfs_key key;
547 enum btrfs_inode_field corrupt_field = convert_inode_field(field);
552 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
553 fprintf(stderr, "Invalid field %s\n", field);
557 key.objectid = inode;
558 key.type = BTRFS_INODE_ITEM_KEY;
559 key.offset = (u64)-1;
561 path = btrfs_alloc_path();
565 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
569 if (!path->slots[0]) {
570 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
578 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
579 if (key.objectid != inode) {
580 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
585 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
586 struct btrfs_inode_item);
587 switch (corrupt_field) {
588 case BTRFS_INODE_FIELD_ISIZE:
589 orig = btrfs_inode_size(path->nodes[0], ei);
590 bogus = generate_u64(orig);
591 btrfs_set_inode_size(path->nodes[0], ei, bogus);
593 case BTRFS_INODE_FIELD_NBYTES:
594 orig = btrfs_inode_nbytes(path->nodes[0], ei);
595 bogus = generate_u64(orig);
596 btrfs_set_inode_nbytes(path->nodes[0], ei, bogus);
602 btrfs_mark_buffer_dirty(path->nodes[0]);
604 btrfs_free_path(path);
608 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
609 struct btrfs_root *root, u64 inode, u64 extent,
612 struct btrfs_file_extent_item *fi;
613 struct btrfs_path *path;
614 struct btrfs_key key;
615 enum btrfs_file_extent_field corrupt_field;
620 corrupt_field = convert_file_extent_field(field);
621 if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
622 fprintf(stderr, "Invalid field %s\n", field);
626 key.objectid = inode;
627 key.type = BTRFS_EXTENT_DATA_KEY;
630 path = btrfs_alloc_path();
634 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
638 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
644 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
645 struct btrfs_file_extent_item);
646 switch (corrupt_field) {
647 case BTRFS_FILE_EXTENT_DISK_BYTENR:
648 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
649 bogus = generate_u64(orig);
650 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
656 btrfs_mark_buffer_dirty(path->nodes[0]);
658 btrfs_free_path(path);
662 static void shift_items(struct btrfs_root *root, struct extent_buffer *eb)
664 int nritems = btrfs_header_nritems(eb);
665 int shift_space = btrfs_leaf_free_space(root, eb) / 2;
666 int slot = nritems / 2;
668 unsigned int data_end = btrfs_item_offset_nr(eb, nritems - 1);
670 /* Shift the item data up to and including slot back by shift space */
671 memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end - shift_space,
672 btrfs_leaf_data(eb) + data_end,
673 btrfs_item_offset_nr(eb, slot - 1) - data_end);
675 /* Now update the item pointers. */
676 for (i = nritems - 1; i >= slot; i--) {
677 u32 offset = btrfs_item_offset_nr(eb, i);
678 offset -= shift_space;
679 btrfs_set_item_offset(eb, btrfs_item_nr(i), offset);
683 static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
686 struct btrfs_trans_handle *trans;
687 struct btrfs_path *path;
688 struct extent_buffer *eb;
689 struct btrfs_key key, root_key;
690 enum btrfs_metadata_block_field corrupt_field;
696 corrupt_field = convert_metadata_block_field(field);
697 if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
698 fprintf(stderr, "Invalid field %s\n", field);
702 eb = read_tree_block(root, block, root->leafsize, 0);
703 if (!extent_buffer_uptodate(eb)) {
704 fprintf(stderr, "Couldn't read in tree block %s\n", field);
707 root_objectid = btrfs_header_owner(eb);
708 level = btrfs_header_level(eb);
710 btrfs_node_key_to_cpu(eb, &key, 0);
712 btrfs_item_key_to_cpu(eb, &key, 0);
713 free_extent_buffer(eb);
715 root_key.objectid = root_objectid;
716 root_key.type = BTRFS_ROOT_ITEM_KEY;
717 root_key.offset = (u64)-1;
719 root = btrfs_read_fs_root(root->fs_info, &root_key);
721 fprintf(stderr, "Couldn't finde owner root %llu\n",
723 return PTR_ERR(root);
726 path = btrfs_alloc_path();
730 trans = btrfs_start_transaction(root, 1);
732 btrfs_free_path(path);
733 fprintf(stderr, "Couldn't start transaction %ld\n",
735 return PTR_ERR(trans);
738 path->lowest_level = level;
739 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
741 fprintf(stderr, "Error searching to node %d\n", ret);
744 eb = path->nodes[level];
747 switch (corrupt_field) {
748 case BTRFS_METADATA_BLOCK_GENERATION:
749 orig = btrfs_header_generation(eb);
750 bogus = generate_u64(orig);
751 btrfs_set_header_generation(eb, bogus);
753 case BTRFS_METADATA_BLOCK_SHIFT_ITEMS:
754 shift_items(root, path->nodes[level]);
760 btrfs_mark_buffer_dirty(path->nodes[level]);
762 btrfs_commit_transaction(trans, root);
763 btrfs_free_path(path);
767 static int corrupt_btrfs_item(struct btrfs_root *root, struct btrfs_key *key,
770 struct btrfs_trans_handle *trans;
771 struct btrfs_path *path;
772 enum btrfs_item_field corrupt_field;
776 corrupt_field = convert_item_field(field);
777 if (corrupt_field == BTRFS_ITEM_BAD) {
778 fprintf(stderr, "Invalid field %s\n", field);
782 path = btrfs_alloc_path();
786 trans = btrfs_start_transaction(root, 1);
788 btrfs_free_path(path);
789 fprintf(stderr, "Couldn't start transaction %ld\n",
791 return PTR_ERR(trans);
794 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
796 fprintf(stderr, "Error searching to node %d\n", ret);
801 switch (corrupt_field) {
802 case BTRFS_ITEM_OFFSET:
803 orig = btrfs_item_offset_nr(path->nodes[0], path->slots[0]);
804 bogus = generate_u32(orig);
805 btrfs_set_item_offset(path->nodes[0],
806 btrfs_item_nr(path->slots[0]), bogus);
812 btrfs_mark_buffer_dirty(path->nodes[0]);
814 btrfs_commit_transaction(trans, root);
815 btrfs_free_path(path);
819 static int delete_item(struct btrfs_root *root, struct btrfs_key *key)
821 struct btrfs_trans_handle *trans;
822 struct btrfs_path *path;
825 path = btrfs_alloc_path();
829 trans = btrfs_start_transaction(root, 1);
831 btrfs_free_path(path);
832 fprintf(stderr, "Couldn't start transaction %ld\n",
834 return PTR_ERR(trans);
837 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
841 fprintf(stderr, "Error searching to node %d\n", ret);
844 ret = btrfs_del_item(trans, root, path);
845 btrfs_mark_buffer_dirty(path->nodes[0]);
847 btrfs_commit_transaction(trans, root);
848 btrfs_free_path(path);
852 static int delete_csum(struct btrfs_root *root, u64 bytenr, u64 bytes)
854 struct btrfs_trans_handle *trans;
857 root = root->fs_info->csum_root;
858 trans = btrfs_start_transaction(root, 1);
860 fprintf(stderr, "Couldn't start transaction %ld\n",
862 return PTR_ERR(trans);
865 ret = btrfs_del_csums(trans, root, bytenr, bytes);
867 fprintf(stderr, "Error deleting csums %d\n", ret);
868 btrfs_commit_transaction(trans, root);
872 /* corrupt item using NO cow.
873 * Because chunk recover will recover based on whole partition scaning,
874 * If using COW, chunk recover will use the old item to recover,
875 * which is still OK but we want to check the ability to rebuild chunk
876 * not only restore the old ones */
877 static int corrupt_item_nocow(struct btrfs_trans_handle *trans,
878 struct btrfs_root *root, struct btrfs_path *path,
882 struct btrfs_key key;
883 struct extent_buffer *leaf;
888 leaf = path->nodes[0];
889 slot = path->slots[0];
890 /* Not deleting the first item of a leaf to keep leaf structure */
893 /* Only accept valid eb */
894 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
895 btrfs_item_key_to_cpu(leaf, &key, slot);
897 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
898 key.objectid, key.type, key.offset);
899 btrfs_del_item(trans, root, path);
901 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
902 key.objectid, key.type, key.offset);
903 ptr = btrfs_item_ptr_offset(leaf, slot);
904 item_size = btrfs_item_size_nr(leaf, slot);
905 memset_extent_buffer(leaf, 0, ptr, item_size);
906 btrfs_mark_buffer_dirty(leaf);
910 static int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
911 struct btrfs_root *root)
916 struct btrfs_path *path;
917 struct btrfs_key key;
918 struct btrfs_key found_key;
919 struct extent_buffer *leaf;
921 path = btrfs_alloc_path();
925 key.objectid = (u64)-1;
926 key.offset = (u64)-1;
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 dev_item first */
943 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
944 slot = path->slots[0];
945 leaf = path->nodes[0];
947 /* Never delete the first item to keep the leaf structure */
948 if (path->slots[0] == 0)
950 ret = corrupt_item_nocow(trans, root, path, del);
954 btrfs_release_path(path);
956 /* Here, cow and ins_len must equals 0 for the following reasons:
957 * 1) chunk recover is based on disk scanning, so COW should be
958 * disabled in case the original chunk being scanned and
959 * recovered using the old chunk.
960 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
963 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
966 fprintf(stderr, "Error searching tree\n");
969 /* corrupt/del chunk then*/
970 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
971 slot = path->slots[0];
972 leaf = path->nodes[0];
974 btrfs_item_key_to_cpu(leaf, &found_key, slot);
975 ret = corrupt_item_nocow(trans, root, path, del);
980 btrfs_free_path(path);
983 static int find_chunk_offset(struct btrfs_root *root,
984 struct btrfs_path *path, u64 offset)
986 struct btrfs_key key;
989 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
990 key.type = BTRFS_CHUNK_ITEM_KEY;
993 /* Here, cow and ins_len must equals 0 for following reasons:
994 * 1) chunk recover is based on disk scanning, so COW should
995 * be disabled in case the original chunk being scanned
996 * and recovered using the old chunk.
997 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
1000 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1002 fprintf(stderr, "Can't find chunk with given offset %llu\n",
1007 fprintf(stderr, "Error searching chunk\n");
1014 int main(int ac, char **av)
1016 struct cache_tree root_cache;
1017 struct btrfs_key key;
1018 struct btrfs_root *root;
1019 struct extent_buffer *eb;
1021 /* chunk offset can be 0,so change to (u64)-1 */
1022 u64 logical = (u64)-1;
1027 int extent_tree = 0;
1028 int corrupt_block_keys = 0;
1031 int corrupt_item = 0;
1034 u64 metadata_block = 0;
1036 u64 file_extent = (u64)-1;
1037 u64 root_objectid = 0;
1038 u64 csum_bytenr = 0;
1039 char field[FIELD_BUF_LEN];
1043 memset(&key, 0, sizeof(key));
1047 static const struct option long_options[] = {
1048 /* { "byte-count", 1, NULL, 'b' }, */
1049 { "logical", required_argument, NULL, 'l' },
1050 { "copy", required_argument, NULL, 'c' },
1051 { "bytes", required_argument, NULL, 'b' },
1052 { "extent-record", no_argument, NULL, 'e' },
1053 { "extent-tree", no_argument, NULL, 'E' },
1054 { "keys", no_argument, NULL, 'k' },
1055 { "chunk-record", no_argument, NULL, 'u' },
1056 { "chunk-tree", no_argument, NULL, 'U' },
1057 { "inode", required_argument, NULL, 'i'},
1058 { "file-extent", required_argument, NULL, 'x'},
1059 { "metadata-block", required_argument, NULL, 'm'},
1060 { "field", required_argument, NULL, 'f'},
1061 { "key", required_argument, NULL, 'K'},
1062 { "item", no_argument, NULL, 'I'},
1063 { "dir-item", no_argument, NULL, 'D'},
1064 { "delete", no_argument, NULL, 'd'},
1065 { "root", no_argument, NULL, 'r'},
1066 { "csum", required_argument, NULL, 'C'},
1067 { "help", no_argument, NULL, GETOPT_VAL_HELP},
1068 { NULL, 0, NULL, 0 }
1071 c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:IDdr:C:",
1072 long_options, NULL);
1077 logical = arg_strtou64(optarg);
1080 copy = arg_strtou64(optarg);
1083 bytes = arg_strtou64(optarg);
1092 corrupt_block_keys = 1;
1101 inode = arg_strtou64(optarg);
1104 strncpy(field, optarg, FIELD_BUF_LEN);
1107 file_extent = arg_strtou64(optarg);
1110 metadata_block = arg_strtou64(optarg);
1113 ret = sscanf(optarg, "%llu,%u,%llu",
1115 (unsigned int *)&key.type,
1118 fprintf(stderr, "error reading key "
1133 root_objectid = arg_strtou64(optarg);
1136 csum_bytenr = arg_strtou64(optarg);
1138 case GETOPT_VAL_HELP:
1140 print_usage(c != GETOPT_VAL_HELP);
1145 if (check_argc_min(ac, 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)
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;