]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Assemble.c
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
, int mdfd
,
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
117 int vers
= 0; /* Keep gcc quite - it really is initialised */
120 unsigned int major
, minor
;
121 unsigned int oldmajor
, oldminor
;
128 int *best
= NULL
; /* indexed by raid_disk */
129 unsigned int bestcnt
= 0;
131 unsigned int okcnt
, sparecnt
;
132 unsigned int req_cnt
;
139 int start_partial_ok
= (runstop
>= 0) && (force
|| devlist
==NULL
|| mdfd
< 0);
140 unsigned int num_devs
;
146 if (get_linux_version() < 2004000)
150 vers
= md_get_version(mdfd
);
152 fprintf(stderr
, Name
": %s appears not to be an md device.\n", mddev
);
156 fprintf(stderr
, Name
": Assemble requires driver version 0.90.0 or later.\n"
157 " Upgrade your kernel or try --build\n");
161 if (ioctl(mdfd
, GET_ARRAY_INFO
, &info
.array
)>=0) {
162 fprintf(stderr
, Name
": device %s already active - cannot assemble it\n",
166 ioctl(mdfd
, STOP_ARRAY
, NULL
); /* just incase it was started but has no content */
169 * If any subdevs are listed, then any that don't
170 * match ident are discarded. Remainder must all match and
172 * If no subdevs, then we scan all devices in the config file, but
173 * there must be something in the identity
177 ident
->uuid_set
== 0 &&
178 ident
->super_minor
< 0 &&
179 ident
->devices
== NULL
) {
180 fprintf(stderr
, Name
": No identity information available for %s - cannot assemble.\n",
181 mddev
? mddev
: "further assembly");
185 devlist
= conf_get_devs();
191 tmpdev
= devlist
; num_devs
= 0;
197 tmpdev
= tmpdev
->next
;
199 devices
= malloc(num_devs
* sizeof(*devices
));
201 if (!st
&& ident
->st
) st
= ident
->st
;
204 fprintf(stderr
, Name
": looking for devices for %s\n",
205 mddev
? mddev
: "further assembly");
207 /* first walk the list of devices to find a consistent set
208 * that match the criterea, if that is possible.
209 * We flag the one we like with 'used'.
211 for (tmpdev
= devlist
;
213 tmpdev
= tmpdev
->next
) {
214 char *devname
= tmpdev
->devname
;
217 struct supertype
*tst
= dup_super(st
);
219 if (tmpdev
->used
> 1) continue;
221 if (ident
->devices
&&
222 !match_oneof(ident
->devices
, devname
)) {
223 if ((inargv
&& verbose
>=0) || verbose
> 0)
224 fprintf(stderr
, Name
": %s is not one of %s\n", devname
, ident
->devices
);
228 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
230 if ((inargv
&& verbose
>= 0) || verbose
> 0)
231 fprintf(stderr
, Name
": cannot open device %s: %s\n",
232 devname
, strerror(errno
));
234 } else if (fstat(dfd
, &stb
)< 0) {
236 fprintf(stderr
, Name
": fstat failed for %s: %s\n",
237 devname
, strerror(errno
));
239 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
240 fprintf(stderr
, Name
": %s is not a block device.\n",
243 } else if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
244 if ((inargv
&& verbose
>= 0) || verbose
> 0)
245 fprintf(stderr
, Name
": no recogniseable superblock on %s\n",
248 } else if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
249 if ((inargv
&& verbose
>= 0) || verbose
> 0)
250 fprintf( stderr
, Name
": no RAID superblock on %s\n",
253 tst
->ss
->getinfo_super(tst
, &info
);
255 if (dfd
>= 0) close(dfd
);
257 if (ident
->uuid_set
&& (!update
|| strcmp(update
, "uuid")!= 0) &&
259 same_uuid(info
.uuid
, ident
->uuid
, tst
->ss
->swapuuid
)==0)) {
260 if ((inargv
&& verbose
>= 0) || verbose
> 0)
261 fprintf(stderr
, Name
": %s has wrong uuid.\n",
265 if (ident
->name
[0] && (!update
|| strcmp(update
, "name")!= 0) &&
267 name_matches(info
.name
, ident
->name
, homehost
)==0)) {
268 if ((inargv
&& verbose
>= 0) || verbose
> 0)
269 fprintf(stderr
, Name
": %s has wrong name.\n",
273 if (ident
->super_minor
!= UnSet
&&
275 ident
->super_minor
!= info
.array
.md_minor
)) {
276 if ((inargv
&& verbose
>= 0) || verbose
> 0)
277 fprintf(stderr
, Name
": %s has wrong super-minor.\n",
281 if (ident
->level
!= UnSet
&&
283 ident
->level
!= info
.array
.level
)) {
284 if ((inargv
&& verbose
>= 0) || verbose
> 0)
285 fprintf(stderr
, Name
": %s has wrong raid level.\n",
289 if (ident
->raid_disks
!= UnSet
&&
291 ident
->raid_disks
!= info
.array
.raid_disks
)) {
292 if ((inargv
&& verbose
>= 0) || verbose
> 0)
293 fprintf(stderr
, Name
": %s requires wrong number of drives.\n",
298 if (tst
== NULL
|| tst
->sb
== NULL
)
300 if (update
== NULL
&&
301 tst
->ss
->match_home(tst
, homehost
)==0) {
302 if ((inargv
&& verbose
>= 0) || verbose
> 0)
303 fprintf(stderr
, Name
": %s is not built for host %s.\n",
305 /* Auto-assemble, and this is not a usable host */
306 /* if update != NULL, we are updating the host
311 /* If we are this far, then we are nearly commited to this device.
312 * If the super_block doesn't exist, or doesn't match others,
313 * then we probably cannot continue
314 * However if one of the arrays is for the homehost, and
315 * the other isn't that can disambiguate.
318 if (!tst
|| !tst
->sb
) {
319 fprintf(stderr
, Name
": %s has no superblock - assembly aborted\n",
321 st
->ss
->free_super(st
);
327 if (st
->minor_version
== -1)
328 st
->minor_version
= tst
->minor_version
;
329 if (st
->ss
!= tst
->ss
||
330 st
->minor_version
!= tst
->minor_version
||
331 st
->ss
->compare_super(st
, tst
) != 0) {
332 /* Some mismatch. If exactly one array matches this host,
333 * we can resolve on that one.
334 * Or, if we are auto assembling, we just ignore the second
340 int first
= st
->ss
->match_home(st
, homehost
);
341 int last
= tst
->ss
->match_home(tst
, homehost
);
342 if (first
+last
== 1) {
343 /* We can do something */
344 if (first
) {/* just ignore this one */
345 if ((inargv
&& verbose
>= 0) || verbose
> 0)
346 fprintf(stderr
, Name
": %s misses out due to wrong homehost\n",
349 } else { /* reject all those sofar */
351 if ((inargv
&& verbose
>= 0) || verbose
> 0)
352 fprintf(stderr
, Name
": %s overrides previous devices due to good homehost\n",
354 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
362 fprintf(stderr
, Name
": superblock on %s doesn't match others - assembly aborted\n",
364 tst
->ss
->free_super(tst
);
365 st
->ss
->free_super(st
);
372 tst
->ss
->free_super(tst
);
376 /* So... it is up to me to open the device.
377 * We create a name '/dev/md/XXX' based on the info in the
378 * superblock, and call open_mddev on that
380 mdu_array_info_t inf
;
385 st
->ss
->getinfo_super(st
, &info
);
386 c
= strchr(info
.name
, ':');
387 if (c
) c
++; else c
= info
.name
;
388 if (isdigit(*c
) && ((ident
->autof
& 7)==4 || (ident
->autof
&7)==6))
389 /* /dev/md/d0 style for partitionable */
390 asprintf(&mddev
, "/dev/md/d%s", c
);
392 asprintf(&mddev
, "/dev/md/%s", c
);
393 mdfd
= open_mddev(mddev
, ident
->autof
);
395 st
->ss
->free_super(st
);
399 vers
= md_get_version(mdfd
);
400 if (ioctl(mdfd
, GET_ARRAY_INFO
, &inf
)==0) {
401 for (tmpdev
= devlist
;
402 tmpdev
&& tmpdev
->used
!= 1;
403 tmpdev
= tmpdev
->next
)
405 fprintf(stderr
, Name
": %s already active, cannot restart it!\n", mddev
);
407 fprintf(stderr
, Name
": %s needed for %s...\n",
408 mddev
, tmpdev
->devname
);
411 st
->ss
->free_super(st
);
418 /* Ok, no bad inconsistancy, we can try updating etc */
420 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) if (tmpdev
->used
== 1) {
421 char *devname
= tmpdev
->devname
;
423 /* looks like a good enough match to update the super block if needed */
427 /* prepare useful information in info structures */
429 struct supertype
*tst
;
432 if (strcmp(update
, "uuid")==0 &&
435 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
436 read(rfd
, ident
->uuid
, 16) != 16) {
437 *(__u32
*)(ident
->uuid
) = random();
438 *(__u32
*)(ident
->uuid
+1) = random();
439 *(__u32
*)(ident
->uuid
+2) = random();
440 *(__u32
*)(ident
->uuid
+3) = random();
442 if (rfd
>= 0) close(rfd
);
444 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
446 remove_partitions(dfd
);
449 tst
->ss
->load_super(tst
, dfd
, NULL
);
450 tst
->ss
->getinfo_super(tst
, &info
);
452 memcpy(info
.uuid
, ident
->uuid
, 16);
453 strcpy(info
.name
, ident
->name
);
454 info
.array
.md_minor
= minor(stb2
.st_rdev
);
456 tst
->ss
->update_super(tst
, &info
, update
,
458 ident
->uuid_set
, homehost
);
459 if (strcmp(update
, "uuid")==0 &&
462 memcpy(ident
->uuid
, info
.uuid
, 16);
465 fprintf(stderr
, Name
": Cannot open %s for superblock update\n",
467 else if (tst
->ss
->store_super(tst
, dfd
))
468 fprintf(stderr
, Name
": Could not re-write superblock on %s.\n",
473 if (strcmp(update
, "uuid")==0 &&
474 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
475 if (bitmap_update_uuid(ident
->bitmap_fd
,
477 tst
->ss
->swapuuid
) != 0)
478 fprintf(stderr
, Name
": Could not update uuid on external bitmap.\n");
482 tst
->ss
->free_super(tst
);
486 struct supertype
*tst
= dup_super(st
);;
488 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
490 remove_partitions(dfd
);
492 tst
->ss
->load_super(tst
, dfd
, NULL
);
493 tst
->ss
->getinfo_super(tst
, &info
);
494 tst
->ss
->free_super(tst
);
501 fprintf(stderr
, Name
": %s is identified as a member of %s, slot %d.\n",
502 devname
, mddev
, info
.disk
.raid_disk
);
503 devices
[devcnt
].devname
= devname
;
504 devices
[devcnt
].major
= major(stb
.st_rdev
);
505 devices
[devcnt
].minor
= minor(stb
.st_rdev
);
506 devices
[devcnt
].oldmajor
= info
.disk
.major
;
507 devices
[devcnt
].oldminor
= info
.disk
.minor
;
508 devices
[devcnt
].events
= info
.events
;
509 devices
[devcnt
].raid_disk
= info
.disk
.raid_disk
;
510 devices
[devcnt
].disk_nr
= info
.disk
.number
;
511 devices
[devcnt
].uptodate
= 0;
512 devices
[devcnt
].state
= info
.disk
.state
;
513 if (most_recent
< devcnt
) {
514 if (devices
[devcnt
].events
515 > devices
[most_recent
].events
)
516 most_recent
= devcnt
;
518 if (info
.array
.level
== -4)
519 /* with multipath, the raid_disk from the superblock is meaningless */
522 i
= devices
[devcnt
].raid_disk
;
524 if (nextspare
< info
.array
.raid_disks
)
525 nextspare
= info
.array
.raid_disks
;
528 if (i
>= info
.array
.raid_disks
&&
534 unsigned int newbestcnt
= i
+10;
535 int *newbest
= malloc(sizeof(int)*newbestcnt
);
537 for (c
=0; c
< newbestcnt
; c
++)
539 newbest
[c
] = best
[c
];
544 bestcnt
= newbestcnt
;
547 devices
[best
[i
]].events
== devices
[devcnt
].events
&&
548 devices
[best
[i
]].minor
!= devices
[devcnt
].minor
&&
549 st
->ss
->major
== 0 &&
550 info
.array
.level
!= -4) {
551 /* two different devices with identical superblock.
552 * Could be a mis-detection caused by overlapping
553 * partitions. fail-safe.
555 fprintf(stderr
, Name
": WARNING %s and %s appear"
556 " to have very similar superblocks.\n"
557 " If they are really different, "
558 "please --zero the superblock on one\n"
559 " If they are the same or overlap,"
560 " please remove one from %s.\n",
561 devices
[best
[i
]].devname
, devname
,
562 inargv
? "the list" :
563 "the\n DEVICE list in mdadm.conf"
565 if (must_close
) close(mdfd
);
569 || devices
[best
[i
]].events
< devices
[devcnt
].events
)
575 if (update
&& strcmp(update
, "byteorder")==0)
576 st
->minor_version
= 90;
579 fprintf(stderr
, Name
": no devices found for %s\n",
581 st
->ss
->free_super(st
);
582 if (must_close
) close(mdfd
);
586 st
->ss
->getinfo_super(st
, &info
);
587 clean
= info
.array
.state
& 1;
589 /* now we have some devices that might be suitable.
592 avail
= malloc(info
.array
.raid_disks
);
593 memset(avail
, 0, info
.array
.raid_disks
);
596 for (i
=0; i
< bestcnt
;i
++) {
598 int event_margin
= 1; /* always allow a difference of '1'
599 * like the kernel does
602 /* note: we ignore error flags in multipath arrays
603 * as they don't make sense
605 if (info
.array
.level
!= -4)
606 if (!(devices
[j
].state
& (1<<MD_DISK_SYNC
))) {
607 if (!(devices
[j
].state
& (1<<MD_DISK_FAULTY
)))
611 if (devices
[j
].events
+event_margin
>=
612 devices
[most_recent
].events
) {
613 devices
[j
].uptodate
= 1;
614 if (i
< info
.array
.raid_disks
) {
621 while (force
&& !enough(info
.array
.level
, info
.array
.raid_disks
,
622 info
.array
.layout
, 1,
624 /* Choose the newest best drive which is
625 * not up-to-date, update the superblock
629 struct supertype
*tst
;
630 long long current_events
;
632 for (i
=0; i
<info
.array
.raid_disks
&& i
< bestcnt
; i
++) {
635 !devices
[j
].uptodate
&&
636 devices
[j
].events
> 0 &&
638 devices
[j
].events
> devices
[chosen_drive
].events
))
641 if (chosen_drive
< 0)
643 current_events
= devices
[chosen_drive
].events
;
646 fprintf(stderr
, Name
": forcing event count in %s(%d) from %d upto %d\n",
647 devices
[chosen_drive
].devname
, devices
[chosen_drive
].raid_disk
,
648 (int)(devices
[chosen_drive
].events
),
649 (int)(devices
[most_recent
].events
));
650 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
652 fprintf(stderr
, Name
": Couldn't open %s for write - not updating\n",
653 devices
[chosen_drive
].devname
);
654 devices
[chosen_drive
].events
= 0;
658 if (tst
->ss
->load_super(st
,fd
, NULL
)) {
660 fprintf(stderr
, Name
": RAID superblock disappeared from %s - not updating.\n",
661 devices
[chosen_drive
].devname
);
662 devices
[chosen_drive
].events
= 0;
665 info
.events
= devices
[most_recent
].events
;
666 tst
->ss
->update_super(tst
, &info
, "force-one",
667 devices
[chosen_drive
].devname
, verbose
,
670 if (tst
->ss
->store_super(tst
, fd
)) {
672 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
673 devices
[chosen_drive
].devname
);
674 devices
[chosen_drive
].events
= 0;
675 tst
->ss
->free_super(tst
);
679 devices
[chosen_drive
].events
= devices
[most_recent
].events
;
680 devices
[chosen_drive
].uptodate
= 1;
681 avail
[chosen_drive
] = 1;
683 tst
->ss
->free_super(tst
);
685 /* If there are any other drives of the same vintage,
686 * add them in as well. We can't lose and we might gain
688 for (i
=0; i
<info
.array
.raid_disks
&& i
< bestcnt
; i
++) {
691 !devices
[j
].uptodate
&&
692 devices
[j
].events
> 0 &&
693 devices
[j
].events
== current_events
) {
700 /* Now we want to look at the superblock which the kernel will base things on
701 * and compare the devices that we think are working with the devices that the
702 * superblock thinks are working.
703 * If there are differences and --force is given, then update this chosen
707 st
->ss
->free_super(st
);
708 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
++) {
714 if (!devices
[j
].uptodate
)
717 if ((fd
=dev_open(devices
[j
].devname
, O_RDONLY
|O_EXCL
))< 0) {
718 fprintf(stderr
, Name
": Cannot open %s: %s\n",
719 devices
[j
].devname
, strerror(errno
));
720 if (must_close
) close(mdfd
);
723 if (st
->ss
->load_super(st
,fd
, NULL
)) {
725 fprintf(stderr
, Name
": RAID superblock has disappeared from %s\n",
727 if (must_close
) close(mdfd
);
732 if (st
->sb
== NULL
) {
733 fprintf(stderr
, Name
": No suitable drives found for %s\n", mddev
);
734 if (must_close
) close(mdfd
);
737 st
->ss
->getinfo_super(st
, &info
);
738 for (i
=0; i
<bestcnt
; i
++) {
740 unsigned int desired_state
;
742 if (i
< info
.array
.raid_disks
)
743 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
749 if (!devices
[j
].uptodate
)
751 info
.disk
.number
= devices
[j
].disk_nr
;
752 info
.disk
.raid_disk
= i
;
753 info
.disk
.state
= desired_state
;
755 if (devices
[j
].uptodate
&&
756 st
->ss
->update_super(st
, &info
, "assemble", NULL
,
760 fprintf(stderr
, Name
": "
761 "clearing FAULTY flag for device %d in %s for %s\n",
762 j
, mddev
, devices
[j
].devname
);
766 fprintf(stderr
, Name
": "
767 "device %d in %s has wrong state in superblock, but %s seems ok\n",
768 i
, mddev
, devices
[j
].devname
);
772 if (!devices
[j
].uptodate
&&
773 !(super
.disks
[i
].state
& (1 << MD_DISK_FAULTY
))) {
774 fprintf(stderr
, Name
": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
779 if (force
&& !clean
&&
780 !enough(info
.array
.level
, info
.array
.raid_disks
,
781 info
.array
.layout
, clean
,
783 change
+= st
->ss
->update_super(st
, &info
, "force-array",
784 devices
[chosen_drive
].devname
, verbose
,
791 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
793 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
794 devices
[chosen_drive
].devname
);
795 if (must_close
) close(mdfd
);
798 if (st
->ss
->store_super(st
, fd
)) {
800 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
801 devices
[chosen_drive
].devname
);
802 if (must_close
) close(mdfd
);
808 /* If we are in the middle of a reshape we may need to restore saved data
809 * that was moved aside due to the reshape overwriting live data
810 * The code of doing this lives in Grow.c
813 if (info
.reshape_active
) {
815 int *fdlist
= malloc(sizeof(int)* bestcnt
);
816 for (i
=0; i
<bestcnt
; i
++) {
819 fdlist
[i
] = dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
821 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
830 err
= Grow_restart(st
, &info
, fdlist
, bestcnt
, backup_file
);
833 if (fdlist
[i
]>=0) close(fdlist
[i
]);
836 fprintf(stderr
, Name
": Failed to restore critical section for reshape, sorry.\n");
837 if (must_close
) close(mdfd
);
842 /* count number of in-sync devices according to the superblock.
843 * We must have this number to start the array without -s or -R
845 req_cnt
= info
.array
.working_disks
;
847 /* Almost ready to actually *do* something */
850 if ((vers
% 100) >= 1) { /* can use different versions */
851 mdu_array_info_t inf
;
852 memset(&inf
, 0, sizeof(inf
));
853 inf
.major_version
= st
->ss
->major
;
854 inf
.minor_version
= st
->minor_version
;
855 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
857 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
860 fprintf(stderr
, Name
": SET_ARRAY_INFO failed for %s: %s\n",
861 mddev
, strerror(errno
));
862 if (must_close
) close(mdfd
);
865 if (ident
->bitmap_fd
>= 0) {
866 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
867 fprintf(stderr
, Name
": SET_BITMAP_FILE failed.\n");
868 if (must_close
) close(mdfd
);
871 } else if (ident
->bitmap_file
) {
872 /* From config file */
873 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
875 fprintf(stderr
, Name
": Could not open bitmap file %s\n",
877 if (must_close
) close(mdfd
);
880 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
881 fprintf(stderr
, Name
": Failed to set bitmapfile for %s\n", mddev
);
883 if (must_close
) close(mdfd
);
889 /* First, add the raid disks, but add the chosen one last */
890 for (i
=0; i
<= bestcnt
; i
++) {
894 if (j
== chosen_drive
)
899 if (j
>= 0 /* && devices[j].uptodate */) {
900 mdu_disk_info_t disk
;
901 memset(&disk
, 0, sizeof(disk
));
902 disk
.major
= devices
[j
].major
;
903 disk
.minor
= devices
[j
].minor
;
904 if (ioctl(mdfd
, ADD_NEW_DISK
, &disk
)!=0) {
905 fprintf(stderr
, Name
": failed to add %s to %s: %s\n",
909 if (i
< info
.array
.raid_disks
|| i
== bestcnt
)
913 } else if (verbose
> 0)
914 fprintf(stderr
, Name
": added %s to %s as %d\n",
915 devices
[j
].devname
, mddev
, devices
[j
].raid_disk
);
916 } else if (verbose
> 0 && i
< info
.array
.raid_disks
)
917 fprintf(stderr
, Name
": no uptodate device for slot %d of %s\n",
923 ( enough(info
.array
.level
, info
.array
.raid_disks
,
924 info
.array
.layout
, clean
, avail
, okcnt
) &&
925 (okcnt
>= req_cnt
|| start_partial_ok
)
927 if (ioctl(mdfd
, RUN_ARRAY
, NULL
)==0) {
929 fprintf(stderr
, Name
": %s has been started with %d drive%s",
930 mddev
, okcnt
, okcnt
==1?"":"s");
931 if (okcnt
< info
.array
.raid_disks
)
932 fprintf(stderr
, " (out of %d)", info
.array
.raid_disks
);
934 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
935 fprintf(stderr
, ".\n");
940 /* There is a nasty race with 'mdadm --monitor'.
941 * If it opens this device before we close it,
942 * it gets an incomplete open on which IO
943 * doesn't work and the capacity if wrong.
944 * If we reopen (to check for layered devices)
945 * before --monitor closes, we loose.
947 * So: wait upto 1 second for there to be
948 * a non-zero capacity.
950 while (usecs
< 1000) {
951 mdfd
= open(mddev
, O_RDONLY
);
953 unsigned long long size
;
954 if (get_dev_size(mdfd
, NULL
, &size
) &&
965 fprintf(stderr
, Name
": failed to RUN_ARRAY %s: %s\n",
966 mddev
, strerror(errno
));
968 if (!enough(info
.array
.level
, info
.array
.raid_disks
,
969 info
.array
.layout
, 1, avail
, okcnt
))
970 fprintf(stderr
, Name
": Not enough devices to "
971 "start the array.\n");
972 else if (!enough(info
.array
.level
,
973 info
.array
.raid_disks
,
974 info
.array
.layout
, clean
,
976 fprintf(stderr
, Name
": Not enough devices to "
977 "start the array while not clean "
978 "- consider --force.\n");
980 if (must_close
) close(mdfd
);
984 fprintf(stderr
, Name
": %s assembled from %d drive%s",
985 mddev
, okcnt
, okcnt
==1?"":"s");
986 if (okcnt
!= info
.array
.raid_disks
)
987 fprintf(stderr
, " (out of %d)", info
.array
.raid_disks
);
988 fprintf(stderr
, ", but not started.\n");
989 if (must_close
) close(mdfd
);
993 fprintf(stderr
, Name
": %s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
995 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
996 if (!enough(info
.array
.level
, info
.array
.raid_disks
,
997 info
.array
.layout
, 1, avail
, okcnt
))
998 fprintf(stderr
, " - not enough to start the array.\n");
999 else if (!enough(info
.array
.level
,
1000 info
.array
.raid_disks
,
1001 info
.array
.layout
, clean
,
1003 fprintf(stderr
, " - not enough to start the "
1004 "array while not clean - consider "
1007 if (req_cnt
== info
.array
.raid_disks
)
1008 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1010 fprintf(stderr
, " - need %d of %d to start", req_cnt
, info
.array
.raid_disks
);
1011 fprintf(stderr
, " (use --run to insist).\n");
1014 if (must_close
) close(mdfd
);
1017 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1018 * been updated to point to the current locations of devices.
1019 * so we can just start the array
1022 dev
= makedev(devices
[chosen_drive
].major
,
1023 devices
[chosen_drive
].minor
);
1024 if (ioctl(mdfd
, START_ARRAY
, dev
)) {
1025 fprintf(stderr
, Name
": Cannot start array: %s\n",
1030 if (must_close
) close(mdfd
);