Blame SOURCES/libvirt-storage-Parse-nvme-disk-source-properties-from-json-pseudo-uri.patch

fbe740
From 52f32252c985693be402c5a1fba79d21807a53cb Mon Sep 17 00:00:00 2001
fbe740
Message-Id: <52f32252c985693be402c5a1fba79d21807a53cb@dist-git>
fbe740
From: Peter Krempa <pkrempa@redhat.com>
fbe740
Date: Tue, 24 Mar 2020 16:26:08 +0100
fbe740
Subject: [PATCH] storage: Parse 'nvme' disk source properties from json:{}
fbe740
 pseudo-uri
fbe740
MIME-Version: 1.0
fbe740
Content-Type: text/plain; charset=UTF-8
fbe740
Content-Transfer-Encoding: 8bit
fbe740
fbe740
Our code allows snapshots of NVMe based disks which means we create
fbe740
overlay file with a 'json:{}' pseudo-uri refering to the NVME device.
fbe740
Our parser code doesn't handle them though. Add the parser and test it
fbe740
via the XML->json->XML round-trip and reference data.
fbe740
fbe740
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
fbe740
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
fbe740
(cherry picked from commit 1b84dd190c16695710a714305517ed24afdd4573)
fbe740
fbe740
https://bugzilla.redhat.com/show_bug.cgi?id=1804617
fbe740
Message-Id: <8460aa915dbc8b4030aacf31cc39fbb7aee07d87.1585063415.git.pkrempa@redhat.com>
fbe740
Reviewed-by: Ján Tomko <jtomko@redhat.com>
fbe740
---
fbe740
 src/util/virstoragefile.c | 30 ++++++++++++++++++++++++++++++
fbe740
 tests/qemublocktest.c     |  5 +++++
fbe740
 tests/virstoragetest.c    |  9 +++++++++
fbe740
 3 files changed, 44 insertions(+)
fbe740
fbe740
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
fbe740
index 5423f0b955..3eb32edc2a 100644
fbe740
--- a/src/util/virstoragefile.c
fbe740
+++ b/src/util/virstoragefile.c
fbe740
@@ -3806,6 +3806,35 @@ virStorageSourceParseBackingJSONVxHS(virStorageSourcePtr src,
fbe740
 }
fbe740
 
fbe740
 
fbe740
+static int
fbe740
+virStorageSourceParseBackingJSONNVMe(virStorageSourcePtr src,
fbe740
+                                     virJSONValuePtr json,
fbe740
+                                     const char *jsonstr G_GNUC_UNUSED,
fbe740
+                                     int opaque G_GNUC_UNUSED)
fbe740
+{
fbe740
+    g_autoptr(virStorageSourceNVMeDef) nvme = g_new0(virStorageSourceNVMeDef, 1);
fbe740
+    const char *device = virJSONValueObjectGetString(json, "device");
fbe740
+
fbe740
+    if (!device || virPCIDeviceAddressParse((char *) device, &nvme->pciAddr) < 0) {
fbe740
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
fbe740
+                       _("missing or malformed 'device' field of 'nvme' storage"));
fbe740
+        return -1;
fbe740
+    }
fbe740
+
fbe740
+    if (virJSONValueObjectGetNumberUlong(json, "namespace", &nvme->namespc) < 0 ||
fbe740
+        nvme->namespc == 0) {
fbe740
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
fbe740
+                       _("missing or malformed 'namespace' field of 'nvme' storage"));
fbe740
+        return -1;
fbe740
+    }
fbe740
+
fbe740
+    src->type = VIR_STORAGE_TYPE_NVME;
fbe740
+    src->nvme = g_steal_pointer(&nvme);
fbe740
+
fbe740
+    return 0;
fbe740
+}
fbe740
+
fbe740
+
fbe740
 struct virStorageSourceJSONDriverParser {
fbe740
     const char *drvname;
fbe740
     bool formatdriver;
fbe740
@@ -3837,6 +3866,7 @@ static const struct virStorageSourceJSONDriverParser jsonParsers[] = {
fbe740
     {"rbd", false, virStorageSourceParseBackingJSONRBD, 0},
fbe740
     {"raw", true, virStorageSourceParseBackingJSONRaw, 0},
fbe740
     {"vxhs", false, virStorageSourceParseBackingJSONVxHS, 0},
fbe740
+    {"nvme", false, virStorageSourceParseBackingJSONNVMe, 0},
fbe740
 };
fbe740
 
fbe740
 
fbe740
diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c
fbe740
index 8b7a50712d..e461b3a23d 100644
fbe740
--- a/tests/qemublocktest.c
fbe740
+++ b/tests/qemublocktest.c
fbe740
@@ -1096,6 +1096,11 @@ mymain(void)
fbe740
     /* type VIR_STORAGE_TYPE_BLOCK is not tested since it parses back to 'file' */
fbe740
     /* type VIR_STORAGE_TYPE_DIR it is a 'format' driver in qemu */
fbe740
 
fbe740
+    TEST_JSON_FORMAT(VIR_STORAGE_TYPE_NVME,
fbe740
+                     "<source type='pci' namespace='1'>\n"
fbe740
+                     "  <address domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>\n"
fbe740
+                     "</source>\n");
fbe740
+
fbe740
     TEST_JSON_FORMAT_NET("<source protocol='http' name=''>\n"
fbe740
                          "  <host name='example.com' port='80'/>\n"
fbe740
                          "</source>\n");
fbe740
diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c
fbe740
index a61522965b..d9244fdfe8 100644
fbe740
--- a/tests/virstoragetest.c
fbe740
+++ b/tests/virstoragetest.c
fbe740
@@ -1637,6 +1637,15 @@ mymain(void)
fbe740
                            "  <timeout seconds='2000'/>\n"
fbe740
                            "</source>\n", 0);
fbe740
 
fbe740
+    TEST_BACKING_PARSE("json:{\"file\":{\"driver\": \"nvme\","
fbe740
+                                       "\"device\": \"0000:01:00.0\","
fbe740
+                                       "\"namespace\": 1"
fbe740
+                                      "}"
fbe740
+                            "}",
fbe740
+                        "<source type='pci' namespace='1'>\n"
fbe740
+                        "  <address domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>\n"
fbe740
+                        "</source>\n");
fbe740
+
fbe740
 #endif /* WITH_YAJL */
fbe740
 
fbe740
  cleanup:
fbe740
-- 
fbe740
2.26.0
fbe740