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