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_extent_leaf(struct btrfs_root
*root
, struct extent_buffer
*l
)
41 struct btrfs_item
*item
;
42 struct btrfs_extent_ref
*ref
;
45 static u64 last_len
= 0;
46 u32 nr
= btrfs_header_nritems(l
);
49 for (i
= 0 ; i
< nr
; i
++) {
50 item
= btrfs_item_nr(l
, i
);
51 btrfs_item_key_to_cpu(l
, &key
, i
);
52 type
= btrfs_key_type(&key
);
54 case BTRFS_EXTENT_ITEM_KEY
:
55 last_len
= key
.offset
;
58 case BTRFS_EXTENT_REF_KEY
:
59 ref
= btrfs_item_ptr(l
, i
, struct btrfs_extent_ref
);
60 printf("%llu %llu extent back ref root %llu gen %llu "
61 "owner %llu num_refs %lu\n",
62 (unsigned long long)last
,
63 (unsigned long long)last_len
,
64 (unsigned long long)btrfs_ref_root(l
, ref
),
65 (unsigned long long)btrfs_ref_generation(l
, ref
),
66 (unsigned long long)btrfs_ref_objectid(l
, ref
),
67 (unsigned long)btrfs_ref_num_refs(l
, ref
));
74 static void print_extents(struct btrfs_root
*root
, struct extent_buffer
*eb
)
82 if (btrfs_is_leaf(eb
)) {
83 print_extent_leaf(root
, eb
);
86 size
= btrfs_level_size(root
, btrfs_header_level(eb
) - 1);
87 nr
= btrfs_header_nritems(eb
);
88 for (i
= 0; i
< nr
; i
++) {
89 struct extent_buffer
*next
= read_tree_block(root
,
90 btrfs_node_blockptr(eb
, i
),
92 btrfs_node_ptr_generation(eb
, i
));
93 if (btrfs_is_leaf(next
) &&
94 btrfs_header_level(eb
) != 1)
96 if (btrfs_header_level(next
) !=
97 btrfs_header_level(eb
) - 1)
99 print_extents(root
, next
);
100 free_extent_buffer(next
);
104 int main(int ac
, char **av
)
106 struct btrfs_root
*root
;
107 struct btrfs_path path
;
108 struct btrfs_key key
;
109 struct btrfs_root_item ri
;
110 struct extent_buffer
*leaf
;
111 struct btrfs_disk_key disk_key
;
112 struct btrfs_key found_key
;
117 struct btrfs_root
*tree_root_scan
;
123 c
= getopt(ac
, av
, "e");
138 root
= open_ctree(av
[optind
], 0, 0);
140 fprintf(stderr
, "unable to open %s\n", av
[optind
]);
144 printf("root tree\n");
145 btrfs_print_tree(root
->fs_info
->tree_root
,
146 root
->fs_info
->tree_root
->node
);
148 printf("chunk tree\n");
149 btrfs_print_tree(root
->fs_info
->chunk_root
,
150 root
->fs_info
->chunk_root
->node
);
152 tree_root_scan
= root
->fs_info
->tree_root
;
154 btrfs_init_path(&path
);
158 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
159 ret
= btrfs_search_slot(NULL
, tree_root_scan
, &key
, &path
, 0, 0);
162 leaf
= path
.nodes
[0];
163 slot
= path
.slots
[0];
164 if (slot
>= btrfs_header_nritems(leaf
)) {
165 ret
= btrfs_next_leaf(tree_root_scan
, &path
);
168 leaf
= path
.nodes
[0];
169 slot
= path
.slots
[0];
171 btrfs_item_key(leaf
, &disk_key
, path
.slots
[0]);
172 btrfs_disk_key_to_cpu(&found_key
, &disk_key
);
173 if (btrfs_key_type(&found_key
) == BTRFS_ROOT_ITEM_KEY
) {
174 unsigned long offset
;
175 struct extent_buffer
*buf
;
176 int skip
= extent_only
;
178 offset
= btrfs_item_ptr_offset(leaf
, slot
);
179 read_extent_buffer(leaf
, &ri
, offset
, sizeof(ri
));
180 buf
= read_tree_block(tree_root_scan
,
181 btrfs_root_bytenr(&ri
),
182 tree_root_scan
->leafsize
, 0);
183 switch(found_key
.objectid
) {
184 case BTRFS_ROOT_TREE_OBJECTID
:
188 case BTRFS_EXTENT_TREE_OBJECTID
:
193 case BTRFS_CHUNK_TREE_OBJECTID
:
198 case BTRFS_DEV_TREE_OBJECTID
:
203 case BTRFS_FS_TREE_OBJECTID
:
208 case BTRFS_ROOT_TREE_DIR_OBJECTID
:
213 case BTRFS_CSUM_TREE_OBJECTID
:
218 case BTRFS_ORPHAN_OBJECTID
:
223 case BTRFS_TREE_LOG_OBJECTID
:
228 case BTRFS_TREE_LOG_FIXUP_OBJECTID
:
233 case BTRFS_TREE_RELOC_OBJECTID
:
238 case BTRFS_DATA_RELOC_TREE_OBJECTID
:
240 printf("data reloc");
243 case BTRFS_EXTENT_CSUM_OBJECTID
:
245 printf("extent checksum");
247 case BTRFS_MULTIPLE_OBJECTIDS
:
257 if (!skip
&& !extent_only
) {
259 btrfs_print_key(&disk_key
);
261 btrfs_print_tree(tree_root_scan
, buf
);
262 } else if (extent_only
&& !skip
) {
263 print_extents(tree_root_scan
, buf
);
268 btrfs_release_path(root
, &path
);
270 if (tree_root_scan
== root
->fs_info
->tree_root
&&
271 root
->fs_info
->log_root_tree
) {
272 tree_root_scan
= root
->fs_info
->log_root_tree
;
279 printf("total bytes %llu\n",
280 (unsigned long long)btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
281 printf("bytes used %llu\n",
282 (unsigned long long)btrfs_super_bytes_used(&root
->fs_info
->super_copy
));
284 uuid_unparse(root
->fs_info
->super_copy
.fsid
, uuidbuf
);
285 printf("uuid %s\n", uuidbuf
);
286 printf("%s\n", BTRFS_BUILD_VERSION
);