btrfs-progs: fix wrong data ratio for raid56 in btrfs-file-usage
[btrfs-progs-unstable/devel.git] / btrfs-corrupt-block.c
blobaf9ae4d4047c1a91a3a65bf28e5fadf65a3e6b24
1 /*
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
20 #define _GNU_SOURCE 1
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include "kerncompat.h"
27 #include "ctree.h"
28 #include "volumes.h"
29 #include "disk-io.h"
30 #include "print-tree.h"
31 #include "transaction.h"
32 #include "list.h"
33 #include "version.h"
34 #include "utils.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)
41 int ret;
42 struct extent_buffer *eb;
43 u64 length;
44 struct btrfs_multi_bio *multi = NULL;
45 struct btrfs_device *device;
46 int num_copies;
47 int mirror_num = 1;
49 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
50 if (!eb)
51 return NULL;
53 length = blocksize;
54 while (1) {
55 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
56 eb->start, &length, &multi,
57 mirror_num, NULL);
58 BUG_ON(ret);
59 device = multi->stripes[0].dev;
60 eb->fd = device->fd;
61 device->total_ios++;
62 eb->dev_bytenr = multi->stripes[0].physical;
64 fprintf(stdout,
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);
68 kfree(multi);
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,
73 mirror_num);
74 memset(eb->data, 0, eb->len);
75 write_extent_to_disk(eb);
76 fsync(eb->fd);
79 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
80 eb->start, eb->len);
81 if (num_copies == 1)
82 break;
84 mirror_num++;
85 if (mirror_num > num_copies)
86 break;
88 return eb;
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-I An item to corrupt (must also specify the field "
112 "to corrupt and a root+key for the item)\n");
113 fprintf(stderr, "\t-D Corrupt a dir item, must specify key and field\n");
114 fprintf(stderr, "\t-d Delete this item (must specify -K)\n");
115 exit(1);
118 static void corrupt_keys(struct btrfs_trans_handle *trans,
119 struct btrfs_root *root,
120 struct extent_buffer *eb)
122 int slot;
123 int bad_slot;
124 int nr;
125 struct btrfs_disk_key bad_key;;
127 nr = btrfs_header_nritems(eb);
128 if (nr == 0)
129 return;
131 slot = rand() % nr;
132 bad_slot = rand() % nr;
134 if (bad_slot == slot)
135 return;
137 fprintf(stderr,
138 "corrupting keys in block %llu slot %d swapping with %d\n",
139 (unsigned long long)eb->start, slot, bad_slot);
141 if (btrfs_header_level(eb) == 0) {
142 btrfs_item_key(eb, &bad_key, bad_slot);
143 btrfs_set_item_key(eb, &bad_key, slot);
144 } else {
145 btrfs_node_key(eb, &bad_key, bad_slot);
146 btrfs_set_node_key(eb, &bad_key, slot);
148 btrfs_mark_buffer_dirty(eb);
149 if (!trans) {
150 u16 csum_size =
151 btrfs_super_csum_size(root->fs_info->super_copy);
152 csum_tree_block_size(eb, csum_size, 0);
153 write_extent_to_disk(eb);
158 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
160 struct extent_buffer *eb;
162 eb = read_tree_block(root, bytenr, root->leafsize, 0);
163 if (!eb)
164 return -EIO;;
166 corrupt_keys(NULL, root, eb);
167 free_extent_buffer(eb);
168 return 0;
171 static int corrupt_extent(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root, u64 bytenr, u64 copy)
174 struct btrfs_key key;
175 struct extent_buffer *leaf;
176 u32 item_size;
177 unsigned long ptr;
178 struct btrfs_path *path;
179 int ret;
180 int slot;
181 int should_del = rand() % 3;
183 path = btrfs_alloc_path();
184 if (!path)
185 return -ENOMEM;
187 key.objectid = bytenr;
188 key.type = (u8)-1;
189 key.offset = (u64)-1;
191 while(1) {
192 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
193 &key, path, -1, 1);
194 if (ret < 0)
195 break;
197 if (ret > 0) {
198 if (path->slots[0] == 0)
199 break;
200 path->slots[0]--;
201 ret = 0;
203 leaf = path->nodes[0];
204 slot = path->slots[0];
205 btrfs_item_key_to_cpu(leaf, &key, slot);
206 if (key.objectid != bytenr)
207 break;
209 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
210 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
211 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
212 key.type != BTRFS_EXTENT_REF_V0_KEY &&
213 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
214 key.type != BTRFS_SHARED_DATA_REF_KEY)
215 goto next;
217 if (should_del) {
218 fprintf(stderr,
219 "deleting extent record: key %llu %u %llu\n",
220 key.objectid, key.type, key.offset);
222 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
223 /* make sure this extent doesn't get
224 * reused for other purposes */
225 btrfs_pin_extent(root->fs_info,
226 key.objectid, key.offset);
229 btrfs_del_item(trans, root, path);
230 } else {
231 fprintf(stderr,
232 "corrupting extent record: key %llu %u %llu\n",
233 key.objectid, key.type, key.offset);
234 ptr = btrfs_item_ptr_offset(leaf, slot);
235 item_size = btrfs_item_size_nr(leaf, slot);
236 memset_extent_buffer(leaf, 0, ptr, item_size);
237 btrfs_mark_buffer_dirty(leaf);
239 next:
240 btrfs_release_path(path);
242 if (key.offset > 0)
243 key.offset--;
244 if (key.offset == 0)
245 break;
248 btrfs_free_path(path);
249 return 0;
252 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
253 struct btrfs_root *root,
254 struct extent_buffer *eb)
256 u32 nr = btrfs_header_nritems(eb);
257 u32 victim = rand() % nr;
258 u64 objectid;
259 struct btrfs_key key;
261 btrfs_item_key_to_cpu(eb, &key, victim);
262 objectid = key.objectid;
263 corrupt_extent(trans, root, objectid, 1);
266 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
267 struct btrfs_root *root,
268 struct extent_buffer *eb)
270 int i;
272 if (!eb)
273 return;
275 if (btrfs_is_leaf(eb)) {
276 btrfs_corrupt_extent_leaf(trans, root, eb);
277 return;
280 if (btrfs_header_level(eb) == 1 && eb != root->node) {
281 if (rand() % 5)
282 return;
285 for (i = 0; i < btrfs_header_nritems(eb); i++) {
286 struct extent_buffer *next;
288 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
289 root->leafsize,
290 btrfs_node_ptr_generation(eb, i));
291 if (!next)
292 continue;
293 btrfs_corrupt_extent_tree(trans, root, next);
294 free_extent_buffer(next);
298 enum btrfs_inode_field {
299 BTRFS_INODE_FIELD_ISIZE,
300 BTRFS_INODE_FIELD_BAD,
303 enum btrfs_file_extent_field {
304 BTRFS_FILE_EXTENT_DISK_BYTENR,
305 BTRFS_FILE_EXTENT_BAD,
308 enum btrfs_dir_item_field {
309 BTRFS_DIR_ITEM_NAME,
310 BTRFS_DIR_ITEM_LOCATION_OBJECTID,
311 BTRFS_DIR_ITEM_BAD,
314 enum btrfs_metadata_block_field {
315 BTRFS_METADATA_BLOCK_GENERATION,
316 BTRFS_METADATA_BLOCK_SHIFT_ITEMS,
317 BTRFS_METADATA_BLOCK_BAD,
320 enum btrfs_item_field {
321 BTRFS_ITEM_OFFSET,
322 BTRFS_ITEM_BAD,
325 enum btrfs_key_field {
326 BTRFS_KEY_OBJECTID,
327 BTRFS_KEY_TYPE,
328 BTRFS_KEY_OFFSET,
329 BTRFS_KEY_BAD,
332 static enum btrfs_inode_field convert_inode_field(char *field)
334 if (!strncmp(field, "isize", FIELD_BUF_LEN))
335 return BTRFS_INODE_FIELD_ISIZE;
336 return BTRFS_INODE_FIELD_BAD;
339 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
341 if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
342 return BTRFS_FILE_EXTENT_DISK_BYTENR;
343 return BTRFS_FILE_EXTENT_BAD;
346 static enum btrfs_metadata_block_field
347 convert_metadata_block_field(char *field)
349 if (!strncmp(field, "generation", FIELD_BUF_LEN))
350 return BTRFS_METADATA_BLOCK_GENERATION;
351 if (!strncmp(field, "shift_items", FIELD_BUF_LEN))
352 return BTRFS_METADATA_BLOCK_SHIFT_ITEMS;
353 return BTRFS_METADATA_BLOCK_BAD;
356 static enum btrfs_key_field convert_key_field(char *field)
358 if (!strncmp(field, "objectid", FIELD_BUF_LEN))
359 return BTRFS_KEY_OBJECTID;
360 if (!strncmp(field, "type", FIELD_BUF_LEN))
361 return BTRFS_KEY_TYPE;
362 if (!strncmp(field, "offset", FIELD_BUF_LEN))
363 return BTRFS_KEY_OFFSET;
364 return BTRFS_KEY_BAD;
367 static enum btrfs_item_field convert_item_field(char *field)
369 if (!strncmp(field, "offset", FIELD_BUF_LEN))
370 return BTRFS_ITEM_OFFSET;
371 return BTRFS_ITEM_BAD;
374 static enum btrfs_dir_item_field convert_dir_item_field(char *field)
376 if (!strncmp(field, "name", FIELD_BUF_LEN))
377 return BTRFS_DIR_ITEM_NAME;
378 if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
379 return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
380 return BTRFS_DIR_ITEM_BAD;
383 static u64 generate_u64(u64 orig)
385 u64 ret;
386 do {
387 ret = rand();
388 } while (ret == orig);
389 return ret;
392 static u32 generate_u32(u32 orig)
394 u32 ret;
395 do {
396 ret = rand();
397 } while (ret == orig);
398 return ret;
401 static u8 generate_u8(u8 orig)
403 u8 ret;
404 do {
405 ret = rand();
406 } while (ret == orig);
407 return ret;
410 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
411 char *field)
413 enum btrfs_key_field corrupt_field = convert_key_field(field);
414 struct btrfs_path *path;
415 struct btrfs_trans_handle *trans;
416 int ret;
418 root = root->fs_info->fs_root;
419 if (corrupt_field == BTRFS_KEY_BAD) {
420 fprintf(stderr, "Invalid field %s\n", field);
421 return -EINVAL;
424 path = btrfs_alloc_path();
425 if (!path)
426 return -ENOMEM;
428 trans = btrfs_start_transaction(root, 1);
429 if (IS_ERR(trans)) {
430 btrfs_free_path(path);
431 return PTR_ERR(trans);
434 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
435 if (ret < 0)
436 goto out;
437 if (ret > 0) {
438 fprintf(stderr, "Couldn't find the key to corrupt\n");
439 ret = -ENOENT;
440 goto out;
443 switch (corrupt_field) {
444 case BTRFS_KEY_OBJECTID:
445 key->objectid = generate_u64(key->objectid);
446 break;
447 case BTRFS_KEY_TYPE:
448 key->type = generate_u8(key->type);
449 break;
450 case BTRFS_KEY_OFFSET:
451 key->offset = generate_u64(key->objectid);
452 break;
453 default:
454 fprintf(stderr, "Invalid field %s, %d\n", field,
455 corrupt_field);
456 ret = -EINVAL;
457 goto out;
460 btrfs_set_item_key_unsafe(root, path, key);
461 out:
462 btrfs_free_path(path);
463 btrfs_commit_transaction(trans, root);
464 return ret;
467 static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
468 char *field)
470 struct btrfs_trans_handle *trans;
471 struct btrfs_dir_item *di;
472 struct btrfs_path *path;
473 char *name;
474 struct btrfs_key location;
475 struct btrfs_disk_key disk_key;
476 unsigned long name_ptr;
477 enum btrfs_dir_item_field corrupt_field =
478 convert_dir_item_field(field);
479 u64 bogus;
480 u16 name_len;
481 int ret;
483 if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
484 fprintf(stderr, "Invalid field %s\n", field);
485 return -EINVAL;
488 path = btrfs_alloc_path();
489 if (!path)
490 return -ENOMEM;
492 trans = btrfs_start_transaction(root, 1);
493 if (IS_ERR(trans)) {
494 btrfs_free_path(path);
495 return PTR_ERR(trans);
498 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
499 if (ret) {
500 if (ret > 0)
501 ret = -ENOENT;
502 fprintf(stderr, "Error searching for dir item %d\n", ret);
503 goto out;
506 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
507 struct btrfs_dir_item);
509 switch (corrupt_field) {
510 case BTRFS_DIR_ITEM_NAME:
511 name_len = btrfs_dir_name_len(path->nodes[0], di);
512 name = malloc(name_len);
513 if (!name) {
514 ret = -ENOMEM;
515 goto out;
517 name_ptr = (unsigned long)(di + 1);
518 read_extent_buffer(path->nodes[0], name, name_ptr, name_len);
519 name[0]++;
520 write_extent_buffer(path->nodes[0], name, name_ptr, name_len);
521 btrfs_mark_buffer_dirty(path->nodes[0]);
522 free(name);
523 goto out;
524 case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
525 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
526 bogus = generate_u64(location.objectid);
527 location.objectid = bogus;
528 btrfs_cpu_key_to_disk(&disk_key, &location);
529 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
530 btrfs_mark_buffer_dirty(path->nodes[0]);
531 goto out;
532 default:
533 ret = -EINVAL;
534 goto out;
536 out:
537 btrfs_commit_transaction(trans, root);
538 btrfs_free_path(path);
539 return ret;
542 static int corrupt_inode(struct btrfs_trans_handle *trans,
543 struct btrfs_root *root, u64 inode, char *field)
545 struct btrfs_inode_item *ei;
546 struct btrfs_path *path;
547 struct btrfs_key key;
548 enum btrfs_inode_field corrupt_field = convert_inode_field(field);
549 u64 bogus;
550 u64 orig;
551 int ret;
553 if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
554 fprintf(stderr, "Invalid field %s\n", field);
555 return -EINVAL;
558 key.objectid = inode;
559 key.type = BTRFS_INODE_ITEM_KEY;
560 key.offset = (u64)-1;
562 path = btrfs_alloc_path();
563 if (!path)
564 return -ENOMEM;
566 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
567 if (ret < 0)
568 goto out;
569 if (ret) {
570 if (!path->slots[0]) {
571 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
572 ret = -ENOENT;
573 goto out;
575 path->slots[0]--;
576 ret = 0;
579 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
580 if (key.objectid != inode) {
581 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
582 ret = -ENOENT;
583 goto out;
586 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
587 struct btrfs_inode_item);
588 switch (corrupt_field) {
589 case BTRFS_INODE_FIELD_ISIZE:
590 orig = btrfs_inode_size(path->nodes[0], ei);
591 bogus = generate_u64(orig);
592 btrfs_set_inode_size(path->nodes[0], ei, bogus);
593 break;
594 default:
595 ret = -EINVAL;
596 break;
598 btrfs_mark_buffer_dirty(path->nodes[0]);
599 out:
600 btrfs_free_path(path);
601 return ret;
604 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
605 struct btrfs_root *root, u64 inode, u64 extent,
606 char *field)
608 struct btrfs_file_extent_item *fi;
609 struct btrfs_path *path;
610 struct btrfs_key key;
611 enum btrfs_file_extent_field corrupt_field;
612 u64 bogus;
613 u64 orig;
614 int ret = 0;
616 corrupt_field = convert_file_extent_field(field);
617 if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
618 fprintf(stderr, "Invalid field %s\n", field);
619 return -EINVAL;
622 key.objectid = inode;
623 key.type = BTRFS_EXTENT_DATA_KEY;
624 key.offset = extent;
626 path = btrfs_alloc_path();
627 if (!path)
628 return -ENOMEM;
630 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
631 if (ret < 0)
632 goto out;
633 if (ret) {
634 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
635 extent, inode);
636 ret = -ENOENT;
637 goto out;
640 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
641 struct btrfs_file_extent_item);
642 switch (corrupt_field) {
643 case BTRFS_FILE_EXTENT_DISK_BYTENR:
644 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
645 bogus = generate_u64(orig);
646 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
647 break;
648 default:
649 ret = -EINVAL;
650 break;
652 btrfs_mark_buffer_dirty(path->nodes[0]);
653 out:
654 btrfs_free_path(path);
655 return ret;
658 static void shift_items(struct btrfs_root *root, struct extent_buffer *eb)
660 int nritems = btrfs_header_nritems(eb);
661 int shift_space = btrfs_leaf_free_space(root, eb) / 2;
662 int slot = nritems / 2;
663 int i = 0;
664 unsigned int data_end = btrfs_item_offset_nr(eb, nritems - 1);
666 /* Shift the item data up to and including slot back by shift space */
667 memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end - shift_space,
668 btrfs_leaf_data(eb) + data_end,
669 btrfs_item_offset_nr(eb, slot - 1) - data_end);
671 /* Now update the item pointers. */
672 for (i = nritems - 1; i >= slot; i--) {
673 u32 offset = btrfs_item_offset_nr(eb, i);
674 offset -= shift_space;
675 btrfs_set_item_offset(eb, btrfs_item_nr(i), offset);
679 static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
680 char *field)
682 struct btrfs_trans_handle *trans;
683 struct btrfs_path *path;
684 struct extent_buffer *eb;
685 struct btrfs_key key, root_key;
686 enum btrfs_metadata_block_field corrupt_field;
687 u64 root_objectid;
688 u64 orig, bogus;
689 u8 level;
690 int ret;
692 corrupt_field = convert_metadata_block_field(field);
693 if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
694 fprintf(stderr, "Invalid field %s\n", field);
695 return -EINVAL;
698 eb = read_tree_block(root, block, root->leafsize, 0);
699 if (!eb) {
700 fprintf(stderr, "Couldn't read in tree block %s\n", field);
701 return -EINVAL;
703 root_objectid = btrfs_header_owner(eb);
704 level = btrfs_header_level(eb);
705 if (level)
706 btrfs_node_key_to_cpu(eb, &key, 0);
707 else
708 btrfs_item_key_to_cpu(eb, &key, 0);
709 free_extent_buffer(eb);
711 root_key.objectid = root_objectid;
712 root_key.type = BTRFS_ROOT_ITEM_KEY;
713 root_key.offset = (u64)-1;
715 root = btrfs_read_fs_root(root->fs_info, &root_key);
716 if (IS_ERR(root)) {
717 fprintf(stderr, "Couldn't finde owner root %llu\n",
718 key.objectid);
719 return PTR_ERR(root);
722 path = btrfs_alloc_path();
723 if (!path)
724 return -ENOMEM;
726 trans = btrfs_start_transaction(root, 1);
727 if (IS_ERR(trans)) {
728 btrfs_free_path(path);
729 fprintf(stderr, "Couldn't start transaction %ld\n",
730 PTR_ERR(trans));
731 return PTR_ERR(trans);
734 path->lowest_level = level;
735 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
736 if (ret < 0) {
737 fprintf(stderr, "Error searching to node %d\n", ret);
738 goto out;
740 eb = path->nodes[level];
742 ret = 0;
743 switch (corrupt_field) {
744 case BTRFS_METADATA_BLOCK_GENERATION:
745 orig = btrfs_header_generation(eb);
746 bogus = generate_u64(orig);
747 btrfs_set_header_generation(eb, bogus);
748 break;
749 case BTRFS_METADATA_BLOCK_SHIFT_ITEMS:
750 shift_items(root, path->nodes[level]);
751 break;
752 default:
753 ret = -EINVAL;
754 break;
756 btrfs_mark_buffer_dirty(path->nodes[level]);
757 out:
758 btrfs_commit_transaction(trans, root);
759 btrfs_free_path(path);
760 return ret;
763 static int corrupt_btrfs_item(struct btrfs_root *root, struct btrfs_key *key,
764 char *field)
766 struct btrfs_trans_handle *trans;
767 struct btrfs_path *path;
768 enum btrfs_item_field corrupt_field;
769 u32 orig, bogus;
770 int ret;
772 corrupt_field = convert_item_field(field);
773 if (corrupt_field == BTRFS_ITEM_BAD) {
774 fprintf(stderr, "Invalid field %s\n", field);
775 return -EINVAL;
778 path = btrfs_alloc_path();
779 if (!path)
780 return -ENOMEM;
782 trans = btrfs_start_transaction(root, 1);
783 if (IS_ERR(trans)) {
784 btrfs_free_path(path);
785 fprintf(stderr, "Couldn't start transaction %ld\n",
786 PTR_ERR(trans));
787 return PTR_ERR(trans);
790 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
791 if (ret != 0) {
792 fprintf(stderr, "Error searching to node %d\n", ret);
793 goto out;
796 ret = 0;
797 switch (corrupt_field) {
798 case BTRFS_ITEM_OFFSET:
799 orig = btrfs_item_offset_nr(path->nodes[0], path->slots[0]);
800 bogus = generate_u32(orig);
801 btrfs_set_item_offset(path->nodes[0],
802 btrfs_item_nr(path->slots[0]), bogus);
803 break;
804 default:
805 ret = -EINVAL;
806 break;
808 btrfs_mark_buffer_dirty(path->nodes[0]);
809 out:
810 btrfs_commit_transaction(trans, root);
811 btrfs_free_path(path);
812 return ret;
815 static int delete_item(struct btrfs_root *root, struct btrfs_key *key)
817 struct btrfs_trans_handle *trans;
818 struct btrfs_path *path;
819 int ret;
821 path = btrfs_alloc_path();
822 if (!path)
823 return -ENOMEM;
825 trans = btrfs_start_transaction(root, 1);
826 if (IS_ERR(trans)) {
827 btrfs_free_path(path);
828 fprintf(stderr, "Couldn't start transaction %ld\n",
829 PTR_ERR(trans));
830 return PTR_ERR(trans);
833 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
834 if (ret) {
835 if (ret > 0)
836 ret = -ENOENT;
837 fprintf(stderr, "Error searching to node %d\n", ret);
838 goto out;
840 ret = btrfs_del_item(trans, root, path);
841 btrfs_mark_buffer_dirty(path->nodes[0]);
842 out:
843 btrfs_commit_transaction(trans, root);
844 btrfs_free_path(path);
845 return ret;
848 static struct option long_options[] = {
849 /* { "byte-count", 1, NULL, 'b' }, */
850 { "logical", 1, NULL, 'l' },
851 { "copy", 1, NULL, 'c' },
852 { "bytes", 1, NULL, 'b' },
853 { "extent-record", 0, NULL, 'e' },
854 { "extent-tree", 0, NULL, 'E' },
855 { "keys", 0, NULL, 'k' },
856 { "chunk-record", 0, NULL, 'u' },
857 { "chunk-tree", 0, NULL, 'U' },
858 { "inode", 1, NULL, 'i'},
859 { "file-extent", 1, NULL, 'x'},
860 { "metadata-block", 1, NULL, 'm'},
861 { "field", 1, NULL, 'f'},
862 { "key", 1, NULL, 'K'},
863 { "item", 0, NULL, 'I'},
864 { "dir-item", 0, NULL, 'D'},
865 { "delete", 0, NULL, 'd'},
866 { 0, 0, 0, 0}
869 /* corrupt item using NO cow.
870 * Because chunk recover will recover based on whole partition scaning,
871 * If using COW, chunk recover will use the old item to recover,
872 * which is still OK but we want to check the ability to rebuild chunk
873 * not only restore the old ones */
874 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
875 struct btrfs_root *root, struct btrfs_path *path,
876 int del)
878 int ret = 0;
879 struct btrfs_key key;
880 struct extent_buffer *leaf;
881 unsigned long ptr;
882 int slot;
883 u32 item_size;
885 leaf = path->nodes[0];
886 slot = path->slots[0];
887 /* Not deleting the first item of a leaf to keep leaf structure */
888 if (slot == 0)
889 del = 0;
890 /* Only accept valid eb */
891 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
892 btrfs_item_key_to_cpu(leaf, &key, slot);
893 if (del) {
894 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
895 key.objectid, key.type, key.offset);
896 btrfs_del_item(trans, root, path);
897 } else {
898 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
899 key.objectid, key.type, key.offset);
900 ptr = btrfs_item_ptr_offset(leaf, slot);
901 item_size = btrfs_item_size_nr(leaf, slot);
902 memset_extent_buffer(leaf, 0, ptr, item_size);
903 btrfs_mark_buffer_dirty(leaf);
905 return ret;
907 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
908 struct btrfs_root *root)
910 int ret;
911 int del;
912 int slot;
913 struct btrfs_path *path;
914 struct btrfs_key key;
915 struct btrfs_key found_key;
916 struct extent_buffer *leaf;
918 path = btrfs_alloc_path();
919 if (!path)
920 return -ENOMEM;
922 key.objectid = (u64)-1;
923 key.offset = (u64)-1;
924 key.type = (u8)-1;
926 /* Here, cow and ins_len must equals 0 for the following reasons:
927 * 1) chunk recover is based on disk scanning, so COW should be
928 * disabled in case the original chunk being scanned and
929 * recovered using the old chunk.
930 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
931 * triggered.
933 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
934 BUG_ON(ret == 0);
935 if (ret < 0) {
936 fprintf(stderr, "Error searching tree\n");
937 goto free_out;
939 /* corrupt/del dev_item first */
940 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
941 slot = path->slots[0];
942 leaf = path->nodes[0];
943 del = rand() % 3;
944 /* Never delete the first item to keep the leaf structure */
945 if (path->slots[0] == 0)
946 del = 0;
947 ret = corrupt_item_nocow(trans, root, path, del);
948 if (ret)
949 goto free_out;
951 btrfs_release_path(path);
953 /* Here, cow and ins_len must equals 0 for the following reasons:
954 * 1) chunk recover is based on disk scanning, so COW should be
955 * disabled in case the original chunk being scanned and
956 * recovered using the old chunk.
957 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
958 * triggered.
960 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
961 BUG_ON(ret == 0);
962 if (ret < 0) {
963 fprintf(stderr, "Error searching tree\n");
964 goto free_out;
966 /* corrupt/del chunk then*/
967 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
968 slot = path->slots[0];
969 leaf = path->nodes[0];
970 del = rand() % 3;
971 btrfs_item_key_to_cpu(leaf, &found_key, slot);
972 ret = corrupt_item_nocow(trans, root, path, del);
973 if (ret)
974 goto free_out;
976 free_out:
977 btrfs_free_path(path);
978 return ret;
980 int find_chunk_offset(struct btrfs_root *root,
981 struct btrfs_path *path, u64 offset)
983 struct btrfs_key key;
984 int ret;
986 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
987 key.type = BTRFS_CHUNK_ITEM_KEY;
988 key.offset = offset;
990 /* Here, cow and ins_len must equals 0 for following reasons:
991 * 1) chunk recover is based on disk scanning, so COW should
992 * be disabled in case the original chunk being scanned
993 * and recovered using the old chunk.
994 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
995 * will be triggered.
997 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
998 if (ret > 0) {
999 fprintf(stderr, "Can't find chunk with given offset %llu\n",
1000 offset);
1001 goto out;
1003 if (ret < 0) {
1004 fprintf(stderr, "Error searching chunk");
1005 goto out;
1007 out:
1008 return ret;
1011 int main(int ac, char **av)
1013 struct cache_tree root_cache;
1014 struct btrfs_key key;
1015 struct btrfs_root *root;
1016 struct extent_buffer *eb;
1017 char *dev;
1018 /* chunk offset can be 0,so change to (u64)-1 */
1019 u64 logical = (u64)-1;
1020 int ret = 0;
1021 int option_index = 0;
1022 u64 copy = 0;
1023 u64 bytes = 4096;
1024 int extent_rec = 0;
1025 int extent_tree = 0;
1026 int corrupt_block_keys = 0;
1027 int chunk_rec = 0;
1028 int chunk_tree = 0;
1029 int corrupt_item = 0;
1030 int corrupt_di = 0;
1031 int delete = 0;
1032 u64 metadata_block = 0;
1033 u64 inode = 0;
1034 u64 file_extent = (u64)-1;
1035 char field[FIELD_BUF_LEN];
1037 field[0] = '\0';
1038 srand(128);
1039 memset(&key, 0, sizeof(key));
1041 while(1) {
1042 int c;
1043 c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:IDd", long_options,
1044 &option_index);
1045 if (c < 0)
1046 break;
1047 switch(c) {
1048 case 'l':
1049 logical = arg_strtou64(optarg);
1050 break;
1051 case 'c':
1052 copy = arg_strtou64(optarg);
1053 break;
1054 case 'b':
1055 bytes = arg_strtou64(optarg);
1056 break;
1057 case 'e':
1058 extent_rec = 1;
1059 break;
1060 case 'E':
1061 extent_tree = 1;
1062 break;
1063 case 'k':
1064 corrupt_block_keys = 1;
1065 break;
1066 case 'u':
1067 chunk_rec = 1;
1068 break;
1069 case 'U':
1070 chunk_tree = 1;
1071 case 'i':
1072 inode = arg_strtou64(optarg);
1073 break;
1074 case 'f':
1075 strncpy(field, optarg, FIELD_BUF_LEN);
1076 break;
1077 case 'x':
1078 file_extent = arg_strtou64(optarg);
1079 break;
1080 case 'm':
1081 metadata_block = arg_strtou64(optarg);
1082 break;
1083 case 'K':
1084 ret = sscanf(optarg, "%llu,%u,%llu",
1085 &key.objectid,
1086 (unsigned int *)&key.type,
1087 &key.offset);
1088 if (ret != 3) {
1089 fprintf(stderr, "error reading key "
1090 "%d\n", errno);
1091 print_usage();
1093 break;
1094 case 'D':
1095 corrupt_di = 1;
1096 break;
1097 case 'I':
1098 corrupt_item = 1;
1099 case 'd':
1100 delete = 1;
1101 break;
1102 default:
1103 print_usage();
1106 set_argv0(av);
1107 ac = ac - optind;
1108 if (check_argc_min(ac, 1))
1109 print_usage();
1110 dev = av[optind];
1112 radix_tree_init();
1113 cache_tree_init(&root_cache);
1115 root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
1116 if (!root) {
1117 fprintf(stderr, "Open ctree failed\n");
1118 exit(1);
1120 if (extent_rec) {
1121 struct btrfs_trans_handle *trans;
1123 if (logical == (u64)-1)
1124 print_usage();
1125 trans = btrfs_start_transaction(root, 1);
1126 ret = corrupt_extent (trans, root, logical, 0);
1127 btrfs_commit_transaction(trans, root);
1128 goto out_close;
1130 if (extent_tree) {
1131 struct btrfs_trans_handle *trans;
1132 trans = btrfs_start_transaction(root, 1);
1133 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
1134 root->fs_info->extent_root->node);
1135 btrfs_commit_transaction(trans, root);
1136 goto out_close;
1138 if (chunk_rec) {
1139 struct btrfs_trans_handle *trans;
1140 struct btrfs_path *path;
1141 int del;
1143 if (logical == (u64)-1)
1144 print_usage();
1145 del = rand() % 3;
1146 path = btrfs_alloc_path();
1147 if (!path) {
1148 fprintf(stderr, "path allocation failed\n");
1149 goto out_close;
1152 if (find_chunk_offset(root->fs_info->chunk_root, path,
1153 logical) != 0) {
1154 btrfs_free_path(path);
1155 goto out_close;
1157 trans = btrfs_start_transaction(root, 1);
1158 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
1159 path, del);
1160 if (ret < 0)
1161 fprintf(stderr, "Failed to corrupt chunk record\n");
1162 btrfs_commit_transaction(trans, root);
1163 goto out_close;
1165 if (chunk_tree) {
1166 struct btrfs_trans_handle *trans;
1167 trans = btrfs_start_transaction(root, 1);
1168 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
1169 if (ret < 0)
1170 fprintf(stderr, "Failed to corrupt chunk tree\n");
1171 btrfs_commit_transaction(trans, root);
1172 goto out_close;
1174 if (inode) {
1175 struct btrfs_trans_handle *trans;
1177 if (!strlen(field))
1178 print_usage();
1180 trans = btrfs_start_transaction(root, 1);
1181 if (file_extent == (u64)-1) {
1182 printf("corrupting inode\n");
1183 ret = corrupt_inode(trans, root, inode, field);
1184 } else {
1185 printf("corrupting file extent\n");
1186 ret = corrupt_file_extent(trans, root, inode,
1187 file_extent, field);
1189 btrfs_commit_transaction(trans, root);
1190 goto out_close;
1192 if (metadata_block) {
1193 if (!strlen(field))
1194 print_usage();
1195 ret = corrupt_metadata_block(root, metadata_block, field);
1196 goto out_close;
1198 if (corrupt_di) {
1199 if (!key.objectid || !strlen(field))
1200 print_usage();
1201 ret = corrupt_dir_item(root, &key, field);
1202 goto out_close;
1204 if (corrupt_item) {
1205 if (!key.objectid)
1206 print_usage();
1207 ret = corrupt_btrfs_item(root, &key, field);
1209 if (delete) {
1210 if (!key.objectid)
1211 print_usage();
1212 ret = delete_item(root, &key);
1213 goto out_close;
1215 if (key.objectid || key.offset || key.type) {
1216 if (!strlen(field))
1217 print_usage();
1218 ret = corrupt_key(root, &key, field);
1219 goto out_close;
1222 * If we made it here and we have extent set then we didn't specify
1223 * inode and we're screwed.
1225 if (file_extent != (u64)-1)
1226 print_usage();
1228 if (logical == (u64)-1)
1229 print_usage();
1231 if (bytes == 0)
1232 bytes = root->sectorsize;
1234 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
1235 bytes *= root->sectorsize;
1237 while (bytes > 0) {
1238 if (corrupt_block_keys) {
1239 corrupt_keys_in_block(root, logical);
1240 } else {
1241 eb = debug_corrupt_block(root, logical,
1242 root->sectorsize, copy);
1243 free_extent_buffer(eb);
1245 logical += root->sectorsize;
1246 bytes -= root->sectorsize;
1248 return ret;
1249 out_close:
1250 close_ctree(root);
1251 return ret;