]> www.infradead.org Git - users/hch/nvme-cli.git/commitdiff
nvme-cli: Add routine to search for controller with specific attributes
authorJames Smart <jsmart2021@gmail.com>
Thu, 1 Aug 2019 23:13:45 +0000 (16:13 -0700)
committerKeith Busch <kbusch@kernel.org>
Fri, 2 Aug 2019 15:43:06 +0000 (09:43 -0600)
In preparation for searching controllers to match with connect args:

Create a new routine find_ctrl_with_connectargs() that will search the
controllers that exist in the system to find one that has attributes
that match the connect arguments specified.  If found, the routine
returns the controller name ("nvme?"). If not found, a NULL is returned.

Routine is defined as a global as a subsequent patch will use it
from the fabrics routines.

Signed-off-by: James Smart <jsmart2021@gmail.com>
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
Reviewed-by: Hannes Reinecke <hare@suse.com>
nvme.c
nvme.h

diff --git a/nvme.c b/nvme.c
index 27d536a8e0867c2c78ac8ed0befe5967ed02ef8f..eb106c06845b718b78ec131ee72804fb77b9c0cf 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -2048,6 +2048,43 @@ cleanup_exit:
        return found;
 }
 
+/*
+ * Look through the system to find an existing controller whose
+ * attributes match the connect arguments specified
+ * If found, a string containing the controller name (ex: "nvme?")
+ * is returned.
+ * If not found, a NULL is returned.
+ */
+char *find_ctrl_with_connectargs(struct connect_args *args)
+{
+       struct dirent **devices;
+       char *devname = NULL;
+       int i, n;
+
+       n = scandir(SYS_NVME, &devices, scan_ctrls_filter, alphasort);
+       if (n < 0) {
+               fprintf(stderr, "no NVMe controller(s) detected.\n");
+               return NULL;
+       }
+
+       for (i = 0; i < n; i++) {
+               if (ctrl_matches_connectargs(devices[i]->d_name, args)) {
+                       devname = strdup(devices[i]->d_name);
+                       if (devname == NULL)
+                               fprintf(stderr, "no memory for ctrl name %s\n",
+                                               devices[i]->d_name);
+                       goto cleanup_devices;
+               }
+       }
+
+cleanup_devices:
+       for (i = 0; i < n; i++)
+               free(devices[i]);
+       free(devices);
+
+       return devname;
+}
+
 int __id_ctrl(int argc, char **argv, struct command *cmd, struct plugin *plugin, void (*vs)(__u8 *vs, struct json_object *root))
 {
        const char *desc = "Send an Identify Controller command to "\
diff --git a/nvme.h b/nvme.h
index 2fecb68b1745195e3e088659267b8c98aca21289..b91a22cf89b545dc14e80477f5c96bf76ffe4c1a 100644 (file)
--- a/nvme.h
+++ b/nvme.h
@@ -190,6 +190,7 @@ struct connect_args {
 #define SYS_NVME               "/sys/class/nvme"
 
 bool ctrl_matches_connectargs(char *name, struct connect_args *args);
+char *find_ctrl_with_connectargs(struct connect_args *args);
 char *__parse_connect_arg(char *conargs, const char delim, const char *fieldnm);
 
 extern const char *conarg_traddr;