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, mirror_num);
55 device = multi->stripes[0].dev;
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);
65 if (!copy || mirror_num == copy) {
66 ret = read_extent_from_disk(eb);
67 printf("corrupting %llu copy %d\n", eb->start,
69 memset(eb->data, 0, eb->len);
70 write_extent_to_disk(eb);
74 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
80 if (mirror_num > num_copies)
86 static void print_usage(void)
88 fprintf(stderr, "usage: btrfs-map-logical [options] mount_point\n");
89 fprintf(stderr, "\t-l Logical extent to map\n");
90 fprintf(stderr, "\t-c Copy of the extent to read (usually 1 or 2)\n");
91 fprintf(stderr, "\t-o Output file to hold the extent\n");
92 fprintf(stderr, "\t-b Number of bytes to read\n");
96 static int corrupt_extent(struct btrfs_trans_handle *trans,
97 struct btrfs_root *root, u64 bytenr, int copy)
100 struct extent_buffer *leaf;
103 struct btrfs_path *path;
106 int should_del = rand() % 3;
108 path = btrfs_alloc_path();
110 key.objectid = bytenr;
112 key.offset = (u64)-1;
115 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
121 if (path->slots[0] == 0)
126 leaf = path->nodes[0];
127 slot = path->slots[0];
128 btrfs_item_key_to_cpu(leaf, &key, slot);
129 if (key.objectid != bytenr)
132 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
133 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
134 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
135 key.type != BTRFS_EXTENT_REF_V0_KEY &&
136 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
137 key.type != BTRFS_SHARED_DATA_REF_KEY)
141 fprintf(stderr, "deleting extent record: key %Lu %u %Lu\n",
142 key.objectid, key.type, key.offset);
144 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
145 /* make sure this extent doesn't get
146 * reused for other purposes */
147 btrfs_pin_extent(root->fs_info,
148 key.objectid, key.offset);
151 btrfs_del_item(trans, root, path);
153 fprintf(stderr, "corrupting extent record: key %Lu %u %Lu\n",
154 key.objectid, key.type, key.offset);
155 ptr = btrfs_item_ptr_offset(leaf, slot);
156 item_size = btrfs_item_size_nr(leaf, slot);
157 memset_extent_buffer(leaf, 0, ptr, item_size);
158 btrfs_mark_buffer_dirty(leaf);
161 btrfs_release_path(NULL, path);
169 btrfs_free_path(path);
173 static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans,
174 struct btrfs_root *root, struct extent_buffer *eb)
176 u32 nr = btrfs_header_nritems(eb);
177 u32 victim = rand() % nr;
179 struct btrfs_key key;
181 btrfs_item_key_to_cpu(eb, &key, victim);
182 objectid = key.objectid;
183 corrupt_extent(trans, root, objectid, 1);
186 static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
187 struct btrfs_root *root, struct extent_buffer *eb)
195 nr = btrfs_header_nritems(eb);
196 if (btrfs_is_leaf(eb)) {
197 btrfs_corrupt_extent_leaf(trans, root, eb);
201 if (btrfs_header_level(eb) == 1 && eb != root->node) {
206 for (i = 0; i < nr; i++) {
207 struct extent_buffer *next;
209 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
210 root->leafsize, btrfs_node_ptr_generation(eb, i));
213 btrfs_corrupt_extent_tree(trans, root, next);
214 free_extent_buffer(next);
218 static struct option long_options[] = {
219 /* { "byte-count", 1, NULL, 'b' }, */
220 { "logical", 1, NULL, 'l' },
221 { "copy", 1, NULL, 'c' },
222 { "bytes", 1, NULL, 'b' },
223 { "extent-record", 0, NULL, 'e' },
224 { "extent-tree", 0, NULL, 'E' },
229 int main(int ac, char **av)
231 struct cache_tree root_cache;
232 struct btrfs_root *root;
233 struct extent_buffer *eb;
237 int option_index = 0;
247 c = getopt_long(ac, av, "l:c:eE", long_options,
253 logical = atoll(optarg);
256 "invalid extent number\n");
264 "invalid copy number\n");
269 bytes = atoll(optarg);
272 "invalid byte count\n");
289 if (logical == 0 && !extent_tree)
297 cache_tree_init(&root_cache);
299 root = open_ctree(dev, 0, 1);
301 fprintf(stderr, "Open ctree failed\n");
305 struct btrfs_trans_handle *trans;
306 trans = btrfs_start_transaction(root, 1);
307 ret = corrupt_extent (trans, root, logical, 0);
308 btrfs_commit_transaction(trans, root);
312 struct btrfs_trans_handle *trans;
313 trans = btrfs_start_transaction(root, 1);
314 btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
315 root->fs_info->extent_root->node);
316 btrfs_commit_transaction(trans, root);
321 bytes = root->sectorsize;
323 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
324 bytes *= root->sectorsize;
327 eb = debug_corrupt_block(root, logical, root->sectorsize, copy);
328 free_extent_buffer(eb);
329 logical += root->sectorsize;
330 bytes -= root->sectorsize;