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