3e1396d3f4406123ebe31712a510907e93ea9ee6
[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 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #include <zlib.h>
27 #include "kerncompat.h"
28 #include "ctree.h"
29 #include "disk-io.h"
30 #include "print-tree.h"
31 #include "transaction.h"
32 #include "list.h"
33 #include "version.h"
34 #include "volumes.h"
35 #include "utils.h"
36 #include "crc32c.h"
37
38 static u16 csum_size = 0;
39 static u64 search_objectid = BTRFS_ROOT_TREE_OBJECTID;
40
41 static void usage()
42 {
43         fprintf(stderr, "Usage: find-roots [-o search_objectid] <device>\n");
44 }
45
46 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 int close_all_devices(struct btrfs_fs_info *fs_info)
69 {
70         struct list_head *list;
71         struct list_head *next;
72         struct btrfs_device *device;
73
74         return 0;
75
76         list = &fs_info->fs_devices->devices;
77         list_for_each(next, list) {
78                 device = list_entry(next, struct btrfs_device, dev_list);
79                 if (device->fd != -1) {
80                         close(device->fd);
81                         device->fd = -1;
82                 }
83         }
84         return 0;
85 }
86
87 static struct btrfs_root *open_ctree_broken(int fd, const char *device)
88 {
89         u32 sectorsize;
90         u32 nodesize;
91         u32 leafsize;
92         u32 blocksize;
93         u32 stripesize;
94         u64 generation;
95         struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
96         struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
97         struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
98         struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
99         struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
100         struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
101         int ret;
102         struct btrfs_super_block *disk_super;
103         struct btrfs_fs_devices *fs_devices = NULL;
104         u64 total_devs;
105         u64 features;
106
107         ret = btrfs_scan_one_device(fd, device, &fs_devices,
108                                     &total_devs, BTRFS_SUPER_INFO_OFFSET);
109
110         if (ret) {
111                 fprintf(stderr, "No valid Btrfs found on %s\n", device);
112                 goto out;
113         }
114
115         if (total_devs != 1) {
116                 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
117                 if (ret)
118                         goto out;
119         }
120
121         memset(fs_info, 0, sizeof(*fs_info));
122         fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
123         fs_info->tree_root = tree_root;
124         fs_info->extent_root = extent_root;
125         fs_info->chunk_root = chunk_root;
126         fs_info->dev_root = dev_root;
127         fs_info->csum_root = csum_root;
128
129         fs_info->readonly = 1;
130
131         extent_io_tree_init(&fs_info->extent_cache);
132         extent_io_tree_init(&fs_info->free_space_cache);
133         extent_io_tree_init(&fs_info->block_group_cache);
134         extent_io_tree_init(&fs_info->pinned_extents);
135         extent_io_tree_init(&fs_info->pending_del);
136         extent_io_tree_init(&fs_info->extent_ins);
137         cache_tree_init(&fs_info->fs_root_cache);
138
139         cache_tree_init(&fs_info->mapping_tree.cache_tree);
140
141         mutex_init(&fs_info->fs_mutex);
142         fs_info->fs_devices = fs_devices;
143         INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
144         INIT_LIST_HEAD(&fs_info->space_info);
145
146         __setup_root(4096, 4096, 4096, 4096, tree_root,
147                      fs_info, BTRFS_ROOT_TREE_OBJECTID);
148
149         ret = btrfs_open_devices(fs_devices, O_RDONLY);
150         if (ret)
151                 goto out_cleanup;
152
153         fs_info->super_bytenr = BTRFS_SUPER_INFO_OFFSET;
154         disk_super = fs_info->super_copy;
155         ret = btrfs_read_dev_super(fs_devices->latest_bdev,
156                                    disk_super, BTRFS_SUPER_INFO_OFFSET);
157         if (ret) {
158                 printk("No valid btrfs found\n");
159                 goto out_devices;
160         }
161
162         memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
163
164
165         features = btrfs_super_incompat_flags(disk_super) &
166                    ~BTRFS_FEATURE_INCOMPAT_SUPP;
167         if (features) {
168                 printk("couldn't open because of unsupported "
169                        "option features (%Lx).\n", features);
170                 goto out_devices;
171         }
172
173         features = btrfs_super_incompat_flags(disk_super);
174         if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
175                 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
176                 btrfs_set_super_incompat_flags(disk_super, features);
177         }
178
179         nodesize = btrfs_super_nodesize(disk_super);
180         leafsize = btrfs_super_leafsize(disk_super);
181         sectorsize = btrfs_super_sectorsize(disk_super);
182         stripesize = btrfs_super_stripesize(disk_super);
183         tree_root->nodesize = nodesize;
184         tree_root->leafsize = leafsize;
185         tree_root->sectorsize = sectorsize;
186         tree_root->stripesize = stripesize;
187
188         ret = btrfs_read_sys_array(tree_root);
189         if (ret)
190                 goto out_devices;
191         blocksize = btrfs_level_size(tree_root,
192                                      btrfs_super_chunk_root_level(disk_super));
193         generation = btrfs_super_chunk_root_generation(disk_super);
194
195         __setup_root(nodesize, leafsize, sectorsize, stripesize,
196                      chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
197
198         chunk_root->node = read_tree_block(chunk_root,
199                                            btrfs_super_chunk_root(disk_super),
200                                            blocksize, generation);
201         if (!chunk_root->node) {
202                 printk("Couldn't read chunk root\n");
203                 goto out_devices;
204         }
205
206         read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
207                  (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
208                  BTRFS_UUID_SIZE);
209
210         if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
211                 ret = btrfs_read_chunk_tree(chunk_root);
212                 if (ret)
213                         goto out_chunk;
214         }
215
216         return fs_info->chunk_root;
217 out_chunk:
218         free_extent_buffer(fs_info->chunk_root->node);
219 out_devices:
220         close_all_devices(fs_info);
221 out_cleanup:
222         extent_io_tree_cleanup(&fs_info->extent_cache);
223         extent_io_tree_cleanup(&fs_info->free_space_cache);
224         extent_io_tree_cleanup(&fs_info->block_group_cache);
225         extent_io_tree_cleanup(&fs_info->pinned_extents);
226         extent_io_tree_cleanup(&fs_info->pending_del);
227         extent_io_tree_cleanup(&fs_info->extent_ins);
228 out:
229         free(tree_root);
230         free(extent_root);
231         free(chunk_root);
232         free(dev_root);
233         free(csum_root);
234         free(fs_info);
235         return NULL;
236 }
237
238 static int search_iobuf(struct btrfs_root *root, void *iobuf,
239                         size_t iobuf_size, off_t offset)
240 {
241         u64 gen = btrfs_super_generation(root->fs_info->super_copy);
242         u64 objectid = search_objectid;
243         u32 size = btrfs_super_nodesize(root->fs_info->super_copy);
244         u8 level = root->fs_info->super_copy->root_level;
245         size_t block_off = 0;
246
247         while (block_off < iobuf_size) {
248                 void *block = iobuf + block_off;
249                 struct btrfs_header *header = block;
250                 u64 h_byte, h_level, h_gen, h_owner;
251
252 //              printf("searching %Lu\n", offset + block_off);
253                 h_byte = le64_to_cpu(header->bytenr);
254                 h_owner = le64_to_cpu(header->owner);
255                 h_level = header->level;
256                 h_gen = le64_to_cpu(header->generation);
257
258                 if (h_owner != objectid)
259                         goto next;
260                 if (h_byte != (offset + block_off))
261                         goto next;
262                 if (h_level != level)
263                         goto next;
264                 if (csum_block(block, size)) {
265                         fprintf(stderr, "Well block %Lu seems good, "
266                                 "but the csum doesn't match\n",
267                                 h_byte);
268                         goto next;
269                 }
270                 if (h_gen != gen) {
271                         fprintf(stderr, "Well block %Lu seems great, "
272                                 "but generation doesn't match, "
273                                 "have=%Lu, want=%Lu\n", h_byte, h_gen,
274                                 gen);
275                         goto next;
276                 }
277                 printf("Found tree root at %Lu\n", h_byte);
278                 return 0;
279 next:
280                 block_off += size;
281         }
282
283         return 1;
284 }
285
286 static int read_physical(struct btrfs_root *root, int fd, u64 offset,
287                          u64 bytenr, u64 len)
288 {
289         char *iobuf = malloc(len);
290         ssize_t done;
291         size_t total_read = 0;
292         int ret = 1;
293
294         if (!iobuf) {
295                 fprintf(stderr, "No memory\n");
296                 return -1;
297         }
298
299         while (total_read < len) {
300                 done = pread64(fd, iobuf + total_read, len - total_read,
301                                bytenr + total_read);
302                 if (done < 0) {
303                         fprintf(stderr, "Failed to read: %s\n",
304                                 strerror(errno));
305                         ret = -1;
306                         goto out;
307                 }
308                 total_read += done;
309         }
310
311         ret = search_iobuf(root, iobuf, total_read, offset);
312 out:
313         free(iobuf);
314         return ret;
315 }
316
317 static int find_root(struct btrfs_root *root)
318 {
319         struct btrfs_multi_bio *multi = NULL;
320         struct btrfs_device *device;
321         u64 metadata_offset = 0, metadata_size = 0;
322         off_t offset = 0;
323         off_t bytenr;
324         int fd;
325         int err;
326         int ret = 1;
327
328         printf("Super think's the tree root is at %Lu, chunk root %Lu\n",
329                btrfs_super_root(root->fs_info->super_copy),
330                btrfs_super_chunk_root(root->fs_info->super_copy));
331
332         err = btrfs_next_metadata(&root->fs_info->mapping_tree,
333                                   &metadata_offset, &metadata_size);
334         if (err)
335                 return ret;
336
337         offset = metadata_offset;
338         while (1) {
339                 u64 map_length = 4096;
340                 u64 type;
341
342                 if (offset >
343                     btrfs_super_total_bytes(root->fs_info->super_copy)) {
344                         printf("Went past the fs size, exiting");
345                         break;
346                 }
347                 if (offset >= (metadata_offset + metadata_size)) {
348                         err = btrfs_next_metadata(&root->fs_info->mapping_tree,
349                                                   &metadata_offset,
350                                                   &metadata_size);
351                         if (err) {
352                                 printf("No more metdata to scan, exiting\n");
353                                 break;
354                         }
355                         offset = metadata_offset;
356                 }
357                 err = __btrfs_map_block(&root->fs_info->mapping_tree, READ,
358                                       offset, &map_length, &type,
359                                       &multi, 0, NULL);
360                 if (err) {
361                         offset += map_length;
362                         continue;
363                 }
364
365                 if (!(type & BTRFS_BLOCK_GROUP_METADATA)) {
366                         offset += map_length;
367                         kfree(multi);
368                         continue;
369                 }
370
371                 device = multi->stripes[0].dev;
372                 fd = device->fd;
373                 bytenr = multi->stripes[0].physical;
374                 kfree(multi);
375
376                 err = read_physical(root, fd, offset, bytenr, map_length);
377                 if (!err) {
378                         ret = 0;
379                         break;
380                 } else if (err < 0) {
381                         ret = err;
382                         break;
383                 }
384                 offset += map_length;
385         }
386         return ret;
387 }
388
389 int main(int argc, char **argv)
390 {
391         struct btrfs_root *root;
392         int dev_fd;
393         int opt;
394         int ret;
395
396         while ((opt = getopt(argc, argv, "o:")) != -1) {
397                 switch(opt) {
398                         case 'o':
399                                 errno = 0;
400                                 search_objectid = (u64)strtoll(optarg, NULL,
401                                                                10);
402                                 if (errno) {
403                                         fprintf(stderr, "Error parsing "
404                                                 "objectid\n");
405                                         exit(1);
406                                 }
407                                 break;
408                         default:
409                                 usage();
410                                 exit(1);
411                 }
412         }
413
414         if (optind >= argc) {
415                 usage();
416                 exit(1);
417         }
418
419         dev_fd = open(argv[optind], O_RDONLY);
420         if (dev_fd < 0) {
421                 fprintf(stderr, "Failed to open device %s\n", argv[optind]);
422                 exit(1);
423         }
424
425         root = open_ctree_broken(dev_fd, argv[optind]);
426         close(dev_fd);
427
428         if (!root) {
429                 fprintf(stderr, "Open ctree failed\n");
430                 exit(1);
431         }
432
433         csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
434         ret = find_root(root);
435         close_ctree(root);
436         return ret;
437 }