From mboxrd@z Thu Jan 1 00:00:00 1970 From: jsmart2021@gmail.com (James Smart) Date: Fri, 19 Jul 2019 15:52:58 -0700 Subject: [PATCH v2 03/10] nvme-cli: allow discover to address discovery controller by persistent name In-Reply-To: <20190719225305.11397-1-jsmart2021@gmail.com> References: <20190719225305.11397-1-jsmart2021@gmail.com> Message-ID: <20190719225305.11397-4-jsmart2021@gmail.com> To support discovery (connect/connect-all) to operate against a persistent discovery controller, let the discovery controller to be specified by its device node name rather than new connection attributes. Example: nvme connect-all ... --device=nvme5 Also centralize extraction of controller instance from the controller name to a common helper. Signed-off-by: James Smart Reviewed-by: Max Gurtovoy Reviewed-by: Minwoo Im Reviewed-by: Hannes Reinecke Reviewed-by: Sagi Grimberg --- fabrics.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/fabrics.c b/fabrics.c index 75dedf8..d92c2ff 100644 --- a/fabrics.c +++ b/fabrics.c @@ -190,6 +190,21 @@ static const char *cms_str(__u8 cm) static int do_discover(char *argstr, bool connect); +static int ctrl_instance(char *device) +{ + char d[64]; + int ret, instance; + + device = basename(device); + ret = sscanf(device, "nvme%d", &instance); + if (ret <= 0) + return -EINVAL; + if (snprintf(d, sizeof(d), "nvme%d", instance) <= 0 || + strcmp(device, d)) + return -EINVAL; + return instance; +} + static int add_ctrl(const char *argstr) { substring_t args[MAX_OPT_ARGS]; @@ -865,7 +880,10 @@ static int do_discover(char *argstr, bool connect) int instance, numrec = 0, ret, err; int status = 0; - instance = add_ctrl(argstr); + if (!cfg.device) + instance = add_ctrl(argstr); + else + instance = ctrl_instance(cfg.device); if (instance < 0) return instance; @@ -873,7 +891,7 @@ static int do_discover(char *argstr, bool connect) return -errno; ret = nvmf_get_log_page_discovery(dev_name, &log, &numrec, &status); free(dev_name); - if (!cfg.persistent) { + if (!cfg.device && !cfg.persistent) { err = remove_ctrl(instance); if (err) return err; @@ -996,6 +1014,7 @@ int discover(const char *desc, int argc, char **argv, bool connect) {"hostnqn", 'q', "LIST", CFG_STRING, &cfg.hostnqn, required_argument, "user-defined hostnqn (if default not used)" }, {"hostid", 'I', "LIST", CFG_STRING, &cfg.hostid, required_argument, "user-defined hostid (if default not used)"}, {"raw", 'r', "LIST", CFG_STRING, &cfg.raw, required_argument, "raw output file" }, + {"device", 'd', "LIST", CFG_STRING, &cfg.device, required_argument, "use existing discovery controller device" }, {"keep-alive-tmo", 'k', "LIST", CFG_INT, &cfg.keep_alive_tmo, required_argument, "keep alive timeout period in seconds" }, {"reconnect-delay", 'c', "LIST", CFG_INT, &cfg.reconnect_delay, required_argument, "reconnect timeout period in seconds" }, {"ctrl-loss-tmo", 'l', "LIST", CFG_INT, &cfg.ctrl_loss_tmo, required_argument, "controller loss timeout period in seconds" }, @@ -1014,6 +1033,9 @@ int discover(const char *desc, int argc, char **argv, bool connect) if (ret) goto out; + if (cfg.device && !strcmp(cfg.device, "none")) + cfg.device = NULL; + cfg.nqn = NVME_DISC_SUBSYS_NAME; if (!cfg.transport && !cfg.traddr) { @@ -1157,15 +1179,10 @@ static int disconnect_by_nqn(char *nqn) static int disconnect_by_device(char *device) { int instance; - int ret; - - device = basename(device); - ret = sscanf(device, "nvme%d", &instance); - if (ret < 0) - return ret; - if (!ret) - return -1; + instance = ctrl_instance(device); + if (instance < 0) + return instance; return remove_ctrl(instance); } -- 2.13.7