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