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