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