0a122b
From b24bec7c4477946f17bf4628608b60d4960b4247 Mon Sep 17 00:00:00 2001
0a122b
From: Jeffrey Cody <jcody@redhat.com>
0a122b
Date: Thu, 6 Mar 2014 21:13:15 +0100
0a122b
Subject: [PATCH 05/16] block: gluster - add reopen support.
0a122b
0a122b
RH-Author: Jeffrey Cody <jcody@redhat.com>
0a122b
Message-id: <2f1e357ca27ef45d16e62fbe44b0ce9cef2602fe.1394129674.git.jcody@redhat.com>
0a122b
Patchwork-id: 58041
0a122b
O-Subject: [RHEL7 qemu-kvm PATCH 2/2] block: gluster - add reopen support.
0a122b
Bugzilla: 1031526
0a122b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
0a122b
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
0a122b
0a122b
Gluster does parse open flags in its .bdrv_open() implementation,
0a122b
and the .bdrv_reopen_* implementations need to do the same.
0a122b
0a122b
A new gluster connection to the image file to be created is established
0a122b
in the .bdrv_reopen_prepare(), and the image file opened with the new
0a122b
flags.
0a122b
0a122b
If this is successful, then the old image file is closed, and the
0a122b
old connection torn down. The relevant structure pointers in the gluster
0a122b
state structure are updated to the new connection.
0a122b
0a122b
If it is not successful, then the new file handle and connection is
0a122b
abandoned (if it exists), while the old connection is not modified at
0a122b
all.
0a122b
0a122b
With reopen supported, block-commit (and offline commit) is now also
0a122b
supported for image files whose base image uses the native gluster
0a122b
protocol driver.
0a122b
0a122b
Signed-off-by: Jeff Cody <jcody@redhat.com>
0a122b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
(cherry picked from commit adccfbcd6020e928db93b2b4faf0dbd05ffbe016)
0a122b
0a122b
Conflicts:
0a122b
	block/gluster.c
0a122b
0a122b
RHEL7 Note:  Conflict was in contextual information, as RHEL7 does not
0a122b
             have gluster zerofill support.
0a122b
---
0a122b
 block/gluster.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0a122b
 1 file changed, 102 insertions(+)
0a122b
0a122b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
0a122b
---
0a122b
 block/gluster.c |  102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
0a122b
 1 files changed, 102 insertions(+), 0 deletions(-)
0a122b
0a122b
diff --git a/block/gluster.c b/block/gluster.c
0a122b
index 95cb05a..f43d3a6 100644
0a122b
--- a/block/gluster.c
0a122b
+++ b/block/gluster.c
0a122b
@@ -376,6 +376,96 @@ out:
0a122b
     return ret;
0a122b
 }
0a122b
 
0a122b
+typedef struct BDRVGlusterReopenState {
0a122b
+    struct glfs *glfs;
0a122b
+    struct glfs_fd *fd;
0a122b
+} BDRVGlusterReopenState;
0a122b
+
0a122b
+
0a122b
+static int qemu_gluster_reopen_prepare(BDRVReopenState *state,
0a122b
+                                       BlockReopenQueue *queue, Error **errp)
0a122b
+{
0a122b
+    int ret = 0;
0a122b
+    BDRVGlusterReopenState *reop_s;
0a122b
+    GlusterConf *gconf = NULL;
0a122b
+    int open_flags = 0;
0a122b
+
0a122b
+    assert(state != NULL);
0a122b
+    assert(state->bs != NULL);
0a122b
+
0a122b
+    state->opaque = g_malloc0(sizeof(BDRVGlusterReopenState));
0a122b
+    reop_s = state->opaque;
0a122b
+
0a122b
+    qemu_gluster_parse_flags(state->flags, &open_flags);
0a122b
+
0a122b
+    gconf = g_malloc0(sizeof(GlusterConf));
0a122b
+
0a122b
+    reop_s->glfs = qemu_gluster_init(gconf, state->bs->filename);
0a122b
+    if (reop_s->glfs == NULL) {
0a122b
+        ret = -errno;
0a122b
+        goto exit;
0a122b
+    }
0a122b
+
0a122b
+    reop_s->fd = glfs_open(reop_s->glfs, gconf->image, open_flags);
0a122b
+    if (reop_s->fd == NULL) {
0a122b
+        /* reops->glfs will be cleaned up in _abort */
0a122b
+        ret = -errno;
0a122b
+        goto exit;
0a122b
+    }
0a122b
+
0a122b
+exit:
0a122b
+    /* state->opaque will be freed in either the _abort or _commit */
0a122b
+    qemu_gluster_gconf_free(gconf);
0a122b
+    return ret;
0a122b
+}
0a122b
+
0a122b
+static void qemu_gluster_reopen_commit(BDRVReopenState *state)
0a122b
+{
0a122b
+    BDRVGlusterReopenState *reop_s = state->opaque;
0a122b
+    BDRVGlusterState *s = state->bs->opaque;
0a122b
+
0a122b
+
0a122b
+    /* close the old */
0a122b
+    if (s->fd) {
0a122b
+        glfs_close(s->fd);
0a122b
+    }
0a122b
+    if (s->glfs) {
0a122b
+        glfs_fini(s->glfs);
0a122b
+    }
0a122b
+
0a122b
+    /* use the newly opened image / connection */
0a122b
+    s->fd         = reop_s->fd;
0a122b
+    s->glfs       = reop_s->glfs;
0a122b
+
0a122b
+    g_free(state->opaque);
0a122b
+    state->opaque = NULL;
0a122b
+
0a122b
+    return;
0a122b
+}
0a122b
+
0a122b
+
0a122b
+static void qemu_gluster_reopen_abort(BDRVReopenState *state)
0a122b
+{
0a122b
+    BDRVGlusterReopenState *reop_s = state->opaque;
0a122b
+
0a122b
+    if (reop_s == NULL) {
0a122b
+        return;
0a122b
+    }
0a122b
+
0a122b
+    if (reop_s->fd) {
0a122b
+        glfs_close(reop_s->fd);
0a122b
+    }
0a122b
+
0a122b
+    if (reop_s->glfs) {
0a122b
+        glfs_fini(reop_s->glfs);
0a122b
+    }
0a122b
+
0a122b
+    g_free(state->opaque);
0a122b
+    state->opaque = NULL;
0a122b
+
0a122b
+    return;
0a122b
+}
0a122b
+
0a122b
 static int qemu_gluster_create(const char *filename,
0a122b
         QEMUOptionParameter *options, Error **errp)
