diff options
author | Max Reitz <mreitz@redhat.com> | 2017-06-13 22:21:02 +0200 |
---|---|---|
committer | Max Reitz <mreitz@redhat.com> | 2017-07-11 17:45:02 +0200 |
commit | 95b98f343b054e100a905181cd140c653c80cbad (patch) | |
tree | e4ccb479cdfe2d26ddefe76f8079974f852aff50 /block/qcow2.c | |
parent | 652fecd0058de7d90609c347bfdbeff76ef817a6 (diff) |
block/qcow2: Metadata preallocation for truncate
We can support PREALLOC_MODE_METADATA by invoking preallocate() in
qcow2_truncate().
Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20170613202107.10125-12-mreitz@redhat.com
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
Diffstat (limited to 'block/qcow2.c')
-rw-r--r-- | block/qcow2.c | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/block/qcow2.c b/block/qcow2.c index d3fb2f3f79..47537d0a3f 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -3077,10 +3077,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; @@ -3104,8 +3105,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; } @@ -3117,6 +3120,32 @@ 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"); + return ret; + } + break; + + default: + g_assert_not_reached(); + } + + if (prealloc != PREALLOC_MODE_OFF) { + /* Flush metadata before actually changing the image size */ + ret = bdrv_flush(bs); + if (ret < 0) { + error_setg_errno(errp, -ret, + "Failed to flush the preallocated area to disk"); + return ret; + } + } + /* write updated header.size */ offset = cpu_to_be64(offset); ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size), |