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