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.
25 #include "kerncompat.h"
29 #include "print-tree.h"
30 #include "transaction.h"
34 #define FIELD_BUF_LEN 80
36 static struct extent_buffer
*debug_corrupt_block(struct btrfs_root
*root
,
37 u64 bytenr
, u32 blocksize
, u64 copy
)
40 struct extent_buffer
*eb
;
42 struct btrfs_multi_bio
*multi
= NULL
;
43 struct btrfs_device
*device
;
47 eb
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
53 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
54 eb
->start
, &length
, &multi
,
57 device
= multi
->stripes
[0].dev
;
60 eb
->dev_bytenr
= multi
->stripes
[0].physical
;
63 "mirror %d logical %llu physical %llu device %s\n",
64 mirror_num
, (unsigned long long)bytenr
,
65 (unsigned long long)eb
->dev_bytenr
, device
->name
);
68 if (!copy
|| mirror_num
== copy
) {
69 ret
= read_extent_from_disk(eb
, 0, eb
->len
);
70 printf("corrupting %llu copy %d\n", eb
->start
,
72 memset(eb
->data
, 0, eb
->len
);
73 write_extent_to_disk(eb
);
77 num_copies
= btrfs_num_copies(&root
->fs_info
->mapping_tree
,
83 if (mirror_num
> num_copies
)
89 static void print_usage(int ret
)
91 fprintf(stderr
, "usage: btrfs-corrupt-block [options] device\n");
92 fprintf(stderr
, "\t-l Logical extent to be corrupted\n");
93 fprintf(stderr
, "\t-c Copy of the extent to be corrupted"
94 " (usually 1 or 2, default: 0)\n");
95 fprintf(stderr
, "\t-b Number of bytes to be corrupted\n");
96 fprintf(stderr
, "\t-e Extent to be corrupted\n");
97 fprintf(stderr
, "\t-E The whole extent tree to be corrupted\n");
98 fprintf(stderr
, "\t-u Given chunk item to be corrupted\n");
99 fprintf(stderr
, "\t-U The whole chunk tree to be corrupted\n");
100 fprintf(stderr
, "\t-i The inode item to corrupt (must also specify "
101 "the field to corrupt)\n");
102 fprintf(stderr
, "\t-x The file extent item to corrupt (must also "
103 "specify -i for the inode and -f for the field to corrupt)\n");
104 fprintf(stderr
, "\t-m The metadata block to corrupt (must also "
105 "specify -f for the field to corrupt)\n");
106 fprintf(stderr
, "\t-K The key to corrupt in the format "
107 "<num>,<num>,<num> (must also specify -f for the field)\n");
108 fprintf(stderr
, "\t-f The field in the item to corrupt\n");
109 fprintf(stderr
, "\t-I An item to corrupt (must also specify the field "
110 "to corrupt and a root+key for the item)\n");
111 fprintf(stderr
, "\t-D Corrupt a dir item, must specify key and field\n");
112 fprintf(stderr
, "\t-d Delete this item (must specify -K)\n");
113 fprintf(stderr
, "\t-r Operate on this root (only works with -d)\n");
114 fprintf(stderr
, "\t-C Delete a csum for the specified bytenr. When "
115 "used with -b it'll delete that many bytes, otherwise it's "
116 "just sectorsize\n");
120 static void corrupt_keys(struct btrfs_trans_handle
*trans
,
121 struct btrfs_root
*root
,
122 struct extent_buffer
*eb
)
127 struct btrfs_disk_key bad_key
;;
129 nr
= btrfs_header_nritems(eb
);
134 bad_slot
= rand() % nr
;
136 if (bad_slot
== slot
)
140 "corrupting keys in block %llu slot %d swapping with %d\n",
141 (unsigned long long)eb
->start
, slot
, bad_slot
);
143 if (btrfs_header_level(eb
) == 0) {
144 btrfs_item_key(eb
, &bad_key
, bad_slot
);
145 btrfs_set_item_key(eb
, &bad_key
, slot
);
147 btrfs_node_key(eb
, &bad_key
, bad_slot
);
148 btrfs_set_node_key(eb
, &bad_key
, slot
);
150 btrfs_mark_buffer_dirty(eb
);
153 btrfs_super_csum_size(root
->fs_info
->super_copy
);
154 csum_tree_block_size(eb
, csum_size
, 0);
155 write_extent_to_disk(eb
);
160 static int corrupt_keys_in_block(struct btrfs_root
*root
, u64 bytenr
)
162 struct extent_buffer
*eb
;
164 eb
= read_tree_block(root
, bytenr
, root
->leafsize
, 0);
165 if (!extent_buffer_uptodate(eb
))
168 corrupt_keys(NULL
, root
, eb
);
169 free_extent_buffer(eb
);
173 static int corrupt_extent(struct btrfs_trans_handle
*trans
,
174 struct btrfs_root
*root
, u64 bytenr
, u64 copy
)
176 struct btrfs_key key
;
177 struct extent_buffer
*leaf
;
180 struct btrfs_path
*path
;
183 int should_del
= rand() % 3;
185 path
= btrfs_alloc_path();
189 key
.objectid
= bytenr
;
191 key
.offset
= (u64
)-1;
194 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
,
200 if (path
->slots
[0] == 0)
205 leaf
= path
->nodes
[0];
206 slot
= path
->slots
[0];
207 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
208 if (key
.objectid
!= bytenr
)
211 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
&&
212 key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
&&
213 key
.type
!= BTRFS_EXTENT_DATA_REF_KEY
&&
214 key
.type
!= BTRFS_EXTENT_REF_V0_KEY
&&
215 key
.type
!= BTRFS_SHARED_BLOCK_REF_KEY
&&
216 key
.type
!= BTRFS_SHARED_DATA_REF_KEY
)
221 "deleting extent record: key %llu %u %llu\n",
222 key
.objectid
, key
.type
, key
.offset
);
224 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
225 /* make sure this extent doesn't get
226 * reused for other purposes */
227 btrfs_pin_extent(root
->fs_info
,
228 key
.objectid
, key
.offset
);
231 btrfs_del_item(trans
, root
, path
);
234 "corrupting extent record: key %llu %u %llu\n",
235 key
.objectid
, key
.type
, key
.offset
);
236 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
237 item_size
= btrfs_item_size_nr(leaf
, slot
);
238 memset_extent_buffer(leaf
, 0, ptr
, item_size
);
239 btrfs_mark_buffer_dirty(leaf
);
242 btrfs_release_path(path
);
250 btrfs_free_path(path
);
254 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle
*trans
,
255 struct btrfs_root
*root
,
256 struct extent_buffer
*eb
)
258 u32 nr
= btrfs_header_nritems(eb
);
259 u32 victim
= rand() % nr
;
261 struct btrfs_key key
;
263 btrfs_item_key_to_cpu(eb
, &key
, victim
);
264 objectid
= key
.objectid
;
265 corrupt_extent(trans
, root
, objectid
, 1);
268 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle
*trans
,
269 struct btrfs_root
*root
,
270 struct extent_buffer
*eb
)
277 if (btrfs_is_leaf(eb
)) {
278 btrfs_corrupt_extent_leaf(trans
, root
, eb
);
282 if (btrfs_header_level(eb
) == 1 && eb
!= root
->node
) {
287 for (i
= 0; i
< btrfs_header_nritems(eb
); i
++) {
288 struct extent_buffer
*next
;
290 next
= read_tree_block(root
, btrfs_node_blockptr(eb
, i
),
292 btrfs_node_ptr_generation(eb
, i
));
293 if (!extent_buffer_uptodate(next
))
295 btrfs_corrupt_extent_tree(trans
, root
, next
);
296 free_extent_buffer(next
);
300 enum btrfs_inode_field
{
301 BTRFS_INODE_FIELD_ISIZE
,
302 BTRFS_INODE_FIELD_NBYTES
,
303 BTRFS_INODE_FIELD_BAD
,
306 enum btrfs_file_extent_field
{
307 BTRFS_FILE_EXTENT_DISK_BYTENR
,
308 BTRFS_FILE_EXTENT_BAD
,
311 enum btrfs_dir_item_field
{
313 BTRFS_DIR_ITEM_LOCATION_OBJECTID
,
317 enum btrfs_metadata_block_field
{
318 BTRFS_METADATA_BLOCK_GENERATION
,
319 BTRFS_METADATA_BLOCK_SHIFT_ITEMS
,
320 BTRFS_METADATA_BLOCK_BAD
,
323 enum btrfs_item_field
{
328 enum btrfs_key_field
{
335 static enum btrfs_inode_field
convert_inode_field(char *field
)
337 if (!strncmp(field
, "isize", FIELD_BUF_LEN
))
338 return BTRFS_INODE_FIELD_ISIZE
;
339 if (!strncmp(field
, "nbytes", FIELD_BUF_LEN
))
340 return BTRFS_INODE_FIELD_NBYTES
;
341 return BTRFS_INODE_FIELD_BAD
;
344 static enum btrfs_file_extent_field
convert_file_extent_field(char *field
)
346 if (!strncmp(field
, "disk_bytenr", FIELD_BUF_LEN
))
347 return BTRFS_FILE_EXTENT_DISK_BYTENR
;
348 return BTRFS_FILE_EXTENT_BAD
;
351 static enum btrfs_metadata_block_field
352 convert_metadata_block_field(char *field
)
354 if (!strncmp(field
, "generation", FIELD_BUF_LEN
))
355 return BTRFS_METADATA_BLOCK_GENERATION
;
356 if (!strncmp(field
, "shift_items", FIELD_BUF_LEN
))
357 return BTRFS_METADATA_BLOCK_SHIFT_ITEMS
;
358 return BTRFS_METADATA_BLOCK_BAD
;
361 static enum btrfs_key_field
convert_key_field(char *field
)
363 if (!strncmp(field
, "objectid", FIELD_BUF_LEN
))
364 return BTRFS_KEY_OBJECTID
;
365 if (!strncmp(field
, "type", FIELD_BUF_LEN
))
366 return BTRFS_KEY_TYPE
;
367 if (!strncmp(field
, "offset", FIELD_BUF_LEN
))
368 return BTRFS_KEY_OFFSET
;
369 return BTRFS_KEY_BAD
;
372 static enum btrfs_item_field
convert_item_field(char *field
)
374 if (!strncmp(field
, "offset", FIELD_BUF_LEN
))
375 return BTRFS_ITEM_OFFSET
;
376 return BTRFS_ITEM_BAD
;
379 static enum btrfs_dir_item_field
convert_dir_item_field(char *field
)
381 if (!strncmp(field
, "name", FIELD_BUF_LEN
))
382 return BTRFS_DIR_ITEM_NAME
;
383 if (!strncmp(field
, "location_objectid", FIELD_BUF_LEN
))
384 return BTRFS_DIR_ITEM_LOCATION_OBJECTID
;
385 return BTRFS_DIR_ITEM_BAD
;
388 static u64
generate_u64(u64 orig
)
393 } while (ret
== orig
);
397 static u32
generate_u32(u32 orig
)
402 } while (ret
== orig
);
406 static u8
generate_u8(u8 orig
)
411 } while (ret
== orig
);
415 static int corrupt_key(struct btrfs_root
*root
, struct btrfs_key
*key
,
418 enum btrfs_key_field corrupt_field
= convert_key_field(field
);
419 struct btrfs_path
*path
;
420 struct btrfs_trans_handle
*trans
;
423 root
= root
->fs_info
->fs_root
;
424 if (corrupt_field
== BTRFS_KEY_BAD
) {
425 fprintf(stderr
, "Invalid field %s\n", field
);
429 path
= btrfs_alloc_path();
433 trans
= btrfs_start_transaction(root
, 1);
435 btrfs_free_path(path
);
436 return PTR_ERR(trans
);
439 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
443 fprintf(stderr
, "Couldn't find the key to corrupt\n");
448 switch (corrupt_field
) {
449 case BTRFS_KEY_OBJECTID
:
450 key
->objectid
= generate_u64(key
->objectid
);
453 key
->type
= generate_u8(key
->type
);
455 case BTRFS_KEY_OFFSET
:
456 key
->offset
= generate_u64(key
->objectid
);
459 fprintf(stderr
, "Invalid field %s, %d\n", field
,
465 btrfs_set_item_key_unsafe(root
, path
, key
);
467 btrfs_free_path(path
);
468 btrfs_commit_transaction(trans
, root
);
472 static int corrupt_dir_item(struct btrfs_root
*root
, struct btrfs_key
*key
,
475 struct btrfs_trans_handle
*trans
;
476 struct btrfs_dir_item
*di
;
477 struct btrfs_path
*path
;
479 struct btrfs_key location
;
480 struct btrfs_disk_key disk_key
;
481 unsigned long name_ptr
;
482 enum btrfs_dir_item_field corrupt_field
=
483 convert_dir_item_field(field
);
488 if (corrupt_field
== BTRFS_DIR_ITEM_BAD
) {
489 fprintf(stderr
, "Invalid field %s\n", field
);
493 path
= btrfs_alloc_path();
497 trans
= btrfs_start_transaction(root
, 1);
499 btrfs_free_path(path
);
500 return PTR_ERR(trans
);
503 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
507 fprintf(stderr
, "Error searching for dir item %d\n", ret
);
511 di
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
512 struct btrfs_dir_item
);
514 switch (corrupt_field
) {
515 case BTRFS_DIR_ITEM_NAME
:
516 name_len
= btrfs_dir_name_len(path
->nodes
[0], di
);
517 name
= malloc(name_len
);
522 name_ptr
= (unsigned long)(di
+ 1);
523 read_extent_buffer(path
->nodes
[0], name
, name_ptr
, name_len
);
525 write_extent_buffer(path
->nodes
[0], name
, name_ptr
, name_len
);
526 btrfs_mark_buffer_dirty(path
->nodes
[0]);
529 case BTRFS_DIR_ITEM_LOCATION_OBJECTID
:
530 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
531 bogus
= generate_u64(location
.objectid
);
532 location
.objectid
= bogus
;
533 btrfs_cpu_key_to_disk(&disk_key
, &location
);
534 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
535 btrfs_mark_buffer_dirty(path
->nodes
[0]);
542 btrfs_commit_transaction(trans
, root
);
543 btrfs_free_path(path
);
547 static int corrupt_inode(struct btrfs_trans_handle
*trans
,
548 struct btrfs_root
*root
, u64 inode
, char *field
)
550 struct btrfs_inode_item
*ei
;
551 struct btrfs_path
*path
;
552 struct btrfs_key key
;
553 enum btrfs_inode_field corrupt_field
= convert_inode_field(field
);
558 if (corrupt_field
== BTRFS_INODE_FIELD_BAD
) {
559 fprintf(stderr
, "Invalid field %s\n", field
);
563 key
.objectid
= inode
;
564 key
.type
= BTRFS_INODE_ITEM_KEY
;
565 key
.offset
= (u64
)-1;
567 path
= btrfs_alloc_path();
571 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
575 if (!path
->slots
[0]) {
576 fprintf(stderr
, "Couldn't find inode %Lu\n", inode
);
584 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
585 if (key
.objectid
!= inode
) {
586 fprintf(stderr
, "Couldn't find inode %Lu\n", inode
);
591 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
592 struct btrfs_inode_item
);
593 switch (corrupt_field
) {
594 case BTRFS_INODE_FIELD_ISIZE
:
595 orig
= btrfs_inode_size(path
->nodes
[0], ei
);
596 bogus
= generate_u64(orig
);
597 btrfs_set_inode_size(path
->nodes
[0], ei
, bogus
);
599 case BTRFS_INODE_FIELD_NBYTES
:
600 orig
= btrfs_inode_nbytes(path
->nodes
[0], ei
);
601 bogus
= generate_u64(orig
);
602 btrfs_set_inode_nbytes(path
->nodes
[0], ei
, bogus
);
608 btrfs_mark_buffer_dirty(path
->nodes
[0]);
610 btrfs_free_path(path
);
614 static int corrupt_file_extent(struct btrfs_trans_handle
*trans
,
615 struct btrfs_root
*root
, u64 inode
, u64 extent
,
618 struct btrfs_file_extent_item
*fi
;
619 struct btrfs_path
*path
;
620 struct btrfs_key key
;
621 enum btrfs_file_extent_field corrupt_field
;
626 corrupt_field
= convert_file_extent_field(field
);
627 if (corrupt_field
== BTRFS_FILE_EXTENT_BAD
) {
628 fprintf(stderr
, "Invalid field %s\n", field
);
632 key
.objectid
= inode
;
633 key
.type
= BTRFS_EXTENT_DATA_KEY
;
636 path
= btrfs_alloc_path();
640 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
644 fprintf(stderr
, "Couldn't find extent %llu for inode %llu\n",
650 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
651 struct btrfs_file_extent_item
);
652 switch (corrupt_field
) {
653 case BTRFS_FILE_EXTENT_DISK_BYTENR
:
654 orig
= btrfs_file_extent_disk_bytenr(path
->nodes
[0], fi
);
655 bogus
= generate_u64(orig
);
656 btrfs_set_file_extent_disk_bytenr(path
->nodes
[0], fi
, bogus
);
662 btrfs_mark_buffer_dirty(path
->nodes
[0]);
664 btrfs_free_path(path
);
668 static void shift_items(struct btrfs_root
*root
, struct extent_buffer
*eb
)
670 int nritems
= btrfs_header_nritems(eb
);
671 int shift_space
= btrfs_leaf_free_space(root
, eb
) / 2;
672 int slot
= nritems
/ 2;
674 unsigned int data_end
= btrfs_item_offset_nr(eb
, nritems
- 1);
676 /* Shift the item data up to and including slot back by shift space */
677 memmove_extent_buffer(eb
, btrfs_leaf_data(eb
) + data_end
- shift_space
,
678 btrfs_leaf_data(eb
) + data_end
,
679 btrfs_item_offset_nr(eb
, slot
- 1) - data_end
);
681 /* Now update the item pointers. */
682 for (i
= nritems
- 1; i
>= slot
; i
--) {
683 u32 offset
= btrfs_item_offset_nr(eb
, i
);
684 offset
-= shift_space
;
685 btrfs_set_item_offset(eb
, btrfs_item_nr(i
), offset
);
689 static int corrupt_metadata_block(struct btrfs_root
*root
, u64 block
,
692 struct btrfs_trans_handle
*trans
;
693 struct btrfs_path
*path
;
694 struct extent_buffer
*eb
;
695 struct btrfs_key key
, root_key
;
696 enum btrfs_metadata_block_field corrupt_field
;
702 corrupt_field
= convert_metadata_block_field(field
);
703 if (corrupt_field
== BTRFS_METADATA_BLOCK_BAD
) {
704 fprintf(stderr
, "Invalid field %s\n", field
);
708 eb
= read_tree_block(root
, block
, root
->leafsize
, 0);
709 if (!extent_buffer_uptodate(eb
)) {
710 fprintf(stderr
, "Couldn't read in tree block %s\n", field
);
713 root_objectid
= btrfs_header_owner(eb
);
714 level
= btrfs_header_level(eb
);
716 btrfs_node_key_to_cpu(eb
, &key
, 0);
718 btrfs_item_key_to_cpu(eb
, &key
, 0);
719 free_extent_buffer(eb
);
721 root_key
.objectid
= root_objectid
;
722 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
723 root_key
.offset
= (u64
)-1;
725 root
= btrfs_read_fs_root(root
->fs_info
, &root_key
);
727 fprintf(stderr
, "Couldn't finde owner root %llu\n",
729 return PTR_ERR(root
);
732 path
= btrfs_alloc_path();
736 trans
= btrfs_start_transaction(root
, 1);
738 btrfs_free_path(path
);
739 fprintf(stderr
, "Couldn't start transaction %ld\n",
741 return PTR_ERR(trans
);
744 path
->lowest_level
= level
;
745 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
747 fprintf(stderr
, "Error searching to node %d\n", ret
);
750 eb
= path
->nodes
[level
];
753 switch (corrupt_field
) {
754 case BTRFS_METADATA_BLOCK_GENERATION
:
755 orig
= btrfs_header_generation(eb
);
756 bogus
= generate_u64(orig
);
757 btrfs_set_header_generation(eb
, bogus
);
759 case BTRFS_METADATA_BLOCK_SHIFT_ITEMS
:
760 shift_items(root
, path
->nodes
[level
]);
766 btrfs_mark_buffer_dirty(path
->nodes
[level
]);
768 btrfs_commit_transaction(trans
, root
);
769 btrfs_free_path(path
);
773 static int corrupt_btrfs_item(struct btrfs_root
*root
, struct btrfs_key
*key
,
776 struct btrfs_trans_handle
*trans
;
777 struct btrfs_path
*path
;
778 enum btrfs_item_field corrupt_field
;
782 corrupt_field
= convert_item_field(field
);
783 if (corrupt_field
== BTRFS_ITEM_BAD
) {
784 fprintf(stderr
, "Invalid field %s\n", field
);
788 path
= btrfs_alloc_path();
792 trans
= btrfs_start_transaction(root
, 1);
794 btrfs_free_path(path
);
795 fprintf(stderr
, "Couldn't start transaction %ld\n",
797 return PTR_ERR(trans
);
800 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
802 fprintf(stderr
, "Error searching to node %d\n", ret
);
807 switch (corrupt_field
) {
808 case BTRFS_ITEM_OFFSET
:
809 orig
= btrfs_item_offset_nr(path
->nodes
[0], path
->slots
[0]);
810 bogus
= generate_u32(orig
);
811 btrfs_set_item_offset(path
->nodes
[0],
812 btrfs_item_nr(path
->slots
[0]), bogus
);
818 btrfs_mark_buffer_dirty(path
->nodes
[0]);
820 btrfs_commit_transaction(trans
, root
);
821 btrfs_free_path(path
);
825 static int delete_item(struct btrfs_root
*root
, struct btrfs_key
*key
)
827 struct btrfs_trans_handle
*trans
;
828 struct btrfs_path
*path
;
831 path
= btrfs_alloc_path();
835 trans
= btrfs_start_transaction(root
, 1);
837 btrfs_free_path(path
);
838 fprintf(stderr
, "Couldn't start transaction %ld\n",
840 return PTR_ERR(trans
);
843 ret
= btrfs_search_slot(trans
, root
, key
, path
, -1, 1);
847 fprintf(stderr
, "Error searching to node %d\n", ret
);
850 ret
= btrfs_del_item(trans
, root
, path
);
851 btrfs_mark_buffer_dirty(path
->nodes
[0]);
853 btrfs_commit_transaction(trans
, root
);
854 btrfs_free_path(path
);
858 static int delete_csum(struct btrfs_root
*root
, u64 bytenr
, u64 bytes
)
860 struct btrfs_trans_handle
*trans
;
863 root
= root
->fs_info
->csum_root
;
864 trans
= btrfs_start_transaction(root
, 1);
866 fprintf(stderr
, "Couldn't start transaction %ld\n",
868 return PTR_ERR(trans
);
871 ret
= btrfs_del_csums(trans
, root
, bytenr
, bytes
);
873 fprintf(stderr
, "Error deleting csums %d\n", ret
);
874 btrfs_commit_transaction(trans
, root
);
878 /* corrupt item using NO cow.
879 * Because chunk recover will recover based on whole partition scaning,
880 * If using COW, chunk recover will use the old item to recover,
881 * which is still OK but we want to check the ability to rebuild chunk
882 * not only restore the old ones */
883 static int corrupt_item_nocow(struct btrfs_trans_handle
*trans
,
884 struct btrfs_root
*root
, struct btrfs_path
*path
,
888 struct btrfs_key key
;
889 struct extent_buffer
*leaf
;
894 leaf
= path
->nodes
[0];
895 slot
= path
->slots
[0];
896 /* Not deleting the first item of a leaf to keep leaf structure */
899 /* Only accept valid eb */
900 BUG_ON(!leaf
->data
|| slot
>= btrfs_header_nritems(leaf
));
901 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
903 fprintf(stdout
, "Deleting key and data [%llu, %u, %llu].\n",
904 key
.objectid
, key
.type
, key
.offset
);
905 btrfs_del_item(trans
, root
, path
);
907 fprintf(stdout
, "Corrupting key and data [%llu, %u, %llu].\n",
908 key
.objectid
, key
.type
, key
.offset
);
909 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
910 item_size
= btrfs_item_size_nr(leaf
, slot
);
911 memset_extent_buffer(leaf
, 0, ptr
, item_size
);
912 btrfs_mark_buffer_dirty(leaf
);
916 static int corrupt_chunk_tree(struct btrfs_trans_handle
*trans
,
917 struct btrfs_root
*root
)
922 struct btrfs_path
*path
;
923 struct btrfs_key key
;
924 struct btrfs_key found_key
;
925 struct extent_buffer
*leaf
;
927 path
= btrfs_alloc_path();
931 key
.objectid
= (u64
)-1;
932 key
.offset
= (u64
)-1;
935 /* Here, cow and ins_len must equals 0 for the following reasons:
936 * 1) chunk recover is based on disk scanning, so COW should be
937 * disabled in case the original chunk being scanned and
938 * recovered using the old chunk.
939 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
942 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
945 fprintf(stderr
, "Error searching tree\n");
948 /* corrupt/del dev_item first */
949 while (!btrfs_previous_item(root
, path
, 0, BTRFS_DEV_ITEM_KEY
)) {
950 slot
= path
->slots
[0];
951 leaf
= path
->nodes
[0];
953 /* Never delete the first item to keep the leaf structure */
954 if (path
->slots
[0] == 0)
956 ret
= corrupt_item_nocow(trans
, root
, path
, del
);
960 btrfs_release_path(path
);
962 /* Here, cow and ins_len must equals 0 for the following reasons:
963 * 1) chunk recover is based on disk scanning, so COW should be
964 * disabled in case the original chunk being scanned and
965 * recovered using the old chunk.
966 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
969 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
972 fprintf(stderr
, "Error searching tree\n");
975 /* corrupt/del chunk then*/
976 while (!btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
)) {
977 slot
= path
->slots
[0];
978 leaf
= path
->nodes
[0];
980 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
981 ret
= corrupt_item_nocow(trans
, root
, path
, del
);
986 btrfs_free_path(path
);
989 static int find_chunk_offset(struct btrfs_root
*root
,
990 struct btrfs_path
*path
, u64 offset
)
992 struct btrfs_key key
;
995 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
996 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
999 /* Here, cow and ins_len must equals 0 for following reasons:
1000 * 1) chunk recover is based on disk scanning, so COW should
1001 * be disabled in case the original chunk being scanned
1002 * and recovered using the old chunk.
1003 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
1004 * will be triggered.
1006 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1008 fprintf(stderr
, "Can't find chunk with given offset %llu\n",
1013 fprintf(stderr
, "Error searching chunk\n");
1020 int main(int ac
, char **av
)
1022 struct cache_tree root_cache
;
1023 struct btrfs_key key
;
1024 struct btrfs_root
*root
;
1025 struct extent_buffer
*eb
;
1027 /* chunk offset can be 0,so change to (u64)-1 */
1028 u64 logical
= (u64
)-1;
1033 int extent_tree
= 0;
1034 int corrupt_block_keys
= 0;
1037 int corrupt_item
= 0;
1040 u64 metadata_block
= 0;
1042 u64 file_extent
= (u64
)-1;
1043 u64 root_objectid
= 0;
1044 u64 csum_bytenr
= 0;
1045 char field
[FIELD_BUF_LEN
];
1049 memset(&key
, 0, sizeof(key
));
1053 static const struct option long_options
[] = {
1054 /* { "byte-count", 1, NULL, 'b' }, */
1055 { "logical", required_argument
, NULL
, 'l' },
1056 { "copy", required_argument
, NULL
, 'c' },
1057 { "bytes", required_argument
, NULL
, 'b' },
1058 { "extent-record", no_argument
, NULL
, 'e' },
1059 { "extent-tree", no_argument
, NULL
, 'E' },
1060 { "keys", no_argument
, NULL
, 'k' },
1061 { "chunk-record", no_argument
, NULL
, 'u' },
1062 { "chunk-tree", no_argument
, NULL
, 'U' },
1063 { "inode", required_argument
, NULL
, 'i'},
1064 { "file-extent", required_argument
, NULL
, 'x'},
1065 { "metadata-block", required_argument
, NULL
, 'm'},
1066 { "field", required_argument
, NULL
, 'f'},
1067 { "key", required_argument
, NULL
, 'K'},
1068 { "item", no_argument
, NULL
, 'I'},
1069 { "dir-item", no_argument
, NULL
, 'D'},
1070 { "delete", no_argument
, NULL
, 'd'},
1071 { "root", no_argument
, NULL
, 'r'},
1072 { "csum", required_argument
, NULL
, 'C'},
1073 { "help", no_argument
, NULL
, GETOPT_VAL_HELP
},
1074 { NULL
, 0, NULL
, 0 }
1077 c
= getopt_long(ac
, av
, "l:c:b:eEkuUi:f:x:m:K:IDdr:C:",
1078 long_options
, NULL
);
1083 logical
= arg_strtou64(optarg
);
1086 copy
= arg_strtou64(optarg
);
1089 bytes
= arg_strtou64(optarg
);
1098 corrupt_block_keys
= 1;
1107 inode
= arg_strtou64(optarg
);
1110 strncpy(field
, optarg
, FIELD_BUF_LEN
);
1113 file_extent
= arg_strtou64(optarg
);
1116 metadata_block
= arg_strtou64(optarg
);
1119 ret
= sscanf(optarg
, "%llu,%u,%llu",
1121 (unsigned int *)&key
.type
,
1124 fprintf(stderr
, "error reading key "
1139 root_objectid
= arg_strtou64(optarg
);
1142 csum_bytenr
= arg_strtou64(optarg
);
1144 case GETOPT_VAL_HELP
:
1146 print_usage(c
!= GETOPT_VAL_HELP
);
1151 if (check_argc_min(ac
, 1))
1156 cache_tree_init(&root_cache
);
1158 root
= open_ctree(dev
, 0, OPEN_CTREE_WRITES
);
1160 fprintf(stderr
, "Open ctree failed\n");
1164 struct btrfs_trans_handle
*trans
;
1166 if (logical
== (u64
)-1)
1168 trans
= btrfs_start_transaction(root
, 1);
1169 ret
= corrupt_extent (trans
, root
, logical
, 0);
1170 btrfs_commit_transaction(trans
, root
);
1174 struct btrfs_trans_handle
*trans
;
1175 trans
= btrfs_start_transaction(root
, 1);
1176 btrfs_corrupt_extent_tree(trans
, root
->fs_info
->extent_root
,
1177 root
->fs_info
->extent_root
->node
);
1178 btrfs_commit_transaction(trans
, root
);
1182 struct btrfs_trans_handle
*trans
;
1183 struct btrfs_path
*path
;
1186 if (logical
== (u64
)-1)
1189 path
= btrfs_alloc_path();
1191 fprintf(stderr
, "path allocation failed\n");
1195 if (find_chunk_offset(root
->fs_info
->chunk_root
, path
,
1197 btrfs_free_path(path
);
1200 trans
= btrfs_start_transaction(root
, 1);
1201 ret
= corrupt_item_nocow(trans
, root
->fs_info
->chunk_root
,
1204 fprintf(stderr
, "Failed to corrupt chunk record\n");
1205 btrfs_commit_transaction(trans
, root
);
1209 struct btrfs_trans_handle
*trans
;
1210 trans
= btrfs_start_transaction(root
, 1);
1211 ret
= corrupt_chunk_tree(trans
, root
->fs_info
->chunk_root
);
1213 fprintf(stderr
, "Failed to corrupt chunk tree\n");
1214 btrfs_commit_transaction(trans
, root
);
1218 struct btrfs_trans_handle
*trans
;
1223 trans
= btrfs_start_transaction(root
, 1);
1224 if (file_extent
== (u64
)-1) {
1225 printf("corrupting inode\n");
1226 ret
= corrupt_inode(trans
, root
, inode
, field
);
1228 printf("corrupting file extent\n");
1229 ret
= corrupt_file_extent(trans
, root
, inode
,
1230 file_extent
, field
);
1232 btrfs_commit_transaction(trans
, root
);
1235 if (metadata_block
) {
1238 ret
= corrupt_metadata_block(root
, metadata_block
, field
);
1242 if (!key
.objectid
|| !strlen(field
))
1244 ret
= corrupt_dir_item(root
, &key
, field
);
1248 ret
= delete_csum(root
, csum_bytenr
, bytes
);
1254 ret
= corrupt_btrfs_item(root
, &key
, field
);
1257 struct btrfs_root
*target
= root
;
1261 if (root_objectid
) {
1262 struct btrfs_key root_key
;
1264 root_key
.objectid
= root_objectid
;
1265 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
1266 root_key
.offset
= (u64
)-1;
1268 target
= btrfs_read_fs_root(root
->fs_info
, &root_key
);
1269 if (IS_ERR(target
)) {
1270 fprintf(stderr
, "Couldn't find root %llu\n",
1271 (unsigned long long)root_objectid
);
1275 ret
= delete_item(target
, &key
);
1278 if (key
.objectid
|| key
.offset
|| key
.type
) {
1281 ret
= corrupt_key(root
, &key
, field
);
1285 * If we made it here and we have extent set then we didn't specify
1286 * inode and we're screwed.
1288 if (file_extent
!= (u64
)-1)
1291 if (logical
== (u64
)-1)
1295 bytes
= root
->sectorsize
;
1297 bytes
= (bytes
+ root
->sectorsize
- 1) / root
->sectorsize
;
1298 bytes
*= root
->sectorsize
;
1301 if (corrupt_block_keys
) {
1302 corrupt_keys_in_block(root
, logical
);
1304 eb
= debug_corrupt_block(root
, logical
,
1305 root
->sectorsize
, copy
);
1306 free_extent_buffer(eb
);
1308 logical
+= root
->sectorsize
;
1309 bytes
-= root
->sectorsize
;