multipath: add '-t' option to dump internal hwtable
[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 #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->bindings_file)
445                 FREE(conf->bindings_file);
446
447         if (conf->prio_name)
448                 FREE(conf->prio_name);
449
450         if (conf->alias_prefix)
451                 FREE(conf->alias_prefix);
452
453         if (conf->prio_args)
454                 FREE(conf->prio_args);
455
456         if (conf->checker_name)
457                 FREE(conf->checker_name);
458
459         free_blacklist(conf->blist_devnode);
460         free_blacklist(conf->blist_wwid);
461         free_blacklist_device(conf->blist_device);
462
463         free_blacklist(conf->elist_devnode);
464         free_blacklist(conf->elist_wwid);
465         free_blacklist_device(conf->elist_device);
466
467         free_mptable(conf->mptable);
468         free_hwtable(conf->hwtable);
469         free_keywords(conf->keywords);
470         FREE(conf);
471 }
472
473 int
474 load_config (char * file)
475 {
476         if (!conf)
477                 conf = alloc_config();
478
479         if (!conf)
480                 return 1;
481
482         /*
483          * internal defaults
484          */
485         if (!conf->verbosity)
486                 conf->verbosity = DEFAULT_VERBOSITY;
487
488         conf->dmrq = dm_drv_get_rq();
489         conf->dev_type = DEV_NONE;
490         conf->minio = DEFAULT_MINIO;
491         conf->minio_rq = DEFAULT_MINIO_RQ;
492         conf->max_fds = 0;
493         conf->bindings_file = set_default(DEFAULT_BINDINGS_FILE);
494         conf->bindings_read_only = 0;
495         conf->multipath_dir = set_default(DEFAULT_MULTIPATHDIR);
496         conf->flush_on_last_del = 0;
497         conf->attribute_flags = 0;
498         conf->reassign_maps = DEFAULT_REASSIGN_MAPS;
499
500         /*
501          * preload default hwtable
502          */
503         if (conf->hwtable == NULL) {
504                 conf->hwtable = vector_alloc();
505
506                 if (!conf->hwtable)
507                         goto out;
508         }
509         if (setup_default_hwtable(conf->hwtable))
510                 goto out;
511
512         /*
513          * read the config file
514          */
515         set_current_keywords(&conf->keywords);
516         alloc_keywords();
517         if (filepresent(file)) {
518                 int builtin_hwtable_size;
519
520                 builtin_hwtable_size = VECTOR_SIZE(conf->hwtable);
521                 if (init_data(file, init_keywords)) {
522                         condlog(0, "error parsing config file");
523                         goto out;
524                 }
525                 if (VECTOR_SIZE(conf->hwtable) > builtin_hwtable_size) {
526                         /*
527                          * remove duplica in hwtable. config file
528                          * takes precedence over build-in hwtable
529                          */
530                         factorize_hwtable(conf->hwtable, builtin_hwtable_size);
531                 }
532
533         } else {
534                 init_keywords();
535         }
536
537         /*
538          * fill the voids left in the config file
539          */
540         if (conf->blist_devnode == NULL) {
541                 conf->blist_devnode = vector_alloc();
542
543                 if (!conf->blist_devnode)
544                         goto out;
545         }
546         if (conf->blist_wwid == NULL) {
547                 conf->blist_wwid = vector_alloc();
548
549                 if (!conf->blist_wwid)
550                         goto out;
551         }
552         if (conf->blist_device == NULL) {
553                 conf->blist_device = vector_alloc();
554
555                 if (!conf->blist_device)
556                         goto out;
557         }
558         if (setup_default_blist(conf))
559                 goto out;
560
561         if (conf->elist_devnode == NULL) {
562                 conf->elist_devnode = vector_alloc();
563
564                 if (!conf->elist_devnode)
565                         goto out;
566         }
567         if (conf->elist_wwid == NULL) {
568                 conf->elist_wwid = vector_alloc();
569
570                 if (!conf->elist_wwid)
571                         goto out;
572         }
573
574         if (conf->elist_device == NULL) {
575                 conf->elist_device = vector_alloc();
576
577                 if (!conf->elist_device)
578                         goto out;
579         }
580
581         if (conf->mptable == NULL) {
582                 conf->mptable = vector_alloc();
583                 if (!conf->mptable)
584                         goto out;
585         }
586         if (conf->udev_dir == NULL)
587                 conf->udev_dir = set_default(DEFAULT_UDEVDIR);
588
589         if (conf->bindings_file == NULL)
590                 conf->bindings_file = set_default(DEFAULT_BINDINGS_FILE);
591
592         if (!conf->udev_dir || !conf->multipath_dir ||
593             !conf->bindings_file)
594                 goto out;
595
596         return 0;
597 out:
598         free_config(conf);
599         return 1;
600 }
601