From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:37022) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UUEyM-000447-1X for qemu-devel@nongnu.org; Mon, 22 Apr 2013 07:31:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UUEyJ-00020S-FT for qemu-devel@nongnu.org; Mon, 22 Apr 2013 07:31:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:11449) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UUEyJ-00020F-8f for qemu-devel@nongnu.org; Mon, 22 Apr 2013 07:31:47 -0400 From: Kevin Wolf Date: Mon, 22 Apr 2013 13:31:15 +0200 Message-Id: <1366630294-18984-2-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> Subject: [Qemu-devel] [PATCH 01/20] qcow2: allow sub-cluster compressed write to last cluster List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: anthony@codemonkey.ws Cc: kwolf@redhat.com, qemu-devel@nongnu.org From: Stefan Hajnoczi 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 Signed-off-by: Kevin Wolf --- 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); -- 1.8.1.4