btrfs-progs: remove stale dir-test
[btrfs-progs-unstable/devel.git] / cmds-inspect.c
blob2fc50c1a2d9097dc455c57492cf1ddc37c496132
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.
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.
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.
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>
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"
36 static const char * const inspect_cmd_group_usage[] = {
37 "btrfs inspect-internal <command> <args>",
38 NULL
41 static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
43 int ret;
44 int i;
45 struct btrfs_ioctl_ino_path_args ipa;
46 struct btrfs_data_container fspath[PATH_MAX];
48 memset(fspath, 0, sizeof(*fspath));
49 ipa.inum = inum;
50 ipa.size = PATH_MAX;
51 ipa.fspath = ptr_to_u64(fspath);
53 ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
54 if (ret < 0) {
55 error("ino paths ioctl: %m");
56 goto out;
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);
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);
78 out:
79 return !!ret;
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
90 static int cmd_inspect_inode_resolve(int argc, char **argv)
92 int fd;
93 int verbose = 0;
94 int ret;
95 DIR *dirstream = NULL;
97 optind = 0;
98 while (1) {
99 int c = getopt(argc, argv, "v");
100 if (c < 0)
101 break;
103 switch (c) {
104 case 'v':
105 verbose = 1;
106 break;
107 default:
108 usage(cmd_inspect_inode_resolve_usage);
112 if (check_argc_exact(argc - optind, 2))
113 usage(cmd_inspect_inode_resolve_usage);
115 fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
116 if (fd < 0)
117 return 1;
119 ret = __ino_to_path_fd(arg_strtou64(argv[optind]), fd, verbose,
120 argv[optind+1]);
121 close_file_or_dir(fd, dirstream);
122 return !!ret;
126 static const char * const cmd_inspect_logical_resolve_usage[] = {
127 "btrfs inspect-internal logical-resolve [-Pv] [-s bufsize] <logical> <path>",
128 "Get file system paths for the given logical address",
129 "-P skip the path resolving and print the inodes instead",
130 "-v verbose mode",
131 "-s bufsize set inode container's size. This is used to increase inode",
132 " container's size in case it is not enough to read all the ",
133 " resolved results. The max value one can set is 64k",
134 NULL
137 static int cmd_inspect_logical_resolve(int argc, char **argv)
139 int ret;
140 int fd;
141 int i;
142 int verbose = 0;
143 int getpath = 1;
144 int bytes_left;
145 struct btrfs_ioctl_logical_ino_args loi;
146 struct btrfs_data_container *inodes;
147 u64 size = 4096;
148 char full_path[PATH_MAX];
149 char *path_ptr;
150 DIR *dirstream = NULL;
152 optind = 0;
153 while (1) {
154 int c = getopt(argc, argv, "Pvs:");
155 if (c < 0)
156 break;
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);
173 if (check_argc_exact(argc - optind, 2))
174 usage(cmd_inspect_logical_resolve_usage);
176 size = min(size, (u64)SZ_64K);
177 inodes = malloc(size);
178 if (!inodes)
179 return 1;
181 memset(inodes, 0, sizeof(*inodes));
182 loi.logical = arg_strtou64(argv[optind]);
183 loi.size = size;
184 loi.inodes = ptr_to_u64(inodes);
186 fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
187 if (fd < 0) {
188 ret = 12;
189 goto out;
192 ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
193 if (ret < 0) {
194 error("logical ino ioctl: %m");
195 goto out;
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);
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;
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;
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;
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;
242 path_fd = btrfs_open_dir(full_path, &dirs, 1);
243 if (path_fd < 0) {
244 ret = -ENOENT;
245 goto out;
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);
257 out:
258 close_file_or_dir(fd, dirstream);
259 free(inodes);
260 return !!ret;
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
269 static int cmd_inspect_subvolid_resolve(int argc, char **argv)
271 int ret;
272 int fd = -1;
273 u64 subvol_id;
274 char path[PATH_MAX];
275 DIR *dirstream = NULL;
277 clean_args_no_options(argc, argv, cmd_inspect_subvolid_resolve_usage);
279 if (check_argc_exact(argc - optind, 2))
280 usage(cmd_inspect_subvolid_resolve_usage);
282 fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
283 if (fd < 0) {
284 ret = -ENOENT;
285 goto out;
288 subvol_id = arg_strtou64(argv[optind]);
289 ret = btrfs_subvolid_resolve(fd, path, sizeof(path), subvol_id);
291 if (ret) {
292 error("resolving subvolid %llu error %d",
293 (unsigned long long)subvol_id, ret);
294 goto out;
297 path[PATH_MAX - 1] = '\0';
298 printf("%s\n", path);
300 out:
301 close_file_or_dir(fd, dirstream);
302 return !!ret;
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
311 static int cmd_inspect_rootid(int argc, char **argv)
313 int ret;
314 int fd = -1;
315 u64 rootid;
316 DIR *dirstream = NULL;
318 clean_args_no_options(argc, argv, cmd_inspect_rootid_usage);
320 if (check_argc_exact(argc - optind, 1))
321 usage(cmd_inspect_rootid_usage);
323 fd = btrfs_open_file_or_dir(argv[optind], &dirstream, 1);
324 if (fd < 0) {
325 ret = -ENOENT;
326 goto out;
329 ret = lookup_path_rootid(fd, &rootid);
330 if (ret) {
331 error("failed to lookup root id: %s", strerror(-ret));
332 goto out;
335 printf("%llu\n", (unsigned long long)rootid);
336 out:
337 close_file_or_dir(fd, dirstream);
339 return !!ret;
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
350 struct dev_extent_elem {
351 u64 start;
352 /* inclusive end */
353 u64 end;
354 struct list_head list;
357 static int add_dev_extent(struct list_head *list,
358 const u64 start, const u64 end,
359 const int append)
361 struct dev_extent_elem *e;
363 e = malloc(sizeof(*e));
364 if (!e)
365 return -ENOMEM;
367 e->start = start;
368 e->end = end;
370 if (append)
371 list_add_tail(&e->list, list);
372 else
373 list_add(&e->list, list);
375 return 0;
378 static void free_dev_extent_list(struct list_head *list)
380 while (!list_empty(list)) {
381 struct dev_extent_elem *e;
383 e = list_first_entry(list, struct dev_extent_elem, list);
384 list_del(&e->list);
385 free(e);
389 static int hole_includes_sb_mirror(const u64 start, const u64 end)
391 int i;
392 int ret = 0;
394 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
395 u64 bytenr = btrfs_sb_offset(i);
397 if (bytenr >= start && bytenr <= end) {
398 ret = 1;
399 break;
403 return ret;
406 static void adjust_dev_min_size(struct list_head *extents,
407 struct list_head *holes,
408 u64 *min_size)
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.
418 u64 scratch_space = 0;
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).
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;
434 e = list_first_entry(extents, struct dev_extent_elem, list);
435 if (e->end <= *min_size)
436 break;
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.
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;
452 if (!found) {
453 *min_size = e->end + 1;
454 break;
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).
464 if (hole_includes_sb_mirror(h->start,
465 h->start + extent_len - 1))
466 *min_size += extent_len;
468 if (hole_len > extent_len) {
469 h->start += extent_len;
470 } else {
471 list_del(&h->list);
472 free(h);
475 list_del(&e->list);
476 free(e);
478 if (extent_len > scratch_space)
479 scratch_space = extent_len;
482 if (scratch_space) {
483 *min_size += scratch_space;
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.
489 *min_size += SZ_32M;
493 static int print_min_dev_size(int fd, u64 devid)
495 int ret = 1;
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.
503 u64 min_size = SZ_1M;
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);
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;
522 while (1) {
523 int i;
524 struct btrfs_ioctl_search_header *sh;
525 unsigned long off = 0;
527 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
528 if (ret < 0) {
529 error("tree search ioctl: %m");
530 ret = 1;
531 goto out;
534 if (sk->nr_items == 0)
535 break;
537 for (i = 0; i < sk->nr_items; i++) {
538 struct btrfs_dev_extent *extent;
539 u64 len;
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);
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;
551 if (btrfs_search_header_objectid(sh) != devid ||
552 btrfs_search_header_type(sh) != BTRFS_DEV_EXTENT_KEY)
553 continue;
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);
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;
571 last_pos = btrfs_search_header_offset(sh) + len;
574 if (sk->min_type != BTRFS_DEV_EXTENT_KEY ||
575 sk->min_objectid != devid)
576 break;
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);
586 return ret;
589 static int cmd_inspect_min_dev_size(int argc, char **argv)
591 int ret;
592 int fd = -1;
593 DIR *dirstream = NULL;
594 u64 devid = 1;
596 optind = 0;
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}
605 c = getopt_long(argc, argv, "", long_options, NULL);
606 if (c < 0)
607 break;
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);
617 if (check_argc_exact(argc - optind, 1))
618 usage(cmd_inspect_min_dev_size_usage);
620 fd = btrfs_open_dir(argv[optind], &dirstream, 1);
621 if (fd < 0) {
622 ret = -ENOENT;
623 goto out;
626 ret = print_min_dev_size(fd, devid);
627 close_file_or_dir(fd, dirstream);
628 out:
629 return !!ret;
632 static const char inspect_cmd_group_info[] =
633 "query various internal information";
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
657 int cmd_inspect(int argc, char **argv)
659 return handle_command_group(&inspect_cmd_group, argc, argv);