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