Skip to content

Commit c12bd2c

Browse files
hreineckedwsuse
authored andcommitted
tree: scan controllers before scanning subsystems in nvme_scan_topology()
The linux sysfs hierarchy only knows about 'controllers' and 'subsystems', so when scanning the topology we should follow this structure and scan first for controllers and then for subsystems. Just scanning for subsystems and all controllers attached to them has the problem that the hostnqn can only be derived from the controller, but by the time the controller is scanned the subsystem has already been created and attached to the default host. Scanning for controllers first will setup the correct hosts (and subsystems), so the subsequent scan for subsystems is primarily there to find all namespaces attached to the subsystems. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent 6b4c2fb commit c12bd2c

6 files changed

Lines changed: 38 additions & 84 deletions

File tree

doc/libnvme.rst

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,6 @@
3838
``struct dirent *** subsys``
3939
4040
41-
.. c:function:: int nvme_scan_subsystem_ctrls (nvme_subsystem_t s, struct dirent *** ctrls)
42-
43-
44-
**Parameters**
45-
46-
``nvme_subsystem_t s``
47-
*undescribed*
48-
49-
``struct dirent *** ctrls``
50-
51-
5241
.. c:function:: int nvme_scan_subsystem_namespaces (nvme_subsystem_t s, struct dirent *** namespaces)
5342
5443

doc/man/nvme_scan_subsystem_ctrls.2

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/libnvme.map

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ LIBNVME_1_0 {
241241
nvme_scan_ctrl_namespaces;
242242
nvme_scan_filter;
243243
nvme_scan_namespace;
244-
nvme_scan_subsystem_ctrls;
245244
nvme_scan_subsystem_namespaces;
246245
nvme_scan_subsystems;
247246
nvme_security_receive;

src/nvme/filters.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,18 @@ int nvme_scan_subsystems(struct dirent ***subsys)
9393
alphasort);
9494
}
9595

96-
int nvme_scan_subsystem_ctrls(nvme_subsystem_t s, struct dirent ***ctrls)
97-
{
98-
return scandir(nvme_subsystem_get_sysfs_dir(s), ctrls,
99-
nvme_ctrls_filter, alphasort);
100-
}
101-
10296
int nvme_scan_subsystem_namespaces(nvme_subsystem_t s, struct dirent ***namespaces)
10397
{
10498
return scandir(nvme_subsystem_get_sysfs_dir(s), namespaces,
10599
nvme_namespace_filter, alphasort);
106100
}
107101

