]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
tree: add 'f_args' argument to pass user data to the filter function
authorHannes Reinecke <hare@suse.de>
Fri, 1 Apr 2022 14:02:18 +0000 (16:02 +0200)
committerHannes Reinecke <hare@suse.de>
Fri, 1 Apr 2022 14:02:18 +0000 (16:02 +0200)
Increase usability by adding an 'f_args' argument to pass user data
to the filter function.

Signed-off-by: Hannes Reinecke <hare@suse.de>
src/nvme/tree.c
src/nvme/tree.h
test/test.c

index 4425863929e56f9ad4ecb975c91ac6d5fc3bba38..22772f169a61749ab55366cfd6a0a75933c765e2 100644 (file)
@@ -39,10 +39,11 @@ static struct nvme_host *default_host;
 static void __nvme_free_host(nvme_host_t h);
 static void __nvme_free_ctrl(nvme_ctrl_t c);
 static int nvme_subsystem_scan_namespace(nvme_root_t r,
-               struct nvme_subsystem *s, char *name, nvme_scan_filter_t f);
+               struct nvme_subsystem *s, char *name,
+               nvme_scan_filter_t f, void *f_args);
 static int nvme_init_subsystem(nvme_subsystem_t s, const char *name);
 static int nvme_scan_subsystem(nvme_root_t r, const char *name,
-                              nvme_scan_filter_t f);
+                              nvme_scan_filter_t f, void *f_args);
 static int nvme_ctrl_scan_namespace(nvme_root_t r, struct nvme_ctrl *c,
                                    char *name);
 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)
        return h;
 }
 
-int nvme_scan_topology(struct nvme_root *r, nvme_scan_filter_t f)
+int nvme_scan_topology(struct nvme_root *r, nvme_scan_filter_t f, void *f_args)
 {
        struct dirent **subsys, **ctrls;
        int i, num_subsys, num_ctrls, ret;
@@ -97,7 +98,7 @@ int nvme_scan_topology(struct nvme_root *r, nvme_scan_filter_t f)
                                 ctrls[i]->d_name, strerror(errno));
                        continue;
                }
