[multipathd] Add options to multipathd to turn off queueing
[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->selector)
141                 FREE(hwe->selector);
142
143         if (hwe->getuid)
144                 FREE(hwe->getuid);
145
146         if (hwe->features)
147                 FREE(hwe->features);
148
149         if (hwe->hwhandler)
150                 FREE(hwe->hwhandler);
151
152         if (hwe->bl_product)
153                 FREE(hwe->bl_product);
154
155         if (hwe->prio_name)
156                 FREE(hwe->prio_name);
157
158         if (hwe->checker_name)
159                 FREE(hwe->checker_name);
160         FREE(hwe);
161 }
162
163 void
164 free_hwtable (vector hwtable)
165 {
166         int i;
167         struct hwentry * hwe;
168
169         if (!hwtable)
170                 return;
171
172         vector_foreach_slot (hwtable, hwe, i)
173                 free_hwe(hwe);
174
175         vector_free(hwtable);
176 }
177
178 void
179 free_mpe (struct mpentry * mpe)
180 {
181         if (!mpe)
182                 return;
183
184         if (mpe->wwid)
185                 FREE(mpe->wwid);
186
187         if (mpe->selector)
188                 FREE(mpe->selector);
189
190         if (mpe->getuid)
191                 FREE(mpe->getuid);
192
193         if (mpe->alias)
194                 FREE(mpe->alias);
195
196         FREE(mpe);
197 }
198
199 void
200 free_mptable (vector mptable)
201 {
202         int i;
203         struct mpentry * mpe;
204
205         if (!mptable)
206                 return;
207
208         vector_foreach_slot (mptable, mpe, i)
209                 free_mpe(mpe);
210
211         vector_free(mptable);
212 }
213
214 struct mpentry *
215 alloc_mpe (void)
216 {
217         struct mpentry * mpe = (struct mpentry *)
218                                 MALLOC(sizeof(struct mpentry));
219
220         return mpe;
221 }
222
223 static struct hwentry *
224 alloc_hwe (void)
225 {
226         struct hwentry * hwe = (struct hwentry *)
227                                 MALLOC(sizeof(struct hwentry));
228
229         return hwe;
230 }
231
232 static char *
233 set_param_str(char * str)
234 {
235         char * dst;
236         int len;
237
238         if (!str)
239                 return NULL;
240
241         len = strlen(str);
242
243         if (!len)
244                 return NULL;
245
246         dst = (char *)MALLOC(len + 1);
247
248         if (!dst)
249                 return NULL;
250
251         strcpy(dst, str);
252         return dst;
253 }
254
255 #define merge_str(s) \
256         if (hwe2->s) { \
257                 if (hwe1->s) \
258                         FREE(hwe1->s); \
259                 if (!(hwe1->s = set_param_str(hwe2->s))) \
260                         return 1; \
261         }
262
263 #define merge_num(s) \
264         if (hwe2->s) \
265                 hwe1->s = hwe2->s
266
267
268 static int
269 merge_hwe (struct hwentry * hwe1, struct hwentry * hwe2)
270 {
271         merge_str(vendor);
272         merge_str(product);
273         merge_str(revision);
274         merge_str(getuid);
275         merge_str(features);
276         merge_str(hwhandler);
277         merge_str(selector);
278         merge_str(checker_name);
279         merge_str(prio_name);
280         merge_str(bl_product);
281         merge_num(pgpolicy);
282         merge_num(pgfailback);
283         merge_num(rr_weight);
284         merge_num(no_path_retry);
285         merge_num(minio);
286
287         return 0;
288 }
289
290 int
291 store_hwe (vector hwtable, struct hwentry * dhwe)
292 {
293         struct hwentry * hwe;
294
295         if (find_hwe_strmatch(hwtable, dhwe))
296                 return 0;
297
298         if (!(hwe = alloc_hwe()))
299                 return 1;
300
301         if (!dhwe->vendor || !(hwe->vendor = set_param_str(dhwe->vendor)))
302                 goto out;
303
304         if (!dhwe->product || !(hwe->product = set_param_str(dhwe->product)))
305                 goto out;
306
307         if (dhwe->revision && !(hwe->revision = set_param_str(dhwe->revision)))
308                 goto out;
309
310         if (dhwe->getuid && !(hwe->getuid = set_param_str(dhwe->getuid)))
311                 goto out;
312
313         if (dhwe->features && !(hwe->features = set_param_str(dhwe->features)))
314                 goto out;
315
316         if (dhwe->hwhandler && !(hwe->hwhandler = set_param_str(dhwe->hwhandler)))
317                 goto out;
318
319         if (dhwe->selector && !(hwe->selector = set_param_str(dhwe->selector)))
320                 goto out;
321
322         if (dhwe->checker_name && !(hwe->checker_name = set_param_str(dhwe->checker_name)))
323                 goto out;
324                                 
325         if (dhwe->prio_name && !(hwe->prio_name = set_param_str(dhwe->prio_name)))
326                 goto out;
327                                 
328         hwe->pgpolicy = dhwe->pgpolicy;
329         hwe->pgfailback = dhwe->pgfailback;
330         hwe->rr_weight = dhwe->rr_weight;
331         hwe->no_path_retry = dhwe->no_path_retry;
332         hwe->minio = dhwe->minio;
333
334         if (dhwe->bl_product && !(hwe->bl_product = set_param_str(dhwe->bl_product)))
335                 goto out;
336
337         if (!vector_alloc_slot(hwtable))
338                 goto out;
339
340         vector_set_slot(hwtable, hwe);
341         return 0;
342 out:
343         free_hwe(hwe);
344         return 1;
345 }
346
347 static int
348 factorize_hwtable (vector hw)
349 {
350         struct hwentry *hwe1, *hwe2;
351         int i, j;
352
353         vector_foreach_slot(hw, hwe1, i) {
354                 j = i+1;
355                 vector_foreach_slot_after(hw, hwe2, j) {
356                         if (hwe_strmatch(hwe1, hwe2))
357                                 continue;
358                         /* dup */
359                         merge_hwe(hwe1, hwe2);
360                         free_hwe(hwe2);
361                         vector_del_slot(hw, j);
362                         j--;
363                 }
364         }
365         return 0;
366 }
367
368 struct config *
369 alloc_config (void)
370 {
371         return (struct config *)MALLOC(sizeof(struct config));
372 }
373
374 void
375 free_config (struct config * conf)
376 {
377         if (!conf)
378                 return;
379
380         if (conf->dev)
381                 FREE(conf->dev);
382
383         if (conf->udev_dir)
384                 FREE(conf->udev_dir);
385
386         if (conf->multipath_dir)
387                 FREE(conf->multipath_dir);
388
389         if (conf->selector)
390                 FREE(conf->selector);
391
392         if (conf->getuid)
393                 FREE(conf->getuid);
394
395         if (conf->features)
396                 FREE(conf->features);
397
398         if (conf->hwhandler)
399                 FREE(conf->hwhandler);
400
401         if (conf->prio_name)
402                 FREE(conf->prio_name);
403
404         if (conf->checker_name)
405                 FREE(conf->checker_name);
406
407         free_blacklist(conf->blist_devnode);
408         free_blacklist(conf->blist_wwid);
409         free_blacklist_device(conf->blist_device);
410
411         free_blacklist(conf->elist_devnode);
412         free_blacklist(conf->elist_wwid);
413         free_blacklist_device(conf->elist_device);
414
415         free_mptable(conf->mptable);
416         free_hwtable(conf->hwtable);
417         free_keywords(conf->keywords);
418         FREE(conf);
419 }
420
421 int
422 load_config (char * file)
423 {
424         if (!conf)
425                 conf = alloc_config();
426
427         if (!conf)
428                 return 1;
429
430         /*
431          * internal defaults
432          */
433         if (!conf->verbosity)
434                 conf->verbosity = 2;
435
436         conf->dev_type = DEV_NONE;
437         conf->minio = 1000;
438         conf->max_fds = 0;
439         conf->bindings_file = DEFAULT_BINDINGS_FILE;
440         conf->multipath_dir = set_default(DEFAULT_MULTIPATHDIR);
441         conf->flush_on_last_del = 0;
442
443         /*
444          * preload default hwtable
445          */
446         if (conf->hwtable == NULL) {
447                 conf->hwtable = vector_alloc();
448
449                 if (!conf->hwtable)
450                         goto out;
451         }
452         if (setup_default_hwtable(conf->hwtable))
453                 goto out;
454
455         /*
456          * read the config file
457          */
458         if (filepresent(file)) {
459                 set_current_keywords(&conf->keywords);
460                 if (init_data(file, init_keywords)) {
461                         condlog(0, "error parsing config file");
462                         goto out;
463                 }
464         }
465
466         /*
467          * remove duplica in hwtable. config file takes precedence
468          * over build-in hwtable
469          */
470         factorize_hwtable(conf->hwtable);
471
472         /*
473          * fill the voids left in the config file
474          */
475         if (conf->blist_devnode == NULL) {
476                 conf->blist_devnode = vector_alloc();
477
478                 if (!conf->blist_devnode)
479                         goto out;
480         }
481         if (conf->blist_wwid == NULL) {
482                 conf->blist_wwid = vector_alloc();
483
484                 if (!conf->blist_wwid)
485                         goto out;
486         }
487         if (conf->blist_device == NULL) {
488                 conf->blist_device = vector_alloc();
489
490                 if (!conf->blist_device)
491                         goto out;
492         }
493         if (setup_default_blist(conf))
494                 goto out;
495
496         if (conf->elist_devnode == NULL) {
497                 conf->elist_devnode = vector_alloc();
498
499                 if (!conf->elist_devnode)
500                         goto out;
501         }
502         if (conf->elist_wwid == NULL) {
503                 conf->elist_wwid = vector_alloc();
504
505                 if (!conf->elist_wwid)
506                         goto out;
507         }
508
509         if (conf->elist_device == NULL) {
510                 conf->elist_device = vector_alloc();
511
512                 if (!conf->elist_device)
513                         goto out;
514         }
515
516         if (conf->mptable == NULL) {
517                 conf->mptable = vector_alloc();
518                 if (!conf->mptable)
519                         goto out;
520         }
521         if (conf->selector == NULL)
522                 conf->selector = set_default(DEFAULT_SELECTOR);
523
524         if (conf->udev_dir == NULL)
525                 conf->udev_dir = set_default(DEFAULT_UDEVDIR);
526
527         if (conf->getuid == NULL)
528                 conf->getuid = set_default(DEFAULT_GETUID);
529
530         if (conf->features == NULL)
531                 conf->features = set_default(DEFAULT_FEATURES);
532
533         if (conf->hwhandler == NULL)
534                 conf->hwhandler = set_default(DEFAULT_HWHANDLER);
535
536         if (!conf->selector  || !conf->udev_dir || !conf->multipath_dir ||
537             !conf->getuid    || !conf->features ||
538             !conf->hwhandler)
539                 goto out;
540
541         if (!conf->prio_name)
542                 conf->prio_name = set_default(DEFAULT_PRIO);
543
544         if (!conf->checker_name)
545                 conf->checker_name = set_default(DEFAULT_CHECKER);
546
547         return 0;
548 out:
549         free_config(conf);
550         return 1;
551 }
552