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 #include "cmds-inspect-dump-tree.h"
36 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
45 if (btrfs_is_leaf(eb)) {
46 btrfs_print_leaf(root, eb);
50 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
51 nr = btrfs_header_nritems(eb);
52 for (i = 0; i < nr; i++) {
53 struct extent_buffer *next = read_tree_block(root,
54 btrfs_node_blockptr(eb, i),
56 btrfs_node_ptr_generation(eb, i));
57 if (!extent_buffer_uptodate(next))
59 if (btrfs_is_leaf(next) &&
60 btrfs_header_level(eb) != 1)
62 if (btrfs_header_level(next) !=
63 btrfs_header_level(eb) - 1)
65 print_extents(root, next);
66 free_extent_buffer(next);
70 static void print_old_roots(struct btrfs_super_block *super)
72 struct btrfs_root_backup *backup;
75 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
76 backup = super->super_roots + i;
77 printf("btrfs root backup slot %d\n", i);
78 printf("\ttree root gen %llu block %llu\n",
79 (unsigned long long)btrfs_backup_tree_root_gen(backup),
80 (unsigned long long)btrfs_backup_tree_root(backup));
82 printf("\t\textent root gen %llu block %llu\n",
83 (unsigned long long)btrfs_backup_extent_root_gen(backup),
84 (unsigned long long)btrfs_backup_extent_root(backup));
86 printf("\t\tchunk root gen %llu block %llu\n",
87 (unsigned long long)btrfs_backup_chunk_root_gen(backup),
88 (unsigned long long)btrfs_backup_chunk_root(backup));
90 printf("\t\tdevice root gen %llu block %llu\n",
91 (unsigned long long)btrfs_backup_dev_root_gen(backup),
92 (unsigned long long)btrfs_backup_dev_root(backup));
94 printf("\t\tcsum root gen %llu block %llu\n",
95 (unsigned long long)btrfs_backup_csum_root_gen(backup),
96 (unsigned long long)btrfs_backup_csum_root(backup));
98 printf("\t\tfs root gen %llu block %llu\n",
99 (unsigned long long)btrfs_backup_fs_root_gen(backup),
100 (unsigned long long)btrfs_backup_fs_root(backup));
102 printf("\t\t%llu used %llu total %llu devices\n",
103 (unsigned long long)btrfs_backup_bytes_used(backup),
104 (unsigned long long)btrfs_backup_total_bytes(backup),
105 (unsigned long long)btrfs_backup_num_devices(backup));
109 const char * const cmd_inspect_dump_tree_usage[] = {
110 "btrfs inspect-internal dump-tree [options] device",
111 "Dump structures from a device",
112 "-e|--extents print detailed extents info",
113 "-d|--device print info of btrfs device and root tree dir only",
114 "-r|--roots print info of roots only",
115 "-R|--backups print info of roots and root backups",
116 "-u|--uuid print info of uuid tree only",
117 "-b|--block <block_num> print info of the specified block only",
118 "-t|--tree <tree_id> print only the tree with the given id",
122 int cmd_inspect_dump_tree(int argc, char **argv)
124 struct btrfs_root *root;
125 struct btrfs_fs_info *info;
126 struct btrfs_path path;
127 struct btrfs_key key;
128 struct btrfs_root_item ri;
129 struct extent_buffer *leaf;
130 struct btrfs_disk_key disk_key;
131 struct btrfs_key found_key;
132 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
137 int uuid_tree_only = 0;
139 int root_backups = 0;
141 struct btrfs_root *tree_root_scan;
146 static const struct option long_options[] = {
147 { "extents", no_argument, NULL, 'e'},
148 { "device", no_argument, NULL, 'd'},
149 { "roots", no_argument, NULL, 'r'},
150 { "backups", no_argument, NULL, 'R'},
151 { "uuid", no_argument, NULL, 'u'},
152 { "block", required_argument, NULL, 'b'},
153 { "tree", required_argument, NULL, 't'},
157 c = getopt_long(argc, argv, "deb:rRut:", long_options, NULL);
178 block_only = arg_strtou64(optarg);
181 tree_id = arg_strtou64(optarg);
184 usage(cmd_inspect_dump_tree_usage);
188 if (check_argc_exact(argc - optind, 1))
189 usage(cmd_inspect_dump_tree_usage);
191 ret = check_arg_type(argv[optind]);
192 if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {
193 error("not a block device or regular file: %s", argv[optind]);
197 info = open_ctree_fs_info(argv[optind], 0, 0, 0, OPEN_CTREE_PARTIAL);
199 error("unable to open %s", argv[optind]);
203 root = info->fs_root;
205 error("unable to open %s", argv[optind]);
210 leaf = read_tree_block(root,
214 if (extent_buffer_uptodate(leaf) &&
215 btrfs_header_level(leaf) != 0) {
216 free_extent_buffer(leaf);
221 leaf = read_tree_block(root,
225 if (!extent_buffer_uptodate(leaf)) {
226 error("failed to read %llu",
227 (unsigned long long)block_only);
230 btrfs_print_tree(root, leaf, 0);
231 free_extent_buffer(leaf);
235 if (!(extent_only || uuid_tree_only || tree_id)) {
237 printf("root tree: %llu level %d\n",
238 (unsigned long long)info->tree_root->node->start,
239 btrfs_header_level(info->tree_root->node));
240 printf("chunk tree: %llu level %d\n",
241 (unsigned long long)info->chunk_root->node->start,
242 btrfs_header_level(info->chunk_root->node));
244 if (info->tree_root->node) {
245 printf("root tree\n");
246 btrfs_print_tree(info->tree_root,
247 info->tree_root->node, 1);
250 if (info->chunk_root->node) {
251 printf("chunk tree\n");
252 btrfs_print_tree(info->chunk_root,
253 info->chunk_root->node, 1);
257 tree_root_scan = info->tree_root;
259 btrfs_init_path(&path);
261 if (!extent_buffer_uptodate(tree_root_scan->node))
265 * Tree's that are not pointed by the tree of tree roots
267 if (tree_id && tree_id == BTRFS_ROOT_TREE_OBJECTID) {
268 if (!info->tree_root->node) {
269 error("cannot print root tree, invalid pointer");
272 printf("root tree\n");
273 btrfs_print_tree(info->tree_root, info->tree_root->node, 1);
277 if (tree_id && tree_id == BTRFS_CHUNK_TREE_OBJECTID) {
278 if (!info->chunk_root->node) {
279 error("cannot print chunk tree, invalid pointer");
282 printf("chunk tree\n");
283 btrfs_print_tree(info->chunk_root, info->chunk_root->node, 1);
289 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
290 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
293 leaf = path.nodes[0];
294 slot = path.slots[0];
295 if (slot >= btrfs_header_nritems(leaf)) {
296 ret = btrfs_next_leaf(tree_root_scan, &path);
299 leaf = path.nodes[0];
300 slot = path.slots[0];
302 btrfs_item_key(leaf, &disk_key, path.slots[0]);
303 btrfs_disk_key_to_cpu(&found_key, &disk_key);
304 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
305 unsigned long offset;
306 struct extent_buffer *buf;
307 int skip = extent_only | device_only | uuid_tree_only;
309 offset = btrfs_item_ptr_offset(leaf, slot);
310 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
311 buf = read_tree_block(tree_root_scan,
312 btrfs_root_bytenr(&ri),
313 btrfs_level_size(tree_root_scan,
314 btrfs_root_level(&ri)),
316 if (!extent_buffer_uptodate(buf))
318 if (tree_id && found_key.objectid != tree_id) {
319 free_extent_buffer(buf);
323 switch (found_key.objectid) {
324 case BTRFS_ROOT_TREE_OBJECTID:
328 case BTRFS_EXTENT_TREE_OBJECTID:
329 if (!device_only && !uuid_tree_only)
334 case BTRFS_CHUNK_TREE_OBJECTID:
339 case BTRFS_DEV_TREE_OBJECTID:
345 case BTRFS_FS_TREE_OBJECTID:
350 case BTRFS_ROOT_TREE_DIR_OBJECTID:
354 case BTRFS_CSUM_TREE_OBJECTID:
359 case BTRFS_ORPHAN_OBJECTID:
364 case BTRFS_TREE_LOG_OBJECTID:
369 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
374 case BTRFS_TREE_RELOC_OBJECTID:
379 case BTRFS_DATA_RELOC_TREE_OBJECTID:
381 printf("data reloc");
384 case BTRFS_EXTENT_CSUM_OBJECTID:
386 printf("extent checksum");
389 case BTRFS_QUOTA_TREE_OBJECTID:
394 case BTRFS_UUID_TREE_OBJECTID:
395 if (!extent_only && !device_only)
400 case BTRFS_FREE_SPACE_TREE_OBJECTID:
402 printf("free space");
404 case BTRFS_MULTIPLE_OBJECTIDS:
414 if (extent_only && !skip) {
415 print_extents(tree_root_scan, buf);
418 btrfs_print_key(&disk_key);
420 printf(" %llu level %d\n",
421 (unsigned long long)buf->start,
422 btrfs_header_level(buf));
425 btrfs_print_tree(tree_root_scan, buf, 1);
428 free_extent_buffer(buf);
434 btrfs_release_path(&path);
436 if (tree_root_scan == info->tree_root &&
437 info->log_root_tree) {
438 tree_root_scan = info->log_root_tree;
442 if (extent_only || device_only || uuid_tree_only)
446 print_old_roots(info->super_copy);
448 printf("total bytes %llu\n",
449 (unsigned long long)btrfs_super_total_bytes(info->super_copy));
450 printf("bytes used %llu\n",
451 (unsigned long long)btrfs_super_bytes_used(info->super_copy));
452 uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
453 uuid_unparse(info->super_copy->fsid, uuidbuf);
454 printf("uuid %s\n", uuidbuf);
455 printf("%s\n", PACKAGE_STRING);
457 ret = close_ctree(root);