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