]>
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 | ||
43 | void pol_new(struct dev_policy **pol, char *name, char *val, char *metadata) | |
44 | { | |
45 | struct dev_policy *n = malloc(sizeof(*n)); | |
46 | const char *real_metadata = NULL; | |
47 | int i; | |
48 | ||
49 | n->name = name; | |
50 | n->value = val; | |
51 | ||
52 | /* We need to normalise the metadata name */ | |
53 | if (metadata) { | |
54 | for (i = 0; superlist[i] ; i++) | |
55 | if (strcmp(metadata, superlist[i]->name) == 0) { | |
56 | real_metadata = superlist[i]->name; | |
57 | break; | |
58 | } | |
59 | if (!real_metadata) { | |
60 | if (strcmp(metadata, "1") == 0 || | |
61 | strcmp(metadata, "1.0") == 0 || | |
62 | strcmp(metadata, "1.1") == 0 || | |
63 | strcmp(metadata, "1.2") == 0) | |
64 | real_metadata = super1.name; | |
65 | } | |
66 | if (!real_metadata) { | |
67 | static char *prev = NULL; | |
68 | if (prev != metadata) { | |
69 | fprintf(stderr, Name ": metadata=%s unrecognised - ignoring rule\n", | |
70 | metadata); | |
71 | prev = metadata; | |
72 | } | |
73 | real_metadata = "unknown"; | |
74 | } | |
75 | } | |
76 | ||
77 | n->metadata = real_metadata; | |
78 | n->next = *pol; | |
79 | *pol = n; | |
80 | } | |
81 | ||
82 | static int pol_lesseq(struct dev_policy *a, struct dev_policy *b) | |
83 | { | |
84 | int cmp; | |
85 | ||
86 | if (a->name < b->name) | |
87 | return 1; | |
88 | if (a->name > b->name) | |
89 | return 0; | |
90 | ||
91 | cmp = strcmp(a->value, b->value); | |
92 | if (cmp < 0) | |
93 | return 1; | |
94 | if (cmp > 0) | |
95 | return 0; | |
96 | ||
97 | return (a->metadata <= b->metadata); | |
98 | } | |
99 | ||
100 | static void pol_sort(struct dev_policy **pol) | |
101 | { | |
102 | /* sort policy list in *pol by name/metadata/value | |
103 | * using merge sort | |
104 | */ | |
105 | ||
106 | struct dev_policy *pl[2]; | |
107 | pl[0] = *pol; | |
108 | pl[1] = NULL; | |
109 | ||
110 | do { | |
111 | struct dev_policy **plp[2], *p[2]; | |
112 | int curr = 0; | |
113 | struct dev_policy nul = { NULL, NULL, NULL, NULL }; | |
114 | struct dev_policy *prev = &nul; | |
115 | int next = 0; | |
116 | ||
117 | /* p[] are the two lists that we are merging. | |
118 | * plp[] are the ends of the two lists we create | |
119 | * from the merge. | |
120 | * 'curr' is which of plp[] that we are currently | |
121 | * adding items to. | |
122 | * 'next' is which if p[] we will take the next | |
123 | * item from. | |
124 | * 'prev' is that last value, which was placed in | |
125 | * plp[curr]. | |
126 | */ | |
127 | plp[0] = &pl[0]; | |
128 | plp[1] = &pl[1]; | |
129 | p[0] = pl[0]; | |
130 | p[1] = pl[1]; | |
131 | ||
132 | /* take least of p[0] and p[1] | |
133 | * if it is larger than prev, add to | |
134 | * plp[curr], else swap curr then add | |
135 | */ | |
136 | while (p[0] || p[1]) { | |
137 | if (p[next] == NULL || | |
138 | (p[1-next] != NULL && | |
139 | !(pol_lesseq(prev, p[1-next]) | |
37194990 N |
140 | ^pol_lesseq(prev, p[next]) |
141 | ^pol_lesseq(p[next], p[1-next]))) | |
5527fc74 N |
142 | ) |
143 | next = 1 - next; | |
144 | ||
145 | if (!pol_lesseq(prev, p[next])) | |
146 | curr = 1 - curr; | |
147 | ||
148 | *plp[curr] = prev = p[next]; | |
149 | plp[curr] = &p[next]->next; | |
150 | p[next] = p[next]->next; | |
151 | } | |
152 | *plp[0] = NULL; | |
153 | *plp[1] = NULL; | |
154 | } while (pl[0] && pl[1]); | |
155 | if (pl[0]) | |
156 | *pol = pl[0]; | |
157 | else | |
158 | *pol = pl[1]; | |
159 | } | |
160 | ||
161 | static void pol_dedup(struct dev_policy *pol) | |
162 | { | |
163 | /* This is a sorted list - remove duplicates. */ | |
164 | while (pol && pol->next) { | |
165 | if (pol_lesseq(pol->next, pol)) { | |
166 | struct dev_policy *tmp = pol->next; | |
167 | pol->next = tmp->next; | |
168 | free(tmp); | |
169 | } else | |
170 | pol = pol->next; | |
171 | } | |
172 | } | |
173 | ||
174 | /* | |
175 | * pol_find finds the first entry in the policy | |
176 | * list to match name. | |
177 | * If it returns non-NULL there is at least one | |
178 | * value, but how many can only be found by | |
179 | * iterating through the list. | |
180 | */ | |
181 | struct dev_policy *pol_find(struct dev_policy *pol, char *name) | |
182 | { | |
183 | while (pol && pol->name < name) | |
184 | pol = pol->next; | |
185 | ||
186 | if (!pol || pol->name != name) | |
187 | return NULL; | |
188 | return pol; | |
189 | } | |
190 | ||
191 | static char *disk_path(struct mdinfo *disk) | |
192 | { | |
193 | struct stat stb; | |
194 | int prefix_len; | |
195 | DIR *by_path; | |
196 | char symlink[PATH_MAX] = "/dev/disk/by-path/"; | |
197 | struct dirent *ent; | |
198 | ||
199 | by_path = opendir(symlink); | |
200 | if (!by_path) | |
201 | return NULL; | |
202 | prefix_len = strlen(symlink); | |
203 | ||
204 | while ((ent = readdir(by_path)) != NULL) { | |
205 | if (ent->d_type != DT_LNK) | |
206 | continue; | |
207 | strncpy(symlink + prefix_len, | |
208 | ent->d_name, | |
209 | sizeof(symlink) - prefix_len); | |
210 | if (stat(symlink, &stb) < 0) | |
211 | continue; | |
212 | if ((stb.st_mode & S_IFMT) != S_IFBLK) | |
213 | continue; | |
214 | if (stb.st_rdev != makedev(disk->disk.major, disk->disk.minor)) | |
215 | continue; | |
216 | closedir(by_path); | |
217 | return strdup(ent->d_name); | |
218 | } | |
219 | closedir(by_path); | |
220 | return NULL; | |
221 | } | |
222 | ||
223 | char type_part[] = "part"; | |
224 | char type_disk[] = "disk"; | |
225 | static char *disk_type(struct mdinfo *disk) | |
226 | { | |
227 | char buf[30+20+20]; | |
228 | struct stat stb; | |
229 | sprintf(buf, "/sys/dev/block/%d:%d/partition", | |
230 | disk->disk.major, disk->disk.minor); | |
231 | if (stat(buf, &stb) == 0) | |
232 | return type_part; | |
233 | else | |
234 | return type_disk; | |
235 | } | |
236 | ||
237 | static int pol_match(struct rule *rule, char *path, char *type) | |
238 | { | |
239 | /* check if this rule matches on path and type */ | |
240 | int pathok = 0; /* 0 == no path, 1 == match, -1 == no match yet */ | |
241 | int typeok = 0; | |
242 | ||
243 | while (rule) { | |
244 | if (rule->name == rule_path) { | |
245 | if (pathok == 0) | |
246 | pathok = -1; | |
247 | if (fnmatch(rule->value, path, 0) == 0) | |
248 | pathok = 1; | |
249 | } | |
250 | if (rule->name == rule_type) { | |
251 | if (typeok == 0) | |
252 | typeok = -1; | |
253 | if (strcmp(rule->value, type) == 0) | |
254 | typeok = 1; | |
255 | } | |
256 | rule = rule->next; | |
257 | } | |
258 | return pathok >= 0 && typeok >= 0; | |
259 | } | |
260 | ||
261 | static void pol_merge(struct dev_policy **pol, struct rule *rule) | |
262 | { | |
263 | /* copy any name assignments from rule into pol */ | |
264 | struct rule *r; | |
265 | char *metadata = NULL; | |
266 | for (r = rule; r ; r = r->next) | |
267 | if (r->name == pol_metadata) | |
268 | metadata = r->value; | |
269 | ||
270 | for (r = rule; r ; r = r->next) | |
271 | if (r->name == pol_act || | |
272 | r->name == pol_domain) | |
273 | pol_new(pol, r->name, r->value, metadata); | |
274 | } | |
275 | ||
276 | static int path_has_part(char *path, char **part) | |
277 | { | |
278 | /* check if path ends with "-partNN" and | |
279 | * if it does, place a pointer to "-pathNN" | |
280 | * in 'part'. | |
281 | */ | |
282 | int l = strlen(path); | |
283 | while (l > 1 && isdigit(path[l-1])) | |
284 | l--; | |
285 | if (l < 5 || strncmp(path+l-5, "-part", 5) != 0) | |
286 | return 0; | |
287 | *part = path+l-4; | |
288 | return 1; | |
289 | } | |
290 | ||
291 | static void pol_merge_part(struct dev_policy **pol, struct rule *rule, char *part) | |
292 | { | |
293 | /* copy any name assignments from rule into pol, appending | |
294 | * -part to any domain. The string with -part appended is | |
295 | * stored with the rule so it has a lifetime to match | |
296 | * the rule. | |
297 | */ | |
298 | struct rule *r; | |
299 | char *metadata = NULL; | |
300 | for (r = rule; r ; r = r->next) | |
301 | if (r->name == pol_metadata) | |
302 | metadata = r->value; | |
303 | ||
304 | for (r = rule; r ; r = r->next) { | |
305 | if (r->name == pol_act) | |
306 | pol_new(pol, r->name, r->value, metadata); | |
307 | else if (r->name == pol_domain) { | |
308 | char *dom; | |
309 | int len; | |
310 | if (r->dups == NULL) | |
311 | r->dups = dl_head(); | |
312 | len = strlen(r->value); | |
313 | for (dom = dl_next(r->dups); dom != r->dups; | |
314 | dom = dl_next(dom)) | |
315 | if (strcmp(dom+len+1, part)== 0) | |
316 | break; | |
317 | if (dom == r->dups) { | |
318 | char *newdom = dl_strndup( | |
319 | r->value, len + 1 + strlen(part)); | |
320 | strcat(strcat(newdom, "-"), part); | |
321 | dl_add(r->dups, newdom); | |
322 | dom = newdom; | |
323 | } | |
324 | pol_new(pol, r->name, dom, metadata); | |
325 | } | |
326 | } | |
327 | } | |
328 | ||
329 | static struct pol_rule *config_rules = NULL; | |
330 | static struct pol_rule **config_rules_end = NULL; | |
331 | static int config_rules_has_path = 0; | |
332 | ||
333 | /* | |
334 | * most policy comes from a set policy rules that are | |
335 | * read from the config file. | |
73c9c47c N |
336 | * path_policy() gathers policy information for the |
337 | * disk described in the given a 'path' and a 'type'. | |
5527fc74 | 338 | */ |
73c9c47c | 339 | struct dev_policy *path_policy(char *path, char *type) |
5527fc74 | 340 | { |
5527fc74 N |
341 | struct pol_rule *rules; |
342 | struct dev_policy *pol = NULL; | |
343 | ||
344 | if (!type) | |
345 | return NULL; | |
5527fc74 N |
346 | |
347 | rules = config_rules; | |
348 | ||
349 | while (rules) { | |
350 | char *part; | |
351 | if (rules->type == rule_policy) | |
352 | if (pol_match(rules->rule, path, type)) | |
353 | pol_merge(&pol, rules->rule); | |
354 | if (rules->type == rule_part && strcmp(type, type_part) == 0) | |
355 | if (path_has_part(path, &part)) { | |
356 | *part = 0; | |
357 | if (pol_match(rules->rule, path, type_disk)) | |
358 | pol_merge_part(&pol, rules->rule, part+1); | |
359 | *part = '-'; | |
360 | } | |
361 | rules = rules->next; | |
362 | } | |
363 | pol_sort(&pol); | |
364 | pol_dedup(pol); | |
73c9c47c N |
365 | return pol; |
366 | } | |
367 | ||
368 | /* | |
369 | * disk_policy() gathers policy information for the | |
370 | * disk described in the given mdinfo (disk.{major,minor}). | |
371 | */ | |
372 | struct dev_policy *disk_policy(struct mdinfo *disk) | |
373 | { | |
374 | char *path = NULL; | |
375 | char *type = disk_type(disk); | |
376 | struct dev_policy *pol = NULL; | |
377 | ||
378 | if (!type) | |
379 | return NULL; | |
380 | if (config_rules_has_path) { | |
381 | path = disk_path(disk); | |
382 | if (!path) | |
383 | return NULL; | |
384 | } | |
385 | ||
386 | pol = path_policy(path, type); | |
387 | ||
5527fc74 N |
388 | free(path); |
389 | return pol; | |
390 | } | |
391 | ||
4e8d9f0a N |
392 | struct dev_policy *devnum_policy(int dev) |
393 | { | |
394 | struct mdinfo disk; | |
395 | disk.disk.major = major(dev); | |
396 | disk.disk.minor = minor(dev); | |
397 | return disk_policy(&disk); | |
398 | } | |
399 | ||
5527fc74 N |
400 | /* |
401 | * process policy rules read from config file. | |
402 | */ | |
403 | ||
404 | char rule_path[] = "path"; | |
405 | char rule_type[] = "type"; | |
406 | ||
407 | char rule_policy[] = "policy"; | |
408 | char rule_part[] = "part-policy"; | |
409 | ||
410 | char pol_metadata[] = "metadata"; | |
411 | char pol_act[] = "action"; | |
412 | char pol_domain[] = "domain"; | |
4e8d9f0a | 413 | char pol_auto[] = "auto"; |
5527fc74 N |
414 | |
415 | static int try_rule(char *w, char *name, struct rule **rp) | |
416 | { | |
417 | struct rule *r; | |
418 | int len = strlen(name); | |
419 | if (strncmp(w, name, len) != 0 || | |
420 | w[len] != '=') | |
421 | return 0; | |
422 | r = malloc(sizeof(*r)); | |
423 | r->next = *rp; | |
424 | r->name = name; | |
425 | r->value = strdup(w+len+1); | |
426 | r->dups = NULL; | |
427 | *rp = r; | |
428 | return 1; | |
429 | } | |
430 | ||
431 | void policyline(char *line, char *type) | |
432 | { | |
433 | struct pol_rule *pr; | |
434 | char *w; | |
435 | ||
436 | if (config_rules_end == NULL) | |
437 | config_rules_end = &config_rules; | |
438 | ||
439 | pr = malloc(sizeof(*pr)); | |
440 | pr->type = type; | |
441 | pr->rule = NULL; | |
442 | for (w = dl_next(line); w != line ; w = dl_next(w)) { | |
443 | if (try_rule(w, rule_path, &pr->rule)) | |
444 | config_rules_has_path = 1; | |
445 | else if (! try_rule(w, rule_type, &pr->rule) && | |
446 | ! try_rule(w, pol_metadata, &pr->rule) && | |
447 | ! try_rule(w, pol_act, &pr->rule) && | |
4e8d9f0a N |
448 | ! try_rule(w, pol_domain, &pr->rule) && |
449 | ! try_rule(w, pol_auto, &pr->rule)) | |
5527fc74 N |
450 | fprintf(stderr, Name ": policy rule %s unrecognised and ignored\n", |
451 | w); | |
452 | } | |
453 | pr->next = config_rules; | |
454 | config_rules = pr; | |
455 | } | |
456 | ||
4e8d9f0a N |
457 | void policy_add(char *type, ...) |
458 | { | |
459 | va_list ap; | |
460 | struct pol_rule *pr; | |
461 | char *name, *val; | |
462 | ||
463 | pr = malloc(sizeof(*pr)); | |
464 | pr->type = type; | |
465 | pr->rule = NULL; | |
466 | ||
467 | va_start(ap, type); | |
468 | while ((name = va_arg(ap, char*)) != NULL) { | |
469 | struct rule *r; | |
470 | ||
471 | val = va_arg(ap, char*); | |
472 | r = malloc(sizeof(*r)); | |
473 | r->next = pr->rule; | |
474 | r->name = name; | |
475 | r->value = strdup(val); | |
476 | r->dups = NULL; | |
477 | pr->rule = r; | |
478 | } | |
479 | pr->next = config_rules; | |
480 | config_rules = pr; | |
481 | } | |
482 | ||
5527fc74 N |
483 | void policy_free(void) |
484 | { | |
485 | while (config_rules) { | |
486 | struct pol_rule *pr = config_rules; | |
487 | struct rule *r; | |
488 | ||
489 | config_rules = config_rules->next; | |
490 | ||
491 | for (r = pr->rule; r; ) { | |
492 | struct rule *next = r->next; | |
493 | free(r->value); | |
494 | if (r->dups) | |
495 | free_line(r->dups); | |
496 | free(r); | |
497 | r = next; | |
498 | } | |
499 | free(pr); | |
500 | } | |
501 | config_rules_end = NULL; | |
502 | config_rules_has_path = 0; | |
503 | } | |
504 | ||
505 | void dev_policy_free(struct dev_policy *p) | |
506 | { | |
507 | struct dev_policy *t; | |
508 | while (p) { | |
509 | t = p; | |
510 | p = p->next; | |
511 | free(t); | |
512 | } | |
513 | } | |
e3bb5f14 N |
514 | |
515 | static enum policy_action map_act(char *act) | |
516 | { | |
517 | if (strcmp(act, "include") == 0) | |
518 | return act_include; | |
519 | if (strcmp(act, "re-add") == 0) | |
520 | return act_re_add; | |
521 | if (strcmp(act, "spare") == 0) | |
522 | return act_spare; | |
523 | if (strcmp(act, "force-spare") == 0) | |
524 | return act_force_spare; | |
525 | return act_err; | |
526 | } | |
527 | ||
528 | static enum policy_action policy_action(struct dev_policy *plist, const char *metadata) | |
529 | { | |
530 | enum policy_action rv = act_default; | |
531 | struct dev_policy *p; | |
532 | ||
533 | plist = pol_find(plist, pol_act); | |
534 | pol_for_each(p, plist, metadata) { | |
535 | enum policy_action a = map_act(p->value); | |
536 | if (a > rv) | |
537 | rv = a; | |
538 | } | |
539 | return rv; | |
540 | } | |
541 | ||
542 | int policy_action_allows(struct dev_policy *plist, const char *metadata, enum policy_action want) | |
543 | { | |
544 | enum policy_action act = policy_action(plist, metadata); | |
545 | ||
546 | if (act == act_err) | |
547 | return 0; | |
548 | return (act >= want); | |
549 | } | |
550 | ||
551 | int disk_action_allows(struct mdinfo *disk, const char *metadata, enum policy_action want) | |
552 | { | |
553 | struct dev_policy *pol = disk_policy(disk); | |
554 | int rv = policy_action_allows(pol, metadata, want); | |
555 | ||
556 | dev_policy_free(pol); | |
557 | return rv; | |
558 | } | |
f5f12c84 N |
559 | |
560 | ||
561 | /* Domain policy: | |
562 | * Any device can have a list of domains asserted by different policy | |
563 | * statements. | |
564 | * An array also has a list of domains comprising all the domains of | |
565 | * all the devices in an array. | |
566 | * Where an array has a spare-group, that becomes an addition domain for | |
567 | * every device in the array and thus for the array. | |
568 | * | |
569 | * We keep the list of domains in a sorted linked list | |
570 | * As dev policies are already sorted, this is fairly easy to manage. | |
571 | */ | |
572 | ||
573 | static struct domainlist **domain_merge_one(struct domainlist **domp, char *domain) | |
574 | { | |
575 | /* merge a domain name into a sorted list and return the | |
576 | * location of the insertion or match | |
577 | */ | |
578 | struct domainlist *dom = *domp; | |
579 | ||
580 | while (dom && strcmp(dom->dom, domain) < 0) { | |
581 | domp = &dom->next; | |
582 | dom = *domp; | |
583 | } | |
584 | if (dom == NULL || strcmp(dom->dom, domain) != 0) { | |
585 | dom = malloc(sizeof(*dom)); | |
586 | dom->next = *domp; | |
587 | dom->dom = domain; | |
588 | *domp = dom; | |
589 | } | |
590 | return domp; | |
591 | } | |
592 | ||
593 | void domain_merge(struct domainlist **domp, struct dev_policy *pollist, | |
594 | const char *metadata) | |
595 | { | |
596 | /* Add to 'domp' all the domains in pol that apply to 'metadata' | |
597 | * which are not already in domp | |
598 | */ | |
599 | struct dev_policy *pol; | |
600 | pollist = pol_find(pollist, pol_domain); | |
601 | pol_for_each(pol, pollist, metadata) | |
602 | domp = domain_merge_one(domp, pol->value); | |
603 | } | |
604 | ||
605 | int domain_test(struct domainlist *dom, struct dev_policy *pol, | |
606 | const char *metadata) | |
607 | { | |
608 | /* Check that all domains in pol (for metadata) are also in | |
609 | * dom. Both lists are sorted. | |
610 | * If pol has no domains, we don't really know about this device | |
611 | * so we reject the match. | |
612 | */ | |
613 | int found_any = 0; | |
614 | struct dev_policy *p; | |
615 | ||
616 | pol = pol_find(pol, pol_domain); | |
617 | pol_for_each(p, pol, metadata) { | |
618 | found_any = 1; | |
4e8d9f0a | 619 | while (dom && strcmp(dom->dom, p->value) < 0) |
f5f12c84 | 620 | dom = dom->next; |
4e8d9f0a | 621 | if (!dom || strcmp(dom->dom, p->value) != 0) |
f5f12c84 N |
622 | return 0; |
623 | } | |
624 | return found_any; | |
625 | } | |
626 | ||
627 | struct domainlist *domain_from_array(struct mdinfo *mdi, const char *metadata) | |
628 | { | |
629 | struct domainlist *domlist = NULL; | |
630 | ||
631 | for (mdi = mdi->devs ; mdi ; mdi = mdi->next) { | |
632 | struct dev_policy *pol = disk_policy(mdi); | |
633 | ||
634 | domain_merge(&domlist, pol, metadata); | |
635 | dev_policy_free(pol); | |
636 | } | |
637 | return domlist; | |
638 | } | |
639 | ||
640 | void domain_free(struct domainlist *dl) | |
641 | { | |
642 | while (dl) { | |
643 | struct domainlist *head = dl; | |
644 | dl = dl->next; | |
645 | free(head); | |
646 | } | |
647 | } |