a7b9285
From 163a01830d0dbdb57f332915ebe298f86be84cb0 Mon Sep 17 00:00:00 2001
Alon Levy 408bdb5
From: Paolo Bonzini <pbonzini@redhat.com>
Alon Levy 408bdb5
Date: Fri, 22 Feb 2013 17:36:41 +0100
a7b9285
Subject: [PATCH] migration: use QEMUFile for migration channel lifetime
Alon Levy 408bdb5
Alon Levy 408bdb5
As a start, use QEMUFile to store the destination and close it.
Alon Levy 408bdb5
qemu_get_fd gets a file descriptor that will be used by the write
Alon Levy 408bdb5
callbacks.
Alon Levy 408bdb5
Alon Levy 408bdb5
Reviewed-by: Orit Wasserman <owasserm@redhat.com>
Alon Levy 408bdb5
Reviewed-by: Juan Quintela <quintela@redhat.com>
Alon Levy 408bdb5
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Alon Levy 408bdb5
Signed-off-by: Juan Quintela <quintela@redhat.com>
a7b9285
(cherry picked from commit f8bbc1286337a8506162b5785babe6f2a7de2476)
a7b9285
a7b9285
Conflicts:
a7b9285
	migration-tcp.c
a7b9285
	migration-unix.c
a7b9285
	savevm.c
Alon Levy 408bdb5
---
Alon Levy 408bdb5
 include/migration/migration.h |  7 ++++---
Alon Levy 408bdb5
 migration-exec.c              | 21 ++-------------------
Alon Levy 408bdb5
 migration-fd.c                | 35 +++--------------------------------
Alon Levy 408bdb5
 migration-tcp.c               | 19 +++----------------
Alon Levy 408bdb5
 migration-unix.c              | 19 +++----------------
Alon Levy 408bdb5
 migration.c                   |  8 +++++---
Alon Levy 408bdb5
 savevm.c                      |  1 +
Alon Levy 408bdb5
 7 files changed, 21 insertions(+), 89 deletions(-)
Alon Levy 408bdb5
Alon Levy 408bdb5
diff --git a/include/migration/migration.h b/include/migration/migration.h
Alon Levy 408bdb5
index cec8643..1f8f305 100644
Alon Levy 408bdb5
--- a/include/migration/migration.h
Alon Levy 408bdb5
+++ b/include/migration/migration.h
Alon Levy 408bdb5
@@ -38,12 +38,13 @@ struct MigrationState
Alon Levy 408bdb5
     QEMUBH *cleanup_bh;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     QEMUFile *file;
Alon Levy 408bdb5
+    QEMUFile *migration_file;
Alon Levy 408bdb5
+
Alon Levy 408bdb5
     int fd;
Alon Levy 408bdb5
-    int state;
Alon Levy 408bdb5
     int (*get_error)(MigrationState *s);
Alon Levy 408bdb5
-    int (*close)(MigrationState *s);
Alon Levy 408bdb5
     int (*write)(MigrationState *s, const void *buff, size_t size);
Alon Levy 408bdb5
-    void *opaque;
Alon Levy 408bdb5
+
Alon Levy 408bdb5
+    int state;
Alon Levy 408bdb5
     MigrationParams params;
Alon Levy 408bdb5
     int64_t total_time;
Alon Levy 408bdb5
     int64_t downtime;
Alon Levy 408bdb5
diff --git a/migration-exec.c b/migration-exec.c
Alon Levy 408bdb5
index a2b5f8d..8c3f720 100644
Alon Levy 408bdb5
--- a/migration-exec.c
Alon Levy 408bdb5
+++ b/migration-exec.c
Alon Levy 408bdb5
@@ -43,33 +43,16 @@ static int file_write(MigrationState *s, const void * buf, size_t size)
Alon Levy 408bdb5
     return write(s->fd, buf, size);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-static int exec_close(MigrationState *s)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    int ret = 0;
Alon Levy 408bdb5
-    DPRINTF("exec_close\n");
Alon Levy 408bdb5
-    ret = qemu_fclose(s->opaque);
Alon Levy 408bdb5
-    s->opaque = NULL;
Alon Levy 408bdb5
-    s->fd = -1;
Alon Levy 408bdb5
-    return ret;
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
 void exec_start_outgoing_migration(MigrationState *s, const char *command, Error **errp)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
