]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-print: Show ANA state only for one namespace
authorDaniel Wagner <dwagner@suse.de>
Thu, 2 Jun 2022 08:58:42 +0000 (10:58 +0200)
committerDaniel Wagner <dwagner@suse.de>
Thu, 2 Jun 2022 09:51:32 +0000 (11:51 +0200)
'nvme list-subsys' shows the state of all controllers belonging to a
subsystem. The ANA state is a per namespace attribute hence it only
makes sense to show it if the user lists the subsystem for a
namespace.

Fixes: 7435ed9ae6a6 ("nvme-print: Show paths from the first namespace only")
Signed-off-by: Daniel Wagner <dwagner@suse.de>
nvme-print.c
nvme-print.h
nvme.c

index 7d264490cb97f2fc9f76f310d18f88abb52ec3e7..99fa9a25848cacdf056d35ddf330cf60a03b0ae6 100644 (file)
@@ -2373,7 +2373,8 @@ void nvme_show_supported_cap_config_log(
        }
 }
 
-static unsigned int nvme_show_subsystem_multipath(nvme_subsystem_t s)
+static unsigned int nvme_show_subsystem_multipath(nvme_subsystem_t s,
+                                                 bool show_ana)
 {
        nvme_ns_t n;
        nvme_path_t p;
@@ -2385,13 +2386,17 @@ static unsigned int nvme_show_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);
 
                printf(" +- %s %s %s %s %s\n",
-                      nvme_ctrl_get_name(c),
-                      nvme_ctrl_get_transport(c),
-                      nvme_ctrl_get_address(c),
-                      nvme_ctrl_get_state(c),
-                      nvme_path_get_ana_state(p));
+                       nvme_ctrl_get_name(c),
+                       nvme_ctrl_get_transport(c),
+                       nvme_ctrl_get_address(c),
+                       nvme_ctrl_get_state(c),
+                       ana_state);
                i++;
        }
 
@@ -2411,7 +2416,7 @@ static void nvme_show_subsystem_ctrls(nvme_subsystem_t s)
        }
 }
 
-static void nvme_show_subsystem(nvme_root_t r)
+static void nvme_show_subsystem(nvme_root_t r, bool show_ana)
 {
        nvme_host_t h;
 
@@ -2423,39 +2428,42 @@ static void nvme_show_subsystem(nvme_root_t r)
                               nvme_subsystem_get_nqn(s));
                        printf("\\\n");
 
