cd9d161
From 06400ebc136bf44f1fa423159fae9cc9a4f6839d Mon Sep 17 00:00:00 2001
cd9d161
From: Gerd Hoffmann <kraxel@redhat.com>
cd9d161
Date: Thu, 27 Oct 2011 09:12:04 +0200
cd9d161
Subject: [PATCH] migration: flush migration data to disk.
cd9d161
MIME-Version: 1.0
cd9d161
Content-Type: text/plain; charset=UTF-8
cd9d161
Content-Transfer-Encoding: 8bit
cd9d161
cd9d161
This patch increases robustness when migrating to a file with
cd9d161
two little changes:
cd9d161
cd9d161
 (1) Before closing the migration file handle checks if it happens to be
cd9d161
     a regular file and if so it issues a fsync.  This way the data is
cd9d161
     flushed to disk before qemu sends the migration completed event.
cd9d161
 (2) It adds error checking.  In case either fsync or close syscall
cd9d161
     fails pass up the error (and fail migration).
cd9d161
cd9d161
[ v2: return -errno instead of -1 ]
cd9d161
cd9d161
Cc: Juan Quintela <quintela@redhat.com>
cd9d161
Cc: Jiri Denemark <jdenemar@redhat.com>
cd9d161
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
cd9d161
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
cd9d161
(cherry picked from commit aab2293687ee54a409f3fb53a1ab3595b595e0fb)
cd9d161
cd9d161
Signed-off-by: Bruce Rogers <brogers@suse.com>
cd9d161
Signed-off-by: Andreas Färber <afaerber@suse.de>
cd9d161
---
cd9d161
 migration-fd.c | 23 ++++++++++++++++++++++-
cd9d161
 1 file changed, 22 insertions(+), 1 deletion(-)
cd9d161
cd9d161
diff --git a/migration-fd.c b/migration-fd.c
cd9d161
index 66d51c1..f986bdf 100644
cd9d161
--- a/migration-fd.c
cd9d161
+++ b/migration-fd.c
cd9d161
@@ -42,10 +42,31 @@ static int fd_write(FdMigrationState *s, const void * buf, size_t size)
cd9d161
 
cd9d161
 static int fd_close(FdMigrationState *s)
cd9d161
 {
cd9d161
+    struct stat st;
cd9d161
+    int ret;
cd9d161
+
cd9d161
     DPRINTF("fd_close\n");
cd9d161
     if (s->fd != -1) {
cd9d161
-        close(s->fd);
cd9d161
+        ret = fstat(s->fd, &st);
cd9d161
+        if (ret == 0 && S_ISREG(st.st_mode)) {
cd9d161
+            /*
cd9d161
+             * If the file handle is a regular file make sure the
cd9d161
+             * data is flushed to disk before signaling success.
cd9d161
+             */
cd9d161
+            ret = fsync(s->fd);
cd9d161
+            if (ret != 0) {
cd9d161
+                ret = -errno;
cd9d161
+                perror("migration-fd: fsync");
cd9d161
+                return ret;
cd9d161
+            }
cd9d161
+        }
cd9d161
+        ret = close(s->fd);
cd9d161
         s->fd = -1;
cd9d161
+        if (ret != 0) {
cd9d161
+            ret = -errno;
cd9d161
+            perror("migration-fd: close");
cd9d161
+            return ret;
cd9d161
+        }
cd9d161
     }
cd9d161
     return 0;
cd9d161
 }
cd9d161
-- 
cd9d161
1.7.11.2
cd9d161