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.
19 #define _XOPEN_SOURCE 500
26 #include "kerncompat.h"
30 #include "print-tree.h"
31 #include "transaction.h"
36 #define FIELD_BUF_LEN 80
38 struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
39 u32 blocksize, u64 copy)
42 struct extent_buffer *eb;
44 struct btrfs_multi_bio *multi = NULL;
45 struct btrfs_device *device;
49 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
55 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
56 eb->start, &length, &multi,
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);
72 printf("corrupting %llu copy %d\n", eb->start,
74 memset(eb->data, 0, eb->len);
75 write_extent_to_disk(eb);
79 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
85 if (mirror_num > num_copies)
91 static void print_usage(void)
93 fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n");
94 fprintf(stderr, "\t-l Logical extent to be corrupted\n");
95 fprintf(stderr, "\t-c Copy of the extent to be corrupted"
96 " (usually 1 or 2, default: 0)\n");
97 fprintf(stderr, "\t-b Number of bytes to be corrupted\n");
98 fprintf(stderr, "\t-e Extent to be corrupted\n");
99 fprintf(stderr, "\t-E The whole extent tree to be corrupted\n");
100 fprintf(stderr, "\t-u Given chunk item to be corrupted\n");
101 fprintf(stderr, "\t-U The whole chunk tree to be corrupted\n");
102 fprintf(stderr, "\t-i The inode item to corrupt (must also specify "
103 "the field to corrupt)\n");
104 fprintf(stderr, "\t-x The file extent item to corrupt (must also "
105 "specify -i for the inode and -f for the field to corrupt)\n");
106 fprintf(stderr, "\t-m The metadata block to corrupt (must also "
107 "specify -f for the field to corrupt)\n");
108 fprintf(stderr, "\t-K The key to corrupt in the format "
109 "<num>,<num>,<num> (must also specify -f for the field)\n");
110 fprintf(stderr, "\t-f The field in the item to corrupt\n");
111 fprintf(stderr, "\t-D Corrupt a dir item, must specify key and field\n");
115 static void corrupt_keys(struct btrfs_trans_handle *trans,
116 struct btrfs_root *root,
117 struct extent_buffer *eb)
122 struct btrfs_disk_key bad_key;;
124 nr = btrfs_header_nritems(eb);
129 bad_slot = rand() % nr;
131 if (bad_slot == slot)
135 "corrupting keys in block %llu slot %d swapping with %d\n",
136 (unsigned long long)eb->start, slot, bad_slot);
138 if (btrfs_header_level(eb) == 0) {
139 btrfs_item_key(eb, &bad_key, bad_slot);
140 btrfs_set_item_key(eb, &bad_key, slot);
142 btrfs_node_key(eb, &bad_key, bad_slot);
143 btrfs_set_node_key(eb, &bad_key, slot);
145 btrfs_mark_buffer_dirty(eb);
148 btrfs_super_csum_size(root->fs_info->super_copy);
149 csum_tree_block_size(eb, csum_size, 0);
150 write_extent_to_disk(eb);
155 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
157 struct extent_buffer *eb;
159 eb = read_tree_block(root, bytenr, root->leafsize, 0);
163 corrupt_keys(NULL, root, eb);
164 free_extent_buffer(eb);
168 static int corrupt_extent(struct btrfs_trans_handle *trans,
169 struct btrfs_root *root, u64 bytenr, u64 copy)
171 struct btrfs_key key;
172 struct extent_buffer *leaf;
175 struct btrfs_path *path;
178 int should_del = rand() % 3;
180 path = btrfs_alloc_path();
184 key.objectid = bytenr;
186 key.offset = (u64)-1;
189 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
195 if (path->slots[0] == 0)
200 leaf = path->nodes[0];
201 slot = path->slots[0];
202 btrfs_item_key_to_cpu(leaf, &key, slot);
203 if (key.objectid != bytenr)
206 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
207 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
208 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
209 key.type != BTRFS_EXTENT_REF_V0_KEY &&
210 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
211 key.type != BTRFS_SHARED_DATA_REF_KEY)
216 "deleting extent record: key %llu %u %llu\n",
217 key.objectid, key.type, key.offset);
219 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
220 /* make sure this extent doesn't get
221 * reused for other purposes */
222 btrfs_pin_extent(root->fs_info,
223 key.objectid, key.offset);
226 btrfs_del_item(trans, root, path);
229 "corrupting extent record: key %llu %u %llu\n",
230 key.objectid, key.type, key.offset);
231 ptr = btrfs_item_ptr_offset(leaf, slot);
232 item_size = btrfs_item_size_nr(leaf, slot);
233 memset_extent_buffer(leaf, 0, ptr, item_size);
234 btrfs_mark_buffer_dirty(leaf);
237 btrfs_release_path(path);
245 btrfs_free_path(path);
249 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
250 struct btrfs_root *root,
251 struct extent_buffer *eb)
253 u32 nr = btrfs_header_nritems(eb);
254 u32 victim = rand() % nr;
256 struct btrfs_key key;
258 btrfs_item_key_to_cpu(eb, &key, victim);
259 objectid = key.objectid;
260 corrupt_extent(trans, root, objectid, 1);
263 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
264 struct btrfs_root *root,
265 struct extent_buffer *eb)
272 if (btrfs_is_leaf(eb)) {
273 btrfs_corrupt_extent_leaf(trans, root, eb);
277 if (btrfs_header_level(eb) == 1 && eb != root->node) {
282 for (i = 0; i < btrfs_header_nritems(eb); i++) {
283 struct extent_buffer *next;
285 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
287 btrfs_node_ptr_generation(eb, i));
290 btrfs_corrupt_extent_tree(trans, root, next);
291 free_extent_buffer(next);
295 enum btrfs_inode_field {
296 BTRFS_INODE_FIELD_ISIZE,
297 BTRFS_INODE_FIELD_BAD,
300 enum btrfs_file_extent_field {
301 BTRFS_FILE_EXTENT_DISK_BYTENR,
302 BTRFS_FILE_EXTENT_BAD,
305 enum btrfs_dir_item_field {
307 BTRFS_DIR_ITEM_LOCATION_OBJECTID,
311 enum btrfs_metadata_block_field {
312 BTRFS_METADATA_BLOCK_GENERATION,
313 BTRFS_METADATA_BLOCK_BAD,
316 enum btrfs_key_field {
323 static enum btrfs_inode_field convert_inode_field(char *field)
325 if (!strncmp(field, "isize", FIELD_BUF_LEN))
326 return BTRFS_INODE_FIELD_ISIZE;
327 return BTRFS_INODE_FIELD_BAD;
330 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
332 if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
333 return BTRFS_FILE_EXTENT_DISK_BYTENR;
334 return BTRFS_FILE_EXTENT_BAD;
337 static enum btrfs_metadata_block_field
338 convert_metadata_block_field(char *field)
340 if (!strncmp(field, "generation", FIELD_BUF_LEN))
341 return BTRFS_METADATA_BLOCK_GENERATION;
342 return BTRFS_METADATA_BLOCK_BAD;
345 static enum btrfs_key_field convert_key_field(char *field)
347 if (!strncmp(field, "objectid", FIELD_BUF_LEN))
348 return BTRFS_KEY_OBJECTID;
349 if (!strncmp(field, "type", FIELD_BUF_LEN))
350 return BTRFS_KEY_TYPE;
351 if (!strncmp(field, "offset", FIELD_BUF_LEN))
352 return BTRFS_KEY_OFFSET;
353 return BTRFS_KEY_BAD;
356 static enum btrfs_dir_item_field convert_dir_item_field(char *field)
358 if (!strncmp(field, "name", FIELD_BUF_LEN))
359 return BTRFS_DIR_ITEM_NAME;
360 if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
361 return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
362 return BTRFS_DIR_ITEM_BAD;
365 static u64 generate_u64(u64 orig)
370 } while (ret == orig);
374 static u8 generate_u8(u8 orig)
379 } while (ret == orig);
383 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
386 enum btrfs_key_field corrupt_field = convert_key_field(field);
387 struct btrfs_path *path;
388 struct btrfs_trans_handle *trans;
391 root = root->fs_info->fs_root;
392 if (corrupt_field == BTRFS_KEY_BAD) {
393 fprintf(stderr, "Invalid field %s\n", field);
397 path = btrfs_alloc_path();
401 trans = btrfs_start_transaction(root, 1);
403 btrfs_free_path(path);
404 return PTR_ERR(trans);
407 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
411 fprintf(stderr, "Couldn't find the key to corrupt\n");
416 switch (corrupt_field) {
417 case BTRFS_KEY_OBJECTID:
418 key->objectid = generate_u64(key->objectid);
421 key->type = generate_u8(key->type);
423 case BTRFS_KEY_OFFSET:
424 key->offset = generate_u64(key->objectid);
427 fprintf(stderr, "Invalid field %s, %d\n", field,
433 btrfs_set_item_key_unsafe(root, path, key);
435 btrfs_free_path(path);
436 btrfs_commit_transaction(trans, root);
440 static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
443 struct btrfs_trans_handle *trans;
444 struct btrfs_dir_item *di;
445 struct btrfs_path *path;
447 struct btrfs_key location;
448 struct btrfs_disk_key disk_key;
449 unsigned long name_ptr;
450 enum btrfs_dir_item_field corrupt_field =
451 convert_dir_item_field(field);
456 if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
457 fprintf(stderr, "Invalid field %s\n", field);
461 path = btrfs_alloc_path();
465 trans = btrfs_start_transaction(root, 1);
467 btrfs_free_path(path);
468 return PTR_ERR(trans);
471 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
475 fprintf(stderr, "Error searching for dir item %d\n", ret);
479 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
480 struct btrfs_dir_item);
482 switch (corrupt_field) {
483 case BTRFS_DIR_ITEM_NAME:
484 name_len = btrfs_dir_name_len(path->nodes[0], di);
485 name = malloc(name_len);
490 name_ptr = (unsigned long)(di + 1);
491 read_extent_buffer(path->nodes[0], name, name_ptr, name_len);
493 write_extent_buffer(path->nodes[0], name, name_ptr, name_len);
494 btrfs_mark_buffer_dirty(path->nodes[0]);
497 case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
498 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
499 bogus = generate_u64(location.objectid);
500 location.objectid = bogus;
501 btrfs_cpu_key_to_disk(&disk_key, &location);
502 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
503 btrfs_mark_buffer_dirty(path->nodes[0]);
510 btrfs_commit_transaction(trans, root);
511 btrfs_free_path(path);
515 static int corrupt_inode(struct btrfs_trans_handle *trans,
516 struct btrfs_root *root, u64 inode, char *field)
518 struct btrfs_inode_item *ei;
519 struct btrfs_path *path;
520 struct btrfs_key key;
521 enum btrfs_inode_field corrupt_field = convert_inode_field(field);
526 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
527 fprintf(stderr, "Invalid field %s\n", field);
531 key.objectid = inode;
532 key.type = BTRFS_INODE_ITEM_KEY;
533 key.offset = (u64)-1;
535 path = btrfs_alloc_path();
539 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
543 if (!path->slots[0]) {
544 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
552 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
553 if (key.objectid != inode) {
554 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
559 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
560 struct btrfs_inode_item);
561 switch (corrupt_field) {
562 case BTRFS_INODE_FIELD_ISIZE:
563 orig = btrfs_inode_size(path->nodes[0], ei);
564 bogus = generate_u64(orig);
565 btrfs_set_inode_size(path->nodes[0], ei, bogus);
571 btrfs_mark_buffer_dirty(path->nodes[0]);
573 btrfs_free_path(path);
577 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
578 struct btrfs_root *root, u64 inode, u64 extent,
581 struct btrfs_file_extent_item *fi;
582 struct btrfs_path *path;
583 struct btrfs_key key;
584 enum btrfs_file_extent_field corrupt_field;
589 corrupt_field = convert_file_extent_field(field);
590 if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
591 fprintf(stderr, "Invalid field %s\n", field);
595 key.objectid = inode;
596 key.type = BTRFS_EXTENT_DATA_KEY;
599 path = btrfs_alloc_path();
603 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
607 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
613 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
614 struct btrfs_file_extent_item);
615 switch (corrupt_field) {
616 case BTRFS_FILE_EXTENT_DISK_BYTENR:
617 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
618 bogus = generate_u64(orig);
619 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
625 btrfs_mark_buffer_dirty(path->nodes[0]);
627 btrfs_free_path(path);
631 static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
634 struct btrfs_trans_handle *trans;
635 struct btrfs_path *path;
636 struct extent_buffer *eb;
637 struct btrfs_key key, root_key;
638 enum btrfs_metadata_block_field corrupt_field;
644 corrupt_field = convert_metadata_block_field(field);
645 if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
646 fprintf(stderr, "Invalid field %s\n", field);
650 eb = read_tree_block(root, block, root->leafsize, 0);
652 fprintf(stderr, "Couldn't read in tree block %s\n", field);
655 root_objectid = btrfs_header_owner(eb);
656 level = btrfs_header_level(eb);
658 btrfs_node_key_to_cpu(eb, &key, 0);
660 btrfs_item_key_to_cpu(eb, &key, 0);
661 free_extent_buffer(eb);
663 root_key.objectid = root_objectid;
664 root_key.type = BTRFS_ROOT_ITEM_KEY;
665 root_key.offset = (u64)-1;
667 root = btrfs_read_fs_root(root->fs_info, &root_key);
669 fprintf(stderr, "Couldn't finde owner root %llu\n",
671 return PTR_ERR(root);
674 path = btrfs_alloc_path();
678 trans = btrfs_start_transaction(root, 1);
680 btrfs_free_path(path);
681 fprintf(stderr, "Couldn't start transaction %ld\n",
683 return PTR_ERR(trans);
686 path->lowest_level = level;
687 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
689 fprintf(stderr, "Error searching to node %d\n", ret);
692 eb = path->nodes[level];
695 switch (corrupt_field) {
696 case BTRFS_METADATA_BLOCK_GENERATION:
697 orig = btrfs_header_generation(eb);
698 bogus = generate_u64(orig);
699 btrfs_set_header_generation(eb, bogus);
705 btrfs_mark_buffer_dirty(path->nodes[level]);
707 btrfs_commit_transaction(trans, root);
708 btrfs_free_path(path);
712 static struct option long_options[] = {
713 /* { "byte-count", 1, NULL, 'b' }, */
714 { "logical", 1, NULL, 'l' },
715 { "copy", 1, NULL, 'c' },
716 { "bytes", 1, NULL, 'b' },
717 { "extent-record", 0, NULL, 'e' },
718 { "extent-tree", 0, NULL, 'E' },
719 { "keys", 0, NULL, 'k' },
720 { "chunk-record", 0, NULL, 'u' },
721 { "chunk-tree", 0, NULL, 'U' },
722 { "inode", 1, NULL, 'i'},
723 { "file-extent", 1, NULL, 'x'},
724 { "metadata-block", 1, NULL, 'm'},
725 { "field", 1, NULL, 'f'},
726 { "key", 1, NULL, 'K'},
727 { "dir-item", 0, NULL, 'D'},
731 /* corrupt item using NO cow.
732 * Because chunk recover will recover based on whole partition scaning,
733 * If using COW, chunk recover will use the old item to recover,
734 * which is still OK but we want to check the ability to rebuild chunk
735 * not only restore the old ones */
736 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
737 struct btrfs_root *root, struct btrfs_path *path,
741 struct btrfs_key key;
742 struct extent_buffer *leaf;
747 leaf = path->nodes[0];
748 slot = path->slots[0];
749 /* Not deleting the first item of a leaf to keep leaf structure */
752 /* Only accept valid eb */
753 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
754 btrfs_item_key_to_cpu(leaf, &key, slot);
756 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
757 key.objectid, key.type, key.offset);
758 btrfs_del_item(trans, root, path);
760 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
761 key.objectid, key.type, key.offset);
762 ptr = btrfs_item_ptr_offset(leaf, slot);
763 item_size = btrfs_item_size_nr(leaf, slot);
764 memset_extent_buffer(leaf, 0, ptr, item_size);
765 btrfs_mark_buffer_dirty(leaf);
769 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
770 struct btrfs_root *root)
775 struct btrfs_path *path;
776 struct btrfs_key key;
777 struct btrfs_key found_key;
778 struct extent_buffer *leaf;
780 path = btrfs_alloc_path();
784 key.objectid = (u64)-1;
785 key.offset = (u64)-1;
788 /* Here, cow and ins_len must equals 0 for the following reasons:
789 * 1) chunk recover is based on disk scanning, so COW should be
790 * disabled in case the original chunk being scanned and
791 * recovered using the old chunk.
792 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
795 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
798 fprintf(stderr, "Error searching tree\n");
801 /* corrupt/del dev_item first */
802 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
803 slot = path->slots[0];
804 leaf = path->nodes[0];
806 /* Never delete the first item to keep the leaf structure */
807 if (path->slots[0] == 0)
809 ret = corrupt_item_nocow(trans, root, path, del);
813 btrfs_release_path(path);
815 /* Here, cow and ins_len must equals 0 for the following reasons:
816 * 1) chunk recover is based on disk scanning, so COW should be
817 * disabled in case the original chunk being scanned and
818 * recovered using the old chunk.
819 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
822 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
825 fprintf(stderr, "Error searching tree\n");
828 /* corrupt/del chunk then*/
829 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
830 slot = path->slots[0];
831 leaf = path->nodes[0];
833 btrfs_item_key_to_cpu(leaf, &found_key, slot);
834 ret = corrupt_item_nocow(trans, root, path, del);
839 btrfs_free_path(path);
842 int find_chunk_offset(struct btrfs_root *root,
843 struct btrfs_path *path, u64 offset)
845 struct btrfs_key key;
848 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
849 key.type = BTRFS_CHUNK_ITEM_KEY;
852 /* Here, cow and ins_len must equals 0 for following reasons:
853 * 1) chunk recover is based on disk scanning, so COW should
854 * be disabled in case the original chunk being scanned
855 * and recovered using the old chunk.
856 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
859 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
861 fprintf(stderr, "Can't find chunk with given offset %llu\n",
866 fprintf(stderr, "Error searching chunk");
873 int main(int ac, char **av)
875 struct cache_tree root_cache;
876 struct btrfs_key key;
877 struct btrfs_root *root;
878 struct extent_buffer *eb;
880 /* chunk offset can be 0,so change to (u64)-1 */
881 u64 logical = (u64)-1;
883 int option_index = 0;
888 int corrupt_block_keys = 0;
892 u64 metadata_block = 0;
894 u64 file_extent = (u64)-1;
895 char field[FIELD_BUF_LEN];
899 memset(&key, 0, sizeof(key));
903 c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:D", long_options,
909 logical = arg_strtou64(optarg);
912 copy = arg_strtou64(optarg);
915 bytes = arg_strtou64(optarg);
924 corrupt_block_keys = 1;
932 inode = arg_strtou64(optarg);
935 strncpy(field, optarg, FIELD_BUF_LEN);
938 file_extent = arg_strtou64(optarg);
941 metadata_block = arg_strtou64(optarg);
944 ret = sscanf(optarg, "%llu,%u,%llu",
946 (unsigned int *)&key.type,
949 fprintf(stderr, "error reading key "
963 if (check_argc_min(ac, 1))
968 cache_tree_init(&root_cache);
970 root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
972 fprintf(stderr, "Open ctree failed\n");
976 struct btrfs_trans_handle *trans;
978 if (logical == (u64)-1)
980 trans = btrfs_start_transaction(root, 1);
981 ret = corrupt_extent (trans, root, logical, 0);
982 btrfs_commit_transaction(trans, root);
986 struct btrfs_trans_handle *trans;
987 trans = btrfs_start_transaction(root, 1);
988 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
989 root->fs_info->extent_root->node);
990 btrfs_commit_transaction(trans, root);
994 struct btrfs_trans_handle *trans;
995 struct btrfs_path *path;
998 if (logical == (u64)-1)
1001 path = btrfs_alloc_path();
1003 fprintf(stderr, "path allocation failed\n");
1007 if (find_chunk_offset(root->fs_info->chunk_root, path,
1009 btrfs_free_path(path);
1012 trans = btrfs_start_transaction(root, 1);
1013 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
1016 fprintf(stderr, "Failed to corrupt chunk record\n");
1017 btrfs_commit_transaction(trans, root);
1021 struct btrfs_trans_handle *trans;
1022 trans = btrfs_start_transaction(root, 1);
1023 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
1025 fprintf(stderr, "Failed to corrupt chunk tree\n");
1026 btrfs_commit_transaction(trans, root);
1030 struct btrfs_trans_handle *trans;
1035 trans = btrfs_start_transaction(root, 1);
1036 if (file_extent == (u64)-1) {
1037 printf("corrupting inode\n");
1038 ret = corrupt_inode(trans, root, inode, field);
1040 printf("corrupting file extent\n");
1041 ret = corrupt_file_extent(trans, root, inode,
1042 file_extent, field);
1044 btrfs_commit_transaction(trans, root);
1047 if (metadata_block) {
1050 ret = corrupt_metadata_block(root, metadata_block, field);
1054 if (!key.objectid || !strlen(field))
1056 ret = corrupt_dir_item(root, &key, field);
1059 if (key.objectid || key.offset || key.type) {
1062 ret = corrupt_key(root, &key, field);
1066 * If we made it here and we have extent set then we didn't specify
1067 * inode and we're screwed.
1069 if (file_extent != (u64)-1)
1072 if (logical == (u64)-1)
1076 bytes = root->sectorsize;
1078 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
1079 bytes *= root->sectorsize;
1082 if (corrupt_block_keys) {
1083 corrupt_keys_in_block(root, logical);
1085 eb = debug_corrupt_block(root, logical,
1086 root->sectorsize, copy);
1087 free_extent_buffer(eb);
1089 logical += root->sectorsize;
1090 bytes -= root->sectorsize;