45e84a0
From 77a02621812952acfde887244f6f480de1b51f95 Mon Sep 17 00:00:00 2001
45e84a0
From: "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>
45e84a0
Date: Sun, 4 Dec 2011 22:35:28 +0530
45e84a0
Subject: [PATCH 04/25] hw/9pfs: use migration blockers to prevent live
45e84a0
 migration when virtfs export path is mounted
45e84a0
45e84a0
Now when you try to migrate with VirtFS export path mounted, you get a proper QMP error:
45e84a0
45e84a0
(qemu) migrate tcp:localhost:4444
45e84a0
Migration is disabled when VirtFS export path '/tmp/' is mounted in the guest using mount_tag 'v_tmp'
45e84a0
(qemu)
45e84a0
45e84a0
Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
45e84a0
---
45e84a0
 hw/9pfs/virtio-9p-device.c |   22 +++++++++++-----------
45e84a0
 hw/9pfs/virtio-9p.c        |   19 +++++++++++++++++++
45e84a0
 hw/9pfs/virtio-9p.h        |    5 +++--
45e84a0
 qerror.c                   |    5 +++++
45e84a0
 qerror.h                   |    3 +++
45e84a0
 5 files changed, 41 insertions(+), 13 deletions(-)
45e84a0
45e84a0
diff --git a/hw/9pfs/virtio-9p-device.c b/hw/9pfs/virtio-9p-device.c
45e84a0
index bba4c54..c9bca8b 100644
45e84a0
--- a/hw/9pfs/virtio-9p-device.c
45e84a0
+++ b/hw/9pfs/virtio-9p-device.c
45e84a0
@@ -33,13 +33,15 @@ static V9fsState *to_virtio_9p(VirtIODevice *vdev)
45e84a0
45e84a0
 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
45e84a0
 {
45e84a0
+    int len;
45e84a0
     struct virtio_9p_config *cfg;
45e84a0
     V9fsState *s = to_virtio_9p(vdev);
45e84a0
45e84a0
-    cfg = g_malloc0(sizeof(struct virtio_9p_config) +
45e84a0
-                        s->tag_len);
45e84a0
-    stw_raw(&cfg->tag_len, s->tag_len);
45e84a0
-    memcpy(cfg->tag, s->tag, s->tag_len);
45e84a0
+    len = strlen(s->tag);
45e84a0
+    cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
45e84a0
+    stw_raw(&cfg->tag_len, len);
45e84a0
+    /* We don't copy the terminating null to config space */
45e84a0
+    memcpy(cfg->tag, s->tag, len);
45e84a0
     memcpy(config, cfg, s->config_size);
45e84a0
     g_free(cfg);
45e84a0
 }
45e84a0
@@ -96,20 +98,18 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
45e84a0
     }
45e84a0
45e84a0
     len = strlen(conf->tag);
45e84a0
-    if (len > MAX_TAG_LEN) {
45e84a0
+    if (len > MAX_TAG_LEN - 1) {
45e84a0
         fprintf(stderr, "mount tag '%s' (%d bytes) is longer than "
45e84a0
-                "maximum (%d bytes)", conf->tag, len, MAX_TAG_LEN);
45e84a0
+                "maximum (%d bytes)", conf->tag, len, MAX_TAG_LEN - 1);
45e84a0
         exit(1);
45e84a0
     }
45e84a0
-    /* s->tag is non-NULL terminated string */
45e84a0
-    s->tag = g_malloc(len);
45e84a0
-    memcpy(s->tag, conf->tag, len);
45e84a0
-    s->tag_len = len;
45e84a0
+
45e84a0
+    s->tag = strdup(conf->tag);
45e84a0
     s->ctx.uid = -1;
45e84a0
45e84a0
     s->ops = fse->ops;
45e84a0
     s->vdev.get_features = virtio_9p_get_features;
45e84a0
-    s->config_size = sizeof(struct virtio_9p_config) + s->tag_len;
45e84a0
+    s->config_size = sizeof(struct virtio_9p_config) + len;
45e84a0
     s->vdev.get_config = virtio_9p_get_config;
45e84a0
     s->fid_list = NULL;
45e84a0
     qemu_co_rwlock_init(&s->rename_lock);
45e84a0
diff --git a/hw/9pfs/virtio-9p.c b/hw/9pfs/virtio-9p.c
45e84a0
index 1b2fc5d..32b98dd 100644
45e84a0
--- a/hw/9pfs/virtio-9p.c
45e84a0
+++ b/hw/9pfs/virtio-9p.c
45e84a0
@@ -23,6 +23,7 @@
45e84a0
 #include "virtio-9p-xattr.h"
45e84a0
 #include "virtio-9p-coth.h"
45e84a0
 #include "trace.h"
45e84a0
+#include "migration.h"
45e84a0
45e84a0
 int open_fd_hw;
