From nobody Mon Feb 9 10:27:30 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1498734139997108.28781008350154; Thu, 29 Jun 2017 04:02:19 -0700 (PDT) Received: from localhost ([::1]:38332 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dQXDD-0002lc-KJ for importer@patchew.org; Thu, 29 Jun 2017 07:02:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51701) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dQX8f-00072N-3C for qemu-devel@nongnu.org; Thu, 29 Jun 2017 06:57:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dQX8d-00083u-N9 for qemu-devel@nongnu.org; Thu, 29 Jun 2017 06:57:33 -0400 Received: from mx-v6.kamp.de ([2a02:248:0:51::16]:39176 helo=mx01.kamp.de) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dQX8d-00081A-EO for qemu-devel@nongnu.org; Thu, 29 Jun 2017 06:57:31 -0400 Received: (qmail 9154 invoked by uid 89); 29 Jun 2017 10:57:28 -0000 Received: from [195.62.97.28] by client-16-kamp (envelope-from , uid 89) with qmail-scanner-2010/03/19-MF (clamdscan: 0.99.2/23517. avast: 1.2.2/17010300. spamassassin: 3.4.1. Clear:RC:1(195.62.97.28):. Processed in 0.236141 secs); 29 Jun 2017 10:57:28 -0000 Received: from smtp.kamp.de (HELO submission.kamp.de) ([195.62.97.28]) by mx01.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted); 29 Jun 2017 10:57:25 -0000 Received: (qmail 12785 invoked from network); 29 Jun 2017 10:57:15 -0000 Received: from lieven-pc.kamp-intra.net (HELO lieven-pc) (relay@kamp.de@::ffff:172.21.12.60) by submission.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted) ESMTPA; 29 Jun 2017 10:57:15 -0000 Received: by lieven-pc (Postfix, from userid 1000) id AF8A326C61; Thu, 29 Jun 2017 12:57:15 +0200 (CEST) X-GL_Whitelist: yes From: Peter Lieven To: qemu-block@nongnu.org Date: Thu, 29 Jun 2017 12:57:10 +0200 Message-Id: <1498733831-15254-8-git-send-email-pl@kamp.de> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1498733831-15254-1-git-send-email-pl@kamp.de> References: <1498733831-15254-1-git-send-email-pl@kamp.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a02:248:0:51::16 Subject: [Qemu-devel] [PATCH V2 7/8] block/qcow2: start using the compress format extension X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Peter Lieven , qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.org, lersek@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" we now pass the parameters to the zlib compressor if the extension is present and use the old default values if the extension is absent. Signed-off-by: Peter Lieven --- block/qcow2-cluster.c | 58 ++++++++++++++++++++++++++++++-----------------= ---- block/qcow2.c | 57 +++++++++++++++++++++++++++--------------------= --- 2 files changed, 65 insertions(+), 50 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 3d341fd..353ac87 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -1521,30 +1521,39 @@ again: } =20 static int decompress_buffer(uint8_t *out_buf, int out_buf_size, - const uint8_t *buf, int buf_size) + const uint8_t *buf, int buf_size, + uint32_t compress_format_id) { - z_stream strm1, *strm =3D &strm1; - int ret, out_len; - - memset(strm, 0, sizeof(*strm)); - - strm->next_in =3D (uint8_t *)buf; - strm->avail_in =3D buf_size; - strm->next_out =3D out_buf; - strm->avail_out =3D out_buf_size; - - ret =3D inflateInit2(strm, -12); - if (ret !=3D Z_OK) - return -1; - ret =3D inflate(strm, Z_FINISH); - out_len =3D strm->next_out - out_buf; - if ((ret !=3D Z_STREAM_END && ret !=3D Z_BUF_ERROR) || - out_len !=3D out_buf_size) { - inflateEnd(strm); - return -1; - } - inflateEnd(strm); - return 0; + int ret =3D 0, out_len; + + switch (compress_format_id) { + case QCOW2_COMPRESS_ZLIB: + case QCOW2_COMPRESS_ZLIB_COMPAT: { + z_stream z_strm =3D {}; + + z_strm.next_in =3D (uint8_t *)buf; + z_strm.avail_in =3D buf_size; + z_strm.next_out =3D out_buf; + z_strm.avail_out =3D out_buf_size; + + ret =3D inflateInit2(&z_strm, -15); + if (ret !=3D Z_OK) { + return -1; + } + ret =3D inflate(&z_strm, Z_FINISH); + out_len =3D z_strm.next_out - out_buf; + ret =3D -(ret !=3D Z_STREAM_END); + inflateEnd(&z_strm); + break; + } + default: + abort(); /* should never reach this point */ + } + + if (out_len !=3D out_buf_size) { + ret =3D -1; + } + return ret; } =20 int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset) @@ -1565,7 +1574,8 @@ int qcow2_decompress_cluster(BlockDriverState *bs, ui= nt64_t cluster_offset) return ret; } if (decompress_buffer(s->cluster_cache, s->cluster_size, - s->cluster_data + sector_offset, csize) < 0)= { + s->cluster_data + sector_offset, csize, + s->compress_format_id) < 0) { return -EIO; } s->cluster_cache_offset =3D coffset; diff --git a/block/qcow2.c b/block/qcow2.c index 0a7202a..b41e58d 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2748,9 +2748,10 @@ qcow2_co_pwritev_compressed(BlockDriverState *bs, ui= nt64_t offset, BDRVQcow2State *s =3D bs->opaque; QEMUIOVector hd_qiov; struct iovec iov; - z_stream strm; - int ret, out_len; - uint8_t *buf, *out_buf, *local_buf =3D NULL; + z_stream z_strm =3D {}; + int z_windowBits =3D -15, z_level =3D Z_DEFAULT_COMPRESSION; + int ret, out_len =3D 0; + uint8_t *buf, *out_buf =3D NULL, *local_buf =3D NULL; uint64_t cluster_offset; =20 if (bytes =3D=3D 0) { @@ -2775,34 +2776,38 @@ qcow2_co_pwritev_compressed(BlockDriverState *bs, u= int64_t offset, buf =3D qiov->iov[0].iov_base; } =20 - out_buf =3D g_malloc(s->cluster_size); + switch (s->compress_format_id) { + case QCOW2_COMPRESS_ZLIB_COMPAT: + z_windowBits =3D -12; + case QCOW2_COMPRESS_ZLIB: + out_buf =3D g_malloc(s->cluster_size); + if (s->compress_format.level > 0) { + z_level =3D s->compress_format.level; + } =20 - /* best compression, small window, no zlib header */ - memset(&strm, 0, sizeof(strm)); - ret =3D deflateInit2(&strm, Z_DEFAULT_COMPRESSION, - Z_DEFLATED, -12, - 9, Z_DEFAULT_STRATEGY); - if (ret !=3D 0) { - ret =3D -EINVAL; - goto fail; - } + ret =3D deflateInit2(&z_strm, z_level, Z_DEFLATED, z_windowBits, 9, + Z_DEFAULT_STRATEGY); + if (ret !=3D Z_OK) { + ret =3D -EINVAL; + goto fail; + } =20 - strm.avail_in =3D s->cluster_size; - strm.next_in =3D (uint8_t *)buf; - strm.avail_out =3D s->cluster_size; - strm.next_out =3D out_buf; + z_strm.avail_in =3D s->cluster_size; + z_strm.next_in =3D (uint8_t *)buf; + z_strm.avail_out =3D s->cluster_size; + z_strm.next_out =3D out_buf; =20 - ret =3D deflate(&strm, Z_FINISH); - if (ret !=3D Z_STREAM_END && ret !=3D Z_OK) { - deflateEnd(&strm); - ret =3D -EINVAL; - goto fail; - } - out_len =3D strm.next_out - out_buf; + ret =3D deflate(&z_strm, Z_FINISH); + out_len =3D z_strm.next_out - out_buf; + deflateEnd(&z_strm); =20 - deflateEnd(&strm); + ret =3D ret !=3D Z_STREAM_END; + break; + default: + abort(); /* should never reach this point */ + } =20 - if (ret !=3D Z_STREAM_END || out_len >=3D s->cluster_size) { + if (ret || out_len >=3D s->cluster_size) { /* could not compress: write normal cluster */ ret =3D qcow2_co_pwritev(bs, offset, bytes, qiov, 0); if (ret < 0) { --=20 1.9.1