Skip to content

Commit 5f7d4df

Browse files
committed
tree: remove nvme_scan
The problem with this function is that it does several things at once which prevents fine grained error handling. And almost every user of this function was not interested in reading a config file. Thus open code this and handle the ENOENT and EACCES case. For the Python binding we just ignore any nvme_scan_topology and nvme_read_config error because this happens inside the global context constructor Signed-off-by: Daniel Wagner <[email protected]>
1 parent a144a9d commit 5f7d4df

10 files changed

Lines changed: 67 additions & 57 deletions

File tree

libnvme/examples/discover-loop.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static void print_discover_log(struct nvmf_discovery_log *log)
5050

5151
int main()
5252
{
53-
struct nvmf_discovery_log *log = NULL;
53+
struct nvmf_discovery_log *log;
5454
struct nvme_global_ctx *ctx;
5555
nvme_host_t h;
5656
nvme_ctrl_t c;
@@ -59,24 +59,30 @@ int main()
5959

6060
nvmf_default_config(&cfg);
6161

62-
ret = nvme_scan(NULL, &ctx);
63-
if (ret)
64-
return ret;
62+
ctx = nvme_create_global_ctx(stdout, DEFAULT_LOGLEVEL);
63+
if (!ctx)
64+
return 1;
65+
66+
ret = nvme_scan_topology(ctx, NULL, NULL);
67+
if (ret) {
68+
nvme_free_global_ctx(ctx);
69+
return 1;
70+
}
6571
ret = nvme_host_get(ctx, NULL, NULL, &h);
6672
if (ret) {
6773
fprintf(stderr, "Failed to allocated memory\n");
68-
return ret;
74+
return 1;
6975
}
7076
ret = nvme_create_ctrl(ctx, NVME_DISC_SUBSYS_NAME, "loop",
7177
NULL, NULL, NULL, NULL, &c);
7278
if (ret) {
7379
fprintf(stderr, "Failed to allocate memory\n");
74-
return ENOMEM;
80+
return 1;
7581
}
7682
ret = nvmf_add_ctrl(h, c, &cfg);
7783
if (ret) {
7884
fprintf(stderr, "no controller found\n");
79-
return ret;
85+
return 1;
8086
}
8187

8288
ret = nvmf_get_discovery_log(c, &log, 4);

libnvme/examples/display-columnar.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,17 @@ int main()
2323
nvme_ctrl_t c;
2424
nvme_path_t p;
2525
nvme_ns_t n;
26+
int err;
2627

27-
if (nvme_scan(NULL, &ctx))
28-
return -1;
28+
ctx = nvme_create_global_ctx(stdout, DEFAULT_LOGLEVEL);
29+
if (!ctx)
30+
return 1;
31+
32+
err = nvme_scan_topology(ctx, NULL, NULL);
33+
if (err) {
34+
nvme_free_global_ctx(ctx);
35+
return 1;
36+
}
2937

3038
printf("%-16s %-96s %-.16s\n", "Subsystem", "Subsystem-NQN", "Controllers");
3139
printf("%-.16s %-.96s %-.16s\n", dash, dash, dash);

libnvme/examples/display-tree.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@ int main()
2222
nvme_ctrl_t c, _c;
2323
nvme_path_t p, _p;
2424
nvme_ns_t n, _n;
25+
int err;
2526

26-
if (nvme_scan(NULL, &ctx))
27-
return -1;
27+
ctx = nvme_create_global_ctx(stdout, DEFAULT_LOGLEVEL);
28+
if (!ctx)
29+
return 1;
30+
31+
err = nvme_scan_topology(ctx, NULL, NULL);
32+
if (err) {
33+
nvme_free_global_ctx(ctx);
34+
return 1;
35+
}
2836

2937
printf(".\n");
3038
nvme_for_each_host(ctx, h) {

libnvme/examples/telemetry-listen.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,14 @@ int main()
135135
nvme_ctrl_t c;
136136
nvme_host_t h;
137137

138-
if (nvme_scan(NULL, &ctx))
138+
ctx = nvme_create_global_ctx(stdout, DEFAULT_LOGLEVEL);
139+
if (!ctx)
140+
return 1;
141+
142+
if (nvme_scan_topology(ctx, NULL, NULL)) {
143+
nvme_free_global_ctx(ctx);
139144
return EXIT_FAILURE;
145+
}
140146

141147
nvme_for_each_host(ctx, h)
142148
nvme_for_each_subsystem(h, s)

libnvme/libnvme/nvme.i

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,14 @@ struct nvme_ns {
487487
%extend nvme_global_ctx {
488488
nvme_global_ctx(const char *config_file = NULL) {
489489
struct nvme_global_ctx *ctx;
490-
if (nvme_scan(config_file, &ctx))
490+
491+
ctx = nvme_create_global_ctx(stdout, DEFAULT_LOGLEVEL);
492+
if (!ctx)
491493
return NULL;
494+
495+
nvme_scan_topology(ctx, NULL, NULL);
496+
nvme_read_config(ctx, config_file);
497+
492498
return ctx;
493499
}
494500
~nvme_global_ctx() {

libnvme/src/libnvme.ld

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ LIBNVME_2_0 {
208208
nvme_release_fds;
209209
nvme_rescan_ctrl;
210210
nvme_revoke_tls_key;
211-
nvme_scan;
212211
nvme_scan_ctrl;
213212
nvme_scan_ctrl_namespace_paths;
214213
nvme_scan_ctrl_namespaces;

libnvme/src/nvme/tree.c

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -345,32 +345,6 @@ int nvme_read_config(struct nvme_global_ctx *ctx, const char *config_file)
345345
return err;
346346
}
347347

348-
int nvme_scan(const char *config_file, struct nvme_global_ctx **ctxp)
349-
{
350-
struct nvme_global_ctx *ctx =
351-
nvme_create_global_ctx(NULL, DEFAULT_LOGLEVEL);
352-
int ret;
353-
354-
if (!ctx)
355-
return -ENOMEM;
356-
357-
ret = nvme_scan_topology(ctx, NULL, NULL);
358-
if (ret && ret != -ENOENT)
359-
goto err;
360-
if (config_file) {
361-
ret = nvme_read_config(ctx, config_file);
362-
if (ret)
363-
goto err;
364-
}
365-
366-
*ctxp = ctx;
367-
return 0;
368-
369-
err:
370-
nvme_free_global_ctx(ctx);
371-
return ret;
372-
}
373-
374348
int nvme_dump_config(struct nvme_global_ctx *ctx, int fd)
375349
{
376350
return json_update_config(ctx, fd);

libnvme/src/nvme/tree.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,15 +1339,6 @@ void nvme_host_release_fds(struct nvme_host *h);
13391339
*/
13401340
void nvme_free_host(nvme_host_t h);
13411341

1342-
/**
1343-
* nvme_scan() - Scan NVMe topology
1344-
* @config_file: Configuration file
1345-
* @ctx: &nvme_global_ctx object to return
1346-
*
1347-
* Return: 0 on success or negative error code otherwise
1348-
*/
1349-
int nvme_scan(const char *config_file, struct nvme_global_ctx **ctx);
1350-
13511342
/**
13521343
* nvme_read_config() - Read NVMe JSON configuration file
13531344
* @ctx: &struct nvme_global_ctx object

libnvme/test/test.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,22 @@ int main(int argc, char **argv)
387387
nvme_ctrl_t c;
388388
nvme_path_t p;
389389
nvme_ns_t n;
390+
int err;
390391
const char *ctrl = "nvme4";
391392
const char *nqn_match = "testnqn";
392393

393394
printf("Test filter for common loop back target\n");
394-
ctx = nvme_create_global_ctx(NULL, DEFAULT_LOGLEVEL);
395+
ctx = nvme_create_global_ctx(stdout, DEFAULT_LOGLEVEL);
395396
if (!ctx)
396397
return 1;
397-
nvme_scan_topology(ctx, nvme_match_subsysnqn_filter, (void *)nqn_match);
398+
399+
err = nvme_scan_topology(ctx, nvme_match_subsysnqn_filter,
400+
(void *)nqn_match);
401+
if (err && !(err == ENOENT || err == EACCES)) {
402+
nvme_free_global_ctx(ctx);
403+
return 1;
404+
}
405+
398406
nvme_for_each_host(ctx, h) {
399407
nvme_for_each_subsystem(h, s) {
400408
printf("%s - NQN=%s\n", nvme_subsystem_get_name(s),
@@ -421,10 +429,6 @@ int main(int argc, char **argv)
421429
nvme_free_ctrl(c);
422430
}
423431
printf("\n");
424-
nvme_free_global_ctx(ctx);
425-
426-
if (nvme_scan(NULL, &ctx))
427-
return -1;
428432

429433
printf("Test walking the topology\n");
430434
nvme_for_each_host(ctx, h) {

libnvme/test/zns.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,17 @@ int main()
8080
nvme_host_t h;
8181
nvme_ctrl_t c;
8282
nvme_ns_t n;
83+
int err;
84+
85+
ctx = nvme_create_global_ctx(stdout, DEFAULT_LOGLEVEL);
86+
if (!ctx)
87+
return 1;
8388

84-
if (nvme_scan(NULL, &ctx))
85-
return -1;
89+
err = nvme_scan_topology(ctx, NULL, NULL);
90+
if (err && !(err == -ENOENT || err == -EACCES)) {
91+
nvme_free_global_ctx(ctx);
92+
return 1;
93+
}
8694

8795
nvme_for_each_host(ctx, h) {
8896
nvme_for_each_subsystem(h, s) {

0 commit comments

Comments
 (0)