0b269036a486b3061703eb9313a583925ac17aab
[platform/upstream/btrfs-progs.git] / btrfs-corrupt-block.c
1 /*
2  * Copyright (C) 2009 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <getopt.h>
24
25 #include "kerncompat.h"
26 #include "ctree.h"
27 #include "volumes.h"
28 #include "disk-io.h"
29 #include "print-tree.h"
30 #include "transaction.h"
31 #include "list.h"
32 #include "utils.h"
33
34 #define FIELD_BUF_LEN 80
35
36 static struct extent_buffer *debug_corrupt_block(struct btrfs_root *root,
37                 u64 bytenr, u32 blocksize, u64 copy)
38 {
39         int ret;
40         struct extent_buffer *eb;
41         u64 length;
42         struct btrfs_multi_bio *multi = NULL;
43         struct btrfs_device *device;
44         int num_copies;
45         int mirror_num = 1;
46
47         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
48         if (!eb)
49                 return NULL;
50
51         length = blocksize;
52         while (1) {
53                 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
54                                       eb->start, &length, &multi,
55                                       mirror_num, NULL);
56                 BUG_ON(ret);
57                 device = multi->stripes[0].dev;
58                 eb->fd = device->fd;
59                 device->total_ios++;
60                 eb->dev_bytenr = multi->stripes[0].physical;
61
62                 fprintf(stdout,
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);
66                 kfree(multi);
67
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,
71                                mirror_num);
72                         memset(eb->data, 0, eb->len);
73                         write_extent_to_disk(eb);
74                         fsync(eb->fd);
75                 }
76
77                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
78                                               eb->start, eb->len);
79                 if (num_copies == 1)
80                         break;
81
82                 mirror_num++;
83                 if (mirror_num > num_copies)
84                         break;
85         }
86         return eb;
87 }
88
89 static void print_usage(int ret)
90 {
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");
117         exit(ret);
118 }
119
120 static void corrupt_keys(struct btrfs_trans_handle *trans,
121                          struct btrfs_root *root,
122                          struct extent_buffer *eb)
123 {
124         int slot;
125         int bad_slot;
126         int nr;
127         struct btrfs_disk_key bad_key;;
128
129         nr = btrfs_header_nritems(eb);
130         if (nr == 0)
131                 return;
132
133         slot = rand() % nr;
134         bad_slot = rand() % nr;
135
136         if (bad_slot == slot)
137                 return;
138
139         fprintf(stderr,
140                 "corrupting keys in block %llu slot %d swapping with %d\n",
141                 (unsigned long long)eb->start, slot, bad_slot);
142
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);
146         } else {
147                 btrfs_node_key(eb, &bad_key, bad_slot);
148                 btrfs_set_node_key(eb, &bad_key, slot);
149         }
150         btrfs_mark_buffer_dirty(eb);
151         if (!trans) {
152                 u16 csum_size =
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);
156         }
157 }
158
159
160 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
161 {
162         struct extent_buffer *eb;
163
164         eb = read_tree_block(root, bytenr, root->leafsize, 0);
165         if (!extent_buffer_uptodate(eb))
166                 return -EIO;;
167
168         corrupt_keys(NULL, root, eb);
169         free_extent_buffer(eb);
170         return 0;
171 }
172
173 static int corrupt_extent(struct btrfs_trans_handle *trans,
174                           struct btrfs_root *root, u64 bytenr, u64 copy)
175 {
176         struct btrfs_key key;
177         struct extent_buffer *leaf;
178         u32 item_size;
179         unsigned long ptr;
180         struct btrfs_path *path;
181         int ret;
182         int slot;
183         int should_del = rand() % 3;
184
185         path = btrfs_alloc_path();
186         if (!path)
187                 return -ENOMEM;
188
189         key.objectid = bytenr;
190         key.type = (u8)-1;
191         key.offset = (u64)-1;
192
193         while(1) {
194                 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
195                                         &key, path, -1, 1);
196                 if (ret < 0)
197                         break;
198
199                 if (ret > 0) {
200                         if (path->slots[0] == 0)
201                                 break;
202                         path->slots[0]--;
203                         ret = 0;
204                 }
205                 leaf = path->nodes[0];
206                 slot = path->slots[0];
207                 btrfs_item_key_to_cpu(leaf, &key, slot);
208                 if (key.objectid != bytenr)
209                         break;
210
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)
217                         goto next;
218
219                 if (should_del) {
220                         fprintf(stderr,
221                                 "deleting extent record: key %llu %u %llu\n",
222                                 key.objectid, key.type, key.offset);
223
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);
229                         }
230
231                         btrfs_del_item(trans, root, path);
232                 } else {
233                         fprintf(stderr,
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);
240                 }
241 next:
242                 btrfs_release_path(path);
243
244                 if (key.offset > 0)
245                         key.offset--;
246                 if (key.offset == 0)
247                         break;
248         }
249
250         btrfs_free_path(path);
251         return 0;
252 }
253
254 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
255                                       struct btrfs_root *root,
256                                       struct extent_buffer *eb)
257 {
258         u32 nr = btrfs_header_nritems(eb);
259         u32 victim = rand() % nr;
260         u64 objectid;
261         struct btrfs_key key;
262
263         btrfs_item_key_to_cpu(eb, &key, victim);
264         objectid = key.objectid;
265         corrupt_extent(trans, root, objectid, 1);
266 }
267
268 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
269                                       struct btrfs_root *root,
270                                       struct extent_buffer *eb)
271 {
272         int i;
273
274         if (!eb)
275                 return;
276
277         if (btrfs_is_leaf(eb)) {
278                 btrfs_corrupt_extent_leaf(trans, root, eb);
279                 return;
280         }
281
282         if (btrfs_header_level(eb) == 1 && eb != root->node) {
283                 if (rand() % 5)
284                         return;
285         }
286
287         for (i = 0; i < btrfs_header_nritems(eb); i++) {
288                 struct extent_buffer *next;
289
290                 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
291                                        root->leafsize,
292                                        btrfs_node_ptr_generation(eb, i));
293                 if (!extent_buffer_uptodate(next))
294                         continue;
295                 btrfs_corrupt_extent_tree(trans, root, next);
296                 free_extent_buffer(next);
297         }
298 }
299
300 enum btrfs_inode_field {
301         BTRFS_INODE_FIELD_ISIZE,
302         BTRFS_INODE_FIELD_NBYTES,
303         BTRFS_INODE_FIELD_BAD,
304 };
305
306 enum btrfs_file_extent_field {
307         BTRFS_FILE_EXTENT_DISK_BYTENR,
308         BTRFS_FILE_EXTENT_BAD,
309 };
310
311 enum btrfs_dir_item_field {
312         BTRFS_DIR_ITEM_NAME,
313         BTRFS_DIR_ITEM_LOCATION_OBJECTID,
314         BTRFS_DIR_ITEM_BAD,
315 };
316
317 enum btrfs_metadata_block_field {
318         BTRFS_METADATA_BLOCK_GENERATION,
319         BTRFS_METADATA_BLOCK_SHIFT_ITEMS,
320         BTRFS_METADATA_BLOCK_BAD,
321 };
322
323 enum btrfs_item_field {
324         BTRFS_ITEM_OFFSET,
325         BTRFS_ITEM_BAD,
326 };
327
328 enum btrfs_key_field {
329         BTRFS_KEY_OBJECTID,
330         BTRFS_KEY_TYPE,
331         BTRFS_KEY_OFFSET,
332         BTRFS_KEY_BAD,
333 };
334
335 static enum btrfs_inode_field convert_inode_field(char *field)
336 {
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;
342 }
343
344 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
345 {
346         if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
347                 return BTRFS_FILE_EXTENT_DISK_BYTENR;
348         return BTRFS_FILE_EXTENT_BAD;
349 }
350
351 static enum btrfs_metadata_block_field
352 convert_metadata_block_field(char *field)
353 {
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;
359 }
360
361 static enum btrfs_key_field convert_key_field(char *field)
362 {
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;
370 }
371
372 static enum btrfs_item_field convert_item_field(char *field)
373 {
374         if (!strncmp(field, "offset", FIELD_BUF_LEN))
375                 return BTRFS_ITEM_OFFSET;
376         return BTRFS_ITEM_BAD;
377 }
378
379 static enum btrfs_dir_item_field convert_dir_item_field(char *field)
380 {
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;
386 }
387
388 static u64 generate_u64(u64 orig)
389 {
390         u64 ret;
391         do {
392                 ret = rand();
393         } while (ret == orig);
394         return ret;
395 }
396
397 static u32 generate_u32(u32 orig)
398 {
399         u32 ret;
400         do {
401                 ret = rand();
402         } while (ret == orig);
403         return ret;
404 }
405
406 static u8 generate_u8(u8 orig)
407 {
408         u8 ret;
409         do {
410                 ret = rand();
411         } while (ret == orig);
412         return ret;
413 }
414
415 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
416                        char *field)
417 {
418         enum btrfs_key_field corrupt_field = convert_key_field(field);
419         struct btrfs_path *path;
420         struct btrfs_trans_handle *trans;
421         int ret;
422
423         root = root->fs_info->fs_root;
424         if (corrupt_field == BTRFS_KEY_BAD) {
425                 fprintf(stderr, "Invalid field %s\n", field);
426                 return -EINVAL;
427         }
428
429         path = btrfs_alloc_path();
430         if (!path)
431                 return -ENOMEM;
432
433         trans = btrfs_start_transaction(root, 1);
434         if (IS_ERR(trans)) {
435                 btrfs_free_path(path);
436                 return PTR_ERR(trans);
437         }
438
439         ret = btrfs_search_slot(trans, root, key, path, 0, 1);
440         if (ret < 0)
441                 goto out;
442         if (ret > 0) {
443                 fprintf(stderr, "Couldn't find the key to corrupt\n");
444                 ret = -ENOENT;
445                 goto out;
446         }
447
448         switch (corrupt_field) {
449         case BTRFS_KEY_OBJECTID:
450                 key->objectid = generate_u64(key->objectid);
451                 break;
452         case BTRFS_KEY_TYPE:
453                 key->type = generate_u8(key->type);
454                 break;
455         case BTRFS_KEY_OFFSET:
456                 key->offset = generate_u64(key->objectid);
457                 break;
458         default:
459                 fprintf(stderr, "Invalid field %s, %d\n", field,
460                         corrupt_field);
461                 ret = -EINVAL;
462                 goto out;
463         }
464
465         btrfs_set_item_key_unsafe(root, path, key);
466 out:
467         btrfs_free_path(path);
468         btrfs_commit_transaction(trans, root);
469         return ret;
470 }
471
472 static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
473                             char *field)
474 {
475         struct btrfs_trans_handle *trans;
476         struct btrfs_dir_item *di;
477         struct btrfs_path *path;
478         char name[PATH_MAX];
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);
484         u64 bogus;
485         u16 name_len;
486         int ret;
487
488         if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
489                 fprintf(stderr, "Invalid field %s\n", field);
490                 return -EINVAL;
491         }
492
493         path = btrfs_alloc_path();
494         if (!path)
495                 return -ENOMEM;
496
497         trans = btrfs_start_transaction(root, 1);
498         if (IS_ERR(trans)) {
499                 btrfs_free_path(path);
500                 return PTR_ERR(trans);
501         }
502
503         ret = btrfs_search_slot(trans, root, key, path, 0, 1);
504         if (ret) {
505                 if (ret > 0)
506                         ret = -ENOENT;
507                 fprintf(stderr, "Error searching for dir item %d\n", ret);
508                 goto out;
509         }
510
511         di = btrfs_item_ptr(path->nodes[0], path->slots[0],
512                             struct btrfs_dir_item);
513
514         switch (corrupt_field) {
515         case BTRFS_DIR_ITEM_NAME:
516                 name_len = btrfs_dir_name_len(path->nodes[0], di);
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                 goto out;
523         case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
524                 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
525                 bogus = generate_u64(location.objectid);
526                 location.objectid = bogus;
527                 btrfs_cpu_key_to_disk(&disk_key, &location);
528                 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
529                 btrfs_mark_buffer_dirty(path->nodes[0]);
530                 goto out;
531         default:
532                 ret = -EINVAL;
533                 goto out;
534         }
535 out:
536         btrfs_commit_transaction(trans, root);
537         btrfs_free_path(path);
538         return ret;
539 }
540
541 static int corrupt_inode(struct btrfs_trans_handle *trans,
542                          struct btrfs_root *root, u64 inode, char *field)
543 {
544         struct btrfs_inode_item *ei;
545         struct btrfs_path *path;
546         struct btrfs_key key;
547         enum btrfs_inode_field corrupt_field = convert_inode_field(field);
548         u64 bogus;
549         u64 orig;
550         int ret;
551
552         if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
553                 fprintf(stderr, "Invalid field %s\n", field);
554                 return -EINVAL;
555         }
556
557         key.objectid = inode;
558         key.type = BTRFS_INODE_ITEM_KEY;
559         key.offset = (u64)-1;
560
561         path = btrfs_alloc_path();
562         if (!path)
563                 return -ENOMEM;
564
565         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
566         if (ret < 0)
567                 goto out;
568         if (ret) {
569                 if (!path->slots[0]) {
570                         fprintf(stderr, "Couldn't find inode %Lu\n", inode);
571                         ret = -ENOENT;
572                         goto out;
573                 }
574                 path->slots[0]--;
575                 ret = 0;
576         }
577
578         btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
579         if (key.objectid != inode) {
580                 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
581                 ret = -ENOENT;
582                 goto out;
583         }
584
585         ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
586                             struct btrfs_inode_item);
587         switch (corrupt_field) {
588         case BTRFS_INODE_FIELD_ISIZE:
589                 orig = btrfs_inode_size(path->nodes[0], ei);
590                 bogus = generate_u64(orig);
591                 btrfs_set_inode_size(path->nodes[0], ei, bogus);
592                 break;
593         case BTRFS_INODE_FIELD_NBYTES:
594                 orig = btrfs_inode_nbytes(path->nodes[0], ei);
595                 bogus = generate_u64(orig);
596                 btrfs_set_inode_nbytes(path->nodes[0], ei, bogus);
597                 break;
598         default:
599                 ret = -EINVAL;
600                 break;
601         }
602         btrfs_mark_buffer_dirty(path->nodes[0]);
603 out:
604         btrfs_free_path(path);
605         return ret;
606 }
607
608 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
609                                struct btrfs_root *root, u64 inode, u64 extent,
610                                char *field)
611 {
612         struct btrfs_file_extent_item *fi;
613         struct btrfs_path *path;
614         struct btrfs_key key;
615         enum btrfs_file_extent_field corrupt_field;
616         u64 bogus;
617         u64 orig;
618         int ret = 0;
619
620         corrupt_field = convert_file_extent_field(field);
621         if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
622                 fprintf(stderr, "Invalid field %s\n", field);
623                 return -EINVAL;
624         }
625
626         key.objectid = inode;
627         key.type = BTRFS_EXTENT_DATA_KEY;
628         key.offset = extent;
629
630         path = btrfs_alloc_path();
631         if (!path)
632                 return -ENOMEM;
633
634         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
635         if (ret < 0)
636                 goto out;
637         if (ret) {
638                 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
639                         extent, inode);
640                 ret = -ENOENT;
641                 goto out;
642         }
643
644         fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
645                             struct btrfs_file_extent_item);
646         switch (corrupt_field) {
647         case BTRFS_FILE_EXTENT_DISK_BYTENR:
648                 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
649                 bogus = generate_u64(orig);
650                 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
651                 break;
652         default:
653                 ret = -EINVAL;
654                 break;
655         }
656         btrfs_mark_buffer_dirty(path->nodes[0]);
657 out:
658         btrfs_free_path(path);
659         return ret;
660 }
661
662 static void shift_items(struct btrfs_root *root, struct extent_buffer *eb)
663 {
664         int nritems = btrfs_header_nritems(eb);
665         int shift_space = btrfs_leaf_free_space(root, eb) / 2;
666         int slot = nritems / 2;
667         int i = 0;
668         unsigned int data_end = btrfs_item_offset_nr(eb, nritems - 1);
669
670         /* Shift the item data up to and including slot back by shift space */
671         memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end - shift_space,
672                               btrfs_leaf_data(eb) + data_end,
673                               btrfs_item_offset_nr(eb, slot - 1) - data_end);
674
675         /* Now update the item pointers. */
676         for (i = nritems - 1; i >= slot; i--) {
677                 u32 offset = btrfs_item_offset_nr(eb, i);
678                 offset -= shift_space;
679                 btrfs_set_item_offset(eb, btrfs_item_nr(i), offset);
680         }
681 }
682
683 static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
684                                   char *field)
685 {
686         struct btrfs_trans_handle *trans;
687         struct btrfs_path *path;
688         struct extent_buffer *eb;
689         struct btrfs_key key, root_key;
690         enum btrfs_metadata_block_field corrupt_field;
691         u64 root_objectid;
692         u64 orig, bogus;
693         u8 level;
694         int ret;
695
696         corrupt_field = convert_metadata_block_field(field);
697         if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
698                 fprintf(stderr, "Invalid field %s\n", field);
699                 return -EINVAL;
700         }
701
702         eb = read_tree_block(root, block, root->leafsize, 0);
703         if (!extent_buffer_uptodate(eb)) {
704                 fprintf(stderr, "Couldn't read in tree block %s\n", field);
705                 return -EINVAL;
706         }
707         root_objectid = btrfs_header_owner(eb);
708         level = btrfs_header_level(eb);
709         if (level)
710                 btrfs_node_key_to_cpu(eb, &key, 0);
711         else
712                 btrfs_item_key_to_cpu(eb, &key, 0);
713         free_extent_buffer(eb);
714
715         root_key.objectid = root_objectid;
716         root_key.type = BTRFS_ROOT_ITEM_KEY;
717         root_key.offset = (u64)-1;
718
719         root = btrfs_read_fs_root(root->fs_info, &root_key);
720         if (IS_ERR(root)) {
721                 fprintf(stderr, "Couldn't finde owner root %llu\n",
722                         key.objectid);
723                 return PTR_ERR(root);
724         }
725
726         path = btrfs_alloc_path();
727         if (!path)
728                 return -ENOMEM;
729
730         trans = btrfs_start_transaction(root, 1);
731         if (IS_ERR(trans)) {
732                 btrfs_free_path(path);
733                 fprintf(stderr, "Couldn't start transaction %ld\n",
734                         PTR_ERR(trans));
735                 return PTR_ERR(trans);
736         }
737
738         path->lowest_level = level;
739         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
740         if (ret < 0) {
741                 fprintf(stderr, "Error searching to node %d\n", ret);
742                 goto out;
743         }
744         eb = path->nodes[level];
745
746         ret = 0;
747         switch (corrupt_field) {
748         case BTRFS_METADATA_BLOCK_GENERATION:
749                 orig = btrfs_header_generation(eb);
750                 bogus = generate_u64(orig);
751                 btrfs_set_header_generation(eb, bogus);
752                 break;
753         case BTRFS_METADATA_BLOCK_SHIFT_ITEMS:
754                 shift_items(root, path->nodes[level]);
755                 break;
756         default:
757                 ret = -EINVAL;
758                 break;
759         }
760         btrfs_mark_buffer_dirty(path->nodes[level]);
761 out:
762         btrfs_commit_transaction(trans, root);
763         btrfs_free_path(path);
764         return ret;
765 }
766
767 static int corrupt_btrfs_item(struct btrfs_root *root, struct btrfs_key *key,
768                               char *field)
769 {
770         struct btrfs_trans_handle *trans;
771         struct btrfs_path *path;
772         enum btrfs_item_field corrupt_field;
773         u32 orig, bogus;
774         int ret;
775
776         corrupt_field = convert_item_field(field);
777         if (corrupt_field == BTRFS_ITEM_BAD) {
778                 fprintf(stderr, "Invalid field %s\n", field);
779                 return -EINVAL;
780         }
781
782         path = btrfs_alloc_path();
783         if (!path)
784                 return -ENOMEM;
785
786         trans = btrfs_start_transaction(root, 1);
787         if (IS_ERR(trans)) {
788                 btrfs_free_path(path);
789                 fprintf(stderr, "Couldn't start transaction %ld\n",
790                         PTR_ERR(trans));
791                 return PTR_ERR(trans);
792         }
793
794         ret = btrfs_search_slot(trans, root, key, path, 0, 1);
795         if (ret != 0) {
796                 fprintf(stderr, "Error searching to node %d\n", ret);
797                 goto out;
798         }
799
800         ret = 0;
801         switch (corrupt_field) {
802         case BTRFS_ITEM_OFFSET:
803                 orig = btrfs_item_offset_nr(path->nodes[0], path->slots[0]);
804                 bogus = generate_u32(orig);
805                 btrfs_set_item_offset(path->nodes[0],
806                                       btrfs_item_nr(path->slots[0]), bogus);
807                 break;
808         default:
809                 ret = -EINVAL;
810                 break;
811         }
812         btrfs_mark_buffer_dirty(path->nodes[0]);
813 out:
814         btrfs_commit_transaction(trans, root);
815         btrfs_free_path(path);
816         return ret;
817 }
818
819 static int delete_item(struct btrfs_root *root, struct btrfs_key *key)
820 {
821         struct btrfs_trans_handle *trans;
822         struct btrfs_path *path;
823         int ret;
824
825         path = btrfs_alloc_path();
826         if (!path)
827                 return -ENOMEM;
828
829         trans = btrfs_start_transaction(root, 1);
830         if (IS_ERR(trans)) {
831                 btrfs_free_path(path);
832                 fprintf(stderr, "Couldn't start transaction %ld\n",
833                         PTR_ERR(trans));
834                 return PTR_ERR(trans);
835         }
836
837         ret = btrfs_search_slot(trans, root, key, path, -1, 1);
838         if (ret) {
839                 if (ret > 0)
840                         ret = -ENOENT;
841                 fprintf(stderr, "Error searching to node %d\n", ret);
842                 goto out;
843         }
844         ret = btrfs_del_item(trans, root, path);
845         btrfs_mark_buffer_dirty(path->nodes[0]);
846 out:
847         btrfs_commit_transaction(trans, root);
848         btrfs_free_path(path);
849         return ret;
850 }
851
852 static int delete_csum(struct btrfs_root *root, u64 bytenr, u64 bytes)
853 {
854         struct btrfs_trans_handle *trans;
855         int ret;
856
857         root = root->fs_info->csum_root;
858         trans = btrfs_start_transaction(root, 1);
859         if (IS_ERR(trans)) {
860                 fprintf(stderr, "Couldn't start transaction %ld\n",
861                         PTR_ERR(trans));
862                 return PTR_ERR(trans);
863         }
864
865         ret = btrfs_del_csums(trans, root, bytenr, bytes);
866         if (ret)
867                 fprintf(stderr, "Error deleting csums %d\n", ret);
868         btrfs_commit_transaction(trans, root);
869         return ret;
870 }
871
872 /* corrupt item using NO cow.
873  * Because chunk recover will recover based on whole partition scaning,
874  * If using COW, chunk recover will use the old item to recover,
875  * which is still OK but we want to check the ability to rebuild chunk
876  * not only restore the old ones */
877 static int corrupt_item_nocow(struct btrfs_trans_handle *trans,
878                        struct btrfs_root *root, struct btrfs_path *path,
879                        int del)
880 {
881         int ret = 0;
882         struct btrfs_key key;
883         struct extent_buffer *leaf;
884         unsigned long ptr;
885         int slot;
886         u32 item_size;
887
888         leaf = path->nodes[0];
889         slot = path->slots[0];
890         /* Not deleting the first item of a leaf to keep leaf structure */
891         if (slot == 0)
892                 del = 0;
893         /* Only accept valid eb */
894         BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
895         btrfs_item_key_to_cpu(leaf, &key, slot);
896         if (del) {
897                 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
898                         key.objectid, key.type, key.offset);
899                 btrfs_del_item(trans, root, path);
900         } else {
901                 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
902                         key.objectid, key.type, key.offset);
903                 ptr = btrfs_item_ptr_offset(leaf, slot);
904                 item_size = btrfs_item_size_nr(leaf, slot);
905                 memset_extent_buffer(leaf, 0, ptr, item_size);
906                 btrfs_mark_buffer_dirty(leaf);
907         }
908         return ret;
909 }
910 static int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
911                        struct btrfs_root *root)
912 {
913         int ret;
914         int del;
915         int slot;
916         struct btrfs_path *path;
917         struct btrfs_key key;
918         struct btrfs_key found_key;
919         struct extent_buffer *leaf;
920
921         path = btrfs_alloc_path();
922         if (!path)
923                 return -ENOMEM;
924
925         key.objectid = (u64)-1;
926         key.offset = (u64)-1;
927         key.type = (u8)-1;
928
929         /* Here, cow and ins_len must equals 0 for the following reasons:
930          * 1) chunk recover is based on disk scanning, so COW should be
931          *    disabled in case the original chunk being scanned and
932          *    recovered using the old chunk.
933          * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
934          *    triggered.
935          */
936         ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
937         BUG_ON(ret == 0);
938         if (ret < 0) {
939                 fprintf(stderr, "Error searching tree\n");
940                 goto free_out;
941         }
942         /* corrupt/del dev_item first */
943         while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
944                 slot = path->slots[0];
945                 leaf = path->nodes[0];
946                 del = rand() % 3;
947                 /* Never delete the first item to keep the leaf structure */
948                 if (path->slots[0] == 0)
949                         del = 0;
950                 ret = corrupt_item_nocow(trans, root, path, del);
951                 if (ret)
952                         goto free_out;
953         }
954         btrfs_release_path(path);
955
956         /* Here, cow and ins_len must equals 0 for the following reasons:
957          * 1) chunk recover is based on disk scanning, so COW should be
958          *    disabled in case the original chunk being scanned and
959          *    recovered using the old chunk.
960          * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
961          *    triggered.
962          */
963         ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
964         BUG_ON(ret == 0);
965         if (ret < 0) {
966                 fprintf(stderr, "Error searching tree\n");
967                 goto free_out;
968         }
969         /* corrupt/del chunk then*/
970         while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
971                 slot = path->slots[0];
972                 leaf = path->nodes[0];
973                 del = rand() % 3;
974                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
975                 ret = corrupt_item_nocow(trans, root, path, del);
976                 if (ret)
977                         goto free_out;
978         }
979 free_out:
980         btrfs_free_path(path);
981         return ret;
982 }
983 static int find_chunk_offset(struct btrfs_root *root,
984                       struct btrfs_path *path, u64 offset)
985 {
986         struct btrfs_key key;
987         int ret;
988
989         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
990         key.type = BTRFS_CHUNK_ITEM_KEY;
991         key.offset = offset;
992
993         /* Here, cow and ins_len must equals 0 for following reasons:
994          * 1) chunk recover is based on disk scanning, so COW should
995          *    be disabled in case the original chunk being scanned
996          *    and recovered using the old chunk.
997          * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
998          *    will be triggered.
999          */
1000         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1001         if (ret > 0) {
1002                 fprintf(stderr, "Can't find chunk with given offset %llu\n",
1003                         offset);
1004                 goto out;
1005         }
1006         if (ret < 0) {
1007                 fprintf(stderr, "Error searching chunk\n");
1008                 goto out;
1009         }
1010 out:
1011         return ret;
1012
1013 }
1014 int main(int ac, char **av)
1015 {
1016         struct cache_tree root_cache;
1017         struct btrfs_key key;
1018         struct btrfs_root *root;
1019         struct extent_buffer *eb;
1020         char *dev;
1021         /* chunk offset can be 0,so change to (u64)-1 */
1022         u64 logical = (u64)-1;
1023         int ret = 0;
1024         u64 copy = 0;
1025         u64 bytes = 4096;
1026         int extent_rec = 0;
1027         int extent_tree = 0;
1028         int corrupt_block_keys = 0;
1029         int chunk_rec = 0;
1030         int chunk_tree = 0;
1031         int corrupt_item = 0;
1032         int corrupt_di = 0;
1033         int delete = 0;
1034         u64 metadata_block = 0;
1035         u64 inode = 0;
1036         u64 file_extent = (u64)-1;
1037         u64 root_objectid = 0;
1038         u64 csum_bytenr = 0;
1039         char field[FIELD_BUF_LEN];
1040
1041         field[0] = '\0';
1042         srand(128);
1043         memset(&key, 0, sizeof(key));
1044
1045         while(1) {
1046                 int c;
1047                 static const struct option long_options[] = {
1048                         /* { "byte-count", 1, NULL, 'b' }, */
1049                         { "logical", required_argument, NULL, 'l' },
1050                         { "copy", required_argument, NULL, 'c' },
1051                         { "bytes", required_argument, NULL, 'b' },
1052                         { "extent-record", no_argument, NULL, 'e' },
1053                         { "extent-tree", no_argument, NULL, 'E' },
1054                         { "keys", no_argument, NULL, 'k' },
1055                         { "chunk-record", no_argument, NULL, 'u' },
1056                         { "chunk-tree", no_argument, NULL, 'U' },
1057                         { "inode", required_argument, NULL, 'i'},
1058                         { "file-extent", required_argument, NULL, 'x'},
1059                         { "metadata-block", required_argument, NULL, 'm'},
1060                         { "field", required_argument, NULL, 'f'},
1061                         { "key", required_argument, NULL, 'K'},
1062                         { "item", no_argument, NULL, 'I'},
1063                         { "dir-item", no_argument, NULL, 'D'},
1064                         { "delete", no_argument, NULL, 'd'},
1065                         { "root", no_argument, NULL, 'r'},
1066                         { "csum", required_argument, NULL, 'C'},
1067                         { "help", no_argument, NULL, GETOPT_VAL_HELP},
1068                         { NULL, 0, NULL, 0 }
1069                 };
1070
1071                 c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:IDdr:C:",
1072                                 long_options, NULL);
1073                 if (c < 0)
1074                         break;
1075                 switch(c) {
1076                         case 'l':
1077                                 logical = arg_strtou64(optarg);
1078                                 break;
1079                         case 'c':
1080                                 copy = arg_strtou64(optarg);
1081                                 break;
1082                         case 'b':
1083                                 bytes = arg_strtou64(optarg);
1084                                 break;
1085                         case 'e':
1086                                 extent_rec = 1;
1087                                 break;
1088                         case 'E':
1089                                 extent_tree = 1;
1090                                 break;
1091                         case 'k':
1092                                 corrupt_block_keys = 1;
1093                                 break;
1094                         case 'u':
1095                                 chunk_rec = 1;
1096                                 break;
1097                         case 'U':
1098                                 chunk_tree = 1;
1099                                 break;
1100                         case 'i':
1101                                 inode = arg_strtou64(optarg);
1102                                 break;
1103                         case 'f':
1104                                 strncpy(field, optarg, FIELD_BUF_LEN);
1105                                 break;
1106                         case 'x':
1107                                 file_extent = arg_strtou64(optarg);
1108                                 break;
1109                         case 'm':
1110                                 metadata_block = arg_strtou64(optarg);
1111                                 break;
1112                         case 'K':
1113                                 ret = sscanf(optarg, "%llu,%u,%llu",
1114                                              &key.objectid,
1115                                              (unsigned int *)&key.type,
1116                                              &key.offset);
1117                                 if (ret != 3) {
1118                                         fprintf(stderr, "error reading key "
1119                                                 "%d\n", errno);
1120                                         print_usage(1);
1121                                 }
1122                                 break;
1123                         case 'D':
1124                                 corrupt_di = 1;
1125                                 break;
1126                         case 'I':
1127                                 corrupt_item = 1;
1128                                 break;
1129                         case 'd':
1130                                 delete = 1;
1131                                 break;
1132                         case 'r':
1133                                 root_objectid = arg_strtou64(optarg);
1134                                 break;
1135                         case 'C':
1136                                 csum_bytenr = arg_strtou64(optarg);
1137                                 break;
1138                         case GETOPT_VAL_HELP:
1139                         default:
1140                                 print_usage(c != GETOPT_VAL_HELP);
1141                 }
1142         }
1143         set_argv0(av);
1144         ac = ac - optind;
1145         if (check_argc_min(ac, 1))
1146                 print_usage(1);
1147         dev = av[optind];
1148
1149         radix_tree_init();
1150         cache_tree_init(&root_cache);
1151
1152         root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
1153         if (!root) {
1154                 fprintf(stderr, "Open ctree failed\n");
1155                 exit(1);
1156         }
1157         if (extent_rec) {
1158                 struct btrfs_trans_handle *trans;
1159
1160                 if (logical == (u64)-1)
1161                         print_usage(1);
1162                 trans = btrfs_start_transaction(root, 1);
1163                 ret = corrupt_extent (trans, root, logical, 0);
1164                 btrfs_commit_transaction(trans, root);
1165                 goto out_close;
1166         }
1167         if (extent_tree) {
1168                 struct btrfs_trans_handle *trans;
1169                 trans = btrfs_start_transaction(root, 1);
1170                 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
1171                                           root->fs_info->extent_root->node);
1172                 btrfs_commit_transaction(trans, root);
1173                 goto out_close;
1174         }
1175         if (chunk_rec) {
1176                 struct btrfs_trans_handle *trans;
1177                 struct btrfs_path *path;
1178                 int del;
1179
1180                 if (logical == (u64)-1)
1181                         print_usage(1);
1182                 del = rand() % 3;
1183                 path = btrfs_alloc_path();
1184                 if (!path) {
1185                         fprintf(stderr, "path allocation failed\n");
1186                         goto out_close;
1187                 }
1188
1189                 if (find_chunk_offset(root->fs_info->chunk_root, path,
1190                                       logical) != 0) {
1191                         btrfs_free_path(path);
1192                         goto out_close;
1193                 }
1194                 trans = btrfs_start_transaction(root, 1);
1195                 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
1196                                          path, del);
1197                 if (ret < 0)
1198                         fprintf(stderr, "Failed to corrupt chunk record\n");
1199                 btrfs_commit_transaction(trans, root);
1200                 goto out_close;
1201         }
1202         if (chunk_tree) {
1203                 struct btrfs_trans_handle *trans;
1204                 trans = btrfs_start_transaction(root, 1);
1205                 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
1206                 if (ret < 0)
1207                         fprintf(stderr, "Failed to corrupt chunk tree\n");
1208                 btrfs_commit_transaction(trans, root);
1209                 goto out_close;
1210         }
1211         if (inode) {
1212                 struct btrfs_trans_handle *trans;
1213
1214                 if (*field == 0)
1215                         print_usage(1);
1216
1217                 trans = btrfs_start_transaction(root, 1);
1218                 if (file_extent == (u64)-1) {
1219                         printf("corrupting inode\n");
1220                         ret = corrupt_inode(trans, root, inode, field);
1221                 } else {
1222                         printf("corrupting file extent\n");
1223                         ret = corrupt_file_extent(trans, root, inode,
1224                                                   file_extent, field);
1225                 }
1226                 btrfs_commit_transaction(trans, root);
1227                 goto out_close;
1228         }
1229         if (metadata_block) {
1230                 if (*field == 0)
1231                         print_usage(1);
1232                 ret = corrupt_metadata_block(root, metadata_block, field);
1233                 goto out_close;
1234         }
1235         if (corrupt_di) {
1236                 if (!key.objectid || *field == 0)
1237                         print_usage(1);
1238                 ret = corrupt_dir_item(root, &key, field);
1239                 goto out_close;
1240         }
1241         if (csum_bytenr) {
1242                 ret = delete_csum(root, csum_bytenr, bytes);
1243                 goto out_close;
1244         }
1245         if (corrupt_item) {
1246                 if (!key.objectid)
1247                         print_usage(1);
1248                 ret = corrupt_btrfs_item(root, &key, field);
1249         }
1250         if (delete) {
1251                 struct btrfs_root *target = root;
1252
1253                 if (!key.objectid)
1254                         print_usage(1);
1255                 if (root_objectid) {
1256                         struct btrfs_key root_key;
1257
1258                         root_key.objectid = root_objectid;
1259                         root_key.type = BTRFS_ROOT_ITEM_KEY;
1260                         root_key.offset = (u64)-1;
1261
1262                         target = btrfs_read_fs_root(root->fs_info, &root_key);
1263                         if (IS_ERR(target)) {
1264                                 fprintf(stderr, "Couldn't find root %llu\n",
1265                                         (unsigned long long)root_objectid);
1266                                 print_usage(1);
1267                         }
1268                 }
1269                 ret = delete_item(target, &key);
1270                 goto out_close;
1271         }
1272         if (key.objectid || key.offset || key.type) {
1273                 if (*field == 0)
1274                         print_usage(1);
1275                 ret = corrupt_key(root, &key, field);
1276                 goto out_close;
1277         }
1278         /*
1279          * If we made it here and we have extent set then we didn't specify
1280          * inode and we're screwed.
1281          */
1282         if (file_extent != (u64)-1)
1283                 print_usage(1);
1284
1285         if (logical == (u64)-1)
1286                 print_usage(1);
1287
1288         if (bytes == 0)
1289                 bytes = root->sectorsize;
1290
1291         bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
1292         bytes *= root->sectorsize;
1293
1294         while (bytes > 0) {
1295                 if (corrupt_block_keys) {
1296                         corrupt_keys_in_block(root, logical);
1297                 } else {
1298                         eb = debug_corrupt_block(root, logical,
1299                                                  root->sectorsize, copy);
1300                         free_extent_buffer(eb);
1301                 }
1302                 logical += root->sectorsize;
1303                 bytes -= root->sectorsize;
1304         }
1305         return ret;
1306 out_close:
1307         close_ctree(root);
1308         return ret;
1309 }