From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL v2 02/12] qcow2: refactor data compression
Date: Thu, 5 Jul 2018 12:25:54 +0200 [thread overview]
Message-ID: <20180705102604.4986-3-kwolf@redhat.com> (raw)
In-Reply-To: <20180705102604.4986-1-kwolf@redhat.com>
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Make a separate function for compression to be parallelized later.
- use .avail_out field instead of .next_out to calculate size of
compressed data. It looks more natural and it allows to keep dest to
be void pointer
- set avail_out to be at least one byte less than input, to be sure
avoid inefficient compression earlier
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
block/qcow2.c | 78 ++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 51 insertions(+), 27 deletions(-)
diff --git a/block/qcow2.c b/block/qcow2.c
index 2f9e58e0c4..9cee653d96 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -23,11 +23,14 @@
*/
#include "qemu/osdep.h"
+
+#define ZLIB_CONST
+#include <zlib.h>
+
#include "block/block_int.h"
#include "block/qdict.h"
#include "sysemu/block-backend.h"
#include "qemu/module.h"
-#include <zlib.h>
#include "qcow2.h"
#include "qemu/error-report.h"
#include "qapi/error.h"
@@ -3650,6 +3653,48 @@ fail:
return ret;
}
+/*
+ * qcow2_compress()
+ *
+ * @dest - destination buffer, at least of @size-1 bytes
+ * @src - source buffer, @size bytes
+ *
+ * Returns: compressed size on success
+ * -1 if compression is inefficient
+ * -2 on any other error
+ */
+static ssize_t qcow2_compress(void *dest, const void *src, size_t size)
+{
+ ssize_t ret;
+ z_stream strm;
+
+ /* best compression, small window, no zlib header */
+ memset(&strm, 0, sizeof(strm));
+ ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
+ -12, 9, Z_DEFAULT_STRATEGY);
+ if (ret != 0) {
+ return -2;
+ }
+
+ /* 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.next_in = (void *) src;
+ strm.avail_out = size - 1;
+ strm.next_out = dest;
+
+ ret = deflate(&strm, Z_FINISH);
+ if (ret == Z_STREAM_END) {
+ ret = size - 1 - strm.avail_out;
+ } else {
+ ret = (ret == Z_OK ? -1 : -2);
+ }
+
+ deflateEnd(&strm);
+
+ return ret;
+}
+
/* XXX: put compressed sectors first, then all the cluster aligned
tables to avoid losing bytes in alignment */
static coroutine_fn int
@@ -3659,8 +3704,8 @@ qcow2_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
BDRVQcow2State *s = bs->opaque;
QEMUIOVector hd_qiov;
struct iovec iov;
- z_stream strm;
- int ret, out_len;
+ int ret;
+ size_t out_len;
uint8_t *buf, *out_buf;
int64_t cluster_offset;
@@ -3694,32 +3739,11 @@ qcow2_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
out_buf = g_malloc(s->cluster_size);
- /* best compression, small window, no zlib header */
- memset(&strm, 0, sizeof(strm));
- ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
- Z_DEFLATED, -12,
- 9, Z_DEFAULT_STRATEGY);
- if (ret != 0) {
+ out_len = qcow2_compress(out_buf, buf, s->cluster_size);
+ if (out_len == -2) {
ret = -EINVAL;
goto fail;
- }
-
- strm.avail_in = s->cluster_size;
- strm.next_in = (uint8_t *)buf;
- strm.avail_out = s->cluster_size;
- strm.next_out = out_buf;
-
- ret = deflate(&strm, Z_FINISH);
- if (ret != Z_STREAM_END && ret != Z_OK) {
- deflateEnd(&strm);
- ret = -EINVAL;
- goto fail;
- }
- out_len = strm.next_out - out_buf;
-
- deflateEnd(&strm);
-
- if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
+ } else if (out_len == -1) {
/* could not compress: write normal cluster */
ret = qcow2_co_pwritev(bs, offset, bytes, qiov, 0);
if (ret < 0) {
--
2.13.6
next prev parent reply other threads:[~2018-07-05 10:26 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-05 10:25 [Qemu-devel] [PULL v2 00/12] Block layer patches Kevin Wolf
2018-07-05 10:25 ` [Qemu-devel] [PULL v2 01/12] qemu-img: allow compressed not-in-order writes Kevin Wolf
2018-07-05 10:25 ` Kevin Wolf [this message]
2018-07-05 10:25 ` [Qemu-devel] [PULL v2 03/12] qcow2: add compress threads Kevin Wolf
2018-07-05 10:25 ` [Qemu-devel] [PULL v2 04/12] block: Move two block permission constants to the relevant enum Kevin Wolf
2018-07-05 10:25 ` [Qemu-devel] [PULL v2 05/12] block: Add blklogwrites Kevin Wolf
2018-07-05 10:25 ` [Qemu-devel] [PULL v2 06/12] block: Don't silently truncate node names Kevin Wolf
2018-07-05 10:25 ` [Qemu-devel] [PULL v2 07/12] block/crypto: Fix memory leak in create error path Kevin Wolf
2018-07-05 10:26 ` [Qemu-devel] [PULL v2 08/12] block/blklogwrites: Change log_sector_size from int64_t to uint64_t Kevin Wolf
2018-07-05 10:26 ` [Qemu-devel] [PULL v2 09/12] block/blklogwrites: Add an option for appending to an old log Kevin Wolf
2018-07-05 16:06 ` Eric Blake
2018-07-06 12:26 ` Ari Sundholm
2018-07-05 10:26 ` [Qemu-devel] [PULL v2 10/12] block/blklogwrites: Add an option for the update interval of the log superblock Kevin Wolf
2018-07-05 10:26 ` [Qemu-devel] [PULL v2 11/12] file-posix: Fix creation locking Kevin Wolf
2018-07-05 10:26 ` [Qemu-devel] [PULL v2 12/12] file-posix: Unlock FD after creation Kevin Wolf
2018-07-05 15:50 ` [Qemu-devel] [PULL v2 00/12] Block layer patches Peter Maydell
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=20180705102604.4986-3-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=peter.maydell@linaro.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).