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"
36 static const char * const inspect_cmd_group_usage
[] = {
37 "btrfs inspect-internal <command> <args>",
41 static int __ino_to_path_fd(u64 inum
, int fd
, int verbose
, const char *prepend
)
45 struct btrfs_ioctl_ino_path_args ipa
;
46 struct btrfs_data_container fspath
[PATH_MAX
];
48 memset(fspath
, 0, sizeof(*fspath
));
51 ipa
.fspath
= ptr_to_u64(fspath
);
53 ret
= ioctl(fd
, BTRFS_IOC_INO_PATHS
, &ipa
);
55 error("ino paths ioctl: %m");
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
) {
69 ptr
= (u64
)(unsigned long)fspath
->val
;
70 ptr
+= fspath
->val
[i
];
71 str
= (char *)(unsigned long)ptr
;
73 printf("%s/%s\n", prepend
, str
);
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",
90 static int cmd_inspect_inode_resolve(int argc
, char **argv
)
95 DIR *dirstream
= NULL
;
99 int c
= getopt(argc
, argv
, "v");
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);
119 ret
= __ino_to_path_fd(arg_strtou64(argv
[optind
]), fd
, verbose
,
121 close_file_or_dir(fd
, dirstream
);
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",
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",
137 static int cmd_inspect_logical_resolve(int argc
, char **argv
)
145 struct btrfs_ioctl_logical_ino_args loi
;
146 struct btrfs_data_container
*inodes
;
148 char full_path
[PATH_MAX
];
150 DIR *dirstream
= NULL
;
154 int c
= getopt(argc
, argv
, "Pvs:");
166 size
= arg_strtou64(optarg
);
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
);
181 memset(inodes
, 0, sizeof(*inodes
));
182 loi
.logical
= arg_strtou64(argv
[optind
]);
184 loi
.inodes
= ptr_to_u64(inodes
);
186 fd
= btrfs_open_dir(argv
[optind
+ 1], &dirstream
, 1);
192 ret
= ioctl(fd
, BTRFS_IOC_LOGICAL_INO
, &loi
);
194 error("logical ino ioctl: %m");
199 printf("ioctl ret=%d, total_size=%llu, bytes_left=%lu, "
200 "bytes_missing=%lu, cnt=%d, missed=%d\n",
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
);
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];
225 name
= btrfs_list_path_for_root(fd
, root
);
235 ret
= snprintf(path_ptr
, bytes_left
, "%s",
238 if (ret
>= bytes_left
) {
239 error("path buffer too small: %d bytes",
243 path_fd
= btrfs_open_dir(full_path
, &dirs
, 1);
249 ret
= __ino_to_path_fd(inum
, path_fd
, verbose
,
252 close_file_or_dir(path_fd
, dirs
);
254 printf("inode %llu offset %llu root %llu\n", inum
,
260 close_file_or_dir(fd
, dirstream
);
265 static const char * const cmd_inspect_subvolid_resolve_usage
[] = {
266 "btrfs inspect-internal subvolid-resolve <subvolid> <path>",
267 "Get file system paths for the given subvolume ID.",
271 static int cmd_inspect_subvolid_resolve(int argc
, char **argv
)
277 DIR *dirstream
= NULL
;
279 clean_args_no_options(argc
, argv
, cmd_inspect_subvolid_resolve_usage
);
281 if (check_argc_exact(argc
- optind
, 2))
282 usage(cmd_inspect_subvolid_resolve_usage
);
284 fd
= btrfs_open_dir(argv
[optind
+ 1], &dirstream
, 1);
290 subvol_id
= arg_strtou64(argv
[optind
]);
291 ret
= btrfs_subvolid_resolve(fd
, path
, sizeof(path
), subvol_id
);
294 error("resolving subvolid %llu error %d",
295 (unsigned long long)subvol_id
, ret
);
299 path
[PATH_MAX
- 1] = '\0';
300 printf("%s\n", path
);
303 close_file_or_dir(fd
, dirstream
);
307 static const char* const cmd_inspect_rootid_usage
[] = {
308 "btrfs inspect-internal rootid <path>",
309 "Get tree ID of the containing subvolume of path.",
313 static int cmd_inspect_rootid(int argc
, char **argv
)
318 DIR *dirstream
= NULL
;
320 clean_args_no_options(argc
, argv
, cmd_inspect_rootid_usage
);
322 if (check_argc_exact(argc
- optind
, 1))
323 usage(cmd_inspect_rootid_usage
);
325 fd
= btrfs_open_file_or_dir(argv
[optind
], &dirstream
, 1);
331 ret
= lookup_path_rootid(fd
, &rootid
);
333 error("failed to lookup root id: %s", strerror(-ret
));
337 printf("%llu\n", (unsigned long long)rootid
);
339 close_file_or_dir(fd
, dirstream
);
344 static const char* const cmd_inspect_min_dev_size_usage
[] = {
345 "btrfs inspect-internal min-dev-size [options] <path>",
346 "Get the minimum size the device can be shrunk to. The",
347 "device id 1 is used by default.",
348 "--id DEVID specify the device id to query",
352 struct dev_extent_elem
{
356 struct list_head list
;
359 static int add_dev_extent(struct list_head
*list
,
360 const u64 start
, const u64 end
,
363 struct dev_extent_elem
*e
;
365 e
= malloc(sizeof(*e
));
373 list_add_tail(&e
->list
, list
);
375 list_add(&e
->list
, list
);
380 static void free_dev_extent_list(struct list_head
*list
)
382 while (!list_empty(list
)) {
383 struct dev_extent_elem
*e
;
385 e
= list_first_entry(list
, struct dev_extent_elem
, list
);
391 static int hole_includes_sb_mirror(const u64 start
, const u64 end
)
396 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
397 u64 bytenr
= btrfs_sb_offset(i
);
399 if (bytenr
>= start
&& bytenr
<= end
) {
408 static void adjust_dev_min_size(struct list_head
*extents
,
409 struct list_head
*holes
,
413 * If relocation of the block group of a device extent must happen (see
414 * below) scratch space is used for the relocation. So track here the
415 * size of the largest device extent that has to be relocated. We track
416 * only the largest and not the sum of the sizes of all relocated block
417 * groups because after each block group is relocated the running
418 * transaction is committed so that pinned space is released.
420 u64 scratch_space
= 0;
423 * List of device extents is sorted by descending order of the extent's
424 * end offset. If some extent goes beyond the computed minimum size,
425 * which initially matches the sum of the lengths of all extents,
426 * we need to check if the extent can be relocated to an hole in the
427 * device between [0, *min_size[ (which is what the resize ioctl does).
429 while (!list_empty(extents
)) {
430 struct dev_extent_elem
*e
;
431 struct dev_extent_elem
*h
;
436 e
= list_first_entry(extents
, struct dev_extent_elem
, list
);
437 if (e
->end
<= *min_size
)
441 * Our extent goes beyond the computed *min_size. See if we can
442 * find a hole large enough to relocate it to. If not we must stop
443 * and set *min_size to the end of the extent.
445 extent_len
= e
->end
- e
->start
+ 1;
446 list_for_each_entry(h
, holes
, list
) {
447 hole_len
= h
->end
- h
->start
+ 1;
448 if (hole_len
>= extent_len
) {
455 *min_size
= e
->end
+ 1;
460 * If the hole found contains the location for a superblock
461 * mirror, we are pessimistic and require allocating one
462 * more extent of the same size. This is because the block
463 * group could be in the worst case used by a single extent
464 * with a size >= (block_group.length - superblock.size).
466 if (hole_includes_sb_mirror(h
->start
,
467 h
->start
+ extent_len
- 1))
468 *min_size
+= extent_len
;
470 if (hole_len
> extent_len
) {
471 h
->start
+= extent_len
;
480 if (extent_len
> scratch_space
)
481 scratch_space
= extent_len
;
485 *min_size
+= scratch_space
;
487 * Chunk allocation requires inserting/updating items in the
488 * chunk tree, so often this can lead to the need of allocating
489 * a new system chunk too, which has a maximum size of 32Mb.
495 static int print_min_dev_size(int fd
, u64 devid
)
499 * Device allocations starts at 1Mb or at the value passed through the
500 * mount option alloc_start if it's bigger than 1Mb. The alloc_start
501 * option is used for debugging and testing only, and recently the
502 * possibility of deprecating/removing it has been discussed, so we
505 u64 min_size
= SZ_1M
;
506 struct btrfs_ioctl_search_args args
;
507 struct btrfs_ioctl_search_key
*sk
= &args
.key
;
508 u64 last_pos
= (u64
)-1;
512 memset(&args
, 0, sizeof(args
));
513 sk
->tree_id
= BTRFS_DEV_TREE_OBJECTID
;
514 sk
->min_objectid
= devid
;
515 sk
->max_objectid
= devid
;
516 sk
->max_type
= BTRFS_DEV_EXTENT_KEY
;
517 sk
->min_type
= BTRFS_DEV_EXTENT_KEY
;
519 sk
->max_offset
= (u64
)-1;
521 sk
->max_transid
= (u64
)-1;
526 struct btrfs_ioctl_search_header
*sh
;
527 unsigned long off
= 0;
529 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
);
531 error("tree search ioctl: %m");
536 if (sk
->nr_items
== 0)
539 for (i
= 0; i
< sk
->nr_items
; i
++) {
540 struct btrfs_dev_extent
*extent
;
543 sh
= (struct btrfs_ioctl_search_header
*)(args
.buf
+
546 extent
= (struct btrfs_dev_extent
*)(args
.buf
+ off
);
547 off
+= btrfs_search_header_len(sh
);
549 sk
->min_objectid
= btrfs_search_header_objectid(sh
);
550 sk
->min_type
= btrfs_search_header_type(sh
);
551 sk
->min_offset
= btrfs_search_header_offset(sh
) + 1;
553 if (btrfs_search_header_objectid(sh
) != devid
||
554 btrfs_search_header_type(sh
) != BTRFS_DEV_EXTENT_KEY
)
557 len
= btrfs_stack_dev_extent_length(extent
);
559 ret
= add_dev_extent(&extents
,
560 btrfs_search_header_offset(sh
),
561 btrfs_search_header_offset(sh
) + len
- 1, 0);
563 if (!ret
&& last_pos
!= (u64
)-1 &&
564 last_pos
!= btrfs_search_header_offset(sh
))
565 ret
= add_dev_extent(&holes
, last_pos
,
566 btrfs_search_header_offset(sh
) - 1, 1);
568 error("add device extent: %s", strerror(-ret
));
573 last_pos
= btrfs_search_header_offset(sh
) + len
;
576 if (sk
->min_type
!= BTRFS_DEV_EXTENT_KEY
||
577 sk
->min_objectid
!= devid
)
581 adjust_dev_min_size(&extents
, &holes
, &min_size
);
582 printf("%llu bytes (%s)\n", min_size
, pretty_size(min_size
));
585 free_dev_extent_list(&extents
);
586 free_dev_extent_list(&holes
);
591 static int cmd_inspect_min_dev_size(int argc
, char **argv
)
595 DIR *dirstream
= NULL
;
601 enum { GETOPT_VAL_DEVID
= 256 };
602 static const struct option long_options
[] = {
603 { "id", required_argument
, NULL
, GETOPT_VAL_DEVID
},
607 c
= getopt_long(argc
, argv
, "", long_options
, NULL
);
612 case GETOPT_VAL_DEVID
:
613 devid
= arg_strtou64(optarg
);
616 usage(cmd_inspect_min_dev_size_usage
);
619 if (check_argc_exact(argc
- optind
, 1))
620 usage(cmd_inspect_min_dev_size_usage
);
622 fd
= btrfs_open_dir(argv
[optind
], &dirstream
, 1);
628 ret
= print_min_dev_size(fd
, devid
);
629 close_file_or_dir(fd
, dirstream
);
634 static const char inspect_cmd_group_info
[] =
635 "query various internal information";
637 const struct cmd_group inspect_cmd_group
= {
638 inspect_cmd_group_usage
, inspect_cmd_group_info
, {
639 { "inode-resolve", cmd_inspect_inode_resolve
,
640 cmd_inspect_inode_resolve_usage
, NULL
, 0 },
641 { "logical-resolve", cmd_inspect_logical_resolve
,
642 cmd_inspect_logical_resolve_usage
, NULL
, 0 },
643 { "subvolid-resolve", cmd_inspect_subvolid_resolve
,
644 cmd_inspect_subvolid_resolve_usage
, NULL
, 0 },
645 { "rootid", cmd_inspect_rootid
, cmd_inspect_rootid_usage
, NULL
,
647 { "min-dev-size", cmd_inspect_min_dev_size
,
648 cmd_inspect_min_dev_size_usage
, NULL
, 0 },
649 { "dump-tree", cmd_inspect_dump_tree
,
650 cmd_inspect_dump_tree_usage
, NULL
, 0 },
651 { "dump-super", cmd_inspect_dump_super
,
652 cmd_inspect_dump_super_usage
, NULL
, 0 },
653 { "tree-stats", cmd_inspect_tree_stats
,
654 cmd_inspect_tree_stats_usage
, NULL
, 0 },
659 int cmd_inspect(int argc
, char **argv
)
661 return handle_command_group(&inspect_cmd_group
, argc
, argv
);