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