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
,
139 * The task of Assemble is to find a collection of
140 * devices that should (according to their superblocks)
141 * form an array, and to give this collection to the MD driver.
142 * In Linux-2.4 and later, this involves submitting a
143 * SET_ARRAY_INFO ioctl with no arg - to prepare
144 * the array - and then submit a number of
145 * ADD_NEW_DISK ioctls to add disks into
146 * the array. Finally RUN_ARRAY might
147 * be submitted to start the array.
149 * Much of the work of Assemble is in finding and/or
150 * checking the disks to make sure they look right.
152 * If mddev is not set, then scan must be set and we
153 * read through the config file for dev+uuid mapping
154 * We recurse, setting mddev, for each device that
156 * - has a valid uuid (or any uuid if !uuidset)
158 * If mddev is set, we try to determine state of md.
159 * check version - must be at least 0.90.0
160 * check kernel version. must be at least 2.4.
161 * If not, we can possibly fall back on START_ARRAY
162 * Try to GET_ARRAY_INFO.
163 * If possible, give up
164 * If not, try to STOP_ARRAY just to make sure
166 * If !uuidset and scan, look in conf-file for uuid
167 * If not found, give up
168 * If !devlist and scan and uuidset, get list of devs from conf-file
171 * Check superblock - discard if bad
172 * Check uuid (set if we don't have one) - discard if no match
173 * Check superblock similarity if we have a superblock - discard if different
174 * Record events, devicenum
175 * This should give us a list of devices for the array
176 * We should collect the most recent event number
178 * Count disks with recent enough event count
179 * While force && !enough disks
180 * Choose newest rejected disks, update event count
181 * mark clean and rewrite superblock
184 * foreach device with recent events : ADD_NEW_DISK
185 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
187 * Check the device numbers in superblock are right
188 * update superblock if any changes
194 int auto_assem
= (mddev
== NULL
&& !ident
->uuid_set
&&
195 ident
->super_minor
== UnSet
&& ident
->name
[0] == 0
196 && (ident
->container
== NULL
|| ident
->member
== NULL
));
198 int vers
= vers
; /* Keep gcc quite - it really is initialised */
201 int uptodate
; /* set once we decide that this device is as
202 * recent as everything else in the array.
207 int *best
= NULL
; /* indexed by raid_disk */
210 unsigned int okcnt
, sparecnt
, rebuilding_cnt
;
211 unsigned int req_cnt
;
217 int report_missmatch
;
221 int start_partial_ok
= (c
->runstop
>= 0) &&
222 (c
->force
|| devlist
==NULL
|| auto_assem
);
223 unsigned int num_devs
;
224 struct mddev_dev
*tmpdev
;
226 struct mdinfo
*content
= NULL
;
231 char chosen_name
[1024];
232 struct domainlist
*domains
= NULL
;
234 if (get_linux_version() < 2004000)
238 * If any subdevs are listed, then any that don't
239 * match ident are discarded. Remainder must all match and
241 * If no subdevs, then we scan all devices in the config file, but
242 * there must be something in the identity
246 ident
->uuid_set
== 0 &&
247 (ident
->super_minor
< 0 || ident
->super_minor
== UnSet
) &&
248 ident
->name
[0] == 0 &&
249 (ident
->container
== NULL
|| ident
->member
== NULL
) &&
250 ident
->devices
== NULL
) {
251 pr_err("No identity information available for %s - cannot assemble.\n",
252 mddev
? mddev
: "further assembly");
257 devlist
= conf_get_devs();
261 report_missmatch
= ((inargv
&& c
->verbose
>= 0) || c
->verbose
> 0);
263 /* We come back here when doing auto-assembly and attempting some
264 * set of devices failed. Those are now marked as ->used==2 and
265 * we ignore them and try again
268 tmpdev
= devlist
; num_devs
= 0;
274 tmpdev
= tmpdev
->next
;
277 if (!st
&& ident
->st
) st
= ident
->st
;
280 pr_err("looking for devices for %s\n",
281 mddev
? mddev
: "further assembly");
283 /* first walk the list of devices to find a consistent set
284 * that match the criterea, if that is possible.
285 * We flag the ones we like with 'used'.
287 for (tmpdev
= devlist
;
289 tmpdev
= tmpdev
? tmpdev
->next
: NULL
) {
290 char *devname
= tmpdev
->devname
;
293 struct supertype
*tst
;
294 struct dev_policy
*pol
= NULL
;
295 int found_container
= 0;
297 if (tmpdev
->used
> 1) continue;
299 if (ident
->devices
&&
300 !match_oneof(ident
->devices
, devname
)) {
301 if (report_missmatch
)
302 pr_err("%s is not one of %s\n", devname
, ident
->devices
);
308 dfd
= dev_open(devname
, O_RDONLY
);
310 if (report_missmatch
)
311 pr_err("cannot open device %s: %s\n",
312 devname
, strerror(errno
));
314 } else if (fstat(dfd
, &stb
)< 0) {
316 pr_err("fstat failed for %s: %s\n",
317 devname
, strerror(errno
));
319 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
320 pr_err("%s is not a block device.\n",
323 } else if (must_be_container(dfd
)) {
325 /* already found some components, this cannot
328 if (report_missmatch
)
329 pr_err("%s is a container, but we are looking for components\n",
332 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
333 } if (!tst
&& (tst
= super_by_fd(dfd
, NULL
)) == NULL
) {
334 if (report_missmatch
)
335 pr_err("not a recognisable container: %s\n",
339 } else if (!tst
->ss
->load_container
340 || tst
->ss
->load_container(tst
, dfd
, NULL
)) {
341 if (report_missmatch
)
342 pr_err("no correct container type: %s\n",
345 } else if (auto_assem
&&
346 !conf_test_metadata(tst
->ss
->name
, (pol
= devnum_policy(stb
.st_rdev
)),
347 tst
->ss
->match_home(tst
, c
->homehost
) == 1)) {
348 if (report_missmatch
)
349 pr_err("%s has metadata type %s for which "
350 "auto-assembly is disabled\n",
351 devname
, tst
->ss
->name
);
356 if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
357 if (report_missmatch
)
358 pr_err("no recogniseable superblock on %s\n",
361 } else if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
362 if (report_missmatch
)
363 pr_err("no RAID superblock on %s\n",
366 } else if (tst
->ss
->compare_super
== NULL
) {
367 if (report_missmatch
)
368 pr_err("Cannot assemble %s metadata on %s\n",
369 tst
->ss
->name
, devname
);
371 } else if (auto_assem
&& st
== NULL
&&
372 !conf_test_metadata(tst
->ss
->name
, (pol
= devnum_policy(stb
.st_rdev
)),
373 tst
->ss
->match_home(tst
, c
->homehost
) == 1)) {
374 if (report_missmatch
)
375 pr_err("%s has metadata type %s for which "
376 "auto-assembly is disabled\n",
377 devname
, tst
->ss
->name
);
381 if (dfd
>= 0) close(dfd
);
382 if (tmpdev
->used
== 2) {
383 if (auto_assem
|| !inargv
)
384 /* Ignore unrecognised devices during auto-assembly */
386 if (ident
->uuid_set
|| ident
->name
[0] ||
387 ident
->super_minor
!= UnSet
)
388 /* Ignore unrecognised device if looking for
393 pr_err("%s has no superblock - assembly aborted\n",
396 st
->ss
->free_super(st
);
397 dev_policy_free(pol
);
398 domain_free(domains
);
402 if (found_container
) {
403 /* tmpdev is a container. We need to be either
404 * looking for a member, or auto-assembling
406 /* should be safe to try an exclusive open now, we
407 * have rejected anything that some other mdadm might
410 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
412 if (report_missmatch
)
413 pr_err("%s is busy - skipping\n", devname
);
418 if (ident
->container
) {
419 if (ident
->container
[0] == '/' &&
420 !same_dev(ident
->container
, devname
)) {
421 if (report_missmatch
)
422 pr_err("%s is not the container required (%s)\n",
423 devname
, ident
->container
);
426 if (ident
->container
[0] != '/') {
431 tst
->ss
->getinfo_super(tst
, content
, NULL
);
433 if (!parse_uuid(ident
->container
, uuid
) ||
434 !same_uuid(content
->uuid
, uuid
, tst
->ss
->swapuuid
)) {
435 if (report_missmatch
)
436 pr_err("%s has wrong UUID to be required container\n",
442 /* It is worth looking inside this container.
445 pr_err("looking in container %s\n",
448 for (content
= tst
->ss
->container_content(tst
, NULL
);
450 content
= content
->next
) {
452 if (!ident_matches(ident
, content
, tst
,
453 c
->homehost
, c
->update
,
454 report_missmatch
? devname
: NULL
))
455 /* message already printed */;
456 else if (is_member_busy(content
->text_version
)) {
457 if (report_missmatch
)
458 pr_err("member %s in %s is already assembled\n",
459 content
->text_version
,
461 } else if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
462 /* do not assemble arrays with unsupported configurations */
463 pr_err("Cannot activate member %s in %s.\n",
464 content
->text_version
,
471 goto loop
; /* empty container */
474 st
= tst
; tst
= NULL
;
475 if (!auto_assem
&& inargv
&& tmpdev
->next
!= NULL
) {
476 pr_err("%s is a container, but is not "
477 "only device given: confused and aborting\n",
479 st
->ss
->free_super(st
);
480 dev_policy_free(pol
);
481 domain_free(domains
);
485 pr_err("found match on member %s in %s\n",
486 content
->text_version
, devname
);
488 /* make sure we finished the loop */
494 tst
->ss
->getinfo_super(tst
, content
, NULL
);
496 if (!ident_matches(ident
, content
, tst
,
497 c
->homehost
, c
->update
,
498 report_missmatch
? devname
: NULL
))
501 /* should be safe to try an exclusive open now, we
502 * have rejected anything that some other mdadm might
505 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
507 if (report_missmatch
)
508 pr_err("%s is busy - skipping\n", devname
);
515 if (st
->minor_version
== -1)
516 st
->minor_version
= tst
->minor_version
;
518 if (memcmp(content
->uuid
, uuid_zero
,
519 sizeof(int[4])) == 0) {
520 /* this is a floating spare. It cannot define
521 * an array unless there are no more arrays of
522 * this type to be found. It can be included
523 * in an array of this type though.
529 if (st
->ss
!= tst
->ss
||
530 st
->minor_version
!= tst
->minor_version
||
531 st
->ss
->compare_super(st
, tst
) != 0) {
532 /* Some mismatch. If exactly one array matches this host,
533 * we can resolve on that one.
534 * Or, if we are auto assembling, we just ignore the second
540 int first
= st
->ss
->match_home(st
, c
->homehost
);
541 int last
= tst
->ss
->match_home(tst
, c
->homehost
);
543 (first
== 1 || last
== 1)) {
544 /* We can do something */
545 if (first
) {/* just ignore this one */
546 if (report_missmatch
)
547 pr_err("%s misses out due to wrong homehost\n",
550 } else { /* reject all those sofar */
551 struct mddev_dev
*td
;
552 if (report_missmatch
)
553 pr_err("%s overrides previous devices due to good homehost\n",
555 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
563 pr_err("superblock on %s doesn't match others - assembly aborted\n",
565 tst
->ss
->free_super(tst
);
566 st
->ss
->free_super(st
);
567 dev_policy_free(pol
);
568 domain_free(domains
);
574 /* Collect domain information from members only */
575 if (tmpdev
&& tmpdev
->used
== 1) {
577 pol
= devnum_policy(stb
.st_rdev
);
578 domain_merge(&domains
, pol
, tst
?tst
->ss
->name
:NULL
);
580 dev_policy_free(pol
);
583 tst
->ss
->free_super(tst
);
586 /* Check if we found some imsm spares but no members */
589 memcmp(uuid_zero
, ident
->uuid
,sizeof(uuid_zero
)) == 0)) &&
591 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
592 if (tmpdev
->used
!= 3)
598 /* we need sb from one of the spares */
599 int dfd
= dev_open(tmpdev
->devname
, O_RDONLY
);
601 st
->ss
->load_super(st
, dfd
, NULL
))
608 /* Now reject spares that don't match domains of identified members */
609 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
611 if (tmpdev
->used
!= 3)
613 if (stat(tmpdev
->devname
, &stb
)< 0) {
614 pr_err("fstat failed for %s: %s\n",
615 tmpdev
->devname
, strerror(errno
));
618 struct dev_policy
*pol
= devnum_policy(stb
.st_rdev
);
619 int dt
= domain_test(domains
, pol
, NULL
);
620 if (inargv
&& dt
!= 0)
621 /* take this spare as domains match
622 * if there are any */
624 else if (!inargv
&& dt
== 1)
625 /* device wasn't explicitly listed, so need
626 * explicit domain match - which we have */
629 /* if domains don't match mark as unused */
631 dev_policy_free(pol
);
634 domain_free(domains
);
636 if (!st
|| !st
->sb
|| !content
)
639 /* Now need to open the array device. Use create_mddev */
640 if (content
== &info
)
641 st
->ss
->getinfo_super(st
, content
, NULL
);
643 trustworthy
= FOREIGN
;
644 name
= content
->name
;
645 switch (st
->ss
->match_home(st
, c
->homehost
)
646 ?: st
->ss
->match_home(st
, "any")) {
649 name
= strchr(content
->name
, ':');
653 name
= content
->name
;
657 /* If the array is listed in mdadm.conf or on
658 * command line, then we trust the name
659 * even if the array doesn't look local
664 content
->array
.level
== LEVEL_CONTAINER
) {
665 name
= content
->text_version
;
666 trustworthy
= METADATA
;
669 if (name
[0] && trustworthy
!= LOCAL
&&
670 ! c
->require_homehost
&&
671 conf_name_is_free(name
))
674 if (trustworthy
== LOCAL
&&
676 /* Ignore 'host:' prefix of name */
677 name
= strchr(name
, ':')+1;
679 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
682 st
->ss
->free_super(st
);
688 vers
= md_get_version(mdfd
);
690 pr_err("Assemble requires driver version 0.90.0 or later.\n"
691 " Upgrade your kernel or try --build\n");
695 if (mddev_busy(fd2devnum(mdfd
))) {
696 pr_err("%s already active, cannot restart it!\n",
698 for (tmpdev
= devlist
;
699 tmpdev
&& tmpdev
->used
!= 1;
700 tmpdev
= tmpdev
->next
)
702 if (tmpdev
&& auto_assem
)
703 pr_err("%s needed for %s...\n",
704 mddev
, tmpdev
->devname
);
707 st
->ss
->free_super(st
);
712 ioctl(mdfd
, STOP_ARRAY
, NULL
); /* just incase it was started but has no content */
715 if (content
!= &info
) {
716 /* This is a member of a container. Try starting the array. */
718 err
= assemble_container_content(st
, mdfd
, content
, c
,
725 /* Ok, no bad inconsistancy, we can try updating etc */
726 devices
= xcalloc(num_devs
, sizeof(*devices
));
727 devmap
= xcalloc(num_devs
, content
->array
.raid_disks
);
728 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) if (tmpdev
->used
== 1) {
729 char *devname
= tmpdev
->devname
;
731 /* looks like a good enough match to update the super block if needed */
735 /* prepare useful information in info structures */
737 struct supertype
*tst
;
741 if (strcmp(c
->update
, "uuid")==0 &&
744 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
745 read(rfd
, ident
->uuid
, 16) != 16) {
746 *(__u32
*)(ident
->uuid
) = random();
747 *(__u32
*)(ident
->uuid
+1) = random();
748 *(__u32
*)(ident
->uuid
+2) = random();
749 *(__u32
*)(ident
->uuid
+3) = random();
751 if (rfd
>= 0) close(rfd
);
753 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
756 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
757 pr_err("cannot re-read metadata from %s - aborting\n",
766 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
768 memcpy(content
->uuid
, ident
->uuid
, 16);
769 strcpy(content
->name
, ident
->name
);
770 content
->array
.md_minor
= minor(stb2
.st_rdev
);
772 if (strcmp(c
->update
, "byteorder") == 0)
775 err
= tst
->ss
->update_super(tst
, content
, c
->update
,
780 pr_err("--update=%s not understood"
781 " for %s metadata\n",
782 c
->update
, tst
->ss
->name
);
783 tst
->ss
->free_super(tst
);
791 if (strcmp(c
->update
, "uuid")==0 &&
794 memcpy(ident
->uuid
, content
->uuid
, 16);
796 if (tst
->ss
->store_super(tst
, dfd
))
797 pr_err("Could not re-write superblock on %s.\n",
801 if (strcmp(c
->update
, "uuid")==0 &&
802 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
803 if (bitmap_update_uuid(ident
->bitmap_fd
,
805 tst
->ss
->swapuuid
) != 0)
806 pr_err("Could not update uuid on external bitmap.\n");
810 tst
->ss
->free_super(tst
);
814 struct supertype
*tst
= dup_super(st
);
816 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
818 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
819 pr_err("cannot re-read metadata from %s - aborting\n",
828 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
829 tst
->ss
->free_super(tst
);
836 pr_err("%s is identified as a member of %s, slot %d.\n",
837 devname
, mddev
, content
->disk
.raid_disk
);
838 devices
[devcnt
].devname
= devname
;
839 devices
[devcnt
].uptodate
= 0;
840 devices
[devcnt
].i
= *content
;
841 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
842 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
843 if (most_recent
< devcnt
) {
844 if (devices
[devcnt
].i
.events
845 > devices
[most_recent
].i
.events
)
846 most_recent
= devcnt
;
848 if (content
->array
.level
== LEVEL_MULTIPATH
)
849 /* with multipath, the raid_disk from the superblock is meaningless */
852 i
= devices
[devcnt
].i
.disk
.raid_disk
;
854 if (nextspare
< content
->array
.raid_disks
)
855 nextspare
= content
->array
.raid_disks
;
858 if (i
>= content
->array
.raid_disks
&&
864 int newbestcnt
= i
+10;
865 int *newbest
= xmalloc(sizeof(int)*newbestcnt
);
867 for (c
=0; c
< newbestcnt
; c
++)
869 newbest
[c
] = best
[c
];
874 bestcnt
= newbestcnt
;
877 devices
[best
[i
]].i
.events
878 == devices
[devcnt
].i
.events
879 && (devices
[best
[i
]].i
.disk
.minor
880 != devices
[devcnt
].i
.disk
.minor
)
882 && content
->array
.level
!= LEVEL_MULTIPATH
) {
883 /* two different devices with identical superblock.
884 * Could be a mis-detection caused by overlapping
885 * partitions. fail-safe.
887 pr_err("WARNING %s and %s appear"
888 " to have very similar superblocks.\n"
889 " If they are really different, "
890 "please --zero the superblock on one\n"
891 " If they are the same or overlap,"
892 " please remove one from %s.\n",
893 devices
[best
[i
]].devname
, devname
,
894 inargv
? "the list" :
895 "the\n DEVICE list in mdadm.conf"
903 || (devices
[best
[i
]].i
.events
904 < devices
[devcnt
].i
.events
))
911 pr_err("no devices found for %s\n",
914 st
->ss
->free_super(st
);
921 if (c
->update
&& strcmp(c
->update
, "byteorder")==0)
922 st
->minor_version
= 90;
924 st
->ss
->getinfo_super(st
, content
, NULL
);
925 clean
= content
->array
.state
& 1;
927 /* now we have some devices that might be suitable.
930 avail
= xcalloc(content
->array
.raid_disks
, 1);
934 for (i
=0; i
< bestcnt
; i
++) {
936 int event_margin
= 1; /* always allow a difference of '1'
937 * like the kernel does
940 /* note: we ignore error flags in multipath arrays
941 * as they don't make sense
943 if (content
->array
.level
!= LEVEL_MULTIPATH
)
944 if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_ACTIVE
))) {
945 if (!(devices
[j
].i
.disk
.state
946 & (1<<MD_DISK_FAULTY
))) {
947 devices
[j
].uptodate
= 1;
952 /* If this device thinks that 'most_recent' has failed, then
953 * we must reject this device.
955 if (j
!= most_recent
&&
956 content
->array
.raid_disks
> 0 &&
957 devices
[most_recent
].i
.disk
.raid_disk
>= 0 &&
958 devmap
[j
* content
->array
.raid_disks
+ devices
[most_recent
].i
.disk
.raid_disk
] == 0) {
960 pr_err("ignoring %s as it reports %s as failed\n",
961 devices
[j
].devname
, devices
[most_recent
].devname
);
965 if (devices
[j
].i
.events
+event_margin
>=
966 devices
[most_recent
].i
.events
) {
967 devices
[j
].uptodate
= 1;
968 if (i
< content
->array
.raid_disks
) {
969 if (devices
[j
].i
.recovery_start
== MaxSector
||
970 (content
->reshape_active
&&
971 ((i
>= content
->array
.raid_disks
- content
->delta_disks
) ||
972 (i
>= content
->array
.raid_disks
- content
->delta_disks
- 1
973 && content
->array
.level
== 4)))) {
984 (!enough(content
->array
.level
, content
->array
.raid_disks
,
985 content
->array
.layout
, 1,
988 (content
->reshape_active
&& content
->delta_disks
> 0 &&
989 !enough(content
->array
.level
, (content
->array
.raid_disks
990 - content
->delta_disks
),
991 content
->new_layout
, 1,
994 /* Choose the newest best drive which is
995 * not up-to-date, update the superblock
999 struct supertype
*tst
;
1000 unsigned long long current_events
;
1002 for (i
= 0; i
< content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
1005 !devices
[j
].uptodate
&&
1006 devices
[j
].i
.recovery_start
== MaxSector
&&
1007 (chosen_drive
< 0 ||
1009 > devices
[chosen_drive
].i
.events
))
1012 if (chosen_drive
< 0)
1014 current_events
= devices
[chosen_drive
].i
.events
;
1016 if (c
->verbose
>= 0)
1017 pr_err("forcing event count in %s(%d) from %d upto %d\n",
1018 devices
[chosen_drive
].devname
,
1019 devices
[chosen_drive
].i
.disk
.raid_disk
,
1020 (int)(devices
[chosen_drive
].i
.events
),
1021 (int)(devices
[most_recent
].i
.events
));
1022 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
1024 pr_err("Couldn't open %s for write - not updating\n",
1025 devices
[chosen_drive
].devname
);
1026 devices
[chosen_drive
].i
.events
= 0;
1029 tst
= dup_super(st
);
1030 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
1032 pr_err("RAID superblock disappeared from %s - not updating.\n",
1033 devices
[chosen_drive
].devname
);
1034 devices
[chosen_drive
].i
.events
= 0;
1037 content
->events
= devices
[most_recent
].i
.events
;
1038 tst
->ss
->update_super(tst
, content
, "force-one",
1039 devices
[chosen_drive
].devname
, c
->verbose
,
1042 if (tst
->ss
->store_super(tst
, fd
)) {
1044 pr_err("Could not re-write superblock on %s\n",
1045 devices
[chosen_drive
].devname
);
1046 devices
[chosen_drive
].i
.events
= 0;
1047 tst
->ss
->free_super(tst
);
1051 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
1052 devices
[chosen_drive
].uptodate
= 1;
1053 avail
[chosen_drive
] = 1;
1055 tst
->ss
->free_super(tst
);
1057 /* If there are any other drives of the same vintage,
1058 * add them in as well. We can't lose and we might gain
1060 for (i
= 0; i
< content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
1063 !devices
[j
].uptodate
&&
1064 devices
[j
].i
.recovery_start
== MaxSector
&&
1065 devices
[j
].i
.events
== current_events
) {
1072 /* Now we want to look at the superblock which the kernel will base things on
1073 * and compare the devices that we think are working with the devices that the
1074 * superblock thinks are working.
1075 * If there are differences and --force is given, then update this chosen
1079 st
->ss
->free_super(st
);
1080 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
++) {
1086 if (!devices
[j
].uptodate
)
1088 if (devices
[j
].i
.events
< devices
[most_recent
].i
.events
)
1091 if ((fd
=dev_open(devices
[j
].devname
, O_RDONLY
|O_EXCL
))< 0) {
1092 pr_err("Cannot open %s: %s\n",
1093 devices
[j
].devname
, strerror(errno
));
1098 if (st
->ss
->load_super(st
,fd
, NULL
)) {
1100 pr_err("RAID superblock has disappeared from %s\n",
1101 devices
[j
].devname
);
1108 if (st
->sb
== NULL
) {
1109 pr_err("No suitable drives found for %s\n", mddev
);
1114 st
->ss
->getinfo_super(st
, content
, NULL
);
1116 sysfs_init(content
, mdfd
, 0);
1118 for (i
=0; i
<bestcnt
; i
++) {
1120 unsigned int desired_state
;
1122 if (i
< content
->array
.raid_disks
)
1123 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
1129 if (!devices
[j
].uptodate
)
1132 devices
[j
].i
.disk
.state
= desired_state
;
1133 if (!(devices
[j
].i
.array
.state
& 1))
1136 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
1137 c
->verbose
, 0, NULL
)) {
1139 if (c
->verbose
>= 0)
1140 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
1141 j
, mddev
, devices
[j
].devname
);
1144 if (c
->verbose
>= -1)
1145 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
1146 i
, mddev
, devices
[j
].devname
);
1150 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
1151 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1156 if (c
->force
&& !clean
&&
1157 !enough(content
->array
.level
, content
->array
.raid_disks
,
1158 content
->array
.layout
, clean
,
1160 change
+= st
->ss
->update_super(st
, content
, "force-array",
1161 devices
[chosen_drive
].devname
, c
->verbose
,
1168 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
1170 pr_err("Could not open %s for write - cannot Assemble array.\n",
1171 devices
[chosen_drive
].devname
);
1176 if (st
->ss
->store_super(st
, fd
)) {
1178 pr_err("Could not re-write superblock on %s\n",
1179 devices
[chosen_drive
].devname
);
1184 if (c
->verbose
>= 0)
1185 pr_err("Marking array %s as 'clean'\n",
1190 /* If we are in the middle of a reshape we may need to restore saved data
1191 * that was moved aside due to the reshape overwriting live data
1192 * The code of doing this lives in Grow.c
1195 if (content
->reshape_active
) {
1197 int *fdlist
= xmalloc(sizeof(int)* bestcnt
);
1199 pr_err(":%s has an active reshape - checking "
1200 "if critical section needs to be restored\n",
1202 for (i
=0; i
<bestcnt
; i
++) {
1205 fdlist
[i
] = dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
1206 if (fdlist
[i
] < 0) {
1207 pr_err("Could not open %s for write - cannot Assemble array.\n",
1208 devices
[j
].devname
);
1216 if (st
->ss
->external
&& st
->ss
->recover_backup
)
1217 err
= st
->ss
->recover_backup(st
, content
);
1219 err
= Grow_restart(st
, content
, fdlist
, bestcnt
,
1220 c
->backup_file
, c
->verbose
> 0);
1221 if (err
&& c
->invalid_backup
) {
1224 " without restoring backup\n");
1230 if (fdlist
[i
]>=0) close(fdlist
[i
]);
1234 pr_err("Failed to restore critical section for reshape, sorry.\n");
1235 if (c
->backup_file
== NULL
)
1236 cont_err("Possibly you needed to specify the --backup-file\n");
1243 /* count number of in-sync devices according to the superblock.
1244 * We must have this number to start the array without -s or -R
1246 req_cnt
= content
->array
.working_disks
;
1248 /* Almost ready to actually *do* something */
1252 /* First, fill in the map, so that udev can find our name
1253 * as soon as we become active.
1255 map_update(NULL
, fd2devnum(mdfd
), content
->text_version
,
1256 content
->uuid
, chosen_name
);
1258 rv
= set_array_info(mdfd
, st
, content
);
1260 pr_err("failed to set array info for %s: %s\n",
1261 mddev
, strerror(errno
));
1262 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1267 if (ident
->bitmap_fd
>= 0) {
1268 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
1269 pr_err("SET_BITMAP_FILE failed.\n");
1270 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1275 } else if (ident
->bitmap_file
) {
1276 /* From config file */
1277 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
1279 pr_err("Could not open bitmap file %s\n",
1280 ident
->bitmap_file
);
1281 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1286 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
1287 pr_err("Failed to set bitmapfile for %s\n", mddev
);
1289 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1297 /* First, add the raid disks, but add the chosen one last */
1298 for (i
=0; i
<= bestcnt
; i
++) {
1302 if (j
== chosen_drive
)
1307 if (j
>= 0 /* && devices[j].uptodate */) {
1308 int dfd
= dev_open(devices
[j
].devname
,
1311 remove_partitions(dfd
);
1314 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
1317 pr_err("failed to add "
1322 if (i
< content
->array
.raid_disks
1327 } else if (c
->verbose
> 0)
1328 pr_err("added %s to %s as %d%s\n",
1329 devices
[j
].devname
, mddev
,
1330 devices
[j
].i
.disk
.raid_disk
,
1331 devices
[j
].uptodate
?"":
1332 " (possibly out of date)");
1333 } else if (c
->verbose
> 0 && i
< content
->array
.raid_disks
)
1334 pr_err("no uptodate device for "
1339 if (content
->array
.level
== LEVEL_CONTAINER
) {
1340 if (c
->verbose
>= 0) {
1341 pr_err("Container %s has been "
1342 "assembled with %d drive%s",
1343 mddev
, okcnt
+sparecnt
, okcnt
+sparecnt
==1?"":"s");
1344 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1345 fprintf(stderr
, " (out of %d)",
1346 content
->array
.raid_disks
);
1347 fprintf(stderr
, "\n");
1349 st
->ss
->free_super(st
);
1350 sysfs_uevent(content
, "change");
1351 wait_for(chosen_name
, mdfd
);
1357 if (c
->runstop
== 1 ||
1359 ( enough(content
->array
.level
, content
->array
.raid_disks
,
1360 content
->array
.layout
, clean
, avail
) &&
1361 (okcnt
+ rebuilding_cnt
>= req_cnt
|| start_partial_ok
)
1363 /* This array is good-to-go.
1364 * If a reshape is in progress then we might need to
1365 * continue monitoring it. In that case we start
1366 * it read-only and let the grow code make it writable.
1370 if (content
->reshape_active
&&
1371 content
->delta_disks
<= 0) {
1372 rv
= sysfs_set_str(content
, NULL
,
1373 "array_state", "readonly");
1375 rv
= Grow_continue(mdfd
, st
, content
,
1378 } else if (c
->readonly
&&
1379 sysfs_attribute_available(
1380 content
, NULL
, "array_state")) {
1381 rv
= sysfs_set_str(content
, NULL
,
1382 "array_state", "readonly");
1385 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1387 if (c
->verbose
>= 0) {
1388 pr_err("%s has been started with %d drive%s",
1389 mddev
, okcnt
, okcnt
==1?"":"s");
1390 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1391 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1393 fprintf(stderr
, "%s %d rebuilding", sparecnt
?",":" and", rebuilding_cnt
);
1395 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1396 fprintf(stderr
, ".\n");
1398 if (content
->reshape_active
&&
1399 content
->array
.level
>= 4 &&
1400 content
->array
.level
<= 6) {
1401 /* might need to increase the size
1402 * of the stripe cache - default is 256
1404 if (256 < 4 * (content
->array
.chunk_size
/4096)) {
1405 struct mdinfo
*sra
= sysfs_read(mdfd
, 0, 0);
1407 sysfs_set_num(sra
, NULL
,
1408 "stripe_cache_size",
1409 (4 * content
->array
.chunk_size
/ 4096) + 1);
1413 if (okcnt
< (unsigned)content
->array
.raid_disks
) {
1414 /* If any devices did not get added
1415 * because the kernel rejected them based
1416 * on event count, try adding them
1417 * again providing the action policy is
1418 * 're-add' or greater. The bitmap
1419 * might allow them to be included, or
1420 * they will become spares.
1422 for (i
= 0; i
< bestcnt
; i
++) {
1424 if (j
>= 0 && !devices
[j
].uptodate
) {
1425 if (!disk_action_allows(&devices
[j
].i
, st
->ss
->name
, act_re_add
))
1427 rv
= add_disk(mdfd
, st
, content
,
1429 if (rv
== 0 && c
->verbose
>= 0)
1430 pr_err("%s has been re-added.\n",
1431 devices
[j
].devname
);
1435 wait_for(mddev
, mdfd
);
1439 /* There is a nasty race with 'mdadm --monitor'.
1440 * If it opens this device before we close it,
1441 * it gets an incomplete open on which IO
1442 * doesn't work and the capacity is
1444 * If we reopen (to check for layered devices)
1445 * before --monitor closes, we loose.
1447 * So: wait upto 1 second for there to be
1448 * a non-zero capacity.
1450 while (usecs
< 1000) {
1451 mdfd
= open(mddev
, O_RDONLY
);
1453 unsigned long long size
;
1454 if (get_dev_size(mdfd
, NULL
, &size
) &&
1466 pr_err("failed to RUN_ARRAY %s: %s\n",
1467 mddev
, strerror(errno
));
1469 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1470 content
->array
.layout
, 1, avail
))
1471 pr_err("Not enough devices to "
1472 "start the array.\n");
1473 else if (!enough(content
->array
.level
,
1474 content
->array
.raid_disks
,
1475 content
->array
.layout
, clean
,
1477 pr_err("Not enough devices to "
1478 "start the array while not clean "
1479 "- consider --force.\n");
1482 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1487 if (c
->runstop
== -1) {
1488 pr_err("%s assembled from %d drive%s",
1489 mddev
, okcnt
, okcnt
==1?"":"s");
1490 if (okcnt
!= (unsigned)content
->array
.raid_disks
)
1491 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1492 fprintf(stderr
, ", but not started.\n");
1497 if (c
->verbose
>= -1) {
1498 pr_err("%s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
1500 fprintf(stderr
, "%s %d rebuilding", sparecnt
?", ":" and ", rebuilding_cnt
);
1502 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1503 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1504 content
->array
.layout
, 1, avail
))
1505 fprintf(stderr
, " - not enough to start the array.\n");
1506 else if (!enough(content
->array
.level
,
1507 content
->array
.raid_disks
,
1508 content
->array
.layout
, clean
,
1510 fprintf(stderr
, " - not enough to start the "
1511 "array while not clean - consider "
1514 if (req_cnt
== (unsigned)content
->array
.raid_disks
)
1515 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1517 fprintf(stderr
, " - need %d of %d to start", req_cnt
, content
->array
.raid_disks
);
1518 fprintf(stderr
, " (use --run to insist).\n");
1522 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1527 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1528 * been updated to point to the current locations of devices.
1529 * so we can just start the array
1532 dev
= makedev(devices
[chosen_drive
].i
.disk
.major
,
1533 devices
[chosen_drive
].i
.disk
.minor
);
1534 if (ioctl(mdfd
, START_ARRAY
, dev
)) {
1535 pr_err("Cannot start array: %s\n",
1546 int assemble_container_content(struct supertype
*st
, int mdfd
,
1547 struct mdinfo
*content
, struct context
*c
,
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 (content
->array
.major_version
== -1 &&
1562 content
->array
.minor_version
== -2 &&
1564 content
->text_version
[0] == '/')
1565 content
->text_version
[0] = '-';
1566 if (sysfs_set_array(content
, md_get_version(mdfd
)) != 0) {
1573 /* There are two types of reshape: container wide or sub-array specific
1574 * Check if metadata requests blocking container wide reshapes
1576 start_reshape
= (content
->reshape_active
&&
1577 !((content
->reshape_active
== CONTAINER_RESHAPE
) &&
1578 (content
->array
.state
& (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))));
1580 /* Block subarray here if it is under reshape now
1581 * Do not allow for any changes in this array
1583 if (st
->ss
->external
&& content
->recovery_blocked
&& start_reshape
)
1584 block_subarray(content
);
1588 old_raid_disks
= content
->array
.raid_disks
- content
->delta_disks
;
1589 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
1590 if (sysfs_add_disk(content
, dev
, 1) == 0) {
1591 if (dev
->disk
.raid_disk
>= old_raid_disks
&&
1592 content
->reshape_active
)
1596 } else if (errno
== EEXIST
)
1598 if (working
+ expansion
== 0)
1599 return 1;/* Nothing new, don't try to start */
1601 map_update(&map
, fd2devnum(mdfd
),
1602 content
->text_version
,
1603 content
->uuid
, chosen_name
);
1605 if (c
->runstop
> 0 ||
1606 (working
+ preexist
+ expansion
) >=
1607 content
->array
.working_disks
) {
1610 if (start_reshape
) {
1611 int spare
= content
->array
.raid_disks
+ expansion
;
1612 if (restore_backup(st
, content
,
1614 spare
, c
->backup_file
, c
->verbose
) == 1)
1617 err
= sysfs_set_str(content
, NULL
,
1618 "array_state", "readonly");
1622 if (st
->ss
->external
) {
1623 if (!mdmon_running(st
->container_dev
))
1624 start_mdmon(st
->container_dev
);
1625 ping_monitor_by_id(st
->container_dev
);
1626 if (mdmon_running(st
->container_dev
) &&
1627 st
->update_tail
== NULL
)
1628 st
->update_tail
= &st
->updates
;
1631 err
= Grow_continue(mdfd
, st
, content
, c
->backup_file
,
1633 } else switch(content
->array
.level
) {
1635 case LEVEL_MULTIPATH
:
1637 err
= sysfs_set_str(content
, NULL
, "array_state",
1638 c
->readonly
? "readonly" : "active");
1641 err
= sysfs_set_str(content
, NULL
, "array_state",
1643 /* start mdmon if needed. */
1645 if (!mdmon_running(st
->container_dev
))
1646 start_mdmon(st
->container_dev
);
1647 ping_monitor_by_id(st
->container_dev
);
1652 sysfs_set_safemode(content
, content
->safe_mode_delay
);
1654 /* Block subarray here if it is not reshaped now
1655 * It has be blocked a little later to allow mdmon to switch in
1658 if (st
->ss
->external
&& content
->recovery_blocked
&&
1660 block_subarray(content
);
1662 if (c
->verbose
>= 0) {
1664 pr_err("array %s now has %d device%s",
1665 chosen_name
, working
+ preexist
,
1666 working
+ preexist
== 1 ? "":"s");
1668 pr_err("Started %s with %d device%s",
1669 chosen_name
, working
+ preexist
,
1670 working
+ preexist
== 1 ? "":"s");
1672 fprintf(stderr
, " (%d new)", working
);
1674 fprintf(stderr
, " ( + %d for expansion)",
1676 fprintf(stderr
, "\n");
1679 wait_for(chosen_name
, mdfd
);
1681 /* FIXME should have an O_EXCL and wait for read-auto */
1683 if (c
->verbose
>= 0) {
1684 pr_err("%s assembled with %d device%s",
1685 chosen_name
, preexist
+ working
,
1686 preexist
+ working
== 1 ? "":"s");
1688 fprintf(stderr
, " (%d new)", working
);
1689 fprintf(stderr
, " but not started\n");