2 * Copyright (C) 2015 - 2016 Red Hat, Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * Author: Gris Ge <fge@redhat.com>
18 * Todd Gill <tgill@redhat.com>
29 #include "libdmmp/libdmmp.h"
30 #include "libdmmp_private.h"
35 uint32_t dmmp_pg_count;
36 struct dmmp_path_group **dmmp_pgs;
40 _dmmp_getter_func_gen(dmmp_mpath_name_get, struct dmmp_mpath, dmmp_mp,
42 _dmmp_getter_func_gen(dmmp_mpath_wwid_get, struct dmmp_mpath, dmmp_mp,
44 _dmmp_getter_func_gen(dmmp_mpath_kdev_name_get, struct dmmp_mpath, dmmp_mp,
45 kdev_name, const char *);
47 struct dmmp_mpath *_dmmp_mpath_new(void)
49 struct dmmp_mpath *dmmp_mp = NULL;
51 dmmp_mp = (struct dmmp_mpath *) malloc(sizeof(struct dmmp_mpath));
53 if (dmmp_mp != NULL) {
55 dmmp_mp->alias = NULL;
56 dmmp_mp->dmmp_pg_count = 0;
57 dmmp_mp->dmmp_pgs = NULL;
62 int _dmmp_mpath_update(struct dmmp_context *ctx, struct dmmp_mpath *dmmp_mp,
63 json_object *j_obj_map)
66 const char *wwid = NULL;
67 const char *alias = NULL;
68 struct array_list *ar_pgs = NULL;
71 struct dmmp_path_group *dmmp_pg = NULL;
72 const char *kdev_name = NULL;
75 assert(dmmp_mp != NULL);
76 assert(j_obj_map != NULL);
78 _json_obj_get_value(ctx, j_obj_map, wwid, "uuid", json_type_string,
79 json_object_get_string, rc, out);
80 _json_obj_get_value(ctx, j_obj_map, alias, "name", json_type_string,
81 json_object_get_string, rc, out);
82 _json_obj_get_value(ctx, j_obj_map, kdev_name, "sysfs",
83 json_type_string, json_object_get_string, rc, out);
85 _dmmp_null_or_empty_str_check(ctx, wwid, rc, out);
86 _dmmp_null_or_empty_str_check(ctx, alias, rc, out);
88 dmmp_mp->wwid = strdup(wwid);
89 _dmmp_alloc_null_check(ctx, dmmp_mp->wwid, rc, out);
90 dmmp_mp->alias = strdup(alias);
91 _dmmp_alloc_null_check(ctx, dmmp_mp->alias, rc, out);
92 dmmp_mp->kdev_name = strdup(kdev_name);
93 _dmmp_alloc_null_check(ctx, dmmp_mp->kdev_name, rc, out);
95 _json_obj_get_value(ctx, j_obj_map, ar_pgs, "path_groups",
96 json_type_array, json_object_get_array, rc, out);
97 ar_pgs_len = array_list_length(ar_pgs);
100 _error(ctx, "BUG: Got negative length for ar_pgs");
103 else if (ar_pgs_len == 0)
106 dmmp_mp->dmmp_pg_count = ar_pgs_len & UINT32_MAX;
108 dmmp_mp->dmmp_pgs = (struct dmmp_path_group **)
109 malloc(sizeof(struct dmmp_path_group *) *
110 dmmp_mp->dmmp_pg_count);
111 _dmmp_alloc_null_check(ctx, dmmp_mp->dmmp_pgs, rc, out);
112 for (; i < dmmp_mp->dmmp_pg_count; ++i)
113 dmmp_mp->dmmp_pgs[i] = NULL;
115 for (i = 0; i < dmmp_mp->dmmp_pg_count; ++i) {
116 dmmp_pg = _dmmp_path_group_new();
117 _dmmp_alloc_null_check(ctx, dmmp_pg, rc, out);
118 dmmp_mp->dmmp_pgs[i] = dmmp_pg;
119 _good(_dmmp_path_group_update(ctx, dmmp_pg,
120 array_list_get_idx(ar_pgs, i)),
124 _debug(ctx, "Got mpath wwid: '%s', alias: '%s'", dmmp_mp->wwid,
129 _dmmp_mpath_free(dmmp_mp);
133 void _dmmp_mpath_free(struct dmmp_mpath *dmmp_mp)
138 free((char *) dmmp_mp->alias);
139 free((char *) dmmp_mp->wwid);
140 free((char *) dmmp_mp->kdev_name);
142 if (dmmp_mp->dmmp_pgs != NULL)
143 _dmmp_path_group_array_free(dmmp_mp->dmmp_pgs,
144 dmmp_mp->dmmp_pg_count);
149 void dmmp_path_group_array_get(struct dmmp_mpath *dmmp_mp,
150 struct dmmp_path_group ***dmmp_pgs,
151 uint32_t *dmmp_pg_count)
153 assert(dmmp_mp != NULL);
154 assert(dmmp_pgs != NULL);
155 assert(dmmp_pg_count != NULL);
157 *dmmp_pgs = dmmp_mp->dmmp_pgs;
158 *dmmp_pg_count = dmmp_mp->dmmp_pg_count;