]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Assemble.c
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 int Assemble(struct supertype
*st
, char *mddev
,
75 mddev_dev_t devlist
, char *backup_file
,
76 int readonly
, int runstop
,
77 char *update
, char *homehost
, int require_homehost
,
78 int verbose
, int force
)
81 * The task of Assemble is to find a collection of
82 * devices that should (according to their superblocks)
83 * form an array, and to give this collection to the MD driver.
84 * In Linux-2.4 and later, this involves submitting a
85 * SET_ARRAY_INFO ioctl with no arg - to prepare
86 * the array - and then submit a number of
87 * ADD_NEW_DISK ioctls to add disks into
88 * the array. Finally RUN_ARRAY might
89 * be submitted to start the array.
91 * Much of the work of Assemble is in finding and/or
92 * checking the disks to make sure they look right.
94 * If mddev is not set, then scan must be set and we
95 * read through the config file for dev+uuid mapping
96 * We recurse, setting mddev, for each device that
98 * - has a valid uuid (or any uuid if !uuidset)
100 * If mddev is set, we try to determine state of md.
101 * check version - must be at least 0.90.0
102 * check kernel version. must be at least 2.4.
103 * If not, we can possibly fall back on START_ARRAY
104 * Try to GET_ARRAY_INFO.
105 * If possible, give up
106 * If not, try to STOP_ARRAY just to make sure
108 * If !uuidset and scan, look in conf-file for uuid
109 * If not found, give up
110 * If !devlist and scan and uuidset, get list of devs from conf-file
113 * Check superblock - discard if bad
114 * Check uuid (set if we don't have one) - discard if no match
115 * Check superblock similarity if we have a superblock - discard if different
116 * Record events, devicenum
117 * This should give us a list of devices for the array
118 * We should collect the most recent event number
120 * Count disks with recent enough event count
121 * While force && !enough disks
122 * Choose newest rejected disks, update event count
123 * mark clean and rewrite superblock
126 * foreach device with recent events : ADD_NEW_DISK
127 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
129 * Check the device numbers in superblock are right
130 * update superblock if any changes
136 int auto_assem
= (mddev
== NULL
&& !ident
->uuid_set
&&
137 ident
->super_minor
== UnSet
&& ident
->name
[0] == 0
138 && (ident
->container
== NULL
|| ident
->member
== NULL
));
140 int vers
= vers
; /* Keep gcc quite - it really is initialised */
143 int uptodate
; /* set once we decide that this device is as
144 * recent as everything else in the array.
148 int *best
= NULL
; /* indexed by raid_disk */
149 unsigned int bestcnt
= 0;
151 unsigned int okcnt
, sparecnt
;
152 unsigned int req_cnt
;
158 int report_missmatch
;
160 int start_partial_ok
= (runstop
>= 0) &&
161 (force
|| devlist
==NULL
|| auto_assem
);
162 unsigned int num_devs
;
165 struct mdinfo
*content
= NULL
;
170 char chosen_name
[1024];
172 if (get_linux_version() < 2004000)
176 * If any subdevs are listed, then any that don't
177 * match ident are discarded. Remainder must all match and
179 * If no subdevs, then we scan all devices in the config file, but
180 * there must be something in the identity
184 ident
->uuid_set
== 0 &&
185 ident
->super_minor
< 0 &&
186 ident
->name
[0] == 0 &&
187 (ident
->container
== NULL
|| ident
->member
== NULL
) &&
188 ident
->devices
== NULL
) {
189 fprintf(stderr
, Name
": No identity information available for %s - cannot assemble.\n",
190 mddev
? mddev
: "further assembly");
195 devlist
= conf_get_devs();
199 report_missmatch
= ((inargv
&& verbose
>= 0) || verbose
> 0);
201 /* We come back here when doing auto-assembly and attempting some
202 * set of devices failed. Those are now marked as ->used==2 and
203 * we ignore them and try again
206 tmpdev
= devlist
; num_devs
= 0;
212 tmpdev
= tmpdev
->next
;
214 devices
= malloc(num_devs
* sizeof(*devices
));
216 if (!st
&& ident
->st
) st
= ident
->st
;
219 fprintf(stderr
, Name
": looking for devices for %s\n",
220 mddev
? mddev
: "further assembly");
222 /* first walk the list of devices to find a consistent set
223 * that match the criterea, if that is possible.
224 * We flag the ones we like with 'used'.
226 for (tmpdev
= devlist
;
228 tmpdev
= tmpdev
->next
) {
229 char *devname
= tmpdev
->devname
;
232 struct supertype
*tst
= dup_super(st
);
234 if (tmpdev
->used
> 1) continue;
236 if (ident
->devices
&&
237 !match_oneof(ident
->devices
, devname
)) {
238 if (report_missmatch
)
239 fprintf(stderr
, Name
": %s is not one of %s\n", devname
, ident
->devices
);
243 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
245 if (report_missmatch
)
246 fprintf(stderr
, Name
": cannot open device %s: %s\n",
247 devname
, strerror(errno
));
249 } else if (fstat(dfd
, &stb
)< 0) {
251 fprintf(stderr
, Name
": fstat failed for %s: %s\n",
252 devname
, strerror(errno
));
254 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
255 fprintf(stderr
, Name
": %s is not a block device.\n",
258 } else if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
259 if (report_missmatch
)
260 fprintf(stderr
, Name
": no recogniseable superblock on %s\n",
263 } else if (auto_assem
&& st
== NULL
&&
264 !conf_test_metadata(tst
->ss
->name
)) {
265 if (report_missmatch
)
266 fprintf(stderr
, Name
": %s has metadata type %s for which "
267 "auto-assembly is disabled\n",
268 devname
, tst
->ss
->name
);
270 } else if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
271 if (report_missmatch
)
272 fprintf( stderr
, Name
": no RAID superblock on %s\n",
276 memset(content
, 0, sizeof(*content
));
277 tst
->ss
->getinfo_super(tst
, content
);
279 if (dfd
>= 0) close(dfd
);
281 if (tst
&& tst
->sb
&& tst
->ss
->container_content
282 && tst
->loaded_container
) {
283 /* tmpdev is a container. We need to be either
284 * looking for a member, or auto-assembling
287 /* already found some components, this cannot
290 if (report_missmatch
)
291 fprintf(stderr
, Name
": %s is a container, but we are looking for components\n",
296 if (ident
->container
) {
297 if (ident
->container
[0] == '/' &&
298 !same_dev(ident
->container
, devname
)) {
299 if (report_missmatch
)
300 fprintf(stderr
, Name
": %s is not the container required (%s)\n",
301 devname
, ident
->container
);
304 if (ident
->container
[0] != '/') {
307 if (!parse_uuid(ident
->container
, uuid
) ||
308 !same_uuid(content
->uuid
, uuid
, tst
->ss
->swapuuid
)) {
309 if (report_missmatch
)
310 fprintf(stderr
, Name
": %s has wrong UUID to be required container\n",
316 /* It is worth looking inside this container.
320 content
= tmpdev
->content
;
322 content
= tst
->ss
->container_content(tst
);
324 goto loop
; /* empty container */
326 tmpdev
->content
= content
->next
;
327 if (tmpdev
->content
== NULL
)
330 } else if (ident
->container
|| ident
->member
) {
331 /* No chance of this matching if we don't have
333 if (report_missmatch
)
334 fprintf(stderr
, Name
"%s is not a container, and one is required.\n",
339 if (ident
->uuid_set
&& (!update
|| strcmp(update
, "uuid")!= 0) &&
341 same_uuid(content
->uuid
, ident
->uuid
, tst
->ss
->swapuuid
)==0)) {
342 if (report_missmatch
)
343 fprintf(stderr
, Name
": %s has wrong uuid.\n",
347 if (ident
->name
[0] && (!update
|| strcmp(update
, "name")!= 0) &&
349 name_matches(content
->name
, ident
->name
, homehost
)==0)) {
350 if (report_missmatch
)
351 fprintf(stderr
, Name
": %s has wrong name.\n",
355 if (ident
->super_minor
!= UnSet
&&
357 ident
->super_minor
!= content
->array
.md_minor
)) {
358 if (report_missmatch
)
359 fprintf(stderr
, Name
": %s has wrong super-minor.\n",
363 if (ident
->level
!= UnSet
&&
365 ident
->level
!= content
->array
.level
)) {
366 if (report_missmatch
)
367 fprintf(stderr
, Name
": %s has wrong raid level.\n",
371 if (ident
->raid_disks
!= UnSet
&&
373 ident
->raid_disks
!= content
->array
.raid_disks
)) {
374 if (report_missmatch
)
375 fprintf(stderr
, Name
": %s requires wrong number of drives.\n",
380 if (tst
== NULL
|| tst
->sb
== NULL
)
383 /* If we are this far, then we are nearly commited to this device.
384 * If the super_block doesn't exist, or doesn't match others,
385 * then we probably cannot continue
386 * However if one of the arrays is for the homehost, and
387 * the other isn't that can disambiguate.
390 if (!tst
|| !tst
->sb
) {
391 fprintf(stderr
, Name
": %s has no superblock - assembly aborted\n",
394 st
->ss
->free_super(st
);
398 if (tst
&& tst
->sb
&& tst
->ss
->container_content
399 && tst
->loaded_container
) {
400 /* we have the one container we need, don't keep
401 * looking. If the chosen member is active, skip.
403 if (is_member_busy(content
->text_version
)) {
404 if (report_missmatch
)
405 fprintf(stderr
, Name
": member %s in %s is already assembled\n",
406 content
->text_version
,
408 tst
->ss
->free_super(tst
);
415 st
= tst
; tst
= NULL
;
416 if (!auto_assem
&& tmpdev
->next
!= NULL
) {
417 fprintf(stderr
, Name
": %s is a container, but is not "
418 "only device given: confused and aborting\n",
420 st
->ss
->free_super(st
);
427 if (st
->minor_version
== -1)
428 st
->minor_version
= tst
->minor_version
;
429 if (st
->ss
!= tst
->ss
||
430 st
->minor_version
!= tst
->minor_version
||
431 st
->ss
->compare_super(st
, tst
) != 0) {
432 /* Some mismatch. If exactly one array matches this host,
433 * we can resolve on that one.
434 * Or, if we are auto assembling, we just ignore the second
440 int first
= st
->ss
->match_home(st
, homehost
);
441 int last
= tst
->ss
->match_home(tst
, homehost
);
443 (first
== 1 || last
== 1)) {
444 /* We can do something */
445 if (first
) {/* just ignore this one */
446 if (report_missmatch
)
447 fprintf(stderr
, Name
": %s misses out due to wrong homehost\n",
450 } else { /* reject all those sofar */
452 if (report_missmatch
)
453 fprintf(stderr
, Name
": %s overrides previous devices due to good homehost\n",
455 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
463 fprintf(stderr
, Name
": superblock on %s doesn't match others - assembly aborted\n",
465 tst
->ss
->free_super(tst
);
466 st
->ss
->free_super(st
);
476 tst
->ss
->free_super(tst
);
479 if (!st
|| !st
->sb
|| !content
)
482 /* Now need to open the array device. Use create_mddev */
483 if (content
== &info
)
484 st
->ss
->getinfo_super(st
, content
);
486 trustworthy
= FOREIGN
;
487 name
= content
->name
;
488 switch (st
->ss
->match_home(st
, homehost
)
489 ?: st
->ss
->match_home(st
, "any")) {
492 name
= strchr(content
->name
, ':');
496 name
= content
->name
;
500 /* If the array is listed in mdadm.conf or on
501 * command line, then we trust the name
502 * even if the array doesn't look local
507 content
->array
.level
== LEVEL_CONTAINER
) {
508 name
= content
->text_version
;
509 trustworthy
= METADATA
;
512 if (name
[0] && trustworthy
!= LOCAL
&&
513 ! require_homehost
&&
514 conf_name_is_free(name
))
517 if (trustworthy
== LOCAL
&&
519 /* Ignore 'host:' prefix of name */
520 name
= strchr(name
, ':')+1;
522 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
525 st
->ss
->free_super(st
);
532 vers
= md_get_version(mdfd
);
534 fprintf(stderr
, Name
": Assemble requires driver version 0.90.0 or later.\n"
535 " Upgrade your kernel or try --build\n");
539 if (mddev_busy(fd2devnum(mdfd
))) {
540 fprintf(stderr
, Name
": %s already active, cannot restart it!\n",
542 for (tmpdev
= devlist
;
543 tmpdev
&& tmpdev
->used
!= 1;
544 tmpdev
= tmpdev
->next
)
546 if (tmpdev
&& auto_assem
)
547 fprintf(stderr
, Name
": %s needed for %s...\n",
548 mddev
, tmpdev
->devname
);
551 st
->ss
->free_super(st
);
557 ioctl(mdfd
, STOP_ARRAY
, NULL
); /* just incase it was started but has no content */
560 if (content
!= &info
) {
561 /* This is a member of a container. Try starting the array. */
562 return assemble_container_content(st
, mdfd
, content
, runstop
,
563 chosen_name
, verbose
);
566 /* Ok, no bad inconsistancy, we can try updating etc */
568 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) if (tmpdev
->used
== 1) {
569 char *devname
= tmpdev
->devname
;
571 /* looks like a good enough match to update the super block if needed */
575 /* prepare useful information in info structures */
577 struct supertype
*tst
;
580 if (strcmp(update
, "uuid")==0 &&
583 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
584 read(rfd
, ident
->uuid
, 16) != 16) {
585 *(__u32
*)(ident
->uuid
) = random();
586 *(__u32
*)(ident
->uuid
+1) = random();
587 *(__u32
*)(ident
->uuid
+2) = random();
588 *(__u32
*)(ident
->uuid
+3) = random();
590 if (rfd
>= 0) close(rfd
);
592 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
594 remove_partitions(dfd
);
597 tst
->ss
->load_super(tst
, dfd
, NULL
);
598 tst
->ss
->getinfo_super(tst
, content
);
600 memcpy(content
->uuid
, ident
->uuid
, 16);
601 strcpy(content
->name
, ident
->name
);
602 content
->array
.md_minor
= minor(stb2
.st_rdev
);
604 tst
->ss
->update_super(tst
, content
, update
,
606 ident
->uuid_set
, homehost
);
607 if (strcmp(update
, "uuid")==0 &&
610 memcpy(ident
->uuid
, content
->uuid
, 16);
613 fprintf(stderr
, Name
": Cannot open %s for superblock update\n",
615 else if (tst
->ss
->store_super(tst
, dfd
))
616 fprintf(stderr
, Name
": Could not re-write superblock on %s.\n",
621 if (strcmp(update
, "uuid")==0 &&
622 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
623 if (bitmap_update_uuid(ident
->bitmap_fd
,
625 tst
->ss
->swapuuid
) != 0)
626 fprintf(stderr
, Name
": Could not update uuid on external bitmap.\n");
630 tst
->ss
->free_super(tst
);
634 struct supertype
*tst
= dup_super(st
);
636 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
638 remove_partitions(dfd
);
640 tst
->ss
->load_super(tst
, dfd
, NULL
);
641 tst
->ss
->getinfo_super(tst
, content
);
642 tst
->ss
->free_super(tst
);
649 fprintf(stderr
, Name
": %s is identified as a member of %s, slot %d.\n",
650 devname
, mddev
, content
->disk
.raid_disk
);
651 devices
[devcnt
].devname
= devname
;
652 devices
[devcnt
].uptodate
= 0;
653 devices
[devcnt
].i
= *content
;
654 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
655 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
656 if (most_recent
< devcnt
) {
657 if (devices
[devcnt
].i
.events
658 > devices
[most_recent
].i
.events
)
659 most_recent
= devcnt
;
661 if (content
->array
.level
== -4)
662 /* with multipath, the raid_disk from the superblock is meaningless */
665 i
= devices
[devcnt
].i
.disk
.raid_disk
;
667 if (nextspare
< content
->array
.raid_disks
)
668 nextspare
= content
->array
.raid_disks
;
671 if (i
>= content
->array
.raid_disks
&&
677 unsigned int newbestcnt
= i
+10;
678 int *newbest
= malloc(sizeof(int)*newbestcnt
);
680 for (c
=0; c
< newbestcnt
; c
++)
682 newbest
[c
] = best
[c
];
687 bestcnt
= newbestcnt
;
690 devices
[best
[i
]].i
.events
691 == devices
[devcnt
].i
.events
692 && (devices
[best
[i
]].i
.disk
.minor
693 != devices
[devcnt
].i
.disk
.minor
)
695 && content
->array
.level
!= LEVEL_MULTIPATH
) {
696 /* two different devices with identical superblock.
697 * Could be a mis-detection caused by overlapping
698 * partitions. fail-safe.
700 fprintf(stderr
, Name
": WARNING %s and %s appear"
701 " to have very similar superblocks.\n"
702 " If they are really different, "
703 "please --zero the superblock on one\n"
704 " If they are the same or overlap,"
705 " please remove one from %s.\n",
706 devices
[best
[i
]].devname
, devname
,
707 inargv
? "the list" :
708 "the\n DEVICE list in mdadm.conf"
714 || (devices
[best
[i
]].i
.events
715 < devices
[devcnt
].i
.events
))
722 fprintf(stderr
, Name
": no devices found for %s\n",
725 st
->ss
->free_super(st
);
730 if (update
&& strcmp(update
, "byteorder")==0)
731 st
->minor_version
= 90;
733 st
->ss
->getinfo_super(st
, content
);
734 clean
= content
->array
.state
& 1;
736 /* now we have some devices that might be suitable.
739 avail
= malloc(content
->array
.raid_disks
);
740 memset(avail
, 0, content
->array
.raid_disks
);
743 for (i
=0; i
< bestcnt
;i
++) {
745 int event_margin
= 1; /* always allow a difference of '1'
746 * like the kernel does
749 /* note: we ignore error flags in multipath arrays
750 * as they don't make sense
752 if (content
->array
.level
!= -4)
753 if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_SYNC
))) {
754 if (!(devices
[j
].i
.disk
.state
755 & (1<<MD_DISK_FAULTY
)))
759 if (devices
[j
].i
.events
+event_margin
>=
760 devices
[most_recent
].i
.events
) {
761 devices
[j
].uptodate
= 1;
762 if (i
< content
->array
.raid_disks
) {
769 while (force
&& !enough(content
->array
.level
, content
->array
.raid_disks
,
770 content
->array
.layout
, 1,
772 /* Choose the newest best drive which is
773 * not up-to-date, update the superblock
777 struct supertype
*tst
;
778 long long current_events
;
780 for (i
=0; i
<content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
783 !devices
[j
].uptodate
&&
786 > devices
[chosen_drive
].i
.events
))
789 if (chosen_drive
< 0)
791 current_events
= devices
[chosen_drive
].i
.events
;
794 fprintf(stderr
, Name
": forcing event count in %s(%d) from %d upto %d\n",
795 devices
[chosen_drive
].devname
,
796 devices
[chosen_drive
].i
.disk
.raid_disk
,
797 (int)(devices
[chosen_drive
].i
.events
),
798 (int)(devices
[most_recent
].i
.events
));
799 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
801 fprintf(stderr
, Name
": Couldn't open %s for write - not updating\n",
802 devices
[chosen_drive
].devname
);
803 devices
[chosen_drive
].i
.events
= 0;
807 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
809 fprintf(stderr
, Name
": RAID superblock disappeared from %s - not updating.\n",
810 devices
[chosen_drive
].devname
);
811 devices
[chosen_drive
].i
.events
= 0;
814 content
->events
= devices
[most_recent
].i
.events
;
815 tst
->ss
->update_super(tst
, content
, "force-one",
816 devices
[chosen_drive
].devname
, verbose
,
819 if (tst
->ss
->store_super(tst
, fd
)) {
821 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
822 devices
[chosen_drive
].devname
);
823 devices
[chosen_drive
].i
.events
= 0;
824 tst
->ss
->free_super(tst
);
828 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
829 devices
[chosen_drive
].uptodate
= 1;
830 avail
[chosen_drive
] = 1;
832 tst
->ss
->free_super(tst
);
834 /* If there are any other drives of the same vintage,
835 * add them in as well. We can't lose and we might gain
837 for (i
=0; i
<content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
840 !devices
[j
].uptodate
&&
841 devices
[j
].i
.events
== current_events
) {
848 /* Now we want to look at the superblock which the kernel will base things on
849 * and compare the devices that we think are working with the devices that the
850 * superblock thinks are working.
851 * If there are differences and --force is given, then update this chosen
855 st
->ss
->free_super(st
);
856 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
++) {
862 if (!devices
[j
].uptodate
)
865 if ((fd
=dev_open(devices
[j
].devname
, O_RDONLY
|O_EXCL
))< 0) {
866 fprintf(stderr
, Name
": Cannot open %s: %s\n",
867 devices
[j
].devname
, strerror(errno
));
871 if (st
->ss
->load_super(st
,fd
, NULL
)) {
873 fprintf(stderr
, Name
": RAID superblock has disappeared from %s\n",
880 if (st
->sb
== NULL
) {
881 fprintf(stderr
, Name
": No suitable drives found for %s\n", mddev
);
885 st
->ss
->getinfo_super(st
, content
);
887 sysfs_init(content
, mdfd
, 0);
889 for (i
=0; i
<bestcnt
; i
++) {
891 unsigned int desired_state
;
893 if (i
< content
->array
.raid_disks
)
894 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
900 if (!devices
[j
].uptodate
)
903 devices
[j
].i
.disk
.state
= desired_state
;
904 if (!(devices
[j
].i
.array
.state
& 1))
907 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
911 fprintf(stderr
, Name
": "
912 "clearing FAULTY flag for device %d in %s for %s\n",
913 j
, mddev
, devices
[j
].devname
);
917 fprintf(stderr
, Name
": "
918 "device %d in %s has wrong state in superblock, but %s seems ok\n",
919 i
, mddev
, devices
[j
].devname
);
923 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
924 fprintf(stderr
, Name
": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
929 if (force
&& !clean
&&
930 !enough(content
->array
.level
, content
->array
.raid_disks
,
931 content
->array
.layout
, clean
,
933 change
+= st
->ss
->update_super(st
, content
, "force-array",
934 devices
[chosen_drive
].devname
, verbose
,
941 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
943 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
944 devices
[chosen_drive
].devname
);
948 if (st
->ss
->store_super(st
, fd
)) {
950 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
951 devices
[chosen_drive
].devname
);
958 /* If we are in the middle of a reshape we may need to restore saved data
959 * that was moved aside due to the reshape overwriting live data
960 * The code of doing this lives in Grow.c
963 if (content
->reshape_active
) {
965 int *fdlist
= malloc(sizeof(int)* bestcnt
);
966 for (i
=0; i
<bestcnt
; i
++) {
969 fdlist
[i
] = dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
971 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
980 err
= Grow_restart(st
, content
, fdlist
, bestcnt
, backup_file
);
983 if (fdlist
[i
]>=0) close(fdlist
[i
]);
986 fprintf(stderr
, Name
": Failed to restore critical section for reshape, sorry.\n");
987 if (backup_file
== NULL
)
988 fprintf(stderr
," Possibly you needed to specify the --backup-file\n");
994 /* count number of in-sync devices according to the superblock.
995 * We must have this number to start the array without -s or -R
997 req_cnt
= content
->array
.working_disks
;
999 /* Almost ready to actually *do* something */
1003 /* First, fill in the map, so that udev can find our name
1004 * as soon as we become active.
1006 map_update(NULL
, fd2devnum(mdfd
), content
->text_version
,
1007 content
->uuid
, chosen_name
);
1009 rv
= set_array_info(mdfd
, st
, content
);
1011 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
1012 mddev
, strerror(errno
));
1016 if (ident
->bitmap_fd
>= 0) {
1017 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
1018 fprintf(stderr
, Name
": SET_BITMAP_FILE failed.\n");
1022 } else if (ident
->bitmap_file
) {
1023 /* From config file */
1024 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
1026 fprintf(stderr
, Name
": Could not open bitmap file %s\n",
1027 ident
->bitmap_file
);
1031 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
1032 fprintf(stderr
, Name
": Failed to set bitmapfile for %s\n", mddev
);
1040 /* First, add the raid disks, but add the chosen one last */
1041 for (i
=0; i
<= bestcnt
; i
++) {
1045 if (j
== chosen_drive
)
1050 if (j
>= 0 /* && devices[j].uptodate */) {
1051 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
1054 fprintf(stderr
, Name
": failed to add "
1059 if (i
< content
->array
.raid_disks
1064 } else if (verbose
> 0)
1065 fprintf(stderr
, Name
": added %s "
1067 devices
[j
].devname
, mddev
,
1068 devices
[j
].i
.disk
.raid_disk
);
1069 } else if (verbose
> 0 && i
< content
->array
.raid_disks
)
1070 fprintf(stderr
, Name
": no uptodate device for "
1075 if (content
->array
.level
== LEVEL_CONTAINER
) {
1077 fprintf(stderr
, Name
": Container %s has been "
1078 "assembled with %d drive%s",
1079 mddev
, okcnt
+sparecnt
, okcnt
+sparecnt
==1?"":"s");
1080 if (okcnt
< content
->array
.raid_disks
)
1081 fprintf(stderr
, " (out of %d)",
1082 content
->array
.raid_disks
);
1083 fprintf(stderr
, "\n");
1085 sysfs_uevent(content
, "change");
1086 wait_for(chosen_name
, mdfd
);
1093 ( enough(content
->array
.level
, content
->array
.raid_disks
,
1094 content
->array
.layout
, clean
, avail
, okcnt
) &&
1095 (okcnt
>= req_cnt
|| start_partial_ok
)
1097 /* This array is good-to-go.
1098 * If a reshape is in progress then we might need to
1099 * continue monitoring it. In that case we start
1100 * it read-only and let the grow code make it writable.
1103 if (content
->reshape_active
&&
1104 content
->delta_disks
<= 0)
1105 rv
= Grow_continue(mdfd
, st
, content
, backup_file
);
1107 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1110 fprintf(stderr
, Name
": %s has been started with %d drive%s",
1111 mddev
, okcnt
, okcnt
==1?"":"s");
1112 if (okcnt
< content
->array
.raid_disks
)
1113 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1115 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1116 fprintf(stderr
, ".\n");
1118 if (content
->reshape_active
&&
1119 content
->array
.level
>= 4 &&
1120 content
->array
.level
<= 6) {
1121 /* might need to increase the size
1122 * of the stripe cache - default is 256
1124 if (256 < 4 * (content
->array
.chunk_size
/4096)) {
1125 struct mdinfo
*sra
= sysfs_read(mdfd
, 0, 0);
1127 sysfs_set_num(sra
, NULL
,
1128 "stripe_cache_size",
1129 (4 * content
->array
.chunk_size
/ 4096) + 1);
1132 wait_for(mddev
, mdfd
);
1136 /* There is a nasty race with 'mdadm --monitor'.
1137 * If it opens this device before we close it,
1138 * it gets an incomplete open on which IO
1139 * doesn't work and the capacity is
1141 * If we reopen (to check for layered devices)
1142 * before --monitor closes, we loose.
1144 * So: wait upto 1 second for there to be
1145 * a non-zero capacity.
1147 while (usecs
< 1000) {
1148 mdfd
= open(mddev
, O_RDONLY
);
1150 unsigned long long size
;
1151 if (get_dev_size(mdfd
, NULL
, &size
) &&
1162 fprintf(stderr
, Name
": failed to RUN_ARRAY %s: %s\n",
1163 mddev
, strerror(errno
));
1165 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1166 content
->array
.layout
, 1, avail
, okcnt
))
1167 fprintf(stderr
, Name
": Not enough devices to "
1168 "start the array.\n");
1169 else if (!enough(content
->array
.level
,
1170 content
->array
.raid_disks
,
1171 content
->array
.layout
, clean
,
1173 fprintf(stderr
, Name
": Not enough devices to "
1174 "start the array while not clean "
1175 "- consider --force.\n");
1178 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1182 if (runstop
== -1) {
1183 fprintf(stderr
, Name
": %s assembled from %d drive%s",
1184 mddev
, okcnt
, okcnt
==1?"":"s");
1185 if (okcnt
!= content
->array
.raid_disks
)
1186 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1187 fprintf(stderr
, ", but not started.\n");
1191 if (verbose
>= -1) {
1192 fprintf(stderr
, Name
": %s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
1194 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1195 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1196 content
->array
.layout
, 1, avail
, okcnt
))
1197 fprintf(stderr
, " - not enough to start the array.\n");
1198 else if (!enough(content
->array
.level
,
1199 content
->array
.raid_disks
,
1200 content
->array
.layout
, clean
,
1202 fprintf(stderr
, " - not enough to start the "
1203 "array while not clean - consider "
1206 if (req_cnt
== content
->array
.raid_disks
)
1207 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1209 fprintf(stderr
, " - need %d of %d to start", req_cnt
, content
->array
.raid_disks
);
1210 fprintf(stderr
, " (use --run to insist).\n");
1214 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1218 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1219 * been updated to point to the current locations of devices.
1220 * so we can just start the array
1223 dev
= makedev(devices
[chosen_drive
].i
.disk
.major
,
1224 devices
[chosen_drive
].i
.disk
.minor
);
1225 if (ioctl(mdfd
, START_ARRAY
, dev
)) {
1226 fprintf(stderr
, Name
": Cannot start array: %s\n",
1236 int assemble_container_content(struct supertype
*st
, int mdfd
,
1237 struct mdinfo
*content
, int runstop
,
1238 char *chosen_name
, int verbose
)
1240 struct mdinfo
*dev
, *sra
;
1241 int working
= 0, preexist
= 0;
1242 struct map_ent
*map
= NULL
;
1244 sysfs_init(content
, mdfd
, 0);
1246 sra
= sysfs_read(mdfd
, 0, GET_VERSION
);
1247 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0)
1248 if (sysfs_set_array(content
, md_get_version(mdfd
)) != 0) {
1255 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
1256 if (sysfs_add_disk(content
, dev
, 1) == 0)
1258 else if (errno
== EEXIST
)
1262 return 1;/* Nothing new, don't try to start */
1265 map_update(&map
, fd2devnum(mdfd
),
1266 content
->text_version
,
1267 content
->uuid
, chosen_name
);
1270 (working
+ preexist
) >= content
->array
.working_disks
) {
1273 switch(content
->array
.level
) {
1275 case LEVEL_MULTIPATH
:
1277 err
= sysfs_set_str(content
, NULL
, "array_state",
1281 err
= sysfs_set_str(content
, NULL
, "array_state",
1283 /* start mdmon if needed. */
1285 if (!mdmon_running(st
->container_dev
))
1286 start_mdmon(st
->container_dev
);
1287 ping_monitor(devnum2devname(st
->container_dev
));
1292 sysfs_set_safemode(content
, content
->safe_mode_delay
);
1295 fprintf(stderr
, Name
1296 ": array %s now has %d devices",
1297 chosen_name
, working
+ preexist
);
1299 fprintf(stderr
, Name
1300 ": Started %s with %d devices",
1301 chosen_name
, working
+ preexist
);
1303 fprintf(stderr
, " (%d new)", working
);
1304 fprintf(stderr
, "\n");
1307 wait_for(chosen_name
, mdfd
);
1310 /* FIXME should have an O_EXCL and wait for read-auto */
1313 fprintf(stderr
, Name
1314 ": %s assembled with %d devices but "
1316 chosen_name
, working
);