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-f The field in the item to corrupt\n");
108 static void corrupt_keys(struct btrfs_trans_handle *trans,
109 struct btrfs_root *root,
110 struct extent_buffer *eb)
115 struct btrfs_disk_key bad_key;;
117 nr = btrfs_header_nritems(eb);
122 bad_slot = rand() % nr;
124 if (bad_slot == slot)
128 "corrupting keys in block %llu slot %d swapping with %d\n",
129 (unsigned long long)eb->start, slot, bad_slot);
131 if (btrfs_header_level(eb) == 0) {
132 btrfs_item_key(eb, &bad_key, bad_slot);
133 btrfs_set_item_key(eb, &bad_key, slot);
135 btrfs_node_key(eb, &bad_key, bad_slot);
136 btrfs_set_node_key(eb, &bad_key, slot);
138 btrfs_mark_buffer_dirty(eb);
140 csum_tree_block(root, eb, 0);
141 write_extent_to_disk(eb);
146 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
148 struct extent_buffer *eb;
150 eb = read_tree_block(root, bytenr, root->leafsize, 0);
154 corrupt_keys(NULL, root, eb);
155 free_extent_buffer(eb);
159 static int corrupt_extent(struct btrfs_trans_handle *trans,
160 struct btrfs_root *root, u64 bytenr, int copy)
162 struct btrfs_key key;
163 struct extent_buffer *leaf;
166 struct btrfs_path *path;
169 int should_del = rand() % 3;
171 path = btrfs_alloc_path();
175 key.objectid = bytenr;
177 key.offset = (u64)-1;
180 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
186 if (path->slots[0] == 0)
191 leaf = path->nodes[0];
192 slot = path->slots[0];
193 btrfs_item_key_to_cpu(leaf, &key, slot);
194 if (key.objectid != bytenr)
197 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
198 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
199 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
200 key.type != BTRFS_EXTENT_REF_V0_KEY &&
201 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
202 key.type != BTRFS_SHARED_DATA_REF_KEY)
207 "deleting extent record: key %llu %u %llu\n",
208 key.objectid, key.type, key.offset);
210 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
211 /* make sure this extent doesn't get
212 * reused for other purposes */
213 btrfs_pin_extent(root->fs_info,
214 key.objectid, key.offset);
217 btrfs_del_item(trans, root, path);
220 "corrupting extent record: key %llu %u %llu\n",
221 key.objectid, key.type, key.offset);
222 ptr = btrfs_item_ptr_offset(leaf, slot);
223 item_size = btrfs_item_size_nr(leaf, slot);
224 memset_extent_buffer(leaf, 0, ptr, item_size);
225 btrfs_mark_buffer_dirty(leaf);
228 btrfs_release_path(path);
236 btrfs_free_path(path);
240 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
241 struct btrfs_root *root,
242 struct extent_buffer *eb)
244 u32 nr = btrfs_header_nritems(eb);
245 u32 victim = rand() % nr;
247 struct btrfs_key key;
249 btrfs_item_key_to_cpu(eb, &key, victim);
250 objectid = key.objectid;
251 corrupt_extent(trans, root, objectid, 1);
254 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
255 struct btrfs_root *root,
256 struct extent_buffer *eb)
264 nr = btrfs_header_nritems(eb);
265 if (btrfs_is_leaf(eb)) {
266 btrfs_corrupt_extent_leaf(trans, root, eb);
270 if (btrfs_header_level(eb) == 1 && eb != root->node) {
275 for (i = 0; i < nr; i++) {
276 struct extent_buffer *next;
278 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
280 btrfs_node_ptr_generation(eb, i));
283 btrfs_corrupt_extent_tree(trans, root, next);
284 free_extent_buffer(next);
288 enum btrfs_inode_field {
289 BTRFS_INODE_FIELD_ISIZE,
290 BTRFS_INODE_FIELD_BAD,
293 static enum btrfs_inode_field convert_field(char *field)
295 if (!strncmp(field, "isize", FIELD_BUF_LEN))
296 return BTRFS_INODE_FIELD_ISIZE;
297 return BTRFS_INODE_FIELD_BAD;
300 static int corrupt_inode(struct btrfs_trans_handle *trans,
301 struct btrfs_root *root, u64 inode, char *field)
303 struct btrfs_inode_item *ei;
304 struct btrfs_path *path;
305 struct btrfs_key key;
306 enum btrfs_inode_field corrupt_field = convert_field(field);
311 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
312 fprintf(stderr, "Invalid field %s\n", field);
316 key.objectid = inode;
317 key.type = BTRFS_INODE_ITEM_KEY;
318 key.offset = (u64)-1;
320 path = btrfs_alloc_path();
324 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
328 if (!path->slots[0]) {
329 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
337 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
338 if (key.objectid != inode) {
339 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
344 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
345 struct btrfs_inode_item);
346 switch (corrupt_field) {
347 case BTRFS_INODE_FIELD_ISIZE:
348 orig = btrfs_inode_size(path->nodes[0], ei);
351 } while (bogus == orig);
353 btrfs_set_inode_size(path->nodes[0], ei, bogus);
359 btrfs_mark_buffer_dirty(path->nodes[0]);
361 btrfs_free_path(path);
365 static struct option long_options[] = {
366 /* { "byte-count", 1, NULL, 'b' }, */
367 { "logical", 1, NULL, 'l' },
368 { "copy", 1, NULL, 'c' },
369 { "bytes", 1, NULL, 'b' },
370 { "extent-record", 0, NULL, 'e' },
371 { "extent-tree", 0, NULL, 'E' },
372 { "keys", 0, NULL, 'k' },
373 { "chunk-record", 0, NULL, 'u' },
374 { "chunk-tree", 0, NULL, 'U' },
375 { "inode", 1, NULL, 'i'},
376 { "field", 1, NULL, 'f'},
380 /* corrupt item using NO cow.
381 * Because chunk recover will recover based on whole partition scaning,
382 * If using COW, chunk recover will use the old item to recover,
383 * which is still OK but we want to check the ability to rebuild chunk
384 * not only restore the old ones */
385 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
386 struct btrfs_root *root, struct btrfs_path *path,
390 struct btrfs_key key;
391 struct extent_buffer *leaf;
396 leaf = path->nodes[0];
397 slot = path->slots[0];
398 /* Not deleting the first item of a leaf to keep leaf structure */
401 /* Only accept valid eb */
402 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
403 btrfs_item_key_to_cpu(leaf, &key, slot);
405 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
406 key.objectid, key.type, key.offset);
407 btrfs_del_item(trans, root, path);
409 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
410 key.objectid, key.type, key.offset);
411 ptr = btrfs_item_ptr_offset(leaf, slot);
412 item_size = btrfs_item_size_nr(leaf, slot);
413 memset_extent_buffer(leaf, 0, ptr, item_size);
414 btrfs_mark_buffer_dirty(leaf);
418 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
419 struct btrfs_root *root)
424 struct btrfs_path *path;
425 struct btrfs_key key;
426 struct btrfs_key found_key;
427 struct extent_buffer *leaf;
429 path = btrfs_alloc_path();
430 key.objectid = (u64)-1;
431 key.offset = (u64)-1;
434 /* Here, cow and ins_len must equals 0 for the following reasons:
435 * 1) chunk recover is based on disk scanning, so COW should be
436 * disabled in case the original chunk being scanned and
437 * recovered using the old chunk.
438 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
441 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
444 fprintf(stderr, "Error searching tree\n");
447 /* corrupt/del dev_item first */
448 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
449 slot = path->slots[0];
450 leaf = path->nodes[0];
452 /* Never delete the first item to keep the leaf structure */
453 if (path->slots[0] == 0)
455 ret = corrupt_item_nocow(trans, root, path, del);
459 btrfs_free_path(path);
461 /* Here, cow and ins_len must equals 0 for the following reasons:
462 * 1) chunk recover is based on disk scanning, so COW should be
463 * disabled in case the original chunk being scanned and
464 * recovered using the old chunk.
465 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
468 path = btrfs_alloc_path();
469 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
472 fprintf(stderr, "Error searching tree\n");
475 /* corrupt/del chunk then*/
476 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
477 slot = path->slots[0];
478 leaf = path->nodes[0];
480 btrfs_item_key_to_cpu(leaf, &found_key, slot);
481 ret = corrupt_item_nocow(trans, root, path, del);
486 btrfs_free_path(path);
489 int find_chunk_offset(struct btrfs_root *root,
490 struct btrfs_path *path, u64 offset)
492 struct btrfs_key key;
495 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
496 key.type = BTRFS_CHUNK_ITEM_KEY;
499 /* Here, cow and ins_len must equals 0 for following reasons:
500 * 1) chunk recover is based on disk scanning, so COW should
501 * be disabled in case the original chunk being scanned
502 * and recovered using the old chunk.
503 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
506 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
508 fprintf(stderr, "Can't find chunk with given offset %llu\n",
513 fprintf(stderr, "Error searching chunk");
520 int main(int ac, char **av)
522 struct cache_tree root_cache;
523 struct btrfs_root *root;
524 struct extent_buffer *eb;
526 /* chunk offset can be 0,so change to (u64)-1 */
527 u64 logical = (u64)-1;
529 int option_index = 0;
534 int corrupt_block_keys = 0;
538 char field[FIELD_BUF_LEN];
545 c = getopt_long(ac, av, "l:c:b:eEkuUi:f:", long_options,
551 logical = atoll(optarg);
557 "invalid copy number\n");
562 bytes = atoll(optarg);
565 "invalid byte count\n");
576 corrupt_block_keys = 1;
584 inode = atoll(optarg);
587 "invalid inode number\n");
592 strncpy(field, optarg, FIELD_BUF_LEN);
604 cache_tree_init(&root_cache);
606 root = open_ctree(dev, 0, 1);
608 fprintf(stderr, "Open ctree failed\n");
612 struct btrfs_trans_handle *trans;
614 if (logical == (u64)-1)
616 trans = btrfs_start_transaction(root, 1);
617 ret = corrupt_extent (trans, root, logical, 0);
618 btrfs_commit_transaction(trans, root);
622 struct btrfs_trans_handle *trans;
623 trans = btrfs_start_transaction(root, 1);
624 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
625 root->fs_info->extent_root->node);
626 btrfs_commit_transaction(trans, root);
630 struct btrfs_trans_handle *trans;
631 struct btrfs_path *path;
634 if (logical == (u64)-1)
637 path = btrfs_alloc_path();
639 if (find_chunk_offset(root->fs_info->chunk_root, path,
641 btrfs_free_path(path);
644 trans = btrfs_start_transaction(root, 1);
645 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
648 fprintf(stderr, "Failed to corrupt chunk record\n");
649 btrfs_commit_transaction(trans, root);
653 struct btrfs_trans_handle *trans;
654 trans = btrfs_start_transaction(root, 1);
655 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
657 fprintf(stderr, "Failed to corrupt chunk tree\n");
658 btrfs_commit_transaction(trans, root);
662 struct btrfs_trans_handle *trans;
666 printf("corrupting inode\n");
667 trans = btrfs_start_transaction(root, 1);
668 ret = corrupt_inode(trans, root, inode, field);
669 btrfs_commit_transaction(trans, root);
673 if (logical == (u64)-1)
677 bytes = root->sectorsize;
679 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
680 bytes *= root->sectorsize;
683 if (corrupt_block_keys) {
684 corrupt_keys_in_block(root, logical);
686 eb = debug_corrupt_block(root, logical,
687 root->sectorsize, copy);
688 free_extent_buffer(eb);
690 logical += root->sectorsize;
691 bytes -= root->sectorsize;