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