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