[PATCH 09/20] qemu: Add helper function for creating save image fd

Jim Fehlig via Devel posted 20 patches 3 months, 2 weeks ago
[PATCH 09/20] qemu: Add helper function for creating save image fd
Posted by Jim Fehlig via Devel 3 months, 2 weeks ago
Move the code in qemuSaveImageCreate that opens, labels, and wraps the
save image fd to a helper function, providing more flexibility for
upcoming mapped-ram support.

Signed-off-by: Jim Fehlig <jfehlig@suse.com>
---
 src/qemu/qemu_saveimage.c | 65 +++++++++++++++++++++++++++------------
 1 file changed, 45 insertions(+), 20 deletions(-)

diff --git a/src/qemu/qemu_saveimage.c b/src/qemu/qemu_saveimage.c
index bffa0a3397..6f2ce40124 100644
--- a/src/qemu/qemu_saveimage.c
+++ b/src/qemu/qemu_saveimage.c
@@ -361,6 +361,50 @@ qemuSaveImageDecompressionStop(virCommand *cmd,
 }
 
 
+static int
+qemuSaveImageCreateFd(virQEMUDriver *driver,
+                      virDomainObj *vm,
+                      const char *path,
+                      virFileWrapperFd *wrapperFd,
+                      bool *needUnlink,
+                      unsigned int flags)
+{
+    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
+    int ret = -1;
+    VIR_AUTOCLOSE fd = -1;
+    int directFlag = 0;
+    unsigned int wrapperFlags = VIR_FILE_WRAPPER_NON_BLOCKING;
+
+    if (flags & VIR_DOMAIN_SAVE_BYPASS_CACHE) {
+        wrapperFlags |= VIR_FILE_WRAPPER_BYPASS_CACHE;
+        directFlag = virFileDirectFdFlag();
+        if (directFlag < 0) {
+            virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+                           _("bypass cache unsupported by this system"));
+            return -1;
+        }
+    }
+
+    fd = virQEMUFileOpenAs(cfg->user, cfg->group, false, path,
+                           O_WRONLY | O_TRUNC | O_CREAT | directFlag,
+                           needUnlink);
+
+    if (fd < 0)
+        return -1;
+
+    if (qemuSecuritySetImageFDLabel(driver->securityManager, vm->def, fd) < 0)
+        return -1;
+
+    if (!(wrapperFd = virFileWrapperFdNew(&fd, path, wrapperFlags)))
+        return -1;
+
+    ret = fd;
+    fd = -1;
+
+    return ret;
+}
+
+
 /* Helper function to execute a migration to file with a correct save header
  * the caller needs to make sure that the processors are stopped and do all other
  * actions besides saving memory */
@@ -377,33 +421,14 @@ qemuSaveImageCreate(virQEMUDriver *driver,
     bool needUnlink = false;
     int ret = -1;
     int fd = -1;
-    int directFlag = 0;
     virFileWrapperFd *wrapperFd = NULL;
-    unsigned int wrapperFlags = VIR_FILE_WRAPPER_NON_BLOCKING;
 
     /* Obtain the file handle.  */
-    if ((flags & VIR_DOMAIN_SAVE_BYPASS_CACHE)) {
-        wrapperFlags |= VIR_FILE_WRAPPER_BYPASS_CACHE;
-        directFlag = virFileDirectFdFlag();
-        if (directFlag < 0) {
-            virReportError(VIR_ERR_OPERATION_FAILED, "%s",
-                           _("bypass cache unsupported by this system"));
-            goto cleanup;
-        }
-    }
+    fd = qemuSaveImageCreateFd(driver, vm, path, wrapperFd, &needUnlink, flags);
 
-    fd = virQEMUFileOpenAs(cfg->user, cfg->group, false, path,
-                           O_WRONLY | O_TRUNC | O_CREAT | directFlag,
-                           &needUnlink);
     if (fd < 0)
         goto cleanup;
 
-    if (qemuSecuritySetImageFDLabel(driver->securityManager, vm->def, fd) < 0)
-        goto cleanup;
-
-    if (!(wrapperFd = virFileWrapperFdNew(&fd, path, wrapperFlags)))
-        goto cleanup;
-
     if (virQEMUSaveDataWrite(data, fd, path) < 0)
         goto cleanup;
 
-- 
2.35.3