]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
fabrics,tree: Fixes for PCIe devices
authorHannes Reinecke <hare@suse.de>
Wed, 16 Jun 2021 15:49:54 +0000 (17:49 +0200)
committerHannes Reinecke <hare@suse.de>
Wed, 16 Jun 2021 16:02:37 +0000 (18:02 +0200)
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 <hare@suse.de>
src/nvme/fabrics.c
src/nvme/tree.c
src/nvme/util.c

index 9a95f32811b2c40d7f5694793a7d02fe8aa0c906..b59b2efade7d77690323b251d0973f54c6506898 100644 (file)
@@ -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"));
 }
index 887c6444b6c7b53c27a1dbeb42e0bbcc1151c5ef..ceda8198f7b70ba16d815eb67d5ff1b8b825a52a 100644 (file)
@@ -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) {
index 761ae81cf8999698f9fa68f863b291e9106f851c..fb0d284e907315857077d5feb8d00cc9cfc96220 100644 (file)
@@ -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)