2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
28 static int name_matches(char *found
, char *required
, char *homehost
)
30 /* See if the name found matches the required name, possibly
31 * prefixed with 'homehost'
35 strncpy(fnd
, found
, 32);
37 if (strcmp(found
, required
)==0)
40 int l
= strlen(homehost
);
41 if (l
< 32 && fnd
[l
] == ':' &&
42 strcmp(fnd
+l
+1, required
)==0)
48 static int is_member_busy(char *metadata_version
)
50 /* check if the given member array is active */
51 struct mdstat_ent
*mdstat
= mdstat_read(1, 0);
52 struct mdstat_ent
*ent
;
55 for (ent
= mdstat
; ent
; ent
= ent
->next
) {
56 if (ent
->metadata_version
== NULL
)
58 if (strncmp(ent
->metadata_version
, "external:", 9) != 0)
60 if (!is_subarray(&ent
->metadata_version
[9]))
62 /* Skip first char - it can be '/' or '-' */
63 if (strcmp(&ent
->metadata_version
[10], metadata_version
+1) == 0) {
73 static int ident_matches(struct mddev_ident
*ident
,
74 struct mdinfo
*content
,
75 struct supertype
*tst
,
77 char *update
, char *devname
)
80 if (ident
->uuid_set
&& (!update
|| strcmp(update
, "uuid")!= 0) &&
81 same_uuid(content
->uuid
, ident
->uuid
, tst
->ss
->swapuuid
)==0 &&
82 memcmp(content
->uuid
, uuid_zero
, sizeof(int[4])) != 0) {
84 pr_err("%s has wrong uuid.\n", devname
);
87 if (ident
->name
[0] && (!update
|| strcmp(update
, "name")!= 0) &&
88 name_matches(content
->name
, ident
->name
, homehost
)==0) {
90 pr_err("%s has wrong name.\n", devname
);
93 if (ident
->super_minor
!= UnSet
&&
94 ident
->super_minor
!= content
->array
.md_minor
) {
96 pr_err("%s has wrong super-minor.\n",
100 if (ident
->level
!= UnSet
&&
101 ident
->level
!= content
->array
.level
) {
103 pr_err("%s has wrong raid level.\n",
107 if (ident
->raid_disks
!= UnSet
&&
108 ident
->raid_disks
!= content
->array
.raid_disks
) {
110 pr_err("%s requires wrong number of drives.\n",
114 if (ident
->member
&& ident
->member
[0]) {
115 /* content->text_version must match */
116 char *s
= strchr(content
->text_version
+1, '/');
119 pr_err("%s is not a container and one is required.\n",
122 } else if (strcmp(ident
->member
, s
+1) != 0) {
124 pr_err("skipping wrong member %s is %s\n",
125 content
->text_version
, devname
);
133 int Assemble(struct supertype
*st
, char *mddev
,
134 struct mddev_ident
*ident
,
135 struct mddev_dev
*devlist
,
136 char *backup_file
, int invalid_backup
,
137 int readonly
, int runstop
,
138 char *update
, char *homehost
, int require_homehost
,
139 int verbose
, int force
, int freeze_reshape
)
142 * The task of Assemble is to find a collection of
143 * devices that should (according to their superblocks)
144 * form an array, and to give this collection to the MD driver.
145 * In Linux-2.4 and later, this involves submitting a
146 * SET_ARRAY_INFO ioctl with no arg - to prepare
147 * the array - and then submit a number of
148 * ADD_NEW_DISK ioctls to add disks into
149 * the array. Finally RUN_ARRAY might
150 * be submitted to start the array.
152 * Much of the work of Assemble is in finding and/or
153 * checking the disks to make sure they look right.
155 * If mddev is not set, then scan must be set and we
156 * read through the config file for dev+uuid mapping
157 * We recurse, setting mddev, for each device that
159 * - has a valid uuid (or any uuid if !uuidset)
161 * If mddev is set, we try to determine state of md.
162 * check version - must be at least 0.90.0
163 * check kernel version. must be at least 2.4.
164 * If not, we can possibly fall back on START_ARRAY
165 * Try to GET_ARRAY_INFO.
166 * If possible, give up
167 * If not, try to STOP_ARRAY just to make sure
169 * If !uuidset and scan, look in conf-file for uuid
170 * If not found, give up
171 * If !devlist and scan and uuidset, get list of devs from conf-file
174 * Check superblock - discard if bad
175 * Check uuid (set if we don't have one) - discard if no match
176 * Check superblock similarity if we have a superblock - discard if different
177 * Record events, devicenum
178 * This should give us a list of devices for the array
179 * We should collect the most recent event number
181 * Count disks with recent enough event count
182 * While force && !enough disks
183 * Choose newest rejected disks, update event count
184 * mark clean and rewrite superblock
187 * foreach device with recent events : ADD_NEW_DISK
188 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
190 * Check the device numbers in superblock are right
191 * update superblock if any changes
197 int auto_assem
= (mddev
== NULL
&& !ident
->uuid_set
&&
198 ident
->super_minor
== UnSet
&& ident
->name
[0] == 0
199 && (ident
->container
== NULL
|| ident
->member
== NULL
));
201 int vers
= vers
; /* Keep gcc quite - it really is initialised */
204 int uptodate
; /* set once we decide that this device is as
205 * recent as everything else in the array.
210 int *best
= NULL
; /* indexed by raid_disk */
213 unsigned int okcnt
, sparecnt
, rebuilding_cnt
;
214 unsigned int req_cnt
;
220 int report_missmatch
;
224 int start_partial_ok
= (runstop
>= 0) &&
225 (force
|| devlist
==NULL
|| auto_assem
);
226 unsigned int num_devs
;
227 struct mddev_dev
*tmpdev
;
229 struct mdinfo
*content
= NULL
;
234 char chosen_name
[1024];
235 struct domainlist
*domains
= NULL
;
237 if (get_linux_version() < 2004000)
241 * If any subdevs are listed, then any that don't
242 * match ident are discarded. Remainder must all match and
244 * If no subdevs, then we scan all devices in the config file, but
245 * there must be something in the identity
249 ident
->uuid_set
== 0 &&
250 (ident
->super_minor
< 0 || ident
->super_minor
== UnSet
) &&
251 ident
->name
[0] == 0 &&
252 (ident
->container
== NULL
|| ident
->member
== NULL
) &&
253 ident
->devices
== NULL
) {
254 pr_err("No identity information available for %s - cannot assemble.\n",
255 mddev
? mddev
: "further assembly");
260 devlist
= conf_get_devs();
264 report_missmatch
= ((inargv
&& verbose
>= 0) || verbose
> 0);
266 /* We come back here when doing auto-assembly and attempting some
267 * set of devices failed. Those are now marked as ->used==2 and
268 * we ignore them and try again
271 tmpdev
= devlist
; num_devs
= 0;
277 tmpdev
= tmpdev
->next
;
280 if (!st
&& ident
->st
) st
= ident
->st
;
283 pr_err("looking for devices for %s\n",
284 mddev
? mddev
: "further assembly");
286 /* first walk the list of devices to find a consistent set
287 * that match the criterea, if that is possible.
288 * We flag the ones we like with 'used'.
290 for (tmpdev
= devlist
;
292 tmpdev
= tmpdev
? tmpdev
->next
: NULL
) {
293 char *devname
= tmpdev
->devname
;
296 struct supertype
*tst
;
297 struct dev_policy
*pol
= NULL
;
298 int found_container
= 0;
300 if (tmpdev
->used
> 1) continue;
302 if (ident
->devices
&&
303 !match_oneof(ident
->devices
, devname
)) {
304 if (report_missmatch
)
305 pr_err("%s is not one of %s\n", devname
, ident
->devices
);
311 dfd
= dev_open(devname
, O_RDONLY
);
313 if (report_missmatch
)
314 pr_err("cannot open device %s: %s\n",
315 devname
, strerror(errno
));
317 } else if (fstat(dfd
, &stb
)< 0) {
319 pr_err("fstat failed for %s: %s\n",
320 devname
, strerror(errno
));
322 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
323 pr_err("%s is not a block device.\n",
326 } else if (must_be_container(dfd
)) {
328 /* already found some components, this cannot
331 if (report_missmatch
)
332 pr_err("%s is a container, but we are looking for components\n",
335 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
336 } if (!tst
&& (tst
= super_by_fd(dfd
, NULL
)) == NULL
) {
337 if (report_missmatch
)
338 pr_err("not a recognisable container: %s\n",
342 } else if (!tst
->ss
->load_container
343 || tst
->ss
->load_container(tst
, dfd
, NULL
)) {
344 if (report_missmatch
)
345 pr_err("no correct container type: %s\n",
348 } else if (auto_assem
&&
349 !conf_test_metadata(tst
->ss
->name
, (pol
= devnum_policy(stb
.st_rdev
)),
350 tst
->ss
->match_home(tst
, homehost
) == 1)) {
351 if (report_missmatch
)
352 pr_err("%s has metadata type %s for which "
353 "auto-assembly is disabled\n",
354 devname
, tst
->ss
->name
);
359 if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
360 if (report_missmatch
)
361 pr_err("no recogniseable superblock on %s\n",
364 } else if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
365 if (report_missmatch
)
366 pr_err("no RAID superblock on %s\n",
369 } else if (tst
->ss
->compare_super
== NULL
) {
370 if (report_missmatch
)
371 pr_err("Cannot assemble %s metadata on %s\n",
372 tst
->ss
->name
, devname
);
374 } else if (auto_assem
&& st
== NULL
&&
375 !conf_test_metadata(tst
->ss
->name
, (pol
= devnum_policy(stb
.st_rdev
)),
376 tst
->ss
->match_home(tst
, homehost
) == 1)) {
377 if (report_missmatch
)
378 pr_err("%s has metadata type %s for which "
379 "auto-assembly is disabled\n",
380 devname
, tst
->ss
->name
);
384 if (dfd
>= 0) close(dfd
);
385 if (tmpdev
->used
== 2) {
386 if (auto_assem
|| !inargv
)
387 /* Ignore unrecognised devices during auto-assembly */
389 if (ident
->uuid_set
|| ident
->name
[0] ||
390 ident
->super_minor
!= UnSet
)
391 /* Ignore unrecognised device if looking for
396 pr_err("%s has no superblock - assembly aborted\n",
399 st
->ss
->free_super(st
);
400 dev_policy_free(pol
);
401 domain_free(domains
);
405 if (found_container
) {
406 /* tmpdev is a container. We need to be either
407 * looking for a member, or auto-assembling
409 /* should be safe to try an exclusive open now, we
410 * have rejected anything that some other mdadm might
413 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
415 if (report_missmatch
)
416 pr_err("%s is busy - skipping\n", devname
);
421 if (ident
->container
) {
422 if (ident
->container
[0] == '/' &&
423 !same_dev(ident
->container
, devname
)) {
424 if (report_missmatch
)
425 pr_err("%s is not the container required (%s)\n",
426 devname
, ident
->container
);
429 if (ident
->container
[0] != '/') {
434 tst
->ss
->getinfo_super(tst
, content
, NULL
);
436 if (!parse_uuid(ident
->container
, uuid
) ||
437 !same_uuid(content
->uuid
, uuid
, tst
->ss
->swapuuid
)) {
438 if (report_missmatch
)
439 pr_err("%s has wrong UUID to be required container\n",
445 /* It is worth looking inside this container.
448 pr_err("looking in container %s\n",
451 for (content
= tst
->ss
->container_content(tst
, NULL
);
453 content
= content
->next
) {
455 if (!ident_matches(ident
, content
, tst
,
457 report_missmatch
? devname
: NULL
))
458 /* message already printed */;
459 else if (is_member_busy(content
->text_version
)) {
460 if (report_missmatch
)
461 pr_err("member %s in %s is already assembled\n",
462 content
->text_version
,
464 } else if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
465 /* do not assemble arrays with unsupported configurations */
466 pr_err("Cannot activate member %s in %s.\n",
467 content
->text_version
,
474 goto loop
; /* empty container */
477 st
= tst
; tst
= NULL
;
478 if (!auto_assem
&& inargv
&& tmpdev
->next
!= NULL
) {
479 pr_err("%s is a container, but is not "
480 "only device given: confused and aborting\n",
482 st
->ss
->free_super(st
);
483 dev_policy_free(pol
);
484 domain_free(domains
);
488 pr_err("found match on member %s in %s\n",
489 content
->text_version
, devname
);
491 /* make sure we finished the loop */
497 tst
->ss
->getinfo_super(tst
, content
, NULL
);
499 if (!ident_matches(ident
, content
, tst
,
501 report_missmatch
? devname
: NULL
))
504 /* should be safe to try an exclusive open now, we
505 * have rejected anything that some other mdadm might
508 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
510 if (report_missmatch
)
511 pr_err("%s is busy - skipping\n", devname
);
518 if (st
->minor_version
== -1)
519 st
->minor_version
= tst
->minor_version
;
521 if (memcmp(content
->uuid
, uuid_zero
,
522 sizeof(int[4])) == 0) {
523 /* this is a floating spare. It cannot define
524 * an array unless there are no more arrays of
525 * this type to be found. It can be included
526 * in an array of this type though.
532 if (st
->ss
!= tst
->ss
||
533 st
->minor_version
!= tst
->minor_version
||
534 st
->ss
->compare_super(st
, tst
) != 0) {
535 /* Some mismatch. If exactly one array matches this host,
536 * we can resolve on that one.
537 * Or, if we are auto assembling, we just ignore the second
543 int first
= st
->ss
->match_home(st
, homehost
);
544 int last
= tst
->ss
->match_home(tst
, homehost
);
546 (first
== 1 || last
== 1)) {
547 /* We can do something */
548 if (first
) {/* just ignore this one */
549 if (report_missmatch
)
550 pr_err("%s misses out due to wrong homehost\n",
553 } else { /* reject all those sofar */
554 struct mddev_dev
*td
;
555 if (report_missmatch
)
556 pr_err("%s overrides previous devices due to good homehost\n",
558 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
566 pr_err("superblock on %s doesn't match others - assembly aborted\n",
568 tst
->ss
->free_super(tst
);
569 st
->ss
->free_super(st
);
570 dev_policy_free(pol
);
571 domain_free(domains
);
577 /* Collect domain information from members only */
578 if (tmpdev
&& tmpdev
->used
== 1) {
580 pol
= devnum_policy(stb
.st_rdev
);
581 domain_merge(&domains
, pol
, tst
?tst
->ss
->name
:NULL
);
583 dev_policy_free(pol
);
586 tst
->ss
->free_super(tst
);
589 /* Check if we found some imsm spares but no members */
592 memcmp(uuid_zero
, ident
->uuid
,sizeof(uuid_zero
)) == 0)) &&
594 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
595 if (tmpdev
->used
!= 3)
601 /* we need sb from one of the spares */
602 int dfd
= dev_open(tmpdev
->devname
, O_RDONLY
);
604 st
->ss
->load_super(st
, dfd
, NULL
))
611 /* Now reject spares that don't match domains of identified members */
612 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
614 if (tmpdev
->used
!= 3)
616 if (stat(tmpdev
->devname
, &stb
)< 0) {
617 pr_err("fstat failed for %s: %s\n",
618 tmpdev
->devname
, strerror(errno
));
621 struct dev_policy
*pol
= devnum_policy(stb
.st_rdev
);
622 int dt
= domain_test(domains
, pol
, NULL
);
623 if (inargv
&& dt
!= 0)
624 /* take this spare as domains match
625 * if there are any */
627 else if (!inargv
&& dt
== 1)
628 /* device wasn't explicitly listed, so need
629 * explicit domain match - which we have */
632 /* if domains don't match mark as unused */
634 dev_policy_free(pol
);
637 domain_free(domains
);
639 if (!st
|| !st
->sb
|| !content
)
642 /* Now need to open the array device. Use create_mddev */
643 if (content
== &info
)
644 st
->ss
->getinfo_super(st
, content
, NULL
);
646 trustworthy
= FOREIGN
;
647 name
= content
->name
;
648 switch (st
->ss
->match_home(st
, homehost
)
649 ?: st
->ss
->match_home(st
, "any")) {
652 name
= strchr(content
->name
, ':');
656 name
= content
->name
;
660 /* If the array is listed in mdadm.conf or on
661 * command line, then we trust the name
662 * even if the array doesn't look local
667 content
->array
.level
== LEVEL_CONTAINER
) {
668 name
= content
->text_version
;
669 trustworthy
= METADATA
;
672 if (name
[0] && trustworthy
!= LOCAL
&&
673 ! require_homehost
&&
674 conf_name_is_free(name
))
677 if (trustworthy
== LOCAL
&&
679 /* Ignore 'host:' prefix of name */
680 name
= strchr(name
, ':')+1;
682 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
685 st
->ss
->free_super(st
);
691 vers
= md_get_version(mdfd
);
693 pr_err("Assemble requires driver version 0.90.0 or later.\n"
694 " Upgrade your kernel or try --build\n");
698 if (mddev_busy(fd2devnum(mdfd
))) {
699 pr_err("%s already active, cannot restart it!\n",
701 for (tmpdev
= devlist
;
702 tmpdev
&& tmpdev
->used
!= 1;
703 tmpdev
= tmpdev
->next
)
705 if (tmpdev
&& auto_assem
)
706 pr_err("%s needed for %s...\n",
707 mddev
, tmpdev
->devname
);
710 st
->ss
->free_super(st
);
715 ioctl(mdfd
, STOP_ARRAY
, NULL
); /* just incase it was started but has no content */
718 if (content
!= &info
) {
719 /* This is a member of a container. Try starting the array. */
721 err
= assemble_container_content(st
, mdfd
, content
, runstop
,
722 chosen_name
, verbose
,
723 backup_file
, freeze_reshape
);
729 /* Ok, no bad inconsistancy, we can try updating etc */
730 devices
= malloc(num_devs
* sizeof(*devices
));
731 devmap
= calloc(num_devs
* content
->array
.raid_disks
, 1);
732 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) if (tmpdev
->used
== 1) {
733 char *devname
= tmpdev
->devname
;
735 /* looks like a good enough match to update the super block if needed */
739 /* prepare useful information in info structures */
741 struct supertype
*tst
;
745 if (strcmp(update
, "uuid")==0 &&
748 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
749 read(rfd
, ident
->uuid
, 16) != 16) {
750 *(__u32
*)(ident
->uuid
) = random();
751 *(__u32
*)(ident
->uuid
+1) = random();
752 *(__u32
*)(ident
->uuid
+2) = random();
753 *(__u32
*)(ident
->uuid
+3) = random();
755 if (rfd
>= 0) close(rfd
);
757 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
760 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
761 pr_err("cannot re-read metadata from %s - aborting\n",
770 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
772 memcpy(content
->uuid
, ident
->uuid
, 16);
773 strcpy(content
->name
, ident
->name
);
774 content
->array
.md_minor
= minor(stb2
.st_rdev
);
776 if (strcmp(update
, "byteorder") == 0)
779 err
= tst
->ss
->update_super(tst
, content
, update
,
784 pr_err("--update=%s not understood"
785 " for %s metadata\n",
786 update
, tst
->ss
->name
);
787 tst
->ss
->free_super(tst
);
795 if (strcmp(update
, "uuid")==0 &&
798 memcpy(ident
->uuid
, content
->uuid
, 16);
800 if (tst
->ss
->store_super(tst
, dfd
))
801 pr_err("Could not re-write superblock on %s.\n",
805 if (strcmp(update
, "uuid")==0 &&
806 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
807 if (bitmap_update_uuid(ident
->bitmap_fd
,
809 tst
->ss
->swapuuid
) != 0)
810 pr_err("Could not update uuid on external bitmap.\n");
814 tst
->ss
->free_super(tst
);
818 struct supertype
*tst
= dup_super(st
);
820 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
822 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
823 pr_err("cannot re-read metadata from %s - aborting\n",
832 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
833 tst
->ss
->free_super(tst
);
840 pr_err("%s is identified as a member of %s, slot %d.\n",
841 devname
, mddev
, content
->disk
.raid_disk
);
842 devices
[devcnt
].devname
= devname
;
843 devices
[devcnt
].uptodate
= 0;
844 devices
[devcnt
].i
= *content
;
845 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
846 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
847 if (most_recent
< devcnt
) {
848 if (devices
[devcnt
].i
.events
849 > devices
[most_recent
].i
.events
)
850 most_recent
= devcnt
;
852 if (content
->array
.level
== LEVEL_MULTIPATH
)
853 /* with multipath, the raid_disk from the superblock is meaningless */
856 i
= devices
[devcnt
].i
.disk
.raid_disk
;
858 if (nextspare
< content
->array
.raid_disks
)
859 nextspare
= content
->array
.raid_disks
;
862 if (i
>= content
->array
.raid_disks
&&
868 int newbestcnt
= i
+10;
869 int *newbest
= malloc(sizeof(int)*newbestcnt
);
871 for (c
=0; c
< newbestcnt
; c
++)
873 newbest
[c
] = best
[c
];
878 bestcnt
= newbestcnt
;
881 devices
[best
[i
]].i
.events
882 == devices
[devcnt
].i
.events
883 && (devices
[best
[i
]].i
.disk
.minor
884 != devices
[devcnt
].i
.disk
.minor
)
886 && content
->array
.level
!= LEVEL_MULTIPATH
) {
887 /* two different devices with identical superblock.
888 * Could be a mis-detection caused by overlapping
889 * partitions. fail-safe.
891 pr_err("WARNING %s and %s appear"
892 " to have very similar superblocks.\n"
893 " If they are really different, "
894 "please --zero the superblock on one\n"
895 " If they are the same or overlap,"
896 " please remove one from %s.\n",
897 devices
[best
[i
]].devname
, devname
,
898 inargv
? "the list" :
899 "the\n DEVICE list in mdadm.conf"
907 || (devices
[best
[i
]].i
.events
908 < devices
[devcnt
].i
.events
))
915 pr_err("no devices found for %s\n",
918 st
->ss
->free_super(st
);
925 if (update
&& strcmp(update
, "byteorder")==0)
926 st
->minor_version
= 90;
928 st
->ss
->getinfo_super(st
, content
, NULL
);
929 clean
= content
->array
.state
& 1;
931 /* now we have some devices that might be suitable.
934 avail
= malloc(content
->array
.raid_disks
);
935 memset(avail
, 0, content
->array
.raid_disks
);
939 for (i
=0; i
< bestcnt
; i
++) {
941 int event_margin
= 1; /* always allow a difference of '1'
942 * like the kernel does
945 /* note: we ignore error flags in multipath arrays
946 * as they don't make sense
948 if (content
->array
.level
!= LEVEL_MULTIPATH
)
949 if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_ACTIVE
))) {
950 if (!(devices
[j
].i
.disk
.state
951 & (1<<MD_DISK_FAULTY
))) {
952 devices
[j
].uptodate
= 1;
957 /* If this device thinks that 'most_recent' has failed, then
958 * we must reject this device.
960 if (j
!= most_recent
&&
961 content
->array
.raid_disks
> 0 &&
962 devices
[most_recent
].i
.disk
.raid_disk
>= 0 &&
963 devmap
[j
* content
->array
.raid_disks
+ devices
[most_recent
].i
.disk
.raid_disk
] == 0) {
965 pr_err("ignoring %s as it reports %s as failed\n",
966 devices
[j
].devname
, devices
[most_recent
].devname
);
970 if (devices
[j
].i
.events
+event_margin
>=
971 devices
[most_recent
].i
.events
) {
972 devices
[j
].uptodate
= 1;
973 if (i
< content
->array
.raid_disks
) {
974 if (devices
[j
].i
.recovery_start
== MaxSector
||
975 (content
->reshape_active
&&
976 ((i
>= content
->array
.raid_disks
- content
->delta_disks
) ||
977 (i
>= content
->array
.raid_disks
- content
->delta_disks
- 1
978 && content
->array
.level
== 4)))) {
989 (!enough(content
->array
.level
, content
->array
.raid_disks
,
990 content
->array
.layout
, 1,
993 (content
->reshape_active
&& content
->delta_disks
> 0 &&
994 !enough(content
->array
.level
, (content
->array
.raid_disks
995 - content
->delta_disks
),
996 content
->new_layout
, 1,
999 /* Choose the newest best drive which is
1000 * not up-to-date, update the superblock
1004 struct supertype
*tst
;
1005 unsigned long long current_events
;
1007 for (i
= 0; i
< content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
1010 !devices
[j
].uptodate
&&
1011 devices
[j
].i
.recovery_start
== MaxSector
&&
1012 (chosen_drive
< 0 ||
1014 > devices
[chosen_drive
].i
.events
))
1017 if (chosen_drive
< 0)
1019 current_events
= devices
[chosen_drive
].i
.events
;
1022 pr_err("forcing event count in %s(%d) from %d upto %d\n",
1023 devices
[chosen_drive
].devname
,
1024 devices
[chosen_drive
].i
.disk
.raid_disk
,
1025 (int)(devices
[chosen_drive
].i
.events
),
1026 (int)(devices
[most_recent
].i
.events
));
1027 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
1029 pr_err("Couldn't open %s for write - not updating\n",
1030 devices
[chosen_drive
].devname
);
1031 devices
[chosen_drive
].i
.events
= 0;
1034 tst
= dup_super(st
);
1035 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
1037 pr_err("RAID superblock disappeared from %s - not updating.\n",
1038 devices
[chosen_drive
].devname
);
1039 devices
[chosen_drive
].i
.events
= 0;
1042 content
->events
= devices
[most_recent
].i
.events
;
1043 tst
->ss
->update_super(tst
, content
, "force-one",
1044 devices
[chosen_drive
].devname
, verbose
,
1047 if (tst
->ss
->store_super(tst
, fd
)) {
1049 pr_err("Could not re-write superblock on %s\n",
1050 devices
[chosen_drive
].devname
);
1051 devices
[chosen_drive
].i
.events
= 0;
1052 tst
->ss
->free_super(tst
);
1056 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
1057 devices
[chosen_drive
].uptodate
= 1;
1058 avail
[chosen_drive
] = 1;
1060 tst
->ss
->free_super(tst
);
1062 /* If there are any other drives of the same vintage,
1063 * add them in as well. We can't lose and we might gain
1065 for (i
= 0; i
< content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
1068 !devices
[j
].uptodate
&&
1069 devices
[j
].i
.recovery_start
== MaxSector
&&
1070 devices
[j
].i
.events
== current_events
) {
1077 /* Now we want to look at the superblock which the kernel will base things on
1078 * and compare the devices that we think are working with the devices that the
1079 * superblock thinks are working.
1080 * If there are differences and --force is given, then update this chosen
1084 st
->ss
->free_super(st
);
1085 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
++) {
1091 if (!devices
[j
].uptodate
)
1093 if (devices
[j
].i
.events
< devices
[most_recent
].i
.events
)
1096 if ((fd
=dev_open(devices
[j
].devname
, O_RDONLY
|O_EXCL
))< 0) {
1097 pr_err("Cannot open %s: %s\n",
1098 devices
[j
].devname
, strerror(errno
));
1103 if (st
->ss
->load_super(st
,fd
, NULL
)) {
1105 pr_err("RAID superblock has disappeared from %s\n",
1106 devices
[j
].devname
);
1113 if (st
->sb
== NULL
) {
1114 pr_err("No suitable drives found for %s\n", mddev
);
1119 st
->ss
->getinfo_super(st
, content
, NULL
);
1121 sysfs_init(content
, mdfd
, 0);
1123 for (i
=0; i
<bestcnt
; i
++) {
1125 unsigned int desired_state
;
1127 if (i
< content
->array
.raid_disks
)
1128 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
1134 if (!devices
[j
].uptodate
)
1137 devices
[j
].i
.disk
.state
= desired_state
;
1138 if (!(devices
[j
].i
.array
.state
& 1))
1141 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
1142 verbose
, 0, NULL
)) {
1145 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
1146 j
, mddev
, devices
[j
].devname
);
1150 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
1151 i
, mddev
, devices
[j
].devname
);
1155 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
1156 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1161 if (force
&& !clean
&&
1162 !enough(content
->array
.level
, content
->array
.raid_disks
,
1163 content
->array
.layout
, clean
,
1165 change
+= st
->ss
->update_super(st
, content
, "force-array",
1166 devices
[chosen_drive
].devname
, verbose
,
1173 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
1175 pr_err("Could not open %s for write - cannot Assemble array.\n",
1176 devices
[chosen_drive
].devname
);
1181 if (st
->ss
->store_super(st
, fd
)) {
1183 pr_err("Could not re-write superblock on %s\n",
1184 devices
[chosen_drive
].devname
);
1190 pr_err("Marking array %s as 'clean'\n",
1195 /* If we are in the middle of a reshape we may need to restore saved data
1196 * that was moved aside due to the reshape overwriting live data
1197 * The code of doing this lives in Grow.c
1200 if (content
->reshape_active
) {
1202 int *fdlist
= malloc(sizeof(int)* bestcnt
);
1204 pr_err(":%s has an active reshape - checking "
1205 "if critical section needs to be restored\n",
1207 for (i
=0; i
<bestcnt
; i
++) {
1210 fdlist
[i
] = dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
1211 if (fdlist
[i
] < 0) {
1212 pr_err("Could not open %s for write - cannot Assemble array.\n",
1213 devices
[j
].devname
);
1221 if (st
->ss
->external
&& st
->ss
->recover_backup
)
1222 err
= st
->ss
->recover_backup(st
, content
);
1224 err
= Grow_restart(st
, content
, fdlist
, bestcnt
,
1225 backup_file
, verbose
> 0);
1226 if (err
&& invalid_backup
) {
1229 " without restoring backup\n");
1235 if (fdlist
[i
]>=0) close(fdlist
[i
]);
1238 pr_err("Failed to restore critical section for reshape, sorry.\n");
1239 if (backup_file
== NULL
)
1240 cont_err("Possibly you needed to specify the --backup-file\n");
1247 /* count number of in-sync devices according to the superblock.
1248 * We must have this number to start the array without -s or -R
1250 req_cnt
= content
->array
.working_disks
;
1252 /* Almost ready to actually *do* something */
1256 /* First, fill in the map, so that udev can find our name
1257 * as soon as we become active.
1259 map_update(NULL
, fd2devnum(mdfd
), content
->text_version
,
1260 content
->uuid
, chosen_name
);
1262 rv
= set_array_info(mdfd
, st
, content
);
1264 pr_err("failed to set array info for %s: %s\n",
1265 mddev
, strerror(errno
));
1266 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1271 if (ident
->bitmap_fd
>= 0) {
1272 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
1273 pr_err("SET_BITMAP_FILE failed.\n");
1274 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1279 } else if (ident
->bitmap_file
) {
1280 /* From config file */
1281 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
1283 pr_err("Could not open bitmap file %s\n",
1284 ident
->bitmap_file
);
1285 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1290 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
1291 pr_err("Failed to set bitmapfile for %s\n", mddev
);
1293 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1301 /* First, add the raid disks, but add the chosen one last */
1302 for (i
=0; i
<= bestcnt
; i
++) {
1306 if (j
== chosen_drive
)
1311 if (j
>= 0 /* && devices[j].uptodate */) {
1312 int dfd
= dev_open(devices
[j
].devname
,
1315 remove_partitions(dfd
);
1318 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
1321 pr_err("failed to add "
1326 if (i
< content
->array
.raid_disks
1331 } else if (verbose
> 0)
1332 pr_err("added %s to %s as %d%s\n",
1333 devices
[j
].devname
, mddev
,
1334 devices
[j
].i
.disk
.raid_disk
,
1335 devices
[j
].uptodate
?"":
1336 " (possibly out of date)");
1337 } else if (verbose
> 0 && i
< content
->array
.raid_disks
)
1338 pr_err("no uptodate device for "
1343 if (content
->array
.level
== LEVEL_CONTAINER
) {
1345 pr_err("Container %s has been "
1346 "assembled with %d drive%s",
1347 mddev
, okcnt
+sparecnt
, okcnt
+sparecnt
==1?"":"s");
1348 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1349 fprintf(stderr
, " (out of %d)",
1350 content
->array
.raid_disks
);
1351 fprintf(stderr
, "\n");
1353 st
->ss
->free_super(st
);
1354 sysfs_uevent(content
, "change");
1355 wait_for(chosen_name
, mdfd
);
1363 ( enough(content
->array
.level
, content
->array
.raid_disks
,
1364 content
->array
.layout
, clean
, avail
) &&
1365 (okcnt
+ rebuilding_cnt
>= req_cnt
|| start_partial_ok
)
1367 /* This array is good-to-go.
1368 * If a reshape is in progress then we might need to
1369 * continue monitoring it. In that case we start
1370 * it read-only and let the grow code make it writable.
1374 if (content
->reshape_active
&&
1375 content
->delta_disks
<= 0) {
1376 rv
= sysfs_set_str(content
, NULL
,
1377 "array_state", "readonly");
1379 rv
= Grow_continue(mdfd
, st
, content
,
1384 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1387 pr_err("%s has been started with %d drive%s",
1388 mddev
, okcnt
, okcnt
==1?"":"s");
1389 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1390 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1392 fprintf(stderr
, "%s %d rebuilding", sparecnt
?",":" and", rebuilding_cnt
);
1394 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1395 fprintf(stderr
, ".\n");
1397 if (content
->reshape_active
&&
1398 content
->array
.level
>= 4 &&
1399 content
->array
.level
<= 6) {
1400 /* might need to increase the size
1401 * of the stripe cache - default is 256
1403 if (256 < 4 * (content
->array
.chunk_size
/4096)) {
1404 struct mdinfo
*sra
= sysfs_read(mdfd
, 0, 0);
1406 sysfs_set_num(sra
, NULL
,
1407 "stripe_cache_size",
1408 (4 * content
->array
.chunk_size
/ 4096) + 1);
1412 if (okcnt
< (unsigned)content
->array
.raid_disks
) {
1413 /* If any devices did not get added
1414 * because the kernel rejected them based
1415 * on event count, try adding them
1416 * again providing the action policy is
1417 * 're-add' or greater. The bitmap
1418 * might allow them to be included, or
1419 * they will become spares.
1421 for (i
= 0; i
< bestcnt
; i
++) {
1423 if (j
>= 0 && !devices
[j
].uptodate
) {
1424 if (!disk_action_allows(&devices
[j
].i
, st
->ss
->name
, act_re_add
))
1426 rv
= add_disk(mdfd
, st
, content
,
1428 if (rv
== 0 && verbose
>= 0)
1429 pr_err("%s has been re-added.\n",
1430 devices
[j
].devname
);
1434 wait_for(mddev
, mdfd
);
1438 /* There is a nasty race with 'mdadm --monitor'.
1439 * If it opens this device before we close it,
1440 * it gets an incomplete open on which IO
1441 * doesn't work and the capacity is
1443 * If we reopen (to check for layered devices)
1444 * before --monitor closes, we loose.
1446 * So: wait upto 1 second for there to be
1447 * a non-zero capacity.
1449 while (usecs
< 1000) {
1450 mdfd
= open(mddev
, O_RDONLY
);
1452 unsigned long long size
;
1453 if (get_dev_size(mdfd
, NULL
, &size
) &&
1465 pr_err("failed to RUN_ARRAY %s: %s\n",
1466 mddev
, strerror(errno
));
1468 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1469 content
->array
.layout
, 1, avail
))
1470 pr_err("Not enough devices to "
1471 "start the array.\n");
1472 else if (!enough(content
->array
.level
,
1473 content
->array
.raid_disks
,
1474 content
->array
.layout
, clean
,
1476 pr_err("Not enough devices to "
1477 "start the array while not clean "
1478 "- consider --force.\n");
1481 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1486 if (runstop
== -1) {
1487 pr_err("%s assembled from %d drive%s",
1488 mddev
, okcnt
, okcnt
==1?"":"s");
1489 if (okcnt
!= (unsigned)content
->array
.raid_disks
)
1490 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1491 fprintf(stderr
, ", but not started.\n");
1496 if (verbose
>= -1) {
1497 pr_err("%s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
1499 fprintf(stderr
, "%s %d rebuilding", sparecnt
?", ":" and ", rebuilding_cnt
);
1501 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1502 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1503 content
->array
.layout
, 1, avail
))
1504 fprintf(stderr
, " - not enough to start the array.\n");
1505 else if (!enough(content
->array
.level
,
1506 content
->array
.raid_disks
,
1507 content
->array
.layout
, clean
,
1509 fprintf(stderr
, " - not enough to start the "
1510 "array while not clean - consider "
1513 if (req_cnt
== (unsigned)content
->array
.raid_disks
)
1514 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1516 fprintf(stderr
, " - need %d of %d to start", req_cnt
, content
->array
.raid_disks
);
1517 fprintf(stderr
, " (use --run to insist).\n");
1521 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1526 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1527 * been updated to point to the current locations of devices.
1528 * so we can just start the array
1531 dev
= makedev(devices
[chosen_drive
].i
.disk
.major
,
1532 devices
[chosen_drive
].i
.disk
.minor
);
1533 if (ioctl(mdfd
, START_ARRAY
, dev
)) {
1534 pr_err("Cannot start array: %s\n",
1545 int assemble_container_content(struct supertype
*st
, int mdfd
,
1546 struct mdinfo
*content
, int runstop
,
1547 char *chosen_name
, int verbose
,
1548 char *backup_file
, int freeze_reshape
)
1550 struct mdinfo
*dev
, *sra
;
1551 int working
= 0, preexist
= 0;
1553 struct map_ent
*map
= NULL
;
1557 sysfs_init(content
, mdfd
, 0);
1559 sra
= sysfs_read(mdfd
, 0, GET_VERSION
);
1560 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0)
1561 if (sysfs_set_array(content
, md_get_version(mdfd
)) != 0) {
1567 /* There are two types of reshape: container wide or sub-array specific
1568 * Check if metadata requests blocking container wide reshapes
1570 start_reshape
= (content
->reshape_active
&&
1571 !((content
->reshape_active
== CONTAINER_RESHAPE
) &&
1572 (content
->array
.state
& (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))));
1574 /* Block subarray here if it is under reshape now
1575 * Do not allow for any changes in this array
1577 if (st
->ss
->external
&& content
->recovery_blocked
&& start_reshape
)
1578 block_subarray(content
);
1582 old_raid_disks
= content
->array
.raid_disks
- content
->delta_disks
;
1583 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
1584 if (sysfs_add_disk(content
, dev
, 1) == 0) {
1585 if (dev
->disk
.raid_disk
>= old_raid_disks
&&
1586 content
->reshape_active
)
1590 } else if (errno
== EEXIST
)
1592 if (working
+ expansion
== 0)
1593 return 1;/* Nothing new, don't try to start */
1595 map_update(&map
, fd2devnum(mdfd
),
1596 content
->text_version
,
1597 content
->uuid
, chosen_name
);
1600 (working
+ preexist
+ expansion
) >=
1601 content
->array
.working_disks
) {
1604 if (start_reshape
) {
1605 int spare
= content
->array
.raid_disks
+ expansion
;
1606 if (restore_backup(st
, content
,
1608 spare
, backup_file
, verbose
) == 1)
1611 err
= sysfs_set_str(content
, NULL
,
1612 "array_state", "readonly");
1616 if (st
->ss
->external
) {
1617 if (!mdmon_running(st
->container_dev
))
1618 start_mdmon(st
->container_dev
);
1619 ping_monitor_by_id(st
->container_dev
);
1620 if (mdmon_running(st
->container_dev
) &&
1621 st
->update_tail
== NULL
)
1622 st
->update_tail
= &st
->updates
;
1625 err
= Grow_continue(mdfd
, st
, content
, backup_file
,
1627 } else switch(content
->array
.level
) {
1629 case LEVEL_MULTIPATH
:
1631 err
= sysfs_set_str(content
, NULL
, "array_state",
1635 err
= sysfs_set_str(content
, NULL
, "array_state",
1637 /* start mdmon if needed. */
1639 if (!mdmon_running(st
->container_dev
))
1640 start_mdmon(st
->container_dev
);
1641 ping_monitor_by_id(st
->container_dev
);
1646 sysfs_set_safemode(content
, content
->safe_mode_delay
);
1648 /* Block subarray here if it is not reshaped now
1649 * It has be blocked a little later to allow mdmon to switch in
1652 if (st
->ss
->external
&& content
->recovery_blocked
&&
1654 block_subarray(content
);
1658 pr_err("array %s now has %d device%s",
1659 chosen_name
, working
+ preexist
,
1660 working
+ preexist
== 1 ? "":"s");
1662 pr_err("Started %s with %d device%s",
1663 chosen_name
, working
+ preexist
,
1664 working
+ preexist
== 1 ? "":"s");
1666 fprintf(stderr
, " (%d new)", working
);
1668 fprintf(stderr
, " ( + %d for expansion)",
1670 fprintf(stderr
, "\n");
1673 wait_for(chosen_name
, mdfd
);
1675 /* FIXME should have an O_EXCL and wait for read-auto */
1678 pr_err("%s assembled with %d device%s",
1679 chosen_name
, preexist
+ working
,
1680 preexist
+ working
== 1 ? "":"s");
1682 fprintf(stderr
, " (%d new)", working
);
1683 fprintf(stderr
, " but not started\n");