From 66d115af824905333d40e8bd7b40069b20b4f88d Mon Sep 17 00:00:00 2001 From: Daniel Wagner Date: Tue, 3 Jun 2025 14:16:04 +0200 Subject: [PATCH 1/2] nvme: extend filter to match device name nvme_match_device_filter is used by list-subsys where it is possible to limit the output by providing a block device. The current implementation only matches on subsystem ID level, but this is not what the expectation is from the user. It is expected to limit the output by the block device name. Change the match to use the block device name instead. Signed-off-by: Daniel Wagner --- nvme.c | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/nvme.c b/nvme.c index 7c4e0bdadc..1dec63a447 100644 --- a/nvme.c +++ b/nvme.c @@ -3388,33 +3388,24 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin * static bool nvme_match_device_filter(nvme_subsystem_t s, nvme_ctrl_t c, nvme_ns_t ns, void *f_args) { - int ret, instance, nsid, s_num; char *devname = f_args; + nvme_ns_t n; - if (!devname || !strlen(devname)) + if (ns && !strcmp(devname, nvme_ns_get_name(ns))) return true; - ret = sscanf(devname, "nvme%dn%d", &instance, &nsid); - if (ret != 2) - return true; - - if (s) { - ret = sscanf(nvme_subsystem_get_name(s), "nvme-subsys%d", - &s_num); - if (ret == 1 && s_num == instance) - return true; - } if (c) { s = nvme_ctrl_get_subsystem(c); - - ret = sscanf(nvme_subsystem_get_name(s), "nvme-subsys%d", - &s_num); - if (ret == 1 && s_num == instance) - return true; + nvme_ctrl_for_each_ns(c, n) { + if (!strcmp(devname, nvme_ns_get_name(n))) + return true; + } } - if (ns) { - if (!strcmp(devname, nvme_ns_get_name(ns))) - return true; + if (s) { + nvme_subsystem_for_each_ns(s, n) { + if (!strcmp(devname, nvme_ns_get_name(n))) + return true; + } } return false; From d884f66f5cb6e1b9883d0bf92188aabd914909b8 Mon Sep 17 00:00:00 2001 From: Daniel Wagner Date: Tue, 3 Jun 2025 14:24:05 +0200 Subject: [PATCH 2/2] nvme-print: suppress output when no ctrl is present for list-subsys When using list-subsys with the optional device filter argument, the subsystem might not have any controller associated anymore, it was filtered out. Avoid outputting any whitespace (or empty json data structures) in this case. Signed-off-by: Daniel Wagner --- nvme-print-json.c | 8 ++++++++ nvme-print-stdout.c | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/nvme-print-json.c b/nvme-print-json.c index 1766cae882..bbb948f53e 100644 --- a/nvme-print-json.c +++ b/nvme-print-json.c @@ -2558,6 +2558,14 @@ static void json_print_nvme_subsystem_list(nvme_root_t r, bool show_ana) obj_add_str(host_attrs, "HostID", hostid); subsystems = json_create_array(); nvme_for_each_subsystem(h, s) { + nvme_ctrl_t c; + bool no_ctrl = true; + + nvme_subsystem_for_each_ctrl(s, c) + no_ctrl = false; + if (no_ctrl) + continue; + subsystem_attrs = json_create_object(); obj_add_str(subsystem_attrs, "Name", nvme_subsystem_get_name(s)); obj_add_str(subsystem_attrs, "NQN", nvme_subsystem_get_nqn(s)); diff --git a/nvme-print-stdout.c b/nvme-print-stdout.c index 4686241e63..7d7d4b2735 100644 --- a/nvme-print-stdout.c +++ b/nvme-print-stdout.c @@ -1134,6 +1134,14 @@ static void stdout_subsystem(nvme_root_t r, bool show_ana) nvme_subsystem_t s; nvme_for_each_subsystem(h, s) { + bool no_ctrl = true; + nvme_ctrl_t c; + + nvme_subsystem_for_each_ctrl(s, c) + no_ctrl = false; + if (no_ctrl) + continue; + if (!first) printf("\n"); first = false;