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