]> git.ipfire.org Git - thirdparty/mdadm.git/blob - config.c
Merge commit '956a13fb850321bed8568dfa8692c0c323538d7c'
[thirdparty/mdadm.git] / config.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@suse.de>
23 */
24
25 #include "mdadm.h"
26 #include "dlink.h"
27 #include <dirent.h>
28 #include <glob.h>
29 #include <fnmatch.h>
30 #include <ctype.h>
31 #include <pwd.h>
32 #include <grp.h>
33
34 /*
35 * Read the config file
36 *
37 * conf_get_uuids gets a list of devicename+uuid pairs
38 * conf_get_devs gets device names after expanding wildcards
39 *
40 * Each keeps the returned list and frees it when asked to make
41 * a new list.
42 *
43 * The format of the config file needs to be fairly extensible.
44 * Now, arrays only have names and uuids and devices merely are.
45 * But later arrays might want names, and devices might want superblock
46 * versions, and who knows what else.
47 * I like free format, abhore backslash line continuation, adore
48 * indentation for structure and am ok about # comments.
49 *
50 * So, each line that isn't blank or a #comment must either start
51 * with a key word, and not be indented, or must start with a
52 * non-key-word and must be indented.
53 *
54 * Keywords are DEVICE and ARRAY ... and several others.
55 * DEV{ICE} introduces some devices that might contain raid components.
56 * e.g.
57 * DEV style=0 /dev/sda* /dev/hd*
58 * DEV style=1 /dev/sd[b-f]*
59 * ARR{AY} describes an array giving md device and attributes like uuid=whatever
60 * e.g.
61 * ARRAY /dev/md0 uuid=whatever name=something
62 * Spaces separate words on each line. Quoting, with "" or '' protects them,
63 * but may not wrap over lines
64 *
65 */
66
67 #ifndef CONFFILE
68 #define CONFFILE "/etc/mdadm.conf"
69 #endif
70 #ifndef CONFFILE2
71 /* for Debian compatibility .... */
72 #define CONFFILE2 "/etc/mdadm/mdadm.conf"
73 #endif
74 char DefaultConfFile[] = CONFFILE;
75 char DefaultAltConfFile[] = CONFFILE2;
76
77 enum linetype { Devices, Array, Mailaddr, Mailfrom, Program, CreateDev,
78 Homehost, AutoMode, Policy, PartPolicy, LTEnd };
79 char *keywords[] = {
80 [Devices] = "devices",
81 [Array] = "array",
82 [Mailaddr] = "mailaddr",
83 [Mailfrom] = "mailfrom",
84 [Program] = "program",
85 [CreateDev]= "create",
86 [Homehost] = "homehost",
87 [AutoMode] = "auto",
88 [Policy] = "policy",
89 [PartPolicy]="part-policy",
90 [LTEnd] = NULL
91 };
92
93 /*
94 * match_keyword returns an index into the keywords array, or -1 for no match
95 * case is ignored, and at least three characters must be given
96 */
97
98 int match_keyword(char *word)
99 {
100 int len = strlen(word);
101 int n;
102
103 if (len < 3) return -1;
104 for (n=0; keywords[n]; n++) {
105 if (strncasecmp(word, keywords[n], len)==0)
106 return n;
107 }
108 return -1;
109 }
110
111 struct conf_dev {
112 struct conf_dev *next;
113 char *name;
114 } *cdevlist = NULL;
115
116 struct mddev_dev *load_partitions(void)
117 {
118 FILE *f = fopen("/proc/partitions", "r");
119 char buf[1024];
120 struct mddev_dev *rv = NULL;
121 if (f == NULL) {
122 pr_err("cannot open /proc/partitions\n");
123 return NULL;
124 }
125 while (fgets(buf, 1024, f)) {
126 int major, minor;
127 char *name, *mp;
128 struct mddev_dev *d;
129
130 buf[1023] = '\0';
131 if (buf[0] != ' ')
132 continue;
133 major = strtoul(buf, &mp, 10);
134 if (mp == buf || *mp != ' ')
135 continue;
136 minor = strtoul(mp, NULL, 10);
137
138 name = map_dev(major, minor, 1);
139 if (!name)
140 continue;
141 d = xmalloc(sizeof(*d));
142 memset(d, 0, sizeof(*d));
143 d->devname = xstrdup(name);
144 d->next = rv;
145 rv = d;
146 }
147 fclose(f);
148 return rv;
149 }
150
151 struct mddev_dev *load_containers(void)
152 {
153 struct mdstat_ent *mdstat = mdstat_read(0, 0);
154 struct mdstat_ent *ent;
155 struct mddev_dev *d;
156 struct mddev_dev *rv = NULL;
157 struct map_ent *map = NULL, *me;
158
159 if (!mdstat)
160 return NULL;
161
162 for (ent = mdstat; ent; ent = ent->next)
163 if (ent->metadata_version &&
164 strncmp(ent->metadata_version, "external:", 9) == 0 &&
165 !is_subarray(&ent->metadata_version[9])) {
166 d = xmalloc(sizeof(*d));
167 memset(d, 0, sizeof(*d));
168 me = map_by_devnm(&map, ent->dev);
169 if (me)
170 d->devname = xstrdup(me->path);
171 else if (asprintf(&d->devname, "/dev/%s", ent->dev) < 0) {
172 free(d);
173 continue;
174 }
175 d->next = rv;
176 rv = d;
177 }
178 free_mdstat(mdstat);
179 map_free(map);
180
181 return rv;
182 }
183
184 struct createinfo createinfo = {
185 .autof = 2, /* by default, create devices with standard names */
186 .symlinks = 1,
187 .names = 0, /* By default, stick with numbered md devices. */
188 #ifdef DEBIAN
189 .gid = 6, /* disk */
190 .mode = 0660,
191 #else
192 .mode = 0600,
193 #endif
194 };
195
196 int parse_auto(char *str, char *msg, int config)
197 {
198 int autof;
199 if (str == NULL || *str == 0)
200 autof = 2;
201 else if (strcasecmp(str,"no")==0)
202 autof = 1;
203 else if (strcasecmp(str,"yes")==0)
204 autof = 2;
205 else if (strcasecmp(str,"md")==0)
206 autof = config?5:3;
207 else {
208 /* There might be digits, and maybe a hypen, at the end */
209 char *e = str + strlen(str);
210 int num = 4;
211 int len;
212 while (e > str && isdigit(e[-1]))
213 e--;
214 if (*e) {
215 num = atoi(e);
216 if (num <= 0) num = 1;
217 }
218 if (e > str && e[-1] == '-')
219 e--;
220 len = e - str;
221 if ((len == 2 && strncasecmp(str,"md",2)==0)) {
222 autof = config ? 5 : 3;
223 } else if ((len == 3 && strncasecmp(str,"yes",3)==0)) {
224 autof = 2;
225 } else if ((len == 3 && strncasecmp(str,"mdp",3)==0)) {
226 autof = config ? 6 : 4;
227 } else if ((len == 1 && strncasecmp(str,"p",1)==0) ||
228 (len >= 4 && strncasecmp(str,"part",4)==0)) {
229 autof = 6;
230 } else {
231 pr_err("%s arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
232 " optionally followed by a number.\n",
233 msg, str);
234 exit(2);
235 }
236 autof |= num << 3;
237 }
238 return autof;
239 }
240
241 static void createline(char *line)
242 {
243 char *w;
244 char *ep;
245
246 for (w=dl_next(line); w!=line; w=dl_next(w)) {
247 if (strncasecmp(w, "auto=", 5) == 0)
248 createinfo.autof = parse_auto(w+5, "auto=", 1);
249 else if (strncasecmp(w, "owner=", 6) == 0) {
250 if (w[6] == 0) {
251 pr_err("missing owner name\n");
252 continue;
253 }
254 createinfo.uid = strtoul(w+6, &ep, 10);
255 if (*ep != 0) {
256 struct passwd *pw;
257 /* must be a name */
258 pw = getpwnam(w+6);
259 if (pw)
260 createinfo.uid = pw->pw_uid;
261 else
262 pr_err("CREATE user %s not found\n", w+6);
263 }
264 } else if (strncasecmp(w, "group=", 6) == 0) {
265 if (w[6] == 0) {
266 pr_err("missing group name\n");
267 continue;
268 }
269 createinfo.gid = strtoul(w+6, &ep, 10);
270 if (*ep != 0) {
271 struct group *gr;
272 /* must be a name */
273 gr = getgrnam(w+6);
274 if (gr)
275 createinfo.gid = gr->gr_gid;
276 else
277 pr_err("CREATE group %s not found\n", w+6);
278 }
279 } else if (strncasecmp(w, "mode=", 5) == 0) {
280 if (w[5] == 0) {
281 pr_err("missing CREATE mode\n");
282 continue;
283 }
284 createinfo.mode = strtoul(w+5, &ep, 8);
285 if (*ep != 0) {
286 createinfo.mode = 0600;
287 pr_err("unrecognised CREATE mode %s\n",
288 w+5);
289 }
290 } else if (strncasecmp(w, "metadata=", 9) == 0) {
291 /* style of metadata to use by default */
292 int i;
293 for (i=0; superlist[i] && !createinfo.supertype; i++)
294 createinfo.supertype =
295 superlist[i]->match_metadata_desc(w+9);
296 if (!createinfo.supertype)
297 pr_err("metadata format %s unknown, ignoring\n",
298 w+9);
299 } else if (strncasecmp(w, "symlinks=yes", 12) == 0)
300 createinfo.symlinks = 1;
301 else if (strncasecmp(w, "symlinks=no", 11) == 0)
302 createinfo.symlinks = 0;
303 else if (strncasecmp(w, "names=yes", 12) == 0)
304 createinfo.names = 1;
305 else if (strncasecmp(w, "names=no", 11) == 0)
306 createinfo.names = 0;
307 else {
308 pr_err("unrecognised word on CREATE line: %s\n",
309 w);
310 }
311 }
312 }
313
314 void devline(char *line)
315 {
316 char *w;
317 struct conf_dev *cd;
318
319 for (w=dl_next(line); w != line; w=dl_next(w)) {
320 if (w[0] == '/' || strcasecmp(w, "partitions") == 0 ||
321 strcasecmp(w, "containers") == 0) {
322 cd = xmalloc(sizeof(*cd));
323 cd->name = xstrdup(w);
324 cd->next = cdevlist;
325 cdevlist = cd;
326 } else {
327 pr_err("unreconised word on DEVICE line: %s\n",
328 w);
329 }
330 }
331 }
332
333 struct mddev_ident *mddevlist = NULL;
334 struct mddev_ident **mddevlp = &mddevlist;
335
336 static int is_number(char *w)
337 {
338 /* check if there are 1 or more digits and nothing else */
339 int digits = 0;
340 while (*w && isdigit(*w)) {
341 digits++;
342 w++;
343 }
344 return (digits && ! *w);
345 }
346
347 void arrayline(char *line)
348 {
349 char *w;
350
351 struct mddev_ident mis;
352 struct mddev_ident *mi;
353
354 mis.uuid_set = 0;
355 mis.super_minor = UnSet;
356 mis.level = UnSet;
357 mis.raid_disks = UnSet;
358 mis.spare_disks = 0;
359 mis.devices = NULL;
360 mis.devname = NULL;
361 mis.spare_group = NULL;
362 mis.autof = 0;
363 mis.next = NULL;
364 mis.st = NULL;
365 mis.bitmap_fd = -1;
366 mis.bitmap_file = NULL;
367 mis.name[0] = 0;
368 mis.container = NULL;
369 mis.member = NULL;
370
371 for (w=dl_next(line); w!=line; w=dl_next(w)) {
372 if (w[0] == '/' || strchr(w, '=') == NULL) {
373 /* This names the device, or is '<ignore>'.
374 * The rules match those in create_mddev.
375 * 'w' must be:
376 * /dev/md/{anything}
377 * /dev/mdNN
378 * /dev/md_dNN
379 * <ignore>
380 * or anything that doesn't start '/' or '<'
381 */
382 if (strcasecmp(w, "<ignore>") == 0 ||
383 strncmp(w, "/dev/md/", 8) == 0 ||
384 (w[0] != '/' && w[0] != '<') ||
385 (strncmp(w, "/dev/md", 7) == 0 &&
386 is_number(w+7)) ||
387 (strncmp(w, "/dev/md_d", 9) == 0 &&
388 is_number(w+9))
389 ) {
390 /* This is acceptable */;
391 if (mis.devname)
392 pr_err("only give one "
393 "device per ARRAY line: %s and %s\n",
394 mis.devname, w);
395 else
396 mis.devname = w;
397 }else {
398 pr_err("%s is an invalid name for "
399 "an md device - ignored.\n", w);
400 }
401 } else if (strncasecmp(w, "uuid=", 5)==0 ) {
402 if (mis.uuid_set)
403 pr_err("only specify uuid once, %s ignored.\n",
404 w);
405 else {
406 if (parse_uuid(w+5, mis.uuid))
407 mis.uuid_set = 1;
408 else
409 pr_err("bad uuid: %s\n", w);
410 }
411 } else if (strncasecmp(w, "super-minor=", 12)==0 ) {
412 if (mis.super_minor != UnSet)
413 pr_err("only specify super-minor once, %s ignored.\n",
414 w);
415 else {
416 char *endptr;
417 int minor = strtol(w+12, &endptr, 10);
418
419 if (w[12]==0 || endptr[0]!=0 || minor < 0)
420 pr_err("invalid super-minor number: %s\n",
421 w);
422 else
423 mis.super_minor = minor;
424 }
425 } else if (strncasecmp(w, "name=", 5)==0) {
426 if (mis.name[0])
427 pr_err("only specify name once, %s ignored.\n",
428 w);
429 else if (strlen(w+5) > 32)
430 pr_err("name too long, ignoring %s\n", w);
431 else
432 strcpy(mis.name, w+5);
433
434 } else if (strncasecmp(w, "bitmap=", 7) == 0) {
435 if (mis.bitmap_file)
436 pr_err("only specify bitmap file once. %s ignored\n",
437 w);
438 else
439 mis.bitmap_file = xstrdup(w+7);
440
441 } else if (strncasecmp(w, "devices=", 8 ) == 0 ) {
442 if (mis.devices)
443 pr_err("only specify devices once (use a comma separated list). %s ignored\n",
444 w);
445 else
446 mis.devices = xstrdup(w+8);
447 } else if (strncasecmp(w, "spare-group=", 12) == 0 ) {
448 if (mis.spare_group)
449 pr_err("only specify one spare group per array. %s ignored.\n",
450 w);
451 else
452 mis.spare_group = xstrdup(w+12);
453 } else if (strncasecmp(w, "level=", 6) == 0 ) {
454 /* this is mainly for compatability with --brief output */
455 mis.level = map_name(pers, w+6);
456 } else if (strncasecmp(w, "disks=", 6) == 0 ) {
457 /* again, for compat */
458 mis.raid_disks = atoi(w+6);
459 } else if (strncasecmp(w, "num-devices=", 12) == 0 ) {
460 /* again, for compat */
461 mis.raid_disks = atoi(w+12);
462 } else if (strncasecmp(w, "spares=", 7) == 0 ) {
463 /* for warning if not all spares present */
464 mis.spare_disks = atoi(w+7);
465 } else if (strncasecmp(w, "metadata=", 9) == 0) {
466 /* style of metadata on the devices. */
467 int i;
468
469 for(i=0; superlist[i] && !mis.st; i++)
470 mis.st = superlist[i]->match_metadata_desc(w+9);
471
472 if (!mis.st)
473 pr_err("metadata format %s unknown, ignored.\n", w+9);
474 } else if (strncasecmp(w, "auto=", 5) == 0 ) {
475 /* whether to create device special files as needed */
476 mis.autof = parse_auto(w+5, "auto type", 0);
477 } else if (strncasecmp(w, "member=", 7) == 0) {
478 /* subarray within a container */
479 mis.member = xstrdup(w+7);
480 } else if (strncasecmp(w, "container=", 10) == 0) {
481 /* the container holding this subarray. Either a device name
482 * or a uuid */
483 mis.container = xstrdup(w+10);
484 } else {
485 pr_err("unrecognised word on ARRAY line: %s\n",
486 w);
487 }
488 }
489 if (mis.uuid_set == 0 && mis.devices == NULL &&
490 mis.super_minor == UnSet && mis.name[0] == 0 &&
491 (mis.container == NULL || mis.member == NULL))
492 pr_err("ARRAY line %s has no identity information.\n", mis.devname);
493 else {
494 mi = xmalloc(sizeof(*mi));
495 *mi = mis;
496 mi->devname = mis.devname ? xstrdup(mis.devname) : NULL;
497 mi->next = NULL;
498 *mddevlp = mi;
499 mddevlp = &mi->next;
500 }
501 }
502
503 static char *alert_email = NULL;
504 void mailline(char *line)
505 {
506 char *w;
507
508 for (w=dl_next(line); w != line ; w=dl_next(w)) {
509 if (alert_email == NULL)
510 alert_email = xstrdup(w);
511 else
512 pr_err("excess address on MAIL line: %s - ignored\n",
513 w);
514 }
515 }
516
517 static char *alert_mail_from = NULL;
518 void mailfromline(char *line)
519 {
520 char *w;
521
522 for (w=dl_next(line); w != line ; w=dl_next(w)) {
523 if (alert_mail_from == NULL)
524 alert_mail_from = xstrdup(w);
525 else {
526 char *t = NULL;
527
528 if (xasprintf(&t, "%s %s", alert_mail_from, w) > 0) {
529 free(alert_mail_from);
530 alert_mail_from = t;
531 }
532 }
533 }
534 }
535
536 static char *alert_program = NULL;
537 void programline(char *line)
538 {
539 char *w;
540
541 for (w=dl_next(line); w != line ; w=dl_next(w)) {
542 if (alert_program == NULL)
543 alert_program = xstrdup(w);
544 else
545 pr_err("excess program on PROGRAM line: %s - ignored\n",
546 w);
547 }
548 }
549
550 static char *home_host = NULL;
551 static int require_homehost = 1;
552 void homehostline(char *line)
553 {
554 char *w;
555
556 for (w=dl_next(line); w != line ; w=dl_next(w)) {
557 if (strcasecmp(w, "<ignore>")==0)
558 require_homehost = 0;
559 else if (home_host == NULL) {
560 if (strcasecmp(w, "<none>")==0)
561 home_host = xstrdup("");
562 else
563 home_host = xstrdup(w);
564 }else
565 pr_err("excess host name on HOMEHOST line: %s - ignored\n",
566 w);
567 }
568 }
569
570 char auto_yes[] = "yes";
571 char auto_no[] = "no";
572 char auto_homehost[] = "homehost";
573
574 static int auto_seen = 0;
575 void autoline(char *line)
576 {
577 char *w;
578 char *seen;
579 int super_cnt;
580 char *dflt = auto_yes;
581 int homehost = 0;
582 int i;
583
584 if (auto_seen) {
585 pr_err("AUTO line may only be give once."
586 " Subsequent lines ignored\n");
587 return;
588 }
589 /* Parse the 'auto' line creating policy statements for the 'auto' policy.
590 *
591 * The default is 'yes' but the 'auto' line might over-ride that.
592 * Words in the line are processed in order with the first
593 * match winning.
594 * word can be:
595 * +version - that version can be assembled
596 * -version - that version cannot be auto-assembled
597 * yes or +all - any other version can be assembled
598 * no or -all - no other version can be assembled.
599 * homehost - any array associated by 'homehost' to this
600 * host can be assembled.
601 *
602 * Thus:
603 * +ddf -0.90 homehost -all
604 * will auto-assemble any ddf array, no 0.90 array, and
605 * any other array (imsm, 1.x) if and only if it is identified
606 * as belonging to this host.
607 *
608 * We translate that to policy by creating 'auto=yes' when we see
609 * a '+version' line, 'auto=no' if we see '-version' before 'homehost',
610 * or 'auto=homehost' if we see '-version' after 'homehost'.
611 * When we see yes, no, +all or -all we stop and any version that hasn't
612 * been seen gets an appropriate auto= entry.
613 */
614
615 for (super_cnt = 0; superlist[super_cnt]; super_cnt++)
616 ;
617 seen = xcalloc(super_cnt, 1);
618
619 for (w = dl_next(line); w != line ; w = dl_next(w)) {
620 char *val;
621
622 if (strcasecmp(w, "yes") == 0) {
623 dflt = auto_yes;
624 break;
625 }
626 if (strcasecmp(w, "no") == 0) {
627 if (homehost)
628 dflt = auto_homehost;
629 else
630 dflt = auto_no;
631 break;
632 }
633 if (strcasecmp(w, "homehost") == 0) {
634 homehost = 1;
635 continue;
636 }
637 if (w[0] == '+')
638 val = auto_yes;
639 else if (w[0] == '-') {
640 if (homehost)
641 val = auto_homehost;
642 else
643 val = auto_no;
644 } else
645 continue;
646
647 if (strcasecmp(w+1, "all") == 0) {
648 dflt = val;
649 break;
650 }
651 for (i = 0; superlist[i]; i++) {
652 const char *version = superlist[i]->name;
653 if (strcasecmp(w+1, version) == 0)
654 break;
655 /* 1 matches 1.x, 0 matches 0.90 */
656 if (version[1] == '.' &&
657 strlen(w+1) == 1 &&
658 w[1] == version[0])
659 break;
660 /* 1.anything matches 1.x */
661 if (strcmp(version, "1.x") == 0 &&
662 strncmp(w+1, "1.", 2) == 0)
663 break;
664 }
665 if (superlist[i] == NULL)
666 /* ignore this word */
667 continue;
668 if (seen[i])
669 /* already know about this metadata */
670 continue;
671 policy_add(rule_policy, pol_auto, val, pol_metadata, superlist[i]->name, NULL);
672 seen[i] = 1;
673 }
674 for (i = 0; i < super_cnt; i++)
675 if (!seen[i])
676 policy_add(rule_policy, pol_auto, dflt, pol_metadata, superlist[i]->name, NULL);
677
678 free(seen);
679 }
680
681 int loaded = 0;
682
683 static char *conffile = NULL;
684 void set_conffile(char *file)
685 {
686 conffile = file;
687 }
688
689 void load_conffile(void)
690 {
691 FILE *f;
692 char *line;
693
694 if (loaded) return;
695 if (conffile == NULL)
696 conffile = DefaultConfFile;
697
698 if (strcmp(conffile, "none") == 0) {
699 loaded = 1;
700 return;
701 }
702 if (strcmp(conffile, "partitions")==0) {
703 char *list = dl_strdup("DEV");
704 dl_init(list);
705 dl_add(list, dl_strdup("partitions"));
706 devline(list);
707 free_line(list);
708 loaded = 1;
709 return;
710 }
711 f = fopen(conffile, "r");
712 /* Debian chose to relocate mdadm.conf into /etc/mdadm/.
713 * To allow Debian users to compile from clean source and still
714 * have a working mdadm, we read /etc/mdadm/mdadm.conf
715 * if /etc/mdadm.conf doesn't exist
716 */
717 if (f == NULL &&
718 conffile == DefaultConfFile) {
719 f = fopen(DefaultAltConfFile, "r");
720 if (f)
721 conffile = DefaultAltConfFile;
722 }
723 if (f == NULL)
724 return;
725
726 loaded = 1;
727 while ((line=conf_line(f))) {
728 switch(match_keyword(line)) {
729 case Devices:
730 devline(line);
731 break;
732 case Array:
733 arrayline(line);
734 break;
735 case Mailaddr:
736 mailline(line);
737 break;
738 case Mailfrom:
739 mailfromline(line);
740 break;
741 case Program:
742 programline(line);
743 break;
744 case CreateDev:
745 createline(line);
746 break;
747 case Homehost:
748 homehostline(line);
749 break;
750 case AutoMode:
751 autoline(line);
752 break;
753 case Policy:
754 policyline(line, rule_policy);
755 break;
756 case PartPolicy:
757 policyline(line, rule_part);
758 break;
759 default:
760 pr_err("Unknown keyword %s\n", line);
761 }
762 free_line(line);
763 }
764
765 fclose(f);
766
767 /* printf("got file\n"); */
768 }
769
770 char *conf_get_mailaddr(void)
771 {
772 load_conffile();
773 return alert_email;
774 }
775
776 char *conf_get_mailfrom(void)
777 {
778 load_conffile();
779 return alert_mail_from;
780 }
781
782 char *conf_get_program(void)
783 {
784 load_conffile();
785 return alert_program;
786 }
787
788 char *conf_get_homehost(int *require_homehostp)
789 {
790 load_conffile();
791 if (require_homehostp)
792 *require_homehostp = require_homehost;
793 return home_host;
794 }
795
796 struct createinfo *conf_get_create_info(void)
797 {
798 load_conffile();
799 return &createinfo;
800 }
801
802 struct mddev_ident *conf_get_ident(char *dev)
803 {
804 struct mddev_ident *rv;
805 load_conffile();
806 rv = mddevlist;
807 while (dev && rv && (rv->devname == NULL
808 || !devname_matches(dev, rv->devname)))
809 rv = rv->next;
810 return rv;
811 }
812
813 static void append_dlist(struct mddev_dev **dlp, struct mddev_dev *list)
814 {
815 while (*dlp)
816 dlp = &(*dlp)->next;
817 *dlp = list;
818 }
819
820 struct mddev_dev *conf_get_devs()
821 {
822 glob_t globbuf;
823 struct conf_dev *cd;
824 int flags = 0;
825 static struct mddev_dev *dlist = NULL;
826 unsigned int i;
827
828 while (dlist) {
829 struct mddev_dev *t = dlist;
830 dlist = dlist->next;
831 free(t->devname);
832 free(t);
833 }
834
835 load_conffile();
836
837 if (cdevlist == NULL) {
838 /* default to 'partitions' and 'containers' */
839 dlist = load_partitions();
840 append_dlist(&dlist, load_containers());
841 }
842
843 for (cd=cdevlist; cd; cd=cd->next) {
844 if (strcasecmp(cd->name, "partitions")==0)
845 append_dlist(&dlist, load_partitions());
846 else if (strcasecmp(cd->name, "containers")==0)
847 append_dlist(&dlist, load_containers());
848 else {
849 glob(cd->name, flags, NULL, &globbuf);
850 flags |= GLOB_APPEND;
851 }
852 }
853 if (flags & GLOB_APPEND) {
854 for (i=0; i<globbuf.gl_pathc; i++) {
855 struct mddev_dev *t = xmalloc(sizeof(*t));
856 memset(t, 0, sizeof(*t));
857 t->devname = xstrdup(globbuf.gl_pathv[i]);
858 t->next = dlist;
859 dlist = t;
860 /* printf("one dev is %s\n", t->devname);*/
861 }
862 globfree(&globbuf);
863 }
864
865 return dlist;
866 }
867
868 int conf_test_dev(char *devname)
869 {
870 struct conf_dev *cd;
871 if (cdevlist == NULL)
872 /* allow anything by default */
873 return 1;
874 for (cd = cdevlist ; cd ; cd = cd->next) {
875 if (strcasecmp(cd->name, "partitions") == 0)
876 return 1;
877 if (fnmatch(cd->name, devname, FNM_PATHNAME) == 0)
878 return 1;
879 }
880 return 0;
881 }
882
883 int conf_test_metadata(const char *version, struct dev_policy *pol, int is_homehost)
884 {
885 /* If anyone said 'yes', that sticks.
886 * else if homehost applies, use that
887 * else if there is a 'no', say 'no'.
888 * else 'yes'.
889 */
890 struct dev_policy *p;
891 int no=0, found_homehost=0;
892 load_conffile();
893
894 pol = pol_find(pol, pol_auto);
895 pol_for_each(p, pol, version) {
896 if (strcmp(p->value, "yes") == 0)
897 return 1;
898 if (strcmp(p->value, "homehost") == 0)
899 found_homehost = 1;
900 if (strcmp(p->value, "no") == 0)
901 no = 1;
902 }
903 if (is_homehost && found_homehost)
904 return 1;
905 if (no)
906 return 0;
907 return 1;
908 }
909
910 int match_oneof(char *devices, char *devname)
911 {
912 /* check if one of the comma separated patterns in devices
913 * matches devname
914 */
915
916 while (devices && *devices) {
917 char patn[1024];
918 char *p = devices;
919 devices = strchr(devices, ',');
920 if (!devices)
921 devices = p + strlen(p);
922 if (devices-p < 1024) {
923 strncpy(patn, p, devices-p);
924 patn[devices-p] = 0;
925 if (fnmatch(patn, devname, FNM_PATHNAME)==0)
926 return 1;
927 }
928 if (*devices == ',')
929 devices++;
930 }
931 return 0;
932 }
933
934 int devname_matches(char *name, char *match)
935 {
936 /* See if the given array name matches the
937 * given match from config file.
938 *
939 * First strip and /dev/md/ or /dev/, then
940 * see if there might be a numeric match of
941 * mdNN with NN
942 * then just strcmp
943 */
944 if (strncmp(name, "/dev/md/", 8) == 0)
945 name += 8;
946 else if (strncmp(name, "/dev/", 5) == 0)
947 name += 5;
948
949 if (strncmp(match, "/dev/md/", 8) == 0)
950 match += 8;
951 else if (strncmp(match, "/dev/", 5) == 0)
952 match += 5;
953
954 if (strncmp(name, "md", 2) == 0 &&
955 isdigit(name[2]))
956 name += 2;
957 if (strncmp(match, "md", 2) == 0 &&
958 isdigit(match[2]))
959 match += 2;
960
961 return (strcmp(name, match) == 0);
962 }
963
964 int conf_name_is_free(char *name)
965 {
966 /* Check if this name is already taken by an ARRAY entry in
967 * the config file.
968 * It can be taken either by a match on devname, name, or
969 * even super-minor.
970 */
971 struct mddev_ident *dev;
972
973 load_conffile();
974 for (dev = mddevlist; dev; dev = dev->next) {
975 char nbuf[100];
976 if (dev->devname && devname_matches(name, dev->devname))
977 return 0;
978 if (dev->name[0] && devname_matches(name, dev->name))
979 return 0;
980 sprintf(nbuf, "%d", dev->super_minor);
981 if (dev->super_minor != UnSet &&
982 devname_matches(name, nbuf))
983 return 0;
984 }
985 return 1;
986 }
987
988 struct mddev_ident *conf_match(struct supertype *st,
989 struct mdinfo *info,
990 char *devname,
991 int verbose, int *rvp)
992 {
993 struct mddev_ident *array_list, *match;
994 array_list = conf_get_ident(NULL);
995 match = NULL;
996 for (; array_list; array_list = array_list->next) {
997 if (array_list->uuid_set &&
998 same_uuid(array_list->uuid, info->uuid, st->ss->swapuuid)
999 == 0) {
1000 if (verbose >= 2 && array_list->devname)
1001 pr_err("UUID differs from %s.\n",
1002 array_list->devname);
1003 continue;
1004 }
1005 if (array_list->name[0] &&
1006 strcasecmp(array_list->name, info->name) != 0) {
1007 if (verbose >= 2 && array_list->devname)
1008 pr_err("Name differs from %s.\n",
1009 array_list->devname);
1010 continue;
1011 }
1012 if (array_list->devices && devname &&
1013 !match_oneof(array_list->devices, devname)) {
1014 if (verbose >= 2 && array_list->devname)
1015 pr_err("Not a listed device for %s.\n",
1016 array_list->devname);
1017 continue;
1018 }
1019 if (array_list->super_minor != UnSet &&
1020 array_list->super_minor != info->array.md_minor) {
1021 if (verbose >= 2 && array_list->devname)
1022 pr_err("Different super-minor to %s.\n",
1023 array_list->devname);
1024 continue;
1025 }
1026 if (!array_list->uuid_set &&
1027 !array_list->name[0] &&
1028 !array_list->devices &&
1029 array_list->super_minor == UnSet) {
1030 if (verbose >= 2 && array_list->devname)
1031 pr_err("%s doesn't have any identifying"
1032 " information.\n",
1033 array_list->devname);
1034 continue;
1035 }
1036 /* FIXME, should I check raid_disks and level too?? */
1037
1038 if (match) {
1039 if (verbose >= 0) {
1040 if (match->devname && array_list->devname)
1041 pr_err("we match both %s and %s - "
1042 "cannot decide which to use.\n",
1043 match->devname,
1044 array_list->devname);
1045 else
1046 pr_err("multiple lines in mdadm.conf"
1047 " match\n");
1048 }
1049 if (rvp)
1050 *rvp = 2;
1051 match = NULL;
1052 break;
1053 }
1054 match = array_list;
1055 }
1056 return match;
1057 }
1058
1059 int conf_verify_devnames(struct mddev_ident *array_list)
1060 {
1061 struct mddev_ident *a1, *a2;
1062
1063 for (a1 = array_list; a1; a1 = a1->next) {
1064 if (!a1->devname)
1065 continue;
1066 if (strcmp(a1->devname, "<ignore>") == 0)
1067 continue;
1068 for (a2 = a1->next; a2; a2 = a2->next) {
1069 if (!a2->devname)
1070 continue;
1071 if (strcmp(a1->devname, a2->devname) != 0)
1072 continue;
1073
1074 if (a1->uuid_set && a2->uuid_set) {
1075 char nbuf[64];
1076 __fname_from_uuid(a1->uuid, 0, nbuf, ':');
1077 pr_err("Devices %s and ",
1078 nbuf);
1079 __fname_from_uuid(a2->uuid, 0, nbuf, ':');
1080 fprintf(stderr,
1081 "%s have the same name: %s\n",
1082 nbuf, a1->devname);
1083 } else
1084 pr_err("Device %s given twice"
1085 " in config file\n", a1->devname);
1086 return 1;
1087 }
1088 }
1089
1090 return 0;
1091 }