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