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.
21 #include <sys/ioctl.h>
26 #include "kerncompat.h"
30 #include "send-utils.h"
33 #include "btrfs-list.h"
35 static const char * const inspect_cmd_group_usage[] = {
36 "btrfs inspect-internal <command> <args>",
40 static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
44 struct btrfs_ioctl_ino_path_args ipa;
45 struct btrfs_data_container fspath[PATH_MAX];
47 memset(fspath, 0, sizeof(*fspath));
50 ipa.fspath = ptr_to_u64(fspath);
52 ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
54 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
59 printf("ioctl ret=%d, bytes_left=%lu, bytes_missing=%lu, "
60 "cnt=%d, missed=%d\n", ret,
61 (unsigned long)fspath->bytes_left,
62 (unsigned long)fspath->bytes_missing,
63 fspath->elem_cnt, fspath->elem_missed);
65 for (i = 0; i < fspath->elem_cnt; ++i) {
68 ptr = (u64)(unsigned long)fspath->val;
69 ptr += fspath->val[i];
70 str = (char *)(unsigned long)ptr;
72 printf("%s/%s\n", prepend, str);
81 static const char * const cmd_inspect_inode_resolve_usage[] = {
82 "btrfs inspect-internal inode-resolve [-v] <inode> <path>",
83 "Get file system paths for the given inode",
89 static int cmd_inspect_inode_resolve(int argc, char **argv)
94 DIR *dirstream = NULL;
98 int c = getopt(argc, argv, "v");
107 usage(cmd_inspect_inode_resolve_usage);
111 if (check_argc_exact(argc - optind, 2))
112 usage(cmd_inspect_inode_resolve_usage);
114 fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
118 ret = __ino_to_path_fd(arg_strtou64(argv[optind]), fd, verbose,
120 close_file_or_dir(fd, dirstream);
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",
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",
136 static int cmd_inspect_logical_resolve(int argc, char **argv)
144 struct btrfs_ioctl_logical_ino_args loi;
145 struct btrfs_data_container *inodes;
147 char full_path[4096];
149 DIR *dirstream = NULL;
153 int c = getopt(argc, argv, "Pvs:");
165 size = arg_strtou64(optarg);
168 usage(cmd_inspect_logical_resolve_usage);
172 if (check_argc_exact(argc - optind, 2))
173 usage(cmd_inspect_logical_resolve_usage);
175 size = min(size, (u64)64 * 1024);
176 inodes = malloc(size);
180 memset(inodes, 0, sizeof(*inodes));
181 loi.logical = arg_strtou64(argv[optind]);
183 loi.inodes = ptr_to_u64(inodes);
185 fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
191 ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
193 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
198 printf("ioctl ret=%d, total_size=%llu, bytes_left=%lu, "
199 "bytes_missing=%lu, cnt=%d, missed=%d\n",
201 (unsigned long)inodes->bytes_left,
202 (unsigned long)inodes->bytes_missing,
203 inodes->elem_cnt, inodes->elem_missed);
205 bytes_left = sizeof(full_path);
206 ret = snprintf(full_path, bytes_left, "%s/", argv[optind+1]);
207 path_ptr = full_path + ret;
208 bytes_left -= ret + 1;
209 BUG_ON(bytes_left < 0);
211 for (i = 0; i < inodes->elem_cnt; i += 3) {
212 u64 inum = inodes->val[i];
213 u64 offset = inodes->val[i+1];
214 u64 root = inodes->val[i+2];
220 name = btrfs_list_path_for_root(fd, root);
230 ret = snprintf(path_ptr, bytes_left, "%s",
232 BUG_ON(ret >= bytes_left);
234 path_fd = btrfs_open_dir(full_path, &dirs, 1);
240 __ino_to_path_fd(inum, path_fd, verbose, full_path);
242 close_file_or_dir(path_fd, dirs);
244 printf("inode %llu offset %llu root %llu\n", inum,
250 close_file_or_dir(fd, dirstream);
255 static const char * const cmd_inspect_subvolid_resolve_usage[] = {
256 "btrfs inspect-internal subvolid-resolve <subvolid> <path>",
257 "Get file system paths for the given subvolume ID.",
261 static int cmd_inspect_subvolid_resolve(int argc, char **argv)
267 DIR *dirstream = NULL;
269 if (check_argc_exact(argc, 3))
270 usage(cmd_inspect_subvolid_resolve_usage);
272 fd = btrfs_open_dir(argv[2], &dirstream, 1);
278 subvol_id = arg_strtou64(argv[1]);
279 ret = btrfs_subvolid_resolve(fd, path, sizeof(path), subvol_id);
283 "%s: btrfs_subvolid_resolve(subvol_id %llu) failed with ret=%d\n",
284 argv[0], (unsigned long long)subvol_id, ret);
288 path[PATH_MAX - 1] = '\0';
289 printf("%s\n", path);
292 close_file_or_dir(fd, dirstream);
296 static const char* const cmd_inspect_rootid_usage[] = {
297 "btrfs inspect-internal rootid <path>",
298 "Get tree ID of the containing subvolume of path.",
302 static int cmd_inspect_rootid(int argc, char **argv)
307 DIR *dirstream = NULL;
309 if (check_argc_exact(argc, 2))
310 usage(cmd_inspect_rootid_usage);
312 fd = btrfs_open_dir(argv[1], &dirstream, 1);
318 ret = lookup_ino_rootid(fd, &rootid);
320 fprintf(stderr, "%s: rootid failed with ret=%d\n",
325 printf("%llu\n", (unsigned long long)rootid);
327 close_file_or_dir(fd, dirstream);
332 static const char* const cmd_inspect_min_dev_size_usage[] = {
333 "btrfs inspect-internal min-dev-size [options] <path>",
334 "Get the minimum size the device can be shrunk to. The",
335 "device id 1 is used by default.",
336 "--id DEVID specify the device id to query",
340 struct dev_extent_elem {
344 struct list_head list;
347 static int add_dev_extent(struct list_head *list,
348 const u64 start, const u64 end,
351 struct dev_extent_elem *e;
353 e = malloc(sizeof(*e));
361 list_add_tail(&e->list, list);
363 list_add(&e->list, list);
368 static void free_dev_extent_list(struct list_head *list)
370 while (!list_empty(list)) {
371 struct dev_extent_elem *e;
373 e = list_first_entry(list, struct dev_extent_elem, list);
379 static int hole_includes_sb_mirror(const u64 start, const u64 end)
384 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
385 u64 bytenr = btrfs_sb_offset(i);
387 if (bytenr >= start && bytenr <= end) {
396 static void adjust_dev_min_size(struct list_head *extents,
397 struct list_head *holes,
401 * If relocation of the block group of a device extent must happen (see
402 * below) scratch space is used for the relocation. So track here the
403 * size of the largest device extent that has to be relocated. We track
404 * only the largest and not the sum of the sizes of all relocated block
405 * groups because after each block group is relocated the running
406 * transaction is committed so that pinned space is released.
408 u64 scratch_space = 0;
411 * List of device extents is sorted by descending order of the extent's
412 * end offset. If some extent goes beyond the computed minimum size,
413 * which initially matches the sum of the lenghts of all extents,
414 * we need to check if the extent can be relocated to an hole in the
415 * device between [0, *min_size[ (which is what the resize ioctl does).
417 while (!list_empty(extents)) {
418 struct dev_extent_elem *e;
419 struct dev_extent_elem *h;
424 e = list_first_entry(extents, struct dev_extent_elem, list);
425 if (e->end <= *min_size)
429 * Our extent goes beyond the computed *min_size. See if we can
430 * find a hole large enough to relocate it to. If not we must stop
431 * and set *min_size to the end of the extent.
433 extent_len = e->end - e->start + 1;
434 list_for_each_entry(h, holes, list) {
435 hole_len = h->end - h->start + 1;
436 if (hole_len >= extent_len) {
443 *min_size = e->end + 1;
448 * If the hole found contains the location for a superblock
449 * mirror, we are pessimistic and require allocating one
450 * more extent of the same size. This is because the block
451 * group could be in the worst case used by a single extent
452 * with a size >= (block_group.length - superblock.size).
454 if (hole_includes_sb_mirror(h->start,
455 h->start + extent_len - 1))
456 *min_size += extent_len;
458 if (hole_len > extent_len) {
459 h->start += extent_len;
468 if (extent_len > scratch_space)
469 scratch_space = extent_len;
473 *min_size += scratch_space;
475 * Chunk allocation requires inserting/updating items in the
476 * chunk tree, so often this can lead to the need of allocating
477 * a new system chunk too, which has a maximum size of 32Mb.
479 *min_size += 32 * 1024 * 1024;
483 static int print_min_dev_size(int fd, u64 devid)
487 * Device allocations starts at 1Mb or at the value passed through the
488 * mount option alloc_start if it's bigger than 1Mb. The alloc_start
489 * option is used for debugging and testing only, and recently the
490 * possibility of deprecating/removing it has been discussed, so we
493 u64 min_size = 1 * 1024 * 1024ull;
494 struct btrfs_ioctl_search_args args;
495 struct btrfs_ioctl_search_key *sk = &args.key;
496 u64 last_pos = (u64)-1;
500 memset(&args, 0, sizeof(args));
501 sk->tree_id = BTRFS_DEV_TREE_OBJECTID;
502 sk->min_objectid = devid;
503 sk->max_objectid = devid;
504 sk->max_type = BTRFS_DEV_EXTENT_KEY;
505 sk->min_type = BTRFS_DEV_EXTENT_KEY;
507 sk->max_offset = (u64)-1;
509 sk->max_transid = (u64)-1;
514 struct btrfs_ioctl_search_header *sh;
515 unsigned long off = 0;
517 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
520 "Error invoking tree search ioctl: %s\n",
526 if (sk->nr_items == 0)
529 for (i = 0; i < sk->nr_items; i++) {
530 struct btrfs_dev_extent *extent;
533 sh = (struct btrfs_ioctl_search_header *)(args.buf +
536 extent = (struct btrfs_dev_extent *)(args.buf + off);
539 sk->min_objectid = sh->objectid;
540 sk->min_type = sh->type;
541 sk->min_offset = sh->offset + 1;
543 if (sh->objectid != devid ||
544 sh->type != BTRFS_DEV_EXTENT_KEY)
547 len = btrfs_stack_dev_extent_length(extent);
549 ret = add_dev_extent(&extents, sh->offset,
550 sh->offset + len - 1, 0);
552 if (!ret && last_pos != (u64)-1 &&
553 last_pos != sh->offset)
554 ret = add_dev_extent(&holes, last_pos,
557 fprintf(stderr, "Error: %s\n", strerror(-ret));
562 last_pos = sh->offset + len;
565 if (sk->min_type != BTRFS_DEV_EXTENT_KEY ||
566 sk->min_objectid != devid)
570 adjust_dev_min_size(&extents, &holes, &min_size);
571 printf("%llu bytes (%s)\n", min_size, pretty_size(min_size));
574 free_dev_extent_list(&extents);
575 free_dev_extent_list(&holes);
580 static int cmd_inspect_min_dev_size(int argc, char **argv)
584 DIR *dirstream = NULL;
589 enum { GETOPT_VAL_DEVID = 256 };
590 static const struct option long_options[] = {
591 { "id", required_argument, NULL, GETOPT_VAL_DEVID },
595 c = getopt_long(argc, argv, "", long_options, NULL);
600 case GETOPT_VAL_DEVID:
601 devid = arg_strtou64(optarg);
604 usage(cmd_inspect_min_dev_size_usage);
607 if (check_argc_exact(argc - optind, 1))
608 usage(cmd_inspect_min_dev_size_usage);
610 fd = btrfs_open_dir(argv[optind], &dirstream, 1);
616 ret = print_min_dev_size(fd, devid);
617 close_file_or_dir(fd, dirstream);
622 static const char inspect_cmd_group_info[] =
623 "query various internal information";
625 const struct cmd_group inspect_cmd_group = {
626 inspect_cmd_group_usage, inspect_cmd_group_info, {
627 { "inode-resolve", cmd_inspect_inode_resolve,
628 cmd_inspect_inode_resolve_usage, NULL, 0 },
629 { "logical-resolve", cmd_inspect_logical_resolve,
630 cmd_inspect_logical_resolve_usage, NULL, 0 },
631 { "subvolid-resolve", cmd_inspect_subvolid_resolve,
632 cmd_inspect_subvolid_resolve_usage, NULL, 0 },
633 { "rootid", cmd_inspect_rootid, cmd_inspect_rootid_usage, NULL,
635 { "min-dev-size", cmd_inspect_min_dev_size,
636 cmd_inspect_min_dev_size_usage, NULL, 0 },
641 int cmd_inspect(int argc, char **argv)
643 return handle_command_group(&inspect_cmd_group, argc, argv);