btrfs-progs: autoconf: use standard PACKAGE_* macros
[platform/upstream/btrfs-progs.git] / btrfs-find-root.c
1 /*
2  * Copyright (C) 2011 Red Hat.  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 <unistd.h>
22 #include <fcntl.h>
23 #include <sys/stat.h>
24 #include <zlib.h>
25 #include "kerncompat.h"
26 #include "ctree.h"
27 #include "disk-io.h"
28 #include "print-tree.h"
29 #include "transaction.h"
30 #include "list.h"
31 #include "volumes.h"
32 #include "utils.h"
33 #include "crc32c.h"
34
35 static u16 csum_size = 0;
36 static u64 search_objectid = BTRFS_ROOT_TREE_OBJECTID;
37 static u64 search_generation = 0;
38 static unsigned long search_level = 0;
39
40 static void usage(void)
41 {
42         fprintf(stderr, "Usage: find-roots [-o search_objectid] "
43                 "[ -g search_generation ] [ -l search_level ] <device>\n");
44 }
45
46 static int csum_block(void *buf, u32 len)
47 {
48         char *result;
49         u32 crc = ~(u32)0;
50         int ret = 0;
51
52         result = malloc(csum_size * sizeof(char));
53         if (!result) {
54                 fprintf(stderr, "No memory\n");
55                 return 1;
56         }
57
58         len -= BTRFS_CSUM_SIZE;
59         crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len);
60         btrfs_csum_final(crc, result);
61
62         if (memcmp(buf, result, csum_size))
63                 ret = 1;
64         free(result);
65         return ret;
66 }
67
68 static struct btrfs_root *open_ctree_broken(int fd, const char *device)
69 {
70         struct btrfs_fs_info *fs_info;
71         struct btrfs_super_block *disk_super;
72         struct btrfs_fs_devices *fs_devices = NULL;
73         struct extent_buffer *eb;
74         int ret;
75
76         fs_info = btrfs_new_fs_info(0, BTRFS_SUPER_INFO_OFFSET);
77         if (!fs_info) {
78                 fprintf(stderr, "Failed to allocate memory for fs_info\n");
79                 return NULL;
80         }
81
82         ret = btrfs_scan_fs_devices(fd, device, &fs_devices, 0, 1);
83         if (ret)
84                 goto out;
85
86         fs_info->fs_devices = fs_devices;
87
88         ret = btrfs_open_devices(fs_devices, O_RDONLY);
89         if (ret)
90                 goto out_devices;
91
92         disk_super = fs_info->super_copy;
93         ret = btrfs_read_dev_super(fs_devices->latest_bdev,
94                                    disk_super, fs_info->super_bytenr, 1);
95         if (ret) {
96                 printk("No valid btrfs found\n");
97                 goto out_devices;
98         }
99
100         memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
101
102         ret = btrfs_check_fs_compatibility(disk_super, 0);
103         if (ret)
104                 goto out_devices;
105
106         ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
107         if (ret)
108                 goto out_chunk;
109
110         eb = fs_info->chunk_root->node;
111         read_extent_buffer(eb, fs_info->chunk_tree_uuid,
112                            btrfs_header_chunk_tree_uuid(eb), BTRFS_UUID_SIZE);
113
114         return fs_info->chunk_root;
115 out_chunk:
116         free_extent_buffer(fs_info->chunk_root->node);
117         btrfs_cleanup_all_caches(fs_info);
118 out_devices:
119         btrfs_close_devices(fs_info->fs_devices);
120 out:
121         btrfs_free_fs_info(fs_info);
122         return NULL;
123 }
124
125 static int search_iobuf(struct btrfs_root *root, void *iobuf,
126                         size_t iobuf_size, off_t offset)
127 {
128         u64 gen = search_generation;
129         u64 objectid = search_objectid;
130         u32 size = btrfs_super_nodesize(root->fs_info->super_copy);
131         u8 level = search_level;
132         size_t block_off = 0;
133
134         while (block_off < iobuf_size) {
135                 void *block = iobuf + block_off;
136                 struct btrfs_header *header = block;
137                 u64 h_byte, h_level, h_gen, h_owner;
138
139 //              printf("searching %Lu\n", offset + block_off);
140                 h_byte = btrfs_stack_header_bytenr(header);
141                 h_owner = btrfs_stack_header_owner(header);
142                 h_level = header->level;
143                 h_gen = btrfs_stack_header_generation(header);
144
145                 if (h_owner != objectid)
146                         goto next;
147                 if (h_byte != (offset + block_off))
148                         goto next;
149                 if (h_level < level)
150                         goto next;
151                 level = h_level;
152                 if (csum_block(block, size)) {
153                         fprintf(stderr, "Well block %Lu seems good, "
154                                 "but the csum doesn't match\n",
155                                 h_byte);
156                         goto next;
157                 }
158                 if (h_gen != gen) {
159                         fprintf(stderr, "Well block %Lu seems great, "
160                                 "but generation doesn't match, "
161                                 "have=%Lu, want=%Lu level %Lu\n", h_byte,
162                                 h_gen, gen, h_level);
163                         goto next;
164                 }
165                 printf("Found tree root at %Lu gen %Lu level %Lu\n", h_byte,
166                        h_gen, h_level);
167                 return 0;
168 next:
169                 block_off += size;
170         }
171
172         return 1;
173 }
174
175 static int read_physical(struct btrfs_root *root, int fd, u64 offset,
176                          u64 bytenr, u64 len)
177 {
178         char *iobuf = malloc(len);
179         ssize_t done;
180         size_t total_read = 0;
181         int ret = 1;
182
183         if (!iobuf) {
184                 fprintf(stderr, "No memory\n");
185                 return -1;
186         }
187
188         while (total_read < len) {
189                 done = pread64(fd, iobuf + total_read, len - total_read,
190                                bytenr + total_read);
191                 if (done < 0) {
192                         fprintf(stderr, "Failed to read: %s\n",
193                                 strerror(errno));
194                         ret = -1;
195                         goto out;
196                 }
197                 total_read += done;
198         }
199
200         ret = search_iobuf(root, iobuf, total_read, offset);
201 out:
202         free(iobuf);
203         return ret;
204 }
205
206 static int find_root(struct btrfs_root *root)
207 {
208         struct btrfs_multi_bio *multi = NULL;
209         struct btrfs_device *device;
210         u64 metadata_offset = 0, metadata_size = 0;
211         off_t offset = 0;
212         off_t bytenr;
213         int fd;
214         int err;
215         int ret = 1;
216
217         printf("Super think's the tree root is at %Lu, chunk root %Lu\n",
218                btrfs_super_root(root->fs_info->super_copy),
219                btrfs_super_chunk_root(root->fs_info->super_copy));
220
221         err = btrfs_next_metadata(&root->fs_info->mapping_tree,
222                                   &metadata_offset, &metadata_size);
223         if (err)
224                 return ret;
225
226         offset = metadata_offset;
227         while (1) {
228                 u64 map_length = 4096;
229                 u64 type;
230
231                 if (offset >
232                     btrfs_super_total_bytes(root->fs_info->super_copy)) {
233                         printf("Went past the fs size, exiting");
234                         break;
235                 }
236                 if (offset >= (metadata_offset + metadata_size)) {
237                         err = btrfs_next_metadata(&root->fs_info->mapping_tree,
238                                                   &metadata_offset,
239                                                   &metadata_size);
240                         if (err) {
241                                 printf("No more metdata to scan, exiting\n");
242                                 break;
243                         }
244                         offset = metadata_offset;
245                 }
246                 err = __btrfs_map_block(&root->fs_info->mapping_tree, READ,
247                                       offset, &map_length, &type,
248                                       &multi, 0, NULL);
249                 if (err) {
250                         offset += map_length;
251                         continue;
252                 }
253
254                 if (!(type & BTRFS_BLOCK_GROUP_METADATA)) {
255                         offset += map_length;
256                         kfree(multi);
257                         continue;
258                 }
259
260                 device = multi->stripes[0].dev;
261                 fd = device->fd;
262                 bytenr = multi->stripes[0].physical;
263                 kfree(multi);
264
265                 err = read_physical(root, fd, offset, bytenr, map_length);
266                 if (!err) {
267                         ret = 0;
268                         break;
269                 } else if (err < 0) {
270                         ret = err;
271                         break;
272                 }
273                 offset += map_length;
274         }
275         return ret;
276 }
277
278 int main(int argc, char **argv)
279 {
280         struct btrfs_root *root;
281         int dev_fd;
282         int opt;
283         int ret;
284
285         while ((opt = getopt(argc, argv, "l:o:g:")) != -1) {
286                 switch(opt) {
287                         case 'o':
288                                 search_objectid = arg_strtou64(optarg);
289                                 break;
290                         case 'g':
291                                 search_generation = arg_strtou64(optarg);
292                                 break;
293                         case 'l':
294                                 search_level = arg_strtou64(optarg);
295                                 break;
296                         default:
297                                 usage();
298                                 exit(1);
299                 }
300         }
301
302         set_argv0(argv);
303         argc = argc - optind;
304         if (check_argc_min(argc, 1)) {
305                 usage();
306                 exit(1);
307         }
308
309         dev_fd = open(argv[optind], O_RDONLY);
310         if (dev_fd < 0) {
311                 fprintf(stderr, "Failed to open device %s\n", argv[optind]);
312                 exit(1);
313         }
314
315         root = open_ctree_broken(dev_fd, argv[optind]);
316         close(dev_fd);
317
318         if (!root) {
319                 fprintf(stderr, "Open ctree failed\n");
320                 exit(1);
321         }
322
323         if (search_generation == 0)
324                 search_generation = btrfs_super_generation(root->fs_info->super_copy);
325
326         csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
327         ret = find_root(root);
328         close_ctree(root);
329         return ret;
330 }