e47a1ae138fc8c788b47f7424cea66d5da6a283e
[platform/upstream/btrfs-progs.git] / btrfs-map-logical.c
1 /*
2  * Copyright (C) 2009 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include "kerncompat.h"
27 #include "ctree.h"
28 #include "volumes.h"
29 #include "disk-io.h"
30 #include "print-tree.h"
31 #include "transaction.h"
32 #include "list.h"
33 #include "version.h"
34 #include "utils.h"
35
36 /* we write the mirror info to stdout unless they are dumping the data
37  * to stdout
38  * */
39 static FILE *info_file;
40
41 static struct extent_buffer * debug_read_block(struct btrfs_root *root,
42                 u64 bytenr, u32 blocksize, u64 copy)
43 {
44         int ret;
45         struct extent_buffer *eb;
46         u64 length;
47         struct btrfs_multi_bio *multi = NULL;
48         struct btrfs_device *device;
49         int num_copies;
50         int mirror_num = 1;
51
52         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
53         if (!eb)
54                 return NULL;
55
56         length = blocksize;
57         while (1) {
58                 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
59                                       eb->start, &length, &multi,
60                                       mirror_num, NULL);
61                 if (ret) {
62                         fprintf(info_file,
63                                 "Error: fails to map mirror%d logical %llu: %s\n",
64                                 mirror_num, (unsigned long long)eb->start,
65                                 strerror(-ret));
66                         free_extent_buffer(eb);
67                         return NULL;
68                 }
69                 device = multi->stripes[0].dev;
70                 eb->fd = device->fd;
71                 device->total_ios++;
72                 eb->dev_bytenr = multi->stripes[0].physical;
73
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);
77                 kfree(multi);
78
79                 if (!copy || mirror_num == copy)
80                         ret = read_extent_from_disk(eb, 0, eb->len);
81
82                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
83                                               eb->start, eb->len);
84                 if (num_copies == 1)
85                         break;
86
87                 mirror_num++;
88                 if (mirror_num > num_copies)
89                         break;
90         }
91         return eb;
92 }
93
94 static void print_usage(void) __attribute__((noreturn));
95 static void print_usage(void)
96 {
97         fprintf(stderr, "usage: btrfs-map-logical [options] device\n");
98         fprintf(stderr, "\t-l Logical extent to map\n");
99         fprintf(stderr, "\t-c Copy of the extent to read (usually 1 or 2)\n");
100         fprintf(stderr, "\t-o Output file to hold the extent\n");
101         fprintf(stderr, "\t-b Number of bytes to read\n");
102         exit(1);
103 }
104
105 static struct option long_options[] = {
106         /* { "byte-count", 1, NULL, 'b' }, */
107         { "logical", 1, NULL, 'l' },
108         { "copy", 1, NULL, 'c' },
109         { "output", 1, NULL, 'o' },
110         { "bytes", 1, NULL, 'b' },
111         { NULL, 0, NULL, 0}
112 };
113
114 int main(int ac, char **av)
115 {
116         struct cache_tree root_cache;
117         struct btrfs_root *root;
118         struct extent_buffer *eb;
119         char *dev;
120         char *output_file = NULL;
121         u64 logical = 0;
122         int ret = 0;
123         int option_index = 0;
124         u64 copy = 0;
125         u64 bytes = 0;
126         int out_fd = 0;
127
128         while(1) {
129                 int c;
130                 c = getopt_long(ac, av, "l:c:o:b:", long_options,
131                                 &option_index);
132                 if (c < 0)
133                         break;
134                 switch(c) {
135                         case 'l':
136                                 logical = arg_strtou64(optarg);
137                                 break;
138                         case 'c':
139                                 copy = arg_strtou64(optarg);
140                                 break;
141                         case 'b':
142                                 bytes = arg_strtou64(optarg);
143                                 break;
144                         case 'o':
145                                 output_file = strdup(optarg);
146                                 break;
147                         default:
148                                 print_usage();
149                 }
150         }
151         ac = ac - optind;
152         if (ac == 0)
153                 print_usage();
154         if (logical == 0)
155                 print_usage();
156
157         dev = av[optind];
158
159         radix_tree_init();
160         cache_tree_init(&root_cache);
161
162         root = open_ctree(dev, 0, 0);
163         if (!root) {
164                 fprintf(stderr, "Open ctree failed\n");
165                 exit(1);
166         }
167
168         info_file = stdout;
169         if (output_file) {
170                 if (strcmp(output_file, "-") == 0) {
171                         out_fd = 1;
172                         info_file = stderr;
173                 } else {
174                         out_fd = open(output_file, O_RDWR | O_CREAT, 0600);
175                         if (out_fd < 0)
176                                 goto close;
177                         ret = ftruncate(out_fd, 0);
178                         if (ret) {
179                                 ret = 1;
180                                 close(out_fd);
181                                 goto close;
182                         }
183                         info_file = stdout;
184                 }
185         }
186
187         if (bytes == 0)
188                 bytes = root->sectorsize;
189
190         bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
191         bytes *= root->sectorsize;
192
193         while (bytes > 0) {
194                 eb = debug_read_block(root, logical, root->sectorsize, copy);
195                 if (eb && output_file) {
196                         ret = write(out_fd, eb->data, eb->len);
197                         if (ret < 0 || ret != eb->len) {
198                                 ret = 1;
199                                 fprintf(stderr, "output file write failed\n");
200                                 goto out_close_fd;
201                         }
202                 }
203                 free_extent_buffer(eb);
204                 logical += root->sectorsize;
205                 bytes -= root->sectorsize;
206         }
207
208 out_close_fd:
209         if (output_file && out_fd != 1)
210                 close(out_fd);
211 close:
212         close_ctree(root);
213         return ret;
214 }