Btrfs progs v4.17.1
[btrfs-progs-unstable/devel.git] / btrfs-map-logical.c
blobcad4afc320115db7c621f30dd2f6a877f3e2c84d
1 /*
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 #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 #include "help.h"
34 #define BUFFER_SIZE SZ_64K
36 /* we write the mirror info to stdout unless they are dumping the data
37 * to stdout
38 * */
39 static FILE *info_file;
41 static int map_one_extent(struct btrfs_fs_info *fs_info,
42 u64 *logical_ret, u64 *len_ret, int search_forward)
44 struct btrfs_path *path;
45 struct btrfs_key key;
46 u64 logical;
47 u64 len = 0;
48 int ret = 0;
50 BUG_ON(!logical_ret);
51 logical = *logical_ret;
53 path = btrfs_alloc_path();
54 if (!path)
55 return -ENOMEM;
57 key.objectid = logical;
58 key.type = 0;
59 key.offset = 0;
61 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path,
62 0, 0);
63 if (ret < 0)
64 goto out;
65 BUG_ON(ret == 0);
66 ret = 0;
68 again:
69 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
70 if ((search_forward && key.objectid < logical) ||
71 (!search_forward && key.objectid > logical) ||
72 (key.type != BTRFS_EXTENT_ITEM_KEY &&
73 key.type != BTRFS_METADATA_ITEM_KEY)) {
74 if (!search_forward)
75 ret = btrfs_previous_extent_item(fs_info->extent_root,
76 path, 0);
77 else
78 ret = btrfs_next_extent_item(fs_info->extent_root,
79 path, 0);
80 if (ret)
81 goto out;
82 goto again;
84 logical = key.objectid;
85 if (key.type == BTRFS_METADATA_ITEM_KEY)
86 len = fs_info->nodesize;
87 else
88 len = key.offset;
90 out:
91 btrfs_free_path(path);
92 if (!ret) {
93 *logical_ret = logical;
94 if (len_ret)
95 *len_ret = len;
97 return ret;
100 static int __print_mapping_info(struct btrfs_fs_info *fs_info, u64 logical,
101 u64 len, int mirror_num)
103 struct btrfs_multi_bio *multi = NULL;
104 u64 cur_offset = 0;
105 u64 cur_len;
106 int ret = 0;
108 while (cur_offset < len) {
109 struct btrfs_device *device;
110 int i;
112 cur_len = len - cur_offset;
113 ret = btrfs_map_block(fs_info, READ, logical + cur_offset,
114 &cur_len, &multi, mirror_num, NULL);
115 if (ret) {
116 fprintf(info_file,
117 "Error: fails to map mirror%d logical %llu: %s\n",
118 mirror_num, logical, strerror(-ret));
119 return ret;
121 for (i = 0; i < multi->num_stripes; i++) {
122 device = multi->stripes[i].dev;
123 fprintf(info_file,
124 "mirror %d logical %Lu physical %Lu device %s\n",
125 mirror_num, logical + cur_offset,
126 multi->stripes[0].physical,
127 device->name);
129 free(multi);
130 multi = NULL;
131 cur_offset += cur_len;
133 return ret;
137 * Logical and len is the exact value of a extent.
138 * And offset is the offset inside the extent. It's only used for case
139 * where user only want to print part of the extent.
141 * Caller *MUST* ensure the range [logical,logical+len) are in one extent.
142 * Or we can encounter the following case, causing a -ENOENT error:
143 * |<-----given parameter------>|
144 * |<------ Extent A ----->|
146 static int print_mapping_info(struct btrfs_fs_info *fs_info, u64 logical,
147 u64 len)
149 int num_copies;
150 int mirror_num;
151 int ret = 0;
153 num_copies = btrfs_num_copies(fs_info, logical, len);
154 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
155 ret = __print_mapping_info(fs_info, logical, len, mirror_num);
156 if (ret < 0)
157 return ret;
159 return ret;
162 /* Same requisition as print_mapping_info function */
163 static int write_extent_content(struct btrfs_fs_info *fs_info, int out_fd,
164 u64 logical, u64 length, int mirror)
166 char buffer[BUFFER_SIZE];
167 u64 cur_offset = 0;
168 u64 cur_len;
169 int ret = 0;
171 while (cur_offset < length) {
172 cur_len = min_t(u64, length - cur_offset, BUFFER_SIZE);
173 ret = read_extent_data(fs_info, buffer,
174 logical + cur_offset, &cur_len, mirror);
175 if (ret < 0) {
176 fprintf(stderr,
177 "Failed to read extent at [%llu, %llu]: %s\n",
178 logical, logical + length, strerror(-ret));
179 return ret;
181 ret = write(out_fd, buffer, cur_len);
182 if (ret < 0 || ret != cur_len) {
183 if (ret > 0)
184 ret = -EINTR;
185 fprintf(stderr, "output file write failed: %s\n",
186 strerror(-ret));
187 return ret;
189 cur_offset += cur_len;
191 return ret;
194 __attribute__((noreturn))
195 static void print_usage(void)
197 printf("usage: btrfs-map-logical [options] device\n");
198 printf("\t-l Logical extent to map\n");
199 printf("\t-c Copy of the extent to read (usually 1 or 2)\n");
200 printf("\t-o Output file to hold the extent\n");
201 printf("\t-b Number of bytes to read\n");
202 exit(1);
205 int main(int argc, char **argv)
207 struct cache_tree root_cache;
208 struct btrfs_root *root;
209 char *dev;
210 char *output_file = NULL;
211 u64 copy = 0;
212 u64 logical = 0;
213 u64 bytes = 0;
214 u64 cur_logical = 0;
215 u64 cur_len = 0;
216 int out_fd = -1;
217 int found = 0;
218 int ret = 0;
220 while(1) {
221 int c;
222 static const struct option long_options[] = {
223 /* { "byte-count", 1, NULL, 'b' }, */
224 { "logical", required_argument, NULL, 'l' },
225 { "copy", required_argument, NULL, 'c' },
226 { "output", required_argument, NULL, 'o' },
227 { "bytes", required_argument, NULL, 'b' },
228 { NULL, 0, NULL, 0}
231 c = getopt_long(argc, argv, "l:c:o:b:", long_options, NULL);
232 if (c < 0)
233 break;
234 switch(c) {
235 case 'l':
236 logical = arg_strtou64(optarg);
237 break;
238 case 'c':
239 copy = arg_strtou64(optarg);
240 break;
241 case 'b':
242 bytes = arg_strtou64(optarg);
243 break;
244 case 'o':
245 output_file = strdup(optarg);
246 break;
247 default:
248 print_usage();
251 set_argv0(argv);
252 if (check_argc_min(argc - optind, 1))
253 print_usage();
254 if (logical == 0)
255 print_usage();
257 dev = argv[optind];
259 radix_tree_init();
260 cache_tree_init(&root_cache);
262 root = open_ctree(dev, 0, 0);
263 if (!root) {
264 fprintf(stderr, "Open ctree failed\n");
265 free(output_file);
266 exit(1);
269 info_file = stdout;
270 if (output_file) {
271 if (strcmp(output_file, "-") == 0) {
272 out_fd = 1;
273 info_file = stderr;
274 } else {
275 out_fd = open(output_file, O_RDWR | O_CREAT, 0600);
276 if (out_fd < 0)
277 goto close;
278 ret = ftruncate(out_fd, 0);
279 if (ret) {
280 ret = 1;
281 close(out_fd);
282 goto close;
284 info_file = stdout;
288 if (bytes == 0)
289 bytes = root->fs_info->nodesize;
290 cur_logical = logical;
291 cur_len = bytes;
293 /* First find the nearest extent */
294 ret = map_one_extent(root->fs_info, &cur_logical, &cur_len, 0);
295 if (ret < 0) {
296 fprintf(stderr, "Failed to find extent at [%llu,%llu): %s\n",
297 cur_logical, cur_logical + cur_len, strerror(-ret));
298 goto out_close_fd;
301 * Normally, search backward should be OK, but for special case like
302 * given logical is quite small where no extents are before it,
303 * we need to search forward.
305 if (ret > 0) {
306 ret = map_one_extent(root->fs_info, &cur_logical, &cur_len, 1);
307 if (ret < 0) {
308 fprintf(stderr,
309 "Failed to find extent at [%llu,%llu): %s\n",
310 cur_logical, cur_logical + cur_len,
311 strerror(-ret));
312 goto out_close_fd;
314 if (ret > 0) {
315 fprintf(stderr,
316 "Failed to find any extent at [%llu,%llu)\n",
317 cur_logical, cur_logical + cur_len);
318 goto out_close_fd;
322 while (cur_logical + cur_len >= logical && cur_logical < logical +
323 bytes) {
324 u64 real_logical;
325 u64 real_len;
327 found = 1;
328 ret = map_one_extent(root->fs_info, &cur_logical, &cur_len, 1);
329 if (ret < 0)
330 goto out_close_fd;
331 if (ret > 0)
332 break;
333 /* check again if there is overlap. */
334 if (cur_logical + cur_len < logical ||
335 cur_logical >= logical + bytes)
336 break;
338 real_logical = max(logical, cur_logical);
339 real_len = min(logical + bytes, cur_logical + cur_len) -
340 real_logical;
342 ret = print_mapping_info(root->fs_info, real_logical, real_len);
343 if (ret < 0)
344 goto out_close_fd;
345 if (output_file && out_fd != -1) {
346 ret = write_extent_content(root->fs_info, out_fd,
347 real_logical, real_len, copy);
348 if (ret < 0)
349 goto out_close_fd;
352 cur_logical += cur_len;
355 if (!found) {
356 fprintf(stderr, "No extent found at range [%llu,%llu)\n",
357 logical, logical + bytes);
359 out_close_fd:
360 if (output_file && out_fd != 1)
361 close(out_fd);
362 close:
363 free(output_file);
364 close_ctree(root);
365 if (ret < 0)
366 ret = 1;
367 btrfs_close_all_devices();
368 return ret;