btrfs-progs: add a recovery utility to pull files from damanged filesystems
[btrfs-progs-unstable/devel.git] / debug-tree.c
blob34cefe9fb3a18a2aca9fcfd413746504be4d46e5
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 u64 block_only = 0;
80 struct btrfs_root *tree_root_scan;
82 radix_tree_init();
84 while(1) {
85 int c;
86 c = getopt(ac, av, "deb:");
87 if (c < 0)
88 break;
89 switch(c) {
90 case 'e':
91 extent_only = 1;
92 break;
93 case 'd':
94 device_only = 1;
95 break;
96 case 'b':
97 block_only = atoll(optarg);
98 break;
99 default:
100 print_usage();
103 ac = ac - optind;
104 if (ac != 1)
105 print_usage();
107 root = open_ctree(av[optind], 0, 0);
108 if (!root) {
109 fprintf(stderr, "unable to open %s\n", av[optind]);
110 exit(1);
112 if (block_only) {
113 leaf = read_tree_block(root,
114 block_only,
115 root->leafsize, 0);
117 if (leaf && btrfs_header_level(leaf) != 0) {
118 free_extent_buffer(leaf);
119 leaf = NULL;
122 if (!leaf) {
123 leaf = read_tree_block(root,
124 block_only,
125 root->nodesize, 0);
127 if (!leaf) {
128 fprintf(stderr, "failed to read %llu\n",
129 (unsigned long long)block_only);
130 return 0;
132 btrfs_print_tree(root, leaf, 0);
133 return 0;
136 if (!extent_only) {
137 printf("root tree\n");
138 btrfs_print_tree(root->fs_info->tree_root,
139 root->fs_info->tree_root->node, 1);
141 printf("chunk tree\n");
142 btrfs_print_tree(root->fs_info->chunk_root,
143 root->fs_info->chunk_root->node, 1);
145 tree_root_scan = root->fs_info->tree_root;
147 btrfs_init_path(&path);
148 again:
149 key.offset = 0;
150 key.objectid = 0;
151 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
152 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
153 BUG_ON(ret < 0);
154 while(1) {
155 leaf = path.nodes[0];
156 slot = path.slots[0];
157 if (slot >= btrfs_header_nritems(leaf)) {
158 ret = btrfs_next_leaf(tree_root_scan, &path);
159 if (ret != 0)
160 break;
161 leaf = path.nodes[0];
162 slot = path.slots[0];
164 btrfs_item_key(leaf, &disk_key, path.slots[0]);
165 btrfs_disk_key_to_cpu(&found_key, &disk_key);
166 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
167 unsigned long offset;
168 struct extent_buffer *buf;
169 int skip = extent_only | device_only;
171 offset = btrfs_item_ptr_offset(leaf, slot);
172 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
173 buf = read_tree_block(tree_root_scan,
174 btrfs_root_bytenr(&ri),
175 btrfs_level_size(tree_root_scan,
176 btrfs_root_level(&ri)),
178 switch(found_key.objectid) {
179 case BTRFS_ROOT_TREE_OBJECTID:
180 if (!skip)
181 printf("root");
182 break;
183 case BTRFS_EXTENT_TREE_OBJECTID:
184 if (!device_only)
185 skip = 0;
186 if (!extent_only && !device_only)
187 printf("extent");
188 break;
189 case BTRFS_CHUNK_TREE_OBJECTID:
190 if (!skip) {
191 printf("chunk");
193 break;
194 case BTRFS_DEV_TREE_OBJECTID:
195 skip = 0;
196 printf("device");
197 break;
198 case BTRFS_FS_TREE_OBJECTID:
199 if (!skip) {
200 printf("fs");
202 break;
203 case BTRFS_ROOT_TREE_DIR_OBJECTID:
204 skip = 0;
205 printf("directory");
206 break;
207 case BTRFS_CSUM_TREE_OBJECTID:
208 if (!skip) {
209 printf("checksum");
211 break;
212 case BTRFS_ORPHAN_OBJECTID:
213 if (!skip) {
214 printf("orphan");
216 break;
217 case BTRFS_TREE_LOG_OBJECTID:
218 if (!skip) {
219 printf("log");
221 break;
222 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
223 if (!skip) {
224 printf("log fixup");
226 break;
227 case BTRFS_TREE_RELOC_OBJECTID:
228 if (!skip) {
229 printf("reloc");
231 break;
232 case BTRFS_DATA_RELOC_TREE_OBJECTID:
233 if (!skip) {
234 printf("data reloc");
236 break;
237 case BTRFS_EXTENT_CSUM_OBJECTID:
238 if (!skip) {
239 printf("extent checksum");
241 case BTRFS_MULTIPLE_OBJECTIDS:
242 if (!skip) {
243 printf("multiple");
245 break;
246 default:
247 if (!skip) {
248 printf("file");
251 if (extent_only && !skip) {
252 print_extents(tree_root_scan, buf);
253 } else if (!skip) {
254 printf(" tree ");
255 btrfs_print_key(&disk_key);
256 printf(" \n");
257 btrfs_print_tree(tree_root_scan, buf, 1);
260 path.slots[0]++;
262 btrfs_release_path(root, &path);
264 if (tree_root_scan == root->fs_info->tree_root &&
265 root->fs_info->log_root_tree) {
266 tree_root_scan = root->fs_info->log_root_tree;
267 goto again;
270 if (extent_only || device_only)
271 return 0;
273 printf("total bytes %llu\n",
274 (unsigned long long)btrfs_super_total_bytes(&root->fs_info->super_copy));
275 printf("bytes used %llu\n",
276 (unsigned long long)btrfs_super_bytes_used(&root->fs_info->super_copy));
277 uuidbuf[36] = '\0';
278 uuid_unparse(root->fs_info->super_copy.fsid, uuidbuf);
279 printf("uuid %s\n", uuidbuf);
280 printf("%s\n", BTRFS_BUILD_VERSION);
281 return 0;