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 <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
2013-09-17 14:54:01 +00:00
|
|
|
#include <getopt.h>
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
#include "kerncompat.h"
|
|
|
|
#include "ctree.h"
|
|
|
|
#include "ioctl.h"
|
|
|
|
#include "utils.h"
|
2015-08-24 08:45:03 +00:00
|
|
|
#include "volumes.h"
|
2015-04-17 16:29:38 +00:00
|
|
|
#include "cmds-fi-usage.h"
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
#include "commands.h"
|
2017-01-25 15:35:00 +00:00
|
|
|
#include "help.h"
|
2017-01-31 22:13:54 +00:00
|
|
|
#include "mkfs/common.h"
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2012-02-08 15:45:54 +00:00
|
|
|
static const char * const device_cmd_group_usage[] = {
|
|
|
|
"btrfs device <command> [<args>]",
|
|
|
|
NULL
|
|
|
|
};
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_device_add_usage[] = {
|
2013-09-17 14:54:01 +00:00
|
|
|
"btrfs device add [options] <device> [<device>...] <path>",
|
2012-02-03 19:00:17 +00:00
|
|
|
"Add a device to a filesystem",
|
2013-09-17 14:54:01 +00:00
|
|
|
"-K|--nodiscard do not perform whole device TRIM",
|
2013-09-27 17:30:05 +00:00
|
|
|
"-f|--force force overwrite existing filesystem on the disk",
|
2012-02-03 19:00:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static int cmd_device_add(int argc, char **argv)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
|
|
|
char *mntpnt;
|
2016-01-12 10:20:18 +00:00
|
|
|
int i, fdmnt, ret = 0;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2013-09-17 14:54:01 +00:00
|
|
|
int discard = 1;
|
2013-09-27 17:30:05 +00:00
|
|
|
int force = 0;
|
2015-10-10 14:30:57 +00:00
|
|
|
int last_dev;
|
2013-09-17 14:54:01 +00:00
|
|
|
|
|
|
|
while (1) {
|
2015-04-08 15:33:55 +00:00
|
|
|
int c;
|
2015-01-19 12:44:49 +00:00
|
|
|
static const struct option long_options[] = {
|
2013-09-17 14:54:01 +00:00
|
|
|
{ "nodiscard", optional_argument, NULL, 'K'},
|
2013-09-27 17:30:05 +00:00
|
|
|
{ "force", no_argument, NULL, 'f'},
|
2015-01-21 17:53:02 +00:00
|
|
|
{ NULL, 0, NULL, 0}
|
2013-09-17 14:54:01 +00:00
|
|
|
};
|
2015-04-08 15:33:55 +00:00
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "Kf", long_options, NULL);
|
2013-09-17 14:54:01 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'K':
|
|
|
|
discard = 0;
|
|
|
|
break;
|
2013-09-27 17:30:05 +00:00
|
|
|
case 'f':
|
|
|
|
force = 1;
|
|
|
|
break;
|
2013-09-17 14:54:01 +00:00
|
|
|
default:
|
2015-07-10 22:05:05 +00:00
|
|
|
usage(cmd_device_add_usage);
|
2013-09-17 14:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-10-10 14:30:57 +00:00
|
|
|
if (check_argc_min(argc - optind, 2))
|
2015-07-10 22:05:05 +00:00
|
|
|
usage(cmd_device_add_usage);
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-10-10 14:30:57 +00:00
|
|
|
last_dev = argc - 1;
|
|
|
|
mntpnt = argv[last_dev];
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-08-26 09:04:23 +00:00
|
|
|
fdmnt = btrfs_open_dir(mntpnt, &dirstream, 1);
|
|
|
|
if (fdmnt < 0)
|
2013-09-04 15:22:22 +00:00
|
|
|
return 1;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-10-10 14:30:57 +00:00
|
|
|
for (i = optind; i < last_dev; i++){
|
2012-02-03 19:00:17 +00:00
|
|
|
struct btrfs_ioctl_vol_args ioctl_args;
|
|
|
|
int devfd, res;
|
|
|
|
u64 dev_block_count = 0;
|
2014-06-04 20:43:11 +00:00
|
|
|
char *path;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-06-10 22:46:30 +00:00
|
|
|
res = test_dev_for_mkfs(argv[i], force);
|
2013-09-27 17:30:05 +00:00
|
|
|
if (res) {
|
2013-10-21 13:58:08 +00:00
|
|
|
ret++;
|
2012-02-03 19:00:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
devfd = open(argv[i], O_RDWR);
|
2013-01-22 21:48:04 +00:00
|
|
|
if (devfd < 0) {
|
2015-11-09 09:59:37 +00:00
|
|
|
error("unable to open device '%s'", argv[i]);
|
2012-02-03 19:00:17 +00:00
|
|
|
ret++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-27 23:47:40 +00:00
|
|
|
res = btrfs_prepare_device(devfd, argv[i], &dev_block_count, 0,
|
|
|
|
PREP_DEVICE_ZERO_END | PREP_DEVICE_VERBOSE |
|
|
|
|
(discard ? PREP_DEVICE_DISCARD : 0));
|
2013-12-18 04:07:55 +00:00
|
|
|
close(devfd);
|
2012-02-03 19:00:17 +00:00
|
|
|
if (res) {
|
|
|
|
ret++;
|
2013-12-18 04:07:55 +00:00
|
|
|
goto error_out;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2014-06-04 20:43:11 +00:00
|
|
|
path = canonicalize_path(argv[i]);
|
|
|
|
if (!path) {
|
2015-11-09 09:59:37 +00:00
|
|
|
error("could not canonicalize pathname '%s': %s",
|
2014-06-04 20:43:11 +00:00
|
|
|
argv[i], strerror(errno));
|
|
|
|
ret++;
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
|
2015-06-12 12:36:51 +00:00
|
|
|
memset(&ioctl_args, 0, sizeof(ioctl_args));
|
2014-06-04 20:43:11 +00:00
|
|
|
strncpy_null(ioctl_args.name, path);
|
2012-02-03 19:00:17 +00:00
|
|
|
res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
|
2014-06-04 20:43:11 +00:00
|
|
|
if (res < 0) {
|
2015-11-09 09:59:37 +00:00
|
|
|
error("error adding device '%s': %s",
|
2016-01-12 10:20:18 +00:00
|
|
|
path, strerror(errno));
|
2012-02-03 19:00:17 +00:00
|
|
|
ret++;
|
|
|
|
}
|
2014-06-04 20:43:11 +00:00
|
|
|
free(path);
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-18 04:07:55 +00:00
|
|
|
error_out:
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fdmnt, dirstream);
|
2013-09-04 15:22:22 +00:00
|
|
|
return !!ret;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static int _cmd_device_remove(int argc, char **argv,
|
|
|
|
const char * const *usagestr)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
|
|
|
char *mntpnt;
|
2016-01-12 10:20:18 +00:00
|
|
|
int i, fdmnt, ret = 0;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
clean_args_no_options(argc, argv, usagestr);
|
|
|
|
|
|
|
|
if (check_argc_min(argc - optind, 2))
|
2015-06-24 16:09:17 +00:00
|
|
|
usage(usagestr);
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
mntpnt = argv[argc - 1];
|
|
|
|
|
2015-08-26 09:04:23 +00:00
|
|
|
fdmnt = btrfs_open_dir(mntpnt, &dirstream, 1);
|
|
|
|
if (fdmnt < 0)
|
2013-09-04 15:22:22 +00:00
|
|
|
return 1;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
for(i = optind; i < argc - 1; i++) {
|
2012-02-03 19:00:17 +00:00
|
|
|
struct btrfs_ioctl_vol_args arg;
|
2016-03-14 08:31:49 +00:00
|
|
|
struct btrfs_ioctl_vol_args_v2 argv2 = {0};
|
|
|
|
int is_devid = 0;
|
2012-02-03 19:00:17 +00:00
|
|
|
int res;
|
|
|
|
|
2016-03-14 08:31:49 +00:00
|
|
|
if (string_is_numerical(argv[i])) {
|
|
|
|
argv2.devid = arg_strtou64(argv[i]);
|
|
|
|
argv2.flags = BTRFS_DEVICE_SPEC_BY_ID;
|
|
|
|
is_devid = 1;
|
|
|
|
} else if (is_block_device(argv[i]) == 1 ||
|
|
|
|
strcmp(argv[i], "missing") == 0) {
|
|
|
|
strncpy_null(argv2.name, argv[i]);
|
|
|
|
} else {
|
2015-11-09 09:59:37 +00:00
|
|
|
error("not a block device: %s", argv[i]);
|
2013-12-12 07:47:00 +00:00
|
|
|
ret++;
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-14 08:31:49 +00:00
|
|
|
|
2016-01-12 12:35:50 +00:00
|
|
|
/*
|
|
|
|
* Positive values are from BTRFS_ERROR_DEV_*,
|
|
|
|
* otherwise it's a generic error, one of errnos
|
|
|
|
*/
|
2016-03-14 08:31:49 +00:00
|
|
|
res = ioctl(fdmnt, BTRFS_IOC_RM_DEV_V2, &argv2);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If BTRFS_IOC_RM_DEV_V2 is not supported we get ENOTTY and if
|
|
|
|
* argv2.flags includes a flag which kernel doesn't understand then
|
|
|
|
* we shall get EOPNOTSUPP
|
|
|
|
*/
|
|
|
|
if (res < 0 && (errno == ENOTTY || errno == EOPNOTSUPP)) {
|
|
|
|
if (is_devid) {
|
|
|
|
error("device delete by id failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
ret++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
memset(&arg, 0, sizeof(arg));
|
|
|
|
strncpy_null(arg.name, argv[i]);
|
|
|
|
res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
|
|
|
|
}
|
|
|
|
|
2015-04-13 12:37:01 +00:00
|
|
|
if (res) {
|
|
|
|
const char *msg;
|
|
|
|
|
2015-05-11 13:55:52 +00:00
|
|
|
if (res > 0)
|
2015-04-13 12:37:01 +00:00
|
|
|
msg = btrfs_err_str(res);
|
|
|
|
else
|
2016-01-12 10:20:18 +00:00
|
|
|
msg = strerror(errno);
|
2016-03-14 08:31:49 +00:00
|
|
|
if (is_devid) {
|
|
|
|
error("error removing devid %llu: %s",
|
|
|
|
(unsigned long long)argv2.devid, msg);
|
|
|
|
} else {
|
|
|
|
error("error removing device '%s': %s",
|
|
|
|
argv[i], msg);
|
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fdmnt, dirstream);
|
2013-09-04 15:22:22 +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_device_remove_usage[] = {
|
2016-03-14 08:31:49 +00:00
|
|
|
"btrfs device remove <device>|<devid> [<device>|<devid>...] <path>",
|
2015-06-24 16:09:17 +00:00
|
|
|
"Remove a device from a filesystem",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static int cmd_device_remove(int argc, char **argv)
|
2015-06-24 16:09:17 +00:00
|
|
|
{
|
2015-07-10 22:05:05 +00:00
|
|
|
return _cmd_device_remove(argc, argv, cmd_device_remove_usage);
|
2015-06-24 16:09:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_device_delete_usage[] = {
|
2016-03-14 08:31:49 +00:00
|
|
|
"btrfs device delete <device>|<devid> [<device>|<devid>...] <path>",
|
2015-06-24 16:09:17 +00:00
|
|
|
"Remove a device from a filesystem",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static int cmd_device_delete(int argc, char **argv)
|
2015-06-24 16:09:17 +00:00
|
|
|
{
|
2015-07-10 22:05:05 +00:00
|
|
|
return _cmd_device_remove(argc, argv, cmd_device_delete_usage);
|
2015-06-24 16:09:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_device_scan_usage[] = {
|
2014-03-19 06:10:02 +00:00
|
|
|
"btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
|
2012-02-03 19:00:17 +00:00
|
|
|
"Scan devices for a btrfs filesystem",
|
2014-09-13 01:21:21 +00:00
|
|
|
" -d|--all-devices (deprecated)",
|
2012-02-03 19:00:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static int cmd_device_scan(int argc, char **argv)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2014-10-15 00:50:38 +00:00
|
|
|
int i;
|
2016-03-11 00:26:13 +00:00
|
|
|
int devstart;
|
2014-03-06 03:36:48 +00:00
|
|
|
int all = 0;
|
|
|
|
int ret = 0;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2014-03-06 03:36:48 +00:00
|
|
|
while (1) {
|
2015-04-08 15:33:55 +00:00
|
|
|
int c;
|
2015-01-19 12:44:49 +00:00
|
|
|
static const struct option long_options[] = {
|
2014-03-06 03:36:48 +00:00
|
|
|
{ "all-devices", no_argument, NULL, 'd'},
|
2015-01-21 17:53:02 +00:00
|
|
|
{ NULL, 0, NULL, 0}
|
2014-03-06 03:36:48 +00:00
|
|
|
};
|
2015-04-08 15:33:55 +00:00
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "d", long_options, NULL);
|
2014-03-06 03:36:48 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
|
|
|
all = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2015-07-10 22:05:05 +00:00
|
|
|
usage(cmd_device_scan_usage);
|
2014-03-06 03:36:48 +00:00
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
2016-03-11 00:26:13 +00:00
|
|
|
devstart = optind;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2016-03-01 15:29:16 +00:00
|
|
|
if (all && check_argc_max(argc - optind, 1))
|
2015-07-10 22:05:05 +00:00
|
|
|
usage(cmd_device_scan_usage);
|
2014-03-06 03:36:48 +00:00
|
|
|
|
2016-03-09 01:19:40 +00:00
|
|
|
if (all || argc - optind == 0) {
|
2012-02-03 19:00:17 +00:00
|
|
|
printf("Scanning for Btrfs filesystems\n");
|
2014-11-11 11:35:58 +00:00
|
|
|
ret = btrfs_scan_devices();
|
2015-11-09 09:59:37 +00:00
|
|
|
error_on(ret, "error %d while scanning", ret);
|
2014-10-31 04:11:20 +00:00
|
|
|
ret = btrfs_register_all_devices();
|
2016-03-11 00:04:35 +00:00
|
|
|
error_on(ret, "there are %d errors while registering devices", ret);
|
2014-03-06 03:36:48 +00:00
|
|
|
goto out;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for( i = devstart ; i < argc ; i++ ){
|
2014-06-04 20:43:11 +00:00
|
|
|
char *path;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-08-28 14:11:30 +00:00
|
|
|
if (is_block_device(argv[i]) != 1) {
|
2015-11-09 09:59:37 +00:00
|
|
|
error("not a block device: %s", argv[i]);
|
2014-03-06 03:36:48 +00:00
|
|
|
ret = 1;
|
2014-10-15 00:50:38 +00:00
|
|
|
goto out;
|
2013-12-12 07:47:00 +00:00
|
|
|
}
|
2014-06-04 20:43:11 +00:00
|
|
|
path = canonicalize_path(argv[i]);
|
|
|
|
if (!path) {
|
2015-11-09 09:59:37 +00:00
|
|
|
error("could not canonicalize path '%s': %s",
|
2014-06-04 20:43:11 +00:00
|
|
|
argv[i], strerror(errno));
|
|
|
|
ret = 1;
|
2014-10-15 00:50:38 +00:00
|
|
|
goto out;
|
2014-06-04 20:43:11 +00:00
|
|
|
}
|
|
|
|
printf("Scanning for Btrfs filesystems in '%s'\n", path);
|
2014-10-15 00:50:38 +00:00
|
|
|
if (btrfs_register_one_device(path) != 0) {
|
|
|
|
ret = 1;
|
2014-06-04 20:43:11 +00:00
|
|
|
free(path);
|
2014-10-15 00:50:38 +00:00
|
|
|
goto out;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
2014-06-04 20:43:11 +00:00
|
|
|
free(path);
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 03:36:48 +00:00
|
|
|
out:
|
|
|
|
return !!ret;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_device_ready_usage[] = {
|
2013-01-19 18:06:20 +00:00
|
|
|
"btrfs device ready <device>",
|
2014-09-02 11:57:20 +00:00
|
|
|
"Check device to see if it has all of its devices in cache for mounting",
|
2013-01-19 18:06:20 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static int cmd_device_ready(int argc, char **argv)
|
2013-01-19 18:06:20 +00:00
|
|
|
{
|
|
|
|
struct btrfs_ioctl_vol_args args;
|
|
|
|
int fd;
|
|
|
|
int ret;
|
2014-06-04 20:43:11 +00:00
|
|
|
char *path;
|
2013-01-19 18:06:20 +00:00
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
clean_args_no_options(argc, argv, cmd_device_ready_usage);
|
|
|
|
|
2016-03-14 00:27:22 +00:00
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2015-07-10 22:05:05 +00:00
|
|
|
usage(cmd_device_ready_usage);
|
2013-01-19 18:06:20 +00:00
|
|
|
|
|
|
|
fd = open("/dev/btrfs-control", O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("failed to open /dev/btrfs-control");
|
2013-09-04 15:22:22 +00:00
|
|
|
return 1;
|
2013-01-19 18:06:20 +00:00
|
|
|
}
|
2014-06-04 20:43:11 +00:00
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
path = canonicalize_path(argv[optind]);
|
2014-06-04 20:43:11 +00:00
|
|
|
if (!path) {
|
2015-11-09 09:59:37 +00:00
|
|
|
error("could not canonicalize pathname '%s': %s",
|
2016-03-01 15:02:08 +00:00
|
|
|
argv[optind], strerror(errno));
|
2014-06-04 20:43:11 +00:00
|
|
|
ret = 1;
|
|
|
|
goto out;
|
2013-12-12 07:47:00 +00:00
|
|
|
}
|
2013-01-19 18:06:20 +00:00
|
|
|
|
2015-08-28 14:11:30 +00:00
|
|
|
if (is_block_device(path) != 1) {
|
2015-11-09 09:59:37 +00:00
|
|
|
error("not a block device: %s", path);
|
2014-06-04 20:43:11 +00:00
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-06-12 12:36:51 +00:00
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
strncpy_null(args.name, path);
|
2013-01-19 18:06:20 +00:00
|
|
|
ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
|
|
|
|
if (ret < 0) {
|
2015-11-09 09:59:37 +00:00
|
|
|
error("unable to determine if device '%s' is ready for mount: %s",
|
|
|
|
path, strerror(errno));
|
2013-01-19 18:06:20 +00:00
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
2014-06-04 20:43:11 +00:00
|
|
|
out:
|
|
|
|
free(path);
|
2013-01-19 18:06:20 +00:00
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_device_stats_usage[] = {
|
2016-12-08 18:21:28 +00:00
|
|
|
"btrfs device stats [options] <path>|<device>",
|
|
|
|
"Show device IO error statistics",
|
|
|
|
"Show device IO error statistics for all devices of the given filesystem",
|
|
|
|
"identified by PATH or DEVICE. The filesystem must be mounted.",
|
2015-08-25 15:30:37 +00:00
|
|
|
"",
|
2016-12-12 17:02:19 +00:00
|
|
|
"-c|--check return non-zero if any stat counter is not zero",
|
2016-12-08 18:12:10 +00:00
|
|
|
"-z|--reset show current stats and reset values to zero",
|
2012-05-15 09:30:39 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static int cmd_device_stats(int argc, char **argv)
|
2012-05-15 09:30:39 +00:00
|
|
|
{
|
2013-08-14 23:16:34 +00:00
|
|
|
char *dev_path;
|
2012-05-15 09:30:39 +00:00
|
|
|
struct btrfs_ioctl_fs_info_args fi_args;
|
|
|
|
struct btrfs_ioctl_dev_info_args *di_args = NULL;
|
|
|
|
int ret;
|
|
|
|
int fdmnt;
|
|
|
|
int i;
|
|
|
|
int err = 0;
|
2016-12-12 17:02:19 +00:00
|
|
|
int check = 0;
|
2012-05-15 09:30:39 +00:00
|
|
|
__u64 flags = 0;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-05-15 09:30:39 +00:00
|
|
|
|
2016-12-08 18:12:10 +00:00
|
|
|
while (1) {
|
|
|
|
int c;
|
|
|
|
static const struct option long_options[] = {
|
2017-06-22 07:57:53 +00:00
|
|
|
{"check", no_argument, NULL, 'c'},
|
2016-12-08 18:12:10 +00:00
|
|
|
{"reset", no_argument, NULL, 'z'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2016-12-12 17:02:19 +00:00
|
|
|
c = getopt_long(argc, argv, "cz", long_options, NULL);
|
2016-12-08 18:12:10 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
|
2012-05-15 09:30:39 +00:00
|
|
|
switch (c) {
|
2016-12-12 17:02:19 +00:00
|
|
|
case 'c':
|
|
|
|
check = 1;
|
|
|
|
break;
|
2012-05-15 09:30:39 +00:00
|
|
|
case 'z':
|
|
|
|
flags = BTRFS_DEV_STATS_RESET;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
2015-07-10 22:05:05 +00:00
|
|
|
usage(cmd_device_stats_usage);
|
2012-05-15 09:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 15:29:16 +00:00
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2015-07-10 22:05:05 +00:00
|
|
|
usage(cmd_device_stats_usage);
|
2012-05-15 09:30:39 +00:00
|
|
|
|
2013-08-14 23:16:34 +00:00
|
|
|
dev_path = argv[optind];
|
2012-05-15 09:30:39 +00:00
|
|
|
|
2015-10-12 13:23:02 +00:00
|
|
|
fdmnt = open_path_or_dev_mnt(dev_path, &dirstream, 1);
|
|
|
|
if (fdmnt < 0)
|
2013-09-04 15:22:22 +00:00
|
|
|
return 1;
|
2012-05-15 09:30:39 +00:00
|
|
|
|
2013-08-14 23:16:34 +00:00
|
|
|
ret = get_fs_info(dev_path, &fi_args, &di_args);
|
2012-05-15 09:30:39 +00:00
|
|
|
if (ret) {
|
2016-12-08 17:33:30 +00:00
|
|
|
error("getting device info for %s failed: %s", dev_path,
|
2015-11-09 09:59:37 +00:00
|
|
|
strerror(-ret));
|
2012-05-15 09:30:39 +00:00
|
|
|
err = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!fi_args.num_devices) {
|
2015-11-09 09:59:37 +00:00
|
|
|
error("no devices found");
|
2012-05-15 09:30:39 +00:00
|
|
|
err = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < fi_args.num_devices; i++) {
|
|
|
|
struct btrfs_ioctl_get_dev_stats args = {0};
|
2016-12-08 17:37:10 +00:00
|
|
|
char path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
|
2012-05-15 09:30:39 +00:00
|
|
|
|
2016-12-08 17:37:10 +00:00
|
|
|
strncpy(path, (char *)di_args[i].path,
|
2012-05-15 09:30:39 +00:00
|
|
|
BTRFS_DEVICE_PATH_NAME_MAX);
|
2016-12-08 17:37:10 +00:00
|
|
|
path[BTRFS_DEVICE_PATH_NAME_MAX] = 0;
|
2012-05-15 09:30:39 +00:00
|
|
|
|
|
|
|
args.devid = di_args[i].devid;
|
|
|
|
args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
|
|
|
|
args.flags = flags;
|
|
|
|
|
|
|
|
if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
|
2016-12-08 17:33:30 +00:00
|
|
|
error("device stats ioctl failed on %s: %s",
|
2015-11-09 09:59:37 +00:00
|
|
|
path, strerror(errno));
|
2016-12-05 18:35:20 +00:00
|
|
|
err |= 1;
|
2012-05-15 09:30:39 +00:00
|
|
|
} else {
|
2014-11-13 01:36:20 +00:00
|
|
|
char *canonical_path;
|
2016-12-08 18:01:00 +00:00
|
|
|
int j;
|
|
|
|
static const struct {
|
|
|
|
const char name[32];
|
|
|
|
u64 num;
|
|
|
|
} dev_stats[] = {
|
|
|
|
{ "write_io_errs", BTRFS_DEV_STAT_WRITE_ERRS },
|
|
|
|
{ "read_io_errs", BTRFS_DEV_STAT_READ_ERRS },
|
|
|
|
{ "flush_io_errs", BTRFS_DEV_STAT_FLUSH_ERRS },
|
|
|
|
{ "corruption_errs",
|
|
|
|
BTRFS_DEV_STAT_CORRUPTION_ERRS },
|
|
|
|
{ "generation_errs",
|
|
|
|
BTRFS_DEV_STAT_GENERATION_ERRS },
|
|
|
|
};
|
2014-11-13 01:36:20 +00:00
|
|
|
|
2016-12-08 17:37:10 +00:00
|
|
|
canonical_path = canonicalize_path(path);
|
2014-11-13 01:36:20 +00:00
|
|
|
|
2016-04-05 15:27:43 +00:00
|
|
|
/* No path when device is missing. */
|
|
|
|
if (!canonical_path) {
|
|
|
|
canonical_path = malloc(32);
|
2016-05-02 13:03:24 +00:00
|
|
|
if (!canonical_path) {
|
|
|
|
error("not enough memory for path buffer");
|
|
|
|
goto out;
|
|
|
|
}
|
2016-04-05 15:27:43 +00:00
|
|
|
snprintf(canonical_path, 32,
|
|
|
|
"devid:%llu", args.devid);
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:01:00 +00:00
|
|
|
for (j = 0; j < ARRAY_SIZE(dev_stats); j++) {
|
|
|
|
/* We got fewer items than we know */
|
|
|
|
if (args.nr_items < dev_stats[j].num + 1)
|
|
|
|
continue;
|
|
|
|
printf("[%s].%-16s %llu\n", canonical_path,
|
|
|
|
dev_stats[j].name,
|
|
|
|
(unsigned long long)
|
|
|
|
args.values[dev_stats[j].num]);
|
2016-12-12 17:02:19 +00:00
|
|
|
if ((check == 1)
|
2016-12-08 18:01:00 +00:00
|
|
|
&& (args.values[dev_stats[j].num] > 0))
|
2016-12-05 18:35:20 +00:00
|
|
|
err |= 64;
|
|
|
|
}
|
2014-11-13 01:36:20 +00:00
|
|
|
|
|
|
|
free(canonical_path);
|
2012-05-15 09:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(di_args);
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fdmnt, dirstream);
|
2012-05-15 09:30:39 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-09-14 21:27:19 +00:00
|
|
|
static const char * const cmd_device_usage_usage[] = {
|
2014-12-17 17:16:18 +00:00
|
|
|
"btrfs device usage [options] <path> [<path>..]",
|
|
|
|
"Show detailed information about internal allocations in devices.",
|
2015-11-06 15:39:49 +00:00
|
|
|
HELPINFO_UNITS_SHORT_LONG,
|
2014-04-23 17:00:22 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2014-12-18 13:52:36 +00:00
|
|
|
static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
|
2014-04-23 17:00:22 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
2014-04-25 17:39:11 +00:00
|
|
|
struct chunk_info *chunkinfo = NULL;
|
|
|
|
struct device_info *devinfo = NULL;
|
|
|
|
int chunkcount = 0;
|
|
|
|
int devcount = 0;
|
2014-04-23 17:00:22 +00:00
|
|
|
|
2014-04-25 17:39:11 +00:00
|
|
|
ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
|
|
|
|
&devcount);
|
2014-04-28 16:55:05 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
2014-04-23 17:00:22 +00:00
|
|
|
|
2014-04-25 17:39:11 +00:00
|
|
|
for (i = 0; i < devcount; i++) {
|
|
|
|
printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
|
2017-02-09 16:42:02 +00:00
|
|
|
print_device_sizes(&devinfo[i], unit_mode);
|
2017-02-09 16:42:02 +00:00
|
|
|
print_device_chunks(&devinfo[i], chunkinfo, chunkcount,
|
2014-12-18 13:52:36 +00:00
|
|
|
unit_mode);
|
2014-04-23 17:00:22 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2014-04-28 16:55:05 +00:00
|
|
|
out:
|
2014-04-25 17:39:11 +00:00
|
|
|
free(devinfo);
|
|
|
|
free(chunkinfo);
|
2014-04-23 17:00:22 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-14 21:39:28 +00:00
|
|
|
static int cmd_device_usage(int argc, char **argv)
|
2014-04-23 17:00:22 +00:00
|
|
|
{
|
2015-08-31 11:03:43 +00:00
|
|
|
unsigned unit_mode;
|
2014-04-28 16:55:05 +00:00
|
|
|
int ret = 0;
|
2015-08-31 11:03:43 +00:00
|
|
|
int i;
|
2014-04-23 17:00:22 +00:00
|
|
|
|
2015-08-31 11:03:43 +00:00
|
|
|
unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
|
2014-04-23 17:00:22 +00:00
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
clean_args_no_options(argc, argv, cmd_device_usage_usage);
|
|
|
|
|
|
|
|
if (check_argc_min(argc - optind, 1))
|
2014-04-23 17:00:22 +00:00
|
|
|
usage(cmd_device_usage_usage);
|
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
for (i = optind; i < argc; i++) {
|
2014-04-28 16:55:05 +00:00
|
|
|
int fd;
|
2015-08-31 11:03:43 +00:00
|
|
|
DIR *dirstream = NULL;
|
|
|
|
|
2015-11-09 09:59:38 +00:00
|
|
|
if (i > 1)
|
2014-04-23 17:00:22 +00:00
|
|
|
printf("\n");
|
|
|
|
|
2015-08-26 09:04:23 +00:00
|
|
|
fd = btrfs_open_dir(argv[i], &dirstream, 1);
|
2014-04-23 17:00:22 +00:00
|
|
|
if (fd < 0) {
|
2014-04-28 16:55:05 +00:00
|
|
|
ret = 1;
|
2015-11-09 09:59:38 +00:00
|
|
|
break;
|
2014-04-23 17:00:22 +00:00
|
|
|
}
|
2014-04-25 17:39:11 +00:00
|
|
|
|
2014-12-17 17:16:18 +00:00
|
|
|
ret = _cmd_device_usage(fd, argv[i], unit_mode);
|
2014-04-23 17:00:22 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
|
|
|
|
2014-04-28 16:55:05 +00:00
|
|
|
if (ret)
|
2015-11-09 09:59:38 +00:00
|
|
|
break;
|
2014-04-23 17:00:22 +00:00
|
|
|
}
|
2015-11-09 09:59:38 +00:00
|
|
|
|
2014-04-28 16:55:05 +00:00
|
|
|
return !!ret;
|
2014-04-23 17:00:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 12:22:05 +00:00
|
|
|
static const char device_cmd_group_info[] =
|
|
|
|
"manage and query devices in the filesystem";
|
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
const struct cmd_group device_cmd_group = {
|
2015-06-09 12:22:05 +00:00
|
|
|
device_cmd_group_usage, device_cmd_group_info, {
|
2015-07-10 22:05:05 +00:00
|
|
|
{ "add", cmd_device_add, cmd_device_add_usage, NULL, 0 },
|
|
|
|
{ "delete", cmd_device_delete, cmd_device_delete_usage, NULL,
|
|
|
|
CMD_ALIAS },
|
|
|
|
{ "remove", cmd_device_remove, cmd_device_remove_usage, NULL, 0 },
|
|
|
|
{ "scan", cmd_device_scan, cmd_device_scan_usage, NULL, 0 },
|
|
|
|
{ "ready", cmd_device_ready, cmd_device_ready_usage, NULL, 0 },
|
|
|
|
{ "stats", cmd_device_stats, cmd_device_stats_usage, NULL, 0 },
|
2014-04-23 17:00:22 +00:00
|
|
|
{ "usage", cmd_device_usage,
|
|
|
|
cmd_device_usage_usage, NULL, 0 },
|
2013-08-14 23:16:45 +00:00
|
|
|
NULL_CMD_STRUCT
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int cmd_device(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return handle_command_group(&device_cmd_group, argc, argv);
|
|
|
|
}
|