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