btrfs-progs: tests: add shell quotes to mkfs test scripts
[platform/upstream/btrfs-progs.git] / cmds-replace.c
index 1ff028a..032a44f 100644 (file)
@@ -37,7 +37,8 @@
 #include "disk-io.h"
 
 #include "commands.h"
-
+#include "help.h"
+#include "mkfs/common.h"
 
 static int print_replace_status(int fd, const char *path, int once);
 static char *time2string(char *buf, size_t s, __u64 t);
@@ -166,10 +167,10 @@ static int cmd_replace_start(int argc, char **argv)
        status_args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS;
        status_args.result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT;
        ret = ioctl(fdmnt, BTRFS_IOC_DEV_REPLACE, &status_args);
-       if (ret) {
+       if (ret < 0) {
                fprintf(stderr,
-                       "ERROR: ioctl(DEV_REPLACE_STATUS) failed on \"%s\": %s",
-                       path, strerror(errno));
+                       "ERROR: ioctl(DEV_REPLACE_STATUS) failed on \"%s\": %m",
+                       path);
                if (status_args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT)
                        fprintf(stderr, ", %s\n",
                                replace_dev_result2string(status_args.result));
@@ -193,8 +194,8 @@ static int cmd_replace_start(int argc, char **argv)
        srcdev = argv[optind];
        dstdev = canonicalize_path(argv[optind + 1]);
        if (!dstdev) {
-               error("cannot canonicalize path '%s': %s",
-                       argv[optind + 1], strerror(errno));
+               error("cannot canonicalize path '%s': %m",
+                       argv[optind + 1]);
                goto leave_with_error;
        }
 
@@ -249,13 +250,13 @@ static int cmd_replace_start(int argc, char **argv)
 
        fddstdev = open(dstdev, O_RDWR);
        if (fddstdev < 0) {
-               error("unable to open %s: %s", dstdev, strerror(errno));
+               error("unable to open %s: %m", dstdev);
                goto leave_with_error;
        }
        strncpy((char *)start_args.start.tgtdev_name, dstdev,
                BTRFS_DEVICE_PATH_NAME_MAX);
-       ret = btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0,
-                               0);
+       ret = btrfs_prepare_device(fddstdev, dstdev, &dstdev_block_count, 0,
+                       PREP_DEVICE_ZERO_END | PREP_DEVICE_VERBOSE);
        if (ret)
                goto leave_with_error;
 
@@ -267,7 +268,7 @@ static int cmd_replace_start(int argc, char **argv)
        dev_replace_handle_sigint(fdmnt);
        if (!do_not_background) {
                if (daemon(0, 0) < 0) {
-                       error("backgrounding failed: %s", strerror(errno));
+                       error("backgrounding failed: %m");
                        goto leave_with_error;
                }
        }
@@ -276,10 +277,10 @@ static int cmd_replace_start(int argc, char **argv)
        start_args.result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT;
        ret = ioctl(fdmnt, BTRFS_IOC_DEV_REPLACE, &start_args);
        if (do_not_background) {
-               if (ret) {
+               if (ret < 0) {
                        fprintf(stderr,
-                               "ERROR: ioctl(DEV_REPLACE_START) failed on \"%s\": %s",
-                               path, strerror(errno));
+                               "ERROR: ioctl(DEV_REPLACE_START) failed on \"%s\": %m",
+                               path);
                        if (start_args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT)
                                fprintf(stderr, ", %s\n",
                                        replace_dev_result2string(start_args.result));
@@ -372,9 +373,9 @@ static int print_replace_status(int fd, const char *path, int once)
                args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS;
                args.result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT;
                ret = ioctl(fd, BTRFS_IOC_DEV_REPLACE, &args);
-               if (ret) {
-                       fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_STATUS) failed on \"%s\": %s",
-                               path, strerror(errno));
+               if (ret < 0) {
+                       fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_STATUS) failed on \"%s\": %m",
+                               path);
                        if (args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT)
                                fprintf(stderr, ", %s\n",
                                        replace_dev_result2string(args.result));
@@ -436,7 +437,7 @@ static int print_replace_status(int fd, const char *path, int once)
                        printf("Never started");
                        break;
                default:
-                       error("unknown status from ioctl DEV_REPLACE_STATUS on '%s': %llu\n",
+                       error("unknown status from ioctl DEV_REPLACE_STATUS on '%s': %llu",
                                        path, status->replace_state);
                        return -EINVAL;
                }
@@ -497,7 +498,6 @@ static int cmd_replace_cancel(int argc, char **argv)
        int ret;
        int c;
        int fd;
-       int e;
        char *path;
        DIR *dirstream = NULL;
 
@@ -520,11 +520,10 @@ static int cmd_replace_cancel(int argc, char **argv)
        args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL;
        args.result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT;
        ret = ioctl(fd, BTRFS_IOC_DEV_REPLACE, &args);
-       e = errno;
        close_file_or_dir(fd, dirstream);
-       if (ret) {
-               fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_CANCEL) failed on \"%s\": %s",
-                       path, strerror(e));
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_CANCEL) failed on \"%s\": %m",
+                       path);
                if (args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT)
                        fprintf(stderr, ", %s\n",
                                replace_dev_result2string(args.result));