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"
36 #define FIELD_BUF_LEN 80
38 static int debug_corrupt_block(struct extent_buffer *eb,
39 struct btrfs_root *root, u64 bytenr, u32 blocksize, u64 copy)
43 struct btrfs_multi_bio *multi = NULL;
44 struct btrfs_device *device;
50 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
51 eb->start, &length, &multi,
54 error("cannot map block %llu length %llu mirror %d: %d",
55 (unsigned long long)eb->start,
56 (unsigned long long)length,
60 device = multi->stripes[0].dev;
63 eb->dev_bytenr = multi->stripes[0].physical;
66 "mirror %d logical %llu physical %llu device %s\n",
67 mirror_num, (unsigned long long)bytenr,
68 (unsigned long long)eb->dev_bytenr, device->name);
71 if (!copy || mirror_num == copy) {
72 ret = read_extent_from_disk(eb, 0, eb->len);
74 error("cannot read eb bytenr %llu: %s",
75 (unsigned long long)eb->dev_bytenr,
79 printf("corrupting %llu copy %d\n", eb->start,
81 memset(eb->data, 0, eb->len);
82 ret = write_extent_to_disk(eb);
84 error("cannot write eb bytenr %llu: %s",
85 (unsigned long long)eb->dev_bytenr,
92 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
98 if (mirror_num > num_copies)
105 static void print_usage(int ret)
107 printf("usage: btrfs-corrupt-block [options] device\n");
108 printf("\t-l Logical extent to be corrupted\n");
109 printf("\t-c Copy of the extent to be corrupted (usually 1 or 2, default: 0)\n");
110 printf("\t-b Number of bytes to be corrupted\n");
111 printf("\t-e Extent to be corrupted\n");
112 printf("\t-E The whole extent tree to be corrupted\n");
113 printf("\t-u Given chunk item to be corrupted\n");
114 printf("\t-U The whole chunk tree to be corrupted\n");
115 printf("\t-i The inode item to corrupt (must also specify the field to corrupt)\n");
116 printf("\t-x The file extent item to corrupt (must also specify -i for the inode and -f for the field to corrupt)\n");
117 printf("\t-m The metadata block to corrupt (must also specify -f for the field to corrupt)\n");
118 printf("\t-K The key to corrupt in the format <num>,<num>,<num> (must also specify -f for the field)\n");
119 printf("\t-f The field in the item to corrupt\n");
120 printf("\t-I An item to corrupt (must also specify the field to corrupt and a root+key for the item)\n");
121 printf("\t-D Corrupt a dir item, must specify key and field\n");
122 printf("\t-d Delete this item (must specify -K)\n");
123 printf("\t-r Operate on this root (only works with -d)\n");
124 printf("\t-C Delete a csum for the specified bytenr. When used with -b it'll delete that many bytes, otherwise it's just sectorsize\n");
128 static void corrupt_keys(struct btrfs_trans_handle *trans,
129 struct btrfs_fs_info *fs_info,
130 struct extent_buffer *eb)
135 struct btrfs_disk_key bad_key;;
137 nr = btrfs_header_nritems(eb);
141 slot = rand_range(nr);
142 bad_slot = rand_range(nr);
144 if (bad_slot == slot)
148 "corrupting keys in block %llu slot %d swapping with %d\n",
149 (unsigned long long)eb->start, slot, bad_slot);
151 if (btrfs_header_level(eb) == 0) {
152 btrfs_item_key(eb, &bad_key, bad_slot);
153 btrfs_set_item_key(eb, &bad_key, slot);
155 btrfs_node_key(eb, &bad_key, bad_slot);
156 btrfs_set_node_key(eb, &bad_key, slot);
158 btrfs_mark_buffer_dirty(eb);
161 btrfs_super_csum_size(fs_info->super_copy);
162 csum_tree_block_size(eb, csum_size, 0);
163 write_extent_to_disk(eb);
168 static int corrupt_keys_in_block(struct btrfs_fs_info *fs_info, u64 bytenr)
170 struct extent_buffer *eb;
172 eb = read_tree_block(fs_info, bytenr, fs_info->nodesize, 0);
173 if (!extent_buffer_uptodate(eb))
176 corrupt_keys(NULL, fs_info, eb);
177 free_extent_buffer(eb);
181 static int corrupt_extent(struct btrfs_trans_handle *trans,
182 struct btrfs_root *root, u64 bytenr)
184 struct btrfs_key key;
185 struct extent_buffer *leaf;
188 struct btrfs_path *path;
191 int should_del = rand_range(3);
193 path = btrfs_alloc_path();
197 key.objectid = bytenr;
199 key.offset = (u64)-1;
202 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
208 if (path->slots[0] == 0)
213 leaf = path->nodes[0];
214 slot = path->slots[0];
215 btrfs_item_key_to_cpu(leaf, &key, slot);
216 if (key.objectid != bytenr)
219 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
220 key.type != BTRFS_METADATA_ITEM_KEY &&
221 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
222 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
223 key.type != BTRFS_EXTENT_REF_V0_KEY &&
224 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
225 key.type != BTRFS_SHARED_DATA_REF_KEY)
230 "deleting extent record: key %llu %u %llu\n",
231 key.objectid, key.type, key.offset);
233 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
234 /* make sure this extent doesn't get
235 * reused for other purposes */
236 btrfs_pin_extent(root->fs_info,
237 key.objectid, key.offset);
240 btrfs_del_item(trans, root, path);
243 "corrupting extent record: key %llu %u %llu\n",
244 key.objectid, key.type, key.offset);
245 ptr = btrfs_item_ptr_offset(leaf, slot);
246 item_size = btrfs_item_size_nr(leaf, slot);
247 memset_extent_buffer(leaf, 0, ptr, item_size);
248 btrfs_mark_buffer_dirty(leaf);
251 btrfs_release_path(path);
259 btrfs_free_path(path);
263 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
264 struct btrfs_root *root,
265 struct extent_buffer *eb)
267 u32 nr = btrfs_header_nritems(eb);
268 u32 victim = rand_range(nr);
270 struct btrfs_key key;
272 btrfs_item_key_to_cpu(eb, &key, victim);
273 objectid = key.objectid;
274 corrupt_extent(trans, root, objectid);
277 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
278 struct btrfs_root *root,
279 struct extent_buffer *eb)
281 struct btrfs_fs_info *fs_info = root->fs_info;
287 if (btrfs_is_leaf(eb)) {
288 btrfs_corrupt_extent_leaf(trans, root, eb);
292 if (btrfs_header_level(eb) == 1 && eb != root->node) {
297 for (i = 0; i < btrfs_header_nritems(eb); i++) {
298 struct extent_buffer *next;
300 next = read_tree_block(fs_info, btrfs_node_blockptr(eb, i),
302 btrfs_node_ptr_generation(eb, i));
303 if (!extent_buffer_uptodate(next))
305 btrfs_corrupt_extent_tree(trans, root, next);
306 free_extent_buffer(next);
310 enum btrfs_inode_field {
311 BTRFS_INODE_FIELD_ISIZE,
312 BTRFS_INODE_FIELD_NBYTES,
313 BTRFS_INODE_FIELD_NLINK,
314 BTRFS_INODE_FIELD_GENERATION,
315 BTRFS_INODE_FIELD_TRANSID,
316 BTRFS_INODE_FIELD_BLOCK_GROUP,
317 BTRFS_INODE_FIELD_MODE,
318 BTRFS_INODE_FIELD_UID,
319 BTRFS_INODE_FIELD_GID,
320 BTRFS_INODE_FIELD_BAD,
323 enum btrfs_file_extent_field {
324 BTRFS_FILE_EXTENT_DISK_BYTENR,
325 BTRFS_FILE_EXTENT_BAD,
328 enum btrfs_dir_item_field {
330 BTRFS_DIR_ITEM_LOCATION_OBJECTID,
334 enum btrfs_metadata_block_field {
335 BTRFS_METADATA_BLOCK_GENERATION,
336 BTRFS_METADATA_BLOCK_SHIFT_ITEMS,
337 BTRFS_METADATA_BLOCK_BAD,
340 enum btrfs_item_field {
345 enum btrfs_key_field {
352 static enum btrfs_inode_field convert_inode_field(char *field)
354 if (!strncmp(field, "isize", FIELD_BUF_LEN))
355 return BTRFS_INODE_FIELD_ISIZE;
356 if (!strncmp(field, "nbytes", FIELD_BUF_LEN))
357 return BTRFS_INODE_FIELD_NBYTES;
358 if (!strncmp(field, "nlink", FIELD_BUF_LEN))
359 return BTRFS_INODE_FIELD_NLINK;
360 if (!strncmp(field, "generation", FIELD_BUF_LEN))
361 return BTRFS_INODE_FIELD_GENERATION;
362 if (!strncmp(field, "transid", FIELD_BUF_LEN))
363 return BTRFS_INODE_FIELD_TRANSID;
364 if (!strncmp(field, "block_group", FIELD_BUF_LEN))
365 return BTRFS_INODE_FIELD_BLOCK_GROUP;
366 if (!strncmp(field, "mode", FIELD_BUF_LEN))
367 return BTRFS_INODE_FIELD_MODE;
368 if (!strncmp(field, "uid", FIELD_BUF_LEN))
369 return BTRFS_INODE_FIELD_UID;
370 if (!strncmp(field, "gid", FIELD_BUF_LEN))
371 return BTRFS_INODE_FIELD_GID;
372 return BTRFS_INODE_FIELD_BAD;
375 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
377 if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
378 return BTRFS_FILE_EXTENT_DISK_BYTENR;
379 return BTRFS_FILE_EXTENT_BAD;
382 static enum btrfs_metadata_block_field
383 convert_metadata_block_field(char *field)
385 if (!strncmp(field, "generation", FIELD_BUF_LEN))
386 return BTRFS_METADATA_BLOCK_GENERATION;
387 if (!strncmp(field, "shift_items", FIELD_BUF_LEN))
388 return BTRFS_METADATA_BLOCK_SHIFT_ITEMS;
389 return BTRFS_METADATA_BLOCK_BAD;
392 static enum btrfs_key_field convert_key_field(char *field)
394 if (!strncmp(field, "objectid", FIELD_BUF_LEN))
395 return BTRFS_KEY_OBJECTID;
396 if (!strncmp(field, "type", FIELD_BUF_LEN))
397 return BTRFS_KEY_TYPE;
398 if (!strncmp(field, "offset", FIELD_BUF_LEN))
399 return BTRFS_KEY_OFFSET;
400 return BTRFS_KEY_BAD;
403 static enum btrfs_item_field convert_item_field(char *field)
405 if (!strncmp(field, "offset", FIELD_BUF_LEN))
406 return BTRFS_ITEM_OFFSET;
407 return BTRFS_ITEM_BAD;
410 static enum btrfs_dir_item_field convert_dir_item_field(char *field)
412 if (!strncmp(field, "name", FIELD_BUF_LEN))
413 return BTRFS_DIR_ITEM_NAME;
414 if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
415 return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
416 return BTRFS_DIR_ITEM_BAD;
419 static u64 generate_u64(u64 orig)
424 } while (ret == orig);
428 static u32 generate_u32(u32 orig)
433 } while (ret == orig);
437 static u8 generate_u8(u8 orig)
442 } while (ret == orig);
446 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
449 enum btrfs_key_field corrupt_field = convert_key_field(field);
450 struct btrfs_path *path;
451 struct btrfs_trans_handle *trans;
454 root = root->fs_info->fs_root;
455 if (corrupt_field == BTRFS_KEY_BAD) {
456 fprintf(stderr, "Invalid field %s\n", field);
460 path = btrfs_alloc_path();
464 trans = btrfs_start_transaction(root, 1);
466 btrfs_free_path(path);
467 return PTR_ERR(trans);
470 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
474 fprintf(stderr, "Couldn't find the key to corrupt\n");
479 switch (corrupt_field) {
480 case BTRFS_KEY_OBJECTID:
481 key->objectid = generate_u64(key->objectid);
484 key->type = generate_u8(key->type);
486 case BTRFS_KEY_OFFSET:
487 key->offset = generate_u64(key->objectid);
490 fprintf(stderr, "Invalid field %s, %d\n", field,
496 btrfs_set_item_key_unsafe(root, path, key);
498 btrfs_free_path(path);
499 btrfs_commit_transaction(trans, root);
503 static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
506 struct btrfs_trans_handle *trans;
507 struct btrfs_dir_item *di;
508 struct btrfs_path *path;
510 struct btrfs_key location;
511 struct btrfs_disk_key disk_key;
512 unsigned long name_ptr;
513 enum btrfs_dir_item_field corrupt_field =
514 convert_dir_item_field(field);
519 if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
520 fprintf(stderr, "Invalid field %s\n", field);
524 path = btrfs_alloc_path();
528 trans = btrfs_start_transaction(root, 1);
530 btrfs_free_path(path);
531 return PTR_ERR(trans);
534 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
538 fprintf(stderr, "Error searching for dir item %d\n", ret);
542 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
543 struct btrfs_dir_item);
545 switch (corrupt_field) {
546 case BTRFS_DIR_ITEM_NAME:
547 name_len = btrfs_dir_name_len(path->nodes[0], di);
548 name_ptr = (unsigned long)(di + 1);
549 read_extent_buffer(path->nodes[0], name, name_ptr, name_len);
551 write_extent_buffer(path->nodes[0], name, name_ptr, name_len);
552 btrfs_mark_buffer_dirty(path->nodes[0]);
554 case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
555 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
556 bogus = generate_u64(location.objectid);
557 location.objectid = bogus;
558 btrfs_cpu_key_to_disk(&disk_key, &location);
559 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
560 btrfs_mark_buffer_dirty(path->nodes[0]);
567 btrfs_commit_transaction(trans, root);
568 btrfs_free_path(path);
572 static int corrupt_inode(struct btrfs_trans_handle *trans,
573 struct btrfs_root *root, u64 inode, char *field)
575 struct btrfs_inode_item *ei;
576 struct btrfs_path *path;
577 struct btrfs_key key;
578 enum btrfs_inode_field corrupt_field = convert_inode_field(field);
583 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
584 fprintf(stderr, "Invalid field %s\n", field);
588 key.objectid = inode;
589 key.type = BTRFS_INODE_ITEM_KEY;
590 key.offset = (u64)-1;
592 path = btrfs_alloc_path();
596 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
600 if (!path->slots[0]) {
601 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
609 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
610 if (key.objectid != inode) {
611 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
616 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
617 struct btrfs_inode_item);
618 switch (corrupt_field) {
619 case BTRFS_INODE_FIELD_ISIZE:
620 orig = btrfs_inode_size(path->nodes[0], ei);
621 bogus = generate_u64(orig);
622 btrfs_set_inode_size(path->nodes[0], ei, bogus);
624 case BTRFS_INODE_FIELD_NBYTES:
625 orig = btrfs_inode_nbytes(path->nodes[0], ei);
626 bogus = generate_u64(orig);
627 btrfs_set_inode_nbytes(path->nodes[0], ei, bogus);
629 case BTRFS_INODE_FIELD_NLINK:
630 orig = btrfs_inode_nlink(path->nodes[0], ei);
631 bogus = generate_u32(orig);
632 btrfs_set_inode_nlink(path->nodes[0], ei, bogus);
634 case BTRFS_INODE_FIELD_GENERATION:
635 orig = btrfs_inode_generation(path->nodes[0], ei);
636 bogus = generate_u64(orig);
637 btrfs_set_inode_generation(path->nodes[0], ei, bogus);
639 case BTRFS_INODE_FIELD_TRANSID:
640 orig = btrfs_inode_transid(path->nodes[0], ei);
641 bogus = generate_u64(orig);
642 btrfs_set_inode_transid(path->nodes[0], ei, bogus);
644 case BTRFS_INODE_FIELD_BLOCK_GROUP:
645 orig = btrfs_inode_block_group(path->nodes[0], ei);
646 bogus = generate_u64(orig);
647 btrfs_set_inode_block_group(path->nodes[0], ei, bogus);
649 case BTRFS_INODE_FIELD_MODE:
650 orig = btrfs_inode_mode(path->nodes[0], ei);
651 bogus = generate_u32(orig);
652 btrfs_set_inode_mode(path->nodes[0], ei, bogus);
654 case BTRFS_INODE_FIELD_UID:
655 orig = btrfs_inode_uid(path->nodes[0], ei);
656 bogus = generate_u32(orig);
657 btrfs_set_inode_uid(path->nodes[0], ei, bogus);
659 case BTRFS_INODE_FIELD_GID:
660 orig = btrfs_inode_gid(path->nodes[0], ei);
661 bogus = generate_u32(orig);
662 btrfs_set_inode_gid(path->nodes[0], ei, bogus);
668 btrfs_mark_buffer_dirty(path->nodes[0]);
670 btrfs_free_path(path);
674 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
675 struct btrfs_root *root, u64 inode, u64 extent,
678 struct btrfs_file_extent_item *fi;
679 struct btrfs_path *path;
680 struct btrfs_key key;
681 enum btrfs_file_extent_field corrupt_field;
686 corrupt_field = convert_file_extent_field(field);
687 if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
688 fprintf(stderr, "Invalid field %s\n", field);
692 key.objectid = inode;
693 key.type = BTRFS_EXTENT_DATA_KEY;
696 path = btrfs_alloc_path();
700 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
704 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
710 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
711 struct btrfs_file_extent_item);
712 switch (corrupt_field) {
713 case BTRFS_FILE_EXTENT_DISK_BYTENR:
714 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
715 bogus = generate_u64(orig);
716 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
722 btrfs_mark_buffer_dirty(path->nodes[0]);
724 btrfs_free_path(path);
728 static void shift_items(struct btrfs_root *root, struct extent_buffer *eb)
730 int nritems = btrfs_header_nritems(eb);
731 int shift_space = btrfs_leaf_free_space(root, eb) / 2;
732 int slot = nritems / 2;
734 unsigned int data_end = btrfs_item_offset_nr(eb, nritems - 1);
736 /* Shift the item data up to and including slot back by shift space */
737 memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end - shift_space,
738 btrfs_leaf_data(eb) + data_end,
739 btrfs_item_offset_nr(eb, slot - 1) - data_end);
741 /* Now update the item pointers. */
742 for (i = nritems - 1; i >= slot; i--) {
743 u32 offset = btrfs_item_offset_nr(eb, i);
744 offset -= shift_space;
745 btrfs_set_item_offset(eb, btrfs_item_nr(i), offset);
749 static int corrupt_metadata_block(struct btrfs_fs_info *fs_info, u64 block,
752 struct btrfs_trans_handle *trans;
753 struct btrfs_root *root;
754 struct btrfs_path *path;
755 struct extent_buffer *eb;
756 struct btrfs_key key, root_key;
757 enum btrfs_metadata_block_field corrupt_field;
763 corrupt_field = convert_metadata_block_field(field);
764 if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
765 fprintf(stderr, "Invalid field %s\n", field);
769 eb = read_tree_block(fs_info, block, fs_info->nodesize, 0);
770 if (!extent_buffer_uptodate(eb)) {
771 fprintf(stderr, "Couldn't read in tree block %s\n", field);
774 root_objectid = btrfs_header_owner(eb);
775 level = btrfs_header_level(eb);
777 btrfs_node_key_to_cpu(eb, &key, 0);
779 btrfs_item_key_to_cpu(eb, &key, 0);
780 free_extent_buffer(eb);
782 root_key.objectid = root_objectid;
783 root_key.type = BTRFS_ROOT_ITEM_KEY;
784 root_key.offset = (u64)-1;
786 root = btrfs_read_fs_root(fs_info, &root_key);
788 fprintf(stderr, "Couldn't find owner root %llu\n",
790 return PTR_ERR(root);
793 path = btrfs_alloc_path();
797 trans = btrfs_start_transaction(root, 1);
799 btrfs_free_path(path);
800 fprintf(stderr, "Couldn't start transaction %ld\n",
802 return PTR_ERR(trans);
805 path->lowest_level = level;
806 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
808 fprintf(stderr, "Error searching to node %d\n", ret);
811 eb = path->nodes[level];
814 switch (corrupt_field) {
815 case BTRFS_METADATA_BLOCK_GENERATION:
816 orig = btrfs_header_generation(eb);
817 bogus = generate_u64(orig);
818 btrfs_set_header_generation(eb, bogus);
820 case BTRFS_METADATA_BLOCK_SHIFT_ITEMS:
821 shift_items(root, path->nodes[level]);
827 btrfs_mark_buffer_dirty(path->nodes[level]);
829 btrfs_commit_transaction(trans, root);
830 btrfs_free_path(path);
834 static int corrupt_btrfs_item(struct btrfs_root *root, struct btrfs_key *key,
837 struct btrfs_trans_handle *trans;
838 struct btrfs_path *path;
839 enum btrfs_item_field corrupt_field;
843 corrupt_field = convert_item_field(field);
844 if (corrupt_field == BTRFS_ITEM_BAD) {
845 fprintf(stderr, "Invalid field %s\n", field);
849 path = btrfs_alloc_path();
853 trans = btrfs_start_transaction(root, 1);
855 btrfs_free_path(path);
856 fprintf(stderr, "Couldn't start transaction %ld\n",
858 return PTR_ERR(trans);
861 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
863 fprintf(stderr, "Error searching to node %d\n", ret);
868 switch (corrupt_field) {
869 case BTRFS_ITEM_OFFSET:
870 orig = btrfs_item_offset_nr(path->nodes[0], path->slots[0]);
871 bogus = generate_u32(orig);
872 btrfs_set_item_offset(path->nodes[0],
873 btrfs_item_nr(path->slots[0]), bogus);
879 btrfs_mark_buffer_dirty(path->nodes[0]);
881 btrfs_commit_transaction(trans, root);
882 btrfs_free_path(path);
886 static int delete_item(struct btrfs_root *root, struct btrfs_key *key)
888 struct btrfs_trans_handle *trans;
889 struct btrfs_path *path;
892 path = btrfs_alloc_path();
896 trans = btrfs_start_transaction(root, 1);
898 btrfs_free_path(path);
899 fprintf(stderr, "Couldn't start transaction %ld\n",
901 return PTR_ERR(trans);
904 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
908 fprintf(stderr, "Error searching to node %d\n", ret);
911 ret = btrfs_del_item(trans, root, path);
912 btrfs_mark_buffer_dirty(path->nodes[0]);
914 btrfs_commit_transaction(trans, root);
915 btrfs_free_path(path);
919 static int delete_csum(struct btrfs_root *root, u64 bytenr, u64 bytes)
921 struct btrfs_trans_handle *trans;
924 root = root->fs_info->csum_root;
925 trans = btrfs_start_transaction(root, 1);
927 fprintf(stderr, "Couldn't start transaction %ld\n",
929 return PTR_ERR(trans);
932 ret = btrfs_del_csums(trans, root, bytenr, bytes);
934 fprintf(stderr, "Error deleting csums %d\n", ret);
935 btrfs_commit_transaction(trans, root);
939 /* corrupt item using NO cow.
940 * Because chunk recover will recover based on whole partition scanning,
941 * If using COW, chunk recover will use the old item to recover,
942 * which is still OK but we want to check the ability to rebuild chunk
943 * not only restore the old ones */
944 static int corrupt_item_nocow(struct btrfs_trans_handle *trans,
945 struct btrfs_root *root, struct btrfs_path *path,
949 struct btrfs_key key;
950 struct extent_buffer *leaf;
955 leaf = path->nodes[0];
956 slot = path->slots[0];
957 /* Not deleting the first item of a leaf to keep leaf structure */
960 /* Only accept valid eb */
961 if (slot >= btrfs_header_nritems(leaf)) {
962 error("invalid eb: no data or slot out of range: %d >= %d",
963 slot, btrfs_header_nritems(leaf));
966 btrfs_item_key_to_cpu(leaf, &key, slot);
968 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
969 key.objectid, key.type, key.offset);
970 btrfs_del_item(trans, root, path);
972 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
973 key.objectid, key.type, key.offset);
974 ptr = btrfs_item_ptr_offset(leaf, slot);
975 item_size = btrfs_item_size_nr(leaf, slot);
976 memset_extent_buffer(leaf, 0, ptr, item_size);
977 btrfs_mark_buffer_dirty(leaf);
981 static int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
982 struct btrfs_root *root)
987 struct btrfs_path *path;
988 struct btrfs_key key;
989 struct btrfs_key found_key;
990 struct extent_buffer *leaf;
992 path = btrfs_alloc_path();
996 key.objectid = (u64)-1;
997 key.offset = (u64)-1;
1000 /* Here, cow and ins_len must equals 0 for the following reasons:
1001 * 1) chunk recover is based on disk scanning, so COW should be
1002 * disabled in case the original chunk being scanned and
1003 * recovered using the old chunk.
1004 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
1007 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
1010 fprintf(stderr, "Error searching tree\n");
1013 /* corrupt/del dev_item first */
1014 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
1015 slot = path->slots[0];
1016 leaf = path->nodes[0];
1017 del = rand_range(3);
1018 /* Never delete the first item to keep the leaf structure */
1019 if (path->slots[0] == 0)
1021 ret = corrupt_item_nocow(trans, root, path, del);
1025 btrfs_release_path(path);
1027 /* Here, cow and ins_len must equals 0 for the following reasons:
1028 * 1) chunk recover is based on disk scanning, so COW should be
1029 * disabled in case the original chunk being scanned and
1030 * recovered using the old chunk.
1031 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
1034 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
1037 fprintf(stderr, "Error searching tree\n");
1040 /* corrupt/del chunk then*/
1041 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
1042 slot = path->slots[0];
1043 leaf = path->nodes[0];
1044 del = rand_range(3);
1045 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1046 ret = corrupt_item_nocow(trans, root, path, del);
1051 btrfs_free_path(path);
1054 static int find_chunk_offset(struct btrfs_root *root,
1055 struct btrfs_path *path, u64 offset)
1057 struct btrfs_key key;
1060 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1061 key.type = BTRFS_CHUNK_ITEM_KEY;
1062 key.offset = offset;
1064 /* Here, cow and ins_len must equals 0 for following reasons:
1065 * 1) chunk recover is based on disk scanning, so COW should
1066 * be disabled in case the original chunk being scanned
1067 * and recovered using the old chunk.
1068 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
1069 * will be triggered.
1071 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1073 fprintf(stderr, "Can't find chunk with given offset %llu\n",
1078 fprintf(stderr, "Error searching chunk\n");
1085 int main(int argc, char **argv)
1087 struct cache_tree root_cache;
1088 struct btrfs_key key;
1089 struct btrfs_root *root;
1091 /* chunk offset can be 0,so change to (u64)-1 */
1092 u64 logical = (u64)-1;
1097 int extent_tree = 0;
1098 int corrupt_block_keys = 0;
1101 int corrupt_item = 0;
1104 u64 metadata_block = 0;
1106 u64 file_extent = (u64)-1;
1107 u64 root_objectid = 0;
1108 u64 csum_bytenr = 0;
1109 char field[FIELD_BUF_LEN];
1112 memset(&key, 0, sizeof(key));
1116 static const struct option long_options[] = {
1117 /* { "byte-count", 1, NULL, 'b' }, */
1118 { "logical", required_argument, NULL, 'l' },
1119 { "copy", required_argument, NULL, 'c' },
1120 { "bytes", required_argument, NULL, 'b' },
1121 { "extent-record", no_argument, NULL, 'e' },
1122 { "extent-tree", no_argument, NULL, 'E' },
1123 { "keys", no_argument, NULL, 'k' },
1124 { "chunk-record", no_argument, NULL, 'u' },
1125 { "chunk-tree", no_argument, NULL, 'U' },
1126 { "inode", required_argument, NULL, 'i'},
1127 { "file-extent", required_argument, NULL, 'x'},
1128 { "metadata-block", required_argument, NULL, 'm'},
1129 { "field", required_argument, NULL, 'f'},
1130 { "key", required_argument, NULL, 'K'},
1131 { "item", no_argument, NULL, 'I'},
1132 { "dir-item", no_argument, NULL, 'D'},
1133 { "delete", no_argument, NULL, 'd'},
1134 { "root", no_argument, NULL, 'r'},
1135 { "csum", required_argument, NULL, 'C'},
1136 { "help", no_argument, NULL, GETOPT_VAL_HELP},
1137 { NULL, 0, NULL, 0 }
1140 c = getopt_long(argc, argv, "l:c:b:eEkuUi:f:x:m:K:IDdr:C:",
1141 long_options, NULL);
1146 logical = arg_strtou64(optarg);
1149 copy = arg_strtou64(optarg);
1152 bytes = arg_strtou64(optarg);
1161 corrupt_block_keys = 1;
1170 inode = arg_strtou64(optarg);
1173 strncpy(field, optarg, FIELD_BUF_LEN);
1176 file_extent = arg_strtou64(optarg);
1179 metadata_block = arg_strtou64(optarg);
1182 ret = sscanf(optarg, "%llu,%u,%llu",
1184 (unsigned int *)&key.type,
1187 fprintf(stderr, "error reading key "
1202 root_objectid = arg_strtou64(optarg);
1205 csum_bytenr = arg_strtou64(optarg);
1207 case GETOPT_VAL_HELP:
1209 print_usage(c != GETOPT_VAL_HELP);
1213 if (check_argc_min(argc - optind, 1))
1218 cache_tree_init(&root_cache);
1220 root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
1222 fprintf(stderr, "Open ctree failed\n");
1226 struct btrfs_trans_handle *trans;
1228 if (logical == (u64)-1)
1230 trans = btrfs_start_transaction(root, 1);
1231 ret = corrupt_extent(trans, root, logical);
1232 btrfs_commit_transaction(trans, root);
1236 struct btrfs_trans_handle *trans;
1237 trans = btrfs_start_transaction(root, 1);
1238 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
1239 root->fs_info->extent_root->node);
1240 btrfs_commit_transaction(trans, root);
1244 struct btrfs_trans_handle *trans;
1245 struct btrfs_path *path;
1248 if (logical == (u64)-1)
1250 del = rand_range(3);
1251 path = btrfs_alloc_path();
1253 fprintf(stderr, "path allocation failed\n");
1257 if (find_chunk_offset(root->fs_info->chunk_root, path,
1259 btrfs_free_path(path);
1262 trans = btrfs_start_transaction(root, 1);
1263 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
1266 fprintf(stderr, "Failed to corrupt chunk record\n");
1267 btrfs_commit_transaction(trans, root);
1271 struct btrfs_trans_handle *trans;
1272 trans = btrfs_start_transaction(root, 1);
1273 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
1275 fprintf(stderr, "Failed to corrupt chunk tree\n");
1276 btrfs_commit_transaction(trans, root);
1280 struct btrfs_trans_handle *trans;
1285 trans = btrfs_start_transaction(root, 1);
1286 if (file_extent == (u64)-1) {
1287 printf("corrupting inode\n");
1288 ret = corrupt_inode(trans, root, inode, field);
1290 printf("corrupting file extent\n");
1291 ret = corrupt_file_extent(trans, root, inode,
1292 file_extent, field);
1294 btrfs_commit_transaction(trans, root);
1297 if (metadata_block) {
1300 ret = corrupt_metadata_block(root->fs_info, metadata_block,
1305 if (!key.objectid || *field == 0)
1307 ret = corrupt_dir_item(root, &key, field);
1311 ret = delete_csum(root, csum_bytenr, bytes);
1317 ret = corrupt_btrfs_item(root, &key, field);
1320 struct btrfs_root *target = root;
1324 if (root_objectid) {
1325 struct btrfs_key root_key;
1327 root_key.objectid = root_objectid;
1328 root_key.type = BTRFS_ROOT_ITEM_KEY;
1329 root_key.offset = (u64)-1;
1331 target = btrfs_read_fs_root(root->fs_info, &root_key);
1332 if (IS_ERR(target)) {
1333 fprintf(stderr, "Couldn't find root %llu\n",
1334 (unsigned long long)root_objectid);
1338 ret = delete_item(target, &key);
1341 if (key.objectid || key.offset || key.type) {
1344 ret = corrupt_key(root, &key, field);
1348 * If we made it here and we have extent set then we didn't specify
1349 * inode and we're screwed.
1351 if (file_extent != (u64)-1)
1354 if (logical == (u64)-1)
1358 bytes = root->fs_info->sectorsize;
1360 bytes = round_up(bytes, root->fs_info->sectorsize);
1363 if (corrupt_block_keys) {
1364 corrupt_keys_in_block(root->fs_info, logical);
1366 struct extent_buffer *eb;
1368 eb = btrfs_find_create_tree_block(root->fs_info,
1369 logical, root->fs_info->sectorsize);
1372 "not enough memory to allocate extent buffer for bytenr %llu",
1373 (unsigned long long)logical);
1378 debug_corrupt_block(eb, root, logical,
1379 root->fs_info->sectorsize, copy);
1380 free_extent_buffer(eb);
1382 logical += root->fs_info->sectorsize;
1383 bytes -= root->fs_info->sectorsize;