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