a7b9285
From 67f17b78f85bfa766ba9bde63148c3c171ef3426 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:17 +0100
a7b9285
Subject: [PATCH] migration: simplify error handling
Alon Levy 408bdb5
Alon Levy 408bdb5
Always use qemu_file_get_error to detect errors, since that is how
Alon Levy 408bdb5
QEMUFile itself drops I/O after an error occurs.  There is no need
Alon Levy 408bdb5
to propagate and check return values all the time.
Alon Levy 408bdb5
Alon Levy 408bdb5
Also remove the "complete" member, since we know that it is set (via
Alon Levy 408bdb5
migrate_fd_cleanup) only when the state changes.
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 dba433c03a0f5dc22a459435dd89557886298921)
Alon Levy 408bdb5
---
Alon Levy 408bdb5
 include/migration/migration.h |  1 -
Alon Levy 408bdb5
 migration.c                   | 46 +++++++++++++------------------------------
Alon Levy 408bdb5
 2 files changed, 14 insertions(+), 33 deletions(-)
Alon Levy 408bdb5
Alon Levy 408bdb5
diff --git a/include/migration/migration.h b/include/migration/migration.h
Alon Levy 408bdb5
index d121409..3e680af 100644
Alon Levy 408bdb5
--- a/include/migration/migration.h
Alon Levy 408bdb5
+++ b/include/migration/migration.h
Alon Levy 408bdb5
@@ -54,7 +54,6 @@ struct MigrationState
Alon Levy 408bdb5
     int64_t dirty_bytes_rate;
Alon Levy 408bdb5
     bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Alon Levy 408bdb5
     int64_t xbzrle_cache_size;
Alon Levy 408bdb5
-    bool complete;
Alon Levy 408bdb5
 };
Alon Levy 408bdb5
 
Alon Levy 408bdb5
 void process_incoming_migration(QEMUFile *f);
Alon Levy 408bdb5
diff --git a/migration.c b/migration.c
Alon Levy 408bdb5
index da5f175..41a592c 100644
Alon Levy 408bdb5
--- a/migration.c
Alon Levy 408bdb5
+++ b/migration.c
Alon Levy 408bdb5
@@ -525,6 +525,10 @@ static void buffered_flush(MigrationState *s)
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     DPRINTF("flushing %zu byte(s) of data\n", s->buffer_size);
Alon Levy 408bdb5
 
