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 close_all_devices(struct btrfs_fs_info *fs_info)
70 struct list_head *list;
71 struct list_head *next;
72 struct btrfs_device *device;
76 list = &fs_info->fs_devices->devices;
77 list_for_each(next, list) {
78 device = list_entry(next, struct btrfs_device, dev_list);
84 static struct btrfs_root *open_ctree_broken(int fd, const char *device)
92 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
93 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
94 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
95 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
96 struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
97 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
99 struct btrfs_super_block *disk_super;
100 struct btrfs_fs_devices *fs_devices = NULL;
104 ret = btrfs_scan_one_device(fd, device, &fs_devices,
105 &total_devs, BTRFS_SUPER_INFO_OFFSET);
108 fprintf(stderr, "No valid Btrfs found on %s\n", device);
112 if (total_devs != 1) {
113 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
118 memset(fs_info, 0, sizeof(*fs_info));
119 fs_info->tree_root = tree_root;
120 fs_info->extent_root = extent_root;
121 fs_info->chunk_root = chunk_root;
122 fs_info->dev_root = dev_root;
123 fs_info->csum_root = csum_root;
125 fs_info->readonly = 1;
127 extent_io_tree_init(&fs_info->extent_cache);
128 extent_io_tree_init(&fs_info->free_space_cache);
129 extent_io_tree_init(&fs_info->block_group_cache);
130 extent_io_tree_init(&fs_info->pinned_extents);
131 extent_io_tree_init(&fs_info->pending_del);
132 extent_io_tree_init(&fs_info->extent_ins);
133 cache_tree_init(&fs_info->fs_root_cache);
135 cache_tree_init(&fs_info->mapping_tree.cache_tree);
137 mutex_init(&fs_info->fs_mutex);
138 fs_info->fs_devices = fs_devices;
139 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
140 INIT_LIST_HEAD(&fs_info->space_info);
142 __setup_root(4096, 4096, 4096, 4096, tree_root,
143 fs_info, BTRFS_ROOT_TREE_OBJECTID);
145 ret = btrfs_open_devices(fs_devices, O_RDONLY);
149 fs_info->super_bytenr = BTRFS_SUPER_INFO_OFFSET;
150 disk_super = &fs_info->super_copy;
151 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
152 disk_super, BTRFS_SUPER_INFO_OFFSET);
154 printk("No valid btrfs found\n");
158 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
161 features = btrfs_super_incompat_flags(disk_super) &
162 ~BTRFS_FEATURE_INCOMPAT_SUPP;
164 printk("couldn't open because of unsupported "
165 "option features (%Lx).\n", features);
169 features = btrfs_super_incompat_flags(disk_super);
170 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
171 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
172 btrfs_set_super_incompat_flags(disk_super, features);
175 nodesize = btrfs_super_nodesize(disk_super);
176 leafsize = btrfs_super_leafsize(disk_super);
177 sectorsize = btrfs_super_sectorsize(disk_super);
178 stripesize = btrfs_super_stripesize(disk_super);
179 tree_root->nodesize = nodesize;
180 tree_root->leafsize = leafsize;
181 tree_root->sectorsize = sectorsize;
182 tree_root->stripesize = stripesize;
184 ret = btrfs_read_sys_array(tree_root);
187 blocksize = btrfs_level_size(tree_root,
188 btrfs_super_chunk_root_level(disk_super));
189 generation = btrfs_super_chunk_root_generation(disk_super);
191 __setup_root(nodesize, leafsize, sectorsize, stripesize,
192 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
194 chunk_root->node = read_tree_block(chunk_root,
195 btrfs_super_chunk_root(disk_super),
196 blocksize, generation);
197 if (!chunk_root->node) {
198 printk("Couldn't read chunk root\n");
202 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
203 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
206 if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
207 ret = btrfs_read_chunk_tree(chunk_root);
212 return fs_info->chunk_root;
214 free_extent_buffer(fs_info->chunk_root->node);
216 close_all_devices(fs_info);
218 extent_io_tree_cleanup(&fs_info->extent_cache);
219 extent_io_tree_cleanup(&fs_info->free_space_cache);
220 extent_io_tree_cleanup(&fs_info->block_group_cache);
221 extent_io_tree_cleanup(&fs_info->pinned_extents);
222 extent_io_tree_cleanup(&fs_info->pending_del);
223 extent_io_tree_cleanup(&fs_info->extent_ins);
234 static int search_iobuf(struct btrfs_root *root, void *iobuf,
235 size_t iobuf_size, off_t offset)
237 u64 gen = btrfs_super_generation(&root->fs_info->super_copy);
238 u64 objectid = search_objectid;
239 u32 size = btrfs_super_nodesize(&root->fs_info->super_copy);
240 u8 level = root->fs_info->super_copy.root_level;
241 size_t block_off = 0;
243 while (block_off < iobuf_size) {
244 void *block = iobuf + block_off;
245 struct btrfs_header *header = block;
246 u64 h_byte, h_level, h_gen, h_owner;
248 // printf("searching %Lu\n", offset + block_off);
249 h_byte = le64_to_cpu(header->bytenr);
250 h_owner = le64_to_cpu(header->owner);
251 h_level = header->level;
252 h_gen = le64_to_cpu(header->generation);
254 if (h_owner != objectid)
256 if (h_byte != (offset + block_off))
258 if (h_level != level)
260 if (csum_block(block, size)) {
261 fprintf(stderr, "Well block %Lu seems good, "
262 "but the csum doesn't match\n",
267 fprintf(stderr, "Well block %Lu seems great, "
268 "but generation doesn't match, "
269 "have=%Lu, want=%Lu\n", h_byte, h_gen,
273 printf("Found tree root at %Lu\n", h_byte);
282 static int read_physical(struct btrfs_root *root, int fd, u64 offset,
285 char *iobuf = malloc(len);
287 size_t total_read = 0;
291 fprintf(stderr, "No memory\n");
295 while (total_read < len) {
296 done = pread64(fd, iobuf + total_read, len - total_read,
297 bytenr + total_read);
299 fprintf(stderr, "Failed to read: %s\n",
307 ret = search_iobuf(root, iobuf, total_read, offset);
313 static int find_root(struct btrfs_root *root)
315 struct btrfs_multi_bio *multi = NULL;
316 struct btrfs_device *device;
317 u64 metadata_offset = 0, metadata_size = 0;
324 printf("Super think's the tree root is at %Lu, chunk root %Lu\n",
325 btrfs_super_root(&root->fs_info->super_copy),
326 btrfs_super_chunk_root(&root->fs_info->super_copy));
328 err = btrfs_next_metadata(&root->fs_info->mapping_tree,
329 &metadata_offset, &metadata_size);
333 offset = metadata_offset;
335 u64 map_length = 4096;
339 btrfs_super_total_bytes(&root->fs_info->super_copy)) {
340 printf("Went past the fs size, exiting");
343 if (offset >= (metadata_offset + metadata_size)) {
344 err = btrfs_next_metadata(&root->fs_info->mapping_tree,
348 printf("No more metdata to scan, exiting\n");
351 offset = metadata_offset;
353 err = __btrfs_map_block(&root->fs_info->mapping_tree, READ,
354 offset, &map_length, &type,
357 offset += map_length;
361 if (!(type & BTRFS_BLOCK_GROUP_METADATA)) {
362 offset += map_length;
367 device = multi->stripes[0].dev;
369 bytenr = multi->stripes[0].physical;
372 err = read_physical(root, fd, offset, bytenr, map_length);
376 } else if (err < 0) {
380 offset += map_length;
385 int main(int argc, char **argv)
387 struct btrfs_root *root;
392 while ((opt = getopt(argc, argv, "o:")) != -1) {
396 search_objectid = (u64)strtoll(optarg, NULL,
399 fprintf(stderr, "Error parsing "
410 if (optind >= argc) {
415 dev_fd = open(argv[optind], O_RDONLY);
417 fprintf(stderr, "Failed to open device %s\n", argv[optind]);
421 root = open_ctree_broken(dev_fd, argv[optind]);
425 fprintf(stderr, "Open ctree failed\n");
429 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
430 ret = find_root(root);