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