Patchwork [21/22] savevm: Convert do_loadvm() to QError

login
register
mail settings
Submitter Luiz Capitulino
Date April 20, 2010, 9:09 p.m.
Message ID <1271797792-24571-22-git-send-email-lcapitulino@redhat.com>
Download mbox | patch
Permalink /patch/50596/
State New
Headers show

Comments

Luiz Capitulino - April 20, 2010, 9:09 p.m.
Note that the current code (switch statement) reports errors and
warnings. In QMP though, only errors can be reported.

To fix this we introduce two functions: load_vmstate_warn() and
load_vmstate_error(). So that we maintain the user monitor behavior,
and do the right thing for QMP.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 savevm.c |   67 ++++++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 46 insertions(+), 21 deletions(-)

Patch

diff --git a/savevm.c b/savevm.c
index 39a935d..8473833 100644
--- a/savevm.c
+++ b/savevm.c
@@ -1753,6 +1753,42 @@  int do_savevm(Monitor *mon, const QDict *qdict, QObject **ret_data)
     return (ret < 0 ? -1 : 0);
 }
 
+static void load_vmstate_warn(int err_code, BlockDriverState *bs1,
+                              const char *name)
+{
+    switch (err_code) {
+    case -ENOTSUP:
+        error_report("Warning: Snapshots not supported on device '%s'",
+                     bdrv_get_device_name(bs1));
+        break;
+    case -ENOENT:
+        error_report("Warning: Could not find snapshot '%s' on device '%s'",
+                     name, bdrv_get_device_name(bs1));
+        break;
+    default:
+        error_report("Warning: Error %d while activating snapshot on '%s'",
+                     err_code, bdrv_get_device_name(bs1));
+        break;
+    }
+}
+
+static void load_vmstate_error(int err_code, BlockDriverState *bs1,
+                               const char *name)
+{
+    switch (err_code) {
+    case -ENOTSUP:
+        qerror_report(QERR_DEVICE_NO_SNAPSHOT, bdrv_get_device_name(bs1));
+        break;
+    case -ENOENT:
+        qerror_report(QERR_SNAPSHOT_NOT_FOUND, bdrv_get_device_name(bs1), name);
+        break;
+    default:
+        qerror_report(QERR_SNAPSHOT_ACTIVATE_FAILED,
+                      bdrv_get_device_name(bs1), get_errno_string(err_code));
+        break;
+    }
+}
+
 int load_vmstate(const char *name)
 {
     DriveInfo *dinfo;
@@ -1763,7 +1799,7 @@  int load_vmstate(const char *name)
 
     bs = get_bs_snapshots();
     if (!bs) {
-        error_report("No block device supports snapshots");
+        qerror_report(QERR_SNAPSHOT_NO_DEVICE);
         return -EINVAL;
     }
 
@@ -1775,26 +1811,13 @@  int load_vmstate(const char *name)
         if (bdrv_has_snapshot(bs1)) {
             ret = bdrv_snapshot_goto(bs1, name);
             if (ret < 0) {
-                switch(ret) {
-                case -ENOTSUP:
-                    error_report("%sSnapshots not supported on device '%s'",
-                                 bs != bs1 ? "Warning: " : "",
-                                 bdrv_get_device_name(bs1));
-                    break;
-                case -ENOENT:
-                    error_report("%sCould not find snapshot '%s' on device '%s'",
-                                 bs != bs1 ? "Warning: " : "",
-                                 name, bdrv_get_device_name(bs1));
-                    break;
-                default:
-                    error_report("%sError %d while activating snapshot on '%s'",
-                                 bs != bs1 ? "Warning: " : "",
-                                 ret, bdrv_get_device_name(bs1));
-                    break;
-                }
-                /* fatal on snapshot block device */
-                if (bs == bs1)
+                if (bs != bs1) {
+                   load_vmstate_warn(ret, bs1, name); 
+                } else {
+                    /* fatal on snapshot block device */
+                    load_vmstate_error(ret, bs1, name);
                     return ret;
+                }
             }
         }
     }
@@ -1802,8 +1825,10 @@  int load_vmstate(const char *name)
     /* Don't even try to load empty VM states */
     ret = bdrv_snapshot_find(bs, &sn, name);
     if (ret < 0) {
+        qerror_report(QERR_SNAPSHOT_NOT_FOUND, bdrv_get_device_name(bs), name);
         return ret;
     } else if (sn.vm_state_size == 0) {
+        qerror_report(QERR_STATEVM_LOAD_FAILED, get_errno_string(-EINVAL));
         return -EINVAL;
     }
 
@@ -1812,7 +1837,7 @@  int load_vmstate(const char *name)
     ret = qemu_loadvm_state(f);
     qemu_fclose(f);
     if (ret < 0) {
-        error_report("Error %d while loading VM state", ret);
+        qerror_report(QERR_STATEVM_LOAD_FAILED, get_errno_string(ret));
         return ret;
     }
     return 0;