btrfs-progs: add btrfs_clear_free_space_tree() from the kernel
[platform/upstream/btrfs-progs.git] / cmds-inspect-dump-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 "commands.h"
33 #include "utils.h"
34 #include "cmds-inspect-dump-tree.h"
35
36 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
37 {
38         struct extent_buffer *next;
39         int i;
40         u32 nr;
41         u32 size;
42
43         if (!eb)
44                 return;
45
46         if (btrfs_is_leaf(eb)) {
47                 btrfs_print_leaf(root, eb);
48                 return;
49         }
50
51         size = root->nodesize;
52         nr = btrfs_header_nritems(eb);
53         for (i = 0; i < nr; i++) {
54                 next = read_tree_block(root, btrfs_node_blockptr(eb, i),
55                                 size, btrfs_node_ptr_generation(eb, i));
56                 if (!extent_buffer_uptodate(next))
57                         continue;
58                 if (btrfs_is_leaf(next) && btrfs_header_level(eb) != 1) {
59                         warning(
60         "eb corrupted: item %d eb level %d next level %d, skipping the rest",
61                                 i, btrfs_header_level(next),
62                                 btrfs_header_level(eb));
63                         goto out;
64                 }
65                 if (btrfs_header_level(next) != btrfs_header_level(eb) - 1) {
66                         warning(
67         "eb corrupted: item %d eb level %d next level %d, skipping the rest",
68                                 i, btrfs_header_level(next),
69                                 btrfs_header_level(eb));
70                         goto out;
71                 }
72                 print_extents(root, next);
73                 free_extent_buffer(next);
74         }
75
76         return;
77
78 out:
79         free_extent_buffer(next);
80 }
81
82 static void print_old_roots(struct btrfs_super_block *super)
83 {
84         struct btrfs_root_backup *backup;
85         int i;
86
87         for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
88                 backup = super->super_roots + i;
89                 printf("btrfs root backup slot %d\n", i);
90                 printf("\ttree root gen %llu block %llu\n",
91                        (unsigned long long)btrfs_backup_tree_root_gen(backup),
92                        (unsigned long long)btrfs_backup_tree_root(backup));
93
94                 printf("\t\textent root gen %llu block %llu\n",
95                        (unsigned long long)btrfs_backup_extent_root_gen(backup),
96                        (unsigned long long)btrfs_backup_extent_root(backup));
97
98                 printf("\t\tchunk root gen %llu block %llu\n",
99                        (unsigned long long)btrfs_backup_chunk_root_gen(backup),
100                        (unsigned long long)btrfs_backup_chunk_root(backup));
101
102                 printf("\t\tdevice root gen %llu block %llu\n",
103                        (unsigned long long)btrfs_backup_dev_root_gen(backup),
104                        (unsigned long long)btrfs_backup_dev_root(backup));
105
106                 printf("\t\tcsum root gen %llu block %llu\n",
107                        (unsigned long long)btrfs_backup_csum_root_gen(backup),
108                        (unsigned long long)btrfs_backup_csum_root(backup));
109
110                 printf("\t\tfs root gen %llu block %llu\n",
111                        (unsigned long long)btrfs_backup_fs_root_gen(backup),
112                        (unsigned long long)btrfs_backup_fs_root(backup));
113
114                 printf("\t\t%llu used %llu total %llu devices\n",
115                        (unsigned long long)btrfs_backup_bytes_used(backup),
116                        (unsigned long long)btrfs_backup_total_bytes(backup),
117                        (unsigned long long)btrfs_backup_num_devices(backup));
118         }
119 }
120
121 /*
122  * Convert a tree name from various forms to the numerical id if possible
123  * Accepted forms:
124  * - case does not matter
125  * - same as the key name, BTRFS_ROOT_TREE_OBJECTID
126  * - dtto shortened, BTRFS_ROOT_TREE
127  * - dtto without prefix, ROOT_TREE
128  * - common name, ROOT, CHUNK, EXTENT, ...
129  * - dtto alias, DEVICE for DEV, CHECKSUM for CSUM
130  *
131  * Returns 0 if the tree id was not recognized.
132  */
133 static u64 treeid_from_string(const char *str, const char **end)
134 {
135         int match = 0;
136         int i;
137         u64 id;
138         static struct treename {
139                 const char *name;
140                 u64 id;
141         } tn[] = {
142                 { "ROOT", BTRFS_ROOT_TREE_OBJECTID },
143                 { "EXTENT", BTRFS_EXTENT_TREE_OBJECTID },
144                 { "CHUNK", BTRFS_CHUNK_TREE_OBJECTID },
145                 { "DEVICE", BTRFS_DEV_TREE_OBJECTID },
146                 { "DEV", BTRFS_DEV_TREE_OBJECTID },
147                 { "FS_TREE", BTRFS_FS_TREE_OBJECTID },
148                 { "CSUM", BTRFS_CSUM_TREE_OBJECTID },
149                 { "CHECKSUM", BTRFS_CSUM_TREE_OBJECTID },
150                 { "QUOTA", BTRFS_QUOTA_TREE_OBJECTID },
151                 { "UUID", BTRFS_UUID_TREE_OBJECTID },
152                 { "FREE_SPACE", BTRFS_FREE_SPACE_TREE_OBJECTID },
153                 { "TREE_LOG_FIXUP", BTRFS_TREE_LOG_FIXUP_OBJECTID },
154                 { "TREE_LOG", BTRFS_TREE_LOG_OBJECTID },
155                 { "TREE_RELOC", BTRFS_TREE_RELOC_OBJECTID },
156                 { "DATA_RELOC", BTRFS_DATA_RELOC_TREE_OBJECTID }
157         };
158
159         if (strncasecmp("BTRFS_", str, strlen("BTRFS_")) == 0)
160                 str += strlen("BTRFS_");
161
162         for (i = 0; i < ARRAY_SIZE(tn); i++) {
163                 int len = strlen(tn[i].name);
164
165                 if (strncasecmp(tn[i].name, str, len) == 0) {
166                         id = tn[i].id;
167                         match = 1;
168                         str += len;
169                         break;
170                 }
171         }
172
173         if (!match)
174                 return 0;
175
176         if (strncasecmp("_TREE", str, strlen("_TREE")) == 0)
177                 str += strlen("_TREE");
178
179         if (strncasecmp("_OBJECTID", str, strlen("_OBJECTID")) == 0)
180                 str += strlen("_OBJECTID");
181
182         *end = str;
183
184         return id;
185 }
186
187 const char * const cmd_inspect_dump_tree_usage[] = {
188         "btrfs inspect-internal dump-tree [options] device",
189         "Dump tree structures from a given device",
190         "Dump tree structures from a given device in textual form, expand keys to human",
191         "readable equivalents where possible.",
192         "Note: contains file names, consider that if you're asked to send the dump",
193         "for analysis.",
194         "",
195         "-e|--extents           print only extent info: extent and device trees",
196         "-d|--device            print only device info: tree root, chunk and device trees",
197         "-r|--roots             print only short root node info",
198         "-R|--backups           same as --roots plus print backup root info",
199         "-u|--uuid              print only the uuid tree",
200         "-b|--block <block_num> print info from the specified block only",
201         "-t|--tree <tree_id>    print only tree with the given id (string or number)",
202         NULL
203 };
204
205 int cmd_inspect_dump_tree(int argc, char **argv)
206 {
207         struct btrfs_root *root;
208         struct btrfs_fs_info *info;
209         struct btrfs_path path;
210         struct btrfs_key key;
211         struct btrfs_root_item ri;
212         struct extent_buffer *leaf;
213         struct btrfs_disk_key disk_key;
214         struct btrfs_key found_key;
215         char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
216         int ret;
217         int slot;
218         int extent_only = 0;
219         int device_only = 0;
220         int uuid_tree_only = 0;
221         int roots_only = 0;
222         int root_backups = 0;
223         u64 block_only = 0;
224         struct btrfs_root *tree_root_scan;
225         u64 tree_id = 0;
226
227         while (1) {
228                 int c;
229                 static const struct option long_options[] = {
230                         { "extents", no_argument, NULL, 'e'},
231                         { "device", no_argument, NULL, 'd'},
232                         { "roots", no_argument, NULL, 'r'},
233                         { "backups", no_argument, NULL, 'R'},
234                         { "uuid", no_argument, NULL, 'u'},
235                         { "block", required_argument, NULL, 'b'},
236                         { "tree", required_argument, NULL, 't'},
237                         { NULL, 0, NULL, 0 }
238                 };
239
240                 c = getopt_long(argc, argv, "deb:rRut:", long_options, NULL);
241                 if (c < 0)
242                         break;
243                 switch (c) {
244                 case 'e':
245                         extent_only = 1;
246                         break;
247                 case 'd':
248                         device_only = 1;
249                         break;
250                 case 'r':
251                         roots_only = 1;
252                         break;
253                 case 'u':
254                         uuid_tree_only = 1;
255                         break;
256                 case 'R':
257                         roots_only = 1;
258                         root_backups = 1;
259                         break;
260                 case 'b':
261                         block_only = arg_strtou64(optarg);
262                         break;
263                 case 't': {
264                         const char *end = NULL;
265
266                         if (string_is_numerical(optarg))
267                                 tree_id = arg_strtou64(optarg);
268                         else
269                                 tree_id = treeid_from_string(optarg, &end);
270
271                         if (!tree_id) {
272                                 error("unrecognized tree id: %s\n",
273                                                 optarg);
274                                 exit(1);
275                         }
276
277                         if (end && *end) {
278                                 error("unexpected tree id suffix of '%s': %s",
279                                                 optarg, end);
280                                 exit(1);
281                         }
282                         break;
283                         }
284                 default:
285                         usage(cmd_inspect_dump_tree_usage);
286                 }
287         }
288
289         if (check_argc_exact(argc - optind, 1))
290                 usage(cmd_inspect_dump_tree_usage);
291
292         ret = check_arg_type(argv[optind]);
293         if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {
294                 error("not a block device or regular file: %s", argv[optind]);
295                 goto out;
296         }
297
298         printf("%s\n", PACKAGE_STRING);
299
300         info = open_ctree_fs_info(argv[optind], 0, 0, 0, OPEN_CTREE_PARTIAL);
301         if (!info) {
302                 error("unable to open %s", argv[optind]);
303                 goto out;
304         }
305
306         root = info->fs_root;
307         if (!root) {
308                 error("unable to open %s", argv[optind]);
309                 goto out;
310         }
311
312         if (block_only) {
313                 leaf = read_tree_block(root,
314                                       block_only,
315                                       root->nodesize, 0);
316
317                 if (extent_buffer_uptodate(leaf) &&
318                     btrfs_header_level(leaf) != 0) {
319                         free_extent_buffer(leaf);
320                         leaf = NULL;
321                 }
322
323                 if (!leaf) {
324                         leaf = read_tree_block(root,
325                                               block_only,
326                                               root->nodesize, 0);
327                 }
328                 if (!extent_buffer_uptodate(leaf)) {
329                         error("failed to read %llu",
330                                 (unsigned long long)block_only);
331                         goto close_root;
332                 }
333                 btrfs_print_tree(root, leaf, 0);
334                 free_extent_buffer(leaf);
335                 goto close_root;
336         }
337
338         if (!(extent_only || uuid_tree_only || tree_id)) {
339                 if (roots_only) {
340                         printf("root tree: %llu level %d\n",
341                              (unsigned long long)info->tree_root->node->start,
342                              btrfs_header_level(info->tree_root->node));
343                         printf("chunk tree: %llu level %d\n",
344                              (unsigned long long)info->chunk_root->node->start,
345                              btrfs_header_level(info->chunk_root->node));
346                 } else {
347                         if (info->tree_root->node) {
348                                 printf("root tree\n");
349                                 btrfs_print_tree(info->tree_root,
350                                                  info->tree_root->node, 1);
351                         }
352
353                         if (info->chunk_root->node) {
354                                 printf("chunk tree\n");
355                                 btrfs_print_tree(info->chunk_root,
356                                                  info->chunk_root->node, 1);
357                         }
358                 }
359         }
360         tree_root_scan = info->tree_root;
361
362         btrfs_init_path(&path);
363 again:
364         if (!extent_buffer_uptodate(tree_root_scan->node))
365                 goto no_node;
366
367         /*
368          * Tree's that are not pointed by the tree of tree roots
369          */
370         if (tree_id && tree_id == BTRFS_ROOT_TREE_OBJECTID) {
371                 if (!info->tree_root->node) {
372                         error("cannot print root tree, invalid pointer");
373                         goto no_node;
374                 }
375                 printf("root tree\n");
376                 btrfs_print_tree(info->tree_root, info->tree_root->node, 1);
377                 goto no_node;
378         }
379
380         if (tree_id && tree_id == BTRFS_CHUNK_TREE_OBJECTID) {
381                 if (!info->chunk_root->node) {
382                         error("cannot print chunk tree, invalid pointer");
383                         goto no_node;
384                 }
385                 printf("chunk tree\n");
386                 btrfs_print_tree(info->chunk_root, info->chunk_root->node, 1);
387                 goto no_node;
388         }
389
390         key.offset = 0;
391         key.objectid = 0;
392         key.type = BTRFS_ROOT_ITEM_KEY;
393         ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
394         if (ret < 0) {
395                 error("cannot read ROOT_ITEM from tree %llu: %s",
396                         (unsigned long long)tree_root_scan->root_key.objectid,
397                         strerror(-ret));
398                 goto close_root;
399         }
400         while (1) {
401                 leaf = path.nodes[0];
402                 slot = path.slots[0];
403                 if (slot >= btrfs_header_nritems(leaf)) {
404                         ret = btrfs_next_leaf(tree_root_scan, &path);
405                         if (ret != 0)
406                                 break;
407                         leaf = path.nodes[0];
408                         slot = path.slots[0];
409                 }
410                 btrfs_item_key(leaf, &disk_key, path.slots[0]);
411                 btrfs_disk_key_to_cpu(&found_key, &disk_key);
412                 if (found_key.type == BTRFS_ROOT_ITEM_KEY) {
413                         unsigned long offset;
414                         struct extent_buffer *buf;
415                         int skip = extent_only | device_only | uuid_tree_only;
416
417                         offset = btrfs_item_ptr_offset(leaf, slot);
418                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
419                         buf = read_tree_block(tree_root_scan,
420                                               btrfs_root_bytenr(&ri),
421                                               tree_root_scan->nodesize,
422                                               0);
423                         if (!extent_buffer_uptodate(buf))
424                                 goto next;
425                         if (tree_id && found_key.objectid != tree_id) {
426                                 free_extent_buffer(buf);
427                                 goto next;
428                         }
429
430                         switch (found_key.objectid) {
431                         case BTRFS_ROOT_TREE_OBJECTID:
432                                 if (!skip)
433                                         printf("root");
434                                 break;
435                         case BTRFS_EXTENT_TREE_OBJECTID:
436                                 if (!device_only && !uuid_tree_only)
437                                         skip = 0;
438                                 if (!skip)
439                                         printf("extent");
440                                 break;
441                         case BTRFS_CHUNK_TREE_OBJECTID:
442                                 if (!skip) {
443                                         printf("chunk");
444                                 }
445                                 break;
446                         case BTRFS_DEV_TREE_OBJECTID:
447                                 if (!uuid_tree_only)
448                                         skip = 0;
449                                 if (!skip)
450                                         printf("device");
451                                 break;
452                         case BTRFS_FS_TREE_OBJECTID:
453                                 if (!skip) {
454                                         printf("fs");
455                                 }
456                                 break;
457                         case BTRFS_ROOT_TREE_DIR_OBJECTID:
458                                 skip = 0;
459                                 printf("directory");
460                                 break;
461                         case BTRFS_CSUM_TREE_OBJECTID:
462                                 if (!skip) {
463                                         printf("checksum");
464                                 }
465                                 break;
466                         case BTRFS_ORPHAN_OBJECTID:
467                                 if (!skip) {
468                                         printf("orphan");
469                                 }
470                                 break;
471                         case BTRFS_TREE_LOG_OBJECTID:
472                                 if (!skip) {
473                                         printf("log");
474                                 }
475                                 break;
476                         case BTRFS_TREE_LOG_FIXUP_OBJECTID:
477                                 if (!skip) {
478                                         printf("log fixup");
479                                 }
480                                 break;
481                         case BTRFS_TREE_RELOC_OBJECTID:
482                                 if (!skip) {
483                                         printf("reloc");
484                                 }
485                                 break;
486                         case BTRFS_DATA_RELOC_TREE_OBJECTID:
487                                 if (!skip) {
488                                         printf("data reloc");
489                                 }
490                                 break;
491                         case BTRFS_EXTENT_CSUM_OBJECTID:
492                                 if (!skip) {
493                                         printf("extent checksum");
494                                 }
495                                 break;
496                         case BTRFS_QUOTA_TREE_OBJECTID:
497                                 if (!skip) {
498                                         printf("quota");
499                                 }
500                                 break;
501                         case BTRFS_UUID_TREE_OBJECTID:
502                                 if (!extent_only && !device_only)
503                                         skip = 0;
504                                 if (!skip)
505                                         printf("uuid");
506                                 break;
507                         case BTRFS_FREE_SPACE_TREE_OBJECTID:
508                                 if (!skip)
509                                         printf("free space");
510                                 break;
511                         case BTRFS_MULTIPLE_OBJECTIDS:
512                                 if (!skip) {
513                                         printf("multiple");
514                                 }
515                                 break;
516                         default:
517                                 if (!skip) {
518                                         printf("file");
519                                 }
520                         }
521                         if (extent_only && !skip) {
522                                 printf(" tree ");
523                                 btrfs_print_key(&disk_key);
524                                 printf("\n");
525                                 print_extents(tree_root_scan, buf);
526                         } else if (!skip) {
527                                 printf(" tree ");
528                                 btrfs_print_key(&disk_key);
529                                 if (roots_only) {
530                                         printf(" %llu level %d\n",
531                                                (unsigned long long)buf->start,
532                                                btrfs_header_level(buf));
533                                 } else {
534                                         printf(" \n");
535                                         btrfs_print_tree(tree_root_scan, buf, 1);
536                                 }
537                         }
538                         free_extent_buffer(buf);
539                 }
540 next:
541                 path.slots[0]++;
542         }
543 no_node:
544         btrfs_release_path(&path);
545
546         if (tree_root_scan == info->tree_root && info->log_root_tree) {
547                 tree_root_scan = info->log_root_tree;
548                 goto again;
549         }
550
551         if (extent_only || device_only || uuid_tree_only)
552                 goto close_root;
553
554         if (root_backups)
555                 print_old_roots(info->super_copy);
556
557         printf("total bytes %llu\n",
558                (unsigned long long)btrfs_super_total_bytes(info->super_copy));
559         printf("bytes used %llu\n",
560                (unsigned long long)btrfs_super_bytes_used(info->super_copy));
561         uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
562         uuid_unparse(info->super_copy->fsid, uuidbuf);
563         printf("uuid %s\n", uuidbuf);
564 close_root:
565         ret = close_ctree(root);
566 out:
567         return !!ret;
568 }