45e84a0
 int total_open_fd;
45e84a0
@@ -373,6 +374,19 @@ static void put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
45e84a0
      * Don't free the fid if it is in reclaim list
45e84a0
      */
45e84a0
     if (!fidp->ref && fidp->clunked) {
45e84a0
+        if (fidp->fid == pdu->s->root_fid) {
45e84a0
+            /*
45e84a0
+             * if the clunked fid is root fid then we
45e84a0
+             * have unmounted the fs on the client side.
45e84a0
+             * delete the migration blocker. Ideally, this
45e84a0
+             * should be hooked to transport close notification
45e84a0
+             */
45e84a0
+            if (pdu->s->migration_blocker) {
45e84a0
+                migrate_del_blocker(pdu->s->migration_blocker);
45e84a0
+                error_free(pdu->s->migration_blocker);
45e84a0
+                pdu->s->migration_blocker = NULL;
45e84a0
+            }
45e84a0
+        }
45e84a0
         free_fid(pdu, fidp);
45e84a0
     }
45e84a0
 }
45e84a0
@@ -1235,6 +1249,11 @@ static void v9fs_attach(void *opaque)
45e84a0
     err = offset;
45e84a0
     trace_v9fs_attach_return(pdu->tag, pdu->id,
45e84a0
                              qid.type, qid.version, qid.path);
45e84a0
+    s->root_fid = fid;
45e84a0
+    /* disable migration */
45e84a0
+    error_set(&s->migration_blocker, QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION,
45e84a0
+              s->ctx.fs_root, s->tag);
45e84a0
+    migrate_add_blocker(s->migration_blocker);
45e84a0
 out:
45e84a0
     put_fid(pdu, fidp);
45e84a0
 out_nofid:
45e84a0
diff --git a/hw/9pfs/virtio-9p.h b/hw/9pfs/virtio-9p.h
45e84a0
index 7f88356..8b612da 100644
45e84a0
--- a/hw/9pfs/virtio-9p.h
45e84a0
+++ b/hw/9pfs/virtio-9p.h
45e84a0
@@ -246,8 +246,7 @@ typedef struct V9fsState
45e84a0
     V9fsFidState *fid_list;
45e84a0
     FileOperations *ops;
45e84a0
     FsContext ctx;
45e84a0
-    uint16_t tag_len;
45e84a0
-    uint8_t *tag;
45e84a0
+    char *tag;
45e84a0
     size_t config_size;
45e84a0
     enum p9_proto_version proto_version;
45e84a0
     int32_t msize;
45e84a0
@@ -256,6 +255,8 @@ typedef struct V9fsState
45e84a0
      * on rename.
45e84a0
      */
45e84a0
     CoRwlock rename_lock;
45e84a0
+    int32_t root_fid;
45e84a0
+    Error *migration_blocker;
45e84a0
 } V9fsState;
45e84a0
45e84a0
 typedef struct V9fsStatState {
45e84a0
diff --git a/qerror.c b/qerror.c
45e84a0
index fdf62b9..25bc91e 100644
45e84a0
--- a/qerror.c
45e84a0
+++ b/qerror.c
45e84a0
@@ -235,6 +235,11 @@ static const QErrorStringTable qerror_table[] = {
45e84a0
                      "supported by this qemu version: %(feature)",
45e84a0
     },
45e84a0
     {
45e84a0
+        .error_fmt = QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION,
45e84a0
+        .desc      = "Migration is disabled when VirtFS export path '%(path)' "
45e84a0
+                     "is mounted in the guest using mount_tag '%(tag)'",
45e84a0
+    },
45e84a0
+    {
45e84a0
         .error_fmt = QERR_VNC_SERVER_FAILED,
45e84a0
         .desc      = "Could not start VNC server on %(target)",
45e84a0
     },
45e84a0
diff --git a/qerror.h b/qerror.h
45e84a0
index 2d3d43b..6414cd9 100644
45e84a0
--- a/qerror.h
45e84a0
+++ b/qerror.h
45e84a0
@@ -192,6 +192,9 @@ QError *qobject_to_qerror(const QObject *obj);
45e84a0
 #define QERR_UNKNOWN_BLOCK_FORMAT_FEATURE \
45e84a0
     "{ 'class': 'UnknownBlockFormatFeature', 'data': { 'device': %s, 'format': %s, 'feature': %s } }"
45e84a0
45e84a0
+#define QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION \
45e84a0
+    "{ 'class': 'VirtFSFeatureBlocksMigration', 'data': { 'path': %s, 'tag': %s } }"
45e84a0
+
45e84a0
 #define QERR_VNC_SERVER_FAILED \
45e84a0
     "{ 'class': 'VNCServerFailed', 'data': { 'target': %s } }"
45e84a0
45e84a0
-- 
45e84a0
1.7.7.5
45e84a0