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