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
;
81 struct btrfs_root
*tree_root_scan
;
87 c
= getopt(ac
, av
, "deb:r");
101 block_only
= atoll(optarg
);
111 root
= open_ctree(av
[optind
], 0, 0);
113 fprintf(stderr
, "unable to open %s\n", av
[optind
]);
117 leaf
= read_tree_block(root
,
121 if (leaf
&& btrfs_header_level(leaf
) != 0) {
122 free_extent_buffer(leaf
);
127 leaf
= read_tree_block(root
,
132 fprintf(stderr
, "failed to read %llu\n",
133 (unsigned long long)block_only
);
136 btrfs_print_tree(root
, leaf
, 0);
142 printf("root tree: %llu level %d\n",
143 (unsigned long long)root
->fs_info
->tree_root
->node
->start
,
144 btrfs_header_level(root
->fs_info
->tree_root
->node
));
145 printf("chunk tree: %llu level %d\n",
146 (unsigned long long)root
->fs_info
->chunk_root
->node
->start
,
147 btrfs_header_level(root
->fs_info
->chunk_root
->node
));
149 printf("root tree\n");
150 btrfs_print_tree(root
->fs_info
->tree_root
,
151 root
->fs_info
->tree_root
->node
, 1);
153 printf("chunk tree\n");
154 btrfs_print_tree(root
->fs_info
->chunk_root
,
155 root
->fs_info
->chunk_root
->node
, 1);
158 tree_root_scan
= root
->fs_info
->tree_root
;
160 btrfs_init_path(&path
);
164 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
165 ret
= btrfs_search_slot(NULL
, tree_root_scan
, &key
, &path
, 0, 0);
168 leaf
= path
.nodes
[0];
169 slot
= path
.slots
[0];
170 if (slot
>= btrfs_header_nritems(leaf
)) {
171 ret
= btrfs_next_leaf(tree_root_scan
, &path
);
174 leaf
= path
.nodes
[0];
175 slot
= path
.slots
[0];
177 btrfs_item_key(leaf
, &disk_key
, path
.slots
[0]);
178 btrfs_disk_key_to_cpu(&found_key
, &disk_key
);
179 if (btrfs_key_type(&found_key
) == BTRFS_ROOT_ITEM_KEY
) {
180 unsigned long offset
;
181 struct extent_buffer
*buf
;
182 int skip
= extent_only
| device_only
;
184 offset
= btrfs_item_ptr_offset(leaf
, slot
);
185 read_extent_buffer(leaf
, &ri
, offset
, sizeof(ri
));
186 buf
= read_tree_block(tree_root_scan
,
187 btrfs_root_bytenr(&ri
),
188 btrfs_level_size(tree_root_scan
,
189 btrfs_root_level(&ri
)),
191 switch(found_key
.objectid
) {
192 case BTRFS_ROOT_TREE_OBJECTID
:
196 case BTRFS_EXTENT_TREE_OBJECTID
:
199 if (!extent_only
&& !device_only
)
202 case BTRFS_CHUNK_TREE_OBJECTID
:
207 case BTRFS_DEV_TREE_OBJECTID
:
211 case BTRFS_FS_TREE_OBJECTID
:
216 case BTRFS_ROOT_TREE_DIR_OBJECTID
:
220 case BTRFS_CSUM_TREE_OBJECTID
:
225 case BTRFS_ORPHAN_OBJECTID
:
230 case BTRFS_TREE_LOG_OBJECTID
:
235 case BTRFS_TREE_LOG_FIXUP_OBJECTID
:
240 case BTRFS_TREE_RELOC_OBJECTID
:
245 case BTRFS_DATA_RELOC_TREE_OBJECTID
:
247 printf("data reloc");
250 case BTRFS_EXTENT_CSUM_OBJECTID
:
252 printf("extent checksum");
254 case BTRFS_MULTIPLE_OBJECTIDS
:
264 if (extent_only
&& !skip
) {
265 print_extents(tree_root_scan
, buf
);
268 btrfs_print_key(&disk_key
);
270 printf(" %llu level %d\n",
271 (unsigned long long)buf
->start
,
272 btrfs_header_level(buf
));
275 btrfs_print_tree(tree_root_scan
, buf
, 1);
281 btrfs_release_path(root
, &path
);
283 if (tree_root_scan
== root
->fs_info
->tree_root
&&
284 root
->fs_info
->log_root_tree
) {
285 tree_root_scan
= root
->fs_info
->log_root_tree
;
289 if (extent_only
|| device_only
)
292 printf("total bytes %llu\n",
293 (unsigned long long)btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
294 printf("bytes used %llu\n",
295 (unsigned long long)btrfs_super_bytes_used(&root
->fs_info
->super_copy
));
297 uuid_unparse(root
->fs_info
->super_copy
.fsid
, uuidbuf
);
298 printf("uuid %s\n", uuidbuf
);
299 printf("%s\n", BTRFS_BUILD_VERSION
);