2 * Copyright (C) 2012 STRATO. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
24 #include <sys/ioctl.h>
32 #include "kerncompat.h"
42 static int print_replace_status(int fd
, const char *path
, int once
);
43 static char *time2string(char *buf
, size_t s
, __u64 t
);
44 static char *progress2string(char *buf
, size_t s
, int progress_1000
);
47 static const char *replace_dev_result2string(__u64 result
)
50 case BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR
:
52 case BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED
:
54 case BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED
:
55 return "already started";
57 return "<illegal result value>";
61 static const char * const replace_cmd_group_usage
[] = {
62 "btrfs replace <command> [<args>]",
66 static int is_numerical(const char *str
)
68 if (!(*str
>= '0' && *str
<= '9'))
70 while (*str
>= '0' && *str
<= '9')
77 static int dev_replace_cancel_fd
= -1;
78 static void dev_replace_sigint_handler(int signal
)
81 struct btrfs_ioctl_dev_replace_args args
= {0};
83 args
.cmd
= BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL
;
84 ret
= ioctl(dev_replace_cancel_fd
, BTRFS_IOC_DEV_REPLACE
, &args
);
86 perror("Device replace cancel failed");
89 static int dev_replace_handle_sigint(int fd
)
91 struct sigaction sa
= {
92 .sa_handler
= fd
== -1 ? SIG_DFL
: dev_replace_sigint_handler
95 dev_replace_cancel_fd
= fd
;
96 return sigaction(SIGINT
, &sa
, NULL
);
99 static const char *const cmd_start_replace_usage
[] = {
100 "btrfs replace start [-Bfr] <srcdev>|<devid> <targetdev> <mount_point>",
101 "Replace device of a btrfs filesystem.",
102 "On a live filesystem, duplicate the data to the target device which",
103 "is currently stored on the source device. If the source device is not",
104 "available anymore, or if the -r option is set, the data is built",
105 "only using the RAID redundancy mechanisms. After completion of the",
106 "operation, the source device is removed from the filesystem.",
107 "If the <srcdev> is a numerical value, it is assumed to be the device id",
108 "of the filesystem which is mounted at <mount_point>, otherwise it is",
109 "the path to the source device. If the source device is disconnected,",
110 "from the system, you have to use the <devid> parameter format.",
111 "The <targetdev> needs to be same size or larger than the <srcdev>.",
113 "-r only read from <srcdev> if no other zero-defect mirror exists",
114 " (enable this if your drive has lots of read errors, the access",
115 " would be very slow)",
116 "-f force using and overwriting <targetdev> even if it looks like",
117 " containing a valid btrfs filesystem. A valid filesystem is",
118 " assumed if a btrfs superblock is found which contains a",
119 " correct checksum. Devices which are currently mounted are",
120 " never allowed to be used as the <targetdev>",
121 "-B do not background",
125 static int cmd_start_replace(int argc
, char **argv
)
127 struct btrfs_ioctl_dev_replace_args start_args
= {0};
128 struct btrfs_ioctl_dev_replace_args status_args
= {0};
138 int avoid_reading_from_srcdev
= 0;
139 int force_using_targetdev
= 0;
141 u64 dstdev_block_count
;
142 int do_not_background
= 0;
144 DIR *dirstream
= NULL
;
145 char estr
[100]; /* check test_dev_for_mkfs() for error string size*/
147 while ((c
= getopt(argc
, argv
, "Brf")) != -1) {
150 do_not_background
= 1;
153 avoid_reading_from_srcdev
= 1;
156 force_using_targetdev
= 1;
160 usage(cmd_start_replace_usage
);
164 start_args
.start
.cont_reading_from_srcdev_mode
=
165 avoid_reading_from_srcdev
?
166 BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID
:
167 BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS
;
168 if (check_argc_exact(argc
- optind
, 3))
169 usage(cmd_start_replace_usage
);
170 path
= argv
[optind
+ 2];
172 fdmnt
= open_path_or_dev_mnt(path
, &dirstream
);
177 "ERROR: '%s' is not a mounted btrfs device\n",
180 fprintf(stderr
, "ERROR: can't access '%s': %s\n",
181 path
, strerror(errno
));
182 goto leave_with_error
;
185 /* check for possible errors before backgrounding */
186 status_args
.cmd
= BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS
;
187 ret
= ioctl(fdmnt
, BTRFS_IOC_DEV_REPLACE
, &status_args
);
190 "ERROR: ioctl(DEV_REPLACE_STATUS) failed on \"%s\": %s, %s\n",
191 path
, strerror(errno
),
192 replace_dev_result2string(status_args
.result
));
193 goto leave_with_error
;
196 if (status_args
.result
!= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR
) {
198 "ERROR: ioctl(DEV_REPLACE_STATUS) on \"%s\" returns error: %s\n",
199 path
, replace_dev_result2string(status_args
.result
));
200 goto leave_with_error
;
203 if (status_args
.status
.replace_state
==
204 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED
) {
206 "ERROR: btrfs replace on \"%s\" already started!\n",
208 goto leave_with_error
;
211 srcdev
= argv
[optind
];
212 dstdev
= canonicalize_path(argv
[optind
+ 1]);
215 "ERROR: Could not canonicalize path '%s': %s\n",
216 argv
[optind
+ 1], strerror(errno
));
219 if (is_numerical(srcdev
)) {
220 struct btrfs_ioctl_fs_info_args fi_args
;
221 struct btrfs_ioctl_dev_info_args
*di_args
= NULL
;
223 start_args
.start
.srcdevid
= arg_strtou64(srcdev
);
225 ret
= get_fs_info(path
, &fi_args
, &di_args
);
227 fprintf(stderr
, "ERROR: getting dev info for devstats failed: "
228 "%s\n", strerror(-ret
));
230 goto leave_with_error
;
232 if (!fi_args
.num_devices
) {
233 fprintf(stderr
, "ERROR: no devices found\n");
235 goto leave_with_error
;
238 for (i
= 0; i
< fi_args
.num_devices
; i
++)
239 if (start_args
.start
.srcdevid
== di_args
[i
].devid
)
242 if (i
== fi_args
.num_devices
) {
243 fprintf(stderr
, "Error: '%s' is not a valid devid for filesystem '%s'\n",
245 goto leave_with_error
;
248 fdsrcdev
= open(srcdev
, O_RDWR
);
250 fprintf(stderr
, "Error: Unable to open device '%s'\n",
252 fprintf(stderr
, "\tTry using the devid instead of the path\n");
253 goto leave_with_error
;
255 ret
= fstat(fdsrcdev
, &st
);
257 fprintf(stderr
, "Error: Unable to stat '%s'\n", srcdev
);
258 goto leave_with_error
;
260 if (!S_ISBLK(st
.st_mode
)) {
261 fprintf(stderr
, "Error: '%s' is not a block device\n",
263 goto leave_with_error
;
265 strncpy((char *)start_args
.start
.srcdev_name
, srcdev
,
266 BTRFS_DEVICE_PATH_NAME_MAX
);
269 start_args
.start
.srcdevid
= 0;
272 ret
= test_dev_for_mkfs(dstdev
, force_using_targetdev
, estr
);
274 fprintf(stderr
, "%s", estr
);
275 goto leave_with_error
;
277 fddstdev
= open(dstdev
, O_RDWR
);
279 fprintf(stderr
, "Unable to open %s\n", dstdev
);
280 goto leave_with_error
;
282 strncpy((char *)start_args
.start
.tgtdev_name
, dstdev
,
283 BTRFS_DEVICE_PATH_NAME_MAX
);
284 ret
= btrfs_prepare_device(fddstdev
, dstdev
, 1, &dstdev_block_count
, 0,
287 goto leave_with_error
;
294 dev_replace_handle_sigint(fdmnt
);
295 if (!do_not_background
) {
296 if (daemon(0, 0) < 0) {
297 fprintf(stderr
, "ERROR, backgrounding failed: %s\n",
299 goto leave_with_error
;
303 start_args
.cmd
= BTRFS_IOCTL_DEV_REPLACE_CMD_START
;
304 ret
= ioctl(fdmnt
, BTRFS_IOC_DEV_REPLACE
, &start_args
);
305 if (do_not_background
) {
308 "ERROR: ioctl(DEV_REPLACE_START) failed on \"%s\": %s, %s\n",
309 path
, strerror(errno
),
310 replace_dev_result2string(start_args
.result
));
311 goto leave_with_error
;
314 if (start_args
.result
!=
315 BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR
) {
317 "ERROR: ioctl(DEV_REPLACE_START) on \"%s\" returns error: %s\n",
319 replace_dev_result2string(start_args
.result
));
320 goto leave_with_error
;
323 close_file_or_dir(fdmnt
, dirstream
);
338 static const char *const cmd_status_replace_usage
[] = {
339 "btrfs replace status [-1] <mount_point>",
340 "Print status and progress information of a running device replace",
343 "-1 print once instead of print continuously until the replace",
344 " operation finishes (or is canceled)",
348 static int cmd_status_replace(int argc
, char **argv
)
356 DIR *dirstream
= NULL
;
358 while ((c
= getopt(argc
, argv
, "1")) != -1) {
365 usage(cmd_status_replace_usage
);
369 if (check_argc_exact(argc
- optind
, 1))
370 usage(cmd_status_replace_usage
);
373 fd
= open_file_or_dir(path
, &dirstream
);
376 fprintf(stderr
, "ERROR: can't access \"%s\": %s\n",
381 ret
= print_replace_status(fd
, path
, once
);
382 close_file_or_dir(fd
, dirstream
);
386 static int print_replace_status(int fd
, const char *path
, int once
)
388 struct btrfs_ioctl_dev_replace_args args
= {0};
389 struct btrfs_ioctl_dev_replace_status_params
*status
;
391 int prevent_loop
= 0;
399 args
.cmd
= BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS
;
400 ret
= ioctl(fd
, BTRFS_IOC_DEV_REPLACE
, &args
);
402 fprintf(stderr
, "ERROR: ioctl(DEV_REPLACE_STATUS) failed on \"%s\": %s, %s\n",
403 path
, strerror(errno
),
404 replace_dev_result2string(args
.result
));
408 status
= &args
.status
;
409 if (args
.result
!= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR
) {
410 fprintf(stderr
, "ERROR: ioctl(DEV_REPLACE_STATUS) on \"%s\" returns error: %s\n",
412 replace_dev_result2string(args
.result
));
418 switch (status
->replace_state
) {
419 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED
:
422 progress2string(string3
,
424 status
->progress_1000
));
426 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED
:
428 printf("Started on %s, finished on %s",
429 time2string(string1
, sizeof(string1
),
430 status
->time_started
),
431 time2string(string2
, sizeof(string2
),
432 status
->time_stopped
));
434 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
:
436 printf("Started on %s, canceled on %s at %s",
437 time2string(string1
, sizeof(string1
),
438 status
->time_started
),
439 time2string(string2
, sizeof(string2
),
440 status
->time_stopped
),
441 progress2string(string3
, sizeof(string3
),
442 status
->progress_1000
));
444 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED
:
446 printf("Started on %s, suspended on %s at %s",
447 time2string(string1
, sizeof(string1
),
448 status
->time_started
),
449 time2string(string2
, sizeof(string2
),
450 status
->time_stopped
),
451 progress2string(string3
, sizeof(string3
),
452 status
->progress_1000
));
454 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED
:
457 printf("Never started");
462 "Unknown btrfs dev replace status:%llu",
463 status
->replace_state
);
470 ", %llu write errs, %llu uncorr. read errs",
471 (unsigned long long)status
->num_write_errors
,
473 status
->num_uncorrectable_read_errors
);
474 if (once
|| prevent_loop
|| ret
) {
481 while (num_chars
> 0) {
491 time2string(char *buf
, size_t s
, __u64 t
)
496 t_time_t
= (time_t)t
;
497 assert((__u64
)t_time_t
== t
);
498 localtime_r(&t_time_t
, &t_tm
);
499 strftime(buf
, s
, "%e.%b %T", &t_tm
);
504 progress2string(char *buf
, size_t s
, int progress_1000
)
506 snprintf(buf
, s
, "%d.%01d%%", progress_1000
/ 10, progress_1000
% 10);
512 static const char *const cmd_cancel_replace_usage
[] = {
513 "btrfs replace cancel <mount_point>",
514 "Cancel a running device replace operation.",
518 static int cmd_cancel_replace(int argc
, char **argv
)
520 struct btrfs_ioctl_dev_replace_args args
= {0};
526 DIR *dirstream
= NULL
;
528 while ((c
= getopt(argc
, argv
, "")) != -1) {
532 usage(cmd_cancel_replace_usage
);
536 if (check_argc_exact(argc
- optind
, 1))
537 usage(cmd_cancel_replace_usage
);
540 fd
= open_file_or_dir(path
, &dirstream
);
542 fprintf(stderr
, "ERROR: can't access \"%s\": %s\n",
543 path
, strerror(errno
));
547 args
.cmd
= BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL
;
548 ret
= ioctl(fd
, BTRFS_IOC_DEV_REPLACE
, &args
);
550 close_file_or_dir(fd
, dirstream
);
552 fprintf(stderr
, "ERROR: ioctl(DEV_REPLACE_CANCEL) failed on \"%s\": %s, %s\n",
554 replace_dev_result2string(args
.result
));
557 if (args
.result
== BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED
) {
558 printf("INFO: ioctl(DEV_REPLACE_CANCEL)\"%s\": %s\n",
559 path
, replace_dev_result2string(args
.result
));
565 const struct cmd_group replace_cmd_group
= {
566 replace_cmd_group_usage
, NULL
, {
567 { "start", cmd_start_replace
, cmd_start_replace_usage
, NULL
,
569 { "status", cmd_status_replace
, cmd_status_replace_usage
, NULL
,
571 { "cancel", cmd_cancel_replace
, cmd_cancel_replace_usage
, NULL
,
577 int cmd_replace(int argc
, char **argv
)
579 return handle_command_group(&replace_cmd_group
, argc
, argv
);