qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Denis Plotnikov <dplotnikov@virtuozzo.com>
To: kwolf@redhat.com, mreitz@redhat.com, eblake@redhat.com,
	armbru@redhat.com, qemu-block@nongnu.org
Cc: vsementsov@virtuozzo.com, qemu-devel@nongnu.org, den@virtuozzo.com
Subject: [Qemu-devel] [PATCH v0 2/3] qcow2: add compression type processing
Date: Tue, 28 May 2019 17:37:26 +0300	[thread overview]
Message-ID: <20190528143727.10529-3-dplotnikov@virtuozzo.com> (raw)
In-Reply-To: <20190528143727.10529-1-dplotnikov@virtuozzo.com>

With the patch, qcow2 is able to process image compression type
defined in the image header and choose the corresponding method
for clusters compressing.

Also, it rework the cluster compression code for adding more
compression types.

Signed-off-by: Denis Plotnikov <dplotnikov@virtuozzo.com>
---
 block/qcow2.c | 103 ++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 92 insertions(+), 11 deletions(-)

diff --git a/block/qcow2.c b/block/qcow2.c
index c4b5b93408..90f15cc3c9 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -400,11 +400,39 @@ static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
             break;
 
         case QCOW2_EXT_MAGIC_COMPRESSION_TYPE:
+            /* Compression type always goes with the compression type bit set */
+            if (!(s->incompatible_features & QCOW2_INCOMPAT_COMPRESSION_TYPE)) {
+                error_setg(errp,
+                           "compression_type_ext: "
+                           "expect compression type bit set");
+                return -EINVAL;
+            }
+
+            ret = bdrv_pread(bs->file, offset, &s->compression_type, ext.len);
+            s->compression_type = be32_to_cpu(s->compression_type);
+
+            if (ret < 0) {
+                error_setg_errno(errp, -ret,
+                                 "ERROR: Could not read compression type");
+                return ret;
+            }
+
             /*
-             * Setting compression type to BDRVQcow2State->compression_type
-             * from the image header is going to be here
+             * The default compression type is not allowed when the extension
+             * is present. ZLIB is used as the default compression type.
+             * When compression type extension header is present then
+             * compression_type should have a value different from the default.
              */
-             break;
+            if (s->compression_type == QCOW2_COMPRESSION_TYPE_ZLIB) {
+                error_setg(errp,
+                           "compression_type_ext:"
+                           "invalid compression type %d",
+                           QCOW2_COMPRESSION_TYPE_ZLIB);
+            }
+#ifdef DEBUG_EXT
+            printf("Qcow2: image compression type %s\n", s->compression_type);
+#endif
+            break;
 
         case QCOW2_EXT_MAGIC_DATA_FILE:
         {
@@ -1492,6 +1520,9 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
     QLIST_INIT(&s->cluster_allocs);
     QTAILQ_INIT(&s->discards);
 
+    /* Set default compression type */
+    s->compression_type = QCOW2_COMPRESSION_TYPE_ZLIB;
+
     /* read qcow2 extensions */
     if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
                               flags, &update_header, &local_err)) {
@@ -1500,6 +1531,34 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
         goto fail;
     }
 
+    /*
+     * The compression type is set on the extension header processing
+     * if the compression type extension header is present.
+     * When the compression type is different from ZLIB (default) there
+     * should be both the compression type bit and the compression
+     * type extension header set. When the ZLIB (default) compression
+     * type is used there should be neither the compression type bit nor
+     * the compression type extension header set.
+     */
+
+    if ((s->incompatible_features & QCOW2_INCOMPAT_COMPRESSION_TYPE) &
+        (s->compression_type == QCOW2_COMPRESSION_TYPE_ZLIB)) {
+        error_setg(errp, "Illegal compression type setting");
+        ret = -EINVAL;
+        goto fail;
+    }
+
+    /* Check available compression types */
+    switch (s->compression_type) {
+    case QCOW2_COMPRESSION_TYPE_ZLIB:
+        break;
+
+    default:
+        error_setg(errp, "Unknown compression type");
+        ret = -EINVAL;
+        goto fail;
+    }
+
     /* Open external data file */
     s->data_file = bdrv_open_child(NULL, options, "data-file", bs, &child_file,
                                    true, &local_err);
@@ -3970,7 +4029,7 @@ fail:
 }
 
 /*
- * qcow2_compress()
+ * zlib_compress()
  *
  * @dest - destination buffer, @dest_size bytes
  * @src - source buffer, @src_size bytes
@@ -3979,7 +4038,7 @@ fail:
  *          -1 destination buffer is not enough to store compressed data
  *          -2 on any other error
  */
