26ba25
From 0433f2d3bd818b83908636ae240ecbfd256e0a9c Mon Sep 17 00:00:00 2001
26ba25
From: John Snow <jsnow@redhat.com>
26ba25
Date: Wed, 10 Oct 2018 20:30:13 +0100
26ba25
Subject: [PATCH 2/4] block/rbd: Attempt to parse legacy filenames
26ba25
26ba25
RH-Author: John Snow <jsnow@redhat.com>
26ba25
Message-id: <20181010203015.11719-3-jsnow@redhat.com>
26ba25
Patchwork-id: 82629
26ba25
O-Subject: [RHEL8/rhel qemu-kvm PATCH 2/4] block/rbd: Attempt to parse legacy filenames
26ba25
Bugzilla: 1635585
26ba25
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
26ba25
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
RH-Acked-by: Thomas Huth <thuth@redhat.com>
26ba25
26ba25
From: Jeff Cody <jcody@redhat.com>
26ba25
26ba25
When we converted rbd to get rid of the older key/value-centric
26ba25
encoding format, we broke compatibility with image files with backing
26ba25
file strings encoded in the old format.
26ba25
26ba25
This leaves a bit of an ugly conundrum, and a hacky solution.
26ba25
26ba25
If the initial attempt to parse the "proper" options fails, it assumes
26ba25
that we may have an older key/value encoded filename.  Fall back to
26ba25
attempting to parse the filename, and extract the required options from
26ba25
it.  If that fails, pass along the original error message.
26ba25
26ba25
We do not support mixed modern usage alongside legacy keyvalue pair
26ba25
usage.
26ba25
26ba25
A deprecation warning has been added, although care should be taken
26ba25
when actually deprecating since the impact is not limited to
26ba25
commandline or qapi usage, but also opening existing images.
26ba25
26ba25
Reviewed-by: Eric Blake <eblake@redhat.com>
26ba25
Signed-off-by: Jeff Cody <jcody@redhat.com>
26ba25
Message-id: 15b332e5432ad069441f7275a46080f465d789a0.1536704901.git.jcody@redhat.com
26ba25
Signed-off-by: Jeff Cody <jcody@redhat.com>
26ba25
(cherry picked from commit 084d1d13bdb753d558b991996e7686c077bd6d80)
26ba25
Signed-off-by: John Snow <jsnow@redhat.com>
26ba25
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
26ba25
---
26ba25
 block/rbd.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
26ba25
 1 file changed, 52 insertions(+), 2 deletions(-)
26ba25
26ba25
diff --git a/block/rbd.c b/block/rbd.c
26ba25
index 1e4d339..ebe0701 100644
26ba25
--- a/block/rbd.c
26ba25
+++ b/block/rbd.c
26ba25
@@ -671,6 +671,33 @@ static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts,
26ba25
     return 0;
26ba25
 }
26ba25
 
26ba25
+static int qemu_rbd_attempt_legacy_options(QDict *options,
26ba25
+                                           BlockdevOptionsRbd **opts,
26ba25
+                                           char **keypairs)
26ba25
+{
26ba25
+    char *filename;
26ba25
+    int r;
26ba25
+
26ba25
+    filename = g_strdup(qdict_get_try_str(options, "filename"));
26ba25
+    if (!filename) {
26ba25
+        return -EINVAL;
26ba25
+    }
26ba25
+    qdict_del(options, "filename");
26ba25
+
26ba25
+    qemu_rbd_parse_filename(filename, options, NULL);
26ba25
+
26ba25
+    /* keypairs freed by caller */
26ba25
+    *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
26ba25
+    if (*keypairs) {
26ba25
+        qdict_del(options, "=keyvalue-pairs");
26ba25
+    }
26ba25
+
26ba25
+    r = qemu_rbd_convert_options(options, opts, NULL);
26ba25
+
26ba25
+    g_free(filename);
26ba25
+    return r;
26ba25
+}
26ba25
+
26ba25
 static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
26ba25
                          Error **errp)
26ba25
 {
26ba25
@@ -693,8 +720,31 @@ static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
26ba25
 
26ba25
     r = qemu_rbd_convert_options(options, &opts, &local_err);
26ba25
     if (local_err) {
26ba25
-        error_propagate(errp, local_err);
26ba25
-        goto out;
26ba25
+        /* If keypairs are present, that means some options are present in
26ba25
+         * the modern option format.  Don't attempt to parse legacy option
26ba25
+         * formats, as we won't support mixed usage. */
26ba25
+        if (keypairs) {
26ba25
+            error_propagate(errp, local_err);
26ba25
+            goto out;
26ba25
+        }
26ba25
+
26ba25
+        /* If the initial attempt to convert and process the options failed,
26ba25
+         * we may be attempting to open an image file that has the rbd options
26ba25
+         * specified in the older format consisting of all key/value pairs
26ba25
+         * encoded in the filename.  Go ahead and attempt to parse the
26ba25
+         * filename, and see if we can pull out the required options. */
26ba25
+        r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs);
26ba25
+        if (r < 0) {
26ba25
+            /* Propagate the original error, not the legacy parsing fallback
26ba25
+             * error, as the latter was just a best-effort attempt. */
26ba25
+            error_propagate(errp, local_err);
26ba25
+            goto out;
26ba25
+        }
26ba25
+        /* Take care whenever deciding to actually deprecate; once this ability
26ba25
+         * is removed, we will not be able to open any images with legacy-styled
26ba25
+         * backing image strings. */
26ba25
+        error_report("RBD options encoded in the filename as keyvalue pairs "
26ba25
+                     "is deprecated");
26ba25
     }
26ba25
 
26ba25
     /* Remove the processed options from the QDict (the visitor processes
26ba25
-- 
26ba25
1.8.3.1
26ba25