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
= xcalloc(num_devs
, sizeof(*devices
));
731 devmap
= xcalloc(num_devs
, content
->array
.raid_disks
);
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
= xmalloc(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
= xcalloc(content
->array
.raid_disks
, 1);
938 for (i
=0; i
< bestcnt
; i
++) {
940 int event_margin
= 1; /* always allow a difference of '1'
941 * like the kernel does
944 /* note: we ignore error flags in multipath arrays
945 * as they don't make sense
947 if (content
->array
.level
!= LEVEL_MULTIPATH
)
948 if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_ACTIVE
))) {
949 if (!(devices
[j
].i
.disk
.state
950 & (1<<MD_DISK_FAULTY
))) {
951 devices
[j
].uptodate
= 1;
956 /* If this device thinks that 'most_recent' has failed, then
957 * we must reject this device.
959 if (j
!= most_recent
&&
960 content
->array
.raid_disks
> 0 &&
961 devices
[most_recent
].i
.disk
.raid_disk
>= 0 &&
962 devmap
[j
* content
->array
.raid_disks
+ devices
[most_recent
].i
.disk
.raid_disk
] == 0) {
964 pr_err("ignoring %s as it reports %s as failed\n",
965 devices
[j
].devname
, devices
[most_recent
].devname
);
969 if (devices
[j
].i
.events
+event_margin
>=
970 devices
[most_recent
].i
.events
) {
971 devices
[j
].uptodate
= 1;
972 if (i
< content
->array
.raid_disks
) {
973 if (devices
[j
].i
.recovery_start
== MaxSector
||
974 (content
->reshape_active
&&
975 ((i
>= content
->array
.raid_disks
- content
->delta_disks
) ||
976 (i
>= content
->array
.raid_disks
- content
->delta_disks
- 1
977 && content
->array
.level
== 4)))) {
988 (!enough(content
->array
.level
, content
->array
.raid_disks
,
989 content
->array
.layout
, 1,
992 (content
->reshape_active
&& content
->delta_disks
> 0 &&
993 !enough(content
->array
.level
, (content
->array
.raid_disks
994 - content
->delta_disks
),
995 content
->new_layout
, 1,
998 /* Choose the newest best drive which is
999 * not up-to-date, update the superblock
1003 struct supertype
*tst
;
1004 unsigned long long current_events
;
1006 for (i
= 0; i
< content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
1009 !devices
[j
].uptodate
&&
1010 devices
[j
].i
.recovery_start
== MaxSector
&&
1011 (chosen_drive
< 0 ||
1013 > devices
[chosen_drive
].i
.events
))
1016 if (chosen_drive
< 0)
1018 current_events
= devices
[chosen_drive
].i
.events
;
1021 pr_err("forcing event count in %s(%d) from %d upto %d\n",
1022 devices
[chosen_drive
].devname
,
1023 devices
[chosen_drive
].i
.disk
.raid_disk
,
1024 (int)(devices
[chosen_drive
].i
.events
),
1025 (int)(devices
[most_recent
].i
.events
));
1026 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
1028 pr_err("Couldn't open %s for write - not updating\n",
1029 devices
[chosen_drive
].devname
);
1030 devices
[chosen_drive
].i
.events
= 0;
1033 tst
= dup_super(st
);
1034 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
1036 pr_err("RAID superblock disappeared from %s - not updating.\n",
1037 devices
[chosen_drive
].devname
);
1038 devices
[chosen_drive
].i
.events
= 0;
1041 content
->events
= devices
[most_recent
].i
.events
;
1042 tst
->ss
->update_super(tst
, content
, "force-one",
1043 devices
[chosen_drive
].devname
, verbose
,
1046 if (tst
->ss
->store_super(tst
, fd
)) {
1048 pr_err("Could not re-write superblock on %s\n",
1049 devices
[chosen_drive
].devname
);
1050 devices
[chosen_drive
].i
.events
= 0;
1051 tst
->ss
->free_super(tst
);
1055 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
1056 devices
[chosen_drive
].uptodate
= 1;
1057 avail
[chosen_drive
] = 1;
1059 tst
->ss
->free_super(tst
);
1061 /* If there are any other drives of the same vintage,
1062 * add them in as well. We can't lose and we might gain
1064 for (i
= 0; i
< content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
1067 !devices
[j
].uptodate
&&
1068 devices
[j
].i
.recovery_start
== MaxSector
&&
1069 devices
[j
].i
.events
== current_events
) {
1076 /* Now we want to look at the superblock which the kernel will base things on
1077 * and compare the devices that we think are working with the devices that the
1078 * superblock thinks are working.
1079 * If there are differences and --force is given, then update this chosen
1083 st
->ss
->free_super(st
);
1084 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
++) {
1090 if (!devices
[j
].uptodate
)
1092 if (devices
[j
].i
.events
< devices
[most_recent
].i
.events
)
1095 if ((fd
=dev_open(devices
[j
].devname
, O_RDONLY
|O_EXCL
))< 0) {
1096 pr_err("Cannot open %s: %s\n",
1097 devices
[j
].devname
, strerror(errno
));
1102 if (st
->ss
->load_super(st
,fd
, NULL
)) {
1104 pr_err("RAID superblock has disappeared from %s\n",
1105 devices
[j
].devname
);
1112 if (st
->sb
== NULL
) {
1113 pr_err("No suitable drives found for %s\n", mddev
);
1118 st
->ss
->getinfo_super(st
, content
, NULL
);
1120 sysfs_init(content
, mdfd
, 0);
1122 for (i
=0; i
<bestcnt
; i
++) {
1124 unsigned int desired_state
;
1126 if (i
< content
->array
.raid_disks
)
1127 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
1133 if (!devices
[j
].uptodate
)
1136 devices
[j
].i
.disk
.state
= desired_state
;
1137 if (!(devices
[j
].i
.array
.state
& 1))
1140 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
1141 verbose
, 0, NULL
)) {
1144 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
1145 j
, mddev
, devices
[j
].devname
);
1149 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
1150 i
, mddev
, devices
[j
].devname
);
1154 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
1155 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1160 if (force
&& !clean
&&
1161 !enough(content
->array
.level
, content
->array
.raid_disks
,
1162 content
->array
.layout
, clean
,
1164 change
+= st
->ss
->update_super(st
, content
, "force-array",
1165 devices
[chosen_drive
].devname
, verbose
,
1172 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
1174 pr_err("Could not open %s for write - cannot Assemble array.\n",
1175 devices
[chosen_drive
].devname
);
1180 if (st
->ss
->store_super(st
, fd
)) {
1182 pr_err("Could not re-write superblock on %s\n",
1183 devices
[chosen_drive
].devname
);
1189 pr_err("Marking array %s as 'clean'\n",
1194 /* If we are in the middle of a reshape we may need to restore saved data
1195 * that was moved aside due to the reshape overwriting live data
1196 * The code of doing this lives in Grow.c
1199 if (content
->reshape_active
) {
1201 int *fdlist
= xmalloc(sizeof(int)* bestcnt
);
1203 pr_err(":%s has an active reshape - checking "
1204 "if critical section needs to be restored\n",
1206 for (i
=0; i
<bestcnt
; i
++) {
1209 fdlist
[i
] = dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
1210 if (fdlist
[i
] < 0) {
1211 pr_err("Could not open %s for write - cannot Assemble array.\n",
1212 devices
[j
].devname
);
1220 if (st
->ss
->external
&& st
->ss
->recover_backup
)
1221 err
= st
->ss
->recover_backup(st
, content
);
1223 err
= Grow_restart(st
, content
, fdlist
, bestcnt
,
1224 backup_file
, verbose
> 0);
1225 if (err
&& invalid_backup
) {
1228 " without restoring backup\n");
1234 if (fdlist
[i
]>=0) close(fdlist
[i
]);
1237 pr_err("Failed to restore critical section for reshape, sorry.\n");
1238 if (backup_file
== NULL
)
1239 cont_err("Possibly you needed to specify the --backup-file\n");
1246 /* count number of in-sync devices according to the superblock.
1247 * We must have this number to start the array without -s or -R
1249 req_cnt
= content
->array
.working_disks
;
1251 /* Almost ready to actually *do* something */
1255 /* First, fill in the map, so that udev can find our name
1256 * as soon as we become active.
1258 map_update(NULL
, fd2devnum(mdfd
), content
->text_version
,
1259 content
->uuid
, chosen_name
);
1261 rv
= set_array_info(mdfd
, st
, content
);
1263 pr_err("failed to set array info for %s: %s\n",
1264 mddev
, strerror(errno
));
1265 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1270 if (ident
->bitmap_fd
>= 0) {
1271 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
1272 pr_err("SET_BITMAP_FILE failed.\n");
1273 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1278 } else if (ident
->bitmap_file
) {
1279 /* From config file */
1280 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
1282 pr_err("Could not open bitmap file %s\n",
1283 ident
->bitmap_file
);
1284 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1289 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
1290 pr_err("Failed to set bitmapfile for %s\n", mddev
);
1292 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1300 /* First, add the raid disks, but add the chosen one last */
1301 for (i
=0; i
<= bestcnt
; i
++) {
1305 if (j
== chosen_drive
)
1310 if (j
>= 0 /* && devices[j].uptodate */) {
1311 int dfd
= dev_open(devices
[j
].devname
,
1314 remove_partitions(dfd
);
1317 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
1320 pr_err("failed to add "
1325 if (i
< content
->array
.raid_disks
1330 } else if (verbose
> 0)
1331 pr_err("added %s to %s as %d%s\n",
1332 devices
[j
].devname
, mddev
,
1333 devices
[j
].i
.disk
.raid_disk
,
1334 devices
[j
].uptodate
?"":
1335 " (possibly out of date)");
1336 } else if (verbose
> 0 && i
< content
->array
.raid_disks
)
1337 pr_err("no uptodate device for "
1342 if (content
->array
.level
== LEVEL_CONTAINER
) {
1344 pr_err("Container %s has been "
1345 "assembled with %d drive%s",
1346 mddev
, okcnt
+sparecnt
, okcnt
+sparecnt
==1?"":"s");
1347 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1348 fprintf(stderr
, " (out of %d)",
1349 content
->array
.raid_disks
);
1350 fprintf(stderr
, "\n");
1352 st
->ss
->free_super(st
);
1353 sysfs_uevent(content
, "change");
1354 wait_for(chosen_name
, mdfd
);
1362 ( enough(content
->array
.level
, content
->array
.raid_disks
,
1363 content
->array
.layout
, clean
, avail
) &&
1364 (okcnt
+ rebuilding_cnt
>= req_cnt
|| start_partial_ok
)
1366 /* This array is good-to-go.
1367 * If a reshape is in progress then we might need to
1368 * continue monitoring it. In that case we start
1369 * it read-only and let the grow code make it writable.
1373 if (content
->reshape_active
&&
1374 content
->delta_disks
<= 0) {
1375 rv
= sysfs_set_str(content
, NULL
,
1376 "array_state", "readonly");
1378 rv
= Grow_continue(mdfd
, st
, content
,
1383 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1386 pr_err("%s has been started with %d drive%s",
1387 mddev
, okcnt
, okcnt
==1?"":"s");
1388 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1389 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1391 fprintf(stderr
, "%s %d rebuilding", sparecnt
?",":" and", rebuilding_cnt
);
1393 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1394 fprintf(stderr
, ".\n");
1396 if (content
->reshape_active
&&
1397 content
->array
.level
>= 4 &&
1398 content
->array
.level
<= 6) {
1399 /* might need to increase the size
1400 * of the stripe cache - default is 256
1402 if (256 < 4 * (content
->array
.chunk_size
/4096)) {
1403 struct mdinfo
*sra
= sysfs_read(mdfd
, 0, 0);
1405 sysfs_set_num(sra
, NULL
,
1406 "stripe_cache_size",
1407 (4 * content
->array
.chunk_size
/ 4096) + 1);
1411 if (okcnt
< (unsigned)content
->array
.raid_disks
) {
1412 /* If any devices did not get added
1413 * because the kernel rejected them based
1414 * on event count, try adding them
1415 * again providing the action policy is
1416 * 're-add' or greater. The bitmap
1417 * might allow them to be included, or
1418 * they will become spares.
1420 for (i
= 0; i
< bestcnt
; i
++) {
1422 if (j
>= 0 && !devices
[j
].uptodate
) {
1423 if (!disk_action_allows(&devices
[j
].i
, st
->ss
->name
, act_re_add
))
1425 rv
= add_disk(mdfd
, st
, content
,
1427 if (rv
== 0 && verbose
>= 0)
1428 pr_err("%s has been re-added.\n",
1429 devices
[j
].devname
);
1433 wait_for(mddev
, mdfd
);
1437 /* There is a nasty race with 'mdadm --monitor'.
1438 * If it opens this device before we close it,
1439 * it gets an incomplete open on which IO
1440 * doesn't work and the capacity is
1442 * If we reopen (to check for layered devices)
1443 * before --monitor closes, we loose.
1445 * So: wait upto 1 second for there to be
1446 * a non-zero capacity.
1448 while (usecs
< 1000) {
1449 mdfd
= open(mddev
, O_RDONLY
);
1451 unsigned long long size
;
1452 if (get_dev_size(mdfd
, NULL
, &size
) &&
1464 pr_err("failed to RUN_ARRAY %s: %s\n",
1465 mddev
, strerror(errno
));
1467 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1468 content
->array
.layout
, 1, avail
))
1469 pr_err("Not enough devices to "
1470 "start the array.\n");
1471 else if (!enough(content
->array
.level
,
1472 content
->array
.raid_disks
,
1473 content
->array
.layout
, clean
,
1475 pr_err("Not enough devices to "
1476 "start the array while not clean "
1477 "- consider --force.\n");
1480 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1485 if (runstop
== -1) {
1486 pr_err("%s assembled from %d drive%s",
1487 mddev
, okcnt
, okcnt
==1?"":"s");
1488 if (okcnt
!= (unsigned)content
->array
.raid_disks
)
1489 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1490 fprintf(stderr
, ", but not started.\n");
1495 if (verbose
>= -1) {
1496 pr_err("%s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
1498 fprintf(stderr
, "%s %d rebuilding", sparecnt
?", ":" and ", rebuilding_cnt
);
1500 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1501 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1502 content
->array
.layout
, 1, avail
))
1503 fprintf(stderr
, " - not enough to start the array.\n");
1504 else if (!enough(content
->array
.level
,
1505 content
->array
.raid_disks
,
1506 content
->array
.layout
, clean
,
1508 fprintf(stderr
, " - not enough to start the "
1509 "array while not clean - consider "
1512 if (req_cnt
== (unsigned)content
->array
.raid_disks
)
1513 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1515 fprintf(stderr
, " - need %d of %d to start", req_cnt
, content
->array
.raid_disks
);
1516 fprintf(stderr
, " (use --run to insist).\n");
1520 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1525 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1526 * been updated to point to the current locations of devices.
1527 * so we can just start the array
1530 dev
= makedev(devices
[chosen_drive
].i
.disk
.major
,
1531 devices
[chosen_drive
].i
.disk
.minor
);
1532 if (ioctl(mdfd
, START_ARRAY
, dev
)) {
1533 pr_err("Cannot start array: %s\n",
1544 int assemble_container_content(struct supertype
*st
, int mdfd
,
1545 struct mdinfo
*content
, int runstop
,
1546 char *chosen_name
, int verbose
,
1547 char *backup_file
, int freeze_reshape
)
1549 struct mdinfo
*dev
, *sra
;
1550 int working
= 0, preexist
= 0;
1552 struct map_ent
*map
= NULL
;
1556 sysfs_init(content
, mdfd
, 0);
1558 sra
= sysfs_read(mdfd
, 0, GET_VERSION
);
1559 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0)
1560 if (sysfs_set_array(content
, md_get_version(mdfd
)) != 0) {
1566 /* There are two types of reshape: container wide or sub-array specific
1567 * Check if metadata requests blocking container wide reshapes
1569 start_reshape
= (content
->reshape_active
&&
1570 !((content
->reshape_active
== CONTAINER_RESHAPE
) &&
1571 (content
->array
.state
& (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))));
1573 /* Block subarray here if it is under reshape now
1574 * Do not allow for any changes in this array
1576 if (st
->ss
->external
&& content
->recovery_blocked
&& start_reshape
)
1577 block_subarray(content
);
1581 old_raid_disks
= content
->array
.raid_disks
- content
->delta_disks
;
1582 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
1583 if (sysfs_add_disk(content
, dev
, 1) == 0) {
1584 if (dev
->disk
.raid_disk
>= old_raid_disks
&&
1585 content
->reshape_active
)
1589 } else if (errno
== EEXIST
)
1591 if (working
+ expansion
== 0)
1592 return 1;/* Nothing new, don't try to start */
1594 map_update(&map
, fd2devnum(mdfd
),
1595 content
->text_version
,
1596 content
->uuid
, chosen_name
);
1599 (working
+ preexist
+ expansion
) >=
1600 content
->array
.working_disks
) {
1603 if (start_reshape
) {
1604 int spare
= content
->array
.raid_disks
+ expansion
;
1605 if (restore_backup(st
, content
,
1607 spare
, backup_file
, verbose
) == 1)
1610 err
= sysfs_set_str(content
, NULL
,
1611 "array_state", "readonly");
1615 if (st
->ss
->external
) {
1616 if (!mdmon_running(st
->container_dev
))
1617 start_mdmon(st
->container_dev
);
1618 ping_monitor_by_id(st
->container_dev
);
1619 if (mdmon_running(st
->container_dev
) &&
1620 st
->update_tail
== NULL
)
1621 st
->update_tail
= &st
->updates
;
1624 err
= Grow_continue(mdfd
, st
, content
, backup_file
,
1626 } else switch(content
->array
.level
) {
1628 case LEVEL_MULTIPATH
:
1630 err
= sysfs_set_str(content
, NULL
, "array_state",
1634 err
= sysfs_set_str(content
, NULL
, "array_state",
1636 /* start mdmon if needed. */
1638 if (!mdmon_running(st
->container_dev
))
1639 start_mdmon(st
->container_dev
);
1640 ping_monitor_by_id(st
->container_dev
);
1645 sysfs_set_safemode(content
, content
->safe_mode_delay
);
1647 /* Block subarray here if it is not reshaped now
1648 * It has be blocked a little later to allow mdmon to switch in
1651 if (st
->ss
->external
&& content
->recovery_blocked
&&
1653 block_subarray(content
);
1657 pr_err("array %s now has %d device%s",
1658 chosen_name
, working
+ preexist
,
1659 working
+ preexist
== 1 ? "":"s");
1661 pr_err("Started %s with %d device%s",
1662 chosen_name
, working
+ preexist
,
1663 working
+ preexist
== 1 ? "":"s");
1665 fprintf(stderr
, " (%d new)", working
);
1667 fprintf(stderr
, " ( + %d for expansion)",
1669 fprintf(stderr
, "\n");
1672 wait_for(chosen_name
, mdfd
);
1674 /* FIXME should have an O_EXCL and wait for read-auto */
1677 pr_err("%s assembled with %d device%s",
1678 chosen_name
, preexist
+ working
,
1679 preexist
+ working
== 1 ? "":"s");
1681 fprintf(stderr
, " (%d new)", working
);
1682 fprintf(stderr
, " but not started\n");