From mboxrd@z Thu Jan 1 00:00:00 1970 From: jsmart2021@gmail.com (James Smart) Date: Thu, 11 Jul 2019 17:31:37 -0700 Subject: [PATCH rfc v2 07/10] nvme-cli: Add routine to search for controller with specific attributes In-Reply-To: <20190712003140.16221-1-jsmart2021@gmail.com> References: <20190712003140.16221-1-jsmart2021@gmail.com> Message-ID: <20190712003140.16221-8-jsmart2021@gmail.com> 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 CC: Sagi Grimberg CC: Hannes Reinecke --- nvme.c | 37 +++++++++++++++++++++++++++++++++++++ nvme.h | 1 + 2 files changed, 38 insertions(+) diff --git a/nvme.c b/nvme.c index ef3c61c..314a04f 100644 --- a/nvme.c +++ b/nvme.c @@ -2050,6 +2050,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(nvme_ctrl_dir, &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 ctlr 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 537b8dd..04f8d46 100644 --- a/nvme.h +++ b/nvme.h @@ -188,6 +188,7 @@ struct connect_args { }; 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; -- 2.13.7