[libvirt] [PATCH v3 16/18] qemu: Factor out qemuDomainSnapshotValidate() helper

Eric Blake eblake at redhat.com
Tue Mar 5 03:34:43 UTC 2019


Straight code motion, coupled with changing goto into return -1
as needed. This change will be important to later patches adding
bulk redefinition (where each snapshot in a list has to meet the
same constraints).

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 src/qemu/qemu_driver.c | 120 ++++++++++++++++++++++-------------------
 1 file changed, 66 insertions(+), 54 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 76fab0421b..39cc45537d 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -15673,6 +15673,69 @@ qemuDomainSnapshotCreateActiveExternal(virQEMUDriverPtr driver,
 }


+/* Validate that a snapshot object does not violate any qemu-specific
+ * constraints. */
+static int
+qemuDomainSnapshotValidate(virDomainSnapshotDefPtr def,
+                           virDomainSnapshotState state,
+                           unsigned int flags)
+{
+    /* reject snapshot names containing slashes or starting with dot as
+     * snapshot definitions are saved in files named by the snapshot name */
+    if (!(flags & VIR_DOMAIN_SNAPSHOT_CREATE_NO_METADATA)) {
+        if (strchr(def->name, '/')) {
+            virReportError(VIR_ERR_XML_DETAIL,
+                           _("invalid snapshot name '%s': "
+                             "name can't contain '/'"),
+                           def->name);
+            return -1;
+        }
+
+        if (def->name[0] == '.') {
+            virReportError(VIR_ERR_XML_DETAIL,
+                           _("invalid snapshot name '%s': "
+                             "name can't start with '.'"),
+                           def->name);
+            return -1;
+        }
+    }
+
+    /* allow snapshots only in certain states */
+    switch (state) {
+        /* valid states */
+    case VIR_SNAP_STATE_RUNNING:
+    case VIR_SNAP_STATE_PAUSED:
+    case VIR_SNAP_STATE_SHUTDOWN:
+    case VIR_SNAP_STATE_SHUTOFF:
+    case VIR_SNAP_STATE_CRASHED:
+        break;
+
+    case VIR_SNAP_STATE_DISK_SNAPSHOT:
+        if (!(flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE)) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, _("Invalid domain state %s"),
+                           virDomainSnapshotStateTypeToString(state));
+            return -1;
+        }
+        break;
+
+    case VIR_SNAP_STATE_PMSUSPENDED:
+        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
+                       _("qemu doesn't support taking snapshots of "
+                         "PMSUSPENDED guests"));
+        return -1;
+
+        /* invalid states */
+    case VIR_SNAP_STATE_NOSTATE:
+    case VIR_SNAP_STATE_BLOCKED: /* invalid state, unused in qemu */
+    case VIR_SNAP_STATE_LAST:
+        virReportError(VIR_ERR_INTERNAL_ERROR, _("Invalid domain state %s"),
+                       virDomainSnapshotStateTypeToString(state));
+        return -1;
+    }
+    return 0;
+}
+
+
 static virDomainSnapshotPtr
 qemuDomainSnapshotCreateXML(virDomainPtr domain,
                             const char *xmlDesc,
@@ -15693,7 +15756,6 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain,
     virQEMUDriverConfigPtr cfg = NULL;
     virCapsPtr caps = NULL;
     qemuDomainObjPrivatePtr priv;
-    virDomainSnapshotState state;

     virCheckFlags(VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
                   VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT |
@@ -15748,25 +15810,9 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain,
                                                 parse_flags)))
         goto cleanup;

-    /* reject snapshot names containing slashes or starting with dot as
-     * snapshot definitions are saved in files named by the snapshot name */
-    if (!(flags & VIR_DOMAIN_SNAPSHOT_CREATE_NO_METADATA)) {
-        if (strchr(def->name, '/')) {
-            virReportError(VIR_ERR_XML_DETAIL,
-                           _("invalid snapshot name '%s': "
-                             "name can't contain '/'"),
-                           def->name);
-            goto cleanup;
-        }
-
-        if (def->name[0] == '.') {
-            virReportError(VIR_ERR_XML_DETAIL,
-                           _("invalid snapshot name '%s': "
-                             "name can't start with '.'"),
-                           def->name);
-            goto cleanup;
-        }
-    }
+    if (qemuDomainSnapshotValidate(def, redefine ? def->state : vm->state.state,
+                                   flags) < 0)
+        goto cleanup;

     /* reject the VIR_DOMAIN_SNAPSHOT_CREATE_LIVE flag where not supported */
     if (flags & VIR_DOMAIN_SNAPSHOT_CREATE_LIVE &&
@@ -15778,40 +15824,6 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain,
         goto cleanup;
     }

-    /* allow snapshots only in certain states */
-    state = redefine ? def->state : vm->state.state;
-    switch (state) {
-        /* valid states */
-    case VIR_SNAP_STATE_RUNNING:
-    case VIR_SNAP_STATE_PAUSED:
-    case VIR_SNAP_STATE_SHUTDOWN:
-    case VIR_SNAP_STATE_SHUTOFF:
-    case VIR_SNAP_STATE_CRASHED:
-        break;
-
-    case VIR_SNAP_STATE_DISK_SNAPSHOT:
-        if (!redefine) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, _("Invalid domain state %s"),
-                           virDomainSnapshotStateTypeToString(state));
-            goto cleanup;
-        }
-        break;
-
-    case VIR_SNAP_STATE_PMSUSPENDED:
-        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
-                       _("qemu doesn't support taking snapshots of "
-                         "PMSUSPENDED guests"));
-        goto cleanup;
-
-        /* invalid states */
-    case VIR_SNAP_STATE_NOSTATE:
-    case VIR_SNAP_STATE_BLOCKED: /* invalid state, unused in qemu */
-    case VIR_SNAP_STATE_LAST:
-        virReportError(VIR_ERR_INTERNAL_ERROR, _("Invalid domain state %s"),
-                       virDomainSnapshotStateTypeToString(state));
-        goto cleanup;
-    }
-
     /* We are going to modify the domain below. Internal snapshots would use
      * a regular job, so we need to set the job mask to disallow query as
      * 'savevm' blocks the monitor. External snapshot will then modify the
-- 
2.20.1




More information about the libvir-list mailing list