btrfs-progs: map-logical: introduce map_one_extent 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 struct extent_buffer * debug_read_block(struct btrfs_root *root,
97                 u64 bytenr, u32 blocksize, u64 copy)
98 {
99         int ret;
100         struct extent_buffer *eb;
101         u64 length;
102         struct btrfs_multi_bio *multi = NULL;
103         struct btrfs_device *device;
104         int num_copies;
105         int mirror_num = 1;
106
107         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
108         if (!eb)
109                 return NULL;
110
111         length = blocksize;
112         while (1) {
113                 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
114                                       eb->start, &length, &multi,
115                                       mirror_num, NULL);
116                 if (ret) {
117                         fprintf(info_file,
118                                 "Error: fails to map mirror%d logical %llu: %s\n",
119                                 mirror_num, (unsigned long long)eb->start,
120                                 strerror(-ret));
121                         free_extent_buffer(eb);
122                         return NULL;
123                 }
124                 device = multi->stripes[0].dev;
125                 eb->fd = device->fd;
126                 device->total_ios++;
127                 eb->dev_bytenr = multi->stripes[0].physical;
128
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);
132                 kfree(multi);
133
134                 if (!copy || mirror_num == copy) {
135                         ret = read_extent_from_disk(eb, 0, eb->len);
136                         if (ret) {
137                                 fprintf(info_file,
138                                         "Error: failed to read extent: mirror %d logical %llu: %s\n",
139                                         mirror_num, (unsigned long long)eb->start,
140                                         strerror(-ret));
141                                 free_extent_buffer(eb);
142                                 eb = NULL;
143                                 break;
144                         }
145                 }
146
147                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
148                                               eb->start, eb->len);
149                 if (num_copies == 1)
150                         break;
151
152                 mirror_num++;
153                 if (mirror_num > num_copies)
154                         break;
155         }
156         return eb;
157 }
158
159 static void print_usage(void) __attribute__((noreturn));
160 static void print_usage(void)
161 {
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");
167         exit(1);
168 }
169
170 int main(int ac, char **av)
171 {
172         struct cache_tree root_cache;
173         struct btrfs_root *root;
174         struct extent_buffer *eb;
175         char *dev;
176         char *output_file = NULL;
177         u64 logical = 0;
178         int ret = 0;
179         u64 copy = 0;
180         u64 bytes = 0;
181         int out_fd = 0;
182
183         while(1) {
184                 int c;
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' },
191                         { NULL, 0, NULL, 0}
192                 };
193
194                 c = getopt_long(ac, av, "l:c:o:b:", long_options, NULL);
195                 if (c < 0)
196                         break;
197                 switch(c) {
198                         case 'l':
199                                 logical = arg_strtou64(optarg);
200                                 break;
201                         case 'c':
202                                 copy = arg_strtou64(optarg);
203                                 break;
204                         case 'b':
205                                 bytes = arg_strtou64(optarg);
206                                 break;
207                         case 'o':
208                                 output_file = strdup(optarg);
209                                 break;
210                         default:
211                                 print_usage();
212                 }
213         }
214         set_argv0(av);
215         ac = ac - optind;
216         if (check_argc_min(ac, 1))
217                 print_usage();
218         if (logical == 0)
219                 print_usage();
220
221         dev = av[optind];
222
223         radix_tree_init();
224         cache_tree_init(&root_cache);
225
226         root = open_ctree(dev, 0, 0);
227         if (!root) {
228                 fprintf(stderr, "Open ctree failed\n");
229                 exit(1);
230         }
231
232         info_file = stdout;
233         if (output_file) {
234                 if (strcmp(output_file, "-") == 0) {
235                         out_fd = 1;
236                         info_file = stderr;
237                 } else {
238                         out_fd = open(output_file, O_RDWR | O_CREAT, 0600);
239                         if (out_fd < 0)
240                                 goto close;
241                         ret = ftruncate(out_fd, 0);
242                         if (ret) {
243                                 ret = 1;
244                                 close(out_fd);
245                                 goto close;
246                         }
247                         info_file = stdout;
248                 }
249         }
250
251         if (bytes == 0)
252                 bytes = root->sectorsize;
253
254         bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
255         bytes *= root->sectorsize;
256
257         while (bytes > 0) {
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) {
262                                 ret = 1;
263                                 fprintf(stderr, "output file write failed\n");
264                                 goto out_close_fd;
265                         }
266                 }
267                 free_extent_buffer(eb);
268                 logical += root->sectorsize;
269                 bytes -= root->sectorsize;
270         }
271
272 out_close_fd:
273         if (output_file && out_fd != 1)
274                 close(out_fd);
275 close:
276         close_ctree(root);
277         return ret;
278 }