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