From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41742) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dEIX6-00007k-HK for qemu-devel@nongnu.org; Fri, 26 May 2017 12:56:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dEIX5-0003kd-Os for qemu-devel@nongnu.org; Fri, 26 May 2017 12:56:12 -0400 From: Max Reitz Date: Fri, 26 May 2017 18:55:13 +0200 Message-Id: <20170526165518.7580-12-mreitz@redhat.com> In-Reply-To: <20170526165518.7580-1-mreitz@redhat.com> References: <20170526165518.7580-1-mreitz@redhat.com> Subject: [Qemu-devel] [PATCH v3 11/16] block/qcow2: Metadata preallocation for truncate List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-block@nongnu.org Cc: qemu-devel@nongnu.org, Max Reitz , Kevin Wolf , Stefan Hajnoczi We can support PREALLOC_MODE_METADATA by invoking preallocate() in qcow2_truncate(). Signed-off-by: Max Reitz Reviewed-by: Stefan Hajnoczi --- block/qcow2.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index a8223a0..2de27c3 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2616,10 +2616,11 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset, PreallocMode prealloc, Error **errp) { BDRVQcow2State *s = bs->opaque; + uint64_t old_length; int64_t new_l1_size; int ret; - if (prealloc != PREALLOC_MODE_OFF) { + if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA) { error_setg(errp, "Unsupported preallocation mode '%s'", PreallocMode_lookup[prealloc]); return -ENOTSUP; @@ -2636,8 +2637,10 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset, return -ENOTSUP; } + old_length = bs->total_sectors * 512; + /* shrinking is currently not supported */ - if (offset < bs->total_sectors * 512) { + if (offset < old_length) { error_setg(errp, "qcow2 doesn't support shrinking images yet"); return -ENOTSUP; } @@ -2649,6 +2652,23 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset, return ret; } + switch (prealloc) { + case PREALLOC_MODE_OFF: + break; + + case PREALLOC_MODE_METADATA: + ret = preallocate(bs, old_length, offset); + if (ret < 0) { + error_setg_errno(errp, -ret, "Preallocation failed, image may now " + "occupy more space than necessary"); + return ret; + } + break; + + default: + g_assert_not_reached(); + } + /* write updated header.size */ offset = cpu_to_be64(offset); ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size), -- 2.9.4