From f78628a0040131b28b5dd28837f21dd452e61cc9 Mon Sep 17 00:00:00 2001 From: Andreas Hindborg Date: Mon, 13 Sep 2021 13:13:53 +0000 Subject: [PATCH] zns: Add list command Signed-off-by: Andreas Hindborg --- nvme-print.c | 2 +- nvme-print.h | 1 + plugins/zns/zns.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++ plugins/zns/zns.h | 1 + 4 files changed, 98 insertions(+), 1 deletion(-) diff --git a/nvme-print.c b/nvme-print.c index 7bb34cd3..fc6d9c51 100644 --- a/nvme-print.c +++ b/nvme-print.c @@ -5632,7 +5632,7 @@ void nvme_show_lba_status(struct nvme_lba_status *list, unsigned long len, } } -static void nvme_show_list_item(nvme_ns_t n) +void nvme_show_list_item(nvme_ns_t n) { char usage[128] = { 0 }, format[128] = { 0 }; diff --git a/nvme-print.h b/nvme-print.h index 0a606f4e..c3ba64ae 100644 --- a/nvme-print.h +++ b/nvme-print.h @@ -103,6 +103,7 @@ void nvme_show_zns_changed( struct nvme_zns_changed_zone_log *log, unsigned long flags); void nvme_show_zns_report_zones(void *report, __u32 descs, __u8 ext_size, __u32 report_size, unsigned long flags); +void nvme_show_list_item(nvme_ns_t n); const char *nvme_cmd_to_string(int admin, __u8 opcode); const char *nvme_select_to_string(int sel); diff --git a/plugins/zns/zns.c b/plugins/zns/zns.c index 3b4ae393..9562d364 100644 --- a/plugins/zns/zns.c +++ b/plugins/zns/zns.c @@ -15,6 +15,101 @@ #include "zns.h" static const char *namespace_id = "Namespace identifier to use"; +static const char dash[100] = { [0 ... 99] = '-' }; + +static int detect_zns(nvme_ns_t ns, int *out_supported) +{ + int err = 0; + char *zoned; + + *out_supported = 0; + + zoned = nvme_get_attr(nvme_ns_get_sysfs_dir(ns), "queue/zoned"); + if (!zoned) { + *out_supported = 0; + return err; + } + + *out_supported = strcmp("host-managed", zoned) == 0; + free(zoned); + + return err; +} + +static int print_zns_list_ns(nvme_ns_t ns) +{ + int supported; + int err = 0; + + err = detect_zns(ns, &supported); + if (err) { + perror("Failed to enumerate namespace"); + return err; + } + + if (supported) { + nvme_show_list_item(ns); + } + + return err; +} + +static int print_zns_list(nvme_root_t nvme_root) +{ + int err = 0; + nvme_host_t h; + nvme_subsystem_t s; + nvme_ctrl_t c; + nvme_ns_t n; + nvme_for_each_host(nvme_root, h) + { + nvme_for_each_subsystem(h, s) + { + nvme_subsystem_for_each_ns(s, n) + { + err = print_zns_list_ns(n); + if (err) + return err; + } + + nvme_subsystem_for_each_ctrl(s, c) + { + nvme_ctrl_for_each_ns(c, n) + { + err = print_zns_list_ns(n); + if (err) + return err; + } + } + } + } + + return err; +} + +static int list(int argc, char **argv, struct command *cmd, + struct plugin *plugin) +{ + int err = 0; + nvme_root_t nvme_root; + + printf("%-21s %-20s %-40s %-9s %-26s %-16s %-8s\n", "Node", "SN", + "Model", "Namespace", "Usage", "Format", "FW Rev"); + printf("%-.21s %-.20s %-.40s %-.9s %-.26s %-.16s %-.8s\n", dash, dash, + dash, dash, dash, dash, dash); + + nvme_root = nvme_scan(NULL); + if (nvme_root) { + err = print_zns_list(nvme_root); + } else { + fprintf(stderr, "Failed to scan nvme subsystems\n"); + err = -errno; + } + + nvme_free_tree(nvme_root); + + return err; +} static int id_ctrl(int argc, char **argv, struct command *cmd, struct plugin *plugin) { diff --git a/plugins/zns/zns.h b/plugins/zns/zns.h index a92de69c..a746aaa7 100644 --- a/plugins/zns/zns.h +++ b/plugins/zns/zns.h @@ -21,6 +21,7 @@ PLUGIN(NAME("zns", "Zoned Namespace Command Set"), ENTRY("set-zone-desc", "Attaches zone descriptor extension data", set_zone_desc) ENTRY("zone-append", "Writes data and metadata (if applicable), appended to the end of the requested zone", zone_append) ENTRY("changed-zone-list", "Retrieves the changed zone list log", changed_zone_list) + ENTRY("list", "List ZNS enabled devices", list) ) ); -- 2.50.1