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