btrfs-progs: mkfs: add option to skip trim
[btrfs-progs-unstable/devel.git] / btrfs-corrupt-block.c
blobb57e75736fe6f537a2ab59cf53abff7c8b53bd0b
1 /*
2 * Copyright (C) 2009 Oracle. All rights reserved.
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.
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.
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.
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"
35 struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
36 u32 blocksize, int copy)
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;
46 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
47 if (!eb)
48 return NULL;
50 length = blocksize;
51 while (1) {
52 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
53 eb->start, &length, &multi, mirror_num);
54 BUG_ON(ret);
55 device = multi->stripes[0].dev;
56 eb->fd = device->fd;
57 device->total_ios++;
58 eb->dev_bytenr = multi->stripes[0].physical;
60 fprintf(stdout, "mirror %d logical %Lu physical %Lu "
61 "device %s\n", mirror_num, (unsigned long long)bytenr,
62 (unsigned long long)eb->dev_bytenr, device->name);
63 kfree(multi);
65 if (!copy || mirror_num == copy) {
66 ret = read_extent_from_disk(eb);
67 printf("corrupting %llu copy %d\n", eb->start,
68 mirror_num);
69 memset(eb->data, 0, eb->len);
70 write_extent_to_disk(eb);
71 fsync(eb->fd);
74 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
75 eb->start, eb->len);
76 if (num_copies == 1)
77 break;
79 mirror_num++;
80 if (mirror_num > num_copies)
81 break;
83 return eb;
86 static void print_usage(void)
88 fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n");
89 fprintf(stderr, "\t-l Logical extent to be corrupted\n");
90 fprintf(stderr, "\t-c Copy of the extent to be corrupted"
91 " (usually 1 or 2, default: 0)\n");
92 fprintf(stderr, "\t-b Number of bytes to be corrupted\n");
93 fprintf(stderr, "\t-e Extent to be corrupted\n");
94 fprintf(stderr, "\t-E The whole extent free to be corrupted\n");
95 exit(1);
98 static void corrupt_keys(struct btrfs_trans_handle *trans,
99 struct btrfs_root *root,
100 struct extent_buffer *eb)
102 int slot;
103 int bad_slot;
104 int nr;
105 struct btrfs_disk_key bad_key;;
107 nr = btrfs_header_nritems(eb);
108 if (nr == 0)
109 return;
111 slot = rand() % nr;
112 bad_slot = rand() % nr;
114 if (bad_slot == slot)
115 return;
117 fprintf(stderr, "corrupting keys in block %llu slot %d swapping with %d\n",
118 (unsigned long long)eb->start, slot, bad_slot);
120 if (btrfs_header_level(eb) == 0) {
121 btrfs_item_key(eb, &bad_key, bad_slot);
122 btrfs_set_item_key(eb, &bad_key, slot);
123 } else {
124 btrfs_node_key(eb, &bad_key, bad_slot);
125 btrfs_set_node_key(eb, &bad_key, slot);
127 btrfs_mark_buffer_dirty(eb);
128 if (!trans) {
129 csum_tree_block(root, eb, 0);
130 write_extent_to_disk(eb);
135 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
137 struct extent_buffer *eb;
139 eb = read_tree_block(root, bytenr, root->leafsize, 0);
140 if (!eb)
141 return -EIO;;
143 corrupt_keys(NULL, root, eb);
144 free_extent_buffer(eb);
145 return 0;
148 static int corrupt_extent(struct btrfs_trans_handle *trans,
149 struct btrfs_root *root, u64 bytenr, int copy)
151 struct btrfs_key key;
152 struct extent_buffer *leaf;
153 u32 item_size;
154 unsigned long ptr;
155 struct btrfs_path *path;
156 int ret;
157 int slot;
158 int should_del = rand() % 3;
160 path = btrfs_alloc_path();
162 key.objectid = bytenr;
163 key.type = (u8)-1;
164 key.offset = (u64)-1;
166 while(1) {
167 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
168 &key, path, -1, 1);
169 if (ret < 0)
170 break;
172 if (ret > 0) {
173 if (path->slots[0] == 0)
174 break;
175 path->slots[0]--;
176 ret = 0;
178 leaf = path->nodes[0];
179 slot = path->slots[0];
180 btrfs_item_key_to_cpu(leaf, &key, slot);
181 if (key.objectid != bytenr)
182 break;
184 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
185 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
186 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
187 key.type != BTRFS_EXTENT_REF_V0_KEY &&
188 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
189 key.type != BTRFS_SHARED_DATA_REF_KEY)
190 goto next;
192 if (should_del) {
193 fprintf(stderr, "deleting extent record: key %Lu %u %Lu\n",
194 key.objectid, key.type, key.offset);
196 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
197 /* make sure this extent doesn't get
198 * reused for other purposes */
199 btrfs_pin_extent(root->fs_info,
200 key.objectid, key.offset);
203 btrfs_del_item(trans, root, path);
204 } else {
205 fprintf(stderr, "corrupting extent record: key %Lu %u %Lu\n",
206 key.objectid, key.type, key.offset);
207 ptr = btrfs_item_ptr_offset(leaf, slot);
208 item_size = btrfs_item_size_nr(leaf, slot);
209 memset_extent_buffer(leaf, 0, ptr, item_size);
210 btrfs_mark_buffer_dirty(leaf);
212 next:
213 btrfs_release_path(NULL, path);
215 if (key.offset > 0)
216 key.offset--;
217 if (key.offset == 0)
218 break;
221 btrfs_free_path(path);
222 return 0;
225 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
226 struct btrfs_root *root, struct extent_buffer *eb)
228 u32 nr = btrfs_header_nritems(eb);
229 u32 victim = rand() % nr;
230 u64 objectid;
231 struct btrfs_key key;
233 btrfs_item_key_to_cpu(eb, &key, victim);
234 objectid = key.objectid;
235 corrupt_extent(trans, root, objectid, 1);
238 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
239 struct btrfs_root *root, struct extent_buffer *eb)
241 int i;
242 u32 nr;
244 if (!eb)
245 return;
247 nr = btrfs_header_nritems(eb);
248 if (btrfs_is_leaf(eb)) {
249 btrfs_corrupt_extent_leaf(trans, root, eb);
250 return;
253 if (btrfs_header_level(eb) == 1 && eb != root->node) {
254 if (rand() % 5)
255 return;
258 for (i = 0; i < nr; i++) {
259 struct extent_buffer *next;
261 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
262 root->leafsize, btrfs_node_ptr_generation(eb, i));
263 if (!next)
264 continue;
265 btrfs_corrupt_extent_tree(trans, root, next);
266 free_extent_buffer(next);
270 static struct option long_options[] = {
271 /* { "byte-count", 1, NULL, 'b' }, */
272 { "logical", 1, NULL, 'l' },
273 { "copy", 1, NULL, 'c' },
274 { "bytes", 1, NULL, 'b' },
275 { "extent-record", 0, NULL, 'e' },
276 { "extent-tree", 0, NULL, 'E' },
277 { "keys", 0, NULL, 'k' },
278 { 0, 0, 0, 0}
282 int main(int ac, char **av)
284 struct cache_tree root_cache;
285 struct btrfs_root *root;
286 struct extent_buffer *eb;
287 char *dev;
288 u64 logical = 0;
289 int ret = 0;
290 int option_index = 0;
291 int copy = 0;
292 u64 bytes = 4096;
293 int extent_rec = 0;
294 int extent_tree = 0;
295 int corrupt_block_keys = 0;
297 srand(128);
299 while(1) {
300 int c;
301 c = getopt_long(ac, av, "l:c:b:eEk", long_options,
302 &option_index);
303 if (c < 0)
304 break;
305 switch(c) {
306 case 'l':
307 logical = atoll(optarg);
308 if (logical == 0) {
309 fprintf(stderr,
310 "invalid extent number\n");
311 print_usage();
313 break;
314 case 'c':
315 copy = atoi(optarg);
316 if (copy == 0) {
317 fprintf(stderr,
318 "invalid copy number\n");
319 print_usage();
321 break;
322 case 'b':
323 bytes = atoll(optarg);
324 if (bytes == 0) {
325 fprintf(stderr,
326 "invalid byte count\n");
327 print_usage();
329 break;
330 case 'e':
331 extent_rec = 1;
332 break;
333 case 'E':
334 extent_tree = 1;
335 break;
336 case 'k':
337 corrupt_block_keys = 1;
338 break;
339 default:
340 print_usage();
343 ac = ac - optind;
344 if (ac == 0)
345 print_usage();
346 if (logical == 0 && !extent_tree)
347 print_usage();
348 if (copy < 0)
349 print_usage();
351 dev = av[optind];
353 radix_tree_init();
354 cache_tree_init(&root_cache);
356 root = open_ctree(dev, 0, 1);
357 if (!root) {
358 fprintf(stderr, "Open ctree failed\n");
359 exit(1);
361 if (extent_rec) {
362 struct btrfs_trans_handle *trans;
363 trans = btrfs_start_transaction(root, 1);
364 ret = corrupt_extent (trans, root, logical, 0);
365 btrfs_commit_transaction(trans, root);
366 goto out_close;
368 if (extent_tree) {
369 struct btrfs_trans_handle *trans;
370 trans = btrfs_start_transaction(root, 1);
371 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
372 root->fs_info->extent_root->node);
373 btrfs_commit_transaction(trans, root);
374 goto out_close;
377 if (bytes == 0)
378 bytes = root->sectorsize;
380 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
381 bytes *= root->sectorsize;
383 while (bytes > 0) {
384 if (corrupt_block_keys) {
385 corrupt_keys_in_block(root, logical);
386 } else {
387 eb = debug_corrupt_block(root, logical,
388 root->sectorsize, copy);
389 free_extent_buffer(eb);
391 logical += root->sectorsize;
392 bytes -= root->sectorsize;
394 return ret;
395 out_close:
396 close_ctree(root);
397 return ret;