]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
tree: add 'nvme_host' structure
authorHannes Reinecke <hare@suse.de>
Thu, 8 Apr 2021 12:13:48 +0000 (14:13 +0200)
committerHannes Reinecke <hare@suse.de>
Fri, 11 Jun 2021 09:28:09 +0000 (11:28 +0200)
Add a 'nvme_host' structure to hold the host NQN and host ID and
place it between 'nvme_root' and 'nvme_subsystem'.

Signed-off-by: Hannes Reinecke <hare@suse.de>
examples/display-columnar.c
examples/display-tree.c
examples/telemetry-listen.c
src/nvme/tree.c
src/nvme/tree.h
test/cpp.cc
test/test.c
test/zns.c

index 283f073e44b4de3a0673489a43addbd88cf62584..cd3f007f16a55cb435ae95788acdb23068b1b5d5 100644 (file)
@@ -17,6 +17,7 @@ static const char dash[101] = {[0 ... 99] = '-'};
 int main()
 {
        nvme_root_t r;
+       nvme_host_t h;
        nvme_subsystem_t s;
        nvme_ctrl_t c;
        nvme_path_t p;
@@ -30,15 +31,19 @@ int main()
        printf("%-16s %-96s %-.16s\n", "Subsystem", "Subsystem-NQN", "Controllers");
        printf("%-.16s %-.96s %-.16s\n", dash, dash, dash);
 
-       nvme_for_each_subsystem(r, s) {
-               bool first = true;
-               printf("%-16s %-96s ", nvme_subsystem_get_name(s), nvme_subsystem_get_nqn(s));
+       nvme_for_each_host(r, h) {
+               nvme_for_each_subsystem(h, s) {
+                       bool first = true;
+                       printf("%-16s %-96s ", nvme_subsystem_get_name(s),
+                              nvme_subsystem_get_nqn(s));
 
-               nvme_subsystem_for_each_ctrl(s, c) {
-                       printf("%s%s", first ? "": ", ", nvme_ctrl_get_name(c));
-                       first = false;
+                       nvme_subsystem_for_each_ctrl(s, c) {
+                               printf("%s%s", first ? "": ", ",
+                                      nvme_ctrl_get_name(c));
+                               first = false;
+                       }
+                       printf("\n");
                }
-               printf("\n");
        }
        printf("\n");
 
@@ -47,28 +52,33 @@ int main()
        printf("%-.8s %-.20s %-.40s %-.8s %-.6s %-.14s %-.12s %-.16s\n", dash, dash,
                dash, dash, dash, dash, dash, dash);
 
-       nvme_for_each_subsystem(r, s) {
-               nvme_subsystem_for_each_ctrl(s, c) {
-                       bool first = true;
-
-                       printf("%-8s %-20s %-40s %-8s %-6s %-14s %-12s ",
-                               nvme_ctrl_get_name(c), nvme_ctrl_get_serial(c),
-                               nvme_ctrl_get_model(c), nvme_ctrl_get_firmware(c),
-                               nvme_ctrl_get_transport(c), nvme_ctrl_get_address(c),
-                               nvme_subsystem_get_name(s));
-
-                       nvme_ctrl_for_each_ns(c, n) {
-                               printf("%s%s", first ? "": ", ",
-                                       nvme_ns_get_name(n));
-                               first = false;
-                       }
-
-                       nvme_ctrl_for_each_path(c, p) {
-                               printf("%s%s", first ? "": ", ",
-                                       nvme_ns_get_name(nvme_path_get_ns(p)));
-                               first = false;
+       nvme_for_each_host(r, h) {
+               nvme_for_each_subsystem(h, s) {
+                       nvme_subsystem_for_each_ctrl(s, c) {
+                               bool first = true;
+
+                               printf("%-8s %-20s %-40s %-8s %-6s %-14s %-12s ",
+                                      nvme_ctrl_get_name(c),
+                                      nvme_ctrl_get_serial(c),
+                                      nvme_ctrl_get_model(c),
+                                      nvme_ctrl_get_firmware(c),
+                                      nvme_ctrl_get_transport(c),
+                                      nvme_ctrl_get_address(c),
+                                      nvme_subsystem_get_name(s));
+
+                               nvme_ctrl_for_each_ns(c, n) {
+                                       printf("%s%s", first ? "": ", ",
+                                              nvme_ns_get_name(n));
+                                       first = false;
+                               }
+
+                               nvme_ctrl_for_each_path(c, p) {
+                                       printf("%s%s", first ? "": ", ",
+                                              nvme_ns_get_name(nvme_path_get_ns(p)));
+                                       first = false;
+                               }
+                               printf("\n");
                        }
-                       printf("\n");
                }
        }
        printf("\n");
@@ -76,31 +86,33 @@ int main()
        printf("%-12s %-8s %-16s %-8s %-16s\n", "Device", "NSID", "Sectors", "Format", "Controllers");
        printf("%-.12s %-.8s %-.16s %-.8s %-.16s\n", dash, dash, dash, dash, dash);
 
-       nvme_for_each_subsystem(r, s) {
-               nvme_subsystem_for_each_ctrl(s, c)
-                       nvme_ctrl_for_each_ns(c, n)
-                               printf("%-12s %-8d %-16lu %-8d %s\n",
-                                       nvme_ns_get_name(n),
-                                       nvme_ns_get_nsid(n),
-                                       nvme_ns_get_lba_count(n),
-                                       nvme_ns_get_lba_size(n),
-                                       nvme_ctrl_get_name(c));
-
-               nvme_subsystem_for_each_ns(s, n) {
-                       bool first = true;
-
-                       printf("%-12s %-8d %-16lu %-8d ",
-                               nvme_ns_get_name(n),
-                               nvme_ns_get_nsid(n),
-                               nvme_ns_get_lba_count(n),
-                               nvme_ns_get_lba_size(n));
-
+       nvme_for_each_host(r, h) {
+               nvme_for_each_subsystem(h, s) {
                        nvme_subsystem_for_each_ctrl(s, c) {
-                               printf("%s%s", first ? "" : ", ",
-                                       nvme_ctrl_get_name(c));
-                               first = false;
+                               nvme_ctrl_for_each_ns(c, n)
+                                       printf("%-12s %-8d %-16lu %-8d %s\n",
+                                              nvme_ns_get_name(n),
+                                              nvme_ns_get_nsid(n),
+                                              nvme_ns_get_lba_count(n),
+                                              nvme_ns_get_lba_size(n),
+                                              nvme_ctrl_get_name(c));
+                       }
+
+                       nvme_subsystem_for_each_ns(s, n) {
+                               bool first = true;
+
+                               printf("%-12s %-8d %-16lu %-8d ",
+                                      nvme_ns_get_name(n),
+                                      nvme_ns_get_nsid(n),
+                                      nvme_ns_get_lba_count(n),
+                                      nvme_ns_get_lba_size(n));
+                               nvme_subsystem_for_each_ctrl(s, c) {
+                                       printf("%s%s", first ? "" : ", ",
+                                              nvme_ctrl_get_name(c));
+                                       first = false;
+                               }
+                               printf("\n");
                        }
-                       printf("\n");
                }
        }
        return 0;
index 58520dfd96ffde210925a89439a92b9847587fde..66b16c276d699b3cee5eeeb4532758b385051fd7 100644 (file)
@@ -16,6 +16,7 @@
 int main()
 {
        nvme_root_t r;
+       nvme_host_t h;
        nvme_subsystem_t s, _s;
        nvme_ctrl_t c, _c;
        nvme_path_t p, _p;
@@ -26,40 +27,43 @@ int main()
                return -1;
 
        printf(".\n");
-       nvme_for_each_subsystem_safe(r, s, _s) {
-               printf("%c-- %s - NQN=%s\n", _s ? '|' : '`',
-                       nvme_subsystem_get_name(s),
-                       nvme_subsystem_get_nqn(s));
+       nvme_for_each_host(r, h) {
+               nvme_for_each_subsystem_safe(h, s, _s) {
+                       printf("%c-- %s - NQN=%s\n", _s ? '|' : '`',
+                              nvme_subsystem_get_name(s),
+                              nvme_subsystem_get_nqn(s));
 
-               nvme_subsystem_for_each_ns_safe(s, n, _n) {
-                       printf("%c   |-- %s lba size:%d lba max:%lu\n",
-                               _s ? '|' : ' ',
-                               nvme_ns_get_name(n), nvme_ns_get_lba_size(n),
-                               nvme_ns_get_lba_count(n));
-               }
+                       nvme_subsystem_for_each_ns_safe(s, n, _n) {
+                               printf("%c   |-- %s lba size:%d lba max:%lu\n",
+                                      _s ? '|' : ' ',
+                                      nvme_ns_get_name(n),
+                                      nvme_ns_get_lba_size(n),
+                                      nvme_ns_get_lba_count(n));
+                       }
 
-               nvme_subsystem_for_each_ctrl_safe(s, c, _c) {
-                       printf("%c   %c-- %s %s %s %s\n",
-                               _s ? '|' : ' ', _c ? '|' : '`',
-                               nvme_ctrl_get_name(c),
-                               nvme_ctrl_get_transport(c),
-                               nvme_ctrl_get_address(c),
-                               nvme_ctrl_get_state(c));
+                       nvme_subsystem_for_each_ctrl_safe(s, c, _c) {
+                               printf("%c   %c-- %s %s %s %s\n",
+                                      _s ? '|' : ' ', _c ? '|' : '`',
+                                      nvme_ctrl_get_name(c),
+                                      nvme_ctrl_get_transport(c),
+                                      nvme_ctrl_get_address(c),
+                                      nvme_ctrl_get_state(c));
 
-                       nvme_ctrl_for_each_ns_safe(c, n, _n)
-                               printf("%c   %c   %c-- %s lba size:%d lba max:%lu\n",
-                                       _s ? '|' : ' ', _c ? '|' : ' ',
-                                       _n ? '|' : '`',
-                                       nvme_ns_get_name(n),
-                                       nvme_ns_get_lba_size(n),
-                                       nvme_ns_get_lba_count(n));
+                               nvme_ctrl_for_each_ns_safe(c, n, _n)
+                                       printf("%c   %c   %c-- %s lba size:%d lba max:%lu\n",
+                                              _s ? '|' : ' ', _c ? '|' : ' ',
+                                              _n ? '|' : '`',
+                                              nvme_ns_get_name(n),
+                                              nvme_ns_get_lba_size(n),
+                                              nvme_ns_get_lba_count(n));
 
-                       nvme_ctrl_for_each_path_safe(c, p, _p)
-                               printf("%c   %c   %c-- %s %s\n",
-                                       _s ? '|' : ' ', _c ? '|' : ' ',
-                                       _p ? '|' : '`',
-                                       nvme_path_get_name(p),
-                                       nvme_path_get_ana_state(p));
+                               nvme_ctrl_for_each_path_safe(c, p, _p)
+                                       printf("%c   %c   %c-- %s %s\n",
+                                              _s ? '|' : ' ', _c ? '|' : ' ',
+                                              _p ? '|' : '`',
+                                              nvme_path_get_name(p),
+                                              nvme_path_get_ana_state(p));
+                       }
                }
        }
        nvme_free_tree(r);
index 7d1f260bda7ba7f4446f87523a9c54ef57f954df..c4cde957dbedc3864002441ba4352a474ee592ec 100644 (file)
@@ -126,30 +126,34 @@ int main()
 
        nvme_subsystem_t s;
        nvme_ctrl_t c;
+       nvme_host_t h;
        nvme_root_t r;
 
        r = nvme_scan();
        if (!r)
                return EXIT_FAILURE;
 
-       nvme_for_each_subsystem(r, s)
-               nvme_subsystem_for_each_ctrl(s, c)
-                       i++;
+       nvme_for_each_host(r, h)
+               nvme_for_each_subsystem(h, s)
+                       nvme_subsystem_for_each_ctrl(s, c)
+                               i++;
 
        e = calloc(i, sizeof(e));
        FD_ZERO(&fds);
        i = 0;
 
-       nvme_for_each_subsystem(r, s) {
-               nvme_subsystem_for_each_ctrl(s, c) {
-                       int fd = open_uevent(c);
-
-                       if (fd < 0)
-                               continue;
-                       FD_SET(fd, &fds);
-                       e[i].uevent_fd = fd;
-                       e[i].c = c;
-                       i++;
+       nvme_for_each_host(r, h) {
+               nvme_for_each_subsystem(h, s) {
+                       nvme_subsystem_for_each_ctrl(s, c) {
+                               int fd = open_uevent(c);
+
+                               if (fd < 0)
+                                       continue;
+                               FD_SET(fd, &fds);
+                               e[i].uevent_fd = fd;
+                               e[i].c = c;
+                               i++;
+                       }
                }
        }
 
index 669f83be9f5eee9dd2beefc57781988252bc3801..eb554a3523127dd336a905c3ebc7f7b2247692d3 100644 (file)
 #include "tree.h"
 #include "filters.h"
 #include "util.h"
+#include "fabrics.h"
 
 /* XXX: Make a place for private declarations */
 extern int nvme_set_attr(const char *dir, const char *attr, const char *value);
 
+static void nvme_free_host(struct nvme_host *h);
 static void nvme_free_subsystem(struct nvme_subsystem *s);
 static int nvme_subsystem_scan_namespace(struct nvme_subsystem *s, char *name);
-static int nvme_scan_subsystem(struct nvme_root *r, char *name,
+static int nvme_scan_subsystem(struct nvme_host *h, char *name,
                               nvme_scan_filter_t f);
 static int nvme_subsystem_scan_ctrl(struct nvme_subsystem *s, char *name);
 static int nvme_ctrl_scan_namespace(struct nvme_ctrl *c, char *name);
@@ -104,15 +106,24 @@ struct nvme_subsystem {
        struct list_node entry;
        struct list_head ctrls;
        struct list_head namespaces;
-       struct nvme_root *r;
+       struct nvme_host *h;
 
        char *name;
        char *sysfs_dir;
        char *subsysnqn;
 };
 
-struct nvme_root {
+struct nvme_host {
+       struct list_node entry;
        struct list_head subsystems;
+       struct nvme_root *r;
+
+       char *hostnqn;
+       char *hostid;
+};
+
+struct nvme_root {
+       struct list_head hosts;
 };
 
 static inline void nvme_free_dirents(struct dirent **d, int i)
@@ -121,17 +132,42 @@ static inline void nvme_free_dirents(struct dirent **d, int i)
                free(d[i]);
        free(d);
 }
+
+nvme_host_t nvme_default_host(nvme_root_t r)
+{
+       struct nvme_host *h = calloc(1, sizeof(*h));
+
+       if (!h) {
+               errno = ENOMEM;
+               return NULL;
+       }
+
+       h->hostnqn = nvmf_hostnqn_from_file();
+       h->hostid = nvmf_hostid_from_file();
+       list_head_init(&h->subsystems);
+       list_add(&r->hosts, &h->entry);
+       h->r = r;
+       return h;
+}
+
 static int nvme_scan_topology(struct nvme_root *r, nvme_scan_filter_t f)
 {
+       struct nvme_host *h;
        struct dirent **subsys;
        int i, ret;
 
+       h = nvme_default_host(r);
+       if (!h) {
+               free(r);
+               errno = ENOMEM;
+               return -1;
+       }
        ret = nvme_scan_subsystems(&subsys);
        if (ret < 0)
                return ret;
 
        for (i = 0; i < ret; i++)
-               nvme_scan_subsystem(r, subsys[i]->d_name, f);
+               nvme_scan_subsystem(h, subsys[i]->d_name, f);
 
        nvme_free_dirents(subsys, i);
        return 0;
@@ -146,7 +182,7 @@ nvme_root_t nvme_scan_filter(nvme_scan_filter_t f)
                return NULL;
        }
 
-       list_head_init(&r->subsystems);
+       list_head_init(&r->hosts);
        nvme_scan_topology(r, f);
        return r;
 }
@@ -156,40 +192,65 @@ nvme_root_t nvme_scan()
        return nvme_scan_filter(NULL);
 }
 
-nvme_subsystem_t nvme_first_subsystem(nvme_root_t r)
+nvme_host_t nvme_first_host(nvme_root_t r)
+{
+       return list_top(&r->hosts, struct nvme_host, entry);
+}
+
+nvme_host_t nvme_next_host(nvme_root_t r, nvme_host_t h)
+{
+       return h ? list_next(&r->hosts, h, entry) : NULL;
+}
+
+nvme_root_t nvme_host_get_root(nvme_host_t h)
+{
+       return h->r;
+}
+
+const char *nvme_host_get_hostnqn(nvme_host_t h)
+{
+       return h->hostnqn;
+}
+
+const char *nvme_host_get_hostid(nvme_host_t h)
 {
-       return list_top(&r->subsystems, struct nvme_subsystem, entry);
+       return h->hostid;
 }
 
-nvme_subsystem_t nvme_next_subsystem(nvme_root_t r, nvme_subsystem_t s)
+nvme_subsystem_t nvme_first_subsystem(nvme_host_t h)
 {
-       return s ? list_next(&r->subsystems, s, entry) : NULL;
+       return list_top(&h->subsystems, struct nvme_subsystem, entry);
+}
+
+nvme_subsystem_t nvme_next_subsystem(nvme_host_t h, nvme_subsystem_t s)
+{
+       return s ? list_next(&h->subsystems, s, entry) : NULL;
 }
 
 void nvme_refresh_topology(nvme_root_t r)
 {
-       struct nvme_subsystem *s, *_s;
+       struct nvme_host *h, *_h;
 
-       nvme_for_each_subsystem_safe(r, s, _s)
-               nvme_free_subsystem(s);
+       nvme_for_each_host_safe(r, h, _h)
+               nvme_free_host(h);
        nvme_scan_topology(r, NULL);
 }
 
 void nvme_reset_topology(nvme_root_t r)
 {
-       struct nvme_subsystem *s, *_s;
+       struct nvme_host *h, *_h;
 
-       nvme_for_each_subsystem_safe(r, s, _s)
-               nvme_free_subsystem(s);
+       nvme_for_each_host_safe(r, h, _h)
+               nvme_free_host(h);
        nvme_scan_topology(r, NULL);
 }
 
 void nvme_free_tree(nvme_root_t r)
 {
-       struct nvme_subsystem *s, *_s;
+       struct nvme_host *h, *_h;
 
-       nvme_for_each_subsystem_safe(r, s, _s)
-               nvme_free_subsystem(s);
+       nvme_for_each_host_safe(r, h, _h)
+               nvme_free_host(h);
        free(r);
 }
 
@@ -218,6 +279,11 @@ nvme_ctrl_t nvme_subsystem_next_ctrl(nvme_subsystem_t s, nvme_ctrl_t c)
        return c ? list_next(&s->ctrls, c, entry) : NULL;
 }
 
+nvme_host_t nvme_subsystem_get_host(nvme_subsystem_t s)
+{
+       return s->h;
+}
+
 nvme_ns_t nvme_subsystem_first_ns(nvme_subsystem_t s)
 {
        return list_top(&s->namespaces, struct nvme_ns, entry);
@@ -255,6 +321,19 @@ static void nvme_free_subsystem(struct nvme_subsystem *s)
        free(s);
 }
 
+void nvme_free_host(struct nvme_host *h)
+{
+       struct nvme_subsystem *s, *_s;
+
+       list_del_init(&h->entry);
+       nvme_for_each_subsystem_safe(h, s, _s)
+               nvme_free_subsystem(s);
+       free(h->hostnqn);
+       if (h->hostid)
+               free(h->hostid);
+       free(h);
+}
+
 static int nvme_subsystem_scan_namespaces(struct nvme_subsystem *s)
 {
        struct dirent **namespaces;
@@ -287,7 +366,7 @@ static int nvme_subsystem_scan_ctrls(struct nvme_subsystem *s)
        return 0;
 }
 
-static int nvme_scan_subsystem(struct nvme_root *r, char *name,
+static int nvme_scan_subsystem(struct nvme_host *h, char *name,
                               nvme_scan_filter_t f)
 {
        struct nvme_subsystem *s;
@@ -304,16 +383,16 @@ static int nvme_scan_subsystem(struct nvme_root *r, char *name,
                goto free_path;
        }
 
-       s->r = r;
-       s->name = strdup(name);;
+       s->h = h;
+       s->name = strdup(name);
        s->sysfs_dir = path;
        s->subsysnqn = nvme_get_subsys_attr(s, "subsysnqn");
        list_head_init(&s->ctrls);
        list_head_init(&s->namespaces);
+       list_add(&h->subsystems, &s->entry);
 
        nvme_subsystem_scan_namespaces(s);
        nvme_subsystem_scan_ctrls(s);
-       list_add(&r->subsystems, &s->entry);
 
        if (f && !f(s)) {
                nvme_free_subsystem(s);
index a2842bc22cf1c5c1d22f523a8e2a1a2306f50964..b3c96ed8cc7e5be65c724cb0677f97e4e59bc582 100644 (file)
@@ -44,6 +44,11 @@ typedef struct nvme_ctrl *nvme_ctrl_t;
  */
 typedef struct nvme_subsystem *nvme_subsystem_t;
 
+/**
+ *
+ */
+typedef struct nvme_host *nvme_host_t;
+
 /**
  *
  */
@@ -55,21 +60,78 @@ typedef struct nvme_root *nvme_root_t;
 typedef bool (*nvme_scan_filter_t)(nvme_subsystem_t);
 
 /**
- * nvme_first_subsystem() -
+ * nvme_first_host() -
  * @r:
  *
  * Return: 
  */
-nvme_subsystem_t nvme_first_subsystem(nvme_root_t r);
+nvme_host_t nvme_first_host(nvme_root_t r);
 
 /**
- * nvme_next_subsystem() -
+ * nvme_next_host() -
  * @r:
+ * @h:
+ *
+ * Return: 
+ */
+nvme_host_t nvme_next_host(nvme_root_t r, nvme_host_t h);
+
+/**
+ * nvme_host_get_root() -
+ * @h:
+ *
+ * Return:
+ */
+nvme_root_t nvme_host_get_root(nvme_host_t h);
+
+/**
+ * nvme_host_get_hostnqn() -
+ * @h:
+ *
+ * Return: 
+ */
+const char *nvme_host_get_hostnqn(nvme_host_t h);
+
+/**
+ * nvme_host_get_hostid() -
+ * @h:
+ *
+ * Return: 
+ */
+const char *nvme_host_get_hostid(nvme_host_t h);
+
+/**
+ * nvme_default_host() -
+ * @r:
+ *
+ * Return:
+ */
+nvme_host_t nvme_default_host(nvme_root_t r);
+
+/**
+ * nvme_first_subsystem() -
+ * @h:
+ *
+ * Return: 
+ */
+nvme_subsystem_t nvme_first_subsystem(nvme_host_t h);
+
+/**
+ * nvme_next_subsystem() -
+ * @h:
  * @s:
  *
  * Return: 
  */
-nvme_subsystem_t nvme_next_subsystem(nvme_root_t r, nvme_subsystem_t s);
+nvme_subsystem_t nvme_next_subsystem(nvme_host_t h, nvme_subsystem_t s);
+
+/**
+ * nvme_subsystem_get_host() -
+ * @s:
+ *
+ * Return: 
+ */
+nvme_host_t nvme_subsystem_get_host(nvme_subsystem_t s);
 
 /**
  * nvme_ctrl_first_ns() -
@@ -139,21 +201,37 @@ nvme_ns_t nvme_subsystem_first_ns(nvme_subsystem_t s);
  */
 nvme_ns_t nvme_subsystem_next_ns(nvme_subsystem_t s, nvme_ns_t n);
 
+/**
+ * nvme_for_each_host_safe()
+ */
+#define nvme_for_each_host_safe(r, h, _h)              \
+       for (h = nvme_first_host(r),                    \
+            _h = nvme_next_host(r, h);                 \
+             h != NULL;                                \
+            h = _h, _h = nvme_next_host(r, h))
+
+/**
+ * nvme_for_each_host()
+ */
+#define nvme_for_each_host(r, h)                       \
+       for (h = nvme_first_host(r); h != NULL;         \
+            h = nvme_next_host(r, h))
+
 /**
  * nvme_for_each_subsystem_safe()
  */
-#define nvme_for_each_subsystem_safe(r, s, _s)                 \
-       for (s = nvme_first_subsystem(r),                       \
-             _s = nvme_next_subsystem(r, s);                   \
+#define nvme_for_each_subsystem_safe(h, s, _s)                 \
+       for (s = nvme_first_subsystem(h),                       \
+             _s = nvme_next_subsystem(h, s);                   \
              s != NULL;                                        \
-            s = _s, _s = nvme_next_subsystem(r, s))
+            s = _s, _s = nvme_next_subsystem(h, s))
 
 /**
  * nvme_for_each_subsystem()
  */
-#define nvme_for_each_subsystem(r, s)                          \
-       for (s = nvme_first_subsystem(r); s != NULL;            \
-               s = nvme_next_subsystem(r, s))
+#define nvme_for_each_subsystem(h, s)                          \
+       for (s = nvme_first_subsystem(h); s != NULL;            \
+               s = nvme_next_subsystem(h, s))
 
 /**
  * nvme_subsystem_for_each_ctrl_safe()
index eb48b78e3a3a7dd6f37703b49aef81ea54d918c0..f1463b46f595029c467416117de413a206396371 100644 (file)
@@ -12,6 +12,7 @@
 int main()
 {
        nvme_root_t r;
+       nvme_host_t h;
        nvme_subsystem_t s;
        nvme_ctrl_t c;
        nvme_path_t p;
@@ -21,25 +22,41 @@ int main()
        if (!r)
                return -1;
 
-       nvme_for_each_subsystem(r, s) {
-               std::cout <<  nvme_subsystem_get_name(s) << " - NQN=" << nvme_subsystem_get_nqn(s) << "\n";
-               nvme_subsystem_for_each_ctrl(s, c) {
-                       std::cout << " `- " << nvme_ctrl_get_name(c) << " " <<
-                               nvme_ctrl_get_transport(c) << " " <<
-                               nvme_ctrl_get_address(c) << " " <<
-                               nvme_ctrl_get_state(c) << "\n";
-                       nvme_ctrl_for_each_ns(c, n)
-                               std::cout << "   `- " << nvme_ns_get_name(n) <<
-                                       "lba size:" << nvme_ns_get_lba_size(n) << " lba max:" <<
-                                       nvme_ns_get_lba_count(n) << "\n";
-                       nvme_ctrl_for_each_path(c, p)
-                               std::cout << "   `- " << nvme_path_get_name(p) << " " <<
-                                       nvme_path_get_ana_state(p) << "\n";
-               }
-               nvme_subsystem_for_each_ns(s, n) {
-                       std::cout << "   `- " << nvme_ns_get_name(n) <<
-                               "lba size:" << nvme_ns_get_lba_size(n) << " lba max:" <<
-                                       nvme_ns_get_lba_count(n) << "\n";
+       nvme_for_each_host(r, h) {
+               nvme_for_each_subsystem(h, s) {
+                       std::cout <<  nvme_subsystem_get_name(s)
+                                 << " - NQN=" << nvme_subsystem_get_nqn(s)
+                                 << "\n";
+                       nvme_subsystem_for_each_ctrl(s, c) {
+                               std::cout << " `- " << nvme_ctrl_get_name(c)
+                                         << " " << nvme_ctrl_get_transport(c)
+                                         << " " << nvme_ctrl_get_address(c)
+                                         << " " << nvme_ctrl_get_state(c)
+                                         << "\n";
+                               nvme_ctrl_for_each_ns(c, n) {
+                                       std::cout << "   `- "
+                                                 << nvme_ns_get_name(n)
+                                                 << "lba size:"
+                                                 << nvme_ns_get_lba_size(n)
+                                                 << " lba max:"
+                                                 << nvme_ns_get_lba_count(n)
+                                                 << "\n";
+                               }
+                               nvme_ctrl_for_each_path(c, p) {
+                                       std::cout << "   `- "
+                                                 << nvme_path_get_name(p)
+                                                 << " "
+                                                 << nvme_path_get_ana_state(p)
+                                                 << "\n";
+                               }
+                       }
+                       nvme_subsystem_for_each_ns(s, n) {
+                               std::cout << "   `- " << nvme_ns_get_name(n)
+                                         << "lba size:"
+                                         << nvme_ns_get_lba_size(n)
+                                         << " lba max:"
+                                         << nvme_ns_get_lba_count(n) << "\n";
+                       }
                }
        }
        std::cout << "\n";
index 0bc96b0a941912152e3b9673333a0f0741f1b6b9..e8ee0403a60fde3615d6254cd7f7cbbde56f2d5f 100644 (file)
@@ -303,6 +303,7 @@ static void print_hex(const uint8_t *x, int len)
 int main()
 {
        nvme_root_t r;
+       nvme_host_t h;
        nvme_subsystem_t s;
        nvme_ctrl_t c;
        nvme_path_t p;
@@ -311,14 +312,16 @@ int main()
        printf("Test filter for common loop back target\n");
        nqn_match = "testnqn";
        r = nvme_scan_filter(nvme_match_subsysnqn_filter);
-       nvme_for_each_subsystem(r, s) {
-               printf("%s - NQN=%s\n", nvme_subsystem_get_name(s),
-                       nvme_subsystem_get_nqn(s));
-               nvme_subsystem_for_each_ctrl(s, c) {
-                       printf("  %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_for_each_host(r, h) {
+               nvme_for_each_subsystem(h, s) {
+                       printf("%s - NQN=%s\n", nvme_subsystem_get_name(s),
+                              nvme_subsystem_get_nqn(s));
+                       nvme_subsystem_for_each_ctrl(s, c) {
+                               printf("  %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));
+                       }
                }
        }
        printf("\n");
@@ -340,61 +343,70 @@ int main()
                return -1;
 
        printf("Test walking the topology\n");
-       nvme_for_each_subsystem(r, s) {
-               printf("%s - NQN=%s\n", nvme_subsystem_get_name(s),
-                       nvme_subsystem_get_nqn(s));
-               nvme_subsystem_for_each_ctrl(s, c) {
-                       printf(" `- %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_for_each_host(r, h) {
+               nvme_for_each_subsystem(h, s) {
+                       printf("%s - NQN=%s\n", nvme_subsystem_get_name(s),
+                              nvme_subsystem_get_nqn(s));
+                       nvme_subsystem_for_each_ctrl(s, c) {
+                               printf(" `- %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_ctrl_for_each_ns(c, n) {
+                               nvme_ctrl_for_each_ns(c, n) {
 #ifdef CONFIG_LIBUUID
-                               char uuid_str[40];
-                               uuid_t uuid;
+                                       char uuid_str[40];
+                                       uuid_t uuid;
 #endif
-                               printf("   `- %s lba size:%d lba max:%lu\n",
-                                       nvme_ns_get_name(n), nvme_ns_get_lba_size(n),
-                                       nvme_ns_get_lba_count(n));
-                               printf("      eui:");
-                               print_hex(nvme_ns_get_eui64(n), 8);
-                               printf(" nguid:");
-                               print_hex(nvme_ns_get_nguid(n), 16);
+                                       printf("   `- %s lba size:%d lba max:%lu\n",
+                                              nvme_ns_get_name(n),
+                                              nvme_ns_get_lba_size(n),
+                                              nvme_ns_get_lba_count(n));
+                                       printf("      eui:");
+                                       print_hex(nvme_ns_get_eui64(n), 8);
+                                       printf(" nguid:");
+                                       print_hex(nvme_ns_get_nguid(n), 16);
 #ifdef CONFIG_LIBUUID
-                               nvme_ns_get_uuid(n, uuid);
-                               uuid_unparse_lower(uuid, uuid_str);
-                               printf(" uuid:%s csi:%d\n", uuid_str, nvme_ns_get_csi(n));
+                                       nvme_ns_get_uuid(n, uuid);
+                                       uuid_unparse_lower(uuid, uuid_str);
+                                       printf(" uuid:%s csi:%d\n", uuid_str,
+                                              nvme_ns_get_csi(n));
 #endif
-                       }
+                               }
 
-                       nvme_ctrl_for_each_path(c, p)
-                               printf("   `- %s %s\n", nvme_path_get_name(p),
-                                       nvme_path_get_ana_state(p));
-               }
+                               nvme_ctrl_for_each_path(c, p)
+                                       printf("   `- %s %s\n",
+                                              nvme_path_get_name(p),
+                                              nvme_path_get_ana_state(p));
+                       }
 
-               nvme_subsystem_for_each_ns(s, n) {
-                       printf(" `- %s lba size:%d lba max:%lu\n",
-                               nvme_ns_get_name(n), nvme_ns_get_lba_size(n),
-                               nvme_ns_get_lba_count(n));
+                       nvme_subsystem_for_each_ns(s, n) {
+                               printf(" `- %s lba size:%d lba max:%lu\n",
+                                      nvme_ns_get_name(n),
+                                      nvme_ns_get_lba_size(n),
+                                      nvme_ns_get_lba_count(n));
+                       }
                }
+               printf("\n");
        }
-       printf("\n");
 
        printf("Test identification, logs, and features\n");
-       nvme_for_each_subsystem(r, s) {
-               nvme_subsystem_for_each_ctrl(s, c) {
-                       test_ctrl(c);
-                       printf("\n");
-                       nvme_ctrl_for_each_ns(c, n) {
+       nvme_for_each_host(r, h) {
+               nvme_for_each_subsystem(h, s) {
+                       nvme_subsystem_for_each_ctrl(s, c) {
+                               test_ctrl(c);
+                               printf("\n");
+                               nvme_ctrl_for_each_ns(c, n) {
+                                       test_namespace(n);
+                                       printf("\n");
+                               }
+                       }
+                       nvme_subsystem_for_each_ns(s, n) {
                                test_namespace(n);
                                printf("\n");
                        }
                }
-               nvme_subsystem_for_each_ns(s, n) {
-                       test_namespace(n);
-                       printf("\n");
-               }
        }
        nvme_free_tree(r);
 
index e54c7abaf802c0cd9fdd48e6ad0b1d54a1f5646b..7d854cd7457d6760fce53b8f5a51defb91196ea8 100644 (file)
@@ -57,6 +57,7 @@ int main()
 {
        nvme_subsystem_t s;
        nvme_root_t r;
+       nvme_host_t h;
        nvme_ctrl_t c;
        nvme_ns_t n;
 
@@ -64,17 +65,19 @@ int main()
        if (!r)
                return -1;
 
-       nvme_for_each_subsystem(r, s) {
-               nvme_subsystem_for_each_ctrl(s, c) {
-                       nvme_ctrl_for_each_ns(c, n) {
+       nvme_for_each_host(r, h) {
+               nvme_for_each_subsystem(h, s) {
+                       nvme_subsystem_for_each_ctrl(s, c) {
+                               nvme_ctrl_for_each_ns(c, n) {
+                                       if (nvme_ns_get_csi(n) == NVME_CSI_ZNS)
+                                               show_zns_properties(n);
+                               }
+                       }
+                       nvme_subsystem_for_each_ns(s, n) {
                                if (nvme_ns_get_csi(n) == NVME_CSI_ZNS)
                                        show_zns_properties(n);
                        }
                }
-               nvme_subsystem_for_each_ns(s, n) {
-                       if (nvme_ns_get_csi(n) == NVME_CSI_ZNS)
-                               show_zns_properties(n);
-               }
        }
        nvme_free_tree(r);
 }