From: Hannes Reinecke Date: Mon, 21 Jun 2021 15:09:27 +0000 (+0200) Subject: tree: check hostnqn and hostid when scanning topology X-Git-Tag: v1.0-rc0~124^2 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=7254cdd67def84200567e93b6343ddc9dd5f10bd;p=users%2Fsagi%2Flibnvme.git tree: check hostnqn and hostid when scanning topology Newer kernel versions will display the hostnqn and hostid in sysfs attributes, so we should be using them to find the host structure instead of the default host. Signed-off-by: Hannes Reinecke --- diff --git a/src/nvme/tree.c b/src/nvme/tree.c index 56cb7333..f964772d 100644 --- a/src/nvme/tree.c +++ b/src/nvme/tree.c @@ -33,7 +33,7 @@ static struct nvme_host *default_host; static int nvme_subsystem_scan_namespace(struct nvme_subsystem *s, char *name); -static int nvme_scan_subsystem(struct nvme_host *h, char *name, +static int nvme_scan_subsystem(struct nvme_root *r, char *name, nvme_scan_filter_t f); static int nvme_subsystem_scan_ctrl(struct nvme_subsystem *s, char *name); static int nvme_ctrl_scan_namespace(struct nvme_ctrl *c, char *name); @@ -66,22 +66,15 @@ nvme_host_t nvme_default_host(nvme_root_t r) static int nvme_scan_topology(struct nvme_root *r, nvme_scan_filter_t f) { - struct nvme_host *h; struct dirent **subsys; int i, ret; - h = nvme_default_host(r); - if (!h) { - free(r); - errno = ENOMEM; - return -1; - } ret = nvme_scan_subsystems(&subsys); if (ret < 0) return ret; for (i = 0; i < ret; i++) - nvme_scan_subsystem(h, subsys[i]->d_name, f); + nvme_scan_subsystem(r, subsys[i]->d_name, f); nvme_free_dirents(subsys, i); return 0; @@ -384,17 +377,33 @@ static int nvme_init_subsystem(nvme_subsystem_t s, const char *name, return 0; } -static int nvme_scan_subsystem(struct nvme_host *h, char *name, +static int nvme_scan_subsystem(struct nvme_root *r, char *name, nvme_scan_filter_t f) { struct nvme_subsystem *s; char *path, *subsysnqn; + char *hostnqn, *hostid = NULL; + nvme_host_t h = NULL; int ret; ret = asprintf(&path, "%s/%s", nvme_subsys_sysfs_dir, name); if (ret < 0) return ret; + hostnqn = nvme_get_attr(path, "hostnqn"); + if (hostnqn) { + hostid = nvme_get_attr(path, "hostid"); + h = nvme_lookup_host(r, hostnqn, hostid); + free(hostnqn); + if (hostid) + free(hostid); + } + if (!h) + h = nvme_default_host(r); + if (!h) { + errno = ENOMEM; + return -1; + } subsysnqn = nvme_get_attr(path, "subsysnqn"); if (!subsysnqn) { errno = ENODEV;