2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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.
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.
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
22 * Email: <neilb@suse.de>
33 * Policy module for mdadm.
34 * A policy statement about a device lists a set of values for each
35 * of a set of names. Each value can have a metadata type as context.
38 * action - the actions that can be taken on hot-plug
39 * domain - the domain(s) that the device is part of
41 * Policy information is extracted from various sources, but
42 * particularly from a set of policy rules in mdadm.conf
45 static void pol_new(struct dev_policy
**pol
, char *name
, const char *val
,
48 struct dev_policy
*n
= xmalloc(sizeof(*n
));
49 const char *real_metadata
= NULL
;
55 /* We need to normalise the metadata name */
57 for (i
= 0; superlist
[i
] ; i
++)
58 if (strcmp(metadata
, superlist
[i
]->name
) == 0) {
59 real_metadata
= superlist
[i
]->name
;
63 if (strcmp(metadata
, "1") == 0 ||
64 strcmp(metadata
, "1.0") == 0 ||
65 strcmp(metadata
, "1.1") == 0 ||
66 strcmp(metadata
, "1.2") == 0)
67 real_metadata
= super1
.name
;
70 static const char *prev
= NULL
;
71 if (prev
!= metadata
) {
72 pr_err("metadata=%s unrecognised - ignoring rule\n",
76 real_metadata
= "unknown";
80 n
->metadata
= real_metadata
;
85 static int pol_lesseq(struct dev_policy
*a
, struct dev_policy
*b
)
89 if (a
->name
< b
->name
)
91 if (a
->name
> b
->name
)
94 cmp
= strcmp(a
->value
, b
->value
);
100 return (a
->metadata
<= b
->metadata
);
103 static void pol_sort(struct dev_policy
**pol
)
105 /* sort policy list in *pol by name/metadata/value
109 struct dev_policy
*pl
[2];
114 struct dev_policy
**plp
[2], *p
[2];
116 struct dev_policy nul
= { NULL
, NULL
, NULL
, NULL
};
117 struct dev_policy
*prev
= &nul
;
120 /* p[] are the two lists that we are merging.
121 * plp[] are the ends of the two lists we create
123 * 'curr' is which of plp[] that we are currently
125 * 'next' is which if p[] we will take the next
127 * 'prev' is that last value, which was placed in
135 /* take least of p[0] and p[1]
136 * if it is larger than prev, add to
137 * plp[curr], else swap curr then add
139 while (p
[0] || p
[1]) {
140 if (p
[next
] == NULL
||
141 (p
[1-next
] != NULL
&&
142 !(pol_lesseq(prev
, p
[1-next
])
143 ^pol_lesseq(prev
, p
[next
])
144 ^pol_lesseq(p
[next
], p
[1-next
])))
148 if (!pol_lesseq(prev
, p
[next
]))
151 *plp
[curr
] = prev
= p
[next
];
152 plp
[curr
] = &p
[next
]->next
;
153 p
[next
] = p
[next
]->next
;
157 } while (pl
[0] && pl
[1]);
164 static void pol_dedup(struct dev_policy
*pol
)
166 /* This is a sorted list - remove duplicates. */
167 while (pol
&& pol
->next
) {
168 if (pol_lesseq(pol
->next
, pol
)) {
169 struct dev_policy
*tmp
= pol
->next
;
170 pol
->next
= tmp
->next
;
178 * pol_find finds the first entry in the policy
179 * list to match name.
180 * If it returns non-NULL there is at least one
181 * value, but how many can only be found by
182 * iterating through the list.
184 struct dev_policy
*pol_find(struct dev_policy
*pol
, char *name
)
186 while (pol
&& pol
->name
< name
)
189 if (!pol
|| pol
->name
!= name
)
194 static char **disk_paths(struct mdinfo
*disk
)
199 char symlink
[PATH_MAX
] = "/dev/disk/by-path/";
204 paths
= xmalloc(sizeof(*paths
) * (cnt
+1));
206 by_path
= opendir(symlink
);
208 prefix_len
= strlen(symlink
);
209 while ((ent
= readdir(by_path
)) != NULL
) {
210 if (ent
->d_type
!= DT_LNK
)
212 strncpy(symlink
+ prefix_len
,
214 sizeof(symlink
) - prefix_len
);
215 if (stat(symlink
, &stb
) < 0)
217 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
)
219 if (stb
.st_rdev
!= makedev(disk
->disk
.major
, disk
->disk
.minor
))
221 paths
[cnt
++] = xstrdup(ent
->d_name
);
222 paths
= xrealloc(paths
, sizeof(*paths
) * (cnt
+1));
230 char type_part
[] = "part";
231 char type_disk
[] = "disk";
232 static char *disk_type(struct mdinfo
*disk
)
236 sprintf(buf
, "/sys/dev/block/%d:%d/partition",
237 disk
->disk
.major
, disk
->disk
.minor
);
238 if (stat(buf
, &stb
) == 0)
244 static int path_has_part(char *path
, char **part
)
246 /* check if path ends with "-partNN" and
247 * if it does, place a pointer to "-pathNN"
254 while (l
> 1 && isdigit(path
[l
-1]))
256 if (l
< 5 || strncmp(path
+l
-5, "-part", 5) != 0)
262 static int pol_match(struct rule
*rule
, char **paths
, char *type
, char **part
)
264 /* Check if this rule matches on any path and type.
265 * If 'part' is not NULL, then 'path' must end in -partN, which
266 * we ignore for matching, and return in *part on success.
268 int pathok
= 0; /* 0 == no path, 1 == match, -1 == no match yet */
271 for (; rule
; rule
= rule
->next
) {
272 if (rule
->name
== rule_path
) {
279 for (i
= 0; paths
[i
]; i
++) {
281 if (!path_has_part(paths
[i
], &p
))
286 if (fnmatch(rule
->value
, paths
[i
], 0) == 0)
292 if (rule
->name
== rule_type
) {
295 if (type
&& strcmp(rule
->value
, type
) == 0)
299 return pathok
>= 0 && typeok
>= 0;
302 static void pol_merge(struct dev_policy
**pol
, struct rule
*rule
)
304 /* copy any name assignments from rule into pol */
306 char *metadata
= NULL
;
307 for (r
= rule
; r
; r
= r
->next
)
308 if (r
->name
== pol_metadata
)
311 for (r
= rule
; r
; r
= r
->next
)
312 if (r
->name
== pol_act
||
313 r
->name
== pol_domain
||
315 pol_new(pol
, r
->name
, r
->value
, metadata
);
318 static void pol_merge_part(struct dev_policy
**pol
, struct rule
*rule
, char *part
)
320 /* copy any name assignments from rule into pol, appending
321 * -part to any domain. The string with -part appended is
322 * stored with the rule so it has a lifetime to match
326 char *metadata
= NULL
;
327 for (r
= rule
; r
; r
= r
->next
)
328 if (r
->name
== pol_metadata
)
331 for (r
= rule
; r
; r
= r
->next
) {
332 if (r
->name
== pol_act
)
333 pol_new(pol
, r
->name
, r
->value
, metadata
);
334 else if (r
->name
== pol_domain
) {
339 len
= strlen(r
->value
);
340 for (dom
= dl_next(r
->dups
); dom
!= r
->dups
;
342 if (strcmp(dom
+len
+1, part
)== 0)
344 if (dom
== r
->dups
) {
345 char *newdom
= dl_strndup(
346 r
->value
, len
+ 1 + strlen(part
));
347 strcat(strcat(newdom
, "-"), part
);
348 dl_add(r
->dups
, newdom
);
351 pol_new(pol
, r
->name
, dom
, metadata
);
356 static struct pol_rule
*config_rules
= NULL
;
357 static struct pol_rule
**config_rules_end
= NULL
;
358 static int config_rules_has_path
= 0;
361 * most policy comes from a set policy rules that are
362 * read from the config file.
363 * path_policy() gathers policy information for the
364 * disk described in the given a 'path' and a 'type'.
366 struct dev_policy
*path_policy(char **paths
, char *type
)
368 struct pol_rule
*rules
;
369 struct dev_policy
*pol
= NULL
;
371 rules
= config_rules
;
375 if (rules
->type
== rule_policy
)
376 if (pol_match(rules
->rule
, paths
, type
, NULL
))
377 pol_merge(&pol
, rules
->rule
);
378 if (rules
->type
== rule_part
&& strcmp(type
, type_part
) == 0)
379 if (pol_match(rules
->rule
, paths
, type_disk
, &part
))
380 pol_merge_part(&pol
, rules
->rule
, part
);
390 * drive_test_and_add_policies() - get policies for drive and add them to pols.
392 * @pols: pointer to pointer of first list entry, cannot be NULL, may point to NULL.
393 * @fd: device descriptor.
394 * @verbose: verbose flag.
396 * If supertype doesn't support this functionality return success. Use metadata handler to get
399 mdadm_status_t
drive_test_and_add_policies(struct supertype
*st
, dev_policy_t
**pols
, int fd
,
402 if (!st
->ss
->test_and_add_drive_policies
)
403 return MDADM_STATUS_SUCCESS
;
405 if (st
->ss
->test_and_add_drive_policies(pols
, fd
, verbose
) == MDADM_STATUS_SUCCESS
) {
406 /* After successful call list cannot be empty */
408 return MDADM_STATUS_SUCCESS
;
411 return MDADM_STATUS_ERROR
;
415 * sysfs_test_and_add_policies() - get policies for mddev and add them to pols.
417 * @pols: pointer to pointer of first list entry, cannot be NULL, may point to NULL.
418 * @mdi: mdinfo describes the MD array, must have GET_DISKS option.
419 * @verbose: verbose flag.
421 * If supertype doesn't support this functionality return success. To get policies, all disks
422 * connected to mddev are analyzed.
424 mdadm_status_t
sysfs_test_and_add_drive_policies(struct supertype
*st
, dev_policy_t
**pols
,
425 struct mdinfo
*mdi
, const int verbose
)
429 if (!st
->ss
->test_and_add_drive_policies
)
430 return MDADM_STATUS_SUCCESS
;
432 for (sd
= mdi
->devs
; sd
; sd
= sd
->next
) {
433 char *devpath
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
434 int fd
= dev_open(devpath
, O_RDONLY
);
437 if (!is_fd_valid(fd
)) {
438 pr_err("Cannot open fd for %s\n", devpath
);
439 return MDADM_STATUS_ERROR
;
442 rv
= drive_test_and_add_policies(st
, pols
, fd
, verbose
);
446 return MDADM_STATUS_ERROR
;
449 return MDADM_STATUS_SUCCESS
;
453 * mddev_test_and_add_policies() - get policies for mddev and add them to pols.
455 * @pols: pointer to pointer of first list entry, cannot be NULL, may point to NULL.
456 * @array_fd: MD device descriptor.
457 * @verbose: verbose flag.
459 * If supertype doesn't support this functionality return success. Use fd to extract disks.
461 mdadm_status_t
mddev_test_and_add_drive_policies(struct supertype
*st
, dev_policy_t
**pols
,
462 int array_fd
, const int verbose
)
467 if (!st
->ss
->test_and_add_drive_policies
)
468 return MDADM_STATUS_SUCCESS
;
470 sra
= sysfs_read(array_fd
, NULL
, GET_DEVS
);
472 pr_err("Cannot load sysfs for %s\n", fd2devnm(array_fd
));
473 return MDADM_STATUS_ERROR
;
476 ret
= sysfs_test_and_add_drive_policies(st
, pols
, sra
, verbose
);
482 void pol_add(struct dev_policy
**pol
,
483 char *name
, char *val
,
486 pol_new(pol
, name
, val
, metadata
);
491 static void free_paths(char **paths
)
498 for (i
= 0; paths
[i
]; i
++)
504 * disk_policy() gathers policy information for the
505 * disk described in the given mdinfo (disk.{major,minor}).
507 struct dev_policy
*disk_policy(struct mdinfo
*disk
)
510 char *type
= disk_type(disk
);
511 struct dev_policy
*pol
= NULL
;
513 if (config_rules_has_path
)
514 paths
= disk_paths(disk
);
516 pol
= path_policy(paths
, type
);
522 struct dev_policy
*devid_policy(int dev
)
525 disk
.disk
.major
= major(dev
);
526 disk
.disk
.minor
= minor(dev
);
527 return disk_policy(&disk
);
531 * process policy rules read from config file.
534 char rule_path
[] = "path";
535 char rule_type
[] = "type";
537 char rule_policy
[] = "policy";
538 char rule_part
[] = "part-policy";
540 char pol_metadata
[] = "metadata";
541 char pol_act
[] = "action";
542 char pol_domain
[] = "domain";
543 char pol_auto
[] = "auto";
545 static int try_rule(char *w
, char *name
, struct rule
**rp
)
548 int len
= strlen(name
);
549 if (strncmp(w
, name
, len
) != 0 ||
552 r
= xmalloc(sizeof(*r
));
555 r
->value
= xstrdup(w
+len
+1);
561 void policyline(char *line
, char *type
)
566 if (config_rules_end
== NULL
)
567 config_rules_end
= &config_rules
;
569 pr
= xmalloc(sizeof(*pr
));
572 for (w
= dl_next(line
); w
!= line
; w
= dl_next(w
)) {
573 if (try_rule(w
, rule_path
, &pr
->rule
))
574 config_rules_has_path
= 1;
575 else if (! try_rule(w
, rule_type
, &pr
->rule
) &&
576 ! try_rule(w
, pol_metadata
, &pr
->rule
) &&
577 ! try_rule(w
, pol_act
, &pr
->rule
) &&
578 ! try_rule(w
, pol_domain
, &pr
->rule
) &&
579 ! try_rule(w
, pol_auto
, &pr
->rule
))
580 pr_err("policy rule %s unrecognised and ignored\n",
583 pr
->next
= config_rules
;
587 void policy_add(char *type
, ...)
593 pr
= xmalloc(sizeof(*pr
));
598 while ((name
= va_arg(ap
, char*)) != NULL
) {
601 val
= va_arg(ap
, char*);
602 r
= xmalloc(sizeof(*r
));
605 r
->value
= xstrdup(val
);
609 pr
->next
= config_rules
;
614 void policy_free(void)
616 while (config_rules
) {
617 struct pol_rule
*pr
= config_rules
;
620 config_rules
= config_rules
->next
;
622 for (r
= pr
->rule
; r
; ) {
623 struct rule
*next
= r
->next
;
632 config_rules_end
= NULL
;
633 config_rules_has_path
= 0;
636 void dev_policy_free(struct dev_policy
*p
)
638 struct dev_policy
*t
;
646 static enum policy_action
map_act(const char *act
)
648 if (strcmp(act
, "include") == 0)
650 if (strcmp(act
, "re-add") == 0)
652 if (strcmp(act
, "spare") == 0)
654 if (strcmp(act
, "spare-same-slot") == 0)
655 return act_spare_same_slot
;
656 if (strcmp(act
, "force-spare") == 0)
657 return act_force_spare
;
661 static enum policy_action
policy_action(struct dev_policy
*plist
, const char *metadata
)
663 enum policy_action rv
= act_default
;
664 struct dev_policy
*p
;
666 plist
= pol_find(plist
, pol_act
);
667 pol_for_each(p
, plist
, metadata
) {
668 enum policy_action a
= map_act(p
->value
);
675 int policy_action_allows(struct dev_policy
*plist
, const char *metadata
, enum policy_action want
)
677 enum policy_action act
= policy_action(plist
, metadata
);
681 return (act
>= want
);
684 int disk_action_allows(struct mdinfo
*disk
, const char *metadata
, enum policy_action want
)
686 struct dev_policy
*pol
= disk_policy(disk
);
687 int rv
= policy_action_allows(pol
, metadata
, want
);
689 dev_policy_free(pol
);
694 * Any device can have a list of domains asserted by different policy
696 * An array also has a list of domains comprising all the domains of
697 * all the devices in an array.
698 * Where an array has a spare-group, that becomes an addition domain for
699 * every device in the array and thus for the array.
701 * We keep the list of domains in a sorted linked list
702 * As dev policies are already sorted, this is fairly easy to manage.
705 static struct domainlist
**domain_merge_one(struct domainlist
**domp
,
708 /* merge a domain name into a sorted list and return the
709 * location of the insertion or match
711 struct domainlist
*dom
= *domp
;
713 while (dom
&& strcmp(dom
->dom
, domain
) < 0) {
717 if (dom
== NULL
|| strcmp(dom
->dom
, domain
) != 0) {
718 dom
= xmalloc(sizeof(*dom
));
727 void dump_policy(struct dev_policy
*policy
)
730 dprintf("policy: %p name: %s value: %s metadata: %s\n",
735 policy
= policy
->next
;
740 void domain_merge(struct domainlist
**domp
, struct dev_policy
*pollist
,
741 const char *metadata
)
743 /* Add to 'domp' all the domains in pol that apply to 'metadata'
744 * which are not already in domp
746 struct dev_policy
*pol
;
747 pollist
= pol_find(pollist
, pol_domain
);
748 pol_for_each(pol
, pollist
, metadata
)
749 domain_merge_one(domp
, pol
->value
);
752 int domain_test(struct domainlist
*dom
, struct dev_policy
*pol
,
753 const char *metadata
)
755 /* Check that all domains in pol (for metadata) are also in
756 * dom. Both lists are sorted.
757 * If pol has no domains, we don't really know about this device
758 * so we allow caller to choose:
760 * 0: has domains, not all match
761 * 1: has domains, all match
764 struct dev_policy
*p
;
766 pol
= pol_find(pol
, pol_domain
);
767 pol_for_each(p
, pol
, metadata
) {
769 while (dom
&& strcmp(dom
->dom
, p
->value
) < 0)
771 if (!dom
|| strcmp(dom
->dom
, p
->value
) != 0)
777 void domainlist_add_dev(struct domainlist
**dom
, int devid
, const char *metadata
)
779 struct dev_policy
*pol
= devid_policy(devid
);
780 domain_merge(dom
, pol
, metadata
);
781 dev_policy_free(pol
);
784 struct domainlist
*domain_from_array(struct mdinfo
*mdi
, const char *metadata
)
786 struct domainlist
*domlist
= NULL
;
790 for (mdi
= mdi
->devs
; mdi
; mdi
= mdi
->next
)
791 domainlist_add_dev(&domlist
, makedev(mdi
->disk
.major
,
798 void domain_add(struct domainlist
**domp
, char *domain
)
800 domain_merge_one(domp
, domain
);
803 void domain_free(struct domainlist
*dl
)
806 struct domainlist
*head
= dl
;
814 * Some policy decisions are guided by knowledge of which
815 * array previously owned the device at a given physical location (path).
816 * When removing a device from an array we might record the array against
817 * the path, and when finding a new device, we might look for which
818 * array previously used that path.
820 * The 'array' is described by a map_ent, and the path by a the disk in an
821 * mdinfo, or a string.
824 void policy_save_path(char *id_path
, struct map_ent
*array
)
829 if (mkdir(FAILED_SLOTS_DIR
, S_IRWXU
) < 0 && errno
!= EEXIST
) {
830 pr_err("can't create file to save path to old disk: %s\n", strerror(errno
));
834 snprintf(path
, PATH_MAX
, FAILED_SLOTS_DIR
"/%s", id_path
);
835 f
= fopen(path
, "w");
837 pr_err("can't create file to save path to old disk: %s\n",
842 if (fprintf(f
, "%20s %08x:%08x:%08x:%08x\n",
844 array
->uuid
[0], array
->uuid
[1],
845 array
->uuid
[2], array
->uuid
[3]) <= 0)
846 pr_err("Failed to write to <id_path> cookie\n");
851 int policy_check_path(struct mdinfo
*disk
, struct map_ent
*array
)
855 char **id_paths
= disk_paths(disk
);
859 for (i
= 0; id_paths
[i
]; i
++) {
860 snprintf(path
, PATH_MAX
, FAILED_SLOTS_DIR
"/%s", id_paths
[i
]);
861 f
= fopen(path
, "r");
865 rv
= fscanf(f
, " %20s %x:%x:%x:%x\n",
874 free_paths(id_paths
);
878 /* invocation of udev rule file */
879 char udev_template_start
[] =
880 "# do not edit this file, it is automatically generated by mdadm\n"
883 /* find rule named rule_type and return its value */
884 char *find_rule(struct rule
*rule
, char *rule_type
)
887 if (rule
->name
== rule_type
)
895 #define UDEV_RULE_FORMAT \
896 "ACTION==\"add\", SUBSYSTEM==\"block\", " \
897 "ENV{DEVTYPE}==\"%s\", ENV{ID_PATH}==\"%s\", " \
898 "RUN+=\"" BINDIR "/mdadm --incremental $env{DEVNAME}\"\n"
900 #define UDEV_RULE_FORMAT_NOTYPE \
901 "ACTION==\"add\", SUBSYSTEM==\"block\", " \
902 "ENV{ID_PATH}==\"%s\", " \
903 "RUN+=\"" BINDIR "/mdadm --incremental $env{DEVNAME}\"\n"
905 /* Write rule in the rule file. Use format from UDEV_RULE_FORMAT */
906 int write_rule(struct rule
*rule
, int fd
, int force_part
)
909 char *pth
= find_rule(rule
, rule_path
);
910 char *typ
= find_rule(rule
, rule_type
);
917 snprintf(line
, sizeof(line
) - 1, UDEV_RULE_FORMAT
, typ
, pth
);
919 snprintf(line
, sizeof(line
) - 1, UDEV_RULE_FORMAT_NOTYPE
, pth
);
920 return write(fd
, line
, strlen(line
)) == (int)strlen(line
);
923 /* Generate single entry in udev rule basing on POLICY line found in config
924 * file. Take only those with paths, only first occurrence if paths are equal
925 * and if actions supports handling of spares (>=act_spare_same_slot)
927 int generate_entries(int fd
)
929 struct pol_rule
*loop
, *dup
;
930 char *loop_value
, *dup_value
;
933 for (loop
= config_rules
; loop
; loop
= loop
->next
) {
934 if (loop
->type
!= rule_policy
&& loop
->type
!= rule_part
)
938 /* only policies with paths and with actions supporting
939 * bare disks are considered */
940 loop_value
= find_rule(loop
->rule
, pol_act
);
941 if (!loop_value
|| map_act(loop_value
) < act_spare_same_slot
)
943 loop_value
= find_rule(loop
->rule
, rule_path
);
946 for (dup
= config_rules
; dup
!= loop
; dup
= dup
->next
) {
947 if (dup
->type
!= rule_policy
&& loop
->type
!= rule_part
)
949 dup_value
= find_rule(dup
->rule
, pol_act
);
950 if (!dup_value
|| map_act(dup_value
) < act_spare_same_slot
)
952 dup_value
= find_rule(dup
->rule
, rule_path
);
955 if (strcmp(loop_value
, dup_value
) == 0) {
961 /* not a dup or first occurrence */
963 if (!write_rule(loop
->rule
, fd
, loop
->type
== rule_part
) )
969 /* Write_rules routine creates dynamic udev rules used to handle
970 * hot-plug events for bare devices (and making them spares)
972 int Write_rules(char *rule_name
)
974 int fd
= fileno(stdout
);
977 fd
= creat(rule_name
, 0644);
979 if (!is_fd_valid(fd
))
982 /* write static invocation */
983 if (write(fd
, udev_template_start
, sizeof(udev_template_start
) - 1) !=
984 (int)sizeof(udev_template_start
) - 1)
987 /* iterate, if none created or error occurred, remove file */
988 if (generate_entries(fd
) < 0)