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
));
110 * Convert a tree name from various forms to the numerical id if possible
112 * - case does not matter
113 * - same as the key name, BTRFS_ROOT_TREE_OBJECTID
114 * - dtto shortened, BTRFS_ROOT_TREE
115 * - dtto without prefix, ROOT_TREE
116 * - common name, ROOT, CHUNK, EXTENT, ...
117 * - dtto alias, DEVICE for DEV, CHECKSUM for CSUM
119 * Returns 0 if the tree id was not recognized.
121 static u64
treeid_from_string(const char *str
, const char **end
)
126 static struct treename
{
130 { "ROOT", BTRFS_ROOT_TREE_OBJECTID
},
131 { "EXTENT", BTRFS_EXTENT_TREE_OBJECTID
},
132 { "CHUNK", BTRFS_CHUNK_TREE_OBJECTID
},
133 { "DEVICE", BTRFS_DEV_TREE_OBJECTID
},
134 { "DEV", BTRFS_DEV_TREE_OBJECTID
},
135 { "FS_TREE", BTRFS_FS_TREE_OBJECTID
},
136 { "CSUM", BTRFS_CSUM_TREE_OBJECTID
},
137 { "CHECKSUM", BTRFS_CSUM_TREE_OBJECTID
},
138 { "QUOTA", BTRFS_QUOTA_TREE_OBJECTID
},
139 { "UUID", BTRFS_UUID_TREE_OBJECTID
},
140 { "FREE_SPACE", BTRFS_FREE_SPACE_TREE_OBJECTID
},
141 { "TREE_LOG_FIXUP", BTRFS_TREE_LOG_FIXUP_OBJECTID
},
142 { "TREE_LOG", BTRFS_TREE_LOG_OBJECTID
},
143 { "TREE_RELOC", BTRFS_TREE_RELOC_OBJECTID
},
144 { "DATA_RELOC", BTRFS_DATA_RELOC_TREE_OBJECTID
}
147 if (strncasecmp("BTRFS_", str
, strlen("BTRFS_")) == 0)
148 str
+= strlen("BTRFS_");
150 for (i
= 0; i
< ARRAY_SIZE(tn
); i
++) {
151 int len
= strlen(tn
[i
].name
);
153 if (strncasecmp(tn
[i
].name
, str
, len
) == 0) {
164 if (strncasecmp("_TREE", str
, strlen("_TREE")) == 0)
165 str
+= strlen("_TREE");
167 if (strncasecmp("_OBJECTID", str
, strlen("_OBJECTID")) == 0)
168 str
+= strlen("_OBJECTID");
175 const char * const cmd_inspect_dump_tree_usage
[] = {
176 "btrfs inspect-internal dump-tree [options] device",
177 "Dump tree structures from a given device",
178 "Dump tree structures from a given device in textual form, expand keys to human",
179 "readable equivalents where possible.",
180 "Note: contains file names, consider that if you're asked to send the dump",
183 "-e|--extents print only extent info: extent and device trees",
184 "-d|--device print only device info: tree root, chunk and device trees",
185 "-r|--roots print only short root node info",
186 "-R|--backups same as --roots plus print backup root info",
187 "-u|--uuid print only the uuid tree",
188 "-b|--block <block_num> print info from the specified block only",
189 "-t|--tree <tree_id> print only tree with the given id (string or number)",
193 int cmd_inspect_dump_tree(int argc
, char **argv
)
195 struct btrfs_root
*root
;
196 struct btrfs_fs_info
*info
;
197 struct btrfs_path path
;
198 struct btrfs_key key
;
199 struct btrfs_root_item ri
;
200 struct extent_buffer
*leaf
;
201 struct btrfs_disk_key disk_key
;
202 struct btrfs_key found_key
;
203 char uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
];
208 int uuid_tree_only
= 0;
210 int root_backups
= 0;
212 struct btrfs_root
*tree_root_scan
;
217 static const struct option long_options
[] = {
218 { "extents", no_argument
, NULL
, 'e'},
219 { "device", no_argument
, NULL
, 'd'},
220 { "roots", no_argument
, NULL
, 'r'},
221 { "backups", no_argument
, NULL
, 'R'},
222 { "uuid", no_argument
, NULL
, 'u'},
223 { "block", required_argument
, NULL
, 'b'},
224 { "tree", required_argument
, NULL
, 't'},
228 c
= getopt_long(argc
, argv
, "deb:rRut:", long_options
, NULL
);
249 block_only
= arg_strtou64(optarg
);
252 if (string_is_numerical(optarg
)) {
253 tree_id
= arg_strtou64(optarg
);
255 const char *end
= NULL
;
257 tree_id
= treeid_from_string(optarg
, &end
);
260 error("unexpected tree id suffix of '%s': %s\n",
266 error("unrecognized tree id: %s\n",
272 usage(cmd_inspect_dump_tree_usage
);
276 if (check_argc_exact(argc
- optind
, 1))
277 usage(cmd_inspect_dump_tree_usage
);
279 ret
= check_arg_type(argv
[optind
]);
280 if (ret
!= BTRFS_ARG_BLKDEV
&& ret
!= BTRFS_ARG_REG
) {
281 error("not a block device or regular file: %s", argv
[optind
]);
285 printf("%s\n", PACKAGE_STRING
);
287 info
= open_ctree_fs_info(argv
[optind
], 0, 0, 0, OPEN_CTREE_PARTIAL
);
289 error("unable to open %s", argv
[optind
]);
293 root
= info
->fs_root
;
295 error("unable to open %s", argv
[optind
]);
300 leaf
= read_tree_block(root
,
304 if (extent_buffer_uptodate(leaf
) &&
305 btrfs_header_level(leaf
) != 0) {
306 free_extent_buffer(leaf
);
311 leaf
= read_tree_block(root
,
315 if (!extent_buffer_uptodate(leaf
)) {
316 error("failed to read %llu",
317 (unsigned long long)block_only
);
320 btrfs_print_tree(root
, leaf
, 0);
321 free_extent_buffer(leaf
);
325 if (!(extent_only
|| uuid_tree_only
|| tree_id
)) {
327 printf("root tree: %llu level %d\n",
328 (unsigned long long)info
->tree_root
->node
->start
,
329 btrfs_header_level(info
->tree_root
->node
));
330 printf("chunk tree: %llu level %d\n",
331 (unsigned long long)info
->chunk_root
->node
->start
,
332 btrfs_header_level(info
->chunk_root
->node
));
334 if (info
->tree_root
->node
) {
335 printf("root tree\n");
336 btrfs_print_tree(info
->tree_root
,
337 info
->tree_root
->node
, 1);
340 if (info
->chunk_root
->node
) {
341 printf("chunk tree\n");
342 btrfs_print_tree(info
->chunk_root
,
343 info
->chunk_root
->node
, 1);
347 tree_root_scan
= info
->tree_root
;
349 btrfs_init_path(&path
);
351 if (!extent_buffer_uptodate(tree_root_scan
->node
))
355 * Tree's that are not pointed by the tree of tree roots
357 if (tree_id
&& tree_id
== BTRFS_ROOT_TREE_OBJECTID
) {
358 if (!info
->tree_root
->node
) {
359 error("cannot print root tree, invalid pointer");
362 printf("root tree\n");
363 btrfs_print_tree(info
->tree_root
, info
->tree_root
->node
, 1);
367 if (tree_id
&& tree_id
== BTRFS_CHUNK_TREE_OBJECTID
) {
368 if (!info
->chunk_root
->node
) {
369 error("cannot print chunk tree, invalid pointer");
372 printf("chunk tree\n");
373 btrfs_print_tree(info
->chunk_root
, info
->chunk_root
->node
, 1);
379 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
380 ret
= btrfs_search_slot(NULL
, tree_root_scan
, &key
, &path
, 0, 0);
383 leaf
= path
.nodes
[0];
384 slot
= path
.slots
[0];
385 if (slot
>= btrfs_header_nritems(leaf
)) {
386 ret
= btrfs_next_leaf(tree_root_scan
, &path
);
389 leaf
= path
.nodes
[0];
390 slot
= path
.slots
[0];
392 btrfs_item_key(leaf
, &disk_key
, path
.slots
[0]);
393 btrfs_disk_key_to_cpu(&found_key
, &disk_key
);
394 if (btrfs_key_type(&found_key
) == BTRFS_ROOT_ITEM_KEY
) {
395 unsigned long offset
;
396 struct extent_buffer
*buf
;
397 int skip
= extent_only
| device_only
| uuid_tree_only
;
399 offset
= btrfs_item_ptr_offset(leaf
, slot
);
400 read_extent_buffer(leaf
, &ri
, offset
, sizeof(ri
));
401 buf
= read_tree_block(tree_root_scan
,
402 btrfs_root_bytenr(&ri
),
403 btrfs_level_size(tree_root_scan
,
404 btrfs_root_level(&ri
)),
406 if (!extent_buffer_uptodate(buf
))
408 if (tree_id
&& found_key
.objectid
!= tree_id
) {
409 free_extent_buffer(buf
);
413 switch (found_key
.objectid
) {
414 case BTRFS_ROOT_TREE_OBJECTID
:
418 case BTRFS_EXTENT_TREE_OBJECTID
:
419 if (!device_only
&& !uuid_tree_only
)
424 case BTRFS_CHUNK_TREE_OBJECTID
:
429 case BTRFS_DEV_TREE_OBJECTID
:
435 case BTRFS_FS_TREE_OBJECTID
:
440 case BTRFS_ROOT_TREE_DIR_OBJECTID
:
444 case BTRFS_CSUM_TREE_OBJECTID
:
449 case BTRFS_ORPHAN_OBJECTID
:
454 case BTRFS_TREE_LOG_OBJECTID
:
459 case BTRFS_TREE_LOG_FIXUP_OBJECTID
:
464 case BTRFS_TREE_RELOC_OBJECTID
:
469 case BTRFS_DATA_RELOC_TREE_OBJECTID
:
471 printf("data reloc");
474 case BTRFS_EXTENT_CSUM_OBJECTID
:
476 printf("extent checksum");
479 case BTRFS_QUOTA_TREE_OBJECTID
:
484 case BTRFS_UUID_TREE_OBJECTID
:
485 if (!extent_only
&& !device_only
)
490 case BTRFS_FREE_SPACE_TREE_OBJECTID
:
492 printf("free space");
494 case BTRFS_MULTIPLE_OBJECTIDS
:
504 if (extent_only
&& !skip
) {
506 btrfs_print_key(&disk_key
);
508 print_extents(tree_root_scan
, buf
);
511 btrfs_print_key(&disk_key
);
513 printf(" %llu level %d\n",
514 (unsigned long long)buf
->start
,
515 btrfs_header_level(buf
));
518 btrfs_print_tree(tree_root_scan
, buf
, 1);
521 free_extent_buffer(buf
);
527 btrfs_release_path(&path
);
529 if (tree_root_scan
== info
->tree_root
&&
530 info
->log_root_tree
) {
531 tree_root_scan
= info
->log_root_tree
;
535 if (extent_only
|| device_only
|| uuid_tree_only
)
539 print_old_roots(info
->super_copy
);
541 printf("total bytes %llu\n",
542 (unsigned long long)btrfs_super_total_bytes(info
->super_copy
));
543 printf("bytes used %llu\n",
544 (unsigned long long)btrfs_super_bytes_used(info
->super_copy
));
545 uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
- 1] = '\0';
546 uuid_unparse(info
->super_copy
->fsid
, uuidbuf
);
547 printf("uuid %s\n", uuidbuf
);
549 ret
= close_ctree(root
);