[libmultipath] embed a struct dm_info pointer in struct multipath
[platform/upstream/multipath-tools.git] / libmultipath / structs.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <libdevmapper.h>
4
5 #include "memory.h"
6 #include "vector.h"
7 #include "util.h"
8 #include "structs.h"
9 #include "config.h"
10 #include "debug.h"
11
12 struct path *
13 alloc_path (void)
14 {
15         struct path * pp;
16         
17         pp = (struct path *)MALLOC(sizeof(struct path));
18
19         if (pp) {
20                 pp->sg_id.host_no = -1;
21                 pp->sg_id.channel = -1;
22                 pp->sg_id.scsi_id = -1;
23                 pp->sg_id.lun = -1;
24                 pp->fd = -1;
25         }
26         return pp;
27 }
28
29 void
30 free_path (struct path * pp)
31 {
32         if (!pp)
33                 return;
34
35         if (pp->checker_context)
36                 free(pp->checker_context);
37
38         if (pp->fd >= 0)
39                 close(pp->fd);
40
41         FREE(pp);
42 }
43
44 void
45 free_pathvec (vector vec, int free_paths)
46 {
47         int i;
48         struct path * pp;
49
50         if (!vec)
51                 return;
52
53         if (free_paths)
54                 vector_foreach_slot(vec, pp, i)
55                         free_path(pp);
56
57         vector_free(vec);
58 }
59
60 struct pathgroup *
61 alloc_pathgroup (void)
62 {
63         struct pathgroup * pgp;
64
65         pgp = (struct pathgroup *)MALLOC(sizeof(struct pathgroup));
66
67         if (!pgp)
68                 return NULL;
69
70         pgp->paths = vector_alloc();
71
72         if (!pgp->paths)
73                 FREE(pgp);
74
75         return pgp;
76 }
77
78 void
79 free_pathgroup (struct pathgroup * pgp, int free_paths)
80 {
81         if (!pgp)
82                 return;
83
84         free_pathvec(pgp->paths, free_paths);
85         FREE(pgp);
86 }
87
88 void
89 free_pgvec (vector pgvec, int free_paths)
90 {
91         int i;
92         struct pathgroup * pgp;
93
94         if (!pgvec)
95                 return;
96
97         vector_foreach_slot(pgvec, pgp, i)
98                 free_pathgroup(pgp, free_paths);
99
100         vector_free(pgvec);
101 }
102
103 struct multipath *
104 alloc_multipath (void)
105 {
106         struct multipath * mpp;
107
108         mpp = (struct multipath *)MALLOC(sizeof(struct multipath));
109
110         if (mpp)
111                 mpp->nextpg = 1;
112
113         return mpp;
114 }
115
116 void
117 free_multipath (struct multipath * mpp, int free_paths)
118 {
119         if (!mpp)
120                 return;
121
122         if (mpp->selector &&
123             mpp->selector != conf->selector &&
124             (!mpp->mpe || (mpp->mpe && mpp->selector != mpp->mpe->selector)) &&
125             (!mpp->hwe || (mpp->hwe && mpp->selector != mpp->hwe->selector)))
126                 FREE(mpp->selector);
127
128         if (mpp->alias &&
129             (!mpp->mpe || (mpp->mpe && mpp->alias != mpp->mpe->alias)) &&
130             (mpp->wwid && mpp->alias != mpp->wwid))
131                 FREE(mpp->alias);
132
133         if (mpp->features &&
134             mpp->features != conf->features &&
135             (!mpp->hwe || (mpp->hwe && mpp->features != mpp->hwe->features)))
136                 FREE(mpp->features);
137
138         if (mpp->hwhandler &&
139             mpp->hwhandler != conf->default_hwhandler &&
140             (!mpp->hwe || (mpp->hwe && mpp->hwhandler != mpp->hwe->hwhandler)))
141                 FREE(mpp->hwhandler);
142
143         if (mpp->dmi)
144                 FREE(mpp->dmi);
145         
146         free_pathvec(mpp->paths, free_paths);
147         free_pgvec(mpp->pg, free_paths);
148         FREE(mpp);
149 }
150
151 void
152 drop_multipath (vector mpvec, char * wwid, int free_paths)
153 {
154         int i;
155         struct multipath * mpp;
156
157         if (!mpvec)
158                 return;
159
160         vector_foreach_slot (mpvec, mpp, i) {
161                 if (!strncmp(mpp->wwid, wwid, WWID_SIZE)) {
162                         free_multipath(mpp, free_paths);
163                         vector_del_slot(mpvec, i);
164                         return;
165                 }
166         }
167 }
168
169 void
170 free_multipathvec (vector mpvec, int free_paths)
171 {
172         int i;
173         struct multipath * mpp;
174
175         if (!mpvec)
176                 return;
177
178         vector_foreach_slot (mpvec, mpp, i)
179                 free_multipath(mpp, free_paths);
180
181         vector_free(mpvec);
182 }
183
184 int
185 store_path (vector pathvec, struct path * pp)
186 {
187         if (!vector_alloc_slot(pathvec))
188                 return 1;
189
190         vector_set_slot(pathvec, pp);
191
192         return 0;
193 }
194
195 int
196 store_pathgroup (vector pgvec, struct pathgroup * pgp)
197 {
198         if (!vector_alloc_slot(pgvec))
199                 return 1;
200
201         vector_set_slot(pgvec, pgp);
202
203         return 0;
204 }
205
206 struct multipath *
207 find_mp_by_minor (vector mp, int minor)
208 {
209         int i;
210         struct multipath * mpp;
211         
212         if (!mpp->dmi)
213                 return NULL;
214
215         vector_foreach_slot (mp, mpp, i)
216                 if (mpp->dmi->minor == minor)
217                         return mpp;
218
219         return NULL;
220 }
221
222 struct multipath *
223 find_mp_by_wwid (vector mp, char * wwid)
224 {
225         int i;
226         struct multipath * mpp;
227         
228         vector_foreach_slot (mp, mpp, i)
229                 if (!strncmp(mpp->wwid, wwid, WWID_SIZE))
230                         return mpp;
231
232         return NULL;
233 }
234
235 struct multipath *
236 find_mp_by_alias (vector mp, char * alias)
237 {
238         int i;
239         int len;
240         struct multipath * mpp;
241         
242         len = strlen(alias);
243
244         if (!len)
245                 return NULL;
246         
247         vector_foreach_slot (mp, mpp, i) {
248                 if (strlen(mpp->alias) == len &&
249                     !strncmp(mpp->alias, alias, len))
250                         return mpp;
251         }
252         return NULL;
253 }
254
255 struct path *
256 find_path_by_dev (vector pathvec, char * dev)
257 {
258         int i;
259         struct path * pp;
260         
261         vector_foreach_slot (pathvec, pp, i)
262                 if (!strcmp_chomp(pp->dev, dev))
263                         return pp;
264
265         condlog(3, "path %s not found in pathvec\n", dev);
266         return NULL;
267 }
268
269 struct path *
270 find_path_by_devt (vector pathvec, char * dev_t)
271 {
272         int i;
273         struct path * pp;
274
275         vector_foreach_slot (pathvec, pp, i)
276                 if (!strcmp_chomp(pp->dev_t, dev_t))
277                         return pp;
278
279         condlog(3, "path %s not found in pathvec\n", dev_t);
280         return NULL;
281 }
282