[libmultipath] load default hwtable entries not overriden by configlets
[platform/upstream/multipath-tools.git] / libmultipath / config.c
1 #include <stdio.h>
2 #include <string.h>
3
4 #include "memory.h"
5 #include "util.h"
6 #include "debug.h"
7 #include "parser.h"
8 #include "dict.h"
9 #include "hwtable.h"
10 #include "vector.h"
11 #include "blacklist.h"
12 #include "defaults.h"
13 #include "config.h"
14
15 #include "../libcheckers/checkers.h"
16
17 struct hwentry *
18 find_hwe (vector hwtable, char * vendor, char * product)
19 {
20         int i;
21         struct hwentry * hwe;
22
23         vector_foreach_slot (hwtable, hwe, i) {
24                 if (strcmp_chomp(hwe->vendor, vendor) == 0 &&
25                     (hwe->product[0] == '*' ||
26                     strcmp_chomp(hwe->product, product) == 0))
27                         return hwe;
28         }
29         return NULL;
30 }
31
32 extern struct mpentry *
33 find_mpe (char * wwid)
34 {
35         int i;
36         struct mpentry * mpe;
37
38         if (!wwid)
39                 return NULL;
40
41         vector_foreach_slot (conf->mptable, mpe, i)
42                 if (mpe->wwid && !strcmp(mpe->wwid, wwid))
43                         return mpe;
44
45         return NULL;
46 }
47
48 extern char *
49 get_mpe_wwid (char * alias)
50 {
51         int i;
52         struct mpentry * mpe;
53
54         if (!alias)
55                 return NULL;
56
57         vector_foreach_slot (conf->mptable, mpe, i)
58                 if (mpe->alias && strcmp(mpe->alias, alias) == 0)
59                         return mpe->wwid;
60
61         return NULL;
62 }
63
64 void
65 free_hwe (struct hwentry * hwe)
66 {
67         if (!hwe)
68                 return;
69
70         if (hwe->vendor)
71                 FREE(hwe->vendor);
72
73         if (hwe->product)
74                 FREE(hwe->product);
75
76         if (hwe->selector)
77                 FREE(hwe->selector);
78
79         if (hwe->getuid)
80                 FREE(hwe->getuid);
81
82         if (hwe->getprio)
83                 FREE(hwe->getprio);
84
85         if (hwe->features)
86                 FREE(hwe->features);
87
88         if (hwe->hwhandler)
89                 FREE(hwe->hwhandler);
90
91         FREE(hwe);
92 }
93
94 void
95 free_hwtable (vector hwtable)
96 {
97         int i;
98         struct hwentry * hwe;
99
100         if (!hwtable)
101                 return;
102
103         vector_foreach_slot (hwtable, hwe, i)
104                 free_hwe(hwe);
105
106         vector_free(hwtable);
107 }
108
109 void
110 free_mpe (struct mpentry * mpe)
111 {
112         if (!mpe)
113                 return;
114
115         if (mpe->wwid)
116                 FREE(mpe->wwid);
117
118         if (mpe->selector)
119                 FREE(mpe->selector);
120
121         if (mpe->getuid)
122                 FREE(mpe->getuid);
123
124         if (mpe->alias)
125                 FREE(mpe->alias);
126
127         FREE(mpe);
128 }
129
130 void
131 free_mptable (vector mptable)
132 {
133         int i;
134         struct mpentry * mpe;
135
136         if (!mptable)
137                 return;
138
139         vector_foreach_slot (mptable, mpe, i)
140                 free_mpe(mpe);
141
142         vector_free(mptable);
143 }
144
145 struct mpentry *
146 alloc_mpe (void)
147 {
148         struct mpentry * mpe = (struct mpentry *)
149                                 MALLOC(sizeof(struct mpentry));
150
151         return mpe;
152 }
153
154 static struct hwentry *
155 alloc_hwe (void)
156 {
157         struct hwentry * hwe = (struct hwentry *)
158                                 MALLOC(sizeof(struct hwentry));
159
160         return hwe;
161 }
162
163 static char *
164 set_param_str(char * str)
165 {
166         char * dst;
167         int len;
168
169         if (!str)
170                 return NULL;
171
172         len = strlen(str);
173
174         if (!len)
175                 return NULL;
176
177         dst = (char *)MALLOC(len + 1);
178
179         if (!dst)
180                 return NULL;
181
182         strcpy(dst, str);
183         return dst;
184 }
185
186 static int
187 dup_hwe (vector hwtable, char * vendor, char * product)
188 {
189         struct hwentry * hwe = find_hwe(hwtable, vendor, product);
190
191         if (hwe)
192                 return 1;
193
194         return 0;
195 }
196
197 int
198 store_hwe (vector hwtable, char * vendor, char * product, int pgp,
199            char * getuid)
200 {
201         struct hwentry * hwe;
202
203         if (dup_hwe(hwtable, vendor, product))
204                 return 0;
205
206         hwe = alloc_hwe();
207
208         if (!hwe)
209                 return 1;
210
211         hwe->vendor = set_param_str(vendor);
212
213         if (!hwe->vendor)
214                 goto out;
215         
216         hwe->product = set_param_str(product);
217
218         if (!hwe->product)
219                 goto out;
220         
221         if (pgp)
222                 hwe->pgpolicy = pgp;
223
224         if (getuid)
225                 hwe->getuid = set_param_str(getuid);
226         else
227                 hwe->getuid = set_default(DEFAULT_GETUID);
228
229         if (!hwe->getuid)
230                 goto out;
231         
232         if (!vector_alloc_slot(hwtable))
233                 goto out;
234
235         vector_set_slot(hwtable, hwe);
236         return 0;
237 out:
238         free_hwe(hwe);
239         return 1;
240 }
241
242 int
243 store_hwe_ext (vector hwtable, char * vendor, char * product, int pgp,
244            char * getuid, char * getprio, char * hwhandler,
245            char * features, char * checker, int pgfailback)
246 {
247         struct hwentry * hwe;
248
249         if (dup_hwe(hwtable, vendor, product))
250                 return 0;
251         
252         hwe = alloc_hwe();
253
254         if (!hwe)
255                 return 1;
256
257         hwe->vendor = set_param_str(vendor);
258
259         if (!hwe->vendor)
260                 goto out;
261         
262         hwe->product = set_param_str(product);
263
264         if (!hwe->product)
265                 goto out;
266         
267         if (pgp)
268                 hwe->pgpolicy = pgp;
269
270         if (getuid)
271                 hwe->getuid = set_param_str(getuid);
272         else
273                 hwe->getuid = set_default(DEFAULT_GETUID);
274
275         if (!hwe->getuid)
276                 goto out;
277         
278         if (getprio)
279                 hwe->getprio = set_param_str(getprio);
280         else
281                 hwe->getprio = NULL;
282
283         if (hwhandler)  
284                 hwe->hwhandler = set_param_str(hwhandler);
285         else
286                 hwe->hwhandler = set_default(DEFAULT_HWHANDLER);
287
288         if (!hwe->hwhandler)
289                 goto out;
290
291         if (features)
292                 hwe->features = set_param_str(features);
293         else
294                 hwe->features = set_default(DEFAULT_FEATURES);
295
296         if (!hwe->features)
297                 goto out;
298
299         if (checker)
300                 hwe->checker_index = get_checker_id(checker);
301         else
302                 hwe->checker_index = get_checker_id(DEFAULT_CHECKER);
303
304         hwe->pgfailback = pgfailback;
305
306         if (!vector_alloc_slot(hwtable))
307                 goto out;
308
309         vector_set_slot(hwtable, hwe);
310         return 0;
311 out:
312         free_hwe(hwe);
313         return 1;
314 }
315
316 struct config *
317 alloc_config (void)
318 {
319         return (struct config *)MALLOC(sizeof(struct config));
320 }
321
322 void
323 free_config (struct config * conf)
324 {
325         if (!conf)
326                 return;
327
328         if (conf->dev)
329                 FREE(conf->dev);
330
331         if (conf->udev_dir)
332                 FREE(conf->udev_dir);
333
334         if (conf->default_selector)
335                 FREE(conf->default_selector);
336
337         if (conf->default_getuid)
338                 FREE(conf->default_getuid);
339
340         if (conf->default_getprio)
341                 FREE(conf->default_getprio);
342
343         if (conf->default_features)
344                 FREE(conf->default_features);
345
346         if (conf->default_hwhandler)
347                 FREE(conf->default_hwhandler);
348
349         free_blacklist(conf->blist);
350         free_mptable(conf->mptable);
351         free_hwtable(conf->hwtable);
352
353         FREE(conf);
354 }
355
356 int
357 load_config (char * file)
358 {
359         if (!conf)
360                 conf = alloc_config();
361
362         if (!conf)
363                 return 1;
364
365         /*
366          * internal defaults
367          */
368         if (!conf->verbosity)
369                 conf->verbosity = 2;
370
371         conf->dev_type = DEV_NONE;
372         conf->minio = 1000;
373
374         /*
375          * read the config file
376          */
377         if (filepresent(file)) {
378                 if (init_data(file, init_keywords)) {
379                         condlog(0, "error parsing config file");
380                         goto out;
381                 }
382         }
383         
384         /*
385          * fill the voids left in the config file
386          */
387         if (conf->hwtable == NULL) {
388                 conf->hwtable = vector_alloc();
389                 
390                 if (!conf->hwtable)
391                         goto out;
392                 
393         }
394         if (setup_default_hwtable(conf->hwtable))
395                 goto out;
396
397         if (conf->blist == NULL) {
398                 conf->blist = vector_alloc();
399                 
400                 if (!conf->blist)
401                         goto out;
402                 
403                 if (setup_default_blist(conf->blist))
404                         goto out;
405         }
406         if (conf->mptable == NULL) {
407                 conf->mptable = vector_alloc();
408
409                 if (!conf->mptable)
410                         goto out;
411         }
412         if (conf->default_selector == NULL)
413                 conf->default_selector = set_default(DEFAULT_SELECTOR);
414
415         if (conf->udev_dir == NULL)
416                 conf->udev_dir = set_default(DEFAULT_UDEVDIR);
417
418         if (conf->default_getuid == NULL)
419                 conf->default_getuid = set_default(DEFAULT_GETUID);
420
421         if (conf->default_features == NULL)
422                 conf->default_features = set_default(DEFAULT_FEATURES);
423
424         if (conf->default_hwhandler == NULL)
425                 conf->default_hwhandler = set_default(DEFAULT_HWHANDLER);
426
427         if (!conf->default_selector  || !conf->udev_dir         ||
428             !conf->default_getuid    || !conf->default_features ||
429             !conf->default_hwhandler)
430                 goto out;
431
432         if (!conf->default_checker_index)
433                 conf->default_checker_index = READSECTOR0;
434
435         return 0;
436 out:
437         free_config(conf);
438         return 1;
439 }
440