Initial git import.
[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         return (struct path *)MALLOC(sizeof(struct path));
15 }
16
17 void
18 free_path (struct path * pp)
19 {
20         if (!pp)
21                 return;
22
23         if (pp->checker_context)
24                 FREE(pp->checker_context);
25
26         if (pp->fd > 0)
27                 close(pp->fd);
28
29         FREE(pp);
30 }
31
32 void
33 free_pathvec (vector vec, int free_paths)
34 {
35         int i;
36         struct path * pp;
37
38         if (!vec)
39                 return;
40
41         if (free_paths)
42                 vector_foreach_slot(vec, pp, i)
43                         free_path(pp);
44
45         vector_free(vec);
46 }
47
48 struct pathgroup *
49 alloc_pathgroup (void)
50 {
51         struct pathgroup * pgp;
52
53         pgp = (struct pathgroup *)MALLOC(sizeof(struct pathgroup));
54
55         if (!pgp)
56                 return NULL;
57
58         pgp->paths = vector_alloc();
59
60         if (!pgp->paths)
61                 FREE(pgp);
62
63         return pgp;
64 }
65
66 void
67 free_pathgroup (struct pathgroup * pgp, int free_paths)
68 {
69         if (!pgp)
70                 return;
71
72         free_pathvec(pgp->paths, free_paths);
73         FREE(pgp);
74 }
75
76 void
77 free_pgvec (vector pgvec, int free_paths)
78 {
79         int i;
80         struct pathgroup * pgp;
81
82         if (!pgvec)
83                 return;
84
85         vector_foreach_slot(pgvec, pgp, i)
86                 free_pathgroup(pgp, free_paths);
87
88         vector_free(pgvec);
89 }
90
91 struct multipath *
92 alloc_multipath (void)
93 {
94         return (struct multipath *)MALLOC(sizeof(struct multipath));
95 }
96
97 void
98 free_multipath (struct multipath * mpp, int free_paths)
99 {
100         if (!mpp)
101                 return;
102
103         if (mpp->selector &&
104             mpp->selector != conf->default_selector &&
105             (!mpp->mpe || (mpp->mpe && mpp->selector != mpp->mpe->selector)) &&
106             (!mpp->hwe || (mpp->hwe && mpp->selector != mpp->hwe->selector)))
107                 FREE(mpp->selector);
108
109         if (mpp->alias &&
110             (!mpp->mpe || (mpp->mpe && mpp->alias != mpp->mpe->alias)) &&
111             (mpp->wwid && mpp->alias != mpp->wwid))
112                 FREE(mpp->alias);
113
114         if (mpp->features &&
115             mpp->features != conf->default_features &&
116             (!mpp->hwe || (mpp->hwe && mpp->features != mpp->hwe->features)))
117                 FREE(mpp->features);
118
119         if (mpp->hwhandler &&
120             mpp->hwhandler != conf->default_hwhandler &&
121             (!mpp->hwe || (mpp->hwe && mpp->hwhandler != mpp->hwe->hwhandler)))
122                 FREE(mpp->hwhandler);
123
124         free_pathvec(mpp->paths, free_paths);
125         free_pgvec(mpp->pg, free_paths);
126         FREE(mpp);
127 }
128
129 void
130 free_multipathvec (vector mpvec, int free_paths)
131 {
132         int i;
133         struct multipath * mpp;
134
135         if (!mpvec)
136                 return;
137
138         vector_foreach_slot (mpvec, mpp, i)
139                 free_multipath(mpp, free_paths);
140
141         vector_free(mpvec);
142 }
143
144 int
145 store_path (vector pathvec, struct path * pp)
146 {
147         if (!vector_alloc_slot(pathvec))
148                 return 1;
149
150         vector_set_slot(pathvec, pp);
151
152         return 0;
153 }
154
155 int
156 store_pathgroup (vector pgvec, struct pathgroup * pgp)
157 {
158         if (!vector_alloc_slot(pgvec))
159                 return 1;
160
161         vector_set_slot(pgvec, pgp);
162
163         return 0;
164 }
165
166 struct multipath *
167 find_mp (vector mp, char * alias)
168 {
169         int i;
170         int len;
171         struct multipath * mpp;
172         
173         len = strlen(alias);
174
175         if (!len)
176                 return NULL;
177         
178         vector_foreach_slot (mp, mpp, i) {
179                 if (strlen(mpp->alias) == len &&
180                     !strncmp(mpp->alias, alias, len))
181                         return mpp;
182         }
183         return NULL;
184 }
185
186 struct path *
187 find_path_by_dev (vector pathvec, char * dev)
188 {
189         int i;
190         struct path * pp;
191         
192         vector_foreach_slot (pathvec, pp, i)
193                 if (!strcmp_chomp(pp->dev, dev))
194                         return pp;
195
196         condlog(3, "path %s not found in pathvec\n", dev);
197         return NULL;
198 }
199
200 struct path *
201 find_path_by_devt (vector pathvec, char * dev_t)
202 {
203         int i;
204         struct path * pp;
205
206         vector_foreach_slot (pathvec, pp, i)
207                 if (!strcmp_chomp(pp->dev_t, dev_t))
208                         return pp;
209
210         condlog(3, "path %s not found in pathvec\n", dev_t);
211         return NULL;
212 }
213