2 * Copyright (C) 2011 Red Hat. All rights reserved.
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.
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.
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.
19 #define _XOPEN_SOURCE 500
27 #include "kerncompat.h"
30 #include "print-tree.h"
31 #include "transaction.h"
38 static u16 csum_size = 0;
39 static u64 search_objectid = BTRFS_ROOT_TREE_OBJECTID;
43 fprintf(stderr, "Usage: find-roots [-o search_objectid] <device>\n");
46 int csum_block(void *buf, u32 len)
52 result = malloc(csum_size * sizeof(char));
54 fprintf(stderr, "No memory\n");
58 len -= BTRFS_CSUM_SIZE;
59 crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len);
60 btrfs_csum_final(crc, result);
62 if (memcmp(buf, result, csum_size))
68 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
69 u32 stripesize, struct btrfs_root *root,
70 struct btrfs_fs_info *fs_info, u64 objectid)
73 root->commit_root = NULL;
74 root->sectorsize = sectorsize;
75 root->nodesize = nodesize;
76 root->leafsize = leafsize;
77 root->stripesize = stripesize;
79 root->track_dirty = 0;
81 root->fs_info = fs_info;
82 root->objectid = objectid;
84 root->highest_inode = 0;
85 root->last_inode_alloc = 0;
87 INIT_LIST_HEAD(&root->dirty_list);
88 memset(&root->root_key, 0, sizeof(root->root_key));
89 memset(&root->root_item, 0, sizeof(root->root_item));
90 root->root_key.objectid = objectid;
94 static int close_all_devices(struct btrfs_fs_info *fs_info)
96 struct list_head *list;
97 struct list_head *next;
98 struct btrfs_device *device;
102 list = &fs_info->fs_devices->devices;
103 list_for_each(next, list) {
104 device = list_entry(next, struct btrfs_device, dev_list);
110 static struct btrfs_root *open_ctree_broken(int fd, const char *device)
118 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
119 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
120 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
121 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
122 struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
123 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
125 struct btrfs_super_block *disk_super;
126 struct btrfs_fs_devices *fs_devices = NULL;
130 ret = btrfs_scan_one_device(fd, device, &fs_devices,
131 &total_devs, BTRFS_SUPER_INFO_OFFSET);
134 fprintf(stderr, "No valid Btrfs found on %s\n", device);
138 if (total_devs != 1) {
139 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
144 memset(fs_info, 0, sizeof(*fs_info));
145 fs_info->tree_root = tree_root;
146 fs_info->extent_root = extent_root;
147 fs_info->chunk_root = chunk_root;
148 fs_info->dev_root = dev_root;
149 fs_info->csum_root = csum_root;
151 fs_info->readonly = 1;
153 extent_io_tree_init(&fs_info->extent_cache);
154 extent_io_tree_init(&fs_info->free_space_cache);
155 extent_io_tree_init(&fs_info->block_group_cache);
156 extent_io_tree_init(&fs_info->pinned_extents);
157 extent_io_tree_init(&fs_info->pending_del);
158 extent_io_tree_init(&fs_info->extent_ins);
159 cache_tree_init(&fs_info->fs_root_cache);
161 cache_tree_init(&fs_info->mapping_tree.cache_tree);
163 mutex_init(&fs_info->fs_mutex);
164 fs_info->fs_devices = fs_devices;
165 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
166 INIT_LIST_HEAD(&fs_info->space_info);
168 __setup_root(4096, 4096, 4096, 4096, tree_root,
169 fs_info, BTRFS_ROOT_TREE_OBJECTID);
171 ret = btrfs_open_devices(fs_devices, O_RDONLY);
175 fs_info->super_bytenr = BTRFS_SUPER_INFO_OFFSET;
176 disk_super = &fs_info->super_copy;
177 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
178 disk_super, BTRFS_SUPER_INFO_OFFSET);
180 printk("No valid btrfs found\n");
184 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
187 features = btrfs_super_incompat_flags(disk_super) &
188 ~BTRFS_FEATURE_INCOMPAT_SUPP;
190 printk("couldn't open because of unsupported "
191 "option features (%Lx).\n", features);
195 features = btrfs_super_incompat_flags(disk_super);
196 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
197 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
198 btrfs_set_super_incompat_flags(disk_super, features);
201 nodesize = btrfs_super_nodesize(disk_super);
202 leafsize = btrfs_super_leafsize(disk_super);
203 sectorsize = btrfs_super_sectorsize(disk_super);
204 stripesize = btrfs_super_stripesize(disk_super);
205 tree_root->nodesize = nodesize;
206 tree_root->leafsize = leafsize;
207 tree_root->sectorsize = sectorsize;
208 tree_root->stripesize = stripesize;
210 ret = btrfs_read_sys_array(tree_root);
213 blocksize = btrfs_level_size(tree_root,
214 btrfs_super_chunk_root_level(disk_super));
215 generation = btrfs_super_chunk_root_generation(disk_super);
217 __setup_root(nodesize, leafsize, sectorsize, stripesize,
218 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
220 chunk_root->node = read_tree_block(chunk_root,
221 btrfs_super_chunk_root(disk_super),
222 blocksize, generation);
223 if (!chunk_root->node) {
224 printk("Couldn't read chunk root\n");
228 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
229 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
232 if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
233 ret = btrfs_read_chunk_tree(chunk_root);
238 return fs_info->chunk_root;
240 free_extent_buffer(fs_info->chunk_root->node);
242 close_all_devices(fs_info);
244 extent_io_tree_cleanup(&fs_info->extent_cache);
245 extent_io_tree_cleanup(&fs_info->free_space_cache);
246 extent_io_tree_cleanup(&fs_info->block_group_cache);
247 extent_io_tree_cleanup(&fs_info->pinned_extents);
248 extent_io_tree_cleanup(&fs_info->pending_del);
249 extent_io_tree_cleanup(&fs_info->extent_ins);
260 static int search_iobuf(struct btrfs_root *root, void *iobuf,
261 size_t iobuf_size, off_t offset)
263 u64 gen = btrfs_super_generation(&root->fs_info->super_copy);
264 u64 objectid = search_objectid;
265 u32 size = btrfs_super_nodesize(&root->fs_info->super_copy);
266 u8 level = root->fs_info->super_copy.root_level;
267 size_t block_off = 0;
269 while (block_off < iobuf_size) {
270 void *block = iobuf + block_off;
271 struct btrfs_header *header = block;
272 u64 h_byte, h_level, h_gen, h_owner;
274 // printf("searching %Lu\n", offset + block_off);
275 h_byte = le64_to_cpu(header->bytenr);
276 h_owner = le64_to_cpu(header->owner);
277 h_level = header->level;
278 h_gen = le64_to_cpu(header->generation);
280 if (h_owner != objectid)
282 if (h_byte != (offset + block_off))
284 if (h_level != level)
286 if (csum_block(block, size)) {
287 fprintf(stderr, "Well block %Lu seems good, "
288 "but the csum doesn't match\n",
293 fprintf(stderr, "Well block %Lu seems great, "
294 "but generation doesn't match, "
295 "have=%Lu, want=%Lu\n", h_byte, h_gen,
299 printf("Found tree root at %Lu\n", h_byte);
308 static int read_physical(struct btrfs_root *root, int fd, u64 offset,
311 char *iobuf = malloc(len);
313 size_t total_read = 0;
317 fprintf(stderr, "No memory\n");
321 while (total_read < len) {
322 done = pread64(fd, iobuf + total_read, len - total_read,
323 bytenr + total_read);
325 fprintf(stderr, "Failed to read: %s\n",
333 ret = search_iobuf(root, iobuf, total_read, offset);
339 static int find_root(struct btrfs_root *root)
341 struct btrfs_multi_bio *multi = NULL;
342 struct btrfs_device *device;
343 u64 metadata_offset = 0, metadata_size = 0;
350 printf("Super think's the tree root is at %Lu, chunk root %Lu\n",
351 btrfs_super_root(&root->fs_info->super_copy),
352 btrfs_super_chunk_root(&root->fs_info->super_copy));
354 err = btrfs_next_metadata(&root->fs_info->mapping_tree,
355 &metadata_offset, &metadata_size);
359 offset = metadata_offset;
361 u64 map_length = 4096;
365 btrfs_super_total_bytes(&root->fs_info->super_copy)) {
366 printf("Went past the fs size, exiting");
369 if (offset >= (metadata_offset + metadata_size)) {
370 err = btrfs_next_metadata(&root->fs_info->mapping_tree,
374 printf("No more metdata to scan, exiting\n");
377 offset = metadata_offset;
379 err = __btrfs_map_block(&root->fs_info->mapping_tree, READ,
380 offset, &map_length, &type, &multi, 0);
382 offset += map_length;
386 if (!(type & BTRFS_BLOCK_GROUP_METADATA)) {
387 offset += map_length;
392 device = multi->stripes[0].dev;
394 bytenr = multi->stripes[0].physical;
397 err = read_physical(root, fd, offset, bytenr, map_length);
401 } else if (err < 0) {
405 offset += map_length;
410 int main(int argc, char **argv)
412 struct btrfs_root *root;
417 while ((opt = getopt(argc, argv, "o:")) != -1) {
421 search_objectid = (u64)strtoll(optarg, NULL,
424 fprintf(stderr, "Error parsing "
435 if (optind >= argc) {
440 dev_fd = open(argv[optind], O_RDONLY);
442 fprintf(stderr, "Failed to open device %s\n", argv[optind]);
446 root = open_ctree_broken(dev_fd, argv[optind]);
450 fprintf(stderr, "Open ctree failed\n");
454 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
455 ret = find_root(root);