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.
25 #include "kerncompat.h"
28 #include "print-tree.h"
29 #include "transaction.h"
34 #include "extent-cache.h"
35 #include "find-root.h"
37 static u16 csum_size = 0;
38 static u64 search_objectid = BTRFS_ROOT_TREE_OBJECTID;
39 static u64 search_generation = 0;
40 static unsigned long search_level = 0;
42 static void usage(void)
44 fprintf(stderr, "Usage: find-roots [-o search_objectid] "
45 "[ -g search_generation ] [ -l search_level ] <device>\n");
48 static int csum_block(void *buf, u32 len)
54 result = malloc(csum_size * sizeof(char));
56 fprintf(stderr, "No memory\n");
60 len -= BTRFS_CSUM_SIZE;
61 crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len);
62 btrfs_csum_final(crc, result);
64 if (memcmp(buf, result, csum_size))
70 static struct btrfs_root *open_ctree_broken(int fd, const char *device)
72 struct btrfs_fs_info *fs_info;
73 struct btrfs_super_block *disk_super;
74 struct btrfs_fs_devices *fs_devices = NULL;
75 struct extent_buffer *eb;
78 fs_info = btrfs_new_fs_info(0, BTRFS_SUPER_INFO_OFFSET);
80 fprintf(stderr, "Failed to allocate memory for fs_info\n");
84 ret = btrfs_scan_fs_devices(fd, device, &fs_devices, 0, 1, 0);
88 fs_info->fs_devices = fs_devices;
90 ret = btrfs_open_devices(fs_devices, O_RDONLY);
94 disk_super = fs_info->super_copy;
95 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
96 disk_super, fs_info->super_bytenr, 1);
98 printk("No valid btrfs found\n");
102 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
104 ret = btrfs_check_fs_compatibility(disk_super, 0);
108 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
112 eb = fs_info->chunk_root->node;
113 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
114 btrfs_header_chunk_tree_uuid(eb), BTRFS_UUID_SIZE);
116 return fs_info->chunk_root;
118 free_extent_buffer(fs_info->chunk_root->node);
119 btrfs_cleanup_all_caches(fs_info);
121 btrfs_close_devices(fs_info->fs_devices);
123 btrfs_free_fs_info(fs_info);
127 static int search_iobuf(struct btrfs_root *root, void *iobuf,
128 size_t iobuf_size, off_t offset)
130 u64 gen = search_generation;
131 u64 objectid = search_objectid;
132 u32 size = btrfs_super_nodesize(root->fs_info->super_copy);
133 u8 level = search_level;
134 size_t block_off = 0;
136 while (block_off < iobuf_size) {
137 void *block = iobuf + block_off;
138 struct btrfs_header *header = block;
139 u64 h_byte, h_level, h_gen, h_owner;
141 // printf("searching %Lu\n", offset + block_off);
142 h_byte = btrfs_stack_header_bytenr(header);
143 h_owner = btrfs_stack_header_owner(header);
144 h_level = header->level;
145 h_gen = btrfs_stack_header_generation(header);
147 if (h_owner != objectid)
149 if (h_byte != (offset + block_off))
154 if (csum_block(block, size)) {
155 fprintf(stderr, "Well block %Lu seems good, "
156 "but the csum doesn't match\n",
161 fprintf(stderr, "Well block %Lu seems great, "
162 "but generation doesn't match, "
163 "have=%Lu, want=%Lu level %Lu\n", h_byte,
164 h_gen, gen, h_level);
167 printf("Found tree root at %Lu gen %Lu level %Lu\n", h_byte,
177 static int read_physical(struct btrfs_root *root, int fd, u64 offset,
180 char *iobuf = malloc(len);
182 size_t total_read = 0;
186 fprintf(stderr, "No memory\n");
190 while (total_read < len) {
191 done = pread64(fd, iobuf + total_read, len - total_read,
192 bytenr + total_read);
194 fprintf(stderr, "Failed to read: %s\n",
202 ret = search_iobuf(root, iobuf, total_read, offset);
208 static int find_root(struct btrfs_root *root)
210 struct btrfs_multi_bio *multi = NULL;
211 struct btrfs_device *device;
212 u64 metadata_offset = 0, metadata_size = 0;
219 printf("Super think's the tree root is at %Lu, chunk root %Lu\n",
220 btrfs_super_root(root->fs_info->super_copy),
221 btrfs_super_chunk_root(root->fs_info->super_copy));
223 err = btrfs_next_metadata(&root->fs_info->mapping_tree,
224 &metadata_offset, &metadata_size);
228 offset = metadata_offset;
230 u64 map_length = 4096;
234 btrfs_super_total_bytes(root->fs_info->super_copy)) {
235 printf("Went past the fs size, exiting");
238 if (offset >= (metadata_offset + metadata_size)) {
239 err = btrfs_next_metadata(&root->fs_info->mapping_tree,
243 printf("No more metdata to scan, exiting\n");
246 offset = metadata_offset;
248 err = __btrfs_map_block(&root->fs_info->mapping_tree, READ,
249 offset, &map_length, &type,
252 offset += map_length;
256 if (!(type & BTRFS_BLOCK_GROUP_METADATA)) {
257 offset += map_length;
262 device = multi->stripes[0].dev;
264 bytenr = multi->stripes[0].physical;
267 err = read_physical(root, fd, offset, bytenr, map_length);
271 } else if (err < 0) {
275 offset += map_length;
281 * Get reliable generation and level for given root.
283 * We have two sources of gen/level: superblock and tree root.
284 * superblock include the following level:
286 * and the following generations:
288 * Other gen/leven can only be read from its btrfs_tree_root if possible.
290 * Currently we only believe things from superblock.
292 static void get_root_gen_and_level(u64 objectid, struct btrfs_fs_info *fs_info,
293 u64 *ret_gen, u8 *ret_level)
295 struct btrfs_super_block *super = fs_info->super_copy;
300 case BTRFS_ROOT_TREE_OBJECTID:
301 level = btrfs_super_root_level(super);
302 gen = btrfs_super_generation(super);
304 case BTRFS_CHUNK_TREE_OBJECTID:
305 level = btrfs_super_chunk_root_level(super);
306 gen = btrfs_super_chunk_root_generation(super);
307 printf("Search for chunk root is not supported yet\n");
309 case BTRFS_TREE_LOG_OBJECTID:
310 level = btrfs_super_log_root_level(super);
311 gen = btrfs_super_log_root_transid(super);
313 case BTRFS_UUID_TREE_OBJECTID:
314 gen = btrfs_super_uuid_tree_generation(super);
317 if (gen != (u64)-1) {
318 printf("Superblock thinks the generation is %llu\n", gen);
322 printf("Superblock doesn't contain generation info for root %llu\n",
325 if (level != (u8)-1) {
326 printf("Superblock thinks the level is %u\n", level);
330 printf("Superblock doesn't contain the level info for root %llu\n",
335 static void print_one_result(struct cache_extent *tree_block,
336 u8 level, u64 generation,
337 struct btrfs_find_root_filter *filter)
341 if (filter->match_gen == (u64)-1 || filter->match_level == (u8)-1)
343 printf("Well block %llu(gen: %llu level: %u) seems good, ",
344 tree_block->start, generation, level);
346 printf("but we are unsure about the correct generation/level\n");
348 printf("but generation/level doesn't match, want gen: %llu level: %u\n",
349 filter->match_gen, filter->match_level);
352 static void print_find_root_result(struct cache_tree *result,
353 struct btrfs_find_root_filter *filter)
355 struct btrfs_find_root_gen_cache *gen_cache;
356 struct cache_extent *cache;
357 struct cache_extent *tree_block;
361 for (cache = last_cache_extent(result);
362 cache; cache = prev_cache_extent(cache)) {
363 gen_cache = container_of(cache,
364 struct btrfs_find_root_gen_cache, cache);
365 level = gen_cache->highest_level;
366 generation = cache->start;
367 if (level == filter->match_level &&
368 generation == filter->match_gen)
370 for (tree_block = last_cache_extent(&gen_cache->eb_tree);
371 tree_block; tree_block = prev_cache_extent(tree_block))
372 print_one_result(tree_block, level, generation, filter);
376 int main(int argc, char **argv)
378 struct btrfs_root *root;
379 struct btrfs_find_root_filter filter = {0};
380 struct cache_tree result;
381 struct cache_extent *found;
385 /* Default to search root tree */
386 filter.objectid = BTRFS_ROOT_TREE_OBJECTID;
387 filter.match_gen = (u64)-1;
388 filter.match_level = (u8)-1;
389 while ((opt = getopt(argc, argv, "l:o:g:")) != -1) {
392 filter.objectid = arg_strtou64(optarg);
395 filter.generation = arg_strtou64(optarg);
398 filter.level = arg_strtou64(optarg);
407 argc = argc - optind;
408 if (check_argc_min(argc, 1)) {
413 root = open_ctree(argv[optind], 0, OPEN_CTREE_CHUNK_ROOT_ONLY);
415 fprintf(stderr, "Open ctree failed\n");
418 cache_tree_init(&result);
420 get_root_gen_and_level(filter.objectid, root->fs_info,
421 &filter.match_gen, &filter.match_level);
422 ret = btrfs_find_root_search(root, &filter, &result, &found);
424 fprintf(stderr, "Fail to search the tree root: %s\n",
429 printf("Found tree root at %llu gen %llu level %u\n",
430 found->start, filter.match_gen, filter.match_level);
433 print_find_root_result(&result, &filter);
435 btrfs_find_root_free(&result);