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"
41 #include "mkfs/common.h"
43 static int print_replace_status(int fd
, const char *path
, int once
);
44 static char *time2string(char *buf
, size_t s
, __u64 t
);
45 static char *progress2string(char *buf
, size_t s
, int progress_1000
);
48 static const char *replace_dev_result2string(__u64 result
)
51 case BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR
:
53 case BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED
:
55 case BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED
:
56 return "already started";
57 case BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS
:
58 return "scrub is in progress";
60 return "<illegal result value>";
64 static const char * const replace_cmd_group_usage
[] = {
65 "btrfs replace <command> [<args>]",
69 static int dev_replace_cancel_fd
= -1;
70 static void dev_replace_sigint_handler(int signal
)
73 struct btrfs_ioctl_dev_replace_args args
= {0};
75 args
.cmd
= BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL
;
76 ret
= ioctl(dev_replace_cancel_fd
, BTRFS_IOC_DEV_REPLACE
, &args
);
78 perror("Device replace cancel failed");
81 static int dev_replace_handle_sigint(int fd
)
83 struct sigaction sa
= {
84 .sa_handler
= fd
== -1 ? SIG_DFL
: dev_replace_sigint_handler
87 dev_replace_cancel_fd
= fd
;
88 return sigaction(SIGINT
, &sa
, NULL
);
91 static const char *const cmd_replace_start_usage
[] = {
92 "btrfs replace start [-Bfr] <srcdev>|<devid> <targetdev> <mount_point>",
93 "Replace device of a btrfs filesystem.",
94 "On a live filesystem, duplicate the data to the target device which",
95 "is currently stored on the source device. If the source device is not",
96 "available anymore, or if the -r option is set, the data is built",
97 "only using the RAID redundancy mechanisms. After completion of the",
98 "operation, the source device is removed from the filesystem.",
99 "If the <srcdev> is a numerical value, it is assumed to be the device id",
100 "of the filesystem which is mounted at <mount_point>, otherwise it is",
101 "the path to the source device. If the source device is disconnected,",
102 "from the system, you have to use the <devid> parameter format.",
103 "The <targetdev> needs to be same size or larger than the <srcdev>.",
105 "-r only read from <srcdev> if no other zero-defect mirror exists",
106 " (enable this if your drive has lots of read errors, the access",
107 " would be very slow)",
108 "-f force using and overwriting <targetdev> even if it looks like",
109 " containing a valid btrfs filesystem. A valid filesystem is",
110 " assumed if a btrfs superblock is found which contains a",
111 " correct checksum. Devices which are currently mounted are",
112 " never allowed to be used as the <targetdev>",
113 "-B do not background",
117 static int cmd_replace_start(int argc
, char **argv
)
119 struct btrfs_ioctl_dev_replace_args start_args
= {0};
120 struct btrfs_ioctl_dev_replace_args status_args
= {0};
129 int avoid_reading_from_srcdev
= 0;
130 int force_using_targetdev
= 0;
131 u64 dstdev_block_count
;
132 int do_not_background
= 0;
133 DIR *dirstream
= NULL
;
138 while ((c
= getopt(argc
, argv
, "Brf")) != -1) {
141 do_not_background
= 1;
144 avoid_reading_from_srcdev
= 1;
147 force_using_targetdev
= 1;
151 usage(cmd_replace_start_usage
);
155 start_args
.start
.cont_reading_from_srcdev_mode
=
156 avoid_reading_from_srcdev
?
157 BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID
:
158 BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS
;
159 if (check_argc_exact(argc
- optind
, 3))
160 usage(cmd_replace_start_usage
);
161 path
= argv
[optind
+ 2];
163 fdmnt
= open_path_or_dev_mnt(path
, &dirstream
, 1);
165 goto leave_with_error
;
167 /* check for possible errors before backgrounding */
168 status_args
.cmd
= BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS
;
169 status_args
.result
= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT
;
170 ret
= ioctl(fdmnt
, BTRFS_IOC_DEV_REPLACE
, &status_args
);
173 "ERROR: ioctl(DEV_REPLACE_STATUS) failed on \"%s\": %m",
175 if (status_args
.result
!= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT
)
176 fprintf(stderr
, ", %s\n",
177 replace_dev_result2string(status_args
.result
));
179 fprintf(stderr
, "\n");
180 goto leave_with_error
;
183 if (status_args
.result
!= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR
) {
184 error("ioctl(DEV_REPLACE_STATUS) on '%s' returns error: %s",
185 path
, replace_dev_result2string(status_args
.result
));
186 goto leave_with_error
;
189 if (status_args
.status
.replace_state
==
190 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED
) {
191 error("device replace on '%s' already started", path
);
192 goto leave_with_error
;
195 srcdev
= argv
[optind
];
196 dstdev
= canonicalize_path(argv
[optind
+ 1]);
198 error("cannot canonicalize path '%s': %m",
200 goto leave_with_error
;
203 if (string_is_numerical(srcdev
)) {
204 struct btrfs_ioctl_fs_info_args fi_args
;
205 struct btrfs_ioctl_dev_info_args
*di_args
= NULL
;
207 start_args
.start
.srcdevid
= arg_strtou64(srcdev
);
209 ret
= get_fs_info(path
, &fi_args
, &di_args
);
211 error("failed to get device info: %s", strerror(-ret
));
213 goto leave_with_error
;
215 if (!fi_args
.num_devices
) {
216 error("no devices found");
218 goto leave_with_error
;
221 for (i
= 0; i
< fi_args
.num_devices
; i
++)
222 if (start_args
.start
.srcdevid
== di_args
[i
].devid
)
224 srcdev_size
= di_args
[i
].total_bytes
;
226 if (i
== fi_args
.num_devices
) {
227 error("'%s' is not a valid devid for filesystem '%s'",
229 goto leave_with_error
;
231 } else if (is_block_device(srcdev
) > 0) {
232 strncpy((char *)start_args
.start
.srcdev_name
, srcdev
,
233 BTRFS_DEVICE_PATH_NAME_MAX
);
234 start_args
.start
.srcdevid
= 0;
235 srcdev_size
= get_partition_size(srcdev
);
237 error("source device must be a block device or a devid");
238 goto leave_with_error
;
241 ret
= test_dev_for_mkfs(dstdev
, force_using_targetdev
);
243 goto leave_with_error
;
245 dstdev_size
= get_partition_size(dstdev
);
246 if (srcdev_size
> dstdev_size
) {
247 error("target device smaller than source device (required %llu bytes)",
249 goto leave_with_error
;
252 fddstdev
= open(dstdev
, O_RDWR
);
254 error("unable to open %s: %m", dstdev
);
255 goto leave_with_error
;
257 strncpy((char *)start_args
.start
.tgtdev_name
, dstdev
,
258 BTRFS_DEVICE_PATH_NAME_MAX
);
259 ret
= btrfs_prepare_device(fddstdev
, dstdev
, &dstdev_block_count
, 0,
260 PREP_DEVICE_ZERO_END
| PREP_DEVICE_VERBOSE
);
262 goto leave_with_error
;
269 dev_replace_handle_sigint(fdmnt
);
270 if (!do_not_background
) {
271 if (daemon(0, 0) < 0) {
272 error("backgrounding failed: %m");
273 goto leave_with_error
;
277 start_args
.cmd
= BTRFS_IOCTL_DEV_REPLACE_CMD_START
;
278 start_args
.result
= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT
;
279 ret
= ioctl(fdmnt
, BTRFS_IOC_DEV_REPLACE
, &start_args
);
280 if (do_not_background
) {
283 "ERROR: ioctl(DEV_REPLACE_START) failed on \"%s\": %m",
285 if (start_args
.result
!= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT
)
286 fprintf(stderr
, ", %s\n",
287 replace_dev_result2string(start_args
.result
));
289 fprintf(stderr
, "\n");
291 if (errno
== EOPNOTSUPP
)
292 warning("device replace of RAID5/6 not supported with this kernel");
294 goto leave_with_error
;
297 if (start_args
.result
!=
298 BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR
) {
299 error("ioctl(DEV_REPLACE_START) on '%s' returns error: %s",
301 replace_dev_result2string(start_args
.result
));
302 goto leave_with_error
;
305 close_file_or_dir(fdmnt
, dirstream
);
318 static const char *const cmd_replace_status_usage
[] = {
319 "btrfs replace status [-1] <mount_point>",
320 "Print status and progress information of a running device replace",
323 "-1 print once instead of print continuously until the replace",
324 " operation finishes (or is canceled)",
328 static int cmd_replace_status(int argc
, char **argv
)
335 DIR *dirstream
= NULL
;
338 while ((c
= getopt(argc
, argv
, "1")) != -1) {
345 usage(cmd_replace_status_usage
);
349 if (check_argc_exact(argc
- optind
, 1))
350 usage(cmd_replace_status_usage
);
353 fd
= btrfs_open_dir(path
, &dirstream
, 1);
357 ret
= print_replace_status(fd
, path
, once
);
358 close_file_or_dir(fd
, dirstream
);
362 static int print_replace_status(int fd
, const char *path
, int once
)
364 struct btrfs_ioctl_dev_replace_args args
= {0};
365 struct btrfs_ioctl_dev_replace_status_params
*status
;
367 int prevent_loop
= 0;
375 args
.cmd
= BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS
;
376 args
.result
= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT
;
377 ret
= ioctl(fd
, BTRFS_IOC_DEV_REPLACE
, &args
);
379 fprintf(stderr
, "ERROR: ioctl(DEV_REPLACE_STATUS) failed on \"%s\": %m",
381 if (args
.result
!= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT
)
382 fprintf(stderr
, ", %s\n",
383 replace_dev_result2string(args
.result
));
385 fprintf(stderr
, "\n");
389 if (args
.result
!= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR
) {
390 error("ioctl(DEV_REPLACE_STATUS) on '%s' returns error: %s",
392 replace_dev_result2string(args
.result
));
396 status
= &args
.status
;
400 switch (status
->replace_state
) {
401 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED
:
404 progress2string(string3
,
406 status
->progress_1000
));
408 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED
:
410 printf("Started on %s, finished on %s",
411 time2string(string1
, sizeof(string1
),
412 status
->time_started
),
413 time2string(string2
, sizeof(string2
),
414 status
->time_stopped
));
416 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
:
418 printf("Started on %s, canceled on %s at %s",
419 time2string(string1
, sizeof(string1
),
420 status
->time_started
),
421 time2string(string2
, sizeof(string2
),
422 status
->time_stopped
),
423 progress2string(string3
, sizeof(string3
),
424 status
->progress_1000
));
426 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED
:
428 printf("Started on %s, suspended on %s at %s",
429 time2string(string1
, sizeof(string1
),
430 status
->time_started
),
431 time2string(string2
, sizeof(string2
),
432 status
->time_stopped
),
433 progress2string(string3
, sizeof(string3
),
434 status
->progress_1000
));
436 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED
:
439 printf("Never started");
442 error("unknown status from ioctl DEV_REPLACE_STATUS on '%s': %llu",
443 path
, status
->replace_state
);
449 ", %llu write errs, %llu uncorr. read errs",
450 (unsigned long long)status
->num_write_errors
,
452 status
->num_uncorrectable_read_errors
);
453 if (once
|| prevent_loop
) {
460 while (num_chars
> 0) {
470 time2string(char *buf
, size_t s
, __u64 t
)
475 t_time_t
= (time_t)t
;
476 assert((__u64
)t_time_t
== t
);
477 localtime_r(&t_time_t
, &t_tm
);
478 strftime(buf
, s
, "%e.%b %T", &t_tm
);
483 progress2string(char *buf
, size_t s
, int progress_1000
)
485 snprintf(buf
, s
, "%d.%01d%%", progress_1000
/ 10, progress_1000
% 10);
491 static const char *const cmd_replace_cancel_usage
[] = {
492 "btrfs replace cancel <mount_point>",
493 "Cancel a running device replace operation.",
497 static int cmd_replace_cancel(int argc
, char **argv
)
499 struct btrfs_ioctl_dev_replace_args args
= {0};
504 DIR *dirstream
= NULL
;
507 while ((c
= getopt(argc
, argv
, "")) != -1) {
511 usage(cmd_replace_cancel_usage
);
515 if (check_argc_exact(argc
- optind
, 1))
516 usage(cmd_replace_cancel_usage
);
519 fd
= btrfs_open_dir(path
, &dirstream
, 1);
523 args
.cmd
= BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL
;
524 args
.result
= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT
;
525 ret
= ioctl(fd
, BTRFS_IOC_DEV_REPLACE
, &args
);
526 close_file_or_dir(fd
, dirstream
);
528 fprintf(stderr
, "ERROR: ioctl(DEV_REPLACE_CANCEL) failed on \"%s\": %m",
530 if (args
.result
!= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT
)
531 fprintf(stderr
, ", %s\n",
532 replace_dev_result2string(args
.result
));
534 fprintf(stderr
, "\n");
537 if (args
.result
== BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED
) {
538 printf("INFO: ioctl(DEV_REPLACE_CANCEL)\"%s\": %s\n",
539 path
, replace_dev_result2string(args
.result
));
545 static const char replace_cmd_group_info
[] =
546 "replace a device in the filesystem";
548 const struct cmd_group replace_cmd_group
= {
549 replace_cmd_group_usage
, replace_cmd_group_info
, {
550 { "start", cmd_replace_start
, cmd_replace_start_usage
, NULL
,
552 { "status", cmd_replace_status
, cmd_replace_status_usage
, NULL
,
554 { "cancel", cmd_replace_cancel
, cmd_replace_cancel_usage
, NULL
,
560 int cmd_replace(int argc
, char **argv
)
562 return handle_command_group(&replace_cmd_group
, argc
, argv
);