From patchwork Wed May 9 05:57:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 10388157 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 42CE360153 for ; Wed, 9 May 2018 06:03:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2D0CF20683 for ; Wed, 9 May 2018 06:03:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 21D4327F8F; Wed, 9 May 2018 06:03:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id AB47020683 for ; Wed, 9 May 2018 06:03:16 +0000 (UTC) Received: from localhost ([::1]:54674 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGIC3-0000nB-QA for patchwork-qemu-devel@patchwork.kernel.org; Wed, 09 May 2018 02:03:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49598) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGI7P-0005md-6G for qemu-devel@nongnu.org; Wed, 09 May 2018 01:58:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fGI7O-0000xI-AC for qemu-devel@nongnu.org; Wed, 09 May 2018 01:58:27 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:59956 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fGI7K-0000og-0n; Wed, 09 May 2018 01:58:22 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 821C64067F19; Wed, 9 May 2018 05:58:21 +0000 (UTC) Received: from lemon.usersys.redhat.com (ovpn-12-170.pek2.redhat.com [10.72.12.170]) by smtp.corp.redhat.com (Postfix) with ESMTP id B440D10F1BF7; Wed, 9 May 2018 05:58:17 +0000 (UTC) From: Fam Zheng To: qemu-devel@nongnu.org Date: Wed, 9 May 2018 13:57:59 +0800 Message-Id: <20180509055802.28423-3-famz@redhat.com> In-Reply-To: <20180509055802.28423-1-famz@redhat.com> References: <20180509055802.28423-1-famz@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Wed, 09 May 2018 05:58:21 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Wed, 09 May 2018 05:58:21 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'famz@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 2/5] vmdk: Refactor vmdk_create_extent X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , Michael Roth , qemu-block@nongnu.org, Markus Armbruster , Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP The extracted vmdk_init_extent takes a BlockBackend object and initializes the format metadata. It is the common part between "qemu-img create" and "blockdev-create". Add a "BlockBackend *pbb" parameter to vmdk_create_extent, to return the opened BB to the caller in the next patch. Signed-off-by: Fam Zheng --- block/vmdk.c | 71 +++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index 84f8bbe480..083942f806 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1714,35 +1714,17 @@ static int coroutine_fn vmdk_co_pwrite_zeroes(BlockDriverState *bs, return ret; } -static int vmdk_create_extent(const char *filename, int64_t filesize, - bool flat, bool compress, bool zeroed_grain, - QemuOpts *opts, Error **errp) +static int vmdk_init_extent(BlockBackend *blk, + int64_t filesize, bool flat, + bool compress, bool zeroed_grain, + Error **errp) { int ret, i; - BlockBackend *blk = NULL; VMDK4Header header; - Error *local_err = NULL; uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count; uint32_t *gd_buf = NULL; int gd_buf_size; - ret = bdrv_create_file(filename, opts, &local_err); - if (ret < 0) { - error_propagate(errp, local_err); - goto exit; - } - - blk = blk_new_open(filename, NULL, NULL, - BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, - &local_err); - if (blk == NULL) { - error_propagate(errp, local_err); - ret = -EIO; - goto exit; - } - - blk_set_allow_write_beyond_eof(blk, true); - if (flat) { ret = blk_truncate(blk, filesize, PREALLOC_MODE_OFF, errp); goto exit; @@ -1836,18 +1818,53 @@ static int vmdk_create_extent(const char *filename, int64_t filesize, gd_buf, gd_buf_size, 0); if (ret < 0) { error_setg(errp, QERR_IO_ERROR); - goto exit; } ret = 0; exit: - if (blk) { - blk_unref(blk); - } g_free(gd_buf); return ret; } +static int vmdk_create_extent(const char *filename, int64_t filesize, + bool flat, bool compress, bool zeroed_grain, + BlockBackend **pbb, + QemuOpts *opts, Error **errp) +{ + int ret; + BlockBackend *blk = NULL; + Error *local_err = NULL; + + ret = bdrv_create_file(filename, opts, &local_err); + if (ret < 0) { + error_propagate(errp, local_err); + goto exit; + } + + blk = blk_new_open(filename, NULL, NULL, + BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, + &local_err); + if (blk == NULL) { + error_propagate(errp, local_err); + ret = -EIO; + goto exit; + } + + blk_set_allow_write_beyond_eof(blk, true); + + ret = vmdk_init_extent(blk, filesize, flat, compress, zeroed_grain, errp); +exit: + if (blk) { + if (pbb) { + *pbb = blk; + } else { + blk_unref(blk); + blk = NULL; + } + } + return ret; +} + static int filename_decompose(const char *filename, char *path, char *prefix, char *postfix, size_t buf_len, Error **errp) { @@ -2067,7 +2084,7 @@ static int coroutine_fn vmdk_co_create_opts(const char *filename, QemuOpts *opts snprintf(ext_filename, PATH_MAX, "%s%s", path, desc_filename); if (vmdk_create_extent(ext_filename, size, - flat, compress, zeroed_grain, opts, errp)) { + flat, compress, zeroed_grain, NULL, opts, errp)) { ret = -EINVAL; goto exit; }