render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
3e5111
From d37bad5cb5da2804d51cd6c45903f538d9ec85c5 Mon Sep 17 00:00:00 2001
3e5111
Message-Id: <d37bad5cb5da2804d51cd6c45903f538d9ec85c5@dist-git>
3e5111
From: Jiri Denemark <jdenemar@redhat.com>
3e5111
Date: Tue, 6 Jun 2017 22:29:24 +0200
3e5111
Subject: [PATCH] qemu: Introduce virQEMUSaveData{New,Free}
3e5111
3e5111
This is a preparation for creating a new virQEMUSaveData structure which
3e5111
will encapsulate all save image header data.
3e5111
3e5111
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
3e5111
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
3e5111
(cherry picked from commit a2d2aae148980f00dd83093d61b1aa06c54fbe96)
3e5111
3e5111
https://bugzilla.redhat.com/show_bug.cgi?id=1441662
3e5111
3e5111
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
3e5111
---
3e5111
 src/qemu/qemu_driver.c | 128 ++++++++++++++++++++++++++++++++-----------------
3e5111
 1 file changed, 83 insertions(+), 45 deletions(-)
3e5111
3e5111
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
3e5111
index 5db92b04a1..4a05793339 100644
3e5111
--- a/src/qemu/qemu_driver.c
3e5111
+++ b/src/qemu/qemu_driver.c
3e5111
@@ -2826,6 +2826,36 @@ bswap_header(virQEMUSaveHeaderPtr hdr)
3e5111
 }
3e5111
 
3e5111
 
3e5111
+static void
3e5111
+virQEMUSaveDataFree(virQEMUSaveHeaderPtr header)
3e5111
+{
3e5111
+    if (!header)
3e5111
+        return;
3e5111
+
3e5111
+    VIR_FREE(header);
3e5111
+}
3e5111
+
3e5111
+
3e5111
+static virQEMUSaveHeaderPtr
3e5111
+virQEMUSaveDataNew(char *domXML,
3e5111
+                   bool running,
3e5111
+                   int compressed)
3e5111
+{
3e5111
+    virQEMUSaveHeaderPtr header = NULL;
3e5111
+
3e5111
+    if (VIR_ALLOC(header) < 0)
3e5111
+        return NULL;
3e5111
+
3e5111
+    memcpy(header->magic, QEMU_SAVE_PARTIAL, sizeof(header->magic));
3e5111
+    header->version = QEMU_SAVE_VERSION;
3e5111
+    header->was_running = running ? 1 : 0;
3e5111
+    header->compressed = compressed;
3e5111
+    header->data_len = strlen(domXML) + 1;
3e5111
+
3e5111
+    return header;
3e5111
+}
3e5111
+
3e5111
+
3e5111
 /* return -errno on failure, or 0 on success */
3e5111
 static int
