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)
38 struct extent_buffer *next;
46 if (btrfs_is_leaf(eb)) {
47 btrfs_print_leaf(root, eb);
51 size = root->nodesize;
52 nr = btrfs_header_nritems(eb);
53 for (i = 0; i < nr; i++) {
54 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
55 size, btrfs_node_ptr_generation(eb, i));
56 if (!extent_buffer_uptodate(next))
58 if (btrfs_is_leaf(next) && btrfs_header_level(eb) != 1) {
60 "eb corrupted: item %d eb level %d next level %d, skipping the rest",
61 i, btrfs_header_level(next),
62 btrfs_header_level(eb));
65 if (btrfs_header_level(next) != btrfs_header_level(eb) - 1) {
67 "eb corrupted: item %d eb level %d next level %d, skipping the rest",
68 i, btrfs_header_level(next),
69 btrfs_header_level(eb));
72 print_extents(root, next);
73 free_extent_buffer(next);
79 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));
122 * Convert a tree name from various forms to the numerical id if possible
124 * - case does not matter
125 * - same as the key name, BTRFS_ROOT_TREE_OBJECTID
126 * - dtto shortened, BTRFS_ROOT_TREE
127 * - dtto without prefix, ROOT_TREE
128 * - common name, ROOT, CHUNK, EXTENT, ...
129 * - dtto alias, DEVICE for DEV, CHECKSUM for CSUM
131 * Returns 0 if the tree id was not recognized.
133 static u64 treeid_from_string(const char *str, const char **end)
138 static struct treename {
142 { "ROOT", BTRFS_ROOT_TREE_OBJECTID },
143 { "EXTENT", BTRFS_EXTENT_TREE_OBJECTID },
144 { "CHUNK", BTRFS_CHUNK_TREE_OBJECTID },
145 { "DEVICE", BTRFS_DEV_TREE_OBJECTID },
146 { "DEV", BTRFS_DEV_TREE_OBJECTID },
147 { "FS_TREE", BTRFS_FS_TREE_OBJECTID },
148 { "CSUM", BTRFS_CSUM_TREE_OBJECTID },
149 { "CHECKSUM", BTRFS_CSUM_TREE_OBJECTID },
150 { "QUOTA", BTRFS_QUOTA_TREE_OBJECTID },
151 { "UUID", BTRFS_UUID_TREE_OBJECTID },
152 { "FREE_SPACE", BTRFS_FREE_SPACE_TREE_OBJECTID },
153 { "TREE_LOG_FIXUP", BTRFS_TREE_LOG_FIXUP_OBJECTID },
154 { "TREE_LOG", BTRFS_TREE_LOG_OBJECTID },
155 { "TREE_RELOC", BTRFS_TREE_RELOC_OBJECTID },
156 { "DATA_RELOC", BTRFS_DATA_RELOC_TREE_OBJECTID }
159 if (strncasecmp("BTRFS_", str, strlen("BTRFS_")) == 0)
160 str += strlen("BTRFS_");
162 for (i = 0; i < ARRAY_SIZE(tn); i++) {
163 int len = strlen(tn[i].name);
165 if (strncasecmp(tn[i].name, str, len) == 0) {
176 if (strncasecmp("_TREE", str, strlen("_TREE")) == 0)
177 str += strlen("_TREE");
179 if (strncasecmp("_OBJECTID", str, strlen("_OBJECTID")) == 0)
180 str += strlen("_OBJECTID");
187 const char * const cmd_inspect_dump_tree_usage[] = {
188 "btrfs inspect-internal dump-tree [options] device",
189 "Dump tree structures from a given device",
190 "Dump tree structures from a given device in textual form, expand keys to human",
191 "readable equivalents where possible.",
192 "Note: contains file names, consider that if you're asked to send the dump",
195 "-e|--extents print only extent info: extent and device trees",
196 "-d|--device print only device info: tree root, chunk and device trees",
197 "-r|--roots print only short root node info",
198 "-R|--backups same as --roots plus print backup root info",
199 "-u|--uuid print only the uuid tree",
200 "-b|--block <block_num> print info from the specified block only",
201 "-t|--tree <tree_id> print only tree with the given id (string or number)",
205 int cmd_inspect_dump_tree(int argc, char **argv)
207 struct btrfs_root *root;
208 struct btrfs_fs_info *info;
209 struct btrfs_path path;
210 struct btrfs_key key;
211 struct btrfs_root_item ri;
212 struct extent_buffer *leaf;
213 struct btrfs_disk_key disk_key;
214 struct btrfs_key found_key;
215 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
220 int uuid_tree_only = 0;
222 int root_backups = 0;
224 struct btrfs_root *tree_root_scan;
229 static const struct option long_options[] = {
230 { "extents", no_argument, NULL, 'e'},
231 { "device", no_argument, NULL, 'd'},
232 { "roots", no_argument, NULL, 'r'},
233 { "backups", no_argument, NULL, 'R'},
234 { "uuid", no_argument, NULL, 'u'},
235 { "block", required_argument, NULL, 'b'},
236 { "tree", required_argument, NULL, 't'},
240 c = getopt_long(argc, argv, "deb:rRut:", long_options, NULL);
261 block_only = arg_strtou64(optarg);
264 if (string_is_numerical(optarg)) {
265 tree_id = arg_strtou64(optarg);
267 const char *end = NULL;
269 tree_id = treeid_from_string(optarg, &end);
272 error("unexpected tree id suffix of '%s': %s\n",
278 error("unrecognized tree id: %s\n",
284 usage(cmd_inspect_dump_tree_usage);
288 if (check_argc_exact(argc - optind, 1))
289 usage(cmd_inspect_dump_tree_usage);
291 ret = check_arg_type(argv[optind]);
292 if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {
293 error("not a block device or regular file: %s", argv[optind]);
297 printf("%s\n", PACKAGE_STRING);
299 info = open_ctree_fs_info(argv[optind], 0, 0, 0, OPEN_CTREE_PARTIAL);
301 error("unable to open %s", argv[optind]);
305 root = info->fs_root;
307 error("unable to open %s", argv[optind]);
312 leaf = read_tree_block(root,
316 if (extent_buffer_uptodate(leaf) &&
317 btrfs_header_level(leaf) != 0) {
318 free_extent_buffer(leaf);
323 leaf = read_tree_block(root,
327 if (!extent_buffer_uptodate(leaf)) {
328 error("failed to read %llu",
329 (unsigned long long)block_only);
332 btrfs_print_tree(root, leaf, 0);
333 free_extent_buffer(leaf);
337 if (!(extent_only || uuid_tree_only || tree_id)) {
339 printf("root tree: %llu level %d\n",
340 (unsigned long long)info->tree_root->node->start,
341 btrfs_header_level(info->tree_root->node));
342 printf("chunk tree: %llu level %d\n",
343 (unsigned long long)info->chunk_root->node->start,
344 btrfs_header_level(info->chunk_root->node));
346 if (info->tree_root->node) {
347 printf("root tree\n");
348 btrfs_print_tree(info->tree_root,
349 info->tree_root->node, 1);
352 if (info->chunk_root->node) {
353 printf("chunk tree\n");
354 btrfs_print_tree(info->chunk_root,
355 info->chunk_root->node, 1);
359 tree_root_scan = info->tree_root;
361 btrfs_init_path(&path);
363 if (!extent_buffer_uptodate(tree_root_scan->node))
367 * Tree's that are not pointed by the tree of tree roots
369 if (tree_id && tree_id == BTRFS_ROOT_TREE_OBJECTID) {
370 if (!info->tree_root->node) {
371 error("cannot print root tree, invalid pointer");
374 printf("root tree\n");
375 btrfs_print_tree(info->tree_root, info->tree_root->node, 1);
379 if (tree_id && tree_id == BTRFS_CHUNK_TREE_OBJECTID) {
380 if (!info->chunk_root->node) {
381 error("cannot print chunk tree, invalid pointer");
384 printf("chunk tree\n");
385 btrfs_print_tree(info->chunk_root, info->chunk_root->node, 1);
391 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
392 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
394 error("cannot read ROOT_ITEM from tree %llu: %s",
395 (unsigned long long)tree_root_scan->root_key.objectid,
400 leaf = path.nodes[0];
401 slot = path.slots[0];
402 if (slot >= btrfs_header_nritems(leaf)) {
403 ret = btrfs_next_leaf(tree_root_scan, &path);
406 leaf = path.nodes[0];
407 slot = path.slots[0];
409 btrfs_item_key(leaf, &disk_key, path.slots[0]);
410 btrfs_disk_key_to_cpu(&found_key, &disk_key);
411 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
412 unsigned long offset;
413 struct extent_buffer *buf;
414 int skip = extent_only | device_only | uuid_tree_only;
416 offset = btrfs_item_ptr_offset(leaf, slot);
417 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
418 buf = read_tree_block(tree_root_scan,
419 btrfs_root_bytenr(&ri),
420 tree_root_scan->nodesize,
422 if (!extent_buffer_uptodate(buf))
424 if (tree_id && found_key.objectid != tree_id) {
425 free_extent_buffer(buf);
429 switch (found_key.objectid) {
430 case BTRFS_ROOT_TREE_OBJECTID:
434 case BTRFS_EXTENT_TREE_OBJECTID:
435 if (!device_only && !uuid_tree_only)
440 case BTRFS_CHUNK_TREE_OBJECTID:
445 case BTRFS_DEV_TREE_OBJECTID:
451 case BTRFS_FS_TREE_OBJECTID:
456 case BTRFS_ROOT_TREE_DIR_OBJECTID:
460 case BTRFS_CSUM_TREE_OBJECTID:
465 case BTRFS_ORPHAN_OBJECTID:
470 case BTRFS_TREE_LOG_OBJECTID:
475 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
480 case BTRFS_TREE_RELOC_OBJECTID:
485 case BTRFS_DATA_RELOC_TREE_OBJECTID:
487 printf("data reloc");
490 case BTRFS_EXTENT_CSUM_OBJECTID:
492 printf("extent checksum");
495 case BTRFS_QUOTA_TREE_OBJECTID:
500 case BTRFS_UUID_TREE_OBJECTID:
501 if (!extent_only && !device_only)
506 case BTRFS_FREE_SPACE_TREE_OBJECTID:
508 printf("free space");
510 case BTRFS_MULTIPLE_OBJECTIDS:
520 if (extent_only && !skip) {
522 btrfs_print_key(&disk_key);
524 print_extents(tree_root_scan, buf);
527 btrfs_print_key(&disk_key);
529 printf(" %llu level %d\n",
530 (unsigned long long)buf->start,
531 btrfs_header_level(buf));
534 btrfs_print_tree(tree_root_scan, buf, 1);
537 free_extent_buffer(buf);
543 btrfs_release_path(&path);
545 if (tree_root_scan == info->tree_root && info->log_root_tree) {
546 tree_root_scan = info->log_root_tree;
550 if (extent_only || device_only || uuid_tree_only)
554 print_old_roots(info->super_copy);
556 printf("total bytes %llu\n",
557 (unsigned long long)btrfs_super_total_bytes(info->super_copy));
558 printf("bytes used %llu\n",
559 (unsigned long long)btrfs_super_bytes_used(info->super_copy));
560 uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
561 uuid_unparse(info->super_copy->fsid, uuidbuf);
562 printf("uuid %s\n", uuidbuf);
564 ret = close_ctree(root);