Skip to content

Commit 267dbc2

Browse files
committed
tree: move nvme_init_subsystem() into nvme_lookup_subsystem()
We're always calling nvme_init_subsystem() when nvme_lookup_subsystem() is called with a non-NULL 'name' parameter. So we might as well move it into nvme_lookup_subsystem() and simplify the callers. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent 851f186 commit 267dbc2

1 file changed

Lines changed: 25 additions & 30 deletions

File tree

src/nvme/tree.c

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static void __nvme_free_host(nvme_host_t h);
4040
static void __nvme_free_ctrl(nvme_ctrl_t c);
4141
static int nvme_subsystem_scan_namespace(nvme_root_t r,
4242
struct nvme_subsystem *s, char *name);
43+
static int nvme_init_subsystem(nvme_subsystem_t s, const char *name);
4344
static int nvme_scan_subsystem(nvme_root_t r, const char *name,
4445
nvme_scan_filter_t f);
4546
static int nvme_ctrl_scan_namespace(nvme_root_t r, struct nvme_ctrl *c,
@@ -370,26 +371,20 @@ void nvme_free_subsystem(nvme_subsystem_t s)
370371
{
371372
}
372373

373-
struct nvme_subsystem *nvme_lookup_subsystem(struct nvme_host *h,
374-
const char *name,
375-
const char *subsysnqn)
374+
struct nvme_subsystem *nvme_alloc_subsystem(struct nvme_host *h,
375+
const char *name,
376+
const char *subsysnqn)
376377
{
377378
struct nvme_subsystem *s;
378379

379-
nvme_for_each_subsystem(h, s) {
380-
if (strcmp(s->subsysnqn, subsysnqn))
381-
continue;
382-
if (name && s->name &&
383-
strcmp(s->name, name))
384-
continue;
385-
return s;
386-
}
387380
s = calloc(1, sizeof(*s));
388381
if (!s)
389382
return NULL;
390383

391384
s->h = h;
392385
s->subsysnqn = strdup(subsysnqn);
386+
if (name)
387+
nvme_init_subsystem(s, name);
393388
list_head_init(&s->ctrls);
394389
list_head_init(&s->namespaces);
395390
list_node_init(&s->entry);
@@ -398,6 +393,23 @@ struct nvme_subsystem *nvme_lookup_subsystem(struct nvme_host *h,
398393
return s;
399394
}
400395

396+
struct nvme_subsystem *nvme_lookup_subsystem(struct nvme_host *h,
397+
const char *name,
398+
const char *subsysnqn)
399+
{
400+
struct nvme_subsystem *s;
401+
402+
nvme_for_each_subsystem(h, s) {
403+
if (strcmp(s->subsysnqn, subsysnqn))
404+
continue;
405+
if (name && s->name &&
406+
strcmp(s->name, name))
407+
continue;
408+
return s;
409+
}
410+
return nvme_alloc_subsystem(h, name, subsysnqn);
411+
}
412+
401413
static void __nvme_free_host(struct nvme_host *h)
402414
{
403415
struct nvme_subsystem *s, *_s;
@@ -551,12 +563,6 @@ static int nvme_scan_subsystem(struct nvme_root *r, const char *name,
551563
errno = ENOMEM;
552564
return -1;
553565
}
554-
if (!s->name) {
555-
ret = nvme_init_subsystem(s, name);
556-
if (ret < 0)
557-
return -1;
558-
}
559-
560566
nvme_subsystem_scan_namespaces(r, s);
561567

562568
if (f && !f(s)) {
@@ -1199,14 +1205,6 @@ int nvme_init_ctrl(nvme_host_t h, nvme_ctrl_t c, int instance)
11991205
ret = -1;
12001206
goto out_free_subsys;
12011207
}
1202-
if (!s->name) {
1203-
ret = nvme_init_subsystem(s, subsys_name);
1204-
if (ret < 0) {
1205-
nvme_msg(h->r, LOG_ERR, "Failed to init subsystem %s\n",
1206-
subsys_name);
1207-
goto out_free_subsys;
1208-
}
1209-
}
12101208
if (s->subsystype && !strcmp(s->subsystype, "discovery"))
12111209
c->discovery_ctrl = true;
12121210
c->s = s;
@@ -1372,12 +1370,9 @@ nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name)
13721370
/* subsysname might be NULL here */
13731371
s = nvme_lookup_subsystem(h, subsysname, subsysnqn);
13741372
free(subsysnqn);
1375-
1376-
ret = 0;
1377-
if (s && !s->name && subsysname)
1378-
ret = nvme_init_subsystem(s, subsysname);
13791373
free(subsysname);
1380-
if (!s || ret < 0) {
1374+
1375+
if (!s) {
13811376
free(path);
13821377
errno = ENOMEM;
13831378
return NULL;

0 commit comments

Comments
 (0)