From patchwork Tue Aug 14 14:14:05 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benoit Canet X-Patchwork-Id: 177286 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 2F7F42C0080 for ; Wed, 15 Aug 2012 00:14:43 +1000 (EST) Received: from localhost ([::1]:53159 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T1HtJ-0006t7-7F for incoming@patchwork.ozlabs.org; Tue, 14 Aug 2012 10:14:41 -0400 Received: from eggs.gnu.org ([208.118.235.92]:47416) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T1Ht3-0006j7-4y for qemu-devel@nongnu.org; Tue, 14 Aug 2012 10:14:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T1Hsy-0004Mr-PS for qemu-devel@nongnu.org; Tue, 14 Aug 2012 10:14:25 -0400 Received: from mail-wi0-f169.google.com ([209.85.212.169]:58222) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T1Hsy-0004Ml-Iu for qemu-devel@nongnu.org; Tue, 14 Aug 2012 10:14:20 -0400 Received: by wibhm2 with SMTP id hm2so3927580wib.4 for ; Tue, 14 Aug 2012 07:14:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=16y+PBkWr+yw0BqcYB+iIMKUklEZf6iRkd3TaA9EaJg=; b=w+3WsFx3VWTJuxoO9NrJSjWxw+8fyJ+3b65WQEhnQchGtJyxFxIuFGF1vf4hyoHTFJ 01rz2u8C0/5B7NWh02GoITG86Xy69YIXC4BqXXPCkGiAxxsOsflh5a4VE/zwrpG3qSOu LUm+6Y3XSb3vwySLrPPtOnnOiywIaLelGTaLKXfhGZSnqLIzEJtBwA4TjGlFsDhUgTiu sfyjbXzf9iU1f86kQijlrmPt5WeG893rWXTX/2iyzvZLnAQMP2g2UX/EpCAUO0Qqiwq/ QuUquqHGmsF0jjBWrvFqZhDgs/IWmyjU9ZFWrPeX8a+b0SxmqhNJXwKK3p44KFWsLWGB 1cBw== Received: by 10.216.132.135 with SMTP id o7mr7873653wei.6.1344953659783; Tue, 14 Aug 2012 07:14:19 -0700 (PDT) Received: from Laure.box.in.irqsave.net (paradis.irqsave.net. [109.190.18.76]) by mx.google.com with ESMTPS id j6sm33013649wiy.4.2012.08.14.07.14.18 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 14 Aug 2012 07:14:19 -0700 (PDT) From: "=?UTF-8?q?Beno=C3=AEt=20Canet?=" To: qemu-devel@nongnu.org Date: Tue, 14 Aug 2012 16:14:05 +0200 Message-Id: <1344953651-16622-4-git-send-email-benoit@irqsave.net> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1344953651-16622-1-git-send-email-benoit@irqsave.net> References: <1344953651-16622-1-git-send-email-benoit@irqsave.net> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.212.169 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?= Subject: [Qemu-devel] [RFC V3 3/9] quorum: Add quorum_open() and quorum_close(). X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Benoit Canet --- block/quorum.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/block/quorum.c b/block/quorum.c index bab6175..f228428 100644 --- a/block/quorum.c +++ b/block/quorum.c @@ -52,11 +52,124 @@ struct QuorumAIOCB { int vote_ret; }; +/* Valid quorum filenames look like + * quorum:n/m:path/to/image_1, ... ,path/to/image_m + */ +static int quorum_open(BlockDriverState *bs, const char *filename, int flags) +{ + BDRVQuorumState *s = bs->opaque; + int escape, i, j, len, ret = 0; + char *a, *b, *names; + + /* Parse the quorum: prefix */ + if (strncmp(filename, "quorum:", strlen("quorum:"))) { + return -EINVAL; + } + + filename += strlen("quorum:"); + + /* Get n */ + errno = 0; + s->n = strtoul(filename, &a, 10); + if (*a != '/' || errno) { + return -EINVAL; + } + a += 1; + + /* Get m */ + errno = 0; + s->m = strtoul(a, &b, 10); + if (*b != ':' || errno) { + return -EINVAL; + } + b += 1; + + if (s->n < 1 || s->m < 2) { + return -EINVAL; + } + + if (s->n > s->m) { + return -EINVAL; + } + + s->bs = g_malloc0(sizeof(BlockDriverState *) * s->m); + /* Two allocations for all filenames: simpler to free */ + s->filenames = g_malloc0(sizeof(char *) * s->m); + names = g_strdup(b); + + /* Get the filenames pointers */ + escape = 0; + s->filenames[0] = names; + len = strlen(names); + for (i = 0, j = 1; i < len && j < s->m; i++) { + if (!escape && names[i] == ':') { + names[i] = '\0'; + s->filenames[j] = names + i + 1; + j += 1; + } + + if (!escape && names[i] == '\\') { + escape = 1; + } else { + escape = 0; + } + } + + if (j != s->m) { + ret = -EINVAL; + goto free_exit; + } + + /* Open files */ + for (i = 0; i < s->m; 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->m; 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)