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