qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: anthony@codemonkey.ws
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 08/33] qcow2: Implement bdrv_amend_options
Date: Fri, 13 Sep 2013 13:50:38 +0200	[thread overview]
Message-ID: <1379073063-14963-9-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1379073063-14963-1-git-send-email-kwolf@redhat.com>

From: Max Reitz <mreitz@redhat.com>

Implement bdrv_amend_options for compat, size, backing_file, backing_fmt
and lazy_refcounts.

Downgrading images from compat=1.1 to compat=0.10 is achieved through
handling all incompatible flags accordingly, clearing all compatible and
autoclear flags and expanding all zero clusters.

Signed-off-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/qcow2.c | 194 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 194 insertions(+)

diff --git a/block/qcow2.c b/block/qcow2.c
index 27203f8..7c9354c 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -1820,6 +1820,199 @@ static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
     return ret;
 }
 
+/*
+ * Downgrades an image's version. To achieve this, any incompatible features
+ * have to be removed.
+ */
+static int qcow2_downgrade(BlockDriverState *bs, int target_version)
+{
+    BDRVQcowState *s = bs->opaque;
+    int current_version = s->qcow_version;
+    int ret;
+
+    if (target_version == current_version) {
+        return 0;
+    } else if (target_version > current_version) {
+        return -EINVAL;
+    } else if (target_version != 2) {
+        return -EINVAL;
+    }
+
+    if (s->refcount_order != 4) {
+        /* we would have to convert the image to a refcount_order == 4 image
+         * here; however, since qemu (at the time of writing this) does not
+         * support anything different than 4 anyway, there is no point in doing
+         * so right now; however, we should error out (if qemu supports this in
+         * the future and this code has not been adapted) */
+        error_report("qcow2_downgrade: Image refcount orders other than 4 are"
+                     "currently not supported.");
+        return -ENOTSUP;
+    }
+
+    /* clear incompatible features */
+    if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
+        ret = qcow2_mark_clean(bs);
+        if (ret < 0) {
+            return ret;
+        }
+    }
+
+    /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
+     * the first place; if that happens nonetheless, returning -ENOTSUP is the
+     * best thing to do anyway */
+
+    if (s->incompatible_features) {
+        return -ENOTSUP;
+    }
+
+    /* since we can ignore compatible features, we can set them to 0 as well */
+    s->compatible_features = 0;
+    /* if lazy refcounts have been used, they have already been fixed through
+     * clearing the dirty flag */
+
+    /* clearing autoclear features is trivial */
+    s->autoclear_features = 0;
+
+    ret = qcow2_expand_zero_clusters(bs);
+    if (ret < 0) {
+        return ret;
+    }
+
+    s->qcow_version = target_version;
+    ret = qcow2_update_header(bs);
+    if (ret < 0) {
+        s->qcow_version = current_version;
+        return ret;
+    }
+    return 0;
+}
+
+static int qcow2_amend_options(BlockDriverState *bs,
+                               QEMUOptionParameter *options)
+{
+    BDRVQcowState *s = bs->opaque;
+    int old_version = s->qcow_version, new_version = old_version;
+    uint64_t new_size = 0;
+    const char *backing_file = NULL, *backing_format = NULL;
+    bool lazy_refcounts = s->use_lazy_refcounts;
+    int ret;
+    int i;
+
+    for (i = 0; options[i].name; i++)
+    {
+        if (!options[i].assigned) {
+            /* only change explicitly defined options */
+            continue;
+        }
+
+        if (!strcmp(options[i].name, "compat")) {
+            if (!options[i].value.s) {
+                /* preserve default */
+            } else if (!strcmp(options[i].value.s, "0.10")) {
+                new_version = 2;
+            } else if (!strcmp(options[i].value.s, "1.1")) {
+                new_version = 3;
+            } else {
+                fprintf(stderr, "Unknown compatibility level %s.\n",
+                        options[i].value.s);
+                return -EINVAL;
+            }
+        } else if (!strcmp(options[i].name, "preallocation")) {
+            fprintf(stderr, "Cannot change preallocation mode.\n");
+            return -ENOTSUP;
+        } else if (!strcmp(options[i].name, "size")) {
+            new_size = options[i].value.n;
+        } else if (!strcmp(options[i].name, "backing_file")) {
+            backing_file = options[i].value.s;
+        } else if (!strcmp(options[i].name, "backing_fmt")) {
+            backing_format = options[i].value.s;
+        } else if (!strcmp(options[i].name, "encryption")) {
+            if ((options[i].value.n != !!s->crypt_method)) {
+                fprintf(stderr, "Changing the encryption flag is not "
+                        "supported.\n");
+                return -ENOTSUP;
+            }
+        } else if (!strcmp(options[i].name, "cluster_size")) {
+            if (options[i].value.n != s->cluster_size) {
+                fprintf(stderr, "Changing the cluster size is not "
+                        "supported.\n");
+                return -ENOTSUP;
+            }
+        } else if (!strcmp(options[i].name, "lazy_refcounts")) {
+            lazy_refcounts = options[i].value.n;
+        } else {
+            /* if this assertion fails, this probably means a new option was
+             * added without having it covered here */
+            assert(false);
+        }
+    }
+
+    if (new_version != old_version) {
+        if (new_version > old_version) {
+            /* Upgrade */
+            s->qcow_version = new_version;
+            ret = qcow2_update_header(bs);
+            if (ret < 0) {
+                s->qcow_version = old_version;
+                return ret;
+            }
+        } else {
+            ret = qcow2_downgrade(bs, new_version);
+            if (ret < 0) {
+                return ret;
+            }
+        }
+    }
+
+    if (backing_file || backing_format) {
+        ret = qcow2_change_backing_file(bs, backing_file ?: bs->backing_file,
+                                        backing_format ?: bs->backing_format);
+        if (ret < 0) {
+            return ret;
+        }
+    }
+
+    if (s->use_lazy_refcounts != lazy_refcounts) {
+        if (lazy_refcounts) {
+            if (s->qcow_version < 3) {
+                fprintf(stderr, "Lazy refcounts only supported with compatibility "
+                        "level 1.1 and above (use compat=1.1 or greater)\n");
+                return -EINVAL;
+            }
+            s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
+            ret = qcow2_update_header(bs);
+            if (ret < 0) {
+                s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
+                return ret;
+            }
+            s->use_lazy_refcounts = true;
+        } else {
+            /* make image clean first */
+            ret = qcow2_mark_clean(bs);
+            if (ret < 0) {
+                return ret;
+            }
+            /* now disallow lazy refcounts */
+            s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
+            ret = qcow2_update_header(bs);
+            if (ret < 0) {
+                s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
+                return ret;
+            }
+            s->use_lazy_refcounts = false;
+        }
+    }
+
+    if (new_size) {
+        ret = bdrv_truncate(bs, new_size);
+        if (ret < 0) {
+            return ret;
+        }
+    }
+
+    return 0;
+}
+
 static QEMUOptionParameter qcow2_create_options[] = {
     {
         .name = BLOCK_OPT_SIZE,
@@ -1903,6 +2096,7 @@ static BlockDriver bdrv_qcow2 = {
 
     .create_options = qcow2_create_options,
     .bdrv_check = qcow2_check,
+    .bdrv_amend_options = qcow2_amend_options,
 };
 
 static void bdrv_qcow2_init(void)
-- 
1.8.1.4

  parent reply	other threads:[~2013-09-13 11:51 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-13 11:50 [Qemu-devel] [PULL 00/33] Block patches Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 01/33] qcow2: Pass discard type to qcow2_discard_clusters() Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 02/33] qcow2: Discard VM state in active L1 after creating snapshot Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 03/33] raw-win32.c: Fix incorrect handling behaviour of small block files Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 04/33] block: Image file option amendment Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 05/33] qcow2-cache: Empty cache Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 06/33] qcow2-cluster: Expand zero clusters Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 07/33] qcow2: Save refcount order in BDRVQcowState Kevin Wolf
