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