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