Patchwork [RFC,04/12] qorum: Add qorum_open().

login
register
mail settings
Submitter Benoit Canet
Date Aug. 2, 2012, 10:16 a.m.
Message ID <1343902604-13981-5-git-send-email-benoit@irqsave.net>
Download mbox | patch
Permalink /patch/174733/
State New
Headers show

Comments

Benoit Canet - Aug. 2, 2012, 10:16 a.m.
Signed-off-by: Benoit Canet <benoit@irqsave.net>
---
 block/qorum.c |   62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

Patch

diff --git a/block/qorum.c b/block/qorum.c
index ea2a720..bdf1530 100644
--- a/block/qorum.c
+++ b/block/qorum.c
@@ -48,11 +48,73 @@  struct QorumAIOCB {
     int vote_ret;
 };
 
+/* Valid qorum filenames look like
+ * qorum:path/to/a_image:path/to/b_image:path/to/c_image
+ */
+static int qorum_open(BlockDriverState *bs, const char *filename, int flags)
+{
+    BDRVQorumState *s = bs->opaque;
+    int ret, i;
+    char *a, *b, *c, *filenames[3];
+
+    /* Parse the qorum: prefix */
+    if (strncmp(filename, "qorum:", strlen("qorum:"))) {
+        return -EINVAL;
+    }
+    a = g_strdup(filename + strlen("qorum:"));
+
+    /* Find separators */
+    b = strchr(a, ':');
+    if (b == NULL) {
+        return -EINVAL;
+    }
+
+    c = strrchr(a, ':');
+    if (c == NULL) {
+        return -EINVAL;
+    }
+
+    /* Check that filename contains two separate ':' */
+    if (b == c) {
+        return -EINVAL;
+    }
+
+    /* Split string */
+    *b = '\0';
+    *c = '\0';
+
+    filenames[0] = a;
+    filenames[1] = b + 1;
+    filenames[2] = c + 1;
+
+    /* Open files */
+    for (i = 0; i <= 2; i++) {
+        s->bs[i] = bdrv_new("");
+        ret = bdrv_open(s->bs[i], filenames[i], flags, NULL);
+        if (ret < 0) {
+            goto error_exit;
+        }
+    }
+
+    goto clean_exit;
+
+error_exit:
+    for (; i >= 0; i--) {
+        bdrv_delete(s->bs[i]);
+        s->bs[i] = NULL;
+    }
+clean_exit:
+    g_free(a);
+    return ret;
+}
+
 static BlockDriver bdrv_qorum = {
     .format_name        = "qorum",
     .protocol_name      = "qorum",
 
     .instance_size      = sizeof(BDRVQorumState),
+
+    .bdrv_file_open     = qorum_open,
 };
 
 static void bdrv_qorum_init(void)