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