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
));
348 if (info
->tree_root
->node
) {
349 printf("root tree\n");
350 btrfs_print_tree(info
->tree_root
,
351 info
->tree_root
->node
, 1);
354 if (info
->chunk_root
->node
) {
355 printf("chunk tree\n");
356 btrfs_print_tree(info
->chunk_root
,
357 info
->chunk_root
->node
, 1);
361 tree_root_scan
= info
->tree_root
;
363 btrfs_init_path(&path
);
365 if (!extent_buffer_uptodate(tree_root_scan
->node
))
369 * Tree's that are not pointed by the tree of tree roots
371 if (tree_id
&& tree_id
== BTRFS_ROOT_TREE_OBJECTID
) {
372 if (!info
->tree_root
->node
) {
373 error("cannot print root tree, invalid pointer");
376 printf("root tree\n");
377 btrfs_print_tree(info
->tree_root
, info
->tree_root
->node
, 1);
381 if (tree_id
&& tree_id
== BTRFS_CHUNK_TREE_OBJECTID
) {
382 if (!info
->chunk_root
->node
) {
383 error("cannot print chunk tree, invalid pointer");
386 printf("chunk tree\n");
387 btrfs_print_tree(info
->chunk_root
, info
->chunk_root
->node
, 1);
393 key
.type
= BTRFS_ROOT_ITEM_KEY
;
394 ret
= btrfs_search_slot(NULL
, tree_root_scan
, &key
, &path
, 0, 0);
396 error("cannot read ROOT_ITEM from tree %llu: %s",
397 (unsigned long long)tree_root_scan
->root_key
.objectid
,
402 leaf
= path
.nodes
[0];
403 slot
= path
.slots
[0];
404 if (slot
>= btrfs_header_nritems(leaf
)) {
405 ret
= btrfs_next_leaf(tree_root_scan
, &path
);
408 leaf
= path
.nodes
[0];
409 slot
= path
.slots
[0];
411 btrfs_item_key(leaf
, &disk_key
, path
.slots
[0]);
412 btrfs_disk_key_to_cpu(&found_key
, &disk_key
);
413 if (found_key
.type
== BTRFS_ROOT_ITEM_KEY
) {
414 unsigned long offset
;
415 struct extent_buffer
*buf
;
416 int skip
= extent_only
| device_only
| uuid_tree_only
;
418 offset
= btrfs_item_ptr_offset(leaf
, slot
);
419 read_extent_buffer(leaf
, &ri
, offset
, sizeof(ri
));
420 buf
= read_tree_block(tree_root_scan
,
421 btrfs_root_bytenr(&ri
),
422 tree_root_scan
->nodesize
,
424 if (!extent_buffer_uptodate(buf
))
426 if (tree_id
&& found_key
.objectid
!= tree_id
) {
427 free_extent_buffer(buf
);
431 switch (found_key
.objectid
) {
432 case BTRFS_ROOT_TREE_OBJECTID
:
436 case BTRFS_EXTENT_TREE_OBJECTID
:
437 if (!device_only
&& !uuid_tree_only
)
442 case BTRFS_CHUNK_TREE_OBJECTID
:
447 case BTRFS_DEV_TREE_OBJECTID
:
453 case BTRFS_FS_TREE_OBJECTID
:
458 case BTRFS_ROOT_TREE_DIR_OBJECTID
:
462 case BTRFS_CSUM_TREE_OBJECTID
:
467 case BTRFS_ORPHAN_OBJECTID
:
472 case BTRFS_TREE_LOG_OBJECTID
:
477 case BTRFS_TREE_LOG_FIXUP_OBJECTID
:
482 case BTRFS_TREE_RELOC_OBJECTID
:
487 case BTRFS_DATA_RELOC_TREE_OBJECTID
:
489 printf("data reloc");
492 case BTRFS_EXTENT_CSUM_OBJECTID
:
494 printf("extent checksum");
497 case BTRFS_QUOTA_TREE_OBJECTID
:
502 case BTRFS_UUID_TREE_OBJECTID
:
503 if (!extent_only
&& !device_only
)
508 case BTRFS_FREE_SPACE_TREE_OBJECTID
:
510 printf("free space");
512 case BTRFS_MULTIPLE_OBJECTIDS
:
522 if (extent_only
&& !skip
) {
524 btrfs_print_key(&disk_key
);
526 print_extents(tree_root_scan
, buf
);
529 btrfs_print_key(&disk_key
);
531 printf(" %llu level %d\n",
532 (unsigned long long)buf
->start
,
533 btrfs_header_level(buf
));
536 btrfs_print_tree(tree_root_scan
, buf
, 1);
539 free_extent_buffer(buf
);
545 btrfs_release_path(&path
);
547 if (tree_root_scan
== info
->tree_root
&& info
->log_root_tree
) {
548 tree_root_scan
= info
->log_root_tree
;
552 if (extent_only
|| device_only
|| uuid_tree_only
)
556 print_old_roots(info
->super_copy
);
558 printf("total bytes %llu\n",
559 (unsigned long long)btrfs_super_total_bytes(info
->super_copy
));
560 printf("bytes used %llu\n",
561 (unsigned long long)btrfs_super_bytes_used(info
->super_copy
));
562 uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
- 1] = '\0';
563 uuid_unparse(info
->super_copy
->fsid
, uuidbuf
);
564 printf("uuid %s\n", uuidbuf
);
566 ret
= close_ctree(root
);