Btrfs-progs: map-logical: introduce print_mapping_info function
[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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include "kerncompat.h"
25 #include "ctree.h"
26 #include "volumes.h"
27 #include "disk-io.h"
28 #include "print-tree.h"
29 #include "transaction.h"
30 #include "list.h"
31 #include "utils.h"
32
33 /* we write the mirror info to stdout unless they are dumping the data
34  * to stdout
35  * */
36 static FILE *info_file;
37
38 static int map_one_extent(struct btrfs_fs_info *fs_info,
39                           u64 *logical_ret, u64 *len_ret, int search_foward)
40 {
41         struct btrfs_path *path;
42         struct btrfs_key key;
43         u64 logical;
44         u64 len = 0;
45         int ret = 0;
46
47         BUG_ON(!logical_ret);
48         logical = *logical_ret;
49
50         path = btrfs_alloc_path();
51         if (!path)
52                 return -ENOMEM;
53
54         key.objectid = logical;
55         key.type = 0;
56         key.offset = 0;
57
58         ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path,
59                                 0, 0);
60         if (ret < 0)
61                 goto out;
62         BUG_ON(ret == 0);
63         ret = 0;
64
65 again:
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)) {
71                 if (!search_foward)
72                         ret = btrfs_previous_extent_item(fs_info->extent_root,
73                                                          path, 0);
74                 else
75                         ret = btrfs_next_item(fs_info->extent_root, path);
76                 if (ret)
77                         goto out;
78                 goto again;
79         }
80         logical = key.objectid;
81         if (key.type == BTRFS_METADATA_ITEM_KEY)
82                 len = fs_info->tree_root->leafsize;
83         else
84                 len = key.offset;
85
86 out:
87         btrfs_free_path(path);
88         if (!ret) {
89                 *logical_ret = logical;
90                 if (len_ret)
91                         *len_ret = len;
92         }
93         return ret;
94 }
95
96 static int __print_mapping_info(struct btrfs_fs_info *fs_info, u64 logical,
97                                 u64 len, int mirror_num)
98 {
99         struct btrfs_multi_bio *multi = NULL;
100         u64 cur_offset = 0;
101         u64 cur_len;
102         int ret = 0;
103
104         while (cur_offset < len) {
105                 struct btrfs_device *device;
106                 int i;
107
108                 cur_len = len - cur_offset;
109                 ret = btrfs_map_block(&fs_info->mapping_tree, READ,
110                                 logical + cur_offset, &cur_len,
111                                 &multi, mirror_num, NULL);
112                 if (ret) {
113                         fprintf(info_file,
114                                 "Error: fails to map mirror%d logical %llu: %s\n",
115                                 mirror_num, logical, strerror(-ret));
116                         return ret;
117                 }
118                 for (i = 0; i < multi->num_stripes; i++) {
119                         device = multi->stripes[i].dev;
120                         fprintf(info_file,
121                                 "mirror %d logical %Lu physical %Lu device %s\n",
122                                 mirror_num, logical + cur_offset,
123                                 multi->stripes[0].physical,
124                                 device->name);
125                 }
126                 kfree(multi);
127                 multi = NULL;
128                 cur_offset += cur_len;
129         }
130         return ret;
131 }
132
133 /*
134  * Logical and len is the exact value of a extent.
135  * And offset is the offset inside the extent. It's only used for case
136  * where user only want to print part of the extent.
137  *
138  * Caller *MUST* ensure the range [logical,logical+len) are in one extent.
139  * Or we can encounter the following case, causing a -ENOENT error:
140  * |<-----given parameter------>|
141  *              |<------ Extent A ----->|
142  */
143 static int print_mapping_info(struct btrfs_fs_info *fs_info, u64 logical,
144                               u64 len)
145 {
146         int num_copies;
147         int mirror_num;
148         int ret = 0;
149
150         num_copies = btrfs_num_copies(&fs_info->mapping_tree, logical, len);
151         for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
152                 ret = __print_mapping_info(fs_info, logical, len, mirror_num);
153                 if (ret < 0)
154                         return ret;
155         }
156         return ret;
157 }
158
159 static struct extent_buffer * debug_read_block(struct btrfs_root *root,
160                 u64 bytenr, u32 blocksize, u64 copy)
161 {
162         int ret;
163         struct extent_buffer *eb;
164         u64 length;
165         struct btrfs_multi_bio *multi = NULL;
166         struct btrfs_device *device;
167         int num_copies;
168         int mirror_num = 1;
169
170         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
171         if (!eb)
172                 return NULL;
173
174         length = blocksize;
175         while (1) {
176                 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
177                                       eb->start, &length, &multi,
178                                       mirror_num, NULL);
179                 if (ret) {
180                         fprintf(info_file,
181                                 "Error: fails to map mirror%d logical %llu: %s\n",
182                                 mirror_num, (unsigned long long)eb->start,
183                                 strerror(-ret));
184                         free_extent_buffer(eb);
185                         return NULL;
186                 }
187                 device = multi->stripes[0].dev;
188                 eb->fd = device->fd;
189                 device->total_ios++;
190                 eb->dev_bytenr = multi->stripes[0].physical;
191
192                 fprintf(info_file, "mirror %d logical %Lu physical %Lu "
193                         "device %s\n", mirror_num, (unsigned long long)bytenr,
194                         (unsigned long long)eb->dev_bytenr, device->name);
195                 kfree(multi);
196
197                 if (!copy || mirror_num == copy) {
198                         ret = read_extent_from_disk(eb, 0, eb->len);
199                         if (ret) {
200                                 fprintf(info_file,
201                                         "Error: failed to read extent: mirror %d logical %llu: %s\n",
202                                         mirror_num, (unsigned long long)eb->start,
203                                         strerror(-ret));
204                                 free_extent_buffer(eb);
205                                 eb = NULL;
206                                 break;
207                         }
208                 }
209
210                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
211                                               eb->start, eb->len);
212                 if (num_copies == 1)
213                         break;
214
215                 mirror_num++;
216                 if (mirror_num > num_copies)
217                         break;
218         }
219         return eb;
220 }
221
222 static void print_usage(void) __attribute__((noreturn));
223 static void print_usage(void)
224 {
225         fprintf(stderr, "usage: btrfs-map-logical [options] device\n");
226         fprintf(stderr, "\t-l Logical extent to map\n");
227         fprintf(stderr, "\t-c Copy of the extent to read (usually 1 or 2)\n");
228         fprintf(stderr, "\t-o Output file to hold the extent\n");
229         fprintf(stderr, "\t-b Number of bytes to read\n");
230         exit(1);
231 }
232
233 int main(int ac, char **av)
234 {
235         struct cache_tree root_cache;
236         struct btrfs_root *root;
237         struct extent_buffer *eb;
238         char *dev;
239         char *output_file = NULL;
240         u64 logical = 0;
241         int ret = 0;
242         u64 copy = 0;
243         u64 bytes = 0;
244         int out_fd = 0;
245
246         while(1) {
247                 int c;
248                 static const struct option long_options[] = {
249                         /* { "byte-count", 1, NULL, 'b' }, */
250                         { "logical", required_argument, NULL, 'l' },
251                         { "copy", required_argument, NULL, 'c' },
252                         { "output", required_argument, NULL, 'o' },
253                         { "bytes", required_argument, NULL, 'b' },
254                         { NULL, 0, NULL, 0}
255                 };
256
257                 c = getopt_long(ac, av, "l:c:o:b:", long_options, NULL);
258                 if (c < 0)
259                         break;
260                 switch(c) {
261                         case 'l':
262                                 logical = arg_strtou64(optarg);
263                                 break;
264                         case 'c':
265                                 copy = arg_strtou64(optarg);
266                                 break;
267                         case 'b':
268                                 bytes = arg_strtou64(optarg);
269                                 break;
270                         case 'o':
271                                 output_file = strdup(optarg);
272                                 break;
273                         default:
274                                 print_usage();
275                 }
276         }
277         set_argv0(av);
278         ac = ac - optind;
279         if (check_argc_min(ac, 1))
280                 print_usage();
281         if (logical == 0)
282                 print_usage();
283
284         dev = av[optind];
285
286         radix_tree_init();
287         cache_tree_init(&root_cache);
288
289         root = open_ctree(dev, 0, 0);
290         if (!root) {
291                 fprintf(stderr, "Open ctree failed\n");
292                 exit(1);
293         }
294
295         info_file = stdout;
296         if (output_file) {
297                 if (strcmp(output_file, "-") == 0) {
298                         out_fd = 1;
299                         info_file = stderr;
300                 } else {
301                         out_fd = open(output_file, O_RDWR | O_CREAT, 0600);
302                         if (out_fd < 0)
303                                 goto close;
304                         ret = ftruncate(out_fd, 0);
305                         if (ret) {
306                                 ret = 1;
307                                 close(out_fd);
308                                 goto close;
309                         }
310                         info_file = stdout;
311                 }
312         }
313
314         if (bytes == 0)
315                 bytes = root->sectorsize;
316
317         bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
318         bytes *= root->sectorsize;
319
320         while (bytes > 0) {
321                 eb = debug_read_block(root, logical, root->sectorsize, copy);
322                 if (eb && output_file) {
323                         ret = write(out_fd, eb->data, eb->len);
324                         if (ret < 0 || ret != eb->len) {
325                                 ret = 1;
326                                 fprintf(stderr, "output file write failed\n");
327                                 goto out_close_fd;
328                         }
329                 }
330                 free_extent_buffer(eb);
331                 logical += root->sectorsize;
332                 bytes -= root->sectorsize;
333         }
334
335 out_close_fd:
336         if (output_file && out_fd != 1)
337                 close(out_fd);
338 close:
339         close_ctree(root);
340         return ret;
341 }