Fix inode link count checks in btrfsck
[btrfs-progs-unstable.git] / debug-tree.c
blob40628e09cc1098e6772d1ddf6ccd207fc62f38b0
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_extent_leaf(struct btrfs_root *root, struct extent_buffer *l)
40 int i;
41 struct btrfs_item *item;
42 // struct btrfs_extent_ref *ref;
43 struct btrfs_key key;
44 static u64 last = 0;
45 static u64 last_len = 0;
46 u32 nr = btrfs_header_nritems(l);
47 u32 type;
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);
53 switch (type) {
54 case BTRFS_EXTENT_ITEM_KEY:
55 last_len = key.offset;
56 last = key.objectid;
57 break;
58 #if 0
59 case BTRFS_EXTENT_REF_KEY:
60 ref = btrfs_item_ptr(l, i, struct btrfs_extent_ref);
61 printf("%llu %llu extent back ref root %llu gen %llu "
62 "owner %llu num_refs %lu\n",
63 (unsigned long long)last,
64 (unsigned long long)last_len,
65 (unsigned long long)btrfs_ref_root(l, ref),
66 (unsigned long long)btrfs_ref_generation(l, ref),
67 (unsigned long long)btrfs_ref_objectid(l, ref),
68 (unsigned long)btrfs_ref_num_refs(l, ref));
69 break;
70 #endif
72 fflush(stdout);
76 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
78 int i;
79 u32 nr;
80 u32 size;
82 if (!eb)
83 return;
84 if (btrfs_is_leaf(eb)) {
85 print_extent_leaf(root, eb);
86 return;
88 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
89 nr = btrfs_header_nritems(eb);
90 for (i = 0; i < nr; i++) {
91 struct extent_buffer *next = read_tree_block(root,
92 btrfs_node_blockptr(eb, i),
93 size,
94 btrfs_node_ptr_generation(eb, i));
95 if (btrfs_is_leaf(next) &&
96 btrfs_header_level(eb) != 1)
97 BUG();
98 if (btrfs_header_level(next) !=
99 btrfs_header_level(eb) - 1)
100 BUG();
101 print_extents(root, next);
102 free_extent_buffer(next);
106 int main(int ac, char **av)
108 struct btrfs_root *root;
109 struct btrfs_path path;
110 struct btrfs_key key;
111 struct btrfs_root_item ri;
112 struct extent_buffer *leaf;
113 struct btrfs_disk_key disk_key;
114 struct btrfs_key found_key;
115 char uuidbuf[37];
116 int ret;
117 int slot;
118 int extent_only = 0;
119 u64 block_only = 0;
120 struct btrfs_root *tree_root_scan;
122 radix_tree_init();
124 while(1) {
125 int c;
126 c = getopt(ac, av, "eb:");
127 if (c < 0)
128 break;
129 switch(c) {
130 case 'e':
131 extent_only = 1;
132 break;
133 case 'b':
134 block_only = atoll(optarg);
135 break;
136 default:
137 print_usage();
140 ac = ac - optind;
141 if (ac != 1)
142 print_usage();
144 root = open_ctree(av[optind], 0, 0);
145 if (!root) {
146 fprintf(stderr, "unable to open %s\n", av[optind]);
147 exit(1);
149 if (block_only) {
150 leaf = read_tree_block(root,
151 block_only,
152 root->leafsize, 0);
154 if (leaf && btrfs_header_level(leaf) != 0) {
155 free_extent_buffer(leaf);
156 leaf = NULL;
159 if (!leaf) {
160 leaf = read_tree_block(root,
161 block_only,
162 root->nodesize, 0);
164 if (!leaf) {
165 fprintf(stderr, "failed to read %llu\n", block_only);
166 return 0;
168 btrfs_print_tree(root, leaf, 0);
169 return 0;
172 if (!extent_only) {
173 printf("root tree\n");
174 btrfs_print_tree(root->fs_info->tree_root,
175 root->fs_info->tree_root->node, 1);
177 printf("chunk tree\n");
178 btrfs_print_tree(root->fs_info->chunk_root,
179 root->fs_info->chunk_root->node, 1);
181 tree_root_scan = root->fs_info->tree_root;
183 btrfs_init_path(&path);
184 again:
185 key.offset = 0;
186 key.objectid = 0;
187 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
188 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
189 BUG_ON(ret < 0);
190 while(1) {
191 leaf = path.nodes[0];
192 slot = path.slots[0];
193 if (slot >= btrfs_header_nritems(leaf)) {
194 ret = btrfs_next_leaf(tree_root_scan, &path);
195 if (ret != 0)
196 break;
197 leaf = path.nodes[0];
198 slot = path.slots[0];
200 btrfs_item_key(leaf, &disk_key, path.slots[0]);
201 btrfs_disk_key_to_cpu(&found_key, &disk_key);
202 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
203 unsigned long offset;
204 struct extent_buffer *buf;
205 int skip = extent_only;
207 offset = btrfs_item_ptr_offset(leaf, slot);
208 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
209 buf = read_tree_block(tree_root_scan,
210 btrfs_root_bytenr(&ri),
211 tree_root_scan->leafsize, 0);
212 switch(found_key.objectid) {
213 case BTRFS_ROOT_TREE_OBJECTID:
214 if (!skip)
215 printf("root");
216 break;
217 case BTRFS_EXTENT_TREE_OBJECTID:
218 skip = 0;
219 if (!extent_only)
220 printf("extent");
221 break;
222 case BTRFS_CHUNK_TREE_OBJECTID:
223 if (!skip) {
224 printf("chunk");
226 break;
227 case BTRFS_DEV_TREE_OBJECTID:
228 if (!skip) {
229 printf("device");
231 break;
232 case BTRFS_FS_TREE_OBJECTID:
233 if (!skip) {
234 printf("fs");
236 break;
237 case BTRFS_ROOT_TREE_DIR_OBJECTID:
238 if (!skip) {
239 printf("directory");
241 break;
242 case BTRFS_CSUM_TREE_OBJECTID:
243 if (!skip) {
244 printf("checksum");
246 break;
247 case BTRFS_ORPHAN_OBJECTID:
248 if (!skip) {
249 printf("orphan");
251 break;
252 case BTRFS_TREE_LOG_OBJECTID:
253 if (!skip) {
254 printf("log");
256 break;
257 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
258 if (!skip) {
259 printf("log fixup");
261 break;
262 case BTRFS_TREE_RELOC_OBJECTID:
263 if (!skip) {
264 printf("reloc");
266 break;
267 case BTRFS_DATA_RELOC_TREE_OBJECTID:
268 if (!skip) {
269 printf("data reloc");
271 break;
272 case BTRFS_EXTENT_CSUM_OBJECTID:
273 if (!skip) {
274 printf("extent checksum");
276 case BTRFS_MULTIPLE_OBJECTIDS:
277 if (!skip) {
278 printf("multiple");
280 break;
281 default:
282 if (!skip) {
283 printf("file");
286 if (!skip && !extent_only) {
287 printf(" tree ");
288 btrfs_print_key(&disk_key);
289 printf(" \n");
290 btrfs_print_tree(tree_root_scan, buf, 1);
291 } else if (extent_only && !skip) {
292 print_extents(tree_root_scan, buf);
295 path.slots[0]++;
297 btrfs_release_path(root, &path);
299 if (tree_root_scan == root->fs_info->tree_root &&
300 root->fs_info->log_root_tree) {
301 tree_root_scan = root->fs_info->log_root_tree;
302 goto again;
305 if (extent_only)
306 return 0;
308 printf("total bytes %llu\n",
309 (unsigned long long)btrfs_super_total_bytes(&root->fs_info->super_copy));
310 printf("bytes used %llu\n",
311 (unsigned long long)btrfs_super_bytes_used(&root->fs_info->super_copy));
312 uuidbuf[36] = '\0';
313 uuid_unparse(root->fs_info->super_copy.fsid, uuidbuf);
314 printf("uuid %s\n", uuidbuf);
315 printf("%s\n", BTRFS_BUILD_VERSION);
316 return 0;