]> git.ipfire.org Git - thirdparty/mdadm.git/blame - config.c
config: support MDADM_CONF_AUTO= env var.
[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
3c6e95c1 510 for (w=dl_next(line); w != line ; w=dl_next(w))
e0d19036 511 if (alert_email == NULL)
503975b9 512 alert_email = xstrdup(w);
e0d19036
NB
513}
514
4948b8f7
NB
515static char *alert_mail_from = NULL;
516void mailfromline(char *line)
517{
518 char *w;
519
520 for (w=dl_next(line); w != line ; w=dl_next(w)) {
521 if (alert_mail_from == NULL)
503975b9 522 alert_mail_from = xstrdup(w);
4948b8f7 523 else {
3d2c4fc7
DW
524 char *t = NULL;
525
78fbcc10 526 if (xasprintf(&t, "%s %s", alert_mail_from, w) > 0) {
3d2c4fc7
DW
527 free(alert_mail_from);
528 alert_mail_from = t;
529 }
4948b8f7
NB
530 }
531 }
532}
533
e0d19036
NB
534static char *alert_program = NULL;
535void programline(char *line)
536{
537 char *w;
538
3c6e95c1 539 for (w=dl_next(line); w != line ; w=dl_next(w))
e0d19036 540 if (alert_program == NULL)
503975b9 541 alert_program = xstrdup(w);
e0d19036
NB
542}
543
997aed5d 544static char *home_host = NULL;
0ac91628 545static int require_homehost = 1;
997aed5d
NB
546void homehostline(char *line)
547{
548 char *w;
549
550 for (w=dl_next(line); w != line ; w=dl_next(w)) {
0ac91628
N
551 if (strcasecmp(w, "<ignore>")==0)
552 require_homehost = 0;
0f23aa88
N
553 else if (home_host == NULL) {
554 if (strcasecmp(w, "<none>")==0)
503975b9 555 home_host = xstrdup("");
0f23aa88 556 else
503975b9 557 home_host = xstrdup(w);
3c6e95c1 558 }
997aed5d
NB
559 }
560}
561
4e8d9f0a
N
562char auto_yes[] = "yes";
563char auto_no[] = "no";
564char auto_homehost[] = "homehost";
565
566static int auto_seen = 0;
31015d57
N
567void autoline(char *line)
568{
4d0b563b 569 char *w;
4e8d9f0a
N
570 char *seen;
571 int super_cnt;
572 char *dflt = auto_yes;
573 int homehost = 0;
574 int i;
4d0b563b 575
3c6e95c1 576 if (auto_seen)
31015d57 577 return;
3c6e95c1 578
4e8d9f0a
N
579 /* Parse the 'auto' line creating policy statements for the 'auto' policy.
580 *
581 * The default is 'yes' but the 'auto' line might over-ride that.
582 * Words in the line are processed in order with the first
583 * match winning.
584 * word can be:
585 * +version - that version can be assembled
586 * -version - that version cannot be auto-assembled
587 * yes or +all - any other version can be assembled
588 * no or -all - no other version can be assembled.
589 * homehost - any array associated by 'homehost' to this
590 * host can be assembled.
591 *
592 * Thus:
593 * +ddf -0.90 homehost -all
594 * will auto-assemble any ddf array, no 0.90 array, and
595 * any other array (imsm, 1.x) if and only if it is identified
596 * as belonging to this host.
597 *
598 * We translate that to policy by creating 'auto=yes' when we see
599 * a '+version' line, 'auto=no' if we see '-version' before 'homehost',
600 * or 'auto=homehost' if we see '-version' after 'homehost'.
b10c663e 601 * When we see yes, no, +all or -all we stop and any version that hasn't
4e8d9f0a
N
602 * been seen gets an appropriate auto= entry.
603 */
4d0b563b 604
2dfb675b
N
605 /* If environment variable MDADM_CONF_AUTO is defined, then
606 * it is prepended to the auto line. This allow a script
607 * to easily disable some metadata types.
608 */
609 w = getenv("MDADM_CONF_AUTO");
610 if (w && *w) {
611 char *l = xstrdup(w);
612 char *head = line;
613 w = strtok(l, " \t");
614 while (w) {
615 char *nw = dl_strdup(w);
616 dl_insert(head, nw);
617 head = nw;
618 w = strtok(NULL, " \t");
619 }
620 free(l);
621 }
622
4e8d9f0a
N
623 for (super_cnt = 0; superlist[super_cnt]; super_cnt++)
624 ;
503975b9 625 seen = xcalloc(super_cnt, 1);
4d0b563b 626
4e8d9f0a
N
627 for (w = dl_next(line); w != line ; w = dl_next(w)) {
628 char *val;
629
630 if (strcasecmp(w, "yes") == 0) {
631 dflt = auto_yes;
632 break;
633 }
634 if (strcasecmp(w, "no") == 0) {
635 if (homehost)
636 dflt = auto_homehost;
637 else
638 dflt = auto_no;
639 break;
640 }
641 if (strcasecmp(w, "homehost") == 0) {
642 homehost = 1;
643 continue;
644 }
645 if (w[0] == '+')
646 val = auto_yes;
647 else if (w[0] == '-') {
648 if (homehost)
649 val = auto_homehost;
650 else
651 val = auto_no;
652 } else
653 continue;
654
655 if (strcasecmp(w+1, "all") == 0) {
656 dflt = val;
657 break;
658 }
659 for (i = 0; superlist[i]; i++) {
660 const char *version = superlist[i]->name;
661 if (strcasecmp(w+1, version) == 0)
662 break;
663 /* 1 matches 1.x, 0 matches 0.90 */
664 if (version[1] == '.' &&
665 strlen(w+1) == 1 &&
666 w[1] == version[0])
667 break;
668 /* 1.anything matches 1.x */
669 if (strcmp(version, "1.x") == 0 &&
670 strncmp(w+1, "1.", 2) == 0)
671 break;
672 }
673 if (superlist[i] == NULL)
674 /* ignore this word */
675 continue;
676 if (seen[i])
677 /* already know about this metadata */
678 continue;
679 policy_add(rule_policy, pol_auto, val, pol_metadata, superlist[i]->name, NULL);
680 seen[i] = 1;
4d0b563b 681 }
4e8d9f0a
N
682 for (i = 0; i < super_cnt; i++)
683 if (!seen[i])
684 policy_add(rule_policy, pol_auto, dflt, pol_metadata, superlist[i]->name, NULL);
42de2ac2
TJ
685
686 free(seen);
31015d57 687}
e0d19036 688
82b27616
NB
689int loaded = 0;
690
8aec876d
NB
691static char *conffile = NULL;
692void set_conffile(char *file)
693{
694 conffile = file;
695}
696
15af9692 697void conf_file(FILE *f)
82b27616 698{
52826846 699 char *line;
52826846
NB
700 while ((line=conf_line(f))) {
701 switch(match_keyword(line)) {
8fe9db0f 702 case Devices:
52826846
NB
703 devline(line);
704 break;
8fe9db0f 705 case Array:
52826846
NB
706 arrayline(line);
707 break;
8fe9db0f 708 case Mailaddr:
e0d19036
NB
709 mailline(line);
710 break;
8fe9db0f 711 case Mailfrom:
4948b8f7
NB
712 mailfromline(line);
713 break;
8fe9db0f
NB
714 case Program:
715 programline(line);
716 break;
717 case CreateDev:
5bbb4842
NB
718 createline(line);
719 break;
997aed5d
NB
720 case Homehost:
721 homehostline(line);
722 break;
31015d57
N
723 case AutoMode:
724 autoline(line);
725 break;
5527fc74
N
726 case Policy:
727 policyline(line, rule_policy);
728 break;
729 case PartPolicy:
730 policyline(line, rule_part);
731 break;
52826846 732 default:
e7b84f9d 733 pr_err("Unknown keyword %s\n", line);
52826846
NB
734 }
735 free_line(line);
82b27616 736 }
15af9692
N
737}
738
343b7e75
N
739struct fname {
740 struct fname *next;
741 char name[];
742};
743
744void conf_file_or_dir(FILE *f)
745{
746 struct stat st;
747 DIR *dir;
748 struct dirent *dp;
749 struct fname *list = NULL;
750
751 fstat(fileno(f), &st);
752 if (S_ISREG(st.st_mode))
753 conf_file(f);
754 else if (!S_ISDIR(st.st_mode))
755 return;
756#if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
757 dir = fdopendir(fileno(f));
758 if (!dir)
759 return;
760 while ((dp = readdir(dir)) != NULL) {
761 int l;
762 struct fname *fn, **p;
763 if (dp->d_ino == 0)
764 continue;
765 if (dp->d_name[0] == '.')
766 continue;
767 l = strlen(dp->d_name);
768 if (l < 6 || strcmp(dp->d_name+l-5, ".conf") != 0)
769 continue;
770 fn = xmalloc(sizeof(*fn)+l+1);
771 strcpy(fn->name, dp->d_name);
772 for (p = &list;
773 *p && strcmp((*p)->name, fn->name) < 0;
774 p = & (*p)->next)
775 ;
776 fn->next = *p;
777 *p = fn;
778 }
779 while (list) {
780 int fd;
781 FILE *f2;
782 struct fname *fn = list;
783 list = list->next;
784 fd = openat(fileno(f), fn->name, O_RDONLY);
785 free(fn);
786 if (fd < 0)
787 continue;
788 f2 = fdopen(fd, "r");
789 if (!f2) {
790 close(fd);
791 continue;
792 }
793 conf_file(f2);
794 fclose(f2);
795 }
796 closedir(dir);
797#endif
798}
799
15af9692
N
800void load_conffile(void)
801{
802 FILE *f;
9dc70cbc 803 char *confdir = NULL;
2dfb675b 804 char *head;
15af9692 805
824e5727
N
806 if (loaded)
807 return;
9dc70cbc 808 if (conffile == NULL) {
15af9692 809 conffile = DefaultConfFile;
9dc70cbc
N
810 confdir = DefaultConfDir;
811 }
15af9692 812
15af9692
N
813 if (strcmp(conffile, "partitions")==0) {
814 char *list = dl_strdup("DEV");
815 dl_init(list);
816 dl_add(list, dl_strdup("partitions"));
817 devline(list);
818 free_line(list);
824e5727
N
819 } else if (strcmp(conffile, "none") != 0) {
820 f = fopen(conffile, "r");
821 /* Debian chose to relocate mdadm.conf into /etc/mdadm/.
822 * To allow Debian users to compile from clean source and still
823 * have a working mdadm, we read /etc/mdadm/mdadm.conf
824 * if /etc/mdadm.conf doesn't exist
825 */
826 if (f == NULL &&
827 conffile == DefaultConfFile) {
828 f = fopen(DefaultAltConfFile, "r");
829 if (f) {
830 conffile = DefaultAltConfFile;
831 confdir = DefaultAltConfDir;
832 }
9dc70cbc 833 }
9dc70cbc 834 if (f) {
9dc70cbc
N
835 conf_file_or_dir(f);
836 fclose(f);
837 }
824e5727
N
838 if (confdir) {
839 f = fopen(confdir, "r");
840 if (f) {
841 conf_file_or_dir(f);
842 fclose(f);
843 }
844 }
15af9692 845 }
2dfb675b
N
846 /* If there was no AUTO line, process an empty line
847 * now so that the MDADM_CONF_AUTO env var gets processed.
848 */
849 head = dl_strdup("AUTO");
850 dl_init(head);
851 autoline(head);
852 free_line(head);
853
824e5727 854 loaded = 1;
82b27616
NB
855}
856
8aec876d 857char *conf_get_mailaddr(void)
e0d19036 858{
8aec876d 859 load_conffile();
e0d19036
NB
860 return alert_email;
861}
862
8aec876d 863char *conf_get_mailfrom(void)
4948b8f7 864{
8aec876d 865 load_conffile();
4948b8f7
NB
866 return alert_mail_from;
867}
868
8aec876d 869char *conf_get_program(void)
e0d19036 870{
8aec876d 871 load_conffile();
e0d19036
NB
872 return alert_program;
873}
874
0ac91628 875char *conf_get_homehost(int *require_homehostp)
997aed5d 876{
8aec876d 877 load_conffile();
0ac91628
N
878 if (require_homehostp)
879 *require_homehostp = require_homehost;
997aed5d
NB
880 return home_host;
881}
882
8aec876d 883struct createinfo *conf_get_create_info(void)
5bbb4842 884{
8aec876d 885 load_conffile();
5bbb4842
NB
886 return &createinfo;
887}
82b27616 888
fa56eddb 889struct mddev_ident *conf_get_ident(char *dev)
64c4757e 890{
fa56eddb 891 struct mddev_ident *rv;
8aec876d 892 load_conffile();
52826846 893 rv = mddevlist;
fe056d1f 894 while (dev && rv && (rv->devname == NULL
5c4c9ab1 895 || !devname_matches(dev, rv->devname)))
52826846
NB
896 rv = rv->next;
897 return rv;
64c4757e
NB
898}
899
a655e550 900static void append_dlist(struct mddev_dev **dlp, struct mddev_dev *list)
f64165f7
DW
901{
902 while (*dlp)
903 dlp = &(*dlp)->next;
904 *dlp = list;
905}
906
a655e550 907struct mddev_dev *conf_get_devs()
64c4757e 908{
52826846
NB
909 glob_t globbuf;
910 struct conf_dev *cd;
911 int flags = 0;
a655e550 912 static struct mddev_dev *dlist = NULL;
98c6faba 913 unsigned int i;
52826846
NB
914
915 while (dlist) {
a655e550 916 struct mddev_dev *t = dlist;
52826846
NB
917 dlist = dlist->next;
918 free(t->devname);
919 free(t);
920 }
aba69144 921
8aec876d 922 load_conffile();
a99d6b66 923
f8f84cd5
DW
924 if (cdevlist == NULL) {
925 /* default to 'partitions' and 'containers' */
a99d6b66 926 dlist = load_partitions();
f8f84cd5
DW
927 append_dlist(&dlist, load_containers());
928 }
a99d6b66 929
52826846 930 for (cd=cdevlist; cd; cd=cd->next) {
f64165f7
DW
931 if (strcasecmp(cd->name, "partitions")==0)
932 append_dlist(&dlist, load_partitions());
933 else if (strcasecmp(cd->name, "containers")==0)
934 append_dlist(&dlist, load_containers());
057bd352
NB
935 else {
936 glob(cd->name, flags, NULL, &globbuf);
937 flags |= GLOB_APPEND;
938 }
52826846 939 }
e0d19036
NB
940 if (flags & GLOB_APPEND) {
941 for (i=0; i<globbuf.gl_pathc; i++) {
503975b9 942 struct mddev_dev *t = xmalloc(sizeof(*t));
5e73b024 943 memset(t, 0, sizeof(*t));
503975b9 944 t->devname = xstrdup(globbuf.gl_pathv[i]);
e0d19036
NB
945 t->next = dlist;
946 dlist = t;
82b27616 947/* printf("one dev is %s\n", t->devname);*/
e0d19036
NB
948 }
949 globfree(&globbuf);
52826846 950 }
82b27616 951
52826846 952 return dlist;
64c4757e
NB
953}
954
8382f19b
NB
955int conf_test_dev(char *devname)
956{
957 struct conf_dev *cd;
958 if (cdevlist == NULL)
959 /* allow anything by default */
960 return 1;
961 for (cd = cdevlist ; cd ; cd = cd->next) {
962 if (strcasecmp(cd->name, "partitions") == 0)
963 return 1;
964 if (fnmatch(cd->name, devname, FNM_PATHNAME) == 0)
965 return 1;
966 }
967 return 0;
968}
969
4e8d9f0a 970int conf_test_metadata(const char *version, struct dev_policy *pol, int is_homehost)
31015d57 971{
4e8d9f0a
N
972 /* If anyone said 'yes', that sticks.
973 * else if homehost applies, use that
974 * else if there is a 'no', say 'no'.
975 * else 'yes'.
31015d57 976 */
4e8d9f0a 977 struct dev_policy *p;
b10c663e 978 int no=0, found_homehost=0;
31015d57 979 load_conffile();
4e8d9f0a
N
980
981 pol = pol_find(pol, pol_auto);
982 pol_for_each(p, pol, version) {
983 if (strcmp(p->value, "yes") == 0)
31015d57 984 return 1;
b10c663e
N
985 if (strcmp(p->value, "homehost") == 0)
986 found_homehost = 1;
4e8d9f0a
N
987 if (strcmp(p->value, "no") == 0)
988 no = 1;
31015d57 989 }
b10c663e 990 if (is_homehost && found_homehost)
4e8d9f0a
N
991 return 1;
992 if (no)
993 return 0;
31015d57
N
994 return 1;
995}
8382f19b 996
52826846
NB
997int match_oneof(char *devices, char *devname)
998{
5d500228
N
999 /* check if one of the comma separated patterns in devices
1000 * matches devname
1001 */
1002
1003 while (devices && *devices) {
1004 char patn[1024];
1005 char *p = devices;
1006 devices = strchr(devices, ',');
1007 if (!devices)
1008 devices = p + strlen(p);
1009 if (devices-p < 1024) {
1010 strncpy(patn, p, devices-p);
1011 patn[devices-p] = 0;
1012 if (fnmatch(patn, devname, FNM_PATHNAME)==0)
1013 return 1;
1014 }
1015 if (*devices == ',')
1016 devices++;
52826846 1017 }
5d500228 1018 return 0;
52826846 1019}
0ac91628
N
1020
1021int devname_matches(char *name, char *match)
1022{
1023 /* See if the given array name matches the
1024 * given match from config file.
1025 *
1026 * First strip and /dev/md/ or /dev/, then
1027 * see if there might be a numeric match of
1028 * mdNN with NN
1029 * then just strcmp
1030 */
1031 if (strncmp(name, "/dev/md/", 8) == 0)
1032 name += 8;
1033 else if (strncmp(name, "/dev/", 5) == 0)
1034 name += 5;
1035
1036 if (strncmp(match, "/dev/md/", 8) == 0)
1037 match += 8;
1038 else if (strncmp(match, "/dev/", 5) == 0)
1039 match += 5;
1040
0ac91628
N
1041 if (strncmp(name, "md", 2) == 0 &&
1042 isdigit(name[2]))
1043 name += 2;
1044 if (strncmp(match, "md", 2) == 0 &&
1045 isdigit(match[2]))
1046 match += 2;
1047
1048 return (strcmp(name, match) == 0);
1049}
1050
1051int conf_name_is_free(char *name)
1052{
4dd2df09 1053 /* Check if this name is already taken by an ARRAY entry in
0ac91628
N
1054 * the config file.
1055 * It can be taken either by a match on devname, name, or
1056 * even super-minor.
1057 */
fa56eddb 1058 struct mddev_ident *dev;
0ac91628
N
1059
1060 load_conffile();
1061 for (dev = mddevlist; dev; dev = dev->next) {
1062 char nbuf[100];
1063 if (dev->devname && devname_matches(name, dev->devname))
1064 return 0;
1065 if (dev->name[0] && devname_matches(name, dev->name))
1066 return 0;
1067 sprintf(nbuf, "%d", dev->super_minor);
1068 if (dev->super_minor != UnSet &&
1069 devname_matches(name, nbuf))
1070 return 0;
1071 }
1072 return 1;
1073}
360b4636 1074
2244d1a9
N
1075struct mddev_ident *conf_match(struct supertype *st,
1076 struct mdinfo *info,
1077 char *devname,
1078 int verbose, int *rvp)
360b4636 1079{
fa56eddb 1080 struct mddev_ident *array_list, *match;
360b4636
N
1081 array_list = conf_get_ident(NULL);
1082 match = NULL;
1083 for (; array_list; array_list = array_list->next) {
1084 if (array_list->uuid_set &&
1085 same_uuid(array_list->uuid, info->uuid, st->ss->swapuuid)
1086 == 0) {
1087 if (verbose >= 2 && array_list->devname)
e7b84f9d
N
1088 pr_err("UUID differs from %s.\n",
1089 array_list->devname);
360b4636
N
1090 continue;
1091 }
1092 if (array_list->name[0] &&
1093 strcasecmp(array_list->name, info->name) != 0) {
1094 if (verbose >= 2 && array_list->devname)
e7b84f9d
N
1095 pr_err("Name differs from %s.\n",
1096 array_list->devname);
360b4636
N
1097 continue;
1098 }
9f1b0f0f 1099 if (array_list->devices && devname &&
360b4636
N
1100 !match_oneof(array_list->devices, devname)) {
1101 if (verbose >= 2 && array_list->devname)
e7b84f9d
N
1102 pr_err("Not a listed device for %s.\n",
1103 array_list->devname);
360b4636
N
1104 continue;
1105 }
1106 if (array_list->super_minor != UnSet &&
1107 array_list->super_minor != info->array.md_minor) {
1108 if (verbose >= 2 && array_list->devname)
e7b84f9d
N
1109 pr_err("Different super-minor to %s.\n",
1110 array_list->devname);
360b4636
N
1111 continue;
1112 }
1113 if (!array_list->uuid_set &&
1114 !array_list->name[0] &&
1115 !array_list->devices &&
1116 array_list->super_minor == UnSet) {
1117 if (verbose >= 2 && array_list->devname)
e7b84f9d
N
1118 pr_err("%s doesn't have any identifying"
1119 " information.\n",
1120 array_list->devname);
360b4636
N
1121 continue;
1122 }
1123 /* FIXME, should I check raid_disks and level too?? */
1124
1125 if (match) {
1126 if (verbose >= 0) {
1127 if (match->devname && array_list->devname)
e7b84f9d
N
1128 pr_err("we match both %s and %s - "
1129 "cannot decide which to use.\n",
1130 match->devname,
1131 array_list->devname);
360b4636 1132 else
e7b84f9d
N
1133 pr_err("multiple lines in mdadm.conf"
1134 " match\n");
360b4636 1135 }
2244d1a9
N
1136 if (rvp)
1137 *rvp = 2;
1138 match = NULL;
1139 break;
360b4636
N
1140 }
1141 match = array_list;
1142 }
1143 return match;
1144}
7c336758
LO
1145
1146int conf_verify_devnames(struct mddev_ident *array_list)
1147{
1148 struct mddev_ident *a1, *a2;
1149
1150 for (a1 = array_list; a1; a1 = a1->next) {
1151 if (!a1->devname)
1152 continue;
13f2dd6b
N
1153 if (strcmp(a1->devname, "<ignore>") == 0)
1154 continue;
7c336758
LO
1155 for (a2 = a1->next; a2; a2 = a2->next) {
1156 if (!a2->devname)
1157 continue;
1158 if (strcmp(a1->devname, a2->devname) != 0)
1159 continue;
1160
1161 if (a1->uuid_set && a2->uuid_set) {
1162 char nbuf[64];
1163 __fname_from_uuid(a1->uuid, 0, nbuf, ':');
e7b84f9d
N
1164 pr_err("Devices %s and ",
1165 nbuf);
7c336758
LO
1166 __fname_from_uuid(a2->uuid, 0, nbuf, ':');
1167 fprintf(stderr,
1168 "%s have the same name: %s\n",
1169 nbuf, a1->devname);
1170 } else
e7b84f9d 1171 pr_err("Device %s given twice"
7c336758
LO
1172 " in config file\n", a1->devname);
1173 return 1;
1174 }
1175 }
1176
1177 return 0;
1178}