2012-05-07 12:00:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 STRATO. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
#include <time.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <sys/wait.h>
|
2020-11-05 02:38:00 +00:00
|
|
|
#include <getopt.h>
|
2012-05-07 12:00:20 +00:00
|
|
|
|
|
|
|
#include "kerncompat.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/ctree.h"
|
2012-05-07 12:00:20 +00:00
|
|
|
#include "ioctl.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/utils.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/volumes.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/disk-io.h"
|
2021-04-29 17:52:15 +00:00
|
|
|
#include "common/open-utils.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "cmds/commands.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"
|
2017-01-31 22:13:54 +00:00
|
|
|
#include "mkfs/common.h"
|
2012-05-07 12:00:20 +00:00
|
|
|
|
|
|
|
static int print_replace_status(int fd, const char *path, int once);
|
|
|
|
static char *time2string(char *buf, size_t s, __u64 t);
|
|
|
|
static char *progress2string(char *buf, size_t s, int progress_1000);
|
|
|
|
|
|
|
|
|
|
|
|
static const char *replace_dev_result2string(__u64 result)
|
|
|
|
{
|
|
|
|
switch (result) {
|
|
|
|
case BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR:
|
|
|
|
return "no error";
|
|
|
|
case BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED:
|
|
|
|
return "not started";
|
|
|
|
case BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED:
|
|
|
|
return "already started";
|
2014-10-13 04:42:13 +00:00
|
|
|
case BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS:
|
|
|
|
return "scrub is in progress";
|
2012-05-07 12:00:20 +00:00
|
|
|
default:
|
|
|
|
return "<illegal result value>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char * const replace_cmd_group_usage[] = {
|
|
|
|
"btrfs replace <command> [<args>]",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static int dev_replace_cancel_fd = -1;
|
|
|
|
static void dev_replace_sigint_handler(int signal)
|
|
|
|
{
|
2013-03-04 22:35:42 +00:00
|
|
|
int ret;
|
2012-05-07 12:00:20 +00:00
|
|
|
struct btrfs_ioctl_dev_replace_args args = {0};
|
|
|
|
|
|
|
|
args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL;
|
2013-03-04 22:35:42 +00:00
|
|
|
ret = ioctl(dev_replace_cancel_fd, BTRFS_IOC_DEV_REPLACE, &args);
|
|
|
|
if (ret < 0)
|
|
|
|
perror("Device replace cancel failed");
|
2012-05-07 12:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dev_replace_handle_sigint(int fd)
|
|
|
|
{
|
|
|
|
struct sigaction sa = {
|
|
|
|
.sa_handler = fd == -1 ? SIG_DFL : dev_replace_sigint_handler
|
|
|
|
};
|
|
|
|
|
|
|
|
dev_replace_cancel_fd = fd;
|
|
|
|
return sigaction(SIGINT, &sa, NULL);
|
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char *const cmd_replace_start_usage[] = {
|
2013-07-23 02:43:21 +00:00
|
|
|
"btrfs replace start [-Bfr] <srcdev>|<devid> <targetdev> <mount_point>",
|
2012-05-07 12:00:20 +00:00
|
|
|
"Replace device of a btrfs filesystem.",
|
|
|
|
"On a live filesystem, duplicate the data to the target device which",
|
|
|
|
"is currently stored on the source device. If the source device is not",
|
|
|
|
"available anymore, or if the -r option is set, the data is built",
|
|
|
|
"only using the RAID redundancy mechanisms. After completion of the",
|
|
|
|
"operation, the source device is removed from the filesystem.",
|
2013-07-23 02:43:21 +00:00
|
|
|
"If the <srcdev> is a numerical value, it is assumed to be the device id",
|
|
|
|
"of the filesystem which is mounted at <mount_point>, otherwise it is",
|
2012-05-07 12:00:20 +00:00
|
|
|
"the path to the source device. If the source device is disconnected,",
|
2013-07-23 02:43:21 +00:00
|
|
|
"from the system, you have to use the <devid> parameter format.",
|
|
|
|
"The <targetdev> needs to be same size or larger than the <srcdev>.",
|
2012-05-07 12:00:20 +00:00
|
|
|
"",
|
2013-07-23 02:43:21 +00:00
|
|
|
"-r only read from <srcdev> if no other zero-defect mirror exists",
|
2012-05-07 12:00:20 +00:00
|
|
|
" (enable this if your drive has lots of read errors, the access",
|
|
|
|
" would be very slow)",
|
2013-07-23 02:43:21 +00:00
|
|
|
"-f force using and overwriting <targetdev> even if it looks like",
|
2012-05-07 12:00:20 +00:00
|
|
|
" containing a valid btrfs filesystem. A valid filesystem is",
|
|
|
|
" assumed if a btrfs superblock is found which contains a",
|
|
|
|
" correct checksum. Devices which are currently mounted are",
|
2013-07-23 02:43:21 +00:00
|
|
|
" never allowed to be used as the <targetdev>",
|
2012-05-07 12:00:20 +00:00
|
|
|
"-B do not background",
|
2021-09-27 17:57:03 +00:00
|
|
|
"--enqueue wait if there's another exclusive operation running,",
|
|
|
|
" otherwise continue",
|
|
|
|
"-K|--nodiscard do not perform whole device TRIM",
|
2012-05-07 12:00:20 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_replace_start(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-05-07 12:00:20 +00:00
|
|
|
{
|
2021-04-26 06:27:42 +00:00
|
|
|
struct btrfs_ioctl_feature_flags feature_flags;
|
2012-05-07 12:00:20 +00:00
|
|
|
struct btrfs_ioctl_dev_replace_args start_args = {0};
|
|
|
|
struct btrfs_ioctl_dev_replace_args status_args = {0};
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
int fdmnt = -1;
|
|
|
|
int fddstdev = -1;
|
2021-04-26 06:27:42 +00:00
|
|
|
int zoned;
|
2012-05-07 12:00:20 +00:00
|
|
|
char *path;
|
|
|
|
char *srcdev;
|
2014-06-04 20:43:11 +00:00
|
|
|
char *dstdev = NULL;
|
2012-05-07 12:00:20 +00:00
|
|
|
int avoid_reading_from_srcdev = 0;
|
|
|
|
int force_using_targetdev = 0;
|
|
|
|
u64 dstdev_block_count;
|
|
|
|
int do_not_background = 0;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2015-08-20 05:24:23 +00:00
|
|
|
u64 srcdev_size;
|
|
|
|
u64 dstdev_size;
|
2020-11-05 02:38:00 +00:00
|
|
|
bool enqueue = false;
|
2021-09-27 17:57:03 +00:00
|
|
|
bool discard = true;
|
2012-05-07 12:00:20 +00:00
|
|
|
|
2018-06-20 00:38:38 +00:00
|
|
|
optind = 0;
|
2020-11-05 02:38:00 +00:00
|
|
|
while (1) {
|
|
|
|
int c;
|
|
|
|
enum { GETOPT_VAL_ENQUEUE = 256 };
|
|
|
|
static const struct option long_options[] = {
|
|
|
|
{ "enqueue", no_argument, NULL, GETOPT_VAL_ENQUEUE},
|
2021-09-27 17:57:03 +00:00
|
|
|
{ "nodiscard", no_argument, NULL, 'K' },
|
2020-11-05 02:38:00 +00:00
|
|
|
{ NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2021-09-27 17:57:03 +00:00
|
|
|
c = getopt_long(argc, argv, "BKrf", long_options, NULL);
|
2020-11-05 02:38:00 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
2012-05-07 12:00:20 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'B':
|
|
|
|
do_not_background = 1;
|
|
|
|
break;
|
2021-09-27 17:57:03 +00:00
|
|
|
case 'K':
|
|
|
|
discard = false;
|
|
|
|
break;
|
2012-05-07 12:00:20 +00:00
|
|
|
case 'r':
|
|
|
|
avoid_reading_from_srcdev = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
force_using_targetdev = 1;
|
|
|
|
break;
|
2020-11-05 02:38:00 +00:00
|
|
|
case GETOPT_VAL_ENQUEUE:
|
|
|
|
enqueue = true;
|
|
|
|
break;
|
2012-05-07 12:00:20 +00:00
|
|
|
default:
|
2018-03-07 02:54:38 +00:00
|
|
|
usage_unknown_option(cmd, argv);
|
2012-05-07 12:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start_args.start.cont_reading_from_srcdev_mode =
|
|
|
|
avoid_reading_from_srcdev ?
|
|
|
|
BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID :
|
|
|
|
BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
|
|
|
|
if (check_argc_exact(argc - optind, 3))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2012-05-07 12:00:20 +00:00
|
|
|
path = argv[optind + 2];
|
2013-03-12 04:17:40 +00:00
|
|
|
|
2015-10-12 13:23:02 +00:00
|
|
|
fdmnt = open_path_or_dev_mnt(path, &dirstream, 1);
|
|
|
|
if (fdmnt < 0)
|
2012-05-07 12:00:20 +00:00
|
|
|
goto leave_with_error;
|
|
|
|
|
2021-04-26 06:27:42 +00:00
|
|
|
ret = ioctl(fdmnt, BTRFS_IOC_GET_FEATURES, &feature_flags);
|
|
|
|
if (ret) {
|
|
|
|
error("zoned: ioctl(GET_FEATURES) on '%s' returns error: %m",
|
|
|
|
path);
|
|
|
|
goto leave_with_error;
|
|
|
|
}
|
|
|
|
zoned = (feature_flags.incompat_flags & BTRFS_FEATURE_INCOMPAT_ZONED);
|
|
|
|
|
2012-05-07 12:00:20 +00:00
|
|
|
/* check for possible errors before backgrounding */
|
|
|
|
status_args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS;
|
2014-10-10 13:16:16 +00:00
|
|
|
status_args.result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT;
|
2012-05-07 12:00:20 +00:00
|
|
|
ret = ioctl(fdmnt, BTRFS_IOC_DEV_REPLACE, &status_args);
|
2016-01-12 12:35:50 +00:00
|
|
|
if (ret < 0) {
|
2012-05-07 12:00:20 +00:00
|
|
|
fprintf(stderr,
|
2018-01-07 21:54:21 +00:00
|
|
|
"ERROR: ioctl(DEV_REPLACE_STATUS) failed on \"%s\": %m",
|
|
|
|
path);
|
2014-10-10 13:16:16 +00:00
|
|
|
if (status_args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT)
|
|
|
|
fprintf(stderr, ", %s\n",
|
|
|
|
replace_dev_result2string(status_args.result));
|
|
|
|
else
|
|
|
|
fprintf(stderr, "\n");
|
2012-05-07 12:00:20 +00:00
|
|
|
goto leave_with_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status_args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("ioctl(DEV_REPLACE_STATUS) on '%s' returns error: %s",
|
2012-05-07 12:00:20 +00:00
|
|
|
path, replace_dev_result2string(status_args.result));
|
|
|
|
goto leave_with_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status_args.status.replace_state ==
|
|
|
|
BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("device replace on '%s' already started", path);
|
2012-05-07 12:00:20 +00:00
|
|
|
goto leave_with_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
srcdev = argv[optind];
|
2021-01-21 15:25:51 +00:00
|
|
|
dstdev = path_canonicalize(argv[optind + 1]);
|
2014-06-04 20:43:11 +00:00
|
|
|
if (!dstdev) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("cannot canonicalize path '%s': %m",
|
|
|
|
argv[optind + 1]);
|
2014-10-08 09:42:29 +00:00
|
|
|
goto leave_with_error;
|
2014-06-04 20:43:11 +00:00
|
|
|
}
|
2012-05-07 12:00:20 +00:00
|
|
|
|
2015-10-10 14:30:56 +00:00
|
|
|
if (string_is_numerical(srcdev)) {
|
2012-05-07 12:00:20 +00:00
|
|
|
struct btrfs_ioctl_fs_info_args fi_args;
|
|
|
|
struct btrfs_ioctl_dev_info_args *di_args = NULL;
|
|
|
|
|
2014-02-20 01:30:52 +00:00
|
|
|
start_args.start.srcdevid = arg_strtou64(srcdev);
|
2012-05-07 12:00:20 +00:00
|
|
|
|
2013-03-12 04:17:40 +00:00
|
|
|
ret = get_fs_info(path, &fi_args, &di_args);
|
2012-05-07 12:00:20 +00:00
|
|
|
if (ret) {
|
2018-10-25 12:10:54 +00:00
|
|
|
errno = -ret;
|
|
|
|
error("failed to get device info: %m");
|
2012-05-07 12:00:20 +00:00
|
|
|
free(di_args);
|
|
|
|
goto leave_with_error;
|
|
|
|
}
|
|
|
|
if (!fi_args.num_devices) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("no devices found");
|
2012-05-07 12:00:20 +00:00
|
|
|
free(di_args);
|
|
|
|
goto leave_with_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < fi_args.num_devices; i++)
|
|
|
|
if (start_args.start.srcdevid == di_args[i].devid)
|
|
|
|
break;
|
2015-08-26 11:34:39 +00:00
|
|
|
srcdev_size = di_args[i].total_bytes;
|
2013-02-25 22:54:45 +00:00
|
|
|
free(di_args);
|
2012-05-07 12:00:20 +00:00
|
|
|
if (i == fi_args.num_devices) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("'%s' is not a valid devid for filesystem '%s'",
|
2012-05-07 12:00:20 +00:00
|
|
|
srcdev, path);
|
|
|
|
goto leave_with_error;
|
|
|
|
}
|
2019-07-01 19:29:43 +00:00
|
|
|
} else if (path_is_block_device(srcdev) > 0) {
|
2012-05-07 12:00:20 +00:00
|
|
|
strncpy((char *)start_args.start.srcdev_name, srcdev,
|
|
|
|
BTRFS_DEVICE_PATH_NAME_MAX);
|
|
|
|
start_args.start.srcdevid = 0;
|
2021-04-29 16:36:44 +00:00
|
|
|
srcdev_size = device_get_partition_size(srcdev);
|
2015-08-20 05:24:23 +00:00
|
|
|
} else {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("source device must be a block device or a devid");
|
2015-08-20 05:24:23 +00:00
|
|
|
goto leave_with_error;
|
2012-05-07 12:00:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 22:46:30 +00:00
|
|
|
ret = test_dev_for_mkfs(dstdev, force_using_targetdev);
|
|
|
|
if (ret)
|
2012-05-07 12:00:20 +00:00
|
|
|
goto leave_with_error;
|
2015-06-10 22:46:30 +00:00
|
|
|
|
2021-04-29 16:36:44 +00:00
|
|
|
dstdev_size = device_get_partition_size(dstdev);
|
2015-08-20 05:24:23 +00:00
|
|
|
if (srcdev_size > dstdev_size) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("target device smaller than source device (required %llu bytes)",
|
2015-08-20 05:24:23 +00:00
|
|
|
srcdev_size);
|
|
|
|
goto leave_with_error;
|
|
|
|
}
|
|
|
|
|
2012-05-07 12:00:20 +00:00
|
|
|
fddstdev = open(dstdev, O_RDWR);
|
|
|
|
if (fddstdev < 0) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("unable to open %s: %m", dstdev);
|
2012-05-07 12:00:20 +00:00
|
|
|
goto leave_with_error;
|
|
|
|
}
|
2020-08-25 15:03:37 +00:00
|
|
|
|
|
|
|
/* Check status before any potentially destructive operation */
|
2020-11-05 02:38:00 +00:00
|
|
|
ret = check_running_fs_exclop(fdmnt, BTRFS_EXCLOP_DEV_REPLACE, 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);
|
|
|
|
goto leave_with_error;
|
|
|
|
}
|
|
|
|
|
2012-05-07 12:00:20 +00:00
|
|
|
strncpy((char *)start_args.start.tgtdev_name, dstdev,
|
|
|
|
BTRFS_DEVICE_PATH_NAME_MAX);
|
2016-07-27 23:47:40 +00:00
|
|
|
ret = btrfs_prepare_device(fddstdev, dstdev, &dstdev_block_count, 0,
|
2021-04-26 06:27:42 +00:00
|
|
|
PREP_DEVICE_ZERO_END | PREP_DEVICE_VERBOSE |
|
2021-09-27 17:57:03 +00:00
|
|
|
(discard ? PREP_DEVICE_DISCARD : 0) |
|
2021-04-26 06:27:42 +00:00
|
|
|
(zoned ? PREP_DEVICE_ZONED : 0));
|
2013-12-18 04:07:55 +00:00
|
|
|
if (ret)
|
2012-05-07 12:00:20 +00:00
|
|
|
goto leave_with_error;
|
2013-12-18 04:07:55 +00:00
|
|
|
|
2012-05-07 12:00:20 +00:00
|
|
|
close(fddstdev);
|
|
|
|
fddstdev = -1;
|
2014-06-04 20:43:11 +00:00
|
|
|
free(dstdev);
|
|
|
|
dstdev = NULL;
|
2012-05-07 12:00:20 +00:00
|
|
|
|
|
|
|
dev_replace_handle_sigint(fdmnt);
|
|
|
|
if (!do_not_background) {
|
|
|
|
if (daemon(0, 0) < 0) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("backgrounding failed: %m");
|
2012-05-07 12:00:20 +00:00
|
|
|
goto leave_with_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start_args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_START;
|
2014-10-10 13:16:16 +00:00
|
|
|
start_args.result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT;
|
2012-05-07 12:00:20 +00:00
|
|
|
ret = ioctl(fdmnt, BTRFS_IOC_DEV_REPLACE, &start_args);
|
|
|
|
if (do_not_background) {
|
2016-01-12 12:35:50 +00:00
|
|
|
if (ret < 0) {
|
2012-05-07 12:00:20 +00:00
|
|
|
fprintf(stderr,
|
2018-01-07 21:54:21 +00:00
|
|
|
"ERROR: ioctl(DEV_REPLACE_START) failed on \"%s\": %m",
|
|
|
|
path);
|
2014-10-10 13:16:16 +00:00
|
|
|
if (start_args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT)
|
|
|
|
fprintf(stderr, ", %s\n",
|
|
|
|
replace_dev_result2string(start_args.result));
|
|
|
|
else
|
|
|
|
fprintf(stderr, "\n");
|
2014-06-05 02:02:52 +00:00
|
|
|
|
|
|
|
if (errno == EOPNOTSUPP)
|
2016-01-11 10:01:47 +00:00
|
|
|
warning("device replace of RAID5/6 not supported with this kernel");
|
2014-06-05 02:02:52 +00:00
|
|
|
|
2012-05-07 12:00:20 +00:00
|
|
|
goto leave_with_error;
|
|
|
|
}
|
|
|
|
|
2020-09-30 02:46:14 +00:00
|
|
|
if (ret > 0) {
|
2019-01-31 10:05:41 +00:00
|
|
|
error("ioctl(DEV_REPLACE_START) '%s': %s", path,
|
|
|
|
btrfs_err_str(ret));
|
2020-09-30 02:46:14 +00:00
|
|
|
goto leave_with_error;
|
|
|
|
}
|
2019-01-31 10:05:41 +00:00
|
|
|
|
2019-01-31 10:05:40 +00:00
|
|
|
if (start_args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT &&
|
|
|
|
start_args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("ioctl(DEV_REPLACE_START) on '%s' returns error: %s",
|
2012-05-07 12:00:20 +00:00
|
|
|
path,
|
|
|
|
replace_dev_result2string(start_args.result));
|
|
|
|
goto leave_with_error;
|
|
|
|
}
|
|
|
|
}
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fdmnt, dirstream);
|
2012-05-07 12:00:20 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
leave_with_error:
|
2014-06-04 20:43:11 +00:00
|
|
|
if (dstdev)
|
|
|
|
free(dstdev);
|
2012-05-07 12:00:20 +00:00
|
|
|
if (fdmnt != -1)
|
|
|
|
close(fdmnt);
|
|
|
|
if (fddstdev != -1)
|
|
|
|
close(fddstdev);
|
2013-09-04 15:22:36 +00:00
|
|
|
return 1;
|
2012-05-07 12:00:20 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(replace_start, "start");
|
2012-05-07 12:00:20 +00:00
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char *const cmd_replace_status_usage[] = {
|
2013-07-23 02:43:21 +00:00
|
|
|
"btrfs replace status [-1] <mount_point>",
|
2020-06-29 14:57:54 +00:00
|
|
|
"Print status and progress information of a running device replace operation",
|
2012-05-07 12:00:20 +00:00
|
|
|
"",
|
2013-07-23 02:43:21 +00:00
|
|
|
"-1 print once instead of print continuously until the replace",
|
2012-05-07 12:00:20 +00:00
|
|
|
" operation finishes (or is canceled)",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_replace_status(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-05-07 12:00:20 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int c;
|
|
|
|
char *path;
|
|
|
|
int once = 0;
|
|
|
|
int ret;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-05-07 12:00:20 +00:00
|
|
|
|
2018-06-20 00:38:38 +00:00
|
|
|
optind = 0;
|
2012-05-07 12:00:20 +00:00
|
|
|
while ((c = getopt(argc, argv, "1")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case '1':
|
|
|
|
once = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2018-03-07 02:54:38 +00:00
|
|
|
usage_unknown_option(cmd, argv);
|
2012-05-07 12:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2012-05-07 12:00:20 +00:00
|
|
|
|
|
|
|
path = argv[optind];
|
2015-10-12 13:23:03 +00:00
|
|
|
fd = btrfs_open_dir(path, &dirstream, 1);
|
|
|
|
if (fd < 0)
|
2013-09-04 15:22:36 +00:00
|
|
|
return 1;
|
2012-05-07 12:00:20 +00:00
|
|
|
|
|
|
|
ret = print_replace_status(fd, path, once);
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
2013-09-04 15:22:36 +00:00
|
|
|
return !!ret;
|
2012-05-07 12:00:20 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(replace_status, "status");
|
2012-05-07 12:00:20 +00:00
|
|
|
|
|
|
|
static int print_replace_status(int fd, const char *path, int once)
|
|
|
|
{
|
|
|
|
struct btrfs_ioctl_dev_replace_args args = {0};
|
|
|
|
struct btrfs_ioctl_dev_replace_status_params *status;
|
|
|
|
int ret;
|
|
|
|
int prevent_loop = 0;
|
|
|
|
int skip_stats;
|
|
|
|
int num_chars;
|
|
|
|
char string1[80];
|
|
|
|
char string2[80];
|
|
|
|
char string3[80];
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS;
|
2014-10-10 13:16:16 +00:00
|
|
|
args.result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT;
|
2012-05-07 12:00:20 +00:00
|
|
|
ret = ioctl(fd, BTRFS_IOC_DEV_REPLACE, &args);
|
2016-01-12 12:35:50 +00:00
|
|
|
if (ret < 0) {
|
2018-01-07 21:54:21 +00:00
|
|
|
fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_STATUS) failed on \"%s\": %m",
|
|
|
|
path);
|
2014-10-10 13:16:16 +00:00
|
|
|
if (args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT)
|
|
|
|
fprintf(stderr, ", %s\n",
|
|
|
|
replace_dev_result2string(args.result));
|
|
|
|
else
|
|
|
|
fprintf(stderr, "\n");
|
2012-05-07 12:00:20 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("ioctl(DEV_REPLACE_STATUS) on '%s' returns error: %s",
|
2012-05-07 12:00:20 +00:00
|
|
|
path,
|
|
|
|
replace_dev_result2string(args.result));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-25 14:50:08 +00:00
|
|
|
status = &args.status;
|
|
|
|
|
2012-05-07 12:00:20 +00:00
|
|
|
skip_stats = 0;
|
|
|
|
num_chars = 0;
|
|
|
|
switch (status->replace_state) {
|
|
|
|
case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
|
|
|
|
num_chars =
|
|
|
|
printf("%s done",
|
|
|
|
progress2string(string3,
|
|
|
|
sizeof(string3),
|
|
|
|
status->progress_1000));
|
|
|
|
break;
|
|
|
|
case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
|
|
|
|
prevent_loop = 1;
|
|
|
|
printf("Started on %s, finished on %s",
|
|
|
|
time2string(string1, sizeof(string1),
|
|
|
|
status->time_started),
|
|
|
|
time2string(string2, sizeof(string2),
|
|
|
|
status->time_stopped));
|
|
|
|
break;
|
|
|
|
case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
|
|
|
|
prevent_loop = 1;
|
|
|
|
printf("Started on %s, canceled on %s at %s",
|
|
|
|
time2string(string1, sizeof(string1),
|
|
|
|
status->time_started),
|
|
|
|
time2string(string2, sizeof(string2),
|
|
|
|
status->time_stopped),
|
|
|
|
progress2string(string3, sizeof(string3),
|
|
|
|
status->progress_1000));
|
|
|
|
break;
|
|
|
|
case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
|
|
|
|
prevent_loop = 1;
|
|
|
|
printf("Started on %s, suspended on %s at %s",
|
|
|
|
time2string(string1, sizeof(string1),
|
|
|
|
status->time_started),
|
|
|
|
time2string(string2, sizeof(string2),
|
|
|
|
status->time_stopped),
|
|
|
|
progress2string(string3, sizeof(string3),
|
|
|
|
status->progress_1000));
|
|
|
|
break;
|
|
|
|
case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
|
|
|
|
prevent_loop = 1;
|
|
|
|
skip_stats = 1;
|
|
|
|
printf("Never started");
|
|
|
|
break;
|
|
|
|
default:
|
2016-10-28 16:47:35 +00:00
|
|
|
error("unknown status from ioctl DEV_REPLACE_STATUS on '%s': %llu",
|
2015-08-25 14:50:08 +00:00
|
|
|
path, status->replace_state);
|
|
|
|
return -EINVAL;
|
2012-05-07 12:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!skip_stats)
|
|
|
|
num_chars += printf(
|
|
|
|
", %llu write errs, %llu uncorr. read errs",
|
|
|
|
(unsigned long long)status->num_write_errors,
|
|
|
|
(unsigned long long)
|
|
|
|
status->num_uncorrectable_read_errors);
|
2015-08-25 14:50:08 +00:00
|
|
|
if (once || prevent_loop) {
|
2012-05-07 12:00:20 +00:00
|
|
|
printf("\n");
|
2015-08-25 14:50:08 +00:00
|
|
|
break;
|
2012-05-07 12:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
sleep(1);
|
|
|
|
while (num_chars > 0) {
|
|
|
|
putchar('\b');
|
|
|
|
num_chars--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
time2string(char *buf, size_t s, __u64 t)
|
|
|
|
{
|
|
|
|
struct tm t_tm;
|
|
|
|
time_t t_time_t;
|
|
|
|
|
|
|
|
t_time_t = (time_t)t;
|
|
|
|
assert((__u64)t_time_t == t);
|
|
|
|
localtime_r(&t_time_t, &t_tm);
|
|
|
|
strftime(buf, s, "%e.%b %T", &t_tm);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
progress2string(char *buf, size_t s, int progress_1000)
|
|
|
|
{
|
|
|
|
snprintf(buf, s, "%d.%01d%%", progress_1000 / 10, progress_1000 % 10);
|
|
|
|
assert(s > 0);
|
|
|
|
buf[s - 1] = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char *const cmd_replace_cancel_usage[] = {
|
2013-07-23 02:43:21 +00:00
|
|
|
"btrfs replace cancel <mount_point>",
|
2012-05-07 12:00:20 +00:00
|
|
|
"Cancel a running device replace operation.",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_replace_cancel(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-05-07 12:00:20 +00:00
|
|
|
{
|
|
|
|
struct btrfs_ioctl_dev_replace_args args = {0};
|
|
|
|
int ret;
|
|
|
|
int c;
|
|
|
|
int fd;
|
|
|
|
char *path;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-05-07 12:00:20 +00:00
|
|
|
|
2018-06-20 00:38:38 +00:00
|
|
|
optind = 0;
|
2012-05-07 12:00:20 +00:00
|
|
|
while ((c = getopt(argc, argv, "")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case '?':
|
|
|
|
default:
|
2018-03-07 02:54:38 +00:00
|
|
|
usage_unknown_option(cmd, argv);
|
2012-05-07 12:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2012-05-07 12:00:20 +00:00
|
|
|
|
|
|
|
path = argv[optind];
|
2015-10-12 13:23:03 +00:00
|
|
|
fd = btrfs_open_dir(path, &dirstream, 1);
|
|
|
|
if (fd < 0)
|
2013-09-04 15:22:36 +00:00
|
|
|
return 1;
|
2012-05-07 12:00:20 +00:00
|
|
|
|
|
|
|
args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL;
|
2014-10-10 13:16:16 +00:00
|
|
|
args.result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT;
|
2012-05-07 12:00:20 +00:00
|
|
|
ret = ioctl(fd, BTRFS_IOC_DEV_REPLACE, &args);
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
2016-01-12 12:35:50 +00:00
|
|
|
if (ret < 0) {
|
2018-01-07 21:54:21 +00:00
|
|
|
fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_CANCEL) failed on \"%s\": %m",
|
|
|
|
path);
|
2014-10-10 13:16:16 +00:00
|
|
|
if (args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT)
|
|
|
|
fprintf(stderr, ", %s\n",
|
|
|
|
replace_dev_result2string(args.result));
|
|
|
|
else
|
|
|
|
fprintf(stderr, "\n");
|
2013-09-04 15:22:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (args.result == BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED) {
|
|
|
|
printf("INFO: ioctl(DEV_REPLACE_CANCEL)\"%s\": %s\n",
|
|
|
|
path, replace_dev_result2string(args.result));
|
|
|
|
return 2;
|
2012-05-07 12:00:20 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(replace_cancel, "cancel");
|
2012-05-07 12:00:20 +00:00
|
|
|
|
2015-06-09 12:22:05 +00:00
|
|
|
static const char replace_cmd_group_info[] =
|
|
|
|
"replace a device in the filesystem";
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
static const struct cmd_group replace_cmd_group = {
|
2015-06-09 12:22:05 +00:00
|
|
|
replace_cmd_group_usage, replace_cmd_group_info, {
|
2018-03-07 02:07:03 +00:00
|
|
|
&cmd_struct_replace_start,
|
|
|
|
&cmd_struct_replace_status,
|
|
|
|
&cmd_struct_replace_cancel,
|
|
|
|
NULL
|
2012-05-07 12:00:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
DEFINE_GROUP_COMMAND_TOKEN(replace);
|