2 * Incremental.c - support --incremental. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Email: <neilb@suse.de>
27 * QVB Post Office, NSW 1230
35 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
36 int mdfd
, char **availp
,
38 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
39 int number
, __u64 events
, int verbose
,
41 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
42 struct map_ent
*target
,
43 struct supertype
*st
, int verbose
);
45 static int Incremental_container(struct supertype
*st
, char *devname
,
47 int verbose
, int runstop
, int autof
,
50 int Incremental(char *devname
, int verbose
, int runstop
,
51 struct supertype
*st
, char *homehost
, int require_homehost
,
52 int autof
, int freeze_reshape
)
54 /* Add this device to an array, creating the array if necessary
55 * and starting the array if sensible or - if runstop>0 - if possible.
57 * This has several steps:
59 * 1/ Check if device is permitted by mdadm.conf, reject if not.
60 * 2/ Find metadata, reject if none appropriate (check
61 * version/name from args)
62 * 3/ Check if there is a match in mdadm.conf
63 * 3a/ if not, check for homehost match. If no match, assemble as
65 * 4/ Determine device number.
66 * - If in mdadm.conf with std name, use that
67 * - UUID in /var/run/mdadm.map use that
68 * - If name is suggestive, use that. unless in use with different uuid.
69 * - Choose a free, high number.
70 * - Use a partitioned device unless strong suggestion not to.
72 * Don't choose partitioned for containers.
73 * 5/ Find out if array already exists
75 * - choose a name, from mdadm.conf or 'name' field in array.
79 * - check one drive in array to make sure metadata is a reasonably
80 * close match. Reject if not (e.g. different type)
82 * 6/ Make sure /var/run/mdadm.map contains this array.
83 * 7/ Is there enough devices to possibly start the array?
84 * For a container, this means running Incremental_container.
85 * 7a/ if not, finish with success.
87 * - read all metadata and arrange devices like -A does
88 * - if number of OK devices match expected, or -R and there are enough,
89 * start the array (auto-readonly).
92 struct mdinfo info
, dinfo
;
93 struct mdinfo
*sra
= NULL
, *d
;
94 struct mddev_ident
*match
;
95 char chosen_name
[1024];
97 struct map_ent
*mp
, *map
= NULL
;
98 int dfd
= -1, mdfd
= -1;
103 mdu_array_info_t ainf
;
104 struct dev_policy
*policy
= NULL
;
105 struct map_ent target_array
;
108 struct createinfo
*ci
= conf_get_create_info();
110 if (stat(devname
, &stb
) < 0) {
112 fprintf(stderr
, Name
": stat failed for %s: %s.\n",
113 devname
, strerror(errno
));
116 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
118 fprintf(stderr
, Name
": %s is not a block device.\n",
122 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
125 fprintf(stderr
, Name
": cannot open %s: %s.\n",
126 devname
, strerror(errno
));
129 /* If the device is a container, we do something very different */
130 if (must_be_container(dfd
)) {
132 st
= super_by_fd(dfd
, NULL
);
133 if (st
&& st
->ss
->load_container
)
134 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
137 if (!rv
&& st
->ss
->container_content
) {
139 fprintf(stderr
, Name
": failed to get "
140 "exclusive lock on mapfile\n");
141 rv
= Incremental_container(st
, devname
, homehost
,
142 verbose
, runstop
, autof
,
148 fprintf(stderr
, Name
": %s is not part of an md array.\n",
153 /* 1/ Check if device is permitted by mdadm.conf */
155 if (!conf_test_dev(devname
)) {
158 ": %s not permitted by mdadm.conf.\n",
163 /* 2/ Find metadata, reject if none appropriate (check
164 * version/name from args) */
166 if (fstat(dfd
, &stb
) < 0) {
168 fprintf(stderr
, Name
": fstat failed for %s: %s.\n",
169 devname
, strerror(errno
));
172 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
174 fprintf(stderr
, Name
": %s is not a block device.\n",
179 dinfo
.disk
.major
= major(stb
.st_rdev
);
180 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
182 policy
= disk_policy(&dinfo
);
183 have_target
= policy_check_path(&dinfo
, &target_array
);
185 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
188 ": no recognisable superblock on %s.\n",
190 rv
= try_spare(devname
, &dfd
, policy
,
191 have_target
? &target_array
: NULL
,
195 if (st
->ss
->compare_super
== NULL
||
196 st
->ss
->load_super(st
, dfd
, NULL
)) {
198 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
200 rv
= try_spare(devname
, &dfd
, policy
,
201 have_target
? &target_array
: NULL
,
206 close (dfd
); dfd
= -1;
208 st
->ss
->getinfo_super(st
, &info
, NULL
);
210 /* 3/ Check if there is a match in mdadm.conf */
211 match
= conf_match(st
, &info
, devname
, verbose
, &rv
);
212 if (!match
&& rv
== 2)
215 if (match
&& match
->devname
216 && strcasecmp(match
->devname
, "<ignore>") == 0) {
218 fprintf(stderr
, Name
": array containing %s is explicitly"
219 " ignored by mdadm.conf\n",
224 /* 3a/ if not, check for homehost match. If no match, continue
225 * but don't trust the 'name' in the array. Thus a 'random' minor
226 * number will be assigned, and the device name will be based
230 else if (st
->ss
->match_home(st
, homehost
) == 1)
232 else if (st
->ss
->match_home(st
, "any") == 1)
233 trustworthy
= LOCAL_ANY
;
235 trustworthy
= FOREIGN
;
238 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
239 (trustworthy
== LOCAL
))) {
242 ": %s has metadata type %s for which "
243 "auto-assembly is disabled\n",
244 devname
, st
->ss
->name
);
247 if (trustworthy
== LOCAL_ANY
)
250 /* There are three possible sources for 'autof': command line,
251 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
252 * ARRAY takes precedence, then command line, then
255 if (match
&& match
->autof
)
256 autof
= match
->autof
;
260 name_to_use
= info
.name
;
261 if (name_to_use
[0] == 0 &&
262 info
.array
.level
== LEVEL_CONTAINER
) {
263 name_to_use
= info
.text_version
;
264 trustworthy
= METADATA
;
266 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
267 ! require_homehost
&&
268 conf_name_is_free(name_to_use
))
271 /* strip "hostname:" prefix from name if we have decided
272 * to treat it as LOCAL
274 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
275 name_to_use
= strchr(name_to_use
, ':')+1;
277 /* 4/ Check if array exists.
280 fprintf(stderr
, Name
": failed to get exclusive lock on "
282 mp
= map_by_uuid(&map
, info
.uuid
);
284 mdfd
= open_dev(mp
->devnum
);
290 /* Couldn't find an existing array, maybe make a new one */
291 mdfd
= create_mddev(match
? match
->devname
: NULL
,
292 name_to_use
, autof
, trustworthy
, chosen_name
);
297 sysfs_init(&info
, mdfd
, 0);
299 if (set_array_info(mdfd
, st
, &info
) != 0) {
300 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
301 chosen_name
, strerror(errno
));
307 dinfo
.disk
.major
= major(stb
.st_rdev
);
308 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
309 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
310 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
311 devname
, chosen_name
, strerror(errno
));
312 ioctl(mdfd
, STOP_ARRAY
, 0);
316 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
317 GET_OFFSET
| GET_SIZE
));
319 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
320 /* It really should be 'none' - must be old buggy
321 * kernel, and mdadm -I may not be able to complete.
324 ioctl(mdfd
, STOP_ARRAY
, NULL
);
326 ": You have an old buggy kernel which cannot support\n"
327 " --incremental reliably. Aborting.\n");
331 info
.array
.working_disks
= 1;
332 /* 6/ Make sure /var/run/mdadm.map contains this array. */
333 map_update(&map
, fd2devnum(mdfd
),
335 info
.uuid
, chosen_name
);
338 /* - check one drive in array to make sure metadata is a reasonably */
339 /* close match. Reject if not (e.g. different type) */
340 /* - add the device */
344 struct supertype
*st2
;
345 struct mdinfo info2
, *d
;
347 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
348 GET_OFFSET
| GET_SIZE
));
351 strcpy(chosen_name
, mp
->path
);
353 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
355 /* It is generally not OK to add non-spare drives to a
356 * running array as they are probably missing because
357 * they failed. However if runstop is 1, then the
358 * array was possibly started early and our best bet is
359 * to add this anyway.
360 * Also if action policy is re-add or better we allow
362 * This doesn't apply to containers as the 'non-spare'
363 * flag has a different meaning. The test has to happen
364 * at the device level there
366 if (!st
->ss
->external
367 && (info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
368 && ! policy_action_allows(policy
, st
->ss
->name
,
371 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
373 ": not adding %s to active array (without --run) %s\n",
374 devname
, chosen_name
);
384 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
385 sra
->devs
->disk
.minor
);
386 dfd2
= dev_open(dn
, O_RDONLY
);
389 ": unable to open %s\n", devname
);
394 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
395 st
->ss
->compare_super(st
, st2
) != 0) {
397 ": metadata mismatch between %s and "
399 devname
, chosen_name
);
405 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
406 st2
->ss
->free_super(st2
);
407 if (info
.array
.level
!= info2
.array
.level
||
408 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
409 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
411 ": unexpected difference between %s and %s.\n",
412 chosen_name
, devname
);
417 info
.disk
.major
= major(stb
.st_rdev
);
418 info
.disk
.minor
= minor(stb
.st_rdev
);
419 /* add disk needs to know about containers */
420 if (st
->ss
->external
)
421 sra
->array
.level
= LEVEL_CONTAINER
;
422 err
= add_disk(mdfd
, st
, sra
, &info
);
423 if (err
< 0 && errno
== EBUSY
) {
424 /* could be another device present with the same
425 * disk.number. Find and reject any such
427 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
428 info
.events
, verbose
, chosen_name
);
429 err
= add_disk(mdfd
, st
, sra
, &info
);
432 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
433 devname
, chosen_name
, strerror(errno
));
437 info
.array
.working_disks
= 0;
438 for (d
= sra
->devs
; d
; d
=d
->next
)
439 info
.array
.working_disks
++;
443 /* 7/ Is there enough devices to possibly start the array? */
444 /* 7a/ if not, finish with success. */
445 if (info
.array
.level
== LEVEL_CONTAINER
) {
446 int devnum
= devnum
; /* defined and used iff ->external */
447 /* Try to assemble within the container */
448 sysfs_uevent(sra
, "change");
451 ": container %s now has %d device%s\n",
452 chosen_name
, info
.array
.working_disks
,
453 info
.array
.working_disks
==1?"":"s");
454 wait_for(chosen_name
, mdfd
);
455 if (st
->ss
->external
)
456 devnum
= fd2devnum(mdfd
);
457 if (st
->ss
->load_container
)
458 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
462 rv
= Incremental_container(st
, chosen_name
, homehost
,
463 verbose
, runstop
, autof
,
467 /* Don't fail the whole -I if a subarray didn't
468 * have enough devices to start yet
471 /* after spare is added, ping monitor for external metadata
472 * so that it can eg. try to rebuild degraded array */
473 if (st
->ss
->external
)
474 ping_monitor_by_id(devnum
);
478 /* We have added something to the array, so need to re-read the
479 * state. Eventually this state should be kept up-to-date as
483 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
484 GET_OFFSET
| GET_SIZE
));
485 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
486 if (enough(info
.array
.level
, info
.array
.raid_disks
,
487 info
.array
.layout
, info
.array
.state
& 1,
491 ": %s attached to %s, not enough to start (%d).\n",
492 devname
, chosen_name
, active_disks
);
498 /* - if number of OK devices match expected, or -R and there */
500 /* + add any bitmap file */
501 /* + start the array (auto-readonly). */
503 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
506 ": %s attached to %s which is already active.\n",
507 devname
, chosen_name
);
513 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
515 /* Let's try to start it */
516 if (match
&& match
->bitmap_file
) {
517 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
520 ": Could not open bitmap file %s.\n",
524 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
527 ": Failed to set bitmapfile for %s.\n",
533 /* Need to remove from the array any devices which
534 * 'count_active' discerned were too old or inappropriate
536 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
537 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
538 remove_disk(mdfd
, st
, sra
, d
);
540 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
541 && trustworthy
!= FOREIGN
)
542 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
544 rv
= sysfs_set_str(sra
, NULL
,
545 "array_state", "read-auto");
549 ": %s attached to %s, which has been started.\n",
550 devname
, chosen_name
);
552 wait_for(chosen_name
, mdfd
);
553 /* We just started the array, so some devices
554 * might have been evicted from the array
555 * because their event counts were too old.
556 * If the action=re-add policy is in-force for
557 * those devices we should re-add them now.
559 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
560 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
561 add_disk(mdfd
, st
, sra
, dsk
) == 0)
563 ": %s re-added to %s\n",
564 dsk
->sys_name
, chosen_name
);
568 ": %s attached to %s, but failed to start: %s.\n",
569 devname
, chosen_name
, strerror(errno
));
575 ": %s attached to %s, not enough to start safely.\n",
576 devname
, chosen_name
);
586 dev_policy_free(policy
);
595 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
596 int number
, __u64 events
, int verbose
,
599 /* Find a device attached to this array with a disk.number of number
600 * and events less than the passed events, and remove the device.
605 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
606 return; /* not safe to remove from active arrays
607 * without thinking more */
609 for (d
= sra
->devs
; d
; d
= d
->next
) {
613 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
614 dfd
= dev_open(dn
, O_RDONLY
);
617 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
621 st
->ss
->getinfo_super(st
, &info
, NULL
);
622 st
->ss
->free_super(st
);
625 if (info
.disk
.number
!= number
||
626 info
.events
>= events
)
629 if (d
->disk
.raid_disk
> -1)
630 sysfs_set_str(sra
, d
, "slot", "none");
631 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
634 ": removing old device %s from %s\n",
635 d
->sys_name
+4, array_name
);
639 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
640 int mdfd
, char **availp
,
641 struct mdinfo
*bestinfo
)
643 /* count how many devices in sra think they are active */
646 __u64 max_events
= 0;
658 for (d
= sra
->devs
; d
; d
= d
->next
)
660 for (d
= sra
->devs
, devnum
=0 ; d
; d
= d
->next
, devnum
++) {
666 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
667 dfd
= dev_open(dn
, O_RDONLY
);
670 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
674 info
.array
.raid_disks
= raid_disks
;
675 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
677 raid_disks
= info
.array
.raid_disks
;
678 avail
= calloc(raid_disks
, 1);
680 fprintf(stderr
, Name
": out of memory.\n");
685 best
= calloc(raid_disks
, sizeof(int));
686 devmap
= calloc(raid_disks
* numdevs
, 1);
688 st
->ss
->getinfo_super(st
, &info
, devmap
);
691 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
695 max_events
= info
.events
;
696 avail
[info
.disk
.raid_disk
] = 2;
697 best
[info
.disk
.raid_disk
] = devnum
;
698 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
699 } else if (info
.events
== max_events
) {
700 avail
[info
.disk
.raid_disk
] = 2;
701 best
[info
.disk
.raid_disk
] = devnum
;
702 } else if (info
.events
== max_events
-1) {
703 if (avail
[info
.disk
.raid_disk
] == 0) {
704 avail
[info
.disk
.raid_disk
] = 1;
705 best
[info
.disk
.raid_disk
] = devnum
;
707 } else if (info
.events
< max_events
- 1)
709 else if (info
.events
== max_events
+1) {
711 max_events
= info
.events
;
712 for (i
=0; i
< raid_disks
; i
++)
715 avail
[info
.disk
.raid_disk
] = 2;
716 best
[info
.disk
.raid_disk
] = devnum
;
717 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
718 } else { /* info.events much bigger */
719 memset(avail
, 0, raid_disks
);
720 max_events
= info
.events
;
721 avail
[info
.disk
.raid_disk
] = 2;
722 best
[info
.disk
.raid_disk
] = devnum
;
723 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
726 st
->ss
->free_super(st
);
730 /* We need to reject any device that thinks the best device is
731 * failed or missing */
732 for (b
= 0; b
< raid_disks
; b
++)
736 for (i
= 0 ; i
< raid_disks
; i
++) {
737 if (i
!= b
&& avail
[i
])
738 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
739 /* This device thinks 'b' is failed -
742 for (d
=sra
->devs
; devnum
; d
= d
->next
)
744 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
755 /* test if container has degraded member(s) */
756 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
758 mdu_array_info_t array
;
760 int max_degraded
= 0;
762 for(; map
; map
= map
->next
) {
763 if (!is_subarray(map
->metadata
) ||
764 devname2devnum(map
->metadata
+1) != me
->devnum
)
766 afd
= open_dev(map
->devnum
);
769 /* most accurate information regarding array degradation */
770 if (ioctl(afd
, GET_ARRAY_INFO
, &array
) >= 0) {
771 int degraded
= array
.raid_disks
- array
.active_disks
-
773 if (degraded
> max_degraded
)
774 max_degraded
= degraded
;
778 return (max_degraded
);
781 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
782 struct map_ent
*target
, int bare
,
783 struct supertype
*st
, int verbose
)
785 /* This device doesn't have any md metadata
786 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
787 * If 'st' is not set, then we only know that some metadata allows this,
788 * others possibly don't.
789 * So look for a container or array to attach the device to.
790 * Prefer 'target' if that is set and the array is found.
792 * If st is set, then only arrays of that type are considered
793 * Return 0 on success, or some exit code on failure, probably 1.
797 struct map_ent
*mp
, *map
= NULL
;
798 struct mdinfo
*chosen
= NULL
;
801 if (fstat(dfd
, &stb
) != 0)
805 * Now we need to find a suitable array to add this to.
806 * We only accept arrays that:
808 * - are in the same domains as the device
809 * - are of an size for which the device will be useful
810 * and we choose the one that is the most degraded
813 if (map_lock(&map
)) {
814 fprintf(stderr
, Name
": failed to get exclusive lock on "
818 for (mp
= map
; mp
; mp
= mp
->next
) {
819 struct supertype
*st2
;
820 struct domainlist
*dl
= NULL
;
822 unsigned long long devsize
;
823 unsigned long long component_size
= 0;
825 if (is_subarray(mp
->metadata
))
828 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
830 (st
->minor_version
>= 0 &&
831 st
->minor_version
!= st2
->minor_version
)) {
833 fprintf(stderr
, Name
": not adding %s to %s as metadata type doesn't match\n",
840 sra
= sysfs_read(-1, mp
->devnum
,
841 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
842 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
844 /* Probably a container - no degraded info */
845 sra
= sysfs_read(-1, mp
->devnum
,
846 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
847 GET_COMPONENT
|GET_VERSION
);
849 sra
->array
.failed_disks
= -1;
856 for(i
=0; !st2
&& superlist
[i
]; i
++)
857 st2
= superlist
[i
]->match_metadata_desc(
861 fprintf(stderr
, Name
": not adding %s to %s"
862 " as metadata not recognised.\n",
866 /* Need to double check the 'act_spare' permissions applies
869 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
871 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
872 act_spare_same_slot
))
876 /* update number of failed disks for mostly degraded
877 * container member */
878 if (sra
->array
.failed_disks
== -1)
879 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
881 get_dev_size(dfd
, NULL
, &devsize
);
882 if (sra
->component_size
== 0) {
883 /* true for containers, here we must read superblock
884 * to obtain minimum spare size */
885 struct supertype
*st3
= dup_super(st2
);
886 int mdfd
= open_dev(mp
->devnum
);
891 if (st3
->ss
->load_container
&&
892 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
893 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
894 st3
->ss
->free_super(st3
);
899 if ((sra
->component_size
> 0 &&
900 st2
->ss
->avail_size(st2
, devsize
) < sra
->component_size
)
902 (sra
->component_size
== 0 && devsize
< component_size
)) {
904 fprintf(stderr
, Name
": not adding %s to %s as it is too small\n",
908 /* test against target.
909 * If 'target' is set and 'bare' is false, we only accept
910 * arrays/containers that match 'target'.
911 * If 'target' is set and 'bare' is true, we prefer the
912 * array which matches 'target'.
913 * target is considered only if we deal with degraded array
915 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
916 act_spare_same_slot
)) {
917 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
918 memcmp(target
->uuid
, mp
->uuid
,
919 sizeof(target
->uuid
)) == 0 &&
920 sra
->array
.failed_disks
> 0) {
921 /* This is our target!! */
926 /* skip to end so we don't check any more */
936 dl
= domain_from_array(sra
, st2
->ss
->name
);
937 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
938 /* domain test fails */
940 fprintf(stderr
, Name
": not adding %s to %s as"
941 " it is not in a compatible domain\n",
946 /* all tests passed, OK to add to this array */
950 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
964 /* add current device to chosen array as a spare */
965 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
967 struct mddev_dev devlist
;
972 devlist
.writemostly
= 0;
973 devlist
.devname
= devname
;
974 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
976 devlist
.disposition
= 'a';
979 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
985 fprintf(stderr
, Name
": added %s as spare for %s\n",
986 devname
, chosen
->sys_name
);
988 fprintf(stderr
, Name
": failed to add %s as spare for %s\n",
989 devname
, chosen
->sys_name
);
997 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
998 struct supertype
*st
, int verbose
)
1000 /* we know that at least one partition virtual-metadata is
1001 * allowed to incorporate spares like this device. We need to
1002 * find a suitable device to copy partition information from.
1004 * Getting a list of all disk (not partition) devices is
1005 * slightly non-trivial. We could look at /sys/block, but
1006 * that is theoretically due to be removed. Maybe best to use
1007 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1008 * we depend on this directory of 'path' info. But that fails
1009 * to find loop devices and probably others. Maybe don't
1010 * worry about that, they aren't the real target.
1012 * So: check things in /dev/disk/by-path to see if they are in
1013 * a compatible domain, then load the partition table and see
1014 * if it is OK for the new device, and choose the largest
1015 * partition table that fits.
1019 char *chosen
= NULL
;
1020 unsigned long long chosen_size
= 0;
1021 struct supertype
*chosen_st
= NULL
;
1024 dir
= opendir("/dev/disk/by-path");
1027 while ((de
= readdir(dir
)) != NULL
) {
1029 struct dev_policy
*pol2
= NULL
;
1030 struct domainlist
*domlist
= NULL
;
1033 struct supertype
*st2
= NULL
;
1034 char *devname
= NULL
;
1035 unsigned long long devsectors
;
1037 if (de
->d_ino
== 0 ||
1038 de
->d_name
[0] == '.' ||
1039 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1042 ep
= de
->d_name
+ strlen(de
->d_name
);
1043 while (ep
> de
->d_name
&&
1046 if (ep
> de
->d_name
+ 5 &&
1047 strncmp(ep
-5, "-part", 5) == 0)
1048 /* This is a partition - skip it */
1051 pol2
= path_policy(de
->d_name
, type_disk
);
1053 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1054 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1055 /* new device is incompatible with this device. */
1058 domain_free(domlist
);
1061 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1065 fd
= open(devname
, O_RDONLY
);
1068 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1073 st2
= dup_super(st
);
1075 st2
= guess_super_type(fd
, guess_partitions
);
1077 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1081 /* Check domain policy again, this time referring to metadata */
1082 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1083 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1084 /* Incompatible devices for this metadata type */
1086 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1087 /* Some partition types allow sparing, but not
1093 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1094 if (info
.component_size
> devsectors
)
1095 /* This partitioning doesn't fit in the device */
1098 /* This is an acceptable device to copy partition
1099 * metadata from. We could just stop here, but I
1100 * think I want to keep looking incase a larger
1101 * metadata which makes better use of the device can
1104 if (chosen
== NULL
||
1105 chosen_size
< info
.component_size
) {
1106 chosen_size
= info
.component_size
;
1111 chosen_st
->ss
->free_super(chosen_st
);
1120 domain_free(domlist
);
1121 dev_policy_free(pol2
);
1123 st2
->ss
->free_super(st2
);
1135 /* 'chosen' is the best device we can find. Let's write its
1136 * metadata to devname dfd is read-only so don't use that
1138 fd
= open(devname
, O_RDWR
);
1140 chosen_st
->ss
->store_super(chosen_st
, fd
);
1144 chosen_st
->ss
->free_super(chosen_st
);
1149 static int is_bare(int dfd
)
1151 unsigned long long size
= 0;
1152 char bufpad
[4096 + 4096];
1153 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1155 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1156 read(dfd
, buf
, 4096) != 4096)
1159 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1161 if (memcmp(buf
, buf
+1, 4095) != 0)
1164 /* OK, first 4K appear blank, try the end. */
1165 get_dev_size(dfd
, NULL
, &size
);
1166 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1167 read(dfd
, buf
, 4096) != 4096)
1170 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1172 if (memcmp(buf
, buf
+1, 4095) != 0)
1178 /* adding a spare to a regular array is quite different from adding one to
1179 * a set-of-partitions virtual array.
1180 * This function determines which is worth trying and tries as appropriate.
1181 * Arrays are given priority over partitions.
1183 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1184 struct map_ent
*target
,
1185 struct supertype
*st
, int verbose
)
1190 int partitions_ok
= 0;
1194 /* Can only add a spare if device has at least one domain */
1195 if (pol_find(pol
, pol_domain
) == NULL
)
1197 /* And only if some action allows spares */
1198 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1201 /* Now check if the device is bare.
1202 * bare devices can always be added as a spare
1203 * non-bare devices can only be added if spare-same-slot is permitted,
1204 * and this device is replacing a previous device - in which case 'target'
1207 if (!is_bare(dfd
)) {
1208 /* Must have a target and allow same_slot */
1209 /* Later - may allow force_spare without target */
1211 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1212 act_spare_same_slot
)) {
1214 fprintf(stderr
, Name
": %s is not bare, so not "
1215 "considering as a spare\n",
1223 /* It might be OK to add this device to an array - need to see
1224 * what arrays might be candidates.
1227 /* just try try 'array' or 'partition' based on this metadata */
1228 if (st
->ss
->add_to_super
)
1229 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1232 return partition_try_spare(devname
, dfdp
, pol
,
1235 /* No metadata was specified or found so options are open.
1236 * Check for whether any array metadata, or any partition metadata
1237 * might allow adding the spare. This check is just help to avoid
1238 * a more costly scan of all arrays when we can be sure that will
1241 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1242 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1243 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1245 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1246 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1251 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1253 if (rv
!= 0 && partitions_ok
)
1254 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1258 int IncrementalScan(int verbose
)
1260 /* look at every device listed in the 'map' file.
1261 * If one is found that is not running then:
1262 * look in mdadm.conf for bitmap file.
1263 * if one exists, but array has none, add it.
1264 * try to start array in auto-readonly mode
1266 struct map_ent
*mapl
= NULL
;
1268 struct mddev_ident
*devs
, *mddev
;
1272 devs
= conf_get_ident(NULL
);
1274 for (me
= mapl
; me
; me
= me
->next
) {
1275 mdu_array_info_t array
;
1276 mdu_bitmap_file_t bmf
;
1278 int mdfd
= open_dev(me
->devnum
);
1282 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1287 /* Ok, we can try this one. Maybe it needs a bitmap */
1288 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1289 if (mddev
->devname
&& me
->path
1290 && devname_matches(mddev
->devname
, me
->path
))
1292 if (mddev
&& mddev
->bitmap_file
) {
1294 * Note: early kernels will wrongly fail this, so it
1298 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1299 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1301 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1308 fprintf(stderr
, Name
1309 ": Added bitmap %s to %s\n",
1310 mddev
->bitmap_file
, me
->path
);
1311 else if (errno
!= EEXIST
)
1312 fprintf(stderr
, Name
1313 ": Failed to add bitmap to %s: %s\n",
1314 me
->path
, strerror(errno
));
1317 sra
= sysfs_read(mdfd
, 0, 0);
1319 if (sysfs_set_str(sra
, NULL
,
1320 "array_state", "read-auto") == 0) {
1322 fprintf(stderr
, Name
1323 ": started array %s\n",
1324 me
->path
?: devnum2devname(me
->devnum
));
1326 fprintf(stderr
, Name
1327 ": failed to start array %s: %s\n",
1328 me
->path
?: devnum2devname(me
->devnum
),
1338 static char *container2devname(char *devname
)
1340 char *mdname
= NULL
;
1342 if (devname
[0] == '/') {
1343 int fd
= open(devname
, O_RDONLY
);
1345 mdname
= devnum2devname(fd2devnum(fd
));
1350 struct map_ent
*mp
, *map
= NULL
;
1352 if (!parse_uuid(devname
, uuid
))
1354 mp
= map_by_uuid(&map
, uuid
);
1356 mdname
= devnum2devname(mp
->devnum
);
1363 static int Incremental_container(struct supertype
*st
, char *devname
,
1364 char *homehost
, int verbose
,
1365 int runstop
, int autof
, int freeze_reshape
)
1367 /* Collect the contents of this container and for each
1368 * array, choose a device name and assemble the array.
1371 struct mdinfo
*list
;
1373 struct map_ent
*map
= NULL
;
1376 struct mddev_ident
*match
;
1378 struct domainlist
*domains
;
1379 struct map_ent
*smp
;
1385 st
->ss
->getinfo_super(st
, &info
, NULL
);
1387 if ((runstop
> 0 && info
.container_enough
>= 0) ||
1388 info
.container_enough
> 0)
1392 fprintf(stderr
, Name
": not enough devices to start the container\n");
1396 match
= conf_match(st
, &info
, devname
, verbose
, &rv
);
1397 if (match
== NULL
&& rv
== 2)
1400 /* Need to compute 'trustworthy' */
1402 trustworthy
= LOCAL
;
1403 else if (st
->ss
->match_home(st
, homehost
) == 1)
1404 trustworthy
= LOCAL
;
1405 else if (st
->ss
->match_home(st
, "any") == 1)
1406 trustworthy
= LOCAL
;
1408 trustworthy
= FOREIGN
;
1410 list
= st
->ss
->container_content(st
, NULL
);
1411 /* when nothing to activate - quit */
1414 for (ra
= list
; ra
; ra
= ra
->next
) {
1416 char chosen_name
[1024];
1418 struct mddev_ident
*match
= NULL
;
1421 /* do not activate arrays blocked by metadata handler */
1422 if (ra
->array
.state
& (1 << MD_SB_BLOCK_VOLUME
)) {
1423 fprintf(stderr
, Name
": Cannot activate array %s in %s.\n",
1424 ra
->text_version
, devname
);
1428 mp
= map_by_uuid(&map
, ra
->uuid
);
1431 mdfd
= open_dev(mp
->devnum
);
1433 strcpy(chosen_name
, mp
->path
);
1435 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1438 /* Check in mdadm.conf for container == devname and
1439 * member == ra->text_version after second slash.
1441 char *sub
= strchr(ra
->text_version
+1, '/');
1442 struct mddev_ident
*array_list
;
1445 array_list
= conf_get_ident(NULL
);
1448 for(; array_list
; array_list
= array_list
->next
) {
1450 if (array_list
->member
== NULL
||
1451 array_list
->container
== NULL
)
1453 if (strcmp(array_list
->member
, sub
) != 0)
1455 if (array_list
->uuid_set
&&
1456 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1458 dn
= container2devname(array_list
->container
);
1461 if (strncmp(dn
, ra
->text_version
+1,
1463 ra
->text_version
[strlen(dn
)+1] != '/') {
1468 /* we have a match */
1471 fprintf(stderr
, Name
": match found for member %s\n",
1472 array_list
->member
);
1476 if (match
&& match
->devname
&&
1477 strcasecmp(match
->devname
, "<ignore>") == 0) {
1479 fprintf(stderr
, Name
": array %s/%s is "
1480 "explicitly ignored by mdadm.conf\n",
1481 match
->container
, match
->member
);
1485 trustworthy
= LOCAL
;
1487 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1495 fprintf(stderr
, Name
": failed to open %s: %s.\n",
1496 chosen_name
, strerror(errno
));
1500 assemble_container_content(st
, mdfd
, ra
, runstop
,
1501 chosen_name
, verbose
, NULL
,
1506 /* don't move spares to container with volume being activated
1507 when all volumes are blocked */
1508 if (ra_all
== ra_blocked
)
1511 /* Now move all suitable spares from spare container */
1512 domains
= domain_from_array(list
, st
->ss
->name
);
1513 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1515 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1516 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1517 /* spare container found */
1518 struct supertype
*sst
=
1519 super_imsm
.match_metadata_desc("imsm");
1520 struct mdinfo
*sinfo
;
1521 unsigned long long min_size
= 0;
1522 if (st
->ss
->min_acceptable_spare_size
)
1523 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1524 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1526 sinfo
= container_choose_spares(sst
, min_size
,
1529 sst
->ss
->free_super(sst
);
1532 struct mdinfo
*disks
= sinfo
->devs
;
1534 /* move spare from spare
1535 * container to currently
1541 makedev(disks
->disk
.major
,
1542 disks
->disk
.minor
)))
1544 disks
= disks
->next
;
1547 fprintf(stderr
, Name
1548 ": Added %d spare%s to %s\n",
1549 count
, count
>1?"s":"", devname
);
1555 domain_free(domains
);
1560 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1561 * raid arrays, and if so first fail (if needed) and then remove the device.
1563 * @devname - The device we want to remove
1564 * @id_path - name as found in /dev/disk/by-path for this device
1566 * Note: the device name must be a kernel name like "sda", so
1567 * that we can find it in /proc/mdstat
1569 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1573 struct mdstat_ent
*ent
;
1574 struct mddev_dev devlist
;
1577 dprintf(Name
": incremental removal without --path <id_path> "
1578 "lacks the possibility to re-add new device in this "
1581 if (strchr(devname
, '/')) {
1582 fprintf(stderr
, Name
": incremental removal requires a "
1583 "kernel device name, not a file: %s\n", devname
);
1586 ent
= mdstat_by_component(devname
);
1588 fprintf(stderr
, Name
": %s does not appear to be a component "
1589 "of any array\n", devname
);
1592 mdfd
= open_dev(ent
->devnum
);
1594 fprintf(stderr
, Name
": Cannot open array %s!!\n", ent
->dev
);
1600 struct map_ent
*map
= NULL
, *me
;
1601 me
= map_by_devnum(&map
, ent
->devnum
);
1603 policy_save_path(id_path
, me
);
1607 memset(&devlist
, 0, sizeof(devlist
));
1608 devlist
.devname
= devname
;
1609 devlist
.disposition
= 'f';
1610 /* for a container, we must fail each member array */
1611 if (ent
->metadata_version
&&
1612 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1613 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1614 struct mdstat_ent
*memb
;
1615 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1616 if (is_container_member(memb
, ent
->dev
)) {
1617 int subfd
= open_dev(memb
->devnum
);
1619 Manage_subdevs(memb
->dev
, subfd
,
1620 &devlist
, verbose
, 0,
1625 free_mdstat(mdstat
);
1627 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);
1628 devlist
.disposition
= 'r';
1629 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);