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"
35 static u16 csum_size = 0;
36 static u64 search_objectid = BTRFS_ROOT_TREE_OBJECTID;
37 static u64 search_generation = 0;
38 static unsigned long search_level = 0;
40 static void usage(void)
42 fprintf(stderr, "Usage: find-roots [-o search_objectid] "
43 "[ -g search_generation ] [ -l search_level ] <device>\n");
46 static 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 struct btrfs_root *open_ctree_broken(int fd, const char *device)
70 struct btrfs_fs_info *fs_info;
71 struct btrfs_super_block *disk_super;
72 struct btrfs_fs_devices *fs_devices = NULL;
73 struct extent_buffer *eb;
76 fs_info = btrfs_new_fs_info(0, BTRFS_SUPER_INFO_OFFSET);
78 fprintf(stderr, "Failed to allocate memory for fs_info\n");
82 ret = btrfs_scan_fs_devices(fd, device, &fs_devices, 0, 1, 0);
86 fs_info->fs_devices = fs_devices;
88 ret = btrfs_open_devices(fs_devices, O_RDONLY);
92 disk_super = fs_info->super_copy;
93 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
94 disk_super, fs_info->super_bytenr, 1);
96 printk("No valid btrfs found\n");
100 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
102 ret = btrfs_check_fs_compatibility(disk_super, 0);
106 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
110 eb = fs_info->chunk_root->node;
111 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
112 btrfs_header_chunk_tree_uuid(eb), BTRFS_UUID_SIZE);
114 return fs_info->chunk_root;
116 free_extent_buffer(fs_info->chunk_root->node);
117 btrfs_cleanup_all_caches(fs_info);
119 btrfs_close_devices(fs_info->fs_devices);
121 btrfs_free_fs_info(fs_info);
125 static int search_iobuf(struct btrfs_root *root, void *iobuf,
126 size_t iobuf_size, off_t offset)
128 u64 gen = search_generation;
129 u64 objectid = search_objectid;
130 u32 size = btrfs_super_nodesize(root->fs_info->super_copy);
131 u8 level = search_level;
132 size_t block_off = 0;
134 while (block_off < iobuf_size) {
135 void *block = iobuf + block_off;
136 struct btrfs_header *header = block;
137 u64 h_byte, h_level, h_gen, h_owner;
139 // printf("searching %Lu\n", offset + block_off);
140 h_byte = btrfs_stack_header_bytenr(header);
141 h_owner = btrfs_stack_header_owner(header);
142 h_level = header->level;
143 h_gen = btrfs_stack_header_generation(header);
145 if (h_owner != objectid)
147 if (h_byte != (offset + block_off))
152 if (csum_block(block, size)) {
153 fprintf(stderr, "Well block %Lu seems good, "
154 "but the csum doesn't match\n",
159 fprintf(stderr, "Well block %Lu seems great, "
160 "but generation doesn't match, "
161 "have=%Lu, want=%Lu level %Lu\n", h_byte,
162 h_gen, gen, h_level);
165 printf("Found tree root at %Lu gen %Lu level %Lu\n", h_byte,
175 static int read_physical(struct btrfs_root *root, int fd, u64 offset,
178 char *iobuf = malloc(len);
180 size_t total_read = 0;
184 fprintf(stderr, "No memory\n");
188 while (total_read < len) {
189 done = pread64(fd, iobuf + total_read, len - total_read,
190 bytenr + total_read);
192 fprintf(stderr, "Failed to read: %s\n",
200 ret = search_iobuf(root, iobuf, total_read, offset);
206 static int find_root(struct btrfs_root *root)
208 struct btrfs_multi_bio *multi = NULL;
209 struct btrfs_device *device;
210 u64 metadata_offset = 0, metadata_size = 0;
217 printf("Super think's the tree root is at %Lu, chunk root %Lu\n",
218 btrfs_super_root(root->fs_info->super_copy),
219 btrfs_super_chunk_root(root->fs_info->super_copy));
221 err = btrfs_next_metadata(&root->fs_info->mapping_tree,
222 &metadata_offset, &metadata_size);
226 offset = metadata_offset;
228 u64 map_length = 4096;
232 btrfs_super_total_bytes(root->fs_info->super_copy)) {
233 printf("Went past the fs size, exiting");
236 if (offset >= (metadata_offset + metadata_size)) {
237 err = btrfs_next_metadata(&root->fs_info->mapping_tree,
241 printf("No more metdata to scan, exiting\n");
244 offset = metadata_offset;
246 err = __btrfs_map_block(&root->fs_info->mapping_tree, READ,
247 offset, &map_length, &type,
250 offset += map_length;
254 if (!(type & BTRFS_BLOCK_GROUP_METADATA)) {
255 offset += map_length;
260 device = multi->stripes[0].dev;
262 bytenr = multi->stripes[0].physical;
265 err = read_physical(root, fd, offset, bytenr, map_length);
269 } else if (err < 0) {
273 offset += map_length;
279 * Get reliable generation and level for given root.
281 * We have two sources of gen/level: superblock and tree root.
282 * superblock include the following level:
284 * and the following generations:
286 * Other gen/leven can only be read from its btrfs_tree_root if possible.
288 * Currently we only believe things from superblock.
290 static void get_root_gen_and_level(u64 objectid, struct btrfs_fs_info *fs_info,
291 u64 *ret_gen, u8 *ret_level)
293 struct btrfs_super_block *super = fs_info->super_copy;
298 case BTRFS_ROOT_TREE_OBJECTID:
299 level = btrfs_super_root_level(super);
300 gen = btrfs_super_generation(super);
302 case BTRFS_CHUNK_TREE_OBJECTID:
303 level = btrfs_super_chunk_root_level(super);
304 gen = btrfs_super_chunk_root_generation(super);
305 printf("Search for chunk root is not supported yet\n");
307 case BTRFS_TREE_LOG_OBJECTID:
308 level = btrfs_super_log_root_level(super);
309 gen = btrfs_super_log_root_transid(super);
311 case BTRFS_UUID_TREE_OBJECTID:
312 gen = btrfs_super_uuid_tree_generation(super);
315 if (gen != (u64)-1) {
316 printf("Superblock thinks the generation is %llu\n", gen);
320 printf("Superblock doesn't contain generation info for root %llu\n",
323 if (level != (u8)-1) {
324 printf("Superblock thinks the level is %u\n", level);
328 printf("Superblock doesn't contain the level info for root %llu\n",
333 int main(int argc, char **argv)
335 struct btrfs_root *root;
339 while ((opt = getopt(argc, argv, "l:o:g:")) != -1) {
342 search_objectid = arg_strtou64(optarg);
345 search_generation = arg_strtou64(optarg);
348 search_level = arg_strtou64(optarg);
357 argc = argc - optind;
358 if (check_argc_min(argc, 1)) {
363 root = open_ctree(argv[optind], 0, OPEN_CTREE_CHUNK_ROOT_ONLY);
365 fprintf(stderr, "Open ctree failed\n");
369 if (search_generation == 0)
370 get_root_gen_and_level(search_objectid, root->fs_info,
371 &search_generation, NULL);
373 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
374 ret = find_root(root);