-    QEMUFile *f;
Alon Levy 408bdb5
-    f = qemu_popen_cmd(command, "w");
Alon Levy 408bdb5
-    if (f == NULL) {
Alon Levy 408bdb5
+    s->migration_file = qemu_popen_cmd(command, "w");
Alon Levy 408bdb5
+    if (s->migration_file == NULL) {
Alon Levy 408bdb5
         error_setg_errno(errp, errno, "failed to popen the migration target");
Alon Levy 408bdb5
         return;
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-    s->opaque = f;
Alon Levy 408bdb5
-    s->fd = qemu_get_fd(f);
Alon Levy 408bdb5
-    assert(s->fd != -1);
Alon Levy 408bdb5
-
Alon Levy 408bdb5
-    s->close = exec_close;
Alon Levy 408bdb5
     s->get_error = file_errno;
Alon Levy 408bdb5
     s->write = file_write;
Alon Levy 408bdb5
-
Alon Levy 408bdb5
     migrate_fd_connect(s);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
diff --git a/migration-fd.c b/migration-fd.c
Alon Levy 408bdb5
index a99e0e3..4636457 100644
Alon Levy 408bdb5
--- a/migration-fd.c
Alon Levy 408bdb5
+++ b/migration-fd.c
Alon Levy 408bdb5
@@ -40,45 +40,16 @@ static int fd_write(MigrationState *s, const void * buf, size_t size)
Alon Levy 408bdb5
     return write(s->fd, buf, size);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-static int fd_close(MigrationState *s)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    struct stat st;
Alon Levy 408bdb5
-    int ret;
Alon Levy 408bdb5
-
Alon Levy 408bdb5
-    DPRINTF("fd_close\n");
Alon Levy 408bdb5
-    ret = fstat(s->fd, &st);
Alon Levy 408bdb5
-    if (ret == 0 && S_ISREG(st.st_mode)) {
Alon Levy 408bdb5
-        /*
Alon Levy 408bdb5
-         * If the file handle is a regular file make sure the
Alon Levy 408bdb5
-         * data is flushed to disk before signaling success.
Alon Levy 408bdb5
-         */
Alon Levy 408bdb5
-        ret = fsync(s->fd);
Alon Levy 408bdb5
-        if (ret != 0) {
Alon Levy 408bdb5
-            ret = -errno;
Alon Levy 408bdb5
-            perror("migration-fd: fsync");
Alon Levy 408bdb5
-            return ret;
Alon Levy 408bdb5
-        }
Alon Levy 408bdb5
-    }
Alon Levy 408bdb5
-    ret = close(s->fd);
Alon Levy 408bdb5
-    s->fd = -1;
Alon Levy 408bdb5
-    if (ret != 0) {
Alon Levy 408bdb5
-        ret = -errno;
Alon Levy 408bdb5
-        perror("migration-fd: close");
Alon Levy 408bdb5
-    }
Alon Levy 408bdb5
-    return ret;
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
 void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
-    s->fd = monitor_get_fd(cur_mon, fdname, errp);
Alon Levy 408bdb5
-    if (s->fd == -1) {
Alon Levy 408bdb5
+    int fd = monitor_get_fd(cur_mon, fdname, errp);
Alon Levy 408bdb5
+    if (fd == -1) {
Alon Levy 408bdb5
         return;
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
+    s->migration_file = qemu_fdopen(fd, "wb");
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     s->get_error = fd_errno;
Alon Levy 408bdb5
     s->write = fd_write;
Alon Levy 408bdb5
-    s->close = fd_close;
Alon Levy 408bdb5
-
Alon Levy 408bdb5
     migrate_fd_connect(s);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
diff --git a/migration-tcp.c b/migration-tcp.c
Alon Levy 408bdb5
index a748195..1e8e004 100644
Alon Levy 408bdb5
--- a/migration-tcp.c
Alon Levy 408bdb5
+++ b/migration-tcp.c
Alon Levy 408bdb5
@@ -39,28 +39,17 @@ static int socket_write(MigrationState *s, const void * buf, size_t size)
Alon Levy 408bdb5
     return send(s->fd, buf, size, 0);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-static int tcp_close(MigrationState *s)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    int r = 0;
Alon Levy 408bdb5
-    DPRINTF("tcp_close\n");
Alon Levy 408bdb5
-    if (closesocket(s->fd) < 0) {
Alon Levy 408bdb5
-        r = -socket_error();
Alon Levy 408bdb5
-    }
Alon Levy 408bdb5
-    return r;
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
 static void tcp_wait_for_connect(int fd, void *opaque)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
     MigrationState *s = opaque;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     if (fd < 0) {
Alon Levy 408bdb5
         DPRINTF("migrate connect error\n");
Alon Levy 408bdb5
-        s->fd = -1;
Alon Levy 408bdb5
+        s->migration_file = NULL;
Alon Levy 408bdb5
         migrate_fd_error(s);
Alon Levy 408bdb5
     } else {
Alon Levy 408bdb5
         DPRINTF("migrate connect success\n");
Alon Levy 408bdb5
-        s->fd = fd;
Alon Levy 408bdb5
-        qemu_set_block(s->fd);
Alon Levy 408bdb5
+        s->migration_file = qemu_fopen_socket(fd, "wb");
Alon Levy 408bdb5
         migrate_fd_connect(s);
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
@@ -69,9 +58,7 @@ void tcp_start_outgoing_migration(MigrationState *s, const char *host_port, Erro
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
     s->get_error = socket_errno;
Alon Levy 408bdb5
     s->write = socket_write;
Alon Levy 408bdb5
-    s->close = tcp_close;
Alon Levy 408bdb5
-
Alon Levy 408bdb5
-    s->fd = inet_nonblocking_connect(host_port, tcp_wait_for_connect, s, errp);
Alon Levy 408bdb5
+    inet_nonblocking_connect(host_port, tcp_wait_for_connect, s, errp);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
 static void tcp_accept_incoming_migration(void *opaque)
Alon Levy 408bdb5
diff --git a/migration-unix.c b/migration-unix.c
Alon Levy 408bdb5
index d078f43..11917f4 100644
Alon Levy 408bdb5
--- a/migration-unix.c
Alon Levy 408bdb5
+++ b/migration-unix.c
Alon Levy 408bdb5
@@ -39,28 +39,17 @@ static int unix_write(MigrationState *s, const void * buf, size_t size)
Alon Levy 408bdb5
     return write(s->fd, buf, size);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-static int unix_close(MigrationState *s)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    int r = 0;
Alon Levy 408bdb5
-    DPRINTF("unix_close\n");
Alon Levy 408bdb5
-    if (close(s->fd) < 0) {
Alon Levy 408bdb5
-        r = -errno;
Alon Levy 408bdb5
-    }
Alon Levy 408bdb5
-    return r;
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
 static void unix_wait_for_connect(int fd, void *opaque)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
     MigrationState *s = opaque;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     if (fd < 0) {
Alon Levy 408bdb5
         DPRINTF("migrate connect error\n");
Alon Levy 408bdb5
-        s->fd = -1;
Alon Levy 408bdb5
+        s->migration_file = NULL;
Alon Levy 408bdb5
         migrate_fd_error(s);
Alon Levy 408bdb5
     } else {
Alon Levy 408bdb5
         DPRINTF("migrate connect success\n");
Alon Levy 408bdb5
-        s->fd = fd;
Alon Levy 408bdb5
-        qemu_set_block(s->fd);
Alon Levy 408bdb5
+        s->migration_file = qemu_fopen_socket(fd, "wb");
Alon Levy 408bdb5
         migrate_fd_connect(s);
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
@@ -69,9 +58,7 @@ void unix_start_outgoing_migration(MigrationState *s, const char *path, Error **
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
     s->get_error = unix_errno;
Alon Levy 408bdb5
     s->write = unix_write;
Alon Levy 408bdb5
-    s->close = unix_close;
Alon Levy 408bdb5
-
Alon Levy 408bdb5
-    s->fd = unix_nonblocking_connect(path, unix_wait_for_connect, s, errp);
Alon Levy 408bdb5
+    unix_nonblocking_connect(path, unix_wait_for_connect, s, errp);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
 static void unix_accept_incoming_migration(void *opaque)
Alon Levy 408bdb5
diff --git a/migration.c b/migration.c
Alon Levy 408bdb5
index ae2c83c..fa630ed 100644
Alon Levy 408bdb5
--- a/migration.c
Alon Levy 408bdb5
+++ b/migration.c
Alon Levy 408bdb5
@@ -274,7 +274,7 @@ static void migrate_fd_cleanup(void *opaque)
Alon Levy 408bdb5
         s->file = NULL;
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-    assert(s->fd == -1);
Alon Levy 408bdb5
+    assert(s->migration_file == NULL);
Alon Levy 408bdb5
     assert(s->state != MIG_STATE_ACTIVE);
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     if (s->state != MIG_STATE_COMPLETED) {
Alon Levy 408bdb5
@@ -330,8 +330,9 @@ static void migrate_fd_cancel(MigrationState *s)
Alon Levy 408bdb5
 int migrate_fd_close(MigrationState *s)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
     int rc = 0;
Alon Levy 408bdb5
-    if (s->fd != -1) {
Alon Levy 408bdb5
-        rc = s->close(s);
Alon Levy 408bdb5
+    if (s->migration_file != NULL) {
Alon Levy 408bdb5
+        rc = qemu_fclose(s->migration_file);
Alon Levy 408bdb5
+        s->migration_file = NULL;
Alon Levy 408bdb5
         s->fd = -1;
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
     return rc;
Alon Levy 408bdb5
@@ -720,6 +721,7 @@ void migrate_fd_connect(MigrationState *s)
Alon Levy 408bdb5
     s->xfer_limit = s->bandwidth_limit / XFER_LIMIT_RATIO;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Alon Levy 408bdb5
+    s->fd = qemu_get_fd(s->migration_file);
Alon Levy 408bdb5
     s->file = qemu_fopen_ops(s, &migration_file_ops);
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     qemu_thread_create(&s->thread, migration_thread, s,
Alon Levy 408bdb5
diff --git a/savevm.c b/savevm.c
Alon Levy 408bdb5
index c60ace3..43963ce 100644
Alon Levy 408bdb5
--- a/savevm.c
Alon Levy 408bdb5
+++ b/savevm.c
Alon Levy 408bdb5
@@ -400,6 +400,7 @@ QEMUFile *qemu_fopen_socket(int fd, const char *mode)
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     s->fd = fd;
Alon Levy 408bdb5
     if (mode[0] == 'w') {
Alon Levy 408bdb5
+        qemu_set_block(s->fd);
Alon Levy 408bdb5
         s->file = qemu_fopen_ops(s, &socket_write_ops);
Alon Levy 408bdb5
     } else {
Alon Levy 408bdb5
         s->file = qemu_fopen_ops(s, &socket_read_ops);