btrfs-progs: fix wrong extent buffer size when reading tree block
[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 #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
71                 };
72                 fflush(stdout);
73         }
74 }
75
76 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
77 {
78         int i;
79         u32 nr;
80         u32 size;
81
82         if (!eb)
83                 return;
84         if (btrfs_is_leaf(eb)) {
85                 print_extent_leaf(root, eb);
86                 return;
87         }
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);
103         }
104 }
105
106 int main(int ac, char **av)
107 {
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         int device_only = 0;
120         u64 block_only = 0;
121         struct btrfs_root *tree_root_scan;
122
123         radix_tree_init();
124
125         while(1) {
126                 int c;
127                 c = getopt(ac, av, "deb:");
128                 if (c < 0)
129                         break;
130                 switch(c) {
131                         case 'e':
132                                 extent_only = 1;
133                                 break;
134                         case 'd':
135                                 device_only = 1;
136                                 break;
137                         case 'b':
138                                 block_only = atoll(optarg);
139                                 break;
140                         default:
141                                 print_usage();
142                 }
143         }
144         ac = ac - optind;
145         if (ac != 1)
146                 print_usage();
147
148         root = open_ctree(av[optind], 0, 0);
149         if (!root) {
150                 fprintf(stderr, "unable to open %s\n", av[optind]);
151                 exit(1);
152         }
153         if (block_only) {
154                 leaf = read_tree_block(root,
155                                       block_only,
156                                       root->leafsize, 0);
157
158                 if (leaf && btrfs_header_level(leaf) != 0) {
159                         free_extent_buffer(leaf);
160                         leaf = NULL;
161                 }
162
163                 if (!leaf) {
164                         leaf = read_tree_block(root,
165                                               block_only,
166                                               root->nodesize, 0);
167                 }
168                 if (!leaf) {
169                         fprintf(stderr, "failed to read %llu\n", block_only);
170                         return 0;
171                 }
172                 btrfs_print_tree(root, leaf, 0);
173                 return 0;
174         }
175
176         if (!extent_only) {
177                 printf("root tree\n");
178                 btrfs_print_tree(root->fs_info->tree_root,
179                                  root->fs_info->tree_root->node, 1);
180
181                 printf("chunk tree\n");
182                 btrfs_print_tree(root->fs_info->chunk_root,
183                                  root->fs_info->chunk_root->node, 1);
184         }
185         tree_root_scan = root->fs_info->tree_root;
186
187         btrfs_init_path(&path);
188 again:
189         key.offset = 0;
190         key.objectid = 0;
191         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
192         ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
193         BUG_ON(ret < 0);
194         while(1) {
195                 leaf = path.nodes[0];
196                 slot = path.slots[0];
197                 if (slot >= btrfs_header_nritems(leaf)) {
198                         ret = btrfs_next_leaf(tree_root_scan, &path);
199                         if (ret != 0)
200                                 break;
201                         leaf = path.nodes[0];
202                         slot = path.slots[0];
203                 }
204                 btrfs_item_key(leaf, &disk_key, path.slots[0]);
205                 btrfs_disk_key_to_cpu(&found_key, &disk_key);
206                 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
207                         unsigned long offset;
208                         struct extent_buffer *buf;
209                         int skip = extent_only | device_only;
210
211                         offset = btrfs_item_ptr_offset(leaf, slot);
212                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
213                         buf = read_tree_block(tree_root_scan,
214                                               btrfs_root_bytenr(&ri),
215                                               btrfs_level_size(tree_root_scan,
216                                                         btrfs_root_level(&ri)),
217                                               0);
218                         switch(found_key.objectid) {
219                         case BTRFS_ROOT_TREE_OBJECTID:
220                                 if (!skip)
221                                         printf("root");
222                                 break;
223                         case BTRFS_EXTENT_TREE_OBJECTID:
224                                 if (!device_only)
225                                         skip = 0;
226                                 if (!extent_only && !device_only)
227                                         printf("extent");
228                                 break;
229                         case BTRFS_CHUNK_TREE_OBJECTID:
230                                 if (!skip) {
231                                         printf("chunk");
232                                 }
233                                 break;
234                         case BTRFS_DEV_TREE_OBJECTID:
235                                 skip = 0;
236                                 printf("device");
237                                 break;
238                         case BTRFS_FS_TREE_OBJECTID:
239                                 if (!skip) {
240                                         printf("fs");
241                                 }
242                                 break;
243                         case BTRFS_ROOT_TREE_DIR_OBJECTID:
244                                 skip = 0;
245                                 printf("directory");
246                                 break;
247                         case BTRFS_CSUM_TREE_OBJECTID:
248                                 if (!skip) {
249                                         printf("checksum");
250                                 }
251                                 break;
252                         case BTRFS_ORPHAN_OBJECTID:
253                                 if (!skip) {
254                                         printf("orphan");
255                                 }
256                                 break;
257                         case BTRFS_TREE_LOG_OBJECTID:
258                                 if (!skip) {
259                                         printf("log");
260                                 }
261                                 break;
262                         case BTRFS_TREE_LOG_FIXUP_OBJECTID:
263                                 if (!skip) {
264                                         printf("log fixup");
265                                 }
266                                 break;
267                         case BTRFS_TREE_RELOC_OBJECTID:
268                                 if (!skip) {
269                                         printf("reloc");
270                                 }
271                                 break;
272                         case BTRFS_DATA_RELOC_TREE_OBJECTID:
273                                 if (!skip) {
274                                         printf("data reloc");
275                                 }
276                                 break;
277                         case BTRFS_EXTENT_CSUM_OBJECTID:
278                                 if (!skip) {
279                                         printf("extent checksum");
280                                 }
281                         case BTRFS_MULTIPLE_OBJECTIDS:
282                                 if (!skip) {
283                                         printf("multiple");
284                                 }
285                                 break;
286                         default:
287                                 if (!skip) {
288                                         printf("file");
289                                 }
290                         }
291                         if (extent_only && !skip) {
292                                 print_extents(tree_root_scan, buf);
293                         } else if (!skip) {
294                                 printf(" tree ");
295                                 btrfs_print_key(&disk_key);
296                                 printf(" \n");
297                                 btrfs_print_tree(tree_root_scan, buf, 1);
298                         }
299                 }
300                 path.slots[0]++;
301         }
302         btrfs_release_path(root, &path);
303
304         if (tree_root_scan == root->fs_info->tree_root &&
305             root->fs_info->log_root_tree) {
306                 tree_root_scan = root->fs_info->log_root_tree;
307                 goto again;
308         }
309
310         if (extent_only || device_only)
311                 return 0;
312
313         printf("total bytes %llu\n",
314                (unsigned long long)btrfs_super_total_bytes(&root->fs_info->super_copy));
315         printf("bytes used %llu\n",
316                (unsigned long long)btrfs_super_bytes_used(&root->fs_info->super_copy));
317         uuidbuf[36] = '\0';
318         uuid_unparse(root->fs_info->super_copy.fsid, uuidbuf);
319         printf("uuid %s\n", uuidbuf);
320         printf("%s\n", BTRFS_BUILD_VERSION);
321         return 0;
322 }