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 struct option long_options[] = {
97 /* { "byte-count", 1, NULL, 'b' }, */
98 { "logical", 1, NULL, 'l' },
99 { "copy", 1, NULL, 'c' },
100 { "bytes", 1, NULL, 'b' },
104 static int corrupt_extent(struct btrfs_root *root, u64 bytenr, int copy)
106 struct btrfs_trans_handle *trans;
107 struct btrfs_key key;
108 struct extent_buffer *leaf;
111 struct btrfs_path *path;
115 trans = btrfs_start_transaction(root, 1);
116 path = btrfs_alloc_path();
118 key.objectid = bytenr;
120 key.offset = (u64)-1;
123 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
129 if (path->slots[0] == 0)
133 leaf = path->nodes[0];
134 slot = path->slots[0];
135 btrfs_item_key_to_cpu(leaf, &key, slot);
136 if (key.objectid != bytenr)
139 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
140 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
141 key.type != BTRFS_EXTENT_DATA_REF_KEY &&
142 key.type != BTRFS_EXTENT_REF_V0_KEY &&
143 key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
144 key.type != BTRFS_SHARED_DATA_REF_KEY)
147 fprintf(stderr, "corrupting extent record: key %Lu %u %Lu\n",
148 key.objectid, key.type, key.offset);
150 ptr = btrfs_item_ptr_offset(leaf, slot);
151 item_size = btrfs_item_size_nr(leaf, slot);
152 memset_extent_buffer(leaf, 0, ptr, item_size);
153 btrfs_mark_buffer_dirty(leaf);
155 btrfs_release_path(NULL, path);
163 btrfs_free_path(path);
164 btrfs_commit_transaction(trans, root);
165 ret = close_ctree(root);
170 int main(int ac, char **av)
172 struct cache_tree root_cache;
173 struct btrfs_root *root;
174 struct extent_buffer *eb;
178 int option_index = 0;
185 c = getopt_long(ac, av, "l:c:e", long_options,
191 logical = atoll(optarg);
194 "invalid extent number\n");
202 "invalid copy number\n");
207 bytes = atoll(optarg);
210 "invalid byte count\n");
232 cache_tree_init(&root_cache);
234 root = open_ctree(dev, 0, 1);
236 fprintf(stderr, "Open ctree failed\n");
240 ret = corrupt_extent (root, logical, 0);
245 bytes = root->sectorsize;
247 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
248 bytes *= root->sectorsize;
251 eb = debug_corrupt_block(root, logical, root->sectorsize, copy);
252 free_extent_buffer(eb);
253 logical += root->sectorsize;
254 bytes -= root->sectorsize;