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