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>
25 #include "kerncompat.h"
26 #include "radix-tree.h"
29 #include "print-tree.h"
30 #include "transaction.h"
34 static int print_usage(int ret)
36 fprintf(stderr, "usage: btrfs-debug-tree [-e] [-d] [-r] [-R] [-u]\n");
37 fprintf(stderr, " [-b block_num ] device\n");
38 fprintf(stderr, "\t-e : print detailed extents info\n");
39 fprintf(stderr, "\t-d : print info of btrfs device and root tree dirs"
41 fprintf(stderr, "\t-r : print info of roots only\n");
42 fprintf(stderr, "\t-R : print info of roots and root backups\n");
43 fprintf(stderr, "\t-u : print info of uuid tree only\n");
44 fprintf(stderr, "\t-b block_num : print info of the specified block"
47 "\t-t tree_id : print only the tree with the given id\n");
48 fprintf(stderr, "%s\n", PACKAGE_STRING);
52 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
61 if (btrfs_is_leaf(eb)) {
62 btrfs_print_leaf(root, eb);
66 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
67 nr = btrfs_header_nritems(eb);
68 for (i = 0; i < nr; i++) {
69 struct extent_buffer *next = read_tree_block(root,
70 btrfs_node_blockptr(eb, i),
72 btrfs_node_ptr_generation(eb, i));
73 if (!extent_buffer_uptodate(next))
75 if (btrfs_is_leaf(next) &&
76 btrfs_header_level(eb) != 1)
78 if (btrfs_header_level(next) !=
79 btrfs_header_level(eb) - 1)
81 print_extents(root, next);
82 free_extent_buffer(next);
86 static void print_old_roots(struct btrfs_super_block *super)
88 struct btrfs_root_backup *backup;
91 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
92 backup = super->super_roots + i;
93 printf("btrfs root backup slot %d\n", i);
94 printf("\ttree root gen %llu block %llu\n",
95 (unsigned long long)btrfs_backup_tree_root_gen(backup),
96 (unsigned long long)btrfs_backup_tree_root(backup));
98 printf("\t\textent root gen %llu block %llu\n",
99 (unsigned long long)btrfs_backup_extent_root_gen(backup),
100 (unsigned long long)btrfs_backup_extent_root(backup));
102 printf("\t\tchunk root gen %llu block %llu\n",
103 (unsigned long long)btrfs_backup_chunk_root_gen(backup),
104 (unsigned long long)btrfs_backup_chunk_root(backup));
106 printf("\t\tdevice root gen %llu block %llu\n",
107 (unsigned long long)btrfs_backup_dev_root_gen(backup),
108 (unsigned long long)btrfs_backup_dev_root(backup));
110 printf("\t\tcsum root gen %llu block %llu\n",
111 (unsigned long long)btrfs_backup_csum_root_gen(backup),
112 (unsigned long long)btrfs_backup_csum_root(backup));
114 printf("\t\tfs root gen %llu block %llu\n",
115 (unsigned long long)btrfs_backup_fs_root_gen(backup),
116 (unsigned long long)btrfs_backup_fs_root(backup));
118 printf("\t\t%llu used %llu total %llu devices\n",
119 (unsigned long long)btrfs_backup_bytes_used(backup),
120 (unsigned long long)btrfs_backup_total_bytes(backup),
121 (unsigned long long)btrfs_backup_num_devices(backup));
125 int main(int ac, char **av)
127 struct btrfs_root *root;
128 struct btrfs_fs_info *info;
129 struct btrfs_path path;
130 struct btrfs_key key;
131 struct btrfs_root_item ri;
132 struct extent_buffer *leaf;
133 struct btrfs_disk_key disk_key;
134 struct btrfs_key found_key;
135 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
140 int uuid_tree_only = 0;
142 int root_backups = 0;
144 struct btrfs_root *tree_root_scan;
151 static const struct option long_options[] = {
152 { "help", no_argument, NULL, GETOPT_VAL_HELP},
156 c = getopt_long(ac, av, "deb:rRut:", long_options, NULL);
177 block_only = arg_strtou64(optarg);
180 tree_id = arg_strtou64(optarg);
182 case GETOPT_VAL_HELP:
184 print_usage(c != GETOPT_VAL_HELP);
189 if (check_argc_exact(ac, 1))
192 ret = check_arg_type(av[optind]);
193 if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {
194 fprintf(stderr, "'%s' is not a block device or regular file\n",
199 info = open_ctree_fs_info(av[optind], 0, 0, OPEN_CTREE_PARTIAL);
201 fprintf(stderr, "unable to open %s\n", av[optind]);
205 root = info->fs_root;
207 fprintf(stderr, "unable to open %s\n", av[optind]);
212 leaf = read_tree_block(root,
216 if (extent_buffer_uptodate(leaf) &&
217 btrfs_header_level(leaf) != 0) {
218 free_extent_buffer(leaf);
223 leaf = read_tree_block(root,
227 if (!extent_buffer_uptodate(leaf)) {
228 fprintf(stderr, "failed to read %llu\n",
229 (unsigned long long)block_only);
232 btrfs_print_tree(root, leaf, 0);
233 free_extent_buffer(leaf);
237 if (!(extent_only || uuid_tree_only || tree_id)) {
239 printf("root tree: %llu level %d\n",
240 (unsigned long long)info->tree_root->node->start,
241 btrfs_header_level(info->tree_root->node));
242 printf("chunk tree: %llu level %d\n",
243 (unsigned long long)info->chunk_root->node->start,
244 btrfs_header_level(info->chunk_root->node));
246 if (info->tree_root->node) {
247 printf("root tree\n");
248 btrfs_print_tree(info->tree_root,
249 info->tree_root->node, 1);
252 if (info->chunk_root->node) {
253 printf("chunk tree\n");
254 btrfs_print_tree(info->chunk_root,
255 info->chunk_root->node, 1);
259 tree_root_scan = info->tree_root;
261 btrfs_init_path(&path);
263 if (!extent_buffer_uptodate(tree_root_scan->node))
268 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
269 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
272 leaf = path.nodes[0];
273 slot = path.slots[0];
274 if (slot >= btrfs_header_nritems(leaf)) {
275 ret = btrfs_next_leaf(tree_root_scan, &path);
278 leaf = path.nodes[0];
279 slot = path.slots[0];
281 btrfs_item_key(leaf, &disk_key, path.slots[0]);
282 btrfs_disk_key_to_cpu(&found_key, &disk_key);
283 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
284 unsigned long offset;
285 struct extent_buffer *buf;
286 int skip = extent_only | device_only | uuid_tree_only;
288 offset = btrfs_item_ptr_offset(leaf, slot);
289 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
290 buf = read_tree_block(tree_root_scan,
291 btrfs_root_bytenr(&ri),
292 btrfs_level_size(tree_root_scan,
293 btrfs_root_level(&ri)),
295 if (!extent_buffer_uptodate(buf))
297 if (tree_id && found_key.objectid != tree_id) {
298 free_extent_buffer(buf);
302 switch(found_key.objectid) {
303 case BTRFS_ROOT_TREE_OBJECTID:
307 case BTRFS_EXTENT_TREE_OBJECTID:
308 if (!device_only && !uuid_tree_only)
313 case BTRFS_CHUNK_TREE_OBJECTID:
318 case BTRFS_DEV_TREE_OBJECTID:
324 case BTRFS_FS_TREE_OBJECTID:
329 case BTRFS_ROOT_TREE_DIR_OBJECTID:
333 case BTRFS_CSUM_TREE_OBJECTID:
338 case BTRFS_ORPHAN_OBJECTID:
343 case BTRFS_TREE_LOG_OBJECTID:
348 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
353 case BTRFS_TREE_RELOC_OBJECTID:
358 case BTRFS_DATA_RELOC_TREE_OBJECTID:
360 printf("data reloc");
363 case BTRFS_EXTENT_CSUM_OBJECTID:
365 printf("extent checksum");
368 case BTRFS_QUOTA_TREE_OBJECTID:
373 case BTRFS_UUID_TREE_OBJECTID:
374 if (!extent_only && !device_only)
379 case BTRFS_MULTIPLE_OBJECTIDS:
389 if (extent_only && !skip) {
390 print_extents(tree_root_scan, buf);
393 btrfs_print_key(&disk_key);
395 printf(" %llu level %d\n",
396 (unsigned long long)buf->start,
397 btrfs_header_level(buf));
400 btrfs_print_tree(tree_root_scan, buf, 1);
403 free_extent_buffer(buf);
409 btrfs_release_path(&path);
411 if (tree_root_scan == info->tree_root &&
412 info->log_root_tree) {
413 tree_root_scan = info->log_root_tree;
417 if (extent_only || device_only || uuid_tree_only)
421 print_old_roots(info->super_copy);
423 printf("total bytes %llu\n",
424 (unsigned long long)btrfs_super_total_bytes(info->super_copy));
425 printf("bytes used %llu\n",
426 (unsigned long long)btrfs_super_bytes_used(info->super_copy));
427 uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
428 uuid_unparse(info->super_copy->fsid, uuidbuf);
429 printf("uuid %s\n", uuidbuf);
430 printf("%s\n", PACKAGE_STRING);
432 ret = close_ctree(root);
433 btrfs_close_all_devices();