From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:34179) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T3QSw-0004MH-04 for qemu-devel@nongnu.org; Mon, 20 Aug 2012 07:48:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T3QSu-0004C3-Na for qemu-devel@nongnu.org; Mon, 20 Aug 2012 07:48:17 -0400 Received: from mail-wi0-f175.google.com ([209.85.212.175]:37858) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T3QSu-0004Bt-HM for qemu-devel@nongnu.org; Mon, 20 Aug 2012 07:48:16 -0400 Received: by wibhm2 with SMTP id hm2so2779429wib.10 for ; Mon, 20 Aug 2012 04:48:15 -0700 (PDT) From: "=?UTF-8?q?Beno=C3=AEt=20Canet?=" Date: Mon, 20 Aug 2012 13:47:57 +0200 Message-Id: <1345463283-9684-4-git-send-email-benoit@irqsave.net> In-Reply-To: <1345463283-9684-1-git-send-email-benoit@irqsave.net> References: <1345463283-9684-1-git-send-email-benoit@irqsave.net> Subject: [Qemu-devel] [RFC V4 3/9] quorum: Add quorum_open() and quorum_close(). List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, stefanha@linux.vnet.ibm.com, blauwirbel@gmail.com, anthony@codemonkey.ws, pbonzini@redhat.com, eblake@redhat.com, afaerber@suse.de, =?UTF-8?q?Beno=C3=AEt=20Canet?= Valid quorum resources look like: quorum:threshold/total:path/to/image_1: ... :path/to/image_total '\' can escape the ':' character. Signed-off-by: Benoit Canet --- block/quorum.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/block/quorum.c b/block/quorum.c index 19a9a44..70e7216 100644 --- a/block/quorum.c +++ b/block/quorum.c @@ -52,11 +52,121 @@ struct QuorumAIOCB { int vote_ret; }; +/* Valid quorum resources look like + * quorum:threshold/total:path/to/image_1: ... :path/to/image_total + */ +static int quorum_open(BlockDriverState *bs, const char *filename, int flags) +{ + BDRVQuorumState *s = bs->opaque; + int i, j, len, ret = 0; + char *a, *b, *names; + bool escape; + + /* Parse the quorum: prefix */ + if (strncmp(filename, "quorum:", strlen("quorum:"))) { + return -EINVAL; + } + + filename += strlen("quorum:"); + + /* Get n */ + errno = 0; + s->threshold = strtoul(filename, &a, 10); + if (*a != '/' || errno) { + return -EINVAL; + } + a++; + + /* Get m */ + errno = 0; + s->total = strtoul(a, &b, 10); + if (*b != ':' || errno) { + return -EINVAL; + } + b++; + + if (s->threshold < 1 || s->total < 2) { + return -EINVAL; + } + + if (s->threshold > s->total) { + return -EINVAL; + } + + s->bs = g_malloc0(sizeof(BlockDriverState *) * s->total); + /* Two allocations for all filenames: simpler to free */ + s->filenames = g_malloc0(sizeof(char *) * s->total); + names = g_strdup(b); + + /* Get the filenames pointers */ + escape = false; + s->filenames[0] = names; + len = strlen(names); + for (i = 0, j = 1; i < len && j < s->total; i++) { + if (!escape && names[i] == ':') { + names[i] = '\0'; + s->filenames[j] = names + i + 1; + j++; + } + + escape = !escape && names[i] == '\\'; + } + + if (j != s->total) { + ret = -EINVAL; + goto free_exit; + } + + /* Open files */ + for (i = 0; i < s->total; i++) { + s->bs[i] = bdrv_new(""); + ret = bdrv_open(s->bs[i], s->filenames[i], flags, NULL); + if (ret < 0) { + goto error_exit; + } + } + + goto exit; + +error_exit: + for (; i >= 0; i--) { + bdrv_delete(s->bs[i]); + s->bs[i] = NULL; + } +free_exit: + g_free(s->filenames[0]); + g_free(s->filenames); + s->filenames = NULL; + g_free(s->bs); +exit: + return ret; +} + +static void quorum_close(BlockDriverState *bs) +{ + BDRVQuorumState *s = bs->opaque; + int i; + + for (i = 0; i < s->total; i++) { + /* Ensure writes reach stable storage */ + bdrv_flush(s->bs[i]); + bdrv_delete(s->bs[i]); + } + + g_free(s->filenames[0]); + g_free(s->filenames); + s->filenames = NULL; + g_free(s->bs); +} + static BlockDriver bdrv_quorum = { .format_name = "quorum", .protocol_name = "quorum", .instance_size = sizeof(BDRVQuorumState), + + .bdrv_file_open = quorum_open, + .bdrv_close = quorum_close, }; static void bdrv_quorum_init(void) -- 1.7.9.5