From patchwork Fri Apr 30 14:00:24 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 51333 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id BB241B7D43 for ; Sat, 1 May 2010 00:10:19 +1000 (EST) Received: from localhost ([127.0.0.1]:45621 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O7qrH-00006S-Rl for incoming@patchwork.ozlabs.org; Fri, 30 Apr 2010 10:06:23 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O7qmn-0006z1-GP for qemu-devel@nongnu.org; Fri, 30 Apr 2010 10:01:45 -0400 Received: from [140.186.70.92] (port=55015 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O7qmi-0006vh-SV for qemu-devel@nongnu.org; Fri, 30 Apr 2010 10:01:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O7qmg-0000Ji-KR for qemu-devel@nongnu.org; Fri, 30 Apr 2010 10:01:40 -0400 Received: from mx1.redhat.com ([209.132.183.28]:2802) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O7qmf-0000JU-Tj for qemu-devel@nongnu.org; Fri, 30 Apr 2010 10:01:38 -0400 Received: from int-mx04.intmail.prod.int.phx2.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.17]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o3UE1ZfR009932 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 30 Apr 2010 10:01:35 -0400 Received: from localhost.localdomain (vpn1-6-55.ams2.redhat.com [10.36.6.55]) by int-mx04.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o3UE14s1007196; Fri, 30 Apr 2010 10:01:32 -0400 From: Kevin Wolf To: aliguori@linux.vnet.ibm.com Date: Fri, 30 Apr 2010 16:00:24 +0200 Message-Id: <1272636040-17374-3-git-send-email-kwolf@redhat.com> In-Reply-To: <1272636040-17374-1-git-send-email-kwolf@redhat.com> References: <1272636040-17374-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.17 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [Qemu-devel] [PATCH 02/18] block: Split bdrv_open X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org bdrv_open contains quite some code that is only useful for opening images (as opposed to opening files by a protocol), for example snapshots. This patch splits the code so that we have bdrv_open_file() for files (uses protocols), bdrv_open() for images (uses format drivers) and bdrv_open_common() for the code common for opening both images and files. Signed-off-by: Kevin Wolf --- block.c | 135 ++++++++++++++++++++++++++++++++++++++++----------------------- 1 files changed, 86 insertions(+), 49 deletions(-) diff --git a/block.c b/block.c index ad681db..6efc2b3 100644 --- a/block.c +++ b/block.c @@ -42,6 +42,9 @@ #include #endif +static int bdrv_open_common(BlockDriverState *bs, const char *filename, + int flags, BlockDriver *drv); + static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque); @@ -350,6 +353,9 @@ static BlockDriver *find_image_format(const char *filename) return drv; } +/* + * Opens a file using a protocol (file, host_device, nbd, ...) + */ int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) { BlockDriverState *bs; @@ -362,7 +368,7 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) } bs = bdrv_new(""); - ret = bdrv_open(bs, filename, flags, drv); + ret = bdrv_open_common(bs, filename, flags, drv); if (ret < 0) { bdrv_delete(bs); return ret; @@ -372,19 +378,13 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) return 0; } +/* + * Opens a disk image (raw, qcow2, vmdk, ...) + */ int bdrv_open(BlockDriverState *bs, const char *filename, int flags, BlockDriver *drv) { - int ret, open_flags; - char tmp_filename[PATH_MAX]; - char backing_filename[PATH_MAX]; - - bs->is_temporary = 0; - bs->encrypted = 0; - bs->valid_key = 0; - bs->open_flags = flags; - /* buffer_alignment defaulted to 512, drivers can change this value */ - bs->buffer_alignment = 512; + int ret; if (flags & BDRV_O_SNAPSHOT) { BlockDriverState *bs1; @@ -392,6 +392,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags, int is_protocol = 0; BlockDriver *bdrv_qcow2; QEMUOptionParameter *options; + char tmp_filename[PATH_MAX]; + char backing_filename[PATH_MAX]; /* if snapshot, we create a temporary backing file and open it instead of opening 'filename' directly */ @@ -439,8 +441,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags, bs->is_temporary = 1; } - pstrcpy(bs->filename, sizeof(bs->filename), filename); - + /* Find the right image format driver */ if (!drv) { drv = find_image_format(filename); } @@ -449,11 +450,81 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags, ret = -ENOENT; goto unlink_and_fail; } - if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { - ret = -ENOTSUP; + + /* Open the image */ + ret = bdrv_open_common(bs, filename, flags, drv); + if (ret < 0) { goto unlink_and_fail; } + /* 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(""); + path_combine(backing_filename, sizeof(backing_filename), + filename, bs->backing_file); + 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 (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)) { + /* call the change callback */ + bs->media_changed = 1; + if (bs->change_cb) + bs->change_cb(bs->change_opaque); + } + + return 0; + +unlink_and_fail: + if (bs->is_temporary) { + unlink(filename); + } + return ret; +} + +/* + * Common part for opening disk images and files + */ +static int bdrv_open_common(BlockDriverState *bs, const char *filename, + int flags, BlockDriver *drv) +{ + int ret, open_flags; + + assert(drv != NULL); + + bs->is_temporary = 0; + bs->encrypted = 0; + bs->valid_key = 0; + bs->open_flags = flags; + /* buffer_alignment defaulted to 512, drivers can change this value */ + bs->buffer_alignment = 512; + + pstrcpy(bs->filename, sizeof(bs->filename), filename); + + if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { + return -ENOTSUP; + } + bs->drv = drv; bs->opaque = qemu_mallocz(drv->instance_size); @@ -493,46 +564,12 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags, unlink(filename); } #endif - if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { - /* if there is a backing file, use it */ - BlockDriver *back_drv = NULL; - bs->backing_hd = bdrv_new(""); - path_combine(backing_filename, sizeof(backing_filename), - filename, bs->backing_file); - if (bs->backing_format[0] != '\0') - back_drv = bdrv_find_format(bs->backing_format); - - /* backing files always opened read-only */ - open_flags &= ~BDRV_O_RDWR; - - ret = bdrv_open(bs->backing_hd, backing_filename, open_flags, back_drv); - 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)) { - /* call the change callback */ - bs->media_changed = 1; - if (bs->change_cb) - bs->change_cb(bs->change_opaque); - } return 0; free_and_fail: qemu_free(bs->opaque); bs->opaque = NULL; bs->drv = NULL; -unlink_and_fail: - if (bs->is_temporary) - unlink(filename); return ret; }