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"
35 struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
36 u32 blocksize, int copy)
39 struct extent_buffer *eb;
41 struct btrfs_multi_bio *multi = NULL;
42 struct btrfs_device *device;
46 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
52 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
53 eb->start, &length, &multi,
56 device = multi->stripes[0].dev;
59 eb->dev_bytenr = multi->stripes[0].physical;
62 "mirror %d logical %llu physical %llu device %s\n",
63 mirror_num, (unsigned long long)bytenr,
64 (unsigned long long)eb->dev_bytenr, device->name);
67 if (!copy || mirror_num == copy) {
68 ret = read_extent_from_disk(eb, 0, eb->len);
69 printf("corrupting %llu copy %d\n", eb->start,
71 memset(eb->data, 0, eb->len);
72 write_extent_to_disk(eb);
76 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
82 if (mirror_num > num_copies)
88 static void print_usage(void)
90 fprintf(stderr, "usage: btrfs-corrupt-block [options] device\n");
91 fprintf(stderr, "\t-l Logical extent to be corrupted\n");
92 fprintf(stderr, "\t-c Copy of the extent to be corrupted"
93 " (usually 1 or 2, default: 0)\n");
94 fprintf(stderr, "\t-b Number of bytes to be corrupted\n");
95 fprintf(stderr, "\t-e Extent to be corrupted\n");
96 fprintf(stderr, "\t-E The whole extent tree to be corrupted\n");
97 fprintf(stderr, "\t-u Given chunk item to be corrupted\n");
98 fprintf(stderr, "\t-U The whole chunk tree to be corrupted\n");
102 static void corrupt_keys(struct btrfs_trans_handle *trans,
103 struct btrfs_root *root,
104 struct extent_buffer *eb)
109 struct btrfs_disk_key bad_key;;
111 nr = btrfs_header_nritems(eb);
116 bad_slot = rand() % nr;
118 if (bad_slot == slot)
122 "corrupting keys in block %llu slot %d swapping with %d\n",
123 (unsigned long long)eb->start, slot, bad_slot);
125 if (btrfs_header_level(eb) == 0) {
126 btrfs_item_key(eb, &bad_key, bad_slot);
127 btrfs_set_item_key(eb, &bad_key, slot);
129 btrfs_node_key(eb, &bad_key, bad_slot);
130 btrfs_set_node_key(eb, &bad_key, slot);
132 btrfs_mark_buffer_dirty(eb);
134 csum_tree_block(root, eb, 0);
135 write_extent_to_disk(eb);
140 static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
142 struct extent_buffer *eb;
144 eb = read_tree_block(root, bytenr, root->leafsize, 0);
148 corrupt_keys(NULL, root, eb);
149 free_extent_buffer(eb);
153 static int corrupt_extent(struct btrfs_trans_handle *trans,
154 struct btrfs_root *root, u64 bytenr, int copy)
156 struct btrfs_key key;
157 struct extent_buffer *leaf;
160 struct btrfs_path *path;
163 int should_del = rand() % 3;
165 path = btrfs_alloc_path();
167 key.objectid = bytenr;
169 key.offset = (u64)-1;
172 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
178 if (path->slots[0] == 0)
183 leaf = path->nodes[0];
184 slot = path->slots[0];
185 btrfs_item_key_to_cpu(leaf, &key, slot);
186 if (key.objectid != bytenr)
189 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
190 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
191 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
192 key.type != BTRFS_EXTENT_REF_V0_KEY &&
193 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
194 key.type != BTRFS_SHARED_DATA_REF_KEY)
199 "deleting extent record: key %llu %u %llu\n",
200 key.objectid, key.type, key.offset);
202 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
203 /* make sure this extent doesn't get
204 * reused for other purposes */
205 btrfs_pin_extent(root->fs_info,
206 key.objectid, key.offset);
209 btrfs_del_item(trans, root, path);
212 "corrupting extent record: key %llu %u %llu\n",
213 key.objectid, key.type, key.offset);
214 ptr = btrfs_item_ptr_offset(leaf, slot);
215 item_size = btrfs_item_size_nr(leaf, slot);
216 memset_extent_buffer(leaf, 0, ptr, item_size);
217 btrfs_mark_buffer_dirty(leaf);
220 btrfs_release_path(NULL, path);
228 btrfs_free_path(path);
232 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
233 struct btrfs_root *root,
234 struct extent_buffer *eb)
236 u32 nr = btrfs_header_nritems(eb);
237 u32 victim = rand() % nr;
239 struct btrfs_key key;
241 btrfs_item_key_to_cpu(eb, &key, victim);
242 objectid = key.objectid;
243 corrupt_extent(trans, root, objectid, 1);
246 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
247 struct btrfs_root *root,
248 struct extent_buffer *eb)
256 nr = btrfs_header_nritems(eb);
257 if (btrfs_is_leaf(eb)) {
258 btrfs_corrupt_extent_leaf(trans, root, eb);
262 if (btrfs_header_level(eb) == 1 && eb != root->node) {
267 for (i = 0; i < nr; i++) {
268 struct extent_buffer *next;
270 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
272 btrfs_node_ptr_generation(eb, i));
275 btrfs_corrupt_extent_tree(trans, root, next);
276 free_extent_buffer(next);
280 static struct option long_options[] = {
281 /* { "byte-count", 1, NULL, 'b' }, */
282 { "logical", 1, NULL, 'l' },
283 { "copy", 1, NULL, 'c' },
284 { "bytes", 1, NULL, 'b' },
285 { "extent-record", 0, NULL, 'e' },
286 { "extent-tree", 0, NULL, 'E' },
287 { "keys", 0, NULL, 'k' },
288 { "chunk-record", 0, NULL, 'u' },
289 { "chunk-tree", 0, NULL, 'U' },
293 /* corrupt item using NO cow.
294 * Because chunk recover will recover based on whole partition scaning,
295 * If using COW, chunk recover will use the old item to recover,
296 * which is still OK but we want to check the ability to rebuild chunk
297 * not only restore the old ones */
298 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
299 struct btrfs_root *root, struct btrfs_path *path,
303 struct btrfs_key key;
304 struct extent_buffer *leaf;
309 leaf = path->nodes[0];
310 slot = path->slots[0];
311 /* Not deleting the first item of a leaf to keep leaf structure */
314 /* Only accept valid eb */
315 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
316 btrfs_item_key_to_cpu(leaf, &key, slot);
318 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
319 key.objectid, key.type, key.offset);
320 btrfs_del_item(trans, root, path);
322 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
323 key.objectid, key.type, key.offset);
324 ptr = btrfs_item_ptr_offset(leaf, slot);
325 item_size = btrfs_item_size_nr(leaf, slot);
326 memset_extent_buffer(leaf, 0, ptr, item_size);
327 btrfs_mark_buffer_dirty(leaf);
331 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
332 struct btrfs_root *root)
337 struct btrfs_path *path;
338 struct btrfs_key key;
339 struct btrfs_key found_key;
340 struct extent_buffer *leaf;
342 path = btrfs_alloc_path();
343 key.objectid = (u64)-1;
344 key.offset = (u64)-1;
347 /* Here, cow and ins_len must equals 0 for the following reasons:
348 * 1) chunk recover is based on disk scanning, so COW should be
349 * disabled in case the original chunk being scanned and
350 * recovered using the old chunk.
351 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
354 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
357 fprintf(stderr, "Error searching tree\n");
360 /* corrupt/del dev_item first */
361 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
362 slot = path->slots[0];
363 leaf = path->nodes[0];
365 /* Never delete the first item to keep the leaf structure */
366 if (path->slots[0] == 0)
368 ret = corrupt_item_nocow(trans, root, path, del);
372 btrfs_free_path(path);
374 /* Here, cow and ins_len must equals 0 for the following reasons:
375 * 1) chunk recover is based on disk scanning, so COW should be
376 * disabled in case the original chunk being scanned and
377 * recovered using the old chunk.
378 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
381 path = btrfs_alloc_path();
382 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
385 fprintf(stderr, "Error searching tree\n");
388 /* corrupt/del chunk then*/
389 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
390 slot = path->slots[0];
391 leaf = path->nodes[0];
393 btrfs_item_key_to_cpu(leaf, &found_key, slot);
394 ret = corrupt_item_nocow(trans, root, path, del);
399 btrfs_free_path(path);
402 int find_chunk_offset(struct btrfs_root *root,
403 struct btrfs_path *path, u64 offset)
405 struct btrfs_key key;
408 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
409 key.type = BTRFS_CHUNK_ITEM_KEY;
412 /* Here, cow and ins_len must equals 0 for following reasons:
413 * 1) chunk recover is based on disk scanning, so COW should
414 * be disabled in case the original chunk being scanned
415 * and recovered using the old chunk.
416 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
419 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
421 fprintf(stderr, "Can't find chunk with given offset %llu\n",
426 fprintf(stderr, "Error searching chunk");
433 int main(int ac, char **av)
435 struct cache_tree root_cache;
436 struct btrfs_root *root;
437 struct extent_buffer *eb;
439 /* chunk offset can be 0,so change to (u64)-1 */
440 u64 logical = (u64)-1;
442 int option_index = 0;
447 int corrupt_block_keys = 0;
455 c = getopt_long(ac, av, "l:c:b:eEkuU", long_options,
461 logical = atoll(optarg);
467 "invalid copy number\n");
472 bytes = atoll(optarg);
475 "invalid byte count\n");
486 corrupt_block_keys = 1;
501 if (logical == (u64)-1 && !(extent_tree || chunk_tree))
509 cache_tree_init(&root_cache);
511 root = open_ctree(dev, 0, 1);
513 fprintf(stderr, "Open ctree failed\n");
517 struct btrfs_trans_handle *trans;
518 trans = btrfs_start_transaction(root, 1);
519 ret = corrupt_extent (trans, root, logical, 0);
520 btrfs_commit_transaction(trans, root);
524 struct btrfs_trans_handle *trans;
525 trans = btrfs_start_transaction(root, 1);
526 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
527 root->fs_info->extent_root->node);
528 btrfs_commit_transaction(trans, root);
532 struct btrfs_trans_handle *trans;
533 struct btrfs_path *path;
537 path = btrfs_alloc_path();
539 if (find_chunk_offset(root->fs_info->chunk_root, path,
541 btrfs_free_path(path);
544 trans = btrfs_start_transaction(root, 1);
545 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
548 fprintf(stderr, "Failed to corrupt chunk record\n");
549 btrfs_commit_transaction(trans, root);
553 struct btrfs_trans_handle *trans;
554 trans = btrfs_start_transaction(root, 1);
555 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
557 fprintf(stderr, "Failed to corrupt chunk tree\n");
558 btrfs_commit_transaction(trans, root);
563 bytes = root->sectorsize;
565 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
566 bytes *= root->sectorsize;
569 if (corrupt_block_keys) {
570 corrupt_keys_in_block(root, logical);
572 eb = debug_corrupt_block(root, logical,
573 root->sectorsize, copy);
574 free_extent_buffer(eb);
576 logical += root->sectorsize;
577 bytes -= root->sectorsize;