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"
32 static int print_usage(void)
34 fprintf(stderr, "usage: btrfs-debug-tree [-e] [-d] [-r] [-R] [-u]\n");
35 fprintf(stderr, " [-b block_num ] device\n");
36 fprintf(stderr, "\t-e : print detailed extents info\n");
37 fprintf(stderr, "\t-d : print info of btrfs device and root tree dirs"
39 fprintf(stderr, "\t-r : print info of roots only\n");
40 fprintf(stderr, "\t-R : print info of roots and root backups\n");
41 fprintf(stderr, "\t-u : print info of uuid tree only\n");
42 fprintf(stderr, "\t-b block_num : print info of the specified block"
44 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
48 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
57 if (btrfs_is_leaf(eb)) {
58 btrfs_print_leaf(root, eb);
62 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
63 nr = btrfs_header_nritems(eb);
64 for (i = 0; i < nr; i++) {
65 struct extent_buffer *next = read_tree_block(root,
66 btrfs_node_blockptr(eb, i),
68 btrfs_node_ptr_generation(eb, i));
69 if (btrfs_is_leaf(next) &&
70 btrfs_header_level(eb) != 1)
72 if (btrfs_header_level(next) !=
73 btrfs_header_level(eb) - 1)
75 print_extents(root, next);
76 free_extent_buffer(next);
80 static void print_old_roots(struct btrfs_super_block *super)
82 struct btrfs_root_backup *backup;
85 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
86 backup = super->super_roots + i;
87 printf("btrfs root backup slot %d\n", i);
88 printf("\ttree root gen %llu block %llu\n",
89 (unsigned long long)btrfs_backup_tree_root_gen(backup),
90 (unsigned long long)btrfs_backup_tree_root(backup));
92 printf("\t\textent root gen %llu block %llu\n",
93 (unsigned long long)btrfs_backup_extent_root_gen(backup),
94 (unsigned long long)btrfs_backup_extent_root(backup));
96 printf("\t\tchunk root gen %llu block %llu\n",
97 (unsigned long long)btrfs_backup_chunk_root_gen(backup),
98 (unsigned long long)btrfs_backup_chunk_root(backup));
100 printf("\t\tdevice root gen %llu block %llu\n",
101 (unsigned long long)btrfs_backup_dev_root_gen(backup),
102 (unsigned long long)btrfs_backup_dev_root(backup));
104 printf("\t\tcsum root gen %llu block %llu\n",
105 (unsigned long long)btrfs_backup_csum_root_gen(backup),
106 (unsigned long long)btrfs_backup_csum_root(backup));
108 printf("\t\tfs root gen %llu block %llu\n",
109 (unsigned long long)btrfs_backup_fs_root_gen(backup),
110 (unsigned long long)btrfs_backup_fs_root(backup));
112 printf("\t\t%llu used %llu total %llu devices\n",
113 (unsigned long long)btrfs_backup_bytes_used(backup),
114 (unsigned long long)btrfs_backup_total_bytes(backup),
115 (unsigned long long)btrfs_backup_num_devices(backup));
119 int main(int ac, char **av)
121 struct btrfs_root *root;
122 struct btrfs_fs_info *info;
123 struct btrfs_path path;
124 struct btrfs_key key;
125 struct btrfs_root_item ri;
126 struct extent_buffer *leaf;
127 struct btrfs_disk_key disk_key;
128 struct btrfs_key found_key;
129 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
134 int uuid_tree_only = 0;
136 int root_backups = 0;
138 struct btrfs_root *tree_root_scan;
144 c = getopt(ac, av, "deb:rRu");
165 block_only = atoll(optarg);
175 info = open_ctree_fs_info(av[optind], 0, 0, OPEN_CTREE_PARTIAL);
177 fprintf(stderr, "unable to open %s\n", av[optind]);
180 root = info->fs_root;
184 fprintf(stderr, "unable to open %s\n", av[optind]);
187 leaf = read_tree_block(root,
191 if (leaf && btrfs_header_level(leaf) != 0) {
192 free_extent_buffer(leaf);
197 leaf = read_tree_block(root,
202 fprintf(stderr, "failed to read %llu\n",
203 (unsigned long long)block_only);
206 btrfs_print_tree(root, leaf, 0);
210 if (!(extent_only || uuid_tree_only)) {
212 printf("root tree: %llu level %d\n",
213 (unsigned long long)info->tree_root->node->start,
214 btrfs_header_level(info->tree_root->node));
215 printf("chunk tree: %llu level %d\n",
216 (unsigned long long)info->chunk_root->node->start,
217 btrfs_header_level(info->chunk_root->node));
219 if (info->tree_root->node) {
220 printf("root tree\n");
221 btrfs_print_tree(info->tree_root,
222 info->tree_root->node, 1);
225 if (info->chunk_root->node) {
226 printf("chunk tree\n");
227 btrfs_print_tree(info->chunk_root,
228 info->chunk_root->node, 1);
232 tree_root_scan = info->tree_root;
234 btrfs_init_path(&path);
236 if (!extent_buffer_uptodate(tree_root_scan->node))
241 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
242 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
245 leaf = path.nodes[0];
246 slot = path.slots[0];
247 if (slot >= btrfs_header_nritems(leaf)) {
248 ret = btrfs_next_leaf(tree_root_scan, &path);
251 leaf = path.nodes[0];
252 slot = path.slots[0];
254 btrfs_item_key(leaf, &disk_key, path.slots[0]);
255 btrfs_disk_key_to_cpu(&found_key, &disk_key);
256 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
257 unsigned long offset;
258 struct extent_buffer *buf;
259 int skip = extent_only | device_only | uuid_tree_only;
261 offset = btrfs_item_ptr_offset(leaf, slot);
262 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
263 buf = read_tree_block(tree_root_scan,
264 btrfs_root_bytenr(&ri),
265 btrfs_level_size(tree_root_scan,
266 btrfs_root_level(&ri)),
268 if (!extent_buffer_uptodate(buf))
271 switch(found_key.objectid) {
272 case BTRFS_ROOT_TREE_OBJECTID:
276 case BTRFS_EXTENT_TREE_OBJECTID:
277 if (!device_only && !uuid_tree_only)
282 case BTRFS_CHUNK_TREE_OBJECTID:
287 case BTRFS_DEV_TREE_OBJECTID:
293 case BTRFS_FS_TREE_OBJECTID:
298 case BTRFS_ROOT_TREE_DIR_OBJECTID:
302 case BTRFS_CSUM_TREE_OBJECTID:
307 case BTRFS_ORPHAN_OBJECTID:
312 case BTRFS_TREE_LOG_OBJECTID:
317 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
322 case BTRFS_TREE_RELOC_OBJECTID:
327 case BTRFS_DATA_RELOC_TREE_OBJECTID:
329 printf("data reloc");
332 case BTRFS_EXTENT_CSUM_OBJECTID:
334 printf("extent checksum");
337 case BTRFS_QUOTA_TREE_OBJECTID:
342 case BTRFS_UUID_TREE_OBJECTID:
343 if (!extent_only && !device_only)
348 case BTRFS_MULTIPLE_OBJECTIDS:
358 if (extent_only && !skip) {
359 print_extents(tree_root_scan, buf);
362 btrfs_print_key(&disk_key);
364 printf(" %llu level %d\n",
365 (unsigned long long)buf->start,
366 btrfs_header_level(buf));
369 btrfs_print_tree(tree_root_scan, buf, 1);
372 free_extent_buffer(buf);
378 btrfs_release_path(&path);
380 if (tree_root_scan == info->tree_root &&
381 info->log_root_tree) {
382 tree_root_scan = info->log_root_tree;
386 if (extent_only || device_only || uuid_tree_only)
390 print_old_roots(info->super_copy);
392 printf("total bytes %llu\n",
393 (unsigned long long)btrfs_super_total_bytes(info->super_copy));
394 printf("bytes used %llu\n",
395 (unsigned long long)btrfs_super_bytes_used(info->super_copy));
396 uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
397 uuid_unparse(info->super_copy->fsid, uuidbuf);
398 printf("uuid %s\n", uuidbuf);
399 printf("%s\n", BTRFS_BUILD_VERSION);
401 return close_ctree(root);