Skip to content

Commit 80ad62b

Browse files
shroffniigaw
authored andcommitted
nvme-list: make verbose JSON output backward compatible
The commit 64bed0a ("nvme-list: fix verbose JSON output for 'nvme list' command") changed the JSON output format of the nvme list --verbose command. While the new format is more structured, it introduced a regression by breaking compatibility with tools and scripts relying on the previous JSON schema. So to restore backward compatibility, we now leverage the existing --output-format-version option. With this patch, 1. The default --output-format-version=1 retains the original (legacy) JSON format for nvme list --verbose. 2. If the user explicitly sets --output-format-version=2 then the newer JSON structure introduced by commit 64bed0a ("nvme-list: fix verbose JSON output for 'nvme list' command") is used. This ensures that existing users and automation relying on the older format do not break, while still supporting the newer schema for forward-looking users. Fixes: 64bed0a ("nvme-list: fix verbose JSON output for 'nvme list' command") Signed-off-by: Nilay Shroff <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Daniel Wagner <[email protected]>
1 parent 468e60f commit 80ad62b

File tree

1 file changed

+113
-4
lines changed

1 file changed

+113
-4
lines changed

nvme-print-json.c

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4519,7 +4519,7 @@ static void json_print_detail_list(nvme_subsystem_t s, struct json_object *jss)
45194519
obj_add_obj(jss, "Controllers", jctrls);
45204520
}
45214521

4522-
static void json_detail_list(nvme_root_t t)
4522+
static void json_detail_list_v2(nvme_root_t t)
45234523
{
45244524
struct json_object *r = json_create_object();
45254525
struct json_object *jdev = json_create_array();
@@ -4560,6 +4560,112 @@ static void json_detail_list(nvme_root_t t)
45604560
json_print(r);
45614561
}
45624562

