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