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_root
*root
,
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(root
->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_root
*root
, u64 bytenr
)
170 struct extent_buffer
*eb
;
172 eb
= read_tree_block(root
, bytenr
, root
->nodesize
, 0);
173 if (!extent_buffer_uptodate(eb
))
176 corrupt_keys(NULL
, root
, 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
)
286 if (btrfs_is_leaf(eb
)) {
287 btrfs_corrupt_extent_leaf(trans
, root
, eb
);
291 if (btrfs_header_level(eb
) == 1 && eb
!= root
->node
) {
296 for (i
= 0; i
< btrfs_header_nritems(eb
); i
++) {
297 struct extent_buffer
*next
;
299 next
= read_tree_block(root
, btrfs_node_blockptr(eb
, i
),
301 btrfs_node_ptr_generation(eb
, i
));
302 if (!extent_buffer_uptodate(next
))
304 btrfs_corrupt_extent_tree(trans
, root
, next
);
305 free_extent_buffer(next
);
309 enum btrfs_inode_field
{
310 BTRFS_INODE_FIELD_ISIZE
,
311 BTRFS_INODE_FIELD_NBYTES
,
312 BTRFS_INODE_FIELD_NLINK
,
313 BTRFS_INODE_FIELD_GENERATION
,
314 BTRFS_INODE_FIELD_TRANSID
,
315 BTRFS_INODE_FIELD_BLOCK_GROUP
,
316 BTRFS_INODE_FIELD_MODE
,
317 BTRFS_INODE_FIELD_UID
,
318 BTRFS_INODE_FIELD_GID
,
319 BTRFS_INODE_FIELD_BAD
,
322 enum btrfs_file_extent_field
{
323 BTRFS_FILE_EXTENT_DISK_BYTENR
,
324 BTRFS_FILE_EXTENT_BAD
,
327 enum btrfs_dir_item_field
{
329 BTRFS_DIR_ITEM_LOCATION_OBJECTID
,
333 enum btrfs_metadata_block_field
{
334 BTRFS_METADATA_BLOCK_GENERATION
,
335 BTRFS_METADATA_BLOCK_SHIFT_ITEMS
,
336 BTRFS_METADATA_BLOCK_BAD
,
339 enum btrfs_item_field
{
344 enum btrfs_key_field
{
351 static enum btrfs_inode_field
convert_inode_field(char *field
)
353 if (!strncmp(field
, "isize", FIELD_BUF_LEN
))
354 return BTRFS_INODE_FIELD_ISIZE
;
355 if (!strncmp(field
, "nbytes", FIELD_BUF_LEN
))
356 return BTRFS_INODE_FIELD_NBYTES
;
357 if (!strncmp(field
, "nlink", FIELD_BUF_LEN
))
358 return BTRFS_INODE_FIELD_NLINK
;
359 if (!strncmp(field
, "generation", FIELD_BUF_LEN
))
360 return BTRFS_INODE_FIELD_GENERATION
;
361 if (!strncmp(field
, "transid", FIELD_BUF_LEN
))
362 return BTRFS_INODE_FIELD_TRANSID
;
363 if (!strncmp(field
, "block_group", FIELD_BUF_LEN
))
364 return BTRFS_INODE_FIELD_BLOCK_GROUP
;
365 if (!strncmp(field
, "mode", FIELD_BUF_LEN
))
366 return BTRFS_INODE_FIELD_MODE
;
367 if (!strncmp(field
, "uid", FIELD_BUF_LEN
))
368 return BTRFS_INODE_FIELD_UID
;
369 if (!strncmp(field
, "gid", FIELD_BUF_LEN
))
370 return BTRFS_INODE_FIELD_GID
;
371 return BTRFS_INODE_FIELD_BAD
;
374 static enum btrfs_file_extent_field
convert_file_extent_field(char *field
)
376 if (!strncmp(field
, "disk_bytenr", FIELD_BUF_LEN
))
377 return BTRFS_FILE_EXTENT_DISK_BYTENR
;
378 return BTRFS_FILE_EXTENT_BAD
;
381 static enum btrfs_metadata_block_field
382 convert_metadata_block_field(char *field
)
384 if (!strncmp(field
, "generation", FIELD_BUF_LEN
))
385 return BTRFS_METADATA_BLOCK_GENERATION
;
386 if (!strncmp(field
, "shift_items", FIELD_BUF_LEN
))
387 return BTRFS_METADATA_BLOCK_SHIFT_ITEMS
;
388 return BTRFS_METADATA_BLOCK_BAD
;
391 static enum btrfs_key_field
convert_key_field(char *field
)
393 if (!strncmp(field
, "objectid", FIELD_BUF_LEN
))
394 return BTRFS_KEY_OBJECTID
;
395 if (!strncmp(field
, "type", FIELD_BUF_LEN
))
396 return BTRFS_KEY_TYPE
;
397 if (!strncmp(field
, "offset", FIELD_BUF_LEN
))
398 return BTRFS_KEY_OFFSET
;
399 return BTRFS_KEY_BAD
;
402 static enum btrfs_item_field
convert_item_field(char *field
)
404 if (!strncmp(field
, "offset", FIELD_BUF_LEN
))
405 return BTRFS_ITEM_OFFSET
;
406 return BTRFS_ITEM_BAD
;
409 static enum btrfs_dir_item_field
convert_dir_item_field(char *field
)
411 if (!strncmp(field
, "name", FIELD_BUF_LEN
))
412 return BTRFS_DIR_ITEM_NAME
;
413 if (!strncmp(field
, "location_objectid", FIELD_BUF_LEN
))
414 return BTRFS_DIR_ITEM_LOCATION_OBJECTID
;
415 return BTRFS_DIR_ITEM_BAD
;
418 static u64
generate_u64(u64 orig
)
423 } while (ret
== orig
);
427 static u32
generate_u32(u32 orig
)
432 } while (ret
== orig
);
436 static u8
generate_u8(u8 orig
)
441 } while (ret
== orig
);
445 static int corrupt_key(struct btrfs_root
*root
, struct btrfs_key
*key
,
448 enum btrfs_key_field corrupt_field
= convert_key_field(field
);
449 struct btrfs_path
*path
;
450 struct btrfs_trans_handle
*trans
;
453 root
= root
->fs_info
->fs_root
;
454 if (corrupt_field
== BTRFS_KEY_BAD
) {
455 fprintf(stderr
, "Invalid field %s\n", field
);
459 path
= btrfs_alloc_path();
463 trans
= btrfs_start_transaction(root
, 1);
465 btrfs_free_path(path
);
466 return PTR_ERR(trans
);
469 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
473 fprintf(stderr
, "Couldn't find the key to corrupt\n");
478 switch (corrupt_field
) {
479 case BTRFS_KEY_OBJECTID
:
480 key
->objectid
= generate_u64(key
->objectid
);
483 key
->type
= generate_u8(key
->type
);
485 case BTRFS_KEY_OFFSET
:
486 key
->offset
= generate_u64(key
->objectid
);
489 fprintf(stderr
, "Invalid field %s, %d\n", field
,
495 btrfs_set_item_key_unsafe(root
, path
, key
);
497 btrfs_free_path(path
);
498 btrfs_commit_transaction(trans
, root
);
502 static int corrupt_dir_item(struct btrfs_root
*root
, struct btrfs_key
*key
,
505 struct btrfs_trans_handle
*trans
;
506 struct btrfs_dir_item
*di
;
507 struct btrfs_path
*path
;
509 struct btrfs_key location
;
510 struct btrfs_disk_key disk_key
;
511 unsigned long name_ptr
;
512 enum btrfs_dir_item_field corrupt_field
=
513 convert_dir_item_field(field
);
518 if (corrupt_field
== BTRFS_DIR_ITEM_BAD
) {
519 fprintf(stderr
, "Invalid field %s\n", field
);
523 path
= btrfs_alloc_path();
527 trans
= btrfs_start_transaction(root
, 1);
529 btrfs_free_path(path
);
530 return PTR_ERR(trans
);
533 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
537 fprintf(stderr
, "Error searching for dir item %d\n", ret
);
541 di
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
542 struct btrfs_dir_item
);
544 switch (corrupt_field
) {
545 case BTRFS_DIR_ITEM_NAME
:
546 name_len
= btrfs_dir_name_len(path
->nodes
[0], di
);
547 name_ptr
= (unsigned long)(di
+ 1);
548 read_extent_buffer(path
->nodes
[0], name
, name_ptr
, name_len
);
550 write_extent_buffer(path
->nodes
[0], name
, name_ptr
, name_len
);
551 btrfs_mark_buffer_dirty(path
->nodes
[0]);
553 case BTRFS_DIR_ITEM_LOCATION_OBJECTID
:
554 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
555 bogus
= generate_u64(location
.objectid
);
556 location
.objectid
= bogus
;
557 btrfs_cpu_key_to_disk(&disk_key
, &location
);
558 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
559 btrfs_mark_buffer_dirty(path
->nodes
[0]);
566 btrfs_commit_transaction(trans
, root
);
567 btrfs_free_path(path
);
571 static int corrupt_inode(struct btrfs_trans_handle
*trans
,
572 struct btrfs_root
*root
, u64 inode
, char *field
)
574 struct btrfs_inode_item
*ei
;
575 struct btrfs_path
*path
;
576 struct btrfs_key key
;
577 enum btrfs_inode_field corrupt_field
= convert_inode_field(field
);
582 if (corrupt_field
== BTRFS_INODE_FIELD_BAD
) {
583 fprintf(stderr
, "Invalid field %s\n", field
);
587 key
.objectid
= inode
;
588 key
.type
= BTRFS_INODE_ITEM_KEY
;
589 key
.offset
= (u64
)-1;
591 path
= btrfs_alloc_path();
595 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
599 if (!path
->slots
[0]) {
600 fprintf(stderr
, "Couldn't find inode %Lu\n", inode
);
608 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
609 if (key
.objectid
!= inode
) {
610 fprintf(stderr
, "Couldn't find inode %Lu\n", inode
);
615 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
616 struct btrfs_inode_item
);
617 switch (corrupt_field
) {
618 case BTRFS_INODE_FIELD_ISIZE
:
619 orig
= btrfs_inode_size(path
->nodes
[0], ei
);
620 bogus
= generate_u64(orig
);
621 btrfs_set_inode_size(path
->nodes
[0], ei
, bogus
);
623 case BTRFS_INODE_FIELD_NBYTES
:
624 orig
= btrfs_inode_nbytes(path
->nodes
[0], ei
);
625 bogus
= generate_u64(orig
);
626 btrfs_set_inode_nbytes(path
->nodes
[0], ei
, bogus
);
628 case BTRFS_INODE_FIELD_NLINK
:
629 orig
= btrfs_inode_nlink(path
->nodes
[0], ei
);
630 bogus
= generate_u32(orig
);
631 btrfs_set_inode_nlink(path
->nodes
[0], ei
, bogus
);
633 case BTRFS_INODE_FIELD_GENERATION
:
634 orig
= btrfs_inode_generation(path
->nodes
[0], ei
);
635 bogus
= generate_u64(orig
);
636 btrfs_set_inode_generation(path
->nodes
[0], ei
, bogus
);
638 case BTRFS_INODE_FIELD_TRANSID
:
639 orig
= btrfs_inode_transid(path
->nodes
[0], ei
);
640 bogus
= generate_u64(orig
);
641 btrfs_set_inode_transid(path
->nodes
[0], ei
, bogus
);
643 case BTRFS_INODE_FIELD_BLOCK_GROUP
:
644 orig
= btrfs_inode_block_group(path
->nodes
[0], ei
);
645 bogus
= generate_u64(orig
);
646 btrfs_set_inode_block_group(path
->nodes
[0], ei
, bogus
);
648 case BTRFS_INODE_FIELD_MODE
:
649 orig
= btrfs_inode_mode(path
->nodes
[0], ei
);
650 bogus
= generate_u32(orig
);
651 btrfs_set_inode_mode(path
->nodes
[0], ei
, bogus
);
653 case BTRFS_INODE_FIELD_UID
:
654 orig
= btrfs_inode_uid(path
->nodes
[0], ei
);
655 bogus
= generate_u32(orig
);
656 btrfs_set_inode_uid(path
->nodes
[0], ei
, bogus
);
658 case BTRFS_INODE_FIELD_GID
:
659 orig
= btrfs_inode_gid(path
->nodes
[0], ei
);
660 bogus
= generate_u32(orig
);
661 btrfs_set_inode_gid(path
->nodes
[0], ei
, bogus
);
667 btrfs_mark_buffer_dirty(path
->nodes
[0]);
669 btrfs_free_path(path
);
673 static int corrupt_file_extent(struct btrfs_trans_handle
*trans
,
674 struct btrfs_root
*root
, u64 inode
, u64 extent
,
677 struct btrfs_file_extent_item
*fi
;
678 struct btrfs_path
*path
;
679 struct btrfs_key key
;
680 enum btrfs_file_extent_field corrupt_field
;
685 corrupt_field
= convert_file_extent_field(field
);
686 if (corrupt_field
== BTRFS_FILE_EXTENT_BAD
) {
687 fprintf(stderr
, "Invalid field %s\n", field
);
691 key
.objectid
= inode
;
692 key
.type
= BTRFS_EXTENT_DATA_KEY
;
695 path
= btrfs_alloc_path();
699 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
703 fprintf(stderr
, "Couldn't find extent %llu for inode %llu\n",
709 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
710 struct btrfs_file_extent_item
);
711 switch (corrupt_field
) {
712 case BTRFS_FILE_EXTENT_DISK_BYTENR
:
713 orig
= btrfs_file_extent_disk_bytenr(path
->nodes
[0], fi
);
714 bogus
= generate_u64(orig
);
715 btrfs_set_file_extent_disk_bytenr(path
->nodes
[0], fi
, bogus
);
721 btrfs_mark_buffer_dirty(path
->nodes
[0]);
723 btrfs_free_path(path
);
727 static void shift_items(struct btrfs_root
*root
, struct extent_buffer
*eb
)
729 int nritems
= btrfs_header_nritems(eb
);
730 int shift_space
= btrfs_leaf_free_space(root
, eb
) / 2;
731 int slot
= nritems
/ 2;
733 unsigned int data_end
= btrfs_item_offset_nr(eb
, nritems
- 1);
735 /* Shift the item data up to and including slot back by shift space */
736 memmove_extent_buffer(eb
, btrfs_leaf_data(eb
) + data_end
- shift_space
,
737 btrfs_leaf_data(eb
) + data_end
,
738 btrfs_item_offset_nr(eb
, slot
- 1) - data_end
);
740 /* Now update the item pointers. */
741 for (i
= nritems
- 1; i
>= slot
; i
--) {
742 u32 offset
= btrfs_item_offset_nr(eb
, i
);
743 offset
-= shift_space
;
744 btrfs_set_item_offset(eb
, btrfs_item_nr(i
), offset
);
748 static int corrupt_metadata_block(struct btrfs_root
*root
, u64 block
,
751 struct btrfs_trans_handle
*trans
;
752 struct btrfs_path
*path
;
753 struct extent_buffer
*eb
;
754 struct btrfs_key key
, root_key
;
755 enum btrfs_metadata_block_field corrupt_field
;
761 corrupt_field
= convert_metadata_block_field(field
);
762 if (corrupt_field
== BTRFS_METADATA_BLOCK_BAD
) {
763 fprintf(stderr
, "Invalid field %s\n", field
);
767 eb
= read_tree_block(root
, block
, root
->nodesize
, 0);
768 if (!extent_buffer_uptodate(eb
)) {
769 fprintf(stderr
, "Couldn't read in tree block %s\n", field
);
772 root_objectid
= btrfs_header_owner(eb
);
773 level
= btrfs_header_level(eb
);
775 btrfs_node_key_to_cpu(eb
, &key
, 0);
777 btrfs_item_key_to_cpu(eb
, &key
, 0);
778 free_extent_buffer(eb
);
780 root_key
.objectid
= root_objectid
;
781 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
782 root_key
.offset
= (u64
)-1;
784 root
= btrfs_read_fs_root(root
->fs_info
, &root_key
);
786 fprintf(stderr
, "Couldn't find owner root %llu\n",
788 return PTR_ERR(root
);
791 path
= btrfs_alloc_path();
795 trans
= btrfs_start_transaction(root
, 1);
797 btrfs_free_path(path
);
798 fprintf(stderr
, "Couldn't start transaction %ld\n",
800 return PTR_ERR(trans
);
803 path
->lowest_level
= level
;
804 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
806 fprintf(stderr
, "Error searching to node %d\n", ret
);
809 eb
= path
->nodes
[level
];
812 switch (corrupt_field
) {
813 case BTRFS_METADATA_BLOCK_GENERATION
:
814 orig
= btrfs_header_generation(eb
);
815 bogus
= generate_u64(orig
);
816 btrfs_set_header_generation(eb
, bogus
);
818 case BTRFS_METADATA_BLOCK_SHIFT_ITEMS
:
819 shift_items(root
, path
->nodes
[level
]);
825 btrfs_mark_buffer_dirty(path
->nodes
[level
]);
827 btrfs_commit_transaction(trans
, root
);
828 btrfs_free_path(path
);
832 static int corrupt_btrfs_item(struct btrfs_root
*root
, struct btrfs_key
*key
,
835 struct btrfs_trans_handle
*trans
;
836 struct btrfs_path
*path
;
837 enum btrfs_item_field corrupt_field
;
841 corrupt_field
= convert_item_field(field
);
842 if (corrupt_field
== BTRFS_ITEM_BAD
) {
843 fprintf(stderr
, "Invalid field %s\n", field
);
847 path
= btrfs_alloc_path();
851 trans
= btrfs_start_transaction(root
, 1);
853 btrfs_free_path(path
);
854 fprintf(stderr
, "Couldn't start transaction %ld\n",
856 return PTR_ERR(trans
);
859 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
861 fprintf(stderr
, "Error searching to node %d\n", ret
);
866 switch (corrupt_field
) {
867 case BTRFS_ITEM_OFFSET
:
868 orig
= btrfs_item_offset_nr(path
->nodes
[0], path
->slots
[0]);
869 bogus
= generate_u32(orig
);
870 btrfs_set_item_offset(path
->nodes
[0],
871 btrfs_item_nr(path
->slots
[0]), bogus
);
877 btrfs_mark_buffer_dirty(path
->nodes
[0]);
879 btrfs_commit_transaction(trans
, root
);
880 btrfs_free_path(path
);
884 static int delete_item(struct btrfs_root
*root
, struct btrfs_key
*key
)
886 struct btrfs_trans_handle
*trans
;
887 struct btrfs_path
*path
;
890 path
= btrfs_alloc_path();
894 trans
= btrfs_start_transaction(root
, 1);
896 btrfs_free_path(path
);
897 fprintf(stderr
, "Couldn't start transaction %ld\n",
899 return PTR_ERR(trans
);
902 ret
= btrfs_search_slot(trans
, root
, key
, path
, -1, 1);
906 fprintf(stderr
, "Error searching to node %d\n", ret
);
909 ret
= btrfs_del_item(trans
, root
, path
);
910 btrfs_mark_buffer_dirty(path
->nodes
[0]);
912 btrfs_commit_transaction(trans
, root
);
913 btrfs_free_path(path
);
917 static int delete_csum(struct btrfs_root
*root
, u64 bytenr
, u64 bytes
)
919 struct btrfs_trans_handle
*trans
;
922 root
= root
->fs_info
->csum_root
;
923 trans
= btrfs_start_transaction(root
, 1);
925 fprintf(stderr
, "Couldn't start transaction %ld\n",
927 return PTR_ERR(trans
);
930 ret
= btrfs_del_csums(trans
, root
, bytenr
, bytes
);
932 fprintf(stderr
, "Error deleting csums %d\n", ret
);
933 btrfs_commit_transaction(trans
, root
);
937 /* corrupt item using NO cow.
938 * Because chunk recover will recover based on whole partition scanning,
939 * If using COW, chunk recover will use the old item to recover,
940 * which is still OK but we want to check the ability to rebuild chunk
941 * not only restore the old ones */
942 static int corrupt_item_nocow(struct btrfs_trans_handle
*trans
,
943 struct btrfs_root
*root
, struct btrfs_path
*path
,
947 struct btrfs_key key
;
948 struct extent_buffer
*leaf
;
953 leaf
= path
->nodes
[0];
954 slot
= path
->slots
[0];
955 /* Not deleting the first item of a leaf to keep leaf structure */
958 /* Only accept valid eb */
959 if (slot
>= btrfs_header_nritems(leaf
)) {
960 error("invalid eb: no data or slot out of range: %d >= %d",
961 slot
, btrfs_header_nritems(leaf
));
964 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
966 fprintf(stdout
, "Deleting key and data [%llu, %u, %llu].\n",
967 key
.objectid
, key
.type
, key
.offset
);
968 btrfs_del_item(trans
, root
, path
);
970 fprintf(stdout
, "Corrupting key and data [%llu, %u, %llu].\n",
971 key
.objectid
, key
.type
, key
.offset
);
972 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
973 item_size
= btrfs_item_size_nr(leaf
, slot
);
974 memset_extent_buffer(leaf
, 0, ptr
, item_size
);
975 btrfs_mark_buffer_dirty(leaf
);
979 static int corrupt_chunk_tree(struct btrfs_trans_handle
*trans
,
980 struct btrfs_root
*root
)
985 struct btrfs_path
*path
;
986 struct btrfs_key key
;
987 struct btrfs_key found_key
;
988 struct extent_buffer
*leaf
;
990 path
= btrfs_alloc_path();
994 key
.objectid
= (u64
)-1;
995 key
.offset
= (u64
)-1;
998 /* Here, cow and ins_len must equals 0 for the following reasons:
999 * 1) chunk recover is based on disk scanning, so COW should be
1000 * disabled in case the original chunk being scanned and
1001 * recovered using the old chunk.
1002 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
1005 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
1008 fprintf(stderr
, "Error searching tree\n");
1011 /* corrupt/del dev_item first */
1012 while (!btrfs_previous_item(root
, path
, 0, BTRFS_DEV_ITEM_KEY
)) {
1013 slot
= path
->slots
[0];
1014 leaf
= path
->nodes
[0];
1015 del
= rand_range(3);
1016 /* Never delete the first item to keep the leaf structure */
1017 if (path
->slots
[0] == 0)
1019 ret
= corrupt_item_nocow(trans
, root
, path
, del
);
1023 btrfs_release_path(path
);
1025 /* Here, cow and ins_len must equals 0 for the following reasons:
1026 * 1) chunk recover is based on disk scanning, so COW should be
1027 * disabled in case the original chunk being scanned and
1028 * recovered using the old chunk.
1029 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
1032 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
1035 fprintf(stderr
, "Error searching tree\n");
1038 /* corrupt/del chunk then*/
1039 while (!btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
)) {
1040 slot
= path
->slots
[0];
1041 leaf
= path
->nodes
[0];
1042 del
= rand_range(3);
1043 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1044 ret
= corrupt_item_nocow(trans
, root
, path
, del
);
1049 btrfs_free_path(path
);
1052 static int find_chunk_offset(struct btrfs_root
*root
,
1053 struct btrfs_path
*path
, u64 offset
)
1055 struct btrfs_key key
;
1058 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1059 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1060 key
.offset
= offset
;
1062 /* Here, cow and ins_len must equals 0 for following reasons:
1063 * 1) chunk recover is based on disk scanning, so COW should
1064 * be disabled in case the original chunk being scanned
1065 * and recovered using the old chunk.
1066 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
1067 * will be triggered.
1069 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1071 fprintf(stderr
, "Can't find chunk with given offset %llu\n",
1076 fprintf(stderr
, "Error searching chunk\n");
1083 int main(int argc
, char **argv
)
1085 struct cache_tree root_cache
;
1086 struct btrfs_key key
;
1087 struct btrfs_root
*root
;
1089 /* chunk offset can be 0,so change to (u64)-1 */
1090 u64 logical
= (u64
)-1;
1095 int extent_tree
= 0;
1096 int corrupt_block_keys
= 0;
1099 int corrupt_item
= 0;
1102 u64 metadata_block
= 0;
1104 u64 file_extent
= (u64
)-1;
1105 u64 root_objectid
= 0;
1106 u64 csum_bytenr
= 0;
1107 char field
[FIELD_BUF_LEN
];
1110 memset(&key
, 0, sizeof(key
));
1114 static const struct option long_options
[] = {
1115 /* { "byte-count", 1, NULL, 'b' }, */
1116 { "logical", required_argument
, NULL
, 'l' },
1117 { "copy", required_argument
, NULL
, 'c' },
1118 { "bytes", required_argument
, NULL
, 'b' },
1119 { "extent-record", no_argument
, NULL
, 'e' },
1120 { "extent-tree", no_argument
, NULL
, 'E' },
1121 { "keys", no_argument
, NULL
, 'k' },
1122 { "chunk-record", no_argument
, NULL
, 'u' },
1123 { "chunk-tree", no_argument
, NULL
, 'U' },
1124 { "inode", required_argument
, NULL
, 'i'},
1125 { "file-extent", required_argument
, NULL
, 'x'},
1126 { "metadata-block", required_argument
, NULL
, 'm'},
1127 { "field", required_argument
, NULL
, 'f'},
1128 { "key", required_argument
, NULL
, 'K'},
1129 { "item", no_argument
, NULL
, 'I'},
1130 { "dir-item", no_argument
, NULL
, 'D'},
1131 { "delete", no_argument
, NULL
, 'd'},
1132 { "root", no_argument
, NULL
, 'r'},
1133 { "csum", required_argument
, NULL
, 'C'},
1134 { "help", no_argument
, NULL
, GETOPT_VAL_HELP
},
1135 { NULL
, 0, NULL
, 0 }
1138 c
= getopt_long(argc
, argv
, "l:c:b:eEkuUi:f:x:m:K:IDdr:C:",
1139 long_options
, NULL
);
1144 logical
= arg_strtou64(optarg
);
1147 copy
= arg_strtou64(optarg
);
1150 bytes
= arg_strtou64(optarg
);
1159 corrupt_block_keys
= 1;
1168 inode
= arg_strtou64(optarg
);
1171 strncpy(field
, optarg
, FIELD_BUF_LEN
);
1174 file_extent
= arg_strtou64(optarg
);
1177 metadata_block
= arg_strtou64(optarg
);
1180 ret
= sscanf(optarg
, "%llu,%u,%llu",
1182 (unsigned int *)&key
.type
,
1185 fprintf(stderr
, "error reading key "
1200 root_objectid
= arg_strtou64(optarg
);
1203 csum_bytenr
= arg_strtou64(optarg
);
1205 case GETOPT_VAL_HELP
:
1207 print_usage(c
!= GETOPT_VAL_HELP
);
1211 if (check_argc_min(argc
- optind
, 1))
1216 cache_tree_init(&root_cache
);
1218 root
= open_ctree(dev
, 0, OPEN_CTREE_WRITES
);
1220 fprintf(stderr
, "Open ctree failed\n");
1224 struct btrfs_trans_handle
*trans
;
1226 if (logical
== (u64
)-1)
1228 trans
= btrfs_start_transaction(root
, 1);
1229 ret
= corrupt_extent(trans
, root
, logical
);
1230 btrfs_commit_transaction(trans
, root
);
1234 struct btrfs_trans_handle
*trans
;
1235 trans
= btrfs_start_transaction(root
, 1);
1236 btrfs_corrupt_extent_tree(trans
, root
->fs_info
->extent_root
,
1237 root
->fs_info
->extent_root
->node
);
1238 btrfs_commit_transaction(trans
, root
);
1242 struct btrfs_trans_handle
*trans
;
1243 struct btrfs_path
*path
;
1246 if (logical
== (u64
)-1)
1248 del
= rand_range(3);
1249 path
= btrfs_alloc_path();
1251 fprintf(stderr
, "path allocation failed\n");
1255 if (find_chunk_offset(root
->fs_info
->chunk_root
, path
,
1257 btrfs_free_path(path
);
1260 trans
= btrfs_start_transaction(root
, 1);
1261 ret
= corrupt_item_nocow(trans
, root
->fs_info
->chunk_root
,
1264 fprintf(stderr
, "Failed to corrupt chunk record\n");
1265 btrfs_commit_transaction(trans
, root
);
1269 struct btrfs_trans_handle
*trans
;
1270 trans
= btrfs_start_transaction(root
, 1);
1271 ret
= corrupt_chunk_tree(trans
, root
->fs_info
->chunk_root
);
1273 fprintf(stderr
, "Failed to corrupt chunk tree\n");
1274 btrfs_commit_transaction(trans
, root
);
1278 struct btrfs_trans_handle
*trans
;
1283 trans
= btrfs_start_transaction(root
, 1);
1284 if (file_extent
== (u64
)-1) {
1285 printf("corrupting inode\n");
1286 ret
= corrupt_inode(trans
, root
, inode
, field
);
1288 printf("corrupting file extent\n");
1289 ret
= corrupt_file_extent(trans
, root
, inode
,
1290 file_extent
, field
);
1292 btrfs_commit_transaction(trans
, root
);
1295 if (metadata_block
) {
1298 ret
= corrupt_metadata_block(root
, metadata_block
, field
);
1302 if (!key
.objectid
|| *field
== 0)
1304 ret
= corrupt_dir_item(root
, &key
, field
);
1308 ret
= delete_csum(root
, csum_bytenr
, bytes
);
1314 ret
= corrupt_btrfs_item(root
, &key
, field
);
1317 struct btrfs_root
*target
= root
;
1321 if (root_objectid
) {
1322 struct btrfs_key root_key
;
1324 root_key
.objectid
= root_objectid
;
1325 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
1326 root_key
.offset
= (u64
)-1;
1328 target
= btrfs_read_fs_root(root
->fs_info
, &root_key
);
1329 if (IS_ERR(target
)) {
1330 fprintf(stderr
, "Couldn't find root %llu\n",
1331 (unsigned long long)root_objectid
);
1335 ret
= delete_item(target
, &key
);
1338 if (key
.objectid
|| key
.offset
|| key
.type
) {
1341 ret
= corrupt_key(root
, &key
, field
);
1345 * If we made it here and we have extent set then we didn't specify
1346 * inode and we're screwed.
1348 if (file_extent
!= (u64
)-1)
1351 if (logical
== (u64
)-1)
1355 bytes
= root
->sectorsize
;
1357 bytes
= (bytes
+ root
->sectorsize
- 1) / root
->sectorsize
;
1358 bytes
*= root
->sectorsize
;
1361 if (corrupt_block_keys
) {
1362 corrupt_keys_in_block(root
, logical
);
1364 struct extent_buffer
*eb
;
1366 eb
= btrfs_find_create_tree_block(root
->fs_info
,
1367 logical
, root
->sectorsize
);
1370 "not enough memory to allocate extent buffer for bytenr %llu",
1371 (unsigned long long)logical
);
1376 debug_corrupt_block(eb
, root
, logical
, root
->sectorsize
,
1378 free_extent_buffer(eb
);
1380 logical
+= root
->sectorsize
;
1381 bytes
-= root
->sectorsize
;