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 fprintf(stderr
, Name
": %s has wrong uuid.\n",
88 if (ident
->name
[0] && (!update
|| strcmp(update
, "name")!= 0) &&
89 name_matches(content
->name
, ident
->name
, homehost
)==0) {
91 fprintf(stderr
, Name
": %s has wrong name.\n",
95 if (ident
->super_minor
!= UnSet
&&
96 ident
->super_minor
!= content
->array
.md_minor
) {
98 fprintf(stderr
, Name
": %s has wrong super-minor.\n",
102 if (ident
->level
!= UnSet
&&
103 ident
->level
!= content
->array
.level
) {
105 fprintf(stderr
, Name
": %s has wrong raid level.\n",
109 if (ident
->raid_disks
!= UnSet
&&
110 ident
->raid_disks
!= content
->array
.raid_disks
) {
112 fprintf(stderr
, Name
": %s requires wrong number of drives.\n",
116 if (ident
->member
&& ident
->member
[0]) {
117 /* content->text_version must match */
118 char *s
= strchr(content
->text_version
+1, '/');
121 fprintf(stderr
, Name
": %s is not a container and one is required.\n",
124 } else if (strcmp(ident
->member
, s
+1) != 0) {
126 fprintf(stderr
, Name
": skipping wrong member %s is %s\n",
127 content
->text_version
, devname
);
135 int Assemble(struct supertype
*st
, char *mddev
,
136 struct mddev_ident
*ident
,
137 struct mddev_dev
*devlist
,
138 char *backup_file
, int invalid_backup
,
139 int readonly
, int runstop
,
140 char *update
, char *homehost
, int require_homehost
,
141 int verbose
, int force
, int freeze_reshape
)
144 * The task of Assemble is to find a collection of
145 * devices that should (according to their superblocks)
146 * form an array, and to give this collection to the MD driver.
147 * In Linux-2.4 and later, this involves submitting a
148 * SET_ARRAY_INFO ioctl with no arg - to prepare
149 * the array - and then submit a number of
150 * ADD_NEW_DISK ioctls to add disks into
151 * the array. Finally RUN_ARRAY might
152 * be submitted to start the array.
154 * Much of the work of Assemble is in finding and/or
155 * checking the disks to make sure they look right.
157 * If mddev is not set, then scan must be set and we
158 * read through the config file for dev+uuid mapping
159 * We recurse, setting mddev, for each device that
161 * - has a valid uuid (or any uuid if !uuidset)
163 * If mddev is set, we try to determine state of md.
164 * check version - must be at least 0.90.0
165 * check kernel version. must be at least 2.4.
166 * If not, we can possibly fall back on START_ARRAY
167 * Try to GET_ARRAY_INFO.
168 * If possible, give up
169 * If not, try to STOP_ARRAY just to make sure
171 * If !uuidset and scan, look in conf-file for uuid
172 * If not found, give up
173 * If !devlist and scan and uuidset, get list of devs from conf-file
176 * Check superblock - discard if bad
177 * Check uuid (set if we don't have one) - discard if no match
178 * Check superblock similarity if we have a superblock - discard if different
179 * Record events, devicenum
180 * This should give us a list of devices for the array
181 * We should collect the most recent event number
183 * Count disks with recent enough event count
184 * While force && !enough disks
185 * Choose newest rejected disks, update event count
186 * mark clean and rewrite superblock
189 * foreach device with recent events : ADD_NEW_DISK
190 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
192 * Check the device numbers in superblock are right
193 * update superblock if any changes
199 int auto_assem
= (mddev
== NULL
&& !ident
->uuid_set
&&
200 ident
->super_minor
== UnSet
&& ident
->name
[0] == 0
201 && (ident
->container
== NULL
|| ident
->member
== NULL
));
203 int vers
= vers
; /* Keep gcc quite - it really is initialised */
206 int uptodate
; /* set once we decide that this device is as
207 * recent as everything else in the array.
212 int *best
= NULL
; /* indexed by raid_disk */
215 unsigned int okcnt
, sparecnt
, rebuilding_cnt
;
216 unsigned int req_cnt
;
222 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 fprintf(stderr
, Name
": 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 fprintf(stderr
, Name
": 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
= dup_super(st
);
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 fprintf(stderr
, Name
": %s is not one of %s\n", devname
, ident
->devices
);
309 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
311 if (report_missmatch
)
312 fprintf(stderr
, Name
": cannot open device %s: %s\n",
313 devname
, strerror(errno
));
315 } else if (fstat(dfd
, &stb
)< 0) {
317 fprintf(stderr
, Name
": fstat failed for %s: %s\n",
318 devname
, strerror(errno
));
320 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
321 fprintf(stderr
, Name
": %s is not a block device.\n",
324 } else if (must_be_container(dfd
)) {
326 /* already found some components, this cannot
329 if (report_missmatch
)
330 fprintf(stderr
, Name
": %s is a container, but we are looking for components\n",
333 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
334 } if (!tst
&& (tst
= super_by_fd(dfd
, NULL
)) == NULL
) {
335 if (report_missmatch
)
336 fprintf(stderr
, Name
": not a recognisable container: %s\n",
340 } else if (!tst
->ss
->load_container
341 || tst
->ss
->load_container(tst
, dfd
, NULL
)) {
342 if (report_missmatch
)
343 fprintf(stderr
, Name
": no correct container type: %s\n",
346 } else if (auto_assem
&&
347 !conf_test_metadata(tst
->ss
->name
, (pol
= devnum_policy(stb
.st_rdev
)),
348 tst
->ss
->match_home(tst
, homehost
) == 1)) {
349 if (report_missmatch
)
350 fprintf(stderr
, Name
": %s has metadata type %s for which "
351 "auto-assembly is disabled\n",
352 devname
, tst
->ss
->name
);
357 if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
358 if (report_missmatch
)
359 fprintf(stderr
, Name
": no recogniseable superblock on %s\n",
362 } else if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
363 if (report_missmatch
)
364 fprintf(stderr
, Name
": no RAID superblock on %s\n",
367 } else if (tst
->ss
->compare_super
== NULL
) {
368 if (report_missmatch
)
369 fprintf(stderr
, Name
": Cannot assemble %s metadata on %s\n",
370 tst
->ss
->name
, devname
);
372 } else if (auto_assem
&& st
== NULL
&&
373 !conf_test_metadata(tst
->ss
->name
, (pol
= devnum_policy(stb
.st_rdev
)),
374 tst
->ss
->match_home(tst
, homehost
) == 1)) {
375 if (report_missmatch
)
376 fprintf(stderr
, Name
": %s has metadata type %s for which "
377 "auto-assembly is disabled\n",
378 devname
, tst
->ss
->name
);
382 if (dfd
>= 0) close(dfd
);
383 if (tmpdev
->used
== 2) {
384 if (auto_assem
|| !inargv
)
385 /* Ignore unrecognised devices during auto-assembly */
387 if (ident
->uuid_set
|| ident
->name
[0] ||
388 ident
->super_minor
!= UnSet
)
389 /* Ignore unrecognised device if looking for
394 fprintf(stderr
, Name
": %s has no superblock - assembly aborted\n",
397 st
->ss
->free_super(st
);
398 dev_policy_free(pol
);
399 domain_free(domains
);
403 if (found_container
) {
404 /* tmpdev is a container. We need to be either
405 * looking for a member, or auto-assembling
408 if (ident
->container
) {
409 if (ident
->container
[0] == '/' &&
410 !same_dev(ident
->container
, devname
)) {
411 if (report_missmatch
)
412 fprintf(stderr
, Name
": %s is not the container required (%s)\n",
413 devname
, ident
->container
);
416 if (ident
->container
[0] != '/') {
421 tst
->ss
->getinfo_super(tst
, content
, NULL
);
423 if (!parse_uuid(ident
->container
, uuid
) ||
424 !same_uuid(content
->uuid
, uuid
, tst
->ss
->swapuuid
)) {
425 if (report_missmatch
)
426 fprintf(stderr
, Name
": %s has wrong UUID to be required container\n",
432 /* It is worth looking inside this container.
435 fprintf(stderr
, Name
": looking in container %s\n",
438 for (content
= tst
->ss
->container_content(tst
, NULL
);
440 content
= content
->next
) {
442 if (!ident_matches(ident
, content
, tst
,
444 report_missmatch
? devname
: NULL
))
445 /* message already printed */;
446 else if (is_member_busy(content
->text_version
)) {
447 if (report_missmatch
)
448 fprintf(stderr
, Name
": member %s in %s is already assembled\n",
449 content
->text_version
,
451 } else if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
452 /* do not assemble arrays with unsupported configurations */
453 fprintf(stderr
, Name
": Cannot activate member %s in %s.\n",
454 content
->text_version
,
461 goto loop
; /* empty container */
464 st
= tst
; tst
= NULL
;
465 if (!auto_assem
&& inargv
&& tmpdev
->next
!= NULL
) {
466 fprintf(stderr
, Name
": %s is a container, but is not "
467 "only device given: confused and aborting\n",
469 st
->ss
->free_super(st
);
470 dev_policy_free(pol
);
471 domain_free(domains
);
475 fprintf(stderr
, Name
": found match on member %s in %s\n",
476 content
->text_version
, devname
);
478 /* make sure we finished the loop */
484 tst
->ss
->getinfo_super(tst
, content
, NULL
);
486 if (!ident_matches(ident
, content
, tst
,
488 report_missmatch
? devname
: NULL
))
493 if (st
->minor_version
== -1)
494 st
->minor_version
= tst
->minor_version
;
496 if (memcmp(content
->uuid
, uuid_zero
,
497 sizeof(int[4])) == 0) {
498 /* this is a floating spare. It cannot define
499 * an array unless there are no more arrays of
500 * this type to be found. It can be included
501 * in an array of this type though.
507 if (st
->ss
!= tst
->ss
||
508 st
->minor_version
!= tst
->minor_version
||
509 st
->ss
->compare_super(st
, tst
) != 0) {
510 /* Some mismatch. If exactly one array matches this host,
511 * we can resolve on that one.
512 * Or, if we are auto assembling, we just ignore the second
518 int first
= st
->ss
->match_home(st
, homehost
);
519 int last
= tst
->ss
->match_home(tst
, homehost
);
521 (first
== 1 || last
== 1)) {
522 /* We can do something */
523 if (first
) {/* just ignore this one */
524 if (report_missmatch
)
525 fprintf(stderr
, Name
": %s misses out due to wrong homehost\n",
528 } else { /* reject all those sofar */
529 struct mddev_dev
*td
;
530 if (report_missmatch
)
531 fprintf(stderr
, Name
": %s overrides previous devices due to good homehost\n",
533 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
541 fprintf(stderr
, Name
": superblock on %s doesn't match others - assembly aborted\n",
543 tst
->ss
->free_super(tst
);
544 st
->ss
->free_super(st
);
545 dev_policy_free(pol
);
546 domain_free(domains
);
552 /* Collect domain information from members only */
553 if (tmpdev
&& tmpdev
->used
== 1) {
555 pol
= devnum_policy(stb
.st_rdev
);
556 domain_merge(&domains
, pol
, tst
?tst
->ss
->name
:NULL
);
558 dev_policy_free(pol
);
561 tst
->ss
->free_super(tst
);
564 /* Check if we found some imsm spares but no members */
567 memcmp(uuid_zero
, ident
->uuid
,sizeof(uuid_zero
)) == 0)) &&
569 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
570 if (tmpdev
->used
!= 3)
576 /* we need sb from one of the spares */
577 int dfd
= dev_open(tmpdev
->devname
, O_RDONLY
);
579 st
->ss
->load_super(st
, dfd
, NULL
))
586 /* Now reject spares that don't match domains of identified members */
587 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
589 if (tmpdev
->used
!= 3)
591 if (stat(tmpdev
->devname
, &stb
)< 0) {
592 fprintf(stderr
, Name
": fstat failed for %s: %s\n",
593 tmpdev
->devname
, strerror(errno
));
596 struct dev_policy
*pol
= devnum_policy(stb
.st_rdev
);
597 int dt
= domain_test(domains
, pol
, NULL
);
598 if (inargv
&& dt
!= 0)
599 /* take this spare as domains match
600 * if there are any */
602 else if (!inargv
&& dt
== 1)
603 /* device wasn't explicitly listed, so need
604 * explicit domain match - which we have */
607 /* if domains don't match mark as unused */
609 dev_policy_free(pol
);
612 domain_free(domains
);
614 if (!st
|| !st
->sb
|| !content
)
617 /* Now need to open the array device. Use create_mddev */
618 if (content
== &info
)
619 st
->ss
->getinfo_super(st
, content
, NULL
);
621 trustworthy
= FOREIGN
;
622 name
= content
->name
;
623 switch (st
->ss
->match_home(st
, homehost
)
624 ?: st
->ss
->match_home(st
, "any")) {
627 name
= strchr(content
->name
, ':');
631 name
= content
->name
;
635 /* If the array is listed in mdadm.conf or on
636 * command line, then we trust the name
637 * even if the array doesn't look local
642 content
->array
.level
== LEVEL_CONTAINER
) {
643 name
= content
->text_version
;
644 trustworthy
= METADATA
;
647 if (name
[0] && trustworthy
!= LOCAL
&&
648 ! require_homehost
&&
649 conf_name_is_free(name
))
652 if (trustworthy
== LOCAL
&&
654 /* Ignore 'host:' prefix of name */
655 name
= strchr(name
, ':')+1;
657 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
660 st
->ss
->free_super(st
);
666 vers
= md_get_version(mdfd
);
668 fprintf(stderr
, Name
": Assemble requires driver version 0.90.0 or later.\n"
669 " Upgrade your kernel or try --build\n");
673 if (mddev_busy(fd2devnum(mdfd
))) {
674 fprintf(stderr
, Name
": %s already active, cannot restart it!\n",
676 for (tmpdev
= devlist
;
677 tmpdev
&& tmpdev
->used
!= 1;
678 tmpdev
= tmpdev
->next
)
680 if (tmpdev
&& auto_assem
)
681 fprintf(stderr
, Name
": %s needed for %s...\n",
682 mddev
, tmpdev
->devname
);
685 st
->ss
->free_super(st
);
690 ioctl(mdfd
, STOP_ARRAY
, NULL
); /* just incase it was started but has no content */
693 if (content
!= &info
) {
694 /* This is a member of a container. Try starting the array. */
696 err
= assemble_container_content(st
, mdfd
, content
, runstop
,
697 chosen_name
, verbose
,
698 backup_file
, freeze_reshape
);
703 /* Ok, no bad inconsistancy, we can try updating etc */
705 content
->update_private
= NULL
;
706 devices
= malloc(num_devs
* sizeof(*devices
));
707 devmap
= calloc(num_devs
* content
->array
.raid_disks
, 1);
708 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) if (tmpdev
->used
== 1) {
709 char *devname
= tmpdev
->devname
;
711 /* looks like a good enough match to update the super block if needed */
715 /* prepare useful information in info structures */
717 struct supertype
*tst
;
721 if (strcmp(update
, "uuid")==0 &&
724 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
725 read(rfd
, ident
->uuid
, 16) != 16) {
726 *(__u32
*)(ident
->uuid
) = random();
727 *(__u32
*)(ident
->uuid
+1) = random();
728 *(__u32
*)(ident
->uuid
+2) = random();
729 *(__u32
*)(ident
->uuid
+3) = random();
731 if (rfd
>= 0) close(rfd
);
733 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
736 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
737 fprintf(stderr
, Name
": cannot re-read metadata from %s - aborting\n",
746 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
748 memcpy(content
->uuid
, ident
->uuid
, 16);
749 strcpy(content
->name
, ident
->name
);
750 content
->array
.md_minor
= minor(stb2
.st_rdev
);
752 if (strcmp(update
, "byteorder") == 0)
755 err
= tst
->ss
->update_super(tst
, content
, update
,
761 Name
": --update=%s not understood"
762 " for %s metadata\n",
763 update
, tst
->ss
->name
);
764 tst
->ss
->free_super(tst
);
772 if (strcmp(update
, "uuid")==0 &&
775 memcpy(ident
->uuid
, content
->uuid
, 16);
777 if (tst
->ss
->store_super(tst
, dfd
))
778 fprintf(stderr
, Name
": Could not re-write superblock on %s.\n",
782 if (strcmp(update
, "uuid")==0 &&
783 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
784 if (bitmap_update_uuid(ident
->bitmap_fd
,
786 tst
->ss
->swapuuid
) != 0)
787 fprintf(stderr
, Name
": Could not update uuid on external bitmap.\n");
791 tst
->ss
->free_super(tst
);
795 struct supertype
*tst
= dup_super(st
);
797 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
799 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
800 fprintf(stderr
, Name
": cannot re-read metadata from %s - aborting\n",
809 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
810 tst
->ss
->free_super(tst
);
817 fprintf(stderr
, Name
": %s is identified as a member of %s, slot %d.\n",
818 devname
, mddev
, content
->disk
.raid_disk
);
819 devices
[devcnt
].devname
= devname
;
820 devices
[devcnt
].uptodate
= 0;
821 devices
[devcnt
].i
= *content
;
822 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
823 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
824 if (most_recent
< devcnt
) {
825 if (devices
[devcnt
].i
.events
826 > devices
[most_recent
].i
.events
)
827 most_recent
= devcnt
;
829 if (content
->array
.level
== LEVEL_MULTIPATH
)
830 /* with multipath, the raid_disk from the superblock is meaningless */
833 i
= devices
[devcnt
].i
.disk
.raid_disk
;
835 if (nextspare
< content
->array
.raid_disks
)
836 nextspare
= content
->array
.raid_disks
;
839 if (i
>= content
->array
.raid_disks
&&
845 int newbestcnt
= i
+10;
846 int *newbest
= malloc(sizeof(int)*newbestcnt
);
848 for (c
=0; c
< newbestcnt
; c
++)
850 newbest
[c
] = best
[c
];
855 bestcnt
= newbestcnt
;
858 devices
[best
[i
]].i
.events
859 == devices
[devcnt
].i
.events
860 && (devices
[best
[i
]].i
.disk
.minor
861 != devices
[devcnt
].i
.disk
.minor
)
863 && content
->array
.level
!= LEVEL_MULTIPATH
) {
864 /* two different devices with identical superblock.
865 * Could be a mis-detection caused by overlapping
866 * partitions. fail-safe.
868 fprintf(stderr
, Name
": WARNING %s and %s appear"
869 " to have very similar superblocks.\n"
870 " If they are really different, "
871 "please --zero the superblock on one\n"
872 " If they are the same or overlap,"
873 " please remove one from %s.\n",
874 devices
[best
[i
]].devname
, devname
,
875 inargv
? "the list" :
876 "the\n DEVICE list in mdadm.conf"
884 || (devices
[best
[i
]].i
.events
885 < devices
[devcnt
].i
.events
))
890 free(content
->update_private
);
891 content
->update_private
= NULL
;
894 fprintf(stderr
, Name
": no devices found for %s\n",
897 st
->ss
->free_super(st
);
904 if (update
&& strcmp(update
, "byteorder")==0)
905 st
->minor_version
= 90;
907 st
->ss
->getinfo_super(st
, content
, NULL
);
908 clean
= content
->array
.state
& 1;
910 /* now we have some devices that might be suitable.
913 avail
= malloc(content
->array
.raid_disks
);
914 memset(avail
, 0, content
->array
.raid_disks
);
918 for (i
=0; i
< bestcnt
; i
++) {
920 int event_margin
= 1; /* always allow a difference of '1'
921 * like the kernel does
924 /* note: we ignore error flags in multipath arrays
925 * as they don't make sense
927 if (content
->array
.level
!= LEVEL_MULTIPATH
)
928 if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_ACTIVE
))) {
929 if (!(devices
[j
].i
.disk
.state
930 & (1<<MD_DISK_FAULTY
))) {
931 devices
[j
].uptodate
= 1;
936 /* If this devices thinks that 'most_recent' has failed, then
937 * we must reject this device.
939 if (j
!= most_recent
&&
940 content
->array
.raid_disks
> 0 &&
941 devices
[most_recent
].i
.disk
.raid_disk
>= 0 &&
942 devmap
[j
* content
->array
.raid_disks
+ devices
[most_recent
].i
.disk
.raid_disk
] == 0) {
944 fprintf(stderr
, Name
": ignoring %s as it reports %s as failed\n",
945 devices
[j
].devname
, devices
[most_recent
].devname
);
949 if (devices
[j
].i
.events
+event_margin
>=
950 devices
[most_recent
].i
.events
) {
951 devices
[j
].uptodate
= 1;
952 if (i
< content
->array
.raid_disks
) {
953 if (devices
[j
].i
.recovery_start
== MaxSector
||
954 (content
->reshape_active
&&
955 j
>= content
->array
.raid_disks
- content
->delta_disks
)) {
965 while (force
&& !enough(content
->array
.level
, content
->array
.raid_disks
,
966 content
->array
.layout
, 1,
968 /* Choose the newest best drive which is
969 * not up-to-date, update the superblock
973 struct supertype
*tst
;
974 unsigned long long current_events
;
976 for (i
= 0; i
< content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
979 !devices
[j
].uptodate
&&
980 devices
[j
].i
.recovery_start
== MaxSector
&&
983 > devices
[chosen_drive
].i
.events
))
986 if (chosen_drive
< 0)
988 current_events
= devices
[chosen_drive
].i
.events
;
991 fprintf(stderr
, Name
": forcing event count in %s(%d) from %d upto %d\n",
992 devices
[chosen_drive
].devname
,
993 devices
[chosen_drive
].i
.disk
.raid_disk
,
994 (int)(devices
[chosen_drive
].i
.events
),
995 (int)(devices
[most_recent
].i
.events
));
996 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
998 fprintf(stderr
, Name
": Couldn't open %s for write - not updating\n",
999 devices
[chosen_drive
].devname
);
1000 devices
[chosen_drive
].i
.events
= 0;
1003 tst
= dup_super(st
);
1004 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
1006 fprintf(stderr
, Name
": RAID superblock disappeared from %s - not updating.\n",
1007 devices
[chosen_drive
].devname
);
1008 devices
[chosen_drive
].i
.events
= 0;
1011 content
->events
= devices
[most_recent
].i
.events
;
1012 tst
->ss
->update_super(tst
, content
, "force-one",
1013 devices
[chosen_drive
].devname
, verbose
,
1016 if (tst
->ss
->store_super(tst
, fd
)) {
1018 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
1019 devices
[chosen_drive
].devname
);
1020 devices
[chosen_drive
].i
.events
= 0;
1021 tst
->ss
->free_super(tst
);
1025 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
1026 devices
[chosen_drive
].uptodate
= 1;
1027 avail
[chosen_drive
] = 1;
1029 tst
->ss
->free_super(tst
);
1031 /* If there are any other drives of the same vintage,
1032 * add them in as well. We can't lose and we might gain
1034 for (i
= 0; i
< content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
1037 !devices
[j
].uptodate
&&
1038 devices
[j
].i
.events
== current_events
) {
1045 /* Now we want to look at the superblock which the kernel will base things on
1046 * and compare the devices that we think are working with the devices that the
1047 * superblock thinks are working.
1048 * If there are differences and --force is given, then update this chosen
1052 st
->ss
->free_super(st
);
1053 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
++) {
1059 if (!devices
[j
].uptodate
)
1061 if (devices
[j
].i
.events
< devices
[most_recent
].i
.events
)
1064 if ((fd
=dev_open(devices
[j
].devname
, O_RDONLY
|O_EXCL
))< 0) {
1065 fprintf(stderr
, Name
": Cannot open %s: %s\n",
1066 devices
[j
].devname
, strerror(errno
));
1071 if (st
->ss
->load_super(st
,fd
, NULL
)) {
1073 fprintf(stderr
, Name
": RAID superblock has disappeared from %s\n",
1074 devices
[j
].devname
);
1081 if (st
->sb
== NULL
) {
1082 fprintf(stderr
, Name
": No suitable drives found for %s\n", mddev
);
1087 st
->ss
->getinfo_super(st
, content
, NULL
);
1089 sysfs_init(content
, mdfd
, 0);
1091 for (i
=0; i
<bestcnt
; i
++) {
1093 unsigned int desired_state
;
1095 if (i
< content
->array
.raid_disks
)
1096 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
1102 if (!devices
[j
].uptodate
)
1105 devices
[j
].i
.disk
.state
= desired_state
;
1106 if (!(devices
[j
].i
.array
.state
& 1))
1109 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
1110 verbose
, 0, NULL
)) {
1113 fprintf(stderr
, Name
": "
1114 "clearing FAULTY flag for device %d in %s for %s\n",
1115 j
, mddev
, devices
[j
].devname
);
1119 fprintf(stderr
, Name
": "
1120 "device %d in %s has wrong state in superblock, but %s seems ok\n",
1121 i
, mddev
, devices
[j
].devname
);
1125 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
1126 fprintf(stderr
, Name
": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1131 if (force
&& !clean
&&
1132 !enough(content
->array
.level
, content
->array
.raid_disks
,
1133 content
->array
.layout
, clean
,
1135 change
+= st
->ss
->update_super(st
, content
, "force-array",
1136 devices
[chosen_drive
].devname
, verbose
,
1143 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
1145 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
1146 devices
[chosen_drive
].devname
);
1151 if (st
->ss
->store_super(st
, fd
)) {
1153 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
1154 devices
[chosen_drive
].devname
);
1160 fprintf(stderr
, Name
": Marking array %s as 'clean'\n",
1165 /* If we are in the middle of a reshape we may need to restore saved data
1166 * that was moved aside due to the reshape overwriting live data
1167 * The code of doing this lives in Grow.c
1170 if (content
->reshape_active
) {
1172 int *fdlist
= malloc(sizeof(int)* bestcnt
);
1174 fprintf(stderr
, Name
":%s has an active reshape - checking "
1175 "if critical section needs to be restored\n",
1177 for (i
=0; i
<bestcnt
; i
++) {
1180 fdlist
[i
] = dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
1181 if (fdlist
[i
] < 0) {
1182 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
1183 devices
[j
].devname
);
1191 if (st
->ss
->external
&& st
->ss
->recover_backup
)
1192 err
= st
->ss
->recover_backup(st
, content
);
1194 err
= Grow_restart(st
, content
, fdlist
, bestcnt
,
1195 backup_file
, verbose
> 0);
1196 if (err
&& invalid_backup
) {
1198 fprintf(stderr
, Name
": continuing"
1199 " without restoring backup\n");
1205 if (fdlist
[i
]>=0) close(fdlist
[i
]);
1208 fprintf(stderr
, Name
": Failed to restore critical section for reshape, sorry.\n");
1209 if (backup_file
== NULL
)
1210 fprintf(stderr
," Possibly you needed to specify the --backup-file\n");
1217 /* count number of in-sync devices according to the superblock.
1218 * We must have this number to start the array without -s or -R
1220 req_cnt
= content
->array
.working_disks
;
1222 /* Almost ready to actually *do* something */
1226 /* First, fill in the map, so that udev can find our name
1227 * as soon as we become active.
1229 map_update(NULL
, fd2devnum(mdfd
), content
->text_version
,
1230 content
->uuid
, chosen_name
);
1232 rv
= set_array_info(mdfd
, st
, content
);
1234 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
1235 mddev
, strerror(errno
));
1236 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1241 if (ident
->bitmap_fd
>= 0) {
1242 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
1243 fprintf(stderr
, Name
": SET_BITMAP_FILE failed.\n");
1244 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1249 } else if (ident
->bitmap_file
) {
1250 /* From config file */
1251 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
1253 fprintf(stderr
, Name
": Could not open bitmap file %s\n",
1254 ident
->bitmap_file
);
1255 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1260 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
1261 fprintf(stderr
, Name
": Failed to set bitmapfile for %s\n", mddev
);
1263 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1271 /* First, add the raid disks, but add the chosen one last */
1272 for (i
=0; i
<= bestcnt
; i
++) {
1276 if (j
== chosen_drive
)
1281 if (j
>= 0 /* && devices[j].uptodate */) {
1282 int dfd
= dev_open(devices
[j
].devname
,
1285 remove_partitions(dfd
);
1288 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
1291 fprintf(stderr
, Name
": failed to add "
1296 if (i
< content
->array
.raid_disks
1301 } else if (verbose
> 0)
1302 fprintf(stderr
, Name
": added %s "
1304 devices
[j
].devname
, mddev
,
1305 devices
[j
].i
.disk
.raid_disk
);
1306 } else if (verbose
> 0 && i
< content
->array
.raid_disks
)
1307 fprintf(stderr
, Name
": no uptodate device for "
1312 if (content
->array
.level
== LEVEL_CONTAINER
) {
1314 fprintf(stderr
, Name
": Container %s has been "
1315 "assembled with %d drive%s",
1316 mddev
, okcnt
+sparecnt
, okcnt
+sparecnt
==1?"":"s");
1317 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1318 fprintf(stderr
, " (out of %d)",
1319 content
->array
.raid_disks
);
1320 fprintf(stderr
, "\n");
1322 st
->ss
->free_super(st
);
1323 sysfs_uevent(content
, "change");
1324 wait_for(chosen_name
, mdfd
);
1332 ( enough(content
->array
.level
, content
->array
.raid_disks
,
1333 content
->array
.layout
, clean
, avail
, okcnt
) &&
1334 (okcnt
+ rebuilding_cnt
>= req_cnt
|| start_partial_ok
)
1336 /* This array is good-to-go.
1337 * If a reshape is in progress then we might need to
1338 * continue monitoring it. In that case we start
1339 * it read-only and let the grow code make it writable.
1343 if (content
->reshape_active
&&
1344 content
->delta_disks
<= 0) {
1345 rv
= sysfs_set_str(content
, NULL
,
1346 "array_state", "readonly");
1348 rv
= Grow_continue(mdfd
, st
, content
,
1353 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1356 fprintf(stderr
, Name
": %s has been started with %d drive%s",
1357 mddev
, okcnt
, okcnt
==1?"":"s");
1358 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1359 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1361 fprintf(stderr
, "%s %d rebuilding", sparecnt
?",":" and", rebuilding_cnt
);
1363 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1364 fprintf(stderr
, ".\n");
1366 if (content
->reshape_active
&&
1367 content
->array
.level
>= 4 &&
1368 content
->array
.level
<= 6) {
1369 /* might need to increase the size
1370 * of the stripe cache - default is 256
1372 if (256 < 4 * (content
->array
.chunk_size
/4096)) {
1373 struct mdinfo
*sra
= sysfs_read(mdfd
, 0, 0);
1375 sysfs_set_num(sra
, NULL
,
1376 "stripe_cache_size",
1377 (4 * content
->array
.chunk_size
/ 4096) + 1);
1381 if (okcnt
< (unsigned)content
->array
.raid_disks
) {
1382 /* If any devices did not get added
1383 * because the kernel rejected them based
1384 * on event count, try adding them
1385 * again providing the action policy is
1386 * 're-add' or greater. The bitmap
1387 * might allow them to be included, or
1388 * they will become spares.
1390 for (i
= 0; i
< bestcnt
; i
++) {
1392 if (j
>= 0 && !devices
[j
].uptodate
) {
1393 if (!disk_action_allows(&devices
[j
].i
, st
->ss
->name
, act_re_add
))
1395 rv
= add_disk(mdfd
, st
, content
,
1397 if (rv
== 0 && verbose
>= 0)
1399 Name
": %s has been re-added.\n",
1400 devices
[j
].devname
);
1404 wait_for(mddev
, mdfd
);
1408 /* There is a nasty race with 'mdadm --monitor'.
1409 * If it opens this device before we close it,
1410 * it gets an incomplete open on which IO
1411 * doesn't work and the capacity is
1413 * If we reopen (to check for layered devices)
1414 * before --monitor closes, we loose.
1416 * So: wait upto 1 second for there to be
1417 * a non-zero capacity.
1419 while (usecs
< 1000) {
1420 mdfd
= open(mddev
, O_RDONLY
);
1422 unsigned long long size
;
1423 if (get_dev_size(mdfd
, NULL
, &size
) &&
1435 fprintf(stderr
, Name
": failed to RUN_ARRAY %s: %s\n",
1436 mddev
, strerror(errno
));
1438 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1439 content
->array
.layout
, 1, avail
, okcnt
))
1440 fprintf(stderr
, Name
": Not enough devices to "
1441 "start the array.\n");
1442 else if (!enough(content
->array
.level
,
1443 content
->array
.raid_disks
,
1444 content
->array
.layout
, clean
,
1446 fprintf(stderr
, Name
": Not enough devices to "
1447 "start the array while not clean "
1448 "- consider --force.\n");
1451 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1456 if (runstop
== -1) {
1457 fprintf(stderr
, Name
": %s assembled from %d drive%s",
1458 mddev
, okcnt
, okcnt
==1?"":"s");
1459 if (okcnt
!= (unsigned)content
->array
.raid_disks
)
1460 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1461 fprintf(stderr
, ", but not started.\n");
1466 if (verbose
>= -1) {
1467 fprintf(stderr
, Name
": %s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
1469 fprintf(stderr
, "%s %d rebuilding", sparecnt
?", ":" and ", rebuilding_cnt
);
1471 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1472 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1473 content
->array
.layout
, 1, avail
, okcnt
))
1474 fprintf(stderr
, " - not enough to start the array.\n");
1475 else if (!enough(content
->array
.level
,
1476 content
->array
.raid_disks
,
1477 content
->array
.layout
, clean
,
1479 fprintf(stderr
, " - not enough to start the "
1480 "array while not clean - consider "
1483 if (req_cnt
== (unsigned)content
->array
.raid_disks
)
1484 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1486 fprintf(stderr
, " - need %d of %d to start", req_cnt
, content
->array
.raid_disks
);
1487 fprintf(stderr
, " (use --run to insist).\n");
1491 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1496 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1497 * been updated to point to the current locations of devices.
1498 * so we can just start the array
1501 dev
= makedev(devices
[chosen_drive
].i
.disk
.major
,
1502 devices
[chosen_drive
].i
.disk
.minor
);
1503 if (ioctl(mdfd
, START_ARRAY
, dev
)) {
1504 fprintf(stderr
, Name
": Cannot start array: %s\n",
1515 int assemble_container_content(struct supertype
*st
, int mdfd
,
1516 struct mdinfo
*content
, int runstop
,
1517 char *chosen_name
, int verbose
,
1518 char *backup_file
, int freeze_reshape
)
1520 struct mdinfo
*dev
, *sra
;
1521 int working
= 0, preexist
= 0;
1523 struct map_ent
*map
= NULL
;
1526 sysfs_init(content
, mdfd
, 0);
1528 sra
= sysfs_read(mdfd
, 0, GET_VERSION
);
1529 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0)
1530 if (sysfs_set_array(content
, md_get_version(mdfd
)) != 0)
1533 if (st
->ss
->external
&& content
->recovery_blocked
)
1534 block_subarray(content
);
1538 old_raid_disks
= content
->array
.raid_disks
- content
->delta_disks
;
1539 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
1540 if (sysfs_add_disk(content
, dev
, 1) == 0) {
1541 if (dev
->disk
.raid_disk
>= old_raid_disks
&&
1542 content
->reshape_active
)
1546 } else if (errno
== EEXIST
)
1549 return 1;/* Nothing new, don't try to start */
1551 map_update(&map
, fd2devnum(mdfd
),
1552 content
->text_version
,
1553 content
->uuid
, chosen_name
);
1556 (working
+ preexist
+ expansion
) >=
1557 content
->array
.working_disks
) {
1561 /* There are two types of reshape: container wide or sub-array specific
1562 * Check if metadata requests blocking container wide reshapes
1564 start_reshape
= (content
->reshape_active
&&
1565 !((content
->reshape_active
== CONTAINER_RESHAPE
) &&
1566 (content
->array
.state
& (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))));
1567 if (start_reshape
) {
1568 int spare
= content
->array
.raid_disks
+ expansion
;
1569 if (restore_backup(st
, content
,
1571 spare
, backup_file
, verbose
) == 1)
1574 err
= sysfs_set_str(content
, NULL
,
1575 "array_state", "readonly");
1579 if (st
->ss
->external
) {
1580 if (!mdmon_running(st
->container_dev
))
1581 start_mdmon(st
->container_dev
);
1582 ping_monitor_by_id(st
->container_dev
);
1585 err
= Grow_continue(mdfd
, st
, content
, backup_file
,
1587 } else switch(content
->array
.level
) {
1589 case LEVEL_MULTIPATH
:
1591 err
= sysfs_set_str(content
, NULL
, "array_state",
1595 err
= sysfs_set_str(content
, NULL
, "array_state",
1597 /* start mdmon if needed. */
1599 if (!mdmon_running(st
->container_dev
))
1600 start_mdmon(st
->container_dev
);
1601 ping_monitor_by_id(st
->container_dev
);
1606 sysfs_set_safemode(content
, content
->safe_mode_delay
);
1609 fprintf(stderr
, Name
1610 ": array %s now has %d devices",
1611 chosen_name
, working
+ preexist
);
1613 fprintf(stderr
, Name
1614 ": Started %s with %d devices",
1615 chosen_name
, working
+ preexist
);
1617 fprintf(stderr
, " (%d new)", working
);
1619 fprintf(stderr
, " ( + %d for expansion)",
1621 fprintf(stderr
, "\n");
1624 wait_for(chosen_name
, mdfd
);
1626 /* FIXME should have an O_EXCL and wait for read-auto */
1629 fprintf(stderr
, Name
1630 ": %s assembled with %d devices but "
1632 chosen_name
, working
);