Alon Levy 408bdb5
+    if (qemu_file_get_error(s->file)) {
Alon Levy 408bdb5
+        s->buffer_size = 0;
Alon Levy 408bdb5
+        return;
Alon Levy 408bdb5
+    }
Alon Levy 408bdb5
     qemu_fflush(s->file);
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     while (s->bytes_xfer < s->xfer_limit && offset < s->buffer_size) {
Alon Levy 408bdb5
@@ -592,7 +596,6 @@ static int buffered_close(void *opaque)
Alon Levy 408bdb5
     while (!qemu_file_get_error(s->file) && s->buffer_size) {
Alon Levy 408bdb5
         buffered_flush(s);
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
-    s->complete = true;
Alon Levy 408bdb5
     return migrate_fd_close(s);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
@@ -656,37 +659,21 @@ static void *buffered_file_thread(void *opaque)
Alon Levy 408bdb5
     int64_t sleep_time = 0;
Alon Levy 408bdb5
     int64_t max_size = 0;
Alon Levy 408bdb5
     bool last_round = false;
Alon Levy 408bdb5
-    int ret;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     qemu_mutex_lock_iothread();
Alon Levy 408bdb5
     DPRINTF("beginning savevm\n");
Alon Levy 408bdb5
-    ret = qemu_savevm_state_begin(s->file, &s->params);
Alon Levy 408bdb5
-    qemu_mutex_unlock_iothread();
Alon Levy 408bdb5
+    qemu_savevm_state_begin(s->file, &s->params);
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-    while (ret >= 0) {
Alon Levy 408bdb5
+    while (s->state == MIG_STATE_ACTIVE) {
Alon Levy 408bdb5
         int64_t current_time;
Alon Levy 408bdb5
         uint64_t pending_size;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-        qemu_mutex_lock_iothread();
Alon Levy 408bdb5
-        if (s->state != MIG_STATE_ACTIVE) {
Alon Levy 408bdb5
-            DPRINTF("put_ready returning because of non-active state\n");
Alon Levy 408bdb5
-            qemu_mutex_unlock_iothread();
Alon Levy 408bdb5
-            break;
Alon Levy 408bdb5
-        }
Alon Levy 408bdb5
-        if (s->complete) {
Alon Levy 408bdb5
-            qemu_mutex_unlock_iothread();
Alon Levy 408bdb5
-            break;
Alon Levy 408bdb5
-        }
Alon Levy 408bdb5
         if (s->bytes_xfer < s->xfer_limit) {
Alon Levy 408bdb5
             DPRINTF("iterate\n");
Alon Levy 408bdb5
             pending_size = qemu_savevm_state_pending(s->file, max_size);
Alon Levy 408bdb5
             DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
Alon Levy 408bdb5
             if (pending_size && pending_size >= max_size) {
Alon Levy 408bdb5
-                ret = qemu_savevm_state_iterate(s->file);
Alon Levy 408bdb5
-                if (ret < 0) {
Alon Levy 408bdb5
-                    qemu_mutex_unlock_iothread();
Alon Levy 408bdb5
-                    break;
Alon Levy 408bdb5
-                }
Alon Levy 408bdb5
+                qemu_savevm_state_iterate(s->file);
Alon Levy 408bdb5
             } else {
Alon Levy 408bdb5
                 int old_vm_running = runstate_is_running();
Alon Levy 408bdb5
                 int64_t start_time, end_time;
Alon Levy 408bdb5
@@ -695,13 +682,8 @@ static void *buffered_file_thread(void *opaque)
Alon Levy 408bdb5
                 start_time = qemu_get_clock_ms(rt_clock);
Alon Levy 408bdb5
                 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Alon Levy 408bdb5
                 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Alon Levy 408bdb5
-                ret = qemu_savevm_state_complete(s->file);
Alon Levy 408bdb5
-                if (ret < 0) {
Alon Levy 408bdb5
-                    qemu_mutex_unlock_iothread();
Alon Levy 408bdb5
-                    break;
Alon Levy 408bdb5
-                } else {
Alon Levy 408bdb5
-                    migrate_fd_completed(s);
Alon Levy 408bdb5
-                }
Alon Levy 408bdb5
+                qemu_savevm_state_complete(s->file);
Alon Levy 408bdb5
+                migrate_fd_completed(s);
Alon Levy 408bdb5
                 end_time = qemu_get_clock_ms(rt_clock);
Alon Levy 408bdb5
                 s->total_time = end_time - s->total_time;
Alon Levy 408bdb5
                 s->downtime = end_time - start_time;
Alon Levy 408bdb5
@@ -740,12 +722,13 @@ static void *buffered_file_thread(void *opaque)
Alon Levy 408bdb5
             sleep_time += qemu_get_clock_ms(rt_clock) - current_time;
Alon Levy 408bdb5
         }
Alon Levy 408bdb5
         buffered_flush(s);
Alon Levy 408bdb5
-        ret = qemu_file_get_error(s->file);
Alon Levy 408bdb5
+        qemu_mutex_lock_iothread();
Alon Levy 408bdb5
+        if (qemu_file_get_error(s->file)) {
Alon Levy 408bdb5
+            migrate_fd_error(s);
Alon Levy 408bdb5
+        }
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-    if (ret < 0) {
Alon Levy 408bdb5
-        migrate_fd_error(s);
Alon Levy 408bdb5
-    }
Alon Levy 408bdb5
+    qemu_mutex_unlock_iothread();
Alon Levy 408bdb5
     g_free(s->buffer);
Alon Levy 408bdb5
     return NULL;
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
@@ -770,7 +753,6 @@ void migrate_fd_connect(MigrationState *s)
Alon Levy 408bdb5
     s->expected_downtime = max_downtime/1000000;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     s->xfer_limit = s->bandwidth_limit / XFER_LIMIT_RATIO;
Alon Levy 408bdb5
-    s->complete = false;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     s->file = qemu_fopen_ops(s, &buffered_file_ops);
Alon Levy 408bdb5