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