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