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