From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:58236) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SfY7o-0004tS-AQ for qemu-devel@nongnu.org; Fri, 15 Jun 2012 11:07:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SfY7h-0005De-UW for qemu-devel@nongnu.org; Fri, 15 Jun 2012 11:07:47 -0400 Received: from mail-pz0-f45.google.com ([209.85.210.45]:49781) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SfY7h-0004fx-MI for qemu-devel@nongnu.org; Fri, 15 Jun 2012 11:07:41 -0400 Received: by mail-pz0-f45.google.com with SMTP id n2so3246681dad.4 for ; Fri, 15 Jun 2012 08:07:40 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini Date: Fri, 15 Jun 2012 17:05:50 +0200 Message-Id: <1339772759-31004-28-git-send-email-pbonzini@redhat.com> In-Reply-To: <1339772759-31004-1-git-send-email-pbonzini@redhat.com> References: <1339772759-31004-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [RFC PATCH 27/36] block: add bdrv_ensure_backing_file List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, stefanha@linux.vnet.ibm.com, lcapitulino@redhat.com Mirroring runs without the backing file so that it can be copied outside QEMU. However, we need to add it at the time the job is completed and QEMU switches to the target. Factor out the common bits of opening an image and completing a mirroring operation. Signed-off-by: Paolo Bonzini --- block.c | 69 ++++++++++++++++++++++++++++++++++++++++----------------------- block.h | 1 + 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/block.c b/block.c index dbb9041..5b08c5b 100644 --- a/block.c +++ b/block.c @@ -730,6 +730,48 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) return 0; } +int bdrv_ensure_backing_file(BlockDriverState *bs) +{ + char backing_filename[PATH_MAX]; + int back_flags, ret; + BlockDriver *back_drv = NULL; + + if (bs->backing_hd != NULL) { + return 0; + } + + bs->open_flags &= ~BDRV_O_NO_BACKING; + if (bs->backing_file[0] == '\0') { + return 0; + } + + bs->backing_hd = bdrv_new(""); + bdrv_get_full_backing_filename(bs, backing_filename, + sizeof(backing_filename)); + + if (bs->backing_format[0] != '\0') { + back_drv = bdrv_find_format(bs->backing_format); + } + + /* backing files always opened read-only */ + back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT); + + ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); + if (ret < 0) { + bdrv_close(bs); + bdrv_delete(bs->backing_hd); + bs->backing_hd = NULL; + return ret; + } + if (bs->is_temporary) { + bs->backing_hd->keep_read_only = !(bs->open_flags & BDRV_O_RDWR); + } else { + /* base images use the same setting as leaf */ + bs->backing_hd->keep_read_only = bs->keep_read_only; + } + return 0; +} + /* * Opens a disk image (raw, qcow2, vmdk, ...) */ @@ -813,34 +855,11 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags, } /* If there is a backing file, use it */ - if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { - char backing_filename[PATH_MAX]; - int back_flags; - BlockDriver *back_drv = NULL; - - bs->backing_hd = bdrv_new(""); - bdrv_get_full_backing_filename(bs, backing_filename, - sizeof(backing_filename)); - - if (bs->backing_format[0] != '\0') { - back_drv = bdrv_find_format(bs->backing_format); - } - - /* backing files always opened read-only */ - back_flags = - flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); - - ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); + if ((flags & BDRV_O_NO_BACKING) == 0) { + ret = bdrv_ensure_backing_file(bs); if (ret < 0) { - bdrv_close(bs); return ret; } - if (bs->is_temporary) { - bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); - } else { - /* base image inherits from "parent" */ - bs->backing_hd->keep_read_only = bs->keep_read_only; - } } if (!bdrv_key_required(bs)) { diff --git a/block.h b/block.h index c13d779..a4aea47 100644 --- a/block.h +++ b/block.h @@ -122,6 +122,7 @@ void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top); void bdrv_delete(BlockDriverState *bs); int bdrv_parse_cache_flags(const char *mode, int *flags); int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags); +int bdrv_ensure_backing_file(BlockDriverState *bs); int bdrv_open(BlockDriverState *bs, const char *filename, int flags, BlockDriver *drv); void bdrv_close(BlockDriverState *bs); -- 1.7.10.2