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