From patchwork Thu Aug 22 20:10:59 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 269203 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 008942C0097 for ; Fri, 23 Aug 2013 07:15:12 +1000 (EST) Received: from localhost ([::1]:33468 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VCbGH-0004pw-TK for incoming@patchwork.ozlabs.org; Thu, 22 Aug 2013 16:13:41 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59114) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VCbEd-00033F-90 for qemu-devel@nongnu.org; Thu, 22 Aug 2013 16:12:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VCbEX-0004Dj-Vq for qemu-devel@nongnu.org; Thu, 22 Aug 2013 16:11:59 -0400 Received: from mx1.redhat.com ([209.132.183.28]:8763) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VCbEX-0004Db-Ol for qemu-devel@nongnu.org; Thu, 22 Aug 2013 16:11:53 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r7MKBoSQ002761 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 22 Aug 2013 16:11:50 -0400 Received: from localhost (ovpn-112-17.ams2.redhat.com [10.36.112.17]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r7MKBnqv010267; Thu, 22 Aug 2013 16:11:50 -0400 From: Stefan Hajnoczi To: Date: Thu, 22 Aug 2013 22:10:59 +0200 Message-Id: <1377202298-22896-4-git-send-email-stefanha@redhat.com> In-Reply-To: <1377202298-22896-1-git-send-email-stefanha@redhat.com> References: <1377202298-22896-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Asias He , Stefan Hajnoczi , MORITA Kazutaka , Anthony Liguori Subject: [Qemu-devel] [PULL 03/42] block: Produce zeros when protocols reading beyond end of file X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: MORITA Kazutaka While Asias is debugging an issue creating qcow2 images on top of non-file protocols. It boils down to this example using NBD: $ qemu-io -c 'open -g nbd+unix:///?socket=/tmp/nbd.sock' -c 'read -v 0 512' Notice the open -g option to set bs->growable. This means you can read/write beyond end of file. Reading beyond end of file is supposed to produce zeroes. We rely on this behavior in qcow2_create2() during qcow2 image creation. We create a new file and then write the qcow2 header structure using bdrv_pwrite(). Since QCowHeader is not a multiple of sector size, block.c first uses bdrv_read() on the empty file to fetch the first sector (should be all zeroes). Here is the output from the qemu-io NBD example above: $ qemu-io -c 'open -g nbd+unix:///?socket=/tmp/nbd.sock' -c 'read -v 0 512' 00000000: ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ................ 00000010: ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ................ 00000020: ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ................ ... We are not zeroing the buffer! As a result qcow2 image creation on top of protocols is not guaranteed to work even when file creation is supported by the protocol. [Adapted this patch to use bs->zero_beyond_eof. -- Stefan] Signed-off-by: MORITA Kazutaka Signed-off-by: Asias He Signed-off-by: Stefan Hajnoczi --- block.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/block.c b/block.c index e655052..1615d89 100644 --- a/block.c +++ b/block.c @@ -2571,7 +2571,35 @@ static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, } } - ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); + if (!(bs->zero_beyond_eof && bs->growable)) { + ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); + } else { + /* Read zeros after EOF of growable BDSes */ + int64_t len, total_sectors, max_nb_sectors; + + len = bdrv_getlength(bs); + if (len < 0) { + ret = len; + goto out; + } + + total_sectors = len >> BDRV_SECTOR_BITS; + max_nb_sectors = MAX(0, total_sectors - sector_num); + if (max_nb_sectors > 0) { + ret = drv->bdrv_co_readv(bs, sector_num, + MIN(nb_sectors, max_nb_sectors), qiov); + } else { + ret = 0; + } + + /* Reading beyond end of file is supposed to produce zeroes */ + if (ret == 0 && total_sectors < sector_num + nb_sectors) { + uint64_t offset = MAX(0, total_sectors - sector_num); + uint64_t bytes = (sector_num + nb_sectors - offset) * + BDRV_SECTOR_SIZE; + qemu_iovec_memset(qiov, offset * BDRV_SECTOR_SIZE, 0, bytes); + } + } out: tracked_request_end(&req);