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