]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Assemble.c
b09021fca5e881752699e7a089cdfecf6f2630c0
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
33 static int name_matches(char *found
, char *required
, char *homehost
)
35 /* See if the name found matches the required name, possibly
36 * prefixed with 'homehost'
40 strncpy(fnd
, found
, 32);
42 if (strcmp(found
, required
)==0)
45 int l
= strlen(homehost
);
46 if (l
< 32 && fnd
[l
] == ':' &&
47 strcmp(fnd
+l
+1, required
)==0)
53 int Assemble(struct supertype
*st
, char *mddev
,
55 mddev_dev_t devlist
, char *backup_file
,
56 int readonly
, int runstop
,
57 char *update
, char *homehost
,
58 int verbose
, int force
)
61 * The task of Assemble is to find a collection of
62 * devices that should (according to their superblocks)
63 * form an array, and to give this collection to the MD driver.
64 * In Linux-2.4 and later, this involves submitting a
65 * SET_ARRAY_INFO ioctl with no arg - to prepare
66 * the array - and then submit a number of
67 * ADD_NEW_DISK ioctls to add disks into
68 * the array. Finally RUN_ARRAY might
69 * be submitted to start the array.
71 * Much of the work of Assemble is in finding and/or
72 * checking the disks to make sure they look right.
74 * If mddev is not set, then scan must be set and we
75 * read through the config file for dev+uuid mapping
76 * We recurse, setting mddev, for each device that
78 * - has a valid uuid (or any uuid if !uuidset)
80 * If mddev is set, we try to determine state of md.
81 * check version - must be at least 0.90.0
82 * check kernel version. must be at least 2.4.
83 * If not, we can possibly fall back on START_ARRAY
84 * Try to GET_ARRAY_INFO.
85 * If possible, give up
86 * If not, try to STOP_ARRAY just to make sure
88 * If !uuidset and scan, look in conf-file for uuid
89 * If not found, give up
90 * If !devlist and scan and uuidset, get list of devs from conf-file
93 * Check superblock - discard if bad
94 * Check uuid (set if we don't have one) - discard if no match
95 * Check superblock similarity if we have a superblock - discard if different
96 * Record events, devicenum
97 * This should give us a list of devices for the array
98 * We should collect the most recent event number
100 * Count disks with recent enough event count
101 * While force && !enough disks
102 * Choose newest rejected disks, update event count
103 * mark clean and rewrite superblock
106 * foreach device with recent events : ADD_NEW_DISK
107 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
109 * Check the device numbers in superblock are right
110 * update superblock if any changes
116 int auto_assem
= (mddev
== NULL
);
118 int vers
= vers
; /* Keep gcc quite - it really is initialised */
121 int uptodate
; /* set once we decide that this device is as
122 * recent as everything else in the array.
126 int *best
= NULL
; /* indexed by raid_disk */
127 unsigned int bestcnt
= 0;
129 unsigned int okcnt
, sparecnt
;
130 unsigned int req_cnt
;
137 int start_partial_ok
= (runstop
>= 0) &&
138 (force
|| devlist
==NULL
|| auto_assem
);
139 unsigned int num_devs
;
142 struct mdinfo
*content
= NULL
;
143 mdu_array_info_t tmp_inf
;
148 char chosen_name
[1024];
150 if (get_linux_version() < 2004000)
154 * If any subdevs are listed, then any that don't
155 * match ident are discarded. Remainder must all match and
157 * If no subdevs, then we scan all devices in the config file, but
158 * there must be something in the identity
162 ident
->uuid_set
== 0 &&
163 ident
->super_minor
< 0 &&
164 ident
->devices
== NULL
) {
165 fprintf(stderr
, Name
": No identity information available for %s - cannot assemble.\n",
166 mddev
? mddev
: "further assembly");
170 /* if the configuration specifies a container then we use that to
171 * determine the devices and retrieve the array configuration
174 if (ident
->container
&& ident
->member
) {
175 int cfd
= open(ident
->container
, O_RDWR
);
177 struct supertype container
;
180 fprintf(stderr
, Name
": looking to assemble member array %s"
181 " inside container %s\n", ident
->member
, ident
->container
);
184 fprintf(stderr
, Name
": unable to open container %s: %s\n",
185 ident
->container
, strerror(errno
));
189 mdi
= sysfs_read(cfd
, fd2devnum(cfd
), GET_VERSION
);
193 fprintf(stderr
, Name
": unable to read container %s\n",
197 container
.ss
= find_metadata_methods(mdi
->text_version
);
201 fprintf(stderr
, Name
": %s uses unknown metadata: %s\n",
202 ident
->container
, mdi
->text_version
);
205 if (container
.ss
->load_super(&container
, cfd
, ident
->container
)) {
206 fprintf(stderr
, Name
": Cannot load metadata for container %s\n",
211 return Incremental_container(&container
, ident
->container
,
212 verbose
, runstop
, ident
->autof
,
217 devlist
= conf_get_devs();
222 /* We come back here when doing auto-assembly and attempting some
223 * set of devices failed. Those are now marked as ->used==2 and
224 * we ignore them and try again
227 tmpdev
= devlist
; num_devs
= 0;
233 tmpdev
= tmpdev
->next
;
235 devices
= malloc(num_devs
* sizeof(*devices
));
237 if (!st
&& ident
->st
) st
= ident
->st
;
240 fprintf(stderr
, Name
": looking for devices for %s\n",
241 mddev
? mddev
: "further assembly");
243 /* first walk the list of devices to find a consistent set
244 * that match the criterea, if that is possible.
245 * We flag the ones we like with 'used'.
247 for (tmpdev
= devlist
;
249 tmpdev
= tmpdev
->next
) {
250 char *devname
= tmpdev
->devname
;
253 struct supertype
*tst
= dup_super(st
);
255 if (tmpdev
->used
> 1) continue;
257 if (ident
->devices
&&
258 !match_oneof(ident
->devices
, devname
)) {
259 if ((inargv
&& verbose
>=0) || verbose
> 0)
260 fprintf(stderr
, Name
": %s is not one of %s\n", devname
, ident
->devices
);
264 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
266 if ((inargv
&& verbose
>= 0) || verbose
> 0)
267 fprintf(stderr
, Name
": cannot open device %s: %s\n",
268 devname
, strerror(errno
));
270 } else if (fstat(dfd
, &stb
)< 0) {
272 fprintf(stderr
, Name
": fstat failed for %s: %s\n",
273 devname
, strerror(errno
));
275 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
276 fprintf(stderr
, Name
": %s is not a block device.\n",
279 } else if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
280 if ((inargv
&& verbose
>= 0) || verbose
> 0)
281 fprintf(stderr
, Name
": no recogniseable superblock on %s\n",
284 } else if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
285 if ((inargv
&& verbose
>= 0) || verbose
> 0)
286 fprintf( stderr
, Name
": no RAID superblock on %s\n",
290 memset(content
, 0, sizeof(*content
));
291 tst
->ss
->getinfo_super(tst
, content
);
293 if (dfd
>= 0) close(dfd
);
295 if (ident
->uuid_set
&& (!update
|| strcmp(update
, "uuid")!= 0) &&
297 same_uuid(content
->uuid
, ident
->uuid
, tst
->ss
->swapuuid
)==0)) {
298 if ((inargv
&& verbose
>= 0) || verbose
> 0)
299 fprintf(stderr
, Name
": %s has wrong uuid.\n",
303 if (ident
->name
[0] && (!update
|| strcmp(update
, "name")!= 0) &&
305 name_matches(content
->name
, ident
->name
, homehost
)==0)) {
306 if ((inargv
&& verbose
>= 0) || verbose
> 0)
307 fprintf(stderr
, Name
": %s has wrong name.\n",
311 if (ident
->super_minor
!= UnSet
&&
313 ident
->super_minor
!= content
->array
.md_minor
)) {
314 if ((inargv
&& verbose
>= 0) || verbose
> 0)
315 fprintf(stderr
, Name
": %s has wrong super-minor.\n",
319 if (ident
->level
!= UnSet
&&
321 ident
->level
!= content
->array
.level
)) {
322 if ((inargv
&& verbose
>= 0) || verbose
> 0)
323 fprintf(stderr
, Name
": %s has wrong raid level.\n",
327 if (ident
->raid_disks
!= UnSet
&&
329 ident
->raid_disks
!= content
->array
.raid_disks
)) {
330 if ((inargv
&& verbose
>= 0) || verbose
> 0)
331 fprintf(stderr
, Name
": %s requires wrong number of drives.\n",
336 if (tst
== NULL
|| tst
->sb
== NULL
)
339 /* If we are this far, then we are nearly commited to this device.
340 * If the super_block doesn't exist, or doesn't match others,
341 * then we probably cannot continue
342 * However if one of the arrays is for the homehost, and
343 * the other isn't that can disambiguate.
346 if (!tst
|| !tst
->sb
) {
347 fprintf(stderr
, Name
": %s has no superblock - assembly aborted\n",
350 st
->ss
->free_super(st
);
356 if (st
->minor_version
== -1)
357 st
->minor_version
= tst
->minor_version
;
358 if (st
->ss
!= tst
->ss
||
359 st
->minor_version
!= tst
->minor_version
||
360 st
->ss
->compare_super(st
, tst
) != 0) {
361 /* Some mismatch. If exactly one array matches this host,
362 * we can resolve on that one.
363 * Or, if we are auto assembling, we just ignore the second
369 int first
= st
->ss
->match_home(st
, homehost
);
370 int last
= tst
->ss
->match_home(tst
, homehost
);
372 (first
== 1 || last
== 1)) {
373 /* We can do something */
374 if (first
) {/* just ignore this one */
375 if ((inargv
&& verbose
>= 0) || verbose
> 0)
376 fprintf(stderr
, Name
": %s misses out due to wrong homehost\n",
379 } else { /* reject all those sofar */
381 if ((inargv
&& verbose
>= 0) || verbose
> 0)
382 fprintf(stderr
, Name
": %s overrides previous devices due to good homehost\n",
384 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
392 fprintf(stderr
, Name
": superblock on %s doesn't match others - assembly aborted\n",
394 tst
->ss
->free_super(tst
);
395 st
->ss
->free_super(st
);
403 tst
->ss
->free_super(tst
);
406 if (!st
|| !st
->sb
|| !content
)
409 /* Now need to open array the device. Use create_mddev */
410 if (content
== &info
)
411 st
->ss
->getinfo_super(st
, content
);
413 trustworthy
= FOREIGN
;
414 switch (st
->ss
->match_home(st
, homehost
)) {
416 trustworthy
= FOREIGN
;
417 name
= content
->name
;
421 name
= strchr(content
->name
, ':');
425 name
= content
->name
;
428 trustworthy
= FOREIGN
;
431 if (!auto_assem
&& trustworthy
== FOREIGN
)
432 /* If the array is listed in mdadm or on
433 * command line, then we trust the name
434 * even if the array doesn't look local
438 if (content
->name
[0] == 0 &&
439 content
->array
.level
== LEVEL_CONTAINER
) {
440 name
= content
->text_version
;
441 trustworthy
= METADATA
;
443 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
446 st
->ss
->free_super(st
);
453 vers
= md_get_version(mdfd
);
455 fprintf(stderr
, Name
": Assemble requires driver version 0.90.0 or later.\n"
456 " Upgrade your kernel or try --build\n");
460 if (ioctl(mdfd
, GET_ARRAY_INFO
, &tmp_inf
)==0) {
461 fprintf(stderr
, Name
": %s already active, cannot restart it!\n",
463 for (tmpdev
= devlist
;
464 tmpdev
&& tmpdev
->used
!= 1;
465 tmpdev
= tmpdev
->next
)
467 if (tmpdev
&& auto_assem
)
468 fprintf(stderr
, Name
": %s needed for %s...\n",
469 mddev
, tmpdev
->devname
);
472 st
->ss
->free_super(st
);
478 ioctl(mdfd
, STOP_ARRAY
, NULL
); /* just incase it was started but has no content */
480 /* Ok, no bad inconsistancy, we can try updating etc */
482 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) if (tmpdev
->used
== 1) {
483 char *devname
= tmpdev
->devname
;
485 /* looks like a good enough match to update the super block if needed */
489 /* prepare useful information in info structures */
491 struct supertype
*tst
;
494 if (strcmp(update
, "uuid")==0 &&
497 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
498 read(rfd
, ident
->uuid
, 16) != 16) {
499 *(__u32
*)(ident
->uuid
) = random();
500 *(__u32
*)(ident
->uuid
+1) = random();
501 *(__u32
*)(ident
->uuid
+2) = random();
502 *(__u32
*)(ident
->uuid
+3) = random();
504 if (rfd
>= 0) close(rfd
);
506 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
508 remove_partitions(dfd
);
511 tst
->ss
->load_super(tst
, dfd
, NULL
);
512 tst
->ss
->getinfo_super(tst
, content
);
514 memcpy(content
->uuid
, ident
->uuid
, 16);
515 strcpy(content
->name
, ident
->name
);
516 content
->array
.md_minor
= minor(stb2
.st_rdev
);
518 tst
->ss
->update_super(tst
, content
, update
,
520 ident
->uuid_set
, homehost
);
521 if (strcmp(update
, "uuid")==0 &&
524 memcpy(ident
->uuid
, content
->uuid
, 16);
527 fprintf(stderr
, Name
": Cannot open %s for superblock update\n",
529 else if (tst
->ss
->store_super(tst
, dfd
))
530 fprintf(stderr
, Name
": Could not re-write superblock on %s.\n",
535 if (strcmp(update
, "uuid")==0 &&
536 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
537 if (bitmap_update_uuid(ident
->bitmap_fd
,
539 tst
->ss
->swapuuid
) != 0)
540 fprintf(stderr
, Name
": Could not update uuid on external bitmap.\n");
544 tst
->ss
->free_super(tst
);
548 struct supertype
*tst
= dup_super(st
);
550 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
552 remove_partitions(dfd
);
554 tst
->ss
->load_super(tst
, dfd
, NULL
);
555 tst
->ss
->getinfo_super(tst
, content
);
556 tst
->ss
->free_super(tst
);
563 fprintf(stderr
, Name
": %s is identified as a member of %s, slot %d.\n",
564 devname
, mddev
, content
->disk
.raid_disk
);
565 devices
[devcnt
].devname
= devname
;
566 devices
[devcnt
].uptodate
= 0;
567 devices
[devcnt
].i
= *content
;
568 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
569 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
570 if (most_recent
< devcnt
) {
571 if (devices
[devcnt
].i
.events
572 > devices
[most_recent
].i
.events
)
573 most_recent
= devcnt
;
575 if (content
->array
.level
== -4)
576 /* with multipath, the raid_disk from the superblock is meaningless */
579 i
= devices
[devcnt
].i
.disk
.raid_disk
;
581 if (nextspare
< content
->array
.raid_disks
)
582 nextspare
= content
->array
.raid_disks
;
585 if (i
>= content
->array
.raid_disks
&&
591 unsigned int newbestcnt
= i
+10;
592 int *newbest
= malloc(sizeof(int)*newbestcnt
);
594 for (c
=0; c
< newbestcnt
; c
++)
596 newbest
[c
] = best
[c
];
601 bestcnt
= newbestcnt
;
604 devices
[best
[i
]].i
.events
605 == devices
[devcnt
].i
.events
606 && (devices
[best
[i
]].i
.disk
.minor
607 != devices
[devcnt
].i
.disk
.minor
)
609 && content
->array
.level
!= LEVEL_MULTIPATH
) {
610 /* two different devices with identical superblock.
611 * Could be a mis-detection caused by overlapping
612 * partitions. fail-safe.
614 fprintf(stderr
, Name
": WARNING %s and %s appear"
615 " to have very similar superblocks.\n"
616 " If they are really different, "
617 "please --zero the superblock on one\n"
618 " If they are the same or overlap,"
619 " please remove one from %s.\n",
620 devices
[best
[i
]].devname
, devname
,
621 inargv
? "the list" :
622 "the\n DEVICE list in mdadm.conf"
628 || (devices
[best
[i
]].i
.events
629 < devices
[devcnt
].i
.events
))
636 fprintf(stderr
, Name
": no devices found for %s\n",
639 st
->ss
->free_super(st
);
644 if (update
&& strcmp(update
, "byteorder")==0)
645 st
->minor_version
= 90;
647 st
->ss
->getinfo_super(st
, content
);
648 clean
= content
->array
.state
& 1;
650 /* now we have some devices that might be suitable.
653 avail
= malloc(content
->array
.raid_disks
);
654 memset(avail
, 0, content
->array
.raid_disks
);
657 for (i
=0; i
< bestcnt
;i
++) {
659 int event_margin
= 1; /* always allow a difference of '1'
660 * like the kernel does
663 /* note: we ignore error flags in multipath arrays
664 * as they don't make sense
666 if (content
->array
.level
!= -4)
667 if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_SYNC
))) {
668 if (!(devices
[j
].i
.disk
.state
669 & (1<<MD_DISK_FAULTY
)))
673 if (devices
[j
].i
.events
+event_margin
>=
674 devices
[most_recent
].i
.events
) {
675 devices
[j
].uptodate
= 1;
676 if (i
< content
->array
.raid_disks
) {
683 while (force
&& !enough(content
->array
.level
, content
->array
.raid_disks
,
684 content
->array
.layout
, 1,
686 /* Choose the newest best drive which is
687 * not up-to-date, update the superblock
691 struct supertype
*tst
;
692 long long current_events
;
694 for (i
=0; i
<content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
697 !devices
[j
].uptodate
&&
698 devices
[j
].i
.events
> 0 &&
701 > devices
[chosen_drive
].i
.events
))
704 if (chosen_drive
< 0)
706 current_events
= devices
[chosen_drive
].i
.events
;
709 fprintf(stderr
, Name
": forcing event count in %s(%d) from %d upto %d\n",
710 devices
[chosen_drive
].devname
,
711 devices
[chosen_drive
].i
.disk
.raid_disk
,
712 (int)(devices
[chosen_drive
].i
.events
),
713 (int)(devices
[most_recent
].i
.events
));
714 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
716 fprintf(stderr
, Name
": Couldn't open %s for write - not updating\n",
717 devices
[chosen_drive
].devname
);
718 devices
[chosen_drive
].i
.events
= 0;
722 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
724 fprintf(stderr
, Name
": RAID superblock disappeared from %s - not updating.\n",
725 devices
[chosen_drive
].devname
);
726 devices
[chosen_drive
].i
.events
= 0;
729 content
->events
= devices
[most_recent
].i
.events
;
730 tst
->ss
->update_super(tst
, content
, "force-one",
731 devices
[chosen_drive
].devname
, verbose
,
734 if (tst
->ss
->store_super(tst
, fd
)) {
736 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
737 devices
[chosen_drive
].devname
);
738 devices
[chosen_drive
].i
.events
= 0;
739 tst
->ss
->free_super(tst
);
743 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
744 devices
[chosen_drive
].uptodate
= 1;
745 avail
[chosen_drive
] = 1;
747 tst
->ss
->free_super(tst
);
749 /* If there are any other drives of the same vintage,
750 * add them in as well. We can't lose and we might gain
752 for (i
=0; i
<content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
755 !devices
[j
].uptodate
&&
756 devices
[j
].i
.events
> 0 &&
757 devices
[j
].i
.events
== current_events
) {
764 /* Now we want to look at the superblock which the kernel will base things on
765 * and compare the devices that we think are working with the devices that the
766 * superblock thinks are working.
767 * If there are differences and --force is given, then update this chosen
771 st
->ss
->free_super(st
);
772 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
++) {
778 if (!devices
[j
].uptodate
)
781 if ((fd
=dev_open(devices
[j
].devname
, O_RDONLY
|O_EXCL
))< 0) {
782 fprintf(stderr
, Name
": Cannot open %s: %s\n",
783 devices
[j
].devname
, strerror(errno
));
787 if (st
->ss
->load_super(st
,fd
, NULL
)) {
789 fprintf(stderr
, Name
": RAID superblock has disappeared from %s\n",
796 if (st
->sb
== NULL
) {
797 fprintf(stderr
, Name
": No suitable drives found for %s\n", mddev
);
801 st
->ss
->getinfo_super(st
, content
);
803 sysfs_init(content
, mdfd
, 0);
805 for (i
=0; i
<bestcnt
; i
++) {
807 unsigned int desired_state
;
809 if (i
< content
->array
.raid_disks
)
810 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
816 if (!devices
[j
].uptodate
)
819 devices
[j
].i
.disk
.state
= desired_state
;
821 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
825 fprintf(stderr
, Name
": "
826 "clearing FAULTY flag for device %d in %s for %s\n",
827 j
, mddev
, devices
[j
].devname
);
831 fprintf(stderr
, Name
": "
832 "device %d in %s has wrong state in superblock, but %s seems ok\n",
833 i
, mddev
, devices
[j
].devname
);
837 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
838 fprintf(stderr
, Name
": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
843 if (force
&& !clean
&&
844 !enough(content
->array
.level
, content
->array
.raid_disks
,
845 content
->array
.layout
, clean
,
847 change
+= st
->ss
->update_super(st
, content
, "force-array",
848 devices
[chosen_drive
].devname
, verbose
,
855 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
857 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
858 devices
[chosen_drive
].devname
);
862 if (st
->ss
->store_super(st
, fd
)) {
864 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
865 devices
[chosen_drive
].devname
);
872 /* If we are in the middle of a reshape we may need to restore saved data
873 * that was moved aside due to the reshape overwriting live data
874 * The code of doing this lives in Grow.c
877 if (content
->reshape_active
) {
879 int *fdlist
= malloc(sizeof(int)* bestcnt
);
880 for (i
=0; i
<bestcnt
; i
++) {
883 fdlist
[i
] = dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
885 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
894 err
= Grow_restart(st
, content
, fdlist
, bestcnt
, backup_file
);
897 if (fdlist
[i
]>=0) close(fdlist
[i
]);
900 fprintf(stderr
, Name
": Failed to restore critical section for reshape, sorry.\n");
906 /* count number of in-sync devices according to the superblock.
907 * We must have this number to start the array without -s or -R
909 req_cnt
= content
->array
.working_disks
;
911 /* Almost ready to actually *do* something */
915 /* First, fill in the map, so that udev can find our name
916 * as soon as we become active.
918 map_update(NULL
, fd2devnum(mdfd
), content
->text_version
,
919 content
->uuid
, chosen_name
);
921 rv
= set_array_info(mdfd
, st
, content
);
923 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
924 mddev
, strerror(errno
));
928 if (ident
->bitmap_fd
>= 0) {
929 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
930 fprintf(stderr
, Name
": SET_BITMAP_FILE failed.\n");
934 } else if (ident
->bitmap_file
) {
935 /* From config file */
936 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
938 fprintf(stderr
, Name
": Could not open bitmap file %s\n",
943 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
944 fprintf(stderr
, Name
": Failed to set bitmapfile for %s\n", mddev
);
952 /* First, add the raid disks, but add the chosen one last */
953 for (i
=0; i
<= bestcnt
; i
++) {
957 if (j
== chosen_drive
)
962 if (j
>= 0 /* && devices[j].uptodate */) {
963 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
966 fprintf(stderr
, Name
": failed to add "
971 if (i
< content
->array
.raid_disks
976 } else if (verbose
> 0)
977 fprintf(stderr
, Name
": added %s "
979 devices
[j
].devname
, mddev
,
980 devices
[j
].i
.disk
.raid_disk
);
981 } else if (verbose
> 0 && i
< content
->array
.raid_disks
)
982 fprintf(stderr
, Name
": no uptodate device for "
987 if (content
->array
.level
== LEVEL_CONTAINER
) {
989 fprintf(stderr
, Name
": Container %s has been "
990 "assembled with %d drive%s",
991 mddev
, okcnt
+sparecnt
, okcnt
+sparecnt
==1?"":"s");
992 if (okcnt
< content
->array
.raid_disks
)
993 fprintf(stderr
, " (out of %d)",
994 content
->array
.raid_disks
);
995 fprintf(stderr
, "\n");
997 sysfs_uevent(content
, "change");
1004 ( enough(content
->array
.level
, content
->array
.raid_disks
,
1005 content
->array
.layout
, clean
, avail
, okcnt
) &&
1006 (okcnt
>= req_cnt
|| start_partial_ok
)
1008 if (ioctl(mdfd
, RUN_ARRAY
, NULL
)==0) {
1010 fprintf(stderr
, Name
": %s has been started with %d drive%s",
1011 mddev
, okcnt
, okcnt
==1?"":"s");
1012 if (okcnt
< content
->array
.raid_disks
)
1013 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1015 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1016 fprintf(stderr
, ".\n");
1021 /* There is a nasty race with 'mdadm --monitor'.
1022 * If it opens this device before we close it,
1023 * it gets an incomplete open on which IO
1024 * doesn't work and the capacity is
1026 * If we reopen (to check for layered devices)
1027 * before --monitor closes, we loose.
1029 * So: wait upto 1 second for there to be
1030 * a non-zero capacity.
1032 while (usecs
< 1000) {
1033 mdfd
= open(mddev
, O_RDONLY
);
1035 unsigned long long size
;
1036 if (get_dev_size(mdfd
, NULL
, &size
) &&
1047 fprintf(stderr
, Name
": failed to RUN_ARRAY %s: %s\n",
1048 mddev
, strerror(errno
));
1050 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1051 content
->array
.layout
, 1, avail
, okcnt
))
1052 fprintf(stderr
, Name
": Not enough devices to "
1053 "start the array.\n");
1054 else if (!enough(content
->array
.level
,
1055 content
->array
.raid_disks
,
1056 content
->array
.layout
, clean
,
1058 fprintf(stderr
, Name
": Not enough devices to "
1059 "start the array while not clean "
1060 "- consider --force.\n");
1063 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1067 if (runstop
== -1) {
1068 fprintf(stderr
, Name
": %s assembled from %d drive%s",
1069 mddev
, okcnt
, okcnt
==1?"":"s");
1070 if (okcnt
!= content
->array
.raid_disks
)
1071 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1072 fprintf(stderr
, ", but not started.\n");
1076 if (verbose
>= -1) {
1077 fprintf(stderr
, Name
": %s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
1079 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1080 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1081 content
->array
.layout
, 1, avail
, okcnt
))
1082 fprintf(stderr
, " - not enough to start the array.\n");
1083 else if (!enough(content
->array
.level
,
1084 content
->array
.raid_disks
,
1085 content
->array
.layout
, clean
,
1087 fprintf(stderr
, " - not enough to start the "
1088 "array while not clean - consider "
1091 if (req_cnt
== content
->array
.raid_disks
)
1092 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1094 fprintf(stderr
, " - need %d of %d to start", req_cnt
, content
->array
.raid_disks
);
1095 fprintf(stderr
, " (use --run to insist).\n");
1099 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1102 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1103 * been updated to point to the current locations of devices.
1104 * so we can just start the array
1107 dev
= makedev(devices
[chosen_drive
].i
.disk
.major
,
1108 devices
[chosen_drive
].i
.disk
.minor
);
1109 if (ioctl(mdfd
, START_ARRAY
, dev
)) {
1110 fprintf(stderr
, Name
": Cannot start array: %s\n",