cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
0a122b
From 53db4963174d9f65bd8ba04636ba0adba7615996 Mon Sep 17 00:00:00 2001
0a122b
From: Max Reitz <mreitz@redhat.com>
0a122b
Date: Tue, 7 Jan 2014 21:57:13 +0100
0a122b
Subject: [PATCH 08/14] qcow2: Implement bdrv_amend_options
0a122b
0a122b
RH-Author: Max Reitz <mreitz@redhat.com>
0a122b
Message-id: <1389131839-12920-9-git-send-email-mreitz@redhat.com>
0a122b
Patchwork-id: 56544
0a122b
O-Subject: [RHEL-7.0 qemu-kvm PATCH v2 08/14] qcow2: Implement bdrv_amend_options
0a122b
Bugzilla: 1033490
0a122b
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
0a122b
RH-Acked-by: Fam Zheng <famz@redhat.com>
0a122b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
0a122b
BZ: 1033490
0a122b
0a122b
Implement bdrv_amend_options for compat, size, backing_file, backing_fmt
0a122b
and lazy_refcounts.
0a122b
0a122b
Downgrading images from compat=1.1 to compat=0.10 is achieved through
0a122b
handling all incompatible flags accordingly, clearing all compatible and
0a122b
autoclear flags and expanding all zero clusters.
0a122b
0a122b
Signed-off-by: Max Reitz <mreitz@redhat.com>
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
(cherry picked from commit 9296b3ed7050cc6e0645fbc3b0aea74406d7eeb2)
0a122b
0a122b
Signed-off-by: Max Reitz <mreitz@redhat.com>
0a122b
---
0a122b
 block/qcow2.c | 194 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0a122b
 1 file changed, 194 insertions(+)
0a122b
0a122b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
0a122b
---
0a122b
 block/qcow2.c |  194 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0a122b
 1 files changed, 194 insertions(+), 0 deletions(-)
0a122b
0a122b
diff --git a/block/qcow2.c b/block/qcow2.c
0a122b
index 0a53a3c..5b28259 100644
0a122b
--- a/block/qcow2.c
0a122b
+++ b/block/qcow2.c
0a122b
@@ -1989,6 +1989,199 @@ static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
0a122b
     return ret;
0a122b
 }
0a122b
 
