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 /* we write the mirror info to stdout unless they are dumping the data
38 static FILE *info_file;
40 struct extent_buffer *debug_read_block(struct btrfs_root *root, u64 bytenr,
41 u32 blocksize, int 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);
59 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
60 eb->start, &length, &multi, mirror_num);
62 device = multi->stripes[0].dev;
65 eb->dev_bytenr = multi->stripes[0].physical;
67 fprintf(info_file, "mirror %d logical %Lu physical %Lu "
68 "device %s\n", mirror_num, bytenr, eb->dev_bytenr,
72 if (!copy || mirror_num == copy)
73 ret = read_extent_from_disk(eb);
75 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
81 if (mirror_num > num_copies)
87 static void print_usage(void)
89 fprintf(stderr, "usage: btrfs-map-logical [options] mount_point\n");
90 fprintf(stderr, "\t-l Logical extent to map\n");
91 fprintf(stderr, "\t-c Copy of the extent to read (usually 1 or 2)\n");
92 fprintf(stderr, "\t-o Output file to hold the extent\n");
93 fprintf(stderr, "\t-s Number of bytes to read\n");
97 static struct option long_options[] = {
98 /* { "byte-count", 1, NULL, 'b' }, */
99 { "logical", 1, NULL, 'l' },
100 { "copy", 1, NULL, 'c' },
101 { "output", 1, NULL, 'c' },
102 { "bytes", 1, NULL, 'b' },
106 int main(int ac, char **av)
108 struct cache_tree root_cache;
109 struct btrfs_root *root;
110 struct extent_buffer *eb;
112 char *output_file = NULL;
115 int option_index = 0;
123 c = getopt_long(ac, av, "l:c:o:b:", long_options,
129 logical = atoll(optarg);
132 "invalid extent number\n");
140 "invalid copy number\n");
145 bytes = atoll(optarg);
148 "invalid byte count\n");
153 output_file = strdup(optarg);
170 cache_tree_init(&root_cache);
172 root = open_ctree(dev, 0, 0);
174 fprintf(stderr, "Open ctree failed\n");
179 if (strcmp(output_file, "-") == 0) {
183 out_fd = open(output_file, O_RDWR | O_CREAT, 0600);
186 err = ftruncate(out_fd, 0);
196 bytes = root->sectorsize;
198 bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
199 bytes *= root->sectorsize;
202 eb = debug_read_block(root, logical, root->sectorsize, copy);
203 if (eb && output_file) {
204 err = write(out_fd, eb->data, eb->len);
205 if (err < 0 || err != eb->len) {
206 fprintf(stderr, "output file write failed\n");
210 free_extent_buffer(eb);
211 logical += root->sectorsize;
212 bytes -= root->sectorsize;
216 if (output_file && out_fd != 1)