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