Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions nvme-print-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions util/json.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>

#include "json.h"
#include "types.h"
Expand Down Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions util/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */