All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-devel@nongnu.org, qemu-block@nongnu.org
Cc: mreitz@redhat.com, kwolf@redhat.com, den@openvz.org,
	vsementsov@virtuozzo.com
Subject: [Qemu-devel] [PATCH 4/7] qcow2: refactor decompress_buffer
Date: Thu,  1 Nov 2018 21:27:35 +0300	[thread overview]
Message-ID: <20181101182738.70462-5-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20181101182738.70462-1-vsementsov@virtuozzo.com>

- make it look more like a pair of qcow2_compress - rename the function
  and its parameters
- drop extra out_len variable, check filling of output buffer by strm
  structure itself
- fix code style
- add some documentation

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
 block/qcow2.c | 56 ++++++++++++++++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 21 deletions(-)

diff --git a/block/qcow2.c b/block/qcow2.c
index b45a6c4135..e9d24b801e 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -3758,32 +3758,46 @@ 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)
+/* qcow2_decompress()
+ *
+ * Decompress some data (not more than @src_size bytes) to produce exactly
+ * @dest_size bytes.
+ *
+ * @dest - destination buffer, @dest_size bytes
+ * @src - source buffer, @src_size bytes
+ *
+ * Returns: 0 on success
+ *          -1 on fail
+ */
+static ssize_t qcow2_decompress(void *dest, size_t dest_size,
+                                const void *src, size_t src_size)
 {
-    z_stream strm1, *strm = &strm1;
-    int ret, out_len;
-
-    memset(strm, 0, sizeof(*strm));
+    int ret = 0;
+    z_stream strm;
 
-    strm->next_in = (uint8_t *)buf;
-    strm->avail_in = buf_size;
-    strm->next_out = out_buf;
-    strm->avail_out = out_buf_size;
+    memset(&strm, 0, sizeof(strm));
+    strm.avail_in = src_size;
+    strm.next_in = (void *) src;
+    strm.avail_out = dest_size;
+    strm.next_out = dest;
 
-    ret = inflateInit2(strm, -12);
+    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;
+
+    ret = inflate(&strm, Z_FINISH);
+    if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) || strm.avail_out != 0) {
+        /* We approve Z_BUF_ERROR because we need @dest buffer to be filled, but
+         * @src buffer may be processed partly (because in qcow2 we know size of
+         * compressed data with precision of one sector)
+         */
+        ret = -1;
     }
-    inflateEnd(strm);
-    return 0;
+
+    inflateEnd(&strm);
+
+    return ret;
 }
 
 #define MAX_COMPRESS_THREADS 4
@@ -3973,8 +3987,8 @@ int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
         if (ret < 0) {
             return ret;
         }
-        if (decompress_buffer(s->cluster_cache, s->cluster_size,
-                              s->cluster_data + sector_offset, csize) < 0) {
+        if (qcow2_decompress(s->cluster_cache, s->cluster_size,
+                             s->cluster_data + sector_offset, csize) < 0) {
             return -EIO;
         }
         s->cluster_cache_offset = coffset;
-- 
2.18.0

  parent reply	other threads:[~2018-11-01 18:27 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-01 18:27 [Qemu-devel] [PATCH 0/7] qcow2 decompress in threads Vladimir Sementsov-Ogievskiy
2018-11-01 18:27 ` [Qemu-devel] [PATCH 1/7] qcow2: use Z_OK instead of 0 for deflateInit2 return code check Vladimir Sementsov-Ogievskiy
2018-11-05 15:30   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2018-11-01 18:27 ` [Qemu-devel] [PATCH 2/7] qcow2: make more generic interface for qcow2_compress Vladimir Sementsov-Ogievskiy
2018-11-05 15:45   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2018-11-01 18:27 ` [Qemu-devel] [PATCH 3/7] qcow2: move decompression from qcow2-cluster.c to qcow2.c Vladimir Sementsov-Ogievskiy
2018-11-05 15:45   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2018-11-01 18:27 ` Vladimir Sementsov-Ogievskiy [this message]
2018-11-06 10:45   ` [Qemu-devel] [Qemu-block] [PATCH 4/7] qcow2: refactor decompress_buffer Alberto Garcia
2018-11-01 18:27 ` [Qemu-devel] [PATCH 5/7] qcow2: use byte-based read in qcow2_decompress_cluster Vladimir Sementsov-Ogievskiy
2018-11-06 13:53   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2018-11-06 15:18     ` Vladimir Sementsov-Ogievskiy
2018-11-01 18:27 ` [Qemu-devel] [PATCH 6/7] qcow2: aio support for compressed cluster read Vladimir Sementsov-Ogievskiy
2018-11-06 15:06   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2018-11-06 15:13     ` Vladimir Sementsov-Ogievskiy
2018-11-06 15:30       ` Alberto Garcia
2018-11-06 16:24         ` Vladimir Sementsov-Ogievskiy
2018-11-09 13:51           ` Alberto Garcia
2018-11-01 18:27 ` [Qemu-devel] [PATCH 7/7] qcow2: do decompression in threads Vladimir Sementsov-Ogievskiy
2018-11-06 14:03   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2018-11-06 23:40   ` [Qemu-devel] " Paolo Bonzini
2018-11-12 15:32 ` [Qemu-devel] [PATCH 0/7] qcow2 decompress " Kevin Wolf
2018-11-12 16:16   ` Vladimir Sementsov-Ogievskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181101182738.70462-5-vsementsov@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=den@openvz.org \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.