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