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.
|
|
|
|
*/
|
|
|
|
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "kerncompat.h"
|
|
|
|
#include <sys/ioctl.h>
|
2012-02-03 19:00:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2013-09-17 14:54:01 +00:00
|
|
|
#include <getopt.h>
|
2022-09-15 11:59:39 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdbool.h>
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/ctree.h"
|
2021-04-26 06:27:41 +00:00
|
|
|
#include "kernel-shared/zoned.h"
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "common/string-table.h"
|
|
|
|
#include "common/utils.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/help.h"
|
2019-07-01 18:54:39 +00:00
|
|
|
#include "common/path-utils.h"
|
2019-07-01 22:42:23 +00:00
|
|
|
#include "common/device-utils.h"
|
2019-07-01 22:42:23 +00:00
|
|
|
#include "common/device-scan.h"
|
2020-12-11 16:48:12 +00:00
|
|
|
#include "common/format-output.h"
|
2021-04-29 17:52:15 +00:00
|
|
|
#include "common/open-utils.h"
|
2021-04-29 21:43:24 +00:00
|
|
|
#include "common/units.h"
|
2022-09-15 21:15:17 +00:00
|
|
|
#include "common/string-utils.h"
|
2022-09-15 11:59:39 +00:00
|
|
|
#include "common/messages.h"
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "cmds/commands.h"
|
|
|
|
#include "cmds/filesystem-usage.h"
|
2017-01-31 22:13:54 +00:00
|
|
|
#include "mkfs/common.h"
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "ioctl.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>",
|
2019-03-01 17:07:52 +00:00
|
|
|
"Add one or more devices to a mounted filesystem.",
|
|
|
|
"",
|
|
|
|
"-K|--nodiscard do not perform whole device TRIM on devices that report such capability",
|
2013-09-27 17:30:05 +00:00
|
|
|
"-f|--force force overwrite existing filesystem on the disk",
|
2020-11-05 02:38:00 +00:00
|
|
|
"--enqueue wait if there's another exclusive operation running,",
|
|
|
|
" otherwise continue",
|
2012-02-03 19:00:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_device_add(const struct cmd_struct *cmd,
|
|
|
|
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;
|
2022-10-05 18:02:13 +00:00
|
|
|
bool discard = true;
|
|
|
|
bool force = false;
|
2015-10-10 14:30:57 +00:00
|
|
|
int last_dev;
|
2020-11-05 02:38:00 +00:00
|
|
|
bool enqueue = false;
|
2021-04-26 06:27:41 +00:00
|
|
|
int zoned;
|
|
|
|
struct btrfs_ioctl_feature_flags feature_flags;
|
2013-09-17 14:54:01 +00:00
|
|
|
|
2018-06-20 00:38:38 +00:00
|
|
|
optind = 0;
|
2013-09-17 14:54:01 +00:00
|
|
|
while (1) {
|
2015-04-08 15:33:55 +00:00
|
|
|
int c;
|
2022-06-21 00:20:21 +00:00
|
|
|
enum { GETOPT_VAL_ENQUEUE = GETOPT_VAL_FIRST };
|
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'},
|
2020-11-05 02:38:00 +00:00
|
|
|
{ "enqueue", no_argument, NULL, GETOPT_VAL_ENQUEUE},
|
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':
|
2022-10-05 18:02:13 +00:00
|
|
|
discard = false;
|
2013-09-17 14:54:01 +00:00
|
|
|
break;
|
2013-09-27 17:30:05 +00:00
|
|
|
case 'f':
|
2022-10-05 18:02:13 +00:00
|
|
|
force = true;
|
2013-09-27 17:30:05 +00:00
|
|
|
break;
|
2020-11-05 02:38:00 +00:00
|
|
|
case GETOPT_VAL_ENQUEUE:
|
|
|
|
enqueue = true;
|
|
|
|
break;
|
2013-09-17 14:54:01 +00:00
|
|
|
default:
|
2018-03-07 02:54:38 +00:00
|
|
|
usage_unknown_option(cmd, argv);
|
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))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
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
|
|
|
|
2020-11-05 02:38:00 +00:00
|
|
|
ret = check_running_fs_exclop(fdmnt, BTRFS_EXCLOP_DEV_ADD, enqueue);
|
|
|
|
if (ret != 0) {
|
|
|
|
if (ret < 0)
|
|
|
|
error("unable to check status of exclusive operation: %m");
|
2020-08-25 15:03:37 +00:00
|
|
|
close_file_or_dir(fdmnt, dirstream);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-04-26 06:27:41 +00:00
|
|
|
ret = ioctl(fdmnt, BTRFS_IOC_GET_FEATURES, &feature_flags);
|
|
|
|
if (ret) {
|
|
|
|
error("error getting feature flags '%s': %m", mntpnt);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
zoned = (feature_flags.incompat_flags & BTRFS_FEATURE_INCOMPAT_ZONED);
|
|
|
|
|
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
|
|
|
|
2021-04-26 06:27:41 +00:00
|
|
|
if (!zoned && zoned_model(argv[i]) == ZONED_HOST_MANAGED) {
|
|
|
|
error(
|
|
|
|
"zoned: cannot add host-managed zoned device to non-zoned filesystem '%s'",
|
|
|
|
argv[i]);
|
|
|
|
ret++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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 |
|
2021-04-26 06:27:41 +00:00
|
|
|
(discard ? PREP_DEVICE_DISCARD : 0) |
|
|
|
|
(zoned ? PREP_DEVICE_ZONED : 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
|
|
|
}
|
|
|
|
|
2021-01-21 15:25:51 +00:00
|
|
|
path = path_canonicalize(argv[i]);
|
2014-06-04 20:43:11 +00:00
|
|
|
if (!path) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("could not canonicalize pathname '%s': %m",
|
|
|
|
argv[i]);
|
2014-06-04 20:43:11 +00:00
|
|
|
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) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("error adding device '%s': %m", path);
|
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:
|
2020-05-04 12:34:59 +00:00
|
|
|
btrfs_warn_multiple_profiles(fdmnt);
|
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
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(device_add, "add");
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int _cmd_device_remove(const struct cmd_struct *cmd,
|
|
|
|
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;
|
2020-11-05 02:38:00 +00:00
|
|
|
bool enqueue = false;
|
2021-05-20 20:14:18 +00:00
|
|
|
bool cancel = false;
|
2020-11-05 02:38:00 +00:00
|
|
|
|
|
|
|
optind = 0;
|
|
|
|
while (1) {
|
|
|
|
int c;
|
2022-06-21 00:20:21 +00:00
|
|
|
enum { GETOPT_VAL_ENQUEUE = GETOPT_VAL_FIRST };
|
2020-11-05 02:38:00 +00:00
|
|
|
static const struct option long_options[] = {
|
|
|
|
{ "enqueue", no_argument, NULL, GETOPT_VAL_ENQUEUE},
|
|
|
|
{ NULL, 0, NULL, 0}
|
|
|
|
};
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2020-11-05 02:38:00 +00:00
|
|
|
c = getopt_long(argc, argv, "", long_options, NULL);
|
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case GETOPT_VAL_ENQUEUE:
|
|
|
|
enqueue = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage_unknown_option(cmd, argv);
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 15:02:08 +00:00
|
|
|
|
|
|
|
if (check_argc_min(argc - optind, 2))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
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
|
|
|
|
2021-05-20 20:14:18 +00:00
|
|
|
/* Scan device arguments for 'cancel', that must be the only "device" */
|
|
|
|
for (i = optind; i < argc - 1; i++) {
|
|
|
|
if (cancel) {
|
|
|
|
error("cancel requested but another device specified: %s\n",
|
|
|
|
argv[i]);
|
|
|
|
close_file_or_dir(fdmnt, dirstream);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (strcmp("cancel", argv[i]) == 0) {
|
|
|
|
cancel = true;
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "Request to cancel running device deletion\n");
|
2021-05-20 20:14:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cancel) {
|
|
|
|
ret = check_running_fs_exclop(fdmnt, BTRFS_EXCLOP_DEV_REMOVE,
|
|
|
|
enqueue);
|
|
|
|
if (ret != 0) {
|
|
|
|
if (ret < 0)
|
|
|
|
error(
|
|
|
|
"unable to check status of exclusive operation: %m");
|
|
|
|
close_file_or_dir(fdmnt, dirstream);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-08-25 15:03:37 +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};
|
2022-10-05 18:02:13 +00:00
|
|
|
bool is_devid = false;
|
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;
|
2022-10-05 18:02:13 +00:00
|
|
|
is_devid = true;
|
2021-05-20 20:14:18 +00:00
|
|
|
} else if (strcmp(argv[i], "missing") == 0 ||
|
|
|
|
cancel ||
|
|
|
|
path_is_block_device(argv[i]) == 1) {
|
2016-03-14 08:31:49 +00:00
|
|
|
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) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("device delete by id failed: %m");
|
2016-03-14 08:31:49 +00:00
|
|
|
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",
|
2022-09-27 23:13:00 +00:00
|
|
|
argv2.devid, msg);
|
2016-03-14 08:31:49 +00:00
|
|
|
} else {
|
|
|
|
error("error removing device '%s': %s",
|
|
|
|
argv[i], msg);
|
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 12:34:59 +00:00
|
|
|
btrfs_warn_multiple_profiles(fdmnt);
|
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
|
|
|
}
|
|
|
|
|
2017-10-20 01:43:05 +00:00
|
|
|
#define COMMON_USAGE_REMOVE_DELETE \
|
2019-03-01 17:07:52 +00:00
|
|
|
"Remove a device from a filesystem, specified by a path to the device or", \
|
|
|
|
"as a device id in the filesystem. The btrfs signature is removed from", \
|
|
|
|
"the device.", \
|
2017-10-20 01:43:05 +00:00
|
|
|
"If 'missing' is specified for <device>, the first device that is", \
|
|
|
|
"described by the filesystem metadata, but not present at the mount", \
|
2021-05-20 20:14:18 +00:00
|
|
|
"time will be removed. (only in degraded mode)", \
|
2022-01-13 16:24:52 +00:00
|
|
|
"If 'cancel' is specified as the only device to delete, request cancellation", \
|
2021-05-20 20:14:18 +00:00
|
|
|
"of a previously started device deletion and wait until kernel finishes", \
|
|
|
|
"any pending work. This will not delete the device and the size will be", \
|
|
|
|
"restored to previous state. When deletion is not running, this will fail."
|
2017-10-20 01:43:05 +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",
|
2017-10-20 01:43:05 +00:00
|
|
|
COMMON_USAGE_REMOVE_DELETE,
|
2019-06-21 13:36:04 +00:00
|
|
|
"",
|
2020-11-05 02:38:00 +00:00
|
|
|
"--enqueue wait if there's another exclusive operation running,",
|
|
|
|
" otherwise continue",
|
2015-06-24 16:09:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_device_remove(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2015-06-24 16:09:17 +00:00
|
|
|
{
|
2018-03-07 02:24:46 +00:00
|
|
|
return _cmd_device_remove(cmd, argc, argv);
|
2015-06-24 16:09:17 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(device_remove, "remove");
|
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>",
|
2017-10-20 01:42:13 +00:00
|
|
|
"Remove a device from a filesystem (alias of \"btrfs device remove\")",
|
2017-10-20 01:43:05 +00:00
|
|
|
COMMON_USAGE_REMOVE_DELETE,
|
2019-06-21 13:36:04 +00:00
|
|
|
"",
|
2020-11-05 02:38:00 +00:00
|
|
|
"--enqueue wait if there's another exclusive operation running,",
|
|
|
|
" otherwise continue",
|
2015-06-24 16:09:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_device_delete(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2015-06-24 16:09:17 +00:00
|
|
|
{
|
2018-03-07 02:24:46 +00:00
|
|
|
return _cmd_device_remove(cmd, argc, argv);
|
2015-06-24 16:09:17 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_COMMAND(device_delete, "delete", cmd_device_delete,
|
|
|
|
cmd_device_delete_usage, NULL, CMD_ALIAS);
|
2015-06-24 16:09:17 +00:00
|
|
|
|
2019-03-01 13:01:25 +00:00
|
|
|
static int btrfs_forget_devices(const char *path)
|
2019-01-04 05:31:52 +00:00
|
|
|
{
|
|
|
|
struct btrfs_ioctl_vol_args args;
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open("/dev/btrfs-control", O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
if (path)
|
|
|
|
strncpy_null(args.name, path);
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_FORGET_DEV, &args);
|
|
|
|
if (ret)
|
|
|
|
ret = -errno;
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_device_scan_usage[] = {
|
2019-03-01 13:02:10 +00:00
|
|
|
"btrfs device scan [-d|--all-devices] <device> [<device>...]\n"
|
|
|
|
"btrfs device scan -u|--forget [<device>...]",
|
2019-03-01 13:22:44 +00:00
|
|
|
"Scan or forget (unregister) devices of btrfs filesystems",
|
|
|
|
"Scan or forget (unregister) devices of btrfs filesystems. Multi-device",
|
|
|
|
"filesystems need to scan devices before mount. The blkid provides list",
|
|
|
|
"of devices in case no path is given. If blkid is no available, there's",
|
|
|
|
"a fallback to manual enumeration of device nodes.",
|
|
|
|
"",
|
|
|
|
"The reverse is done by the forget option, such devices must be unmounted.",
|
|
|
|
"No argument will unregister all devices that are not part of a mounted filesystem.",
|
|
|
|
"",
|
|
|
|
" -d|--all-devices enumerate and register all devices, use as a fallback",
|
|
|
|
" if blkid is not available",
|
|
|
|
" -u|--forget [<device>...] unregister a given device or all stale devices if no path ",
|
2019-11-25 10:39:16 +00:00
|
|
|
HELPINFO_INSERT_GLOBALS,
|
|
|
|
HELPINFO_INSERT_VERBOSE,
|
2019-11-25 10:39:17 +00:00
|
|
|
HELPINFO_INSERT_QUIET,
|
2012-02-03 19:00:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_device_scan(const struct cmd_struct *cmd, 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;
|
2022-10-05 18:02:13 +00:00
|
|
|
bool all = false;
|
|
|
|
bool forget = 0;
|
2014-03-06 03:36:48 +00:00
|
|
|
int ret = 0;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2018-06-20 00:38:38 +00:00
|
|
|
optind = 0;
|
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'},
|
2019-01-04 05:31:52 +00:00
|
|
|
{ "forget", no_argument, NULL, 'u'},
|
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
|
|
|
|
2019-01-04 05:31:52 +00:00
|
|
|
c = getopt_long(argc, argv, "du", long_options, NULL);
|
2014-03-06 03:36:48 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
2022-10-05 18:02:13 +00:00
|
|
|
all = true;
|
2014-03-06 03:36:48 +00:00
|
|
|
break;
|
2019-01-04 05:31:52 +00:00
|
|
|
case 'u':
|
2022-10-05 18:02:13 +00:00
|
|
|
forget = true;
|
2019-01-04 05:31:52 +00:00
|
|
|
break;
|
2014-03-06 03:36:48 +00:00
|
|
|
default:
|
2018-03-07 02:54:38 +00:00
|
|
|
usage_unknown_option(cmd, argv);
|
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
|
|
|
|
2019-01-04 05:31:52 +00:00
|
|
|
if (all && forget)
|
2018-03-07 02:54:38 +00:00
|
|
|
usage(cmd);
|
2019-01-04 05:31:52 +00:00
|
|
|
|
2016-03-01 15:29:16 +00:00
|
|
|
if (all && check_argc_max(argc - optind, 1))
|
2018-03-07 02:54:38 +00:00
|
|
|
usage(cmd);
|
2014-03-06 03:36:48 +00:00
|
|
|
|
2016-03-09 01:19:40 +00:00
|
|
|
if (all || argc - optind == 0) {
|
2019-01-04 05:31:52 +00:00
|
|
|
if (forget) {
|
|
|
|
ret = btrfs_forget_devices(NULL);
|
2019-03-01 16:14:48 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = -ret;
|
|
|
|
error("cannot unregister devices: %m");
|
|
|
|
}
|
2019-01-04 05:31:52 +00:00
|
|
|
} else {
|
2022-10-04 14:28:33 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "Scanning for Btrfs filesystems\n");
|
2019-11-25 10:39:16 +00:00
|
|
|
ret = btrfs_scan_devices(1);
|
2019-01-04 05:31:52 +00:00
|
|
|
error_on(ret, "error %d while scanning", ret);
|
|
|
|
ret = btrfs_register_all_devices();
|
|
|
|
error_on(ret,
|
2019-03-01 16:14:48 +00:00
|
|
|
"there were %d errors while registering devices",
|
2019-01-04 05:31:52 +00:00
|
|
|
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
|
|
|
|
2019-07-01 19:29:43 +00:00
|
|
|
if (path_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
|
|
|
}
|
2021-01-21 15:25:51 +00:00
|
|
|
path = path_canonicalize(argv[i]);
|
2014-06-04 20:43:11 +00:00
|
|
|
if (!path) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("could not canonicalize path '%s': %m", argv[i]);
|
2014-06-04 20:43:11 +00:00
|
|
|
ret = 1;
|
2014-10-15 00:50:38 +00:00
|
|
|
goto out;
|
2014-06-04 20:43:11 +00:00
|
|
|
}
|
2019-01-04 05:31:52 +00:00
|
|
|
if (forget) {
|
|
|
|
ret = btrfs_forget_devices(path);
|
2019-03-01 16:14:48 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
errno = -ret;
|
|
|
|
error("cannot unregister device '%s': %m", path);
|
|
|
|
}
|
2019-01-04 05:31:52 +00:00
|
|
|
} else {
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "Scanning for btrfs filesystems on '%s'\n", path);
|
2019-01-04 05:31:52 +00:00
|
|
|
if (btrfs_register_one_device(path) != 0) {
|
|
|
|
ret = 1;
|
|
|
|
free(path);
|
|
|
|
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
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(device_scan, "scan");
|
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>",
|
2019-03-01 17:07:52 +00:00
|
|
|
"Check and wait until a group of devices of a filesystem is ready for mount",
|
2013-01-19 18:06:20 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_device_ready(const struct cmd_struct *cmd, 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
|
|
|
|
2018-03-07 02:42:01 +00:00
|
|
|
clean_args_no_options(cmd, argc, argv);
|
2016-03-01 15:02:08 +00:00
|
|
|
|
2016-03-14 00:27:22 +00:00
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
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
|
|
|
|
2021-01-21 15:25:51 +00:00
|
|
|
path = path_canonicalize(argv[optind]);
|
2014-06-04 20:43:11 +00:00
|
|
|
if (!path) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("could not canonicalize pathname '%s': %m",
|
|
|
|
argv[optind]);
|
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
|
|
|
|
2019-07-01 19:29:43 +00:00
|
|
|
if (path_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) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("unable to determine if device '%s' is ready for mount: %m",
|
|
|
|
path);
|
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;
|
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(device_ready, "ready");
|
2013-01-19 18:06:20 +00:00
|
|
|
|
2020-12-11 16:48:12 +00:00
|
|
|
static const struct rowspec device_stats_rowspec[] = {
|
2022-12-15 23:43:25 +00:00
|
|
|
{ .key = "device", .fmt = "str", .out_text = "device", .out_json = "device" },
|
2022-12-15 23:49:07 +00:00
|
|
|
{ .key = "devid", .fmt = "%llu", .out_text = "devid", .out_json = "devid" },
|
2020-12-11 16:48:12 +00:00
|
|
|
{ .key = "write_io_errs", .fmt = "%llu", .out_text = "write_io_errs", .out_json = "write_io_errs" },
|
|
|
|
{ .key = "read_io_errs", .fmt = "%llu", .out_text = "read_io_errs", .out_json = "read_io_errs" },
|
|
|
|
{ .key = "flush_io_errs", .fmt = "%llu", .out_text = "flush_io_errs", .out_json = "flush_io_errs" },
|
|
|
|
{ .key = "corruption_errs", .fmt = "%llu", .out_text = "corruption_errs", .out_json = "corruption_errs" },
|
|
|
|
{ .key = "generation_errs", .fmt = "%llu", .out_text = "generation_errs", .out_json = "generation_errs" },
|
|
|
|
ROWSPEC_END
|
|
|
|
};
|
|
|
|
|
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",
|
2022-07-18 11:34:39 +00:00
|
|
|
"-T show current stats in tabular format",
|
2020-12-11 16:48:12 +00:00
|
|
|
HELPINFO_INSERT_GLOBALS,
|
|
|
|
HELPINFO_INSERT_FORMAT,
|
2012-05-15 09:30:39 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2022-07-18 11:34:38 +00:00
|
|
|
static int print_device_stat_string(struct format_ctx *fctx,
|
|
|
|
struct btrfs_ioctl_get_dev_stats *args, char *path, bool check)
|
|
|
|
{
|
|
|
|
char *canonical_path = path_canonicalize(path);
|
|
|
|
char devid_str[32];
|
|
|
|
int j;
|
|
|
|
int err = 0;
|
|
|
|
static const struct {
|
|
|
|
const char name[32];
|
|
|
|
enum btrfs_dev_stat_values stat_idx;
|
|
|
|
} 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 },
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* The plain text and json formats cannot be mapped directly in all
|
|
|
|
* cases and we have to switch.
|
|
|
|
*/
|
|
|
|
const bool json = (bconf.output_format == CMD_FORMAT_JSON);
|
|
|
|
|
|
|
|
/* No path when device is missing. */
|
|
|
|
if (!canonical_path) {
|
|
|
|
canonical_path = malloc(32);
|
|
|
|
|
|
|
|
if (!canonical_path) {
|
2022-09-30 07:12:06 +00:00
|
|
|
error_msg(ERROR_MSG_MEMORY, "device path buffer");
|
2022-07-18 11:34:38 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(canonical_path, 32, "devid:%llu", args->devid);
|
|
|
|
}
|
|
|
|
snprintf(devid_str, 32, "%llu", args->devid);
|
|
|
|
fmt_print_start_group(fctx, NULL, JSON_TYPE_MAP);
|
|
|
|
/* Plain text does not print device info */
|
|
|
|
if (json) {
|
|
|
|
fmt_print(fctx, "device", canonical_path);
|
|
|
|
fmt_print(fctx, "devid", args->devid);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < ARRAY_SIZE(dev_stats); j++) {
|
|
|
|
enum btrfs_dev_stat_values stat_idx = dev_stats[j].stat_idx;
|
|
|
|
|
|
|
|
/* We got fewer items than we know */
|
|
|
|
if (args->nr_items < stat_idx + 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Own format due to [/dev/name].value */
|
|
|
|
if (json) {
|
|
|
|
fmt_print(fctx, dev_stats[j].name, args->values[stat_idx]);
|
|
|
|
} else {
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "[%s].%-16s %llu\n", canonical_path, dev_stats[j].name,
|
2022-09-27 23:13:00 +00:00
|
|
|
args->values[stat_idx]);
|
2022-07-18 11:34:38 +00:00
|
|
|
}
|
|
|
|
if (check && (args->values[stat_idx] > 0))
|
|
|
|
err |= 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt_print_end_group(fctx, NULL);
|
|
|
|
free(canonical_path);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-07-18 11:34:39 +00:00
|
|
|
|
|
|
|
static int print_device_stat_tabular(struct string_table *table, int row,
|
|
|
|
struct btrfs_ioctl_get_dev_stats *args, char *path, bool check)
|
|
|
|
{
|
|
|
|
char *canonical_path = path_canonicalize(path);
|
|
|
|
int j;
|
|
|
|
int err = 0;
|
|
|
|
static const struct {
|
|
|
|
const char name[32];
|
|
|
|
enum btrfs_dev_stat_values stat_idx;
|
|
|
|
} 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 },
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Skip header + --- line */
|
|
|
|
row += 2;
|
|
|
|
|
|
|
|
/* No path when device is missing. */
|
|
|
|
if (!canonical_path) {
|
|
|
|
canonical_path = malloc(32);
|
|
|
|
|
|
|
|
if (!canonical_path) {
|
2022-09-30 07:12:06 +00:00
|
|
|
error_msg(ERROR_MSG_MEMORY, "device path buffer");
|
2022-07-18 11:34:39 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(canonical_path, 32, "devid:%llu", args->devid);
|
|
|
|
}
|
|
|
|
table_printf(table, 0, row, ">%llu", args->devid);
|
|
|
|
table_printf(table, 1, row, ">%s", canonical_path);
|
|
|
|
free(canonical_path);
|
|
|
|
|
|
|
|
for (j = 0; j < ARRAY_SIZE(dev_stats); j++) {
|
|
|
|
enum btrfs_dev_stat_values stat_idx = dev_stats[j].stat_idx;
|
|
|
|
|
|
|
|
/* We got fewer items than we know */
|
|
|
|
if (args->nr_items < stat_idx + 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
table_printf(table, 2, row, ">%llu", args->values[stat_idx]);
|
|
|
|
table_printf(table, 3, row, ">%llu", args->values[stat_idx]);
|
|
|
|
table_printf(table, 4, row, ">%llu", args->values[stat_idx]);
|
|
|
|
table_printf(table, 5, row, ">%llu", args->values[stat_idx]);
|
|
|
|
table_printf(table, 6, row, ">%llu", args->values[stat_idx]);
|
|
|
|
|
|
|
|
if (check && (args->values[stat_idx] > 0))
|
|
|
|
err |= 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_device_stats(const struct cmd_struct *cmd, 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;
|
2022-07-18 11:34:39 +00:00
|
|
|
struct string_table *table = NULL;
|
2012-05-15 09:30:39 +00:00
|
|
|
int ret;
|
|
|
|
int fdmnt;
|
|
|
|
int i;
|
|
|
|
int err = 0;
|
2022-07-18 11:34:38 +00:00
|
|
|
bool check = false;
|
2022-07-18 11:34:39 +00:00
|
|
|
bool free_table = false;
|
|
|
|
bool tabular = false;
|
2012-05-15 09:30:39 +00:00
|
|
|
__u64 flags = 0;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2020-12-11 16:48:12 +00:00
|
|
|
struct format_ctx fctx;
|
2012-05-15 09:30:39 +00:00
|
|
|
|
2018-06-20 00:38:38 +00:00
|
|
|
optind = 0;
|
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}
|
|
|
|
};
|
|
|
|
|
2022-07-18 11:34:39 +00:00
|
|
|
c = getopt_long(argc, argv, "czT", 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':
|
2022-07-18 11:34:38 +00:00
|
|
|
check = true;
|
2016-12-12 17:02:19 +00:00
|
|
|
break;
|
2012-05-15 09:30:39 +00:00
|
|
|
case 'z':
|
|
|
|
flags = BTRFS_DEV_STATS_RESET;
|
|
|
|
break;
|
2022-07-18 11:34:39 +00:00
|
|
|
case 'T':
|
|
|
|
tabular = true;
|
|
|
|
break;
|
2012-05-15 09:30:39 +00:00
|
|
|
default:
|
2018-03-07 02:54:38 +00:00
|
|
|
usage_unknown_option(cmd, argv);
|
2012-05-15 09:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 15:29:16 +00:00
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
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) {
|
2018-10-25 12:10:54 +00:00
|
|
|
errno = -ret;
|
|
|
|
error("getting device info for %s failed: %m", dev_path);
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-18 11:34:39 +00:00
|
|
|
if (tabular) {
|
|
|
|
/*
|
|
|
|
* cols = Id/Path/write/read/flush/corruption/generation
|
|
|
|
* rows = num devices + 2 (header and ---- line)
|
|
|
|
*/
|
|
|
|
table = table_create(7, fi_args.num_devices + 2);
|
|
|
|
if (!table) {
|
2022-09-30 07:12:06 +00:00
|
|
|
error_msg(ERROR_MSG_MEMORY, NULL);
|
2022-07-18 11:34:39 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
free_table = true;
|
|
|
|
table_printf(table, 0,0, "<Id");
|
|
|
|
table_printf(table, 1,0, "<Path");
|
|
|
|
table_printf(table, 2,0, "<Write errors");
|
|
|
|
table_printf(table, 3,0, "<Read errors");
|
|
|
|
table_printf(table, 4,0, "<Flush errors");
|
|
|
|
table_printf(table, 5,0, "<Corruption errors");
|
|
|
|
table_printf(table, 6,0, "<Generation errors");
|
|
|
|
for (i = 0; i < 7; i++)
|
|
|
|
table_printf(table, i, 1, "*-");
|
|
|
|
} else {
|
|
|
|
fmt_start(&fctx, device_stats_rowspec, 24, 0);
|
|
|
|
fmt_print_start_group(&fctx, "device-stats", JSON_TYPE_ARRAY);
|
|
|
|
}
|
|
|
|
|
2012-05-15 09:30:39 +00:00
|
|
|
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];
|
2022-07-18 11:34:38 +00:00
|
|
|
int err2;
|
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) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("device stats ioctl failed on %s: %m",
|
|
|
|
path);
|
2016-12-05 18:35:20 +00:00
|
|
|
err |= 1;
|
2022-07-18 11:34:38 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2016-04-05 15:27:43 +00:00
|
|
|
|
2022-07-18 11:34:39 +00:00
|
|
|
if (tabular)
|
|
|
|
err2 = print_device_stat_tabular(table, i, &args, path, check);
|
|
|
|
else
|
|
|
|
err2 = print_device_stat_string(&fctx, &args, path, check);
|
|
|
|
|
2022-07-18 11:34:38 +00:00
|
|
|
if (err2) {
|
|
|
|
if (err2 < 0) {
|
|
|
|
err = err2;
|
|
|
|
goto out;
|
|
|
|
} else {
|
|
|
|
err |= err2;
|
2016-12-05 18:35:20 +00:00
|
|
|
}
|
2012-05-15 09:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 11:34:39 +00:00
|
|
|
if (tabular) {
|
|
|
|
table_dump(table);
|
|
|
|
} else {
|
|
|
|
fmt_print_end_group(&fctx, "device-stats");
|
|
|
|
fmt_end(&fctx);
|
|
|
|
}
|
2020-12-11 16:48:12 +00:00
|
|
|
|
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);
|
2022-07-18 11:34:39 +00:00
|
|
|
if (free_table)
|
|
|
|
table_free(table);
|
2012-05-15 09:30:39 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2020-12-11 16:48:12 +00:00
|
|
|
static DEFINE_COMMAND_WITH_FLAGS(device_stats, "stats", CMD_FORMAT_JSON);
|
2012-05-15 09:30:39 +00:00
|
|
|
|
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.",
|
2019-06-21 13:36:04 +00:00
|
|
|
"",
|
2015-11-06 15:39:49 +00:00
|
|
|
HELPINFO_UNITS_SHORT_LONG,
|
2014-04-23 17:00:22 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-05-16 21:38:36 +00:00
|
|
|
static int _cmd_device_usage(int fd, const 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++) {
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "%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);
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\n");
|
2014-04-23 17:00:22 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_device_usage(const struct cmd_struct *cmd, 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
|
|
|
|
2018-03-07 02:42:01 +00:00
|
|
|
clean_args_no_options(cmd, argc, argv);
|
2016-03-01 15:02:08 +00:00
|
|
|
|
|
|
|
if (check_argc_min(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2014-04-23 17:00:22 +00:00
|
|
|
|
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)
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\n");
|
2014-04-23 17:00:22 +00:00
|
|
|
|
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);
|
2020-05-04 12:34:59 +00:00
|
|
|
btrfs_warn_multiple_profiles(fd);
|
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
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(device_usage, "usage");
|
2014-04-23 17:00:22 +00:00
|
|
|
|
2022-07-18 17:26:39 +00:00
|
|
|
static const char * const cmd_device_replace_usage[] = {
|
|
|
|
"btrfs device replace <command> [...]\n"
|
|
|
|
"\tReplace a device (alias of \"btrfs replace\")",
|
|
|
|
"Please see \"btrfs replace --help\" for more information.",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static int cmd_device_replace(const struct cmd_struct *unused,
|
|
|
|
int argc, char **argv)
|
|
|
|
{
|
|
|
|
return cmd_execute(&cmd_struct_replace, argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Alias of 1st level command 'replace' as a subcommand of 'device' */
|
|
|
|
static DEFINE_COMMAND(device_replace, "replace", cmd_device_replace,
|
|
|
|
cmd_device_replace_usage, NULL, CMD_ALIAS);
|
|
|
|
|
2015-06-09 12:22:05 +00:00
|
|
|
static const char device_cmd_group_info[] =
|
|
|
|
"manage and query devices in the filesystem";
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
static const struct cmd_group device_cmd_group = {
|
2015-06-09 12:22:05 +00:00
|
|
|
device_cmd_group_usage, device_cmd_group_info, {
|
2018-03-07 02:07:03 +00:00
|
|
|
&cmd_struct_device_add,
|
|
|
|
&cmd_struct_device_delete,
|
|
|
|
&cmd_struct_device_remove,
|
2022-07-18 17:26:39 +00:00
|
|
|
&cmd_struct_device_replace,
|
2018-03-07 02:07:03 +00:00
|
|
|
&cmd_struct_device_scan,
|
|
|
|
&cmd_struct_device_ready,
|
|
|
|
&cmd_struct_device_stats,
|
|
|
|
&cmd_struct_device_usage,
|
|
|
|
NULL
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
DEFINE_GROUP_COMMAND_TOKEN(device);
|