Merge branch 'master' of git://git.kernel.org/pub/scm/linux/storage/multipath-tools/
[platform/upstream/multipath-tools.git] / libmultipath / config.c
1 /*
2  * Copyright (c) 2004, 2005 Christophe Varoqui
3  * Copyright (c) 2005 Benjamin Marzinski, Redhat
4  * Copyright (c) 2005 Edward Goggin, EMC
5  */
6 #include <stdio.h>
7 #include <string.h>
8
9 #include "checkers.h"
10 #include "memory.h"
11 #include "util.h"
12 #include "debug.h"
13 #include "parser.h"
14 #include "dict.h"
15 #include "hwtable.h"
16 #include "vector.h"
17 #include "structs.h"
18 #include "config.h"
19 #include "blacklist.h"
20 #include "defaults.h"
21 #include "prio.h"
22
23 static int
24 hwe_strmatch (struct hwentry *hwe1, struct hwentry *hwe2)
25 {
26         if (hwe1->vendor && hwe2->vendor && strcmp(hwe1->vendor, hwe2->vendor))
27                 return 1;
28
29         if (hwe1->product && hwe2->product && strcmp(hwe1->product, hwe2->product))
30                 return 1;
31
32         if (hwe1->revision && hwe2->revision && strcmp(hwe1->revision, hwe2->revision))
33                 return 1;
34
35         return 0;
36 }
37
38 static struct hwentry *
39 find_hwe_strmatch (vector hwtable, struct hwentry *hwe)
40 {
41         int i;
42         struct hwentry *tmp, *ret = NULL;
43
44         vector_foreach_slot (hwtable, tmp, i) {
45                 if (hwe_strmatch(tmp, hwe))
46                         continue;
47                 ret = tmp;
48                 break;
49         }
50         return ret;
51 }
52
53 struct hwentry *
54 find_hwe (vector hwtable, char * vendor, char * product, char * revision)
55 {
56         int i;
57         struct hwentry *hwe, *ret = NULL;
58         regex_t vre, pre, rre;
59
60         vector_foreach_slot (hwtable, hwe, i) {
61                 if (hwe->vendor &&
62                     regcomp(&vre, hwe->vendor, REG_EXTENDED|REG_NOSUB))
63                         break;
64                 if (hwe->product &&
65                     regcomp(&pre, hwe->product, REG_EXTENDED|REG_NOSUB)) {
66                         regfree(&vre);
67                         break;
68                 }
69                 if (hwe->revision &&
70                     regcomp(&rre, hwe->revision, REG_EXTENDED|REG_NOSUB)) {
71                         regfree(&vre);
72                         regfree(&pre);
73                         break;
74                 }
75                 if ((!hwe->vendor || !regexec(&vre, vendor, 0, NULL, 0)) &&
76                     (!hwe->product || !regexec(&pre, product, 0, NULL, 0)) &&
77                     (!hwe->revision || !regexec(&rre, revision, 0, NULL, 0)))
78                         ret = hwe;
79
80                 if (hwe->revision)
81                         regfree(&rre);
82                 if (hwe->product)
83                         regfree(&pre);
84                 if (hwe->vendor)
85                         regfree(&vre);
86
87                 if (ret)
88                         break;
89         }
90         return ret;
91 }
92
93 extern struct mpentry *
94 find_mpe (char * wwid)
95 {
96         int i;
97         struct mpentry * mpe;
98
99         if (!wwid)
100                 return NULL;
101
102         vector_foreach_slot (conf->mptable, mpe, i)
103                 if (mpe->wwid && !strcmp(mpe->wwid, wwid))
104                         return mpe;
105
106         return NULL;
107 }
108
109 extern char *
110 get_mpe_wwid (char * alias)
111 {
112         int i;
113         struct mpentry * mpe;
114
115         if (!alias)
116                 return NULL;
117
118         vector_foreach_slot (conf->mptable, mpe, i)
119                 if (mpe->alias && strcmp(mpe->alias, alias) == 0)
120                         return mpe->wwid;
121
122         return NULL;
123 }
124
125 void
126 free_hwe (struct hwentry * hwe)
127 {
128         if (!hwe)
129                 return;
130
131         if (hwe->vendor)
132                 FREE(hwe->vendor);
133
134         if (hwe->product)
135                 FREE(hwe->product);
136
137         if (hwe->revision)
138                 FREE(hwe->revision);
139
140         if (hwe->getuid)
141                 FREE(hwe->getuid);
142
143         if (hwe->features)
144                 FREE(hwe->features);
145
146         if (hwe->hwhandler)
147                 FREE(hwe->hwhandler);
148
149         if (hwe->selector)
150                 FREE(hwe->selector);
151
152         if (hwe->checker_name)
153                 FREE(hwe->checker_name);
154
155         if (hwe->prio_name)
156                 FREE(hwe->prio_name);
157
158         if (hwe->bl_product)
159                 FREE(hwe->bl_product);
160
161         FREE(hwe);
162 }
163
164 void
165 free_hwtable (vector hwtable)
166 {
167         int i;
168         struct hwentry * hwe;
169
170         if (!hwtable)
171                 return;
172
173         vector_foreach_slot (hwtable, hwe, i)
174                 free_hwe(hwe);
175
176         vector_free(hwtable);
177 }
178
179 void
180 free_mpe (struct mpentry * mpe)
181 {
182         if (!mpe)
183                 return;
184
185         if (mpe->wwid)
186                 FREE(mpe->wwid);
187
188         if (mpe->selector)
189                 FREE(mpe->selector);
190
191         if (mpe->getuid)
192                 FREE(mpe->getuid);
193
194         if (mpe->alias)
195                 FREE(mpe->alias);
196
197         FREE(mpe);
198 }
199
200 void
201 free_mptable (vector mptable)
202 {
203         int i;
204         struct mpentry * mpe;
205
206         if (!mptable)
207                 return;
208
209         vector_foreach_slot (mptable, mpe, i)
210                 free_mpe(mpe);
211
212         vector_free(mptable);
213 }
214
215 struct mpentry *
216 alloc_mpe (void)
217 {
218         struct mpentry * mpe = (struct mpentry *)
219                                 MALLOC(sizeof(struct mpentry));
220
221         return mpe;
222 }
223
224 struct hwentry *
225 alloc_hwe (void)
226 {
227         struct hwentry * hwe = (struct hwentry *)
228                                 MALLOC(sizeof(struct hwentry));
229
230         return hwe;
231 }
232
233 static char *
234 set_param_str(char * str)
235 {
236         char * dst;
237         int len;
238
239         if (!str)
240                 return NULL;
241
242         len = strlen(str);
243
244         if (!len)
245                 return NULL;
246
247         dst = (char *)MALLOC(len + 1);
248
249         if (!dst)
250                 return NULL;
251
252         strcpy(dst, str);
253         return dst;
254 }
255
256 #define merge_str(s) \
257         if (hwe2->s) { \
258                 if (hwe1->s) \
259                         FREE(hwe1->s); \
260                 if (!(hwe1->s = set_param_str(hwe2->s))) \
261                         return 1; \
262         }
263
264 #define merge_num(s) \
265         if (hwe2->s) \
266                 hwe1->s = hwe2->s
267
268
269 static int
270 merge_hwe (struct hwentry * hwe1, struct hwentry * hwe2)
271 {
272         merge_str(vendor);
273         merge_str(product);
274         merge_str(revision);
275         merge_str(getuid);
276         merge_str(features);
277         merge_str(hwhandler);
278         merge_str(selector);
279         merge_str(checker_name);
280         merge_str(prio_name);
281         merge_str(bl_product);
282         merge_num(pgpolicy);
283         merge_num(pgfailback);
284         merge_num(rr_weight);
285         merge_num(no_path_retry);
286         merge_num(minio);
287
288         return 0;
289 }
290
291 int
292 store_hwe (vector hwtable, struct hwentry * dhwe)
293 {
294         struct hwentry * hwe;
295
296         if (find_hwe_strmatch(hwtable, dhwe))
297                 return 0;
298
299         if (!(hwe = alloc_hwe()))
300                 return 1;
301
302         if (!dhwe->vendor || !(hwe->vendor = set_param_str(dhwe->vendor)))
303                 goto out;
304
305         if (!dhwe->product || !(hwe->product = set_param_str(dhwe->product)))
306                 goto out;
307
308         if (dhwe->revision && !(hwe->revision = set_param_str(dhwe->revision)))
309                 goto out;
310
311         if (dhwe->getuid && !(hwe->getuid = set_param_str(dhwe->getuid)))
312                 goto out;
313
314         if (dhwe->features && !(hwe->features = set_param_str(dhwe->features)))
315                 goto out;
316
317         if (dhwe->hwhandler && !(hwe->hwhandler = set_param_str(dhwe->hwhandler)))
318                 goto out;
319
320         if (dhwe->selector && !(hwe->selector = set_param_str(dhwe->selector)))
321                 goto out;
322
323         if (dhwe->checker_name && !(hwe->checker_name = set_param_str(dhwe->checker_name)))
324                 goto out;
325                                 
326         if (dhwe->prio_name && !(hwe->prio_name = set_param_str(dhwe->prio_name)))
327                 goto out;
328                                 
329         hwe->pgpolicy = dhwe->pgpolicy;
330         hwe->pgfailback = dhwe->pgfailback;
331         hwe->rr_weight = dhwe->rr_weight;
332         hwe->no_path_retry = dhwe->no_path_retry;
333         hwe->minio = dhwe->minio;
334
335         if (dhwe->bl_product && !(hwe->bl_product = set_param_str(dhwe->bl_product)))
336                 goto out;
337
338         if (!vector_alloc_slot(hwtable))
339                 goto out;
340
341         vector_set_slot(hwtable, hwe);
342         return 0;
343 out:
344         free_hwe(hwe);
345         return 1;
346 }
347
348 static int
349 factorize_hwtable (vector hw)
350 {
351         struct hwentry *hwe1, *hwe2;
352         int i, j;
353
354         vector_foreach_slot(hw, hwe1, i) {
355                 j = i+1;
356                 vector_foreach_slot_after(hw, hwe2, j) {
357                         if (hwe_strmatch(hwe1, hwe2))
358                                 continue;
359                         /* dup */
360                         merge_hwe(hwe1, hwe2);
361                         free_hwe(hwe2);
362                         vector_del_slot(hw, j);
363                         j--;
364                 }
365         }
366         return 0;
367 }
368
369 struct config *
370 alloc_config (void)
371 {
372         return (struct config *)MALLOC(sizeof(struct config));
373 }
374
375 void
376 free_config (struct config * conf)
377 {
378         if (!conf)
379                 return;
380
381         if (conf->dev)
382                 FREE(conf->dev);
383
384         if (conf->udev_dir)
385                 FREE(conf->udev_dir);
386
387         if (conf->multipath_dir)
388                 FREE(conf->multipath_dir);
389
390         if (conf->selector)
391                 FREE(conf->selector);
392
393         if (conf->getuid)
394                 FREE(conf->getuid);
395
396         if (conf->features)
397                 FREE(conf->features);
398
399         if (conf->hwhandler)
400                 FREE(conf->hwhandler);
401
402         if (conf->prio_name)
403                 FREE(conf->prio_name);
404
405         if (conf->checker_name)
406                 FREE(conf->checker_name);
407
408         free_blacklist(conf->blist_devnode);
409         free_blacklist(conf->blist_wwid);
410         free_blacklist_device(conf->blist_device);
411
412         free_blacklist(conf->elist_devnode);
413         free_blacklist(conf->elist_wwid);
414         free_blacklist_device(conf->elist_device);
415
416         free_mptable(conf->mptable);
417         free_hwtable(conf->hwtable);
418         free_keywords(conf->keywords);
419         FREE(conf);
420 }
421
422 int
423 load_config (char * file)
424 {
425         if (!conf)
426                 conf = alloc_config();
427
428         if (!conf)
429                 return 1;
430
431         /*
432          * internal defaults
433          */
434         if (!conf->verbosity)
435                 conf->verbosity = DEFAULT_VERBOSITY;
436
437         conf->dev_type = DEV_NONE;
438         conf->minio = 1000;
439         conf->max_fds = 0;
440         conf->bindings_file = DEFAULT_BINDINGS_FILE;
441         conf->multipath_dir = set_default(DEFAULT_MULTIPATHDIR);
442         conf->flush_on_last_del = 0;
443         conf->attribute_flags = 0;
444
445         /*
446          * preload default hwtable
447          */
448         if (conf->hwtable == NULL) {
449                 conf->hwtable = vector_alloc();
450
451                 if (!conf->hwtable)
452                         goto out;
453         }
454         if (setup_default_hwtable(conf->hwtable))
455                 goto out;
456
457         /*
458          * read the config file
459          */
460         if (filepresent(file)) {
461                 set_current_keywords(&conf->keywords);
462                 if (init_data(file, init_keywords)) {
463                         condlog(0, "error parsing config file");
464                         goto out;
465                 }
466         }
467
468         /*
469          * remove duplica in hwtable. config file takes precedence
470          * over build-in hwtable
471          */
472         factorize_hwtable(conf->hwtable);
473
474         /*
475          * fill the voids left in the config file
476          */
477         if (conf->blist_devnode == NULL) {
478                 conf->blist_devnode = vector_alloc();
479
480                 if (!conf->blist_devnode)
481                         goto out;
482         }
483         if (conf->blist_wwid == NULL) {
484                 conf->blist_wwid = vector_alloc();
485
486                 if (!conf->blist_wwid)
487                         goto out;
488         }
489         if (conf->blist_device == NULL) {
490                 conf->blist_device = vector_alloc();
491
492                 if (!conf->blist_device)
493                         goto out;
494         }
495         if (setup_default_blist(conf))
496                 goto out;
497
498         if (conf->elist_devnode == NULL) {
499                 conf->elist_devnode = vector_alloc();
500
501                 if (!conf->elist_devnode)
502                         goto out;
503         }
504         if (conf->elist_wwid == NULL) {
505                 conf->elist_wwid = vector_alloc();
506
507                 if (!conf->elist_wwid)
508                         goto out;
509         }
510
511         if (conf->elist_device == NULL) {
512                 conf->elist_device = vector_alloc();
513
514                 if (!conf->elist_device)
515                         goto out;
516         }
517
518         if (conf->mptable == NULL) {
519                 conf->mptable = vector_alloc();
520                 if (!conf->mptable)
521                         goto out;
522         }
523         if (conf->selector == NULL)
524                 conf->selector = set_default(DEFAULT_SELECTOR);
525
526         if (conf->udev_dir == NULL)
527                 conf->udev_dir = set_default(DEFAULT_UDEVDIR);
528
529         if (conf->getuid == NULL)
530                 conf->getuid = set_default(DEFAULT_GETUID);
531
532         if (conf->features == NULL)
533                 conf->features = set_default(DEFAULT_FEATURES);
534
535         if (conf->hwhandler == NULL)
536                 conf->hwhandler = set_default(DEFAULT_HWHANDLER);
537
538         if (!conf->selector  || !conf->udev_dir || !conf->multipath_dir ||
539             !conf->getuid    || !conf->features ||
540             !conf->hwhandler)
541                 goto out;
542
543         if (!conf->prio_name)
544                 conf->prio_name = set_default(DEFAULT_PRIO);
545
546         if (!conf->checker_name)
547                 conf->checker_name = set_default(DEFAULT_CHECKER);
548
549         return 0;
550 out:
551         free_config(conf);
552         return 1;
553 }
554