From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MKFbL-0007Zc-Cg for qemu-devel@nongnu.org; Fri, 26 Jun 2009 13:52:39 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MKFbI-0007Xp-0e for qemu-devel@nongnu.org; Fri, 26 Jun 2009 13:52:39 -0400 Received: from [199.232.76.173] (port=54111 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MKFbH-0007Xe-12 for qemu-devel@nongnu.org; Fri, 26 Jun 2009 13:52:35 -0400 Received: from mx2.redhat.com ([66.187.237.31]:55976) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MKFbG-00030e-1z for qemu-devel@nongnu.org; Fri, 26 Jun 2009 13:52:34 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n5QHqWF6006161 for ; Fri, 26 Jun 2009 13:52:32 -0400 From: Kevin Wolf Date: Fri, 26 Jun 2009 19:51:24 +0200 Message-Id: <1246038684-3702-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH] block-raw: Allow pread beyond the end of growable images List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf When using O_DIRECT, qcow2 snapshots didn't work any more for me. In the process of creating the snapshot, qcow2 tries to pwrite some new information (e.g. new L1 table) which will often end up being after the old end of the image file. Now pwrite tries to align things and reads the old contents of the file, read returns 0 because there is nothing to read after the end of file and pwrite is stuck in an endless loop. This patch allows to pread beyond the end of an image file. Whenever the given offset is after the end of the image file, the read succeeds and fills the buffer with zeros. Signed-off-by: Kevin Wolf --- block/raw-posix.c | 11 +++++++++++ 1 files changed, 11 insertions(+), 0 deletions(-) diff --git a/block/raw-posix.c b/block/raw-posix.c index fa1a394..985bf69 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -117,6 +117,7 @@ typedef struct BDRVRawState { static int posix_aio_init(void); static int fd_open(BlockDriverState *bs); +static int64_t raw_getlength(BlockDriverState *bs); #if defined(__FreeBSD__) static int cdrom_reopen(BlockDriverState *bs); @@ -231,6 +232,16 @@ static int raw_pread_aligned(BlockDriverState *bs, int64_t offset, if (ret == count) goto label__raw_read__success; + /* Allow reads beyond the end (needed for pwrite) */ + if ((ret == 0) && bs->growable) { + int64_t size = raw_getlength(bs); + if (offset >= size) { + memset(buf, 0, count); + ret = count; + goto label__raw_read__success; + } + } + DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 "] read failed %d : %d = %s\n", s->fd, bs->filename, offset, buf, count, -- 1.6.0.6