neil / rpms / libblockdev

Forked from rpms/libblockdev a year ago
Clone
61580b
From b25fd9caca9b2fb34e5a4d7d4bee0031e4758d0a Mon Sep 17 00:00:00 2001
61580b
From: Tomas Bzatek <tbzatek@redhat.com>
61580b
Date: Thu, 22 Sep 2022 16:31:28 +0200
61580b
Subject: [PATCH 1/5] nvme: Avoid sending NVME_IDENTIFY_CNS_NS_DESC_LIST on
61580b
 older devices
61580b
61580b
Turned out this Identify feature was introduced only with the NVMe 1.3
61580b
specification. To find out device supported NVMe revision an extra
61580b
Identify Controller call is needed.
61580b
---
61580b
 src/plugins/nvme/nvme-info.c | 14 +++++++++++---
61580b
 1 file changed, 11 insertions(+), 3 deletions(-)
61580b
61580b
diff --git a/src/plugins/nvme/nvme-info.c b/src/plugins/nvme/nvme-info.c
61580b
index fdd90459..112b4054 100644
61580b
--- a/src/plugins/nvme/nvme-info.c
61580b
+++ b/src/plugins/nvme/nvme-info.c
61580b
@@ -431,7 +431,7 @@ BDNVMEControllerInfo * bd_nvme_get_controller_info (const gchar *device, GError
61580b
     if (fd < 0)
61580b
         return NULL;
61580b
 
61580b
-    /* send the NVME_IDENTIFY_CNS_NS + NVME_IDENTIFY_CNS_CTRL ioctl */
61580b
+    /* send the NVME_IDENTIFY_CNS_CTRL ioctl */
61580b
     ret = nvme_identify_ctrl (fd, &ctrl_id);
61580b
     if (ret != 0) {
61580b
         _nvme_status_to_error (ret, FALSE, error);
61580b
@@ -539,9 +539,11 @@ BDNVMEControllerInfo * bd_nvme_get_controller_info (const gchar *device, GError
61580b
  */
61580b
 BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **error) {
61580b
     int ret;
61580b
-    int ret_desc;
61580b
+    int ret_ctrl;
61580b
+    int ret_desc = -1;
61580b
     int fd;
61580b
     __u32 nsid = 0;
61580b
+    struct nvme_id_ctrl ctrl_id = ZERO_INIT;
61580b
     struct nvme_id_ns ns_info = ZERO_INIT;
61580b
     uint8_t desc[NVME_IDENTIFY_DATA_SIZE] = ZERO_INIT;
61580b
     guint8 flbas;
61580b
@@ -565,7 +567,6 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
61580b
     }
61580b
 
61580b
     /* send the NVME_IDENTIFY_CNS_NS ioctl */
61580b
-    ret_desc = nvme_identify_ns_descs (fd, nsid, (struct nvme_ns_id_desc *) &desc);
61580b
     ret = nvme_identify_ns (fd, nsid, &ns_info);
61580b
     if (ret != 0) {
61580b
         _nvme_status_to_error (ret, FALSE, error);
61580b
@@ -573,6 +574,13 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
61580b
         close (fd);
61580b
         return NULL;
61580b
     }
61580b
+
61580b
+    /* send the NVME_IDENTIFY_CNS_CTRL ioctl */
61580b
+    ret_ctrl = nvme_identify_ctrl (fd, &ctrl_id);
61580b
+
61580b
+    /* send the NVME_IDENTIFY_CNS_NS_DESC_LIST ioctl, NVMe 1.3 */
61580b
+    if (ret_ctrl == 0 && GUINT32_FROM_LE (ctrl_id.ver) >= 0x10300)
61580b
+        ret_desc = nvme_identify_ns_descs (fd, nsid, (struct nvme_ns_id_desc *) &desc);
61580b
     close (fd);
61580b
 
61580b
     info = g_new0 (BDNVMENamespaceInfo, 1);
61580b
-- 
61580b
2.39.0
61580b
61580b
61580b
From e6f7d0c4562623b03df96dc6b89ab00d8e4d6b90 Mon Sep 17 00:00:00 2001
61580b
From: Tomas Bzatek <tbzatek@redhat.com>
61580b
Date: Thu, 22 Sep 2022 16:56:26 +0200
61580b
Subject: [PATCH 2/5] nvme: Add BD_NVME_NS_FEAT_ROTATIONAL
61580b
61580b
A NVMe 2.0 feature indicating rotational medium on a namespace level.
61580b
Further information can be found in the Rotational Media Information Log
61580b
page (Log Identifier 16h) that is not implemented in libblockdev yet.
61580b
---
61580b
 src/lib/plugin_apis/nvme.api |  2 ++
61580b
 src/plugins/nvme/nvme-info.c | 10 ++++++++++
61580b
 src/plugins/nvme/nvme.h      |  2 ++
61580b
 tests/nvme_test.py           |  1 +
61580b
 4 files changed, 15 insertions(+)
61580b
61580b
diff --git a/src/lib/plugin_apis/nvme.api b/src/lib/plugin_apis/nvme.api
61580b
index 79247a01..7bc2cf9e 100644
61580b
--- a/src/lib/plugin_apis/nvme.api
61580b
+++ b/src/lib/plugin_apis/nvme.api
61580b
@@ -317,12 +317,14 @@ GType bd_nvme_lba_format_get_type () {
61580b
  *                                    in the NVM subsystem concurrently.
61580b
  * @BD_NVME_NS_FEAT_FORMAT_PROGRESS: indicates the capability to report the percentage of the namespace
61580b
  *                                   that remains to be formatted.
61580b
+ * @BD_NVME_NS_FEAT_ROTATIONAL: indicates a rotational medium.
61580b
  */
61580b
 /* BpG-skip-end */
61580b
 typedef enum {
61580b
     BD_NVME_NS_FEAT_THIN             = 1 << 0,
61580b
     BD_NVME_NS_FEAT_MULTIPATH_SHARED = 1 << 1,
61580b
     BD_NVME_NS_FEAT_FORMAT_PROGRESS  = 1 << 2,
61580b
+    BD_NVME_NS_FEAT_ROTATIONAL       = 1 << 3,
61580b
 } BDNVMENamespaceFeature;
61580b
 
61580b
 #define BD_NVME_TYPE_NAMESPACE_INFO (bd_nvme_namespace_info_get_type ())
61580b
diff --git a/src/plugins/nvme/nvme-info.c b/src/plugins/nvme/nvme-info.c
61580b
index 112b4054..c574a6f3 100644
61580b
--- a/src/plugins/nvme/nvme-info.c
61580b
+++ b/src/plugins/nvme/nvme-info.c
61580b
@@ -541,10 +541,12 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
61580b
     int ret;
61580b
     int ret_ctrl;
61580b
     int ret_desc = -1;
61580b
+    int ret_ns_ind = -1;
61580b
     int fd;
61580b
     __u32 nsid = 0;
61580b
     struct nvme_id_ctrl ctrl_id = ZERO_INIT;
61580b
     struct nvme_id_ns ns_info = ZERO_INIT;
61580b
+    struct nvme_id_independent_id_ns ns_info_ind = ZERO_INIT;
61580b
     uint8_t desc[NVME_IDENTIFY_DATA_SIZE] = ZERO_INIT;
61580b
     guint8 flbas;
61580b
     guint i;
61580b
@@ -581,6 +583,10 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
61580b
     /* send the NVME_IDENTIFY_CNS_NS_DESC_LIST ioctl, NVMe 1.3 */
61580b
     if (ret_ctrl == 0 && GUINT32_FROM_LE (ctrl_id.ver) >= 0x10300)
61580b
         ret_desc = nvme_identify_ns_descs (fd, nsid, (struct nvme_ns_id_desc *) &desc);
61580b
+
61580b
+    /* send the NVME_IDENTIFY_CNS_CSI_INDEPENDENT_ID_NS ioctl, NVMe 2.0 */
61580b
+    if (ret_ctrl == 0 && GUINT32_FROM_LE (ctrl_id.ver) >= 0x20000)
61580b
+        ret_ns_ind = nvme_identify_independent_identify_ns (fd, nsid, &ns_info_ind);
61580b
     close (fd);
61580b
 
61580b
     info = g_new0 (BDNVMENamespaceInfo, 1);
61580b
@@ -627,6 +633,10 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
61580b
             }
61580b
         }
61580b
     }
