[multipath]
[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         return (struct multipath *)MALLOC(sizeof(struct multipath));
105 }
106
107 void
108 free_multipath (struct multipath * mpp, int free_paths)
109 {
110         if (!mpp)
111                 return;
112
113         if (mpp->selector &&
114             mpp->selector != conf->default_selector &&
115             (!mpp->mpe || (mpp->mpe && mpp->selector != mpp->mpe->selector)) &&
116             (!mpp->hwe || (mpp->hwe && mpp->selector != mpp->hwe->selector)))
117                 FREE(mpp->selector);
118
119         if (mpp->alias &&
120             (!mpp->mpe || (mpp->mpe && mpp->alias != mpp->mpe->alias)) &&
121             (mpp->wwid && mpp->alias != mpp->wwid))
122                 FREE(mpp->alias);
123
124         if (mpp->features &&
125             mpp->features != conf->default_features &&
126             (!mpp->hwe || (mpp->hwe && mpp->features != mpp->hwe->features)))
127                 FREE(mpp->features);
128
129         if (mpp->hwhandler &&
130             mpp->hwhandler != conf->default_hwhandler &&
131             (!mpp->hwe || (mpp->hwe && mpp->hwhandler != mpp->hwe->hwhandler)))
132                 FREE(mpp->hwhandler);
133
134         free_pathvec(mpp->paths, free_paths);
135         free_pgvec(mpp->pg, free_paths);
136         FREE(mpp);
137 }
138
139 void
140 free_multipathvec (vector mpvec, int free_paths)
141 {
142         int i;
143         struct multipath * mpp;
144
145         if (!mpvec)
146                 return;
147
148         vector_foreach_slot (mpvec, mpp, i)
149                 free_multipath(mpp, free_paths);
150
151         vector_free(mpvec);
152 }
153
154 int
155 store_path (vector pathvec, struct path * pp)
156 {
157         if (!vector_alloc_slot(pathvec))
158                 return 1;
159
160         vector_set_slot(pathvec, pp);
161
162         return 0;
163 }
164
165 int
166 store_pathgroup (vector pgvec, struct pathgroup * pgp)
167 {
168         if (!vector_alloc_slot(pgvec))
169                 return 1;
170
171         vector_set_slot(pgvec, pgp);
172
173         return 0;
174 }
175
176 struct multipath *
177 find_mp (vector mp, char * alias)
178 {
179         int i;
180         int len;
181         struct multipath * mpp;
182         
183         len = strlen(alias);
184
185         if (!len)
186                 return NULL;
187         
188         vector_foreach_slot (mp, mpp, i) {
189                 if (strlen(mpp->alias) == len &&
190                     !strncmp(mpp->alias, alias, len))
191                         return mpp;
192         }
193         return NULL;
194 }
195
196 struct path *
197 find_path_by_dev (vector pathvec, char * dev)
198 {
199         int i;
200         struct path * pp;
201         
202         vector_foreach_slot (pathvec, pp, i)
203                 if (!strcmp_chomp(pp->dev, dev))
204                         return pp;
205
206         condlog(3, "path %s not found in pathvec\n", dev);
207         return NULL;
208 }
209
210 struct path *
211 find_path_by_devt (vector pathvec, char * dev_t)
212 {
213         int i;
214         struct path * pp;
215
216         vector_foreach_slot (pathvec, pp, i)
217                 if (!strcmp_chomp(pp->dev_t, dev_t))
218                         return pp;
219
220         condlog(3, "path %s not found in pathvec\n", dev_t);
221         return NULL;
222 }
223