]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
tree: add 'nvme_update_config()' as API function
authorHannes Reinecke <hare@suse.de>
Wed, 5 May 2021 12:15:55 +0000 (14:15 +0200)
committerHannes Reinecke <hare@suse.de>
Fri, 11 Jun 2021 10:18:19 +0000 (12:18 +0200)
Add a function nvme_update_config() to write out the configuration
as an JSON file.

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

index 98280822f23bff085bb9306cfbe96694d9abe851..c902a3e92efae3a1ebe7e76364af3a70c861c935 100644 (file)
@@ -227,11 +227,12 @@ static void json_update_subsys(struct json_object *subsys_array,
        json_object_array_add(subsys_array, subsys_obj);
 }
 
-void json_update_config(nvme_root_t r, const char *config_file)
+int json_update_config(nvme_root_t r, const char *config_file)
 {
        nvme_host_t h;
        struct json_object *json_root, *host_obj;
        struct json_object *subsys_array;
+       int ret = 0;
 
        json_root = json_object_new_array();
        nvme_for_each_host(r, h) {
@@ -260,6 +261,10 @@ void json_update_config(nvme_root_t r, const char *config_file)
                                    JSON_C_TO_STRING_PRETTY) < 0) {
                fprintf(stderr, "Failed to write %s, %s\n",
                        config_file, json_util_get_last_err());
+               ret = -1;
+               errno = EIO;
        }
        json_object_put(json_root);
+
+       return ret;
 }
index bc823abb27d7bfd9944ef0ab32c5a0d7e3ca50f2..3d1a164f51ee803909114dd48e0073d0ef888e61 100644 (file)
@@ -13,6 +13,6 @@ int nvme_set_attr(const char *dir, const char *attr, const char *value);
 
 void json_read_config(nvme_root_t r, const char *config_file);
 
-void json_update_config(nvme_root_t r, const char *config_file);
+int json_update_config(nvme_root_t r, const char *config_file);
 
 #endif /* _LIBNVME_PRIVATE_H */
index 518e89edf2dba59bdf084065854dd77b61918ab2..38333e9712aa14a0455e19a0899b92454dc533bb 100644 (file)
@@ -132,6 +132,7 @@ struct nvme_host {
 
 struct nvme_root {
        struct list_head hosts;
+       bool modified;
 };
 
 static inline void nvme_free_dirents(struct dirent **d, int i)
@@ -202,6 +203,13 @@ nvme_root_t nvme_scan(const char *config_file)
        return r;
 }
 
+int nvme_update_config(nvme_root_t r, const char *config_file)
+{
+       if (!r->modified)
+               return 0;
+       return json_update_config(r, config_file);
+}
+
 nvme_host_t nvme_first_host(nvme_root_t r)
 {
        return list_top(&r->hosts, struct nvme_host, entry);
@@ -360,6 +368,7 @@ struct nvme_subsystem *nvme_lookup_subsystem(struct nvme_host *h,
        list_head_init(&s->ctrls);
        list_head_init(&s->namespaces);
        list_add(&h->subsystems, &s->entry);
+       h->r->modified = true;
        return s;
 }
 
@@ -373,6 +382,7 @@ static void nvme_free_host(struct nvme_host *h)
        free(h->hostnqn);
        if (h->hostid)
                free(h->hostid);
+       h->r->modified = true;
        free(h);
 }
 
@@ -399,6 +409,7 @@ struct nvme_host *nvme_lookup_host(nvme_root_t r, const char *hostnqn,
        list_node_init(&h->entry);
        h->r = r;
        list_add(&r->hosts, &h->entry);
+       r->modified = true;
 
        return h;
 }
@@ -435,7 +446,8 @@ static int nvme_subsystem_scan_ctrls(struct nvme_subsystem *s)
        return 0;
 }
 
-int nvme_init_subsystem(nvme_subsystem_t s, const char *name, const char *path)
+static int nvme_init_subsystem(nvme_subsystem_t s, const char *name,
+                              const char *path)
 {
        s->model = nvme_get_attr(path, "model");
        if (!s->model) {
@@ -693,6 +705,7 @@ struct nvme_fabrics_config *nvme_ctrl_get_config(nvme_ctrl_t c)
 void nvme_ctrl_disable_sqflow(nvme_ctrl_t c, bool disable_sqflow)
 {
        c->cfg.disable_sqflow = disable_sqflow;
+       c->s->h->r->modified = true;
 }
 
 void nvme_ctrl_set_discovered(nvme_ctrl_t c, bool discovered)
@@ -888,6 +901,7 @@ struct nvme_ctrl *nvme_lookup_ctrl(struct nvme_subsystem *s,
        if (c) {
                c->s = s;
                list_add(&s->ctrls, &c->entry);
+               s->h->r->modified = true;
        }
        return c;
 }
index 68428e9fe3adebb7358cac8b622f781ad0b0c8f3..8823254a09d31aa4f5714f6d496175c3d59a5289 100644 (file)
@@ -974,6 +974,15 @@ void nvme_refresh_topology(nvme_root_t r);
  */
 void nvme_reset_topology(nvme_root_t r);
 
+/**
+ * nvme_update_config() -
+ * @r:
+ * @config_file:
+ *
+ * Return:
+ */
+int nvme_update_config(nvme_root_t r, const char *config_file);
+
 /**
  * nvme_free_tree() -
  * @r: