From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59179) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UpizB-0000lm-KY for qemu-devel@nongnu.org; Thu, 20 Jun 2013 13:49:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UpizA-0002uM-AD for qemu-devel@nongnu.org; Thu, 20 Jun 2013 13:49:29 -0400 Received: from mail-wi0-x236.google.com ([2a00:1450:400c:c05::236]:52637) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Upiwe-0002Is-Et for qemu-devel@nongnu.org; Thu, 20 Jun 2013 13:46:52 -0400 Received: by mail-wi0-f182.google.com with SMTP id m6so1960437wiv.9 for ; Thu, 20 Jun 2013 10:46:51 -0700 (PDT) From: "=?UTF-8?q?Marc-Andr=C3=A9=20Lureau?=" Date: Thu, 20 Jun 2013 19:46:07 +0200 Message-Id: <1371750371-18491-9-git-send-email-marcandre.lureau@redhat.com> In-Reply-To: <1371750371-18491-1-git-send-email-marcandre.lureau@redhat.com> References: <1371750371-18491-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [Qemu-devel] [PATCH 08/12] block: extract make_snapshot() from bdrv_open() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: spice-devel@lists.freedesktop.org, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Signed-off-by: Marc-André Lureau --- block.c | 107 +++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 62 insertions(+), 45 deletions(-) diff --git a/block.c b/block.c index f502eed..5db8fa1 100644 --- a/block.c +++ b/block.c @@ -959,6 +959,65 @@ static void extract_subqdict(QDict *src, QDict **dst, const char *start) } } +static int make_snapshot(BlockDriverState *bs, int64_t total_size, + const char **pfilename, BlockDriver **pdrv) +{ + const char *filename = *pfilename; + BlockDriver *drv = *pdrv; + int ret; + BlockDriver *bdrv_qcow2; + QEMUOptionParameter *create_options; + char backing_filename[PATH_MAX]; + /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ + char tmp_filename[PATH_MAX + 1]; + + assert(filename != NULL); + total_size &= BDRV_SECTOR_MASK; + + /* if snapshot, we create a temporary backing file and open it + instead of opening 'filename' directly */ + + ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); + if (ret < 0) { + goto fail; + } + + /* Real path is meaningless for protocols */ + if (path_has_protocol(filename)) { + snprintf(backing_filename, sizeof(backing_filename), + "%s", filename); + } else if (!realpath(filename, backing_filename)) { + ret = -errno; + goto fail; + } + + bdrv_qcow2 = bdrv_find_format("qcow2"); + create_options = parse_option_parameters("", bdrv_qcow2->create_options, + NULL); + + set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size); + set_option_parameter(create_options, BLOCK_OPT_BACKING_FILE, + backing_filename); + if (drv) { + set_option_parameter(create_options, BLOCK_OPT_BACKING_FMT, + drv->format_name); + } + + ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options); + free_option_parameters(create_options); + if (ret < 0) { + goto fail; + } + + *pfilename = tmp_filename; + *pdrv = bdrv_qcow2; + bs->is_temporary = 1; + return 0; + +fail: + return ret; +} + /* * Opens a disk image (raw, qcow2, vmdk, ...) * @@ -971,8 +1030,6 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, int flags, BlockDriver *drv) { int ret; - /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ - char tmp_filename[PATH_MAX + 1]; BlockDriverState *file = NULL; QDict *file_options = NULL; @@ -988,66 +1045,26 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, if (flags & BDRV_O_SNAPSHOT) { BlockDriverState *bs1; int64_t total_size; - BlockDriver *bdrv_qcow2; - QEMUOptionParameter *create_options; - char backing_filename[PATH_MAX]; if (qdict_size(options) != 0) { error_report("Can't use snapshot=on with driver-specific options"); ret = -EINVAL; goto fail; } - assert(filename != NULL); - /* if snapshot, we create a temporary backing file and open it - instead of opening 'filename' directly */ - - /* if there is a backing file, use it */ - bs1 = bdrv_new_int("", bs); + bs1 = bdrv_new_int("", NULL); ret = bdrv_open(bs1, filename, NULL, 0, drv); if (ret < 0) { bdrv_delete(bs1); goto fail; } - total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; - + total_size = bdrv_getlength(bs1); bdrv_delete(bs1); - ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); + ret = make_snapshot(bs, total_size, &filename, &drv); if (ret < 0) { goto fail; } - - /* Real path is meaningless for protocols */ - if (path_has_protocol(filename)) { - snprintf(backing_filename, sizeof(backing_filename), - "%s", filename); - } else if (!realpath(filename, backing_filename)) { - ret = -errno; - goto fail; - } - - bdrv_qcow2 = bdrv_find_format("qcow2"); - create_options = parse_option_parameters("", bdrv_qcow2->create_options, - NULL); - - set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size); - set_option_parameter(create_options, BLOCK_OPT_BACKING_FILE, - backing_filename); - if (drv) { - set_option_parameter(create_options, BLOCK_OPT_BACKING_FMT, - drv->format_name); - } - - ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options); - free_option_parameters(create_options); - if (ret < 0) { - goto fail; - } - - filename = tmp_filename; - drv = bdrv_qcow2; - bs->is_temporary = 1; } /* Open image file without format layer */ -- 1.8.3.rc1.49.g8d97506