4563+
static void json_detail_list(nvme_root_t t)
4564+
{
4565+
struct json_object *r = json_create_object();
4566+
struct json_object *jdev = json_create_array();
4567+
4568+
nvme_host_t h;
4569+
nvme_subsystem_t s;
4570+
nvme_ctrl_t c;
4571+
nvme_path_t p;
4572+
nvme_ns_t n;
4573+
4574+
nvme_for_each_host(t, h) {
4575+
struct json_object *hss = json_create_object();
4576+
struct json_object *jsslist = json_create_array();
4577+
const char *hostid;
4578+
4579+
obj_add_str(hss, "HostNQN", nvme_host_get_hostnqn(h));
4580+
hostid = nvme_host_get_hostid(h);
4581+
if (hostid)
4582+
obj_add_str(hss, "HostID", hostid);
4583+
4584+
nvme_for_each_subsystem(h, s) {
4585+
struct json_object *jss = json_create_object();
4586+
struct json_object *jctrls = json_create_array();
4587+
struct json_object *jnss = json_create_array();
4588+
4589+
obj_add_str(jss, "Subsystem", nvme_subsystem_get_name(s));
4590+
obj_add_str(jss, "SubsystemNQN", nvme_subsystem_get_nqn(s));
4591+
4592+
nvme_subsystem_for_each_ctrl(s, c) {
4593+
struct json_object *jctrl = json_create_object();
4594+
struct json_object *jnss = json_create_array();
4595+
struct json_object *jpaths = json_create_array();
4596+
4597+
obj_add_str(jctrl, "Controller", nvme_ctrl_get_name(c));
4598+
obj_add_str(jctrl, "Cntlid", nvme_ctrl_get_cntlid(c));
4599+
obj_add_str(jctrl, "SerialNumber", nvme_ctrl_get_serial(c));
4600+
obj_add_str(jctrl, "ModelNumber", nvme_ctrl_get_model(c));
4601+
obj_add_str(jctrl, "Firmware", nvme_ctrl_get_firmware(c));
4602+
obj_add_str(jctrl, "Transport", nvme_ctrl_get_transport(c));
4603+
obj_add_str(jctrl, "Address", nvme_ctrl_get_address(c));
4604+
obj_add_str(jctrl, "Slot", nvme_ctrl_get_phy_slot(c));
4605+
4606+
nvme_ctrl_for_each_ns(c, n) {
4607+
struct json_object *jns = json_create_object();
4608+
int lba = nvme_ns_get_lba_size(n);
4609+
uint64_t nsze = nvme_ns_get_lba_count(n) * lba;
4610+
uint64_t nuse = nvme_ns_get_lba_util(n) * lba;
4611+
4612+
obj_add_str(jns, "NameSpace", nvme_ns_get_name(n));
4613+
obj_add_str(jns, "Generic", nvme_ns_get_generic_name(n));
4614+
obj_add_int(jns, "NSID", nvme_ns_get_nsid(n));
4615+
obj_add_uint64(jns, "UsedBytes", nuse);
4616+
obj_add_uint64(jns, "MaximumLBA", nvme_ns_get_lba_count(n));
4617+
obj_add_uint64(jns, "PhysicalSize", nsze);
4618+
obj_add_int(jns, "SectorSize", lba);
4619+
4620+
array_add_obj(jnss, jns);
4621+
}
4622+
obj_add_obj(jctrl, "Namespaces", jnss);
4623+
4624+
nvme_ctrl_for_each_path(c, p) {
4625+
struct json_object *jpath = json_create_object();
4626+
4627+
obj_add_str(jpath, "Path", nvme_path_get_name(p));
4628+
obj_add_str(jpath, "ANAState", nvme_path_get_ana_state(p));
4629+
4630+
array_add_obj(jpaths, jpath);
4631+
}
4632+
obj_add_obj(jctrl, "Paths", jpaths);
4633+
4634+
array_add_obj(jctrls, jctrl);
4635+
}
4636+
obj_add_obj(jss, "Controllers", jctrls);
4637+
4638+
nvme_subsystem_for_each_ns(s, n) {
4639+
struct json_object *jns = json_create_object();
4640+
4641+
int lba = nvme_ns_get_lba_size(n);
4642+
uint64_t nsze = nvme_ns_get_lba_count(n) * lba;
4643+
uint64_t nuse = nvme_ns_get_lba_util(n) * lba;
4644+
4645+
obj_add_str(jns, "NameSpace", nvme_ns_get_name(n));
4646+
obj_add_str(jns, "Generic", nvme_ns_get_generic_name(n));
4647+
obj_add_int(jns, "NSID", nvme_ns_get_nsid(n));
4648+
obj_add_uint64(jns, "UsedBytes", nuse);
4649+
obj_add_uint64(jns, "MaximumLBA", nvme_ns_get_lba_count(n));
4650+
obj_add_uint64(jns, "PhysicalSize", nsze);
4651+
obj_add_int(jns, "SectorSize", lba);
4652+
4653+
array_add_obj(jnss, jns);
4654+
}
4655+
obj_add_obj(jss, "Namespaces", jnss);
4656+
4657+
array_add_obj(jsslist, jss);
4658+
}
4659+
4660+
obj_add_obj(hss, "Subsystems", jsslist);
4661+
array_add_obj(jdev, hss);
4662+
}
4663+
4664+
obj_add_array(r, "Devices", jdev);
4665+
4666+
json_print(r);
4667+
}
4668+
45634669
static struct json_object *json_list_item_obj(nvme_ns_t n)
45644670
{
45654671
struct json_object *r = json_create_object();
@@ -4622,9 +4728,12 @@ static void json_list_item(nvme_ns_t n)
46224728

46234729
static void json_print_list_items(nvme_root_t t)
46244730
{
4625-
if (verbose_mode())
4626-
json_detail_list(t);
4627-
else
4731+
if (json_print_ops.flags & VERBOSE) {
4732+
if (nvme_cfg.output_format_ver == 2)
4733+
json_detail_list_v2(t);
4734+
else
4735+
json_detail_list(t);
4736+
} else
46284737
json_simple_list(t);
46294738
}
46304739

0 commit comments

Comments
 (0)