2012-02-03 19:00:17 +00:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2012-11-15 22:24:03 +00:00
|
|
|
#include <stdint.h>
|
2012-02-03 19:00:17 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <errno.h>
|
2015-07-20 15:31:43 +00:00
|
|
|
#include <getopt.h>
|
2015-07-29 18:37:37 +00:00
|
|
|
#include <limits.h>
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
#include "kerncompat.h"
|
|
|
|
#include "ioctl.h"
|
2013-01-28 05:22:30 +00:00
|
|
|
#include "utils.h"
|
2013-04-23 10:18:55 +00:00
|
|
|
#include "ctree.h"
|
|
|
|
#include "send-utils.h"
|
2015-07-20 15:31:43 +00:00
|
|
|
#include "disk-io.h"
|
2012-02-03 19:00:17 +00:00
|
|
|
#include "commands.h"
|
2012-08-08 08:54:48 +00:00
|
|
|
#include "btrfs-list.h"
|
2016-02-22 14:49:49 +00:00
|
|
|
#include "cmds-inspect-dump-tree.h"
|
2016-02-24 10:52:33 +00:00
|
|
|
#include "cmds-inspect-dump-super.h"
|
2016-03-11 11:49:45 +00:00
|
|
|
#include "cmds-inspect-tree-stats.h"
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2012-02-08 15:45:54 +00:00
|
|
|
static const char * const inspect_cmd_group_usage[] = {
|
|
|
|
"btrfs inspect-internal <command> <args>",
|
|
|
|
NULL
|
|
|
|
};
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
struct btrfs_ioctl_ino_path_args ipa;
|
2015-11-06 16:47:17 +00:00
|
|
|
struct btrfs_data_container fspath[PATH_MAX];
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2013-01-25 00:18:51 +00:00
|
|
|
memset(fspath, 0, sizeof(*fspath));
|
2012-02-03 19:00:17 +00:00
|
|
|
ipa.inum = inum;
|
2015-11-06 16:47:17 +00:00
|
|
|
ipa.size = PATH_MAX;
|
2014-07-26 16:49:55 +00:00
|
|
|
ipa.fspath = ptr_to_u64(fspath);
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
|
2016-01-12 12:35:50 +00:00
|
|
|
if (ret < 0) {
|
2016-03-02 14:47:19 +00:00
|
|
|
error("ino paths ioctl: %s", strerror(errno));
|
2012-02-03 19:00:17 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
printf("ioctl ret=%d, bytes_left=%lu, bytes_missing=%lu, "
|
|
|
|
"cnt=%d, missed=%d\n", ret,
|
|
|
|
(unsigned long)fspath->bytes_left,
|
|
|
|
(unsigned long)fspath->bytes_missing,
|
|
|
|
fspath->elem_cnt, fspath->elem_missed);
|
|
|
|
|
|
|
|
for (i = 0; i < fspath->elem_cnt; ++i) {
|
2013-02-14 18:29:50 +00:00
|
|
|
u64 ptr;
|
|
|
|
char *str;
|
|
|
|
ptr = (u64)(unsigned long)fspath->val;
|
|
|
|
ptr += fspath->val[i];
|
|
|
|
str = (char *)(unsigned long)ptr;
|
2012-02-03 19:00:17 +00:00
|
|
|
if (prepend)
|
2013-02-14 18:29:50 +00:00
|
|
|
printf("%s/%s\n", prepend, str);
|
2012-02-03 19:00:17 +00:00
|
|
|
else
|
2013-02-14 18:29:50 +00:00
|
|
|
printf("%s\n", str);
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2013-09-04 15:22:24 +00:00
|
|
|
return !!ret;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_inspect_inode_resolve_usage[] = {
|
2012-02-03 19:00:17 +00:00
|
|
|
"btrfs inspect-internal inode-resolve [-v] <inode> <path>",
|
|
|
|
"Get file system paths for the given inode",
|
2013-07-23 02:43:21 +00:00
|
|
|
"",
|
|
|
|
"-v verbose mode",
|
2012-02-03 19:00:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static int cmd_inspect_inode_resolve(int argc, char **argv)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int verbose = 0;
|
2013-01-24 18:45:00 +00:00
|
|
|
int ret;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
optind = 1;
|
|
|
|
while (1) {
|
2012-02-03 19:00:17 +00:00
|
|
|
int c = getopt(argc, argv, "v");
|
2012-02-03 19:00:17 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2015-07-10 22:05:05 +00:00
|
|
|
usage(cmd_inspect_inode_resolve_usage);
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 2))
|
2015-07-10 22:05:05 +00:00
|
|
|
usage(cmd_inspect_inode_resolve_usage);
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-10-12 13:22:59 +00:00
|
|
|
fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
|
|
|
|
if (fd < 0)
|
2013-09-04 15:22:24 +00:00
|
|
|
return 1;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2014-02-20 01:30:51 +00:00
|
|
|
ret = __ino_to_path_fd(arg_strtou64(argv[optind]), fd, verbose,
|
2013-01-24 18:45:00 +00:00
|
|
|
argv[optind+1]);
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
2013-09-04 15:22:24 +00:00
|
|
|
return !!ret;
|
2013-01-24 18:45:00 +00:00
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_inspect_logical_resolve_usage[] = {
|
2012-08-31 05:40:44 +00:00
|
|
|
"btrfs inspect-internal logical-resolve [-Pv] [-s bufsize] <logical> <path>",
|
2012-02-03 19:00:17 +00:00
|
|
|
"Get file system paths for the given logical address",
|
2012-08-31 05:40:44 +00:00
|
|
|
"-P skip the path resolving and print the inodes instead",
|
|
|
|
"-v verbose mode",
|
|
|
|
"-s bufsize set inode container's size. This is used to increase inode",
|
|
|
|
" container's size in case it is not enough to read all the ",
|
|
|
|
" resolved results. The max value one can set is 64k",
|
2012-02-03 19:00:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static int cmd_inspect_logical_resolve(int argc, char **argv)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
int i;
|
|
|
|
int verbose = 0;
|
|
|
|
int getpath = 1;
|
|
|
|
int bytes_left;
|
|
|
|
struct btrfs_ioctl_logical_ino_args loi;
|
|
|
|
struct btrfs_data_container *inodes;
|
2012-08-31 05:40:44 +00:00
|
|
|
u64 size = 4096;
|
2012-02-03 19:00:17 +00:00
|
|
|
char full_path[4096];
|
|
|
|
char *path_ptr;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
optind = 1;
|
|
|
|
while (1) {
|
2012-08-31 05:40:44 +00:00
|
|
|
int c = getopt(argc, argv, "Pvs:");
|
2012-02-03 19:00:17 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'P':
|
|
|
|
getpath = 0;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
2012-08-31 05:40:44 +00:00
|
|
|
case 's':
|
2014-02-20 01:30:51 +00:00
|
|
|
size = arg_strtou64(optarg);
|
2012-08-31 05:40:44 +00:00
|
|
|
break;
|
2012-02-03 19:00:17 +00:00
|
|
|
default:
|
2015-07-10 22:05:05 +00:00
|
|
|
usage(cmd_inspect_logical_resolve_usage);
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 2))
|
2015-07-10 22:05:05 +00:00
|
|
|
usage(cmd_inspect_logical_resolve_usage);
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2012-10-05 00:35:31 +00:00
|
|
|
size = min(size, (u64)64 * 1024);
|
2012-08-31 05:40:44 +00:00
|
|
|
inodes = malloc(size);
|
2012-02-03 19:00:17 +00:00
|
|
|
if (!inodes)
|
|
|
|
return 1;
|
|
|
|
|
2013-01-25 00:18:51 +00:00
|
|
|
memset(inodes, 0, sizeof(*inodes));
|
2014-02-20 01:30:51 +00:00
|
|
|
loi.logical = arg_strtou64(argv[optind]);
|
2012-08-31 05:40:44 +00:00
|
|
|
loi.size = size;
|
2014-07-26 16:49:55 +00:00
|
|
|
loi.inodes = ptr_to_u64(inodes);
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-10-12 13:22:59 +00:00
|
|
|
fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
|
2012-02-03 19:00:17 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
ret = 12;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
|
2016-01-12 12:35:50 +00:00
|
|
|
if (ret < 0) {
|
2016-03-02 14:47:19 +00:00
|
|
|
error("logical ino ioctl: %s", strerror(errno));
|
2012-02-03 19:00:17 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose)
|
2012-08-31 05:40:44 +00:00
|
|
|
printf("ioctl ret=%d, total_size=%llu, bytes_left=%lu, "
|
|
|
|
"bytes_missing=%lu, cnt=%d, missed=%d\n",
|
|
|
|
ret, size,
|
2012-02-03 19:00:17 +00:00
|
|
|
(unsigned long)inodes->bytes_left,
|
|
|
|
(unsigned long)inodes->bytes_missing,
|
|
|
|
inodes->elem_cnt, inodes->elem_missed);
|
|
|
|
|
|
|
|
bytes_left = sizeof(full_path);
|
|
|
|
ret = snprintf(full_path, bytes_left, "%s/", argv[optind+1]);
|
|
|
|
path_ptr = full_path + ret;
|
|
|
|
bytes_left -= ret + 1;
|
|
|
|
BUG_ON(bytes_left < 0);
|
|
|
|
|
|
|
|
for (i = 0; i < inodes->elem_cnt; i += 3) {
|
|
|
|
u64 inum = inodes->val[i];
|
|
|
|
u64 offset = inodes->val[i+1];
|
|
|
|
u64 root = inodes->val[i+2];
|
|
|
|
int path_fd;
|
|
|
|
char *name;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirs = NULL;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
if (getpath) {
|
Btrfs-progs: restructure list_subvolumes
The current code of list_subvols() has very bad scalability, if we want to
add new filter conditions or new sort methods, we have to modify lots of code.
Beside that, the most code of list_snapshots() is similar to list_subvols(),
So I restructure list_subvols(), and split the subvolume filter function,
the subvolume sort function and the output function from list_subvols().
In order to implement it, we defined some importtant structures:
struct btrfs_list_filter {
btrfs_list_filter_func filter_func;
void *data;
};
struct btrfs_list_comparer {
btrfs_list_comp_func comp_func;
int is_descending;
};
struct {
char *name;
char *column_name;
int need_print;
} btrfs_list_columns[];
If we want to add a new filter condition, we can choose a suitable filter
function, or implement a new filter function[1], and add it into a set of
the filters, and then pass the filter set into list_subvols(). We also can
mix several filters (just add those filters into the set, and pass the set
into list_subvols()) if the users specify two or more filter conditions.
The subvolume sort function is similar to the subvolume filter function. The
differentiation is the order of comparers in the array which is passed into
list_subvols() show us the priority of the sort methods.
The output function is different with the above two functions, we define a
array to manage all the columns that can be outputed, and use a member variant
(->need_print) to control the output of the relative column. Some columns are
outputed by default. But we can change it according to the requirement of the
users.
After appling this patch, we needn't implement a independent list_snapshots()
function, just pass a filter function which is used to identify the snapshot
into list_subvols().
[1]: If we implement new filter functions or compare functions, we must add
them into the array all_filter_funcs or the array all_comp_funcs, and modify
the relative enum variants(btrfs_list_filter_enum, btrfs_list_comp_enum).
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
2012-09-18 06:51:49 +00:00
|
|
|
name = btrfs_list_path_for_root(fd, root);
|
2013-01-24 18:53:49 +00:00
|
|
|
if (IS_ERR(name)) {
|
|
|
|
ret = PTR_ERR(name);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
if (!name) {
|
|
|
|
path_ptr[-1] = '\0';
|
|
|
|
path_fd = fd;
|
|
|
|
} else {
|
|
|
|
path_ptr[-1] = '/';
|
|
|
|
ret = snprintf(path_ptr, bytes_left, "%s",
|
|
|
|
name);
|
|
|
|
BUG_ON(ret >= bytes_left);
|
|
|
|
free(name);
|
2015-10-12 13:22:59 +00:00
|
|
|
path_fd = btrfs_open_dir(full_path, &dirs, 1);
|
2012-02-03 19:00:17 +00:00
|
|
|
if (path_fd < 0) {
|
2015-10-12 13:22:58 +00:00
|
|
|
ret = -ENOENT;
|
2012-02-03 19:00:17 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__ino_to_path_fd(inum, path_fd, verbose, full_path);
|
2013-01-24 18:53:49 +00:00
|
|
|
if (path_fd != fd)
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(path_fd, dirs);
|
2012-02-03 19:00:17 +00:00
|
|
|
} else {
|
|
|
|
printf("inode %llu offset %llu root %llu\n", inum,
|
|
|
|
offset, root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
2012-02-03 19:00:17 +00:00
|
|
|
free(inodes);
|
2013-09-04 15:22:24 +00:00
|
|
|
return !!ret;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_inspect_subvolid_resolve_usage[] = {
|
2013-04-23 10:18:55 +00:00
|
|
|
"btrfs inspect-internal subvolid-resolve <subvolid> <path>",
|
|
|
|
"Get file system paths for the given subvolume ID.",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static int cmd_inspect_subvolid_resolve(int argc, char **argv)
|
2013-04-23 10:18:55 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int fd = -1;
|
|
|
|
u64 subvol_id;
|
2015-06-12 11:18:44 +00:00
|
|
|
char path[PATH_MAX];
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2013-04-23 10:18:55 +00:00
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
clean_args_no_options(argc, argv, cmd_inspect_subvolid_resolve_usage);
|
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 2))
|
2015-07-10 22:05:05 +00:00
|
|
|
usage(cmd_inspect_subvolid_resolve_usage);
|
2013-04-23 10:18:55 +00:00
|
|
|
|
2016-03-18 01:31:32 +00:00
|
|
|
fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
|
2013-04-23 10:18:55 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
subvol_id = arg_strtou64(argv[optind]);
|
2013-04-23 10:18:55 +00:00
|
|
|
ret = btrfs_subvolid_resolve(fd, path, sizeof(path), subvol_id);
|
|
|
|
|
|
|
|
if (ret) {
|
2016-03-02 14:47:19 +00:00
|
|
|
error("resolving subvolid %llu error %d",
|
2016-03-01 15:02:08 +00:00
|
|
|
(unsigned long long)subvol_id, ret);
|
2013-04-23 10:18:55 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:43:56 +00:00
|
|
|
path[PATH_MAX - 1] = '\0';
|
2013-04-23 10:18:55 +00:00
|
|
|
printf("%s\n", path);
|
|
|
|
|
|
|
|
out:
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
2016-03-11 11:49:45 +00:00
|
|
|
return !!ret;
|
2013-04-23 10:18:55 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char* const cmd_inspect_rootid_usage[] = {
|
2013-09-18 16:19:30 +00:00
|
|
|
"btrfs inspect-internal rootid <path>",
|
|
|
|
"Get tree ID of the containing subvolume of path.",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static int cmd_inspect_rootid(int argc, char **argv)
|
2013-09-18 16:19:30 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int fd = -1;
|
|
|
|
u64 rootid;
|
|
|
|
DIR *dirstream = NULL;
|
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
clean_args_no_options(argc, argv, cmd_inspect_rootid_usage);
|
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2015-07-10 22:05:05 +00:00
|
|
|
usage(cmd_inspect_rootid_usage);
|
2013-09-18 16:19:30 +00:00
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
fd = btrfs_open_dir(argv[optind], &dirstream, 1);
|
2013-09-18 16:19:30 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = lookup_ino_rootid(fd, &rootid);
|
|
|
|
if (ret) {
|
2016-03-02 14:47:19 +00:00
|
|
|
error("rootid failed with ret=%d", ret);
|
2013-09-18 16:19:30 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%llu\n", (unsigned long long)rootid);
|
|
|
|
out:
|
|
|
|
close_file_or_dir(fd, dirstream);
|
|
|
|
|
|
|
|
return !!ret;
|
|
|
|
}
|
|
|
|
|
2016-01-11 13:31:20 +00:00
|
|
|
static const char* const cmd_inspect_min_dev_size_usage[] = {
|
|
|
|
"btrfs inspect-internal min-dev-size [options] <path>",
|
|
|
|
"Get the minimum size the device can be shrunk to. The",
|
|
|
|
"device id 1 is used by default.",
|
|
|
|
"--id DEVID specify the device id to query",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2015-07-20 15:29:24 +00:00
|
|
|
struct dev_extent_elem {
|
|
|
|
u64 start;
|
|
|
|
/* inclusive end */
|
|
|
|
u64 end;
|
|
|
|
struct list_head list;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int add_dev_extent(struct list_head *list,
|
|
|
|
const u64 start, const u64 end,
|
|
|
|
const int append)
|
|
|
|
{
|
|
|
|
struct dev_extent_elem *e;
|
|
|
|
|
|
|
|
e = malloc(sizeof(*e));
|
|
|
|
if (!e)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
e->start = start;
|
|
|
|
e->end = end;
|
|
|
|
|
|
|
|
if (append)
|
|
|
|
list_add_tail(&e->list, list);
|
|
|
|
else
|
|
|
|
list_add(&e->list, list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_dev_extent_list(struct list_head *list)
|
|
|
|
{
|
|
|
|
while (!list_empty(list)) {
|
|
|
|
struct dev_extent_elem *e;
|
|
|
|
|
|
|
|
e = list_first_entry(list, struct dev_extent_elem, list);
|
|
|
|
list_del(&e->list);
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hole_includes_sb_mirror(const u64 start, const u64 end)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
|
|
|
|
u64 bytenr = btrfs_sb_offset(i);
|
|
|
|
|
|
|
|
if (bytenr >= start && bytenr <= end) {
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void adjust_dev_min_size(struct list_head *extents,
|
|
|
|
struct list_head *holes,
|
|
|
|
u64 *min_size)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If relocation of the block group of a device extent must happen (see
|
|
|
|
* below) scratch space is used for the relocation. So track here the
|
|
|
|
* size of the largest device extent that has to be relocated. We track
|
|
|
|
* only the largest and not the sum of the sizes of all relocated block
|
|
|
|
* groups because after each block group is relocated the running
|
|
|
|
* transaction is committed so that pinned space is released.
|
|
|
|
*/
|
|
|
|
u64 scratch_space = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* List of device extents is sorted by descending order of the extent's
|
|
|
|
* end offset. If some extent goes beyond the computed minimum size,
|
2016-05-11 23:50:36 +00:00
|
|
|
* which initially matches the sum of the lengths of all extents,
|
2015-07-20 15:29:24 +00:00
|
|
|
* we need to check if the extent can be relocated to an hole in the
|
|
|
|
* device between [0, *min_size[ (which is what the resize ioctl does).
|
|
|
|
*/
|
|
|
|
while (!list_empty(extents)) {
|
|
|
|
struct dev_extent_elem *e;
|
|
|
|
struct dev_extent_elem *h;
|
|
|
|
int found = 0;
|
|
|
|
u64 extent_len;
|
|
|
|
u64 hole_len = 0;
|
|
|
|
|
|
|
|
e = list_first_entry(extents, struct dev_extent_elem, list);
|
|
|
|
if (e->end <= *min_size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Our extent goes beyond the computed *min_size. See if we can
|
|
|
|
* find a hole large enough to relocate it to. If not we must stop
|
|
|
|
* and set *min_size to the end of the extent.
|
|
|
|
*/
|
|
|
|
extent_len = e->end - e->start + 1;
|
|
|
|
list_for_each_entry(h, holes, list) {
|
|
|
|
hole_len = h->end - h->start + 1;
|
|
|
|
if (hole_len >= extent_len) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
*min_size = e->end + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the hole found contains the location for a superblock
|
|
|
|
* mirror, we are pessimistic and require allocating one
|
|
|
|
* more extent of the same size. This is because the block
|
|
|
|
* group could be in the worst case used by a single extent
|
|
|
|
* with a size >= (block_group.length - superblock.size).
|
|
|
|
*/
|
|
|
|
if (hole_includes_sb_mirror(h->start,
|
|
|
|
h->start + extent_len - 1))
|
|
|
|
*min_size += extent_len;
|
|
|
|
|
|
|
|
if (hole_len > extent_len) {
|
|
|
|
h->start += extent_len;
|
|
|
|
} else {
|
|
|
|
list_del(&h->list);
|
|
|
|
free(h);
|
|
|
|
}
|
|
|
|
|
|
|
|
list_del(&e->list);
|
|
|
|
free(e);
|
|
|
|
|
|
|
|
if (extent_len > scratch_space)
|
|
|
|
scratch_space = extent_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scratch_space) {
|
|
|
|
*min_size += scratch_space;
|
|
|
|
/*
|
|
|
|
* Chunk allocation requires inserting/updating items in the
|
|
|
|
* chunk tree, so often this can lead to the need of allocating
|
|
|
|
* a new system chunk too, which has a maximum size of 32Mb.
|
|
|
|
*/
|
|
|
|
*min_size += 32 * 1024 * 1024;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 15:31:43 +00:00
|
|
|
static int print_min_dev_size(int fd, u64 devid)
|
2015-07-20 15:29:24 +00:00
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
/*
|
|
|
|
* Device allocations starts at 1Mb or at the value passed through the
|
|
|
|
* mount option alloc_start if it's bigger than 1Mb. The alloc_start
|
|
|
|
* option is used for debugging and testing only, and recently the
|
|
|
|
* possibility of deprecating/removing it has been discussed, so we
|
|
|
|
* ignore it here.
|
|
|
|
*/
|
|
|
|
u64 min_size = 1 * 1024 * 1024ull;
|
|
|
|
struct btrfs_ioctl_search_args args;
|
|
|
|
struct btrfs_ioctl_search_key *sk = &args.key;
|
|
|
|
u64 last_pos = (u64)-1;
|
|
|
|
LIST_HEAD(extents);
|
|
|
|
LIST_HEAD(holes);
|
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
sk->tree_id = BTRFS_DEV_TREE_OBJECTID;
|
|
|
|
sk->min_objectid = devid;
|
|
|
|
sk->max_objectid = devid;
|
|
|
|
sk->max_type = BTRFS_DEV_EXTENT_KEY;
|
|
|
|
sk->min_type = BTRFS_DEV_EXTENT_KEY;
|
|
|
|
sk->min_offset = 0;
|
|
|
|
sk->max_offset = (u64)-1;
|
|
|
|
sk->min_transid = 0;
|
|
|
|
sk->max_transid = (u64)-1;
|
|
|
|
sk->nr_items = 4096;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int i;
|
|
|
|
struct btrfs_ioctl_search_header *sh;
|
|
|
|
unsigned long off = 0;
|
|
|
|
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
|
|
|
|
if (ret < 0) {
|
2016-03-02 14:47:19 +00:00
|
|
|
error("tree search ioctl: %s", strerror(errno));
|
2015-07-20 15:29:24 +00:00
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sk->nr_items == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (i = 0; i < sk->nr_items; i++) {
|
|
|
|
struct btrfs_dev_extent *extent;
|
|
|
|
u64 len;
|
|
|
|
|
|
|
|
sh = (struct btrfs_ioctl_search_header *)(args.buf +
|
|
|
|
off);
|
|
|
|
off += sizeof(*sh);
|
|
|
|
extent = (struct btrfs_dev_extent *)(args.buf + off);
|
2016-05-03 14:20:52 +00:00
|
|
|
off += btrfs_search_header_len(sh);
|
2015-07-20 15:29:24 +00:00
|
|
|
|
2016-05-03 14:20:52 +00:00
|
|
|
sk->min_objectid = btrfs_search_header_objectid(sh);
|
|
|
|
sk->min_type = btrfs_search_header_type(sh);
|
|
|
|
sk->min_offset = btrfs_search_header_offset(sh) + 1;
|
2015-07-20 15:29:24 +00:00
|
|
|
|
2016-05-03 14:20:52 +00:00
|
|
|
if (btrfs_search_header_objectid(sh) != devid ||
|
|
|
|
btrfs_search_header_type(sh) != BTRFS_DEV_EXTENT_KEY)
|
2015-07-20 15:29:24 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
len = btrfs_stack_dev_extent_length(extent);
|
|
|
|
min_size += len;
|
2016-05-03 14:20:52 +00:00
|
|
|
ret = add_dev_extent(&extents,
|
|
|
|
btrfs_search_header_offset(sh),
|
|
|
|
btrfs_search_header_offset(sh) + len - 1, 0);
|
2015-07-20 15:29:24 +00:00
|
|
|
|
|
|
|
if (!ret && last_pos != (u64)-1 &&
|
2016-05-03 14:20:52 +00:00
|
|
|
last_pos != btrfs_search_header_offset(sh))
|
2015-07-20 15:29:24 +00:00
|
|
|
ret = add_dev_extent(&holes, last_pos,
|
2016-05-03 14:20:52 +00:00
|
|
|
btrfs_search_header_offset(sh) - 1, 1);
|
2015-07-20 15:29:24 +00:00
|
|
|
if (ret) {
|
2016-03-02 14:47:19 +00:00
|
|
|
error("add device extent: %s", strerror(-ret));
|
2015-07-20 15:29:24 +00:00
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-05-03 14:20:52 +00:00
|
|
|
last_pos = btrfs_search_header_offset(sh) + len;
|
2015-07-20 15:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sk->min_type != BTRFS_DEV_EXTENT_KEY ||
|
|
|
|
sk->min_objectid != devid)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
adjust_dev_min_size(&extents, &holes, &min_size);
|
|
|
|
printf("%llu bytes (%s)\n", min_size, pretty_size(min_size));
|
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
free_dev_extent_list(&extents);
|
|
|
|
free_dev_extent_list(&holes);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-20 15:31:43 +00:00
|
|
|
static int cmd_inspect_min_dev_size(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int fd = -1;
|
|
|
|
DIR *dirstream = NULL;
|
|
|
|
u64 devid = 1;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int c;
|
|
|
|
enum { GETOPT_VAL_DEVID = 256 };
|
|
|
|
static const struct option long_options[] = {
|
|
|
|
{ "id", required_argument, NULL, GETOPT_VAL_DEVID },
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "", long_options, NULL);
|
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case GETOPT_VAL_DEVID:
|
|
|
|
devid = arg_strtou64(optarg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(cmd_inspect_min_dev_size_usage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (check_argc_exact(argc - optind, 1))
|
|
|
|
usage(cmd_inspect_min_dev_size_usage);
|
|
|
|
|
2015-10-12 13:22:59 +00:00
|
|
|
fd = btrfs_open_dir(argv[optind], &dirstream, 1);
|
2015-07-20 15:31:43 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = print_min_dev_size(fd, devid);
|
|
|
|
close_file_or_dir(fd, dirstream);
|
2015-10-12 13:22:57 +00:00
|
|
|
out:
|
2015-07-20 15:31:43 +00:00
|
|
|
return !!ret;
|
|
|
|
}
|
|
|
|
|
2015-06-09 12:22:05 +00:00
|
|
|
static const char inspect_cmd_group_info[] =
|
|
|
|
"query various internal information";
|
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
const struct cmd_group inspect_cmd_group = {
|
2015-06-09 12:22:05 +00:00
|
|
|
inspect_cmd_group_usage, inspect_cmd_group_info, {
|
2015-07-10 22:05:05 +00:00
|
|
|
{ "inode-resolve", cmd_inspect_inode_resolve,
|
|
|
|
cmd_inspect_inode_resolve_usage, NULL, 0 },
|
|
|
|
{ "logical-resolve", cmd_inspect_logical_resolve,
|
|
|
|
cmd_inspect_logical_resolve_usage, NULL, 0 },
|
|
|
|
{ "subvolid-resolve", cmd_inspect_subvolid_resolve,
|
|
|
|
cmd_inspect_subvolid_resolve_usage, NULL, 0 },
|
|
|
|
{ "rootid", cmd_inspect_rootid, cmd_inspect_rootid_usage, NULL,
|
|
|
|
0 },
|
2015-07-20 15:31:43 +00:00
|
|
|
{ "min-dev-size", cmd_inspect_min_dev_size,
|
|
|
|
cmd_inspect_min_dev_size_usage, NULL, 0 },
|
2016-03-01 15:34:26 +00:00
|
|
|
{ "dump-tree", cmd_inspect_dump_tree,
|
2016-02-22 14:49:49 +00:00
|
|
|
cmd_inspect_dump_tree_usage, NULL, 0 },
|
2016-03-01 15:34:26 +00:00
|
|
|
{ "dump-super", cmd_inspect_dump_super,
|
2016-02-24 10:52:33 +00:00
|
|
|
cmd_inspect_dump_super_usage, NULL, 0 },
|
2016-03-11 11:49:45 +00:00
|
|
|
{ "tree-stats", cmd_inspect_tree_stats,
|
|
|
|
cmd_inspect_tree_stats_usage, NULL, 0 },
|
2013-08-14 23:16:45 +00:00
|
|
|
NULL_CMD_STRUCT
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int cmd_inspect(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return handle_command_group(&inspect_cmd_group, argc, argv);
|
|
|
|
}
|