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