Skip to content

Commit 1fe56b7

Browse files
committed
tree: rework nvme_scan_subsystem()
The nvme subsystem does not have a 'hostnqn' sysfs entry, so we cannot infer from the nvme subsystem sysfs entry to which host it relates. And really, the subsystem should already have been created by the previous call to nvme_scan_ctrl(). So do not call nvme_lookup_subsystem() in nvme_scan_subystem(), but rather just validate the sysfs subsystem entries and create any missing subsystems. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent 3849139 commit 1fe56b7

1 file changed

Lines changed: 36 additions & 29 deletions

File tree

src/nvme/tree.c

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,8 @@ static int nvme_init_subsystem(nvme_subsystem_t s, const char *name)
516516
static int nvme_scan_subsystem(struct nvme_root *r, const char *name,
517517
nvme_scan_filter_t f)
518518
{
519-
struct nvme_subsystem *s;
519+
struct nvme_subsystem *s = NULL, *_s;
520520
char *path, *subsysnqn;
521-
char *hostnqn, *hostid = NULL;
522521
nvme_host_t h = NULL;
523522
int ret;
524523

@@ -527,42 +526,50 @@ static int nvme_scan_subsystem(struct nvme_root *r, const char *name,
527526
if (ret < 0)
528527
return ret;
529528

530-
hostnqn = nvme_get_attr(path, "hostnqn");
531-
if (hostnqn) {
532-
hostid = nvme_get_attr(path, "hostid");
533-
h = nvme_lookup_host(r, hostnqn, hostid);
534-
free(hostnqn);
535-
if (hostid)
536-
free(hostid);
537-
if (h) {
538-
if (h->dhchap_key)
539-
free(h->dhchap_key);
540-
h->dhchap_key = nvme_get_attr(path, "dhchap_secret");
541-
if (h->dhchap_key && !strcmp(h->dhchap_key, "none")) {
542-
free(h->dhchap_key);
543-
h->dhchap_key = NULL;
544-
}
545-
}
546-
}
547-
if (!h)
548-
h = nvme_default_host(r);
549-
if (!h) {
550-
free(path);
551-
errno = ENOMEM;
552-
return -1;
553-
}
554529
subsysnqn = nvme_get_attr(path, "subsysnqn");
555530
free(path);
556531
if (!subsysnqn) {
557532
errno = ENODEV;
558533
return -1;
559534
}
560-
s = nvme_lookup_subsystem(h, name, subsysnqn);
561-
free(subsysnqn);
535+
nvme_for_each_host(r, h) {
536+
nvme_for_each_subsystem(h, _s) {
537+
/*
538+
* We are always called after nvme_scan_ctrl(),
539+
* so any subsystem we're interested at _must_
540+
* have a name.
541+
*/
542+
if (!_s->name)
543+
continue;
544+
if (strcmp(_s->name, name))
545+
continue;
546+
s = _s;
547+
}
548+
}
562549
if (!s) {
563-
errno = ENOMEM;
550+
/*
551+
* Subsystem with non-matching controller. odd.
552+
* Create a subsystem with the default host
553+
* and hope for the best.
554+
*/
555+
nvme_msg(r, LOG_DEBUG, "creating detached subsystem '%s'\n",
556+
name);
557+
h = nvme_default_host(r);
558+
s = nvme_alloc_subsystem(h, name, subsysnqn);
559+
if (!s) {
560+
errno = ENOMEM;
561+
}
562+
} else if (strcmp(s->subsysnqn, subsysnqn)) {
563+
nvme_msg(r, LOG_WARNING, "NQN mismatch for subsystem '%s'\n",
564+
name);
565+
s = NULL;
566+
errno = EINVAL;
564567
return -1;
565568
}
569+
free(subsysnqn);
570+
if (!s)
571+
return -1;
572+
566573
nvme_subsystem_scan_namespaces(r, s);
567574

568575
if (f && !f(s)) {

0 commit comments

Comments
 (0)