Add basic RAID[56] support
[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 struct extent_buffer *debug_read_block(struct btrfs_root *root, u64 bytenr,
41                                      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                 BUG_ON(ret);
61                 device = multi->stripes[0].dev;
62                 eb->fd = device->fd;
63                 device->total_ios++;
64                 eb->dev_bytenr = multi->stripes[0].physical;
65
66                 fprintf(info_file, "mirror %d logical %Lu physical %Lu "
67                         "device %s\n", mirror_num, (unsigned long long)bytenr,
68                         (unsigned long long)eb->dev_bytenr, device->name);
69                 kfree(multi);
70
71                 if (!copy || mirror_num == copy)
72                         ret = read_extent_from_disk(eb, 0, eb->len);
73
74                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
75                                               eb->start, eb->len);
76                 if (num_copies == 1)
77                         break;
78
79                 mirror_num++;
80                 if (mirror_num > num_copies)
81                         break;
82         }
83         return eb;
84 }
85
86 static void print_usage(void)
87 {
88         fprintf(stderr, "usage: btrfs-map-logical [options] device\n");
89         fprintf(stderr, "\t-l Logical extent to map\n");
90         fprintf(stderr, "\t-c Copy of the extent to read (usually 1 or 2)\n");
91         fprintf(stderr, "\t-o Output file to hold the extent\n");
92         fprintf(stderr, "\t-b Number of bytes to read\n");
93         exit(1);
94 }
95
96 static struct option long_options[] = {
97         /* { "byte-count", 1, NULL, 'b' }, */
98         { "logical", 1, NULL, 'l' },
99         { "copy", 1, NULL, 'c' },
100         { "output", 1, NULL, 'o' },
101         { "bytes", 1, NULL, 'b' },
102         { 0, 0, 0, 0}
103 };
104
105 int main(int ac, char **av)
106 {
107         struct cache_tree root_cache;
108         struct btrfs_root *root;
109         struct extent_buffer *eb;
110         char *dev;
111         char *output_file = NULL;
112         u64 logical = 0;
113         int ret = 0;
114         int option_index = 0;
115         int copy = 0;
116         u64 bytes = 0;
117         int out_fd = 0;
118         int err;
119
120         while(1) {
121                 int c;
122                 c = getopt_long(ac, av, "l:c:o:b:", long_options,
123                                 &option_index);
124                 if (c < 0)
125                         break;
126                 switch(c) {
127                         case 'l':
128                                 logical = atoll(optarg);
129                                 if (logical == 0) {
130                                         fprintf(stderr,
131                                                 "invalid extent number\n");
132                                         print_usage();
133                                 }
134                                 break;
135                         case 'c':
136                                 copy = atoi(optarg);
137                                 if (copy == 0) {
138                                         fprintf(stderr,
139                                                 "invalid copy number\n");
140                                         print_usage();
141                                 }
142                                 break;
143                         case 'b':
144                                 bytes = atoll(optarg);
145                                 if (bytes == 0) {
146                                         fprintf(stderr,
147                                                 "invalid byte count\n");
148                                         print_usage();
149                                 }
150                                 break;
151                         case 'o':
152                                 output_file = strdup(optarg);
153                                 break;
154                         default:
155                                 print_usage();
156                 }
157         }
158         ac = ac - optind;
159         if (ac == 0)
160                 print_usage();
161         if (logical == 0)
162                 print_usage();
163         if (copy < 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                         err = ftruncate(out_fd, 0);
187                         if (err) {
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                         err = write(out_fd, eb->data, eb->len);
205                         if (err < 0 || err != eb->len) {
206                                 fprintf(stderr, "output file write failed\n");
207                                 goto out_close_fd;
208                         }
209                 }
210                 free_extent_buffer(eb);
211                 logical += root->sectorsize;
212                 bytes -= root->sectorsize;
213         }
214
215 out_close_fd:
216         if (output_file && out_fd != 1)
217                 close(out_fd);
218 close:
219         close_ctree(root);
220         return ret;
221 }