2 * Copyright (C) 2007 Oracle. 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.
22 #include <uuid/uuid.h>
23 #include "kerncompat.h"
24 #include "radix-tree.h"
27 #include "print-tree.h"
28 #include "transaction.h"
31 static int print_usage(void)
33 fprintf(stderr, "usage: debug-tree [ -e ] device\n");
34 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
38 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
47 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
48 nr = btrfs_header_nritems(eb);
49 for (i = 0; i < nr; i++) {
50 struct extent_buffer *next = read_tree_block(root,
51 btrfs_node_blockptr(eb, i),
53 btrfs_node_ptr_generation(eb, i));
54 if (btrfs_is_leaf(next) &&
55 btrfs_header_level(eb) != 1)
57 if (btrfs_header_level(next) !=
58 btrfs_header_level(eb) - 1)
60 print_extents(root, next);
61 free_extent_buffer(next);
65 static void print_old_roots(struct btrfs_super_block *super)
67 struct btrfs_root_backup *backup;
70 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
71 backup = super->super_roots + i;
72 printf("btrfs root backup slot %d\n", i);
73 printf("\ttree root gen %llu block %llu\n",
74 (unsigned long long)btrfs_backup_tree_root_gen(backup),
75 (unsigned long long)btrfs_backup_tree_root(backup));
77 printf("\t\textent root gen %llu block %llu\n",
78 (unsigned long long)btrfs_backup_extent_root_gen(backup),
79 (unsigned long long)btrfs_backup_extent_root(backup));
81 printf("\t\tchunk root gen %llu block %llu\n",
82 (unsigned long long)btrfs_backup_chunk_root_gen(backup),
83 (unsigned long long)btrfs_backup_chunk_root(backup));
85 printf("\t\tdevice root gen %llu block %llu\n",
86 (unsigned long long)btrfs_backup_dev_root_gen(backup),
87 (unsigned long long)btrfs_backup_dev_root(backup));
89 printf("\t\tcsum root gen %llu block %llu\n",
90 (unsigned long long)btrfs_backup_csum_root_gen(backup),
91 (unsigned long long)btrfs_backup_csum_root(backup));
93 printf("\t\tfs root gen %llu block %llu\n",
94 (unsigned long long)btrfs_backup_fs_root_gen(backup),
95 (unsigned long long)btrfs_backup_fs_root(backup));
97 printf("\t\t%llu used %llu total %llu devices\n",
98 (unsigned long long)btrfs_backup_bytes_used(backup),
99 (unsigned long long)btrfs_backup_total_bytes(backup),
100 (unsigned long long)btrfs_backup_num_devices(backup));
104 int main(int ac, char **av)
106 struct btrfs_root *root;
107 struct btrfs_fs_info *info;
108 struct btrfs_path path;
109 struct btrfs_key key;
110 struct btrfs_root_item ri;
111 struct extent_buffer *leaf;
112 struct btrfs_disk_key disk_key;
113 struct btrfs_key found_key;
120 int root_backups = 0;
122 struct btrfs_root *tree_root_scan;
128 c = getopt(ac, av, "deb:rR");
146 block_only = atoll(optarg);
156 info = open_ctree_fs_info(av[optind], 0, 0, 1);
158 fprintf(stderr, "unable to open %s\n", av[optind]);
161 root = info->fs_root;
165 fprintf(stderr, "unable to open %s\n", av[optind]);
168 leaf = read_tree_block(root,
172 if (leaf && btrfs_header_level(leaf) != 0) {
173 free_extent_buffer(leaf);
178 leaf = read_tree_block(root,
183 fprintf(stderr, "failed to read %llu\n",
184 (unsigned long long)block_only);
187 btrfs_print_tree(root, leaf, 0);
193 printf("root tree: %llu level %d\n",
194 (unsigned long long)info->tree_root->node->start,
195 btrfs_header_level(info->tree_root->node));
196 printf("chunk tree: %llu level %d\n",
197 (unsigned long long)info->chunk_root->node->start,
198 btrfs_header_level(info->chunk_root->node));
200 if (info->tree_root->node) {
201 printf("root tree\n");
202 btrfs_print_tree(info->tree_root,
203 info->tree_root->node, 1);
206 if (info->chunk_root->node) {
207 printf("chunk tree\n");
208 btrfs_print_tree(info->chunk_root,
209 info->chunk_root->node, 1);
213 tree_root_scan = info->tree_root;
215 btrfs_init_path(&path);
217 if (!extent_buffer_uptodate(tree_root_scan->node))
222 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
223 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
226 leaf = path.nodes[0];
227 slot = path.slots[0];
228 if (slot >= btrfs_header_nritems(leaf)) {
229 ret = btrfs_next_leaf(tree_root_scan, &path);
232 leaf = path.nodes[0];
233 slot = path.slots[0];
235 btrfs_item_key(leaf, &disk_key, path.slots[0]);
236 btrfs_disk_key_to_cpu(&found_key, &disk_key);
237 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
238 unsigned long offset;
239 struct extent_buffer *buf;
240 int skip = extent_only | device_only;
242 offset = btrfs_item_ptr_offset(leaf, slot);
243 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
244 buf = read_tree_block(tree_root_scan,
245 btrfs_root_bytenr(&ri),
246 btrfs_level_size(tree_root_scan,
247 btrfs_root_level(&ri)),
249 if (!extent_buffer_uptodate(buf))
252 switch(found_key.objectid) {
253 case BTRFS_ROOT_TREE_OBJECTID:
257 case BTRFS_EXTENT_TREE_OBJECTID:
260 if (!extent_only && !device_only)
263 case BTRFS_CHUNK_TREE_OBJECTID:
268 case BTRFS_DEV_TREE_OBJECTID:
272 case BTRFS_FS_TREE_OBJECTID:
277 case BTRFS_ROOT_TREE_DIR_OBJECTID:
281 case BTRFS_CSUM_TREE_OBJECTID:
286 case BTRFS_ORPHAN_OBJECTID:
291 case BTRFS_TREE_LOG_OBJECTID:
296 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
301 case BTRFS_TREE_RELOC_OBJECTID:
306 case BTRFS_DATA_RELOC_TREE_OBJECTID:
308 printf("data reloc");
311 case BTRFS_EXTENT_CSUM_OBJECTID:
313 printf("extent checksum");
315 case BTRFS_MULTIPLE_OBJECTIDS:
325 if (extent_only && !skip) {
326 print_extents(tree_root_scan, buf);
329 btrfs_print_key(&disk_key);
331 printf(" %llu level %d\n",
332 (unsigned long long)buf->start,
333 btrfs_header_level(buf));
336 btrfs_print_tree(tree_root_scan, buf, 1);
344 btrfs_release_path(root, &path);
346 if (tree_root_scan == info->tree_root &&
347 info->log_root_tree) {
348 tree_root_scan = info->log_root_tree;
352 if (extent_only || device_only)
356 print_old_roots(&info->super_copy);
358 printf("total bytes %llu\n",
359 (unsigned long long)btrfs_super_total_bytes(&info->super_copy));
360 printf("bytes used %llu\n",
361 (unsigned long long)btrfs_super_bytes_used(&info->super_copy));
363 uuid_unparse(info->super_copy.fsid, uuidbuf);
364 printf("uuid %s\n", uuidbuf);
365 printf("%s\n", BTRFS_BUILD_VERSION);