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>
23 #include "kerncompat.h"
24 #include "radix-tree.h"
27 #include "print-tree.h"
28 #include "transaction.h"
31 static int print_usage(void)
33 fprintf(stderr
, "usage: debug-tree [ -e ] device\n");
34 fprintf(stderr
, "%s\n", BTRFS_BUILD_VERSION
);
38 static void print_extents(struct btrfs_root
*root
, struct extent_buffer
*eb
)
47 size
= btrfs_level_size(root
, btrfs_header_level(eb
) - 1);
48 nr
= btrfs_header_nritems(eb
);
49 for (i
= 0; i
< nr
; i
++) {
50 struct extent_buffer
*next
= read_tree_block(root
,
51 btrfs_node_blockptr(eb
, i
),
53 btrfs_node_ptr_generation(eb
, i
));
54 if (btrfs_is_leaf(next
) &&
55 btrfs_header_level(eb
) != 1)
57 if (btrfs_header_level(next
) !=
58 btrfs_header_level(eb
) - 1)
60 print_extents(root
, next
);
61 free_extent_buffer(next
);
65 int main(int ac
, char **av
)
67 struct btrfs_root
*root
;
68 struct btrfs_path path
;
70 struct btrfs_root_item ri
;
71 struct extent_buffer
*leaf
;
72 struct btrfs_disk_key disk_key
;
73 struct btrfs_key found_key
;
80 struct btrfs_root
*tree_root_scan
;
86 c
= getopt(ac
, av
, "deb:");
97 block_only
= atoll(optarg
);
107 root
= open_ctree(av
[optind
], 0, 0);
109 fprintf(stderr
, "unable to open %s\n", av
[optind
]);
113 leaf
= read_tree_block(root
,
117 if (leaf
&& btrfs_header_level(leaf
) != 0) {
118 free_extent_buffer(leaf
);
123 leaf
= read_tree_block(root
,
128 fprintf(stderr
, "failed to read %llu\n",
129 (unsigned long long)block_only
);
132 btrfs_print_tree(root
, leaf
, 0);
137 printf("root tree\n");
138 btrfs_print_tree(root
->fs_info
->tree_root
,
139 root
->fs_info
->tree_root
->node
, 1);
141 printf("chunk tree\n");
142 btrfs_print_tree(root
->fs_info
->chunk_root
,
143 root
->fs_info
->chunk_root
->node
, 1);
145 tree_root_scan
= root
->fs_info
->tree_root
;
147 btrfs_init_path(&path
);
151 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
152 ret
= btrfs_search_slot(NULL
, tree_root_scan
, &key
, &path
, 0, 0);
155 leaf
= path
.nodes
[0];
156 slot
= path
.slots
[0];
157 if (slot
>= btrfs_header_nritems(leaf
)) {
158 ret
= btrfs_next_leaf(tree_root_scan
, &path
);
161 leaf
= path
.nodes
[0];
162 slot
= path
.slots
[0];
164 btrfs_item_key(leaf
, &disk_key
, path
.slots
[0]);
165 btrfs_disk_key_to_cpu(&found_key
, &disk_key
);
166 if (btrfs_key_type(&found_key
) == BTRFS_ROOT_ITEM_KEY
) {
167 unsigned long offset
;
168 struct extent_buffer
*buf
;
169 int skip
= extent_only
| device_only
;
171 offset
= btrfs_item_ptr_offset(leaf
, slot
);
172 read_extent_buffer(leaf
, &ri
, offset
, sizeof(ri
));
173 buf
= read_tree_block(tree_root_scan
,
174 btrfs_root_bytenr(&ri
),
175 btrfs_level_size(tree_root_scan
,
176 btrfs_root_level(&ri
)),
178 switch(found_key
.objectid
) {
179 case BTRFS_ROOT_TREE_OBJECTID
:
183 case BTRFS_EXTENT_TREE_OBJECTID
:
186 if (!extent_only
&& !device_only
)
189 case BTRFS_CHUNK_TREE_OBJECTID
:
194 case BTRFS_DEV_TREE_OBJECTID
:
198 case BTRFS_FS_TREE_OBJECTID
:
203 case BTRFS_ROOT_TREE_DIR_OBJECTID
:
207 case BTRFS_CSUM_TREE_OBJECTID
:
212 case BTRFS_ORPHAN_OBJECTID
:
217 case BTRFS_TREE_LOG_OBJECTID
:
222 case BTRFS_TREE_LOG_FIXUP_OBJECTID
:
227 case BTRFS_TREE_RELOC_OBJECTID
:
232 case BTRFS_DATA_RELOC_TREE_OBJECTID
:
234 printf("data reloc");
237 case BTRFS_EXTENT_CSUM_OBJECTID
:
239 printf("extent checksum");
241 case BTRFS_MULTIPLE_OBJECTIDS
:
251 if (extent_only
&& !skip
) {
252 print_extents(tree_root_scan
, buf
);
255 btrfs_print_key(&disk_key
);
257 btrfs_print_tree(tree_root_scan
, buf
, 1);
262 btrfs_release_path(root
, &path
);
264 if (tree_root_scan
== root
->fs_info
->tree_root
&&
265 root
->fs_info
->log_root_tree
) {
266 tree_root_scan
= root
->fs_info
->log_root_tree
;
270 if (extent_only
|| device_only
)
273 printf("total bytes %llu\n",
274 (unsigned long long)btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
275 printf("bytes used %llu\n",
276 (unsigned long long)btrfs_super_bytes_used(&root
->fs_info
->super_copy
));
278 uuid_unparse(root
->fs_info
->super_copy
.fsid
, uuidbuf
);
279 printf("uuid %s\n", uuidbuf
);
280 printf("%s\n", BTRFS_BUILD_VERSION
);