xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Ian Jackson <ian.jackson@eu.citrix.com>
To: xen-devel@lists.xen.org
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Subject: [PATCH 12/21] libxl: Make libxl__domain_save_device_model asynchronous
Date: Fri, 15 Jun 2012 12:53:57 +0100	[thread overview]
Message-ID: <1339761246-27641-13-git-send-email-ian.jackson@eu.citrix.com> (raw)
In-Reply-To: <1339761246-27641-1-git-send-email-ian.jackson@eu.citrix.com>

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>

Changes in series v3:
 * Improve one more a debugging message.
---
 tools/libxl/libxl_dom.c      |  100 +++++++++++++++++++++++++++---------------
 tools/libxl/libxl_internal.h |    1 +
 2 files changed, 66 insertions(+), 35 deletions(-)

diff --git a/tools/libxl/libxl_dom.c b/tools/libxl/libxl_dom.c
index e104744..dbae98c 100644
--- a/tools/libxl/libxl_dom.c
+++ b/tools/libxl/libxl_dom.c
@@ -1148,15 +1148,17 @@ out:
     domain_suspend_done(egc, dss, rc);
 }
 
+static void save_device_model_datacopier_done(libxl__egc *egc,
+     libxl__datacopier_state *dc, int onwrite, int errnoval);
+
 void libxl__domain_save_device_model(libxl__egc *egc,
                                      libxl__domain_suspend_state *dss,
                                      libxl__save_device_model_cb *callback)
 {
     STATE_AO_GC(dss->ao);
-    int rc, fd2 = -1, c;
-    char buf[1024];
     struct stat st;
     uint32_t qemu_state_len;
+    int rc;
 
     dss->save_dm_callback = callback;
 
@@ -1164,49 +1166,77 @@ void libxl__domain_save_device_model(libxl__egc *egc,
     const char *const filename = dss->dm_savefile;
     const int fd = dss->fd;
 
-    if (stat(filename, &st) < 0)
+    libxl__datacopier_state *dc = &dss->save_dm_datacopier;
+    memset(dc, 0, sizeof(*dc));
+    dc->readwhat = GCSPRINTF("qemu save file %s", filename);
+    dc->ao = ao;
+    dc->readfd = -1;
+    dc->writefd = fd;
+    dc->maxsz = INT_MAX;
+    dc->copywhat = GCSPRINTF("qemu save file for domain %"PRIu32, dss->domid);
+    dc->writewhat = "save/migration stream";
+    dc->callback = save_device_model_datacopier_done;
+
+    dc->readfd = open(filename, O_RDONLY);
+    if (dc->readfd < 0) {
+        LOGE(ERROR, "unable to open %s", dc->readwhat);
+        goto out;
+    }
+
+    if (fstat(dc->readfd, &st))
     {
-        LOG(ERROR, "Unable to stat qemu save file\n");
-        rc = ERROR_FAIL;
+        LOGE(ERROR, "unable to fstat %s", dc->readwhat);
+        goto out;
+    }
+
+    if (!S_ISREG(st.st_mode)) {
+        LOG(ERROR, "%s is not a plain file!", dc->readwhat);
         goto out;
     }
 
     qemu_state_len = st.st_size;
-    LOG(DEBUG, "Qemu state is %d bytes\n", qemu_state_len);
+    LOG(DEBUG, "%s is %d bytes", dc->readwhat, qemu_state_len);
 
-    rc = libxl_write_exactly(CTX, fd, QEMU_SIGNATURE, strlen(QEMU_SIGNATURE),
-                              "saved-state file", "qemu signature");
-    if (rc)
-        goto out;
+    rc = libxl__datacopier_start(dc);
+    if (rc) goto out;
 
-    rc = libxl_write_exactly(CTX, fd, &qemu_state_len, sizeof(qemu_state_len),
-                            "saved-state file", "saved-state length");
-    if (rc)
-        goto out;
+    libxl__datacopier_prefixdata(egc, dc,
+                                 QEMU_SIGNATURE, strlen(QEMU_SIGNATURE));
 
-    fd2 = open(filename, O_RDONLY);
-    if (fd2 < 0) {
-        LOGE(ERROR, "Unable to open qemu save file\n");
-        goto out;
-    }
-    while ((c = read(fd2, buf, sizeof(buf))) != 0) {
-        if (c < 0) {
-            if (errno == EINTR)
-                continue;
-            rc = errno;
-            goto out;
-        }
-        rc = libxl_write_exactly(
-            CTX, fd, buf, c, "saved-state file", "qemu state");
-        if (rc)
-            goto out;
+    libxl__datacopier_prefixdata(egc, dc,
+                                 &qemu_state_len, sizeof(qemu_state_len));
+    return;
+
+ out:
+    save_device_model_datacopier_done(egc, dc, -1, 0);
+}
+
+static void save_device_model_datacopier_done(libxl__egc *egc,
+     libxl__datacopier_state *dc, int onwrite, int errnoval)
+{
+    libxl__domain_suspend_state *dss =
+        CONTAINER_OF(dc, *dss, save_dm_datacopier);
+    STATE_AO_GC(dss->ao);
+
+    /* Convenience aliases */
+    const char *const filename = dss->dm_savefile;
+    int our_rc = 0;
+    int rc;
+
+    libxl__datacopier_kill(dc);
+
+    if (onwrite || errnoval)
+        our_rc = ERROR_FAIL;
+
+    if (dc->readfd >= 0) {
+        close(dc->readfd);
+        dc->readfd = -1;
     }
-    rc = 0;
-out:
-    if (fd2 >= 0) close(fd2);
-    unlink(filename);
 
-    dss->save_dm_callback(egc, dss, rc);
+    rc = libxl__remove_file(gc, filename);
+    if (!our_rc) our_rc = rc;
+
+    dss->save_dm_callback(egc, dss, our_rc);
 }
 
 static void domain_suspend_done(libxl__egc *egc,
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index e95892a..c9b4189 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -1901,6 +1901,7 @@ struct libxl__domain_suspend_state {
     libxl__logdirty_switch logdirty;
     /* private for libxl__domain_save_device_model */
     libxl__save_device_model_cb *save_dm_callback;
+    libxl__datacopier_state save_dm_datacopier;
 };
 
 
-- 
1.7.2.5

  parent reply	other threads:[~2012-06-15 11:53 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-15 11:53 [PATCH v3 00/18] libxl: domain save/restore: run in a separate process Ian Jackson
2012-06-15 11:53 ` [PATCH 01/21] libxc: xc_domain_restore, make toolstack_restore const-correct Ian Jackson
2012-06-15 11:53 ` [PATCH 02/21] libxc: Do not segfault if (e.g.) switch_qemu_logdirty fails Ian Jackson
2012-06-21  8:47   ` Ian Campbell
2012-06-15 11:53 ` [PATCH 03/21] libxl: domain save: rename variables etc Ian Jackson
2012-06-15 11:53 ` [PATCH 04/21] libxl: domain restore: reshuffle, preparing for ao Ian Jackson
2012-06-15 11:53 ` [PATCH 05/21] libxl: domain save: API changes for asynchrony Ian Jackson
2012-06-22 11:47   ` Ian Campbell
2012-06-26 16:48     ` Ian Jackson
2012-06-15 11:53 ` [PATCH 06/21] libxl: domain save/restore: run in a separate process Ian Jackson
2012-06-22 11:59   ` Ian Campbell
2012-06-26 16:52     ` Ian Jackson
2012-06-15 11:53 ` [PATCH 07/21] libxl: rename libxl_dom:save_helper to physmap_path Ian Jackson
2012-06-15 11:53 ` [PATCH 08/21] libxl: provide libxl__xs_*_checked and libxl__xs_transaction_* Ian Jackson
2012-06-15 11:53 ` [PATCH 09/21] libxl: wait for qemu to acknowledge logdirty command Ian Jackson
2012-06-15 11:53 ` [PATCH 10/21] libxl: datacopier: provide "prefix data" facility Ian Jackson
2012-06-15 11:53 ` [PATCH 11/21] libxl: prepare for asynchronous writing of qemu save file Ian Jackson
2012-06-15 11:53 ` Ian Jackson [this message]
2012-06-15 11:53 ` [PATCH 13/21] libxl: Add a gc to libxl_get_cpu_topology Ian Jackson
2012-06-15 11:53 ` [PATCH 14/21] libxl: Do not pass NULL as gc_opt; introduce NOGC Ian Jackson
2012-06-15 11:54 ` [PATCH 15/21] libxl: Get compiler to warn about gc_opt==NULL Ian Jackson
2012-06-15 11:54 ` [PATCH 16/21] xl: Handle return value from libxl_domain_suspend correctly Ian Jackson
2012-06-15 11:54 ` [PATCH 17/21] libxl: do not leak dms->saved_state Ian Jackson
2012-06-15 11:54 ` [PATCH 18/21] libxl: do not leak spawned middle children Ian Jackson
2012-06-15 11:54 ` [PATCH 19/21] libxl: do not leak an event struct on ignored ao progress Ian Jackson
2012-06-15 11:54 ` [PATCH 20/21] libxl: further fixups re LIBXL_DOMAIN_TYPE Ian Jackson
2012-06-22 11:42   ` Ian Campbell
2012-06-15 11:54 ` [PATCH 21/21] libxl: DO NOT APPLY enforce prohibition on internal Ian Jackson
2012-06-22 11:45   ` Ian Campbell
2012-06-26 15:41     ` Ian Jackson
2012-06-15 13:40 ` [PATCH v3 00/18] libxl: domain save/restore: run in a separate process Stefano Stabellini
2012-06-22 13:39   ` Ian Campbell
2012-06-22 13:49     ` Ian Campbell
2012-06-22 12:22 ` Ian Campbell
2012-06-26 17:06   ` Ian Jackson
  -- strict thread matches above, loose matches on Subject: below --
2012-06-26 17:54 [PATCH v5 00/21] " Ian Jackson
2012-06-26 17:55 ` [PATCH 12/21] libxl: Make libxl__domain_save_device_model asynchronous Ian Jackson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1339761246-27641-13-git-send-email-ian.jackson@eu.citrix.com \
    --to=ian.jackson@eu.citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).