Btrfs-progs: cleanup similar code in open_ctree_* and close_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         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);
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);
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                            (unsigned long)btrfs_header_chunk_tree_uuid(eb),
113                            BTRFS_UUID_SIZE);
114
115         return fs_info->chunk_root;
116 out_chunk:
117         free_extent_buffer(fs_info->chunk_root->node);
118         btrfs_cleanup_all_caches(fs_info);
119 out_devices:
120         btrfs_close_devices(fs_info->fs_devices);
121 out:
122         btrfs_free_fs_info(fs_info);
123         return NULL;
124 }
125
126 static int search_iobuf(struct btrfs_root *root, void *iobuf,
127                         size_t iobuf_size, off_t offset)
128 {
129         u64 gen = btrfs_super_generation(root->fs_info->super_copy);
130         u64 objectid = search_objectid;
131         u32 size = btrfs_super_nodesize(root->fs_info->super_copy);
132         u8 level = root->fs_info->super_copy->root_level;
133         size_t block_off = 0;
134
135         while (block_off < iobuf_size) {
136                 void *block = iobuf + block_off;
137                 struct btrfs_header *header = block;
138                 u64 h_byte, h_level, h_gen, h_owner;
139
140 //              printf("searching %Lu\n", offset + block_off);
141                 h_byte = le64_to_cpu(header->bytenr);
142                 h_owner = le64_to_cpu(header->owner);
143                 h_level = header->level;
144                 h_gen = le64_to_cpu(header->generation);
145
146                 if (h_owner != objectid)
147                         goto next;
148                 if (h_byte != (offset + block_off))
149                         goto next;
150                 if (h_level != level)
151                         goto next;
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\n", h_byte, h_gen,
162                                 gen);
163                         goto next;
164                 }
165                 printf("Found tree root at %Lu\n", h_byte);
166                 return 0;
167 next:
168                 block_off += size;
169         }
170
171         return 1;
172 }
173
174 static int read_physical(struct btrfs_root *root, int fd, u64 offset,
175                          u64 bytenr, u64 len)
176 {
177         char *iobuf = malloc(len);
178         ssize_t done;
179         size_t total_read = 0;
180         int ret = 1;
181
182         if (!iobuf) {
183                 fprintf(stderr, "No memory\n");
184                 return -1;
185         }
186
187         while (total_read < len) {
188                 done = pread64(fd, iobuf + total_read, len - total_read,
189                                bytenr + total_read);
190                 if (done < 0) {
191                         fprintf(stderr, "Failed to read: %s\n",
192                                 strerror(errno));
193                         ret = -1;
194                         goto out;
195                 }
196                 total_read += done;
197         }
198
199         ret = search_iobuf(root, iobuf, total_read, offset);
200 out:
201         free(iobuf);
202         return ret;
203 }
204
205 static int find_root(struct btrfs_root *root)
206 {
207         struct btrfs_multi_bio *multi = NULL;
208         struct btrfs_device *device;
209         u64 metadata_offset = 0, metadata_size = 0;
210         off_t offset = 0;
211         off_t bytenr;
212         int fd;
213         int err;
214         int ret = 1;
215
216         printf("Super think's the tree root is at %Lu, chunk root %Lu\n",
217                btrfs_super_root(root->fs_info->super_copy),
218                btrfs_super_chunk_root(root->fs_info->super_copy));
219
220         err = btrfs_next_metadata(&root->fs_info->mapping_tree,
221                                   &metadata_offset, &metadata_size);
222         if (err)
223                 return ret;
224
225         offset = metadata_offset;
226         while (1) {
227                 u64 map_length = 4096;
228                 u64 type;
229
230                 if (offset >
231                     btrfs_super_total_bytes(root->fs_info->super_copy)) {
232                         printf("Went past the fs size, exiting");
233                         break;
234                 }
235                 if (offset >= (metadata_offset + metadata_size)) {
236                         err = btrfs_next_metadata(&root->fs_info->mapping_tree,
237                                                   &metadata_offset,
238                                                   &metadata_size);
239                         if (err) {
240                                 printf("No more metdata to scan, exiting\n");
241                                 break;
242                         }
243                         offset = metadata_offset;
244                 }
245                 err = __btrfs_map_block(&root->fs_info->mapping_tree, READ,
246                                       offset, &map_length, &type,
247                                       &multi, 0, NULL);
248                 if (err) {
249                         offset += map_length;
250                         continue;
251                 }
252
253                 if (!(type & BTRFS_BLOCK_GROUP_METADATA)) {
254                         offset += map_length;
255                         kfree(multi);
256                         continue;
257                 }
258
259                 device = multi->stripes[0].dev;
260                 fd = device->fd;
261                 bytenr = multi->stripes[0].physical;
262                 kfree(multi);
263
264                 err = read_physical(root, fd, offset, bytenr, map_length);
265                 if (!err) {
266                         ret = 0;
267                         break;
268                 } else if (err < 0) {
269                         ret = err;
270                         break;
271                 }
272                 offset += map_length;
273         }
274         return ret;
275 }
276
277 int main(int argc, char **argv)
278 {
279         struct btrfs_root *root;
280         int dev_fd;
281         int opt;
282         int ret;
283
284         while ((opt = getopt(argc, argv, "o:")) != -1) {
285                 switch(opt) {
286                         case 'o':
287                                 errno = 0;
288                                 search_objectid = (u64)strtoll(optarg, NULL,
289                                                                10);
290                                 if (errno) {
291                                         fprintf(stderr, "Error parsing "
292                                                 "objectid\n");
293                                         exit(1);
294                                 }
295                                 break;
296                         default:
297                                 usage();
298                                 exit(1);
299                 }
300         }
301
302         if (optind >= argc) {
303                 usage();
304                 exit(1);
305         }
306
307         dev_fd = open(argv[optind], O_RDONLY);
308         if (dev_fd < 0) {
309                 fprintf(stderr, "Failed to open device %s\n", argv[optind]);
310                 exit(1);
311         }
312
313         root = open_ctree_broken(dev_fd, argv[optind]);
314         close(dev_fd);
315
316         if (!root) {
317                 fprintf(stderr, "Open ctree failed\n");
318                 exit(1);
319         }
320
321         csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
322         ret = find_root(root);
323         close_ctree(root);
324         return ret;
325 }