Merge branch 'cov-fixes-v1-integration-20130201' of http://git.zabbo.net/cgit/btrfs...
[platform/upstream/btrfs-progs.git] / btrfs-corrupt-block.c
1 /*
2  * Copyright (C) 2009 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include "kerncompat.h"
27 #include "ctree.h"
28 #include "volumes.h"
29 #include "disk-io.h"
30 #include "print-tree.h"
31 #include "transaction.h"
32 #include "list.h"
33 #include "version.h"
34
35 struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
36                                      u32 blocksize, int copy)
37 {
38         int ret;
39         struct extent_buffer *eb;
40         u64 length;
41         struct btrfs_multi_bio *multi = NULL;
42         struct btrfs_device *device;
43         int num_copies;
44         int mirror_num = 1;
45
46         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
47         if (!eb)
48                 return NULL;
49
50         length = blocksize;
51         while (1) {
52                 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
53                                       eb->start, &length, &multi,
54                                       mirror_num, NULL);
55                 BUG_ON(ret);
56                 device = multi->stripes[0].dev;
57                 eb->fd = device->fd;
58                 device->total_ios++;
59                 eb->dev_bytenr = multi->stripes[0].physical;
60
61                 fprintf(stdout, "mirror %d logical %Lu physical %Lu "
62                         "device %s\n", mirror_num, (unsigned long long)bytenr,
63                         (unsigned long long)eb->dev_bytenr, device->name);
64                 kfree(multi);
65
66                 if (!copy || mirror_num == copy) {
67                         ret = read_extent_from_disk(eb, 0, eb->len);
68                         printf("corrupting %llu copy %d\n", eb->start,
69                                mirror_num);
70                         memset(eb->data, 0, eb->len);
71                         write_extent_to_disk(eb);
72                         fsync(eb->fd);
73                 }
74
75                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
76                                               eb->start, eb->len);
77                 if (num_copies == 1)
78                         break;
79
80                 mirror_num++;
81                 if (mirror_num > num_copies)
82                         break;
83         }
84         return eb;
85 }
86
87 static void print_usage(void)
88 {
89         fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n");
90         fprintf(stderr, "\t-l Logical extent to be corrupted\n");
91         fprintf(stderr, "\t-c Copy of the extent to be corrupted"
92                 " (usually 1 or 2, default: 0)\n");
93         fprintf(stderr, "\t-b Number of bytes to be corrupted\n");
94         fprintf(stderr, "\t-e Extent to be corrupted\n");
95         fprintf(stderr, "\t-E The whole extent free to be corrupted\n");
96         exit(1);
97 }
98
99 static void corrupt_keys(struct btrfs_trans_handle *trans,
100                          struct btrfs_root *root,
101                          struct extent_buffer *eb)
102 {
103         int slot;
104         int bad_slot;
105         int nr;
106         struct btrfs_disk_key bad_key;;
107
108         nr = btrfs_header_nritems(eb);
109         if (nr == 0)
110                 return;
111
112         slot = rand() % nr;
113         bad_slot = rand() % nr;
114
115         if (bad_slot == slot)
116                 return;
117
118         fprintf(stderr, "corrupting keys in block %llu slot %d swapping with %d\n",
119                 (unsigned long long)eb->start, slot, bad_slot);
120
121         if (btrfs_header_level(eb) == 0) {
122                 btrfs_item_key(eb, &bad_key, bad_slot);
123                 btrfs_set_item_key(eb, &bad_key, slot);
124         } else {
125                 btrfs_node_key(eb, &bad_key, bad_slot);
126                 btrfs_set_node_key(eb, &bad_key, slot);
127         }
128         btrfs_mark_buffer_dirty(eb);
129         if (!trans) {
130                 csum_tree_block(root, eb, 0);
131                 write_extent_to_disk(eb);
132         }
133 }
134
135
136 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
137 {
138         struct extent_buffer *eb;
139
140         eb = read_tree_block(root, bytenr, root->leafsize, 0);
141         if (!eb)
142                 return -EIO;;
143
144         corrupt_keys(NULL, root, eb);
145         free_extent_buffer(eb);
146         return 0;
147 }
148
149 static int corrupt_extent(struct btrfs_trans_handle *trans,
150                           struct btrfs_root *root, u64 bytenr, int copy)
151 {
152         struct btrfs_key key;
153         struct extent_buffer *leaf;
154         u32 item_size;
155         unsigned long ptr;
156         struct btrfs_path *path;
157         int ret;
158         int slot;
159         int should_del = rand() % 3;
160
161         path = btrfs_alloc_path();
162
163         key.objectid = bytenr;
164         key.type = (u8)-1;
165         key.offset = (u64)-1;
166
167         while(1) {
168                 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
169                                         &key, path, -1, 1);
170                 if (ret < 0)
171                         break;
172
173                 if (ret > 0) {
174                         if (path->slots[0] == 0)
175                                 break;
176                         path->slots[0]--;
177                         ret = 0;
178                 }
179                 leaf = path->nodes[0];
180                 slot = path->slots[0];
181                 btrfs_item_key_to_cpu(leaf, &key, slot);
182                 if (key.objectid != bytenr)
183                         break;
184
185                 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
186                     key.type != BTRFS_TREE_BLOCK_REF_KEY &&
187                     key.type != BTRFS_EXTENT_DATA_REF_KEY &&
188                     key.type != BTRFS_EXTENT_REF_V0_KEY &&
189                     key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
190                     key.type != BTRFS_SHARED_DATA_REF_KEY)
191                         goto next;
192
193                 if (should_del) {
194                         fprintf(stderr, "deleting extent record: key %Lu %u %Lu\n",
195                                 key.objectid, key.type, key.offset);
196
197                         if (key.type == BTRFS_EXTENT_ITEM_KEY) {
198                                 /* make sure this extent doesn't get
199                                  * reused for other purposes */
200                                 btrfs_pin_extent(root->fs_info,
201                                                  key.objectid, key.offset);
202                         }
203
204                         btrfs_del_item(trans, root, path);
205                 } else {
206                         fprintf(stderr, "corrupting extent record: key %Lu %u %Lu\n",
207                                 key.objectid, key.type, key.offset);
208                         ptr = btrfs_item_ptr_offset(leaf, slot);
209                         item_size = btrfs_item_size_nr(leaf, slot);
210                         memset_extent_buffer(leaf, 0, ptr, item_size);
211                         btrfs_mark_buffer_dirty(leaf);
212                 }
213 next:
214                 btrfs_release_path(NULL, path);
215
216                 if (key.offset > 0)
217                         key.offset--;
218                 if (key.offset == 0)
219                         break;
220         }
221
222         btrfs_free_path(path);
223         return 0;
224 }
225
226 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
227                                       struct btrfs_root *root, struct extent_buffer *eb)
228 {
229         u32 nr = btrfs_header_nritems(eb);
230         u32 victim = rand() % nr;
231         u64 objectid;
232         struct btrfs_key key;
233
234         btrfs_item_key_to_cpu(eb, &key, victim);
235         objectid = key.objectid;
236         corrupt_extent(trans, root, objectid, 1);
237 }
238
239 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
240                                       struct btrfs_root *root, struct extent_buffer *eb)
241 {
242         int i;
243         u32 nr;
244
245         if (!eb)
246                 return;
247
248         nr = btrfs_header_nritems(eb);
249         if (btrfs_is_leaf(eb)) {
250                 btrfs_corrupt_extent_leaf(trans, root, eb);
251                 return;
252         }
253
254         if (btrfs_header_level(eb) == 1 && eb != root->node) {
255                 if (rand() % 5)
256                         return;
257         }
258
259         for (i = 0; i < nr; i++) {
260                 struct extent_buffer *next;
261
262                 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
263                                        root->leafsize, btrfs_node_ptr_generation(eb, i));
264                 if (!next)
265                         continue;
266                 btrfs_corrupt_extent_tree(trans, root, next);
267                 free_extent_buffer(next);
268         }
269 }
270
271 static struct option long_options[] = {
272         /* { "byte-count", 1, NULL, 'b' }, */
273         { "logical", 1, NULL, 'l' },
274         { "copy", 1, NULL, 'c' },
275         { "bytes", 1, NULL, 'b' },
276         { "extent-record", 0, NULL, 'e' },
277         { "extent-tree", 0, NULL, 'E' },
278         { "keys", 0, NULL, 'k' },
279         { 0, 0, 0, 0}
280 };
281
282
283 int main(int ac, char **av)
284 {
285         struct cache_tree root_cache;
286         struct btrfs_root *root;
287         struct extent_buffer *eb;
288         char *dev;
289         u64 logical = 0;
290         int ret = 0;
291         int option_index = 0;
292         int copy = 0;
293         u64 bytes = 4096;
294         int extent_rec = 0;
295         int extent_tree = 0;
296         int corrupt_block_keys = 0;
297
298         srand(128);
299
300         while(1) {
301                 int c;
302                 c = getopt_long(ac, av, "l:c:b:eEk", long_options,
303                                 &option_index);
304                 if (c < 0)
305                         break;
306                 switch(c) {
307                         case 'l':
308                                 logical = atoll(optarg);
309                                 if (logical == 0) {
310                                         fprintf(stderr,
311                                                 "invalid extent number\n");
312                                         print_usage();
313                                 }
314                                 break;
315                         case 'c':
316                                 copy = atoi(optarg);
317                                 if (copy == 0) {
318                                         fprintf(stderr,
319                                                 "invalid copy number\n");
320                                         print_usage();
321                                 }
322                                 break;
323                         case 'b':
324                                 bytes = atoll(optarg);
325                                 if (bytes == 0) {
326                                         fprintf(stderr,
327                                                 "invalid byte count\n");
328                                         print_usage();
329                                 }
330                                 break;
331                         case 'e':
332                                 extent_rec = 1;
333                                 break;
334                         case 'E':
335                                 extent_tree = 1;
336                                 break;
337                         case 'k':
338                                 corrupt_block_keys = 1;
339                                 break;
340                         default:
341                                 print_usage();
342                 }
343         }
344         ac = ac - optind;
345         if (ac == 0)
346                 print_usage();
347         if (logical == 0 && !extent_tree)
348                 print_usage();
349         if (copy < 0)
350                 print_usage();
351
352         dev = av[optind];
353
354         radix_tree_init();
355         cache_tree_init(&root_cache);
356
357         root = open_ctree(dev, 0, 1);
358         if (!root) {
359                 fprintf(stderr, "Open ctree failed\n");
360                 exit(1);
361         }
362         if (extent_rec) {
363                 struct btrfs_trans_handle *trans;
364                 trans = btrfs_start_transaction(root, 1);
365                 ret = corrupt_extent (trans, root, logical, 0);
366                 btrfs_commit_transaction(trans, root);
367                 goto out_close;
368         }
369         if (extent_tree) {
370                 struct btrfs_trans_handle *trans;
371                 trans = btrfs_start_transaction(root, 1);
372                 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
373                                           root->fs_info->extent_root->node);
374                 btrfs_commit_transaction(trans, root);
375                 goto out_close;
376         }
377
378         if (bytes == 0)
379                 bytes = root->sectorsize;
380
381         bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
382         bytes *= root->sectorsize;
383
384         while (bytes > 0) {
385                 if (corrupt_block_keys) {
386                         corrupt_keys_in_block(root, logical);
387                 } else {
388                         eb = debug_corrupt_block(root, logical,
389                                                  root->sectorsize, copy);
390                         free_extent_buffer(eb);
391                 }
392                 logical += root->sectorsize;
393                 bytes -= root->sectorsize;
394         }
395         return ret;
396 out_close:
397         close_ctree(root);
398         return ret;
399 }