3e5111
 qemuDomainSaveHeader(int fd, const char *path, const char *xml,
3e5111
@@ -3048,13 +3078,11 @@ qemuDomainSaveMemory(virQEMUDriverPtr driver,
3e5111
                      virDomainObjPtr vm,
3e5111
                      const char *path,
3e5111
                      const char *domXML,
3e5111
-                     int compressed,
3e5111
+                     virQEMUSaveHeaderPtr header,
3e5111
                      const char *compressedpath,
3e5111
-                     bool was_running,
3e5111
                      unsigned int flags,
3e5111
                      qemuDomainAsyncJob asyncJob)
3e5111
 {
3e5111
-    virQEMUSaveHeader header;
3e5111
     bool bypassSecurityDriver = false;
3e5111
     bool needUnlink = false;
3e5111
     int ret = -1;
3e5111
@@ -3063,13 +3091,6 @@ qemuDomainSaveMemory(virQEMUDriverPtr driver,
3e5111
     virFileWrapperFdPtr wrapperFd = NULL;
3e5111
     unsigned int wrapperFlags = VIR_FILE_WRAPPER_NON_BLOCKING;
3e5111
 
3e5111
-    memset(&header, 0, sizeof(header));
3e5111
-    memcpy(header.magic, QEMU_SAVE_PARTIAL, sizeof(header.magic));
3e5111
-    header.version = QEMU_SAVE_VERSION;
3e5111
-    header.was_running = was_running ? 1 : 0;
3e5111
-    header.compressed = compressed;
3e5111
-    header.data_len = strlen(domXML) + 1;
3e5111
-
3e5111
     /* Obtain the file handle.  */
3e5111
     if ((flags & VIR_DOMAIN_SAVE_BYPASS_CACHE)) {
3e5111
         wrapperFlags |= VIR_FILE_WRAPPER_BYPASS_CACHE;
3e5111
@@ -3093,7 +3114,7 @@ qemuDomainSaveMemory(virQEMUDriverPtr driver,
3e5111
         goto cleanup;
3e5111
 
3e5111
     /* Write header to file, followed by XML */
3e5111
-    if (qemuDomainSaveHeader(fd, path, domXML, &header) < 0)
3e5111
+    if (qemuDomainSaveHeader(fd, path, domXML, header) < 0)
3e5111
         goto cleanup;
3e5111
 
3e5111
     /* Perform the migration */
3e5111
@@ -3117,7 +3138,7 @@ qemuDomainSaveMemory(virQEMUDriverPtr driver,
3e5111
     if ((fd = qemuOpenFile(driver, vm, path, O_WRONLY, NULL, NULL)) < 0)
3e5111
         goto cleanup;
3e5111
 
3e5111
-    memcpy(header.magic, QEMU_SAVE_MAGIC, sizeof(header.magic));
3e5111
+    memcpy(header->magic, QEMU_SAVE_MAGIC, sizeof(header->magic));
3e5111
 
3e5111
     if (safewrite(fd, &header, sizeof(header)) != sizeof(header)) {
3e5111
         virReportSystemError(errno, _("unable to write %s"), path);
3e5111
@@ -3158,6 +3179,7 @@ qemuDomainSaveInternal(virQEMUDriverPtr driver, virDomainPtr dom,
3e5111
     virObjectEventPtr event = NULL;
3e5111
     qemuDomainObjPrivatePtr priv = vm->privateData;
3e5111
     virCapsPtr caps;
3e5111
+    virQEMUSaveHeaderPtr header = NULL;
3e5111
 
3e5111
     if (!(caps = virQEMUDriverGetCapabilities(driver, false)))
3e5111
         goto cleanup;
3e5111
@@ -3223,9 +3245,11 @@ qemuDomainSaveInternal(virQEMUDriverPtr driver, virDomainPtr dom,
3e5111
         goto endjob;
3e5111
     }
3e5111
 
3e5111
-    ret = qemuDomainSaveMemory(driver, vm, path, xml, compressed,
3e5111
-                               compressedpath, was_running, flags,
3e5111
-                               QEMU_ASYNC_JOB_SAVE);
3e5111
+    if (!(header = virQEMUSaveDataNew(xml, was_running, compressed)))
3e5111
+        goto endjob;
3e5111
+
3e5111
+    ret = qemuDomainSaveMemory(driver, vm, path, xml, header, compressedpath,
3e5111
+                               flags, QEMU_ASYNC_JOB_SAVE);
3e5111
     if (ret < 0)
3e5111
         goto endjob;
3e5111
 
3e5111
@@ -3258,6 +3282,7 @@ qemuDomainSaveInternal(virQEMUDriverPtr driver, virDomainPtr dom,
3e5111
 
3e5111
  cleanup:
3e5111
     VIR_FREE(xml);
3e5111
+    virQEMUSaveDataFree(header);
3e5111
     qemuDomainEventQueue(driver, event);
3e5111
     virObjectUnref(caps);
3e5111
     return ret;
3e5111
@@ -6189,7 +6214,7 @@ static int ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4)
3e5111
 qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
3e5111
                         const char *path,
3e5111
                         virDomainDefPtr *ret_def,
3e5111
-                        virQEMUSaveHeaderPtr ret_header,
3e5111
+                        virQEMUSaveHeaderPtr *ret_header,
3e5111
                         char **xmlout,
3e5111
                         bool bypass_cache,
3e5111
                         virFileWrapperFdPtr *wrapperFd,
3e5111
@@ -6197,8 +6222,8 @@ qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
3e5111
                         bool unlink_corrupt)
3e5111
 {
3e5111
     int fd = -1;
3e5111
-    virQEMUSaveHeader header;
3e5111
     char *xml = NULL;
3e5111
+    virQEMUSaveHeaderPtr header = NULL;
3e5111
     virDomainDefPtr def = NULL;
3e5111
     int oflags = open_write ? O_RDWR : O_RDONLY;
3e5111
     virCapsPtr caps = NULL;
3e5111
@@ -6223,7 +6248,10 @@ qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
3e5111
                                            VIR_FILE_WRAPPER_BYPASS_CACHE)))
3e5111
         goto error;
3e5111
 
3e5111
-    if (saferead(fd, &header, sizeof(header)) != sizeof(header)) {
3e5111
+    if (VIR_ALLOC(header) < 0)
3e5111
+        goto error;
3e5111
+
3e5111
+    if (saferead(fd, header, sizeof(*header)) != sizeof(*header)) {
3e5111
         if (unlink_corrupt) {
3e5111
             if (VIR_CLOSE(fd) < 0 || unlink(path) < 0) {
3e5111
                 virReportSystemError(errno,
3e5111
@@ -6239,11 +6267,11 @@ qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
3e5111
         goto error;
3e5111
     }
3e5111
 
3e5111
-    if (memcmp(header.magic, QEMU_SAVE_MAGIC, sizeof(header.magic)) != 0) {
3e5111
+    if (memcmp(header->magic, QEMU_SAVE_MAGIC, sizeof(header->magic)) != 0) {
3e5111
         const char *msg = _("image magic is incorrect");
3e5111
 
3e5111
-        if (memcmp(header.magic, QEMU_SAVE_PARTIAL,
3e5111
-                   sizeof(header.magic)) == 0) {
3e5111
+        if (memcmp(header->magic, QEMU_SAVE_PARTIAL,
3e5111
+                   sizeof(header->magic)) == 0) {
3e5111
             msg = _("save image is incomplete");
3e5111
             if (unlink_corrupt) {
3e5111
                 if (VIR_CLOSE(fd) < 0 || unlink(path) < 0) {
3e5111
@@ -6260,28 +6288,28 @@ qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
3e5111
         goto error;
3e5111
     }
3e5111
 
3e5111
-    if (header.version > QEMU_SAVE_VERSION) {
3e5111
+    if (header->version > QEMU_SAVE_VERSION) {
3e5111
         /* convert endianess and try again */
3e5111
-        bswap_header(&header);
3e5111
+        bswap_header(header);
3e5111
     }
3e5111
 
3e5111
-    if (header.version > QEMU_SAVE_VERSION) {
3e5111
+    if (header->version > QEMU_SAVE_VERSION) {
3e5111
         virReportError(VIR_ERR_OPERATION_FAILED,
3e5111
                        _("image version is not supported (%d > %d)"),
3e5111
-                       header.version, QEMU_SAVE_VERSION);
3e5111
+                       header->version, QEMU_SAVE_VERSION);
3e5111
         goto error;
3e5111
     }
3e5111
 
3e5111
-    if (header.data_len <= 0) {
3e5111
+    if (header->data_len <= 0) {
3e5111
         virReportError(VIR_ERR_OPERATION_FAILED,
3e5111
-                       _("invalid XML length: %d"), header.data_len);
3e5111
+                       _("invalid XML length: %d"), header->data_len);
3e5111
         goto error;
3e5111
     }
3e5111
 
3e5111
-    if (VIR_ALLOC_N(xml, header.data_len) < 0)
3e5111
+    if (VIR_ALLOC_N(xml, header->data_len) < 0)
3e5111
         goto error;
3e5111
 
3e5111
-    if (saferead(fd, xml, header.data_len) != header.data_len) {
3e5111
+    if (saferead(fd, xml, header->data_len) != header->data_len) {
3e5111
         virReportError(VIR_ERR_OPERATION_FAILED,
3e5111
                        "%s", _("failed to read XML"));
3e5111
         goto error;
3e5111
@@ -6308,6 +6336,7 @@ qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
3e5111
  error:
3e5111
     virDomainDefFree(def);
3e5111
     VIR_FREE(xml);
3e5111
+    virQEMUSaveDataFree(header);
3e5111
     VIR_FORCE_CLOSE(fd);
3e5111
     virObjectUnref(caps);
3e5111
 
3e5111
@@ -6319,7 +6348,7 @@ qemuDomainSaveImageStartVM(virConnectPtr conn,
3e5111
                            virQEMUDriverPtr driver,
3e5111
                            virDomainObjPtr vm,
3e5111
                            int *fd,
3e5111
-                           const virQEMUSaveHeader *header,
3e5111
+                           virQEMUSaveHeaderPtr header,
3e5111
                            const char *path,
3e5111
                            bool start_paused,
3e5111
                            qemuDomainAsyncJob asyncJob)
3e5111
@@ -6445,7 +6474,7 @@ qemuDomainRestoreFlags(virConnectPtr conn,
3e5111
     const char *newxml = dxml;
3e5111
     int fd = -1;
3e5111
     int ret = -1;
3e5111
-    virQEMUSaveHeader header;
3e5111
+    virQEMUSaveHeaderPtr header = NULL;
3e5111
     virFileWrapperFdPtr wrapperFd = NULL;
3e5111
     bool hook_taint = false;
3e5111
 
3e5111
@@ -6502,9 +6531,9 @@ qemuDomainRestoreFlags(virConnectPtr conn,
3e5111
     def = NULL;
3e5111
 
3e5111
     if (flags & VIR_DOMAIN_SAVE_RUNNING)
3e5111
-        header.was_running = 1;
3e5111
+        header->was_running = 1;
3e5111
     else if (flags & VIR_DOMAIN_SAVE_PAUSED)
3e5111
-        header.was_running = 0;
3e5111
+        header->was_running = 0;
3e5111
 
3e5111
     if (hook_taint) {
3e5111
         priv = vm->privateData;
3e5111
@@ -6514,7 +6543,7 @@ qemuDomainRestoreFlags(virConnectPtr conn,
3e5111
     if (qemuProcessBeginJob(driver, vm, VIR_DOMAIN_JOB_OPERATION_RESTORE) < 0)
3e5111
         goto cleanup;
3e5111
 
3e5111
-    ret = qemuDomainSaveImageStartVM(conn, driver, vm, &fd, &header, path,
3e5111
+    ret = qemuDomainSaveImageStartVM(conn, driver, vm, &fd, header, path,
3e5111
                                      false, QEMU_ASYNC_JOB_START);
3e5111
     if (virFileWrapperFdClose(wrapperFd) < 0)
3e5111
         VIR_WARN("Failed to close %s", path);
3e5111
@@ -6525,6 +6554,7 @@ qemuDomainRestoreFlags(virConnectPtr conn,
3e5111
     virDomainDefFree(def);
3e5111
     VIR_FORCE_CLOSE(fd);
3e5111
     VIR_FREE(xml);
3e5111
+    virQEMUSaveDataFree(header);
3e5111
     VIR_FREE(xmlout);
3e5111
     virFileWrapperFdFree(wrapperFd);
3e5111
     if (vm && ret < 0)
3e5111
@@ -6549,7 +6579,7 @@ qemuDomainSaveImageGetXMLDesc(virConnectPtr conn, const char *path,
3e5111
     char *ret = NULL;
3e5111
     virDomainDefPtr def = NULL;
3e5111
     int fd = -1;
3e5111
-    virQEMUSaveHeader header;
3e5111
+    virQEMUSaveHeaderPtr header = NULL;
3e5111
 
3e5111
     /* We only take subset of virDomainDefFormat flags.  */
3e5111
     virCheckFlags(VIR_DOMAIN_XML_SECURE, NULL);
3e5111
@@ -6566,6 +6596,7 @@ qemuDomainSaveImageGetXMLDesc(virConnectPtr conn, const char *path,
3e5111
     ret = qemuDomainDefFormatXML(driver, def, flags);
3e5111
 
3e5111
  cleanup:
3e5111
+    virQEMUSaveDataFree(header);
3e5111
     virDomainDefFree(def);
3e5111
     VIR_FORCE_CLOSE(fd);
3e5111
     return ret;
3e5111
@@ -6580,9 +6611,9 @@ qemuDomainSaveImageDefineXML(virConnectPtr conn, const char *path,
3e5111
     virDomainDefPtr def = NULL;
3e5111
     virDomainDefPtr newdef = NULL;
3e5111
     int fd = -1;
3e5111
-    virQEMUSaveHeader header;
3e5111
     char *xml = NULL;
3e5111
     size_t len;
3e5111
+    virQEMUSaveHeaderPtr header = NULL;
3e5111
     int state = -1;
3e5111
 
3e5111
     virCheckFlags(VIR_DOMAIN_SAVE_RUNNING |
3e5111
@@ -6603,14 +6634,14 @@ qemuDomainSaveImageDefineXML(virConnectPtr conn, const char *path,
3e5111
         goto cleanup;
3e5111
 
3e5111
     if (STREQ(xml, dxml) &&
3e5111
-        (state < 0 || state == header.was_running)) {
3e5111
+        (state < 0 || state == header->was_running)) {
3e5111
         /* no change to the XML */
3e5111
         ret = 0;
3e5111
         goto cleanup;
3e5111
     }
3e5111
 
3e5111
     if (state >= 0)
3e5111
-        header.was_running = state;
3e5111
+        header->was_running = state;
3e5111
 
3e5111
     if (!(newdef = qemuDomainSaveImageUpdateDef(driver, def, dxml)))
3e5111
         goto cleanup;
3e5111
@@ -6625,12 +6656,12 @@ qemuDomainSaveImageDefineXML(virConnectPtr conn, const char *path,
3e5111
         goto cleanup;
3e5111
     len = strlen(xml) + 1;
3e5111
 
3e5111
-    if (len > header.data_len) {
3e5111
+    if (len > header->data_len) {
3e5111
         virReportError(VIR_ERR_OPERATION_FAILED, "%s",
3e5111
                        _("new xml too large to fit in file"));
3e5111
         goto cleanup;
3e5111
     }
3e5111
-    if (VIR_EXPAND_N(xml, len, header.data_len - len) < 0)
3e5111
+    if (VIR_EXPAND_N(xml, len, header->data_len - len) < 0)
3e5111
         goto cleanup;
3e5111
 
3e5111
     if (lseek(fd, 0, SEEK_SET) != 0) {
3e5111
@@ -6651,6 +6682,7 @@ qemuDomainSaveImageDefineXML(virConnectPtr conn, const char *path,
3e5111
     virDomainDefFree(newdef);
3e5111
     VIR_FORCE_CLOSE(fd);
3e5111
     VIR_FREE(xml);
3e5111
+    virQEMUSaveDataFree(header);
3e5111
     return ret;
3e5111
 }
3e5111
 
3e5111
@@ -6671,7 +6703,7 @@ qemuDomainObjRestore(virConnectPtr conn,
3e5111
     int ret = -1;
3e5111
     char *xml = NULL;
3e5111
     char *xmlout = NULL;
3e5111
-    virQEMUSaveHeader header;
3e5111
+    virQEMUSaveHeaderPtr header = NULL;
3e5111
     virFileWrapperFdPtr wrapperFd = NULL;
3e5111
 
3e5111
     fd = qemuDomainSaveImageOpen(driver, path, &def, &header, &xml,
3e5111
@@ -6722,13 +6754,14 @@ qemuDomainObjRestore(virConnectPtr conn,
3e5111
     virDomainObjAssignDef(vm, def, true, NULL);
3e5111
     def = NULL;
3e5111
 
3e5111
-    ret = qemuDomainSaveImageStartVM(conn, driver, vm, &fd, &header, path,
3e5111
+    ret = qemuDomainSaveImageStartVM(conn, driver, vm, &fd, header, path,
3e5111
                                      start_paused, asyncJob);
3e5111
     if (virFileWrapperFdClose(wrapperFd) < 0)
3e5111
         VIR_WARN("Failed to close %s", path);
3e5111
 
3e5111
  cleanup:
3e5111
     VIR_FREE(xml);
3e5111
+    virQEMUSaveDataFree(header);
3e5111
     VIR_FREE(xmlout);
3e5111
     virDomainDefFree(def);
3e5111
     VIR_FORCE_CLOSE(fd);
3e5111
@@ -14301,6 +14334,7 @@ qemuDomainSnapshotCreateActiveExternal(virConnectPtr conn,
3e5111
     virQEMUDriverConfigPtr cfg = NULL;
3e5111
     int compressed;
3e5111
     char *compressedpath = NULL;
3e5111
+    virQEMUSaveHeaderPtr header = NULL;
3e5111
 
3e5111
     /* If quiesce was requested, then issue a freeze command, and a
3e5111
      * counterpart thaw command when it is actually sent to agent.
3e5111
@@ -14372,9 +14406,12 @@ qemuDomainSnapshotCreateActiveExternal(virConnectPtr conn,
3e5111
         if (!(xml = qemuDomainDefFormatLive(driver, vm->def, true, true)))
3e5111
             goto cleanup;
3e5111
 
3e5111
-        if ((ret = qemuDomainSaveMemory(driver, vm, snap->def->file, xml,
3e5111
-                                        compressed, compressedpath, resume,
3e5111
-                                        0, QEMU_ASYNC_JOB_SNAPSHOT)) < 0)
3e5111
+        if (!(header = virQEMUSaveDataNew(xml, resume, compressed)))
3e5111
+            goto cleanup;
3e5111
+
3e5111
+        if ((ret = qemuDomainSaveMemory(driver, vm, snap->def->file, xml, header,
3e5111
+                                        compressedpath, 0,
3e5111
+                                        QEMU_ASYNC_JOB_SNAPSHOT)) < 0)
3e5111
             goto cleanup;
3e5111
 
3e5111
         /* the memory image was created, remove it on errors */
3e5111
@@ -14442,6 +14479,7 @@ qemuDomainSnapshotCreateActiveExternal(virConnectPtr conn,
3e5111
             ret = -1;
3e5111
     }
3e5111
 
3e5111
+    virQEMUSaveDataFree(header);
3e5111
     VIR_FREE(xml);
3e5111
     VIR_FREE(compressedpath);
3e5111
     virObjectUnref(cfg);
3e5111
-- 
3e5111
2.13.1
3e5111