From patchwork Mon Apr 22 11:31:16 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 238465 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id E2C962C007C for ; Mon, 22 Apr 2013 21:32:41 +1000 (EST) Received: from localhost ([::1]:34367 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UUEz8-0004RT-Nk for incoming@patchwork.ozlabs.org; Mon, 22 Apr 2013 07:32:38 -0400 Received: from eggs.gnu.org ([208.118.235.92]:37039) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UUEyM-00044D-Vt for qemu-devel@nongnu.org; Mon, 22 Apr 2013 07:31:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UUEyK-00021A-UT for qemu-devel@nongnu.org; Mon, 22 Apr 2013 07:31:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43224) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UUEyK-00020x-Lw for qemu-devel@nongnu.org; Mon, 22 Apr 2013 07:31:48 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r3MBVlgH020431 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 22 Apr 2013 07:31:47 -0400 Received: from dhcp-200-207.str.redhat.com (ovpn-116-69.ams2.redhat.com [10.36.116.69]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r3MBVfDW031324; Mon, 22 Apr 2013 07:31:45 -0400 From: Kevin Wolf To: anthony@codemonkey.ws Date: Mon, 22 Apr 2013 13:31:16 +0200 Message-Id: <1366630294-18984-3-git-send-email-kwolf@redhat.com> In-Reply-To: <1366630294-18984-1-git-send-email-kwolf@redhat.com> References: <1366630294-18984-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [Qemu-devel] [PATCH 02/20] qcow: allow sub-cluster compressed write to last cluster 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: Stefan Hajnoczi Compression in qcow requires image length to be a multiple of the cluster size. Lift this requirement by zero-padding the final cluster when necessary. The virtual disk size is still not cluster-aligned, so the guest cannot access the zero sectors. Note that this is almost identical to the qcow2 version of this code. qcow2's compression code is drawn from qcow. Signed-off-by: Stefan Hajnoczi Signed-off-by: Kevin Wolf --- block/qcow.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/block/qcow.c b/block/qcow.c index 3278e55..e2a64c7 100644 --- a/block/qcow.c +++ b/block/qcow.c @@ -787,8 +787,21 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num, uint8_t *out_buf; uint64_t cluster_offset; - if (nb_sectors != s->cluster_sectors) - return -EINVAL; + if (nb_sectors != s->cluster_sectors) { + ret = -EINVAL; + + /* Zero-pad last write if image size is not cluster aligned */ + if (sector_num + nb_sectors == bs->total_sectors && + nb_sectors < s->cluster_sectors) { + uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size); + memset(pad_buf, 0, s->cluster_size); + memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE); + ret = qcow_write_compressed(bs, sector_num, + pad_buf, s->cluster_sectors); + qemu_vfree(pad_buf); + } + return ret; + } out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);