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