From cdeaab8fca6e86514b85dd285a23b8161999b75e Mon Sep 17 00:00:00 2001 From: Daniel Wagner Date: Thu, 2 Jun 2022 10:58:42 +0200 Subject: [PATCH] nvme-print: Show ANA state only for one namespace '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 --- nvme-print.c | 73 +++++++++++++++++++++++++++++----------------------- nvme-print.h | 3 ++- nvme.c | 5 ++-- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/nvme-print.c b/nvme-print.c index 7d264490..99fa9a25 100644 --- a/nvme-print.c +++ b/nvme-print.c @@ -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) diff --git a/nvme-print.h b/nvme-print.h index 63041255..a318862d 100644 --- a/nvme-print.h +++ b/nvme-print.h @@ -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 bd6a6add..217a8e00 100644 --- 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) -- 2.50.1