61580b
+    if (ret_ns_ind == 0) {
61580b
+        if ((ns_info_ind.nsfeat & 1 << 4) == 1 << 4)
61580b
+            info->features |= BD_NVME_NS_FEAT_ROTATIONAL;
61580b
+    }
61580b
 
61580b
     /* translate the LBA Format array */
61580b
     ptr_array = g_ptr_array_new ();
61580b
diff --git a/src/plugins/nvme/nvme.h b/src/plugins/nvme/nvme.h
61580b
index a7d30d79..ad456a82 100644
61580b
--- a/src/plugins/nvme/nvme.h
61580b
+++ b/src/plugins/nvme/nvme.h
61580b
@@ -202,11 +202,13 @@ typedef struct BDNVMELBAFormat {
61580b
  *                                    in the NVM subsystem concurrently.
61580b
  * @BD_NVME_NS_FEAT_FORMAT_PROGRESS: indicates the capability to report the percentage of the namespace
61580b
  *                                   that remains to be formatted.
61580b
+ * @BD_NVME_NS_FEAT_ROTATIONAL: indicates a rotational medium.
61580b
  */
61580b
 typedef enum {
61580b
     BD_NVME_NS_FEAT_THIN             = 1 << 0,
61580b
     BD_NVME_NS_FEAT_MULTIPATH_SHARED = 1 << 1,
61580b
     BD_NVME_NS_FEAT_FORMAT_PROGRESS  = 1 << 2,
61580b
+    BD_NVME_NS_FEAT_ROTATIONAL       = 1 << 3,
61580b
 } BDNVMENamespaceFeature;
61580b
 
61580b
 /**
61580b
diff --git a/tests/nvme_test.py b/tests/nvme_test.py
61580b
index a46f7422..f205e539 100644
61580b
--- a/tests/nvme_test.py
61580b
+++ b/tests/nvme_test.py
61580b
@@ -73,6 +73,7 @@ class NVMeTestCase(NVMeTest):
61580b
         self.assertFalse(info.features & BlockDev.NVMENamespaceFeature.THIN)
61580b
         self.assertTrue (info.features & BlockDev.NVMENamespaceFeature.MULTIPATH_SHARED)
61580b
         self.assertFalse(info.features & BlockDev.NVMENamespaceFeature.FORMAT_PROGRESS)
61580b
+        self.assertFalse(info.features & BlockDev.NVMENamespaceFeature.ROTATIONAL)
61580b
         self.assertEqual(info.eui64, "0000000000000000")
61580b
         self.assertEqual(info.format_progress_remaining, 0)
61580b
         self.assertEqual(len(info.lba_formats), 1)
61580b
-- 
61580b
2.39.0
61580b
61580b
61580b
From 4ff0df937dcd357623e7b7d960c08c476b1deffb Mon Sep 17 00:00:00 2001
61580b
From: Tomas Bzatek <tbzatek@redhat.com>
61580b
Date: Fri, 9 Dec 2022 16:13:43 +0100
61580b
Subject: [PATCH 3/5] nvme: Fix namespace identifiers
61580b
61580b
Use Namespace Identification Descriptor list (CNS 03h) data when available
61580b
and NVM Command Set Identify Namespace Data Structure (CNS 00h) as a fallback.
61580b
61580b
Also, if the CNS 00h EUI64 or NGUID fields equal to zero, return NULL
61580b
instead of zeroes:
61580b
  "If the controller is not able to provide a ... identifier in this field,
61580b
   then this field shall be cleared to 0h."
61580b
---
61580b
 src/plugins/nvme/nvme-info.c | 26 +++++++++++++++++++-------
61580b
 tests/nvme_test.py           |  2 +-
61580b
 2 files changed, 20 insertions(+), 8 deletions(-)
61580b
61580b
diff --git a/src/plugins/nvme/nvme-info.c b/src/plugins/nvme/nvme-info.c
61580b
index c574a6f3..ac189abe 100644
61580b
--- a/src/plugins/nvme/nvme-info.c
61580b
+++ b/src/plugins/nvme/nvme-info.c
61580b
@@ -603,12 +603,7 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
61580b
     info->format_progress_remaining = ns_info.fpi & NVME_NS_FPI_REMAINING;
61580b
     /* TODO: what the ns_info.nvmcap really stands for? */
61580b
     info->write_protected = (ns_info.nsattr & NVME_NS_NSATTR_WRITE_PROTECTED) == NVME_NS_NSATTR_WRITE_PROTECTED;
61580b
-    info->nguid = g_malloc0 (sizeof (ns_info.nguid) * 2 + 1);
61580b
-    for (i = 0; i < G_N_ELEMENTS (ns_info.nguid); i++)
61580b
-        snprintf (info->nguid + i * 2, 3, "%02x", ns_info.nguid[i]);
61580b
-    info->eui64 = g_malloc0 (sizeof (ns_info.eui64) * 2 + 1);
61580b
-    for (i = 0; i < G_N_ELEMENTS (ns_info.eui64); i++)
61580b
-        snprintf (info->eui64 + i * 2, 3, "%02x", ns_info.eui64[i]);
61580b
+
61580b
     if (ret_desc == 0) {
61580b
         for (i = 0; i < NVME_IDENTIFY_DATA_SIZE; i += len) {
61580b
             struct nvme_ns_id_desc *d = (void *) desc + i;
61580b
@@ -620,8 +615,14 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
61580b
 
61580b
             switch (d->nidt) {
61580b
                 case NVME_NIDT_EUI64:
61580b
+                    info->eui64 = g_malloc0 (d->nidl * 2 + 1);
61580b
+                    for (i = 0; i < d->nidl; i++)
61580b
+                        snprintf (info->eui64 + i * 2, 3, "%02x", d->nid[i]);
61580b
+                    break;
61580b
                 case NVME_NIDT_NGUID:
61580b
-                    /* already have these from nvme_identify_ns() */
61580b
+                    info->nguid = g_malloc0 (d->nidl * 2 + 1);
61580b
+                    for (i = 0; i < d->nidl; i++)
61580b
+                        snprintf (info->nguid + i * 2, 3, "%02x", d->nid[i]);
61580b
                     break;
61580b
                 case NVME_NIDT_UUID:
61580b
                     uuid_unparse (d->nid, uuid_buf);
61580b
@@ -633,6 +634,17 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
61580b
             }
61580b
         }
61580b
     }
61580b
+
61580b
+    if (info->nguid == NULL && ns_info.nguid[G_N_ELEMENTS (ns_info.nguid) - 1] > 0) {
61580b
+        info->nguid = g_malloc0 (sizeof (ns_info.nguid) * 2 + 1);
61580b
+        for (i = 0; i < G_N_ELEMENTS (ns_info.nguid); i++)
61580b
+            snprintf (info->nguid + i * 2, 3, "%02x", ns_info.nguid[i]);
61580b
+    }
61580b
+    if (info->eui64 == NULL && ns_info.eui64[G_N_ELEMENTS (ns_info.eui64) - 1] > 0) {
61580b
+        info->eui64 = g_malloc0 (sizeof (ns_info.eui64) * 2 + 1);
61580b
+        for (i = 0; i < G_N_ELEMENTS (ns_info.eui64); i++)
61580b
+            snprintf (info->eui64 + i * 2, 3, "%02x", ns_info.eui64[i]);
61580b
+    }
61580b
     if (ret_ns_ind == 0) {
61580b
         if ((ns_info_ind.nsfeat & 1 << 4) == 1 << 4)
61580b
             info->features |= BD_NVME_NS_FEAT_ROTATIONAL;
61580b
diff --git a/tests/nvme_test.py b/tests/nvme_test.py
61580b
index f205e539..a1822be6 100644
61580b
--- a/tests/nvme_test.py
61580b
+++ b/tests/nvme_test.py
61580b
@@ -74,7 +74,7 @@ class NVMeTestCase(NVMeTest):
61580b
         self.assertTrue (info.features & BlockDev.NVMENamespaceFeature.MULTIPATH_SHARED)
61580b
         self.assertFalse(info.features & BlockDev.NVMENamespaceFeature.FORMAT_PROGRESS)
61580b
         self.assertFalse(info.features & BlockDev.NVMENamespaceFeature.ROTATIONAL)
61580b
-        self.assertEqual(info.eui64, "0000000000000000")
61580b
+        self.assertIsNone(info.eui64)
61580b
         self.assertEqual(info.format_progress_remaining, 0)
61580b
         self.assertEqual(len(info.lba_formats), 1)
61580b
         self.assertGreater(len(info.nguid), 0)
61580b
-- 
61580b
2.39.0
61580b
61580b
61580b
From cdbb9a37a19d3f388910f68c4c384bafae8901ae Mon Sep 17 00:00:00 2001
61580b
From: Tomas Bzatek <tbzatek@redhat.com>
61580b
Date: Wed, 11 Jan 2023 18:19:36 +0100
61580b
Subject: [PATCH 4/5] nvme: Use libnvme-1.2's nvme_uuid_to_string()
61580b
61580b
This also bumps libnvme dependency to 1.2
61580b
---
61580b
 configure.ac                    |  5 +----
61580b
 src/plugins/nvme/Makefile.am    |  4 ++--
61580b
 src/plugins/nvme/nvme-error.c   |  3 ---
61580b
 src/plugins/nvme/nvme-fabrics.c |  1 -
61580b
 src/plugins/nvme/nvme-info.c    | 17 ++++++++++-------
61580b
 src/plugins/nvme/nvme-op.c      |  1 -
61580b
 src/plugins/nvme/nvme.c         |  1 -
61580b
 7 files changed, 13 insertions(+), 19 deletions(-)
61580b
61580b
diff --git a/configure.ac b/configure.ac
61580b
index ec789c91..fbd70473 100644
61580b
--- a/configure.ac
61580b
+++ b/configure.ac
61580b
@@ -281,10 +281,7 @@ AS_IF([test "x$with_nvdimm" != "xno"],
61580b
       [])
61580b
 
61580b
 AS_IF([test "x$with_nvme" != "xno"],
61580b
-      [LIBBLOCKDEV_PKG_CHECK_MODULES([NVME], [libnvme >= 1.0])
61580b
-      AS_IF([$PKG_CONFIG --atleast-version=1.1 libnvme],
61580b
-            [AC_DEFINE([HAVE_LIBNVME_1_1])], [])
61580b
-      ],
61580b
+      [LIBBLOCKDEV_PKG_CHECK_MODULES([NVME], [libnvme >= 1.2])],
61580b
       [])
61580b
 
61580b
 AS_IF([test "x$with_vdo" != "xno"],
61580b
diff --git a/src/plugins/nvme/Makefile.am b/src/plugins/nvme/Makefile.am
61580b
index b4a10ce0..a8a856d4 100644
61580b
--- a/src/plugins/nvme/Makefile.am
61580b
+++ b/src/plugins/nvme/Makefile.am
61580b
@@ -2,8 +2,8 @@ AUTOMAKE_OPTIONS = subdir-objects
61580b
 
61580b
 lib_LTLIBRARIES = libbd_nvme.la
61580b
 
61580b
-libbd_nvme_la_CFLAGS = $(GLIB_CFLAGS) $(GIO_CFLAGS) $(UUID_CFLAGS) $(NVME_CFLAGS) -Wall -Wextra -Werror
61580b
-libbd_nvme_la_LIBADD = ${builddir}/../../utils/libbd_utils.la $(GLIB_LIBS) $(GIO_LIBS) $(UUID_LIBS) $(NVME_LIBS)
61580b
+libbd_nvme_la_CFLAGS = $(GLIB_CFLAGS) $(GIO_CFLAGS) $(NVME_CFLAGS) -Wall -Wextra -Werror
61580b
+libbd_nvme_la_LIBADD = ${builddir}/../../utils/libbd_utils.la $(GLIB_LIBS) $(GIO_LIBS) $(NVME_LIBS)
61580b
 libbd_nvme_la_LDFLAGS = -L${srcdir}/../../utils/ -version-info 2:0:0 -Wl,--no-undefined
61580b
 libbd_nvme_la_CPPFLAGS = -I${builddir}/../../../include/ -I${srcdir}/../ -I. -DPACKAGE_SYSCONF_DIR=\""$(sysconfdir)"\"
61580b
 
61580b
diff --git a/src/plugins/nvme/nvme-error.c b/src/plugins/nvme/nvme-error.c
61580b
index 86f0d6a3..cb95a46d 100644
61580b
--- a/src/plugins/nvme/nvme-error.c
61580b
+++ b/src/plugins/nvme/nvme-error.c
61580b
@@ -28,7 +28,6 @@
61580b
 #include <malloc.h>
61580b
 
61580b
 #include <libnvme.h>
61580b
-#include <uuid/uuid.h>
61580b
 
61580b
 #include <blockdev/utils.h>
61580b
 #include <check_deps.h>
61580b
@@ -123,7 +122,6 @@ void _nvme_fabrics_errno_to_gerror (int result, int _errno, GError **error)
61580b
             case ENVME_CONNECT_LOOKUP_SUBSYS:
61580b
                 code = BD_NVME_ERROR_CONNECT;
61580b
                 break;
61580b
-#ifdef HAVE_LIBNVME_1_1
61580b
             case ENVME_CONNECT_ALREADY:
61580b
                 code = BD_NVME_ERROR_CONNECT_ALREADY;
61580b
                 break;
61580b
@@ -139,7 +137,6 @@ void _nvme_fabrics_errno_to_gerror (int result, int _errno, GError **error)
61580b
             case ENVME_CONNECT_OPNOTSUPP:
61580b
                 code = BD_NVME_ERROR_CONNECT_OPNOTSUPP;
61580b
                 break;
61580b
-#endif
61580b
             default:
61580b
                 code = BD_NVME_ERROR_CONNECT;
61580b
         }
61580b
diff --git a/src/plugins/nvme/nvme-fabrics.c b/src/plugins/nvme/nvme-fabrics.c
61580b
index 20ed57f5..bba7392d 100644
61580b
--- a/src/plugins/nvme/nvme-fabrics.c
61580b
+++ b/src/plugins/nvme/nvme-fabrics.c
61580b
@@ -30,7 +30,6 @@
61580b
 #include <glib/gstdio.h>
61580b
 
61580b
 #include <libnvme.h>
61580b
-#include <uuid/uuid.h>
61580b
 
61580b
 #include <blockdev/utils.h>
61580b
 #include <check_deps.h>
61580b
diff --git a/src/plugins/nvme/nvme-info.c b/src/plugins/nvme/nvme-info.c
61580b
index ac189abe..18719d51 100644
61580b
--- a/src/plugins/nvme/nvme-info.c
61580b
+++ b/src/plugins/nvme/nvme-info.c
61580b
@@ -28,7 +28,6 @@
61580b
 #include <malloc.h>
61580b
 
61580b
 #include <libnvme.h>
61580b
-#include <uuid/uuid.h>
61580b
 
61580b
 #include <blockdev/utils.h>
61580b
 #include <check_deps.h>
61580b
@@ -408,6 +407,14 @@ static gchar *decode_nvme_rev (guint32 ver) {
61580b
         return g_strdup_printf ("%u.%u.%u", mjr, mnr, ter);
61580b
 }
61580b
 
61580b
+static gchar *_uuid_to_str (unsigned char uuid[NVME_UUID_LEN]) {
61580b
+    gchar uuid_buf[NVME_UUID_LEN_STRING] = ZERO_INIT;
61580b
+
61580b
+    if (nvme_uuid_to_string (uuid, uuid_buf) == 0)
61580b
+        return g_strdup (uuid_buf);
61580b
+    return NULL;
61580b
+}
61580b
+
61580b
 /**
61580b
  * bd_nvme_get_controller_info:
61580b
  * @device: a NVMe controller device (e.g. `/dev/nvme0`)
61580b
@@ -461,9 +468,7 @@ BDNVMEControllerInfo * bd_nvme_get_controller_info (const gchar *device, GError
61580b
     info->pci_vendor_id = GUINT16_FROM_LE (ctrl_id.vid);
61580b
     info->pci_subsys_vendor_id = GUINT16_FROM_LE (ctrl_id.ssvid);
61580b
     info->ctrl_id = GUINT16_FROM_LE (ctrl_id.cntlid);
61580b
-    /* TODO: decode fguid as 128-bit hex string? */
61580b
-    info->fguid = g_strdup_printf ("%-.*s", (int) sizeof (ctrl_id.fguid), ctrl_id.fguid);
61580b
-    g_strstrip (info->fguid);
61580b
+    info->fguid = _uuid_to_str (ctrl_id.fguid);
61580b
     info->model_number = g_strndup (ctrl_id.mn, sizeof (ctrl_id.mn));
61580b
     g_strstrip (info->model_number);
61580b
     info->serial_number = g_strndup (ctrl_id.sn, sizeof (ctrl_id.sn));
61580b
@@ -607,7 +612,6 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
61580b
     if (ret_desc == 0) {
61580b
         for (i = 0; i < NVME_IDENTIFY_DATA_SIZE; i += len) {
61580b
             struct nvme_ns_id_desc *d = (void *) desc + i;
61580b
-            gchar uuid_buf[37] = ZERO_INIT;
61580b
 
61580b
             if (!d->nidl)
61580b
                 break;
61580b
@@ -625,8 +629,7 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
61580b
                         snprintf (info->nguid + i * 2, 3, "%02x", d->nid[i]);
61580b
                     break;
61580b
                 case NVME_NIDT_UUID:
61580b
-                    uuid_unparse (d->nid, uuid_buf);
61580b
-                    info->uuid = g_strdup (uuid_buf);
61580b
+                    info->uuid = _uuid_to_str (d->nid);
61580b
                     break;
61580b
                 case NVME_NIDT_CSI:
61580b
                     /* unused */
61580b
diff --git a/src/plugins/nvme/nvme-op.c b/src/plugins/nvme/nvme-op.c
61580b
index 4568c453..c9e92697 100644
61580b
--- a/src/plugins/nvme/nvme-op.c
61580b
+++ b/src/plugins/nvme/nvme-op.c
61580b
@@ -29,7 +29,6 @@
61580b
 #include <linux/fs.h>
61580b
 
61580b
 #include <libnvme.h>
61580b
-#include <uuid/uuid.h>
61580b
 
61580b
 #include <blockdev/utils.h>
61580b
 #include <check_deps.h>
61580b
diff --git a/src/plugins/nvme/nvme.c b/src/plugins/nvme/nvme.c
61580b
index 00f2f76e..4a32ac4e 100644
61580b
--- a/src/plugins/nvme/nvme.c
61580b
+++ b/src/plugins/nvme/nvme.c
61580b
@@ -28,7 +28,6 @@
61580b
 #include <malloc.h>
61580b
 
61580b
 #include <libnvme.h>
61580b
-#include <uuid/uuid.h>
61580b
 
61580b
 #include <blockdev/utils.h>
61580b
 #include <check_deps.h>
61580b
-- 
61580b
2.39.0
61580b
61580b
61580b
From 64263599ec39b6b0f20d8e16c1169afcf66f5d9a Mon Sep 17 00:00:00 2001
61580b
From: Tomas Bzatek <tbzatek@redhat.com>
61580b
Date: Thu, 12 Jan 2023 16:01:28 +0100
61580b
Subject: [PATCH 5/5] nvme: Fix zeroed struct fields detection
61580b
61580b
As often stated in the NVMe specification, fields of features that
61580b
are either not implemented or not valid are typically cleared to zero (0h).
61580b
---
61580b
 src/plugins/nvme/nvme-info.c | 20 +++++++++++++++-----
61580b
 tests/nvme_test.py           |  2 +-
61580b
 2 files changed, 16 insertions(+), 6 deletions(-)
61580b
61580b
diff --git a/src/plugins/nvme/nvme-info.c b/src/plugins/nvme/nvme-info.c
61580b
index 18719d51..85f94a32 100644
61580b
--- a/src/plugins/nvme/nvme-info.c
61580b
+++ b/src/plugins/nvme/nvme-info.c
61580b
@@ -415,6 +415,15 @@ static gchar *_uuid_to_str (unsigned char uuid[NVME_UUID_LEN]) {
61580b
     return NULL;
61580b
 }
61580b
 
61580b
+static gboolean _nvme_a_is_zero (const __u8 a[], int len) {
61580b
+    int i;
61580b
+
61580b
+    for (i = 0; i < len; i++)
61580b
+        if (a[i] > 0)
61580b
+            return FALSE;
61580b
+    return TRUE;
61580b
+}
61580b
+
61580b
 /**
61580b
  * bd_nvme_get_controller_info:
61580b
  * @device: a NVMe controller device (e.g. `/dev/nvme0`)
61580b
@@ -468,7 +477,8 @@ BDNVMEControllerInfo * bd_nvme_get_controller_info (const gchar *device, GError
61580b
     info->pci_vendor_id = GUINT16_FROM_LE (ctrl_id.vid);
61580b
     info->pci_subsys_vendor_id = GUINT16_FROM_LE (ctrl_id.ssvid);
61580b
     info->ctrl_id = GUINT16_FROM_LE (ctrl_id.cntlid);
61580b
-    info->fguid = _uuid_to_str (ctrl_id.fguid);
61580b
+    if (!_nvme_a_is_zero (ctrl_id.fguid, sizeof (ctrl_id.fguid)))
61580b
+        info->fguid = _uuid_to_str (ctrl_id.fguid);
61580b
     info->model_number = g_strndup (ctrl_id.mn, sizeof (ctrl_id.mn));
61580b
     g_strstrip (info->model_number);
61580b
     info->serial_number = g_strndup (ctrl_id.sn, sizeof (ctrl_id.sn));
61580b
@@ -638,14 +648,14 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
61580b
         }
61580b
     }
61580b
 
61580b
-    if (info->nguid == NULL && ns_info.nguid[G_N_ELEMENTS (ns_info.nguid) - 1] > 0) {
61580b
+    if (info->nguid == NULL && !_nvme_a_is_zero (ns_info.nguid, sizeof (ns_info.nguid))) {
61580b
         info->nguid = g_malloc0 (sizeof (ns_info.nguid) * 2 + 1);
61580b
-        for (i = 0; i < G_N_ELEMENTS (ns_info.nguid); i++)
61580b
+        for (i = 0; i < sizeof (ns_info.nguid); i++)
61580b
             snprintf (info->nguid + i * 2, 3, "%02x", ns_info.nguid[i]);
61580b
     }
61580b
-    if (info->eui64 == NULL && ns_info.eui64[G_N_ELEMENTS (ns_info.eui64) - 1] > 0) {
61580b
+    if (info->eui64 == NULL && !_nvme_a_is_zero (ns_info.eui64, sizeof (ns_info.eui64))) {
61580b
         info->eui64 = g_malloc0 (sizeof (ns_info.eui64) * 2 + 1);
61580b
-        for (i = 0; i < G_N_ELEMENTS (ns_info.eui64); i++)
61580b
+        for (i = 0; i < sizeof (ns_info.eui64); i++)
61580b
             snprintf (info->eui64 + i * 2, 3, "%02x", ns_info.eui64[i]);
61580b
     }
61580b
     if (ret_ns_ind == 0) {
61580b
diff --git a/tests/nvme_test.py b/tests/nvme_test.py
61580b
index a1822be6..a1494d9a 100644
61580b
--- a/tests/nvme_test.py
61580b
+++ b/tests/nvme_test.py
61580b
@@ -116,7 +116,7 @@ class NVMeTestCase(NVMeTest):
61580b
         self.assertFalse(info.features & BlockDev.NVMEControllerFeature.ENCLOSURE)
61580b
         self.assertFalse(info.features & BlockDev.NVMEControllerFeature.MGMT_PCIE)
61580b
         self.assertFalse(info.features & BlockDev.NVMEControllerFeature.MGMT_SMBUS)
61580b
-        self.assertEqual(info.fguid, "")
61580b
+        self.assertIsNone(info.fguid)
61580b
         self.assertEqual(info.pci_vendor_id, 0)
61580b
         self.assertEqual(info.pci_subsys_vendor_id, 0)
61580b
         self.assertIn("Linux", info.model_number)
61580b
-- 
61580b
2.39.0
61580b