]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
print topology for NVMe nodes in kernel and path
authorTomasz Zawadzki <tomasz.zawadzki@intel.com>
Mon, 13 Jul 2020 14:25:17 +0000 (10:25 -0400)
committerKeith Busch <kbusch@kernel.org>
Thu, 14 Jan 2021 21:50:08 +0000 (14:50 -0700)
This patch enables listing kernel devices (under /dev/ or sysfs)
and the ones available under passed path in a single call.

The additional path parameter is optional and first kernel
device topology is checked. After that legacy_list is called
on that and topologies are joint together.

Signed-off-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
nvme-topology.c
nvme.c

index 0fd3d13b76a917590a212b5c079009b267eef42b..71371c5fff263413d520d75b4e52f26e7d141bd4 100644 (file)
@@ -529,38 +529,64 @@ static void free_subsystem(struct nvme_subsystem *s)
        free(s->namespaces);
 }
 
+static int scan_subsystem_dir(struct nvme_topology *t, char *dev_dir)
+{
+       struct nvme_topology dev_dir_t = { };
+       int ret, i, total_nr_subsystems;
+
+       ret = legacy_list(&dev_dir_t, dev_dir);
+       if (ret != 0)
+               return ret;
+
+       total_nr_subsystems = t->nr_subsystems + dev_dir_t.nr_subsystems;
+       t->subsystems = realloc(t->subsystems,
+                               total_nr_subsystems * sizeof(struct nvme_subsystem));
+       for (i = 0; i < dev_dir_t.nr_subsystems; i++){
+               t->subsystems[i+t->nr_subsystems] = dev_dir_t.subsystems[i];
+       }
+       t->nr_subsystems = total_nr_subsystems;
+
+       return 0;
+}
+
 int scan_subsystems(struct nvme_topology *t, const char *subsysnqn,
                    __u32 ns_instance, char *dev_dir)
 {
        struct nvme_subsystem *s;
        struct dirent **subsys;
-       int i, j = 0;
-
-       if (dev_dir != NULL)
-               return legacy_list(t, dev_dir);
+       int ret, i, j = 0;
 
        t->nr_subsystems = scandir(subsys_dir, &subsys, scan_subsys_filter,
                                   alphasort);
-       if (t->nr_subsystems < 0)
-               return legacy_list(t, (char *)dev);
+       if (t->nr_subsystems < 0) {
+               ret = legacy_list(t, (char *)dev);
+               if (ret != 0)
+                       return ret;
+       } else {
 
-       t->subsystems = calloc(t->nr_subsystems, sizeof(*s));
-       for (i = 0; i < t->nr_subsystems; i++) {
-               s = &t->subsystems[j];
-               s->name = strdup(subsys[i]->d_name);
-               scan_subsystem(s, ns_instance);
-
-               if (!subsysnqn || !strcmp(s->subsysnqn, subsysnqn))
-                       j++;
-               else
-                       free_subsystem(s);
+               t->subsystems = calloc(t->nr_subsystems, sizeof(*s));
+               for (i = 0; i < t->nr_subsystems; i++) {
+                       s = &t->subsystems[j];
+                       s->name = strdup(subsys[i]->d_name);
+                       scan_subsystem(s, ns_instance);
+
+                       if (!subsysnqn || !strcmp(s->subsysnqn, subsysnqn))
+                               j++;
+                       else
+                               free_subsystem(s);
+               }
+               t->nr_subsystems = j;
+
+               while (i--)
+                       free(subsys[i]);
+               free(subsys);
        }
-       t->nr_subsystems = j;
 
-       while (i--)
-               free(subsys[i]);
-       free(subsys);
-       return 0;
+       if (dev_dir != NULL && strcmp(dev_dir, "/dev/")) {
+               ret = scan_subsystem_dir(t, dev_dir);
+       }
+
+       return ret;
 }
 
 void free_topology(struct nvme_topology *t)
diff --git a/nvme.c b/nvme.c
index d84b6774f3db0afe4aa48c7b9fa9dc9220dc6e38..d1ef6e944acfca3c016cb13241b4ce801bd7376b 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -1606,7 +1606,7 @@ ret:
 static int list(int argc, char **argv, struct command *cmd, struct plugin *plugin)
 {
        const char *desc = "Retrieve basic information for all NVMe namespaces";
-       const char *device_dir = "Directory to search for devices";
+       const char *device_dir = "Additional directory to search for devices";
        const char *verbose = "Increase output verbosity";
        struct nvme_topology t = { };
        enum nvme_print_flags flags;