Btrfs-progs: add ability to corrupt file extent disk bytenr
[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, int 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-f The field in the item to corrupt\n");
107         exit(1);
108 }
109
110 static void corrupt_keys(struct btrfs_trans_handle *trans,
111                          struct btrfs_root *root,
112                          struct extent_buffer *eb)
113 {
114         int slot;
115         int bad_slot;
116         int nr;
117         struct btrfs_disk_key bad_key;;
118
119         nr = btrfs_header_nritems(eb);
120         if (nr == 0)
121                 return;
122
123         slot = rand() % nr;
124         bad_slot = rand() % nr;
125
126         if (bad_slot == slot)
127                 return;
128
129         fprintf(stderr,
130                 "corrupting keys in block %llu slot %d swapping with %d\n",
131                 (unsigned long long)eb->start, slot, bad_slot);
132
133         if (btrfs_header_level(eb) == 0) {
134                 btrfs_item_key(eb, &bad_key, bad_slot);
135                 btrfs_set_item_key(eb, &bad_key, slot);
136         } else {
137                 btrfs_node_key(eb, &bad_key, bad_slot);
138                 btrfs_set_node_key(eb, &bad_key, slot);
139         }
140         btrfs_mark_buffer_dirty(eb);
141         if (!trans) {
142                 csum_tree_block(root, eb, 0);
143                 write_extent_to_disk(eb);
144         }
145 }
146
147
148 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
149 {
150         struct extent_buffer *eb;
151
152         eb = read_tree_block(root, bytenr, root->leafsize, 0);
153         if (!eb)
154                 return -EIO;;
155
156         corrupt_keys(NULL, root, eb);
157         free_extent_buffer(eb);
158         return 0;
159 }
160
161 static int corrupt_extent(struct btrfs_trans_handle *trans,
162                           struct btrfs_root *root, u64 bytenr, int copy)
163 {
164         struct btrfs_key key;
165         struct extent_buffer *leaf;
166         u32 item_size;
167         unsigned long ptr;
168         struct btrfs_path *path;
169         int ret;
170         int slot;
171         int should_del = rand() % 3;
172
173         path = btrfs_alloc_path();
174         if (!path)
175                 return -ENOMEM;
176
177         key.objectid = bytenr;
178         key.type = (u8)-1;
179         key.offset = (u64)-1;
180
181         while(1) {
182                 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
183                                         &key, path, -1, 1);
184                 if (ret < 0)
185                         break;
186
187                 if (ret > 0) {
188                         if (path->slots[0] == 0)
189                                 break;
190                         path->slots[0]--;
191                         ret = 0;
192                 }
193                 leaf = path->nodes[0];
194                 slot = path->slots[0];
195                 btrfs_item_key_to_cpu(leaf, &key, slot);
196                 if (key.objectid != bytenr)
197                         break;
198
199                 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
200                     key.type != BTRFS_TREE_BLOCK_REF_KEY &&
201                     key.type != BTRFS_EXTENT_DATA_REF_KEY &&
202                     key.type != BTRFS_EXTENT_REF_V0_KEY &&
203                     key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
204                     key.type != BTRFS_SHARED_DATA_REF_KEY)
205                         goto next;
206
207                 if (should_del) {
208                         fprintf(stderr,
209                                 "deleting extent record: key %llu %u %llu\n",
210                                 key.objectid, key.type, key.offset);
211
212                         if (key.type == BTRFS_EXTENT_ITEM_KEY) {
213                                 /* make sure this extent doesn't get
214                                  * reused for other purposes */
215                                 btrfs_pin_extent(root->fs_info,
216                                                  key.objectid, key.offset);
217                         }
218
219                         btrfs_del_item(trans, root, path);
220                 } else {
221                         fprintf(stderr,
222                                 "corrupting extent record: key %llu %u %llu\n",
223                                 key.objectid, key.type, key.offset);
224                         ptr = btrfs_item_ptr_offset(leaf, slot);
225                         item_size = btrfs_item_size_nr(leaf, slot);
226                         memset_extent_buffer(leaf, 0, ptr, item_size);
227                         btrfs_mark_buffer_dirty(leaf);
228                 }
229 next:
230                 btrfs_release_path(path);
231
232                 if (key.offset > 0)
233                         key.offset--;
234                 if (key.offset == 0)
235                         break;
236         }
237
238         btrfs_free_path(path);
239         return 0;
240 }
241
242 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
243                                       struct btrfs_root *root,
244                                       struct extent_buffer *eb)
245 {
246         u32 nr = btrfs_header_nritems(eb);
247         u32 victim = rand() % nr;
248         u64 objectid;
249         struct btrfs_key key;
250
251         btrfs_item_key_to_cpu(eb, &key, victim);
252         objectid = key.objectid;
253         corrupt_extent(trans, root, objectid, 1);
254 }
255
256 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
257                                       struct btrfs_root *root,
258                                       struct extent_buffer *eb)
259 {
260         int i;
261         u32 nr;
262
263         if (!eb)
264                 return;
265
266         nr = btrfs_header_nritems(eb);
267         if (btrfs_is_leaf(eb)) {
268                 btrfs_corrupt_extent_leaf(trans, root, eb);
269                 return;
270         }
271
272         if (btrfs_header_level(eb) == 1 && eb != root->node) {
273                 if (rand() % 5)
274                         return;
275         }
276
277         for (i = 0; i < nr; i++) {
278                 struct extent_buffer *next;
279
280                 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
281                                        root->leafsize,
282                                        btrfs_node_ptr_generation(eb, i));
283                 if (!next)
284                         continue;
285                 btrfs_corrupt_extent_tree(trans, root, next);
286                 free_extent_buffer(next);
287         }
288 }
289
290 enum btrfs_inode_field {
291         BTRFS_INODE_FIELD_ISIZE,
292         BTRFS_INODE_FIELD_BAD,
293 };
294
295 enum btrfs_file_extent_field {
296         BTRFS_FILE_EXTENT_DISK_BYTENR,
297         BTRFS_FILE_EXTENT_BAD,
298 };
299
300 static enum btrfs_inode_field convert_inode_field(char *field)
301 {
302         if (!strncmp(field, "isize", FIELD_BUF_LEN))
303                 return BTRFS_INODE_FIELD_ISIZE;
304         return BTRFS_INODE_FIELD_BAD;
305 }
306
307 static enum btrfs_file_extent_field convert_file_extent_field(char *field)
308 {
309         if (!strncmp(field, "disk_bytenr", FIELD_BUF_LEN))
310                 return BTRFS_FILE_EXTENT_DISK_BYTENR;
311         return BTRFS_FILE_EXTENT_BAD;
312 }
313
314 static u64 generate_u64(u64 orig)
315 {
316         u64 ret;
317         do {
318                 ret = rand();
319         } while (ret == orig);
320         return ret;
321 }
322
323 static int corrupt_inode(struct btrfs_trans_handle *trans,
324                          struct btrfs_root *root, u64 inode, char *field)
325 {
326         struct btrfs_inode_item *ei;
327         struct btrfs_path *path;
328         struct btrfs_key key;
329         enum btrfs_inode_field corrupt_field = convert_inode_field(field);
330         u64 bogus;
331         u64 orig;
332         int ret;
333
334         if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
335                 fprintf(stderr, "Invalid field %s\n", field);
336                 return -EINVAL;
337         }
338
339         key.objectid = inode;
340         key.type = BTRFS_INODE_ITEM_KEY;
341         key.offset = (u64)-1;
342
343         path = btrfs_alloc_path();
344         if (!path)
345                 return -ENOMEM;
346
347         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
348         if (ret < 0)
349                 goto out;
350         if (ret) {
351                 if (!path->slots[0]) {
352                         fprintf(stderr, "Couldn't find inode %Lu\n", inode);
353                         ret = -ENOENT;
354                         goto out;
355                 }
356                 path->slots[0]--;
357                 ret = 0;
358         }
359
360         btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
361         if (key.objectid != inode) {
362                 fprintf(stderr, "Couldn't find inode %Lu\n", inode);
363                 ret = -ENOENT;
364                 goto out;
365         }
366
367         ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
368                             struct btrfs_inode_item);
369         switch (corrupt_field) {
370         case BTRFS_INODE_FIELD_ISIZE:
371                 orig = btrfs_inode_size(path->nodes[0], ei);
372                 bogus = generate_u64(orig);
373                 btrfs_set_inode_size(path->nodes[0], ei, bogus);
374                 break;
375         default:
376                 ret = -EINVAL;
377                 break;
378         }
379         btrfs_mark_buffer_dirty(path->nodes[0]);
380 out:
381         btrfs_free_path(path);
382         return ret;
383 }
384
385 static int corrupt_file_extent(struct btrfs_trans_handle *trans,
386                                struct btrfs_root *root, u64 inode, u64 extent,
387                                char *field)
388 {
389         struct btrfs_file_extent_item *fi;
390         struct btrfs_path *path;
391         struct btrfs_key key;
392         enum btrfs_file_extent_field corrupt_field;
393         u64 bogus;
394         u64 orig;
395         int ret = 0;
396
397         corrupt_field = convert_file_extent_field(field);
398         if (corrupt_field == BTRFS_FILE_EXTENT_BAD) {
399                 fprintf(stderr, "Invalid field %s\n", field);
400                 return -EINVAL;
401         }
402
403         key.objectid = inode;
404         key.type = BTRFS_EXTENT_DATA_KEY;
405         key.offset = extent;
406
407         path = btrfs_alloc_path();
408         if (!path)
409                 return -ENOMEM;
410
411         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
412         if (ret < 0)
413                 goto out;
414         if (ret) {
415                 fprintf(stderr, "Couldn't find extent %llu for inode %llu\n",
416                         extent, inode);
417                 ret = -ENOENT;
418                 goto out;
419         }
420
421         fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
422                             struct btrfs_file_extent_item);
423         switch (corrupt_field) {
424         case BTRFS_FILE_EXTENT_DISK_BYTENR:
425                 orig = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
426                 bogus = generate_u64(orig);
427                 btrfs_set_file_extent_disk_bytenr(path->nodes[0], fi, bogus);
428                 break;
429         default:
430                 ret = -EINVAL;
431                 break;
432         }
433         btrfs_mark_buffer_dirty(path->nodes[0]);
434 out:
435         btrfs_free_path(path);
436         return ret;
437 }
438
439 static struct option long_options[] = {
440         /* { "byte-count", 1, NULL, 'b' }, */
441         { "logical", 1, NULL, 'l' },
442         { "copy", 1, NULL, 'c' },
443         { "bytes", 1, NULL, 'b' },
444         { "extent-record", 0, NULL, 'e' },
445         { "extent-tree", 0, NULL, 'E' },
446         { "keys", 0, NULL, 'k' },
447         { "chunk-record", 0, NULL, 'u' },
448         { "chunk-tree", 0, NULL, 'U' },
449         { "inode", 1, NULL, 'i'},
450         { "file-extent", 1, NULL, 'x'},
451         { "field", 1, NULL, 'f'},
452         { 0, 0, 0, 0}
453 };
454
455 /* corrupt item using NO cow.
456  * Because chunk recover will recover based on whole partition scaning,
457  * If using COW, chunk recover will use the old item to recover,
458  * which is still OK but we want to check the ability to rebuild chunk
459  * not only restore the old ones */
460 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
461                        struct btrfs_root *root, struct btrfs_path *path,
462                        int del)
463 {
464         int ret = 0;
465         struct btrfs_key key;
466         struct extent_buffer *leaf;
467         unsigned long ptr;
468         int slot;
469         u32 item_size;
470
471         leaf = path->nodes[0];
472         slot = path->slots[0];
473         /* Not deleting the first item of a leaf to keep leaf structure */
474         if (slot == 0)
475                 del = 0;
476         /* Only accept valid eb */
477         BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
478         btrfs_item_key_to_cpu(leaf, &key, slot);
479         if (del) {
480                 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
481                         key.objectid, key.type, key.offset);
482                 btrfs_del_item(trans, root, path);
483         } else {
484                 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
485                         key.objectid, key.type, key.offset);
486                 ptr = btrfs_item_ptr_offset(leaf, slot);
487                 item_size = btrfs_item_size_nr(leaf, slot);
488                 memset_extent_buffer(leaf, 0, ptr, item_size);
489                 btrfs_mark_buffer_dirty(leaf);
490         }
491         return ret;
492 }
493 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
494                        struct btrfs_root *root)
495 {
496         int ret;
497         int del;
498         int slot;
499         struct btrfs_path *path;
500         struct btrfs_key key;
501         struct btrfs_key found_key;
502         struct extent_buffer *leaf;
503
504         path = btrfs_alloc_path();
505         key.objectid = (u64)-1;
506         key.offset = (u64)-1;
507         key.type = (u8)-1;
508
509         /* Here, cow and ins_len must equals 0 for the following reasons:
510          * 1) chunk recover is based on disk scanning, so COW should be
511          *    disabled in case the original chunk being scanned and
512          *    recovered using the old chunk.
513          * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
514          *    triggered.
515          */
516         ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
517         BUG_ON(ret == 0);
518         if (ret < 0) {
519                 fprintf(stderr, "Error searching tree\n");
520                 goto free_out;
521         }
522         /* corrupt/del dev_item first */
523         while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
524                 slot = path->slots[0];
525                 leaf = path->nodes[0];
526                 del = rand() % 3;
527                 /* Never delete the first item to keep the leaf structure */
528                 if (path->slots[0] == 0)
529                         del = 0;
530                 ret = corrupt_item_nocow(trans, root, path, del);
531                 if (ret)
532                         goto free_out;
533         }
534         btrfs_free_path(path);
535
536         /* Here, cow and ins_len must equals 0 for the following reasons:
537          * 1) chunk recover is based on disk scanning, so COW should be
538          *    disabled in case the original chunk being scanned and
539          *    recovered using the old chunk.
540          * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
541          *    triggered.
542          */
543         path = btrfs_alloc_path();
544         ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
545         BUG_ON(ret == 0);
546         if (ret < 0) {
547                 fprintf(stderr, "Error searching tree\n");
548                 goto free_out;
549         }
550         /* corrupt/del chunk then*/
551         while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
552                 slot = path->slots[0];
553                 leaf = path->nodes[0];
554                 del = rand() % 3;
555                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
556                 ret = corrupt_item_nocow(trans, root, path, del);
557                 if (ret)
558                         goto free_out;
559         }
560 free_out:
561         btrfs_free_path(path);
562         return ret;
563 }
564 int find_chunk_offset(struct btrfs_root *root,
565                       struct btrfs_path *path, u64 offset)
566 {
567         struct btrfs_key key;
568         int ret;
569
570         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
571         key.type = BTRFS_CHUNK_ITEM_KEY;
572         key.offset = offset;
573
574         /* Here, cow and ins_len must equals 0 for following reasons:
575          * 1) chunk recover is based on disk scanning, so COW should
576          *    be disabled in case the original chunk being scanned
577          *    and recovered using the old chunk.
578          * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
579          *    will be triggered.
580          */
581         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
582         if (ret > 0) {
583                 fprintf(stderr, "Can't find chunk with given offset %llu\n",
584                         offset);
585                 goto out;
586         }
587         if (ret < 0) {
588                 fprintf(stderr, "Error searching chunk");
589                 goto out;
590         }
591 out:
592         return ret;
593
594 }
595 int main(int ac, char **av)
596 {
597         struct cache_tree root_cache;
598         struct btrfs_root *root;
599         struct extent_buffer *eb;
600         char *dev;
601         /* chunk offset can be 0,so change to (u64)-1 */
602         u64 logical = (u64)-1;
603         int ret = 0;
604         int option_index = 0;
605         int copy = 0;
606         u64 bytes = 4096;
607         int extent_rec = 0;
608         int extent_tree = 0;
609         int corrupt_block_keys = 0;
610         int chunk_rec = 0;
611         int chunk_tree = 0;
612         u64 inode = 0;
613         u64 file_extent = (u64)-1;
614         char field[FIELD_BUF_LEN];
615
616         field[0] = '\0';
617         srand(128);
618
619         while(1) {
620                 int c;
621                 c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:", long_options,
622                                 &option_index);
623                 if (c < 0)
624                         break;
625                 switch(c) {
626                         case 'l':
627                                 logical = atoll(optarg);
628                                 break;
629                         case 'c':
630                                 copy = atoi(optarg);
631                                 if (copy <= 0) {
632                                         fprintf(stderr,
633                                                 "invalid copy number\n");
634                                         print_usage();
635                                 }
636                                 break;
637                         case 'b':
638                                 bytes = atoll(optarg);
639                                 if (bytes == 0) {
640                                         fprintf(stderr,
641                                                 "invalid byte count\n");
642                                         print_usage();
643                                 }
644                                 break;
645                         case 'e':
646                                 extent_rec = 1;
647                                 break;
648                         case 'E':
649                                 extent_tree = 1;
650                                 break;
651                         case 'k':
652                                 corrupt_block_keys = 1;
653                                 break;
654                         case 'u':
655                                 chunk_rec = 1;
656                                 break;
657                         case 'U':
658                                 chunk_tree = 1;
659                         case 'i':
660                                 inode = atoll(optarg);
661                                 if (inode == 0) {
662                                         fprintf(stderr,
663                                                 "invalid inode number\n");
664                                         print_usage();
665                                 }
666                                 break;
667                         case 'f':
668                                 strncpy(field, optarg, FIELD_BUF_LEN);
669                                 break;
670                         case 'x':
671                                 errno = 0;
672                                 file_extent = atoll(optarg);
673                                 if (errno) {
674                                         fprintf(stderr, "error converting "
675                                                 "%d\n", errno);
676                                         print_usage();
677                                 }
678                                 break;
679                         default:
680                                 print_usage();
681                 }
682         }
683         ac = ac - optind;
684         if (ac == 0)
685                 print_usage();
686         dev = av[optind];
687
688         radix_tree_init();
689         cache_tree_init(&root_cache);
690
691         root = open_ctree(dev, 0, 1);
692         if (!root) {
693                 fprintf(stderr, "Open ctree failed\n");
694                 exit(1);
695         }
696         if (extent_rec) {
697                 struct btrfs_trans_handle *trans;
698
699                 if (logical == (u64)-1)
700                         print_usage();
701                 trans = btrfs_start_transaction(root, 1);
702                 ret = corrupt_extent (trans, root, logical, 0);
703                 btrfs_commit_transaction(trans, root);
704                 goto out_close;
705         }
706         if (extent_tree) {
707                 struct btrfs_trans_handle *trans;
708                 trans = btrfs_start_transaction(root, 1);
709                 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
710                                           root->fs_info->extent_root->node);
711                 btrfs_commit_transaction(trans, root);
712                 goto out_close;
713         }
714         if (chunk_rec) {
715                 struct btrfs_trans_handle *trans;
716                 struct btrfs_path *path;
717                 int del;
718
719                 if (logical == (u64)-1)
720                         print_usage();
721                 del = rand() % 3;
722                 path = btrfs_alloc_path();
723
724                 if (find_chunk_offset(root->fs_info->chunk_root, path,
725                                       logical) != 0) {
726                         btrfs_free_path(path);
727                         goto out_close;
728                 }
729                 trans = btrfs_start_transaction(root, 1);
730                 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
731                                          path, del);
732                 if (ret < 0)
733                         fprintf(stderr, "Failed to corrupt chunk record\n");
734                 btrfs_commit_transaction(trans, root);
735                 goto out_close;
736         }
737         if (chunk_tree) {
738                 struct btrfs_trans_handle *trans;
739                 trans = btrfs_start_transaction(root, 1);
740                 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
741                 if (ret < 0)
742                         fprintf(stderr, "Failed to corrupt chunk tree\n");
743                 btrfs_commit_transaction(trans, root);
744                 goto out_close;
745         }
746         if (inode) {
747                 struct btrfs_trans_handle *trans;
748
749                 if (!strlen(field))
750                         print_usage();
751
752                 trans = btrfs_start_transaction(root, 1);
753                 if (file_extent == (u64)-1) {
754                         printf("corrupting inode\n");
755                         ret = corrupt_inode(trans, root, inode, field);
756                 } else {
757                         printf("corrupting file extent\n");
758                         ret = corrupt_file_extent(trans, root, inode,
759                                                   file_extent, field);
760                 }
761                 btrfs_commit_transaction(trans, root);
762                 goto out_close;
763         }
764
765         /*
766          * If we made it here and we have extent set then we didn't specify
767          * inode and we're screwed.
768          */
769         if (file_extent != (u64)-1)
770                 print_usage();
771
772         if (logical == (u64)-1)
773                 print_usage();
774
775         if (bytes == 0)
776                 bytes = root->sectorsize;
777
778         bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
779         bytes *= root->sectorsize;
780
781         while (bytes > 0) {
782                 if (corrupt_block_keys) {
783                         corrupt_keys_in_block(root, logical);
784                 } else {
785                         eb = debug_corrupt_block(root, logical,
786                                                  root->sectorsize, copy);
787                         free_extent_buffer(eb);
788                 }
789                 logical += root->sectorsize;
790                 bytes -= root->sectorsize;
791         }
792         return ret;
793 out_close:
794         close_ctree(root);
795         return ret;
796 }