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
);
49 static struct mddev_ident
*search_mdstat(struct supertype
*st
,
52 int verbose
, int *rvp
);
54 int Incremental(char *devname
, int verbose
, int runstop
,
55 struct supertype
*st
, char *homehost
, int require_homehost
,
58 /* Add this device to an array, creating the array if necessary
59 * and starting the array if sensible or - if runstop>0 - if possible.
61 * This has several steps:
63 * 1/ Check if device is permitted by mdadm.conf, reject if not.
64 * 2/ Find metadata, reject if none appropriate (check
65 * version/name from args)
66 * 3/ Check if there is a match in mdadm.conf
67 * 3a/ if not, check for homehost match. If no match, assemble as
69 * 4/ Determine device number.
70 * - If in mdadm.conf with std name, use that
71 * - UUID in /var/run/mdadm.map use that
72 * - If name is suggestive, use that. unless in use with different uuid.
73 * - Choose a free, high number.
74 * - Use a partitioned device unless strong suggestion not to.
76 * Don't choose partitioned for containers.
77 * 5/ Find out if array already exists
79 * - choose a name, from mdadm.conf or 'name' field in array.
83 * - check one drive in array to make sure metadata is a reasonably
84 * close match. Reject if not (e.g. different type)
86 * 6/ Make sure /var/run/mdadm.map contains this array.
87 * 7/ Is there enough devices to possibly start the array?
88 * For a container, this means running Incremental_container.
89 * 7a/ if not, finish with success.
91 * - read all metadata and arrange devices like -A does
92 * - if number of OK devices match expected, or -R and there are enough,
93 * start the array (auto-readonly).
96 struct mdinfo info
, dinfo
;
97 struct mdinfo
*sra
= NULL
, *d
;
98 struct mddev_ident
*match
;
99 char chosen_name
[1024];
101 struct map_ent
*mp
, *map
= NULL
;
102 int dfd
= -1, mdfd
= -1;
107 mdu_array_info_t ainf
;
108 struct dev_policy
*policy
= NULL
;
109 struct map_ent target_array
;
112 struct createinfo
*ci
= conf_get_create_info();
114 if (stat(devname
, &stb
) < 0) {
116 fprintf(stderr
, Name
": stat failed for %s: %s.\n",
117 devname
, strerror(errno
));
120 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
122 fprintf(stderr
, Name
": %s is not a block device.\n",
126 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
129 fprintf(stderr
, Name
": cannot open %s: %s.\n",
130 devname
, strerror(errno
));
133 /* If the device is a container, we do something very different */
134 if (must_be_container(dfd
)) {
136 st
= super_by_fd(dfd
, NULL
);
137 if (st
&& st
->ss
->load_container
)
138 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
141 if (!rv
&& st
->ss
->container_content
)
142 return Incremental_container(st
, devname
, homehost
,
143 verbose
, runstop
, autof
);
145 fprintf(stderr
, Name
": %s is not part of an md array.\n",
150 /* 1/ Check if device is permitted by mdadm.conf */
152 if (!conf_test_dev(devname
)) {
155 ": %s not permitted by mdadm.conf.\n",
160 /* 2/ Find metadata, reject if none appropriate (check
161 * version/name from args) */
163 if (fstat(dfd
, &stb
) < 0) {
165 fprintf(stderr
, Name
": fstat failed for %s: %s.\n",
166 devname
, strerror(errno
));
169 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
171 fprintf(stderr
, Name
": %s is not a block device.\n",
176 dinfo
.disk
.major
= major(stb
.st_rdev
);
177 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
179 policy
= disk_policy(&dinfo
);
180 have_target
= policy_check_path(&dinfo
, &target_array
);
182 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
185 ": no recognisable superblock on %s.\n",
187 rv
= try_spare(devname
, &dfd
, policy
,
188 have_target
? &target_array
: NULL
,
192 if (st
->ss
->compare_super
== NULL
||
193 st
->ss
->load_super(st
, dfd
, NULL
)) {
195 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
197 rv
= try_spare(devname
, &dfd
, policy
,
198 have_target
? &target_array
: NULL
,
203 close (dfd
); dfd
= -1;
205 memset(&info
, 0, sizeof(info
));
206 st
->ss
->getinfo_super(st
, &info
, NULL
);
208 /* 3/ Check if there is a match in mdadm.conf */
209 match
= search_mdstat(st
, &info
, devname
, verbose
, &rv
);
210 if (!match
&& rv
== 2)
213 if (match
&& match
->devname
214 && strcasecmp(match
->devname
, "<ignore>") == 0) {
216 fprintf(stderr
, Name
": array containing %s is explicitly"
217 " ignored by mdadm.conf\n",
222 /* 3a/ if not, check for homehost match. If no match, continue
223 * but don't trust the 'name' in the array. Thus a 'random' minor
224 * number will be assigned, and the device name will be based
228 else if (st
->ss
->match_home(st
, homehost
) == 1)
230 else if (st
->ss
->match_home(st
, "any") == 1)
231 trustworthy
= LOCAL_ANY
;
233 trustworthy
= FOREIGN
;
236 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
237 (trustworthy
== LOCAL
))) {
240 ": %s has metadata type %s for which "
241 "auto-assembly is disabled\n",
242 devname
, st
->ss
->name
);
245 if (trustworthy
== LOCAL_ANY
)
248 /* There are three possible sources for 'autof': command line,
249 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
250 * ARRAY takes precedence, then command line, then
253 if (match
&& match
->autof
)
254 autof
= match
->autof
;
258 name_to_use
= info
.name
;
259 if (name_to_use
[0] == 0 &&
260 info
.array
.level
== LEVEL_CONTAINER
&&
261 trustworthy
== LOCAL
) {
262 name_to_use
= info
.text_version
;
263 trustworthy
= METADATA
;
265 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
266 ! require_homehost
&&
267 conf_name_is_free(name_to_use
))
270 /* strip "hostname:" prefix from name if we have decided
271 * to treat it as LOCAL
273 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
274 name_to_use
= strchr(name_to_use
, ':')+1;
276 /* 4/ Check if array exists.
279 fprintf(stderr
, Name
": failed to get exclusive lock on "
281 mp
= map_by_uuid(&map
, info
.uuid
);
283 mdfd
= open_dev(mp
->devnum
);
289 /* Couldn't find an existing array, maybe make a new one */
290 mdfd
= create_mddev(match
? match
->devname
: NULL
,
291 name_to_use
, autof
, trustworthy
, chosen_name
);
296 sysfs_init(&info
, mdfd
, 0);
298 if (set_array_info(mdfd
, st
, &info
) != 0) {
299 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
300 chosen_name
, strerror(errno
));
306 dinfo
.disk
.major
= major(stb
.st_rdev
);
307 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
308 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
309 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
310 devname
, chosen_name
, strerror(errno
));
311 ioctl(mdfd
, STOP_ARRAY
, 0);
315 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
316 GET_OFFSET
| GET_SIZE
));
318 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
319 /* It really should be 'none' - must be old buggy
320 * kernel, and mdadm -I may not be able to complete.
323 ioctl(mdfd
, STOP_ARRAY
, NULL
);
325 ": You have an old buggy kernel which cannot support\n"
326 " --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
363 if ((info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
364 && ! policy_action_allows(policy
, st
->ss
->name
,
369 if (st
->ss
->external
) {
370 char *devname
= devnum2devname(fd2devnum(mdfd
));
372 active
= devname
&& is_container_active(devname
);
374 } else if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0)
378 ": not adding %s to active array (without --run) %s\n",
379 devname
, chosen_name
);
389 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
390 sra
->devs
->disk
.minor
);
391 dfd2
= dev_open(dn
, O_RDONLY
);
393 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
394 st
->ss
->compare_super(st
, st2
) != 0) {
396 ": metadata mismatch between %s and "
398 devname
, chosen_name
);
404 memset(&info2
, 0, sizeof(info2
));
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 info2
.disk
.major
= major(stb
.st_rdev
);
418 info2
.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
, &info2
);
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
, &info2
);
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 char *devname
= NULL
;
447 /* Try to assemble within the container */
449 sysfs_uevent(&info
, "change");
452 ": container %s now has %d devices\n",
453 chosen_name
, info
.array
.working_disks
);
454 wait_for(chosen_name
, mdfd
);
455 if (st
->ss
->external
)
456 devname
= devnum2devname(fd2devnum(mdfd
));
459 rv
= Incremental(chosen_name
, verbose
, runstop
,
460 NULL
, homehost
, require_homehost
, autof
);
462 /* Don't fail the whole -I if a subarray didn't
463 * have enough devices to start yet
466 /* after spare is added, ping monitor for external metadata
467 * so that it can eg. try to rebuild degraded array */
468 if (st
->ss
->external
) {
469 ping_monitor(devname
);
475 /* We have added something to the array, so need to re-read the
476 * state. Eventually this state should be kept up-to-date as
480 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
481 GET_OFFSET
| GET_SIZE
));
482 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
483 if (enough(info
.array
.level
, info
.array
.raid_disks
,
484 info
.array
.layout
, info
.array
.state
& 1,
485 avail
, active_disks
) == 0) {
488 ": %s attached to %s, not enough to start (%d).\n",
489 devname
, chosen_name
, active_disks
);
496 /* - if number of OK devices match expected, or -R and there */
498 /* + add any bitmap file */
499 /* + start the array (auto-readonly). */
501 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
504 ": %s attached to %s which is already active.\n",
505 devname
, chosen_name
);
512 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
514 /* Let's try to start it */
515 if (match
&& match
->bitmap_file
) {
516 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
519 ": Could not open bitmap file %s.\n",
523 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
526 ": Failed to set bitmapfile for %s.\n",
532 /* Need to remove from the array any devices which
533 * 'count_active' discerned were too old or inappropriate
535 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
536 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
537 remove_disk(mdfd
, st
, sra
, d
);
539 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
540 && trustworthy
!= FOREIGN
)
541 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
543 rv
= sysfs_set_str(sra
, NULL
,
544 "array_state", "read-auto");
548 ": %s attached to %s, which has been started.\n",
549 devname
, chosen_name
);
551 wait_for(chosen_name
, mdfd
);
552 /* We just started the array, so some devices
553 * might have been evicted from the array
554 * because their event counts were too old.
555 * If the action=re-add policy is in-force for
556 * those devices we should re-add them now.
558 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
559 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
560 add_disk(mdfd
, st
, sra
, dsk
) == 0)
562 ": %s re-added to %s\n",
563 dsk
->sys_name
, chosen_name
);
567 ": %s attached to %s, but failed to start: %s.\n",
568 devname
, chosen_name
, strerror(errno
));
574 ": %s attached to %s, not enough to start safely.\n",
575 devname
, chosen_name
);
585 dev_policy_free(policy
);
591 static struct mddev_ident
*search_mdstat(struct supertype
*st
,
594 int verbose
, int *rvp
)
596 struct mddev_ident
*array_list
, *match
;
597 array_list
= conf_get_ident(NULL
);
599 for (; array_list
; array_list
= array_list
->next
) {
600 if (array_list
->uuid_set
&&
601 same_uuid(array_list
->uuid
, info
->uuid
, st
->ss
->swapuuid
)
603 if (verbose
>= 2 && array_list
->devname
)
605 ": UUID differs from %s.\n",
606 array_list
->devname
);
609 if (array_list
->name
[0] &&
610 strcasecmp(array_list
->name
, info
->name
) != 0) {
611 if (verbose
>= 2 && array_list
->devname
)
613 ": Name differs from %s.\n",
614 array_list
->devname
);
617 if (array_list
->devices
&&
618 !match_oneof(array_list
->devices
, devname
)) {
619 if (verbose
>= 2 && array_list
->devname
)
621 ": Not a listed device for %s.\n",
622 array_list
->devname
);
625 if (array_list
->super_minor
!= UnSet
&&
626 array_list
->super_minor
!= info
->array
.md_minor
) {
627 if (verbose
>= 2 && array_list
->devname
)
629 ": Different super-minor to %s.\n",
630 array_list
->devname
);
633 if (!array_list
->uuid_set
&&
634 !array_list
->name
[0] &&
635 !array_list
->devices
&&
636 array_list
->super_minor
== UnSet
) {
637 if (verbose
>= 2 && array_list
->devname
)
639 ": %s doesn't have any identifying information.\n",
640 array_list
->devname
);
643 /* FIXME, should I check raid_disks and level too?? */
647 if (match
->devname
&& array_list
->devname
)
649 ": we match both %s and %s - cannot decide which to use.\n",
650 match
->devname
, array_list
->devname
);
653 ": multiple lines in mdadm.conf match\n");
664 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
665 int number
, __u64 events
, int verbose
,
668 /* Find a device attached to this array with a disk.number of number
669 * and events less than the passed events, and remove the device.
674 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
675 return; /* not safe to remove from active arrays
676 * without thinking more */
678 for (d
= sra
->devs
; d
; d
= d
->next
) {
682 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
683 dfd
= dev_open(dn
, O_RDONLY
);
686 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
690 st
->ss
->getinfo_super(st
, &info
, NULL
);
691 st
->ss
->free_super(st
);
694 if (info
.disk
.number
!= number
||
695 info
.events
>= events
)
698 if (d
->disk
.raid_disk
> -1)
699 sysfs_set_str(sra
, d
, "slot", "none");
700 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
703 ": removing old device %s from %s\n",
704 d
->sys_name
+4, array_name
);
708 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
709 int mdfd
, char **availp
,
710 struct mdinfo
*bestinfo
)
712 /* count how many devices in sra think they are active */
715 __u64 max_events
= 0;
727 for (d
= sra
->devs
; d
; d
= d
->next
)
729 for (d
= sra
->devs
, devnum
=0 ; d
; d
= d
->next
, devnum
++) {
735 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
736 dfd
= dev_open(dn
, O_RDONLY
);
739 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
743 info
.array
.raid_disks
= raid_disks
;
744 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
746 raid_disks
= info
.array
.raid_disks
;
747 avail
= calloc(raid_disks
, 1);
749 fprintf(stderr
, Name
": out of memory.\n");
754 best
= calloc(raid_disks
, sizeof(int));
755 devmap
= calloc(raid_disks
* numdevs
, 1);
757 st
->ss
->getinfo_super(st
, &info
, devmap
);
760 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
764 max_events
= info
.events
;
765 avail
[info
.disk
.raid_disk
] = 2;
766 best
[info
.disk
.raid_disk
] = devnum
;
767 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
768 } else if (info
.events
== max_events
) {
769 avail
[info
.disk
.raid_disk
] = 2;
770 best
[info
.disk
.raid_disk
] = devnum
;
771 } else if (info
.events
== max_events
-1) {
772 if (avail
[info
.disk
.raid_disk
] == 0) {
773 avail
[info
.disk
.raid_disk
] = 1;
774 best
[info
.disk
.raid_disk
] = devnum
;
776 } else if (info
.events
< max_events
- 1)
778 else if (info
.events
== max_events
+1) {
780 max_events
= info
.events
;
781 for (i
=0; i
< raid_disks
; i
++)
784 avail
[info
.disk
.raid_disk
] = 2;
785 best
[info
.disk
.raid_disk
] = devnum
;
786 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
787 } else { /* info.events much bigger */
788 memset(avail
, 0, info
.disk
.raid_disk
);
789 max_events
= info
.events
;
790 avail
[info
.disk
.raid_disk
] = 2;
791 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
794 st
->ss
->free_super(st
);
798 /* We need to reject any device that thinks the best device is
799 * failed or missing */
800 for (b
= 0; b
< raid_disks
; b
++)
804 for (i
= 0 ; i
< raid_disks
; i
++) {
805 if (i
!= b
&& avail
[i
])
806 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
807 /* This device thinks 'b' is failed -
810 for (d
=sra
->devs
; devnum
; d
= d
->next
)
812 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
823 /* test if container has degraded member(s) */
824 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
826 mdu_array_info_t array
;
828 int max_degraded
= 0;
830 for(; map
; map
= map
->next
) {
831 if (!is_subarray(map
->metadata
) ||
832 devname2devnum(map
->metadata
+1) != me
->devnum
)
834 afd
= open_dev(map
->devnum
);
837 /* most accurate information regarding array degradation */
838 if (ioctl(afd
, GET_ARRAY_INFO
, &array
) >= 0) {
839 int degraded
= array
.raid_disks
- array
.active_disks
-
841 if (degraded
> max_degraded
)
842 max_degraded
= degraded
;
846 return (max_degraded
);
849 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
850 struct map_ent
*target
, int bare
,
851 struct supertype
*st
, int verbose
)
853 /* This device doesn't have any md metadata
854 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
855 * If 'st' is not set, then we only know that some metadata allows this,
856 * others possibly don't.
857 * So look for a container or array to attach the device to.
858 * Prefer 'target' if that is set and the array is found.
860 * If st is set, then only arrays of that type are considered
861 * Return 0 on success, or some exit code on failure, probably 1.
865 struct map_ent
*mp
, *map
= NULL
;
866 struct mdinfo
*chosen
= NULL
;
869 if (fstat(dfd
, &stb
) != 0)
873 * Now we need to find a suitable array to add this to.
874 * We only accept arrays that:
876 * - are in the same domains as the device
877 * - are of an size for which the device will be useful
878 * and we choose the one that is the most degraded
881 if (map_lock(&map
)) {
882 fprintf(stderr
, Name
": failed to get exclusive lock on "
886 for (mp
= map
; mp
; mp
= mp
->next
) {
887 struct supertype
*st2
;
888 struct domainlist
*dl
= NULL
;
890 unsigned long long devsize
;
891 unsigned long long component_size
;
893 if (is_subarray(mp
->metadata
))
896 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
898 (st
->minor_version
>= 0 &&
899 st
->minor_version
!= st2
->minor_version
)) {
901 fprintf(stderr
, Name
": not adding %s to %s as metadata type doesn't match\n",
908 sra
= sysfs_read(-1, mp
->devnum
,
909 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
910 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
912 /* Probably a container - no degraded info */
913 sra
= sysfs_read(-1, mp
->devnum
,
914 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
915 GET_COMPONENT
|GET_VERSION
);
917 sra
->array
.failed_disks
= -1;
924 for(i
=0; !st2
&& superlist
[i
]; i
++)
925 st2
= superlist
[i
]->match_metadata_desc(
929 fprintf(stderr
, Name
": not adding %s to %s"
930 " as metadata not recognised.\n",
934 /* Need to double check the 'act_spare' permissions applies
937 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
939 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
940 act_spare_same_slot
))
944 /* update number of failed disks for mostly degraded
945 * container member */
946 if (sra
->array
.failed_disks
== -1)
947 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
949 get_dev_size(dfd
, NULL
, &devsize
);
950 if (sra
->component_size
== 0) {
951 /* true for containers, here we must read superblock
952 * to obtain minimum spare size */
953 struct supertype
*st3
= dup_super(st2
);
954 int mdfd
= open_dev(mp
->devnum
);
957 if (st3
->ss
->load_container
&&
958 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
959 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
960 st3
->ss
->free_super(st3
);
965 if ((sra
->component_size
> 0 &&
966 st2
->ss
->avail_size(st2
, devsize
) < sra
->component_size
)
968 (sra
->component_size
== 0 && devsize
< component_size
)) {
970 fprintf(stderr
, Name
": not adding %s to %s as it is too small\n",
974 /* test against target.
975 * If 'target' is set and 'bare' is false, we only accept
976 * arrays/containers that match 'target'.
977 * If 'target' is set and 'bare' is true, we prefer the
978 * array which matches 'target'.
979 * target is considered only if we deal with degraded array
981 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
982 act_spare_same_slot
)) {
983 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
984 memcmp(target
->uuid
, mp
->uuid
,
985 sizeof(target
->uuid
)) == 0 &&
986 sra
->array
.failed_disks
> 0) {
987 /* This is our target!! */
992 /* skip to end so we don't check any more */
1002 dl
= domain_from_array(sra
, st2
->ss
->name
);
1003 if (!domain_test(dl
, pol
, st2
->ss
->name
)) {
1004 /* domain test fails */
1006 fprintf(stderr
, Name
": not adding %s to %s as"
1007 " it is not in a compatible domain\n",
1012 /* all tests passed, OK to add to this array */
1016 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
1030 /* add current device to chosen array as a spare */
1031 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
1033 struct mddev_dev devlist
;
1035 devlist
.next
= NULL
;
1038 devlist
.writemostly
= 0;
1039 devlist
.devname
= devname
;
1040 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
1041 minor(stb
.st_rdev
));
1042 devlist
.disposition
= 'a';
1045 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
1051 fprintf(stderr
, Name
": added %s as spare for %s\n",
1052 devname
, chosen
->sys_name
);
1054 fprintf(stderr
, Name
": failed to add %s as spare for %s\n",
1055 devname
, chosen
->sys_name
);
1062 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1063 struct supertype
*st
, int verbose
)
1065 /* we know that at least one partition virtual-metadata is
1066 * allowed to incorporate spares like this device. We need to
1067 * find a suitable device to copy partition information from.
1069 * Getting a list of all disk (not partition) devices is
1070 * slightly non-trivial. We could look at /sys/block, but
1071 * that is theoretically due to be removed. Maybe best to use
1072 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1073 * we depend on this directory of 'path' info. But that fails
1074 * to find loop devices and probably others. Maybe don't
1075 * worry about that, they aren't the real target.
1077 * So: check things in /dev/disk/by-path to see if they are in
1078 * a compatible domain, then load the partition table and see
1079 * if it is OK for the new device, and choose the largest
1080 * partition table that fits.
1084 char *chosen
= NULL
;
1085 unsigned long long chosen_size
;
1086 struct supertype
*chosen_st
= NULL
;
1089 dir
= opendir("/dev/disk/by-path");
1092 while ((de
= readdir(dir
)) != NULL
) {
1094 struct dev_policy
*pol2
= NULL
;
1095 struct domainlist
*domlist
= NULL
;
1098 struct supertype
*st2
= NULL
;
1099 char *devname
= NULL
;
1100 unsigned long long devsectors
;
1102 if (de
->d_ino
== 0 ||
1103 de
->d_name
[0] == '.' ||
1104 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1107 ep
= de
->d_name
+ strlen(de
->d_name
);
1108 while (ep
> de
->d_name
&&
1111 if (ep
> de
->d_name
+ 5 &&
1112 strncmp(ep
-5, "-part", 5) == 0)
1113 /* This is a partition - skip it */
1116 pol2
= path_policy(de
->d_name
, type_disk
);
1118 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1119 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) == 0)
1120 /* new device is incompatible with this device. */
1123 domain_free(domlist
);
1126 asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
);
1127 fd
= open(devname
, O_RDONLY
);
1130 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1135 st2
= dup_super(st
);
1137 st2
= guess_super_type(fd
, guess_partitions
);
1139 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1143 /* Check domain policy again, this time referring to metadata */
1144 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1145 if (domain_test(domlist
, pol
, st2
->ss
->name
) == 0)
1146 /* Incompatible devices for this metadata type */
1148 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1149 /* Some partition types allow sparing, but not
1155 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1156 if (info
.component_size
> devsectors
)
1157 /* This partitioning doesn't fit in the device */
1160 /* This is an acceptable device to copy partition
1161 * metadata from. We could just stop here, but I
1162 * think I want to keep looking incase a larger
1163 * metadata which makes better use of the device can
1166 if (chosen
== NULL
||
1167 chosen_size
< info
.component_size
) {
1168 chosen_size
= info
.component_size
;
1173 chosen_st
->ss
->free_super(chosen_st
);
1182 domain_free(domlist
);
1183 dev_policy_free(pol2
);
1185 st2
->ss
->free_super(st2
);
1195 /* 'chosen' is the best device we can find. Let's write its
1196 * metadata to devname dfd is read-only so don't use that
1198 fd
= open(devname
, O_RDWR
);
1200 chosen_st
->ss
->store_super(chosen_st
, fd
);
1204 chosen_st
->ss
->free_super(chosen_st
);
1209 static int is_bare(int dfd
)
1211 unsigned long long size
= 0;
1212 char bufpad
[4096 + 4096];
1213 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1215 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1216 read(dfd
, buf
, 4096) != 4096)
1219 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1221 if (memcmp(buf
, buf
+1, 4095) != 0)
1224 /* OK, first 4K appear blank, try the end. */
1225 get_dev_size(dfd
, NULL
, &size
);
1226 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1227 read(dfd
, buf
, 4096) != 4096)
1230 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1232 if (memcmp(buf
, buf
+1, 4095) != 0)
1238 /* adding a spare to a regular array is quite different from adding one to
1239 * a set-of-partitions virtual array.
1240 * This function determines which is worth trying and tries as appropriate.
1241 * Arrays are given priority over partitions.
1243 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1244 struct map_ent
*target
,
1245 struct supertype
*st
, int verbose
)
1250 int partitions_ok
= 0;
1254 /* Can only add a spare if device has at least one domain */
1255 if (pol_find(pol
, pol_domain
) == NULL
)
1257 /* And only if some action allows spares */
1258 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1261 /* Now check if the device is bare.
1262 * bare devices can always be added as a spare
1263 * non-bare devices can only be added if spare-same-slot is permitted,
1264 * and this device is replacing a previous device - in which case 'target'
1267 if (!is_bare(dfd
)) {
1268 /* Must have a target and allow same_slot */
1269 /* Later - may allow force_spare without target */
1271 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1272 act_spare_same_slot
)) {
1274 fprintf(stderr
, Name
": %s is not bare, so not "
1275 "considering as a spare\n",
1283 /* It might be OK to add this device to an array - need to see
1284 * what arrays might be candidates.
1287 /* just try try 'array' or 'partition' based on this metadata */
1288 if (st
->ss
->add_to_super
)
1289 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1292 return partition_try_spare(devname
, dfdp
, pol
,
1295 /* No metadata was specified or found so options are open.
1296 * Check for whether any array metadata, or any partition metadata
1297 * might allow adding the spare. This check is just help to avoid
1298 * a more costly scan of all arrays when we can be sure that will
1301 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1302 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1303 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1305 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1306 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1311 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1313 if (rv
!= 0 && partitions_ok
)
1314 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1318 int IncrementalScan(int verbose
)
1320 /* look at every device listed in the 'map' file.
1321 * If one is found that is not running then:
1322 * look in mdadm.conf for bitmap file.
1323 * if one exists, but array has none, add it.
1324 * try to start array in auto-readonly mode
1326 struct map_ent
*mapl
= NULL
;
1328 struct mddev_ident
*devs
, *mddev
;
1332 devs
= conf_get_ident(NULL
);
1334 for (me
= mapl
; me
; me
= me
->next
) {
1335 mdu_array_info_t array
;
1336 mdu_bitmap_file_t bmf
;
1338 int mdfd
= open_dev(me
->devnum
);
1342 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1347 /* Ok, we can try this one. Maybe it needs a bitmap */
1348 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1349 if (mddev
->devname
&& me
->path
1350 && devname_matches(mddev
->devname
, me
->path
))
1352 if (mddev
&& mddev
->bitmap_file
) {
1354 * Note: early kernels will wrongly fail this, so it
1358 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1359 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1361 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1368 fprintf(stderr
, Name
1369 ": Added bitmap %s to %s\n",
1370 mddev
->bitmap_file
, me
->path
);
1371 else if (errno
!= EEXIST
)
1372 fprintf(stderr
, Name
1373 ": Failed to add bitmap to %s: %s\n",
1374 me
->path
, strerror(errno
));
1377 sra
= sysfs_read(mdfd
, 0, 0);
1379 if (sysfs_set_str(sra
, NULL
,
1380 "array_state", "read-auto") == 0) {
1382 fprintf(stderr
, Name
1383 ": started array %s\n",
1384 me
->path
?: devnum2devname(me
->devnum
));
1386 fprintf(stderr
, Name
1387 ": failed to start array %s: %s\n",
1388 me
->path
?: devnum2devname(me
->devnum
),
1397 static char *container2devname(char *devname
)
1399 char *mdname
= NULL
;
1401 if (devname
[0] == '/') {
1402 int fd
= open(devname
, O_RDONLY
);
1404 mdname
= devnum2devname(fd2devnum(fd
));
1409 struct map_ent
*mp
, *map
= NULL
;
1411 if (!parse_uuid(devname
, uuid
))
1413 mp
= map_by_uuid(&map
, uuid
);
1415 mdname
= devnum2devname(mp
->devnum
);
1422 static int Incremental_container(struct supertype
*st
, char *devname
,
1423 char *homehost
, int verbose
,
1424 int runstop
, int autof
)
1426 /* Collect the contents of this container and for each
1427 * array, choose a device name and assemble the array.
1430 struct mdinfo
*list
;
1432 struct map_ent
*map
= NULL
;
1435 struct mddev_ident
*match
;
1437 struct domainlist
*domains
;
1438 struct map_ent
*smp
;
1442 memset(&info
, 0, sizeof(info
));
1443 st
->ss
->getinfo_super(st
, &info
, NULL
);
1445 if ((runstop
> 0 && info
.container_enough
>= 0) ||
1446 info
.container_enough
> 0)
1450 fprintf(stderr
, Name
": not enough devices to start the container\n");
1454 match
= search_mdstat(st
, &info
, devname
, verbose
, &rv
);
1455 if (match
== NULL
&& rv
== 2)
1458 /* Need to compute 'trustworthy' */
1460 trustworthy
= LOCAL
;
1461 else if (st
->ss
->match_home(st
, homehost
) == 1)
1462 trustworthy
= LOCAL
;
1463 else if (st
->ss
->match_home(st
, "any") == 1)
1464 trustworthy
= LOCAL
;
1466 trustworthy
= FOREIGN
;
1468 list
= st
->ss
->container_content(st
, NULL
);
1470 fprintf(stderr
, Name
": failed to get exclusive lock on "
1472 /* do not assemble arrays that might have bad blocks */
1473 if (list
->array
.state
& (1<<MD_SB_BBM_ERRORS
)) {
1474 fprintf(stderr
, Name
": BBM log found in metadata. "
1475 "Cannot activate array(s).\n");
1479 for (ra
= list
; ra
; ra
= ra
->next
) {
1481 char chosen_name
[1024];
1483 struct mddev_ident
*match
= NULL
;
1485 mp
= map_by_uuid(&map
, ra
->uuid
);
1488 mdfd
= open_dev(mp
->devnum
);
1490 strcpy(chosen_name
, mp
->path
);
1492 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1495 /* Check in mdadm.conf for container == devname and
1496 * member == ra->text_version after second slash.
1498 char *sub
= strchr(ra
->text_version
+1, '/');
1499 struct mddev_ident
*array_list
;
1502 array_list
= conf_get_ident(NULL
);
1505 for(; array_list
; array_list
= array_list
->next
) {
1507 if (array_list
->member
== NULL
||
1508 array_list
->container
== NULL
)
1510 if (strcmp(array_list
->member
, sub
) != 0)
1512 if (array_list
->uuid_set
&&
1513 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1515 dn
= container2devname(array_list
->container
);
1518 if (strncmp(dn
, ra
->text_version
+1,
1520 ra
->text_version
[strlen(dn
)+1] != '/') {
1525 /* we have a match */
1528 fprintf(stderr
, Name
": match found for member %s\n",
1529 array_list
->member
);
1533 if (match
&& match
->devname
&&
1534 strcasecmp(match
->devname
, "<ignore>") == 0) {
1536 fprintf(stderr
, Name
": array %s/%s is "
1537 "explicitly ignored by mdadm.conf\n",
1538 match
->container
, match
->member
);
1542 trustworthy
= LOCAL
;
1544 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1552 fprintf(stderr
, Name
": failed to open %s: %s.\n",
1553 chosen_name
, strerror(errno
));
1557 assemble_container_content(st
, mdfd
, ra
, runstop
,
1558 chosen_name
, verbose
);
1561 /* Now move all suitable spares from spare container */
1562 domains
= domain_from_array(list
, st
->ss
->name
);
1563 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1565 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1566 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1567 /* spare container found */
1568 struct supertype
*sst
=
1569 super_imsm
.match_metadata_desc("imsm");
1570 struct mdinfo
*sinfo
;
1571 unsigned long long min_size
= 0;
1572 if (st
->ss
->min_acceptable_spare_size
)
1573 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1574 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1576 sinfo
= container_choose_spares(sst
, min_size
,
1579 sst
->ss
->free_super(sst
);
1582 struct mdinfo
*disks
= sinfo
->devs
;
1584 /* move spare from spare
1585 * container to currently
1591 makedev(disks
->disk
.major
,
1592 disks
->disk
.minor
)))
1594 disks
= disks
->next
;
1597 fprintf(stderr
, Name
1598 ": Added %d spare%s to %s\n",
1599 count
, count
>1?"s":"", devname
);
1605 domain_free(domains
);
1611 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1612 * raid arrays, and if so first fail (if needed) and then remove the device.
1614 * @devname - The device we want to remove
1615 * @id_path - name as found in /dev/disk/by-path for this device
1617 * Note: the device name must be a kernel name like "sda", so
1618 * that we can find it in /proc/mdstat
1620 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1624 struct mdstat_ent
*ent
;
1625 struct mddev_dev devlist
;
1628 dprintf(Name
": incremental removal without --path <id_path> "
1629 "lacks the possibility to re-add new device in this "
1632 if (strchr(devname
, '/')) {
1633 fprintf(stderr
, Name
": incremental removal requires a "
1634 "kernel device name, not a file: %s\n", devname
);
1637 ent
= mdstat_by_component(devname
);
1639 fprintf(stderr
, Name
": %s does not appear to be a component "
1640 "of any array\n", devname
);
1643 mdfd
= open_dev(ent
->devnum
);
1645 fprintf(stderr
, Name
": Cannot open array %s!!\n", ent
->dev
);
1651 struct map_ent
*map
= NULL
, *me
;
1652 me
= map_by_devnum(&map
, ent
->devnum
);
1654 policy_save_path(id_path
, me
);
1658 memset(&devlist
, 0, sizeof(devlist
));
1659 devlist
.devname
= devname
;
1660 devlist
.disposition
= 'f';
1661 /* for a container, we must fail each member array */
1662 if (ent
->metadata_version
&&
1663 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1664 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1665 struct mdstat_ent
*memb
;
1666 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1667 if (is_container_member(memb
, ent
->dev
)) {
1668 int subfd
= open_dev(memb
->devnum
);
1670 Manage_subdevs(memb
->dev
, subfd
,
1671 &devlist
, verbose
, 0,
1676 free_mdstat(mdstat
);
1678 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
);
1679 devlist
.disposition
= 'r';
1680 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
);