From patchwork Tue Apr 16 09:14:20 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 236870 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 80E7D2C0109 for ; Tue, 16 Apr 2013 19:15:19 +1000 (EST) Received: from localhost ([::1]:58062 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US1yu-00055z-OG for incoming@patchwork.ozlabs.org; Tue, 16 Apr 2013 05:15:16 -0400 Received: from eggs.gnu.org ([208.118.235.92]:41039) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US1yH-0004x6-1R for qemu-devel@nongnu.org; Tue, 16 Apr 2013 05:14:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1US1yD-00060R-MQ for qemu-devel@nongnu.org; Tue, 16 Apr 2013 05:14:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52392) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US1yD-00060M-FI for qemu-devel@nongnu.org; Tue, 16 Apr 2013 05:14:33 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r3G9EUHh018260 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 16 Apr 2013 05:14:30 -0400 Received: from localhost (ovpn-112-17.ams2.redhat.com [10.36.112.17]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r3G9ETul030172; Tue, 16 Apr 2013 05:14:29 -0400 From: Stefan Hajnoczi To: Date: Tue, 16 Apr 2013 11:14:20 +0200 Message-Id: <1366103663-11574-2-git-send-email-stefanha@redhat.com> In-Reply-To: <1366103663-11574-1-git-send-email-stefanha@redhat.com> References: <1366103663-11574-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Fam Zheng , ilkka.tengvall@cybercom.com, Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v2 1/4] qcow2: 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 Compression in qcow2 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. Signed-off-by: Stefan Hajnoczi --- block/qcow2.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index e8934de..2e346d8 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1537,8 +1537,21 @@ static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num, return 0; } - 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 = qcow2_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);