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 trustworthy
== LOCAL
) {
264 name_to_use
= info
.text_version
;
265 trustworthy
= METADATA
;
267 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
268 ! require_homehost
&&
269 conf_name_is_free(name_to_use
))
272 /* strip "hostname:" prefix from name if we have decided
273 * to treat it as LOCAL
275 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
276 name_to_use
= strchr(name_to_use
, ':')+1;
278 /* 4/ Check if array exists.
281 fprintf(stderr
, Name
": failed to get exclusive lock on "
283 mp
= map_by_uuid(&map
, info
.uuid
);
285 mdfd
= open_dev(mp
->devnum
);
291 /* Couldn't find an existing array, maybe make a new one */
292 mdfd
= create_mddev(match
? match
->devname
: NULL
,
293 name_to_use
, autof
, trustworthy
, chosen_name
);
298 sysfs_init(&info
, mdfd
, 0);
300 if (set_array_info(mdfd
, st
, &info
) != 0) {
301 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
302 chosen_name
, strerror(errno
));
308 dinfo
.disk
.major
= major(stb
.st_rdev
);
309 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
310 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
311 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
312 devname
, chosen_name
, strerror(errno
));
313 ioctl(mdfd
, STOP_ARRAY
, 0);
317 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
318 GET_OFFSET
| GET_SIZE
));
320 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
321 /* It really should be 'none' - must be old buggy
322 * kernel, and mdadm -I may not be able to complete.
325 ioctl(mdfd
, STOP_ARRAY
, NULL
);
327 ": You have an old buggy kernel which cannot support\n"
328 " --incremental reliably. Aborting.\n");
333 info
.array
.working_disks
= 1;
334 /* 6/ Make sure /var/run/mdadm.map contains this array. */
335 map_update(&map
, fd2devnum(mdfd
),
337 info
.uuid
, chosen_name
);
340 /* - check one drive in array to make sure metadata is a reasonably */
341 /* close match. Reject if not (e.g. different type) */
342 /* - add the device */
346 struct supertype
*st2
;
347 struct mdinfo info2
, *d
;
349 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
350 GET_OFFSET
| GET_SIZE
));
353 strcpy(chosen_name
, mp
->path
);
355 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
357 /* It is generally not OK to add non-spare drives to a
358 * running array as they are probably missing because
359 * they failed. However if runstop is 1, then the
360 * array was possibly started early and our best bet is
361 * to add this anyway.
362 * Also if action policy is re-add or better we allow
364 * This doesn't apply to containers as the 'non-spare'
365 * flag has a different meaning. The test has to happen
366 * at the device level there
368 if (!st
->ss
->external
369 && (info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
370 && ! policy_action_allows(policy
, st
->ss
->name
,
373 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
375 ": not adding %s to active array (without --run) %s\n",
376 devname
, chosen_name
);
386 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
387 sra
->devs
->disk
.minor
);
388 dfd2
= dev_open(dn
, O_RDONLY
);
390 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
391 st
->ss
->compare_super(st
, st2
) != 0) {
393 ": metadata mismatch between %s and "
395 devname
, chosen_name
);
401 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
402 st2
->ss
->free_super(st2
);
403 if (info
.array
.level
!= info2
.array
.level
||
404 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
405 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
407 ": unexpected difference between %s and %s.\n",
408 chosen_name
, devname
);
413 info2
.disk
.major
= major(stb
.st_rdev
);
414 info2
.disk
.minor
= minor(stb
.st_rdev
);
415 /* add disk needs to know about containers */
416 if (st
->ss
->external
)
417 sra
->array
.level
= LEVEL_CONTAINER
;
418 err
= add_disk(mdfd
, st
, sra
, &info2
);
419 if (err
< 0 && errno
== EBUSY
) {
420 /* could be another device present with the same
421 * disk.number. Find and reject any such
423 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
424 info
.events
, verbose
, chosen_name
);
425 err
= add_disk(mdfd
, st
, sra
, &info2
);
428 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
429 devname
, chosen_name
, strerror(errno
));
433 info
.array
.working_disks
= 0;
434 for (d
= sra
->devs
; d
; d
=d
->next
)
435 info
.array
.working_disks
++;
439 /* 7/ Is there enough devices to possibly start the array? */
440 /* 7a/ if not, finish with success. */
441 if (info
.array
.level
== LEVEL_CONTAINER
) {
442 int devnum
= devnum
; /* defined and used iff ->external */
443 /* Try to assemble within the container */
446 ": container %s now has %d devices\n",
447 chosen_name
, info
.array
.working_disks
);
448 wait_for(chosen_name
, mdfd
);
449 if (st
->ss
->external
)
450 devnum
= fd2devnum(mdfd
);
451 if (st
->ss
->load_container
)
452 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
456 rv
= Incremental_container(st
, chosen_name
, homehost
,
457 verbose
, runstop
, autof
,
461 /* Don't fail the whole -I if a subarray didn't
462 * have enough devices to start yet
465 /* after spare is added, ping monitor for external metadata
466 * so that it can eg. try to rebuild degraded array */
467 if (st
->ss
->external
)
468 ping_monitor_by_id(devnum
);
472 /* We have added something to the array, so need to re-read the
473 * state. Eventually this state should be kept up-to-date as
477 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
478 GET_OFFSET
| GET_SIZE
));
479 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
480 if (enough(info
.array
.level
, info
.array
.raid_disks
,
481 info
.array
.layout
, info
.array
.state
& 1,
482 avail
, active_disks
) == 0) {
485 ": %s attached to %s, not enough to start (%d).\n",
486 devname
, chosen_name
, active_disks
);
492 /* - if number of OK devices match expected, or -R and there */
494 /* + add any bitmap file */
495 /* + start the array (auto-readonly). */
497 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
500 ": %s attached to %s which is already active.\n",
501 devname
, chosen_name
);
507 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
509 /* Let's try to start it */
510 if (match
&& match
->bitmap_file
) {
511 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
514 ": Could not open bitmap file %s.\n",
518 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
521 ": Failed to set bitmapfile for %s.\n",
527 /* Need to remove from the array any devices which
528 * 'count_active' discerned were too old or inappropriate
530 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
531 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
532 remove_disk(mdfd
, st
, sra
, d
);
534 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
535 && trustworthy
!= FOREIGN
)
536 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
538 rv
= sysfs_set_str(sra
, NULL
,
539 "array_state", "read-auto");
543 ": %s attached to %s, which has been started.\n",
544 devname
, chosen_name
);
546 wait_for(chosen_name
, mdfd
);
547 /* We just started the array, so some devices
548 * might have been evicted from the array
549 * because their event counts were too old.
550 * If the action=re-add policy is in-force for
551 * those devices we should re-add them now.
553 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
554 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
555 add_disk(mdfd
, st
, sra
, dsk
) == 0)
557 ": %s re-added to %s\n",
558 dsk
->sys_name
, chosen_name
);
562 ": %s attached to %s, but failed to start: %s.\n",
563 devname
, chosen_name
, strerror(errno
));
569 ": %s attached to %s, not enough to start safely.\n",
570 devname
, chosen_name
);
580 dev_policy_free(policy
);
589 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
590 int number
, __u64 events
, int verbose
,
593 /* Find a device attached to this array with a disk.number of number
594 * and events less than the passed events, and remove the device.
599 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
600 return; /* not safe to remove from active arrays
601 * without thinking more */
603 for (d
= sra
->devs
; d
; d
= d
->next
) {
607 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
608 dfd
= dev_open(dn
, O_RDONLY
);
611 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
615 st
->ss
->getinfo_super(st
, &info
, NULL
);
616 st
->ss
->free_super(st
);
619 if (info
.disk
.number
!= number
||
620 info
.events
>= events
)
623 if (d
->disk
.raid_disk
> -1)
624 sysfs_set_str(sra
, d
, "slot", "none");
625 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
628 ": removing old device %s from %s\n",
629 d
->sys_name
+4, array_name
);
633 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
634 int mdfd
, char **availp
,
635 struct mdinfo
*bestinfo
)
637 /* count how many devices in sra think they are active */
640 __u64 max_events
= 0;
652 for (d
= sra
->devs
; d
; d
= d
->next
)
654 for (d
= sra
->devs
, devnum
=0 ; d
; d
= d
->next
, devnum
++) {
660 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
661 dfd
= dev_open(dn
, O_RDONLY
);
664 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
668 info
.array
.raid_disks
= raid_disks
;
669 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
671 raid_disks
= info
.array
.raid_disks
;
672 avail
= calloc(raid_disks
, 1);
674 fprintf(stderr
, Name
": out of memory.\n");
679 best
= calloc(raid_disks
, sizeof(int));
680 devmap
= calloc(raid_disks
* numdevs
, 1);
682 st
->ss
->getinfo_super(st
, &info
, devmap
);
685 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
689 max_events
= info
.events
;
690 avail
[info
.disk
.raid_disk
] = 2;
691 best
[info
.disk
.raid_disk
] = devnum
;
692 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
693 } else if (info
.events
== max_events
) {
694 avail
[info
.disk
.raid_disk
] = 2;
695 best
[info
.disk
.raid_disk
] = devnum
;
696 } else if (info
.events
== max_events
-1) {
697 if (avail
[info
.disk
.raid_disk
] == 0) {
698 avail
[info
.disk
.raid_disk
] = 1;
699 best
[info
.disk
.raid_disk
] = devnum
;
701 } else if (info
.events
< max_events
- 1)
703 else if (info
.events
== max_events
+1) {
705 max_events
= info
.events
;
706 for (i
=0; i
< raid_disks
; i
++)
709 avail
[info
.disk
.raid_disk
] = 2;
710 best
[info
.disk
.raid_disk
] = devnum
;
711 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
712 } else { /* info.events much bigger */
713 memset(avail
, 0, raid_disks
);
714 max_events
= info
.events
;
715 avail
[info
.disk
.raid_disk
] = 2;
716 best
[info
.disk
.raid_disk
] = devnum
;
717 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
720 st
->ss
->free_super(st
);
724 /* We need to reject any device that thinks the best device is
725 * failed or missing */
726 for (b
= 0; b
< raid_disks
; b
++)
730 for (i
= 0 ; i
< raid_disks
; i
++) {
731 if (i
!= b
&& avail
[i
])
732 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
733 /* This device thinks 'b' is failed -
736 for (d
=sra
->devs
; devnum
; d
= d
->next
)
738 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
749 /* test if container has degraded member(s) */
750 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
752 mdu_array_info_t array
;
754 int max_degraded
= 0;
756 for(; map
; map
= map
->next
) {
757 if (!is_subarray(map
->metadata
) ||
758 devname2devnum(map
->metadata
+1) != me
->devnum
)
760 afd
= open_dev(map
->devnum
);
763 /* most accurate information regarding array degradation */
764 if (ioctl(afd
, GET_ARRAY_INFO
, &array
) >= 0) {
765 int degraded
= array
.raid_disks
- array
.active_disks
-
767 if (degraded
> max_degraded
)
768 max_degraded
= degraded
;
772 return (max_degraded
);
775 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
776 struct map_ent
*target
, int bare
,
777 struct supertype
*st
, int verbose
)
779 /* This device doesn't have any md metadata
780 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
781 * If 'st' is not set, then we only know that some metadata allows this,
782 * others possibly don't.
783 * So look for a container or array to attach the device to.
784 * Prefer 'target' if that is set and the array is found.
786 * If st is set, then only arrays of that type are considered
787 * Return 0 on success, or some exit code on failure, probably 1.
791 struct map_ent
*mp
, *map
= NULL
;
792 struct mdinfo
*chosen
= NULL
;
795 if (fstat(dfd
, &stb
) != 0)
799 * Now we need to find a suitable array to add this to.
800 * We only accept arrays that:
802 * - are in the same domains as the device
803 * - are of an size for which the device will be useful
804 * and we choose the one that is the most degraded
807 if (map_lock(&map
)) {
808 fprintf(stderr
, Name
": failed to get exclusive lock on "
812 for (mp
= map
; mp
; mp
= mp
->next
) {
813 struct supertype
*st2
;
814 struct domainlist
*dl
= NULL
;
816 unsigned long long devsize
;
817 unsigned long long component_size
= 0;
819 if (is_subarray(mp
->metadata
))
822 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
824 (st
->minor_version
>= 0 &&
825 st
->minor_version
!= st2
->minor_version
)) {
827 fprintf(stderr
, Name
": not adding %s to %s as metadata type doesn't match\n",
834 sra
= sysfs_read(-1, mp
->devnum
,
835 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
836 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
838 /* Probably a container - no degraded info */
839 sra
= sysfs_read(-1, mp
->devnum
,
840 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
841 GET_COMPONENT
|GET_VERSION
);
843 sra
->array
.failed_disks
= -1;
850 for(i
=0; !st2
&& superlist
[i
]; i
++)
851 st2
= superlist
[i
]->match_metadata_desc(
855 fprintf(stderr
, Name
": not adding %s to %s"
856 " as metadata not recognised.\n",
860 /* Need to double check the 'act_spare' permissions applies
863 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
865 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
866 act_spare_same_slot
))
870 /* update number of failed disks for mostly degraded
871 * container member */
872 if (sra
->array
.failed_disks
== -1)
873 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
875 get_dev_size(dfd
, NULL
, &devsize
);
876 if (sra
->component_size
== 0) {
877 /* true for containers, here we must read superblock
878 * to obtain minimum spare size */
879 struct supertype
*st3
= dup_super(st2
);
880 int mdfd
= open_dev(mp
->devnum
);
885 if (st3
->ss
->load_container
&&
886 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
887 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
888 st3
->ss
->free_super(st3
);
893 if ((sra
->component_size
> 0 &&
894 st2
->ss
->avail_size(st2
, devsize
) < sra
->component_size
)
896 (sra
->component_size
== 0 && devsize
< component_size
)) {
898 fprintf(stderr
, Name
": not adding %s to %s as it is too small\n",
902 /* test against target.
903 * If 'target' is set and 'bare' is false, we only accept
904 * arrays/containers that match 'target'.
905 * If 'target' is set and 'bare' is true, we prefer the
906 * array which matches 'target'.
907 * target is considered only if we deal with degraded array
909 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
910 act_spare_same_slot
)) {
911 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
912 memcmp(target
->uuid
, mp
->uuid
,
913 sizeof(target
->uuid
)) == 0 &&
914 sra
->array
.failed_disks
> 0) {
915 /* This is our target!! */
920 /* skip to end so we don't check any more */
930 dl
= domain_from_array(sra
, st2
->ss
->name
);
931 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
932 /* domain test fails */
934 fprintf(stderr
, Name
": not adding %s to %s as"
935 " it is not in a compatible domain\n",
940 /* all tests passed, OK to add to this array */
944 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
958 /* add current device to chosen array as a spare */
959 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
961 struct mddev_dev devlist
;
966 devlist
.writemostly
= 0;
967 devlist
.devname
= devname
;
968 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
970 devlist
.disposition
= 'a';
973 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
979 fprintf(stderr
, Name
": added %s as spare for %s\n",
980 devname
, chosen
->sys_name
);
982 fprintf(stderr
, Name
": failed to add %s as spare for %s\n",
983 devname
, chosen
->sys_name
);
991 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
992 struct supertype
*st
, int verbose
)
994 /* we know that at least one partition virtual-metadata is
995 * allowed to incorporate spares like this device. We need to
996 * find a suitable device to copy partition information from.
998 * Getting a list of all disk (not partition) devices is
999 * slightly non-trivial. We could look at /sys/block, but
1000 * that is theoretically due to be removed. Maybe best to use
1001 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1002 * we depend on this directory of 'path' info. But that fails
1003 * to find loop devices and probably others. Maybe don't
1004 * worry about that, they aren't the real target.
1006 * So: check things in /dev/disk/by-path to see if they are in
1007 * a compatible domain, then load the partition table and see
1008 * if it is OK for the new device, and choose the largest
1009 * partition table that fits.
1013 char *chosen
= NULL
;
1014 unsigned long long chosen_size
= 0;
1015 struct supertype
*chosen_st
= NULL
;
1018 dir
= opendir("/dev/disk/by-path");
1021 while ((de
= readdir(dir
)) != NULL
) {
1023 struct dev_policy
*pol2
= NULL
;
1024 struct domainlist
*domlist
= NULL
;
1027 struct supertype
*st2
= NULL
;
1028 char *devname
= NULL
;
1029 unsigned long long devsectors
;
1031 if (de
->d_ino
== 0 ||
1032 de
->d_name
[0] == '.' ||
1033 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1036 ep
= de
->d_name
+ strlen(de
->d_name
);
1037 while (ep
> de
->d_name
&&
1040 if (ep
> de
->d_name
+ 5 &&
1041 strncmp(ep
-5, "-part", 5) == 0)
1042 /* This is a partition - skip it */
1045 pol2
= path_policy(de
->d_name
, type_disk
);
1047 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1048 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1049 /* new device is incompatible with this device. */
1052 domain_free(domlist
);
1055 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1059 fd
= open(devname
, O_RDONLY
);
1062 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1067 st2
= dup_super(st
);
1069 st2
= guess_super_type(fd
, guess_partitions
);
1071 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1075 /* Check domain policy again, this time referring to metadata */
1076 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1077 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1078 /* Incompatible devices for this metadata type */
1080 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1081 /* Some partition types allow sparing, but not
1087 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1088 if (info
.component_size
> devsectors
)
1089 /* This partitioning doesn't fit in the device */
1092 /* This is an acceptable device to copy partition
1093 * metadata from. We could just stop here, but I
1094 * think I want to keep looking incase a larger
1095 * metadata which makes better use of the device can
1098 if (chosen
== NULL
||
1099 chosen_size
< info
.component_size
) {
1100 chosen_size
= info
.component_size
;
1105 chosen_st
->ss
->free_super(chosen_st
);
1114 domain_free(domlist
);
1115 dev_policy_free(pol2
);
1117 st2
->ss
->free_super(st2
);
1129 /* 'chosen' is the best device we can find. Let's write its
1130 * metadata to devname dfd is read-only so don't use that
1132 fd
= open(devname
, O_RDWR
);
1134 chosen_st
->ss
->store_super(chosen_st
, fd
);
1138 chosen_st
->ss
->free_super(chosen_st
);
1143 static int is_bare(int dfd
)
1145 unsigned long long size
= 0;
1146 char bufpad
[4096 + 4096];
1147 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1149 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1150 read(dfd
, buf
, 4096) != 4096)
1153 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1155 if (memcmp(buf
, buf
+1, 4095) != 0)
1158 /* OK, first 4K appear blank, try the end. */
1159 get_dev_size(dfd
, NULL
, &size
);
1160 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1161 read(dfd
, buf
, 4096) != 4096)
1164 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1166 if (memcmp(buf
, buf
+1, 4095) != 0)
1172 /* adding a spare to a regular array is quite different from adding one to
1173 * a set-of-partitions virtual array.
1174 * This function determines which is worth trying and tries as appropriate.
1175 * Arrays are given priority over partitions.
1177 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1178 struct map_ent
*target
,
1179 struct supertype
*st
, int verbose
)
1184 int partitions_ok
= 0;
1188 /* Can only add a spare if device has at least one domain */
1189 if (pol_find(pol
, pol_domain
) == NULL
)
1191 /* And only if some action allows spares */
1192 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1195 /* Now check if the device is bare.
1196 * bare devices can always be added as a spare
1197 * non-bare devices can only be added if spare-same-slot is permitted,
1198 * and this device is replacing a previous device - in which case 'target'
1201 if (!is_bare(dfd
)) {
1202 /* Must have a target and allow same_slot */
1203 /* Later - may allow force_spare without target */
1205 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1206 act_spare_same_slot
)) {
1208 fprintf(stderr
, Name
": %s is not bare, so not "
1209 "considering as a spare\n",
1217 /* It might be OK to add this device to an array - need to see
1218 * what arrays might be candidates.
1221 /* just try try 'array' or 'partition' based on this metadata */
1222 if (st
->ss
->add_to_super
)
1223 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1226 return partition_try_spare(devname
, dfdp
, pol
,
1229 /* No metadata was specified or found so options are open.
1230 * Check for whether any array metadata, or any partition metadata
1231 * might allow adding the spare. This check is just help to avoid
1232 * a more costly scan of all arrays when we can be sure that will
1235 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1236 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1237 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1239 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1240 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1245 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1247 if (rv
!= 0 && partitions_ok
)
1248 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1252 int IncrementalScan(int verbose
)
1254 /* look at every device listed in the 'map' file.
1255 * If one is found that is not running then:
1256 * look in mdadm.conf for bitmap file.
1257 * if one exists, but array has none, add it.
1258 * try to start array in auto-readonly mode
1260 struct map_ent
*mapl
= NULL
;
1262 struct mddev_ident
*devs
, *mddev
;
1266 devs
= conf_get_ident(NULL
);
1268 for (me
= mapl
; me
; me
= me
->next
) {
1269 mdu_array_info_t array
;
1270 mdu_bitmap_file_t bmf
;
1272 int mdfd
= open_dev(me
->devnum
);
1276 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1281 /* Ok, we can try this one. Maybe it needs a bitmap */
1282 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1283 if (mddev
->devname
&& me
->path
1284 && devname_matches(mddev
->devname
, me
->path
))
1286 if (mddev
&& mddev
->bitmap_file
) {
1288 * Note: early kernels will wrongly fail this, so it
1292 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1293 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1295 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1302 fprintf(stderr
, Name
1303 ": Added bitmap %s to %s\n",
1304 mddev
->bitmap_file
, me
->path
);
1305 else if (errno
!= EEXIST
)
1306 fprintf(stderr
, Name
1307 ": Failed to add bitmap to %s: %s\n",
1308 me
->path
, strerror(errno
));
1311 sra
= sysfs_read(mdfd
, 0, 0);
1313 if (sysfs_set_str(sra
, NULL
,
1314 "array_state", "read-auto") == 0) {
1316 fprintf(stderr
, Name
1317 ": started array %s\n",
1318 me
->path
?: devnum2devname(me
->devnum
));
1320 fprintf(stderr
, Name
1321 ": failed to start array %s: %s\n",
1322 me
->path
?: devnum2devname(me
->devnum
),
1331 static char *container2devname(char *devname
)
1333 char *mdname
= NULL
;
1335 if (devname
[0] == '/') {
1336 int fd
= open(devname
, O_RDONLY
);
1338 mdname
= devnum2devname(fd2devnum(fd
));
1343 struct map_ent
*mp
, *map
= NULL
;
1345 if (!parse_uuid(devname
, uuid
))
1347 mp
= map_by_uuid(&map
, uuid
);
1349 mdname
= devnum2devname(mp
->devnum
);
1356 static int Incremental_container(struct supertype
*st
, char *devname
,
1357 char *homehost
, int verbose
,
1358 int runstop
, int autof
, int freeze_reshape
)
1360 /* Collect the contents of this container and for each
1361 * array, choose a device name and assemble the array.
1364 struct mdinfo
*list
;
1366 struct map_ent
*map
= NULL
;
1369 struct mddev_ident
*match
;
1371 struct domainlist
*domains
;
1372 struct map_ent
*smp
;
1378 st
->ss
->getinfo_super(st
, &info
, NULL
);
1380 if ((runstop
> 0 && info
.container_enough
>= 0) ||
1381 info
.container_enough
> 0)
1385 fprintf(stderr
, Name
": not enough devices to start the container\n");
1389 match
= conf_match(st
, &info
, devname
, verbose
, &rv
);
1390 if (match
== NULL
&& rv
== 2)
1393 /* Need to compute 'trustworthy' */
1395 trustworthy
= LOCAL
;
1396 else if (st
->ss
->match_home(st
, homehost
) == 1)
1397 trustworthy
= LOCAL
;
1398 else if (st
->ss
->match_home(st
, "any") == 1)
1399 trustworthy
= LOCAL
;
1401 trustworthy
= FOREIGN
;
1403 list
= st
->ss
->container_content(st
, NULL
);
1404 /* when nothing to activate - quit */
1408 fprintf(stderr
, Name
": failed to get exclusive lock on "
1410 for (ra
= list
; ra
; ra
= ra
->next
) {
1412 char chosen_name
[1024];
1414 struct mddev_ident
*match
= NULL
;
1417 /* do not activate arrays blocked by metadata handler */
1418 if (ra
->array
.state
& (1 << MD_SB_BLOCK_VOLUME
)) {
1419 fprintf(stderr
, Name
": Cannot activate array %s in %s.\n",
1420 ra
->text_version
, devname
);
1424 mp
= map_by_uuid(&map
, ra
->uuid
);
1427 mdfd
= open_dev(mp
->devnum
);
1429 strcpy(chosen_name
, mp
->path
);
1431 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1434 /* Check in mdadm.conf for container == devname and
1435 * member == ra->text_version after second slash.
1437 char *sub
= strchr(ra
->text_version
+1, '/');
1438 struct mddev_ident
*array_list
;
1441 array_list
= conf_get_ident(NULL
);
1444 for(; array_list
; array_list
= array_list
->next
) {
1446 if (array_list
->member
== NULL
||
1447 array_list
->container
== NULL
)
1449 if (strcmp(array_list
->member
, sub
) != 0)
1451 if (array_list
->uuid_set
&&
1452 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1454 dn
= container2devname(array_list
->container
);
1457 if (strncmp(dn
, ra
->text_version
+1,
1459 ra
->text_version
[strlen(dn
)+1] != '/') {
1464 /* we have a match */
1467 fprintf(stderr
, Name
": match found for member %s\n",
1468 array_list
->member
);
1472 if (match
&& match
->devname
&&
1473 strcasecmp(match
->devname
, "<ignore>") == 0) {
1475 fprintf(stderr
, Name
": array %s/%s is "
1476 "explicitly ignored by mdadm.conf\n",
1477 match
->container
, match
->member
);
1481 trustworthy
= LOCAL
;
1483 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1491 fprintf(stderr
, Name
": failed to open %s: %s.\n",
1492 chosen_name
, strerror(errno
));
1496 assemble_container_content(st
, mdfd
, ra
, runstop
,
1497 chosen_name
, verbose
, NULL
,
1502 /* don't move spares to container with volume being activated
1503 when all volumes are blocked */
1504 if (ra_all
== ra_blocked
) {
1509 /* Now move all suitable spares from spare container */
1510 domains
= domain_from_array(list
, st
->ss
->name
);
1511 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1513 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1514 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1515 /* spare container found */
1516 struct supertype
*sst
=
1517 super_imsm
.match_metadata_desc("imsm");
1518 struct mdinfo
*sinfo
;
1519 unsigned long long min_size
= 0;
1520 if (st
->ss
->min_acceptable_spare_size
)
1521 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1522 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1524 sinfo
= container_choose_spares(sst
, min_size
,
1527 sst
->ss
->free_super(sst
);
1530 struct mdinfo
*disks
= sinfo
->devs
;
1532 /* move spare from spare
1533 * container to currently
1539 makedev(disks
->disk
.major
,
1540 disks
->disk
.minor
)))
1542 disks
= disks
->next
;
1545 fprintf(stderr
, Name
1546 ": Added %d spare%s to %s\n",
1547 count
, count
>1?"s":"", devname
);
1553 domain_free(domains
);
1558 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1559 * raid arrays, and if so first fail (if needed) and then remove the device.
1561 * @devname - The device we want to remove
1562 * @id_path - name as found in /dev/disk/by-path for this device
1564 * Note: the device name must be a kernel name like "sda", so
1565 * that we can find it in /proc/mdstat
1567 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1571 struct mdstat_ent
*ent
;
1572 struct mddev_dev devlist
;
1575 dprintf(Name
": incremental removal without --path <id_path> "
1576 "lacks the possibility to re-add new device in this "
1579 if (strchr(devname
, '/')) {
1580 fprintf(stderr
, Name
": incremental removal requires a "
1581 "kernel device name, not a file: %s\n", devname
);
1584 ent
= mdstat_by_component(devname
);
1586 fprintf(stderr
, Name
": %s does not appear to be a component "
1587 "of any array\n", devname
);
1590 mdfd
= open_dev(ent
->devnum
);
1592 fprintf(stderr
, Name
": Cannot open array %s!!\n", ent
->dev
);
1598 struct map_ent
*map
= NULL
, *me
;
1599 me
= map_by_devnum(&map
, ent
->devnum
);
1601 policy_save_path(id_path
, me
);
1605 memset(&devlist
, 0, sizeof(devlist
));
1606 devlist
.devname
= devname
;
1607 devlist
.disposition
= 'f';
1608 /* for a container, we must fail each member array */
1609 if (ent
->metadata_version
&&
1610 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1611 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1612 struct mdstat_ent
*memb
;
1613 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1614 if (is_container_member(memb
, ent
->dev
)) {
1615 int subfd
= open_dev(memb
->devnum
);
1617 Manage_subdevs(memb
->dev
, subfd
,
1618 &devlist
, verbose
, 0,
1623 free_mdstat(mdstat
);
1625 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);
1626 devlist
.disposition
= 'r';
1627 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);