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"
36 /* we write the mirror info to stdout unless they are dumping the data
39 static FILE *info_file;
41 static struct extent_buffer * debug_read_block(struct btrfs_root *root,
42 u64 bytenr, u32 blocksize, u64 copy)
45 struct extent_buffer *eb;
47 struct btrfs_multi_bio *multi = NULL;
48 struct btrfs_device *device;
52 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
58 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
59 eb->start, &length, &multi,
63 "Error: fails to map mirror%d logical %llu: %s\n",
64 mirror_num, (unsigned long long)eb->start,
66 free_extent_buffer(eb);
69 device = multi->stripes[0].dev;
72 eb->dev_bytenr = multi->stripes[0].physical;
74 fprintf(info_file, "mirror %d logical %Lu physical %Lu "
75 "device %s\n", mirror_num, (unsigned long long)bytenr,
76 (unsigned long long)eb->dev_bytenr, device->name);
79 if (!copy || mirror_num == copy) {
80 ret = read_extent_from_disk(eb, 0, eb->len);
83 "Error: failed to read extent: mirror %d logical %llu: %s\n",
84 mirror_num, (unsigned long long)eb->start,
86 free_extent_buffer(eb);
92 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
98 if (mirror_num > num_copies)
104 static void print_usage(void) __attribute__((noreturn));
105 static void print_usage(void)
107 fprintf(stderr, "usage: btrfs-map-logical [options] device\n");
108 fprintf(stderr, "\t-l Logical extent to map\n");
109 fprintf(stderr, "\t-c Copy of the extent to read (usually 1 or 2)\n");
110 fprintf(stderr, "\t-o Output file to hold the extent\n");
111 fprintf(stderr, "\t-b Number of bytes to read\n");
115 int main(int ac, char **av)
117 struct cache_tree root_cache;
118 struct btrfs_root *root;
119 struct extent_buffer *eb;
121 char *output_file = NULL;
130 int option_index = 0;
131 static const struct option long_options[] = {
132 /* { "byte-count", 1, NULL, 'b' }, */
133 { "logical", 1, NULL, 'l' },
134 { "copy", 1, NULL, 'c' },
135 { "output", 1, NULL, 'o' },
136 { "bytes", 1, NULL, 'b' },
140 c = getopt_long(ac, av, "l:c:o:b:", long_options,
146 logical = arg_strtou64(optarg);
149 copy = arg_strtou64(optarg);
152 bytes = arg_strtou64(optarg);
155 output_file = strdup(optarg);
163 if (check_argc_min(ac, 1))
171 cache_tree_init(&root_cache);
173 root = open_ctree(dev, 0, 0);
175 fprintf(stderr, "Open ctree failed\n");
181 if (strcmp(output_file, "-") == 0) {
185 out_fd = open(output_file, O_RDWR | O_CREAT, 0600);
188 ret = ftruncate(out_fd, 0);
199 bytes = root->sectorsize;
201 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
202 bytes *= root->sectorsize;
205 eb = debug_read_block(root, logical, root->sectorsize, copy);
206 if (eb && output_file) {
207 ret = write(out_fd, eb->data, eb->len);
208 if (ret < 0 || ret != eb->len) {
210 fprintf(stderr, "output file write failed\n");
214 free_extent_buffer(eb);
215 logical += root->sectorsize;
216 bytes -= root->sectorsize;
220 if (output_file && out_fd != 1)