Btrfs-progs: Add missing free_extent_buffer() call to debug-tree
[platform/upstream/btrfs-progs.git] / btrfs-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: btrfs-debug-tree [ -e ] [ -d ] [ -r ] [ -R ]\n");
34         fprintf(stderr, "                        [-b block_num ] device\n");
35         fprintf(stderr, "\t-e : print detailed extents info\n");
36         fprintf(stderr, "\t-d : print info of btrfs device and root tree dirs"
37                     " only\n");
38         fprintf(stderr, "\t-r : print info of roots only\n");
39         fprintf(stderr, "\t-R : print info of roots and root backups\n");
40         fprintf(stderr, "\t-b block_num : print info of the specified block"
41                     " only\n");
42         fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
43         exit(1);
44 }
45
46 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
47 {
48         int i;
49         u32 nr;
50         u32 size;
51
52         if (!eb)
53                 return;
54
55         if (btrfs_is_leaf(eb)) {
56                 btrfs_print_leaf(root, eb);
57                 return;
58         }
59
60         size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
61         nr = btrfs_header_nritems(eb);
62         for (i = 0; i < nr; i++) {
63                 struct extent_buffer *next = read_tree_block(root,
64                                              btrfs_node_blockptr(eb, i),
65                                              size,
66                                              btrfs_node_ptr_generation(eb, i));
67                 if (btrfs_is_leaf(next) &&
68                     btrfs_header_level(eb) != 1)
69                         BUG();
70                 if (btrfs_header_level(next) !=
71                         btrfs_header_level(eb) - 1)
72                         BUG();
73                 print_extents(root, next);
74                 free_extent_buffer(next);
75         }
76 }
77
78 static void print_old_roots(struct btrfs_super_block *super)
79 {
80         struct btrfs_root_backup *backup;
81         int i;
82
83         for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
84                 backup = super->super_roots + i;
85                 printf("btrfs root backup slot %d\n", i);
86                 printf("\ttree root gen %llu block %llu\n",
87                        (unsigned long long)btrfs_backup_tree_root_gen(backup),
88                        (unsigned long long)btrfs_backup_tree_root(backup));
89
90                 printf("\t\textent root gen %llu block %llu\n",
91                        (unsigned long long)btrfs_backup_extent_root_gen(backup),
92                        (unsigned long long)btrfs_backup_extent_root(backup));
93
94                 printf("\t\tchunk root gen %llu block %llu\n",
95                        (unsigned long long)btrfs_backup_chunk_root_gen(backup),
96                        (unsigned long long)btrfs_backup_chunk_root(backup));
97
98                 printf("\t\tdevice root gen %llu block %llu\n",
99                        (unsigned long long)btrfs_backup_dev_root_gen(backup),
100                        (unsigned long long)btrfs_backup_dev_root(backup));
101
102                 printf("\t\tcsum root gen %llu block %llu\n",
103                        (unsigned long long)btrfs_backup_csum_root_gen(backup),
104                        (unsigned long long)btrfs_backup_csum_root(backup));
105
106                 printf("\t\tfs root gen %llu block %llu\n",
107                        (unsigned long long)btrfs_backup_fs_root_gen(backup),
108                        (unsigned long long)btrfs_backup_fs_root(backup));
109
110                 printf("\t\t%llu used %llu total %llu devices\n",
111                        (unsigned long long)btrfs_backup_bytes_used(backup),
112                        (unsigned long long)btrfs_backup_total_bytes(backup),
113                        (unsigned long long)btrfs_backup_num_devices(backup));
114         }
115 }
116
117 int main(int ac, char **av)
118 {
119         struct btrfs_root *root;
120         struct btrfs_fs_info *info;
121         struct btrfs_path path;
122         struct btrfs_key key;
123         struct btrfs_root_item ri;
124         struct extent_buffer *leaf;
125         struct btrfs_disk_key disk_key;
126         struct btrfs_key found_key;
127         char uuidbuf[37];
128         int ret;
129         int slot;
130         int extent_only = 0;
131         int device_only = 0;
132         int roots_only = 0;
133         int root_backups = 0;
134         u64 block_only = 0;
135         struct btrfs_root *tree_root_scan;
136
137         radix_tree_init();
138
139         while(1) {
140                 int c;
141                 c = getopt(ac, av, "deb:rR");
142                 if (c < 0)
143                         break;
144                 switch(c) {
145                         case 'e':
146                                 extent_only = 1;
147                                 break;
148                         case 'd':
149                                 device_only = 1;
150                                 break;
151                         case 'r':
152                                 roots_only = 1;
153                                 break;
154                         case 'R':
155                                 roots_only = 1;
156                                 root_backups = 1;
157                                 break;
158                         case 'b':
159                                 block_only = atoll(optarg);
160                                 break;
161                         default:
162                                 print_usage();
163                 }
164         }
165         ac = ac - optind;
166         if (ac != 1)
167                 print_usage();
168
169         info = open_ctree_fs_info(av[optind], 0, 0, 0, 1);
170         if (!info) {
171                 fprintf(stderr, "unable to open %s\n", av[optind]);
172                 exit(1);
173         }
174         root = info->fs_root;
175
176         if (block_only) {
177                 if (!root) {
178                         fprintf(stderr, "unable to open %s\n", av[optind]);
179                         exit(1);
180                 }
181                 leaf = read_tree_block(root,
182                                       block_only,
183                                       root->leafsize, 0);
184
185                 if (leaf && btrfs_header_level(leaf) != 0) {
186                         free_extent_buffer(leaf);
187                         leaf = NULL;
188                 }
189
190                 if (!leaf) {
191                         leaf = read_tree_block(root,
192                                               block_only,
193                                               root->nodesize, 0);
194                 }
195                 if (!leaf) {
196                         fprintf(stderr, "failed to read %llu\n",
197                                 (unsigned long long)block_only);
198                         return 0;
199                 }
200                 btrfs_print_tree(root, leaf, 0);
201                 return 0;
202         }
203
204         if (!extent_only) {
205                 if (roots_only) {
206                         printf("root tree: %llu level %d\n",
207                              (unsigned long long)info->tree_root->node->start,
208                              btrfs_header_level(info->tree_root->node));
209                         printf("chunk tree: %llu level %d\n",
210                              (unsigned long long)info->chunk_root->node->start,
211                              btrfs_header_level(info->chunk_root->node));
212                 } else {
213                         if (info->tree_root->node) {
214                                 printf("root tree\n");
215                                 btrfs_print_tree(info->tree_root,
216                                                  info->tree_root->node, 1);
217                         }
218
219                         if (info->chunk_root->node) {
220                                 printf("chunk tree\n");
221                                 btrfs_print_tree(info->chunk_root,
222                                                  info->chunk_root->node, 1);
223                         }
224                 }
225         }
226         tree_root_scan = info->tree_root;
227
228         btrfs_init_path(&path);
229 again:
230         if (!extent_buffer_uptodate(tree_root_scan->node))
231                 goto no_node;
232
233         key.offset = 0;
234         key.objectid = 0;
235         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
236         ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
237         BUG_ON(ret < 0);
238         while(1) {
239                 leaf = path.nodes[0];
240                 slot = path.slots[0];
241                 if (slot >= btrfs_header_nritems(leaf)) {
242                         ret = btrfs_next_leaf(tree_root_scan, &path);
243                         if (ret != 0)
244                                 break;
245                         leaf = path.nodes[0];
246                         slot = path.slots[0];
247                 }
248                 btrfs_item_key(leaf, &disk_key, path.slots[0]);
249                 btrfs_disk_key_to_cpu(&found_key, &disk_key);
250                 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
251                         unsigned long offset;
252                         struct extent_buffer *buf;
253                         int skip = extent_only | device_only;
254
255                         offset = btrfs_item_ptr_offset(leaf, slot);
256                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
257                         buf = read_tree_block(tree_root_scan,
258                                               btrfs_root_bytenr(&ri),
259                                               btrfs_level_size(tree_root_scan,
260                                                         btrfs_root_level(&ri)),
261                                               0);
262                         if (!extent_buffer_uptodate(buf))
263                                 goto next;
264
265                         switch(found_key.objectid) {
266                         case BTRFS_ROOT_TREE_OBJECTID:
267                                 if (!skip)
268                                         printf("root");
269                                 break;
270                         case BTRFS_EXTENT_TREE_OBJECTID:
271                                 if (!device_only)
272                                         skip = 0;
273                                 if (!extent_only && !device_only)
274                                         printf("extent");
275                                 break;
276                         case BTRFS_CHUNK_TREE_OBJECTID:
277                                 if (!skip) {
278                                         printf("chunk");
279                                 }
280                                 break;
281                         case BTRFS_DEV_TREE_OBJECTID:
282                                 skip = 0;
283                                 printf("device");
284                                 break;
285                         case BTRFS_FS_TREE_OBJECTID:
286                                 if (!skip) {
287                                         printf("fs");
288                                 }
289                                 break;
290                         case BTRFS_ROOT_TREE_DIR_OBJECTID:
291                                 skip = 0;
292                                 printf("directory");
293                                 break;
294                         case BTRFS_CSUM_TREE_OBJECTID:
295                                 if (!skip) {
296                                         printf("checksum");
297                                 }
298                                 break;
299                         case BTRFS_ORPHAN_OBJECTID:
300                                 if (!skip) {
301                                         printf("orphan");
302                                 }
303                                 break;
304                         case BTRFS_TREE_LOG_OBJECTID:
305                                 if (!skip) {
306                                         printf("log");
307                                 }
308                                 break;
309                         case BTRFS_TREE_LOG_FIXUP_OBJECTID:
310                                 if (!skip) {
311                                         printf("log fixup");
312                                 }
313                                 break;
314                         case BTRFS_TREE_RELOC_OBJECTID:
315                                 if (!skip) {
316                                         printf("reloc");
317                                 }
318                                 break;
319                         case BTRFS_DATA_RELOC_TREE_OBJECTID:
320                                 if (!skip) {
321                                         printf("data reloc");
322                                 }
323                                 break;
324                         case BTRFS_EXTENT_CSUM_OBJECTID:
325                                 if (!skip) {
326                                         printf("extent checksum");
327                                 }
328                                 break;
329                         case BTRFS_QUOTA_TREE_OBJECTID:
330                                 if (!skip) {
331                                         printf("quota");
332                                 }
333                                 break;
334                         case BTRFS_MULTIPLE_OBJECTIDS:
335                                 if (!skip) {
336                                         printf("multiple");
337                                 }
338                                 break;
339                         default:
340                                 if (!skip) {
341                                         printf("file");
342                                 }
343                         }
344                         if (extent_only && !skip) {
345                                 print_extents(tree_root_scan, buf);
346                         } else if (!skip) {
347                                 printf(" tree ");
348                                 btrfs_print_key(&disk_key);
349                                 if (roots_only) {
350                                         printf(" %llu level %d\n",
351                                                (unsigned long long)buf->start,
352                                                btrfs_header_level(buf));
353                                 } else {
354                                         printf(" \n");
355                                         btrfs_print_tree(tree_root_scan, buf, 1);
356                                 }
357                         }
358                         free_extent_buffer(buf);
359                 }
360 next:
361                 path.slots[0]++;
362         }
363 no_node:
364         btrfs_release_path(root, &path);
365
366         if (tree_root_scan == info->tree_root &&
367             info->log_root_tree) {
368                 tree_root_scan = info->log_root_tree;
369                 goto again;
370         }
371
372         if (extent_only || device_only)
373                 return 0;
374
375         if (root_backups)
376                 print_old_roots(info->super_copy);
377
378         printf("total bytes %llu\n",
379                (unsigned long long)btrfs_super_total_bytes(info->super_copy));
380         printf("bytes used %llu\n",
381                (unsigned long long)btrfs_super_bytes_used(info->super_copy));
382         uuidbuf[36] = '\0';
383         uuid_unparse(info->super_copy->fsid, uuidbuf);
384         printf("uuid %s\n", uuidbuf);
385         printf("%s\n", BTRFS_BUILD_VERSION);
386         return 0;
387 }