From: Sagi Grimberg <sagi@grimberg.me>
Date: Mon, 12 Jun 2023 15:34:06 +0000 (+0300)
Subject: nvme-print: fix wrong list-subsys output when ns are attached to different ctrls
X-Git-Tag: v2.5~27
X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=1cc057ae19c8bfaea0120a953a40aa18dfe3e027;p=users%2Fsagi%2Fnvme-cli.git

nvme-print: fix wrong list-subsys output when ns are attached to different ctrls

If we list subsystem controllers not for a specific ns (user did not pass
a namespace device node), we cannot look at the first ns in the subsystem
to list because it may not be attached to all subsystem controllers.

We incorrectly assumes that subsystem namespaces (or at least the first of
them) are attached to all subsystem controllers.

Fix that by showing the multipath subsystem-list flavor only when the user
passed a specific namespace, and fallback to a simple controller list otherwise.

Fixes: cdeaab8fca6e ("nvme-print: Show ANA state only for one namespace")
Fixes: 7435ed9ae6a6 ("nvme-print: Show paths from the first namespace only")
Signed-off-by: Sagi Grimberg <sagi@grimberg.me>
Signed-off-by: Daniel Wagner <dwagner@suse.de>
---

diff --git a/nvme-print-json.c b/nvme-print-json.c
index 132603d3..2f2b686b 100644
--- a/nvme-print-json.c
+++ b/nvme-print-json.c
@@ -1717,7 +1717,6 @@ static void json_nvme_fdp_ruh_status(struct nvme_fdp_ruh_status *status, size_t
 }
 
 static unsigned int json_print_nvme_subsystem_multipath(nvme_subsystem_t s,
-							bool show_ana,
 						        json_object *paths)
 {
 	nvme_ns_t n;
@@ -1741,9 +1740,8 @@ static unsigned int json_print_nvme_subsystem_multipath(nvme_subsystem_t s,
 					     nvme_ctrl_get_address(c));
 		json_object_add_value_string(path_attrs, "State",
 					     nvme_ctrl_get_state(c));
-		if (show_ana)
-			json_object_add_value_string(path_attrs, "ANAState",
-						     nvme_path_get_ana_state(p));
+		json_object_add_value_string(path_attrs, "ANAState",
+					     nvme_path_get_ana_state(p));
 		json_array_add_value_object(paths, path_attrs);
 		i++;
 	}
@@ -1803,7 +1801,7 @@ static void json_print_nvme_subsystem_list(nvme_root_t r, bool show_ana)
 			json_array_add_value_object(subsystems, subsystem_attrs);
 			paths = json_create_array();
 
-			if (!json_print_nvme_subsystem_multipath(s, show_ana, paths))
+			if (!show_ana || !json_print_nvme_subsystem_multipath(s, paths))
 				json_print_nvme_subsystem_ctrls(s, paths);
 
 			json_object_add_value_array(subsystem_attrs, "Paths",
diff --git a/nvme-print-stdout.c b/nvme-print-stdout.c
index 65e48afb..7c0a0520 100644
--- a/nvme-print-stdout.c
+++ b/nvme-print-stdout.c
@@ -784,8 +784,7 @@ static void stdout_supported_cap_config_log(struct nvme_supported_cap_config_lis
 	}
 }
 
-static unsigned int stdout_subsystem_multipath(nvme_subsystem_t s,
-					       bool show_ana)
+static unsigned int stdout_subsystem_multipath(nvme_subsystem_t s)
 {
 	nvme_ns_t n;
 	nvme_path_t p;
@@ -797,10 +796,7 @@ static unsigned int stdout_subsystem_multipath(nvme_subsystem_t s,
 
 	nvme_namespace_for_each_path(n, p) {
 		nvme_ctrl_t c = nvme_path_get_ctrl(p);
-		const char *ana_state = "";
-
-		if (show_ana)
-			ana_state = nvme_path_get_ana_state(p);
+		const char *ana_state = ana_state = nvme_path_get_ana_state(p);
 
 		printf(" +- %s %s %s %s %s\n",
 			nvme_ctrl_get_name(c),
@@ -839,7 +835,7 @@ static void stdout_subsystem(nvme_root_t r, bool show_ana)
 			       nvme_subsystem_get_nqn(s));
 			printf("\\\n");
 
-			if (!stdout_subsystem_multipath(s, show_ana))
+			if (!show_ana || !stdout_subsystem_multipath(s))
 				stdout_subsystem_ctrls(s);
 		}
 	}