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