btrfs-progs: unify naming of argc and argv
[platform/upstream/btrfs-progs.git] / cmds-inspect.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public
4  * License v2 as published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9  * General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the
13  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14  * Boston, MA 021110-1307, USA.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <stdint.h>
21 #include <sys/ioctl.h>
22 #include <errno.h>
23 #include <getopt.h>
24 #include <limits.h>
25
26 #include "kerncompat.h"
27 #include "ioctl.h"
28 #include "utils.h"
29 #include "ctree.h"
30 #include "send-utils.h"
31 #include "disk-io.h"
32 #include "commands.h"
33 #include "btrfs-list.h"
34 #include "cmds-inspect-dump-tree.h"
35 #include "cmds-inspect-dump-super.h"
36
37 static const char * const inspect_cmd_group_usage[] = {
38         "btrfs inspect-internal <command> <args>",
39         NULL
40 };
41
42 static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
43 {
44         int ret;
45         int i;
46         struct btrfs_ioctl_ino_path_args ipa;
47         struct btrfs_data_container fspath[PATH_MAX];
48
49         memset(fspath, 0, sizeof(*fspath));
50         ipa.inum = inum;
51         ipa.size = PATH_MAX;
52         ipa.fspath = ptr_to_u64(fspath);
53
54         ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
55         if (ret < 0) {
56                 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
57                 goto out;
58         }
59
60         if (verbose)
61                 printf("ioctl ret=%d, bytes_left=%lu, bytes_missing=%lu, "
62                         "cnt=%d, missed=%d\n", ret,
63                         (unsigned long)fspath->bytes_left,
64                         (unsigned long)fspath->bytes_missing,
65                         fspath->elem_cnt, fspath->elem_missed);
66
67         for (i = 0; i < fspath->elem_cnt; ++i) {
68                 u64 ptr;
69                 char *str;
70                 ptr = (u64)(unsigned long)fspath->val;
71                 ptr += fspath->val[i];
72                 str = (char *)(unsigned long)ptr;
73                 if (prepend)
74                         printf("%s/%s\n", prepend, str);
75                 else
76                         printf("%s\n", str);
77         }
78
79 out:
80         return !!ret;
81 }
82
83 static const char * const cmd_inspect_inode_resolve_usage[] = {
84         "btrfs inspect-internal inode-resolve [-v] <inode> <path>",
85         "Get file system paths for the given inode",
86         "",
87         "-v   verbose mode",
88         NULL
89 };
90
91 static int cmd_inspect_inode_resolve(int argc, char **argv)
92 {
93         int fd;
94         int verbose = 0;
95         int ret;
96         DIR *dirstream = NULL;
97
98         optind = 1;
99         while (1) {
100                 int c = getopt(argc, argv, "v");
101                 if (c < 0)
102                         break;
103
104                 switch (c) {
105                 case 'v':
106                         verbose = 1;
107                         break;
108                 default:
109                         usage(cmd_inspect_inode_resolve_usage);
110                 }
111         }
112
113         if (check_argc_exact(argc - optind, 2))
114                 usage(cmd_inspect_inode_resolve_usage);
115
116         fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
117         if (fd < 0)
118                 return 1;
119
120         ret = __ino_to_path_fd(arg_strtou64(argv[optind]), fd, verbose,
121                                argv[optind+1]);
122         close_file_or_dir(fd, dirstream);
123         return !!ret;
124
125 }
126
127 static const char * const cmd_inspect_logical_resolve_usage[] = {
128         "btrfs inspect-internal logical-resolve [-Pv] [-s bufsize] <logical> <path>",
129         "Get file system paths for the given logical address",
130         "-P          skip the path resolving and print the inodes instead",
131         "-v          verbose mode",
132         "-s bufsize  set inode container's size. This is used to increase inode",
133         "            container's size in case it is not enough to read all the ",
134         "            resolved results. The max value one can set is 64k",
135         NULL
136 };
137
138 static int cmd_inspect_logical_resolve(int argc, char **argv)
139 {
140         int ret;
141         int fd;
142         int i;
143         int verbose = 0;
144         int getpath = 1;
145         int bytes_left;
146         struct btrfs_ioctl_logical_ino_args loi;
147         struct btrfs_data_container *inodes;
148         u64 size = 4096;
149         char full_path[4096];
150         char *path_ptr;
151         DIR *dirstream = NULL;
152
153         optind = 1;
154         while (1) {
155                 int c = getopt(argc, argv, "Pvs:");
156                 if (c < 0)
157                         break;
158
159                 switch (c) {
160                 case 'P':
161                         getpath = 0;
162                         break;
163                 case 'v':
164                         verbose = 1;
165                         break;
166                 case 's':
167                         size = arg_strtou64(optarg);
168                         break;
169                 default:
170                         usage(cmd_inspect_logical_resolve_usage);
171                 }
172         }
173
174         if (check_argc_exact(argc - optind, 2))
175                 usage(cmd_inspect_logical_resolve_usage);
176
177         size = min(size, (u64)64 * 1024);
178         inodes = malloc(size);
179         if (!inodes)
180                 return 1;
181
182         memset(inodes, 0, sizeof(*inodes));
183         loi.logical = arg_strtou64(argv[optind]);
184         loi.size = size;
185         loi.inodes = ptr_to_u64(inodes);
186
187         fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
188         if (fd < 0) {
189                 ret = 12;
190                 goto out;
191         }
192
193         ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
194         if (ret < 0) {
195                 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
196                 goto out;
197         }
198
199         if (verbose)
200                 printf("ioctl ret=%d, total_size=%llu, bytes_left=%lu, "
201                         "bytes_missing=%lu, cnt=%d, missed=%d\n",
202                         ret, size,
203                         (unsigned long)inodes->bytes_left,
204                         (unsigned long)inodes->bytes_missing,
205                         inodes->elem_cnt, inodes->elem_missed);
206
207         bytes_left = sizeof(full_path);
208         ret = snprintf(full_path, bytes_left, "%s/", argv[optind+1]);
209         path_ptr = full_path + ret;
210         bytes_left -= ret + 1;
211         BUG_ON(bytes_left < 0);
212
213         for (i = 0; i < inodes->elem_cnt; i += 3) {
214                 u64 inum = inodes->val[i];
215                 u64 offset = inodes->val[i+1];
216                 u64 root = inodes->val[i+2];
217                 int path_fd;
218                 char *name;
219                 DIR *dirs = NULL;
220
221                 if (getpath) {
222                         name = btrfs_list_path_for_root(fd, root);
223                         if (IS_ERR(name)) {
224                                 ret = PTR_ERR(name);
225                                 goto out;
226                         }
227                         if (!name) {
228                                 path_ptr[-1] = '\0';
229                                 path_fd = fd;
230                         } else {
231                                 path_ptr[-1] = '/';
232                                 ret = snprintf(path_ptr, bytes_left, "%s",
233                                                 name);
234                                 BUG_ON(ret >= bytes_left);
235                                 free(name);
236                                 path_fd = btrfs_open_dir(full_path, &dirs, 1);
237                                 if (path_fd < 0) {
238                                         ret = -ENOENT;
239                                         goto out;
240                                 }
241                         }
242                         __ino_to_path_fd(inum, path_fd, verbose, full_path);
243                         if (path_fd != fd)
244                                 close_file_or_dir(path_fd, dirs);
245                 } else {
246                         printf("inode %llu offset %llu root %llu\n", inum,
247                                 offset, root);
248                 }
249         }
250
251 out:
252         close_file_or_dir(fd, dirstream);
253         free(inodes);
254         return !!ret;
255 }
256
257 static const char * const cmd_inspect_subvolid_resolve_usage[] = {
258         "btrfs inspect-internal subvolid-resolve <subvolid> <path>",
259         "Get file system paths for the given subvolume ID.",
260         NULL
261 };
262
263 static int cmd_inspect_subvolid_resolve(int argc, char **argv)
264 {
265         int ret;
266         int fd = -1;
267         u64 subvol_id;
268         char path[PATH_MAX];
269         DIR *dirstream = NULL;
270
271         clean_args_no_options(argc, argv, cmd_inspect_subvolid_resolve_usage);
272
273         if (check_argc_exact(argc - optind, 2))
274                 usage(cmd_inspect_subvolid_resolve_usage);
275
276         fd = btrfs_open_dir(argv[optind], &dirstream, 1);
277         if (fd < 0) {
278                 ret = -ENOENT;
279                 goto out;
280         }
281
282         subvol_id = arg_strtou64(argv[optind]);
283         ret = btrfs_subvolid_resolve(fd, path, sizeof(path), subvol_id);
284
285         if (ret) {
286                 fprintf(stderr,
287                         "btrfs_subvolid_resolve(subvol_id %llu) failed with ret=%d\n",
288                         (unsigned long long)subvol_id, ret);
289                 goto out;
290         }
291
292         path[PATH_MAX - 1] = '\0';
293         printf("%s\n", path);
294
295 out:
296         close_file_or_dir(fd, dirstream);
297         return ret ? 1 : 0;
298 }
299
300 static const char* const cmd_inspect_rootid_usage[] = {
301         "btrfs inspect-internal rootid <path>",
302         "Get tree ID of the containing subvolume of path.",
303         NULL
304 };
305
306 static int cmd_inspect_rootid(int argc, char **argv)
307 {
308         int ret;
309         int fd = -1;
310         u64 rootid;
311         DIR *dirstream = NULL;
312
313         clean_args_no_options(argc, argv, cmd_inspect_rootid_usage);
314
315         if (check_argc_exact(argc - optind, 1))
316                 usage(cmd_inspect_rootid_usage);
317
318         fd = btrfs_open_dir(argv[optind], &dirstream, 1);
319         if (fd < 0) {
320                 ret = -ENOENT;
321                 goto out;
322         }
323
324         ret = lookup_ino_rootid(fd, &rootid);
325         if (ret) {
326                 fprintf(stderr, "rootid failed with ret=%d\n", ret);
327                 goto out;
328         }
329
330         printf("%llu\n", (unsigned long long)rootid);
331 out:
332         close_file_or_dir(fd, dirstream);
333
334         return !!ret;
335 }
336
337 static const char* const cmd_inspect_min_dev_size_usage[] = {
338         "btrfs inspect-internal min-dev-size [options] <path>",
339         "Get the minimum size the device can be shrunk to. The",
340         "device id 1 is used by default.",
341         "--id DEVID   specify the device id to query",
342         NULL
343 };
344
345 struct dev_extent_elem {
346         u64 start;
347         /* inclusive end */
348         u64 end;
349         struct list_head list;
350 };
351
352 static int add_dev_extent(struct list_head *list,
353                           const u64 start, const u64 end,
354                           const int append)
355 {
356         struct dev_extent_elem *e;
357
358         e = malloc(sizeof(*e));
359         if (!e)
360                 return -ENOMEM;
361
362         e->start = start;
363         e->end = end;
364
365         if (append)
366                 list_add_tail(&e->list, list);
367         else
368                 list_add(&e->list, list);
369
370         return 0;
371 }
372
373 static void free_dev_extent_list(struct list_head *list)
374 {
375         while (!list_empty(list)) {
376                 struct dev_extent_elem *e;
377
378                 e = list_first_entry(list, struct dev_extent_elem, list);
379                 list_del(&e->list);
380                 free(e);
381         }
382 }
383
384 static int hole_includes_sb_mirror(const u64 start, const u64 end)
385 {
386         int i;
387         int ret = 0;
388
389         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
390                 u64 bytenr = btrfs_sb_offset(i);
391
392                 if (bytenr >= start && bytenr <= end) {
393                         ret = 1;
394                         break;
395                 }
396         }
397
398         return ret;
399 }
400
401 static void adjust_dev_min_size(struct list_head *extents,
402                                 struct list_head *holes,
403                                 u64 *min_size)
404 {
405         /*
406          * If relocation of the block group of a device extent must happen (see
407          * below) scratch space is used for the relocation. So track here the
408          * size of the largest device extent that has to be relocated. We track
409          * only the largest and not the sum of the sizes of all relocated block
410          * groups because after each block group is relocated the running
411          * transaction is committed so that pinned space is released.
412          */
413         u64 scratch_space = 0;
414
415         /*
416          * List of device extents is sorted by descending order of the extent's
417          * end offset. If some extent goes beyond the computed minimum size,
418          * which initially matches the sum of the lenghts of all extents,
419          * we need to check if the extent can be relocated to an hole in the
420          * device between [0, *min_size[ (which is what the resize ioctl does).
421          */
422         while (!list_empty(extents)) {
423                 struct dev_extent_elem *e;
424                 struct dev_extent_elem *h;
425                 int found = 0;
426                 u64 extent_len;
427                 u64 hole_len = 0;
428
429                 e = list_first_entry(extents, struct dev_extent_elem, list);
430                 if (e->end <= *min_size)
431                         break;
432
433                 /*
434                  * Our extent goes beyond the computed *min_size. See if we can
435                  * find a hole large enough to relocate it to. If not we must stop
436                  * and set *min_size to the end of the extent.
437                  */
438                 extent_len = e->end - e->start + 1;
439                 list_for_each_entry(h, holes, list) {
440                         hole_len = h->end - h->start + 1;
441                         if (hole_len >= extent_len) {
442                                 found = 1;
443                                 break;
444                         }
445                 }
446
447                 if (!found) {
448                         *min_size = e->end + 1;
449                         break;
450                 }
451
452                 /*
453                  * If the hole found contains the location for a superblock
454                  * mirror, we are pessimistic and require allocating one
455                  * more extent of the same size. This is because the block
456                  * group could be in the worst case used by a single extent
457                  * with a size >= (block_group.length - superblock.size).
458                  */
459                 if (hole_includes_sb_mirror(h->start,
460                                             h->start + extent_len - 1))
461                         *min_size += extent_len;
462
463                 if (hole_len > extent_len) {
464                         h->start += extent_len;
465                 } else {
466                         list_del(&h->list);
467                         free(h);
468                 }
469
470                 list_del(&e->list);
471                 free(e);
472
473                 if (extent_len > scratch_space)
474                         scratch_space = extent_len;
475         }
476
477         if (scratch_space) {
478                 *min_size += scratch_space;
479                 /*
480                  * Chunk allocation requires inserting/updating items in the
481                  * chunk tree, so often this can lead to the need of allocating
482                  * a new system chunk too, which has a maximum size of 32Mb.
483                  */
484                 *min_size += 32 * 1024 * 1024;
485         }
486 }
487
488 static int print_min_dev_size(int fd, u64 devid)
489 {
490         int ret = 1;
491         /*
492          * Device allocations starts at 1Mb or at the value passed through the
493          * mount option alloc_start if it's bigger than 1Mb. The alloc_start
494          * option is used for debugging and testing only, and recently the
495          * possibility of deprecating/removing it has been discussed, so we
496          * ignore it here.
497          */
498         u64 min_size = 1 * 1024 * 1024ull;
499         struct btrfs_ioctl_search_args args;
500         struct btrfs_ioctl_search_key *sk = &args.key;
501         u64 last_pos = (u64)-1;
502         LIST_HEAD(extents);
503         LIST_HEAD(holes);
504
505         memset(&args, 0, sizeof(args));
506         sk->tree_id = BTRFS_DEV_TREE_OBJECTID;
507         sk->min_objectid = devid;
508         sk->max_objectid = devid;
509         sk->max_type = BTRFS_DEV_EXTENT_KEY;
510         sk->min_type = BTRFS_DEV_EXTENT_KEY;
511         sk->min_offset = 0;
512         sk->max_offset = (u64)-1;
513         sk->min_transid = 0;
514         sk->max_transid = (u64)-1;
515         sk->nr_items = 4096;
516
517         while (1) {
518                 int i;
519                 struct btrfs_ioctl_search_header *sh;
520                 unsigned long off = 0;
521
522                 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
523                 if (ret < 0) {
524                         fprintf(stderr,
525                                 "Error invoking tree search ioctl: %s\n",
526                                 strerror(errno));
527                         ret = 1;
528                         goto out;
529                 }
530
531                 if (sk->nr_items == 0)
532                         break;
533
534                 for (i = 0; i < sk->nr_items; i++) {
535                         struct btrfs_dev_extent *extent;
536                         u64 len;
537
538                         sh = (struct btrfs_ioctl_search_header *)(args.buf +
539                                                                   off);
540                         off += sizeof(*sh);
541                         extent = (struct btrfs_dev_extent *)(args.buf + off);
542                         off += sh->len;
543
544                         sk->min_objectid = sh->objectid;
545                         sk->min_type = sh->type;
546                         sk->min_offset = sh->offset + 1;
547
548                         if (sh->objectid != devid ||
549                             sh->type != BTRFS_DEV_EXTENT_KEY)
550                                 continue;
551
552                         len = btrfs_stack_dev_extent_length(extent);
553                         min_size += len;
554                         ret = add_dev_extent(&extents, sh->offset,
555                                              sh->offset + len - 1, 0);
556
557                         if (!ret && last_pos != (u64)-1 &&
558                             last_pos != sh->offset)
559                                 ret = add_dev_extent(&holes, last_pos,
560                                                      sh->offset - 1, 1);
561                         if (ret) {
562                                 fprintf(stderr, "Error: %s\n", strerror(-ret));
563                                 ret = 1;
564                                 goto out;
565                         }
566
567                         last_pos = sh->offset + len;
568                 }
569
570                 if (sk->min_type != BTRFS_DEV_EXTENT_KEY ||
571                     sk->min_objectid != devid)
572                         break;
573         }
574
575         adjust_dev_min_size(&extents, &holes, &min_size);
576         printf("%llu bytes (%s)\n", min_size, pretty_size(min_size));
577         ret = 0;
578 out:
579         free_dev_extent_list(&extents);
580         free_dev_extent_list(&holes);
581
582         return ret;
583 }
584
585 static int cmd_inspect_min_dev_size(int argc, char **argv)
586 {
587         int ret;
588         int fd = -1;
589         DIR *dirstream = NULL;
590         u64 devid = 1;
591
592         while (1) {
593                 int c;
594                 enum { GETOPT_VAL_DEVID = 256 };
595                 static const struct option long_options[] = {
596                         { "id", required_argument, NULL, GETOPT_VAL_DEVID },
597                         {NULL, 0, NULL, 0}
598                 };
599
600                 c = getopt_long(argc, argv, "", long_options, NULL);
601                 if (c < 0)
602                         break;
603
604                 switch (c) {
605                 case GETOPT_VAL_DEVID:
606                         devid = arg_strtou64(optarg);
607                         break;
608                 default:
609                         usage(cmd_inspect_min_dev_size_usage);
610                 }
611         }
612         if (check_argc_exact(argc - optind, 1))
613                 usage(cmd_inspect_min_dev_size_usage);
614
615         fd = btrfs_open_dir(argv[optind], &dirstream, 1);
616         if (fd < 0) {
617                 ret = -ENOENT;
618                 goto out;
619         }
620
621         ret = print_min_dev_size(fd, devid);
622         close_file_or_dir(fd, dirstream);
623 out:
624         return !!ret;
625 }
626
627 static int cmd_inspect_dump_tree_hook(int argc, char **argv)
628 {
629         return cmd_inspect_dump_tree(argc, argv);
630 }
631
632 static int cmd_inspect_dump_super_hook(int argc, char **argv)
633 {
634         return cmd_inspect_dump_super(argc, argv);
635 }
636
637 static const char inspect_cmd_group_info[] =
638 "query various internal information";
639
640 const struct cmd_group inspect_cmd_group = {
641         inspect_cmd_group_usage, inspect_cmd_group_info, {
642                 { "inode-resolve", cmd_inspect_inode_resolve,
643                         cmd_inspect_inode_resolve_usage, NULL, 0 },
644                 { "logical-resolve", cmd_inspect_logical_resolve,
645                         cmd_inspect_logical_resolve_usage, NULL, 0 },
646                 { "subvolid-resolve", cmd_inspect_subvolid_resolve,
647                         cmd_inspect_subvolid_resolve_usage, NULL, 0 },
648                 { "rootid", cmd_inspect_rootid, cmd_inspect_rootid_usage, NULL,
649                         0 },
650                 { "min-dev-size", cmd_inspect_min_dev_size,
651                         cmd_inspect_min_dev_size_usage, NULL, 0 },
652                 { "dump-tree", cmd_inspect_dump_tree_hook,
653                                 cmd_inspect_dump_tree_usage, NULL, 0 },
654                 { "dump-super", cmd_inspect_dump_super_hook,
655                                 cmd_inspect_dump_super_usage, NULL, 0 },
656                 NULL_CMD_STRUCT
657         }
658 };
659
660 int cmd_inspect(int argc, char **argv)
661 {
662         return handle_command_group(&inspect_cmd_group, argc, argv);
663 }