]> git.ipfire.org Git - thirdparty/mdadm.git/blob - policy.c
mdadm: load default sysfs attributes after assemblation
[thirdparty/mdadm.git] / policy.c
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 static void pol_new(struct dev_policy **pol, char *name, const char *val,
44 const char *metadata)
45 {
46 struct dev_policy *n = xmalloc(sizeof(*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) {
68 static const char *prev = NULL;
69 if (prev != metadata) {
70 pr_err("metadata=%s unrecognised - ignoring rule\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])
141 ^pol_lesseq(prev, p[next])
142 ^pol_lesseq(p[next], p[1-next])))
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_paths(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/";
198 char **paths;
199 int cnt = 0;
200 struct dirent *ent;
201
202 paths = xmalloc(sizeof(*paths) * (cnt+1));
203
204 by_path = opendir(symlink);
205 if (by_path) {
206 prefix_len = strlen(symlink);
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 paths[cnt++] = xstrdup(ent->d_name);
220 paths = xrealloc(paths, sizeof(*paths) * (cnt+1));
221 }
222 closedir(by_path);
223 }
224 paths[cnt] = NULL;
225 return paths;
226 }
227
228 char type_part[] = "part";
229 char type_disk[] = "disk";
230 static char *disk_type(struct mdinfo *disk)
231 {
232 char buf[30+20+20];
233 struct stat stb;
234 sprintf(buf, "/sys/dev/block/%d:%d/partition",
235 disk->disk.major, disk->disk.minor);
236 if (stat(buf, &stb) == 0)
237 return type_part;
238 else
239 return type_disk;
240 }
241
242 static int path_has_part(char *path, char **part)
243 {
244 /* check if path ends with "-partNN" and
245 * if it does, place a pointer to "-pathNN"
246 * in 'part'.
247 */
248 int l;
249 if (!path)
250 return 0;
251 l = strlen(path);
252 while (l > 1 && isdigit(path[l-1]))
253 l--;
254 if (l < 5 || strncmp(path+l-5, "-part", 5) != 0)
255 return 0;
256 *part = path+l-5;
257 return 1;
258 }
259
260 static int pol_match(struct rule *rule, char **paths, char *type, char **part)
261 {
262 /* Check if this rule matches on any path and type.
263 * If 'part' is not NULL, then 'path' must end in -partN, which
264 * we ignore for matching, and return in *part on success.
265 */
266 int pathok = 0; /* 0 == no path, 1 == match, -1 == no match yet */
267 int typeok = 0;
268
269 for (; rule; rule = rule->next) {
270 if (rule->name == rule_path) {
271 char *p = NULL;
272 int i;
273 if (pathok == 0)
274 pathok = -1;
275 if (!paths)
276 continue;
277 for (i = 0; paths[i]; i++) {
278 if (part) {
279 if (!path_has_part(paths[i], &p))
280 continue;
281 *p = '\0';
282 *part = p+1;
283 }
284 if (fnmatch(rule->value, paths[i], 0) == 0)
285 pathok = 1;
286 if (part)
287 *p = '-';
288 }
289 }
290 if (rule->name == rule_type) {
291 if (typeok == 0)
292 typeok = -1;
293 if (type && strcmp(rule->value, type) == 0)
294 typeok = 1;
295 }
296 }
297 return pathok >= 0 && typeok >= 0;
298 }
299
300 static void pol_merge(struct dev_policy **pol, struct rule *rule)
301 {
302 /* copy any name assignments from rule into pol */
303 struct rule *r;
304 char *metadata = NULL;
305 for (r = rule; r ; r = r->next)
306 if (r->name == pol_metadata)
307 metadata = r->value;
308
309 for (r = rule; r ; r = r->next)
310 if (r->name == pol_act ||
311 r->name == pol_domain ||
312 r->name == pol_auto)
313 pol_new(pol, r->name, r->value, metadata);
314 }
315
316 static void pol_merge_part(struct dev_policy **pol, struct rule *rule, char *part)
317 {
318 /* copy any name assignments from rule into pol, appending
319 * -part to any domain. The string with -part appended is
320 * stored with the rule so it has a lifetime to match
321 * the rule.
322 */
323 struct rule *r;
324 char *metadata = NULL;
325 for (r = rule; r ; r = r->next)
326 if (r->name == pol_metadata)
327 metadata = r->value;
328
329 for (r = rule; r ; r = r->next) {
330 if (r->name == pol_act)
331 pol_new(pol, r->name, r->value, metadata);
332 else if (r->name == pol_domain) {
333 char *dom;
334 int len;
335 if (r->dups == NULL)
336 r->dups = dl_head();
337 len = strlen(r->value);
338 for (dom = dl_next(r->dups); dom != r->dups;
339 dom = dl_next(dom))
340 if (strcmp(dom+len+1, part)== 0)
341 break;
342 if (dom == r->dups) {
343 char *newdom = dl_strndup(
344 r->value, len + 1 + strlen(part));
345 strcat(strcat(newdom, "-"), part);
346 dl_add(r->dups, newdom);
347 dom = newdom;
348 }
349 pol_new(pol, r->name, dom, metadata);
350 }
351 }
352 }
353
354 static struct pol_rule *config_rules = NULL;
355 static struct pol_rule **config_rules_end = NULL;
356 static int config_rules_has_path = 0;
357
358 /*
359 * most policy comes from a set policy rules that are
360 * read from the config file.
361 * path_policy() gathers policy information for the
362 * disk described in the given a 'path' and a 'type'.
363 */
364 struct dev_policy *path_policy(char **paths, char *type)
365 {
366 struct pol_rule *rules;
367 struct dev_policy *pol = NULL;
368 int i;
369
370 rules = config_rules;
371
372 while (rules) {
373 char *part = NULL;
374 if (rules->type == rule_policy)
375 if (pol_match(rules->rule, paths, type, NULL))
376 pol_merge(&pol, rules->rule);
377 if (rules->type == rule_part && strcmp(type, type_part) == 0)
378 if (pol_match(rules->rule, paths, type_disk, &part))
379 pol_merge_part(&pol, rules->rule, part);
380 rules = rules->next;
381 }
382
383 /* Now add any metadata-specific internal knowledge
384 * about this path
385 */
386 for (i=0; paths && paths[0] && superlist[i]; i++)
387 if (superlist[i]->get_disk_controller_domain) {
388 const char *d =
389 superlist[i]->get_disk_controller_domain(
390 paths[0]);
391 if (d)
392 pol_new(&pol, pol_domain, d, superlist[i]->name);
393 }
394
395 pol_sort(&pol);
396 pol_dedup(pol);
397 return pol;
398 }
399
400 void pol_add(struct dev_policy **pol,
401 char *name, char *val,
402 char *metadata)
403 {
404 pol_new(pol, name, val, metadata);
405 pol_sort(pol);
406 pol_dedup(*pol);
407 }
408
409 static void free_paths(char **paths)
410 {
411 int i;
412
413 if (!paths)
414 return;
415
416 for (i = 0; paths[i]; i++)
417 free(paths[i]);
418 free(paths);
419 }
420
421 /*
422 * disk_policy() gathers policy information for the
423 * disk described in the given mdinfo (disk.{major,minor}).
424 */
425 struct dev_policy *disk_policy(struct mdinfo *disk)
426 {
427 char **paths = NULL;
428 char *type = disk_type(disk);
429 struct dev_policy *pol = NULL;
430
431 if (config_rules_has_path)
432 paths = disk_paths(disk);
433
434 pol = path_policy(paths, type);
435
436 free_paths(paths);
437 return pol;
438 }
439
440 struct dev_policy *devid_policy(int dev)
441 {
442 struct mdinfo disk;
443 disk.disk.major = major(dev);
444 disk.disk.minor = minor(dev);
445 return disk_policy(&disk);
446 }
447
448 /*
449 * process policy rules read from config file.
450 */
451
452 char rule_path[] = "path";
453 char rule_type[] = "type";
454
455 char rule_policy[] = "policy";
456 char rule_part[] = "part-policy";
457
458 char pol_metadata[] = "metadata";
459 char pol_act[] = "action";
460 char pol_domain[] = "domain";
461 char pol_auto[] = "auto";
462
463 static int try_rule(char *w, char *name, struct rule **rp)
464 {
465 struct rule *r;
466 int len = strlen(name);
467 if (strncmp(w, name, len) != 0 ||
468 w[len] != '=')
469 return 0;
470 r = xmalloc(sizeof(*r));
471 r->next = *rp;
472 r->name = name;
473 r->value = xstrdup(w+len+1);
474 r->dups = NULL;
475 *rp = r;
476 return 1;
477 }
478
479 void policyline(char *line, char *type)
480 {
481 struct pol_rule *pr;
482 char *w;
483
484 if (config_rules_end == NULL)
485 config_rules_end = &config_rules;
486
487 pr = xmalloc(sizeof(*pr));
488 pr->type = type;
489 pr->rule = NULL;
490 for (w = dl_next(line); w != line ; w = dl_next(w)) {
491 if (try_rule(w, rule_path, &pr->rule))
492 config_rules_has_path = 1;
493 else if (! try_rule(w, rule_type, &pr->rule) &&
494 ! try_rule(w, pol_metadata, &pr->rule) &&
495 ! try_rule(w, pol_act, &pr->rule) &&
496 ! try_rule(w, pol_domain, &pr->rule) &&
497 ! try_rule(w, pol_auto, &pr->rule))
498 pr_err("policy rule %s unrecognised and ignored\n",
499 w);
500 }
501 pr->next = config_rules;
502 config_rules = pr;
503 }
504
505 void policy_add(char *type, ...)
506 {
507 va_list ap;
508 struct pol_rule *pr;
509 char *name, *val;
510
511 pr = xmalloc(sizeof(*pr));
512 pr->type = type;
513 pr->rule = NULL;
514
515 va_start(ap, type);
516 while ((name = va_arg(ap, char*)) != NULL) {
517 struct rule *r;
518
519 val = va_arg(ap, char*);
520 r = xmalloc(sizeof(*r));
521 r->next = pr->rule;
522 r->name = name;
523 r->value = xstrdup(val);
524 r->dups = NULL;
525 pr->rule = r;
526 }
527 pr->next = config_rules;
528 config_rules = pr;
529 va_end(ap);
530 }
531
532 void policy_free(void)
533 {
534 while (config_rules) {
535 struct pol_rule *pr = config_rules;
536 struct rule *r;
537
538 config_rules = config_rules->next;
539
540 for (r = pr->rule; r; ) {
541 struct rule *next = r->next;
542 free(r->value);
543 if (r->dups)
544 free_line(r->dups);
545 free(r);
546 r = next;
547 }
548 free(pr);
549 }
550 config_rules_end = NULL;
551 config_rules_has_path = 0;
552 }
553
554 void dev_policy_free(struct dev_policy *p)
555 {
556 struct dev_policy *t;
557 while (p) {
558 t = p;
559 p = p->next;
560 free(t);
561 }
562 }
563
564 static enum policy_action map_act(const char *act)
565 {
566 if (strcmp(act, "include") == 0)
567 return act_include;
568 if (strcmp(act, "re-add") == 0)
569 return act_re_add;
570 if (strcmp(act, "spare") == 0)
571 return act_spare;
572 if (strcmp(act, "spare-same-slot") == 0)
573 return act_spare_same_slot;
574 if (strcmp(act, "force-spare") == 0)
575 return act_force_spare;
576 return act_err;
577 }
578
579 static enum policy_action policy_action(struct dev_policy *plist, const char *metadata)
580 {
581 enum policy_action rv = act_default;
582 struct dev_policy *p;
583
584 plist = pol_find(plist, pol_act);
585 pol_for_each(p, plist, metadata) {
586 enum policy_action a = map_act(p->value);
587 if (a > rv)
588 rv = a;
589 }
590 return rv;
591 }
592
593 int policy_action_allows(struct dev_policy *plist, const char *metadata, enum policy_action want)
594 {
595 enum policy_action act = policy_action(plist, metadata);
596
597 if (act == act_err)
598 return 0;
599 return (act >= want);
600 }
601
602 int disk_action_allows(struct mdinfo *disk, const char *metadata, enum policy_action want)
603 {
604 struct dev_policy *pol = disk_policy(disk);
605 int rv = policy_action_allows(pol, metadata, want);
606
607 dev_policy_free(pol);
608 return rv;
609 }
610
611 /* Domain policy:
612 * Any device can have a list of domains asserted by different policy
613 * statements.
614 * An array also has a list of domains comprising all the domains of
615 * all the devices in an array.
616 * Where an array has a spare-group, that becomes an addition domain for
617 * every device in the array and thus for the array.
618 *
619 * We keep the list of domains in a sorted linked list
620 * As dev policies are already sorted, this is fairly easy to manage.
621 */
622
623 static struct domainlist **domain_merge_one(struct domainlist **domp,
624 const char *domain)
625 {
626 /* merge a domain name into a sorted list and return the
627 * location of the insertion or match
628 */
629 struct domainlist *dom = *domp;
630
631 while (dom && strcmp(dom->dom, domain) < 0) {
632 domp = &dom->next;
633 dom = *domp;
634 }
635 if (dom == NULL || strcmp(dom->dom, domain) != 0) {
636 dom = xmalloc(sizeof(*dom));
637 dom->next = *domp;
638 dom->dom = domain;
639 *domp = dom;
640 }
641 return domp;
642 }
643
644 #if (DEBUG)
645 void dump_policy(struct dev_policy *policy)
646 {
647 while (policy) {
648 dprintf("policy: %p name: %s value: %s metadata: %s\n",
649 policy,
650 policy->name,
651 policy->value,
652 policy->metadata);
653 policy = policy->next;
654 }
655 }
656 #endif
657
658 void domain_merge(struct domainlist **domp, struct dev_policy *pollist,
659 const char *metadata)
660 {
661 /* Add to 'domp' all the domains in pol that apply to 'metadata'
662 * which are not already in domp
663 */
664 struct dev_policy *pol;
665 pollist = pol_find(pollist, pol_domain);
666 pol_for_each(pol, pollist, metadata)
667 domain_merge_one(domp, pol->value);
668 }
669
670 int domain_test(struct domainlist *dom, struct dev_policy *pol,
671 const char *metadata)
672 {
673 /* Check that all domains in pol (for metadata) are also in
674 * dom. Both lists are sorted.
675 * If pol has no domains, we don't really know about this device
676 * so we allow caller to choose:
677 * -1: has no domains
678 * 0: has domains, not all match
679 * 1: has domains, all match
680 */
681 int found_any = -1;
682 int has_one_domain = 1;
683 struct dev_policy *p;
684
685 pol = pol_find(pol, pol_domain);
686 pol_for_each(p, pol, metadata) {
687 found_any = 1;
688 while (dom && strcmp(dom->dom, p->value) < 0)
689 dom = dom->next;
690 if (!dom || strcmp(dom->dom, p->value) != 0)
691 return 0;
692 if (has_one_domain && metadata && strcmp(metadata, "imsm") == 0)
693 found_any = -1;
694 has_one_domain = 0;
695 }
696 return found_any;
697 }
698
699 void domainlist_add_dev(struct domainlist **dom, int devid, const char *metadata)
700 {
701 struct dev_policy *pol = devid_policy(devid);
702 domain_merge(dom, pol, metadata);
703 dev_policy_free(pol);
704 }
705
706 struct domainlist *domain_from_array(struct mdinfo *mdi, const char *metadata)
707 {
708 struct domainlist *domlist = NULL;
709
710 if (!mdi)
711 return NULL;
712 for (mdi = mdi->devs ; mdi ; mdi = mdi->next)
713 domainlist_add_dev(&domlist, makedev(mdi->disk.major,
714 mdi->disk.minor),
715 metadata);
716
717 return domlist;
718 }
719
720 void domain_add(struct domainlist **domp, char *domain)
721 {
722 domain_merge_one(domp, domain);
723 }
724
725 void domain_free(struct domainlist *dl)
726 {
727 while (dl) {
728 struct domainlist *head = dl;
729 dl = dl->next;
730 free(head);
731 }
732 }
733
734 /*
735 * same-path policy.
736 * Some policy decisions are guided by knowledge of which
737 * array previously owned the device at a given physical location (path).
738 * When removing a device from an array we might record the array against
739 * the path, and when finding a new device, we might look for which
740 * array previously used that path.
741 *
742 * The 'array' is described by a map_ent, and the path by a the disk in an
743 * mdinfo, or a string.
744 */
745
746 void policy_save_path(char *id_path, struct map_ent *array)
747 {
748 char path[PATH_MAX];
749 FILE *f = NULL;
750
751 if (mkdir(FAILED_SLOTS_DIR, S_IRWXU) < 0 && errno != EEXIST) {
752 pr_err("can't create file to save path to old disk: %s\n", strerror(errno));
753 return;
754 }
755
756 snprintf(path, PATH_MAX, FAILED_SLOTS_DIR "/%s", id_path);
757 f = fopen(path, "w");
758 if (!f) {
759 pr_err("can't create file to save path to old disk: %s\n",
760 strerror(errno));
761 return;
762 }
763
764 if (fprintf(f, "%s %08x:%08x:%08x:%08x\n",
765 array->metadata,
766 array->uuid[0], array->uuid[1],
767 array->uuid[2], array->uuid[3]) <= 0)
768 pr_err("Failed to write to <id_path> cookie\n");
769
770 fclose(f);
771 }
772
773 int policy_check_path(struct mdinfo *disk, struct map_ent *array)
774 {
775 char path[PATH_MAX];
776 FILE *f = NULL;
777 char **id_paths = disk_paths(disk);
778 int i;
779 int rv = 0;
780
781 for (i = 0; id_paths[i]; i++) {
782 snprintf(path, PATH_MAX, FAILED_SLOTS_DIR "/%s", id_paths[i]);
783 f = fopen(path, "r");
784 if (!f)
785 continue;
786
787 rv = fscanf(f, " %s %x:%x:%x:%x\n",
788 array->metadata,
789 array->uuid,
790 array->uuid+1,
791 array->uuid+2,
792 array->uuid+3);
793 fclose(f);
794 break;
795 }
796 free_paths(id_paths);
797 return rv == 5;
798 }
799
800 /* invocation of udev rule file */
801 char udev_template_start[] =
802 "# do not edit this file, it is automatically generated by mdadm\n"
803 "\n";
804
805 /* find rule named rule_type and return its value */
806 char *find_rule(struct rule *rule, char *rule_type)
807 {
808 while (rule) {
809 if (rule->name == rule_type)
810 return rule->value;
811
812 rule = rule->next;
813 }
814 return NULL;
815 }
816
817 #define UDEV_RULE_FORMAT \
818 "ACTION==\"add\", SUBSYSTEM==\"block\", " \
819 "ENV{DEVTYPE}==\"%s\", ENV{ID_PATH}==\"%s\", " \
820 "RUN+=\"" BINDIR "/mdadm --incremental $env{DEVNAME}\"\n"
821
822 #define UDEV_RULE_FORMAT_NOTYPE \
823 "ACTION==\"add\", SUBSYSTEM==\"block\", " \
824 "ENV{ID_PATH}==\"%s\", " \
825 "RUN+=\"" BINDIR "/mdadm --incremental $env{DEVNAME}\"\n"
826
827 /* Write rule in the rule file. Use format from UDEV_RULE_FORMAT */
828 int write_rule(struct rule *rule, int fd, int force_part)
829 {
830 char line[1024];
831 char *pth = find_rule(rule, rule_path);
832 char *typ = find_rule(rule, rule_type);
833 if (!pth)
834 return -1;
835
836 if (force_part)
837 typ = type_part;
838 if (typ)
839 snprintf(line, sizeof(line) - 1, UDEV_RULE_FORMAT, typ, pth);
840 else
841 snprintf(line, sizeof(line) - 1, UDEV_RULE_FORMAT_NOTYPE, pth);
842 return write(fd, line, strlen(line)) == (int)strlen(line);
843 }
844
845 /* Generate single entry in udev rule basing on POLICY line found in config
846 * file. Take only those with paths, only first occurrence if paths are equal
847 * and if actions supports handling of spares (>=act_spare_same_slot)
848 */
849 int generate_entries(int fd)
850 {
851 struct pol_rule *loop, *dup;
852 char *loop_value, *dup_value;
853 int duplicate;
854
855 for (loop = config_rules; loop; loop = loop->next) {
856 if (loop->type != rule_policy && loop->type != rule_part)
857 continue;
858 duplicate = 0;
859
860 /* only policies with paths and with actions supporting
861 * bare disks are considered */
862 loop_value = find_rule(loop->rule, pol_act);
863 if (!loop_value || map_act(loop_value) < act_spare_same_slot)
864 continue;
865 loop_value = find_rule(loop->rule, rule_path);
866 if (!loop_value)
867 continue;
868 for (dup = config_rules; dup != loop; dup = dup->next) {
869 if (dup->type != rule_policy && loop->type != rule_part)
870 continue;
871 dup_value = find_rule(dup->rule, pol_act);
872 if (!dup_value || map_act(dup_value) < act_spare_same_slot)
873 continue;
874 dup_value = find_rule(dup->rule, rule_path);
875 if (!dup_value)
876 continue;
877 if (strcmp(loop_value, dup_value) == 0) {
878 duplicate = 1;
879 break;
880 }
881 }
882
883 /* not a dup or first occurrence */
884 if (!duplicate)
885 if (!write_rule(loop->rule, fd, loop->type == rule_part) )
886 return 0;
887 }
888 return 1;
889 }
890
891 /* Write_rules routine creates dynamic udev rules used to handle
892 * hot-plug events for bare devices (and making them spares)
893 */
894 int Write_rules(char *rule_name)
895 {
896 int fd;
897 char udev_rule_file[PATH_MAX];
898
899 if (rule_name) {
900 strncpy(udev_rule_file, rule_name, sizeof(udev_rule_file) - 6);
901 udev_rule_file[sizeof(udev_rule_file) - 6] = '\0';
902 strcat(udev_rule_file, ".temp");
903 fd = creat(udev_rule_file,
904 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
905 if (fd == -1)
906 return 1;
907 } else
908 fd = 1;
909
910 /* write static invocation */
911 if (write(fd, udev_template_start, sizeof(udev_template_start) - 1) !=
912 (int)sizeof(udev_template_start) - 1)
913 goto abort;
914
915 /* iterate, if none created or error occurred, remove file */
916 if (generate_entries(fd) < 0)
917 goto abort;
918
919 fsync(fd);
920 if (rule_name) {
921 close(fd);
922 rename(udev_rule_file, rule_name);
923 }
924 return 0;
925 abort:
926 if (rule_name) {
927 close(fd);
928 unlink(udev_rule_file);
929 }
930 return 1;
931 }