From 035314a35f3a95f36f3f34a4d7928a3bd31a871e Mon Sep 17 00:00:00 2001 From: James Smart Date: Thu, 1 Aug 2019 16:13:45 -0700 Subject: [PATCH] nvme-cli: Add routine to search for controller with specific attributes 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 Reviewed-by: Sagi Grimberg Reviewed-by: Hannes Reinecke --- nvme.c | 37 +++++++++++++++++++++++++++++++++++++ nvme.h | 1 + 2 files changed, 38 insertions(+) diff --git a/nvme.c b/nvme.c index 27d536a8..eb106c06 100644 --- 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 2fecb68b..b91a22cf 100644 --- 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; -- 2.50.1