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