debug-tree output tree/key type instead of id
[platform/upstream/btrfs-progs.git] / debug-tree.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
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"
30
31 static int print_usage(void)
32 {
33         fprintf(stderr, "usage: debug-tree [ -e ] device\n");
34         fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
35         exit(1);
36 }
37
38 static void print_extent_leaf(struct btrfs_root *root, struct extent_buffer *l)
39 {
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;
48
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                 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));
68                         break;
69                 };
70                 fflush(stdout);
71         }
72 }
73
74 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
75 {
76         int i;
77         u32 nr;
78         u32 size;
79
80         if (!eb)
81                 return;
82         if (btrfs_is_leaf(eb)) {
83                 print_extent_leaf(root, eb);
84                 return;
85         }
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),
91                                              size,
92                                              btrfs_node_ptr_generation(eb, i));
93                 if (btrfs_is_leaf(next) &&
94                     btrfs_header_level(eb) != 1)
95                         BUG();
96                 if (btrfs_header_level(next) !=
97                         btrfs_header_level(eb) - 1)
98                         BUG();
99                 print_extents(root, next);
100                 free_extent_buffer(next);
101         }
102 }
103
104 int main(int ac, char **av)
105 {
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_key found_key;
112         char uuidbuf[37];
113         int ret;
114         int slot;
115         int extent_only = 0;
116
117         radix_tree_init();
118
119         while(1) {
120                 int c;
121                 c = getopt(ac, av, "e");
122                 if (c < 0)
123                         break;
124                 switch(c) {
125                         case 'e':
126                                 extent_only = 1;
127                                 break;
128                         default:
129                                 print_usage();
130                 }
131         }
132         ac = ac - optind;
133         if (ac != 1)
134                 print_usage();
135
136         root = open_ctree(av[optind], 0, 0);
137         if (!root) {
138                 fprintf(stderr, "unable to open %s\n", av[optind]);
139                 exit(1);
140         }
141         if (!extent_only) {
142                 printf("root tree\n");
143                 btrfs_print_tree(root->fs_info->tree_root,
144                                  root->fs_info->tree_root->node);
145
146                 printf("chunk tree\n");
147                 btrfs_print_tree(root->fs_info->chunk_root,
148                                  root->fs_info->chunk_root->node);
149         }
150         btrfs_init_path(&path);
151         key.offset = 0;
152         key.objectid = 0;
153         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
154         ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
155                                         &key, &path, 0, 0);
156         BUG_ON(ret < 0);
157         while(1) {
158                 leaf = path.nodes[0];
159                 slot = path.slots[0];
160                 if (slot >= btrfs_header_nritems(leaf)) {
161                         ret = btrfs_next_leaf(root, &path);
162                         if (ret != 0)
163                                 break;
164                         leaf = path.nodes[0];
165                         slot = path.slots[0];
166                 }
167                 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
168                 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
169                         unsigned long offset;
170                         struct extent_buffer *buf;
171                         int skip = extent_only;
172
173                         offset = btrfs_item_ptr_offset(leaf, slot);
174                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
175                         buf = read_tree_block(root->fs_info->tree_root,
176                                               btrfs_root_bytenr(&ri),
177                                               root->leafsize, 0);
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                                 skip = 0;
185                                 if (!extent_only)
186                                         printf("extent");
187                                 break;
188                         case BTRFS_CHUNK_TREE_OBJECTID:
189                                 if (!skip) {
190                                         printf("chunk");
191                                 }
192                                 break;
193                         case BTRFS_DEV_TREE_OBJECTID:
194                                 if (!skip) {
195                                         printf("device");
196                                 }
197                                 break;
198                         case BTRFS_FS_TREE_OBJECTID:
199                                 if (!skip) {
200                                         printf("fs");
201                                 }
202                                 break;
203                         case BTRFS_ROOT_TREE_DIR_OBJECTID:
204                                 if (!skip) {
205                                         printf("directory");
206                                 }
207                                 break;
208                         case BTRFS_CSUM_TREE_OBJECTID:
209                                 if (!skip) {
210                                         printf("checksum");
211                                 }
212                                 break;
213                         case BTRFS_ORPHAN_OBJECTID:
214                                 if (!skip) {
215                                         printf("orphan");
216                                 }
217                                 break;
218                         case BTRFS_TREE_LOG_OBJECTID:
219                                 if (!skip) {
220                                         printf("log");
221                                 }
222                                 break;
223                         case BTRFS_TREE_LOG_FIXUP_OBJECTID:
224                                 if (!skip) {
225                                         printf("log fixup");
226                                 }
227                                 break;
228                         case BTRFS_TREE_RELOC_OBJECTID:
229                                 if (!skip) {
230                                         printf("reloc");
231                                 }
232                                 break;
233                         case BTRFS_DATA_RELOC_TREE_OBJECTID:
234                                 if (!skip) {
235                                         printf("data reloc");
236                                 }
237                                 break;
238                         case BTRFS_EXTENT_CSUM_OBJECTID:
239                                 if (!skip) {
240                                         printf("extent checksum");
241                                 }
242                         case BTRFS_MULTIPLE_OBJECTIDS:
243                                 if (!skip) {
244                                         printf("multiple");
245                                 }
246                                 break;
247                         default:
248                                 if (!skip) {
249                                         printf("file");
250                                 }
251                         }
252                         if (!skip && !extent_only) {
253                                 printf(" tree (%llu %u %llu)\n",
254                                        (unsigned long long)found_key.objectid,
255                                        found_key.type,
256                                        (unsigned long long)found_key.offset);
257                                 btrfs_print_tree(root, buf);
258                         } else if (extent_only && !skip) {
259                                 print_extents(root, buf);
260                         }
261                 }
262                 path.slots[0]++;
263         }
264         btrfs_release_path(root, &path);
265         if (extent_only)
266                 return 0;
267
268         printf("total bytes %llu\n",
269                (unsigned long long)btrfs_super_total_bytes(&root->fs_info->super_copy));
270         printf("bytes used %llu\n",
271                (unsigned long long)btrfs_super_bytes_used(&root->fs_info->super_copy));
272         uuidbuf[36] = '\0';
273         uuid_unparse(root->fs_info->super_copy.fsid, uuidbuf);
274         printf("uuid %s\n", uuidbuf);
275         printf("%s\n", BTRFS_BUILD_VERSION);
276         return 0;
277 }