Btrfs-progs: add ability to corrupt dir items
[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 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include "kerncompat.h"
27 #include "ctree.h"
28 #include "volumes.h"
29 #include "disk-io.h"
30 #include "print-tree.h"
31 #include "transaction.h"
32 #include "list.h"
33 #include "version.h"
34 #include "utils.h"
35
36 #define FIELD_BUF_LEN 80
37
38 struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
39                                      u32 blocksize, u64 copy)
40 {
41         int ret;
42         struct extent_buffer *eb;
43         u64 length;
44         struct btrfs_multi_bio *multi = NULL;
45         struct btrfs_device *device;
46         int num_copies;
47         int mirror_num = 1;
48
49         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
50         if (!eb)
51                 return NULL;
52
53         length = blocksize;
54         while (1) {
55                 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
56                                       eb->start, &length, &multi,
57                                       mirror_num, NULL);
58                 BUG_ON(ret);
59                 device = multi->stripes[0].dev;
60                 eb->fd = device->fd;
61                 device->total_ios++;
62                 eb->dev_bytenr = multi->stripes[0].physical;
63
64                 fprintf(stdout,
65                         "mirror %d logical %llu physical %llu device %s\n",
66                         mirror_num, (unsigned long long)bytenr,
67                         (unsigned long long)eb->dev_bytenr, device->name);
68                 kfree(multi);
69
70                 if (!copy || mirror_num == copy) {
71                         ret = read_extent_from_disk(eb, 0, eb->len);
72                         printf("corrupting %llu copy %d\n", eb->start,
73                                mirror_num);
74                         memset(eb->data, 0, eb->len);
75                         write_extent_to_disk(eb);
76                         fsync(eb->fd);
77                 }
78
79                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
80                                               eb->start, eb->len);
81                 if (num_copies == 1)
82                         break;
83
84                 mirror_num++;
85                 if (mirror_num > num_copies)
86                         break;
87         }
88         return eb;
89 }
90
91 static void print_usage(void)
92 {
93         fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n");
94         fprintf(stderr, "\t-l Logical extent to be corrupted\n");
95         fprintf(stderr, "\t-c Copy of the extent to be corrupted"
96                 " (usually 1 or 2, default: 0)\n");
97         fprintf(stderr, "\t-b Number of bytes to be corrupted\n");
98         fprintf(stderr, "\t-e Extent to be corrupted\n");
99         fprintf(stderr, "\t-E The whole extent tree to be corrupted\n");
100         fprintf(stderr, "\t-u Given chunk item to be corrupted\n");
101         fprintf(stderr, "\t-U The whole chunk tree to be corrupted\n");
102         fprintf(stderr, "\t-i The inode item to corrupt (must also specify "
103                 "the field to corrupt)\n");
104         fprintf(stderr, "\t-x The file extent item to corrupt (must also "
105                 "specify -i for the inode and -f for the field to corrupt)\n");
106         fprintf(stderr, "\t-m The metadata block to corrupt (must also "
107                 "specify -f for the field to corrupt)\n");
108         fprintf(stderr, "\t-K The key to corrupt in the format "
109                 "<num>,<num>,<num> (must also specify -f for the field)\n");
110         fprintf(stderr, "\t-f The field in the item to corrupt\n");
111         fprintf(stderr, "\t-D Corrupt a dir item, must specify key and field\n");
112         exit(1);
113 }
114
115 static void corrupt_keys(struct btrfs_trans_handle *trans,
116                          struct btrfs_root *root,
117                          struct extent_buffer *eb)
118 {
119         int slot;
120         int bad_slot;
121         int nr;
122         struct btrfs_disk_key bad_key;;
123
124         nr = btrfs_header_nritems(eb);
125         if (nr == 0)
126                 return;
127
128         slot = rand() % nr;
129         bad_slot = rand() % nr;
130
131         if (bad_slot == slot)
132                 return;
133
134         fprintf(stderr,
135                 "corrupting keys in block %llu slot %d swapping with %d\n",
136                 (unsigned long long)eb->start, slot, bad_slot);
137
138         if (btrfs_header_level(eb) == 0) {
139                 btrfs_item_key(eb, &bad_key, bad_slot);
140                 btrfs_set_item_key(eb, &bad_key, slot);
141         } else {
142                 btrfs_node_key(eb, &bad_key, bad_slot);
143                 btrfs_set_node_key(eb, &bad_key, slot);
144         }
145         btrfs_mark_buffer_dirty(eb);
146         if (!trans) {
147                 u16 csum_size =
148                         btrfs_super_csum_size(root->fs_info->super_copy);
149                 csum_tree_block_size(eb, csum_size, 0);
150                 write_extent_to_disk(eb);
151         }
152 }
153
154
155 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
156 {
157         struct extent_buffer *eb;
158
159         eb = read_tree_block(root, bytenr, root->leafsize, 0);
160         if (!eb)
161                 return -EIO;;
162
163         corrupt_keys(NULL, root, eb);
164         free_extent_buffer(eb);
165         return 0;
166 }
167
168 static int corrupt_extent(struct btrfs_trans_handle *trans,
169                           struct btrfs_root *root, u64 bytenr, u64 copy)
170 {
171         struct btrfs_key key;
172         struct extent_buffer *leaf;
173         u32 item_size;
174         unsigned long ptr;
175         struct btrfs_path *path;
176         int ret;
177         int slot;
178         int should_del = rand() % 3;
179
180         path = btrfs_alloc_path();
181         if (!path)
182                 return -ENOMEM;
183
184         key.objectid = bytenr;
185         key.type = (u8)-1;
186         key.offset = (u64)-1;
187
188         while(1) {
189                 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
190                                         &key, path, -1, 1);
191                 if (ret < 0)
192                         break;
193
194                 if (ret > 0) {
195                         if (path->slots[0] == 0)
196                                 break;
197                         path->slots[0]--;
198                         ret = 0;
199                 }
200                 leaf = path->nodes[0];
201                 slot = path->slots[0];
202                 btrfs_item_key_to_cpu(leaf, &key, slot);
203                 if (key.objectid != bytenr)
204                         break;
205
206                 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
207                     key.type != BTRFS_TREE_BLOCK_REF_KEY &&
208                     key.type != BTRFS_EXTENT_DATA_REF_KEY &&
209                     key.type != BTRFS_EXTENT_REF_V0_KEY &&
210                     key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
211                     key.type != BTRFS_SHARED_DATA_REF_KEY)
212                         goto next;
213
214                 if (should_del) {
215                         fprintf(stderr,
216                                 "deleting extent record: key %llu %u %llu\n",
217                                 key.objectid, key.type, key.offset);
218
219                         if (key.type == BTRFS_EXTENT_ITEM_KEY) {
220                                 /* make sure this extent doesn't get
221                                  * reused for other purposes */
222                                 btrfs_pin_extent(root->fs_info,
223                                                  key.objectid, key.offset);
224                         }
225
226                         btrfs_del_item(trans, root, path);
227                 } else {
228                         fprintf(stderr,
229                                 "corrupting extent record: key %llu %u %llu\n",
230                                 key.objectid, key.type, key.offset);
231                         ptr = btrfs_item_ptr_offset(leaf, slot);
232                         item_size = btrfs_item_size_nr(leaf, slot);
233                         memset_extent_buffer(leaf, 0, ptr, item_size);
234                         btrfs_mark_buffer_dirty(leaf);
235                 }
236 next:
237                 btrfs_release_path(path);
238
239                 if (key.offset > 0)
240                         key.offset--;
241                 if (key.offset == 0)
242                         break;
243         }
244
245         btrfs_free_path(path);
246         return 0;
247 }
248
249 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
250                                       struct btrfs_root *root,
251                                       struct extent_buffer *eb)
252 {
253         u32 nr = btrfs_header_nritems(eb);
254         u32 victim = rand() % nr;
255         u64 objectid;
256         struct btrfs_key key;
257
258         btrfs_item_key_to_cpu(eb, &key, victim);
259         objectid = key.objectid;
260         corrupt_extent(trans, root, objectid, 1);
261 }
262
263 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
264                                       struct btrfs_root *root,
265                                       struct extent_buffer *eb)
266 {
267         int i;
268
269         if (!eb)
270                 return;
271
272         if (btrfs_is_leaf(eb)) {
273                 btrfs_corrupt_extent_leaf(trans, root, eb);
274                 return;
275         }
276
277         if (btrfs_header_level(eb) == 1 && eb != root->node) {
278                 if (rand() % 5)
279                         return;
280         }
281
282         for (i = 0; i < btrfs_header_nritems(eb); i++) {
283                 struct extent_buffer *next;
284
285                 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
286                                        root->leafsize,
287                                        btrfs_node_ptr_generation(eb, i));
288                 if (!next)
289                         continue;
290                 btrfs_corrupt_extent_tree(trans, root, next);
291                 free_extent_buffer(next);
292         }
293 }
294
295 enum btrfs_inode_field {
296         BTRFS_INODE_FIELD_ISIZE,
297         BTRFS_INODE_FIELD_BAD,
298 };
299
300 enum btrfs_file_extent_field {
301         BTRFS_FILE_EXTENT_DISK_BYTENR,
302         BTRFS_FILE_EXTENT_BAD,
303 };
304
305 enum btrfs_dir_item_field {
306         BTRFS_DIR_ITEM_NAME,
307         BTRFS_DIR_ITEM_LOCATION_OBJECTID,
308         BTRFS_DIR_ITEM_BAD,
309 };
310
311 enum btrfs_metadata_block_field {
312         BTRFS_METADATA_BLOCK_GENERATION,
313         BTRFS_METADATA_BLOCK_BAD,
314 };
315
316 enum btrfs_key_field {
317         BTRFS_KEY_OBJECTID,
318         BTRFS_KEY_TYPE,
319         BTRFS_KEY_OFFSET,
320         BTRFS_KEY_BAD,
321 };
322
323 static enum btrfs_inode_field convert_inode_field(char *field)
324 {
325         if (!strncmp(field, "isize", FIELD_BUF_LEN))
326                 return BTRFS_INODE_FIELD_ISIZE;
327         return BTRFS_INODE_FIELD_BAD;
328 }
329
330 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
331 {
332         if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
333                 return BTRFS_FILE_EXTENT_DISK_BYTENR;
334         return BTRFS_FILE_EXTENT_BAD;
335 }
336
337 static enum btrfs_metadata_block_field
338 convert_metadata_block_field(char *field)
339 {
340         if (!strncmp(field, "generation", FIELD_BUF_LEN))
341                 return BTRFS_METADATA_BLOCK_GENERATION;
342         return BTRFS_METADATA_BLOCK_BAD;
343 }
344
345 static enum btrfs_key_field convert_key_field(char *field)
346 {
347         if (!strncmp(field, "objectid", FIELD_BUF_LEN))
348                 return BTRFS_KEY_OBJECTID;
349         if (!strncmp(field, "type", FIELD_BUF_LEN))
350                 return BTRFS_KEY_TYPE;
351         if (!strncmp(field, "offset", FIELD_BUF_LEN))
352                 return BTRFS_KEY_OFFSET;
353         return BTRFS_KEY_BAD;
354 }
355
356 static enum btrfs_dir_item_field convert_dir_item_field(char *field)
357 {
358         if (!strncmp(field, "name", FIELD_BUF_LEN))
359                 return BTRFS_DIR_ITEM_NAME;
360         if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
361                 return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
362         return BTRFS_DIR_ITEM_BAD;
363 }
364
365 static u64 generate_u64(u64 orig)
366 {
367         u64 ret;
368         do {
369                 ret = rand();
370         } while (ret == orig);
371         return ret;
372 }
373
374 static u8 generate_u8(u8 orig)
375 {
376         u8 ret;
377         do {
378                 ret = rand();
379         } while (ret == orig);
380         return ret;
381 }
382
383 static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
384                        char *field)
385 {
386         enum btrfs_key_field corrupt_field = convert_key_field(field);
387         struct btrfs_path *path;
388         struct btrfs_trans_handle *trans;
389         int ret;
390
391         root = root->fs_info->fs_root;
392         if (corrupt_field == BTRFS_KEY_BAD) {
393                 fprintf(stderr, "Invalid field %s\n", field);
394                 return -EINVAL;
395         }
396
397         path = btrfs_alloc_path();
398         if (!path)
399                 return -ENOMEM;
400
401         trans = btrfs_start_transaction(root, 1);
402         if (IS_ERR(trans)) {
403                 btrfs_free_path(path);
404                 return PTR_ERR(trans);
405         }
406
407         ret = btrfs_search_slot(trans, root, key, path, 0, 1);
408         if (ret < 0)
409                 goto out;
410         if (ret > 0) {
411                 fprintf(stderr, "Couldn't find the key to corrupt\n");
412                 ret = -ENOENT;
413                 goto out;
414         }
415
416         switch (corrupt_field) {
417         case BTRFS_KEY_OBJECTID:
418                 key->objectid = generate_u64(key->objectid);
419                 break;
420         case BTRFS_KEY_TYPE:
421                 key->type = generate_u8(key->type);
422                 break;
423         case BTRFS_KEY_OFFSET:
424                 key->offset = generate_u64(key->objectid);
425                 break;
426         default:
427                 fprintf(stderr, "Invalid field %s, %d\n", field,
428                         corrupt_field);
429                 ret = -EINVAL;
430                 goto out;
431         }
432
433         btrfs_set_item_key_unsafe(root, path, key);
434 out:
435         btrfs_free_path(path);
436         btrfs_commit_transaction(trans, root);
437         return ret;
438 }
439
440 static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
441                             char *field)
442 {
443         struct btrfs_trans_handle *trans;
444         struct btrfs_dir_item *di;
445         struct btrfs_path *path;
446         char *name;
447         struct btrfs_key location;
448         struct btrfs_disk_key disk_key;
449         unsigned long name_ptr;
450         enum btrfs_dir_item_field corrupt_field =
451                 convert_dir_item_field(field);
452         u64 bogus;
453         u16 name_len;
454         int ret;
455
456         if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
457                 fprintf(stderr, "Invalid field %s\n", field);
458                 return -EINVAL;
459         }
460
461         path = btrfs_alloc_path();
462         if (!path)
463                 return -ENOMEM;
464
465         trans = btrfs_start_transaction(root, 1);
466         if (IS_ERR(trans)) {
467                 btrfs_free_path(path);
468                 return PTR_ERR(trans);
469         }
470
471         ret = btrfs_search_slot(trans, root, key, path, 0, 1);
472         if (ret) {
473                 if (ret > 0)
474                         ret = -ENOENT;
475                 fprintf(stderr, "Error searching for dir item %d\n", ret);
476                 goto out;
477         }
478
479         di = btrfs_item_ptr(path->nodes[0], path->slots[0],
480                             struct btrfs_dir_item);
481
482         switch (corrupt_field) {
483         case BTRFS_DIR_ITEM_NAME:
484                 name_len = btrfs_dir_name_len(path->nodes[0], di);
485                 name = malloc(name_len);
486                 if (!name) {
487                         ret = -ENOMEM;
488                         goto out;
489                 }
490                 name_ptr = (unsigned long)(di + 1);
491                 read_extent_buffer(path->nodes[0], name, name_ptr, name_len);
492                 name[0]++;
493                 write_extent_buffer(path->nodes[0], name, name_ptr, name_len);
494                 btrfs_mark_buffer_dirty(path->nodes[0]);
495                 free(name);
496                 goto out;
497         case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
498                 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
499                 bogus = generate_u64(location.objectid);
500                 location.objectid = bogus;
501                 btrfs_cpu_key_to_disk(&disk_key, &location);
502                 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
503                 btrfs_mark_buffer_dirty(path->nodes[0]);
504                 goto out;
505         default:
506                 ret = -EINVAL;
507                 goto out;
508         }
509 out:
510         btrfs_commit_transaction(trans, root);
511         btrfs_free_path(path);
512         return ret;
513 }
514
515 static int corrupt_inode(struct btrfs_trans_handle *trans,
516                          struct btrfs_root *root, u64 inode, char *field)
517 {
518         struct btrfs_inode_item *ei;
519         struct btrfs_path *path;
520         struct btrfs_key key;
521         enum btrfs_inode_field corrupt_field = convert_inode_field(field);
522         u64 bogus;
523         u64 orig;
524         int ret;
525
526         if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
527                 fprintf(stderr, "Invalid field %s\n", field);
528                 return -EINVAL;
529         }
530
531         key.objectid = inode;
532         key.type = BTRFS_INODE_ITEM_KEY;
533         key.offset = (u64)-1;
534
535         path = btrfs_alloc_path();
536         if (!path)
537                 return -ENOMEM;
538
539         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
540         if (ret < 0)
541                 goto out;
542         if (ret) {
543                 if (!path->slots[0]) {
544                         fprintf(stderr, "Couldn't find inode %Lu\n", inode);
545                         ret = -ENOENT;
546                         goto out;
547                 }
548                 path->slots[0]--;
549                 ret = 0;
550         }
551
552         btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
553         if (key.objectid != inode) {
554                 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
555                 ret = -ENOENT;
556                 goto out;
557         }
558
559         ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
560                             struct btrfs_inode_item);
561         switch (corrupt_field) {
562         case BTRFS_INODE_FIELD_ISIZE:
563                 orig = btrfs_inode_size(path->nodes[0], ei);
564                 bogus = generate_u64(orig);
565                 btrfs_set_inode_size(path->nodes[0], ei, bogus);
566                 break;
567         default:
568                 ret = -EINVAL;
569                 break;
570         }
571         btrfs_mark_buffer_dirty(path->nodes[0]);
572 out:
573         btrfs_free_path(path);
574         return ret;
575 }
576
577 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
578                                struct btrfs_root *root, u64 inode, u64 extent,
579                                char *field)
580 {
581         struct btrfs_file_extent_item *fi;
582         struct btrfs_path *path;
583         struct btrfs_key key;
584         enum btrfs_file_extent_field corrupt_field;
585         u64 bogus;
586         u64 orig;
587         int ret = 0;
588
589         corrupt_field = convert_file_extent_field(field);
590         if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
591                 fprintf(stderr, "Invalid field %s\n", field);
592                 return -EINVAL;
593         }
594
595         key.objectid = inode;
596         key.type = BTRFS_EXTENT_DATA_KEY;
597         key.offset = extent;
598
599         path = btrfs_alloc_path();
600         if (!path)
601                 return -ENOMEM;
602
603         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
604         if (ret < 0)
605                 goto out;
606         if (ret) {
607                 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
608                         extent, inode);
609                 ret = -ENOENT;
610                 goto out;
611         }
612
613         fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
614                             struct btrfs_file_extent_item);
615         switch (corrupt_field) {
616         case BTRFS_FILE_EXTENT_DISK_BYTENR:
617                 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
618                 bogus = generate_u64(orig);
619                 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
620                 break;
621         default:
622                 ret = -EINVAL;
623                 break;
624         }
625         btrfs_mark_buffer_dirty(path->nodes[0]);
626 out:
627         btrfs_free_path(path);
628         return ret;
629 }
630
631 static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
632                                   char *field)
633 {
634         struct btrfs_trans_handle *trans;
635         struct btrfs_path *path;
636         struct extent_buffer *eb;
637         struct btrfs_key key, root_key;
638         enum btrfs_metadata_block_field corrupt_field;
639         u64 root_objectid;
640         u64 orig, bogus;
641         u8 level;
642         int ret;
643
644         corrupt_field = convert_metadata_block_field(field);
645         if (corrupt_field == BTRFS_METADATA_BLOCK_BAD) {
646                 fprintf(stderr, "Invalid field %s\n", field);
647                 return -EINVAL;
648         }
649
650         eb = read_tree_block(root, block, root->leafsize, 0);
651         if (!eb) {
652                 fprintf(stderr, "Couldn't read in tree block %s\n", field);
653                 return -EINVAL;
654         }
655         root_objectid = btrfs_header_owner(eb);
656         level = btrfs_header_level(eb);
657         if (level)
658                 btrfs_node_key_to_cpu(eb, &key, 0);
659         else
660                 btrfs_item_key_to_cpu(eb, &key, 0);
661         free_extent_buffer(eb);
662
663         root_key.objectid = root_objectid;
664         root_key.type = BTRFS_ROOT_ITEM_KEY;
665         root_key.offset = (u64)-1;
666
667         root = btrfs_read_fs_root(root->fs_info, &root_key);
668         if (IS_ERR(root)) {
669                 fprintf(stderr, "Couldn't finde owner root %llu\n",
670                         key.objectid);
671                 return PTR_ERR(root);
672         }
673
674         path = btrfs_alloc_path();
675         if (!path)
676                 return -ENOMEM;
677
678         trans = btrfs_start_transaction(root, 1);
679         if (IS_ERR(trans)) {
680                 btrfs_free_path(path);
681                 fprintf(stderr, "Couldn't start transaction %ld\n",
682                         PTR_ERR(trans));
683                 return PTR_ERR(trans);
684         }
685
686         path->lowest_level = level;
687         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
688         if (ret < 0) {
689                 fprintf(stderr, "Error searching to node %d\n", ret);
690                 goto out;
691         }
692         eb = path->nodes[level];
693
694         ret = 0;
695         switch (corrupt_field) {
696         case BTRFS_METADATA_BLOCK_GENERATION:
697                 orig = btrfs_header_generation(eb);
698                 bogus = generate_u64(orig);
699                 btrfs_set_header_generation(eb, bogus);
700                 break;
701         default:
702                 ret = -EINVAL;
703                 break;
704         }
705         btrfs_mark_buffer_dirty(path->nodes[level]);
706 out:
707         btrfs_commit_transaction(trans, root);
708         btrfs_free_path(path);
709         return ret;
710 }
711
712 static struct option long_options[] = {
713         /* { "byte-count", 1, NULL, 'b' }, */
714         { "logical", 1, NULL, 'l' },
715         { "copy", 1, NULL, 'c' },
716         { "bytes", 1, NULL, 'b' },
717         { "extent-record", 0, NULL, 'e' },
718         { "extent-tree", 0, NULL, 'E' },
719         { "keys", 0, NULL, 'k' },
720         { "chunk-record", 0, NULL, 'u' },
721         { "chunk-tree", 0, NULL, 'U' },
722         { "inode", 1, NULL, 'i'},
723         { "file-extent", 1, NULL, 'x'},
724         { "metadata-block", 1, NULL, 'm'},
725         { "field", 1, NULL, 'f'},
726         { "key", 1, NULL, 'K'},
727         { "dir-item", 0, NULL, 'D'},
728         { 0, 0, 0, 0}
729 };
730
731 /* corrupt item using NO cow.
732  * Because chunk recover will recover based on whole partition scaning,
733  * If using COW, chunk recover will use the old item to recover,
734  * which is still OK but we want to check the ability to rebuild chunk
735  * not only restore the old ones */
736 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
737                        struct btrfs_root *root, struct btrfs_path *path,
738                        int del)
739 {
740         int ret = 0;
741         struct btrfs_key key;
742         struct extent_buffer *leaf;
743         unsigned long ptr;
744         int slot;
745         u32 item_size;
746
747         leaf = path->nodes[0];
748         slot = path->slots[0];
749         /* Not deleting the first item of a leaf to keep leaf structure */
750         if (slot == 0)
751                 del = 0;
752         /* Only accept valid eb */
753         BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
754         btrfs_item_key_to_cpu(leaf, &key, slot);
755         if (del) {
756                 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
757                         key.objectid, key.type, key.offset);
758                 btrfs_del_item(trans, root, path);
759         } else {
760                 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
761                         key.objectid, key.type, key.offset);
762                 ptr = btrfs_item_ptr_offset(leaf, slot);
763                 item_size = btrfs_item_size_nr(leaf, slot);
764                 memset_extent_buffer(leaf, 0, ptr, item_size);
765                 btrfs_mark_buffer_dirty(leaf);
766         }
767         return ret;
768 }
769 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
770                        struct btrfs_root *root)
771 {
772         int ret;
773         int del;
774         int slot;
775         struct btrfs_path *path;
776         struct btrfs_key key;
777         struct btrfs_key found_key;
778         struct extent_buffer *leaf;
779
780         path = btrfs_alloc_path();
781         if (!path)
782                 return -ENOMEM;
783
784         key.objectid = (u64)-1;
785         key.offset = (u64)-1;
786         key.type = (u8)-1;
787
788         /* Here, cow and ins_len must equals 0 for the following reasons:
789          * 1) chunk recover is based on disk scanning, so COW should be
790          *    disabled in case the original chunk being scanned and
791          *    recovered using the old chunk.
792          * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
793          *    triggered.
794          */
795         ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
796         BUG_ON(ret == 0);
797         if (ret < 0) {
798                 fprintf(stderr, "Error searching tree\n");
799                 goto free_out;
800         }
801         /* corrupt/del dev_item first */
802         while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
803                 slot = path->slots[0];
804                 leaf = path->nodes[0];
805                 del = rand() % 3;
806                 /* Never delete the first item to keep the leaf structure */
807                 if (path->slots[0] == 0)
808                         del = 0;
809                 ret = corrupt_item_nocow(trans, root, path, del);
810                 if (ret)
811                         goto free_out;
812         }
813         btrfs_release_path(path);
814
815         /* Here, cow and ins_len must equals 0 for the following reasons:
816          * 1) chunk recover is based on disk scanning, so COW should be
817          *    disabled in case the original chunk being scanned and
818          *    recovered using the old chunk.
819          * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
820          *    triggered.
821          */
822         ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
823         BUG_ON(ret == 0);
824         if (ret < 0) {
825                 fprintf(stderr, "Error searching tree\n");
826                 goto free_out;
827         }
828         /* corrupt/del chunk then*/
829         while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
830                 slot = path->slots[0];
831                 leaf = path->nodes[0];
832                 del = rand() % 3;
833                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
834                 ret = corrupt_item_nocow(trans, root, path, del);
835                 if (ret)
836                         goto free_out;
837         }
838 free_out:
839         btrfs_free_path(path);
840         return ret;
841 }
842 int find_chunk_offset(struct btrfs_root *root,
843                       struct btrfs_path *path, u64 offset)
844 {
845         struct btrfs_key key;
846         int ret;
847
848         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
849         key.type = BTRFS_CHUNK_ITEM_KEY;
850         key.offset = offset;
851
852         /* Here, cow and ins_len must equals 0 for following reasons:
853          * 1) chunk recover is based on disk scanning, so COW should
854          *    be disabled in case the original chunk being scanned
855          *    and recovered using the old chunk.
856          * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
857          *    will be triggered.
858          */
859         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
860         if (ret > 0) {
861                 fprintf(stderr, "Can't find chunk with given offset %llu\n",
862                         offset);
863                 goto out;
864         }
865         if (ret < 0) {
866                 fprintf(stderr, "Error searching chunk");
867                 goto out;
868         }
869 out:
870         return ret;
871
872 }
873 int main(int ac, char **av)
874 {
875         struct cache_tree root_cache;
876         struct btrfs_key key;
877         struct btrfs_root *root;
878         struct extent_buffer *eb;
879         char *dev;
880         /* chunk offset can be 0,so change to (u64)-1 */
881         u64 logical = (u64)-1;
882         int ret = 0;
883         int option_index = 0;
884         u64 copy = 0;
885         u64 bytes = 4096;
886         int extent_rec = 0;
887         int extent_tree = 0;
888         int corrupt_block_keys = 0;
889         int chunk_rec = 0;
890         int chunk_tree = 0;
891         int corrupt_di = 0;
892         u64 metadata_block = 0;
893         u64 inode = 0;
894         u64 file_extent = (u64)-1;
895         char field[FIELD_BUF_LEN];
896
897         field[0] = '\0';
898         srand(128);
899         memset(&key, 0, sizeof(key));
900
901         while(1) {
902                 int c;
903                 c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:D", long_options,
904                                 &option_index);
905                 if (c < 0)
906                         break;
907                 switch(c) {
908                         case 'l':
909                                 logical = arg_strtou64(optarg);
910                                 break;
911                         case 'c':
912                                 copy = arg_strtou64(optarg);
913                                 break;
914                         case 'b':
915                                 bytes = arg_strtou64(optarg);
916                                 break;
917                         case 'e':
918                                 extent_rec = 1;
919                                 break;
920                         case 'E':
921                                 extent_tree = 1;
922                                 break;
923                         case 'k':
924                                 corrupt_block_keys = 1;
925                                 break;
926                         case 'u':
927                                 chunk_rec = 1;
928                                 break;
929                         case 'U':
930                                 chunk_tree = 1;
931                         case 'i':
932                                 inode = arg_strtou64(optarg);
933                                 break;
934                         case 'f':
935                                 strncpy(field, optarg, FIELD_BUF_LEN);
936                                 break;
937                         case 'x':
938                                 file_extent = arg_strtou64(optarg);
939                                 break;
940                         case 'm':
941                                 metadata_block = arg_strtou64(optarg);
942                                 break;
943                         case 'K':
944                                 ret = sscanf(optarg, "%llu,%u,%llu",
945                                              &key.objectid,
946                                              (unsigned int *)&key.type,
947                                              &key.offset);
948                                 if (ret != 3) {
949                                         fprintf(stderr, "error reading key "
950                                                 "%d\n", errno);
951                                         print_usage();
952                                 }
953                                 break;
954                         case 'D':
955                                 corrupt_di = 1;
956                                 break;
957                         default:
958                                 print_usage();
959                 }
960         }
961         set_argv0(av);
962         ac = ac - optind;
963         if (check_argc_min(ac, 1))
964                 print_usage();
965         dev = av[optind];
966
967         radix_tree_init();
968         cache_tree_init(&root_cache);
969
970         root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
971         if (!root) {
972                 fprintf(stderr, "Open ctree failed\n");
973                 exit(1);
974         }
975         if (extent_rec) {
976                 struct btrfs_trans_handle *trans;
977
978                 if (logical == (u64)-1)
979                         print_usage();
980                 trans = btrfs_start_transaction(root, 1);
981                 ret = corrupt_extent (trans, root, logical, 0);
982                 btrfs_commit_transaction(trans, root);
983                 goto out_close;
984         }
985         if (extent_tree) {
986                 struct btrfs_trans_handle *trans;
987                 trans = btrfs_start_transaction(root, 1);
988                 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
989                                           root->fs_info->extent_root->node);
990                 btrfs_commit_transaction(trans, root);
991                 goto out_close;
992         }
993         if (chunk_rec) {
994                 struct btrfs_trans_handle *trans;
995                 struct btrfs_path *path;
996                 int del;
997
998                 if (logical == (u64)-1)
999                         print_usage();
1000                 del = rand() % 3;
1001                 path = btrfs_alloc_path();
1002                 if (!path) {
1003                         fprintf(stderr, "path allocation failed\n");
1004                         goto out_close;
1005                 }
1006
1007                 if (find_chunk_offset(root->fs_info->chunk_root, path,
1008                                       logical) != 0) {
1009                         btrfs_free_path(path);
1010                         goto out_close;
1011                 }
1012                 trans = btrfs_start_transaction(root, 1);
1013                 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
1014                                          path, del);
1015                 if (ret < 0)
1016                         fprintf(stderr, "Failed to corrupt chunk record\n");
1017                 btrfs_commit_transaction(trans, root);
1018                 goto out_close;
1019         }
1020         if (chunk_tree) {
1021                 struct btrfs_trans_handle *trans;
1022                 trans = btrfs_start_transaction(root, 1);
1023                 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
1024                 if (ret < 0)
1025                         fprintf(stderr, "Failed to corrupt chunk tree\n");
1026                 btrfs_commit_transaction(trans, root);
1027                 goto out_close;
1028         }
1029         if (inode) {
1030                 struct btrfs_trans_handle *trans;
1031
1032                 if (!strlen(field))
1033                         print_usage();
1034
1035                 trans = btrfs_start_transaction(root, 1);
1036                 if (file_extent == (u64)-1) {
1037                         printf("corrupting inode\n");
1038                         ret = corrupt_inode(trans, root, inode, field);
1039                 } else {
1040                         printf("corrupting file extent\n");
1041                         ret = corrupt_file_extent(trans, root, inode,
1042                                                   file_extent, field);
1043                 }
1044                 btrfs_commit_transaction(trans, root);
1045                 goto out_close;
1046         }
1047         if (metadata_block) {
1048                 if (!strlen(field))
1049                         print_usage();
1050                 ret = corrupt_metadata_block(root, metadata_block, field);
1051                 goto out_close;
1052         }
1053         if (corrupt_di) {
1054                 if (!key.objectid || !strlen(field))
1055                         print_usage();
1056                 ret = corrupt_dir_item(root, &key, field);
1057                 goto out_close;
1058         }
1059         if (key.objectid || key.offset || key.type) {
1060                 if (!strlen(field))
1061                         print_usage();
1062                 ret = corrupt_key(root, &key, field);
1063                 goto out_close;
1064         }
1065         /*
1066          * If we made it here and we have extent set then we didn't specify
1067          * inode and we're screwed.
1068          */
1069         if (file_extent != (u64)-1)
1070                 print_usage();
1071
1072         if (logical == (u64)-1)
1073                 print_usage();
1074
1075         if (bytes == 0)
1076                 bytes = root->sectorsize;
1077
1078         bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
1079         bytes *= root->sectorsize;
1080
1081         while (bytes > 0) {
1082                 if (corrupt_block_keys) {
1083                         corrupt_keys_in_block(root, logical);
1084                 } else {
1085                         eb = debug_corrupt_block(root, logical,
1086                                                  root->sectorsize, copy);
1087                         free_extent_buffer(eb);
1088                 }
1089                 logical += root->sectorsize;
1090                 bytes -= root->sectorsize;
1091         }
1092         return ret;
1093 out_close:
1094         close_ctree(root);
1095         return ret;
1096 }