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