Skip to content

Commit 5c204b1

Browse files
authored
Merge pull request #108 from hreinecke/scan-rework
Rework scanning logic
2 parents 0721303 + c12bd2c commit 5c204b1

6 files changed

Lines changed: 54 additions & 102 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
@@ -240,7 +240,6 @@ LIBNVME_1_0 {
240240
nvme_scan_ctrl_namespaces;
241241
nvme_scan_filter;
242242
nvme_scan_namespace;
243-
nvme_scan_subsystem_ctrls;
244243
nvme_scan_subsystem_namespaces;
245244
nvme_scan_subsystems;
246245
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: 42 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ static struct nvme_host *default_host;
3939
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(struct nvme_subsystem *s, char *name);
42-
static int nvme_scan_subsystem(struct nvme_root *r, char *name,
42+
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,34 +439,13 @@ 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)
442+
static int nvme_init_subsystem(nvme_subsystem_t s, const char *name)
423443
{
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-
}
444+
char *path;
442445

443-
nvme_free_dirents(ctrls, i);
444-
return 0;
445-
}
446+
if (asprintf(&path, "%s/%s", nvme_subsys_sysfs_dir, name) < 0)
447+
return -1;
446448

447-
static int nvme_init_subsystem(nvme_subsystem_t s, const char *name,
448-
const char *path)
449-
{
450449
s->model = nvme_get_attr(path, "model");
451450
if (!s->model)
452451
s->model = strdup("undefined");
@@ -465,7 +464,7 @@ static int nvme_init_subsystem(nvme_subsystem_t s, const char *name,
465464
return 0;
466465
}
467466

468-
static int nvme_scan_subsystem(struct nvme_root *r, char *name,
467+
static int nvme_scan_subsystem(struct nvme_root *r, const char *name,
469468
nvme_scan_filter_t f)
470469
{
471470
struct nvme_subsystem *s;
@@ -509,13 +508,12 @@ static int nvme_scan_subsystem(struct nvme_root *r, char *name,
509508
goto free_path;
510509
}
511510
if (!s->name) {
512-
ret = nvme_init_subsystem(s, name, path);
511+
ret = nvme_init_subsystem(s, name);
513512
if (ret < 0)
514513
goto free_path;
515514
}
516515

517516
nvme_subsystem_scan_namespaces(s);
518-
nvme_subsystem_scan_ctrls(s);
519517

520518
if (f && !f(s)) {
521519
__nvme_free_subsystem(s);
@@ -1075,7 +1073,7 @@ static int nvme_ctrl_scan_namespaces(struct nvme_ctrl *c)
10751073
return 0;
10761074
}
10771075

1078-
static char *nvme_ctrl_lookup_subsystem_name(nvme_ctrl_t c)
1076+
static char *nvme_ctrl_lookup_subsystem_name(const char *ctrl_name)
10791077
{
10801078
struct dirent **subsys;
10811079
char *subsys_name = NULL;
@@ -1089,7 +1087,7 @@ static char *nvme_ctrl_lookup_subsystem_name(nvme_ctrl_t c)
10891087
struct stat st;
10901088

10911089
sprintf(path, "%s/%s/%s", nvme_subsys_sysfs_dir,
1092-
subsys[i]->d_name, c->name);
1090+
subsys[i]->d_name, ctrl_name);
10931091
nvme_msg(LOG_DEBUG, "lookup subsystem %s\n", path);
10941092
if (stat(path, &st) < 0)
10951093
continue;
@@ -1162,7 +1160,7 @@ int nvme_init_ctrl(nvme_host_t h, nvme_ctrl_t c, int instance)
11621160
}
11631161
}
11641162

1165-
subsys_name = nvme_ctrl_lookup_subsystem_name(c);
1163+
subsys_name = nvme_ctrl_lookup_subsystem_name(name);
11661164
if (!subsys_name) {
11671165
nvme_msg(LOG_ERR, "Failed to lookup subsystem name for %s\n",
11681166
c->name);
@@ -1177,17 +1175,10 @@ int nvme_init_ctrl(nvme_host_t h, nvme_ctrl_t c, int instance)
11771175
goto out_free_subsys;
11781176
}
11791177
if (!s->name) {
1180-
ret = asprintf(&path, "%s/%s", nvme_subsys_sysfs_dir,
1181-
subsys_name);
1182-
if (ret > 0)
1183-
ret = nvme_init_subsystem(s, subsys_name, path);
1184-
else
1185-
path = NULL;
1178+
ret = nvme_init_subsystem(s, subsys_name);
11861179
if (ret < 0) {
1187-
nvme_msg(LOG_ERR, "Failed to init subsystem %s/%s\n",
1188-
nvme_subsys_sysfs_dir, subsys_name);
1189-
if (path)
1190-
free(path);
1180+
nvme_msg(LOG_ERR, "Failed to init subsystem %s\n",
1181+
subsys_name);
11911182
goto out_free_subsys;
11921183
}
11931184
}
@@ -1289,7 +1280,7 @@ nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name)
12891280
nvme_subsystem_t s;
12901281
nvme_ctrl_t c;
12911282
char *path;
1292-
char *hostnqn, *hostid, *subsysnqn;
1283+
char *hostnqn, *hostid, *subsysnqn, *subsysname;
12931284
int ret;
12941285

12951286
ret = asprintf(&path, "%s/%s", nvme_ctrl_sysfs_dir, name);
@@ -1325,38 +1316,23 @@ nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name)
13251316
errno = ENXIO;
13261317
return NULL;
13271318
}
1328-
s = nvme_lookup_subsystem(h, NULL, subsysnqn);
1319+
subsysname = nvme_ctrl_lookup_subsystem_name(name);
1320+
s = nvme_lookup_subsystem(h, subsysname, subsysnqn);
13291321
free(subsysnqn);
13301322
if (!s) {
13311323
free(path);
13321324
errno = ENOMEM;
13331325
return NULL;
13341326
}
1335-
c = nvme_ctrl_alloc(s, path, name);
1336-
if (!c)
1337-
free(path);
1338-
1339-
return c;
1340-
}
1341-
1342-
static int nvme_subsystem_scan_ctrl(struct nvme_subsystem *s, char *name)
1343-
{
1344-
nvme_ctrl_t c;
1345-
char *path;
1346-
1347-
if (asprintf(&path, "%s/%s", s->sysfs_dir, name) < 0) {
1348-
errno = ENOMEM;
1349-
return -1;
1350-
}
1351-
13521327
c = nvme_ctrl_alloc(s, path, name);
13531328
if (!c) {
13541329
free(path);
1355-
return -1;
1330+
return NULL;
13561331
}
1332+
13571333
nvme_ctrl_scan_namespaces(c);
13581334
nvme_ctrl_scan_paths(c);
1359-
return 0;
1335+
return c;
13601336
}
13611337

13621338
void nvme_rescan_ctrl(struct nvme_ctrl *c)

0 commit comments

Comments
 (0)