0a122b
+/*
0a122b
+ * Downgrades an image's version. To achieve this, any incompatible features
0a122b
+ * have to be removed.
0a122b
+ */
0a122b
+static int qcow2_downgrade(BlockDriverState *bs, int target_version)
0a122b
+{
0a122b
+    BDRVQcowState *s = bs->opaque;
0a122b
+    int current_version = s->qcow_version;
0a122b
+    int ret;
0a122b
+
0a122b
+    if (target_version == current_version) {
0a122b
+        return 0;
0a122b
+    } else if (target_version > current_version) {
0a122b
+        return -EINVAL;
0a122b
+    } else if (target_version != 2) {
0a122b
+        return -EINVAL;
0a122b
+    }
0a122b
+
0a122b
+    if (s->refcount_order != 4) {
0a122b
+        /* we would have to convert the image to a refcount_order == 4 image
0a122b
+         * here; however, since qemu (at the time of writing this) does not
0a122b
+         * support anything different than 4 anyway, there is no point in doing
0a122b
+         * so right now; however, we should error out (if qemu supports this in
0a122b
+         * the future and this code has not been adapted) */
0a122b
+        error_report("qcow2_downgrade: Image refcount orders other than 4 are"
0a122b
+                     "currently not supported.");
0a122b
+        return -ENOTSUP;
0a122b
+    }
0a122b
+
0a122b
+    /* clear incompatible features */
0a122b
+    if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
0a122b
+        ret = qcow2_mark_clean(bs);
0a122b
+        if (ret < 0) {
0a122b
+            return ret;
0a122b
+        }
0a122b
+    }
0a122b
+
0a122b
+    /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
0a122b
+     * the first place; if that happens nonetheless, returning -ENOTSUP is the
0a122b
+     * best thing to do anyway */
0a122b
+
0a122b
+    if (s->incompatible_features) {
0a122b
+        return -ENOTSUP;
0a122b
+    }
0a122b
+
0a122b
+    /* since we can ignore compatible features, we can set them to 0 as well */
0a122b
+    s->compatible_features = 0;
0a122b
+    /* if lazy refcounts have been used, they have already been fixed through
0a122b
+     * clearing the dirty flag */
0a122b
+
0a122b
+    /* clearing autoclear features is trivial */
0a122b
+    s->autoclear_features = 0;
0a122b
+
0a122b
+    ret = qcow2_expand_zero_clusters(bs);
0a122b
+    if (ret < 0) {
0a122b
+        return ret;
0a122b
+    }
0a122b
+
0a122b
+    s->qcow_version = target_version;
0a122b
+    ret = qcow2_update_header(bs);
0a122b
+    if (ret < 0) {
0a122b
+        s->qcow_version = current_version;
0a122b
+        return ret;
0a122b
+    }
0a122b
+    return 0;
0a122b
+}
0a122b
+
0a122b
+static int qcow2_amend_options(BlockDriverState *bs,
0a122b
+                               QEMUOptionParameter *options)
0a122b
+{
0a122b
+    BDRVQcowState *s = bs->opaque;
0a122b
+    int old_version = s->qcow_version, new_version = old_version;
0a122b
+    uint64_t new_size = 0;
0a122b
+    const char *backing_file = NULL, *backing_format = NULL;
0a122b
+    bool lazy_refcounts = s->use_lazy_refcounts;
0a122b
+    int ret;
0a122b
+    int i;
0a122b
+
0a122b
+    for (i = 0; options[i].name; i++)
0a122b
+    {
0a122b
+        if (!options[i].assigned) {
0a122b
+            /* only change explicitly defined options */
0a122b
+            continue;
0a122b
+        }
0a122b
+
0a122b
+        if (!strcmp(options[i].name, "compat")) {
0a122b
+            if (!options[i].value.s) {
0a122b
+                /* preserve default */
0a122b
+            } else if (!strcmp(options[i].value.s, "0.10")) {
0a122b
+                new_version = 2;
0a122b
+            } else if (!strcmp(options[i].value.s, "1.1")) {
0a122b
+                new_version = 3;
0a122b
+            } else {
0a122b
+                fprintf(stderr, "Unknown compatibility level %s.\n",
0a122b
+                        options[i].value.s);
0a122b
+                return -EINVAL;
0a122b
+            }
0a122b
+        } else if (!strcmp(options[i].name, "preallocation")) {
0a122b
+            fprintf(stderr, "Cannot change preallocation mode.\n");
0a122b
+            return -ENOTSUP;
0a122b
+        } else if (!strcmp(options[i].name, "size")) {
0a122b
+            new_size = options[i].value.n;
0a122b
+        } else if (!strcmp(options[i].name, "backing_file")) {
0a122b
+            backing_file = options[i].value.s;
0a122b
+        } else if (!strcmp(options[i].name, "backing_fmt")) {
0a122b
+            backing_format = options[i].value.s;
0a122b
+        } else if (!strcmp(options[i].name, "encryption")) {
0a122b
+            if ((options[i].value.n != !!s->crypt_method)) {
0a122b
+                fprintf(stderr, "Changing the encryption flag is not "
0a122b
+                        "supported.\n");
0a122b
+                return -ENOTSUP;
0a122b
+            }
0a122b
+        } else if (!strcmp(options[i].name, "cluster_size")) {
0a122b
+            if (options[i].value.n != s->cluster_size) {
0a122b
+                fprintf(stderr, "Changing the cluster size is not "
0a122b
+                        "supported.\n");
0a122b
+                return -ENOTSUP;
0a122b
+            }
0a122b
+        } else if (!strcmp(options[i].name, "lazy_refcounts")) {
0a122b
+            lazy_refcounts = options[i].value.n;
0a122b
+        } else {
0a122b
+            /* if this assertion fails, this probably means a new option was
0a122b
+             * added without having it covered here */
0a122b
+            assert(false);
0a122b
+        }
0a122b
+    }
0a122b
+
0a122b
+    if (new_version != old_version) {
0a122b
+        if (new_version > old_version) {
0a122b
+            /* Upgrade */
0a122b
+            s->qcow_version = new_version;
0a122b
+            ret = qcow2_update_header(bs);
0a122b
+            if (ret < 0) {
0a122b
+                s->qcow_version = old_version;
0a122b
+                return ret;
0a122b
+            }
0a122b
+        } else {
0a122b
+            ret = qcow2_downgrade(bs, new_version);
0a122b
+            if (ret < 0) {
0a122b
+                return ret;
0a122b
+            }
0a122b
+        }
0a122b
+    }
0a122b
+
0a122b
+    if (backing_file || backing_format) {
0a122b
+        ret = qcow2_change_backing_file(bs, backing_file ?: bs->backing_file,
0a122b
+                                        backing_format ?: bs->backing_format);
0a122b
+        if (ret < 0) {
0a122b
+            return ret;
0a122b
+        }
0a122b
+    }
0a122b
+
0a122b
+    if (s->use_lazy_refcounts != lazy_refcounts) {
0a122b
+        if (lazy_refcounts) {
0a122b
+            if (s->qcow_version < 3) {
0a122b
+                fprintf(stderr, "Lazy refcounts only supported with compatibility "
0a122b
+                        "level 1.1 and above (use compat=1.1 or greater)\n");
0a122b
+                return -EINVAL;
0a122b
+            }
0a122b
+            s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
0a122b
+            ret = qcow2_update_header(bs);
0a122b
+            if (ret < 0) {
0a122b
+                s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
0a122b
+                return ret;
0a122b
+            }
0a122b
+            s->use_lazy_refcounts = true;
0a122b
+        } else {
0a122b
+            /* make image clean first */
0a122b
+            ret = qcow2_mark_clean(bs);
0a122b
+            if (ret < 0) {
0a122b
+                return ret;
0a122b
+            }
0a122b
+            /* now disallow lazy refcounts */
0a122b
+            s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
0a122b
+            ret = qcow2_update_header(bs);
0a122b
+            if (ret < 0) {
0a122b
+                s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
0a122b
+                return ret;
0a122b
+            }
0a122b
+            s->use_lazy_refcounts = false;
0a122b
+        }
0a122b
+    }
0a122b
+
0a122b
+    if (new_size) {
0a122b
+        ret = bdrv_truncate(bs, new_size);
0a122b
+        if (ret < 0) {
0a122b
+            return ret;
0a122b
+        }
0a122b
+    }
0a122b
+
0a122b
+    return 0;
0a122b
+}
0a122b
+
0a122b
 static QEMUOptionParameter qcow2_create_options[] = {
0a122b
     {
0a122b
         .name = BLOCK_OPT_SIZE,
0a122b
@@ -2073,6 +2266,7 @@ static BlockDriver bdrv_qcow2 = {
0a122b
 
0a122b
     .create_options = qcow2_create_options,
0a122b
     .bdrv_check = qcow2_check,
0a122b
+    .bdrv_amend_options = qcow2_amend_options,
0a122b
 };
0a122b
 
0a122b
 static void bdrv_qcow2_init(void)
0a122b
-- 
0a122b
1.7.1
0a122b