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