hw/nvme/ctrl.c | 6 ++-- hw/nvme/ns.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- hw/nvme/nvme.h | 5 +++- 3 files changed, 87 insertions(+), 17 deletions(-)
From: Jesper Wendel Devantier <j.devantier@samsung.com>
The base specification, from 1.4c to 2.4 states in the figure
"Namespace Identification Descriptor", that a NS for which EUI64
and NGUID are cleared, a descriptor of NIDT 3h containing the
namespace UUID must be returned.
docs/system/devices/nvme.rst, under "Additional Namespaces", says
that `uuid` defaults to an autogenerated value.
This commit derives a deterministic UUID from controller
VID/MN/SN fields and the Namespace's ID - provided the UUID was
not explicitly provided.
This change also ensures this UUID is returned in the NS
descriptor list.
Signed-off-by: Jesper Wendel Devantier <j.devantier@samsung.com>
---
hw/nvme/ctrl.c | 6 ++--
hw/nvme/ns.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
hw/nvme/nvme.h | 5 +++-
3 files changed, 87 insertions(+), 17 deletions(-)
diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 578421c217..0033d09e07 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -6145,10 +6145,10 @@ static uint16_t nvme_identify_ns_descr_list(NvmeCtrl *n, NvmeRequest *req)
return NVME_INVALID_FIELD | NVME_DNR;
}
- if (!qemu_uuid_is_null(&ns->params.uuid)) {
+ if (!qemu_uuid_is_null(&ns->uuid)) {
uuid.hdr.nidt = NVME_NIDT_UUID;
uuid.hdr.nidl = NVME_NIDL_UUID;
- memcpy(uuid.v, ns->params.uuid.data, NVME_NIDL_UUID);
+ memcpy(uuid.v, ns->uuid.data, NVME_NIDL_UUID);
memcpy(pos, &uuid, sizeof(uuid));
pos += sizeof(uuid);
}
@@ -9705,7 +9705,7 @@ static void nvme_realize(PCIDevice *pci_dev, Error **errp)
ns->ctrl = n;
ns->subsys = n->subsys;
- if (nvme_ns_setup(ns, errp)) {
+ if (nvme_ns_setup(ns, n, errp)) {
return;
}
diff --git a/hw/nvme/ns.c b/hw/nvme/ns.c
index 2ae26a9acb..e25191468f 100644
--- a/hw/nvme/ns.c
+++ b/hw/nvme/ns.c
@@ -18,6 +18,7 @@
#include "qemu/error-report.h"
#include "qapi/error.h"
#include "qemu/bitops.h"
+#include "qemu/xxhash.h"
#include "system/system.h"
#include "system/block-backend.h"
#include "migration/vmstate.h"
@@ -63,6 +64,66 @@ void nvme_ns_init_format(NvmeNamespace *ns)
id_ns_nvm->npdgl = npdg;
}
+static uint64_t nvme_ns_hash_field(const uint8_t *field, size_t len)
+{
+ uint64_t h = QEMU_XXHASH_SEED;
+
+ /* fold `field` of `len` bytes into a 64bit hash value */
+ while (len >= 8) {
+ h = qemu_xxhash64_4(h, ldq_le_p(field), 0, 0);
+ field += 8;
+ len -= 8;
+ }
+
+ while (len >= 4) {
+ h = qemu_xxhash64_4(h, ldl_le_p(field), 0, 0);
+ field += 4;
+ len -= 4;
+ }
+
+ while (len) {
+ h = qemu_xxhash64_4(h, *field++, 0, 0);
+ len--;
+ }
+
+ return h;
+}
+
+void nvme_ns_uuid_init(NvmeNamespace *ns, NvmeCtrl *n)
+{
+ uint64_t sn_hash, mn_hash, lo, hi;
+ uint32_t tmp[4];
+
+ /* user-provided value is preferred */
+ if (!qemu_uuid_is_null(&ns->params.uuid)) {
+ ns->uuid = ns->params.uuid;
+ return;
+ }
+
+ /*
+ * No uuid= given: derive one from the controller identity (VID/SN/MN)
+ * and the namespace id. This is deterministic, so an OS can use it
+ * as a persistent disk identity across power cycles and resets.
+ */
+ sn_hash = nvme_ns_hash_field(n->id_ctrl.sn, sizeof(n->id_ctrl.sn));
+ mn_hash = nvme_ns_hash_field(n->id_ctrl.mn, sizeof(n->id_ctrl.mn));
+
+ lo = qemu_xxhash64_4(le16_to_cpu(n->id_ctrl.vid), ns->params.nsid,
+ sn_hash, mn_hash);
+ hi = qemu_xxhash64_4(ns->params.nsid, le16_to_cpu(n->id_ctrl.vid),
+ mn_hash, sn_hash);
+
+ tmp[0] = (uint32_t)(lo >> 32);
+ tmp[1] = (uint32_t)lo;
+ tmp[2] = (uint32_t)(hi >> 32);
+ tmp[3] = (uint32_t)hi;
+ memcpy(&ns->uuid, tmp, sizeof(tmp));
+
+ ns->uuid.data[8] = (ns->uuid.data[8] & 0x3f) | 0x80;
+ /* RFC 9562 - version 8 UUID (custom) */
+ ns->uuid.data[6] = (ns->uuid.data[6] & 0xf) | 0x80;
+}
+
static int nvme_ns_init(NvmeNamespace *ns, Error **errp)
{
static uint64_t ns_count;
@@ -727,7 +788,7 @@ static int nvme_ns_check_constraints(NvmeNamespace *ns, Error **errp)
return 0;
}
-int nvme_ns_setup(NvmeNamespace *ns, Error **errp)
+int nvme_ns_setup(NvmeNamespace *ns, NvmeCtrl *n, Error **errp)
{
if (nvme_ns_check_constraints(ns, errp)) {
return -1;
@@ -753,6 +814,8 @@ int nvme_ns_setup(NvmeNamespace *ns, Error **errp)
}
}
+ nvme_ns_uuid_init(ns, n);
+
return 0;
}
@@ -922,18 +985,10 @@ static void nvme_ns_realize(DeviceState *dev, Error **errp)
ns->subsys = subsys;
ns->endgrp = &subsys->endgrp;
- if (!nvme_ns_set_nsabp(n, ns, errp)) {
- return;
- }
-
- if (!nvme_ns_set_nab(n, ns, errp)) {
- return;
- }
-
- if (nvme_ns_setup(ns, errp)) {
- return;
- }
-
+ /*
+ * Resolve the namespace id before nvme_ns_setup(): the namespace UUID is
+ * derived from it, so it has to be final by then.
+ */
if (!nsid) {
for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
if (nvme_subsys_ns(subsys, i)) {
@@ -953,6 +1008,18 @@ static void nvme_ns_realize(DeviceState *dev, Error **errp)
return;
}
+ if (!nvme_ns_set_nsabp(n, ns, errp)) {
+ return;
+ }
+
+ if (!nvme_ns_set_nab(n, ns, errp)) {
+ return;
+ }
+
+ if (nvme_ns_setup(ns, n, errp)) {
+ return;
+ }
+
subsys->namespaces[nsid] = ns;
ns->id_ns.endgid = cpu_to_le16(0x1);
diff --git a/hw/nvme/nvme.h b/hw/nvme/nvme.h
index a4b2c01a2c..477f7eeb4f 100644
--- a/hw/nvme/nvme.h
+++ b/hw/nvme/nvme.h
@@ -260,6 +260,8 @@ typedef struct NvmeNamespace {
int attached;
uint8_t pif;
+ QemuUUID uuid;
+
struct {
uint16_t zrwas;
uint16_t zrwafg;
@@ -410,7 +412,8 @@ static inline void nvme_fdp_stat_inc(uint64_t *a, uint64_t b)
}
void nvme_ns_init_format(NvmeNamespace *ns);
-int nvme_ns_setup(NvmeNamespace *ns, Error **errp);
+int nvme_ns_setup(NvmeNamespace *ns, NvmeCtrl *n, Error **errp);
+void nvme_ns_uuid_init(NvmeNamespace *ns, NvmeCtrl *n);
void nvme_ns_drain(NvmeNamespace *ns);
void nvme_ns_shutdown(NvmeNamespace *ns);
void nvme_ns_cleanup(NvmeNamespace *ns);
---
base-commit: f6c0588fd7c22caefc5b31f485c8bb3ee994a58b
change-id: 20261008-patch-review-20261008160517-b70-a67a01cbec9a
Best regards,
--
Jesper Wendel Devantier <j.devantier@samsung.com>
© 2016 - 2026 Red Hat, Inc.