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, int 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-f The field in the item to corrupt\n");
110 static void corrupt_keys(struct btrfs_trans_handle *trans,
111 struct btrfs_root *root,
112 struct extent_buffer *eb)
117 struct btrfs_disk_key bad_key;;
119 nr = btrfs_header_nritems(eb);
124 bad_slot = rand() % nr;
126 if (bad_slot == slot)
130 "corrupting keys in block %llu slot %d swapping with %d\n",
131 (unsigned long long)eb->start, slot, bad_slot);
133 if (btrfs_header_level(eb) == 0) {
134 btrfs_item_key(eb, &bad_key, bad_slot);
135 btrfs_set_item_key(eb, &bad_key, slot);
137 btrfs_node_key(eb, &bad_key, bad_slot);
138 btrfs_set_node_key(eb, &bad_key, slot);
140 btrfs_mark_buffer_dirty(eb);
142 csum_tree_block(root, eb, 0);
143 write_extent_to_disk(eb);
148 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
150 struct extent_buffer *eb;
152 eb = read_tree_block(root, bytenr, root->leafsize, 0);
156 corrupt_keys(NULL, root, eb);
157 free_extent_buffer(eb);
161 static int corrupt_extent(struct btrfs_trans_handle *trans,
162 struct btrfs_root *root, u64 bytenr, int copy)
164 struct btrfs_key key;
165 struct extent_buffer *leaf;
168 struct btrfs_path *path;
171 int should_del = rand() % 3;
173 path = btrfs_alloc_path();
177 key.objectid = bytenr;
179 key.offset = (u64)-1;
182 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
188 if (path->slots[0] == 0)
193 leaf = path->nodes[0];
194 slot = path->slots[0];
195 btrfs_item_key_to_cpu(leaf, &key, slot);
196 if (key.objectid != bytenr)
199 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
200 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
201 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
202 key.type != BTRFS_EXTENT_REF_V0_KEY &&
203 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
204 key.type != BTRFS_SHARED_DATA_REF_KEY)
209 "deleting extent record: key %llu %u %llu\n",
210 key.objectid, key.type, key.offset);
212 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
213 /* make sure this extent doesn't get
214 * reused for other purposes */
215 btrfs_pin_extent(root->fs_info,
216 key.objectid, key.offset);
219 btrfs_del_item(trans, root, path);
222 "corrupting extent record: key %llu %u %llu\n",
223 key.objectid, key.type, key.offset);
224 ptr = btrfs_item_ptr_offset(leaf, slot);
225 item_size = btrfs_item_size_nr(leaf, slot);
226 memset_extent_buffer(leaf, 0, ptr, item_size);
227 btrfs_mark_buffer_dirty(leaf);
230 btrfs_release_path(path);
238 btrfs_free_path(path);
242 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
243 struct btrfs_root *root,
244 struct extent_buffer *eb)
246 u32 nr = btrfs_header_nritems(eb);
247 u32 victim = rand() % nr;
249 struct btrfs_key key;
251 btrfs_item_key_to_cpu(eb, &key, victim);
252 objectid = key.objectid;
253 corrupt_extent(trans, root, objectid, 1);
256 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
257 struct btrfs_root *root,
258 struct extent_buffer *eb)
266 nr = btrfs_header_nritems(eb);
267 if (btrfs_is_leaf(eb)) {
268 btrfs_corrupt_extent_leaf(trans, root, eb);
272 if (btrfs_header_level(eb) == 1 && eb != root->node) {
277 for (i = 0; i < nr; i++) {
278 struct extent_buffer *next;
280 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
282 btrfs_node_ptr_generation(eb, i));
285 btrfs_corrupt_extent_tree(trans, root, next);
286 free_extent_buffer(next);
290 enum btrfs_inode_field {
291 BTRFS_INODE_FIELD_ISIZE,
292 BTRFS_INODE_FIELD_BAD,
295 enum btrfs_file_extent_field {
296 BTRFS_FILE_EXTENT_DISK_BYTENR,
297 BTRFS_FILE_EXTENT_BAD,
300 static enum btrfs_inode_field convert_inode_field(char *field)
302 if (!strncmp(field, "isize", FIELD_BUF_LEN))
303 return BTRFS_INODE_FIELD_ISIZE;
304 return BTRFS_INODE_FIELD_BAD;
307 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
309 if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
310 return BTRFS_FILE_EXTENT_DISK_BYTENR;
311 return BTRFS_FILE_EXTENT_BAD;
314 static u64 generate_u64(u64 orig)
319 } while (ret == orig);
323 static int corrupt_inode(struct btrfs_trans_handle *trans,
324 struct btrfs_root *root, u64 inode, char *field)
326 struct btrfs_inode_item *ei;
327 struct btrfs_path *path;
328 struct btrfs_key key;
329 enum btrfs_inode_field corrupt_field = convert_inode_field(field);
334 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
335 fprintf(stderr, "Invalid field %s\n", field);
339 key.objectid = inode;
340 key.type = BTRFS_INODE_ITEM_KEY;
341 key.offset = (u64)-1;
343 path = btrfs_alloc_path();
347 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
351 if (!path->slots[0]) {
352 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
360 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
361 if (key.objectid != inode) {
362 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
367 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
368 struct btrfs_inode_item);
369 switch (corrupt_field) {
370 case BTRFS_INODE_FIELD_ISIZE:
371 orig = btrfs_inode_size(path->nodes[0], ei);
372 bogus = generate_u64(orig);
373 btrfs_set_inode_size(path->nodes[0], ei, bogus);
379 btrfs_mark_buffer_dirty(path->nodes[0]);
381 btrfs_free_path(path);
385 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
386 struct btrfs_root *root, u64 inode, u64 extent,
389 struct btrfs_file_extent_item *fi;
390 struct btrfs_path *path;
391 struct btrfs_key key;
392 enum btrfs_file_extent_field corrupt_field;
397 corrupt_field = convert_file_extent_field(field);
398 if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
399 fprintf(stderr, "Invalid field %s\n", field);
403 key.objectid = inode;
404 key.type = BTRFS_EXTENT_DATA_KEY;
407 path = btrfs_alloc_path();
411 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
415 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
421 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
422 struct btrfs_file_extent_item);
423 switch (corrupt_field) {
424 case BTRFS_FILE_EXTENT_DISK_BYTENR:
425 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
426 bogus = generate_u64(orig);
427 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
433 btrfs_mark_buffer_dirty(path->nodes[0]);
435 btrfs_free_path(path);
439 static struct option long_options[] = {
440 /* { "byte-count", 1, NULL, 'b' }, */
441 { "logical", 1, NULL, 'l' },
442 { "copy", 1, NULL, 'c' },
443 { "bytes", 1, NULL, 'b' },
444 { "extent-record", 0, NULL, 'e' },
445 { "extent-tree", 0, NULL, 'E' },
446 { "keys", 0, NULL, 'k' },
447 { "chunk-record", 0, NULL, 'u' },
448 { "chunk-tree", 0, NULL, 'U' },
449 { "inode", 1, NULL, 'i'},
450 { "file-extent", 1, NULL, 'x'},
451 { "field", 1, NULL, 'f'},
455 /* corrupt item using NO cow.
456 * Because chunk recover will recover based on whole partition scaning,
457 * If using COW, chunk recover will use the old item to recover,
458 * which is still OK but we want to check the ability to rebuild chunk
459 * not only restore the old ones */
460 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
461 struct btrfs_root *root, struct btrfs_path *path,
465 struct btrfs_key key;
466 struct extent_buffer *leaf;
471 leaf = path->nodes[0];
472 slot = path->slots[0];
473 /* Not deleting the first item of a leaf to keep leaf structure */
476 /* Only accept valid eb */
477 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
478 btrfs_item_key_to_cpu(leaf, &key, slot);
480 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
481 key.objectid, key.type, key.offset);
482 btrfs_del_item(trans, root, path);
484 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
485 key.objectid, key.type, key.offset);
486 ptr = btrfs_item_ptr_offset(leaf, slot);
487 item_size = btrfs_item_size_nr(leaf, slot);
488 memset_extent_buffer(leaf, 0, ptr, item_size);
489 btrfs_mark_buffer_dirty(leaf);
493 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
494 struct btrfs_root *root)
499 struct btrfs_path *path;
500 struct btrfs_key key;
501 struct btrfs_key found_key;
502 struct extent_buffer *leaf;
504 path = btrfs_alloc_path();
505 key.objectid = (u64)-1;
506 key.offset = (u64)-1;
509 /* Here, cow and ins_len must equals 0 for the following reasons:
510 * 1) chunk recover is based on disk scanning, so COW should be
511 * disabled in case the original chunk being scanned and
512 * recovered using the old chunk.
513 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
516 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
519 fprintf(stderr, "Error searching tree\n");
522 /* corrupt/del dev_item first */
523 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
524 slot = path->slots[0];
525 leaf = path->nodes[0];
527 /* Never delete the first item to keep the leaf structure */
528 if (path->slots[0] == 0)
530 ret = corrupt_item_nocow(trans, root, path, del);
534 btrfs_free_path(path);
536 /* Here, cow and ins_len must equals 0 for the following reasons:
537 * 1) chunk recover is based on disk scanning, so COW should be
538 * disabled in case the original chunk being scanned and
539 * recovered using the old chunk.
540 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
543 path = btrfs_alloc_path();
544 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
547 fprintf(stderr, "Error searching tree\n");
550 /* corrupt/del chunk then*/
551 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
552 slot = path->slots[0];
553 leaf = path->nodes[0];
555 btrfs_item_key_to_cpu(leaf, &found_key, slot);
556 ret = corrupt_item_nocow(trans, root, path, del);
561 btrfs_free_path(path);
564 int find_chunk_offset(struct btrfs_root *root,
565 struct btrfs_path *path, u64 offset)
567 struct btrfs_key key;
570 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
571 key.type = BTRFS_CHUNK_ITEM_KEY;
574 /* Here, cow and ins_len must equals 0 for following reasons:
575 * 1) chunk recover is based on disk scanning, so COW should
576 * be disabled in case the original chunk being scanned
577 * and recovered using the old chunk.
578 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
581 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
583 fprintf(stderr, "Can't find chunk with given offset %llu\n",
588 fprintf(stderr, "Error searching chunk");
595 int main(int ac, char **av)
597 struct cache_tree root_cache;
598 struct btrfs_root *root;
599 struct extent_buffer *eb;
601 /* chunk offset can be 0,so change to (u64)-1 */
602 u64 logical = (u64)-1;
604 int option_index = 0;
609 int corrupt_block_keys = 0;
613 u64 file_extent = (u64)-1;
614 char field[FIELD_BUF_LEN];
621 c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:", long_options,
627 logical = atoll(optarg);
633 "invalid copy number\n");
638 bytes = atoll(optarg);
641 "invalid byte count\n");
652 corrupt_block_keys = 1;
660 inode = atoll(optarg);
663 "invalid inode number\n");
668 strncpy(field, optarg, FIELD_BUF_LEN);
672 file_extent = atoll(optarg);
674 fprintf(stderr, "error converting "
689 cache_tree_init(&root_cache);
691 root = open_ctree(dev, 0, 1);
693 fprintf(stderr, "Open ctree failed\n");
697 struct btrfs_trans_handle *trans;
699 if (logical == (u64)-1)
701 trans = btrfs_start_transaction(root, 1);
702 ret = corrupt_extent (trans, root, logical, 0);
703 btrfs_commit_transaction(trans, root);
707 struct btrfs_trans_handle *trans;
708 trans = btrfs_start_transaction(root, 1);
709 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
710 root->fs_info->extent_root->node);
711 btrfs_commit_transaction(trans, root);
715 struct btrfs_trans_handle *trans;
716 struct btrfs_path *path;
719 if (logical == (u64)-1)
722 path = btrfs_alloc_path();
724 if (find_chunk_offset(root->fs_info->chunk_root, path,
726 btrfs_free_path(path);
729 trans = btrfs_start_transaction(root, 1);
730 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
733 fprintf(stderr, "Failed to corrupt chunk record\n");
734 btrfs_commit_transaction(trans, root);
738 struct btrfs_trans_handle *trans;
739 trans = btrfs_start_transaction(root, 1);
740 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
742 fprintf(stderr, "Failed to corrupt chunk tree\n");
743 btrfs_commit_transaction(trans, root);
747 struct btrfs_trans_handle *trans;
752 trans = btrfs_start_transaction(root, 1);
753 if (file_extent == (u64)-1) {
754 printf("corrupting inode\n");
755 ret = corrupt_inode(trans, root, inode, field);
757 printf("corrupting file extent\n");
758 ret = corrupt_file_extent(trans, root, inode,
761 btrfs_commit_transaction(trans, root);
766 * If we made it here and we have extent set then we didn't specify
767 * inode and we're screwed.
769 if (file_extent != (u64)-1)
772 if (logical == (u64)-1)
776 bytes = root->sectorsize;
778 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
779 bytes *= root->sectorsize;
782 if (corrupt_block_keys) {
783 corrupt_keys_in_block(root, logical);
785 eb = debug_corrupt_block(root, logical,
786 root->sectorsize, copy);
787 free_extent_buffer(eb);
789 logical += root->sectorsize;
790 bytes -= root->sectorsize;