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"
45 "\t-t tree_id : print only the tree with the given id\n");
46 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
50 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
59 if (btrfs_is_leaf(eb)) {
60 btrfs_print_leaf(root, eb);
64 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
65 nr = btrfs_header_nritems(eb);
66 for (i = 0; i < nr; i++) {
67 struct extent_buffer *next = read_tree_block(root,
68 btrfs_node_blockptr(eb, i),
70 btrfs_node_ptr_generation(eb, i));
71 if (btrfs_is_leaf(next) &&
72 btrfs_header_level(eb) != 1)
74 if (btrfs_header_level(next) !=
75 btrfs_header_level(eb) - 1)
77 print_extents(root, next);
78 free_extent_buffer(next);
82 static void print_old_roots(struct btrfs_super_block *super)
84 struct btrfs_root_backup *backup;
87 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
88 backup = super->super_roots + i;
89 printf("btrfs root backup slot %d\n", i);
90 printf("\ttree root gen %llu block %llu\n",
91 (unsigned long long)btrfs_backup_tree_root_gen(backup),
92 (unsigned long long)btrfs_backup_tree_root(backup));
94 printf("\t\textent root gen %llu block %llu\n",
95 (unsigned long long)btrfs_backup_extent_root_gen(backup),
96 (unsigned long long)btrfs_backup_extent_root(backup));
98 printf("\t\tchunk root gen %llu block %llu\n",
99 (unsigned long long)btrfs_backup_chunk_root_gen(backup),
100 (unsigned long long)btrfs_backup_chunk_root(backup));
102 printf("\t\tdevice root gen %llu block %llu\n",
103 (unsigned long long)btrfs_backup_dev_root_gen(backup),
104 (unsigned long long)btrfs_backup_dev_root(backup));
106 printf("\t\tcsum root gen %llu block %llu\n",
107 (unsigned long long)btrfs_backup_csum_root_gen(backup),
108 (unsigned long long)btrfs_backup_csum_root(backup));
110 printf("\t\tfs root gen %llu block %llu\n",
111 (unsigned long long)btrfs_backup_fs_root_gen(backup),
112 (unsigned long long)btrfs_backup_fs_root(backup));
114 printf("\t\t%llu used %llu total %llu devices\n",
115 (unsigned long long)btrfs_backup_bytes_used(backup),
116 (unsigned long long)btrfs_backup_total_bytes(backup),
117 (unsigned long long)btrfs_backup_num_devices(backup));
121 int main(int ac, char **av)
123 struct btrfs_root *root;
124 struct btrfs_fs_info *info;
125 struct btrfs_path path;
126 struct btrfs_key key;
127 struct btrfs_root_item ri;
128 struct extent_buffer *leaf;
129 struct btrfs_disk_key disk_key;
130 struct btrfs_key found_key;
131 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
136 int uuid_tree_only = 0;
138 int root_backups = 0;
140 struct btrfs_root *tree_root_scan;
147 c = getopt(ac, av, "deb:rRut:");
168 block_only = arg_strtou64(optarg);
171 tree_id = arg_strtou64(optarg);
181 info = open_ctree_fs_info(av[optind], 0, 0, OPEN_CTREE_PARTIAL);
183 fprintf(stderr, "unable to open %s\n", av[optind]);
187 root = info->fs_root;
189 fprintf(stderr, "unable to open %s\n", av[optind]);
194 leaf = read_tree_block(root,
198 if (leaf && btrfs_header_level(leaf) != 0) {
199 free_extent_buffer(leaf);
204 leaf = read_tree_block(root,
209 fprintf(stderr, "failed to read %llu\n",
210 (unsigned long long)block_only);
213 btrfs_print_tree(root, leaf, 0);
217 if (!(extent_only || uuid_tree_only || tree_id)) {
219 printf("root tree: %llu level %d\n",
220 (unsigned long long)info->tree_root->node->start,
221 btrfs_header_level(info->tree_root->node));
222 printf("chunk tree: %llu level %d\n",
223 (unsigned long long)info->chunk_root->node->start,
224 btrfs_header_level(info->chunk_root->node));
226 if (info->tree_root->node) {
227 printf("root tree\n");
228 btrfs_print_tree(info->tree_root,
229 info->tree_root->node, 1);
232 if (info->chunk_root->node) {
233 printf("chunk tree\n");
234 btrfs_print_tree(info->chunk_root,
235 info->chunk_root->node, 1);
239 tree_root_scan = info->tree_root;
241 btrfs_init_path(&path);
243 if (!extent_buffer_uptodate(tree_root_scan->node))
248 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
249 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
252 leaf = path.nodes[0];
253 slot = path.slots[0];
254 if (slot >= btrfs_header_nritems(leaf)) {
255 ret = btrfs_next_leaf(tree_root_scan, &path);
258 leaf = path.nodes[0];
259 slot = path.slots[0];
261 btrfs_item_key(leaf, &disk_key, path.slots[0]);
262 btrfs_disk_key_to_cpu(&found_key, &disk_key);
263 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
264 unsigned long offset;
265 struct extent_buffer *buf;
266 int skip = extent_only | device_only | uuid_tree_only;
268 offset = btrfs_item_ptr_offset(leaf, slot);
269 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
270 buf = read_tree_block(tree_root_scan,
271 btrfs_root_bytenr(&ri),
272 btrfs_level_size(tree_root_scan,
273 btrfs_root_level(&ri)),
275 if (!extent_buffer_uptodate(buf))
277 if (tree_id && found_key.objectid != tree_id)
280 switch(found_key.objectid) {
281 case BTRFS_ROOT_TREE_OBJECTID:
285 case BTRFS_EXTENT_TREE_OBJECTID:
286 if (!device_only && !uuid_tree_only)
291 case BTRFS_CHUNK_TREE_OBJECTID:
296 case BTRFS_DEV_TREE_OBJECTID:
302 case BTRFS_FS_TREE_OBJECTID:
307 case BTRFS_ROOT_TREE_DIR_OBJECTID:
311 case BTRFS_CSUM_TREE_OBJECTID:
316 case BTRFS_ORPHAN_OBJECTID:
321 case BTRFS_TREE_LOG_OBJECTID:
326 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
331 case BTRFS_TREE_RELOC_OBJECTID:
336 case BTRFS_DATA_RELOC_TREE_OBJECTID:
338 printf("data reloc");
341 case BTRFS_EXTENT_CSUM_OBJECTID:
343 printf("extent checksum");
346 case BTRFS_QUOTA_TREE_OBJECTID:
351 case BTRFS_UUID_TREE_OBJECTID:
352 if (!extent_only && !device_only)
357 case BTRFS_MULTIPLE_OBJECTIDS:
367 if (extent_only && !skip) {
368 print_extents(tree_root_scan, buf);
371 btrfs_print_key(&disk_key);
373 printf(" %llu level %d\n",
374 (unsigned long long)buf->start,
375 btrfs_header_level(buf));
378 btrfs_print_tree(tree_root_scan, buf, 1);
381 free_extent_buffer(buf);
387 btrfs_release_path(&path);
389 if (tree_root_scan == info->tree_root &&
390 info->log_root_tree) {
391 tree_root_scan = info->log_root_tree;
395 if (extent_only || device_only || uuid_tree_only)
399 print_old_roots(info->super_copy);
401 printf("total bytes %llu\n",
402 (unsigned long long)btrfs_super_total_bytes(info->super_copy));
403 printf("bytes used %llu\n",
404 (unsigned long long)btrfs_super_bytes_used(info->super_copy));
405 uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
406 uuid_unparse(info->super_copy->fsid, uuidbuf);
407 printf("uuid %s\n", uuidbuf);
408 printf("%s\n", BTRFS_BUILD_VERSION);
410 return close_ctree(root);