forked from kdave/btrfs-progs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btrfs-corrupt-block.c
1641 lines (1477 loc) · 41.2 KB
/
btrfs-corrupt-block.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2009 Oracle. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#include "kerncompat.h"
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <limits.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "kernel-shared/accessors.h"
#include "kernel-shared/uapi/btrfs_tree.h"
#include "kernel-shared/ctree.h"
#include "kernel-shared/volumes.h"
#include "kernel-shared/disk-io.h"
#include "kernel-shared/transaction.h"
#include "kernel-shared/extent_io.h"
#include "kernel-shared/file-item.h"
#include "kernel-shared/tree-checker.h"
#include "common/utils.h"
#include "common/help.h"
#include "common/extent-cache.h"
#include "common/messages.h"
#include "common/string-utils.h"
#include "cmds/commands.h"
#include "check/repair.h"
#define FIELD_BUF_LEN 80
static int debug_corrupt_sector(struct btrfs_root *root, u64 logical, int mirror)
{
const u32 sectorsize = root->fs_info->sectorsize;
struct btrfs_fs_info *fs_info = root->fs_info;
int ret;
int num_copies;
int mirror_num = 1;
void *buf;
buf = malloc(root->fs_info->sectorsize);
if (!buf) {
error_msg(ERROR_MSG_MEMORY, "allocating memory for bytenr %llu",
logical);
return -ENOMEM;
}
while (1) {
if (!mirror || mirror_num == mirror) {
u64 read_len = sectorsize;
ret = read_data_from_disk(fs_info, buf, logical,
&read_len, mirror_num);
if (read_len < sectorsize)
ret = -EIO;
if (ret < 0) {
errno = -ret;
error("cannot read bytenr %llu: %m", logical);
goto out;
}
printf("corrupting %llu copy %d\n", logical, mirror_num);
memset(buf, 0, sectorsize);
ret = write_data_to_disk(fs_info, buf, logical, sectorsize);
if (ret < 0) {
errno = -ret;
error("cannot write bytenr %llu: %m", logical);
goto out;
}
}
num_copies = btrfs_num_copies(root->fs_info, logical, sectorsize);
if (num_copies == 1)
break;
mirror_num++;
if (mirror_num > num_copies)
break;
}
out:
free(buf);
return 0;
}
static const char * const corrupt_block_usage[] = {
"btrfs-corrupt-block [options] device",
"Corrupt data structures on a btrfs filesystem. For testing only!",
"",
OPTLINE("-l|--logical EXTENT", "logical extent to be corrupted"),
OPTLINE("-c|--copy COPY", "copy of the extent to be corrupted (usually 1 or 2, default: 0)"),
OPTLINE("-b|--bytes COUNT", "number of bytes to be corrupted"),
OPTLINE("-e|--extent-record", "corrupt the extent"),
OPTLINE("-E|--extent-tree", "corrupt the whole extent tree"),
OPTLINE("-u|--chunk-record", "corrupt the given chunk"),
OPTLINE("-U|--chunk-tree", "corrupt the whole whole chunk tree"),
OPTLINE("-i|--inode INODE", "inode number to corrupt (must also specify the field to corrupt)"),
OPTLINE("-x|--file-extent EXTENT",
"file extent item to corrupt (must also specify -i for the inode and -f for the field to corrupt)"),
OPTLINE("-m|--metadata-block BLOCK",
"metadata block to corrupt (must also specify -f for the field to corrupt)"),
OPTLINE("-k|--keys"," corrupt block keys (set by --logical)"),
OPTLINE("-K|--key <u64,u8,u64>",
"corrupt the given key (must also specify -f for the field and optionally -r for the root)"),
OPTLINE("-f|--field FIELD", "field name in the item to corrupt"),
OPTLINE("-I|--item", "corrupt an item corresponding to the passed key triplet "
"(must also specify the field, or a (bytes, offset, value) tuple to corrupt and root for the item)"),
OPTLINE("-D|--dir-item",
"corrupt a dir item corresponding to the passed key triplet, must also specify a field"),
OPTLINE("-d|--delete", "delete item corresponding to passed key triplet"),
OPTLINE("-r|--root", "operate on this root"),
OPTLINE("-C|--csum BYTENR", "delete a csum for the specified bytenr. When used "
"with -b it'll delete that many bytes, otherwise it's just sectorsize"),
OPTLINE("--block-group OFFSET", "corrupt the given block group"),
OPTLINE("--value VALUE", "value to use for corrupting item data"),
OPTLINE("--offset OFFSET", "offset to use for corrupting item data"),
NULL
};
static const struct cmd_struct corrupt_block_cmd = {
.usagestr = corrupt_block_usage
};
static void corrupt_keys(struct btrfs_trans_handle *trans,
struct btrfs_fs_info *fs_info,
struct extent_buffer *eb)
{
int slot;
int bad_slot;
int nr;
struct btrfs_disk_key bad_key;;
nr = btrfs_header_nritems(eb);
if (nr == 0)
return;
slot = rand_range(nr);
bad_slot = rand_range(nr);
if (bad_slot == slot)
return;
fprintf(stderr,
"corrupting keys in block %llu slot %d swapping with %d\n",
eb->start, slot, bad_slot);
if (btrfs_header_level(eb) == 0) {
btrfs_item_key(eb, &bad_key, bad_slot);
btrfs_set_item_key(eb, &bad_key, slot);
} else {
btrfs_node_key(eb, &bad_key, bad_slot);
btrfs_set_node_key(eb, &bad_key, slot);
}
btrfs_mark_buffer_dirty(eb);
if (!trans) {
u16 csum_size = fs_info->csum_size;
u16 csum_type = fs_info->csum_type;
csum_tree_block_size(eb, csum_size, 0, csum_type);
write_data_to_disk(eb->fs_info, eb->data, eb->start, eb->len);
}
}
static int corrupt_keys_in_block(struct btrfs_fs_info *fs_info, u64 bytenr)
{
struct extent_buffer *eb;
struct btrfs_tree_parent_check check = { 0 };
eb = read_tree_block(fs_info, bytenr, &check);
if (!extent_buffer_uptodate(eb))
return -EIO;;
corrupt_keys(NULL, fs_info, eb);
free_extent_buffer(eb);
return 0;
}
static int corrupt_extent(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 bytenr)
{
struct btrfs_root *extent_root;
struct btrfs_key key;
struct extent_buffer *leaf;
u32 item_size;
unsigned long ptr;
struct btrfs_path *path;
int ret;
int slot;
int should_del = rand_range(3);
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
key.objectid = bytenr;
key.type = (u8)-1;
key.offset = (u64)-1;
extent_root = btrfs_extent_root(trans->fs_info, bytenr);
while(1) {
ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
if (ret < 0)
break;
if (ret > 0) {
if (path->slots[0] == 0)
break;
path->slots[0]--;
ret = 0;
}
leaf = path->nodes[0];
slot = path->slots[0];
btrfs_item_key_to_cpu(leaf, &key, slot);
if (key.objectid != bytenr)
break;
if (key.type != BTRFS_EXTENT_ITEM_KEY &&
key.type != BTRFS_METADATA_ITEM_KEY &&
key.type != BTRFS_TREE_BLOCK_REF_KEY &&
key.type != BTRFS_EXTENT_DATA_REF_KEY &&
key.type != BTRFS_EXTENT_REF_V0_KEY &&
key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
key.type != BTRFS_SHARED_DATA_REF_KEY)
goto next;
if (should_del) {
fprintf(stderr,
"deleting extent record: key %llu %u %llu\n",
key.objectid, key.type, key.offset);
if (key.type == BTRFS_EXTENT_ITEM_KEY) {
/* make sure this extent doesn't get
* reused for other purposes */
btrfs_pin_extent(root->fs_info,
key.objectid, key.offset);
}
btrfs_del_item(trans, root, path);
} else {
fprintf(stderr,
"corrupting extent record: key %llu %u %llu\n",
key.objectid, key.type, key.offset);
ptr = btrfs_item_ptr_offset(leaf, slot);
item_size = btrfs_item_size(leaf, slot);
memset_extent_buffer(leaf, 0, ptr, item_size);
btrfs_mark_buffer_dirty(leaf);
}
next:
btrfs_release_path(path);
if (key.offset > 0)
key.offset--;
if (key.offset == 0)
break;
}
btrfs_free_path(path);
return 0;
}
static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct extent_buffer *eb)
{
u32 nr = btrfs_header_nritems(eb);
u32 victim = rand_range(nr);
u64 objectid;
struct btrfs_key key;
btrfs_item_key_to_cpu(eb, &key, victim);
objectid = key.objectid;
corrupt_extent(trans, root, objectid);
}
static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct extent_buffer *eb)
{
struct btrfs_fs_info *fs_info = root->fs_info;
int i;
if (!eb)
return;
if (btrfs_is_leaf(eb)) {
btrfs_corrupt_extent_leaf(trans, root, eb);
return;
}
if (btrfs_header_level(eb) == 1 && eb != root->node) {
if (rand_range(5))
return;
}
for (i = 0; i < btrfs_header_nritems(eb); i++) {
struct extent_buffer *next;
struct btrfs_tree_parent_check check = {
.owner_root = btrfs_header_owner(eb),
.transid = btrfs_node_ptr_generation(eb, i),
.level = btrfs_header_level(eb) - 1,
};
next = read_tree_block(fs_info, btrfs_node_blockptr(eb, i),
&check);
if (!extent_buffer_uptodate(next))
continue;
btrfs_corrupt_extent_tree(trans, root, next);
free_extent_buffer(next);
}
}
enum btrfs_inode_field {
BTRFS_INODE_FIELD_ISIZE,
BTRFS_INODE_FIELD_NBYTES,
BTRFS_INODE_FIELD_NLINK,
BTRFS_INODE_FIELD_GENERATION,
BTRFS_INODE_FIELD_TRANSID,
BTRFS_INODE_FIELD_BLOCK_GROUP,
BTRFS_INODE_FIELD_MODE,
BTRFS_INODE_FIELD_UID,
BTRFS_INODE_FIELD_GID,
BTRFS_INODE_FIELD_BAD,
};
enum btrfs_file_extent_field {
BTRFS_FILE_EXTENT_DISK_BYTENR,
BTRFS_FILE_EXTENT_TYPE,
BTRFS_FILE_EXTENT_BAD,
};
enum btrfs_dir_item_field {
BTRFS_DIR_ITEM_NAME,
BTRFS_DIR_ITEM_LOCATION_OBJECTID,
BTRFS_DIR_ITEM_BAD,
};
enum btrfs_metadata_block_field {
BTRFS_METADATA_BLOCK_GENERATION,
BTRFS_METADATA_BLOCK_SHIFT_ITEMS,
BTRFS_METADATA_BLOCK_BAD,
};
enum btrfs_item_field {
BTRFS_ITEM_OFFSET,
BTRFS_ITEM_BAD,
};
enum btrfs_key_field {
BTRFS_KEY_OBJECTID,
BTRFS_KEY_TYPE,
BTRFS_KEY_OFFSET,
BTRFS_KEY_BAD,
};
enum btrfs_block_group_field {
BTRFS_BLOCK_GROUP_ITEM_USED,
BTRFS_BLOCK_GROUP_ITEM_FLAGS,
BTRFS_BLOCK_GROUP_ITEM_CHUNK_OBJECTID,
BTRFS_BLOCK_GROUP_ITEM_BAD,
};
static enum btrfs_block_group_field convert_block_group_field(char *field)
{
if (!strncmp(field, "used", FIELD_BUF_LEN))
return BTRFS_BLOCK_GROUP_ITEM_USED;
if (!strncmp(field, "flags", FIELD_BUF_LEN))
return BTRFS_BLOCK_GROUP_ITEM_FLAGS;
if (!strncmp(field, "chunk_objectid", FIELD_BUF_LEN))
return BTRFS_BLOCK_GROUP_ITEM_CHUNK_OBJECTID;
return BTRFS_BLOCK_GROUP_ITEM_BAD;
}
static enum btrfs_inode_field convert_inode_field(char *field)
{
if (!strncmp(field, "isize", FIELD_BUF_LEN))
return BTRFS_INODE_FIELD_ISIZE;
if (!strncmp(field, "nbytes", FIELD_BUF_LEN))
return BTRFS_INODE_FIELD_NBYTES;
if (!strncmp(field, "nlink", FIELD_BUF_LEN))
return BTRFS_INODE_FIELD_NLINK;
if (!strncmp(field, "generation", FIELD_BUF_LEN))
return BTRFS_INODE_FIELD_GENERATION;
if (!strncmp(field, "transid", FIELD_BUF_LEN))
return BTRFS_INODE_FIELD_TRANSID;
if (!strncmp(field, "block_group", FIELD_BUF_LEN))
return BTRFS_INODE_FIELD_BLOCK_GROUP;
if (!strncmp(field, "mode", FIELD_BUF_LEN))
return BTRFS_INODE_FIELD_MODE;
if (!strncmp(field, "uid", FIELD_BUF_LEN))
return BTRFS_INODE_FIELD_UID;
if (!strncmp(field, "gid", FIELD_BUF_LEN))
return BTRFS_INODE_FIELD_GID;
return BTRFS_INODE_FIELD_BAD;
}
static enum btrfs_file_extent_field convert_file_extent_field(char *field)
{
if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
return BTRFS_FILE_EXTENT_DISK_BYTENR;
if (!strncmp(field, "type", FIELD_BUF_LEN))
return BTRFS_FILE_EXTENT_TYPE;
return BTRFS_FILE_EXTENT_BAD;
}
static enum btrfs_metadata_block_field
convert_metadata_block_field(char *field)
{
if (!strncmp(field, "generation", FIELD_BUF_LEN))
return BTRFS_METADATA_BLOCK_GENERATION;
if (!strncmp(field, "shift_items", FIELD_BUF_LEN))
return BTRFS_METADATA_BLOCK_SHIFT_ITEMS;
return BTRFS_METADATA_BLOCK_BAD;
}
static enum btrfs_key_field convert_key_field(char *field)
{
if (!strncmp(field, "objectid", FIELD_BUF_LEN))
return BTRFS_KEY_OBJECTID;
if (!strncmp(field, "type", FIELD_BUF_LEN))
return BTRFS_KEY_TYPE;
if (!strncmp(field, "offset", FIELD_BUF_LEN))
return BTRFS_KEY_OFFSET;
return BTRFS_KEY_BAD;
}
static enum btrfs_item_field convert_item_field(char *field)
{
if (!strncmp(field, "offset", FIELD_BUF_LEN))
return BTRFS_ITEM_OFFSET;
return BTRFS_ITEM_BAD;
}
static enum btrfs_dir_item_field convert_dir_item_field(char *field)
{
if (!strncmp(field, "name", FIELD_BUF_LEN))
return BTRFS_DIR_ITEM_NAME;
if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
return BTRFS_DIR_ITEM_BAD;
}
static u64 generate_u64(u64 orig)
{
u64 ret;
do {
ret = rand_u64();
} while (ret == orig);
return ret;
}
static u32 generate_u32(u32 orig)
{
u32 ret;
do {
ret = rand_u32();
} while (ret == orig);
return ret;
}
static u8 generate_u8(u8 orig)
{
u8 ret;
do {
ret = rand_u8();
} while (ret == orig);
return ret;
}
static int corrupt_block_group(struct btrfs_root *root, u64 bg, char *field)
{
struct btrfs_trans_handle *trans;
struct btrfs_path *path;
struct btrfs_block_group_item *bgi;
struct btrfs_key key;
enum btrfs_block_group_field corrupt_field;
u64 orig, bogus;
int ret = 0;
root = btrfs_extent_root(root->fs_info, 0);
corrupt_field = convert_block_group_field(field);
if (corrupt_field == BTRFS_BLOCK_GROUP_ITEM_BAD) {
error("invalid field %s", field);
return -EINVAL;
}
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
trans = btrfs_start_transaction(root, 1);
if (IS_ERR(trans)) {
btrfs_free_path(path);
ret = PTR_ERR(trans);
errno = -ret;
error_msg(ERROR_MSG_START_TRANS, "%m");
return ret;
}
key.objectid = bg;
key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
key.offset = 0;
ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
if (ret < 0) {
error("error searching for bg %llu %d", bg, ret);
goto out;
}
ret = 0;
btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
if (key.type != BTRFS_BLOCK_GROUP_ITEM_KEY) {
error("couldn't find the bg %llu", bg);
goto out;
}
bgi = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_block_group_item);
switch (corrupt_field) {
case BTRFS_BLOCK_GROUP_ITEM_USED:
orig = btrfs_block_group_used(path->nodes[0], bgi);
bogus = generate_u64(orig);
btrfs_set_block_group_used(path->nodes[0], bgi, bogus);
break;
case BTRFS_BLOCK_GROUP_ITEM_CHUNK_OBJECTID:
orig = btrfs_block_group_chunk_objectid(path->nodes[0], bgi);
bogus = generate_u64(orig);
btrfs_set_block_group_chunk_objectid(path->nodes[0], bgi,
bogus);
break;
case BTRFS_BLOCK_GROUP_ITEM_FLAGS:
orig = btrfs_block_group_flags(path->nodes[0], bgi);
bogus = generate_u64(orig);
btrfs_set_block_group_flags(path->nodes[0], bgi, bogus);
break;
default:
ret = -EINVAL;
goto out;
}
btrfs_mark_buffer_dirty(path->nodes[0]);
out:
btrfs_commit_transaction(trans, root);
btrfs_free_path(path);
return ret;
}
static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
char *field)
{
enum btrfs_key_field corrupt_field = convert_key_field(field);
struct btrfs_path *path;
struct btrfs_trans_handle *trans;
int ret;
if (corrupt_field == BTRFS_KEY_BAD) {
error("invalid field %s", field);
return -EINVAL;
}
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
trans = btrfs_start_transaction(root, 1);
if (IS_ERR(trans)) {
btrfs_free_path(path);
return PTR_ERR(trans);
}
ret = btrfs_search_slot(trans, root, key, path, 0, 1);
if (ret < 0)
goto out;
if (ret > 0) {
error("couldn't find the key to corrupt");
ret = -ENOENT;
goto out;
}
switch (corrupt_field) {
case BTRFS_KEY_OBJECTID:
key->objectid = generate_u64(key->objectid);
break;
case BTRFS_KEY_TYPE:
key->type = generate_u8(key->type);
break;
case BTRFS_KEY_OFFSET:
key->offset = generate_u64(key->objectid);
break;
default:
error("invalid field %s, %d", field, corrupt_field);
ret = -EINVAL;
goto out;
}
btrfs_set_item_key_unsafe(root, path, key);
out:
btrfs_free_path(path);
btrfs_commit_transaction(trans, root);
return ret;
}
static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
char *field)
{
struct btrfs_trans_handle *trans;
struct btrfs_dir_item *di;
struct btrfs_path *path;
char name[PATH_MAX];
struct btrfs_key location;
struct btrfs_disk_key disk_key;
unsigned long name_ptr;
enum btrfs_dir_item_field corrupt_field =
convert_dir_item_field(field);
u64 bogus;
u16 name_len;
int ret;
if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
error("invalid field %s", field);
return -EINVAL;
}
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
trans = btrfs_start_transaction(root, 1);
if (IS_ERR(trans)) {
btrfs_free_path(path);
return PTR_ERR(trans);
}
ret = btrfs_search_slot(trans, root, key, path, 0, 1);
if (ret) {
if (ret > 0)
ret = -ENOENT;
error("error searching for dir item %d", ret);
goto out;
}
di = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_dir_item);
switch (corrupt_field) {
case BTRFS_DIR_ITEM_NAME:
name_len = btrfs_dir_name_len(path->nodes[0], di);
name_ptr = (unsigned long)(di + 1);
read_extent_buffer(path->nodes[0], name, name_ptr, name_len);
name[0]++;
write_extent_buffer(path->nodes[0], name, name_ptr, name_len);
btrfs_mark_buffer_dirty(path->nodes[0]);
goto out;
case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
bogus = generate_u64(location.objectid);
location.objectid = bogus;
btrfs_cpu_key_to_disk(&disk_key, &location);
btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
btrfs_mark_buffer_dirty(path->nodes[0]);
goto out;
default:
ret = -EINVAL;
goto out;
}
out:
btrfs_commit_transaction(trans, root);
btrfs_free_path(path);
return ret;
}
static int corrupt_inode(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 inode, char *field)
{
struct btrfs_inode_item *ei;
struct btrfs_path *path;
struct btrfs_key key;
enum btrfs_inode_field corrupt_field = convert_inode_field(field);
u64 bogus;
u64 orig;
int ret;
if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
error("invalid field %s", field);
return -EINVAL;
}
key.objectid = inode;
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = (u64)-1;
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
if (ret < 0)
goto out;
if (ret) {
if (!path->slots[0]) {
error("couldn't find inode %llu", inode);
ret = -ENOENT;
goto out;
}
path->slots[0]--;
ret = 0;
}
btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
if (key.objectid != inode) {
error("couldn't find inode %llu", inode);
ret = -ENOENT;
goto out;
}
ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_inode_item);
switch (corrupt_field) {
case BTRFS_INODE_FIELD_ISIZE:
orig = btrfs_inode_size(path->nodes[0], ei);
bogus = generate_u64(orig);
btrfs_set_inode_size(path->nodes[0], ei, bogus);
break;
case BTRFS_INODE_FIELD_NBYTES:
orig = btrfs_inode_nbytes(path->nodes[0], ei);
bogus = generate_u64(orig);
btrfs_set_inode_nbytes(path->nodes[0], ei, bogus);
break;
case BTRFS_INODE_FIELD_NLINK:
orig = btrfs_inode_nlink(path->nodes[0], ei);
bogus = generate_u32(orig);
btrfs_set_inode_nlink(path->nodes[0], ei, bogus);
break;
case BTRFS_INODE_FIELD_GENERATION:
orig = btrfs_inode_generation(path->nodes[0], ei);
bogus = generate_u64(orig);
btrfs_set_inode_generation(path->nodes[0], ei, bogus);
break;
case BTRFS_INODE_FIELD_TRANSID:
orig = btrfs_inode_transid(path->nodes[0], ei);
bogus = generate_u64(orig);
btrfs_set_inode_transid(path->nodes[0], ei, bogus);
break;
case BTRFS_INODE_FIELD_BLOCK_GROUP:
orig = btrfs_inode_block_group(path->nodes[0], ei);
bogus = generate_u64(orig);
btrfs_set_inode_block_group(path->nodes[0], ei, bogus);
break;
case BTRFS_INODE_FIELD_MODE:
orig = btrfs_inode_mode(path->nodes[0], ei);
bogus = generate_u32(orig);
btrfs_set_inode_mode(path->nodes[0], ei, bogus);
break;
case BTRFS_INODE_FIELD_UID:
orig = btrfs_inode_uid(path->nodes[0], ei);
bogus = generate_u32(orig);
btrfs_set_inode_uid(path->nodes[0], ei, bogus);
break;
case BTRFS_INODE_FIELD_GID:
orig = btrfs_inode_gid(path->nodes[0], ei);
bogus = generate_u32(orig);
btrfs_set_inode_gid(path->nodes[0], ei, bogus);
break;
default:
ret = -EINVAL;
break;
}
btrfs_mark_buffer_dirty(path->nodes[0]);
out:
btrfs_free_path(path);
return ret;
}
static int corrupt_file_extent(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 inode, u64 extent,
char *field, u64 bogus)
{
struct btrfs_file_extent_item *fi;
struct btrfs_path *path;
struct btrfs_key key;
enum btrfs_file_extent_field corrupt_field;
u64 orig;
int ret = 0;
corrupt_field = convert_file_extent_field(field);
if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
error("invalid field %s", field);
return -EINVAL;
}
key.objectid = inode;
key.type = BTRFS_EXTENT_DATA_KEY;
key.offset = extent;
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
if (ret < 0)
goto out;
if (ret) {
error("couldn't find extent %llu for inode %llu",
extent, inode);
ret = -ENOENT;
goto out;
}
fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_file_extent_item);
switch (corrupt_field) {
case BTRFS_FILE_EXTENT_DISK_BYTENR:
orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
bogus = (bogus == (u64)-1) ? generate_u64(orig) : bogus;
btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
break;
case BTRFS_FILE_EXTENT_TYPE:
if (bogus == (u64)-1) {
error("specify a new extent type value (-v)");
ret = -EINVAL;
goto out;
}
btrfs_set_file_extent_type(path->nodes[0], fi, (u8)bogus);
break;
default:
ret = -EINVAL;
break;
}
btrfs_mark_buffer_dirty(path->nodes[0]);
out:
btrfs_free_path(path);
return ret;
}
static void shift_items(struct btrfs_root *root, struct extent_buffer *eb)
{
int nritems = btrfs_header_nritems(eb);
int shift_space = btrfs_leaf_free_space(eb) / 2;
int slot = nritems / 2;
int i = 0;
unsigned int data_end = btrfs_item_offset(eb, nritems - 1);
/* Shift the item data up to and including slot back by shift space */
memmove_extent_buffer(eb, btrfs_item_nr_offset(eb, 0) + data_end - shift_space,
btrfs_item_nr_offset(eb, 0) + data_end,
btrfs_item_offset(eb, slot - 1) - data_end);
/* Now update the item pointers. */
for (i = nritems - 1; i >= slot; i--) {
u32 offset = btrfs_item_offset(eb, i);
offset -= shift_space;
btrfs_set_item_offset(eb, i, offset);
}
}
static int corrupt_metadata_block(struct btrfs_fs_info *fs_info, u64 block,
char *field)
{
struct extent_buffer *eb;
struct btrfs_tree_parent_check check = { 0 };
enum btrfs_metadata_block_field corrupt_field;
int ret;
corrupt_field = convert_metadata_block_field(field);
if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
error("invalid field %s", field);
return -EINVAL;
}
eb = read_tree_block(fs_info, block, &check);
if (!extent_buffer_uptodate(eb)) {
error("couldn't read in tree block %s", field);
return -EINVAL;
}
ret = 0;
switch (corrupt_field) {
case BTRFS_METADATA_BLOCK_GENERATION:
{
u64 orig = btrfs_header_generation(eb);
u64 bogus = generate_u64(orig);
btrfs_set_header_generation(eb, bogus);
csum_tree_block_size(eb, fs_info->csum_size, 0,
fs_info->csum_type);
ret = write_data_to_disk(fs_info, eb->data, eb->start, eb->len);
free_extent_buffer(eb);
if (ret < 0) {
errno = -ret;
error("failed to write extent buffer at %llu: %m",
eb->start);
return ret;
}
break;
}
case BTRFS_METADATA_BLOCK_SHIFT_ITEMS:
{
struct btrfs_trans_handle *trans;
struct btrfs_root *root;
struct btrfs_path *path;
struct btrfs_key key, root_key;
u64 root_objectid;
u8 level;
root_objectid = btrfs_header_owner(eb);
level = btrfs_header_level(eb);
if (level)
btrfs_node_key_to_cpu(eb, &key, 0);
else
btrfs_item_key_to_cpu(eb, &key, 0);
free_extent_buffer(eb);
root_key.objectid = root_objectid;
root_key.type = BTRFS_ROOT_ITEM_KEY;
root_key.offset = (u64)-1;
root = btrfs_read_fs_root(fs_info, &root_key);
if (IS_ERR(root)) {
error("couldn't find owner root %llu", key.objectid);
return PTR_ERR(root);
}
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
trans = btrfs_start_transaction(root, 1);
if (IS_ERR(trans)) {
btrfs_free_path(path);
ret = PTR_ERR(trans);
errno = -ret;
error_msg(ERROR_MSG_START_TRANS, "%m");
return ret;
}
path->lowest_level = level;
ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
if (ret < 0) {
error("error searching to node %d", ret);
btrfs_free_path(path);
btrfs_abort_transaction(trans, ret);
return ret;
}
eb = path->nodes[level];
shift_items(root, path->nodes[level]);
btrfs_mark_buffer_dirty(path->nodes[level]);
btrfs_commit_transaction(trans, root);
break;
}
default:
ret = -EINVAL;
break;
}
return ret;
}
static int corrupt_btrfs_item(struct btrfs_root *root, struct btrfs_key *key,
char *field)
{
struct btrfs_trans_handle *trans;
struct btrfs_path *path;
enum btrfs_item_field corrupt_field;
u32 orig, bogus;
int ret;
corrupt_field = convert_item_field(field);
if (corrupt_field == BTRFS_ITEM_BAD) {
error("invalid field %s", field);
return -EINVAL;
}
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
trans = btrfs_start_transaction(root, 1);
if (IS_ERR(trans)) {
btrfs_free_path(path);
ret = PTR_ERR(trans);
errno = -ret;
error_msg(ERROR_MSG_START_TRANS, "%m");
return ret;
}
ret = btrfs_search_slot(trans, root, key, path, 0, 1);
if (ret != 0) {
error("error searching to node %d", ret);