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