From 09ed746006ec4884fb62fc7184db4469c9b910d0 Mon Sep 17 00:00:00 2001 From: Tokunori Ikegami Date: Sun, 18 May 2025 01:18:06 +0900 Subject: [PATCH 1/2] util: add json_object_add_string() function This is to add a format string into the json object. Signed-off-by: Tokunori Ikegami --- util/json.c | 16 ++++++++++++++++ util/json.h | 1 + 2 files changed, 17 insertions(+) diff --git a/util/json.c b/util/json.c index 6c6413c348..b2cdbf8426 100644 --- a/util/json.c +++ b/util/json.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include #include +#include #include "json.h" #include "types.h" @@ -135,3 +136,18 @@ void json_object_add_0nprix64(struct json_object *o, const char *k, uint64_t v, sprintf(str, "0x%0*"PRIx64"", width, v); json_object_add_value_string(o, k, str); } + +void json_object_add_string(struct json_object *o, const char *k, const char *format, ...) +{ + _cleanup_free_ char *value = NULL; + va_list ap; + + va_start(ap, format); + + if (vasprintf(&value, format, ap) < 0) + value = NULL; + + json_object_add_value_string(o, k, value ? value : "Could not allocate string"); + + va_end(ap); +} diff --git a/util/json.h b/util/json.h index 5f51b7b64d..b9ccb393b8 100644 --- a/util/json.h +++ b/util/json.h @@ -78,5 +78,6 @@ void json_object_add_byte_array(struct json_object *o, const char *k, unsigned c void json_object_add_nprix64(struct json_object *o, const char *k, uint64_t v); void json_object_add_uint_0nx(struct json_object *o, const char *k, __u32 v, int width); void json_object_add_0nprix64(struct json_object *o, const char *k, uint64_t v, int width); +void json_object_add_string(struct json_object *o, const char *k, const char *format, ...); #endif /* __JSON__H */ From 66d678736f747ff5faae464c295b7fb7430f39ac Mon Sep 17 00:00:00 2001 From: Tokunori Ikegami Date: Sun, 18 May 2025 01:22:06 +0900 Subject: [PATCH 2/2] nvme-print-json: output actual lbaf data size for verbose mode Also add lbaf and in_use outputs for both normal and verbose modes. Signed-off-by: Tokunori Ikegami --- nvme-print-json.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/nvme-print-json.c b/nvme-print-json.c index 858c6f7327..f6c0899a6b 100644 --- a/nvme-print-json.c +++ b/nvme-print-json.c @@ -14,6 +14,7 @@ #include "util/logging.h" #include "nvme.h" #include "common.h" +#include "libnvme.h" #define ERROR_MSG_LEN 100 #define NAME_LEN 128 @@ -40,6 +41,7 @@ #define obj_add_nprix64 json_object_add_nprix64 #define obj_add_uint_0nx json_object_add_uint_0nx #define obj_add_0nprix64 json_object_add_0nprix64 +#define obj_add_string json_object_add_string static const uint8_t zero_uuid[16] = { 0 }; static struct print_ops json_print_ops; @@ -224,6 +226,32 @@ static void json_id_iocs(struct nvme_id_iocs *iocs) json_print(r); } +static void json_nvme_id_ns_lbaf(struct nvme_id_ns *ns, int i, struct json_object *lbafs) +{ + struct json_object *lbaf = json_create_object(); + __u8 flbas; + + nvme_id_ns_flbas_to_lbaf_inuse(ns->flbas, &flbas); + + if (verbose_mode()) { + obj_add_int(lbaf, "LBA Format", i); + obj_add_string(lbaf, "Metadata Size", "%d bytes", le16_to_cpu(ns->lbaf[i].ms)); + obj_add_string(lbaf, "Data Size", "%d bytes", 1 << ns->lbaf[i].ds); + obj_add_string(lbaf, "Relative Performance", "0x%x %s", ns->lbaf[i].rp, + ns->lbaf[i].rp == 3 ? "Degraded" : ns->lbaf[i].rp == 2 ? "Good" : + ns->lbaf[i].rp == 1 ? "Better" : "Best"); + obj_add_str(lbaf, "in use", i == flbas ? "yes" : "no"); + } else { + obj_add_int(lbaf, "lbaf", i); + obj_add_int(lbaf, "ms", le16_to_cpu(ns->lbaf[i].ms)); + obj_add_int(lbaf, "ds", ns->lbaf[i].ds); + obj_add_int(lbaf, "rp", ns->lbaf[i].rp); + obj_add_int(lbaf, "in_use", i == flbas); + } + + array_add_obj(lbafs, lbaf); +} + static void json_nvme_id_ns(struct nvme_id_ns *ns, unsigned int nsid, unsigned int lba_index, bool cap_only) { @@ -306,15 +334,8 @@ static void json_nvme_id_ns(struct nvme_id_ns *ns, unsigned int nsid, obj_add_array(r, "lbafs", lbafs); - for (i = 0; i <= ns->nlbaf; i++) { - struct json_object *lbaf = json_create_object(); - - obj_add_int(lbaf, "ms", le16_to_cpu(ns->lbaf[i].ms)); - obj_add_int(lbaf, "ds", ns->lbaf[i].ds); - obj_add_int(lbaf, "rp", ns->lbaf[i].rp); - - array_add_obj(lbafs, lbaf); - } + for (i = 0; i <= ns->nlbaf; i++) + json_nvme_id_ns_lbaf(ns, i, lbafs); d_json(ns->vs, strnlen((const char *)ns->vs, sizeof(ns->vs)), 16, 1, vs); obj_add_array(r, "vs", vs);