-                       if (!nvme_show_subsystem_multipath(s))
+                       if (!nvme_show_subsystem_multipath(s, show_ana))
                                nvme_show_subsystem_ctrls(s);
                }
        }
 }
 
 static unsigned int json_print_nvme_subsystem_multipath(nvme_subsystem_t s,
+                                                       bool show_ana,
                                                        json_object *paths)
 {
        nvme_ns_t n;
+       nvme_path_t p;
        unsigned int i = 0;
 
-       nvme_subsystem_for_each_ns(s, n) {
-               nvme_path_t p;
-
-               nvme_namespace_for_each_path(n, p) {
-                       struct json_object *path_attrs;
-                       nvme_ctrl_t c = nvme_path_get_ctrl(p);
-
-                       path_attrs = json_create_object();
-                       json_object_add_value_string(path_attrs, "Name",
-                                                    nvme_ctrl_get_name(c));
-                       json_object_add_value_string(path_attrs, "Transport",
-                                                    nvme_ctrl_get_transport(c));
-                       json_object_add_value_string(path_attrs, "Address",
-                                                    nvme_ctrl_get_address(c));
-                       json_object_add_value_string(path_attrs, "State",
-                                                    nvme_ctrl_get_state(c));
+       n = nvme_subsystem_first_ns(s);
+       if (!n)
+               return 0;
+
+       nvme_namespace_for_each_path(n, p) {
+               struct json_object *path_attrs;
+               nvme_ctrl_t c = nvme_path_get_ctrl(p);
+
+               path_attrs = json_create_object();
+               json_object_add_value_string(path_attrs, "Name",
+                                            nvme_ctrl_get_name(c));
+               json_object_add_value_string(path_attrs, "Transport",
+                                            nvme_ctrl_get_transport(c));
+               json_object_add_value_string(path_attrs, "Address",
+                                            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_array_add_value_object(paths, path_attrs);
-                       i++;
-               }
+               json_array_add_value_object(paths, path_attrs);
+               i++;
        }
 
        return i;
@@ -2482,7 +2490,7 @@ static void json_print_nvme_subsystem_ctrls(nvme_subsystem_t s,
        }
 }
 
-static void json_print_nvme_subsystem_list(nvme_root_t r)
+static void json_print_nvme_subsystem_list(nvme_root_t r, bool show_ana)
 {
        struct json_object *host_attrs, *subsystem_attrs;
        struct json_object *subsystems, *paths;
@@ -2510,7 +2518,7 @@ static void json_print_nvme_subsystem_list(nvme_root_t r)
                        json_array_add_value_object(subsystems, subsystem_attrs);
                        paths = json_create_array();
 
-                       if (!json_print_nvme_subsystem_multipath(s, paths))
+                       if (!json_print_nvme_subsystem_multipath(s, show_ana, paths))
                                json_print_nvme_subsystem_ctrls(s, paths);
 
                        json_object_add_value_array(subsystem_attrs, "Paths",
@@ -2524,11 +2532,12 @@ static void json_print_nvme_subsystem_list(nvme_root_t r)
        json_free_object(root);
 }
 
-void nvme_show_subsystem_list(nvme_root_t r, enum nvme_print_flags flags)
+void nvme_show_subsystem_list(nvme_root_t r, bool show_ana,
+                             enum nvme_print_flags flags)
 {
        if (flags & JSON)
-               return json_print_nvme_subsystem_list(r);
-       nvme_show_subsystem(r);
+               return json_print_nvme_subsystem_list(r, show_ana);
+       nvme_show_subsystem(r, show_ana);
 }
 
 static void nvme_show_registers_cap(struct nvme_bar_cap *cap)
index 63041255c7149dbdf27fba0852b988651e7bf469..a318862d79dc9adeda3ef77d661221e84a4aa0f9 100644 (file)
@@ -88,7 +88,8 @@ void nvme_show_id_ns_descs(void *data, unsigned nsid, enum nvme_print_flags flag
 void nvme_show_lba_status(struct nvme_lba_status *list, unsigned long len,
        enum nvme_print_flags flags);
 void nvme_show_list_items(nvme_root_t t, enum nvme_print_flags flags);
-void nvme_show_subsystem_list(nvme_root_t t, enum nvme_print_flags flags);
+void nvme_show_subsystem_list(nvme_root_t t, bool show_ana,
+                             enum nvme_print_flags flags);
 void nvme_show_id_nvmset(struct nvme_id_nvmset_list *nvmset, unsigned nvmset_id,
        enum nvme_print_flags flags);
 void nvme_show_primary_ctrl_cap(const struct nvme_primary_ctrl_cap *cap,
diff --git a/nvme.c b/nvme.c
index bd6a6addf8906d9e47d5bedba727d579aae9ca61..217a8e000f40bc335cab5d6e3c9b8094d9c841ff 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -2526,6 +2526,7 @@ static int list_subsys(int argc, char **argv, struct command *cmd,
        const char *verbose = "Increase output verbosity";
        nvme_scan_filter_t filter = NULL;
        int err;
+       int nsid = NVME_NSID_ALL;
 
        struct config {
                char    *output_format;
@@ -2574,7 +2575,7 @@ static int list_subsys(int argc, char **argv, struct command *cmd,
        }
 
        if (devicename) {
-               int subsys_num, nsid;
+               int subsys_num;
 
                if (sscanf(devicename,"nvme%dn%d",
                           &subsys_num, &nsid) != 2) {
@@ -2592,7 +2593,7 @@ static int list_subsys(int argc, char **argv, struct command *cmd,
                goto ret;
        }
 
-       nvme_show_subsystem_list(r, flags);
+       nvme_show_subsystem_list(r, nsid != NVME_NSID_ALL, flags);
 
 ret:
        if (r)