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"
37 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
39 struct extent_buffer *next;
47 if (btrfs_is_leaf(eb)) {
48 btrfs_print_leaf(root, eb);
52 size = root->nodesize;
53 nr = btrfs_header_nritems(eb);
54 for (i = 0; i < nr; i++) {
55 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
56 size, btrfs_node_ptr_generation(eb, i));
57 if (!extent_buffer_uptodate(next))
59 if (btrfs_is_leaf(next) && btrfs_header_level(eb) != 1) {
61 "eb corrupted: item %d eb level %d next level %d, skipping the rest",
62 i, btrfs_header_level(next),
63 btrfs_header_level(eb));
66 if (btrfs_header_level(next) != btrfs_header_level(eb) - 1) {
68 "eb corrupted: item %d eb level %d next level %d, skipping the rest",
69 i, btrfs_header_level(next),
70 btrfs_header_level(eb));
73 print_extents(root, next);
74 free_extent_buffer(next);
80 free_extent_buffer(next);
83 static void print_old_roots(struct btrfs_super_block *super)
85 struct btrfs_root_backup *backup;
88 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
89 backup = super->super_roots + i;
90 printf("btrfs root backup slot %d\n", i);
91 printf("\ttree root gen %llu block %llu\n",
92 (unsigned long long)btrfs_backup_tree_root_gen(backup),
93 (unsigned long long)btrfs_backup_tree_root(backup));
95 printf("\t\textent root gen %llu block %llu\n",
96 (unsigned long long)btrfs_backup_extent_root_gen(backup),
97 (unsigned long long)btrfs_backup_extent_root(backup));
99 printf("\t\tchunk root gen %llu block %llu\n",
100 (unsigned long long)btrfs_backup_chunk_root_gen(backup),
101 (unsigned long long)btrfs_backup_chunk_root(backup));
103 printf("\t\tdevice root gen %llu block %llu\n",
104 (unsigned long long)btrfs_backup_dev_root_gen(backup),
105 (unsigned long long)btrfs_backup_dev_root(backup));
107 printf("\t\tcsum root gen %llu block %llu\n",
108 (unsigned long long)btrfs_backup_csum_root_gen(backup),
109 (unsigned long long)btrfs_backup_csum_root(backup));
111 printf("\t\tfs root gen %llu block %llu\n",
112 (unsigned long long)btrfs_backup_fs_root_gen(backup),
113 (unsigned long long)btrfs_backup_fs_root(backup));
115 printf("\t\t%llu used %llu total %llu devices\n",
116 (unsigned long long)btrfs_backup_bytes_used(backup),
117 (unsigned long long)btrfs_backup_total_bytes(backup),
118 (unsigned long long)btrfs_backup_num_devices(backup));
123 * Convert a tree name from various forms to the numerical id if possible
125 * - case does not matter
126 * - same as the key name, BTRFS_ROOT_TREE_OBJECTID
127 * - dtto shortened, BTRFS_ROOT_TREE
128 * - dtto without prefix, ROOT_TREE
129 * - common name, ROOT, CHUNK, EXTENT, ...
130 * - dtto alias, DEVICE for DEV, CHECKSUM for CSUM
132 * Returns 0 if the tree id was not recognized.
134 static u64 treeid_from_string(const char *str, const char **end)
139 static struct treename {
143 { "ROOT", BTRFS_ROOT_TREE_OBJECTID },
144 { "EXTENT", BTRFS_EXTENT_TREE_OBJECTID },
145 { "CHUNK", BTRFS_CHUNK_TREE_OBJECTID },
146 { "DEVICE", BTRFS_DEV_TREE_OBJECTID },
147 { "DEV", BTRFS_DEV_TREE_OBJECTID },
148 { "FS_TREE", BTRFS_FS_TREE_OBJECTID },
149 { "CSUM", BTRFS_CSUM_TREE_OBJECTID },
150 { "CHECKSUM", BTRFS_CSUM_TREE_OBJECTID },
151 { "QUOTA", BTRFS_QUOTA_TREE_OBJECTID },
152 { "UUID", BTRFS_UUID_TREE_OBJECTID },
153 { "FREE_SPACE", BTRFS_FREE_SPACE_TREE_OBJECTID },
154 { "TREE_LOG_FIXUP", BTRFS_TREE_LOG_FIXUP_OBJECTID },
155 { "TREE_LOG", BTRFS_TREE_LOG_OBJECTID },
156 { "TREE_RELOC", BTRFS_TREE_RELOC_OBJECTID },
157 { "DATA_RELOC", BTRFS_DATA_RELOC_TREE_OBJECTID }
160 if (strncasecmp("BTRFS_", str, strlen("BTRFS_")) == 0)
161 str += strlen("BTRFS_");
163 for (i = 0; i < ARRAY_SIZE(tn); i++) {
164 int len = strlen(tn[i].name);
166 if (strncasecmp(tn[i].name, str, len) == 0) {
177 if (strncasecmp("_TREE", str, strlen("_TREE")) == 0)
178 str += strlen("_TREE");
180 if (strncasecmp("_OBJECTID", str, strlen("_OBJECTID")) == 0)
181 str += strlen("_OBJECTID");
188 const char * const cmd_inspect_dump_tree_usage[] = {
189 "btrfs inspect-internal dump-tree [options] device",
190 "Dump tree structures from a given device",
191 "Dump tree structures from a given device in textual form, expand keys to human",
192 "readable equivalents where possible.",
193 "Note: contains file names, consider that if you're asked to send the dump",
196 "-e|--extents print only extent info: extent and device trees",
197 "-d|--device print only device info: tree root, chunk and device trees",
198 "-r|--roots print only short root node info",
199 "-R|--backups same as --roots plus print backup root info",
200 "-u|--uuid print only the uuid tree",
201 "-b|--block <block_num> print info from the specified block only",
202 "-t|--tree <tree_id> print only tree with the given id (string or number)",
206 int cmd_inspect_dump_tree(int argc, char **argv)
208 struct btrfs_root *root;
209 struct btrfs_fs_info *info;
210 struct btrfs_path path;
211 struct btrfs_key key;
212 struct btrfs_root_item ri;
213 struct extent_buffer *leaf;
214 struct btrfs_disk_key disk_key;
215 struct btrfs_key found_key;
216 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
221 int uuid_tree_only = 0;
223 int root_backups = 0;
225 struct btrfs_root *tree_root_scan;
230 static const struct option long_options[] = {
231 { "extents", no_argument, NULL, 'e'},
232 { "device", no_argument, NULL, 'd'},
233 { "roots", no_argument, NULL, 'r'},
234 { "backups", no_argument, NULL, 'R'},
235 { "uuid", no_argument, NULL, 'u'},
236 { "block", required_argument, NULL, 'b'},
237 { "tree", required_argument, NULL, 't'},
241 c = getopt_long(argc, argv, "deb:rRut:", long_options, NULL);
262 block_only = arg_strtou64(optarg);
265 const char *end = NULL;
267 if (string_is_numerical(optarg))
268 tree_id = arg_strtou64(optarg);
270 tree_id = treeid_from_string(optarg, &end);
273 error("unrecognized tree id: %s",
279 error("unexpected tree id suffix of '%s': %s",
286 usage(cmd_inspect_dump_tree_usage);
290 if (check_argc_exact(argc - optind, 1))
291 usage(cmd_inspect_dump_tree_usage);
293 ret = check_arg_type(argv[optind]);
294 if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {
295 error("not a block device or regular file: %s", argv[optind]);
299 printf("%s\n", PACKAGE_STRING);
301 info = open_ctree_fs_info(argv[optind], 0, 0, 0, OPEN_CTREE_PARTIAL);
303 error("unable to open %s", argv[optind]);
307 root = info->fs_root;
309 error("unable to open %s", argv[optind]);
314 leaf = read_tree_block(root,
318 if (extent_buffer_uptodate(leaf) &&
319 btrfs_header_level(leaf) != 0) {
320 free_extent_buffer(leaf);
325 leaf = read_tree_block(root,
329 if (!extent_buffer_uptodate(leaf)) {
330 error("failed to read %llu",
331 (unsigned long long)block_only);
334 btrfs_print_tree(root, leaf, 0);
335 free_extent_buffer(leaf);
339 if (!(extent_only || uuid_tree_only || tree_id)) {
341 printf("root tree: %llu level %d\n",
342 (unsigned long long)info->tree_root->node->start,
343 btrfs_header_level(info->tree_root->node));
344 printf("chunk tree: %llu level %d\n",
345 (unsigned long long)info->chunk_root->node->start,
346 btrfs_header_level(info->chunk_root->node));
347 if (info->log_root_tree)
348 printf("log root tree: %llu level %d\n",
349 info->log_root_tree->node->start,
351 info->log_root_tree->node));
353 if (info->tree_root->node) {
354 printf("root tree\n");
355 btrfs_print_tree(info->tree_root,
356 info->tree_root->node, 1);
359 if (info->chunk_root->node) {
360 printf("chunk tree\n");
361 btrfs_print_tree(info->chunk_root,
362 info->chunk_root->node, 1);
365 if (info->log_root_tree) {
366 printf("log root tree\n");
367 btrfs_print_tree(info->log_root_tree,
368 info->log_root_tree->node, 1);
372 tree_root_scan = info->tree_root;
374 btrfs_init_path(&path);
376 if (!extent_buffer_uptodate(tree_root_scan->node))
380 * Tree's that are not pointed by the tree of tree roots
382 if (tree_id && tree_id == BTRFS_ROOT_TREE_OBJECTID) {
383 if (!info->tree_root->node) {
384 error("cannot print root tree, invalid pointer");
387 printf("root tree\n");
388 btrfs_print_tree(info->tree_root, info->tree_root->node, 1);
392 if (tree_id && tree_id == BTRFS_CHUNK_TREE_OBJECTID) {
393 if (!info->chunk_root->node) {
394 error("cannot print chunk tree, invalid pointer");
397 printf("chunk tree\n");
398 btrfs_print_tree(info->chunk_root, info->chunk_root->node, 1);
402 if (tree_id && tree_id == BTRFS_TREE_LOG_OBJECTID) {
403 if (!info->log_root_tree) {
404 error("cannot print log root tree, invalid pointer");
407 printf("log root tree\n");
408 btrfs_print_tree(info->log_root_tree, info->log_root_tree->node,
415 key.type = BTRFS_ROOT_ITEM_KEY;
416 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
418 error("cannot read ROOT_ITEM from tree %llu: %s",
419 (unsigned long long)tree_root_scan->root_key.objectid,
424 leaf = path.nodes[0];
425 slot = path.slots[0];
426 if (slot >= btrfs_header_nritems(leaf)) {
427 ret = btrfs_next_leaf(tree_root_scan, &path);
430 leaf = path.nodes[0];
431 slot = path.slots[0];
433 btrfs_item_key(leaf, &disk_key, path.slots[0]);
434 btrfs_disk_key_to_cpu(&found_key, &disk_key);
435 if (found_key.type == BTRFS_ROOT_ITEM_KEY) {
436 unsigned long offset;
437 struct extent_buffer *buf;
438 int skip = extent_only | device_only | uuid_tree_only;
440 offset = btrfs_item_ptr_offset(leaf, slot);
441 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
442 buf = read_tree_block(tree_root_scan,
443 btrfs_root_bytenr(&ri),
444 tree_root_scan->nodesize,
446 if (!extent_buffer_uptodate(buf))
448 if (tree_id && found_key.objectid != tree_id) {
449 free_extent_buffer(buf);
453 switch (found_key.objectid) {
454 case BTRFS_ROOT_TREE_OBJECTID:
458 case BTRFS_EXTENT_TREE_OBJECTID:
459 if (!device_only && !uuid_tree_only)
464 case BTRFS_CHUNK_TREE_OBJECTID:
469 case BTRFS_DEV_TREE_OBJECTID:
475 case BTRFS_FS_TREE_OBJECTID:
480 case BTRFS_ROOT_TREE_DIR_OBJECTID:
484 case BTRFS_CSUM_TREE_OBJECTID:
489 case BTRFS_ORPHAN_OBJECTID:
494 case BTRFS_TREE_LOG_OBJECTID:
499 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
504 case BTRFS_TREE_RELOC_OBJECTID:
509 case BTRFS_DATA_RELOC_TREE_OBJECTID:
511 printf("data reloc");
514 case BTRFS_EXTENT_CSUM_OBJECTID:
516 printf("extent checksum");
519 case BTRFS_QUOTA_TREE_OBJECTID:
524 case BTRFS_UUID_TREE_OBJECTID:
525 if (!extent_only && !device_only)
530 case BTRFS_FREE_SPACE_TREE_OBJECTID:
532 printf("free space");
534 case BTRFS_MULTIPLE_OBJECTIDS:
544 if (extent_only && !skip) {
546 btrfs_print_key(&disk_key);
548 print_extents(tree_root_scan, buf);
551 btrfs_print_key(&disk_key);
553 printf(" %llu level %d\n",
554 (unsigned long long)buf->start,
555 btrfs_header_level(buf));
558 btrfs_print_tree(tree_root_scan, buf, 1);
561 free_extent_buffer(buf);
567 btrfs_release_path(&path);
569 if (tree_root_scan == info->tree_root && info->log_root_tree) {
570 tree_root_scan = info->log_root_tree;
574 if (extent_only || device_only || uuid_tree_only)
578 print_old_roots(info->super_copy);
580 printf("total bytes %llu\n",
581 (unsigned long long)btrfs_super_total_bytes(info->super_copy));
582 printf("bytes used %llu\n",
583 (unsigned long long)btrfs_super_bytes_used(info->super_copy));
584 uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
585 uuid_unparse(info->super_copy->fsid, uuidbuf);
586 printf("uuid %s\n", uuidbuf);
588 ret = close_ctree(root);