[multipathd]
[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         }
24         return pp;
25 }
26
27 void
28 free_path (struct path * pp)
29 {
30         if (!pp)
31                 return;
32
33         if (pp->checker_context)
34                 FREE(pp->checker_context);
35
36         if (pp->fd > 0)
37                 close(pp->fd);
38
39         FREE(pp);
40 }
41
42 void
43 free_pathvec (vector vec, int free_paths)
44 {
45         int i;
46         struct path * pp;
47
48         if (!vec)
49                 return;
50
51         if (free_paths)
52                 vector_foreach_slot(vec, pp, i)
53                         free_path(pp);
54
55         vector_free(vec);
56 }
57
58 struct pathgroup *
59 alloc_pathgroup (void)
60 {
61         struct pathgroup * pgp;
62
63         pgp = (struct pathgroup *)MALLOC(sizeof(struct pathgroup));
64
65         if (!pgp)
66                 return NULL;
67
68         pgp->paths = vector_alloc();
69
70         if (!pgp->paths)
71                 FREE(pgp);
72
73         return pgp;
74 }
75
76 void
77 free_pathgroup (struct pathgroup * pgp, int free_paths)
78 {
79         if (!pgp)
80                 return;
81
82         free_pathvec(pgp->paths, free_paths);
83         FREE(pgp);
84 }
85
86 void
87 free_pgvec (vector pgvec, int free_paths)
88 {
89         int i;
90         struct pathgroup * pgp;
91
92         if (!pgvec)
93                 return;
94
95         vector_foreach_slot(pgvec, pgp, i)
96                 free_pathgroup(pgp, free_paths);
97
98         vector_free(pgvec);
99 }
100
101 struct multipath *
102 alloc_multipath (void)
103 {
104         struct multipath * mpp;
105
106         mpp = (struct multipath *)MALLOC(sizeof(struct multipath));
107
108         if (mpp) {
109                 mpp->pgfailback = FAILBACK_IMMEDIATE;
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->default_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->default_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 free_multipathvec (vector mpvec, 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                 free_multipath(mpp, free_paths);
158
159         vector_free(mpvec);
160 }
161
162 int
163 store_path (vector pathvec, struct path * pp)
164 {
165         if (!vector_alloc_slot(pathvec))
166                 return 1;
167
168         vector_set_slot(pathvec, pp);
169
170         return 0;
171 }
172
173 int
174 store_pathgroup (vector pgvec, struct pathgroup * pgp)
175 {
176         if (!vector_alloc_slot(pgvec))
177                 return 1;
178
179         vector_set_slot(pgvec, pgp);
180
181         return 0;
182 }
183
184 struct multipath *
185 find_mp_by_wwid (vector mp, char * wwid)
186 {
187         int i;
188         struct multipath * mpp;
189         
190         vector_foreach_slot (mp, mpp, i)
191                 if (!strncmp(mpp->wwid, wwid, WWID_SIZE))
192                         return mpp;
193
194         return NULL;
195 }
196
197 struct multipath *
198 find_mp (vector mp, char * alias)
199 {
200         int i;
201         int len;
202         struct multipath * mpp;
203         
204         len = strlen(alias);
205
206         if (!len)
207                 return NULL;
208         
209         vector_foreach_slot (mp, mpp, i) {
210                 if (strlen(mpp->alias) == len &&
211                     !strncmp(mpp->alias, alias, len))
212                         return mpp;
213         }
214         return NULL;
215 }
216
217 struct path *
218 find_path_by_dev (vector pathvec, char * dev)
219 {
220         int i;
221         struct path * pp;
222         
223         vector_foreach_slot (pathvec, pp, i)
224                 if (!strcmp_chomp(pp->dev, dev))
225                         return pp;
226
227         condlog(3, "path %s not found in pathvec\n", dev);
228         return NULL;
229 }
230
231 struct path *
232 find_path_by_devt (vector pathvec, char * dev_t)
233 {
234         int i;
235         struct path * pp;
236
237         vector_foreach_slot (pathvec, pp, i)
238                 if (!strcmp_chomp(pp->dev_t, dev_t))
239                         return pp;
240
241         condlog(3, "path %s not found in pathvec\n", dev_t);
242         return NULL;
243 }
244