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"
34 #include "cmds-inspect-dump-tree.h"
35 #include "cmds-inspect-dump-super.h"
37 static const char * const inspect_cmd_group_usage[] = {
38 "btrfs inspect-internal <command> <args>",
42 static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
46 struct btrfs_ioctl_ino_path_args ipa;
47 struct btrfs_data_container fspath[PATH_MAX];
49 memset(fspath, 0, sizeof(*fspath));
52 ipa.fspath = ptr_to_u64(fspath);
54 ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
56 error("ino paths ioctl: %s", strerror(errno));
61 printf("ioctl ret=%d, bytes_left=%lu, bytes_missing=%lu, "
62 "cnt=%d, missed=%d\n", ret,
63 (unsigned long)fspath->bytes_left,
64 (unsigned long)fspath->bytes_missing,
65 fspath->elem_cnt, fspath->elem_missed);
67 for (i = 0; i < fspath->elem_cnt; ++i) {
70 ptr = (u64)(unsigned long)fspath->val;
71 ptr += fspath->val[i];
72 str = (char *)(unsigned long)ptr;
74 printf("%s/%s\n", prepend, str);
83 static const char * const cmd_inspect_inode_resolve_usage[] = {
84 "btrfs inspect-internal inode-resolve [-v] <inode> <path>",
85 "Get file system paths for the given inode",
91 static int cmd_inspect_inode_resolve(int argc, char **argv)
96 DIR *dirstream = NULL;
100 int c = getopt(argc, argv, "v");
109 usage(cmd_inspect_inode_resolve_usage);
113 if (check_argc_exact(argc - optind, 2))
114 usage(cmd_inspect_inode_resolve_usage);
116 fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
120 ret = __ino_to_path_fd(arg_strtou64(argv[optind]), fd, verbose,
122 close_file_or_dir(fd, dirstream);
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",
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",
138 static int cmd_inspect_logical_resolve(int argc, char **argv)
146 struct btrfs_ioctl_logical_ino_args loi;
147 struct btrfs_data_container *inodes;
149 char full_path[4096];
151 DIR *dirstream = NULL;
155 int c = getopt(argc, argv, "Pvs:");
167 size = arg_strtou64(optarg);
170 usage(cmd_inspect_logical_resolve_usage);
174 if (check_argc_exact(argc - optind, 2))
175 usage(cmd_inspect_logical_resolve_usage);
177 size = min(size, (u64)64 * 1024);
178 inodes = malloc(size);
182 memset(inodes, 0, sizeof(*inodes));
183 loi.logical = arg_strtou64(argv[optind]);
185 loi.inodes = ptr_to_u64(inodes);
187 fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
193 ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
195 error("logical ino ioctl: %s", strerror(errno));
200 printf("ioctl ret=%d, total_size=%llu, bytes_left=%lu, "
201 "bytes_missing=%lu, cnt=%d, missed=%d\n",
203 (unsigned long)inodes->bytes_left,
204 (unsigned long)inodes->bytes_missing,
205 inodes->elem_cnt, inodes->elem_missed);
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 BUG_ON(bytes_left < 0);
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];
222 name = btrfs_list_path_for_root(fd, root);
232 ret = snprintf(path_ptr, bytes_left, "%s",
234 BUG_ON(ret >= bytes_left);
236 path_fd = btrfs_open_dir(full_path, &dirs, 1);
242 __ino_to_path_fd(inum, path_fd, verbose, full_path);
244 close_file_or_dir(path_fd, dirs);
246 printf("inode %llu offset %llu root %llu\n", inum,
252 close_file_or_dir(fd, dirstream);
257 static const char * const cmd_inspect_subvolid_resolve_usage[] = {
258 "btrfs inspect-internal subvolid-resolve <subvolid> <path>",
259 "Get file system paths for the given subvolume ID.",
263 static int cmd_inspect_subvolid_resolve(int argc, char **argv)
269 DIR *dirstream = NULL;
271 clean_args_no_options(argc, argv, cmd_inspect_subvolid_resolve_usage);
273 if (check_argc_exact(argc - optind, 2))
274 usage(cmd_inspect_subvolid_resolve_usage);
276 fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
282 subvol_id = arg_strtou64(argv[optind]);
283 ret = btrfs_subvolid_resolve(fd, path, sizeof(path), subvol_id);
286 error("resolving subvolid %llu error %d",
287 (unsigned long long)subvol_id, ret);
291 path[PATH_MAX - 1] = '\0';
292 printf("%s\n", path);
295 close_file_or_dir(fd, dirstream);
299 static const char* const cmd_inspect_rootid_usage[] = {
300 "btrfs inspect-internal rootid <path>",
301 "Get tree ID of the containing subvolume of path.",
305 static int cmd_inspect_rootid(int argc, char **argv)
310 DIR *dirstream = NULL;
312 clean_args_no_options(argc, argv, cmd_inspect_rootid_usage);
314 if (check_argc_exact(argc - optind, 1))
315 usage(cmd_inspect_rootid_usage);
317 fd = btrfs_open_dir(argv[optind], &dirstream, 1);
323 ret = lookup_ino_rootid(fd, &rootid);
325 error("rootid failed with ret=%d", ret);
329 printf("%llu\n", (unsigned long long)rootid);
331 close_file_or_dir(fd, dirstream);
336 static const char* const cmd_inspect_min_dev_size_usage[] = {
337 "btrfs inspect-internal min-dev-size [options] <path>",
338 "Get the minimum size the device can be shrunk to. The",
339 "device id 1 is used by default.",
340 "--id DEVID specify the device id to query",
344 struct dev_extent_elem {
348 struct list_head list;
351 static int add_dev_extent(struct list_head *list,
352 const u64 start, const u64 end,
355 struct dev_extent_elem *e;
357 e = malloc(sizeof(*e));
365 list_add_tail(&e->list, list);
367 list_add(&e->list, list);
372 static void free_dev_extent_list(struct list_head *list)
374 while (!list_empty(list)) {
375 struct dev_extent_elem *e;
377 e = list_first_entry(list, struct dev_extent_elem, list);
383 static int hole_includes_sb_mirror(const u64 start, const u64 end)
388 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
389 u64 bytenr = btrfs_sb_offset(i);
391 if (bytenr >= start && bytenr <= end) {
400 static void adjust_dev_min_size(struct list_head *extents,
401 struct list_head *holes,
405 * If relocation of the block group of a device extent must happen (see
406 * below) scratch space is used for the relocation. So track here the
407 * size of the largest device extent that has to be relocated. We track
408 * only the largest and not the sum of the sizes of all relocated block
409 * groups because after each block group is relocated the running
410 * transaction is committed so that pinned space is released.
412 u64 scratch_space = 0;
415 * List of device extents is sorted by descending order of the extent's
416 * end offset. If some extent goes beyond the computed minimum size,
417 * which initially matches the sum of the lenghts of all extents,
418 * we need to check if the extent can be relocated to an hole in the
419 * device between [0, *min_size[ (which is what the resize ioctl does).
421 while (!list_empty(extents)) {
422 struct dev_extent_elem *e;
423 struct dev_extent_elem *h;
428 e = list_first_entry(extents, struct dev_extent_elem, list);
429 if (e->end <= *min_size)
433 * Our extent goes beyond the computed *min_size. See if we can
434 * find a hole large enough to relocate it to. If not we must stop
435 * and set *min_size to the end of the extent.
437 extent_len = e->end - e->start + 1;
438 list_for_each_entry(h, holes, list) {
439 hole_len = h->end - h->start + 1;
440 if (hole_len >= extent_len) {
447 *min_size = e->end + 1;
452 * If the hole found contains the location for a superblock
453 * mirror, we are pessimistic and require allocating one
454 * more extent of the same size. This is because the block
455 * group could be in the worst case used by a single extent
456 * with a size >= (block_group.length - superblock.size).
458 if (hole_includes_sb_mirror(h->start,
459 h->start + extent_len - 1))
460 *min_size += extent_len;
462 if (hole_len > extent_len) {
463 h->start += extent_len;
472 if (extent_len > scratch_space)
473 scratch_space = extent_len;
477 *min_size += scratch_space;
479 * Chunk allocation requires inserting/updating items in the
480 * chunk tree, so often this can lead to the need of allocating
481 * a new system chunk too, which has a maximum size of 32Mb.
483 *min_size += 32 * 1024 * 1024;
487 static int print_min_dev_size(int fd, u64 devid)
491 * Device allocations starts at 1Mb or at the value passed through the
492 * mount option alloc_start if it's bigger than 1Mb. The alloc_start
493 * option is used for debugging and testing only, and recently the
494 * possibility of deprecating/removing it has been discussed, so we
497 u64 min_size = 1 * 1024 * 1024ull;
498 struct btrfs_ioctl_search_args args;
499 struct btrfs_ioctl_search_key *sk = &args.key;
500 u64 last_pos = (u64)-1;
504 memset(&args, 0, sizeof(args));
505 sk->tree_id = BTRFS_DEV_TREE_OBJECTID;
506 sk->min_objectid = devid;
507 sk->max_objectid = devid;
508 sk->max_type = BTRFS_DEV_EXTENT_KEY;
509 sk->min_type = BTRFS_DEV_EXTENT_KEY;
511 sk->max_offset = (u64)-1;
513 sk->max_transid = (u64)-1;
518 struct btrfs_ioctl_search_header *sh;
519 unsigned long off = 0;
521 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
523 error("tree search ioctl: %s", strerror(errno));
528 if (sk->nr_items == 0)
531 for (i = 0; i < sk->nr_items; i++) {
532 struct btrfs_dev_extent *extent;
535 sh = (struct btrfs_ioctl_search_header *)(args.buf +
538 extent = (struct btrfs_dev_extent *)(args.buf + off);
541 sk->min_objectid = sh->objectid;
542 sk->min_type = sh->type;
543 sk->min_offset = sh->offset + 1;
545 if (sh->objectid != devid ||
546 sh->type != BTRFS_DEV_EXTENT_KEY)
549 len = btrfs_stack_dev_extent_length(extent);
551 ret = add_dev_extent(&extents, sh->offset,
552 sh->offset + len - 1, 0);
554 if (!ret && last_pos != (u64)-1 &&
555 last_pos != sh->offset)
556 ret = add_dev_extent(&holes, last_pos,
559 error("add device extent: %s", strerror(-ret));
564 last_pos = sh->offset + len;
567 if (sk->min_type != BTRFS_DEV_EXTENT_KEY ||
568 sk->min_objectid != devid)
572 adjust_dev_min_size(&extents, &holes, &min_size);
573 printf("%llu bytes (%s)\n", min_size, pretty_size(min_size));
576 free_dev_extent_list(&extents);
577 free_dev_extent_list(&holes);
582 static int cmd_inspect_min_dev_size(int argc, char **argv)
586 DIR *dirstream = NULL;
591 enum { GETOPT_VAL_DEVID = 256 };
592 static const struct option long_options[] = {
593 { "id", required_argument, NULL, GETOPT_VAL_DEVID },
597 c = getopt_long(argc, argv, "", long_options, NULL);
602 case GETOPT_VAL_DEVID:
603 devid = arg_strtou64(optarg);
606 usage(cmd_inspect_min_dev_size_usage);
609 if (check_argc_exact(argc - optind, 1))
610 usage(cmd_inspect_min_dev_size_usage);
612 fd = btrfs_open_dir(argv[optind], &dirstream, 1);
618 ret = print_min_dev_size(fd, devid);
619 close_file_or_dir(fd, dirstream);
624 static const char inspect_cmd_group_info[] =
625 "query various internal information";
627 const struct cmd_group inspect_cmd_group = {
628 inspect_cmd_group_usage, inspect_cmd_group_info, {
629 { "inode-resolve", cmd_inspect_inode_resolve,
630 cmd_inspect_inode_resolve_usage, NULL, 0 },
631 { "logical-resolve", cmd_inspect_logical_resolve,
632 cmd_inspect_logical_resolve_usage, NULL, 0 },
633 { "subvolid-resolve", cmd_inspect_subvolid_resolve,
634 cmd_inspect_subvolid_resolve_usage, NULL, 0 },
635 { "rootid", cmd_inspect_rootid, cmd_inspect_rootid_usage, NULL,
637 { "min-dev-size", cmd_inspect_min_dev_size,
638 cmd_inspect_min_dev_size_usage, NULL, 0 },
639 { "dump-tree", cmd_inspect_dump_tree,
640 cmd_inspect_dump_tree_usage, NULL, 0 },
641 { "dump-super", cmd_inspect_dump_super,
642 cmd_inspect_dump_super_usage, NULL, 0 },
647 int cmd_inspect(int argc, char **argv)
649 return handle_command_group(&inspect_cmd_group, argc, argv);