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