From: Hannes Reinecke Date: Wed, 16 Jun 2021 15:49:54 +0000 (+0200) Subject: fabrics,tree: Fixes for PCIe devices X-Git-Tag: v1.0-rc0~126^2 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=1e761cda67c35b09e43aa2895a8ac7edfabf994e;p=users%2Fsagi%2Flibnvme.git fabrics,tree: Fixes for PCIe devices PCIe devices do not necessarily have a hostnqn set, so use the default hostnqn when looking up hosts. And for PCIe devices the transport address is the entire 'address' string, no parsing required. Signed-off-by: Hannes Reinecke --- diff --git a/src/nvme/fabrics.c b/src/nvme/fabrics.c index 9a95f328..b59b2efa 100644 --- a/src/nvme/fabrics.c +++ b/src/nvme/fabrics.c @@ -696,13 +696,13 @@ static char *nvmf_read_file(const char *f, int len) fd = open(f, O_RDONLY); if (fd < 0) - return false; + return NULL; memset(buf, 0, len); ret = read(fd, buf, len - 1); close (fd); - if (ret < 0) + if (ret < 0 || !strlen(buf)) return NULL; return strndup(buf, strcspn(buf, "\n")); } diff --git a/src/nvme/tree.c b/src/nvme/tree.c index 887c6444..ceda8198 100644 --- a/src/nvme/tree.c +++ b/src/nvme/tree.c @@ -155,12 +155,15 @@ nvme_host_t nvme_default_host(nvme_root_t r) char *hostnqn, *hostid; hostnqn = nvmf_hostnqn_from_file(); + if (!hostnqn) + hostnqn = nvmf_hostnqn_generate(); hostid = nvmf_hostid_from_file(); h = nvme_lookup_host(r, hostnqn, hostid); default_host = h; free(hostnqn); - free(hostid); + if (hostid) + free(hostid); return h; } @@ -402,6 +405,8 @@ struct nvme_host *nvme_lookup_host(nvme_root_t r, const char *hostnqn, { struct nvme_host *h; + if (!hostnqn) + return NULL; nvme_for_each_host(r, h) { if (strcmp(h->hostnqn, hostnqn)) continue; @@ -1147,19 +1152,23 @@ static nvme_ctrl_t nvme_ctrl_alloc(nvme_subsystem_t s, const char *path, /* Parse 'address' string into components */ addr = nvme_get_attr(path, "address"); address = strdup(addr); - a = strtok_r(addr, ",", &e); - while (a && strlen(a)) { - if (!strncmp(a, "traddr=", 7)) - traddr = a + 7; - else if (!strncmp(a, "trsvcid=", 8)) - trsvcid = a + 8; - else if (!strncmp(a, "host_traddr=", 12)) - host_traddr = a + 12; - else if (!strncmp(a, "host_iface=", 11)) - host_iface = a + 12; - a = strtok_r(NULL, ",", &e); + if (!strcmp(transport, "pcie")) { + /* The 'address' string is the transport address */ + traddr = address; + } else { + a = strtok_r(addr, ",", &e); + while (a && strlen(a)) { + if (!strncmp(a, "traddr=", 7)) + traddr = a + 7; + else if (!strncmp(a, "trsvcid=", 8)) + trsvcid = a + 8; + else if (!strncmp(a, "host_traddr=", 12)) + host_traddr = a + 12; + else if (!strncmp(a, "host_iface=", 11)) + host_iface = a + 12; + a = strtok_r(NULL, ",", &e); + } } - c = nvme_lookup_ctrl(s, transport, traddr, host_traddr, host_iface, trsvcid); free(addr); @@ -1188,14 +1197,10 @@ nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name) } hostnqn = nvme_get_attr(path, "hostnqn"); - if (!hostnqn) { - free(path); - errno = ENODEV; - return NULL; - } hostid = nvme_get_attr(path, "hostid"); h = nvme_lookup_host(r, hostnqn, hostid); - free(hostnqn); + if (hostnqn) + free(hostnqn); if (hostid) free(hostid); if (!h) { diff --git a/src/nvme/util.c b/src/nvme/util.c index 761ae81c..fb0d284e 100644 --- a/src/nvme/util.c +++ b/src/nvme/util.c @@ -564,7 +564,7 @@ static int __nvme_set_attr(const char *path, const char *value) fd = open(path, O_WRONLY); if (fd < 0) { - nvme_msg(LOG_ERR, "Failed to open %s: %s\n", path, + nvme_msg(LOG_DEBUG, "Failed to open %s: %s\n", path, strerror(errno)); return -1; } @@ -594,14 +594,14 @@ static char *__nvme_get_attr(const char *path) fd = open(path, O_RDONLY); if (fd < 0) { - nvme_msg(LOG_ERR, "Failed to open %s: %s\n", path, + nvme_msg(LOG_DEBUG, "Failed to open %s: %s\n", path, strerror(errno)); return NULL; } ret = read(fd, value, sizeof(value) - 1); - if (ret < 0) { - close(fd); + close(fd); + if (ret < 0 || !strlen(value)) { return NULL; } @@ -610,8 +610,7 @@ static char *__nvme_get_attr(const char *path) while (strlen(value) > 0 && value[strlen(value) - 1] == ' ') value[strlen(value) - 1] = '\0'; - close(fd); - return strdup(value); + return strlen(value) ? strdup(value) : NULL; } char *nvme_get_attr(const char *dir, const char *attr)