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
;
226 int start_partial_ok
= (runstop
>= 0) &&
227 (force
|| devlist
==NULL
|| auto_assem
);
228 unsigned int num_devs
;
229 struct mddev_dev
*tmpdev
;
231 struct mdinfo
*content
= NULL
;
236 char chosen_name
[1024];
237 struct domainlist
*domains
= NULL
;
239 if (get_linux_version() < 2004000)
243 * If any subdevs are listed, then any that don't
244 * match ident are discarded. Remainder must all match and
246 * If no subdevs, then we scan all devices in the config file, but
247 * there must be something in the identity
251 ident
->uuid_set
== 0 &&
252 (ident
->super_minor
< 0 || ident
->super_minor
== UnSet
) &&
253 ident
->name
[0] == 0 &&
254 (ident
->container
== NULL
|| ident
->member
== NULL
) &&
255 ident
->devices
== NULL
) {
256 fprintf(stderr
, Name
": No identity information available for %s - cannot assemble.\n",
257 mddev
? mddev
: "further assembly");
262 devlist
= conf_get_devs();
266 report_missmatch
= ((inargv
&& verbose
>= 0) || verbose
> 0);
268 /* We come back here when doing auto-assembly and attempting some
269 * set of devices failed. Those are now marked as ->used==2 and
270 * we ignore them and try again
273 tmpdev
= devlist
; num_devs
= 0;
279 tmpdev
= tmpdev
->next
;
282 if (!st
&& ident
->st
) st
= ident
->st
;
285 fprintf(stderr
, Name
": looking for devices for %s\n",
286 mddev
? mddev
: "further assembly");
288 /* first walk the list of devices to find a consistent set
289 * that match the criterea, if that is possible.
290 * We flag the ones we like with 'used'.
292 for (tmpdev
= devlist
;
294 tmpdev
= tmpdev
? tmpdev
->next
: NULL
) {
295 char *devname
= tmpdev
->devname
;
298 struct supertype
*tst
;
299 struct dev_policy
*pol
= NULL
;
300 int found_container
= 0;
302 if (tmpdev
->used
> 1) continue;
304 if (ident
->devices
&&
305 !match_oneof(ident
->devices
, devname
)) {
306 if (report_missmatch
)
307 fprintf(stderr
, Name
": %s is not one of %s\n", devname
, ident
->devices
);
313 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
315 if (report_missmatch
)
316 fprintf(stderr
, Name
": cannot open device %s: %s\n",
317 devname
, strerror(errno
));
319 } else if (fstat(dfd
, &stb
)< 0) {
321 fprintf(stderr
, Name
": fstat failed for %s: %s\n",
322 devname
, strerror(errno
));
324 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
325 fprintf(stderr
, Name
": %s is not a block device.\n",
328 } else if (must_be_container(dfd
)) {
330 /* already found some components, this cannot
333 if (report_missmatch
)
334 fprintf(stderr
, Name
": %s is a container, but we are looking for components\n",
337 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
338 } if (!tst
&& (tst
= super_by_fd(dfd
, NULL
)) == NULL
) {
339 if (report_missmatch
)
340 fprintf(stderr
, Name
": not a recognisable container: %s\n",
344 } else if (!tst
->ss
->load_container
345 || tst
->ss
->load_container(tst
, dfd
, NULL
)) {
346 if (report_missmatch
)
347 fprintf(stderr
, Name
": no correct container type: %s\n",
350 } else if (auto_assem
&&
351 !conf_test_metadata(tst
->ss
->name
, (pol
= devnum_policy(stb
.st_rdev
)),
352 tst
->ss
->match_home(tst
, homehost
) == 1)) {
353 if (report_missmatch
)
354 fprintf(stderr
, Name
": %s has metadata type %s for which "
355 "auto-assembly is disabled\n",
356 devname
, tst
->ss
->name
);
361 if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
362 if (report_missmatch
)
363 fprintf(stderr
, Name
": no recogniseable superblock on %s\n",
366 } else if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
367 if (report_missmatch
)
368 fprintf(stderr
, Name
": no RAID superblock on %s\n",
371 } else if (tst
->ss
->compare_super
== NULL
) {
372 if (report_missmatch
)
373 fprintf(stderr
, Name
": Cannot assemble %s metadata on %s\n",
374 tst
->ss
->name
, devname
);
376 } else if (auto_assem
&& st
== NULL
&&
377 !conf_test_metadata(tst
->ss
->name
, (pol
= devnum_policy(stb
.st_rdev
)),
378 tst
->ss
->match_home(tst
, homehost
) == 1)) {
379 if (report_missmatch
)
380 fprintf(stderr
, Name
": %s has metadata type %s for which "
381 "auto-assembly is disabled\n",
382 devname
, tst
->ss
->name
);
386 if (dfd
>= 0) close(dfd
);
387 if (tmpdev
->used
== 2) {
388 if (auto_assem
|| !inargv
)
389 /* Ignore unrecognised devices during auto-assembly */
391 if (ident
->uuid_set
|| ident
->name
[0] ||
392 ident
->super_minor
!= UnSet
)
393 /* Ignore unrecognised device if looking for
398 fprintf(stderr
, Name
": %s has no superblock - assembly aborted\n",
401 st
->ss
->free_super(st
);
402 dev_policy_free(pol
);
403 domain_free(domains
);
407 if (found_container
) {
408 /* tmpdev is a container. We need to be either
409 * looking for a member, or auto-assembling
412 if (ident
->container
) {
413 if (ident
->container
[0] == '/' &&
414 !same_dev(ident
->container
, devname
)) {
415 if (report_missmatch
)
416 fprintf(stderr
, Name
": %s is not the container required (%s)\n",
417 devname
, ident
->container
);
420 if (ident
->container
[0] != '/') {
425 tst
->ss
->getinfo_super(tst
, content
, NULL
);
427 if (!parse_uuid(ident
->container
, uuid
) ||
428 !same_uuid(content
->uuid
, uuid
, tst
->ss
->swapuuid
)) {
429 if (report_missmatch
)
430 fprintf(stderr
, Name
": %s has wrong UUID to be required container\n",
436 /* It is worth looking inside this container.
439 fprintf(stderr
, Name
": looking in container %s\n",
442 for (content
= tst
->ss
->container_content(tst
, NULL
);
444 content
= content
->next
) {
446 if (!ident_matches(ident
, content
, tst
,
448 report_missmatch
? devname
: NULL
))
449 /* message already printed */;
450 else if (is_member_busy(content
->text_version
)) {
451 if (report_missmatch
)
452 fprintf(stderr
, Name
": member %s in %s is already assembled\n",
453 content
->text_version
,
455 } else if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
456 /* do not assemble arrays with unsupported configurations */
457 fprintf(stderr
, Name
": Cannot activate member %s in %s.\n",
458 content
->text_version
,
465 goto loop
; /* empty container */
468 st
= tst
; tst
= NULL
;
469 if (!auto_assem
&& inargv
&& tmpdev
->next
!= NULL
) {
470 fprintf(stderr
, Name
": %s is a container, but is not "
471 "only device given: confused and aborting\n",
473 st
->ss
->free_super(st
);
474 dev_policy_free(pol
);
475 domain_free(domains
);
479 fprintf(stderr
, Name
": found match on member %s in %s\n",
480 content
->text_version
, devname
);
482 /* make sure we finished the loop */
488 tst
->ss
->getinfo_super(tst
, content
, NULL
);
490 if (!ident_matches(ident
, content
, tst
,
492 report_missmatch
? devname
: NULL
))
497 if (st
->minor_version
== -1)
498 st
->minor_version
= tst
->minor_version
;
500 if (memcmp(content
->uuid
, uuid_zero
,
501 sizeof(int[4])) == 0) {
502 /* this is a floating spare. It cannot define
503 * an array unless there are no more arrays of
504 * this type to be found. It can be included
505 * in an array of this type though.
511 if (st
->ss
!= tst
->ss
||
512 st
->minor_version
!= tst
->minor_version
||
513 st
->ss
->compare_super(st
, tst
) != 0) {
514 /* Some mismatch. If exactly one array matches this host,
515 * we can resolve on that one.
516 * Or, if we are auto assembling, we just ignore the second
522 int first
= st
->ss
->match_home(st
, homehost
);
523 int last
= tst
->ss
->match_home(tst
, homehost
);
525 (first
== 1 || last
== 1)) {
526 /* We can do something */
527 if (first
) {/* just ignore this one */
528 if (report_missmatch
)
529 fprintf(stderr
, Name
": %s misses out due to wrong homehost\n",
532 } else { /* reject all those sofar */
533 struct mddev_dev
*td
;
534 if (report_missmatch
)
535 fprintf(stderr
, Name
": %s overrides previous devices due to good homehost\n",
537 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
545 fprintf(stderr
, Name
": superblock on %s doesn't match others - assembly aborted\n",
547 tst
->ss
->free_super(tst
);
548 st
->ss
->free_super(st
);
549 dev_policy_free(pol
);
550 domain_free(domains
);
556 /* Collect domain information from members only */
557 if (tmpdev
&& tmpdev
->used
== 1) {
559 pol
= devnum_policy(stb
.st_rdev
);
560 domain_merge(&domains
, pol
, tst
?tst
->ss
->name
:NULL
);
562 dev_policy_free(pol
);
565 tst
->ss
->free_super(tst
);
568 /* Check if we found some imsm spares but no members */
571 memcmp(uuid_zero
, ident
->uuid
,sizeof(uuid_zero
)) == 0)) &&
573 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
574 if (tmpdev
->used
!= 3)
580 /* we need sb from one of the spares */
581 int dfd
= dev_open(tmpdev
->devname
, O_RDONLY
);
583 st
->ss
->load_super(st
, dfd
, NULL
))
590 /* Now reject spares that don't match domains of identified members */
591 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
593 if (tmpdev
->used
!= 3)
595 if (stat(tmpdev
->devname
, &stb
)< 0) {
596 fprintf(stderr
, Name
": fstat failed for %s: %s\n",
597 tmpdev
->devname
, strerror(errno
));
600 struct dev_policy
*pol
= devnum_policy(stb
.st_rdev
);
601 int dt
= domain_test(domains
, pol
, NULL
);
602 if (inargv
&& dt
!= 0)
603 /* take this spare as domains match
604 * if there are any */
606 else if (!inargv
&& dt
== 1)
607 /* device wasn't explicitly listed, so need
608 * explicit domain match - which we have */
611 /* if domains don't match mark as unused */
613 dev_policy_free(pol
);
616 domain_free(domains
);
618 if (!st
|| !st
->sb
|| !content
)
621 /* Now need to open the array device. Use create_mddev */
622 if (content
== &info
)
623 st
->ss
->getinfo_super(st
, content
, NULL
);
625 trustworthy
= FOREIGN
;
626 name
= content
->name
;
627 switch (st
->ss
->match_home(st
, homehost
)
628 ?: st
->ss
->match_home(st
, "any")) {
631 name
= strchr(content
->name
, ':');
635 name
= content
->name
;
639 /* If the array is listed in mdadm.conf or on
640 * command line, then we trust the name
641 * even if the array doesn't look local
646 content
->array
.level
== LEVEL_CONTAINER
) {
647 name
= content
->text_version
;
648 trustworthy
= METADATA
;
651 if (name
[0] && trustworthy
!= LOCAL
&&
652 ! require_homehost
&&
653 conf_name_is_free(name
))
656 if (trustworthy
== LOCAL
&&
658 /* Ignore 'host:' prefix of name */
659 name
= strchr(name
, ':')+1;
661 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
664 st
->ss
->free_super(st
);
670 vers
= md_get_version(mdfd
);
672 fprintf(stderr
, Name
": Assemble requires driver version 0.90.0 or later.\n"
673 " Upgrade your kernel or try --build\n");
677 if (mddev_busy(fd2devnum(mdfd
))) {
678 fprintf(stderr
, Name
": %s already active, cannot restart it!\n",
680 for (tmpdev
= devlist
;
681 tmpdev
&& tmpdev
->used
!= 1;
682 tmpdev
= tmpdev
->next
)
684 if (tmpdev
&& auto_assem
)
685 fprintf(stderr
, Name
": %s needed for %s...\n",
686 mddev
, tmpdev
->devname
);
689 st
->ss
->free_super(st
);
694 ioctl(mdfd
, STOP_ARRAY
, NULL
); /* just incase it was started but has no content */
697 if (content
!= &info
) {
698 /* This is a member of a container. Try starting the array. */
700 err
= assemble_container_content(st
, mdfd
, content
, runstop
,
701 chosen_name
, verbose
,
702 backup_file
, freeze_reshape
);
708 /* Ok, no bad inconsistancy, we can try updating etc */
709 devices
= malloc(num_devs
* sizeof(*devices
));
710 devmap
= calloc(num_devs
* content
->array
.raid_disks
, 1);
711 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) if (tmpdev
->used
== 1) {
712 char *devname
= tmpdev
->devname
;
714 /* looks like a good enough match to update the super block if needed */
718 /* prepare useful information in info structures */
720 struct supertype
*tst
;
724 if (strcmp(update
, "uuid")==0 &&
727 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
728 read(rfd
, ident
->uuid
, 16) != 16) {
729 *(__u32
*)(ident
->uuid
) = random();
730 *(__u32
*)(ident
->uuid
+1) = random();
731 *(__u32
*)(ident
->uuid
+2) = random();
732 *(__u32
*)(ident
->uuid
+3) = random();
734 if (rfd
>= 0) close(rfd
);
736 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
739 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
740 fprintf(stderr
, Name
": cannot re-read metadata from %s - aborting\n",
749 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
751 memcpy(content
->uuid
, ident
->uuid
, 16);
752 strcpy(content
->name
, ident
->name
);
753 content
->array
.md_minor
= minor(stb2
.st_rdev
);
755 if (strcmp(update
, "byteorder") == 0)
758 err
= tst
->ss
->update_super(tst
, content
, update
,
764 Name
": --update=%s not understood"
765 " for %s metadata\n",
766 update
, tst
->ss
->name
);
767 tst
->ss
->free_super(tst
);
775 if (strcmp(update
, "uuid")==0 &&
778 memcpy(ident
->uuid
, content
->uuid
, 16);
780 if (tst
->ss
->store_super(tst
, dfd
))
781 fprintf(stderr
, Name
": Could not re-write superblock on %s.\n",
785 if (strcmp(update
, "uuid")==0 &&
786 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
787 if (bitmap_update_uuid(ident
->bitmap_fd
,
789 tst
->ss
->swapuuid
) != 0)
790 fprintf(stderr
, Name
": Could not update uuid on external bitmap.\n");
794 tst
->ss
->free_super(tst
);
798 struct supertype
*tst
= dup_super(st
);
800 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
802 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
803 fprintf(stderr
, Name
": cannot re-read metadata from %s - aborting\n",
812 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
813 tst
->ss
->free_super(tst
);
820 fprintf(stderr
, Name
": %s is identified as a member of %s, slot %d.\n",
821 devname
, mddev
, content
->disk
.raid_disk
);
822 devices
[devcnt
].devname
= devname
;
823 devices
[devcnt
].uptodate
= 0;
824 devices
[devcnt
].i
= *content
;
825 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
826 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
827 if (most_recent
< devcnt
) {
828 if (devices
[devcnt
].i
.events
829 > devices
[most_recent
].i
.events
)
830 most_recent
= devcnt
;
832 if (content
->array
.level
== LEVEL_MULTIPATH
)
833 /* with multipath, the raid_disk from the superblock is meaningless */
836 i
= devices
[devcnt
].i
.disk
.raid_disk
;
838 if (nextspare
< content
->array
.raid_disks
)
839 nextspare
= content
->array
.raid_disks
;
842 if (i
>= content
->array
.raid_disks
&&
848 int newbestcnt
= i
+10;
849 int *newbest
= malloc(sizeof(int)*newbestcnt
);
851 for (c
=0; c
< newbestcnt
; c
++)
853 newbest
[c
] = best
[c
];
858 bestcnt
= newbestcnt
;
861 devices
[best
[i
]].i
.events
862 == devices
[devcnt
].i
.events
863 && (devices
[best
[i
]].i
.disk
.minor
864 != devices
[devcnt
].i
.disk
.minor
)
866 && content
->array
.level
!= LEVEL_MULTIPATH
) {
867 /* two different devices with identical superblock.
868 * Could be a mis-detection caused by overlapping
869 * partitions. fail-safe.
871 fprintf(stderr
, Name
": WARNING %s and %s appear"
872 " to have very similar superblocks.\n"
873 " If they are really different, "
874 "please --zero the superblock on one\n"
875 " If they are the same or overlap,"
876 " please remove one from %s.\n",
877 devices
[best
[i
]].devname
, devname
,
878 inargv
? "the list" :
879 "the\n DEVICE list in mdadm.conf"
887 || (devices
[best
[i
]].i
.events
888 < devices
[devcnt
].i
.events
))
895 fprintf(stderr
, Name
": no devices found for %s\n",
898 st
->ss
->free_super(st
);
905 if (update
&& strcmp(update
, "byteorder")==0)
906 st
->minor_version
= 90;
908 st
->ss
->getinfo_super(st
, content
, NULL
);
909 clean
= content
->array
.state
& 1;
911 /* now we have some devices that might be suitable.
914 avail
= malloc(content
->array
.raid_disks
);
915 memset(avail
, 0, content
->array
.raid_disks
);
919 for (i
=0; i
< bestcnt
; i
++) {
921 int event_margin
= 1; /* always allow a difference of '1'
922 * like the kernel does
925 /* note: we ignore error flags in multipath arrays
926 * as they don't make sense
928 if (content
->array
.level
!= LEVEL_MULTIPATH
)
929 if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_ACTIVE
))) {
930 if (!(devices
[j
].i
.disk
.state
931 & (1<<MD_DISK_FAULTY
))) {
932 devices
[j
].uptodate
= 1;
937 /* If this device thinks that 'most_recent' has failed, then
938 * we must reject this device.
940 if (j
!= most_recent
&&
941 content
->array
.raid_disks
> 0 &&
942 devices
[most_recent
].i
.disk
.raid_disk
>= 0 &&
943 devmap
[j
* content
->array
.raid_disks
+ devices
[most_recent
].i
.disk
.raid_disk
] == 0) {
945 fprintf(stderr
, Name
": ignoring %s as it reports %s as failed\n",
946 devices
[j
].devname
, devices
[most_recent
].devname
);
950 if (devices
[j
].i
.events
+event_margin
>=
951 devices
[most_recent
].i
.events
) {
952 devices
[j
].uptodate
= 1;
953 if (i
< content
->array
.raid_disks
) {
954 if (devices
[j
].i
.recovery_start
== MaxSector
||
955 (content
->reshape_active
&&
956 j
>= content
->array
.raid_disks
- content
->delta_disks
)) {
967 (!enough(content
->array
.level
, content
->array
.raid_disks
,
968 content
->array
.layout
, 1,
971 (content
->reshape_active
&& content
->delta_disks
> 0 &&
972 !enough(content
->array
.level
, (content
->array
.raid_disks
973 - content
->delta_disks
),
974 content
->new_layout
, 1,
977 /* Choose the newest best drive which is
978 * not up-to-date, update the superblock
982 struct supertype
*tst
;
983 unsigned long long current_events
;
985 for (i
= 0; i
< content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
988 !devices
[j
].uptodate
&&
989 devices
[j
].i
.recovery_start
== MaxSector
&&
992 > devices
[chosen_drive
].i
.events
))
995 if (chosen_drive
< 0)
997 current_events
= devices
[chosen_drive
].i
.events
;
1000 fprintf(stderr
, Name
": forcing event count in %s(%d) from %d upto %d\n",
1001 devices
[chosen_drive
].devname
,
1002 devices
[chosen_drive
].i
.disk
.raid_disk
,
1003 (int)(devices
[chosen_drive
].i
.events
),
1004 (int)(devices
[most_recent
].i
.events
));
1005 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
1007 fprintf(stderr
, Name
": Couldn't open %s for write - not updating\n",
1008 devices
[chosen_drive
].devname
);
1009 devices
[chosen_drive
].i
.events
= 0;
1012 tst
= dup_super(st
);
1013 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
1015 fprintf(stderr
, Name
": RAID superblock disappeared from %s - not updating.\n",
1016 devices
[chosen_drive
].devname
);
1017 devices
[chosen_drive
].i
.events
= 0;
1020 content
->events
= devices
[most_recent
].i
.events
;
1021 tst
->ss
->update_super(tst
, content
, "force-one",
1022 devices
[chosen_drive
].devname
, verbose
,
1025 if (tst
->ss
->store_super(tst
, fd
)) {
1027 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
1028 devices
[chosen_drive
].devname
);
1029 devices
[chosen_drive
].i
.events
= 0;
1030 tst
->ss
->free_super(tst
);
1034 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
1035 devices
[chosen_drive
].uptodate
= 1;
1036 avail
[chosen_drive
] = 1;
1038 tst
->ss
->free_super(tst
);
1040 /* If there are any other drives of the same vintage,
1041 * add them in as well. We can't lose and we might gain
1043 for (i
= 0; i
< content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
1046 !devices
[j
].uptodate
&&
1047 devices
[j
].i
.events
== current_events
) {
1054 /* Now we want to look at the superblock which the kernel will base things on
1055 * and compare the devices that we think are working with the devices that the
1056 * superblock thinks are working.
1057 * If there are differences and --force is given, then update this chosen
1061 st
->ss
->free_super(st
);
1062 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
++) {
1068 if (!devices
[j
].uptodate
)
1070 if (devices
[j
].i
.events
< devices
[most_recent
].i
.events
)
1073 if ((fd
=dev_open(devices
[j
].devname
, O_RDONLY
|O_EXCL
))< 0) {
1074 fprintf(stderr
, Name
": Cannot open %s: %s\n",
1075 devices
[j
].devname
, strerror(errno
));
1080 if (st
->ss
->load_super(st
,fd
, NULL
)) {
1082 fprintf(stderr
, Name
": RAID superblock has disappeared from %s\n",
1083 devices
[j
].devname
);
1090 if (st
->sb
== NULL
) {
1091 fprintf(stderr
, Name
": No suitable drives found for %s\n", mddev
);
1096 st
->ss
->getinfo_super(st
, content
, NULL
);
1098 sysfs_init(content
, mdfd
, 0);
1100 for (i
=0; i
<bestcnt
; i
++) {
1102 unsigned int desired_state
;
1104 if (i
< content
->array
.raid_disks
)
1105 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
1111 if (!devices
[j
].uptodate
)
1114 devices
[j
].i
.disk
.state
= desired_state
;
1115 if (!(devices
[j
].i
.array
.state
& 1))
1118 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
1119 verbose
, 0, NULL
)) {
1122 fprintf(stderr
, Name
": "
1123 "clearing FAULTY flag for device %d in %s for %s\n",
1124 j
, mddev
, devices
[j
].devname
);
1128 fprintf(stderr
, Name
": "
1129 "device %d in %s has wrong state in superblock, but %s seems ok\n",
1130 i
, mddev
, devices
[j
].devname
);
1134 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
1135 fprintf(stderr
, Name
": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1140 if (force
&& !clean
&&
1141 !enough(content
->array
.level
, content
->array
.raid_disks
,
1142 content
->array
.layout
, clean
,
1144 change
+= st
->ss
->update_super(st
, content
, "force-array",
1145 devices
[chosen_drive
].devname
, verbose
,
1152 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
1154 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
1155 devices
[chosen_drive
].devname
);
1160 if (st
->ss
->store_super(st
, fd
)) {
1162 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
1163 devices
[chosen_drive
].devname
);
1169 fprintf(stderr
, Name
": Marking array %s as 'clean'\n",
1174 /* If we are in the middle of a reshape we may need to restore saved data
1175 * that was moved aside due to the reshape overwriting live data
1176 * The code of doing this lives in Grow.c
1179 if (content
->reshape_active
) {
1181 int *fdlist
= malloc(sizeof(int)* bestcnt
);
1183 fprintf(stderr
, Name
":%s has an active reshape - checking "
1184 "if critical section needs to be restored\n",
1186 for (i
=0; i
<bestcnt
; i
++) {
1189 fdlist
[i
] = dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
1190 if (fdlist
[i
] < 0) {
1191 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
1192 devices
[j
].devname
);
1200 if (st
->ss
->external
&& st
->ss
->recover_backup
)
1201 err
= st
->ss
->recover_backup(st
, content
);
1203 err
= Grow_restart(st
, content
, fdlist
, bestcnt
,
1204 backup_file
, verbose
> 0);
1205 if (err
&& invalid_backup
) {
1207 fprintf(stderr
, Name
": continuing"
1208 " without restoring backup\n");
1214 if (fdlist
[i
]>=0) close(fdlist
[i
]);
1217 fprintf(stderr
, Name
": Failed to restore critical section for reshape, sorry.\n");
1218 if (backup_file
== NULL
)
1219 fprintf(stderr
," Possibly you needed to specify the --backup-file\n");
1226 /* count number of in-sync devices according to the superblock.
1227 * We must have this number to start the array without -s or -R
1229 req_cnt
= content
->array
.working_disks
;
1231 /* Almost ready to actually *do* something */
1235 /* First, fill in the map, so that udev can find our name
1236 * as soon as we become active.
1238 map_update(NULL
, fd2devnum(mdfd
), content
->text_version
,
1239 content
->uuid
, chosen_name
);
1241 rv
= set_array_info(mdfd
, st
, content
);
1243 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
1244 mddev
, strerror(errno
));
1245 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1250 if (ident
->bitmap_fd
>= 0) {
1251 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
1252 fprintf(stderr
, Name
": SET_BITMAP_FILE failed.\n");
1253 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1258 } else if (ident
->bitmap_file
) {
1259 /* From config file */
1260 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
1262 fprintf(stderr
, Name
": Could not open bitmap file %s\n",
1263 ident
->bitmap_file
);
1264 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1269 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
1270 fprintf(stderr
, Name
": Failed to set bitmapfile for %s\n", mddev
);
1272 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1280 /* First, add the raid disks, but add the chosen one last */
1281 for (i
=0; i
<= bestcnt
; i
++) {
1285 if (j
== chosen_drive
)
1290 if (j
>= 0 /* && devices[j].uptodate */) {
1291 int dfd
= dev_open(devices
[j
].devname
,
1294 remove_partitions(dfd
);
1297 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
1300 fprintf(stderr
, Name
": failed to add "
1305 if (i
< content
->array
.raid_disks
1310 } else if (verbose
> 0)
1311 fprintf(stderr
, Name
": added %s "
1313 devices
[j
].devname
, mddev
,
1314 devices
[j
].i
.disk
.raid_disk
);
1315 } else if (verbose
> 0 && i
< content
->array
.raid_disks
)
1316 fprintf(stderr
, Name
": no uptodate device for "
1321 if (content
->array
.level
== LEVEL_CONTAINER
) {
1323 fprintf(stderr
, Name
": Container %s has been "
1324 "assembled with %d drive%s",
1325 mddev
, okcnt
+sparecnt
, okcnt
+sparecnt
==1?"":"s");
1326 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1327 fprintf(stderr
, " (out of %d)",
1328 content
->array
.raid_disks
);
1329 fprintf(stderr
, "\n");
1331 st
->ss
->free_super(st
);
1332 sysfs_uevent(content
, "change");
1333 wait_for(chosen_name
, mdfd
);
1341 ( enough(content
->array
.level
, content
->array
.raid_disks
,
1342 content
->array
.layout
, clean
, avail
) &&
1343 (okcnt
+ rebuilding_cnt
>= req_cnt
|| start_partial_ok
)
1345 /* This array is good-to-go.
1346 * If a reshape is in progress then we might need to
1347 * continue monitoring it. In that case we start
1348 * it read-only and let the grow code make it writable.
1352 if (content
->reshape_active
&&
1353 content
->delta_disks
<= 0) {
1354 rv
= sysfs_set_str(content
, NULL
,
1355 "array_state", "readonly");
1357 rv
= Grow_continue(mdfd
, st
, content
,
1362 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1365 fprintf(stderr
, Name
": %s has been started with %d drive%s",
1366 mddev
, okcnt
, okcnt
==1?"":"s");
1367 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1368 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1370 fprintf(stderr
, "%s %d rebuilding", sparecnt
?",":" and", rebuilding_cnt
);
1372 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1373 fprintf(stderr
, ".\n");
1375 if (content
->reshape_active
&&
1376 content
->array
.level
>= 4 &&
1377 content
->array
.level
<= 6) {
1378 /* might need to increase the size
1379 * of the stripe cache - default is 256
1381 if (256 < 4 * (content
->array
.chunk_size
/4096)) {
1382 struct mdinfo
*sra
= sysfs_read(mdfd
, 0, 0);
1384 sysfs_set_num(sra
, NULL
,
1385 "stripe_cache_size",
1386 (4 * content
->array
.chunk_size
/ 4096) + 1);
1390 if (okcnt
< (unsigned)content
->array
.raid_disks
) {
1391 /* If any devices did not get added
1392 * because the kernel rejected them based
1393 * on event count, try adding them
1394 * again providing the action policy is
1395 * 're-add' or greater. The bitmap
1396 * might allow them to be included, or
1397 * they will become spares.
1399 for (i
= 0; i
< bestcnt
; i
++) {
1401 if (j
>= 0 && !devices
[j
].uptodate
) {
1402 if (!disk_action_allows(&devices
[j
].i
, st
->ss
->name
, act_re_add
))
1404 rv
= add_disk(mdfd
, st
, content
,
1406 if (rv
== 0 && verbose
>= 0)
1408 Name
": %s has been re-added.\n",
1409 devices
[j
].devname
);
1413 wait_for(mddev
, mdfd
);
1417 /* There is a nasty race with 'mdadm --monitor'.
1418 * If it opens this device before we close it,
1419 * it gets an incomplete open on which IO
1420 * doesn't work and the capacity is
1422 * If we reopen (to check for layered devices)
1423 * before --monitor closes, we loose.
1425 * So: wait upto 1 second for there to be
1426 * a non-zero capacity.
1428 while (usecs
< 1000) {
1429 mdfd
= open(mddev
, O_RDONLY
);
1431 unsigned long long size
;
1432 if (get_dev_size(mdfd
, NULL
, &size
) &&
1444 fprintf(stderr
, Name
": failed to RUN_ARRAY %s: %s\n",
1445 mddev
, strerror(errno
));
1447 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1448 content
->array
.layout
, 1, avail
))
1449 fprintf(stderr
, Name
": Not enough devices to "
1450 "start the array.\n");
1451 else if (!enough(content
->array
.level
,
1452 content
->array
.raid_disks
,
1453 content
->array
.layout
, clean
,
1455 fprintf(stderr
, Name
": Not enough devices to "
1456 "start the array while not clean "
1457 "- consider --force.\n");
1460 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1465 if (runstop
== -1) {
1466 fprintf(stderr
, Name
": %s assembled from %d drive%s",
1467 mddev
, okcnt
, okcnt
==1?"":"s");
1468 if (okcnt
!= (unsigned)content
->array
.raid_disks
)
1469 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1470 fprintf(stderr
, ", but not started.\n");
1475 if (verbose
>= -1) {
1476 fprintf(stderr
, Name
": %s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
1478 fprintf(stderr
, "%s %d rebuilding", sparecnt
?", ":" and ", rebuilding_cnt
);
1480 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1481 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1482 content
->array
.layout
, 1, avail
))
1483 fprintf(stderr
, " - not enough to start the array.\n");
1484 else if (!enough(content
->array
.level
,
1485 content
->array
.raid_disks
,
1486 content
->array
.layout
, clean
,
1488 fprintf(stderr
, " - not enough to start the "
1489 "array while not clean - consider "
1492 if (req_cnt
== (unsigned)content
->array
.raid_disks
)
1493 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1495 fprintf(stderr
, " - need %d of %d to start", req_cnt
, content
->array
.raid_disks
);
1496 fprintf(stderr
, " (use --run to insist).\n");
1500 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1505 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1506 * been updated to point to the current locations of devices.
1507 * so we can just start the array
1510 dev
= makedev(devices
[chosen_drive
].i
.disk
.major
,
1511 devices
[chosen_drive
].i
.disk
.minor
);
1512 if (ioctl(mdfd
, START_ARRAY
, dev
)) {
1513 fprintf(stderr
, Name
": Cannot start array: %s\n",
1524 int assemble_container_content(struct supertype
*st
, int mdfd
,
1525 struct mdinfo
*content
, int runstop
,
1526 char *chosen_name
, int verbose
,
1527 char *backup_file
, int freeze_reshape
)
1529 struct mdinfo
*dev
, *sra
;
1530 int working
= 0, preexist
= 0;
1532 struct map_ent
*map
= NULL
;
1535 sysfs_init(content
, mdfd
, 0);
1537 sra
= sysfs_read(mdfd
, 0, GET_VERSION
);
1538 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0)
1539 if (sysfs_set_array(content
, md_get_version(mdfd
)) != 0) {
1545 if (st
->ss
->external
&& content
->recovery_blocked
)
1546 block_subarray(content
);
1550 old_raid_disks
= content
->array
.raid_disks
- content
->delta_disks
;
1551 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
1552 if (sysfs_add_disk(content
, dev
, 1) == 0) {
1553 if (dev
->disk
.raid_disk
>= old_raid_disks
&&
1554 content
->reshape_active
)
1558 } else if (errno
== EEXIST
)
1560 if (working
+ expansion
== 0)
1561 return 1;/* Nothing new, don't try to start */
1563 map_update(&map
, fd2devnum(mdfd
),
1564 content
->text_version
,
1565 content
->uuid
, chosen_name
);
1568 (working
+ preexist
+ expansion
) >=
1569 content
->array
.working_disks
) {
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
))));
1579 if (start_reshape
) {
1580 int spare
= content
->array
.raid_disks
+ expansion
;
1581 if (restore_backup(st
, content
,
1583 spare
, backup_file
, verbose
) == 1)
1586 err
= sysfs_set_str(content
, NULL
,
1587 "array_state", "readonly");
1591 if (st
->ss
->external
) {
1592 if (!mdmon_running(st
->container_dev
))
1593 start_mdmon(st
->container_dev
);
1594 ping_monitor_by_id(st
->container_dev
);
1595 if (mdmon_running(st
->container_dev
) &&
1596 st
->update_tail
== NULL
)
1597 st
->update_tail
= &st
->updates
;
1600 err
= Grow_continue(mdfd
, st
, content
, backup_file
,
1602 } else switch(content
->array
.level
) {
1604 case LEVEL_MULTIPATH
:
1606 err
= sysfs_set_str(content
, NULL
, "array_state",
1610 err
= sysfs_set_str(content
, NULL
, "array_state",
1612 /* start mdmon if needed. */
1614 if (!mdmon_running(st
->container_dev
))
1615 start_mdmon(st
->container_dev
);
1616 ping_monitor_by_id(st
->container_dev
);
1621 sysfs_set_safemode(content
, content
->safe_mode_delay
);
1624 fprintf(stderr
, Name
1625 ": array %s now has %d device%s",
1626 chosen_name
, working
+ preexist
,
1627 working
+ preexist
== 1 ? "":"s");
1629 fprintf(stderr
, Name
1630 ": Started %s with %d device%s",
1631 chosen_name
, working
+ preexist
,
1632 working
+ preexist
== 1 ? "":"s");
1634 fprintf(stderr
, " (%d new)", working
);
1636 fprintf(stderr
, " ( + %d for expansion)",
1638 fprintf(stderr
, "\n");
1641 wait_for(chosen_name
, mdfd
);
1643 /* FIXME should have an O_EXCL and wait for read-auto */
1646 fprintf(stderr
, Name
1647 ": %s assembled with %d device%s",
1648 chosen_name
, preexist
+ working
,
1649 preexist
+ working
== 1 ? "":"s");
1651 fprintf(stderr
, " (%d new)", working
);
1652 fprintf(stderr
, " but not started\n");