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