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 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);
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);
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 (unsigned long)btrfs_header_chunk_tree_uuid(eb),
115 return fs_info->chunk_root;
117 free_extent_buffer(fs_info->chunk_root->node);
118 btrfs_cleanup_all_caches(fs_info);
120 btrfs_close_devices(fs_info->fs_devices);
122 btrfs_free_fs_info(fs_info);
126 static int search_iobuf(struct btrfs_root *root, void *iobuf,
127 size_t iobuf_size, off_t offset)
129 u64 gen = btrfs_super_generation(root->fs_info->super_copy);
130 u64 objectid = search_objectid;
131 u32 size = btrfs_super_nodesize(root->fs_info->super_copy);
132 u8 level = root->fs_info->super_copy->root_level;
133 size_t block_off = 0;
135 while (block_off < iobuf_size) {
136 void *block = iobuf + block_off;
137 struct btrfs_header *header = block;
138 u64 h_byte, h_level, h_gen, h_owner;
140 // printf("searching %Lu\n", offset + block_off);
141 h_byte = le64_to_cpu(header->bytenr);
142 h_owner = le64_to_cpu(header->owner);
143 h_level = header->level;
144 h_gen = le64_to_cpu(header->generation);
146 if (h_owner != objectid)
148 if (h_byte != (offset + block_off))
150 if (h_level != level)
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\n", h_byte, h_gen,
165 printf("Found tree root at %Lu\n", h_byte);
174 static int read_physical(struct btrfs_root *root, int fd, u64 offset,
177 char *iobuf = malloc(len);
179 size_t total_read = 0;
183 fprintf(stderr, "No memory\n");
187 while (total_read < len) {
188 done = pread64(fd, iobuf + total_read, len - total_read,
189 bytenr + total_read);
191 fprintf(stderr, "Failed to read: %s\n",
199 ret = search_iobuf(root, iobuf, total_read, offset);
205 static int find_root(struct btrfs_root *root)
207 struct btrfs_multi_bio *multi = NULL;
208 struct btrfs_device *device;
209 u64 metadata_offset = 0, metadata_size = 0;
216 printf("Super think's the tree root is at %Lu, chunk root %Lu\n",
217 btrfs_super_root(root->fs_info->super_copy),
218 btrfs_super_chunk_root(root->fs_info->super_copy));
220 err = btrfs_next_metadata(&root->fs_info->mapping_tree,
221 &metadata_offset, &metadata_size);
225 offset = metadata_offset;
227 u64 map_length = 4096;
231 btrfs_super_total_bytes(root->fs_info->super_copy)) {
232 printf("Went past the fs size, exiting");
235 if (offset >= (metadata_offset + metadata_size)) {
236 err = btrfs_next_metadata(&root->fs_info->mapping_tree,
240 printf("No more metdata to scan, exiting\n");
243 offset = metadata_offset;
245 err = __btrfs_map_block(&root->fs_info->mapping_tree, READ,
246 offset, &map_length, &type,
249 offset += map_length;
253 if (!(type & BTRFS_BLOCK_GROUP_METADATA)) {
254 offset += map_length;
259 device = multi->stripes[0].dev;
261 bytenr = multi->stripes[0].physical;
264 err = read_physical(root, fd, offset, bytenr, map_length);
268 } else if (err < 0) {
272 offset += map_length;
277 int main(int argc, char **argv)
279 struct btrfs_root *root;
284 while ((opt = getopt(argc, argv, "o:")) != -1) {
288 search_objectid = (u64)strtoll(optarg, NULL,
291 fprintf(stderr, "Error parsing "
302 if (optind >= argc) {
307 dev_fd = open(argv[optind], O_RDONLY);
309 fprintf(stderr, "Failed to open device %s\n", argv[optind]);
313 root = open_ctree_broken(dev_fd, argv[optind]);
317 fprintf(stderr, "Open ctree failed\n");
321 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
322 ret = find_root(root);