]> git.ipfire.org Git - thirdparty/mdadm.git/blob - config.c
mdadm: set ident.devname if applicable
[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 #ifndef _POSIX_C_SOURCE
67 #define _POSIX_C_SOURCE 200809L
68 #endif
69
70 #ifndef CONFFILE
71 #define CONFFILE "/etc/mdadm.conf"
72 #endif
73 #ifndef CONFFILE2
74 /* for Debian compatibility .... */
75 #define CONFFILE2 "/etc/mdadm/mdadm.conf"
76 #endif
77 char DefaultConfFile[] = CONFFILE;
78 char DefaultConfDir[] = CONFFILE ".d";
79 char DefaultAltConfFile[] = CONFFILE2;
80 char DefaultAltConfDir[] = CONFFILE2 ".d";
81
82 enum linetype { Devices, Array, Mailaddr, Mailfrom, Program, CreateDev,
83 Homehost, HomeCluster, AutoMode, Policy, PartPolicy, Sysfs,
84 MonitorDelay, LTEnd };
85 char *keywords[] = {
86 [Devices] = "devices",
87 [Array] = "array",
88 [Mailaddr] = "mailaddr",
89 [Mailfrom] = "mailfrom",
90 [Program] = "program",
91 [CreateDev]= "create",
92 [Homehost] = "homehost",
93 [HomeCluster] = "homecluster",
94 [AutoMode] = "auto",
95 [Policy] = "policy",
96 [PartPolicy]="part-policy",
97 [Sysfs] = "sysfs",
98 [MonitorDelay] = "monitordelay",
99 [LTEnd] = NULL
100 };
101
102 /*
103 * match_keyword returns an index into the keywords array, or -1 for no match
104 * case is ignored, and at least three characters must be given
105 */
106
107 int match_keyword(char *word)
108 {
109 int len = strlen(word);
110 int n;
111
112 if (len < 3)
113 return -1;
114 for (n = 0; keywords[n]; n++) {
115 if (strncasecmp(word, keywords[n], len) == 0)
116 return n;
117 }
118
119 return -1;
120 }
121
122 /**
123 * is_devname_ignore() - check if &devname is a special "<ignore>" keyword.
124 */
125 bool is_devname_ignore(char *devname)
126 {
127 static const char keyword[] = "<ignore>";
128
129 if (strcasecmp(devname, keyword) == 0)
130 return true;
131 return false;
132 }
133
134 /**
135 * ident_init() - Set defaults.
136 * @ident: ident pointer, not NULL.
137 */
138 inline void ident_init(struct mddev_ident *ident)
139 {
140 assert(ident);
141
142 ident->assembled = false;
143 ident->autof = 0;
144 ident->bitmap_fd = -1;
145 ident->bitmap_file = NULL;
146 ident->container = NULL;
147 ident->devices = NULL;
148 ident->devname = NULL;
149 ident->level = UnSet;
150 ident->member = NULL;
151 ident->name[0] = 0;
152 ident->next = NULL;
153 ident->raid_disks = UnSet;
154 ident->spare_group = NULL;
155 ident->spare_disks = 0;
156 ident->st = NULL;
157 ident->super_minor = UnSet;
158 ident->uuid[0] = 0;
159 ident->uuid_set = 0;
160 }
161
162 struct conf_dev {
163 struct conf_dev *next;
164 char *name;
165 } *cdevlist = NULL;
166
167 struct mddev_dev *load_partitions(void)
168 {
169 FILE *f = fopen("/proc/partitions", "r");
170 char buf[1024];
171 struct mddev_dev *rv = NULL;
172
173 if (f == NULL) {
174 pr_err("cannot open /proc/partitions\n");
175 return NULL;
176 }
177 while (fgets(buf, 1024, f)) {
178 int major, minor;
179 char *name, *mp;
180 struct mddev_dev *d;
181
182 buf[1023] = '\0';
183 if (buf[0] != ' ')
184 continue;
185 major = strtoul(buf, &mp, 10);
186 if (mp == buf || *mp != ' ')
187 continue;
188 minor = strtoul(mp, NULL, 10);
189
190 name = map_dev(major, minor, 1);
191 if (!name)
192 continue;
193 d = xcalloc(1, sizeof(*d));
194 d->devname = xstrdup(name);
195 d->next = rv;
196 rv = d;
197 }
198 fclose(f);
199 return rv;
200 }
201
202 struct mddev_dev *load_containers(void)
203 {
204 struct mdstat_ent *mdstat = mdstat_read(0, 0);
205 struct mdstat_ent *ent;
206 struct mddev_dev *d;
207 struct mddev_dev *rv = NULL;
208 struct map_ent *map = NULL, *me;
209
210 if (!mdstat)
211 return NULL;
212
213 for (ent = mdstat; ent; ent = ent->next)
214 if (ent->metadata_version &&
215 strncmp(ent->metadata_version, "external:", 9) == 0 &&
216 !is_subarray(&ent->metadata_version[9])) {
217 d = xcalloc(1, sizeof(*d));
218 me = map_by_devnm(&map, ent->devnm);
219 if (me)
220 d->devname = xstrdup(me->path);
221 else if (asprintf(&d->devname, "/dev/%s", ent->devnm) < 0) {
222 free(d);
223 continue;
224 }
225 d->next = rv;
226 rv = d;
227 map_free(map);
228 map = NULL;
229 }
230 free_mdstat(mdstat);
231
232 return rv;
233 }
234
235 struct createinfo createinfo = {
236 .autof = 2, /* by default, create devices with standard names */
237 .names = 0, /* By default, stick with numbered md devices. */
238 .bblist = 1, /* Use a bad block list by default */
239 #ifdef DEBIAN
240 .gid = 6, /* disk */
241 .mode = 0660,
242 #else
243 .mode = 0600,
244 #endif
245 };
246
247 int parse_auto(char *str, char *msg, int config)
248 {
249 int autof;
250 if (str == NULL || *str == 0)
251 autof = 2;
252 else if (strcasecmp(str, "no") == 0)
253 autof = 1;
254 else if (strcasecmp(str, "yes") == 0)
255 autof = 2;
256 else if (strcasecmp(str, "md") == 0)
257 autof = config ? 5:3;
258 else {
259 /* There might be digits, and maybe a hypen, at the end */
260 char *e = str + strlen(str);
261 int num = 4;
262 int len;
263 while (e > str && isdigit(e[-1]))
264 e--;
265 if (*e) {
266 num = atoi(e);
267 if (num <= 0)
268 num = 1;
269 }
270 if (e > str && e[-1] == '-')
271 e--;
272 len = e - str;
273 if ((len == 2 && strncasecmp(str, "md", 2) == 0)) {
274 autof = config ? 5 : 3;
275 } else if ((len == 3 && strncasecmp(str, "yes", 3) == 0)) {
276 autof = 2;
277 } else if ((len == 3 && strncasecmp(str, "mdp", 3) == 0)) {
278 autof = config ? 6 : 4;
279 } else if ((len == 1 && strncasecmp(str, "p", 1) == 0) ||
280 (len >= 4 && strncasecmp(str, "part", 4) == 0)) {
281 autof = 6;
282 } else {
283 pr_err("%s arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
284 " optionally followed by a number.\n",
285 msg, str);
286 exit(2);
287 }
288 autof |= num << 3;
289 }
290 return autof;
291 }
292
293 static void createline(char *line)
294 {
295 char *w;
296 char *ep;
297
298 for (w = dl_next(line); w != line; w = dl_next(w)) {
299 if (strncasecmp(w, "auto=", 5) == 0)
300 createinfo.autof = parse_auto(w + 5, "auto=", 1);
301 else if (strncasecmp(w, "owner=", 6) == 0) {
302 if (w[6] == 0) {
303 pr_err("missing owner name\n");
304 continue;
305 }
306 createinfo.uid = strtoul(w + 6, &ep, 10);
307 if (*ep != 0) {
308 struct passwd *pw;
309 /* must be a name */
310 pw = getpwnam(w + 6);
311 if (pw)
312 createinfo.uid = pw->pw_uid;
313 else
314 pr_err("CREATE user %s not found\n",
315 w + 6);
316 }
317 } else if (strncasecmp(w, "group=", 6) == 0) {
318 if (w[6] == 0) {
319 pr_err("missing group name\n");
320 continue;
321 }
322 createinfo.gid = strtoul(w + 6, &ep, 10);
323 if (*ep != 0) {
324 struct group *gr;
325 /* must be a name */
326 gr = getgrnam(w + 6);
327 if (gr)
328 createinfo.gid = gr->gr_gid;
329 else
330 pr_err("CREATE group %s not found\n",
331 w + 6);
332 }
333 } else if (strncasecmp(w, "mode=", 5) == 0) {
334 if (w[5] == 0) {
335 pr_err("missing CREATE mode\n");
336 continue;
337 }
338 createinfo.mode = strtoul(w + 5, &ep, 8);
339 if (*ep != 0) {
340 createinfo.mode = 0600;
341 pr_err("unrecognised CREATE mode %s\n",
342 w + 5);
343 }
344 } else if (strncasecmp(w, "metadata=", 9) == 0) {
345 /* style of metadata to use by default */
346 int i;
347 for (i = 0; superlist[i] && !createinfo.supertype; i++)
348 createinfo.supertype = superlist[i]->match_metadata_desc(w + 9);
349 if (!createinfo.supertype)
350 pr_err("metadata format %s unknown, ignoring\n",
351 w+9);
352 } else if (strncasecmp(w, "names=yes", 12) == 0)
353 createinfo.names = 1;
354 else if (strncasecmp(w, "names=no", 11) == 0)
355 createinfo.names = 0;
356 else if (strncasecmp(w, "bbl=no", 11) == 0)
357 createinfo.bblist = 0;
358 else if (strncasecmp(w, "bbl=yes", 11) == 0)
359 createinfo.bblist = 1;
360 else {
361 pr_err("unrecognised word on CREATE line: %s\n",
362 w);
363 }
364 }
365 }
366
367 void devline(char *line)
368 {
369 char *w;
370 struct conf_dev *cd;
371
372 for (w = dl_next(line); w != line; w = dl_next(w)) {
373 if (w[0] == '/' || strcasecmp(w, "partitions") == 0 ||
374 strcasecmp(w, "containers") == 0) {
375 cd = xmalloc(sizeof(*cd));
376 cd->name = xstrdup(w);
377 cd->next = cdevlist;
378 cdevlist = cd;
379 } else {
380 pr_err("unreconised word on DEVICE line: %s\n", w);
381 }
382 }
383 }
384
385 struct mddev_ident *mddevlist = NULL;
386 struct mddev_ident **mddevlp = &mddevlist;
387
388 void arrayline(char *line)
389 {
390 char *w;
391
392 struct mddev_ident mis;
393 struct mddev_ident *mi;
394
395 ident_init(&mis);
396
397 for (w = dl_next(line); w != line; w = dl_next(w)) {
398 if (w[0] == '/' || strchr(w, '=') == NULL) {
399 /* This names the device, or is '<ignore>'.
400 * The rules match those in create_mddev.
401 * 'w' must be:
402 * /dev/md/{anything}
403 * /dev/mdNN
404 * /dev/md_dNN
405 * <ignore>
406 * or anything that doesn't start '/' or '<'
407 */
408 if (is_devname_ignore(w) == true ||
409 strncmp(w, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0 ||
410 (w[0] != '/' && w[0] != '<') ||
411 is_devname_md_numbered(w) == true ||
412 is_devname_md_d_numbered(w) == true) {
413 /* This is acceptable */;
414 if (mis.devname)
415 pr_err("only give one device per ARRAY line: %s and %s\n",
416 mis.devname, w);
417 else
418 mis.devname = w;
419 }else {
420 pr_err("%s is an invalid name for an md device - ignored.\n", w);
421 }
422 } else if (strncasecmp(w, "uuid=", 5) == 0) {
423 if (mis.uuid_set)
424 pr_err("only specify uuid once, %s ignored.\n",
425 w);
426 else {
427 if (parse_uuid(w + 5, mis.uuid))
428 mis.uuid_set = 1;
429 else
430 pr_err("bad uuid: %s\n", w);
431 }
432 } else if (strncasecmp(w, "super-minor=", 12) == 0) {
433 if (mis.super_minor != UnSet)
434 pr_err("only specify super-minor once, %s ignored.\n",
435 w);
436 else {
437 char *endptr;
438 int minor = strtol(w + 12, &endptr, 10);
439
440 if (w[12] == 0 || endptr[0] != 0 || minor < 0)
441 pr_err("invalid super-minor number: %s\n",
442 w);
443 else
444 mis.super_minor = minor;
445 }
446 } else if (strncasecmp(w, "name=", 5) == 0) {
447 if (mis.name[0])
448 pr_err("only specify name once, %s ignored.\n",
449 w);
450 else if (strlen(w + 5) > 32)
451 pr_err("name too long, ignoring %s\n", w);
452 else
453 strcpy(mis.name, w + 5);
454
455 } else if (strncasecmp(w, "bitmap=", 7) == 0) {
456 if (mis.bitmap_file)
457 pr_err("only specify bitmap file once. %s ignored\n",
458 w);
459 else
460 mis.bitmap_file = xstrdup(w + 7);
461
462 } else if (strncasecmp(w, "devices=", 8 ) == 0) {
463 if (mis.devices)
464 pr_err("only specify devices once (use a comma separated list). %s ignored\n",
465 w);
466 else
467 mis.devices = xstrdup(w + 8);
468 } else if (strncasecmp(w, "spare-group=", 12) == 0) {
469 if (mis.spare_group)
470 pr_err("only specify one spare group per array. %s ignored.\n",
471 w);
472 else
473 mis.spare_group = xstrdup(w + 12);
474 } else if (strncasecmp(w, "level=", 6) == 0 ) {
475 /* this is mainly for compatability with --brief output */
476 mis.level = map_name(pers, w + 6);
477 } else if (strncasecmp(w, "disks=", 6) == 0) {
478 /* again, for compat */
479 mis.raid_disks = atoi(w + 6);
480 } else if (strncasecmp(w, "num-devices=", 12) == 0) {
481 /* again, for compat */
482 mis.raid_disks = atoi(w + 12);
483 } else if (strncasecmp(w, "spares=", 7) == 0) {
484 /* for warning if not all spares present */
485 mis.spare_disks = atoi(w + 7);
486 } else if (strncasecmp(w, "metadata=", 9) == 0) {
487 /* style of metadata on the devices. */
488 int i;
489
490 for(i=0; superlist[i] && !mis.st; i++)
491 mis.st = superlist[i]->
492 match_metadata_desc(w + 9);
493
494 if (!mis.st)
495 pr_err("metadata format %s unknown, ignored.\n",
496 w + 9);
497 } else if (strncasecmp(w, "auto=", 5) == 0 ) {
498 /* whether to create device special files as needed */
499 mis.autof = parse_auto(w + 5, "auto type", 0);
500 } else if (strncasecmp(w, "member=", 7) == 0) {
501 /* subarray within a container */
502 mis.member = xstrdup(w + 7);
503 } else if (strncasecmp(w, "container=", 10) == 0) {
504 /* The container holding this subarray.
505 * Either a device name or a uuid */
506 mis.container = xstrdup(w + 10);
507 } else {
508 pr_err("unrecognised word on ARRAY line: %s\n",
509 w);
510 }
511 }
512 if (mis.uuid_set == 0 && mis.devices == NULL &&
513 mis.super_minor == UnSet && mis.name[0] == 0 &&
514 (mis.container == NULL || mis.member == NULL))
515 pr_err("ARRAY line %s has no identity information.\n",
516 mis.devname);
517 else {
518 mi = xmalloc(sizeof(*mi));
519 *mi = mis;
520 mi->devname = mis.devname ? xstrdup(mis.devname) : NULL;
521 mi->next = NULL;
522 *mddevlp = mi;
523 mddevlp = &mi->next;
524 }
525 }
526
527 static char *alert_email = NULL;
528 void mailline(char *line)
529 {
530 char *w;
531
532 for (w = dl_next(line); w != line; w = dl_next(w))
533 if (alert_email == NULL)
534 alert_email = xstrdup(w);
535 }
536
537 static char *alert_mail_from = NULL;
538 void mailfromline(char *line)
539 {
540 char *w;
541
542 for (w = dl_next(line); w != line; w = dl_next(w)) {
543 if (alert_mail_from == NULL)
544 alert_mail_from = xstrdup(w);
545 else {
546 char *t = NULL;
547
548 if (xasprintf(&t, "%s %s", alert_mail_from, w) > 0) {
549 free(alert_mail_from);
550 alert_mail_from = t;
551 }
552 }
553 }
554 }
555
556 static char *alert_program = NULL;
557 void programline(char *line)
558 {
559 char *w;
560
561 for (w = dl_next(line); w != line; w = dl_next(w))
562 if (alert_program == NULL)
563 alert_program = xstrdup(w);
564 }
565
566 static char *home_host = NULL;
567 static int require_homehost = 1;
568 void homehostline(char *line)
569 {
570 char *w;
571
572 for (w = dl_next(line); w != line; w = dl_next(w)) {
573 if (is_devname_ignore(w) == true)
574 require_homehost = 0;
575 else if (home_host == NULL) {
576 if (strcasecmp(w, "<none>") == 0)
577 home_host = xstrdup("");
578 else
579 home_host = xstrdup(w);
580 }
581 }
582 }
583
584 static char *home_cluster = NULL;
585 void homeclusterline(char *line)
586 {
587 char *w;
588
589 for (w = dl_next(line); w != line; w = dl_next(w)) {
590 if (home_cluster == NULL) {
591 if (strcasecmp(w, "<none>") == 0)
592 home_cluster = xstrdup("");
593 else
594 home_cluster = xstrdup(w);
595 }
596 }
597 }
598
599 static int monitor_delay;
600 void monitordelayline(char *line)
601 {
602 char *w;
603
604 for (w = dl_next(line); w != line; w = dl_next(w)) {
605 if (monitor_delay == 0)
606 monitor_delay = strtol(w, NULL, 10);
607 }
608 }
609
610 char auto_yes[] = "yes";
611 char auto_no[] = "no";
612 char auto_homehost[] = "homehost";
613
614 static int auto_seen = 0;
615 void autoline(char *line)
616 {
617 char *w;
618 char *seen;
619 int super_cnt;
620 char *dflt = auto_yes;
621 int homehost = 0;
622 int i;
623
624 if (auto_seen)
625 return;
626 auto_seen = 1;
627
628 /*
629 * Parse the 'auto' line creating policy statements for the 'auto'
630 * policy.
631 *
632 * The default is 'yes' but the 'auto' line might over-ride that.
633 * Words in the line are processed in order with the first
634 * match winning.
635 * word can be:
636 * +version - that version can be assembled
637 * -version - that version cannot be auto-assembled
638 * yes or +all - any other version can be assembled
639 * no or -all - no other version can be assembled.
640 * homehost - any array associated by 'homehost' to this
641 * host can be assembled.
642 *
643 * Thus:
644 * +ddf -0.90 homehost -all
645 * will auto-assemble any ddf array, no 0.90 array, and
646 * any other array (imsm, 1.x) if and only if it is identified
647 * as belonging to this host.
648 *
649 * We translate that to policy by creating 'auto=yes' when we see
650 * a '+version' line, 'auto=no' if we see '-version' before 'homehost',
651 * or 'auto=homehost' if we see '-version' after 'homehost'.
652 * When we see yes, no, +all or -all we stop and any version that hasn't
653 * been seen gets an appropriate auto= entry.
654 */
655
656 /*
657 * If environment variable MDADM_CONF_AUTO is defined, then
658 * it is prepended to the auto line. This allow a script
659 * to easily disable some metadata types.
660 */
661 w = getenv("MDADM_CONF_AUTO");
662 if (w && *w) {
663 char *l = xstrdup(w);
664 char *head = line;
665 w = strtok(l, " \t");
666 while (w) {
667 char *nw = dl_strdup(w);
668 dl_insert(head, nw);
669 head = nw;
670 w = strtok(NULL, " \t");
671 }
672 free(l);
673 }
674
675 for (super_cnt = 0; superlist[super_cnt]; super_cnt++)
676 ;
677 seen = xcalloc(super_cnt, 1);
678
679 for (w = dl_next(line); w != line; w = dl_next(w)) {
680 char *val;
681
682 if (strcasecmp(w, "yes") == 0) {
683 dflt = auto_yes;
684 break;
685 }
686 if (strcasecmp(w, "no") == 0) {
687 if (homehost)
688 dflt = auto_homehost;
689 else
690 dflt = auto_no;
691 break;
692 }
693 if (strcasecmp(w, "homehost") == 0) {
694 homehost = 1;
695 continue;
696 }
697 if (w[0] == '+')
698 val = auto_yes;
699 else if (w[0] == '-') {
700 if (homehost)
701 val = auto_homehost;
702 else
703 val = auto_no;
704 } else
705 continue;
706
707 if (strcasecmp(w + 1, "all") == 0) {
708 dflt = val;
709 break;
710 }
711 for (i = 0; superlist[i]; i++) {
712 const char *version = superlist[i]->name;
713 if (strcasecmp(w + 1, version) == 0)
714 break;
715 /* 1 matches 1.x, 0 matches 0.90 */
716 if (version[1] == '.' && strlen(w + 1) == 1 &&
717 w[1] == version[0])
718 break;
719 /* 1.anything matches 1.x */
720 if (strcmp(version, "1.x") == 0 &&
721 strncmp(w + 1, "1.", 2) == 0)
722 break;
723 }
724 if (superlist[i] == NULL)
725 /* ignore this word */
726 continue;
727 if (seen[i])
728 /* already know about this metadata */
729 continue;
730 policy_add(rule_policy, pol_auto, val, pol_metadata,
731 superlist[i]->name, NULL);
732 seen[i] = 1;
733 }
734 for (i = 0; i < super_cnt; i++)
735 if (!seen[i])
736 policy_add(rule_policy, pol_auto, dflt, pol_metadata,
737 superlist[i]->name, NULL);
738
739 free(seen);
740 }
741
742 int loaded = 0;
743
744 static char *conffile = NULL;
745 void set_conffile(char *file)
746 {
747 conffile = file;
748 }
749
750 void conf_file(FILE *f)
751 {
752 char *line;
753 while ((line = conf_line(f))) {
754 switch(match_keyword(line)) {
755 case Devices:
756 devline(line);
757 break;
758 case Array:
759 arrayline(line);
760 break;
761 case Mailaddr:
762 mailline(line);
763 break;
764 case Mailfrom:
765 mailfromline(line);
766 break;
767 case Program:
768 programline(line);
769 break;
770 case CreateDev:
771 createline(line);
772 break;
773 case Homehost:
774 homehostline(line);
775 break;
776 case HomeCluster:
777 homeclusterline(line);
778 break;
779 case AutoMode:
780 autoline(line);
781 break;
782 case Policy:
783 policyline(line, rule_policy);
784 break;
785 case PartPolicy:
786 policyline(line, rule_part);
787 break;
788 case Sysfs:
789 sysfsline(line);
790 break;
791 case MonitorDelay:
792 monitordelayline(line);
793 break;
794 default:
795 pr_err("Unknown keyword %s\n", line);
796 }
797 free_line(line);
798 }
799 }
800
801 struct fname {
802 struct fname *next;
803 char name[];
804 };
805
806 void conf_file_or_dir(FILE *f)
807 {
808 struct stat st;
809 DIR *dir;
810 struct dirent *dp;
811 struct fname *list = NULL;
812
813 fstat(fileno(f), &st);
814 if (S_ISREG(st.st_mode))
815 conf_file(f);
816 else if (!S_ISDIR(st.st_mode))
817 return;
818 #if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
819 dir = fdopendir(fileno(f));
820 if (!dir)
821 return;
822 while ((dp = readdir(dir)) != NULL) {
823 int l;
824 struct fname *fn, **p;
825 if (dp->d_ino == 0)
826 continue;
827 if (dp->d_name[0] == '.')
828 continue;
829 l = strlen(dp->d_name);
830 if (l < 6 || strcmp(dp->d_name + l - 5, ".conf") != 0)
831 continue;
832 fn = xmalloc(sizeof(*fn) + l + 1);
833 strcpy(fn->name, dp->d_name);
834 for (p = &list;
835 *p && strcmp((*p)->name, fn->name) < 0;
836 p = & (*p)->next)
837 ;
838 fn->next = *p;
839 *p = fn;
840 }
841 while (list) {
842 int fd;
843 FILE *f2;
844 struct fname *fn = list;
845 list = list->next;
846 fd = openat(fileno(f), fn->name, O_RDONLY);
847 free(fn);
848 if (fd < 0)
849 continue;
850 f2 = fdopen(fd, "r");
851 if (!f2) {
852 close(fd);
853 continue;
854 }
855 conf_file(f2);
856 fclose(f2);
857 }
858 closedir(dir);
859 #endif
860 }
861
862 void load_conffile(void)
863 {
864 FILE *f;
865 char *confdir = NULL;
866 char *head;
867
868 if (loaded)
869 return;
870 if (conffile == NULL) {
871 conffile = DefaultConfFile;
872 confdir = DefaultConfDir;
873 }
874
875 if (strcmp(conffile, "partitions") == 0) {
876 char *list = dl_strdup("DEV");
877 dl_init(list);
878 dl_add(list, dl_strdup("partitions"));
879 devline(list);
880 free_line(list);
881 } else if (strcmp(conffile, "none") != 0) {
882 f = fopen(conffile, "r");
883 /* Debian chose to relocate mdadm.conf into /etc/mdadm/.
884 * To allow Debian users to compile from clean source and still
885 * have a working mdadm, we read /etc/mdadm/mdadm.conf
886 * if /etc/mdadm.conf doesn't exist
887 */
888 if (f == NULL && conffile == DefaultConfFile) {
889 f = fopen(DefaultAltConfFile, "r");
890 if (f) {
891 conffile = DefaultAltConfFile;
892 confdir = DefaultAltConfDir;
893 }
894 }
895 if (f) {
896 conf_file_or_dir(f);
897 fclose(f);
898 }
899 if (confdir) {
900 f = fopen(confdir, "r");
901 if (f) {
902 conf_file_or_dir(f);
903 fclose(f);
904 }
905 }
906 }
907 /* If there was no AUTO line, process an empty line
908 * now so that the MDADM_CONF_AUTO env var gets processed.
909 */
910 head = dl_strdup("AUTO");
911 dl_init(head);
912 autoline(head);
913 free_line(head);
914
915 loaded = 1;
916 }
917
918 char *conf_get_mailaddr(void)
919 {
920 load_conffile();
921 return alert_email;
922 }
923
924 char *conf_get_mailfrom(void)
925 {
926 load_conffile();
927 return alert_mail_from;
928 }
929
930 char *conf_get_program(void)
931 {
932 load_conffile();
933 return alert_program;
934 }
935
936 char *conf_get_homehost(int *require_homehostp)
937 {
938 load_conffile();
939 if (require_homehostp)
940 *require_homehostp = require_homehost;
941 return home_host;
942 }
943
944 char *conf_get_homecluster(void)
945 {
946 load_conffile();
947 return home_cluster;
948 }
949
950 int conf_get_monitor_delay(void)
951 {
952 load_conffile();
953 return monitor_delay;
954 }
955
956 struct createinfo *conf_get_create_info(void)
957 {
958 load_conffile();
959 return &createinfo;
960 }
961
962 struct mddev_ident *conf_get_ident(char *dev)
963 {
964 struct mddev_ident *rv;
965 load_conffile();
966 rv = mddevlist;
967 while (dev && rv && (rv->devname == NULL ||
968 !devname_matches(dev, rv->devname)))
969 rv = rv->next;
970 return rv;
971 }
972
973 static void append_dlist(struct mddev_dev **dlp, struct mddev_dev *list)
974 {
975 while (*dlp)
976 dlp = &(*dlp)->next;
977 *dlp = list;
978 }
979
980 struct mddev_dev *conf_get_devs()
981 {
982 glob_t globbuf;
983 struct conf_dev *cd;
984 int flags = 0;
985 static struct mddev_dev *dlist = NULL;
986 unsigned int i;
987
988 while (dlist) {
989 struct mddev_dev *t = dlist;
990 dlist = dlist->next;
991 free(t->devname);
992 free(t);
993 }
994
995 load_conffile();
996
997 if (cdevlist == NULL) {
998 /* default to 'partitions' and 'containers' */
999 dlist = load_partitions();
1000 append_dlist(&dlist, load_containers());
1001 }
1002
1003 for (cd = cdevlist; cd; cd = cd->next) {
1004 if (strcasecmp(cd->name, "partitions") == 0)
1005 append_dlist(&dlist, load_partitions());
1006 else if (strcasecmp(cd->name, "containers") == 0)
1007 append_dlist(&dlist, load_containers());
1008 else {
1009 glob(cd->name, flags, NULL, &globbuf);
1010 flags |= GLOB_APPEND;
1011 }
1012 }
1013 if (flags & GLOB_APPEND) {
1014 for (i = 0; i < globbuf.gl_pathc; i++) {
1015 struct mddev_dev *t;
1016 t = xcalloc(1, sizeof(*t));
1017 t->devname = xstrdup(globbuf.gl_pathv[i]);
1018 t->next = dlist;
1019 dlist = t;
1020 /* printf("one dev is %s\n", t->devname);*/
1021 }
1022 globfree(&globbuf);
1023 }
1024
1025 return dlist;
1026 }
1027
1028 int conf_test_dev(char *devname)
1029 {
1030 struct conf_dev *cd;
1031 if (cdevlist == NULL)
1032 /* allow anything by default */
1033 return 1;
1034 for (cd = cdevlist; cd; cd = cd->next) {
1035 if (strcasecmp(cd->name, "partitions") == 0)
1036 return 1;
1037 if (fnmatch(cd->name, devname, FNM_PATHNAME) == 0)
1038 return 1;
1039 }
1040 return 0;
1041 }
1042
1043 int conf_test_metadata(const char *version, struct dev_policy *pol, int is_homehost)
1044 {
1045 /* If anyone said 'yes', that sticks.
1046 * else if homehost applies, use that
1047 * else if there is a 'no', say 'no'.
1048 * else 'yes'.
1049 */
1050 struct dev_policy *p;
1051 int no = 0, found_homehost = 0;
1052 load_conffile();
1053
1054 pol = pol_find(pol, pol_auto);
1055 pol_for_each(p, pol, version) {
1056 if (strcmp(p->value, "yes") == 0)
1057 return 1;
1058 if (strcmp(p->value, "homehost") == 0)
1059 found_homehost = 1;
1060 if (strcmp(p->value, "no") == 0)
1061 no = 1;
1062 }
1063 if (is_homehost && found_homehost)
1064 return 1;
1065 if (no)
1066 return 0;
1067 return 1;
1068 }
1069
1070 int match_oneof(char *devices, char *devname)
1071 {
1072 /* check if one of the comma separated patterns in devices
1073 * matches devname
1074 */
1075
1076 while (devices && *devices) {
1077 char patn[1024];
1078 char *p = devices;
1079 devices = strchr(devices, ',');
1080 if (!devices)
1081 devices = p + strlen(p);
1082 if (devices-p < 1024) {
1083 strncpy(patn, p, devices - p);
1084 patn[devices-p] = 0;
1085 if (fnmatch(patn, devname, FNM_PATHNAME) == 0)
1086 return 1;
1087 }
1088 if (*devices == ',')
1089 devices++;
1090 }
1091 return 0;
1092 }
1093
1094 int devname_matches(char *name, char *match)
1095 {
1096 /* See if the given array name matches the
1097 * given match from config file.
1098 *
1099 * First strip and /dev/md/ or /dev/, then
1100 * see if there might be a numeric match of
1101 * mdNN with NN
1102 * then just strcmp
1103 */
1104 if (strncmp(name, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0)
1105 name += DEV_MD_DIR_LEN;
1106 else if (strncmp(name, "/dev/", 5) == 0)
1107 name += 5;
1108
1109 if (strncmp(match, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0)
1110 match += DEV_MD_DIR_LEN;
1111 else if (strncmp(match, "/dev/", 5) == 0)
1112 match += 5;
1113
1114 if (strncmp(name, "md", 2) == 0 && isdigit(name[2]))
1115 name += 2;
1116 if (strncmp(match, "md", 2) == 0 && isdigit(match[2]))
1117 match += 2;
1118
1119 return (strcmp(name, match) == 0);
1120 }
1121
1122 int conf_name_is_free(char *name)
1123 {
1124 /* Check if this name is already taken by an ARRAY entry in
1125 * the config file.
1126 * It can be taken either by a match on devname, name, or
1127 * even super-minor.
1128 */
1129 struct mddev_ident *dev;
1130
1131 load_conffile();
1132 for (dev = mddevlist; dev; dev = dev->next) {
1133 char nbuf[100];
1134 if (dev->devname && devname_matches(name, dev->devname))
1135 return 0;
1136 if (dev->name[0] && devname_matches(name, dev->name))
1137 return 0;
1138 sprintf(nbuf, "%d", dev->super_minor);
1139 if (dev->super_minor != UnSet && devname_matches(name, nbuf))
1140 return 0;
1141 }
1142 return 1;
1143 }
1144
1145 struct mddev_ident *conf_match(struct supertype *st,
1146 struct mdinfo *info,
1147 char *devname,
1148 int verbose, int *rvp)
1149 {
1150 struct mddev_ident *array_list, *match;
1151 array_list = conf_get_ident(NULL);
1152 match = NULL;
1153 for (; array_list; array_list = array_list->next) {
1154 if (array_list->uuid_set &&
1155 same_uuid(array_list->uuid, info->uuid,
1156 st->ss->swapuuid) == 0) {
1157 if (verbose >= 2 && array_list->devname)
1158 pr_err("UUID differs from %s.\n",
1159 array_list->devname);
1160 continue;
1161 }
1162 if (array_list->name[0] &&
1163 strcasecmp(array_list->name, info->name) != 0) {
1164 if (verbose >= 2 && array_list->devname)
1165 pr_err("Name differs from %s.\n",
1166 array_list->devname);
1167 continue;
1168 }
1169 if (array_list->devices && devname &&
1170 !match_oneof(array_list->devices, devname)) {
1171 if (verbose >= 2 && array_list->devname)
1172 pr_err("Not a listed device for %s.\n",
1173 array_list->devname);
1174 continue;
1175 }
1176 if (array_list->super_minor != UnSet &&
1177 array_list->super_minor != info->array.md_minor) {
1178 if (verbose >= 2 && array_list->devname)
1179 pr_err("Different super-minor to %s.\n",
1180 array_list->devname);
1181 continue;
1182 }
1183 if (!array_list->uuid_set && !array_list->name[0] &&
1184 !array_list->devices && array_list->super_minor == UnSet) {
1185 if (verbose >= 2 && array_list->devname)
1186 pr_err("%s doesn't have any identifying information.\n",
1187 array_list->devname);
1188 continue;
1189 }
1190 /* FIXME, should I check raid_disks and level too?? */
1191
1192 if (match) {
1193 if (verbose >= 0) {
1194 if (match->devname && array_list->devname)
1195 pr_err("we match both %s and %s - cannot decide which to use.\n",
1196 match->devname,
1197 array_list->devname);
1198 else
1199 pr_err("multiple lines in mdadm.conf match\n");
1200 }
1201 if (rvp)
1202 *rvp = 2;
1203 match = NULL;
1204 break;
1205 }
1206 match = array_list;
1207 }
1208 return match;
1209 }
1210
1211 int conf_verify_devnames(struct mddev_ident *array_list)
1212 {
1213 struct mddev_ident *a1, *a2;
1214
1215 for (a1 = array_list; a1; a1 = a1->next) {
1216 if (!a1->devname)
1217 continue;
1218 if (strcmp(a1->devname, "<ignore>") == 0)
1219 continue;
1220 for (a2 = a1->next; a2; a2 = a2->next) {
1221 if (!a2->devname)
1222 continue;
1223 if (strcmp(a1->devname, a2->devname) != 0)
1224 continue;
1225
1226 if (a1->uuid_set && a2->uuid_set) {
1227 char nbuf[64];
1228 __fname_from_uuid(a1->uuid, 0, nbuf, ':');
1229 pr_err("Devices %s and ",
1230 nbuf);
1231 __fname_from_uuid(a2->uuid, 0, nbuf, ':');
1232 fprintf(stderr,
1233 "%s have the same name: %s\n",
1234 nbuf, a1->devname);
1235 } else
1236 pr_err("Device %s given twice in config file\n", a1->devname);
1237 return 1;
1238 }
1239 }
1240
1241 return 0;
1242 }