Skip to content

Commit a98edd0

Browse files
committed
tree: add 'f_args' argument to pass user data to the filter function
Increase usability by adding an 'f_args' argument to pass user data to the filter function. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent 38720f1 commit a98edd0

3 files changed

Lines changed: 27 additions & 24 deletions

File tree

src/nvme/tree.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ 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(nvme_root_t r,
42-
struct nvme_subsystem *s, char *name, nvme_scan_filter_t f);
42+
struct nvme_subsystem *s, char *name,
43+
nvme_scan_filter_t f, void *f_args);
4344
static int nvme_init_subsystem(nvme_subsystem_t s, const char *name);
4445
static int nvme_scan_subsystem(nvme_root_t r, const char *name,
45-
nvme_scan_filter_t f);
46+
nvme_scan_filter_t f, void *f_args);
4647
static int nvme_ctrl_scan_namespace(nvme_root_t r, struct nvme_ctrl *c,
4748
char *name);
4849
static int nvme_ctrl_scan_path(nvme_root_t r, struct nvme_ctrl *c, char *name);
@@ -75,7 +76,7 @@ nvme_host_t nvme_default_host(nvme_root_t r)
7576
return h;
7677
}
7778

78-
int nvme_scan_topology(struct nvme_root *r, nvme_scan_filter_t f)
79+
int nvme_scan_topology(struct nvme_root *r, nvme_scan_filter_t f, void *f_args)
7980
{
8081
struct dirent **subsys, **ctrls;
8182
int i, num_subsys, num_ctrls, ret;
@@ -97,7 +98,7 @@ int nvme_scan_topology(struct nvme_root *r, nvme_scan_filter_t f)
9798
ctrls[i]->d_name, strerror(errno));
9899
continue;
99100
}
100-
if ((f) && !f(NULL, c, NULL)) {
101+
if ((f) && !f(NULL, c, NULL, f_args)) {
101102
nvme_msg(r, LOG_DEBUG, "filter out controller %s\n",
102103
ctrls[i]->d_name);
103104
nvme_free_ctrl(c);
@@ -114,7 +115,7 @@ int nvme_scan_topology(struct nvme_root *r, nvme_scan_filter_t f)
114115
}
115116

116117
for (i = 0; i < num_subsys; i++) {
117-
ret = nvme_scan_subsystem(r, subsys[i]->d_name, f);
118+
ret = nvme_scan_subsystem(r, subsys[i]->d_name, f, f_args);
118119
if (ret < 0) {
119120
nvme_msg(r, LOG_DEBUG,
120121
"failed to scan subsystem %s: %s\n",
@@ -172,7 +173,7 @@ nvme_root_t nvme_scan(const char *config_file)
172173
{
173174
nvme_root_t r = nvme_create_root(NULL, DEFAULT_LOGLEVEL);
174175

175-
nvme_scan_topology(r, NULL);
176+
nvme_scan_topology(r, NULL, NULL);
176177
nvme_read_config(r, config_file);
177178
return r;
178179
}
@@ -266,7 +267,7 @@ void nvme_refresh_topology(nvme_root_t r)
266267

267268
nvme_for_each_host_safe(r, h, _h)
268269
__nvme_free_host(h);
269-
nvme_scan_topology(r, NULL);
270+
nvme_scan_topology(r, NULL, NULL);
270271
}
271272

272273
void nvme_free_tree(nvme_root_t r)
@@ -476,7 +477,7 @@ struct nvme_host *nvme_lookup_host(nvme_root_t r, const char *hostnqn,
476477
}
477478

478479
static int nvme_subsystem_scan_namespaces(nvme_root_t r, nvme_subsystem_t s,
479-
nvme_scan_filter_t f)
480+
nvme_scan_filter_t f, void *f_args)
480481
{
481482
struct dirent **namespaces;
482483
int i, num_ns, ret;
@@ -491,7 +492,7 @@ static int nvme_subsystem_scan_namespaces(nvme_root_t r, nvme_subsystem_t s,
491492

492493
for (i = 0; i < num_ns; i++) {
493494
ret = nvme_subsystem_scan_namespace(r, s,
494-
namespaces[i]->d_name, f);
495+
namespaces[i]->d_name, f, f_args);
495496
if (ret < 0)
496497
nvme_msg(r, LOG_DEBUG,
497498
"failed to scan namespace %s: %s\n",
@@ -528,7 +529,7 @@ static int nvme_init_subsystem(nvme_subsystem_t s, const char *name)
528529
}
529530

530531
static int nvme_scan_subsystem(struct nvme_root *r, const char *name,
531-
nvme_scan_filter_t f)
532+
nvme_scan_filter_t f, void *f_args)
532533
{
533534
struct nvme_subsystem *s = NULL, *_s;
534535
char *path, *subsysnqn;
@@ -584,13 +585,13 @@ static int nvme_scan_subsystem(struct nvme_root *r, const char *name,
584585
if (!s)
585586
return -1;
586587

587-
if (f && !f(s, NULL, NULL)) {
588+
if (f && !f(s, NULL, NULL, f_args)) {
588589
nvme_msg(r, LOG_DEBUG, "filter out subsystem %s\n", name);
589590
__nvme_free_subsystem(s);
590591
return 0;
591592
}
592593

593-
nvme_subsystem_scan_namespaces(r, s, f);
594+
nvme_subsystem_scan_namespaces(r, s, f, f_args);
594595

595596
return 0;
596597
}
@@ -1410,9 +1411,9 @@ void nvme_rescan_ctrl(struct nvme_ctrl *c)
14101411
nvme_root_t r = c->s && c->s->h ? c->s->h->r : NULL;
14111412
if (!c->s)
14121413
return;
1413-
nvme_subsystem_scan_namespaces(r, c->s, NULL);
14141414
nvme_ctrl_scan_namespaces(r, c);
14151415
nvme_ctrl_scan_paths(r, c);
1416+
nvme_subsystem_scan_namespaces(r, c->s, NULL, NULL);
14161417
}
14171418

14181419
static int nvme_bytes_to_lba(nvme_ns_t n, off_t offset, size_t count,
@@ -1883,7 +1884,7 @@ static void nvme_subsystem_set_ns_path(nvme_subsystem_t s, nvme_ns_t n)
18831884
}
18841885

18851886
static int nvme_subsystem_scan_namespace(nvme_root_t r, nvme_subsystem_t s,
1886-
char *name, nvme_scan_filter_t f)
1887+
char *name, nvme_scan_filter_t f, void *f_args)
18871888
{
18881889
struct nvme_ns *n;
18891890

@@ -1894,7 +1895,7 @@ static int nvme_subsystem_scan_namespace(nvme_root_t r, nvme_subsystem_t s,
18941895
nvme_msg(r, LOG_DEBUG, "failed to scan namespace %s\n", name);
18951896
return -1;
18961897
}
1897-
if (f && !f(NULL, NULL, n)) {
1898+
if (f && !f(NULL, NULL, n, f_args)) {
18981899
nvme_msg(r, LOG_DEBUG, "filter out namespace %s\n", name);
18991900
__nvme_free_ns(n);
19001901
return 0;

src/nvme/tree.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ typedef struct nvme_subsystem *nvme_subsystem_t;
3333
typedef struct nvme_host *nvme_host_t;
3434
typedef struct nvme_root *nvme_root_t;
3535

36-
typedef bool (*nvme_scan_filter_t)(nvme_subsystem_t, nvme_ctrl_t, nvme_ns_t);
36+
typedef bool (*nvme_scan_filter_t)(nvme_subsystem_t, nvme_ctrl_t,
37+
nvme_ns_t, void *);
3738

3839
/**
3940
* nvme_create_root() - Initialize root object
@@ -1050,15 +1051,16 @@ const char *nvme_subsystem_get_type(nvme_subsystem_t s);
10501051

10511052
/**
10521053
* nvme_scan_topology() - Scan NVMe topology and apply filter
1053-
* @r: nvme_root_t object
1054-
* @f: filter to apply
1054+
* @r: nvme_root_t object
1055+
* @f: filter to apply
1056+
* @f_args: user-specified argument to @f
10551057
*
10561058
* Scans the NVMe topology and filters out the resulting elements
10571059
* by applying @f.
10581060
*
10591061
* Return: Number of elements scanned
10601062
*/
1061-
int nvme_scan_topology(nvme_root_t r, nvme_scan_filter_t f);
1063+
int nvme_scan_topology(nvme_root_t r, nvme_scan_filter_t f, void *f_args);
10621064

10631065
/**
10641066
* nvme_host_get_hostnqn() - Host NQN of an nvme_host_t object

test/test.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424

2525
#include <ccan/endian/endian.h>
2626

27-
static char *nqn_match;
28-
2927
static bool nvme_match_subsysnqn_filter(nvme_subsystem_t s,
30-
nvme_ctrl_t c, nvme_ns_t ns)
28+
nvme_ctrl_t c, nvme_ns_t ns, void *f_args)
3129
{
30+
char *nqn_match = f_args;
31+
3232
if (s)
3333
return strcmp(nvme_subsystem_get_nqn(s), nqn_match) == 0;
3434
return true;
@@ -324,13 +324,13 @@ int main(int argc, char **argv)
324324
nvme_path_t p;
325325
nvme_ns_t n;
326326
const char *ctrl = "nvme4";
327+
const char *nqn_match = "testnqn";
327328

328329
printf("Test filter for common loop back target\n");
329-
nqn_match = "testnqn";
330330
r = nvme_create_root(NULL, DEFAULT_LOGLEVEL);
331331
if (!r)
332332
return 1;
333-
nvme_scan_topology(r, nvme_match_subsysnqn_filter);
333+
nvme_scan_topology(r, nvme_match_subsysnqn_filter, (void *)nqn_match);
334334
nvme_for_each_host(r, h) {
335335
nvme_for_each_subsystem(h, s) {
336336
printf("%s - NQN=%s\n", nvme_subsystem_get_name(s),

0 commit comments

Comments
 (0)