nvme-list: fix verbose JSON output for 'nvme list' command
PATCH 1/1 nvme-list: fix verbose JSON output for 'nvme list' command
The verbose JSON output of the nvme list command is currently incorrect in both multipath and non-multipath configurations. Specifically, it prints empty Namespaces[] and Paths[] arrays in the wrong places, leading to confusion and invalid output. For example, on a system with single NVMe disk, signle controller and one namepsace created, 'nvme list --verbose --output json' prints the following output:
With multipath disabled:
{
"Devices":[
{
...
"Subsystems":[
{
...
"Controllers":[
{
"Controller":"nvme0",
...
"Namespaces":[
{
"NameSpace":"nvme0n1",
...
}
],
"Paths":[] <---- Incorrct: Path should not be present
}
],
"Namespaces":[] <-----Incorrect: Namespaces should not be here
}
]
} ]
}
With multipath enabled, the output changes, but still has misplaced or empty fields:
{
"Devices":[
{
...
"Subsystems":[
{
...
"Controllers":[
{
"Controller":"nvme0",
...
"Namespaces":[] <-----Incorrect: Namespaces should not be here
"Paths":[
{
"Path":"nvme0c0n1",
"ANAState":"optimized"
}
]
}
],
"Namespaces":[
{
"NameSpace":"nvme0n1",
...
}
]
}
]
} ]
}
So as we could see above in both multipath and non-multipath scenarios, the JSON formatted output is incorrect.
The existing JSON formatting logic doesn't differentiate between multipath and non-multipath configurations. As a result:
- "Paths" is printed even when multipath is disabled.
- "Namespaces" appear at incorrect levels in the output tree.
This patch updates the logic for verbose JSON output in nvme list to properly reflect the system configuration:
- When multipath is enabled, each namespace entry includes its associated paths and controller attributes.
- When multipath is disabled, namespaces are shown directly under the controller, and the "Paths" array is omitted.
After this fix, JSON formatted output looks as below:
Scenario1: multipath is enabled
{
Devices: [
{
...
Subsystems: [
{
...
Namespaces: [
{
"NameSpace":"nvme0n1",
...
Paths: [
{
"Path":"nvme0c0n1",
"ANAState":"optimized",
"Controller":"nvme0",
...
}
]
}
]
}
]
} ]
}
Scenario2: multipath is disabled
{
Devices: [
{
...
Subsystems: [
{
...
Controllers: [
{
"Controller":"nvme0",
...
"Namespaces":[
{
"NameSpace":"nvme0n1",
...
}
]
}
]
}
]
} ]
}
This fix ensures the JSON output is semantically accurate and easier to consume by tools that parse nvme list --verbose --output json.
--- nvme-print-json.c | 170 ++++++++++++++++++++++++++------------------ nvme-print-stdout.c | 12 ---- nvme.h | 12 ++++ 3 files changed, 113 insertions(+), 81 deletions(-)
nvme-print-json.c +101 -69
@@ -4423,6 +4423,103 @@ static void json_support_log(struct nvme_supported_log_pages *support_log,json_print (r); } +static void json_print_detail_list_multipath (nvme_subsystem_t s, +struct json_object *jss) +{ +nvme_ns_t n; +nvme_path_t p; +struct json_object *jnss =json_create_array (); + +nvme_subsystem_for_each_ns (s, n) { +struct json_object *jns =json_create_object (); +struct json_object *jpaths =json_create_array (); + +int lba =nvme_ns_get_lba_size (n); +uint64_t nsze =nvme_ns_get_lba_count (n) * lba; +uint64_t nuse =nvme_ns_get_lba_util (n) * lba; + +obj_add_str (jns,"NameSpace" ,nvme_ns_get_name (n)); +obj_add_str (jns,"Generic" ,nvme_ns_get_generic_name (n)); +obj_add_int (jns,"NSID" ,nvme_ns_get_nsid (n)); +obj_add_uint64 (jns,"UsedBytes" , nuse); +obj_add_uint64 (jns,"MaximumLBA" ,nvme_ns_get_lba_count (n)); +obj_add_uint64 (jns,"PhysicalSize" , nsze); +obj_add_int (jns,"SectorSize" , lba); + +nvme_namespace_for_each_path (n, p) { +nvme_ctrl_t c; +struct json_object *jpath =json_create_object (); + +obj_add_str (jpath,"Path" ,nvme_path_get_name (p)); +obj_add_str (jpath,"ANAState" ,nvme_path_get_ana_state (p)); + +/* +* For multipath, each path maps to one controller. +* So get the controller from the path and then add +* controller attributes. +*/ + c =nvme_path_get_ctrl (p); +obj_add_str (jpath,"Controller" ,nvme_ctrl_get_name (c)); +obj_add_str (jpath,"Cntlid" ,nvme_ctrl_get_cntlid (c)); +obj_add_str (jpath,"SerialNumber" ,nvme_ctrl_get_serial (c)); +obj_add_str (jpath,"ModelNumber" ,nvme_ctrl_get_model (c)); +obj_add_str (jpath,"Firmware" ,nvme_ctrl_get_firmware (c)); +obj_add_str (jpath,"Transport" ,nvme_ctrl_get_transport (c)); +obj_add_str (jpath,"Address" ,nvme_ctrl_get_address (c)); +obj_add_str (jpath,"Slot" ,nvme_ctrl_get_phy_slot (c)); + +array_add_obj (jpaths, jpath); + } + +obj_add_obj (jns,"Paths" , jpaths); +array_add_obj (jnss, jns); + } +obj_add_obj (jss,"Namespaces" , jnss); +} + +static void json_print_detail_list (nvme_subsystem_t s,struct json_object *jss) +{ +nvme_ctrl_t c; +nvme_ns_t n; +struct json_object *jctrls =json_create_array (); + +nvme_subsystem_for_each_ctrl (s, c) { +struct json_object *jctrl =json_create_object (); +struct json_object *jnss =json_create_array (); + +obj_add_str (jctrl,"Controller" ,nvme_ctrl_get_name (c)); +obj_add_str (jctrl,"Cntlid" ,nvme_ctrl_get_cntlid (c)); +obj_add_str (jctrl,"SerialNumber" ,nvme_ctrl_get_serial (c)); +obj_add_str (jctrl,"ModelNumber" ,nvme_ctrl_get_model (c)); +obj_add_str (jctrl,"Firmware" ,nvme_ctrl_get_firmware (c)); +obj_add_str (jctrl,"Transport" ,nvme_ctrl_get_transport (c)); +obj_add_str (jctrl,"Address" ,nvme_ctrl_get_address (c)); +obj_add_str (jctrl,"Slot" ,nvme_ctrl_get_phy_slot (c)); + +nvme_ctrl_for_each_ns (c, n) { +struct json_object *jns =json_create_object (); +int lba =nvme_ns_get_lba_size (n); +uint64_t nsze =nvme_ns_get_lba_count (n) * lba; +uint64_t nuse =nvme_ns_get_lba_util (n) * lba; + +obj_add_str (jns,"NameSpace" ,nvme_ns_get_name (n)); +obj_add_str (jns,"Generic" ,nvme_ns_get_generic_name (n)); +obj_add_int (jns,"NSID" ,nvme_ns_get_nsid (n)); +obj_add_uint64 (jns,"UsedBytes" , nuse); +obj_add_uint64 (jns,"MaximumLBA" ,nvme_ns_get_lba_count (n)); +obj_add_uint64 (jns,"PhysicalSize" , nsze); +obj_add_int (jns,"SectorSize" , lba); + +array_add_obj (jnss, jns); + } + +obj_add_obj (jctrl,"Namespaces" , jnss); +array_add_obj (jctrls, jctrl); + } + +obj_add_obj (jss,"Controllers" , jctrls); +} +static void json_detail_list (nvme_root_t t) {struct json_object *r =json_create_object ();
@@ -4430,9 +4527,6 @@ static void json_detail_list(nvme_root_t t)nvme_host_t h;nvme_subsystem_t s; -nvme_ctrl_t c; -nvme_path_t p; -nvme_ns_t n;nvme_for_each_host (t, h) {struct json_object *hss =json_create_object ();
@@ -4446,76 +4540,14 @@ static void json_detail_list(nvme_root_t t)nvme_for_each_subsystem (h, s) {struct json_object *jss =json_create_object (); -struct json_object *jctrls =json_create_array (); -struct json_object *jnss =json_create_array ();obj_add_str (jss,"Subsystem" ,nvme_subsystem_get_name (s));obj_add_str (jss,"SubsystemNQN" ,nvme_subsystem_get_nqn (s)); -nvme_subsystem_for_each_ctrl (s, c) { -struct json_object *jctrl =json_create_object (); -struct json_object *jnss =json_create_array (); -struct json_object *jpaths =json_create_array (); - -obj_add_str (jctrl,"Controller" ,nvme_ctrl_get_name (c)); -obj_add_str (jctrl,"Cntlid" ,nvme_ctrl_get_cntlid (c)); -obj_add_str (jctrl,"SerialNumber" ,nvme_ctrl_get_serial (c)); -obj_add_str (jctrl,"ModelNumber" ,nvme_ctrl_get_model (c)); -obj_add_str (jctrl,"Firmware" ,nvme_ctrl_get_firmware (c)); -obj_add_str (jctrl,"Transport" ,nvme_ctrl_get_transport (c)); -obj_add_str (jctrl,"Address" ,nvme_ctrl_get_address (c)); -obj_add_str (jctrl,"Slot" ,nvme_ctrl_get_phy_slot (c)); - -nvme_ctrl_for_each_ns (c, n) { -struct json_object *jns =json_create_object (); -int lba =nvme_ns_get_lba_size (n); -uint64_t nsze =nvme_ns_get_lba_count (n) * lba; -uint64_t nuse =nvme_ns_get_lba_util (n) * lba; - -obj_add_str (jns,"NameSpace" ,nvme_ns_get_name (n)); -obj_add_str (jns,"Generic" ,nvme_ns_get_generic_name (n)); -obj_add_int (jns,"NSID" ,nvme_ns_get_nsid (n)); -obj_add_uint64 (jns,"UsedBytes" , nuse); -obj_add_uint64 (jns,"MaximumLBA" ,nvme_ns_get_lba_count (n)); -obj_add_uint64 (jns,"PhysicalSize" , nsze); -obj_add_int (jns,"SectorSize" , lba); - -array_add_obj (jnss, jns); - } -obj_add_obj (jctrl,"Namespaces" , jnss); - -nvme_ctrl_for_each_path (c, p) { -struct json_object *jpath =json_create_object (); - -obj_add_str (jpath,"Path" ,nvme_path_get_name (p)); -obj_add_str (jpath,"ANAState" ,nvme_path_get_ana_state (p)); - -array_add_obj (jpaths, jpath); - } -obj_add_obj (jctrl,"Paths" , jpaths); - -array_add_obj (jctrls, jctrl); - } -obj_add_obj (jss,"Controllers" , jctrls); - -nvme_subsystem_for_each_ns (s, n) { -struct json_object *jns =json_create_object (); - -int lba =nvme_ns_get_lba_size (n); -uint64_t nsze =nvme_ns_get_lba_count (n) * lba; -uint64_t nuse =nvme_ns_get_lba_util (n) * lba; - -obj_add_str (jns,"NameSpace" ,nvme_ns_get_name (n)); -obj_add_str (jns,"Generic" ,nvme_ns_get_generic_name (n)); -obj_add_int (jns,"NSID" ,nvme_ns_get_nsid (n)); -obj_add_uint64 (jns,"UsedBytes" , nuse); -obj_add_uint64 (jns,"MaximumLBA" ,nvme_ns_get_lba_count (n)); -obj_add_uint64 (jns,"PhysicalSize" , nsze); -obj_add_int (jns,"SectorSize" , lba); - -array_add_obj (jnss, jns); - } -obj_add_obj (jss,"Namespaces" , jnss); +if (nvme_is_multipath (s)) +json_print_detail_list_multipath (s, jss); +else +json_print_detail_list (s, jss);array_add_obj (jsslist, jss); }
nvme-print-stdout.c +0 -12
@@ -5589,18 +5589,6 @@ static void stdout_list_items(nvme_root_t r)stdout_simple_list (r); } -static bool nvme_is_multipath (nvme_subsystem_t s) -{ -nvme_ns_t n; -nvme_path_t p; - -nvme_subsystem_for_each_ns (s, n) -nvme_namespace_for_each_path (n, p) -return true; - -return false; -} -static void stdout_subsystem_topology_multipath (nvme_subsystem_t s,enum nvme_cli_topo_ranking ranking) {
nvme.h +12 -0
@@ -122,6 +122,18 @@ static inline nvme_mi_ep_t dev_mi_ep(struct nvme_dev *dev)return dev->mi.ep; } +static inline bool nvme_is_multipath (nvme_subsystem_t s) +{ +nvme_ns_t n; +nvme_path_t p; + +nvme_subsystem_for_each_ns (s, n) +nvme_namespace_for_each_path (n, p) +return true; + +return false; +} +void register_extension (struct plugin *plugin);/*
Signature · 1 line
2.50.1
Daniel Wagner
AppliedQuoted 8 lines · Nilay Shroff
The verbose JSON output of the nvme list command is currently incorrect in both multipath and non-multipath configurations. Specifically, it prints empty Namespaces[] and Paths[] arrays in the wrong places, leading to confusion and invalid output. For example, on a system with single NVMe disk, signle controller and one namepsace created, 'nvme list --verbose --output json' prints the following output:
[...]
Applied, thanks!
[1/1] nvme-list: fix verbose JSON output for 'nvme list' command
commit: 64bed0a87a23ca574cfce64fbcf6630194551276
Best regards,
Signature · 1 line
Daniel Wagner <[email protected]>
Quoted 10 lines · Daniel Wagner
Quoted 8 lines · Nilay Shroff
The verbose JSON output of the nvme list command is currently incorrect in both multipath and non-multipath configurations. Specifically, it prints empty Namespaces[] and Paths[] arrays in the wrong places, leading to confusion and invalid output. For example, on a system with single NVMe disk, signle controller and one namepsace created, 'nvme list --verbose --output json' prints the following output:
[...]
FYI, this patch is causing a regression:
Quoted 10 lines · Daniel Wagner
Quoted 9 lines · Daniel Wagner
Quoted 7 lines · Nilay Shroff
The verbose JSON output of the nvme list command is currently incorrect in both multipath and non-multipath configurations. Specifically, it prints empty Namespaces[] and Paths[] arrays in the wrong places, leading to confusion and invalid output. For example, on a system with single NVMe disk, signle controller and one namepsace created, 'nvme list --verbose --output json' prints the following output:
Quoted 5 lines
Quoted 1 line
Quoted 1 line
[...]
FYI, this patch is causing a regression:
Okay so does it make sense to retain the old output with the default --output-format-version=1? But then if the user selects --output-format-version=2 then we'd switch to the new output format?
Thanks,
--Nilay
Quoted 6 lines · Nilay Shroff
Quoted 1 line
Okay so does it make sense to retain the old output with the default --output-format-version=1? But then if the user selects --output-format-version=2 then we'd switch to the new output format?
Yes, the default output for nvme-cli v2 should be --output-format-version=1. As suggested by Tomáš we can set default format version to 2, for the next major nvme-cli release.
Thanks,
Daniel
Quoted 12 lines · Daniel Wagner
Quoted 6 lines · Nilay Shroff
Quoted 1 line
Okay so does it make sense to retain the old output with the default --output-format-version=1? But then if the user selects --output-format-version=2 then we'd switch to the new output format?
Yes, the default output for nvme-cli v2 should be --output-format-version=1. As suggested by Tomáš we can set default format version to 2, for the next major nvme-cli release.
Okay so I have made changes for ensuring backward compatibility. The changes are on its way...
Thanks,
--Nilay