-               if ((f) && !f(NULL, c, NULL)) {
+               if ((f) && !f(NULL, c, NULL, f_args)) {
                        nvme_msg(r, LOG_DEBUG, "filter out controller %s\n",
                                 ctrls[i]->d_name);
                        nvme_free_ctrl(c);
@@ -114,7 +115,7 @@ int nvme_scan_topology(struct nvme_root *r, nvme_scan_filter_t f)
        }
 
        for (i = 0; i < num_subsys; i++) {
-               ret = nvme_scan_subsystem(r, subsys[i]->d_name, f);
+               ret = nvme_scan_subsystem(r, subsys[i]->d_name, f, f_args);
                if (ret < 0) {
                        nvme_msg(r, LOG_DEBUG,
                                 "failed to scan subsystem %s: %s\n",
@@ -172,7 +173,7 @@ nvme_root_t nvme_scan(const char *config_file)
 {
        nvme_root_t r = nvme_create_root(NULL, DEFAULT_LOGLEVEL);
 
-       nvme_scan_topology(r, NULL);
+       nvme_scan_topology(r, NULL, NULL);
        nvme_read_config(r, config_file);
        return r;
 }
@@ -266,7 +267,7 @@ void nvme_refresh_topology(nvme_root_t r)
 
        nvme_for_each_host_safe(r, h, _h)
                __nvme_free_host(h);
-       nvme_scan_topology(r, NULL);
+       nvme_scan_topology(r, NULL, NULL);
 }
 
 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,
 }
 
 static int nvme_subsystem_scan_namespaces(nvme_root_t r, nvme_subsystem_t s,
-               nvme_scan_filter_t f)
+               nvme_scan_filter_t f, void *f_args)
 {
        struct dirent **namespaces;
        int i, num_ns, ret;
@@ -491,7 +492,7 @@ static int nvme_subsystem_scan_namespaces(nvme_root_t r, nvme_subsystem_t s,
 
        for (i = 0; i < num_ns; i++) {
                ret = nvme_subsystem_scan_namespace(r, s,
-                                                   namespaces[i]->d_name, f);
+                               namespaces[i]->d_name, f, f_args);
                if (ret < 0)
                        nvme_msg(r, LOG_DEBUG,
                                 "failed to scan namespace %s: %s\n",
@@ -528,7 +529,7 @@ static int nvme_init_subsystem(nvme_subsystem_t s, const char *name)
 }
 
 static int nvme_scan_subsystem(struct nvme_root *r, const char *name,
-                              nvme_scan_filter_t f)
+               nvme_scan_filter_t f, void *f_args)
 {
        struct nvme_subsystem *s = NULL, *_s;
        char *path, *subsysnqn;
@@ -584,13 +585,13 @@ static int nvme_scan_subsystem(struct nvme_root *r, const char *name,
        if (!s)
                return -1;
 
-       if (f && !f(s, NULL, NULL)) {
+       if (f && !f(s, NULL, NULL, f_args)) {
                nvme_msg(r, LOG_DEBUG, "filter out subsystem %s\n", name);
                __nvme_free_subsystem(s);
                return 0;
        }
 
-       nvme_subsystem_scan_namespaces(r, s, f);
+       nvme_subsystem_scan_namespaces(r, s, f, f_args);
 
        return 0;
 }
@@ -1410,9 +1411,9 @@ void nvme_rescan_ctrl(struct nvme_ctrl *c)
        nvme_root_t r = c->s && c->s->h ? c->s->h->r : NULL;
        if (!c->s)
                return;
-       nvme_subsystem_scan_namespaces(r, c->s, NULL);
        nvme_ctrl_scan_namespaces(r, c);
        nvme_ctrl_scan_paths(r, c);
+       nvme_subsystem_scan_namespaces(r, c->s, NULL, NULL);
 }
 
 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)
 }
 
 static int nvme_subsystem_scan_namespace(nvme_root_t r, nvme_subsystem_t s,
-               char *name, nvme_scan_filter_t f)
+               char *name, nvme_scan_filter_t f, void *f_args)
 {
        struct nvme_ns *n;
 
@@ -1894,7 +1895,7 @@ static int nvme_subsystem_scan_namespace(nvme_root_t r, nvme_subsystem_t s,
                nvme_msg(r, LOG_DEBUG, "failed to scan namespace %s\n", name);
                return -1;
        }
-       if (f && !f(NULL, NULL, n)) {
+       if (f && !f(NULL, NULL, n, f_args)) {
                nvme_msg(r, LOG_DEBUG, "filter out namespace %s\n", name);
                __nvme_free_ns(n);
                return 0;
index 1ed487652c1ecacb035f07a5a531dc799414847a..9f7a621f8ac7ea32a30b94d4a318fa6fd044b42e 100644 (file)
@@ -33,7 +33,8 @@ typedef struct nvme_subsystem *nvme_subsystem_t;
 typedef struct nvme_host *nvme_host_t;
 typedef struct nvme_root *nvme_root_t;
 
-typedef bool (*nvme_scan_filter_t)(nvme_subsystem_t, nvme_ctrl_t, nvme_ns_t);
+typedef bool (*nvme_scan_filter_t)(nvme_subsystem_t, nvme_ctrl_t,
+                                  nvme_ns_t, void *);
 
 /**
  * nvme_create_root() - Initialize root object
@@ -1050,15 +1051,16 @@ const char *nvme_subsystem_get_type(nvme_subsystem_t s);
 
 /**
  * nvme_scan_topology() - Scan NVMe topology and apply filter
- * @r: nvme_root_t object
- * @f: filter to apply
+ * @r:     nvme_root_t object
+ * @f:     filter to apply
+ * @f_args: user-specified argument to @f
  *
  * Scans the NVMe topology and filters out the resulting elements
  * by applying @f.
  *
  * Return: Number of elements scanned
  */
-int nvme_scan_topology(nvme_root_t r, nvme_scan_filter_t f);
+int nvme_scan_topology(nvme_root_t r, nvme_scan_filter_t f, void *f_args);
 
 /**
  * nvme_host_get_hostnqn() - Host NQN of an nvme_host_t object
index b1d277578228156df4632725323441c758569855..bf134125cfe6ee27dde7b00abd19230ae8ea34c7 100644 (file)
 
 #include <ccan/endian/endian.h>
 
-static char *nqn_match;
-
 static bool nvme_match_subsysnqn_filter(nvme_subsystem_t s,
-               nvme_ctrl_t c, nvme_ns_t ns)
+               nvme_ctrl_t c, nvme_ns_t ns, void *f_args)
 {
+       char *nqn_match = f_args;
+
        if (s)
                return strcmp(nvme_subsystem_get_nqn(s), nqn_match) == 0;
        return true;
@@ -324,13 +324,13 @@ int main(int argc, char **argv)
        nvme_path_t p;
        nvme_ns_t n;
        const char *ctrl = "nvme4";
+       const char *nqn_match = "testnqn";
 
        printf("Test filter for common loop back target\n");
-       nqn_match = "testnqn";
        r = nvme_create_root(NULL, DEFAULT_LOGLEVEL);
        if (!r)
                return 1;
-       nvme_scan_topology(r, nvme_match_subsysnqn_filter);
+       nvme_scan_topology(r, nvme_match_subsysnqn_filter, (void *)nqn_match);
        nvme_for_each_host(r, h) {
                nvme_for_each_subsystem(h, s) {
                        printf("%s - NQN=%s\n", nvme_subsystem_get_name(s),