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 int debug_corrupt_block(struct extent_buffer *eb,
38 struct btrfs_root *root, u64 bytenr, u32 blocksize, u64 copy)
42 struct btrfs_multi_bio *multi = NULL;
43 struct btrfs_device *device;
49 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
50 eb->start, &length, &multi,
53 error("cannot map block %llu length %llu mirror %d: %d",
54 (unsigned long long)eb->start,
55 (unsigned long long)length,
59 device = multi->stripes[0].dev;
62 eb->dev_bytenr = multi->stripes[0].physical;
65 "mirror %d logical %llu physical %llu device %s\n",
66 mirror_num, (unsigned long long)bytenr,
67 (unsigned long long)eb->dev_bytenr, device->name);
70 if (!copy || mirror_num == copy) {
71 ret = read_extent_from_disk(eb, 0, eb->len);
73 error("cannot read eb bytenr %llu: %s",
74 (unsigned long long)eb->dev_bytenr,
78 printf("corrupting %llu copy %d\n", eb->start,
80 memset(eb->data, 0, eb->len);
81 ret = write_extent_to_disk(eb);
83 error("cannot write eb bytenr %llu: %s",
84 (unsigned long long)eb->dev_bytenr,
91 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
97 if (mirror_num > num_copies)
104 static void print_usage(int ret)
106 fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n");
107 fprintf(stderr, "\t-l Logical extent to be corrupted\n");
108 fprintf(stderr, "\t-c Copy of the extent to be corrupted"
109 " (usually 1 or 2, default: 0)\n");
110 fprintf(stderr, "\t-b Number of bytes to be corrupted\n");
111 fprintf(stderr, "\t-e Extent to be corrupted\n");
112 fprintf(stderr, "\t-E The whole extent tree to be corrupted\n");
113 fprintf(stderr, "\t-u Given chunk item to be corrupted\n");
114 fprintf(stderr, "\t-U The whole chunk tree to be corrupted\n");
115 fprintf(stderr, "\t-i The inode item to corrupt (must also specify "
116 "the field to corrupt)\n");
117 fprintf(stderr, "\t-x The file extent item to corrupt (must also "
118 "specify -i for the inode and -f for the field to corrupt)\n");
119 fprintf(stderr, "\t-m The metadata block to corrupt (must also "
120 "specify -f for the field to corrupt)\n");
121 fprintf(stderr, "\t-K The key to corrupt in the format "
122 "<num>,<num>,<num> (must also specify -f for the field)\n");
123 fprintf(stderr, "\t-f The field in the item to corrupt\n");
124 fprintf(stderr, "\t-I An item to corrupt (must also specify the field "
125 "to corrupt and a root+key for the item)\n");
126 fprintf(stderr, "\t-D Corrupt a dir item, must specify key and field\n");
127 fprintf(stderr, "\t-d Delete this item (must specify -K)\n");
128 fprintf(stderr, "\t-r Operate on this root (only works with -d)\n");
129 fprintf(stderr, "\t-C Delete a csum for the specified bytenr. When "
130 "used with -b it'll delete that many bytes, otherwise it's "
131 "just sectorsize\n");
135 static void corrupt_keys(struct btrfs_trans_handle *trans,
136 struct btrfs_root *root,
137 struct extent_buffer *eb)
142 struct btrfs_disk_key bad_key;;
144 nr = btrfs_header_nritems(eb);
148 slot = rand_range(nr);
149 bad_slot = rand_range(nr);
151 if (bad_slot == slot)
155 "corrupting keys in block %llu slot %d swapping with %d\n",
156 (unsigned long long)eb->start, slot, bad_slot);
158 if (btrfs_header_level(eb) == 0) {
159 btrfs_item_key(eb, &bad_key, bad_slot);
160 btrfs_set_item_key(eb, &bad_key, slot);
162 btrfs_node_key(eb, &bad_key, bad_slot);
163 btrfs_set_node_key(eb, &bad_key, slot);
165 btrfs_mark_buffer_dirty(eb);
168 btrfs_super_csum_size(root->fs_info->super_copy);
169 csum_tree_block_size(eb, csum_size, 0);
170 write_extent_to_disk(eb);
175 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
177 struct extent_buffer *eb;
179 eb = read_tree_block(root, bytenr, root->nodesize, 0);
180 if (!extent_buffer_uptodate(eb))
183 corrupt_keys(NULL, root, eb);
184 free_extent_buffer(eb);
188 static int corrupt_extent(struct btrfs_trans_handle *trans,
189 struct btrfs_root *root, u64 bytenr, u64 copy)
191 struct btrfs_key key;
192 struct extent_buffer *leaf;
195 struct btrfs_path *path;
198 int should_del = rand_range(3);
200 path = btrfs_alloc_path();
204 key.objectid = bytenr;
206 key.offset = (u64)-1;
209 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
215 if (path->slots[0] == 0)
220 leaf = path->nodes[0];
221 slot = path->slots[0];
222 btrfs_item_key_to_cpu(leaf, &key, slot);
223 if (key.objectid != bytenr)
226 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
227 key.type != BTRFS_METADATA_ITEM_KEY &&
228 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
229 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
230 key.type != BTRFS_EXTENT_REF_V0_KEY &&
231 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
232 key.type != BTRFS_SHARED_DATA_REF_KEY)
237 "deleting extent record: key %llu %u %llu\n",
238 key.objectid, key.type, key.offset);
240 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
241 /* make sure this extent doesn't get
242 * reused for other purposes */
243 btrfs_pin_extent(root->fs_info,
244 key.objectid, key.offset);
247 btrfs_del_item(trans, root, path);
250 "corrupting extent record: key %llu %u %llu\n",
251 key.objectid, key.type, key.offset);
252 ptr = btrfs_item_ptr_offset(leaf, slot);
253 item_size = btrfs_item_size_nr(leaf, slot);
254 memset_extent_buffer(leaf, 0, ptr, item_size);
255 btrfs_mark_buffer_dirty(leaf);
258 btrfs_release_path(path);
266 btrfs_free_path(path);
270 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
271 struct btrfs_root *root,
272 struct extent_buffer *eb)
274 u32 nr = btrfs_header_nritems(eb);
275 u32 victim = rand_range(nr);
277 struct btrfs_key key;
279 btrfs_item_key_to_cpu(eb, &key, victim);
280 objectid = key.objectid;
281 corrupt_extent(trans, root, objectid, 1);
284 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
285 struct btrfs_root *root,
286 struct extent_buffer *eb)
293 if (btrfs_is_leaf(eb)) {
294 btrfs_corrupt_extent_leaf(trans, root, eb);
298 if (btrfs_header_level(eb) == 1 && eb != root->node) {
303 for (i = 0; i < btrfs_header_nritems(eb); i++) {
304 struct extent_buffer *next;
306 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
308 btrfs_node_ptr_generation(eb, i));
309 if (!extent_buffer_uptodate(next))
311 btrfs_corrupt_extent_tree(trans, root, next);
312 free_extent_buffer(next);
316 enum btrfs_inode_field {
317 BTRFS_INODE_FIELD_ISIZE,
318 BTRFS_INODE_FIELD_NBYTES,
319 BTRFS_INODE_FIELD_BAD,
322 enum btrfs_file_extent_field {
323 BTRFS_FILE_EXTENT_DISK_BYTENR,
324 BTRFS_FILE_EXTENT_BAD,
327 enum btrfs_dir_item_field {
329 BTRFS_DIR_ITEM_LOCATION_OBJECTID,
333 enum btrfs_metadata_block_field {
334 BTRFS_METADATA_BLOCK_GENERATION,
335 BTRFS_METADATA_BLOCK_SHIFT_ITEMS,
336 BTRFS_METADATA_BLOCK_BAD,
339 enum btrfs_item_field {
344 enum btrfs_key_field {
351 static enum btrfs_inode_field convert_inode_field(char *field)
353 if (!strncmp(field, "isize", FIELD_BUF_LEN))
354 return BTRFS_INODE_FIELD_ISIZE;
355 if (!strncmp(field, "nbytes", FIELD_BUF_LEN))
356 return BTRFS_INODE_FIELD_NBYTES;
357 return BTRFS_INODE_FIELD_BAD;
360 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
362 if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
363 return BTRFS_FILE_EXTENT_DISK_BYTENR;
364 return BTRFS_FILE_EXTENT_BAD;
367 static enum btrfs_metadata_block_field
368 convert_metadata_block_field(char *field)
370 if (!strncmp(field, "generation", FIELD_BUF_LEN))
371 return BTRFS_METADATA_BLOCK_GENERATION;
372 if (!strncmp(field, "shift_items", FIELD_BUF_LEN))
373 return BTRFS_METADATA_BLOCK_SHIFT_ITEMS;
374 return BTRFS_METADATA_BLOCK_BAD;
377 static enum btrfs_key_field convert_key_field(char *field)
379 if (!strncmp(field, "objectid", FIELD_BUF_LEN))
380 return BTRFS_KEY_OBJECTID;
381 if (!strncmp(field, "type", FIELD_BUF_LEN))
382 return BTRFS_KEY_TYPE;
383 if (!strncmp(field, "offset", FIELD_BUF_LEN))
384 return BTRFS_KEY_OFFSET;
385 return BTRFS_KEY_BAD;
388 static enum btrfs_item_field convert_item_field(char *field)
390 if (!strncmp(field, "offset", FIELD_BUF_LEN))
391 return BTRFS_ITEM_OFFSET;
392 return BTRFS_ITEM_BAD;
395 static enum btrfs_dir_item_field convert_dir_item_field(char *field)
397 if (!strncmp(field, "name", FIELD_BUF_LEN))
398 return BTRFS_DIR_ITEM_NAME;
399 if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
400 return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
401 return BTRFS_DIR_ITEM_BAD;
404 static u64 generate_u64(u64 orig)
409 } while (ret == orig);
413 static u32 generate_u32(u32 orig)
418 } while (ret == orig);
422 static u8 generate_u8(u8 orig)
427 } while (ret == orig);
431 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
434 enum btrfs_key_field corrupt_field = convert_key_field(field);
435 struct btrfs_path *path;
436 struct btrfs_trans_handle *trans;
439 root = root->fs_info->fs_root;
440 if (corrupt_field == BTRFS_KEY_BAD) {
441 fprintf(stderr, "Invalid field %s\n", field);
445 path = btrfs_alloc_path();
449 trans = btrfs_start_transaction(root, 1);
451 btrfs_free_path(path);
452 return PTR_ERR(trans);
455 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
459 fprintf(stderr, "Couldn't find the key to corrupt\n");
464 switch (corrupt_field) {
465 case BTRFS_KEY_OBJECTID:
466 key->objectid = generate_u64(key->objectid);
469 key->type = generate_u8(key->type);
471 case BTRFS_KEY_OFFSET:
472 key->offset = generate_u64(key->objectid);
475 fprintf(stderr, "Invalid field %s, %d\n", field,
481 btrfs_set_item_key_unsafe(root, path, key);
483 btrfs_free_path(path);
484 btrfs_commit_transaction(trans, root);
488 static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
491 struct btrfs_trans_handle *trans;
492 struct btrfs_dir_item *di;
493 struct btrfs_path *path;
495 struct btrfs_key location;
496 struct btrfs_disk_key disk_key;
497 unsigned long name_ptr;
498 enum btrfs_dir_item_field corrupt_field =
499 convert_dir_item_field(field);
504 if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
505 fprintf(stderr, "Invalid field %s\n", field);
509 path = btrfs_alloc_path();
513 trans = btrfs_start_transaction(root, 1);
515 btrfs_free_path(path);
516 return PTR_ERR(trans);
519 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
523 fprintf(stderr, "Error searching for dir item %d\n", ret);
527 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
528 struct btrfs_dir_item);
530 switch (corrupt_field) {
531 case BTRFS_DIR_ITEM_NAME:
532 name_len = btrfs_dir_name_len(path->nodes[0], di);
533 name_ptr = (unsigned long)(di + 1);
534 read_extent_buffer(path->nodes[0], name, name_ptr, name_len);
536 write_extent_buffer(path->nodes[0], name, name_ptr, name_len);
537 btrfs_mark_buffer_dirty(path->nodes[0]);
539 case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
540 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
541 bogus = generate_u64(location.objectid);
542 location.objectid = bogus;
543 btrfs_cpu_key_to_disk(&disk_key, &location);
544 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
545 btrfs_mark_buffer_dirty(path->nodes[0]);
552 btrfs_commit_transaction(trans, root);
553 btrfs_free_path(path);
557 static int corrupt_inode(struct btrfs_trans_handle *trans,
558 struct btrfs_root *root, u64 inode, char *field)
560 struct btrfs_inode_item *ei;
561 struct btrfs_path *path;
562 struct btrfs_key key;
563 enum btrfs_inode_field corrupt_field = convert_inode_field(field);
568 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
569 fprintf(stderr, "Invalid field %s\n", field);
573 key.objectid = inode;
574 key.type = BTRFS_INODE_ITEM_KEY;
575 key.offset = (u64)-1;
577 path = btrfs_alloc_path();
581 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
585 if (!path->slots[0]) {
586 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
594 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
595 if (key.objectid != inode) {
596 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
601 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
602 struct btrfs_inode_item);
603 switch (corrupt_field) {
604 case BTRFS_INODE_FIELD_ISIZE:
605 orig = btrfs_inode_size(path->nodes[0], ei);
606 bogus = generate_u64(orig);
607 btrfs_set_inode_size(path->nodes[0], ei, bogus);
609 case BTRFS_INODE_FIELD_NBYTES:
610 orig = btrfs_inode_nbytes(path->nodes[0], ei);
611 bogus = generate_u64(orig);
612 btrfs_set_inode_nbytes(path->nodes[0], ei, bogus);
618 btrfs_mark_buffer_dirty(path->nodes[0]);
620 btrfs_free_path(path);
624 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
625 struct btrfs_root *root, u64 inode, u64 extent,
628 struct btrfs_file_extent_item *fi;
629 struct btrfs_path *path;
630 struct btrfs_key key;
631 enum btrfs_file_extent_field corrupt_field;
636 corrupt_field = convert_file_extent_field(field);
637 if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
638 fprintf(stderr, "Invalid field %s\n", field);
642 key.objectid = inode;
643 key.type = BTRFS_EXTENT_DATA_KEY;
646 path = btrfs_alloc_path();
650 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
654 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
660 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
661 struct btrfs_file_extent_item);
662 switch (corrupt_field) {
663 case BTRFS_FILE_EXTENT_DISK_BYTENR:
664 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
665 bogus = generate_u64(orig);
666 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
672 btrfs_mark_buffer_dirty(path->nodes[0]);
674 btrfs_free_path(path);
678 static void shift_items(struct btrfs_root *root, struct extent_buffer *eb)
680 int nritems = btrfs_header_nritems(eb);
681 int shift_space = btrfs_leaf_free_space(root, eb) / 2;
682 int slot = nritems / 2;
684 unsigned int data_end = btrfs_item_offset_nr(eb, nritems - 1);
686 /* Shift the item data up to and including slot back by shift space */
687 memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end - shift_space,
688 btrfs_leaf_data(eb) + data_end,
689 btrfs_item_offset_nr(eb, slot - 1) - data_end);
691 /* Now update the item pointers. */
692 for (i = nritems - 1; i >= slot; i--) {
693 u32 offset = btrfs_item_offset_nr(eb, i);
694 offset -= shift_space;
695 btrfs_set_item_offset(eb, btrfs_item_nr(i), offset);
699 static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
702 struct btrfs_trans_handle *trans;
703 struct btrfs_path *path;
704 struct extent_buffer *eb;
705 struct btrfs_key key, root_key;
706 enum btrfs_metadata_block_field corrupt_field;
712 corrupt_field = convert_metadata_block_field(field);
713 if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
714 fprintf(stderr, "Invalid field %s\n", field);
718 eb = read_tree_block(root, block, root->nodesize, 0);
719 if (!extent_buffer_uptodate(eb)) {
720 fprintf(stderr, "Couldn't read in tree block %s\n", field);
723 root_objectid = btrfs_header_owner(eb);
724 level = btrfs_header_level(eb);
726 btrfs_node_key_to_cpu(eb, &key, 0);
728 btrfs_item_key_to_cpu(eb, &key, 0);
729 free_extent_buffer(eb);
731 root_key.objectid = root_objectid;
732 root_key.type = BTRFS_ROOT_ITEM_KEY;
733 root_key.offset = (u64)-1;
735 root = btrfs_read_fs_root(root->fs_info, &root_key);
737 fprintf(stderr, "Couldn't find owner root %llu\n",
739 return PTR_ERR(root);
742 path = btrfs_alloc_path();
746 trans = btrfs_start_transaction(root, 1);
748 btrfs_free_path(path);
749 fprintf(stderr, "Couldn't start transaction %ld\n",
751 return PTR_ERR(trans);
754 path->lowest_level = level;
755 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
757 fprintf(stderr, "Error searching to node %d\n", ret);
760 eb = path->nodes[level];
763 switch (corrupt_field) {
764 case BTRFS_METADATA_BLOCK_GENERATION:
765 orig = btrfs_header_generation(eb);
766 bogus = generate_u64(orig);
767 btrfs_set_header_generation(eb, bogus);
769 case BTRFS_METADATA_BLOCK_SHIFT_ITEMS:
770 shift_items(root, path->nodes[level]);
776 btrfs_mark_buffer_dirty(path->nodes[level]);
778 btrfs_commit_transaction(trans, root);
779 btrfs_free_path(path);
783 static int corrupt_btrfs_item(struct btrfs_root *root, struct btrfs_key *key,
786 struct btrfs_trans_handle *trans;
787 struct btrfs_path *path;
788 enum btrfs_item_field corrupt_field;
792 corrupt_field = convert_item_field(field);
793 if (corrupt_field == BTRFS_ITEM_BAD) {
794 fprintf(stderr, "Invalid field %s\n", field);
798 path = btrfs_alloc_path();
802 trans = btrfs_start_transaction(root, 1);
804 btrfs_free_path(path);
805 fprintf(stderr, "Couldn't start transaction %ld\n",
807 return PTR_ERR(trans);
810 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
812 fprintf(stderr, "Error searching to node %d\n", ret);
817 switch (corrupt_field) {
818 case BTRFS_ITEM_OFFSET:
819 orig = btrfs_item_offset_nr(path->nodes[0], path->slots[0]);
820 bogus = generate_u32(orig);
821 btrfs_set_item_offset(path->nodes[0],
822 btrfs_item_nr(path->slots[0]), bogus);
828 btrfs_mark_buffer_dirty(path->nodes[0]);
830 btrfs_commit_transaction(trans, root);
831 btrfs_free_path(path);
835 static int delete_item(struct btrfs_root *root, struct btrfs_key *key)
837 struct btrfs_trans_handle *trans;
838 struct btrfs_path *path;
841 path = btrfs_alloc_path();
845 trans = btrfs_start_transaction(root, 1);
847 btrfs_free_path(path);
848 fprintf(stderr, "Couldn't start transaction %ld\n",
850 return PTR_ERR(trans);
853 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
857 fprintf(stderr, "Error searching to node %d\n", ret);
860 ret = btrfs_del_item(trans, root, path);
861 btrfs_mark_buffer_dirty(path->nodes[0]);
863 btrfs_commit_transaction(trans, root);
864 btrfs_free_path(path);
868 static int delete_csum(struct btrfs_root *root, u64 bytenr, u64 bytes)
870 struct btrfs_trans_handle *trans;
873 root = root->fs_info->csum_root;
874 trans = btrfs_start_transaction(root, 1);
876 fprintf(stderr, "Couldn't start transaction %ld\n",
878 return PTR_ERR(trans);
881 ret = btrfs_del_csums(trans, root, bytenr, bytes);
883 fprintf(stderr, "Error deleting csums %d\n", ret);
884 btrfs_commit_transaction(trans, root);
888 /* corrupt item using NO cow.
889 * Because chunk recover will recover based on whole partition scanning,
890 * If using COW, chunk recover will use the old item to recover,
891 * which is still OK but we want to check the ability to rebuild chunk
892 * not only restore the old ones */
893 static int corrupt_item_nocow(struct btrfs_trans_handle *trans,
894 struct btrfs_root *root, struct btrfs_path *path,
898 struct btrfs_key key;
899 struct extent_buffer *leaf;
904 leaf = path->nodes[0];
905 slot = path->slots[0];
906 /* Not deleting the first item of a leaf to keep leaf structure */
909 /* Only accept valid eb */
910 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
911 btrfs_item_key_to_cpu(leaf, &key, slot);
913 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
914 key.objectid, key.type, key.offset);
915 btrfs_del_item(trans, root, path);
917 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
918 key.objectid, key.type, key.offset);
919 ptr = btrfs_item_ptr_offset(leaf, slot);
920 item_size = btrfs_item_size_nr(leaf, slot);
921 memset_extent_buffer(leaf, 0, ptr, item_size);
922 btrfs_mark_buffer_dirty(leaf);
926 static int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
927 struct btrfs_root *root)
932 struct btrfs_path *path;
933 struct btrfs_key key;
934 struct btrfs_key found_key;
935 struct extent_buffer *leaf;
937 path = btrfs_alloc_path();
941 key.objectid = (u64)-1;
942 key.offset = (u64)-1;
945 /* Here, cow and ins_len must equals 0 for the following reasons:
946 * 1) chunk recover is based on disk scanning, so COW should be
947 * disabled in case the original chunk being scanned and
948 * recovered using the old chunk.
949 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
952 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
955 fprintf(stderr, "Error searching tree\n");
958 /* corrupt/del dev_item first */
959 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
960 slot = path->slots[0];
961 leaf = path->nodes[0];
963 /* Never delete the first item to keep the leaf structure */
964 if (path->slots[0] == 0)
966 ret = corrupt_item_nocow(trans, root, path, del);
970 btrfs_release_path(path);
972 /* Here, cow and ins_len must equals 0 for the following reasons:
973 * 1) chunk recover is based on disk scanning, so COW should be
974 * disabled in case the original chunk being scanned and
975 * recovered using the old chunk.
976 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
979 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
982 fprintf(stderr, "Error searching tree\n");
985 /* corrupt/del chunk then*/
986 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
987 slot = path->slots[0];
988 leaf = path->nodes[0];
990 btrfs_item_key_to_cpu(leaf, &found_key, slot);
991 ret = corrupt_item_nocow(trans, root, path, del);
996 btrfs_free_path(path);
999 static int find_chunk_offset(struct btrfs_root *root,
1000 struct btrfs_path *path, u64 offset)
1002 struct btrfs_key key;
1005 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1006 key.type = BTRFS_CHUNK_ITEM_KEY;
1007 key.offset = offset;
1009 /* Here, cow and ins_len must equals 0 for following reasons:
1010 * 1) chunk recover is based on disk scanning, so COW should
1011 * be disabled in case the original chunk being scanned
1012 * and recovered using the old chunk.
1013 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
1014 * will be triggered.
1016 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1018 fprintf(stderr, "Can't find chunk with given offset %llu\n",
1023 fprintf(stderr, "Error searching chunk\n");
1030 int main(int argc, char **argv)
1032 struct cache_tree root_cache;
1033 struct btrfs_key key;
1034 struct btrfs_root *root;
1036 /* chunk offset can be 0,so change to (u64)-1 */
1037 u64 logical = (u64)-1;
1042 int extent_tree = 0;
1043 int corrupt_block_keys = 0;
1046 int corrupt_item = 0;
1049 u64 metadata_block = 0;
1051 u64 file_extent = (u64)-1;
1052 u64 root_objectid = 0;
1053 u64 csum_bytenr = 0;
1054 char field[FIELD_BUF_LEN];
1057 memset(&key, 0, sizeof(key));
1061 static const struct option long_options[] = {
1062 /* { "byte-count", 1, NULL, 'b' }, */
1063 { "logical", required_argument, NULL, 'l' },
1064 { "copy", required_argument, NULL, 'c' },
1065 { "bytes", required_argument, NULL, 'b' },
1066 { "extent-record", no_argument, NULL, 'e' },
1067 { "extent-tree", no_argument, NULL, 'E' },
1068 { "keys", no_argument, NULL, 'k' },
1069 { "chunk-record", no_argument, NULL, 'u' },
1070 { "chunk-tree", no_argument, NULL, 'U' },
1071 { "inode", required_argument, NULL, 'i'},
1072 { "file-extent", required_argument, NULL, 'x'},
1073 { "metadata-block", required_argument, NULL, 'm'},
1074 { "field", required_argument, NULL, 'f'},
1075 { "key", required_argument, NULL, 'K'},
1076 { "item", no_argument, NULL, 'I'},
1077 { "dir-item", no_argument, NULL, 'D'},
1078 { "delete", no_argument, NULL, 'd'},
1079 { "root", no_argument, NULL, 'r'},
1080 { "csum", required_argument, NULL, 'C'},
1081 { "help", no_argument, NULL, GETOPT_VAL_HELP},
1082 { NULL, 0, NULL, 0 }
1085 c = getopt_long(argc, argv, "l:c:b:eEkuUi:f:x:m:K:IDdr:C:",
1086 long_options, NULL);
1091 logical = arg_strtou64(optarg);
1094 copy = arg_strtou64(optarg);
1097 bytes = arg_strtou64(optarg);
1106 corrupt_block_keys = 1;
1115 inode = arg_strtou64(optarg);
1118 strncpy(field, optarg, FIELD_BUF_LEN);
1121 file_extent = arg_strtou64(optarg);
1124 metadata_block = arg_strtou64(optarg);
1127 ret = sscanf(optarg, "%llu,%u,%llu",
1129 (unsigned int *)&key.type,
1132 fprintf(stderr, "error reading key "
1147 root_objectid = arg_strtou64(optarg);
1150 csum_bytenr = arg_strtou64(optarg);
1152 case GETOPT_VAL_HELP:
1154 print_usage(c != GETOPT_VAL_HELP);
1158 if (check_argc_min(argc - optind, 1))
1163 cache_tree_init(&root_cache);
1165 root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
1167 fprintf(stderr, "Open ctree failed\n");
1171 struct btrfs_trans_handle *trans;
1173 if (logical == (u64)-1)
1175 trans = btrfs_start_transaction(root, 1);
1176 ret = corrupt_extent (trans, root, logical, 0);
1177 btrfs_commit_transaction(trans, root);
1181 struct btrfs_trans_handle *trans;
1182 trans = btrfs_start_transaction(root, 1);
1183 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
1184 root->fs_info->extent_root->node);
1185 btrfs_commit_transaction(trans, root);
1189 struct btrfs_trans_handle *trans;
1190 struct btrfs_path *path;
1193 if (logical == (u64)-1)
1195 del = rand_range(3);
1196 path = btrfs_alloc_path();
1198 fprintf(stderr, "path allocation failed\n");
1202 if (find_chunk_offset(root->fs_info->chunk_root, path,
1204 btrfs_free_path(path);
1207 trans = btrfs_start_transaction(root, 1);
1208 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
1211 fprintf(stderr, "Failed to corrupt chunk record\n");
1212 btrfs_commit_transaction(trans, root);
1216 struct btrfs_trans_handle *trans;
1217 trans = btrfs_start_transaction(root, 1);
1218 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
1220 fprintf(stderr, "Failed to corrupt chunk tree\n");
1221 btrfs_commit_transaction(trans, root);
1225 struct btrfs_trans_handle *trans;
1230 trans = btrfs_start_transaction(root, 1);
1231 if (file_extent == (u64)-1) {
1232 printf("corrupting inode\n");
1233 ret = corrupt_inode(trans, root, inode, field);
1235 printf("corrupting file extent\n");
1236 ret = corrupt_file_extent(trans, root, inode,
1237 file_extent, field);
1239 btrfs_commit_transaction(trans, root);
1242 if (metadata_block) {
1245 ret = corrupt_metadata_block(root, metadata_block, field);
1249 if (!key.objectid || *field == 0)
1251 ret = corrupt_dir_item(root, &key, field);
1255 ret = delete_csum(root, csum_bytenr, bytes);
1261 ret = corrupt_btrfs_item(root, &key, field);
1264 struct btrfs_root *target = root;
1268 if (root_objectid) {
1269 struct btrfs_key root_key;
1271 root_key.objectid = root_objectid;
1272 root_key.type = BTRFS_ROOT_ITEM_KEY;
1273 root_key.offset = (u64)-1;
1275 target = btrfs_read_fs_root(root->fs_info, &root_key);
1276 if (IS_ERR(target)) {
1277 fprintf(stderr, "Couldn't find root %llu\n",
1278 (unsigned long long)root_objectid);
1282 ret = delete_item(target, &key);
1285 if (key.objectid || key.offset || key.type) {
1288 ret = corrupt_key(root, &key, field);
1292 * If we made it here and we have extent set then we didn't specify
1293 * inode and we're screwed.
1295 if (file_extent != (u64)-1)
1298 if (logical == (u64)-1)
1302 bytes = root->sectorsize;
1304 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
1305 bytes *= root->sectorsize;
1308 if (corrupt_block_keys) {
1309 corrupt_keys_in_block(root, logical);
1311 struct extent_buffer *eb;
1313 eb = btrfs_find_create_tree_block(root->fs_info,
1314 logical, root->sectorsize);
1317 "not enough memory to allocate extent buffer for bytenr %llu",
1318 (unsigned long long)logical);
1323 debug_corrupt_block(eb, root, logical, root->sectorsize,
1325 free_extent_buffer(eb);
1327 logical += root->sectorsize;
1328 bytes -= root->sectorsize;