Use ETA in progress report.
[platform/upstream/cryptsetup.git] / src / crypt_reencrypt.c
index 07cbbf3..3154d59 100644 (file)
@@ -53,6 +53,7 @@ static int opt_random = 0;
 static int opt_urandom = 0;
 static int opt_bsize = 4;
 static int opt_directio = 0;
+static int opt_fsync = 0;
 static int opt_write_log = 0;
 static int opt_tries = 3;
 static int opt_key_slot = CRYPT_ANY_SLOT;
@@ -288,18 +289,17 @@ static int create_empty_header(const char *new_file, const char *old_file)
 
 static int write_log(struct reenc_ctx *rc)
 {
-       static char buf[SECTOR_SIZE];
        ssize_t r;
 
-       memset(buf, 0, SECTOR_SIZE);
-       snprintf(buf, SECTOR_SIZE, "# LUKS reencryption log, DO NOT EDIT OR DELETE.\n"
+       memset(rc->log_buf, 0, SECTOR_SIZE);
+       snprintf(rc->log_buf, SECTOR_SIZE, "# LUKS reencryption log, DO NOT EDIT OR DELETE.\n"
                "version = %d\nUUID = %s\ndirection = %d\n"
                "offset = %" PRIu64 "\nshift = %" PRIu64 "\n# EOF\n",
                1, rc->device_uuid, rc->reencrypt_direction,
                rc->device_offset, rc->device_shift);
 
        lseek(rc->log_fd, 0, SEEK_SET);
-       r = write(rc->log_fd, buf, SECTOR_SIZE);
+       r = write(rc->log_fd, rc->log_buf, SECTOR_SIZE);
        if (r < 0 || r != SECTOR_SIZE) {
                log_err(_("Cannot write reencryption log file.\n"));
                return -EIO;
@@ -383,17 +383,22 @@ static void close_log(struct reenc_ctx *rc)
 
 static int open_log(struct reenc_ctx *rc)
 {
-       int flags;
+       int flags, create_new;
        struct stat st;
 
-       if(stat(rc->log_file, &st) < 0) {
+       if (!stat(rc->log_file, &st))
+               create_new = 0;
+       else if (errno == ENOENT)
+               create_new = 1;
+       else
+               return -EINVAL;
+
+       if (create_new) {
                log_dbg("Creating LUKS reencryption log file %s.", rc->log_file);
                flags = opt_directio ? O_RDWR|O_CREAT|O_DIRECT : O_RDWR|O_CREAT;
                rc->log_fd = open(rc->log_file, flags, S_IRUSR|S_IWUSR);
                if (rc->log_fd == -1)
                        return -EINVAL;
-               if (write_log(rc) < 0)
-                       return -EIO;
        } else {
                log_dbg("Log file %s exists, restarting.", rc->log_file);
                flags = opt_directio ? O_RDWR|O_DIRECT : O_RDWR;
@@ -409,6 +414,11 @@ static int open_log(struct reenc_ctx *rc)
                return -ENOMEM;
        }
 
+       if (create_new && write_log(rc) < 0) {
+               close_log(rc);
+               return -EIO;
+       }
+
        /* Be sure it is correct format */
        return parse_log(rc);
 }
@@ -554,9 +564,9 @@ static int restore_luks_header(struct reenc_ctx *rc)
 
 static void print_progress(struct reenc_ctx *rc, uint64_t bytes, int final)
 {
-       uint64_t mbytes = (bytes - rc->restart_bytes) / 1024 / 1024;
+       unsigned long long mbytes, eta;
        struct timeval now_time;
-       double tdiff;
+       double tdiff, mib;
 
        gettimeofday(&now_time, NULL);
        if (!final && time_diff(rc->end_time, now_time) < 0.5)
@@ -571,13 +581,19 @@ static void print_progress(struct reenc_ctx *rc, uint64_t bytes, int final)
        if (!tdiff)
                return;
 
+       mbytes = (bytes - rc->restart_bytes) / 1024 / 1024;
+       mib = (double)(mbytes) / tdiff;
+       if (!mib)
+               return;
+
+       eta = (unsigned long long)(rc->device_size / 1024 / 1024 / mib - tdiff);
+
        /* vt100 code clear line */
        log_err("\33[2K\r");
-       log_err(_("Progress: %5.1f%%, time elapsed %4.0f seconds, "
+       log_err(_("Progress: %5.1f%%, ETA %02llu:%02llu, "
                "%4llu MiB written, speed %5.1f MiB/s%s"),
                (double)bytes / rc->device_size * 100,
-               time_diff(rc->start_time, rc->end_time),
-               (unsigned long long)mbytes, (double)(mbytes) / tdiff,
+               eta / 60, eta % 60, mbytes, mib,
                final ? "\n" :"");
 }
 
@@ -607,16 +623,23 @@ static int copy_data_forward(struct reenc_ctx *rc, int fd_old, int fd_new,
                                (int)block_size, (int)s1);
                        return -EIO;
                }
+
                s2 = write(fd_new, buf, s1);
                if (s2 < 0) {
                        log_dbg("Write error, expecting %d, got %d.",
                                (int)block_size, (int)s2);
                        return -EIO;
                }
+
                rc->device_offset += s1;
                if (opt_write_log && write_log(rc) < 0)
                        return -EIO;
 
+               if (opt_fsync && fsync(fd_new) < 0) {
+                       log_dbg("Write error, fsync.");
+                       return -EIO;
+               }
+
                *bytes += (uint64_t)s2;
                print_progress(rc, *bytes, 0);
        }
@@ -665,16 +688,23 @@ static int copy_data_backward(struct reenc_ctx *rc, int fd_old, int fd_new,
                                (int)block_size, (int)s1);
                        return -EIO;
                }
+
                s2 = write(fd_new, buf, working_block);
                if (s2 < 0) {
                        log_dbg("Write error, expecting %d, got %d.",
                                (int)block_size, (int)s2);
                        return -EIO;
                }
+
                rc->device_offset -= s1;
                if (opt_write_log && write_log(rc) < 0)
                        return -EIO;
 
+               if (opt_fsync && fsync(fd_new) < 0) {
+                       log_dbg("Write error, fsync.");
+                       return -EIO;
+               }
+
                *bytes += (uint64_t)s2;
                print_progress(rc, *bytes, 0);
        }
@@ -1035,6 +1065,7 @@ int main(int argc, const char **argv)
                { "use-random",        '\0', POPT_ARG_NONE, &opt_random,                0, N_("Use /dev/random for generating volume key."), NULL },
                { "use-urandom",       '\0', POPT_ARG_NONE, &opt_urandom,               0, N_("Use /dev/urandom for generating volume key."), NULL },
                { "use-directio",      '\0', POPT_ARG_NONE, &opt_directio,              0, N_("Use direct-io when accesing devices."), NULL },
+               { "use-fsync",         '\0', POPT_ARG_NONE, &opt_fsync,                 0, N_("Use fsync after each block."), NULL },
                { "write-log",         '\0', POPT_ARG_NONE, &opt_write_log,             0, N_("Update log file after every block."), NULL },
                { "key-slot",          'S',  POPT_ARG_INT, &opt_key_slot,               0, N_("Use only this slot (others will be disabled)."), NULL },
                { "keyfile-offset",   '\0',  POPT_ARG_LONG, &opt_keyfile_offset,        0, N_("Number of bytes to skip in keyfile"), N_("bytes") },