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