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();
169 key.objectid = bytenr;
171 key.offset = (u64)-1;
174 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
180 if (path->slots[0] == 0)
185 leaf = path->nodes[0];
186 slot = path->slots[0];
187 btrfs_item_key_to_cpu(leaf, &key, slot);
188 if (key.objectid != bytenr)
191 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
192 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
193 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
194 key.type != BTRFS_EXTENT_REF_V0_KEY &&
195 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
196 key.type != BTRFS_SHARED_DATA_REF_KEY)
201 "deleting extent record: key %llu %u %llu\n",
202 key.objectid, key.type, key.offset);
204 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
205 /* make sure this extent doesn't get
206 * reused for other purposes */
207 btrfs_pin_extent(root->fs_info,
208 key.objectid, key.offset);
211 btrfs_del_item(trans, root, path);
214 "corrupting extent record: key %llu %u %llu\n",
215 key.objectid, key.type, key.offset);
216 ptr = btrfs_item_ptr_offset(leaf, slot);
217 item_size = btrfs_item_size_nr(leaf, slot);
218 memset_extent_buffer(leaf, 0, ptr, item_size);
219 btrfs_mark_buffer_dirty(leaf);
222 btrfs_release_path(NULL, path);
230 btrfs_free_path(path);
234 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
235 struct btrfs_root *root,
236 struct extent_buffer *eb)
238 u32 nr = btrfs_header_nritems(eb);
239 u32 victim = rand() % nr;
241 struct btrfs_key key;
243 btrfs_item_key_to_cpu(eb, &key, victim);
244 objectid = key.objectid;
245 corrupt_extent(trans, root, objectid, 1);
248 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
249 struct btrfs_root *root,
250 struct extent_buffer *eb)
258 nr = btrfs_header_nritems(eb);
259 if (btrfs_is_leaf(eb)) {
260 btrfs_corrupt_extent_leaf(trans, root, eb);
264 if (btrfs_header_level(eb) == 1 && eb != root->node) {
269 for (i = 0; i < nr; i++) {
270 struct extent_buffer *next;
272 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
274 btrfs_node_ptr_generation(eb, i));
277 btrfs_corrupt_extent_tree(trans, root, next);
278 free_extent_buffer(next);
282 static struct option long_options[] = {
283 /* { "byte-count", 1, NULL, 'b' }, */
284 { "logical", 1, NULL, 'l' },
285 { "copy", 1, NULL, 'c' },
286 { "bytes", 1, NULL, 'b' },
287 { "extent-record", 0, NULL, 'e' },
288 { "extent-tree", 0, NULL, 'E' },
289 { "keys", 0, NULL, 'k' },
290 { "chunk-record", 0, NULL, 'u' },
291 { "chunk-tree", 0, NULL, 'U' },
295 /* corrupt item using NO cow.
296 * Because chunk recover will recover based on whole partition scaning,
297 * If using COW, chunk recover will use the old item to recover,
298 * which is still OK but we want to check the ability to rebuild chunk
299 * not only restore the old ones */
300 int corrupt_item_nocow(struct btrfs_trans_handle *trans,
301 struct btrfs_root *root, struct btrfs_path *path,
305 struct btrfs_key key;
306 struct extent_buffer *leaf;
311 leaf = path->nodes[0];
312 slot = path->slots[0];
313 /* Not deleting the first item of a leaf to keep leaf structure */
316 /* Only accept valid eb */
317 BUG_ON(!leaf->data || slot >= btrfs_header_nritems(leaf));
318 btrfs_item_key_to_cpu(leaf, &key, slot);
320 fprintf(stdout, "Deleting key and data [%llu, %u, %llu].\n",
321 key.objectid, key.type, key.offset);
322 btrfs_del_item(trans, root, path);
324 fprintf(stdout, "Corrupting key and data [%llu, %u, %llu].\n",
325 key.objectid, key.type, key.offset);
326 ptr = btrfs_item_ptr_offset(leaf, slot);
327 item_size = btrfs_item_size_nr(leaf, slot);
328 memset_extent_buffer(leaf, 0, ptr, item_size);
329 btrfs_mark_buffer_dirty(leaf);
333 int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
334 struct btrfs_root *root)
339 struct btrfs_path *path;
340 struct btrfs_key key;
341 struct btrfs_key found_key;
342 struct extent_buffer *leaf;
344 path = btrfs_alloc_path();
345 key.objectid = (u64)-1;
346 key.offset = (u64)-1;
349 /* Here, cow and ins_len must equals 0 for the following reasons:
350 * 1) chunk recover is based on disk scanning, so COW should be
351 * disabled in case the original chunk being scanned and
352 * recovered using the old chunk.
353 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
356 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
359 fprintf(stderr, "Error searching tree\n");
362 /* corrupt/del dev_item first */
363 while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
364 slot = path->slots[0];
365 leaf = path->nodes[0];
367 /* Never delete the first item to keep the leaf structure */
368 if (path->slots[0] == 0)
370 ret = corrupt_item_nocow(trans, root, path, del);
374 btrfs_free_path(path);
376 /* Here, cow and ins_len must equals 0 for the following reasons:
377 * 1) chunk recover is based on disk scanning, so COW should be
378 * disabled in case the original chunk being scanned and
379 * recovered using the old chunk.
380 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON will be
383 path = btrfs_alloc_path();
384 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
387 fprintf(stderr, "Error searching tree\n");
390 /* corrupt/del chunk then*/
391 while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
392 slot = path->slots[0];
393 leaf = path->nodes[0];
395 btrfs_item_key_to_cpu(leaf, &found_key, slot);
396 ret = corrupt_item_nocow(trans, root, path, del);
401 btrfs_free_path(path);
404 int find_chunk_offset(struct btrfs_root *root,
405 struct btrfs_path *path, u64 offset)
407 struct btrfs_key key;
410 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
411 key.type = BTRFS_CHUNK_ITEM_KEY;
414 /* Here, cow and ins_len must equals 0 for following reasons:
415 * 1) chunk recover is based on disk scanning, so COW should
416 * be disabled in case the original chunk being scanned
417 * and recovered using the old chunk.
418 * 2) if cow = 0, ins_len must also be set to 0, or BUG_ON
421 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
423 fprintf(stderr, "Can't find chunk with given offset %llu\n",
428 fprintf(stderr, "Error searching chunk");
435 int main(int ac, char **av)
437 struct cache_tree root_cache;
438 struct btrfs_root *root;
439 struct extent_buffer *eb;
441 /* chunk offset can be 0,so change to (u64)-1 */
442 u64 logical = (u64)-1;
444 int option_index = 0;
449 int corrupt_block_keys = 0;
457 c = getopt_long(ac, av, "l:c:b:eEkuU", long_options,
463 logical = atoll(optarg);
469 "invalid copy number\n");
474 bytes = atoll(optarg);
477 "invalid byte count\n");
488 corrupt_block_keys = 1;
503 if (logical == (u64)-1 && !(extent_tree || chunk_tree))
511 cache_tree_init(&root_cache);
513 root = open_ctree(dev, 0, 1);
515 fprintf(stderr, "Open ctree failed\n");
519 struct btrfs_trans_handle *trans;
520 trans = btrfs_start_transaction(root, 1);
521 ret = corrupt_extent (trans, root, logical, 0);
522 btrfs_commit_transaction(trans, root);
526 struct btrfs_trans_handle *trans;
527 trans = btrfs_start_transaction(root, 1);
528 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
529 root->fs_info->extent_root->node);
530 btrfs_commit_transaction(trans, root);
534 struct btrfs_trans_handle *trans;
535 struct btrfs_path *path;
539 path = btrfs_alloc_path();
541 if (find_chunk_offset(root->fs_info->chunk_root, path,
543 btrfs_free_path(path);
546 trans = btrfs_start_transaction(root, 1);
547 ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
550 fprintf(stderr, "Failed to corrupt chunk record\n");
551 btrfs_commit_transaction(trans, root);
555 struct btrfs_trans_handle *trans;
556 trans = btrfs_start_transaction(root, 1);
557 ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
559 fprintf(stderr, "Failed to corrupt chunk tree\n");
560 btrfs_commit_transaction(trans, root);
565 bytes = root->sectorsize;
567 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
568 bytes *= root->sectorsize;
571 if (corrupt_block_keys) {
572 corrupt_keys_in_block(root, logical);
574 eb = debug_corrupt_block(root, logical,
575 root->sectorsize, copy);
576 free_extent_buffer(eb);
578 logical += root->sectorsize;
579 bytes -= root->sectorsize;