From patchwork Tue Feb 9 10:44:11 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naphtali Sprei X-Patchwork-Id: 44895 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 174E8B7CFA for ; Tue, 9 Feb 2010 21:48:15 +1100 (EST) Received: from localhost ([127.0.0.1]:42289 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Nenc2-0006wU-Jr for incoming@patchwork.ozlabs.org; Tue, 09 Feb 2010 05:46:34 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NenZy-0006us-Ao for qemu-devel@nongnu.org; Tue, 09 Feb 2010 05:44:26 -0500 Received: from [199.232.76.173] (port=55380 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NenZv-0006tC-7L for qemu-devel@nongnu.org; Tue, 09 Feb 2010 05:44:23 -0500 Received: from Debian-exim by monty-python.gnu.org with spam-scanned (Exim 4.60) (envelope-from ) id 1NenZs-0004sc-Q7 for qemu-devel@nongnu.org; Tue, 09 Feb 2010 05:44:22 -0500 Received: from mx1.redhat.com ([209.132.183.28]:44786) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NenZs-0004qk-8m for qemu-devel@nongnu.org; Tue, 09 Feb 2010 05:44:20 -0500 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o19AiFm8002132 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 9 Feb 2010 05:44:15 -0500 Received: from [10.35.0.60] (dhcp-0-60.tlv.redhat.com [10.35.0.60]) by int-mx05.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o19AiBiw027197 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 9 Feb 2010 05:44:13 -0500 Message-ID: <4B713C7B.8040006@redhat.com> Date: Tue, 09 Feb 2010 12:44:11 +0200 From: Naphtali Sprei User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: qemu-devel@nongnu.org X-Scanned-By: MIMEDefang 2.67 on 10.5.11.18 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: Kevin Wolf , Sheng Yang Subject: [Qemu-devel] [PATCH v4] block: more read-only changes, related to backing files 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 This version includes handling the case the backing file cannot be re-opened Open backing file read-only where possible Upgrade backing file to read-write during commit, back to read-only after commit If upgrade fail, back to read-only. If also fail, "disconnect" the drive. Added option for qemu-img.c bdrv_new_open() to open file as read-only Signed-off-by: Naphtali Sprei --- block.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++------- block_int.h | 2 + qemu-img.c | 15 +++++++--- 3 files changed, 82 insertions(+), 15 deletions(-) diff --git a/block.c b/block.c index 1919d19..612a24b 100644 --- a/block.c +++ b/block.c @@ -363,6 +363,7 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, 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; @@ -450,7 +451,6 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE)) bs->enable_write_cache = 1; - bs->read_only = (flags & BDRV_O_RDWR) == 0; if (!(flags & BDRV_O_FILE)) { open_flags = (flags & (BDRV_O_RDWR | BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO)); if (bs->is_temporary) { /* snapshot should be writeable */ @@ -465,6 +465,7 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, goto free_and_fail; } + bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); if (drv->bdrv_getlength) { bs->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; } @@ -481,13 +482,22 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, 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_open2(bs->backing_hd, backing_filename, open_flags, back_drv); - bs->backing_hd->read_only = (open_flags & BDRV_O_RDWR) == 0; 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)) { @@ -563,19 +573,48 @@ int bdrv_commit(BlockDriverState *bs) { BlockDriver *drv = bs->drv; int64_t i, total_sectors; - int n, j; - int ret = 0; + int n, j, ro, open_flags; + int ret = 0, rw_ret = 0; unsigned char sector[512]; + char filename[1024]; + BlockDriverState *bs_rw, *bs_ro; if (!drv) return -ENOMEDIUM; + + if (!bs->backing_hd) { + return -ENOTSUP; + } - if (bs->read_only) { + if (bs->backing_hd->keep_read_only) { return -EACCES; } - - if (!bs->backing_hd) { - return -ENOTSUP; + + ro = bs->backing_hd->read_only; + strncpy(filename, bs->backing_hd->filename, sizeof(filename)); + open_flags = bs->backing_hd->open_flags; + + if (ro) { + /* re-open as RW */ + bdrv_delete(bs->backing_hd); + bs->backing_hd = NULL; + bs_rw = bdrv_new(""); + rw_ret = bdrv_open2(bs_rw, filename, open_flags | BDRV_O_RDWR, NULL); + if (rw_ret < 0) { + bdrv_delete(bs_rw); + /* try to re-open read-only */ + bs_ro = bdrv_new(""); + ret = bdrv_open2(bs_ro, filename, open_flags & ~BDRV_O_RDWR, NULL); + if (ret < 0) { + bdrv_delete(bs_ro); + /* drive not functional anymore */ + bs->drv = NULL; + return ret; + } + bs->backing_hd = bs_ro; + return rw_ret; + } + bs->backing_hd = bs_rw; } total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; @@ -583,11 +622,13 @@ int bdrv_commit(BlockDriverState *bs) if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { for(j = 0; j < n; j++) { if (bdrv_read(bs, i, sector, 1) != 0) { - return -EIO; + ret = -EIO; + goto ro_cleanup; } if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { - return -EIO; + ret = -EIO; + goto ro_cleanup; } i++; } @@ -607,6 +648,25 @@ int bdrv_commit(BlockDriverState *bs) */ if (bs->backing_hd) bdrv_flush(bs->backing_hd); + +ro_cleanup: + + if (ro) { + /* re-open as RO */ + bdrv_delete(bs->backing_hd); + bs->backing_hd = NULL; + bs_ro = bdrv_new(""); + ret = bdrv_open2(bs_ro, filename, open_flags & ~BDRV_O_RDWR, NULL); + if (ret < 0) { + bdrv_delete(bs_ro); + /* drive not functional anymore */ + bs->drv = NULL; + return ret; + } + bs->backing_hd = bs_ro; + bs->backing_hd->keep_read_only = 0; + } + return ret; } diff --git a/block_int.h b/block_int.h index a0ebd90..93669da 100644 --- a/block_int.h +++ b/block_int.h @@ -130,6 +130,8 @@ struct BlockDriverState { int64_t total_sectors; /* if we are reading a disk image, give its size in sectors */ int read_only; /* if true, the media is read only */ + int keep_read_only; /* if true, the media was requested to stay read only */ + int open_flags; /* flags used to open the file, re-used for re-open */ int removable; /* if true, the media can be removed */ int locked; /* if true, the media cannot temporarily be ejected */ int encrypted; /* if true, the media is encrypted */ diff --git a/qemu-img.c b/qemu-img.c index eb5c0f0..d7b5c23 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -189,11 +189,13 @@ static int read_password(char *buf, int buf_size) #endif static BlockDriverState *bdrv_new_open(const char *filename, - const char *fmt) + const char *fmt, + int readonly) { BlockDriverState *bs; BlockDriver *drv; char password[256]; + int flags = BRDV_O_FLAGS; bs = bdrv_new(""); if (!bs) @@ -205,7 +207,10 @@ static BlockDriverState *bdrv_new_open(const char *filename, } else { drv = NULL; } - if (bdrv_open2(bs, filename, BRDV_O_FLAGS | BDRV_O_RDWR, drv) < 0) { + if (!readonly) { + flags |= BDRV_O_RDWR; + } + if (bdrv_open2(bs, filename, flags, drv) < 0) { error("Could not open '%s'", filename); } if (bdrv_is_encrypted(bs)) { @@ -344,7 +349,7 @@ static int img_create(int argc, char **argv) } } - bs = bdrv_new_open(backing_file->value.s, fmt); + bs = bdrv_new_open(backing_file->value.s, fmt, 1); bdrv_get_geometry(bs, &size); size *= 512; bdrv_delete(bs); @@ -628,7 +633,7 @@ static int img_convert(int argc, char **argv) total_sectors = 0; for (bs_i = 0; bs_i < bs_n; bs_i++) { - bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt); + bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, 1); if (!bs[bs_i]) error("Could not open '%s'", argv[optind + bs_i]); bdrv_get_geometry(bs[bs_i], &bs_sectors); @@ -686,7 +691,7 @@ static int img_convert(int argc, char **argv) } } - out_bs = bdrv_new_open(out_filename, out_fmt); + out_bs = bdrv_new_open(out_filename, out_fmt, 0); bs_i = 0; bs_offset = 0;