aboutsummaryrefslogtreecommitdiff
path: root/block
diff options
context:
space:
mode:
authorVladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>2018-11-01 21:27:33 +0300
committerKevin Wolf <kwolf@redhat.com>2018-12-14 11:52:40 +0100
commit6994fd78b9a98e9cd32ee45c23edba1919cfbdd0 (patch)
tree0a1cbd63e8fa704f1ba0e04202b8f4c123bca946 /block
parent19a44488532eeccb522c6ee6c36f08b6c69809b0 (diff)
qcow2: make more generic interface for qcow2_compress
Give explicit size both for source and destination buffers, to make it similar with decompression path and than cleanly reuse parameter structure for decompression threads. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Reviewed-by: Alberto Garcia <berto@igalia.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Diffstat (limited to 'block')
-rw-r--r--block/qcow2.c30
1 files changed, 18 insertions, 12 deletions
diff --git a/block/qcow2.c b/block/qcow2.c
index ab1f7388a7..8606588aca 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -3722,14 +3722,15 @@ fail:
/*
* qcow2_compress()
*
- * @dest - destination buffer, at least of @size-1 bytes
- * @src - source buffer, @size bytes
+ * @dest - destination buffer, @dest_size bytes
+ * @src - source buffer, @src_size bytes
*
* Returns: compressed size on success
- * -1 if compression is inefficient
+ * -1 destination buffer is not enough to store compressed data
* -2 on any other error
*/
-static ssize_t qcow2_compress(void *dest, const void *src, size_t size)
+static ssize_t qcow2_compress(void *dest, size_t dest_size,
+ const void *src, size_t src_size)
{
ssize_t ret;
z_stream strm;
@@ -3744,14 +3745,14 @@ static ssize_t qcow2_compress(void *dest, const void *src, size_t size)
/* strm.next_in is not const in old zlib versions, such as those used on
* OpenBSD/NetBSD, so cast the const away */
- strm.avail_in = size;
+ strm.avail_in = src_size;
strm.next_in = (void *) src;
- strm.avail_out = size - 1;
+ strm.avail_out = dest_size;
strm.next_out = dest;
ret = deflate(&strm, Z_FINISH);
if (ret == Z_STREAM_END) {
- ret = size - 1 - strm.avail_out;
+ ret = dest_size - strm.avail_out;
} else {
ret = (ret == Z_OK ? -1 : -2);
}
@@ -3765,8 +3766,9 @@ static ssize_t qcow2_compress(void *dest, const void *src, size_t size)
typedef struct Qcow2CompressData {
void *dest;
+ size_t dest_size;
const void *src;
- size_t size;
+ size_t src_size;
ssize_t ret;
} Qcow2CompressData;
@@ -3774,7 +3776,8 @@ static int qcow2_compress_pool_func(void *opaque)
{
Qcow2CompressData *data = opaque;
- data->ret = qcow2_compress(data->dest, data->src, data->size);
+ data->ret = qcow2_compress(data->dest, data->dest_size,
+ data->src, data->src_size);
return 0;
}
@@ -3786,15 +3789,17 @@ static void qcow2_compress_complete(void *opaque, int ret)
/* See qcow2_compress definition for parameters description */
static ssize_t qcow2_co_compress(BlockDriverState *bs,
- void *dest, const void *src, size_t size)
+ void *dest, size_t dest_size,
+ const void *src, size_t src_size)
{
BDRVQcow2State *s = bs->opaque;
BlockAIOCB *acb;
ThreadPool *pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
Qcow2CompressData arg = {
.dest = dest,
+ .dest_size = dest_size,
.src = src,
- .size = size,
+ .src_size = src_size,
};
while (s->nb_compress_threads >= MAX_COMPRESS_THREADS) {
@@ -3861,7 +3866,8 @@ qcow2_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
out_buf = g_malloc(s->cluster_size);
- out_len = qcow2_co_compress(bs, out_buf, buf, s->cluster_size);
+ out_len = qcow2_co_compress(bs, out_buf, s->cluster_size - 1,
+ buf, s->cluster_size);
if (out_len == -2) {
ret = -EINVAL;
goto fail;