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);
135 bad_slot = rand() % 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->leafsize, 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() % 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_TREE_BLOCK_REF_KEY &&
214 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
215 key.type != BTRFS_EXTENT_REF_V0_KEY &&
216 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
217 key.type != BTRFS_SHARED_DATA_REF_KEY)
222 "deleting extent record: key %llu %u %llu\n",
223 key.objectid, key.type, key.offset);
225 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
226 /* make sure this extent doesn't get
227 * reused for other purposes */
228 btrfs_pin_extent(root->fs_info,
229 key.objectid, key.offset);
232 btrfs_del_item(trans, root, path);
235 "corrupting extent record: key %llu %u %llu\n",
236 key.objectid, key.type, key.offset);
237 ptr = btrfs_item_ptr_offset(leaf, slot);
238 item_size = btrfs_item_size_nr(leaf, slot);
239 memset_extent_buffer(leaf, 0, ptr, item_size);
240 btrfs_mark_buffer_dirty(leaf);
243 btrfs_release_path(path);
251 btrfs_free_path(path);
255 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
256 struct btrfs_root *root,
257 struct extent_buffer *eb)
259 u32 nr = btrfs_header_nritems(eb);
260 u32 victim = rand() % nr;
262 struct btrfs_key key;
264 btrfs_item_key_to_cpu(eb, &key, victim);
265 objectid = key.objectid;
266 corrupt_extent(trans, root, objectid, 1);
269 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
270 struct btrfs_root *root,
271 struct extent_buffer *eb)
278 if (btrfs_is_leaf(eb)) {
279 btrfs_corrupt_extent_leaf(trans, root, eb);
283 if (btrfs_header_level(eb) == 1 && eb != root->node) {
288 for (i = 0; i < btrfs_header_nritems(eb); i++) {
289 struct extent_buffer *next;
291 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
293 btrfs_node_ptr_generation(eb, i));
294 if (!extent_buffer_uptodate(next))
296 btrfs_corrupt_extent_tree(trans, root, next);
297 free_extent_buffer(next);
301 enum btrfs_inode_field {
302 BTRFS_INODE_FIELD_ISIZE,
303 BTRFS_INODE_FIELD_NBYTES,
304 BTRFS_INODE_FIELD_BAD,
307 enum btrfs_file_extent_field {
308 BTRFS_FILE_EXTENT_DISK_BYTENR,
309 BTRFS_FILE_EXTENT_BAD,
312 enum btrfs_dir_item_field {
314 BTRFS_DIR_ITEM_LOCATION_OBJECTID,
318 enum btrfs_metadata_block_field {
319 BTRFS_METADATA_BLOCK_GENERATION,
320 BTRFS_METADATA_BLOCK_SHIFT_ITEMS,
321 BTRFS_METADATA_BLOCK_BAD,
324 enum btrfs_item_field {
329 enum btrfs_key_field {
336 static enum btrfs_inode_field convert_inode_field(char *field)
338 if (!strncmp(field, "isize", FIELD_BUF_LEN))
339 return BTRFS_INODE_FIELD_ISIZE;
340 if (!strncmp(field, "nbytes", FIELD_BUF_LEN))
341 return BTRFS_INODE_FIELD_NBYTES;
342 return BTRFS_INODE_FIELD_BAD;
345 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
347 if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
348 return BTRFS_FILE_EXTENT_DISK_BYTENR;
349 return BTRFS_FILE_EXTENT_BAD;
352 static enum btrfs_metadata_block_field
353 convert_metadata_block_field(char *field)
355 if (!strncmp(field, "generation", FIELD_BUF_LEN))
356 return BTRFS_METADATA_BLOCK_GENERATION;
357 if (!strncmp(field, "shift_items", FIELD_BUF_LEN))
358 return BTRFS_METADATA_BLOCK_SHIFT_ITEMS;
359 return BTRFS_METADATA_BLOCK_BAD;
362 static enum btrfs_key_field convert_key_field(char *field)
364 if (!strncmp(field, "objectid", FIELD_BUF_LEN))
365 return BTRFS_KEY_OBJECTID;
366 if (!strncmp(field, "type", FIELD_BUF_LEN))
367 return BTRFS_KEY_TYPE;
368 if (!strncmp(field, "offset", FIELD_BUF_LEN))
369 return BTRFS_KEY_OFFSET;
370 return BTRFS_KEY_BAD;
373 static enum btrfs_item_field convert_item_field(char *field)
375 if (!strncmp(field, "offset", FIELD_BUF_LEN))
376 return BTRFS_ITEM_OFFSET;
377 return BTRFS_ITEM_BAD;
380 static enum btrfs_dir_item_field convert_dir_item_field(char *field)
382 if (!strncmp(field, "name", FIELD_BUF_LEN))
383 return BTRFS_DIR_ITEM_NAME;
384 if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
385 return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
386 return BTRFS_DIR_ITEM_BAD;
389 static u64 generate_u64(u64 orig)
394 } while (ret == orig);
398 static u32 generate_u32(u32 orig)
403 } while (ret == orig);
407 static u8 generate_u8(u8 orig)
412 } while (ret == orig);
416 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
419 enum btrfs_key_field corrupt_field = convert_key_field(field);
420 struct btrfs_path *path;
421 struct btrfs_trans_handle *trans;
424 root = root->fs_info->fs_root;
425 if (corrupt_field == BTRFS_KEY_BAD) {
426 fprintf(stderr, "Invalid field %s\n", field);
430 path = btrfs_alloc_path();
434 trans = btrfs_start_transaction(root, 1);
436 btrfs_free_path(path);
437 return PTR_ERR(trans);
440 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
444 fprintf(stderr, "Couldn't find the key to corrupt\n");
449 switch (corrupt_field) {
450 case BTRFS_KEY_OBJECTID:
451 key->objectid = generate_u64(key->objectid);
454 key->type = generate_u8(key->type);
456 case BTRFS_KEY_OFFSET:
457 key->offset = generate_u64(key->objectid);
460 fprintf(stderr, "Invalid field %s, %d\n", field,
466 btrfs_set_item_key_unsafe(root, path, key);
468 btrfs_free_path(path);
469 btrfs_commit_transaction(trans, root);
473 static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
476 struct btrfs_trans_handle *trans;
477 struct btrfs_dir_item *di;
478 struct btrfs_path *path;
480 struct btrfs_key location;
481 struct btrfs_disk_key disk_key;
482 unsigned long name_ptr;
483 enum btrfs_dir_item_field corrupt_field =
484 convert_dir_item_field(field);
489 if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
490 fprintf(stderr, "Invalid field %s\n", field);
494 path = btrfs_alloc_path();
498 trans = btrfs_start_transaction(root, 1);
500 btrfs_free_path(path);
501 return PTR_ERR(trans);
504 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
508 fprintf(stderr, "Error searching for dir item %d\n", ret);
512 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
513 struct btrfs_dir_item);
515 switch (corrupt_field) {
516 case BTRFS_DIR_ITEM_NAME:
517 name_len = btrfs_dir_name_len(path->nodes[0], di);
518 name_ptr = (unsigned long)(di + 1);
519 read_extent_buffer(path->nodes[0], name, name_ptr, name_len);
521 write_extent_buffer(path->nodes[0], name, name_ptr, name_len);
522 btrfs_mark_buffer_dirty(path->nodes[0]);
524 case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
525 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
526 bogus = generate_u64(location.objectid);
527 location.objectid = bogus;
528 btrfs_cpu_key_to_disk(&disk_key, &location);
529 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
530 btrfs_mark_buffer_dirty(path->nodes[0]);
537 btrfs_commit_transaction(trans, root);
538 btrfs_free_path(path);
542 static int corrupt_inode(struct btrfs_trans_handle *trans,
543 struct btrfs_root *root, u64 inode, char *field)
545 struct btrfs_inode_item *ei;
546 struct btrfs_path *path;
547 struct btrfs_key key;
548 enum btrfs_inode_field corrupt_field = convert_inode_field(field);
553 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
554 fprintf(stderr, "Invalid field %s\n", field);
558 key.objectid = inode;
559 key.type = BTRFS_INODE_ITEM_KEY;
560 key.offset = (u64)-1;
562 path = btrfs_alloc_path();
566 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
570 if (!path->slots[0]) {
571 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
579 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
580 if (key.objectid != inode) {
581 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
586 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
587 struct btrfs_inode_item);
588 switch (corrupt_field) {
589 case BTRFS_INODE_FIELD_ISIZE:
590 orig = btrfs_inode_size(path->nodes[0], ei);
591 bogus = generate_u64(orig);
592 btrfs_set_inode_size(path->nodes[0], ei, bogus);
594 case BTRFS_INODE_FIELD_NBYTES:
595 orig = btrfs_inode_nbytes(path->nodes[0], ei);
596 bogus = generate_u64(orig);
597 btrfs_set_inode_nbytes(path->nodes[0], ei, bogus);
603 btrfs_mark_buffer_dirty(path->nodes[0]);
605 btrfs_free_path(path);
609 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
610 struct btrfs_root *root, u64 inode, u64 extent,
613 struct btrfs_file_extent_item *fi;
614 struct btrfs_path *path;
615 struct btrfs_key key;
616 enum btrfs_file_extent_field corrupt_field;
621 corrupt_field = convert_file_extent_field(field);
622 if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
623 fprintf(stderr, "Invalid field %s\n", field);
627 key.objectid = inode;
628 key.type = BTRFS_EXTENT_DATA_KEY;
631 path = btrfs_alloc_path();
635 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
639 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
645 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
646 struct btrfs_file_extent_item);
647 switch (corrupt_field) {
648 case BTRFS_FILE_EXTENT_DISK_BYTENR:
649 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
650 bogus = generate_u64(orig);
651 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
657 btrfs_mark_buffer_dirty(path->nodes[0]);
659 btrfs_free_path(path);
663 static void shift_items(struct btrfs_root *root, struct extent_buffer *eb)
665 int nritems = btrfs_header_nritems(eb);
666 int shift_space = btrfs_leaf_free_space(root, eb) / 2;
667 int slot = nritems / 2;
669 unsigned int data_end = btrfs_item_offset_nr(eb, nritems - 1);
671 /* Shift the item data up to and including slot back by shift space */
672 memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end - shift_space,
673 btrfs_leaf_data(eb) + data_end,
674 btrfs_item_offset_nr(eb, slot - 1) - data_end);
676 /* Now update the item pointers. */
677 for (i = nritems - 1; i >= slot; i--) {
678 u32 offset = btrfs_item_offset_nr(eb, i);
679 offset -= shift_space;
680 btrfs_set_item_offset(eb, btrfs_item_nr(i), offset);
684 static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
687 struct btrfs_trans_handle *trans;
688 struct btrfs_path *path;
689 struct extent_buffer *eb;
690 struct btrfs_key key, root_key;
691 enum btrfs_metadata_block_field corrupt_field;
697 corrupt_field = convert_metadata_block_field(field);
698 if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
699 fprintf(stderr, "Invalid field %s\n", field);
703 eb = read_tree_block(root, block, root->leafsize, 0);
704 if (!extent_buffer_uptodate(eb)) {
705 fprintf(stderr, "Couldn't read in tree block %s\n", field);
708 root_objectid = btrfs_header_owner(eb);
709 level = btrfs_header_level(eb);
711 btrfs_node_key_to_cpu(eb, &key, 0);
713 btrfs_item_key_to_cpu(eb, &key, 0);
714 free_extent_buffer(eb);
716 root_key.objectid = root_objectid;
717 root_key.type = BTRFS_ROOT_ITEM_KEY;
718 root_key.offset = (u64)-1;
720 root = btrfs_read_fs_root(root->fs_info, &root_key);
722 fprintf(stderr, "Couldn't finde owner root %llu\n",
724 return PTR_ERR(root);
727 path = btrfs_alloc_path();
731 trans = btrfs_start_transaction(root, 1);
733 btrfs_free_path(path);
734 fprintf(stderr, "Couldn't start transaction %ld\n",
736 return PTR_ERR(trans);
739 path->lowest_level = level;
740 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
742 fprintf(stderr, "Error searching to node %d\n", ret);
745 eb = path->nodes[level];
748 switch (corrupt_field) {
749 case BTRFS_METADATA_BLOCK_GENERATION:
750 orig = btrfs_header_generation(eb);
751 bogus = generate_u64(orig);
752 btrfs_set_header_generation(eb, bogus);
754 case BTRFS_METADATA_BLOCK_SHIFT_ITEMS:
755 shift_items(root, path->nodes[level]);
761 btrfs_mark_buffer_dirty(path->nodes[level]);
763 btrfs_commit_transaction(trans, root);
764 btrfs_free_path(path);
768 static int corrupt_btrfs_item(struct btrfs_root *root, struct btrfs_key *key,
771 struct btrfs_trans_handle *trans;
772 struct btrfs_path *path;
773 enum btrfs_item_field corrupt_field;
777 corrupt_field = convert_item_field(field);
778 if (corrupt_field == BTRFS_ITEM_BAD) {
779 fprintf(stderr, "Invalid field %s\n", field);
783 path = btrfs_alloc_path();
787 trans = btrfs_start_transaction(root, 1);
789 btrfs_free_path(path);
790 fprintf(stderr, "Couldn't start transaction %ld\n",
792 return PTR_ERR(trans);
795 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
797 fprintf(stderr, "Error searching to node %d\n", ret);
802 switch (corrupt_field) {
803 case BTRFS_ITEM_OFFSET:
804 orig = btrfs_item_offset_nr(path->nodes[0], path->slots[0]);
805 bogus = generate_u32(orig);
806 btrfs_set_item_offset(path->nodes[0],
807 btrfs_item_nr(path->slots[0]), bogus);
813 btrfs_mark_buffer_dirty(path->nodes[0]);
815 btrfs_commit_transaction(trans, root);
816 btrfs_free_path(path);
820 static int delete_item(struct btrfs_root *root, struct btrfs_key *key)
822 struct btrfs_trans_handle *trans;
823 struct btrfs_path *path;
826 path = btrfs_alloc_path();
830 trans = btrfs_start_transaction(root, 1);
832 btrfs_free_path(path);
833 fprintf(stderr, "Couldn't start transaction %ld\n",
835 return PTR_ERR(trans);
838 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
842 fprintf(stderr, "Error searching to node %d\n", ret);
845 ret = btrfs_del_item(trans, root, path);
846 btrfs_mark_buffer_dirty(path->nodes[0]);
848 btrfs_commit_transaction(trans, root);
849 btrfs_free_path(path);
853 static int delete_csum(struct btrfs_root *root, u64 bytenr, u64 bytes)
855 struct btrfs_trans_handle *trans;
858 root = root->fs_info->csum_root;
859 trans = btrfs_start_transaction(root, 1);
861 fprintf(stderr, "Couldn't start transaction %ld\n",
863 return PTR_ERR(trans);
866 ret = btrfs_del_csums(trans, root, bytenr, bytes);
868 fprintf(stderr, "Error deleting csums %d\n", ret);
869 btrfs_commit_transaction(trans, root);
873 /* corrupt item using NO cow.
874 * Because chunk recover will recover based on whole partition scaning,
875 * If using COW, chunk recover will use the old item to recover,
876 * which is still OK but we want to check the ability to rebuild chunk
877 * not only restore the old ones */
878 static int corrupt_item_nocow(struct btrfs_trans_handle *trans,
879 struct btrfs_root *root, struct btrfs_path *path,
883 struct btrfs_key key;
884 struct extent_buffer *leaf;
889 leaf = path->nodes[0];
890 slot = path->slots[0];
891 /* Not deleting the first item of a leaf to keep leaf structure */
894 /* Only accept valid eb */
895 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
896 btrfs_item_key_to_cpu(leaf, &key, slot);
898 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
899 key.objectid, key.type, key.offset);
900 btrfs_del_item(trans, root, path);
902 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
903 key.objectid, key.type, key.offset);
904 ptr = btrfs_item_ptr_offset(leaf, slot);
905 item_size = btrfs_item_size_nr(leaf, slot);
906 memset_extent_buffer(leaf, 0, ptr, item_size);
907 btrfs_mark_buffer_dirty(leaf);
911 static int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
912 struct btrfs_root *root)
917 struct btrfs_path *path;
918 struct btrfs_key key;
919 struct btrfs_key found_key;
920 struct extent_buffer *leaf;
922 path = btrfs_alloc_path();
926 key.objectid = (u64)-1;
927 key.offset = (u64)-1;
930 /* Here, cow and ins_len must equals 0 for the following reasons:
931 * 1) chunk recover is based on disk scanning, so COW should be
932 * disabled in case the original chunk being scanned and
933 * recovered using the old chunk.
934 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
937 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
940 fprintf(stderr, "Error searching tree\n");
943 /* corrupt/del dev_item first */
944 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
945 slot = path->slots[0];
946 leaf = path->nodes[0];
948 /* Never delete the first item to keep the leaf structure */
949 if (path->slots[0] == 0)
951 ret = corrupt_item_nocow(trans, root, path, del);
955 btrfs_release_path(path);
957 /* Here, cow and ins_len must equals 0 for the following reasons:
958 * 1) chunk recover is based on disk scanning, so COW should be
959 * disabled in case the original chunk being scanned and
960 * recovered using the old chunk.
961 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
964 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
967 fprintf(stderr, "Error searching tree\n");
970 /* corrupt/del chunk then*/
971 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
972 slot = path->slots[0];
973 leaf = path->nodes[0];
975 btrfs_item_key_to_cpu(leaf, &found_key, slot);
976 ret = corrupt_item_nocow(trans, root, path, del);
981 btrfs_free_path(path);
984 static int find_chunk_offset(struct btrfs_root *root,
985 struct btrfs_path *path, u64 offset)
987 struct btrfs_key key;
990 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
991 key.type = BTRFS_CHUNK_ITEM_KEY;
994 /* Here, cow and ins_len must equals 0 for following reasons:
995 * 1) chunk recover is based on disk scanning, so COW should
996 * be disabled in case the original chunk being scanned
997 * and recovered using the old chunk.
998 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
1001 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1003 fprintf(stderr, "Can't find chunk with given offset %llu\n",
1008 fprintf(stderr, "Error searching chunk\n");
1015 int main(int ac, char **av)
1017 struct cache_tree root_cache;
1018 struct btrfs_key key;
1019 struct btrfs_root *root;
1020 struct extent_buffer *eb;
1022 /* chunk offset can be 0,so change to (u64)-1 */
1023 u64 logical = (u64)-1;
1028 int extent_tree = 0;
1029 int corrupt_block_keys = 0;
1032 int corrupt_item = 0;
1035 u64 metadata_block = 0;
1037 u64 file_extent = (u64)-1;
1038 u64 root_objectid = 0;
1039 u64 csum_bytenr = 0;
1040 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(ac, av, "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);
1146 if (check_argc_min(ac, 1))
1151 cache_tree_init(&root_cache);
1153 root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
1155 fprintf(stderr, "Open ctree failed\n");
1159 struct btrfs_trans_handle *trans;
1161 if (logical == (u64)-1)
1163 trans = btrfs_start_transaction(root, 1);
1164 ret = corrupt_extent (trans, root, logical, 0);
1165 btrfs_commit_transaction(trans, root);
1169 struct btrfs_trans_handle *trans;
1170 trans = btrfs_start_transaction(root, 1);
1171 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
1172 root->fs_info->extent_root->node);
1173 btrfs_commit_transaction(trans, root);
1177 struct btrfs_trans_handle *trans;
1178 struct btrfs_path *path;
1181 if (logical == (u64)-1)
1184 path = btrfs_alloc_path();
1186 fprintf(stderr, "path allocation failed\n");
1190 if (find_chunk_offset(root->fs_info->chunk_root, path,
1192 btrfs_free_path(path);
1195 trans = btrfs_start_transaction(root, 1);
1196 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
1199 fprintf(stderr, "Failed to corrupt chunk record\n");
1200 btrfs_commit_transaction(trans, root);
1204 struct btrfs_trans_handle *trans;
1205 trans = btrfs_start_transaction(root, 1);
1206 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
1208 fprintf(stderr, "Failed to corrupt chunk tree\n");
1209 btrfs_commit_transaction(trans, root);
1213 struct btrfs_trans_handle *trans;
1218 trans = btrfs_start_transaction(root, 1);
1219 if (file_extent == (u64)-1) {
1220 printf("corrupting inode\n");
1221 ret = corrupt_inode(trans, root, inode, field);
1223 printf("corrupting file extent\n");
1224 ret = corrupt_file_extent(trans, root, inode,
1225 file_extent, field);
1227 btrfs_commit_transaction(trans, root);
1230 if (metadata_block) {
1233 ret = corrupt_metadata_block(root, metadata_block, field);
1237 if (!key.objectid || *field == 0)
1239 ret = corrupt_dir_item(root, &key, field);
1243 ret = delete_csum(root, csum_bytenr, bytes);
1249 ret = corrupt_btrfs_item(root, &key, field);
1252 struct btrfs_root *target = root;
1256 if (root_objectid) {
1257 struct btrfs_key root_key;
1259 root_key.objectid = root_objectid;
1260 root_key.type = BTRFS_ROOT_ITEM_KEY;
1261 root_key.offset = (u64)-1;
1263 target = btrfs_read_fs_root(root->fs_info, &root_key);
1264 if (IS_ERR(target)) {
1265 fprintf(stderr, "Couldn't find root %llu\n",
1266 (unsigned long long)root_objectid);
1270 ret = delete_item(target, &key);
1273 if (key.objectid || key.offset || key.type) {
1276 ret = corrupt_key(root, &key, field);
1280 * If we made it here and we have extent set then we didn't specify
1281 * inode and we're screwed.
1283 if (file_extent != (u64)-1)
1286 if (logical == (u64)-1)
1290 bytes = root->sectorsize;
1292 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
1293 bytes *= root->sectorsize;
1296 if (corrupt_block_keys) {
1297 corrupt_keys_in_block(root, logical);
1299 eb = debug_corrupt_block(root, logical,
1300 root->sectorsize, copy);
1301 free_extent_buffer(eb);
1303 logical += root->sectorsize;
1304 bytes -= root->sectorsize;