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