Btrfsck: add the ability to prune corrupt extent allocation tree blocks
[btrfs-progs-unstable/devel.git] / debug-tree.c
blobc497892e7e77eb89c47c0b40b5fcef02f29523cf
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 static void print_old_roots(struct btrfs_super_block *super)
67 struct btrfs_root_backup *backup;
68 int i;
70 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
71 backup = super->super_roots + i;
72 printf("btrfs root backup slot %d\n", i);
73 printf("\ttree root gen %llu block %llu\n",
74 (unsigned long long)btrfs_backup_tree_root_gen(backup),
75 (unsigned long long)btrfs_backup_tree_root(backup));
77 printf("\t\textent root gen %llu block %llu\n",
78 (unsigned long long)btrfs_backup_extent_root_gen(backup),
79 (unsigned long long)btrfs_backup_extent_root(backup));
81 printf("\t\tchunk root gen %llu block %llu\n",
82 (unsigned long long)btrfs_backup_chunk_root_gen(backup),
83 (unsigned long long)btrfs_backup_chunk_root(backup));
85 printf("\t\tdevice root gen %llu block %llu\n",
86 (unsigned long long)btrfs_backup_dev_root_gen(backup),
87 (unsigned long long)btrfs_backup_dev_root(backup));
89 printf("\t\tcsum root gen %llu block %llu\n",
90 (unsigned long long)btrfs_backup_csum_root_gen(backup),
91 (unsigned long long)btrfs_backup_csum_root(backup));
93 printf("\t\tfs root gen %llu block %llu\n",
94 (unsigned long long)btrfs_backup_fs_root_gen(backup),
95 (unsigned long long)btrfs_backup_fs_root(backup));
97 printf("\t\t%llu used %llu total %llu devices\n",
98 (unsigned long long)btrfs_backup_bytes_used(backup),
99 (unsigned long long)btrfs_backup_total_bytes(backup),
100 (unsigned long long)btrfs_backup_num_devices(backup));
104 int main(int ac, char **av)
106 struct btrfs_root *root;
107 struct btrfs_fs_info *info;
108 struct btrfs_path path;
109 struct btrfs_key key;
110 struct btrfs_root_item ri;
111 struct extent_buffer *leaf;
112 struct btrfs_disk_key disk_key;
113 struct btrfs_key found_key;
114 char uuidbuf[37];
115 int ret;
116 int slot;
117 int extent_only = 0;
118 int device_only = 0;
119 int roots_only = 0;
120 int root_backups = 0;
121 u64 block_only = 0;
122 struct btrfs_root *tree_root_scan;
124 radix_tree_init();
126 while(1) {
127 int c;
128 c = getopt(ac, av, "deb:rR");
129 if (c < 0)
130 break;
131 switch(c) {
132 case 'e':
133 extent_only = 1;
134 break;
135 case 'd':
136 device_only = 1;
137 break;
138 case 'r':
139 roots_only = 1;
140 break;
141 case 'R':
142 roots_only = 1;
143 root_backups = 1;
144 break;
145 case 'b':
146 block_only = atoll(optarg);
147 break;
148 default:
149 print_usage();
152 ac = ac - optind;
153 if (ac != 1)
154 print_usage();
156 info = open_ctree_fs_info(av[optind], 0, 0, 1);
157 if (!info) {
158 fprintf(stderr, "unable to open %s\n", av[optind]);
159 exit(1);
161 root = info->fs_root;
163 if (block_only) {
164 if (!root) {
165 fprintf(stderr, "unable to open %s\n", av[optind]);
166 exit(1);
168 leaf = read_tree_block(root,
169 block_only,
170 root->leafsize, 0);
172 if (leaf && btrfs_header_level(leaf) != 0) {
173 free_extent_buffer(leaf);
174 leaf = NULL;
177 if (!leaf) {
178 leaf = read_tree_block(root,
179 block_only,
180 root->nodesize, 0);
182 if (!leaf) {
183 fprintf(stderr, "failed to read %llu\n",
184 (unsigned long long)block_only);
185 return 0;
187 btrfs_print_tree(root, leaf, 0);
188 return 0;
191 if (!extent_only) {
192 if (roots_only) {
193 printf("root tree: %llu level %d\n",
194 (unsigned long long)info->tree_root->node->start,
195 btrfs_header_level(info->tree_root->node));
196 printf("chunk tree: %llu level %d\n",
197 (unsigned long long)info->chunk_root->node->start,
198 btrfs_header_level(info->chunk_root->node));
199 } else {
200 if (info->tree_root->node) {
201 printf("root tree\n");
202 btrfs_print_tree(info->tree_root,
203 info->tree_root->node, 1);
206 if (info->chunk_root->node) {
207 printf("chunk tree\n");
208 btrfs_print_tree(info->chunk_root,
209 info->chunk_root->node, 1);
213 tree_root_scan = info->tree_root;
215 btrfs_init_path(&path);
216 again:
217 if (!extent_buffer_uptodate(tree_root_scan->node))
218 goto no_node;
220 key.offset = 0;
221 key.objectid = 0;
222 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
223 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
224 BUG_ON(ret < 0);
225 while(1) {
226 leaf = path.nodes[0];
227 slot = path.slots[0];
228 if (slot >= btrfs_header_nritems(leaf)) {
229 ret = btrfs_next_leaf(tree_root_scan, &path);
230 if (ret != 0)
231 break;
232 leaf = path.nodes[0];
233 slot = path.slots[0];
235 btrfs_item_key(leaf, &disk_key, path.slots[0]);
236 btrfs_disk_key_to_cpu(&found_key, &disk_key);
237 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
238 unsigned long offset;
239 struct extent_buffer *buf;
240 int skip = extent_only | device_only;
242 offset = btrfs_item_ptr_offset(leaf, slot);
243 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
244 buf = read_tree_block(tree_root_scan,
245 btrfs_root_bytenr(&ri),
246 btrfs_level_size(tree_root_scan,
247 btrfs_root_level(&ri)),
249 if (!extent_buffer_uptodate(buf))
250 goto next;
252 switch(found_key.objectid) {
253 case BTRFS_ROOT_TREE_OBJECTID:
254 if (!skip)
255 printf("root");
256 break;
257 case BTRFS_EXTENT_TREE_OBJECTID:
258 if (!device_only)
259 skip = 0;
260 if (!extent_only && !device_only)
261 printf("extent");
262 break;
263 case BTRFS_CHUNK_TREE_OBJECTID:
264 if (!skip) {
265 printf("chunk");
267 break;
268 case BTRFS_DEV_TREE_OBJECTID:
269 skip = 0;
270 printf("device");
271 break;
272 case BTRFS_FS_TREE_OBJECTID:
273 if (!skip) {
274 printf("fs");
276 break;
277 case BTRFS_ROOT_TREE_DIR_OBJECTID:
278 skip = 0;
279 printf("directory");
280 break;
281 case BTRFS_CSUM_TREE_OBJECTID:
282 if (!skip) {
283 printf("checksum");
285 break;
286 case BTRFS_ORPHAN_OBJECTID:
287 if (!skip) {
288 printf("orphan");
290 break;
291 case BTRFS_TREE_LOG_OBJECTID:
292 if (!skip) {
293 printf("log");
295 break;
296 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
297 if (!skip) {
298 printf("log fixup");
300 break;
301 case BTRFS_TREE_RELOC_OBJECTID:
302 if (!skip) {
303 printf("reloc");
305 break;
306 case BTRFS_DATA_RELOC_TREE_OBJECTID:
307 if (!skip) {
308 printf("data reloc");
310 break;
311 case BTRFS_EXTENT_CSUM_OBJECTID:
312 if (!skip) {
313 printf("extent checksum");
315 case BTRFS_MULTIPLE_OBJECTIDS:
316 if (!skip) {
317 printf("multiple");
319 break;
320 default:
321 if (!skip) {
322 printf("file");
325 if (extent_only && !skip) {
326 print_extents(tree_root_scan, buf);
327 } else if (!skip) {
328 printf(" tree ");
329 btrfs_print_key(&disk_key);
330 if (roots_only) {
331 printf(" %llu level %d\n",
332 (unsigned long long)buf->start,
333 btrfs_header_level(buf));
334 } else {
335 printf(" \n");
336 btrfs_print_tree(tree_root_scan, buf, 1);
340 next:
341 path.slots[0]++;
343 no_node:
344 btrfs_release_path(root, &path);
346 if (tree_root_scan == info->tree_root &&
347 info->log_root_tree) {
348 tree_root_scan = info->log_root_tree;
349 goto again;
352 if (extent_only || device_only)
353 return 0;
355 if (root_backups)
356 print_old_roots(&info->super_copy);
358 printf("total bytes %llu\n",
359 (unsigned long long)btrfs_super_total_bytes(&info->super_copy));
360 printf("bytes used %llu\n",
361 (unsigned long long)btrfs_super_bytes_used(&info->super_copy));
362 uuidbuf[36] = '\0';
363 uuid_unparse(info->super_copy.fsid, uuidbuf);
364 printf("uuid %s\n", uuidbuf);
365 printf("%s\n", BTRFS_BUILD_VERSION);
366 return 0;