Received: from eggs.gnu.org ([2001:4830:134:3::10]:51075) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ViNvm-0006Yt-UG for qemu-devel@nongnu.org; Mon, 18 Nov 2013 07:28:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ViNve-0006vo-Fx for qemu-devel@nongnu.org; Mon, 18 Nov 2013 07:27:54 -0500 Received: from mail-wi0-x236.google.com ([2a00:1450:400c:c05::236]:65268) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ViNve-0006vb-62 for qemu-devel@nongnu.org; Mon, 18 Nov 2013 07:27:46 -0500 Received: by mail-wi0-f182.google.com with SMTP id en1so1872550wid.9 for ; Mon, 18 Nov 2013 04:27:45 -0800 (PST) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Mon, 18 Nov 2013 13:25:24 +0100 Message-Id: <1384777531-14635-15-git-send-email-marcandre.lureau@gmail.com> In-Reply-To: <1384777531-14635-1-git-send-email-marcandre.lureau@gmail.com> References: <1384777531-14635-1-git-send-email-marcandre.lureau@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [Qemu-devel] [PATCH 14/21] 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: kwolf@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , jcody@redhat.com, kraxel@redhat.com, spice-devel@freedesktop.org From: Marc-André Lureau Signed-off-by: Marc-André Lureau --- block.c | 121 +++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 70 insertions(+), 51 deletions(-) diff --git a/block.c b/block.c index 0558525..09aada5 100644 --- a/block.c +++ b/block.c @@ -1038,6 +1038,73 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp) return 0; } +static int make_snapshot(BlockDriverState *bs, int64_t total_size, + const char **pfilename, BlockDriver **pdrv, + Error **errp) +{ + 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]; + Error *local_err = NULL; + + 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; + error_setg_errno(errp, errno, "Could not resolve path '%s'", filename); + 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, &local_err); + free_option_parameters(create_options); + if (ret < 0) { + error_setg_errno(errp, -ret, "Could not create temporary overlay " + "'%s': %s", tmp_filename, + error_get_pretty(local_err)); + error_free(local_err); + local_err = NULL; + goto fail; + } + + *pfilename = tmp_filename; + *pdrv = bdrv_qcow2; + bs->is_temporary = 1; + return 0; + +fail: + return ret; +} + /* * Opens a disk image (raw, qcow2, vmdk, ...) * @@ -1050,8 +1117,6 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, int flags, BlockDriver *drv, Error **errp) { 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; const char *drvname; @@ -1069,73 +1134,27 @@ 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_setg(errp, "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, &local_err); if (ret < 0) { bdrv_unref(bs1); goto fail; } - total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; + total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; bdrv_unref(bs1); - ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); - if (ret < 0) { - error_setg_errno(errp, -ret, "Could not get temporary filename"); - 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; - error_setg_errno(errp, errno, "Could not resolve path '%s'", filename); - 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, &local_err); - free_option_parameters(create_options); + ret = make_snapshot(bs, total_size, &filename, &drv, errp); if (ret < 0) { - error_setg_errno(errp, -ret, "Could not create temporary overlay " - "'%s': %s", tmp_filename, - error_get_pretty(local_err)); - error_free(local_err); - local_err = NULL; goto fail; } - - filename = tmp_filename; - drv = bdrv_qcow2; - bs->is_temporary = 1; } /* Open image file without format layer */ -- 1.8.3.1