Skip to content

Commit 28e2142

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 921deb9 commit 28e2142

12 files changed

Lines changed: 78 additions & 63 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/cpp.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ int main()
2525
return 1;
2626

2727
err = nvme_scan_topology(ctx, NULL, NULL);
28-
if (err && !(err == -ENOENT || err == -EACCES))
29-
goto out;
30-
err = 0;
28+
if (err && !(err == -ENOENT || err == -EACCES)) {
29+
fprintf(stderr, "nvme_scan_topology failed %d\n", err);
30+
nvme_free_global_ctx(ctx);
31+
return 1;
32+
}
3133

3234
nvme_for_each_host(ctx, h) {
3335
nvme_for_each_subsystem(h, s) {
@@ -68,8 +70,7 @@ int main()
6870
}
6971
std::cout << "\n";
7072

71-
out:
7273
nvme_free_global_ctx(ctx);
7374

74-
return err != 0 ? 1 : 0;
75+
return 0;
7576
}

libnvme/test/sysfs/tree-dump.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ static bool tree_dump(void)
2222
return false;
2323

2424
err = nvme_scan_topology(ctx, NULL, NULL);
25-
if (err && err != ENOENT)
25+
if (err && !(err == ENOENT || err == EACCES)) {
26+
fprintf(stderr, "nvme_scan_topology failed %d\n", err);
2627
goto out;
28+
}
2729

2830
if (nvme_dump_tree(ctx))
2931
goto out;

0 commit comments

Comments
 (0)