btrfs-progs: convert/ext2: Remove check for ext2_ext_attr_entry->e_value_block
[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 "help.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
42         if (!eb)
43                 return;
44
45         if (btrfs_is_leaf(eb)) {
46                 btrfs_print_leaf(root, eb);
47                 return;
48         }
49
50         nr = btrfs_header_nritems(eb);
51         for (i = 0; i < nr; i++) {
52                 next = read_tree_block(root->fs_info,
53                                 btrfs_node_blockptr(eb, i),
54                                 btrfs_node_ptr_generation(eb, i));
55                 if (!extent_buffer_uptodate(next))
56                         continue;
57                 if (btrfs_is_leaf(next) && btrfs_header_level(eb) != 1) {
58                         warning(
59         "eb corrupted: item %d eb level %d next level %d, skipping the rest",
60                                 i, btrfs_header_level(next),
61                                 btrfs_header_level(eb));
62                         goto out;
63                 }
64                 if (btrfs_header_level(next) != btrfs_header_level(eb) - 1) {
65                         warning(
66         "eb corrupted: item %d eb level %d next level %d, skipping the rest",
67                                 i, btrfs_header_level(next),
68                                 btrfs_header_level(eb));
69                         goto out;
70                 }
71                 print_extents(root, next);
72                 free_extent_buffer(next);
73         }
74
75         return;
76
77 out:
78         free_extent_buffer(next);
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 /*
121  * Convert a tree name from various forms to the numerical id if possible
122  * Accepted forms:
123  * - case does not matter
124  * - same as the key name, BTRFS_ROOT_TREE_OBJECTID
125  * - dtto shortened, BTRFS_ROOT_TREE
126  * - dtto without prefix, ROOT_TREE
127  * - common name, ROOT, CHUNK, EXTENT, ...
128  * - dtto alias, DEVICE for DEV, CHECKSUM for CSUM
129  *
130  * Returns 0 if the tree id was not recognized.
131  */
132 static u64 treeid_from_string(const char *str, const char **end)
133 {
134         int match = 0;
135         int i;
136         u64 id;
137         static struct treename {
138                 const char *name;
139                 u64 id;
140         } tn[] = {
141                 { "ROOT", BTRFS_ROOT_TREE_OBJECTID },
142                 { "EXTENT", BTRFS_EXTENT_TREE_OBJECTID },
143                 { "CHUNK", BTRFS_CHUNK_TREE_OBJECTID },
144                 { "DEVICE", BTRFS_DEV_TREE_OBJECTID },
145                 { "DEV", BTRFS_DEV_TREE_OBJECTID },
146                 { "FS", BTRFS_FS_TREE_OBJECTID },
147                 { "CSUM", BTRFS_CSUM_TREE_OBJECTID },
148                 { "CHECKSUM", BTRFS_CSUM_TREE_OBJECTID },
149                 { "QUOTA", BTRFS_QUOTA_TREE_OBJECTID },
150                 { "UUID", BTRFS_UUID_TREE_OBJECTID },
151                 { "FREE_SPACE", BTRFS_FREE_SPACE_TREE_OBJECTID },
152                 { "TREE_LOG_FIXUP", BTRFS_TREE_LOG_FIXUP_OBJECTID },
153                 { "TREE_LOG", BTRFS_TREE_LOG_OBJECTID },
154                 { "TREE_RELOC", BTRFS_TREE_RELOC_OBJECTID },
155                 { "DATA_RELOC", BTRFS_DATA_RELOC_TREE_OBJECTID }
156         };
157
158         if (strncasecmp("BTRFS_", str, strlen("BTRFS_")) == 0)
159                 str += strlen("BTRFS_");
160
161         for (i = 0; i < ARRAY_SIZE(tn); i++) {
162                 int len = strlen(tn[i].name);
163
164                 if (strncasecmp(tn[i].name, str, len) == 0) {
165                         id = tn[i].id;
166                         match = 1;
167                         str += len;
168                         break;
169                 }
170         }
171
172         if (!match)
173                 return 0;
174
175         if (strncasecmp("_TREE", str, strlen("_TREE")) == 0)
176                 str += strlen("_TREE");
177
178         if (strncasecmp("_OBJECTID", str, strlen("_OBJECTID")) == 0)
179                 str += strlen("_OBJECTID");
180
181         *end = str;
182
183         return id;
184 }
185
186 const char * const cmd_inspect_dump_tree_usage[] = {
187         "btrfs inspect-internal dump-tree [options] device",
188         "Dump tree structures from a given device",
189         "Dump tree structures from a given device in textual form, expand keys to human",
190         "readable equivalents where possible.",
191         "Note: contains file names, consider that if you're asked to send the dump",
192         "for analysis.",
193         "",
194         "-e|--extents           print only extent info: extent and device trees",
195         "-d|--device            print only device info: tree root, chunk and device trees",
196         "-r|--roots             print only short root node info",
197         "-R|--backups           same as --roots plus print backup root info",
198         "-u|--uuid              print only the uuid tree",
199         "-b|--block <block_num> print info from the specified block only",
200         "-t|--tree <tree_id>    print only tree with the given id (string or number)",
201         NULL
202 };
203
204 int cmd_inspect_dump_tree(int argc, char **argv)
205 {
206         struct btrfs_root *root;
207         struct btrfs_fs_info *info;
208         struct btrfs_path path;
209         struct btrfs_key key;
210         struct btrfs_root_item ri;
211         struct extent_buffer *leaf;
212         struct btrfs_disk_key disk_key;
213         struct btrfs_key found_key;
214         char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
215         int ret;
216         int slot;
217         int extent_only = 0;
218         int device_only = 0;
219         int uuid_tree_only = 0;
220         int roots_only = 0;
221         int root_backups = 0;
222         unsigned open_ctree_flags = OPEN_CTREE_FS_PARTIAL;
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                         /*
262                          * If only showing one block, no need to fill roots
263                          * other than chunk root
264                          */
265                         open_ctree_flags |= __OPEN_CTREE_RETURN_CHUNK_ROOT;
266                         block_only = arg_strtou64(optarg);
267                         break;
268                 case 't': {
269                         const char *end = NULL;
270
271                         if (string_is_numerical(optarg))
272                                 tree_id = arg_strtou64(optarg);
273                         else
274                                 tree_id = treeid_from_string(optarg, &end);
275
276                         if (!tree_id) {
277                                 error("unrecognized tree id: %s",
278                                                 optarg);
279                                 exit(1);
280                         }
281
282                         if (end && *end) {
283                                 error("unexpected tree id suffix of '%s': %s",
284                                                 optarg, end);
285                                 exit(1);
286                         }
287                         break;
288                         }
289                 default:
290                         usage(cmd_inspect_dump_tree_usage);
291                 }
292         }
293
294         if (check_argc_exact(argc - optind, 1))
295                 usage(cmd_inspect_dump_tree_usage);
296
297         ret = check_arg_type(argv[optind]);
298         if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {
299                 error("not a block device or regular file: %s", argv[optind]);
300                 goto out;
301         }
302
303         printf("%s\n", PACKAGE_STRING);
304
305         info = open_ctree_fs_info(argv[optind], 0, 0, 0, open_ctree_flags);
306         if (!info) {
307                 error("unable to open %s", argv[optind]);
308                 goto out;
309         }
310
311         if (block_only) {
312                 root = info->chunk_root;
313                 leaf = read_tree_block(info, block_only, 0);
314                 if (extent_buffer_uptodate(leaf) &&
315                     btrfs_header_level(leaf) != 0) {
316                         free_extent_buffer(leaf);
317                         leaf = NULL;
318                 }
319
320                 if (!leaf)
321                         leaf = read_tree_block(info, block_only, 0);
322                 if (!extent_buffer_uptodate(leaf)) {
323                         error("failed to read %llu",
324                                 (unsigned long long)block_only);
325                         goto close_root;
326                 }
327                 btrfs_print_tree(root, leaf, 0);
328                 free_extent_buffer(leaf);
329                 goto close_root;
330         }
331
332         root = info->fs_root;
333         if (!root) {
334                 error("unable to open %s", argv[optind]);
335                 goto out;
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                         if (info->log_root_tree)
347                                 printf("log root tree: %llu level %d\n",
348                                        info->log_root_tree->node->start,
349                                         btrfs_header_level(
350                                                 info->log_root_tree->node));
351                 } else {
352                         if (info->tree_root->node) {
353                                 printf("root tree\n");
354                                 btrfs_print_tree(info->tree_root,
355                                                  info->tree_root->node, 1);
356                         }
357
358                         if (info->chunk_root->node) {
359                                 printf("chunk tree\n");
360                                 btrfs_print_tree(info->chunk_root,
361                                                  info->chunk_root->node, 1);
362                         }
363
364                         if (info->log_root_tree) {
365                                 printf("log root tree\n");
366                                 btrfs_print_tree(info->log_root_tree,
367                                                 info->log_root_tree->node, 1);
368                         }
369                 }
370         }
371         tree_root_scan = info->tree_root;
372
373         btrfs_init_path(&path);
374 again:
375         if (!extent_buffer_uptodate(tree_root_scan->node))
376                 goto no_node;
377
378         /*
379          * Tree's that are not pointed by the tree of tree roots
380          */
381         if (tree_id && tree_id == BTRFS_ROOT_TREE_OBJECTID) {
382                 if (!info->tree_root->node) {
383                         error("cannot print root tree, invalid pointer");
384                         goto close_root;
385                 }
386                 printf("root tree\n");
387                 btrfs_print_tree(info->tree_root, info->tree_root->node, 1);
388                 goto close_root;
389         }
390
391         if (tree_id && tree_id == BTRFS_CHUNK_TREE_OBJECTID) {
392                 if (!info->chunk_root->node) {
393                         error("cannot print chunk tree, invalid pointer");
394                         goto close_root;
395                 }
396                 printf("chunk tree\n");
397                 btrfs_print_tree(info->chunk_root, info->chunk_root->node, 1);
398                 goto close_root;
399         }
400
401         if (tree_id && tree_id == BTRFS_TREE_LOG_OBJECTID) {
402                 if (!info->log_root_tree) {
403                         error("cannot print log root tree, invalid pointer");
404                         goto close_root;
405                 }
406                 printf("log root tree\n");
407                 btrfs_print_tree(info->log_root_tree, info->log_root_tree->node,
408                                  1);
409                 goto close_root;
410         }
411
412         key.offset = 0;
413         key.objectid = 0;
414         key.type = BTRFS_ROOT_ITEM_KEY;
415         ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
416         if (ret < 0) {
417                 error("cannot read ROOT_ITEM from tree %llu: %s",
418                         (unsigned long long)tree_root_scan->root_key.objectid,
419                         strerror(-ret));
420                 goto close_root;
421         }
422         while (1) {
423                 leaf = path.nodes[0];
424                 slot = path.slots[0];
425                 if (slot >= btrfs_header_nritems(leaf)) {
426                         ret = btrfs_next_leaf(tree_root_scan, &path);
427                         if (ret != 0)
428                                 break;
429                         leaf = path.nodes[0];
430                         slot = path.slots[0];
431                 }
432                 btrfs_item_key(leaf, &disk_key, path.slots[0]);
433                 btrfs_disk_key_to_cpu(&found_key, &disk_key);
434                 if (found_key.type == BTRFS_ROOT_ITEM_KEY) {
435                         unsigned long offset;
436                         struct extent_buffer *buf;
437                         int skip = extent_only | device_only | uuid_tree_only;
438
439                         offset = btrfs_item_ptr_offset(leaf, slot);
440                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
441                         buf = read_tree_block(info, btrfs_root_bytenr(&ri), 0);
442                         if (!extent_buffer_uptodate(buf))
443                                 goto next;
444                         if (tree_id && found_key.objectid != tree_id) {
445                                 free_extent_buffer(buf);
446                                 goto next;
447                         }
448
449                         switch (found_key.objectid) {
450                         case BTRFS_ROOT_TREE_OBJECTID:
451                                 if (!skip)
452                                         printf("root");
453                                 break;
454                         case BTRFS_EXTENT_TREE_OBJECTID:
455                                 if (!device_only && !uuid_tree_only)
456                                         skip = 0;
457                                 if (!skip)
458                                         printf("extent");
459                                 break;
460                         case BTRFS_CHUNK_TREE_OBJECTID:
461                                 if (!skip) {
462                                         printf("chunk");
463                                 }
464                                 break;
465                         case BTRFS_DEV_TREE_OBJECTID:
466                                 if (!uuid_tree_only)
467                                         skip = 0;
468                                 if (!skip)
469                                         printf("device");
470                                 break;
471                         case BTRFS_FS_TREE_OBJECTID:
472                                 if (!skip) {
473                                         printf("fs");
474                                 }
475                                 break;
476                         case BTRFS_ROOT_TREE_DIR_OBJECTID:
477                                 skip = 0;
478                                 printf("directory");
479                                 break;
480                         case BTRFS_CSUM_TREE_OBJECTID:
481                                 if (!skip) {
482                                         printf("checksum");
483                                 }
484                                 break;
485                         case BTRFS_ORPHAN_OBJECTID:
486                                 if (!skip) {
487                                         printf("orphan");
488                                 }
489                                 break;
490                         case BTRFS_TREE_LOG_OBJECTID:
491                                 if (!skip) {
492                                         printf("log");
493                                 }
494                                 break;
495                         case BTRFS_TREE_LOG_FIXUP_OBJECTID:
496                                 if (!skip) {
497                                         printf("log fixup");
498                                 }
499                                 break;
500                         case BTRFS_TREE_RELOC_OBJECTID:
501                                 if (!skip) {
502                                         printf("reloc");
503                                 }
504                                 break;
505                         case BTRFS_DATA_RELOC_TREE_OBJECTID:
506                                 if (!skip) {
507                                         printf("data reloc");
508                                 }
509                                 break;
510                         case BTRFS_EXTENT_CSUM_OBJECTID:
511                                 if (!skip) {
512                                         printf("extent checksum");
513                                 }
514                                 break;
515                         case BTRFS_QUOTA_TREE_OBJECTID:
516                                 if (!skip) {
517                                         printf("quota");
518                                 }
519                                 break;
520                         case BTRFS_UUID_TREE_OBJECTID:
521                                 if (!extent_only && !device_only)
522                                         skip = 0;
523                                 if (!skip)
524                                         printf("uuid");
525                                 break;
526                         case BTRFS_FREE_SPACE_TREE_OBJECTID:
527                                 if (!skip)
528                                         printf("free space");
529                                 break;
530                         case BTRFS_MULTIPLE_OBJECTIDS:
531                                 if (!skip) {
532                                         printf("multiple");
533                                 }
534                                 break;
535                         default:
536                                 if (!skip) {
537                                         printf("file");
538                                 }
539                         }
540                         if (extent_only && !skip) {
541                                 printf(" tree ");
542                                 btrfs_print_key(&disk_key);
543                                 printf("\n");
544                                 print_extents(tree_root_scan, buf);
545                         } else if (!skip) {
546                                 printf(" tree ");
547                                 btrfs_print_key(&disk_key);
548                                 if (roots_only) {
549                                         printf(" %llu level %d\n",
550                                                (unsigned long long)buf->start,
551                                                btrfs_header_level(buf));
552                                 } else {
553                                         printf(" \n");
554                                         btrfs_print_tree(tree_root_scan, buf, 1);
555                                 }
556                         }
557                         free_extent_buffer(buf);
558                 }
559 next:
560                 path.slots[0]++;
561         }
562 no_node:
563         btrfs_release_path(&path);
564
565         if (tree_root_scan == info->tree_root && info->log_root_tree) {
566                 tree_root_scan = info->log_root_tree;
567                 goto again;
568         }
569
570         if (extent_only || device_only || uuid_tree_only)
571                 goto close_root;
572
573         if (root_backups)
574                 print_old_roots(info->super_copy);
575
576         printf("total bytes %llu\n",
577                (unsigned long long)btrfs_super_total_bytes(info->super_copy));
578         printf("bytes used %llu\n",
579                (unsigned long long)btrfs_super_bytes_used(info->super_copy));
580         uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
581         uuid_unparse(info->super_copy->fsid, uuidbuf);
582         printf("uuid %s\n", uuidbuf);
583 close_root:
584         ret = close_ctree(root);
585 out:
586         return !!ret;
587 }