-static ssize_t qcow2_compress(void *dest, size_t dest_size,
+static ssize_t zlib_compress(void *dest, size_t dest_size,
                               const void *src, size_t src_size)
 {
     ssize_t ret;
@@ -4013,7 +4072,7 @@ static ssize_t qcow2_compress(void *dest, size_t dest_size,
 }
 
 /*
- * qcow2_decompress()
+ * zlib_decompress()
  *
  * Decompress some data (not more than @src_size bytes) to produce exactly
  * @dest_size bytes.
@@ -4024,7 +4083,7 @@ static ssize_t qcow2_compress(void *dest, size_t dest_size,
  * Returns: 0 on success
  *          -1 on fail
  */
-static ssize_t qcow2_decompress(void *dest, size_t dest_size,
+static ssize_t zlib_decompress(void *dest, size_t dest_size,
                                 const void *src, size_t src_size)
 {
     int ret = 0;
@@ -4122,16 +4181,38 @@ static ssize_t coroutine_fn
 qcow2_co_compress(BlockDriverState *bs, void *dest, size_t dest_size,
                   const void *src, size_t src_size)
 {
-    return qcow2_co_do_compress(bs, dest, dest_size, src, src_size,
-                                qcow2_compress);
+    BDRVQcow2State *s = bs->opaque;
+    Qcow2CompressFunc fn;
+
+    switch (s->compression_type) {
+    case QCOW2_COMPRESSION_TYPE_ZLIB:
+        fn = zlib_compress;
+        break;
+
+    default:
+        return -ENOTSUP;
+    }
+
+    return qcow2_co_do_compress(bs, dest, dest_size, src, src_size, fn);
 }
 
 static ssize_t coroutine_fn
 qcow2_co_decompress(BlockDriverState *bs, void *dest, size_t dest_size,
                     const void *src, size_t src_size)
 {
-    return qcow2_co_do_compress(bs, dest, dest_size, src, src_size,
-                                qcow2_decompress);
+    BDRVQcow2State *s = bs->opaque;
+    Qcow2CompressFunc fn;
+
+    switch (s->compression_type) {
+    case QCOW2_COMPRESSION_TYPE_ZLIB:
+        fn = zlib_decompress;
+        break;
+
+    default:
+        return -ENOTSUP;
+    }
+
+    return qcow2_co_do_compress(bs, dest, dest_size, src, src_size, fn);
 }
 
 /* XXX: put compressed sectors first, then all the cluster aligned
-- 
2.17.0



  parent reply	other threads:[~2019-05-28 14:41 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-28 14:37 [Qemu-devel] [PATCH v0 0/3] add zstd cluster compression Denis Plotnikov
2019-05-28 14:37 ` [Qemu-devel] [PATCH v0 1/3] qcow2: introduce compression type feature Denis Plotnikov
2019-05-29 11:40   ` Vladimir Sementsov-Ogievskiy
2019-06-28  9:45     ` Kevin Wolf
2019-06-27 16:35   ` Markus Armbruster
2019-06-28  9:54   ` Kevin Wolf
2019-06-28 11:07     ` Denis Plotnikov
2019-06-28 10:10   ` Kevin Wolf
2019-05-28 14:37 ` Denis Plotnikov [this message]
2019-05-29  9:47   ` [Qemu-devel] [PATCH v0 2/3] qcow2: add compression type processing Vladimir Sementsov-Ogievskiy
2019-06-28 10:23   ` Kevin Wolf
2019-06-28 11:24     ` Denis Plotnikov
2019-06-28 12:06       ` Kevin Wolf
2019-06-28 12:56         ` Denis Plotnikov
2019-06-28 14:24           ` Kevin Wolf
2019-06-28 14:40             ` Denis Plotnikov
2019-06-28 14:54               ` Kevin Wolf
2019-06-28 15:03                 ` Max Reitz
2019-06-28 15:14                   ` Denis Plotnikov
2019-06-28 19:34                 ` Eric Blake
2019-07-02 12:34                   ` Denis Plotnikov
2019-05-28 14:37 ` [Qemu-devel] [PATCH v0 3/3] qcow2: add zstd cluster compression Denis Plotnikov
2019-06-28 11:57   ` Kevin Wolf
2019-07-02 12:33     ` Denis Plotnikov
2019-07-02 12:49     ` Denis Plotnikov
2019-07-02 14:37       ` Kevin Wolf
2019-07-02 14:48         ` Denis Plotnikov
2019-06-04  7:56 ` [Qemu-devel] [PING] [PATCH v0 0/3] " Denis Plotnikov
2019-06-27 15:04   ` [Qemu-devel] [PING PING] " Denis Plotnikov

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=20190528143727.10529-3-dplotnikov@virtuozzo.com \
    --to=dplotnikov@virtuozzo.com \
    --cc=armbru@redhat.com \
    --cc=den@virtuozzo.com \
    --cc=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=vsementsov@virtuozzo.com \
    /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).