[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 09/41] qcow2: move decompression from qcow2-cluster.c
From: |
Kevin Wolf |
Subject: |
[Qemu-devel] [PULL 09/41] qcow2: move decompression from qcow2-cluster.c to qcow2.c |
Date: |
Wed, 12 Dec 2018 14:27:03 +0100 |
From: Vladimir Sementsov-Ogievskiy <address@hidden>
Compression is done in threads in qcow2.c. We want to do decompression
in the same way, so, firstly, move it to the same file.
The only change is braces around if-body in decompress_buffer, to
satisfy checkpatch.
Signed-off-by: Vladimir Sementsov-Ogievskiy <address@hidden>
Reviewed-by: Alberto Garcia <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
block/qcow2-cluster.c | 70 ------------------------------------------
block/qcow2.c | 71 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 71 insertions(+), 70 deletions(-)
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index d37fe08b3d..e2737429f5 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -1571,76 +1571,6 @@ again:
return 0;
}
-static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
- const uint8_t *buf, int buf_size)
-{
- z_stream strm1, *strm = &strm1;
- int ret, out_len;
-
- memset(strm, 0, sizeof(*strm));
-
- strm->next_in = (uint8_t *)buf;
- strm->avail_in = buf_size;
- strm->next_out = out_buf;
- strm->avail_out = out_buf_size;
-
- ret = inflateInit2(strm, -12);
- if (ret != Z_OK)
- return -1;
- ret = inflate(strm, Z_FINISH);
- out_len = strm->next_out - out_buf;
- if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
- out_len != out_buf_size) {
- inflateEnd(strm);
- return -1;
- }
- inflateEnd(strm);
- return 0;
-}
-
-int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
-{
- BDRVQcow2State *s = bs->opaque;
- int ret, csize, nb_csectors, sector_offset;
- uint64_t coffset;
-
- coffset = cluster_offset & s->cluster_offset_mask;
- if (s->cluster_cache_offset != coffset) {
- nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
- sector_offset = coffset & 511;
- csize = nb_csectors * 512 - sector_offset;
-
- /* Allocate buffers on first decompress operation, most images are
- * uncompressed and the memory overhead can be avoided. The buffers
- * are freed in .bdrv_close().
- */
- if (!s->cluster_data) {
- /* one more sector for decompressed data alignment */
- s->cluster_data = qemu_try_blockalign(bs->file->bs,
- QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size + 512);
- if (!s->cluster_data) {
- return -ENOMEM;
- }
- }
- if (!s->cluster_cache) {
- s->cluster_cache = g_malloc(s->cluster_size);
- }
-
- BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
- ret = bdrv_read(bs->file, coffset >> 9, s->cluster_data,
- nb_csectors);
- if (ret < 0) {
- return ret;
- }
- if (decompress_buffer(s->cluster_cache, s->cluster_size,
- s->cluster_data + sector_offset, csize) < 0) {
- return -EIO;
- }
- s->cluster_cache_offset = coffset;
- }
- return 0;
-}
-
/*
* This discards as many clusters of nb_clusters as possible at once (i.e.
* all clusters in the same L2 slice) and returns the number of discarded
diff --git a/block/qcow2.c b/block/qcow2.c
index 0c1569f715..d4d141c495 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -3762,6 +3762,34 @@ static ssize_t qcow2_compress(void *dest, size_t
dest_size,
return ret;
}
+static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
+ const uint8_t *buf, int buf_size)
+{
+ z_stream strm1, *strm = &strm1;
+ int ret, out_len;
+
+ memset(strm, 0, sizeof(*strm));
+
+ strm->next_in = (uint8_t *)buf;
+ strm->avail_in = buf_size;
+ strm->next_out = out_buf;
+ strm->avail_out = out_buf_size;
+
+ ret = inflateInit2(strm, -12);
+ if (ret != Z_OK) {
+ return -1;
+ }
+ ret = inflate(strm, Z_FINISH);
+ out_len = strm->next_out - out_buf;
+ if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
+ out_len != out_buf_size) {
+ inflateEnd(strm);
+ return -1;
+ }
+ inflateEnd(strm);
+ return 0;
+}
+
#define MAX_COMPRESS_THREADS 4
typedef struct Qcow2CompressData {
@@ -3915,6 +3943,49 @@ fail:
return ret;
}
+int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
+{
+ BDRVQcow2State *s = bs->opaque;
+ int ret, csize, nb_csectors, sector_offset;
+ uint64_t coffset;
+
+ coffset = cluster_offset & s->cluster_offset_mask;
+ if (s->cluster_cache_offset != coffset) {
+ nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
+ sector_offset = coffset & 511;
+ csize = nb_csectors * 512 - sector_offset;
+
+ /* Allocate buffers on first decompress operation, most images are
+ * uncompressed and the memory overhead can be avoided. The buffers
+ * are freed in .bdrv_close().
+ */
+ if (!s->cluster_data) {
+ /* one more sector for decompressed data alignment */
+ s->cluster_data = qemu_try_blockalign(bs->file->bs,
+ QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size + 512);
+ if (!s->cluster_data) {
+ return -ENOMEM;
+ }
+ }
+ if (!s->cluster_cache) {
+ s->cluster_cache = g_malloc(s->cluster_size);
+ }
+
+ BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
+ ret = bdrv_read(bs->file, coffset >> 9, s->cluster_data,
+ nb_csectors);
+ if (ret < 0) {
+ return ret;
+ }
+ if (decompress_buffer(s->cluster_cache, s->cluster_size,
+ s->cluster_data + sector_offset, csize) < 0) {
+ return -EIO;
+ }
+ s->cluster_cache_offset = coffset;
+ }
+ return 0;
+}
+
static int make_completely_empty(BlockDriverState *bs)
{
BDRVQcow2State *s = bs->opaque;
--
2.19.2
- [Qemu-devel] [PULL 00/41] Block layer patches, Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 01/41] block: adding lzfse decompressing support as a module., Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 02/41] configure: adding support to lzfse library., Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 03/41] dmg: including dmg-lzfse module inside dmg block driver., Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 05/41] block/replication: drop extra synchronization, Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 04/41] dmg: exchanging hardcoded dmg UDIF block types to enum., Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 09/41] qcow2: move decompression from qcow2-cluster.c to qcow2.c,
Kevin Wolf <=
- [Qemu-devel] [PULL 06/41] block/backup: drop unused synchronization interface, Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 08/41] qcow2: make more generic interface for qcow2_compress, Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 10/41] qcow2: refactor decompress_buffer, Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 11/41] qcow2: use byte-based read in qcow2_decompress_cluster, Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 07/41] qcow2: use Z_OK instead of 0 for deflateInit2 return code check, Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 13/41] qcow2: do decompression in threads, Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 12/41] qcow2: aio support for compressed cluster read, Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 16/41] file-posix: Avoid aio_worker() for QEMU_AIO_TRUNCATE, Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 14/41] file-posix: Reorganise RawPosixAIOData, Kevin Wolf, 2018/12/12
- [Qemu-devel] [PULL 15/41] file-posix: Factor out raw_thread_pool_submit(), Kevin Wolf, 2018/12/12