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