0a122b
 {
0a122b
@@ -605,6 +695,9 @@ static BlockDriver bdrv_gluster = {
0a122b
     .protocol_name                = "gluster",
0a122b
     .instance_size                = sizeof(BDRVGlusterState),
0a122b
     .bdrv_file_open               = qemu_gluster_open,
0a122b
+    .bdrv_reopen_prepare          = qemu_gluster_reopen_prepare,
0a122b
+    .bdrv_reopen_commit           = qemu_gluster_reopen_commit,
0a122b
+    .bdrv_reopen_abort            = qemu_gluster_reopen_abort,
0a122b
     .bdrv_close                   = qemu_gluster_close,
0a122b
     .bdrv_create                  = qemu_gluster_create,
0a122b
     .bdrv_getlength               = qemu_gluster_getlength,
0a122b
@@ -622,6 +715,9 @@ static BlockDriver bdrv_gluster_tcp = {
0a122b
     .protocol_name                = "gluster+tcp",
0a122b
     .instance_size                = sizeof(BDRVGlusterState),
0a122b
     .bdrv_file_open               = qemu_gluster_open,
0a122b
+    .bdrv_reopen_prepare          = qemu_gluster_reopen_prepare,
0a122b
+    .bdrv_reopen_commit           = qemu_gluster_reopen_commit,
0a122b
+    .bdrv_reopen_abort            = qemu_gluster_reopen_abort,
0a122b
     .bdrv_close                   = qemu_gluster_close,
0a122b
     .bdrv_create                  = qemu_gluster_create,
0a122b
     .bdrv_getlength               = qemu_gluster_getlength,
0a122b
@@ -639,6 +735,9 @@ static BlockDriver bdrv_gluster_unix = {
0a122b
     .protocol_name                = "gluster+unix",
0a122b
     .instance_size                = sizeof(BDRVGlusterState),
0a122b
     .bdrv_file_open               = qemu_gluster_open,
0a122b
+    .bdrv_reopen_prepare          = qemu_gluster_reopen_prepare,
0a122b
+    .bdrv_reopen_commit           = qemu_gluster_reopen_commit,
0a122b
+    .bdrv_reopen_abort            = qemu_gluster_reopen_abort,
0a122b
     .bdrv_close                   = qemu_gluster_close,
0a122b
     .bdrv_create                  = qemu_gluster_create,
0a122b
     .bdrv_getlength               = qemu_gluster_getlength,
0a122b
@@ -656,6 +755,9 @@ static BlockDriver bdrv_gluster_rdma = {
0a122b
     .protocol_name                = "gluster+rdma",
0a122b
     .instance_size                = sizeof(BDRVGlusterState),
0a122b
     .bdrv_file_open               = qemu_gluster_open,
0a122b
+    .bdrv_reopen_prepare          = qemu_gluster_reopen_prepare,
0a122b
+    .bdrv_reopen_commit           = qemu_gluster_reopen_commit,
0a122b
+    .bdrv_reopen_abort            = qemu_gluster_reopen_abort,
0a122b
     .bdrv_close                   = qemu_gluster_close,
0a122b
     .bdrv_create                  = qemu_gluster_create,
0a122b
     .bdrv_getlength               = qemu_gluster_getlength,
0a122b
-- 
0a122b
1.7.1
0a122b