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