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