2013-09-13 11:50 ` Kevin Wolf [this message]
2013-09-13 11:50 ` [Qemu-devel] [PULL 09/33] qemu-iotest: qcow2 image option amendment Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 10/33] qemu-iotests: add unix socket help program Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 11/33] qemu-iotests: add infrastructure of fd passing via SCM Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 12/33] qemu-iotests: add tests for runtime fd passing via SCM rights Kevin Wolf
2013-09-13 18:30   ` Eric Blake
2013-09-13 11:50 ` [Qemu-devel] [PULL 13/33] qemu-iotests: New test case in 061 Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 14/33] snapshot: new function bdrv_snapshot_find_by_id_and_name() Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 15/33] snapshot: distinguish id and name in snapshot delete Kevin Wolf
2013-09-13 18:42   ` Eric Blake
2013-09-13 11:50 ` [Qemu-devel] [PULL 16/33] qmp: add internal snapshot support in qmp_transaction Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 17/33] qmp: add interface blockdev-snapshot-internal-sync Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 18/33] qmp: add interface blockdev-snapshot-delete-internal-sync Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 19/33] hmp: add interface hmp_snapshot_blkdev_internal Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 20/33] hmp: add interface hmp_snapshot_delete_blkdev_internal Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 21/33] qemu-iotests: add 057 internal snapshot for block device test case Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 22/33] bdrv: Use "Error" for opening images Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 23/33] bdrv: Use "Error" for creating images Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 24/33] block: Error parameter for open functions Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 25/33] block: Error parameter for create functions Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 26/33] qemu-img create: Emit filename on error Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 27/33] qcow2: Use Error parameter Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 28/33] qemu-iotests: Adjustments due to error propagation Kevin Wolf
2013-09-13 11:50 ` [Qemu-devel] [PULL 29/33] coroutine: add ./configure --disable-coroutine-pool Kevin Wolf
2013-09-13 11:51 ` [Qemu-devel] [PULL 30/33] qemu-img: fix invalid JSON Kevin Wolf
2013-09-13 11:51 ` [Qemu-devel] [PULL 31/33] qemu-iotests: Cleanup test image in test number 007 Kevin Wolf
2013-09-13 11:51 ` [Qemu-devel] [PULL 32/33] block: Assert validity of BdrvActionOps Kevin Wolf
2013-09-13 11:51 ` [Qemu-devel] [PULL 33/33] qemu-iotests: Fix test 038 Kevin Wolf

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=1379073063-14963-9-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=anthony@codemonkey.ws \
    --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).