102+
int nvme_scan_ctrls(struct dirent ***ctrls)
103+
{
104+
return scandir(nvme_ctrl_sysfs_dir, ctrls, nvme_ctrls_filter,
105+
alphasort);
106+
}
107+
108108
int nvme_scan_ctrl_namespace_paths(nvme_ctrl_t c, struct dirent ***namespaces)
109109
{
110110
return scandir(nvme_ctrl_get_sysfs_dir(c), namespaces,

src/nvme/filters.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,21 @@ int nvme_subsys_filter(const struct dirent *d);
5353
int nvme_scan_subsystems(struct dirent ***subsys);
5454

5555
/**
56-
* nvme_scan_subsystem_ctrls() -
56+
* nvme_scan_subsystem_namespaces() -
5757
* @s:
58-
* @ctrls:
58+
* @namespaces:
5959
*
6060
* Return:
6161
*/
62-
int nvme_scan_subsystem_ctrls(nvme_subsystem_t s, struct dirent ***ctrls);
62+
int nvme_scan_subsystem_namespaces(nvme_subsystem_t s, struct dirent ***namespaces);
6363

6464
/**
65-
* nvme_scan_subsystem_namespaces() -
66-
* @s:
67-
* @namespaces:
65+
* nvme_scan_ctrls() -
66+
* @ctrls:
6867
*
6968
* Return:
7069
*/
71-
int nvme_scan_subsystem_namespaces(nvme_subsystem_t s, struct dirent ***namespaces);
70+
int nvme_scan_ctrls(struct dirent ***ctrls);
7271

7372
/**
7473
* nvme_scan_ctrl_namespace_paths() -

src/nvme/tree.c

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ static void __nvme_free_ctrl(nvme_ctrl_t c);
4141
static int nvme_subsystem_scan_namespace(struct nvme_subsystem *s, char *name);
4242
static int nvme_scan_subsystem(struct nvme_root *r, const char *name,
4343
nvme_scan_filter_t f);
44-
static int nvme_subsystem_scan_ctrl(struct nvme_subsystem *s, char *name);
4544
static int nvme_ctrl_scan_namespace(struct nvme_ctrl *c, char *name);
4645
static int nvme_ctrl_scan_path(struct nvme_ctrl *c, char *name);
4746

@@ -72,8 +71,28 @@ nvme_host_t nvme_default_host(nvme_root_t r)
7271

7372
static int nvme_scan_topology(struct nvme_root *r, nvme_scan_filter_t f)
7473
{
75-
struct dirent **subsys;
76-
int i, num_subsys, ret;
74+
struct dirent **subsys, **ctrls;
75+
int i, num_subsys, num_ctrls, ret;
76+
77+
num_ctrls = nvme_scan_ctrls(&ctrls);
78+
if (num_ctrls < 0) {
79+
nvme_msg(LOG_DEBUG, "failed to scan ctrls: %s\n",
80+
strerror(errno));
81+
return num_ctrls;
82+
}
83+
84+
for (i = 0; i < num_ctrls; i++) {
85+
nvme_ctrl_t c = nvme_scan_ctrl(r, ctrls[i]->d_name);
86+
if (!c) {
87+
nvme_msg(LOG_DEBUG, "failed to scan ctrl %s: %s\n",
88+
ctrls[i]->d_name, strerror(errno));
89+
}
90+
if ((f) && !f(c->s)) {
91+
nvme_free_ctrl(c);
92+
}
93+
}
94+
95+
nvme_free_dirents(ctrls, i);
7796

7897
num_subsys = nvme_scan_subsystems(&subsys);
7998
if (num_subsys < 0) {
@@ -91,6 +110,7 @@ static int nvme_scan_topology(struct nvme_root *r, nvme_scan_filter_t f)
91110
}
92111

93112
nvme_free_dirents(subsys, i);
113+
94114
return 0;
95115
}
96116

@@ -419,31 +439,6 @@ static int nvme_subsystem_scan_namespaces(struct nvme_subsystem *s)
419439
return 0;
420440
}
421441

422-
static int nvme_subsystem_scan_ctrls(struct nvme_subsystem *s)
423-
{
424-
struct dirent **ctrls;
425-
int i, num_ctrls, ret;
426-
427-
num_ctrls = nvme_scan_subsystem_ctrls(s, &ctrls);
428-
if (num_ctrls < 0) {
429-
nvme_msg(LOG_DEBUG,
430-
"failed to scan ctrls for subsys %s: %s\n",
431-
s->subsysnqn, strerror(errno));
432-
return num_ctrls;
433-
}
434-
435-
for (i = 0; i < num_ctrls; i++) {
436-
ret = nvme_subsystem_scan_ctrl(s, ctrls[i]->d_name);
437-
if (ret < 0)
438-
nvme_msg(LOG_DEBUG,
439-
"failed to scan ctrl %s: %s\n",
440-
ctrls[i]->d_name, strerror(errno));
441-
}
442-
443-
nvme_free_dirents(ctrls, i);
444-
return 0;
445-
}
446-
447442
static int nvme_init_subsystem(nvme_subsystem_t s, const char *name)
448443
{
449444
char *path;
@@ -519,7 +514,6 @@ static int nvme_scan_subsystem(struct nvme_root *r, const char *name,
519514
}
520515

521516
nvme_subsystem_scan_namespaces(s);
522-
nvme_subsystem_scan_ctrls(s);
523517

524518
if (f && !f(s)) {
525519
__nvme_free_subsystem(s);
@@ -1330,31 +1324,15 @@ nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name)
13301324
errno = ENOMEM;
13311325
return NULL;
13321326
}
1333-
c = nvme_ctrl_alloc(s, path, name);
1334-
if (!c)
1335-
free(path);
1336-
1337-
return c;
1338-
}
1339-
1340-
static int nvme_subsystem_scan_ctrl(struct nvme_subsystem *s, char *name)
1341-
{
1342-
nvme_ctrl_t c;
1343-
char *path;
1344-
1345-
if (asprintf(&path, "%s/%s", s->sysfs_dir, name) < 0) {
1346-
errno = ENOMEM;
1347-
return -1;
1348-
}
1349-
13501327
c = nvme_ctrl_alloc(s, path, name);
13511328
if (!c) {
13521329
free(path);
1353-
return -1;
1330+
return NULL;
13541331
}
1332+
13551333
nvme_ctrl_scan_namespaces(c);
13561334
nvme_ctrl_scan_paths(c);
1357-
return 0;
1335+
return c;
13581336
}
13591337

13601338
void nvme_rescan_ctrl(struct nvme_ctrl *c)

0 commit comments

Comments
 (0)