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