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