]>
Commit | Line | Data |
---|---|---|
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 | 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 | [LTEnd] = NULL | |
99 | }; | |
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 | ||
106 | int match_keyword(char *word) | |
107 | { | |
108 | int len = strlen(word); | |
109 | int n; | |
110 | ||
111 | if (len < 3) | |
112 | return -1; | |
113 | for (n = 0; keywords[n]; n++) { | |
114 | if (strncasecmp(word, keywords[n], len) == 0) | |
115 | return n; | |
116 | } | |
117 | ||
118 | return -1; | |
119 | } | |
120 | ||
121 | struct conf_dev { | |
122 | struct conf_dev *next; | |
123 | char *name; | |
124 | } *cdevlist = NULL; | |
125 | ||
126 | struct mddev_dev *load_partitions(void) | |
127 | { | |
128 | FILE *f = fopen("/proc/partitions", "r"); | |
129 | char buf[1024]; | |
130 | struct mddev_dev *rv = NULL; | |
131 | ||
132 | if (f == NULL) { | |
133 | pr_err("cannot open /proc/partitions\n"); | |
134 | return NULL; | |
135 | } | |
136 | while (fgets(buf, 1024, f)) { | |
137 | int major, minor; | |
138 | char *name, *mp; | |
139 | struct mddev_dev *d; | |
140 | ||
141 | buf[1023] = '\0'; | |
142 | if (buf[0] != ' ') | |
143 | continue; | |
144 | major = strtoul(buf, &mp, 10); | |
145 | if (mp == buf || *mp != ' ') | |
146 | continue; | |
147 | minor = strtoul(mp, NULL, 10); | |
148 | ||
149 | name = map_dev(major, minor, 1); | |
150 | if (!name) | |
151 | continue; | |
152 | d = xcalloc(1, sizeof(*d)); | |
153 | d->devname = xstrdup(name); | |
154 | d->next = rv; | |
155 | rv = d; | |
156 | } | |
157 | fclose(f); | |
158 | return rv; | |
159 | } | |
160 | ||
161 | struct mddev_dev *load_containers(void) | |
162 | { | |
163 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
164 | struct mdstat_ent *ent; | |
165 | struct mddev_dev *d; | |
166 | struct mddev_dev *rv = NULL; | |
167 | struct map_ent *map = NULL, *me; | |
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])) { | |
176 | d = xcalloc(1, sizeof(*d)); | |
177 | me = map_by_devnm(&map, ent->devnm); | |
178 | if (me) | |
179 | d->devname = xstrdup(me->path); | |
180 | else if (asprintf(&d->devname, "/dev/%s", ent->devnm) < 0) { | |
181 | free(d); | |
182 | continue; | |
183 | } | |
184 | d->next = rv; | |
185 | rv = d; | |
186 | map_free(map); | |
187 | map = NULL; | |
188 | } | |
189 | free_mdstat(mdstat); | |
190 | ||
191 | return rv; | |
192 | } | |
193 | ||
194 | struct createinfo createinfo = { | |
195 | .autof = 2, /* by default, create devices with standard names */ | |
196 | .symlinks = 1, | |
197 | .names = 0, /* By default, stick with numbered md devices. */ | |
198 | .bblist = 1, /* Use a bad block list by default */ | |
199 | #ifdef DEBIAN | |
200 | .gid = 6, /* disk */ | |
201 | .mode = 0660, | |
202 | #else | |
203 | .mode = 0600, | |
204 | #endif | |
205 | }; | |
206 | ||
207 | int parse_auto(char *str, char *msg, int config) | |
208 | { | |
209 | int autof; | |
210 | if (str == NULL || *str == 0) | |
211 | autof = 2; | |
212 | else if (strcasecmp(str, "no") == 0) | |
213 | autof = 1; | |
214 | else if (strcasecmp(str, "yes") == 0) | |
215 | autof = 2; | |
216 | else if (strcasecmp(str, "md") == 0) | |
217 | autof = config ? 5:3; | |
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); | |
227 | if (num <= 0) | |
228 | num = 1; | |
229 | } | |
230 | if (e > str && e[-1] == '-') | |
231 | e--; | |
232 | len = e - str; | |
233 | if ((len == 2 && strncasecmp(str, "md", 2) == 0)) { | |
234 | autof = config ? 5 : 3; | |
235 | } else if ((len == 3 && strncasecmp(str, "yes", 3) == 0)) { | |
236 | autof = 2; | |
237 | } else if ((len == 3 && strncasecmp(str, "mdp", 3) == 0)) { | |
238 | autof = config ? 6 : 4; | |
239 | } else if ((len == 1 && strncasecmp(str, "p", 1) == 0) || | |
240 | (len >= 4 && strncasecmp(str, "part", 4) == 0)) { | |
241 | autof = 6; | |
242 | } else { | |
243 | pr_err("%s arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n" | |
244 | " optionally followed by a number.\n", | |
245 | msg, str); | |
246 | exit(2); | |
247 | } | |
248 | autof |= num << 3; | |
249 | } | |
250 | return autof; | |
251 | } | |
252 | ||
253 | static void createline(char *line) | |
254 | { | |
255 | char *w; | |
256 | char *ep; | |
257 | ||
258 | for (w = dl_next(line); w != line; w = dl_next(w)) { | |
259 | if (strncasecmp(w, "auto=", 5) == 0) | |
260 | createinfo.autof = parse_auto(w + 5, "auto=", 1); | |
261 | else if (strncasecmp(w, "owner=", 6) == 0) { | |
262 | if (w[6] == 0) { | |
263 | pr_err("missing owner name\n"); | |
264 | continue; | |
265 | } | |
266 | createinfo.uid = strtoul(w + 6, &ep, 10); | |
267 | if (*ep != 0) { | |
268 | struct passwd *pw; | |
269 | /* must be a name */ | |
270 | pw = getpwnam(w + 6); | |
271 | if (pw) | |
272 | createinfo.uid = pw->pw_uid; | |
273 | else | |
274 | pr_err("CREATE user %s not found\n", | |
275 | w + 6); | |
276 | } | |
277 | } else if (strncasecmp(w, "group=", 6) == 0) { | |
278 | if (w[6] == 0) { | |
279 | pr_err("missing group name\n"); | |
280 | continue; | |
281 | } | |
282 | createinfo.gid = strtoul(w + 6, &ep, 10); | |
283 | if (*ep != 0) { | |
284 | struct group *gr; | |
285 | /* must be a name */ | |
286 | gr = getgrnam(w + 6); | |
287 | if (gr) | |
288 | createinfo.gid = gr->gr_gid; | |
289 | else | |
290 | pr_err("CREATE group %s not found\n", | |
291 | w + 6); | |
292 | } | |
293 | } else if (strncasecmp(w, "mode=", 5) == 0) { | |
294 | if (w[5] == 0) { | |
295 | pr_err("missing CREATE mode\n"); | |
296 | continue; | |
297 | } | |
298 | createinfo.mode = strtoul(w + 5, &ep, 8); | |
299 | if (*ep != 0) { | |
300 | createinfo.mode = 0600; | |
301 | pr_err("unrecognised CREATE mode %s\n", | |
302 | w + 5); | |
303 | } | |
304 | } else if (strncasecmp(w, "metadata=", 9) == 0) { | |
305 | /* style of metadata to use by default */ | |
306 | int i; | |
307 | for (i = 0; superlist[i] && !createinfo.supertype; i++) | |
308 | createinfo.supertype = superlist[i]->match_metadata_desc(w + 9); | |
309 | if (!createinfo.supertype) | |
310 | pr_err("metadata format %s unknown, ignoring\n", | |
311 | w+9); | |
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; | |
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; | |
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; | |
324 | else { | |
325 | pr_err("unrecognised word on CREATE line: %s\n", | |
326 | w); | |
327 | } | |
328 | } | |
329 | } | |
330 | ||
331 | void devline(char *line) | |
332 | { | |
333 | char *w; | |
334 | struct conf_dev *cd; | |
335 | ||
336 | for (w = dl_next(line); w != line; w = dl_next(w)) { | |
337 | if (w[0] == '/' || strcasecmp(w, "partitions") == 0 || | |
338 | strcasecmp(w, "containers") == 0) { | |
339 | cd = xmalloc(sizeof(*cd)); | |
340 | cd->name = xstrdup(w); | |
341 | cd->next = cdevlist; | |
342 | cdevlist = cd; | |
343 | } else { | |
344 | pr_err("unreconised word on DEVICE line: %s\n", w); | |
345 | } | |
346 | } | |
347 | } | |
348 | ||
349 | struct mddev_ident *mddevlist = NULL; | |
350 | struct mddev_ident **mddevlp = &mddevlist; | |
351 | ||
352 | static 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 | ||
363 | void arrayline(char *line) | |
364 | { | |
365 | char *w; | |
366 | ||
367 | struct mddev_ident mis; | |
368 | struct mddev_ident *mi; | |
369 | ||
370 | mis.uuid_set = 0; | |
371 | mis.super_minor = UnSet; | |
372 | mis.level = UnSet; | |
373 | mis.raid_disks = UnSet; | |
374 | mis.spare_disks = 0; | |
375 | mis.devices = NULL; | |
376 | mis.devname = NULL; | |
377 | mis.spare_group = NULL; | |
378 | mis.autof = 0; | |
379 | mis.next = NULL; | |
380 | mis.st = NULL; | |
381 | mis.bitmap_fd = -1; | |
382 | mis.bitmap_file = NULL; | |
383 | mis.name[0] = 0; | |
384 | mis.container = NULL; | |
385 | mis.member = NULL; | |
386 | ||
387 | for (w = dl_next(line); w != line; w = dl_next(w)) { | |
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] != '<') || | |
401 | (strncmp(w, "/dev/md", 7) == 0 && | |
402 | is_number(w + 7)) || | |
403 | (strncmp(w, "/dev/md_d", 9) == 0 && | |
404 | is_number(w + 9))) { | |
405 | /* This is acceptable */; | |
406 | if (mis.devname) | |
407 | pr_err("only give one device per ARRAY line: %s and %s\n", | |
408 | mis.devname, w); | |
409 | else | |
410 | mis.devname = w; | |
411 | }else { | |
412 | pr_err("%s is an invalid name for an md device - ignored.\n", w); | |
413 | } | |
414 | } else if (strncasecmp(w, "uuid=", 5) == 0) { | |
415 | if (mis.uuid_set) | |
416 | pr_err("only specify uuid once, %s ignored.\n", | |
417 | w); | |
418 | else { | |
419 | if (parse_uuid(w + 5, mis.uuid)) | |
420 | mis.uuid_set = 1; | |
421 | else | |
422 | pr_err("bad uuid: %s\n", w); | |
423 | } | |
424 | } else if (strncasecmp(w, "super-minor=", 12) == 0) { | |
425 | if (mis.super_minor != UnSet) | |
426 | pr_err("only specify super-minor once, %s ignored.\n", | |
427 | w); | |
428 | else { | |
429 | char *endptr; | |
430 | int minor = strtol(w + 12, &endptr, 10); | |
431 | ||
432 | if (w[12] == 0 || endptr[0] != 0 || minor < 0) | |
433 | pr_err("invalid super-minor number: %s\n", | |
434 | w); | |
435 | else | |
436 | mis.super_minor = minor; | |
437 | } | |
438 | } else if (strncasecmp(w, "name=", 5) == 0) { | |
439 | if (mis.name[0]) | |
440 | pr_err("only specify name once, %s ignored.\n", | |
441 | w); | |
442 | else if (strlen(w + 5) > 32) | |
443 | pr_err("name too long, ignoring %s\n", w); | |
444 | else | |
445 | strcpy(mis.name, w + 5); | |
446 | ||
447 | } else if (strncasecmp(w, "bitmap=", 7) == 0) { | |
448 | if (mis.bitmap_file) | |
449 | pr_err("only specify bitmap file once. %s ignored\n", | |
450 | w); | |
451 | else | |
452 | mis.bitmap_file = xstrdup(w + 7); | |
453 | ||
454 | } else if (strncasecmp(w, "devices=", 8 ) == 0) { | |
455 | if (mis.devices) | |
456 | pr_err("only specify devices once (use a comma separated list). %s ignored\n", | |
457 | w); | |
458 | else | |
459 | mis.devices = xstrdup(w + 8); | |
460 | } else if (strncasecmp(w, "spare-group=", 12) == 0) { | |
461 | if (mis.spare_group) | |
462 | pr_err("only specify one spare group per array. %s ignored.\n", | |
463 | w); | |
464 | else | |
465 | mis.spare_group = xstrdup(w + 12); | |
466 | } else if (strncasecmp(w, "level=", 6) == 0 ) { | |
467 | /* this is mainly for compatability with --brief output */ | |
468 | mis.level = map_name(pers, w + 6); | |
469 | } else if (strncasecmp(w, "disks=", 6) == 0) { | |
470 | /* again, for compat */ | |
471 | mis.raid_disks = atoi(w + 6); | |
472 | } else if (strncasecmp(w, "num-devices=", 12) == 0) { | |
473 | /* again, for compat */ | |
474 | mis.raid_disks = atoi(w + 12); | |
475 | } else if (strncasecmp(w, "spares=", 7) == 0) { | |
476 | /* for warning if not all spares present */ | |
477 | mis.spare_disks = atoi(w + 7); | |
478 | } else if (strncasecmp(w, "metadata=", 9) == 0) { | |
479 | /* style of metadata on the devices. */ | |
480 | int i; | |
481 | ||
482 | for(i=0; superlist[i] && !mis.st; i++) | |
483 | mis.st = superlist[i]-> | |
484 | match_metadata_desc(w + 9); | |
485 | ||
486 | if (!mis.st) | |
487 | pr_err("metadata format %s unknown, ignored.\n", | |
488 | w + 9); | |
489 | } else if (strncasecmp(w, "auto=", 5) == 0 ) { | |
490 | /* whether to create device special files as needed */ | |
491 | mis.autof = parse_auto(w + 5, "auto type", 0); | |
492 | } else if (strncasecmp(w, "member=", 7) == 0) { | |
493 | /* subarray within a container */ | |
494 | mis.member = xstrdup(w + 7); | |
495 | } else if (strncasecmp(w, "container=", 10) == 0) { | |
496 | /* The container holding this subarray. | |
497 | * Either a device name or a uuid */ | |
498 | mis.container = xstrdup(w + 10); | |
499 | } else { | |
500 | pr_err("unrecognised word on ARRAY line: %s\n", | |
501 | w); | |
502 | } | |
503 | } | |
504 | if (mis.uuid_set == 0 && mis.devices == NULL && | |
505 | mis.super_minor == UnSet && mis.name[0] == 0 && | |
506 | (mis.container == NULL || mis.member == NULL)) | |
507 | pr_err("ARRAY line %s has no identity information.\n", | |
508 | mis.devname); | |
509 | else { | |
510 | mi = xmalloc(sizeof(*mi)); | |
511 | *mi = mis; | |
512 | mi->devname = mis.devname ? xstrdup(mis.devname) : NULL; | |
513 | mi->next = NULL; | |
514 | *mddevlp = mi; | |
515 | mddevlp = &mi->next; | |
516 | } | |
517 | } | |
518 | ||
519 | static char *alert_email = NULL; | |
520 | void mailline(char *line) | |
521 | { | |
522 | char *w; | |
523 | ||
524 | for (w = dl_next(line); w != line; w = dl_next(w)) | |
525 | if (alert_email == NULL) | |
526 | alert_email = xstrdup(w); | |
527 | } | |
528 | ||
529 | static char *alert_mail_from = NULL; | |
530 | void mailfromline(char *line) | |
531 | { | |
532 | char *w; | |
533 | ||
534 | for (w = dl_next(line); w != line; w = dl_next(w)) { | |
535 | if (alert_mail_from == NULL) | |
536 | alert_mail_from = xstrdup(w); | |
537 | else { | |
538 | char *t = NULL; | |
539 | ||
540 | if (xasprintf(&t, "%s %s", alert_mail_from, w) > 0) { | |
541 | free(alert_mail_from); | |
542 | alert_mail_from = t; | |
543 | } | |
544 | } | |
545 | } | |
546 | } | |
547 | ||
548 | static char *alert_program = NULL; | |
549 | void programline(char *line) | |
550 | { | |
551 | char *w; | |
552 | ||
553 | for (w = dl_next(line); w != line; w = dl_next(w)) | |
554 | if (alert_program == NULL) | |
555 | alert_program = xstrdup(w); | |
556 | } | |
557 | ||
558 | static char *home_host = NULL; | |
559 | static int require_homehost = 1; | |
560 | void homehostline(char *line) | |
561 | { | |
562 | char *w; | |
563 | ||
564 | for (w = dl_next(line); w != line; w = dl_next(w)) { | |
565 | if (strcasecmp(w, "<ignore>") == 0) | |
566 | require_homehost = 0; | |
567 | else if (home_host == NULL) { | |
568 | if (strcasecmp(w, "<none>") == 0) | |
569 | home_host = xstrdup(""); | |
570 | else | |
571 | home_host = xstrdup(w); | |
572 | } | |
573 | } | |
574 | } | |
575 | ||
576 | static char *home_cluster = NULL; | |
577 | void homeclusterline(char *line) | |
578 | { | |
579 | char *w; | |
580 | ||
581 | for (w = dl_next(line); w != line; w = dl_next(w)) { | |
582 | if (home_cluster == NULL) { | |
583 | if (strcasecmp(w, "<none>") == 0) | |
584 | home_cluster = xstrdup(""); | |
585 | else | |
586 | home_cluster = xstrdup(w); | |
587 | } | |
588 | } | |
589 | } | |
590 | ||
591 | char auto_yes[] = "yes"; | |
592 | char auto_no[] = "no"; | |
593 | char auto_homehost[] = "homehost"; | |
594 | ||
595 | static int auto_seen = 0; | |
596 | void autoline(char *line) | |
597 | { | |
598 | char *w; | |
599 | char *seen; | |
600 | int super_cnt; | |
601 | char *dflt = auto_yes; | |
602 | int homehost = 0; | |
603 | int i; | |
604 | ||
605 | if (auto_seen) | |
606 | return; | |
607 | auto_seen = 1; | |
608 | ||
609 | /* | |
610 | * Parse the 'auto' line creating policy statements for the 'auto' | |
611 | * policy. | |
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'. | |
633 | * When we see yes, no, +all or -all we stop and any version that hasn't | |
634 | * been seen gets an appropriate auto= entry. | |
635 | */ | |
636 | ||
637 | /* | |
638 | * If environment variable MDADM_CONF_AUTO is defined, then | |
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 | ||
656 | for (super_cnt = 0; superlist[super_cnt]; super_cnt++) | |
657 | ; | |
658 | seen = xcalloc(super_cnt, 1); | |
659 | ||
660 | for (w = dl_next(line); w != line; w = dl_next(w)) { | |
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 | ||
688 | if (strcasecmp(w + 1, "all") == 0) { | |
689 | dflt = val; | |
690 | break; | |
691 | } | |
692 | for (i = 0; superlist[i]; i++) { | |
693 | const char *version = superlist[i]->name; | |
694 | if (strcasecmp(w + 1, version) == 0) | |
695 | break; | |
696 | /* 1 matches 1.x, 0 matches 0.90 */ | |
697 | if (version[1] == '.' && strlen(w + 1) == 1 && | |
698 | w[1] == version[0]) | |
699 | break; | |
700 | /* 1.anything matches 1.x */ | |
701 | if (strcmp(version, "1.x") == 0 && | |
702 | strncmp(w + 1, "1.", 2) == 0) | |
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; | |
711 | policy_add(rule_policy, pol_auto, val, pol_metadata, | |
712 | superlist[i]->name, NULL); | |
713 | seen[i] = 1; | |
714 | } | |
715 | for (i = 0; i < super_cnt; i++) | |
716 | if (!seen[i]) | |
717 | policy_add(rule_policy, pol_auto, dflt, pol_metadata, | |
718 | superlist[i]->name, NULL); | |
719 | ||
720 | free(seen); | |
721 | } | |
722 | ||
723 | int loaded = 0; | |
724 | ||
725 | static char *conffile = NULL; | |
726 | void set_conffile(char *file) | |
727 | { | |
728 | conffile = file; | |
729 | } | |
730 | ||
731 | void conf_file(FILE *f) | |
732 | { | |
733 | char *line; | |
734 | while ((line = conf_line(f))) { | |
735 | switch(match_keyword(line)) { | |
736 | case Devices: | |
737 | devline(line); | |
738 | break; | |
739 | case Array: | |
740 | arrayline(line); | |
741 | break; | |
742 | case Mailaddr: | |
743 | mailline(line); | |
744 | break; | |
745 | case Mailfrom: | |
746 | mailfromline(line); | |
747 | break; | |
748 | case Program: | |
749 | programline(line); | |
750 | break; | |
751 | case CreateDev: | |
752 | createline(line); | |
753 | break; | |
754 | case Homehost: | |
755 | homehostline(line); | |
756 | break; | |
757 | case HomeCluster: | |
758 | homeclusterline(line); | |
759 | break; | |
760 | case AutoMode: | |
761 | autoline(line); | |
762 | break; | |
763 | case Policy: | |
764 | policyline(line, rule_policy); | |
765 | break; | |
766 | case PartPolicy: | |
767 | policyline(line, rule_part); | |
768 | break; | |
769 | case Sysfs: | |
770 | sysfsline(line); | |
771 | break; | |
772 | default: | |
773 | pr_err("Unknown keyword %s\n", line); | |
774 | } | |
775 | free_line(line); | |
776 | } | |
777 | } | |
778 | ||
779 | struct fname { | |
780 | struct fname *next; | |
781 | char name[]; | |
782 | }; | |
783 | ||
784 | void 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); | |
808 | if (l < 6 || strcmp(dp->d_name + l - 5, ".conf") != 0) | |
809 | continue; | |
810 | fn = xmalloc(sizeof(*fn) + l + 1); | |
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 | ||
840 | void load_conffile(void) | |
841 | { | |
842 | FILE *f; | |
843 | char *confdir = NULL; | |
844 | char *head; | |
845 | ||
846 | if (loaded) | |
847 | return; | |
848 | if (conffile == NULL) { | |
849 | conffile = DefaultConfFile; | |
850 | confdir = DefaultConfDir; | |
851 | } | |
852 | ||
853 | if (strcmp(conffile, "partitions") == 0) { | |
854 | char *list = dl_strdup("DEV"); | |
855 | dl_init(list); | |
856 | dl_add(list, dl_strdup("partitions")); | |
857 | devline(list); | |
858 | free_line(list); | |
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 | */ | |
866 | if (f == NULL && conffile == DefaultConfFile) { | |
867 | f = fopen(DefaultAltConfFile, "r"); | |
868 | if (f) { | |
869 | conffile = DefaultAltConfFile; | |
870 | confdir = DefaultAltConfDir; | |
871 | } | |
872 | } | |
873 | if (f) { | |
874 | conf_file_or_dir(f); | |
875 | fclose(f); | |
876 | } | |
877 | if (confdir) { | |
878 | f = fopen(confdir, "r"); | |
879 | if (f) { | |
880 | conf_file_or_dir(f); | |
881 | fclose(f); | |
882 | } | |
883 | } | |
884 | } | |
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 | ||
893 | loaded = 1; | |
894 | } | |
895 | ||
896 | char *conf_get_mailaddr(void) | |
897 | { | |
898 | load_conffile(); | |
899 | return alert_email; | |
900 | } | |
901 | ||
902 | char *conf_get_mailfrom(void) | |
903 | { | |
904 | load_conffile(); | |
905 | return alert_mail_from; | |
906 | } | |
907 | ||
908 | char *conf_get_program(void) | |
909 | { | |
910 | load_conffile(); | |
911 | return alert_program; | |
912 | } | |
913 | ||
914 | char *conf_get_homehost(int *require_homehostp) | |
915 | { | |
916 | load_conffile(); | |
917 | if (require_homehostp) | |
918 | *require_homehostp = require_homehost; | |
919 | return home_host; | |
920 | } | |
921 | ||
922 | char *conf_get_homecluster(void) | |
923 | { | |
924 | load_conffile(); | |
925 | return home_cluster; | |
926 | } | |
927 | ||
928 | struct createinfo *conf_get_create_info(void) | |
929 | { | |
930 | load_conffile(); | |
931 | return &createinfo; | |
932 | } | |
933 | ||
934 | struct mddev_ident *conf_get_ident(char *dev) | |
935 | { | |
936 | struct mddev_ident *rv; | |
937 | load_conffile(); | |
938 | rv = mddevlist; | |
939 | while (dev && rv && (rv->devname == NULL || | |
940 | !devname_matches(dev, rv->devname))) | |
941 | rv = rv->next; | |
942 | return rv; | |
943 | } | |
944 | ||
945 | static void append_dlist(struct mddev_dev **dlp, struct mddev_dev *list) | |
946 | { | |
947 | while (*dlp) | |
948 | dlp = &(*dlp)->next; | |
949 | *dlp = list; | |
950 | } | |
951 | ||
952 | struct mddev_dev *conf_get_devs() | |
953 | { | |
954 | glob_t globbuf; | |
955 | struct conf_dev *cd; | |
956 | int flags = 0; | |
957 | static struct mddev_dev *dlist = NULL; | |
958 | unsigned int i; | |
959 | ||
960 | while (dlist) { | |
961 | struct mddev_dev *t = dlist; | |
962 | dlist = dlist->next; | |
963 | free(t->devname); | |
964 | free(t); | |
965 | } | |
966 | ||
967 | load_conffile(); | |
968 | ||
969 | if (cdevlist == NULL) { | |
970 | /* default to 'partitions' and 'containers' */ | |
971 | dlist = load_partitions(); | |
972 | append_dlist(&dlist, load_containers()); | |
973 | } | |
974 | ||
975 | for (cd = cdevlist; cd; cd = cd->next) { | |
976 | if (strcasecmp(cd->name, "partitions") == 0) | |
977 | append_dlist(&dlist, load_partitions()); | |
978 | else if (strcasecmp(cd->name, "containers") == 0) | |
979 | append_dlist(&dlist, load_containers()); | |
980 | else { | |
981 | glob(cd->name, flags, NULL, &globbuf); | |
982 | flags |= GLOB_APPEND; | |
983 | } | |
984 | } | |
985 | if (flags & GLOB_APPEND) { | |
986 | for (i = 0; i < globbuf.gl_pathc; i++) { | |
987 | struct mddev_dev *t; | |
988 | t = xcalloc(1, sizeof(*t)); | |
989 | t->devname = xstrdup(globbuf.gl_pathv[i]); | |
990 | t->next = dlist; | |
991 | dlist = t; | |
992 | /* printf("one dev is %s\n", t->devname);*/ | |
993 | } | |
994 | globfree(&globbuf); | |
995 | } | |
996 | ||
997 | return dlist; | |
998 | } | |
999 | ||
1000 | int conf_test_dev(char *devname) | |
1001 | { | |
1002 | struct conf_dev *cd; | |
1003 | if (cdevlist == NULL) | |
1004 | /* allow anything by default */ | |
1005 | return 1; | |
1006 | for (cd = cdevlist; cd; cd = cd->next) { | |
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 | ||
1015 | int conf_test_metadata(const char *version, struct dev_policy *pol, int is_homehost) | |
1016 | { | |
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'. | |
1021 | */ | |
1022 | struct dev_policy *p; | |
1023 | int no = 0, found_homehost = 0; | |
1024 | load_conffile(); | |
1025 | ||
1026 | pol = pol_find(pol, pol_auto); | |
1027 | pol_for_each(p, pol, version) { | |
1028 | if (strcmp(p->value, "yes") == 0) | |
1029 | return 1; | |
1030 | if (strcmp(p->value, "homehost") == 0) | |
1031 | found_homehost = 1; | |
1032 | if (strcmp(p->value, "no") == 0) | |
1033 | no = 1; | |
1034 | } | |
1035 | if (is_homehost && found_homehost) | |
1036 | return 1; | |
1037 | if (no) | |
1038 | return 0; | |
1039 | return 1; | |
1040 | } | |
1041 | ||
1042 | int match_oneof(char *devices, char *devname) | |
1043 | { | |
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) { | |
1055 | strncpy(patn, p, devices - p); | |
1056 | patn[devices-p] = 0; | |
1057 | if (fnmatch(patn, devname, FNM_PATHNAME) == 0) | |
1058 | return 1; | |
1059 | } | |
1060 | if (*devices == ',') | |
1061 | devices++; | |
1062 | } | |
1063 | return 0; | |
1064 | } | |
1065 | ||
1066 | int 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 | ||
1086 | if (strncmp(name, "md", 2) == 0 && isdigit(name[2])) | |
1087 | name += 2; | |
1088 | if (strncmp(match, "md", 2) == 0 && isdigit(match[2])) | |
1089 | match += 2; | |
1090 | ||
1091 | return (strcmp(name, match) == 0); | |
1092 | } | |
1093 | ||
1094 | int conf_name_is_free(char *name) | |
1095 | { | |
1096 | /* Check if this name is already taken by an ARRAY entry in | |
1097 | * the config file. | |
1098 | * It can be taken either by a match on devname, name, or | |
1099 | * even super-minor. | |
1100 | */ | |
1101 | struct mddev_ident *dev; | |
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); | |
1111 | if (dev->super_minor != UnSet && devname_matches(name, nbuf)) | |
1112 | return 0; | |
1113 | } | |
1114 | return 1; | |
1115 | } | |
1116 | ||
1117 | struct mddev_ident *conf_match(struct supertype *st, | |
1118 | struct mdinfo *info, | |
1119 | char *devname, | |
1120 | int verbose, int *rvp) | |
1121 | { | |
1122 | struct mddev_ident *array_list, *match; | |
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 && | |
1127 | same_uuid(array_list->uuid, info->uuid, | |
1128 | st->ss->swapuuid) == 0) { | |
1129 | if (verbose >= 2 && array_list->devname) | |
1130 | pr_err("UUID differs from %s.\n", | |
1131 | array_list->devname); | |
1132 | continue; | |
1133 | } | |
1134 | if (array_list->name[0] && | |
1135 | strcasecmp(array_list->name, info->name) != 0) { | |
1136 | if (verbose >= 2 && array_list->devname) | |
1137 | pr_err("Name differs from %s.\n", | |
1138 | array_list->devname); | |
1139 | continue; | |
1140 | } | |
1141 | if (array_list->devices && devname && | |
1142 | !match_oneof(array_list->devices, devname)) { | |
1143 | if (verbose >= 2 && array_list->devname) | |
1144 | pr_err("Not a listed device for %s.\n", | |
1145 | array_list->devname); | |
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) | |
1151 | pr_err("Different super-minor to %s.\n", | |
1152 | array_list->devname); | |
1153 | continue; | |
1154 | } | |
1155 | if (!array_list->uuid_set && !array_list->name[0] && | |
1156 | !array_list->devices && array_list->super_minor == UnSet) { | |
1157 | if (verbose >= 2 && array_list->devname) | |
1158 | pr_err("%s doesn't have any identifying information.\n", | |
1159 | array_list->devname); | |
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) | |
1167 | pr_err("we match both %s and %s - cannot decide which to use.\n", | |
1168 | match->devname, | |
1169 | array_list->devname); | |
1170 | else | |
1171 | pr_err("multiple lines in mdadm.conf match\n"); | |
1172 | } | |
1173 | if (rvp) | |
1174 | *rvp = 2; | |
1175 | match = NULL; | |
1176 | break; | |
1177 | } | |
1178 | match = array_list; | |
1179 | } | |
1180 | return match; | |
1181 | } | |
1182 | ||
1183 | int 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; | |
1190 | if (strcmp(a1->devname, "<ignore>") == 0) | |
1191 | continue; | |
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, ':'); | |
1201 | pr_err("Devices %s and ", | |
1202 | nbuf); | |
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 | |
1208 | pr_err("Device %s given twice in config file\n", a1->devname); | |
1209 | return 1; | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | return 0; | |
1214 | } |