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
26 #include "kerncompat.h"
30 #include "print-tree.h"
31 #include "transaction.h"
36 struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
37 u32 blocksize, int copy)
40 struct extent_buffer *eb;
42 struct btrfs_multi_bio *multi = NULL;
43 struct btrfs_device *device;
47 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
53 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
54 eb->start, &length, &multi,
57 device = multi->stripes[0].dev;
60 eb->dev_bytenr = multi->stripes[0].physical;
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);
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,
72 memset(eb->data, 0, eb->len);
73 write_extent_to_disk(eb);
77 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
83 if (mirror_num > num_copies)
89 static void print_usage(void)
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");
103 static void corrupt_keys(struct btrfs_trans_handle *trans,
104 struct btrfs_root *root,
105 struct extent_buffer *eb)
110 struct btrfs_disk_key bad_key;;
112 nr = btrfs_header_nritems(eb);
117 bad_slot = rand() % nr;
119 if (bad_slot == slot)
123 "corrupting keys in block %llu slot %d swapping with %d\n",
124 (unsigned long long)eb->start, slot, bad_slot);
126 if (btrfs_header_level(eb) == 0) {
127 btrfs_item_key(eb, &bad_key, bad_slot);
128 btrfs_set_item_key(eb, &bad_key, slot);
130 btrfs_node_key(eb, &bad_key, bad_slot);
131 btrfs_set_node_key(eb, &bad_key, slot);
133 btrfs_mark_buffer_dirty(eb);
135 csum_tree_block(root, eb, 0);
136 write_extent_to_disk(eb);
141 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
143 struct extent_buffer *eb;
145 eb = read_tree_block(root, bytenr, root->leafsize, 0);
149 corrupt_keys(NULL, root, eb);
150 free_extent_buffer(eb);
154 static int corrupt_extent(struct btrfs_trans_handle *trans,
155 struct btrfs_root *root, u64 bytenr, int copy)
157 struct btrfs_key key;
158 struct extent_buffer *leaf;
161 struct btrfs_path *path;
164 int should_del = rand() % 3;
166 path = btrfs_alloc_path();
170 key.objectid = bytenr;
172 key.offset = (u64)-1;
175 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
181 if (path->slots[0] == 0)
186 leaf = path->nodes[0];
187 slot = path->slots[0];
188 btrfs_item_key_to_cpu(leaf, &key, slot);
189 if (key.objectid != bytenr)
192 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
193 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
194 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
195 key.type != BTRFS_EXTENT_REF_V0_KEY &&
196 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
197 key.type != BTRFS_SHARED_DATA_REF_KEY)
202 "deleting extent record: key %llu %u %llu\n",
203 key.objectid, key.type, key.offset);
205 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
206 /* make sure this extent doesn't get
207 * reused for other purposes */
208 btrfs_pin_extent(root->fs_info,
209 key.objectid, key.offset);
212 btrfs_del_item(trans, root, path);
215 "corrupting extent record: key %llu %u %llu\n",
216 key.objectid, key.type, key.offset);
217 ptr = btrfs_item_ptr_offset(leaf, slot);
218 item_size = btrfs_item_size_nr(leaf, slot);
219 memset_extent_buffer(leaf, 0, ptr, item_size);
220 btrfs_mark_buffer_dirty(leaf);
223 btrfs_release_path(path);
231 btrfs_free_path(path);
235 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
236 struct btrfs_root *root,
237 struct extent_buffer *eb)
239 u32 nr = btrfs_header_nritems(eb);
240 u32 victim = rand() % nr;
242 struct btrfs_key key;
244 btrfs_item_key_to_cpu(eb, &key, victim);
245 objectid = key.objectid;
246 corrupt_extent(trans, root, objectid, 1);
249 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
250 struct btrfs_root *root,
251 struct extent_buffer *eb)
259 nr = btrfs_header_nritems(eb);
260 if (btrfs_is_leaf(eb)) {
261 btrfs_corrupt_extent_leaf(trans, root, eb);
265 if (btrfs_header_level(eb) == 1 && eb != root->node) {
270 for (i = 0; i < nr; i++) {
271 struct extent_buffer *next;
273 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
275 btrfs_node_ptr_generation(eb, i));
278 btrfs_corrupt_extent_tree(trans, root, next);
279 free_extent_buffer(next);
283 static struct option long_options[] = {
284 /* { "byte-count", 1, NULL, 'b' }, */
285 { "logical", 1, NULL, 'l' },
286 { "copy", 1, NULL, 'c' },
287 { "bytes", 1, NULL, 'b' },
288 { "extent-record", 0, NULL, 'e' },
289 { "extent-tree", 0, NULL, 'E' },
290 { "keys", 0, NULL, 'k' },
291 { "chunk-record", 0, NULL, 'u' },
292 { "chunk-tree", 0, NULL, 'U' },
296 /* corrupt item using NO cow.
297 * Because chunk recover will recover based on whole partition scaning,
298 * If using COW, chunk recover will use the old item to recover,
299 * which is still OK but we want to check the ability to rebuild chunk
300 * not only restore the old ones */
301 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
302 struct btrfs_root *root, struct btrfs_path *path,
306 struct btrfs_key key;
307 struct extent_buffer *leaf;
312 leaf = path->nodes[0];
313 slot = path->slots[0];
314 /* Not deleting the first item of a leaf to keep leaf structure */
317 /* Only accept valid eb */
318 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
319 btrfs_item_key_to_cpu(leaf, &key, slot);
321 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
322 key.objectid, key.type, key.offset);
323 btrfs_del_item(trans, root, path);
325 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
326 key.objectid, key.type, key.offset);
327 ptr = btrfs_item_ptr_offset(leaf, slot);
328 item_size = btrfs_item_size_nr(leaf, slot);
329 memset_extent_buffer(leaf, 0, ptr, item_size);
330 btrfs_mark_buffer_dirty(leaf);
334 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
335 struct btrfs_root *root)
340 struct btrfs_path *path;
341 struct btrfs_key key;
342 struct btrfs_key found_key;
343 struct extent_buffer *leaf;
345 path = btrfs_alloc_path();
346 key.objectid = (u64)-1;
347 key.offset = (u64)-1;
350 /* Here, cow and ins_len must equals 0 for the following reasons:
351 * 1) chunk recover is based on disk scanning, so COW should be
352 * disabled in case the original chunk being scanned and
353 * recovered using the old chunk.
354 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
357 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
360 fprintf(stderr, "Error searching tree\n");
363 /* corrupt/del dev_item first */
364 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
365 slot = path->slots[0];
366 leaf = path->nodes[0];
368 /* Never delete the first item to keep the leaf structure */
369 if (path->slots[0] == 0)
371 ret = corrupt_item_nocow(trans, root, path, del);
375 btrfs_free_path(path);
377 /* Here, cow and ins_len must equals 0 for the following reasons:
378 * 1) chunk recover is based on disk scanning, so COW should be
379 * disabled in case the original chunk being scanned and
380 * recovered using the old chunk.
381 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
384 path = btrfs_alloc_path();
385 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
388 fprintf(stderr, "Error searching tree\n");
391 /* corrupt/del chunk then*/
392 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
393 slot = path->slots[0];
394 leaf = path->nodes[0];
396 btrfs_item_key_to_cpu(leaf, &found_key, slot);
397 ret = corrupt_item_nocow(trans, root, path, del);
402 btrfs_free_path(path);
405 int find_chunk_offset(struct btrfs_root *root,
406 struct btrfs_path *path, u64 offset)
408 struct btrfs_key key;
411 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
412 key.type = BTRFS_CHUNK_ITEM_KEY;
415 /* Here, cow and ins_len must equals 0 for following reasons:
416 * 1) chunk recover is based on disk scanning, so COW should
417 * be disabled in case the original chunk being scanned
418 * and recovered using the old chunk.
419 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
422 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
424 fprintf(stderr, "Can't find chunk with given offset %llu\n",
429 fprintf(stderr, "Error searching chunk");
436 int main(int ac, char **av)
438 struct cache_tree root_cache;
439 struct btrfs_root *root;
440 struct extent_buffer *eb;
442 /* chunk offset can be 0,so change to (u64)-1 */
443 u64 logical = (u64)-1;
445 int option_index = 0;
450 int corrupt_block_keys = 0;
458 c = getopt_long(ac, av, "l:c:b:eEkuU", long_options,
464 logical = atoll(optarg);
470 "invalid copy number\n");
475 bytes = atoll(optarg);
478 "invalid byte count\n");
489 corrupt_block_keys = 1;
504 if (logical == (u64)-1 && !(extent_tree || chunk_tree))
512 cache_tree_init(&root_cache);
514 root = open_ctree(dev, 0, 1);
516 fprintf(stderr, "Open ctree failed\n");
520 struct btrfs_trans_handle *trans;
521 trans = btrfs_start_transaction(root, 1);
522 ret = corrupt_extent (trans, root, logical, 0);
523 btrfs_commit_transaction(trans, root);
527 struct btrfs_trans_handle *trans;
528 trans = btrfs_start_transaction(root, 1);
529 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
530 root->fs_info->extent_root->node);
531 btrfs_commit_transaction(trans, root);
535 struct btrfs_trans_handle *trans;
536 struct btrfs_path *path;
540 path = btrfs_alloc_path();
542 if (find_chunk_offset(root->fs_info->chunk_root, path,
544 btrfs_free_path(path);
547 trans = btrfs_start_transaction(root, 1);
548 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
551 fprintf(stderr, "Failed to corrupt chunk record\n");
552 btrfs_commit_transaction(trans, root);
556 struct btrfs_trans_handle *trans;
557 trans = btrfs_start_transaction(root, 1);
558 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
560 fprintf(stderr, "Failed to corrupt chunk tree\n");
561 btrfs_commit_transaction(trans, root);
566 bytes = root->sectorsize;
568 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
569 bytes *= root->sectorsize;
572 if (corrupt_block_keys) {
573 corrupt_keys_in_block(root, logical);
575 eb = debug_corrupt_block(root, logical,
576 root->sectorsize, copy);
577 free_extent_buffer(eb);
579 logical += root->sectorsize;
580 bytes -= root->sectorsize;