Fix missing entries in listing of subvolumes
[btrfs-progs-unstable/devel.git] / debug-tree.c
blob91e0064f311d50aa1d51b036b99191f8170a76a4
1 /*
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <uuid/uuid.h>
23 #include "kerncompat.h"
24 #include "radix-tree.h"
25 #include "ctree.h"
26 #include "disk-io.h"
27 #include "print-tree.h"
28 #include "transaction.h"
29 #include "version.h"
31 static int print_usage(void)
33 fprintf(stderr, "usage: debug-tree [ -e ] device\n");
34 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
35 exit(1);
38 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
40 int i;
41 u32 nr;
42 u32 size;
44 if (!eb)
45 return;
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),
52 size,
53 btrfs_node_ptr_generation(eb, i));
54 if (btrfs_is_leaf(next) &&
55 btrfs_header_level(eb) != 1)
56 BUG();
57 if (btrfs_header_level(next) !=
58 btrfs_header_level(eb) - 1)
59 BUG();
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;
69 struct btrfs_key key;
70 struct btrfs_root_item ri;
71 struct extent_buffer *leaf;
72 struct btrfs_disk_key disk_key;
73 struct btrfs_key found_key;
74 char uuidbuf[37];
75 int ret;
76 int slot;
77 int extent_only = 0;
78 int device_only = 0;
79 int roots_only = 0;
80 u64 block_only = 0;
81 struct btrfs_root *tree_root_scan;
83 radix_tree_init();
85 while(1) {
86 int c;
87 c = getopt(ac, av, "deb:r");
88 if (c < 0)
89 break;
90 switch(c) {
91 case 'e':
92 extent_only = 1;
93 break;
94 case 'd':
95 device_only = 1;
96 break;
97 case 'r':
98 roots_only = 1;
99 break;
100 case 'b':
101 block_only = atoll(optarg);
102 break;
103 default:
104 print_usage();
107 ac = ac - optind;
108 if (ac != 1)
109 print_usage();
111 root = open_ctree(av[optind], 0, 0);
112 if (!root) {
113 fprintf(stderr, "unable to open %s\n", av[optind]);
114 exit(1);
116 if (block_only) {
117 leaf = read_tree_block(root,
118 block_only,
119 root->leafsize, 0);
121 if (leaf && btrfs_header_level(leaf) != 0) {
122 free_extent_buffer(leaf);
123 leaf = NULL;
126 if (!leaf) {
127 leaf = read_tree_block(root,
128 block_only,
129 root->nodesize, 0);
131 if (!leaf) {
132 fprintf(stderr, "failed to read %llu\n",
133 (unsigned long long)block_only);
134 return 0;
136 btrfs_print_tree(root, leaf, 0);
137 return 0;
140 if (!extent_only) {
141 if (roots_only) {
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));
148 } else {
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);
161 again:
162 key.offset = 0;
163 key.objectid = 0;
164 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
165 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
166 BUG_ON(ret < 0);
167 while(1) {
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);
172 if (ret != 0)
173 break;
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:
193 if (!skip)
194 printf("root");
195 break;
196 case BTRFS_EXTENT_TREE_OBJECTID:
197 if (!device_only)
198 skip = 0;
199 if (!extent_only && !device_only)
200 printf("extent");
201 break;
202 case BTRFS_CHUNK_TREE_OBJECTID:
203 if (!skip) {
204 printf("chunk");
206 break;
207 case BTRFS_DEV_TREE_OBJECTID:
208 skip = 0;
209 printf("device");
210 break;
211 case BTRFS_FS_TREE_OBJECTID:
212 if (!skip) {
213 printf("fs");
215 break;
216 case BTRFS_ROOT_TREE_DIR_OBJECTID:
217 skip = 0;
218 printf("directory");
219 break;
220 case BTRFS_CSUM_TREE_OBJECTID:
221 if (!skip) {
222 printf("checksum");
224 break;
225 case BTRFS_ORPHAN_OBJECTID:
226 if (!skip) {
227 printf("orphan");
229 break;
230 case BTRFS_TREE_LOG_OBJECTID:
231 if (!skip) {
232 printf("log");
234 break;
235 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
236 if (!skip) {
237 printf("log fixup");
239 break;
240 case BTRFS_TREE_RELOC_OBJECTID:
241 if (!skip) {
242 printf("reloc");
244 break;
245 case BTRFS_DATA_RELOC_TREE_OBJECTID:
246 if (!skip) {
247 printf("data reloc");
249 break;
250 case BTRFS_EXTENT_CSUM_OBJECTID:
251 if (!skip) {
252 printf("extent checksum");
254 case BTRFS_MULTIPLE_OBJECTIDS:
255 if (!skip) {
256 printf("multiple");
258 break;
259 default:
260 if (!skip) {
261 printf("file");
264 if (extent_only && !skip) {
265 print_extents(tree_root_scan, buf);
266 } else if (!skip) {
267 printf(" tree ");
268 btrfs_print_key(&disk_key);
269 if (roots_only) {
270 printf(" %llu level %d\n",
271 (unsigned long long)buf->start,
272 btrfs_header_level(buf));
273 } else {
274 printf(" \n");
275 btrfs_print_tree(tree_root_scan, buf, 1);
279 path.slots[0]++;
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;
286 goto again;
289 if (extent_only || device_only)
290 return 0;
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));
296 uuidbuf[36] = '\0';
297 uuid_unparse(root->fs_info->super_copy.fsid, uuidbuf);
298 printf("uuid %s\n", uuidbuf);
299 printf("%s\n", BTRFS_BUILD_VERSION);
300 return 0;