linux-nvme.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] nvme-list: fix verbose JSON output for 'nvme list' command
@ 2025-07-21 10:29 Nilay Shroff
  2025-07-24 14:18 ` Daniel Wagner
  0 siblings, 1 reply; 6+ messages in thread
From: Nilay Shroff @ 2025-07-21 10:29 UTC (permalink / raw)
  To: linux-nvme; +Cc: dwagner, msmurthy, gjoyce

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.

Reported-by: Maram Srimannarayana Murthy <msmurthy@imap.linux.ibm.com>
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
 nvme-print-json.c   | 170 ++++++++++++++++++++++++++------------------
 nvme-print-stdout.c |  12 ----
 nvme.h              |  12 ++++
 3 files changed, 113 insertions(+), 81 deletions(-)

diff --git a/nvme-print-json.c b/nvme-print-json.c
index a4b8df9f..3d656baa 100644
--- a/nvme-print-json.c
+++ b/nvme-print-json.c
@@ -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);
 		}
diff --git a/nvme-print-stdout.c b/nvme-print-stdout.c
index 7943b91c..52d8d2b1 100644
--- a/nvme-print-stdout.c
+++ b/nvme-print-stdout.c
@@ -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)
 {
diff --git a/nvme.h b/nvme.h
index 1a15fc18..248c5c7c 100644
--- a/nvme.h
+++ b/nvme.h
@@ -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);
 
 /*
-- 
2.50.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvme-list: fix verbose JSON output for 'nvme list' command
  2025-07-21 10:29 [PATCH] nvme-list: fix verbose JSON output for 'nvme list' command Nilay Shroff
@ 2025-07-24 14:18 ` Daniel Wagner
  2025-08-04 15:25   ` Daniel Wagner
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Wagner @ 2025-07-24 14:18 UTC (permalink / raw)
  To: linux-nvme, Nilay Shroff; +Cc: Daniel Wagner, dwagner, msmurthy, gjoyce


On Mon, 21 Jul 2025 15:59:11 +0530, Nilay Shroff wrote:
> 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,
-- 
Daniel Wagner <wagi@kernel.org>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvme-list: fix verbose JSON output for 'nvme list' command
  2025-07-24 14:18 ` Daniel Wagner
@ 2025-08-04 15:25   ` Daniel Wagner
  2025-08-05  4:30     ` Nilay Shroff
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Wagner @ 2025-08-04 15:25 UTC (permalink / raw)
  To: Nilay Shroff; +Cc: linux-nvme, msmurthy, gjoyce

On Thu, Jul 24, 2025 at 04:18:16PM +0200, Daniel Wagner wrote:
> 
> On Mon, 21 Jul 2025 15:59:11 +0530, Nilay Shroff wrote:
> > 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:

https://github.com/linux-nvme/nvme-cli/issues/2886


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvme-list: fix verbose JSON output for 'nvme list' command
  2025-08-04 15:25   ` Daniel Wagner
@ 2025-08-05  4:30     ` Nilay Shroff
  2025-08-05  7:27       ` Daniel Wagner
  0 siblings, 1 reply; 6+ messages in thread
From: Nilay Shroff @ 2025-08-05  4:30 UTC (permalink / raw)
  To: Daniel Wagner; +Cc: linux-nvme, msmurthy, gjoyce



On 8/4/25 8:55 PM, Daniel Wagner wrote:
> On Thu, Jul 24, 2025 at 04:18:16PM +0200, Daniel Wagner wrote:
>>
>> On Mon, 21 Jul 2025 15:59:11 +0530, Nilay Shroff wrote:
>>> 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:
> 
> https://github.com/linux-nvme/nvme-cli/issues/2886

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


 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvme-list: fix verbose JSON output for 'nvme list' command
  2025-08-05  4:30     ` Nilay Shroff
@ 2025-08-05  7:27       ` Daniel Wagner
  2025-08-05 10:18         ` Nilay Shroff
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Wagner @ 2025-08-05  7:27 UTC (permalink / raw)
  To: Nilay Shroff; +Cc: linux-nvme, msmurthy, gjoyce, tbzatek

On Tue, Aug 05, 2025 at 10:00:53AM +0530, Nilay Shroff wrote:
> > https://github.com/linux-nvme/nvme-cli/issues/2886
> 
> 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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvme-list: fix verbose JSON output for 'nvme list' command
  2025-08-05  7:27       ` Daniel Wagner
@ 2025-08-05 10:18         ` Nilay Shroff
  0 siblings, 0 replies; 6+ messages in thread
From: Nilay Shroff @ 2025-08-05 10:18 UTC (permalink / raw)
  To: Daniel Wagner; +Cc: linux-nvme, msmurthy, gjoyce, tbzatek



On 8/5/25 12:57 PM, Daniel Wagner wrote:
> On Tue, Aug 05, 2025 at 10:00:53AM +0530, Nilay Shroff wrote:
>>> https://github.com/linux-nvme/nvme-cli/issues/2886
>>
>> 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



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-08-05 10:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-21 10:29 [PATCH] nvme-list: fix verbose JSON output for 'nvme list' command Nilay Shroff
2025-07-24 14:18 ` Daniel Wagner
2025-08-04 15:25   ` Daniel Wagner
2025-08-05  4:30     ` Nilay Shroff
2025-08-05  7:27       ` Daniel Wagner
2025-08-05 10:18         ` Nilay Shroff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).