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.
24 #include "kerncompat.h"
28 #include "print-tree.h"
29 #include "transaction.h"
33 /* we write the mirror info to stdout unless they are dumping the data
36 static FILE *info_file;
38 static int map_one_extent(struct btrfs_fs_info *fs_info,
39 u64 *logical_ret, u64 *len_ret, int search_foward)
41 struct btrfs_path *path;
48 logical = *logical_ret;
50 path = btrfs_alloc_path();
54 key.objectid = logical;
58 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path,
66 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
67 if ((search_foward && key.objectid < logical) ||
68 (!search_foward && key.objectid > logical) ||
69 (key.type != BTRFS_EXTENT_ITEM_KEY &&
70 key.type != BTRFS_METADATA_ITEM_KEY)) {
72 ret = btrfs_previous_extent_item(fs_info->extent_root,
75 ret = btrfs_next_item(fs_info->extent_root, path);
80 logical = key.objectid;
81 if (key.type == BTRFS_METADATA_ITEM_KEY)
82 len = fs_info->tree_root->leafsize;
87 btrfs_free_path(path);
89 *logical_ret = logical;
96 static struct extent_buffer * debug_read_block(struct btrfs_root *root,
97 u64 bytenr, u32 blocksize, u64 copy)
100 struct extent_buffer *eb;
102 struct btrfs_multi_bio *multi = NULL;
103 struct btrfs_device *device;
107 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
113 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
114 eb->start, &length, &multi,
118 "Error: fails to map mirror%d logical %llu: %s\n",
119 mirror_num, (unsigned long long)eb->start,
121 free_extent_buffer(eb);
124 device = multi->stripes[0].dev;
127 eb->dev_bytenr = multi->stripes[0].physical;
129 fprintf(info_file, "mirror %d logical %Lu physical %Lu "
130 "device %s\n", mirror_num, (unsigned long long)bytenr,
131 (unsigned long long)eb->dev_bytenr, device->name);
134 if (!copy || mirror_num == copy) {
135 ret = read_extent_from_disk(eb, 0, eb->len);
138 "Error: failed to read extent: mirror %d logical %llu: %s\n",
139 mirror_num, (unsigned long long)eb->start,
141 free_extent_buffer(eb);
147 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
153 if (mirror_num > num_copies)
159 static void print_usage(void) __attribute__((noreturn));
160 static void print_usage(void)
162 fprintf(stderr, "usage: btrfs-map-logical [options] device\n");
163 fprintf(stderr, "\t-l Logical extent to map\n");
164 fprintf(stderr, "\t-c Copy of the extent to read (usually 1 or 2)\n");
165 fprintf(stderr, "\t-o Output file to hold the extent\n");
166 fprintf(stderr, "\t-b Number of bytes to read\n");
170 int main(int ac, char **av)
172 struct cache_tree root_cache;
173 struct btrfs_root *root;
174 struct extent_buffer *eb;
176 char *output_file = NULL;
185 static const struct option long_options[] = {
186 /* { "byte-count", 1, NULL, 'b' }, */
187 { "logical", required_argument, NULL, 'l' },
188 { "copy", required_argument, NULL, 'c' },
189 { "output", required_argument, NULL, 'o' },
190 { "bytes", required_argument, NULL, 'b' },
194 c = getopt_long(ac, av, "l:c:o:b:", long_options, NULL);
199 logical = arg_strtou64(optarg);
202 copy = arg_strtou64(optarg);
205 bytes = arg_strtou64(optarg);
208 output_file = strdup(optarg);
216 if (check_argc_min(ac, 1))
224 cache_tree_init(&root_cache);
226 root = open_ctree(dev, 0, 0);
228 fprintf(stderr, "Open ctree failed\n");
234 if (strcmp(output_file, "-") == 0) {
238 out_fd = open(output_file, O_RDWR | O_CREAT, 0600);
241 ret = ftruncate(out_fd, 0);
252 bytes = root->sectorsize;
254 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
255 bytes *= root->sectorsize;
258 eb = debug_read_block(root, logical, root->sectorsize, copy);
259 if (eb && output_file) {
260 ret = write(out_fd, eb->data, eb->len);
261 if (ret < 0 || ret != eb->len) {
263 fprintf(stderr, "output file write failed\n");
267 free_extent_buffer(eb);
268 logical += root->sectorsize;
269 bytes -= root->sectorsize;
273 if (output_file && out_fd != 1)