From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:52602) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RqT3F-0004xr-2p for qemu-devel@nongnu.org; Thu, 26 Jan 2012 12:24:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RqT2w-0003Ti-Gz for qemu-devel@nongnu.org; Thu, 26 Jan 2012 12:23:48 -0500 Received: from mail-iy0-f173.google.com ([209.85.210.173]:60416) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RqT2u-0003D7-Ny for qemu-devel@nongnu.org; Thu, 26 Jan 2012 12:23:38 -0500 Received: by mail-iy0-f173.google.com with SMTP id k25so1227300iah.4 for ; Thu, 26 Jan 2012 09:23:35 -0800 (PST) Sender: Paolo Bonzini From: Paolo Bonzini Date: Thu, 26 Jan 2012 18:22:45 +0100 Message-Id: <1327598569-5199-15-git-send-email-pbonzini@redhat.com> In-Reply-To: <1327598569-5199-1-git-send-email-pbonzini@redhat.com> References: <1327598569-5199-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PATCH v2 14/18] block: protect against "torn reads" for guest_block_size > host_block_size List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com When the guest sees a higher alignment than the host, writes may be done in multiple steps. So, reads have to be serialized against overlapping writes, so that the writes look atomic to the guest. This is true even when O_DIRECT is not in use. Signed-off-by: Paolo Bonzini --- block.c | 22 ++++++++++++++++++++++ 1 files changed, 22 insertions(+), 0 deletions(-) diff --git a/block.c b/block.c index c78ca47..683d4a3 100644 --- a/block.c +++ b/block.c @@ -1613,6 +1613,16 @@ static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, get_cluster_size(bs), false); } + /* When the guest sees a higher alignment than the host, writes may be + * done in multiple steps. So, reads have to be serialized against + * overlapping writes, so that the writes look atomic to the guest, + * even when O_DIRECT is not in use. + */ + if (bs->guest_block_size > bs->host_block_size) { + wait_for_overlapping_requests(bs, sector_num, nb_sectors, + bs->guest_block_size, true); + } + tracked_request_begin(&req, bs, sector_num, nb_sectors, false); if (flags & BDRV_REQ_COPY_ON_READ) { @@ -3629,6 +3639,18 @@ BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, void bdrv_set_guest_block_size(BlockDriverState *bs, int align) { bs->guest_block_size = align; + if ((bs->open_flags & BDRV_O_RDWR) && + bs->host_block_size < bs->guest_block_size) { + error_report("Host block size is %d, guest block size is %d. Due to partially\n" + "written sectors, power failures may cause data corruption.%s", + bs->host_block_size, bs->guest_block_size, + + /* The host block size might not be detected correctly if + * the guest is not using O_DIRECT. */ + (bs->open_flags & BDRV_O_NOCACHE) ? "" : "\n" + "If you think this message is wrong, start the guest with cache=none\n" + "and see if it disappears."); + } } void *qemu_blockalign(BlockDriverState *bs, size_t size) -- 1.7.7.6