mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-11 03:24:58 -06:00
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:
parent
19a4448853
commit
6994fd78b9
1 changed files with 18 additions and 12 deletions
|
@ -3722,14 +3722,15 @@ fail:
|
||||||
/*
|
/*
|
||||||
* qcow2_compress()
|
* qcow2_compress()
|
||||||
*
|
*
|
||||||
* @dest - destination buffer, at least of @size-1 bytes
|
* @dest - destination buffer, @dest_size bytes
|
||||||
* @src - source buffer, @size bytes
|
* @src - source buffer, @src_size bytes
|
||||||
*
|
*
|
||||||
* Returns: compressed size on success
|
* 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
|
* -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;
|
ssize_t ret;
|
||||||
z_stream strm;
|
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
|
/* strm.next_in is not const in old zlib versions, such as those used on
|
||||||
* OpenBSD/NetBSD, so cast the const away */
|
* OpenBSD/NetBSD, so cast the const away */
|
||||||
strm.avail_in = size;
|
strm.avail_in = src_size;
|
||||||
strm.next_in = (void *) src;
|
strm.next_in = (void *) src;
|
||||||
strm.avail_out = size - 1;
|
strm.avail_out = dest_size;
|
||||||
strm.next_out = dest;
|
strm.next_out = dest;
|
||||||
|
|
||||||
ret = deflate(&strm, Z_FINISH);
|
ret = deflate(&strm, Z_FINISH);
|
||||||
if (ret == Z_STREAM_END) {
|
if (ret == Z_STREAM_END) {
|
||||||
ret = size - 1 - strm.avail_out;
|
ret = dest_size - strm.avail_out;
|
||||||
} else {
|
} else {
|
||||||
ret = (ret == Z_OK ? -1 : -2);
|
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 {
|
typedef struct Qcow2CompressData {
|
||||||
void *dest;
|
void *dest;
|
||||||
|
size_t dest_size;
|
||||||
const void *src;
|
const void *src;
|
||||||
size_t size;
|
size_t src_size;
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
} Qcow2CompressData;
|
} Qcow2CompressData;
|
||||||
|
|
||||||
|
@ -3774,7 +3776,8 @@ static int qcow2_compress_pool_func(void *opaque)
|
||||||
{
|
{
|
||||||
Qcow2CompressData *data = 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -3786,15 +3789,17 @@ static void qcow2_compress_complete(void *opaque, int ret)
|
||||||
|
|
||||||
/* See qcow2_compress definition for parameters description */
|
/* See qcow2_compress definition for parameters description */
|
||||||
static ssize_t qcow2_co_compress(BlockDriverState *bs,
|
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;
|
BDRVQcow2State *s = bs->opaque;
|
||||||
BlockAIOCB *acb;
|
BlockAIOCB *acb;
|
||||||
ThreadPool *pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
|
ThreadPool *pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
|
||||||
Qcow2CompressData arg = {
|
Qcow2CompressData arg = {
|
||||||
.dest = dest,
|
.dest = dest,
|
||||||
|
.dest_size = dest_size,
|
||||||
.src = src,
|
.src = src,
|
||||||
.size = size,
|
.src_size = src_size,
|
||||||
};
|
};
|
||||||
|
|
||||||
while (s->nb_compress_threads >= MAX_COMPRESS_THREADS) {
|
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_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) {
|
if (out_len == -2) {
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue