From patchwork Tue Aug 13 04:31:58 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Wang X-Patchwork-Id: 266702 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (unknown [IPv6:2001:4830:134:3::12]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id BF2BA2C0100 for ; Tue, 13 Aug 2013 14:44:03 +1000 (EST) Received: from localhost ([::1]:42832 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V96Sf-0002QX-P2 for incoming@patchwork.ozlabs.org; Tue, 13 Aug 2013 00:44:01 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55115) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V96Iq-0007tE-16 for qemu-devel@nongnu.org; Tue, 13 Aug 2013 00:34:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V96Ih-0000Z4-D1 for qemu-devel@nongnu.org; Tue, 13 Aug 2013 00:33:51 -0400 Received: from mail-pd0-x234.google.com ([2607:f8b0:400e:c02::234]:58137) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V96Ih-0000Yt-2u for qemu-devel@nongnu.org; Tue, 13 Aug 2013 00:33:43 -0400 Received: by mail-pd0-f180.google.com with SMTP id y10so4310451pdj.39 for ; Mon, 12 Aug 2013 21:33:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=i3EHhBVmLIene81esRqLJ05ieT4qKfSkls7VZI+Q+NU=; b=tbB9XRqvkKzJFZQrUWPNZODgoQP2G+ygsW07Z1IKm+LepNuR2C1HzBKZXNZTLrYdgs mqm/IBnZhBnb4eexecYfl/xh86VOW9mJMZN1RROtmlC7Qmp0gMIujRsKtQo4bMxWKtQi shUMBuy9QIBUKNrvX6QbBFLgdAoYuDicf51h/EfvveHTb3LflN0sqYeyUW9XKCkUxZsb 7vhjazFWevBS801n2MUa3SpKxdRMFqmHeb6/Ay9sLXkBBDFRlVyNYqUh6+uAuZVKD5/u 6qV1oWkoS+lURlU1JeqJFMVALgUl5yLbzWYlpDbcz0S/q9IJUdTT4eIOoJymNbNTCrY7 4pTQ== X-Received: by 10.68.20.200 with SMTP id p8mr2476080pbe.122.1376368422362; Mon, 12 Aug 2013 21:33:42 -0700 (PDT) Received: from 11.wdongxu.kvm58 ([202.108.130.153]) by mx.google.com with ESMTPSA id mz5sm10285023pbc.18.2013.08.12.21.33.39 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 12 Aug 2013 21:33:41 -0700 (PDT) From: Dong Xu Wang To: qemu-devel@nongnu.org Date: Tue, 13 Aug 2013 12:31:58 +0800 Message-Id: <1376368326-7433-18-git-send-email-wdongxu@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1376368326-7433-1-git-send-email-wdongxu@linux.vnet.ibm.com> References: <1376368326-7433-1-git-send-email-wdongxu@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400e:c02::234 Cc: kwolf@redhat.com, wdongxu@cn.ibm.com, stefanha@redhat.com, Dong Xu Wang Subject: [Qemu-devel] [PATCH V18 17/25] block: add QemuOpts support for sheepdog.c 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 Conflicts: block/sheepdog.c Signed-off-by: Dong Xu Wang --- block/sheepdog.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/block/sheepdog.c b/block/sheepdog.c index afe0533..b09b4fa 100644 --- a/block/sheepdog.c +++ b/block/sheepdog.c @@ -1546,6 +1546,92 @@ out: return ret; } +static int sd_create_new(const char *filename, QemuOpts *opts) +{ + int ret = 0; + uint32_t vid = 0, base_vid = 0; + int64_t vdi_size = 0; + char *backing_file = NULL, *buf = NULL; + BDRVSheepdogState *s; + char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN]; + uint32_t snapid; + bool prealloc = false; + + s = g_malloc0(sizeof(BDRVSheepdogState)); + + memset(vdi, 0, sizeof(vdi)); + memset(tag, 0, sizeof(tag)); + if (strstr(filename, "://")) { + ret = sd_parse_uri(s, filename, vdi, &snapid, tag); + } else { + ret = parse_vdiname(s, filename, vdi, &snapid, tag); + } + if (ret < 0) { + goto out; + } + + vdi_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); + backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); + buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); + if (!buf || !strcmp(buf, "off")) { + prealloc = false; + } else if (!strcmp(buf, "full")) { + prealloc = true; + } else { + error_report("Invalid preallocation mode: '%s'", buf); + ret = -EINVAL; + goto out; + } + if (vdi_size > SD_MAX_VDI_SIZE) { + error_report("too big image size"); + ret = -EINVAL; + goto out; + } + + if (backing_file) { + BlockDriverState *bs; + BDRVSheepdogState *s; + BlockDriver *drv; + + /* Currently, only Sheepdog backing image is supported. */ + drv = bdrv_find_protocol(backing_file, true); + if (!drv || strcmp(drv->protocol_name, "sheepdog") != 0) { + error_report("backing_file must be a sheepdog image"); + ret = -EINVAL; + goto out; + } + + ret = bdrv_file_open(&bs, backing_file, NULL, 0); + if (ret < 0) { + goto out; + } + + s = bs->opaque; + + if (!is_snapshot(&s->inode)) { + error_report("cannot clone from a non snapshot vdi"); + bdrv_delete(bs); + ret = -EINVAL; + goto out; + } + + base_vid = s->inode.vdi_id; + bdrv_delete(bs); + } + + ret = do_sd_create(s, vdi, vdi_size, base_vid, &vid, 0); + if (!prealloc || ret) { + goto out; + } + + ret = sd_prealloc(filename); +out: + g_free(backing_file); + g_free(buf); + g_free(s); + return ret; +} + static void sd_close(BlockDriverState *bs) { BDRVSheepdogState *s = bs->opaque; @@ -2340,6 +2426,29 @@ static QEMUOptionParameter sd_create_options[] = { { NULL } }; +static QemuOptsList sd_create_opts = { + .name = "sheepdog-create-opts", + .head = QTAILQ_HEAD_INITIALIZER(sd_create_opts.head), + .desc = { + { + .name = BLOCK_OPT_SIZE, + .type = QEMU_OPT_SIZE, + .help = "Virtual disk size" + }, + { + .name = BLOCK_OPT_BACKING_FILE, + .type = QEMU_OPT_STRING, + .help = "File name of a base image" + }, + { + .name = BLOCK_OPT_PREALLOC, + .type = QEMU_OPT_STRING, + .help = "Preallocation mode (allowed values: off, full)" + }, + { /* end of list */ } + } +}; + static BlockDriver bdrv_sheepdog = { .format_name = "sheepdog", .protocol_name = "sheepdog", @@ -2347,6 +2456,7 @@ static BlockDriver bdrv_sheepdog = { .bdrv_file_open = sd_open, .bdrv_close = sd_close, .bdrv_create = sd_create, + .bdrv_create_new = sd_create_new, .bdrv_has_zero_init = bdrv_has_zero_init_1, .bdrv_getlength = sd_getlength, .bdrv_truncate = sd_truncate, @@ -2366,6 +2476,7 @@ static BlockDriver bdrv_sheepdog = { .bdrv_load_vmstate = sd_load_vmstate, .create_options = sd_create_options, + .bdrv_create_opts = &sd_create_opts, }; static BlockDriver bdrv_sheepdog_tcp = {