]>
Commit | Line | Data |
---|---|---|
5527fc74 N |
1 | /* |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de> | |
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 | |
22 | * Email: <neilb@suse.de> | |
23 | */ | |
24 | ||
25 | #include "mdadm.h" | |
26 | #include <dirent.h> | |
27 | #include <fnmatch.h> | |
28 | #include <ctype.h> | |
29 | #include "dlink.h" | |
30 | /* | |
31 | * Policy module for mdadm. | |
32 | * A policy statement about a device lists a set of values for each | |
33 | * of a set of names. Each value can have a metadata type as context. | |
34 | * | |
35 | * names include: | |
36 | * action - the actions that can be taken on hot-plug | |
37 | * domain - the domain(s) that the device is part of | |
38 | * | |
39 | * Policy information is extracted from various sources, but | |
40 | * particularly from a set of policy rules in mdadm.conf | |
41 | */ | |
42 | ||
2cda7640 ML |
43 | static void pol_new(struct dev_policy **pol, char *name, const char *val, |
44 | const char *metadata) | |
5527fc74 | 45 | { |
503975b9 | 46 | struct dev_policy *n = xmalloc(sizeof(*n)); |
5527fc74 N |
47 | const char *real_metadata = NULL; |
48 | int i; | |
49 | ||
50 | n->name = name; | |
51 | n->value = val; | |
52 | ||
53 | /* We need to normalise the metadata name */ | |
54 | if (metadata) { | |
55 | for (i = 0; superlist[i] ; i++) | |
56 | if (strcmp(metadata, superlist[i]->name) == 0) { | |
57 | real_metadata = superlist[i]->name; | |
58 | break; | |
59 | } | |
60 | if (!real_metadata) { | |
61 | if (strcmp(metadata, "1") == 0 || | |
62 | strcmp(metadata, "1.0") == 0 || | |
63 | strcmp(metadata, "1.1") == 0 || | |
64 | strcmp(metadata, "1.2") == 0) | |
65 | real_metadata = super1.name; | |
66 | } | |
67 | if (!real_metadata) { | |
2cda7640 | 68 | static const char *prev = NULL; |
5527fc74 | 69 | if (prev != metadata) { |
e7b84f9d | 70 | pr_err("metadata=%s unrecognised - ignoring rule\n", |
5527fc74 N |
71 | metadata); |
72 | prev = metadata; | |
73 | } | |
74 | real_metadata = "unknown"; | |
75 | } | |
76 | } | |
77 | ||
78 | n->metadata = real_metadata; | |
79 | n->next = *pol; | |
80 | *pol = n; | |
81 | } | |
82 | ||
83 | static int pol_lesseq(struct dev_policy *a, struct dev_policy *b) | |
84 | { | |
85 | int cmp; | |
86 | ||
87 | if (a->name < b->name) | |
88 | return 1; | |
89 | if (a->name > b->name) | |
90 | return 0; | |
91 | ||
92 | cmp = strcmp(a->value, b->value); | |
93 | if (cmp < 0) | |
94 | return 1; | |
95 | if (cmp > 0) | |
96 | return 0; | |
97 | ||
98 | return (a->metadata <= b->metadata); | |
99 | } | |
100 | ||
101 | static void pol_sort(struct dev_policy **pol) | |
102 | { | |
103 | /* sort policy list in *pol by name/metadata/value | |
104 | * using merge sort | |
105 | */ | |
106 | ||
107 | struct dev_policy *pl[2]; | |
108 | pl[0] = *pol; | |
109 | pl[1] = NULL; | |
110 | ||
111 | do { | |
112 | struct dev_policy **plp[2], *p[2]; | |
113 | int curr = 0; | |
114 | struct dev_policy nul = { NULL, NULL, NULL, NULL }; | |
115 | struct dev_policy *prev = &nul; | |
116 | int next = 0; | |
117 | ||
118 | /* p[] are the two lists that we are merging. | |
119 | * plp[] are the ends of the two lists we create | |
120 | * from the merge. | |
121 | * 'curr' is which of plp[] that we are currently | |
122 | * adding items to. | |
123 | * 'next' is which if p[] we will take the next | |
124 | * item from. | |
125 | * 'prev' is that last value, which was placed in | |
126 | * plp[curr]. | |
127 | */ | |
128 | plp[0] = &pl[0]; | |
129 | plp[1] = &pl[1]; | |
130 | p[0] = pl[0]; | |
131 | p[1] = pl[1]; | |
132 | ||
133 | /* take least of p[0] and p[1] | |
134 | * if it is larger than prev, add to | |
135 | * plp[curr], else swap curr then add | |
136 | */ | |
137 | while (p[0] || p[1]) { | |
138 | if (p[next] == NULL || | |
139 | (p[1-next] != NULL && | |
140 | !(pol_lesseq(prev, p[1-next]) | |
37194990 N |
141 | ^pol_lesseq(prev, p[next]) |
142 | ^pol_lesseq(p[next], p[1-next]))) | |
5527fc74 N |
143 | ) |
144 | next = 1 - next; | |
145 | ||
146 | if (!pol_lesseq(prev, p[next])) | |
147 | curr = 1 - curr; | |
148 | ||
149 | *plp[curr] = prev = p[next]; | |
150 | plp[curr] = &p[next]->next; | |
151 | p[next] = p[next]->next; | |
152 | } | |
153 | *plp[0] = NULL; | |
154 | *plp[1] = NULL; | |
155 | } while (pl[0] && pl[1]); | |
156 | if (pl[0]) | |
157 | *pol = pl[0]; | |
158 | else | |
159 | *pol = pl[1]; | |
160 | } | |
161 | ||
162 | static void pol_dedup(struct dev_policy *pol) | |
163 | { | |
164 | /* This is a sorted list - remove duplicates. */ | |
165 | while (pol && pol->next) { | |
166 | if (pol_lesseq(pol->next, pol)) { | |
167 | struct dev_policy *tmp = pol->next; | |
168 | pol->next = tmp->next; | |
169 | free(tmp); | |
170 | } else | |
171 | pol = pol->next; | |
172 | } | |
173 | } | |
174 | ||
175 | /* | |
176 | * pol_find finds the first entry in the policy | |
177 | * list to match name. | |
178 | * If it returns non-NULL there is at least one | |
179 | * value, but how many can only be found by | |
180 | * iterating through the list. | |
181 | */ | |
182 | struct dev_policy *pol_find(struct dev_policy *pol, char *name) | |
183 | { | |
184 | while (pol && pol->name < name) | |
185 | pol = pol->next; | |
186 | ||
187 | if (!pol || pol->name != name) | |
188 | return NULL; | |
189 | return pol; | |
190 | } | |
191 | ||
192 | static char *disk_path(struct mdinfo *disk) | |
193 | { | |
194 | struct stat stb; | |
195 | int prefix_len; | |
196 | DIR *by_path; | |
197 | char symlink[PATH_MAX] = "/dev/disk/by-path/"; | |
b451aa48 | 198 | char nm[PATH_MAX]; |
5527fc74 | 199 | struct dirent *ent; |
b451aa48 | 200 | int rv; |
5527fc74 N |
201 | |
202 | by_path = opendir(symlink); | |
203 | if (!by_path) | |
204 | return NULL; | |
205 | prefix_len = strlen(symlink); | |
206 | ||
207 | while ((ent = readdir(by_path)) != NULL) { | |
208 | if (ent->d_type != DT_LNK) | |
209 | continue; | |
210 | strncpy(symlink + prefix_len, | |
211 | ent->d_name, | |
212 | sizeof(symlink) - prefix_len); | |
213 | if (stat(symlink, &stb) < 0) | |
214 | continue; | |
215 | if ((stb.st_mode & S_IFMT) != S_IFBLK) | |
216 | continue; | |
217 | if (stb.st_rdev != makedev(disk->disk.major, disk->disk.minor)) | |
218 | continue; | |
219 | closedir(by_path); | |
503975b9 | 220 | return xstrdup(ent->d_name); |
5527fc74 N |
221 | } |
222 | closedir(by_path); | |
b451aa48 N |
223 | /* A NULL path isn't really acceptable - use the devname.. */ |
224 | sprintf(symlink, "/sys/dev/block/%d:%d", disk->disk.major, disk->disk.minor); | |
9cf014ec | 225 | rv = readlink(symlink, nm, sizeof(nm)-1); |
b451aa48 N |
226 | if (rv > 0) { |
227 | char *dname; | |
228 | nm[rv] = 0; | |
229 | dname = strrchr(nm, '/'); | |
230 | if (dname) | |
503975b9 | 231 | return xstrdup(dname + 1); |
b451aa48 | 232 | } |
503975b9 | 233 | return xstrdup("unknown"); |
5527fc74 N |
234 | } |
235 | ||
236 | char type_part[] = "part"; | |
237 | char type_disk[] = "disk"; | |
238 | static char *disk_type(struct mdinfo *disk) | |
239 | { | |
240 | char buf[30+20+20]; | |
241 | struct stat stb; | |
242 | sprintf(buf, "/sys/dev/block/%d:%d/partition", | |
243 | disk->disk.major, disk->disk.minor); | |
244 | if (stat(buf, &stb) == 0) | |
245 | return type_part; | |
246 | else | |
247 | return type_disk; | |
248 | } | |
249 | ||
250 | static int pol_match(struct rule *rule, char *path, char *type) | |
251 | { | |
252 | /* check if this rule matches on path and type */ | |
253 | int pathok = 0; /* 0 == no path, 1 == match, -1 == no match yet */ | |
254 | int typeok = 0; | |
255 | ||
256 | while (rule) { | |
257 | if (rule->name == rule_path) { | |
258 | if (pathok == 0) | |
259 | pathok = -1; | |
b451aa48 | 260 | if (path && fnmatch(rule->value, path, 0) == 0) |
5527fc74 N |
261 | pathok = 1; |
262 | } | |
263 | if (rule->name == rule_type) { | |
264 | if (typeok == 0) | |
265 | typeok = -1; | |
b451aa48 | 266 | if (type && strcmp(rule->value, type) == 0) |
5527fc74 N |
267 | typeok = 1; |
268 | } | |
269 | rule = rule->next; | |
270 | } | |
271 | return pathok >= 0 && typeok >= 0; | |
272 | } | |
273 | ||
274 | static void pol_merge(struct dev_policy **pol, struct rule *rule) | |
275 | { | |
276 | /* copy any name assignments from rule into pol */ | |
277 | struct rule *r; | |
278 | char *metadata = NULL; | |
279 | for (r = rule; r ; r = r->next) | |
280 | if (r->name == pol_metadata) | |
281 | metadata = r->value; | |
282 | ||
283 | for (r = rule; r ; r = r->next) | |
284 | if (r->name == pol_act || | |
b451aa48 N |
285 | r->name == pol_domain || |
286 | r->name == pol_auto) | |
5527fc74 N |
287 | pol_new(pol, r->name, r->value, metadata); |
288 | } | |
289 | ||
290 | static int path_has_part(char *path, char **part) | |
291 | { | |
292 | /* check if path ends with "-partNN" and | |
293 | * if it does, place a pointer to "-pathNN" | |
294 | * in 'part'. | |
295 | */ | |
b451aa48 N |
296 | int l; |
297 | if (!path) | |
298 | return 0; | |
299 | l = strlen(path); | |
5527fc74 N |
300 | while (l > 1 && isdigit(path[l-1])) |
301 | l--; | |
302 | if (l < 5 || strncmp(path+l-5, "-part", 5) != 0) | |
303 | return 0; | |
304 | *part = path+l-4; | |
305 | return 1; | |
306 | } | |
307 | ||
308 | static void pol_merge_part(struct dev_policy **pol, struct rule *rule, char *part) | |
309 | { | |
310 | /* copy any name assignments from rule into pol, appending | |
311 | * -part to any domain. The string with -part appended is | |
312 | * stored with the rule so it has a lifetime to match | |
313 | * the rule. | |
314 | */ | |
315 | struct rule *r; | |
316 | char *metadata = NULL; | |
317 | for (r = rule; r ; r = r->next) | |
318 | if (r->name == pol_metadata) | |
319 | metadata = r->value; | |
320 | ||
321 | for (r = rule; r ; r = r->next) { | |
322 | if (r->name == pol_act) | |
323 | pol_new(pol, r->name, r->value, metadata); | |
324 | else if (r->name == pol_domain) { | |
325 | char *dom; | |
326 | int len; | |
327 | if (r->dups == NULL) | |
328 | r->dups = dl_head(); | |
329 | len = strlen(r->value); | |
330 | for (dom = dl_next(r->dups); dom != r->dups; | |
331 | dom = dl_next(dom)) | |
332 | if (strcmp(dom+len+1, part)== 0) | |
333 | break; | |
334 | if (dom == r->dups) { | |
335 | char *newdom = dl_strndup( | |
336 | r->value, len + 1 + strlen(part)); | |
337 | strcat(strcat(newdom, "-"), part); | |
338 | dl_add(r->dups, newdom); | |
339 | dom = newdom; | |
340 | } | |
341 | pol_new(pol, r->name, dom, metadata); | |
342 | } | |
343 | } | |
344 | } | |
345 | ||
346 | static struct pol_rule *config_rules = NULL; | |
347 | static struct pol_rule **config_rules_end = NULL; | |
348 | static int config_rules_has_path = 0; | |
349 | ||
350 | /* | |
351 | * most policy comes from a set policy rules that are | |
352 | * read from the config file. | |
73c9c47c N |
353 | * path_policy() gathers policy information for the |
354 | * disk described in the given a 'path' and a 'type'. | |
5527fc74 | 355 | */ |
73c9c47c | 356 | struct dev_policy *path_policy(char *path, char *type) |
5527fc74 | 357 | { |
5527fc74 N |
358 | struct pol_rule *rules; |
359 | struct dev_policy *pol = NULL; | |
2cda7640 | 360 | int i; |
5527fc74 | 361 | |
5527fc74 N |
362 | rules = config_rules; |
363 | ||
364 | while (rules) { | |
365 | char *part; | |
366 | if (rules->type == rule_policy) | |
367 | if (pol_match(rules->rule, path, type)) | |
368 | pol_merge(&pol, rules->rule); | |
369 | if (rules->type == rule_part && strcmp(type, type_part) == 0) | |
370 | if (path_has_part(path, &part)) { | |
371 | *part = 0; | |
372 | if (pol_match(rules->rule, path, type_disk)) | |
373 | pol_merge_part(&pol, rules->rule, part+1); | |
374 | *part = '-'; | |
375 | } | |
376 | rules = rules->next; | |
377 | } | |
2cda7640 ML |
378 | |
379 | /* Now add any metadata-specific internal knowledge | |
380 | * about this path | |
381 | */ | |
b451aa48 | 382 | for (i=0; path && superlist[i]; i++) |
2cda7640 ML |
383 | if (superlist[i]->get_disk_controller_domain) { |
384 | const char *d = | |
385 | superlist[i]->get_disk_controller_domain(path); | |
386 | if (d) | |
387 | pol_new(&pol, pol_domain, d, superlist[i]->name); | |
388 | } | |
389 | ||
5527fc74 N |
390 | pol_sort(&pol); |
391 | pol_dedup(pol); | |
73c9c47c N |
392 | return pol; |
393 | } | |
394 | ||
e78dda3b N |
395 | void pol_add(struct dev_policy **pol, |
396 | char *name, char *val, | |
397 | char *metadata) | |
398 | { | |
399 | pol_new(pol, name, val, metadata); | |
400 | pol_sort(pol); | |
401 | pol_dedup(*pol); | |
402 | } | |
403 | ||
73c9c47c N |
404 | /* |
405 | * disk_policy() gathers policy information for the | |
406 | * disk described in the given mdinfo (disk.{major,minor}). | |
407 | */ | |
408 | struct dev_policy *disk_policy(struct mdinfo *disk) | |
409 | { | |
410 | char *path = NULL; | |
411 | char *type = disk_type(disk); | |
412 | struct dev_policy *pol = NULL; | |
413 | ||
b9b004eb | 414 | if (config_rules_has_path) |
73c9c47c | 415 | path = disk_path(disk); |
73c9c47c N |
416 | |
417 | pol = path_policy(path, type); | |
418 | ||
5527fc74 N |
419 | free(path); |
420 | return pol; | |
421 | } | |
422 | ||
4dd2df09 | 423 | struct dev_policy *devid_policy(int dev) |
4e8d9f0a N |
424 | { |
425 | struct mdinfo disk; | |
426 | disk.disk.major = major(dev); | |
427 | disk.disk.minor = minor(dev); | |
428 | return disk_policy(&disk); | |
429 | } | |
430 | ||
5527fc74 N |
431 | /* |
432 | * process policy rules read from config file. | |
433 | */ | |
434 | ||
435 | char rule_path[] = "path"; | |
436 | char rule_type[] = "type"; | |
437 | ||
438 | char rule_policy[] = "policy"; | |
439 | char rule_part[] = "part-policy"; | |
440 | ||
441 | char pol_metadata[] = "metadata"; | |
442 | char pol_act[] = "action"; | |
443 | char pol_domain[] = "domain"; | |
4e8d9f0a | 444 | char pol_auto[] = "auto"; |
5527fc74 N |
445 | |
446 | static int try_rule(char *w, char *name, struct rule **rp) | |
447 | { | |
448 | struct rule *r; | |
449 | int len = strlen(name); | |
450 | if (strncmp(w, name, len) != 0 || | |
451 | w[len] != '=') | |
452 | return 0; | |
503975b9 | 453 | r = xmalloc(sizeof(*r)); |
5527fc74 N |
454 | r->next = *rp; |
455 | r->name = name; | |
503975b9 | 456 | r->value = xstrdup(w+len+1); |
5527fc74 N |
457 | r->dups = NULL; |
458 | *rp = r; | |
459 | return 1; | |
460 | } | |
461 | ||
462 | void policyline(char *line, char *type) | |
463 | { | |
464 | struct pol_rule *pr; | |
465 | char *w; | |
466 | ||
467 | if (config_rules_end == NULL) | |
468 | config_rules_end = &config_rules; | |
469 | ||
503975b9 | 470 | pr = xmalloc(sizeof(*pr)); |
5527fc74 N |
471 | pr->type = type; |
472 | pr->rule = NULL; | |
473 | for (w = dl_next(line); w != line ; w = dl_next(w)) { | |
474 | if (try_rule(w, rule_path, &pr->rule)) | |
475 | config_rules_has_path = 1; | |
476 | else if (! try_rule(w, rule_type, &pr->rule) && | |
477 | ! try_rule(w, pol_metadata, &pr->rule) && | |
478 | ! try_rule(w, pol_act, &pr->rule) && | |
4e8d9f0a N |
479 | ! try_rule(w, pol_domain, &pr->rule) && |
480 | ! try_rule(w, pol_auto, &pr->rule)) | |
e7b84f9d | 481 | pr_err("policy rule %s unrecognised and ignored\n", |
5527fc74 N |
482 | w); |
483 | } | |
484 | pr->next = config_rules; | |
485 | config_rules = pr; | |
486 | } | |
487 | ||
4e8d9f0a N |
488 | void policy_add(char *type, ...) |
489 | { | |
490 | va_list ap; | |
491 | struct pol_rule *pr; | |
492 | char *name, *val; | |
493 | ||
503975b9 | 494 | pr = xmalloc(sizeof(*pr)); |
4e8d9f0a N |
495 | pr->type = type; |
496 | pr->rule = NULL; | |
497 | ||
498 | va_start(ap, type); | |
499 | while ((name = va_arg(ap, char*)) != NULL) { | |
500 | struct rule *r; | |
501 | ||
502 | val = va_arg(ap, char*); | |
503975b9 | 503 | r = xmalloc(sizeof(*r)); |
4e8d9f0a N |
504 | r->next = pr->rule; |
505 | r->name = name; | |
503975b9 | 506 | r->value = xstrdup(val); |
4e8d9f0a N |
507 | r->dups = NULL; |
508 | pr->rule = r; | |
509 | } | |
510 | pr->next = config_rules; | |
511 | config_rules = pr; | |
a5cd79fe | 512 | va_end(ap); |
4e8d9f0a N |
513 | } |
514 | ||
5527fc74 N |
515 | void policy_free(void) |
516 | { | |
517 | while (config_rules) { | |
518 | struct pol_rule *pr = config_rules; | |
519 | struct rule *r; | |
520 | ||
521 | config_rules = config_rules->next; | |
522 | ||
523 | for (r = pr->rule; r; ) { | |
524 | struct rule *next = r->next; | |
525 | free(r->value); | |
526 | if (r->dups) | |
527 | free_line(r->dups); | |
528 | free(r); | |
529 | r = next; | |
530 | } | |
531 | free(pr); | |
532 | } | |
533 | config_rules_end = NULL; | |
534 | config_rules_has_path = 0; | |
535 | } | |
536 | ||
537 | void dev_policy_free(struct dev_policy *p) | |
538 | { | |
539 | struct dev_policy *t; | |
540 | while (p) { | |
541 | t = p; | |
542 | p = p->next; | |
543 | free(t); | |
544 | } | |
545 | } | |
e3bb5f14 | 546 | |
2cda7640 | 547 | static enum policy_action map_act(const char *act) |
e3bb5f14 N |
548 | { |
549 | if (strcmp(act, "include") == 0) | |
550 | return act_include; | |
551 | if (strcmp(act, "re-add") == 0) | |
552 | return act_re_add; | |
553 | if (strcmp(act, "spare") == 0) | |
554 | return act_spare; | |
d2db3045 N |
555 | if (strcmp(act, "spare-same-slot") == 0) |
556 | return act_spare_same_slot; | |
e3bb5f14 N |
557 | if (strcmp(act, "force-spare") == 0) |
558 | return act_force_spare; | |
559 | return act_err; | |
560 | } | |
561 | ||
562 | static enum policy_action policy_action(struct dev_policy *plist, const char *metadata) | |
563 | { | |
564 | enum policy_action rv = act_default; | |
565 | struct dev_policy *p; | |
566 | ||
567 | plist = pol_find(plist, pol_act); | |
568 | pol_for_each(p, plist, metadata) { | |
569 | enum policy_action a = map_act(p->value); | |
570 | if (a > rv) | |
571 | rv = a; | |
572 | } | |
573 | return rv; | |
574 | } | |
575 | ||
576 | int policy_action_allows(struct dev_policy *plist, const char *metadata, enum policy_action want) | |
577 | { | |
578 | enum policy_action act = policy_action(plist, metadata); | |
579 | ||
580 | if (act == act_err) | |
581 | return 0; | |
582 | return (act >= want); | |
583 | } | |
584 | ||
585 | int disk_action_allows(struct mdinfo *disk, const char *metadata, enum policy_action want) | |
586 | { | |
587 | struct dev_policy *pol = disk_policy(disk); | |
588 | int rv = policy_action_allows(pol, metadata, want); | |
589 | ||
590 | dev_policy_free(pol); | |
591 | return rv; | |
592 | } | |
f5f12c84 | 593 | |
f5f12c84 N |
594 | /* Domain policy: |
595 | * Any device can have a list of domains asserted by different policy | |
596 | * statements. | |
597 | * An array also has a list of domains comprising all the domains of | |
598 | * all the devices in an array. | |
599 | * Where an array has a spare-group, that becomes an addition domain for | |
600 | * every device in the array and thus for the array. | |
601 | * | |
602 | * We keep the list of domains in a sorted linked list | |
603 | * As dev policies are already sorted, this is fairly easy to manage. | |
604 | */ | |
605 | ||
2cda7640 ML |
606 | static struct domainlist **domain_merge_one(struct domainlist **domp, |
607 | const char *domain) | |
f5f12c84 N |
608 | { |
609 | /* merge a domain name into a sorted list and return the | |
610 | * location of the insertion or match | |
611 | */ | |
612 | struct domainlist *dom = *domp; | |
613 | ||
614 | while (dom && strcmp(dom->dom, domain) < 0) { | |
615 | domp = &dom->next; | |
616 | dom = *domp; | |
617 | } | |
618 | if (dom == NULL || strcmp(dom->dom, domain) != 0) { | |
503975b9 | 619 | dom = xmalloc(sizeof(*dom)); |
f5f12c84 N |
620 | dom->next = *domp; |
621 | dom->dom = domain; | |
622 | *domp = dom; | |
623 | } | |
624 | return domp; | |
625 | } | |
626 | ||
2cda7640 ML |
627 | #if (DEBUG) |
628 | void dump_policy(struct dev_policy *policy) | |
629 | { | |
630 | while (policy) { | |
631 | dprintf("policy: %p name: %s value: %s metadata: %s\n", | |
632 | policy, | |
633 | policy->name, | |
634 | policy->value, | |
635 | policy->metadata); | |
636 | policy = policy->next; | |
637 | } | |
638 | } | |
639 | #endif | |
640 | ||
f5f12c84 N |
641 | void domain_merge(struct domainlist **domp, struct dev_policy *pollist, |
642 | const char *metadata) | |
643 | { | |
644 | /* Add to 'domp' all the domains in pol that apply to 'metadata' | |
645 | * which are not already in domp | |
646 | */ | |
647 | struct dev_policy *pol; | |
648 | pollist = pol_find(pollist, pol_domain); | |
649 | pol_for_each(pol, pollist, metadata) | |
e78dda3b | 650 | domain_merge_one(domp, pol->value); |
f5f12c84 N |
651 | } |
652 | ||
653 | int domain_test(struct domainlist *dom, struct dev_policy *pol, | |
654 | const char *metadata) | |
655 | { | |
656 | /* Check that all domains in pol (for metadata) are also in | |
657 | * dom. Both lists are sorted. | |
658 | * If pol has no domains, we don't really know about this device | |
e5508b36 N |
659 | * so we allow caller to choose: |
660 | * -1: has no domains | |
661 | * 0: has domains, not all match | |
662 | * 1: has domains, all match | |
f5f12c84 | 663 | */ |
e5508b36 | 664 | int found_any = -1; |
f5f12c84 N |
665 | struct dev_policy *p; |
666 | ||
667 | pol = pol_find(pol, pol_domain); | |
668 | pol_for_each(p, pol, metadata) { | |
669 | found_any = 1; | |
4e8d9f0a | 670 | while (dom && strcmp(dom->dom, p->value) < 0) |
f5f12c84 | 671 | dom = dom->next; |
4e8d9f0a | 672 | if (!dom || strcmp(dom->dom, p->value) != 0) |
f5f12c84 N |
673 | return 0; |
674 | } | |
675 | return found_any; | |
676 | } | |
677 | ||
4dd2df09 | 678 | void domainlist_add_dev(struct domainlist **dom, int devid, const char *metadata) |
e78dda3b | 679 | { |
4dd2df09 | 680 | struct dev_policy *pol = devid_policy(devid); |
e78dda3b N |
681 | domain_merge(dom, pol, metadata); |
682 | dev_policy_free(pol); | |
683 | } | |
684 | ||
f5f12c84 N |
685 | struct domainlist *domain_from_array(struct mdinfo *mdi, const char *metadata) |
686 | { | |
687 | struct domainlist *domlist = NULL; | |
688 | ||
75c2df65 N |
689 | if (!mdi) |
690 | return NULL; | |
e78dda3b N |
691 | for (mdi = mdi->devs ; mdi ; mdi = mdi->next) |
692 | domainlist_add_dev(&domlist, makedev(mdi->disk.major, | |
693 | mdi->disk.minor), | |
694 | metadata); | |
f5f12c84 | 695 | |
f5f12c84 N |
696 | return domlist; |
697 | } | |
698 | ||
e78dda3b N |
699 | void domain_add(struct domainlist **domp, char *domain) |
700 | { | |
701 | domain_merge_one(domp, domain); | |
702 | } | |
703 | ||
f5f12c84 N |
704 | void domain_free(struct domainlist *dl) |
705 | { | |
706 | while (dl) { | |
707 | struct domainlist *head = dl; | |
708 | dl = dl->next; | |
709 | free(head); | |
710 | } | |
711 | } | |
403410eb PC |
712 | |
713 | /* | |
714 | * same-path policy. | |
715 | * Some policy decisions are guided by knowledge of which | |
716 | * array previously owned the device at a given physical location (path). | |
717 | * When removing a device from an array we might record the array against | |
718 | * the path, and when finding a new device, we might look for which | |
719 | * array previously used that path. | |
720 | * | |
721 | * The 'array' is described by a map_ent, and the path by a the disk in an | |
722 | * mdinfo, or a string. | |
723 | */ | |
724 | ||
725 | void policy_save_path(char *id_path, struct map_ent *array) | |
726 | { | |
727 | char path[PATH_MAX]; | |
728 | FILE *f = NULL; | |
729 | ||
730 | if (mkdir(FAILED_SLOTS_DIR, S_IRWXU) < 0 && errno != EEXIST) { | |
e7b84f9d | 731 | pr_err("can't create file to save path " |
403410eb PC |
732 | "to old disk: %s\n", strerror(errno)); |
733 | return; | |
734 | } | |
735 | ||
736 | snprintf(path, PATH_MAX, FAILED_SLOTS_DIR "/%s", id_path); | |
737 | f = fopen(path, "w"); | |
738 | if (!f) { | |
e7b84f9d | 739 | pr_err("can't create file to" |
403410eb PC |
740 | " save path to old disk: %s\n", |
741 | strerror(errno)); | |
742 | return; | |
743 | } | |
744 | ||
745 | if (fprintf(f, "%s %08x:%08x:%08x:%08x\n", | |
746 | array->metadata, | |
747 | array->uuid[0], array->uuid[1], | |
748 | array->uuid[2], array->uuid[3]) <= 0) | |
e7b84f9d N |
749 | pr_err("Failed to write to " |
750 | "<id_path> cookie\n"); | |
403410eb PC |
751 | |
752 | fclose(f); | |
753 | } | |
754 | ||
755 | int policy_check_path(struct mdinfo *disk, struct map_ent *array) | |
756 | { | |
757 | char path[PATH_MAX]; | |
758 | FILE *f = NULL; | |
759 | char *id_path = disk_path(disk); | |
760 | int rv; | |
761 | ||
762 | if (!id_path) | |
763 | return 0; | |
764 | ||
765 | snprintf(path, PATH_MAX, FAILED_SLOTS_DIR "/%s", id_path); | |
766 | f = fopen(path, "r"); | |
e3da7890 TJ |
767 | if (!f) { |
768 | free(id_path); | |
403410eb | 769 | return 0; |
e3da7890 | 770 | } |
403410eb PC |
771 | |
772 | rv = fscanf(f, " %s %x:%x:%x:%x\n", | |
773 | array->metadata, | |
774 | array->uuid, | |
775 | array->uuid+1, | |
776 | array->uuid+2, | |
777 | array->uuid+3); | |
778 | fclose(f); | |
e3da7890 | 779 | free(id_path); |
403410eb PC |
780 | return rv == 5; |
781 | } | |
20b60dcd LM |
782 | |
783 | /* invocation of udev rule file */ | |
784 | char udev_template_start[] = | |
785 | "# do not edit this file, it is automatically generated by mdadm\n" | |
786 | "\n"; | |
787 | ||
788 | /* find rule named rule_type and return its value */ | |
789 | char *find_rule(struct rule *rule, char *rule_type) | |
790 | { | |
5d500228 N |
791 | while (rule) { |
792 | if (rule->name == rule_type) | |
793 | return rule->value; | |
20b60dcd | 794 | |
5d500228 N |
795 | rule = rule->next; |
796 | } | |
797 | return NULL; | |
20b60dcd LM |
798 | } |
799 | ||
800 | #define UDEV_RULE_FORMAT \ | |
c8826c3e | 801 | "ACTION==\"add\", SUBSYSTEM==\"block\", " \ |
20b60dcd | 802 | "ENV{DEVTYPE}==\"%s\", ENV{ID_PATH}==\"%s\", " \ |
c8826c3e | 803 | "RUN+=\"/sbin/mdadm --incremental $env{DEVNAME}\"\n" |
20b60dcd LM |
804 | |
805 | #define UDEV_RULE_FORMAT_NOTYPE \ | |
c8826c3e | 806 | "ACTION==\"add\", SUBSYSTEM==\"block\", " \ |
20b60dcd | 807 | "ENV{ID_PATH}==\"%s\", " \ |
c8826c3e | 808 | "RUN+=\"/sbin/mdadm --incremental $env{DEVNAME}\"\n" |
20b60dcd LM |
809 | |
810 | /* Write rule in the rule file. Use format from UDEV_RULE_FORMAT */ | |
811 | int write_rule(struct rule *rule, int fd, int force_part) | |
812 | { | |
813 | char line[1024]; | |
814 | char *pth = find_rule(rule, rule_path); | |
815 | char *typ = find_rule(rule, rule_type); | |
816 | if (!pth) | |
817 | return -1; | |
818 | ||
819 | if (force_part) | |
820 | typ = type_part; | |
821 | if (typ) | |
822 | snprintf(line, sizeof(line) - 1, UDEV_RULE_FORMAT, typ, pth); | |
823 | else | |
824 | snprintf(line, sizeof(line) - 1, UDEV_RULE_FORMAT_NOTYPE, pth); | |
825 | return write(fd, line, strlen(line)) == (int)strlen(line); | |
826 | } | |
827 | ||
828 | /* Generate single entry in udev rule basing on POLICY line found in config | |
829 | * file. Take only those with paths, only first occurrence if paths are equal | |
830 | * and if actions supports handling of spares (>=act_spare_same_slot) | |
831 | */ | |
832 | int generate_entries(int fd) | |
833 | { | |
5d500228 N |
834 | struct pol_rule *loop, *dup; |
835 | char *loop_value, *dup_value; | |
836 | int duplicate; | |
837 | ||
838 | for (loop = config_rules; loop; loop = loop->next) { | |
839 | if (loop->type != rule_policy && loop->type != rule_part) | |
840 | continue; | |
841 | duplicate = 0; | |
842 | ||
843 | /* only policies with paths and with actions supporting | |
844 | * bare disks are considered */ | |
845 | loop_value = find_rule(loop->rule, pol_act); | |
846 | if (!loop_value || map_act(loop_value) < act_spare_same_slot) | |
847 | continue; | |
848 | loop_value = find_rule(loop->rule, rule_path); | |
849 | if (!loop_value) | |
850 | continue; | |
851 | for (dup = config_rules; dup != loop; dup = dup->next) { | |
852 | if (dup->type != rule_policy && loop->type != rule_part) | |
853 | continue; | |
854 | dup_value = find_rule(dup->rule, pol_act); | |
855 | if (!dup_value || map_act(dup_value) < act_spare_same_slot) | |
856 | continue; | |
857 | dup_value = find_rule(dup->rule, rule_path); | |
858 | if (!dup_value) | |
859 | continue; | |
860 | if (strcmp(loop_value, dup_value) == 0) { | |
861 | duplicate = 1; | |
862 | break; | |
863 | } | |
864 | } | |
865 | ||
866 | /* not a dup or first occurrence */ | |
867 | if (!duplicate) | |
868 | if (!write_rule(loop->rule, fd, loop->type == rule_part) ) | |
869 | return 0; | |
870 | } | |
871 | return 1; | |
20b60dcd LM |
872 | } |
873 | ||
874 | /* Write_rules routine creates dynamic udev rules used to handle | |
875 | * hot-plug events for bare devices (and making them spares) | |
876 | */ | |
877 | int Write_rules(char *rule_name) | |
878 | { | |
5d500228 N |
879 | int fd; |
880 | char udev_rule_file[PATH_MAX]; | |
881 | ||
882 | if (rule_name) { | |
883 | strncpy(udev_rule_file, rule_name, sizeof(udev_rule_file) - 6); | |
884 | udev_rule_file[sizeof(udev_rule_file) - 6] = '\0'; | |
885 | strcat(udev_rule_file, ".temp"); | |
886 | fd = creat(udev_rule_file, | |
887 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | |
888 | if (fd == -1) | |
889 | return 1; | |
890 | } else | |
891 | fd = 1; | |
892 | ||
893 | /* write static invocation */ | |
894 | if (write(fd, udev_template_start, | |
895 | sizeof(udev_template_start) - 1) | |
896 | != (int)sizeof(udev_template_start)-1) | |
897 | goto abort; | |
898 | ||
899 | /* iterate, if none created or error occurred, remove file */ | |
900 | if (generate_entries(fd) < 0) | |
901 | goto abort; | |
902 | ||
903 | fsync(fd); | |
904 | if (rule_name) { | |
905 | close(fd); | |
906 | rename(udev_rule_file, rule_name); | |
907 | } | |
908 | return 0; | |
20b60dcd | 909 | abort: |
5d500228 N |
910 | if (rule_name) { |
911 | close(fd); | |
912 | unlink(udev_rule_file); | |
913 | } | |
914 | return 1; | |
20b60dcd | 915 | } |