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>
This commit is contained in:
Vladimir Sementsov-Ogievskiy 2018-11-01 21:27:33 +03:00 committed by Kevin Wolf
parent 19a4448853
commit 6994fd78b9

View File

@ -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;