]>
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
,
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");
171 devlist
= conf_get_devs();
176 /* We come back here when doing auto-assembly and attempting some
177 * set of devices failed. Those are now marked as ->used==2 and
178 * we ignore them and try again
181 tmpdev
= devlist
; num_devs
= 0;
187 tmpdev
= tmpdev
->next
;
189 devices
= malloc(num_devs
* sizeof(*devices
));
191 if (!st
&& ident
->st
) st
= ident
->st
;
194 fprintf(stderr
, Name
": looking for devices for %s\n",
195 mddev
? mddev
: "further assembly");
197 /* first walk the list of devices to find a consistent set
198 * that match the criterea, if that is possible.
199 * We flag the ones we like with 'used'.
201 for (tmpdev
= devlist
;
203 tmpdev
= tmpdev
->next
) {
204 char *devname
= tmpdev
->devname
;
207 struct supertype
*tst
= dup_super(st
);
209 if (tmpdev
->used
> 1) continue;
211 if (ident
->devices
&&
212 !match_oneof(ident
->devices
, devname
)) {
213 if ((inargv
&& verbose
>=0) || verbose
> 0)
214 fprintf(stderr
, Name
": %s is not one of %s\n", devname
, ident
->devices
);
218 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
220 if ((inargv
&& verbose
>= 0) || verbose
> 0)
221 fprintf(stderr
, Name
": cannot open device %s: %s\n",
222 devname
, strerror(errno
));
224 } else if (fstat(dfd
, &stb
)< 0) {
226 fprintf(stderr
, Name
": fstat failed for %s: %s\n",
227 devname
, strerror(errno
));
229 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
230 fprintf(stderr
, Name
": %s is not a block device.\n",
233 } else if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
234 if ((inargv
&& verbose
>= 0) || verbose
> 0)
235 fprintf(stderr
, Name
": no recogniseable superblock on %s\n",
238 } else if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
239 if ((inargv
&& verbose
>= 0) || verbose
> 0)
240 fprintf( stderr
, Name
": no RAID superblock on %s\n",
244 memset(content
, 0, sizeof(*content
));
245 tst
->ss
->getinfo_super(tst
, content
);
247 if (dfd
>= 0) close(dfd
);
249 if (ident
->uuid_set
&& (!update
|| strcmp(update
, "uuid")!= 0) &&
251 same_uuid(content
->uuid
, ident
->uuid
, tst
->ss
->swapuuid
)==0)) {
252 if ((inargv
&& verbose
>= 0) || verbose
> 0)
253 fprintf(stderr
, Name
": %s has wrong uuid.\n",
257 if (ident
->name
[0] && (!update
|| strcmp(update
, "name")!= 0) &&
259 name_matches(content
->name
, ident
->name
, homehost
)==0)) {
260 if ((inargv
&& verbose
>= 0) || verbose
> 0)
261 fprintf(stderr
, Name
": %s has wrong name.\n",
265 if (ident
->super_minor
!= UnSet
&&
267 ident
->super_minor
!= content
->array
.md_minor
)) {
268 if ((inargv
&& verbose
>= 0) || verbose
> 0)
269 fprintf(stderr
, Name
": %s has wrong super-minor.\n",
273 if (ident
->level
!= UnSet
&&
275 ident
->level
!= content
->array
.level
)) {
276 if ((inargv
&& verbose
>= 0) || verbose
> 0)
277 fprintf(stderr
, Name
": %s has wrong raid level.\n",
281 if (ident
->raid_disks
!= UnSet
&&
283 ident
->raid_disks
!= content
->array
.raid_disks
)) {
284 if ((inargv
&& verbose
>= 0) || verbose
> 0)
285 fprintf(stderr
, Name
": %s requires wrong number of drives.\n",
290 if (tst
== NULL
|| tst
->sb
== NULL
)
293 /* If we are this far, then we are nearly commited to this device.
294 * If the super_block doesn't exist, or doesn't match others,
295 * then we probably cannot continue
296 * However if one of the arrays is for the homehost, and
297 * the other isn't that can disambiguate.
300 if (!tst
|| !tst
->sb
) {
301 fprintf(stderr
, Name
": %s has no superblock - assembly aborted\n",
304 st
->ss
->free_super(st
);
310 if (st
->minor_version
== -1)
311 st
->minor_version
= tst
->minor_version
;
312 if (st
->ss
!= tst
->ss
||
313 st
->minor_version
!= tst
->minor_version
||
314 st
->ss
->compare_super(st
, tst
) != 0) {
315 /* Some mismatch. If exactly one array matches this host,
316 * we can resolve on that one.
317 * Or, if we are auto assembling, we just ignore the second
323 int first
= st
->ss
->match_home(st
, homehost
);
324 int last
= tst
->ss
->match_home(tst
, homehost
);
326 (first
== 1 || last
== 1)) {
327 /* We can do something */
328 if (first
) {/* just ignore this one */
329 if ((inargv
&& verbose
>= 0) || verbose
> 0)
330 fprintf(stderr
, Name
": %s misses out due to wrong homehost\n",
333 } else { /* reject all those sofar */
335 if ((inargv
&& verbose
>= 0) || verbose
> 0)
336 fprintf(stderr
, Name
": %s overrides previous devices due to good homehost\n",
338 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
346 fprintf(stderr
, Name
": superblock on %s doesn't match others - assembly aborted\n",
348 tst
->ss
->free_super(tst
);
349 st
->ss
->free_super(st
);
357 tst
->ss
->free_super(tst
);
360 if (!st
|| !st
->sb
|| !content
)
363 /* Now need to open array the device. Use create_mddev */
364 if (content
== &info
)
365 st
->ss
->getinfo_super(st
, content
);
367 trustworthy
= FOREIGN
;
368 switch (st
->ss
->match_home(st
, homehost
)) {
370 trustworthy
= FOREIGN
;
371 name
= content
->name
;
375 name
= strchr(content
->name
, ':');
379 name
= content
->name
;
382 trustworthy
= FOREIGN
;
385 if (!auto_assem
&& trustworthy
== FOREIGN
)
386 /* If the array is listed in mdadm or on
387 * command line, then we trust the name
388 * even if the array doesn't look local
392 if (content
->name
[0] == 0 &&
393 content
->array
.level
== LEVEL_CONTAINER
) {
394 name
= content
->text_version
;
395 trustworthy
= METADATA
;
397 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
400 st
->ss
->free_super(st
);
407 vers
= md_get_version(mdfd
);
409 fprintf(stderr
, Name
": Assemble requires driver version 0.90.0 or later.\n"
410 " Upgrade your kernel or try --build\n");
414 if (ioctl(mdfd
, GET_ARRAY_INFO
, &tmp_inf
)==0) {
415 fprintf(stderr
, Name
": %s already active, cannot restart it!\n",
417 for (tmpdev
= devlist
;
418 tmpdev
&& tmpdev
->used
!= 1;
419 tmpdev
= tmpdev
->next
)
421 if (tmpdev
&& auto_assem
)
422 fprintf(stderr
, Name
": %s needed for %s...\n",
423 mddev
, tmpdev
->devname
);
426 st
->ss
->free_super(st
);
432 ioctl(mdfd
, STOP_ARRAY
, NULL
); /* just incase it was started but has no content */
434 /* Ok, no bad inconsistancy, we can try updating etc */
436 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) if (tmpdev
->used
== 1) {
437 char *devname
= tmpdev
->devname
;
439 /* looks like a good enough match to update the super block if needed */
443 /* prepare useful information in info structures */
445 struct supertype
*tst
;
448 if (strcmp(update
, "uuid")==0 &&
451 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
452 read(rfd
, ident
->uuid
, 16) != 16) {
453 *(__u32
*)(ident
->uuid
) = random();
454 *(__u32
*)(ident
->uuid
+1) = random();
455 *(__u32
*)(ident
->uuid
+2) = random();
456 *(__u32
*)(ident
->uuid
+3) = random();
458 if (rfd
>= 0) close(rfd
);
460 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
462 remove_partitions(dfd
);
465 tst
->ss
->load_super(tst
, dfd
, NULL
);
466 tst
->ss
->getinfo_super(tst
, content
);
468 memcpy(content
->uuid
, ident
->uuid
, 16);
469 strcpy(content
->name
, ident
->name
);
470 content
->array
.md_minor
= minor(stb2
.st_rdev
);
472 tst
->ss
->update_super(tst
, content
, update
,
474 ident
->uuid_set
, homehost
);
475 if (strcmp(update
, "uuid")==0 &&
478 memcpy(ident
->uuid
, content
->uuid
, 16);
481 fprintf(stderr
, Name
": Cannot open %s for superblock update\n",
483 else if (tst
->ss
->store_super(tst
, dfd
))
484 fprintf(stderr
, Name
": Could not re-write superblock on %s.\n",
489 if (strcmp(update
, "uuid")==0 &&
490 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
491 if (bitmap_update_uuid(ident
->bitmap_fd
,
493 tst
->ss
->swapuuid
) != 0)
494 fprintf(stderr
, Name
": Could not update uuid on external bitmap.\n");
498 tst
->ss
->free_super(tst
);
502 struct supertype
*tst
= dup_super(st
);
504 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
506 remove_partitions(dfd
);
508 tst
->ss
->load_super(tst
, dfd
, NULL
);
509 tst
->ss
->getinfo_super(tst
, content
);
510 tst
->ss
->free_super(tst
);
517 fprintf(stderr
, Name
": %s is identified as a member of %s, slot %d.\n",
518 devname
, mddev
, content
->disk
.raid_disk
);
519 devices
[devcnt
].devname
= devname
;
520 devices
[devcnt
].uptodate
= 0;
521 devices
[devcnt
].i
= *content
;
522 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
523 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
524 if (most_recent
< devcnt
) {
525 if (devices
[devcnt
].i
.events
526 > devices
[most_recent
].i
.events
)
527 most_recent
= devcnt
;
529 if (content
->array
.level
== -4)
530 /* with multipath, the raid_disk from the superblock is meaningless */
533 i
= devices
[devcnt
].i
.disk
.raid_disk
;
535 if (nextspare
< content
->array
.raid_disks
)
536 nextspare
= content
->array
.raid_disks
;
539 if (i
>= content
->array
.raid_disks
&&
545 unsigned int newbestcnt
= i
+10;
546 int *newbest
= malloc(sizeof(int)*newbestcnt
);
548 for (c
=0; c
< newbestcnt
; c
++)
550 newbest
[c
] = best
[c
];
555 bestcnt
= newbestcnt
;
558 devices
[best
[i
]].i
.events
559 == devices
[devcnt
].i
.events
560 && (devices
[best
[i
]].i
.disk
.minor
561 != devices
[devcnt
].i
.disk
.minor
)
563 && content
->array
.level
!= LEVEL_MULTIPATH
) {
564 /* two different devices with identical superblock.
565 * Could be a mis-detection caused by overlapping
566 * partitions. fail-safe.
568 fprintf(stderr
, Name
": WARNING %s and %s appear"
569 " to have very similar superblocks.\n"
570 " If they are really different, "
571 "please --zero the superblock on one\n"
572 " If they are the same or overlap,"
573 " please remove one from %s.\n",
574 devices
[best
[i
]].devname
, devname
,
575 inargv
? "the list" :
576 "the\n DEVICE list in mdadm.conf"
582 || (devices
[best
[i
]].i
.events
583 < devices
[devcnt
].i
.events
))
590 fprintf(stderr
, Name
": no devices found for %s\n",
593 st
->ss
->free_super(st
);
598 if (update
&& strcmp(update
, "byteorder")==0)
599 st
->minor_version
= 90;
601 st
->ss
->getinfo_super(st
, content
);
602 clean
= content
->array
.state
& 1;
604 /* now we have some devices that might be suitable.
607 avail
= malloc(content
->array
.raid_disks
);
608 memset(avail
, 0, content
->array
.raid_disks
);
611 for (i
=0; i
< bestcnt
;i
++) {
613 int event_margin
= 1; /* always allow a difference of '1'
614 * like the kernel does
617 /* note: we ignore error flags in multipath arrays
618 * as they don't make sense
620 if (content
->array
.level
!= -4)
621 if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_SYNC
))) {
622 if (!(devices
[j
].i
.disk
.state
623 & (1<<MD_DISK_FAULTY
)))
627 if (devices
[j
].i
.events
+event_margin
>=
628 devices
[most_recent
].i
.events
) {
629 devices
[j
].uptodate
= 1;
630 if (i
< content
->array
.raid_disks
) {
637 while (force
&& !enough(content
->array
.level
, content
->array
.raid_disks
,
638 content
->array
.layout
, 1,
640 /* Choose the newest best drive which is
641 * not up-to-date, update the superblock
645 struct supertype
*tst
;
646 long long current_events
;
648 for (i
=0; i
<content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
651 !devices
[j
].uptodate
&&
652 devices
[j
].i
.events
> 0 &&
655 > devices
[chosen_drive
].i
.events
))
658 if (chosen_drive
< 0)
660 current_events
= devices
[chosen_drive
].i
.events
;
663 fprintf(stderr
, Name
": forcing event count in %s(%d) from %d upto %d\n",
664 devices
[chosen_drive
].devname
,
665 devices
[chosen_drive
].i
.disk
.raid_disk
,
666 (int)(devices
[chosen_drive
].i
.events
),
667 (int)(devices
[most_recent
].i
.events
));
668 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
670 fprintf(stderr
, Name
": Couldn't open %s for write - not updating\n",
671 devices
[chosen_drive
].devname
);
672 devices
[chosen_drive
].i
.events
= 0;
676 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
678 fprintf(stderr
, Name
": RAID superblock disappeared from %s - not updating.\n",
679 devices
[chosen_drive
].devname
);
680 devices
[chosen_drive
].i
.events
= 0;
683 content
->events
= devices
[most_recent
].i
.events
;
684 tst
->ss
->update_super(tst
, content
, "force-one",
685 devices
[chosen_drive
].devname
, verbose
,
688 if (tst
->ss
->store_super(tst
, fd
)) {
690 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
691 devices
[chosen_drive
].devname
);
692 devices
[chosen_drive
].i
.events
= 0;
693 tst
->ss
->free_super(tst
);
697 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
698 devices
[chosen_drive
].uptodate
= 1;
699 avail
[chosen_drive
] = 1;
701 tst
->ss
->free_super(tst
);
703 /* If there are any other drives of the same vintage,
704 * add them in as well. We can't lose and we might gain
706 for (i
=0; i
<content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
709 !devices
[j
].uptodate
&&
710 devices
[j
].i
.events
> 0 &&
711 devices
[j
].i
.events
== current_events
) {
718 /* Now we want to look at the superblock which the kernel will base things on
719 * and compare the devices that we think are working with the devices that the
720 * superblock thinks are working.
721 * If there are differences and --force is given, then update this chosen
725 st
->ss
->free_super(st
);
726 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
++) {
732 if (!devices
[j
].uptodate
)
735 if ((fd
=dev_open(devices
[j
].devname
, O_RDONLY
|O_EXCL
))< 0) {
736 fprintf(stderr
, Name
": Cannot open %s: %s\n",
737 devices
[j
].devname
, strerror(errno
));
741 if (st
->ss
->load_super(st
,fd
, NULL
)) {
743 fprintf(stderr
, Name
": RAID superblock has disappeared from %s\n",
750 if (st
->sb
== NULL
) {
751 fprintf(stderr
, Name
": No suitable drives found for %s\n", mddev
);
755 st
->ss
->getinfo_super(st
, content
);
757 sysfs_init(content
, mdfd
, 0);
759 for (i
=0; i
<bestcnt
; i
++) {
761 unsigned int desired_state
;
763 if (i
< content
->array
.raid_disks
)
764 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
770 if (!devices
[j
].uptodate
)
773 devices
[j
].i
.disk
.state
= desired_state
;
775 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
779 fprintf(stderr
, Name
": "
780 "clearing FAULTY flag for device %d in %s for %s\n",
781 j
, mddev
, devices
[j
].devname
);
785 fprintf(stderr
, Name
": "
786 "device %d in %s has wrong state in superblock, but %s seems ok\n",
787 i
, mddev
, devices
[j
].devname
);
791 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
792 fprintf(stderr
, Name
": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
797 if (force
&& !clean
&&
798 !enough(content
->array
.level
, content
->array
.raid_disks
,
799 content
->array
.layout
, clean
,
801 change
+= st
->ss
->update_super(st
, content
, "force-array",
802 devices
[chosen_drive
].devname
, verbose
,
809 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
811 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
812 devices
[chosen_drive
].devname
);
816 if (st
->ss
->store_super(st
, fd
)) {
818 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
819 devices
[chosen_drive
].devname
);
826 /* If we are in the middle of a reshape we may need to restore saved data
827 * that was moved aside due to the reshape overwriting live data
828 * The code of doing this lives in Grow.c
831 if (content
->reshape_active
) {
833 int *fdlist
= malloc(sizeof(int)* bestcnt
);
834 for (i
=0; i
<bestcnt
; i
++) {
837 fdlist
[i
] = dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
839 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
848 err
= Grow_restart(st
, content
, fdlist
, bestcnt
, backup_file
);
851 if (fdlist
[i
]>=0) close(fdlist
[i
]);
854 fprintf(stderr
, Name
": Failed to restore critical section for reshape, sorry.\n");
860 /* count number of in-sync devices according to the superblock.
861 * We must have this number to start the array without -s or -R
863 req_cnt
= content
->array
.working_disks
;
865 /* Almost ready to actually *do* something */
869 /* First, fill in the map, so that udev can find our name
870 * as soon as we become active.
872 map_update(NULL
, fd2devnum(mdfd
), content
->text_version
,
873 content
->uuid
, chosen_name
);
875 rv
= set_array_info(mdfd
, st
, content
);
877 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
878 mddev
, strerror(errno
));
882 if (ident
->bitmap_fd
>= 0) {
883 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
884 fprintf(stderr
, Name
": SET_BITMAP_FILE failed.\n");
888 } else if (ident
->bitmap_file
) {
889 /* From config file */
890 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
892 fprintf(stderr
, Name
": Could not open bitmap file %s\n",
897 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
898 fprintf(stderr
, Name
": Failed to set bitmapfile for %s\n", mddev
);
906 /* First, add the raid disks, but add the chosen one last */
907 for (i
=0; i
<= bestcnt
; i
++) {
911 if (j
== chosen_drive
)
916 if (j
>= 0 /* && devices[j].uptodate */) {
917 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
920 fprintf(stderr
, Name
": failed to add "
925 if (i
< content
->array
.raid_disks
930 } else if (verbose
> 0)
931 fprintf(stderr
, Name
": added %s "
933 devices
[j
].devname
, mddev
,
934 devices
[j
].i
.disk
.raid_disk
);
935 } else if (verbose
> 0 && i
< content
->array
.raid_disks
)
936 fprintf(stderr
, Name
": no uptodate device for "
941 if (content
->array
.level
== LEVEL_CONTAINER
) {
943 fprintf(stderr
, Name
": Container %s has been "
944 "assembled with %d drive%s",
945 mddev
, okcnt
+sparecnt
, okcnt
+sparecnt
==1?"":"s");
946 if (okcnt
< content
->array
.raid_disks
)
947 fprintf(stderr
, " (out of %d)",
948 content
->array
.raid_disks
);
949 fprintf(stderr
, "\n");
951 sysfs_uevent(content
, "change");
958 ( enough(content
->array
.level
, content
->array
.raid_disks
,
959 content
->array
.layout
, clean
, avail
, okcnt
) &&
960 (okcnt
>= req_cnt
|| start_partial_ok
)
962 if (ioctl(mdfd
, RUN_ARRAY
, NULL
)==0) {
964 fprintf(stderr
, Name
": %s has been started with %d drive%s",
965 mddev
, okcnt
, okcnt
==1?"":"s");
966 if (okcnt
< content
->array
.raid_disks
)
967 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
969 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
970 fprintf(stderr
, ".\n");
975 /* There is a nasty race with 'mdadm --monitor'.
976 * If it opens this device before we close it,
977 * it gets an incomplete open on which IO
978 * doesn't work and the capacity is
980 * If we reopen (to check for layered devices)
981 * before --monitor closes, we loose.
983 * So: wait upto 1 second for there to be
984 * a non-zero capacity.
986 while (usecs
< 1000) {
987 mdfd
= open(mddev
, O_RDONLY
);
989 unsigned long long size
;
990 if (get_dev_size(mdfd
, NULL
, &size
) &&
1001 fprintf(stderr
, Name
": failed to RUN_ARRAY %s: %s\n",
1002 mddev
, strerror(errno
));
1004 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1005 content
->array
.layout
, 1, avail
, okcnt
))
1006 fprintf(stderr
, Name
": Not enough devices to "
1007 "start the array.\n");
1008 else if (!enough(content
->array
.level
,
1009 content
->array
.raid_disks
,
1010 content
->array
.layout
, clean
,
1012 fprintf(stderr
, Name
": Not enough devices to "
1013 "start the array while not clean "
1014 "- consider --force.\n");
1017 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1021 if (runstop
== -1) {
1022 fprintf(stderr
, Name
": %s assembled from %d drive%s",
1023 mddev
, okcnt
, okcnt
==1?"":"s");
1024 if (okcnt
!= content
->array
.raid_disks
)
1025 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1026 fprintf(stderr
, ", but not started.\n");
1030 if (verbose
>= -1) {
1031 fprintf(stderr
, Name
": %s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
1033 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1034 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1035 content
->array
.layout
, 1, avail
, okcnt
))
1036 fprintf(stderr
, " - not enough to start the array.\n");
1037 else if (!enough(content
->array
.level
,
1038 content
->array
.raid_disks
,
1039 content
->array
.layout
, clean
,
1041 fprintf(stderr
, " - not enough to start the "
1042 "array while not clean - consider "
1045 if (req_cnt
== content
->array
.raid_disks
)
1046 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1048 fprintf(stderr
, " - need %d of %d to start", req_cnt
, content
->array
.raid_disks
);
1049 fprintf(stderr
, " (use --run to insist).\n");
1053 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1056 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1057 * been updated to point to the current locations of devices.
1058 * so we can just start the array
1061 dev
= makedev(devices
[chosen_drive
].i
.disk
.major
,
1062 devices
[chosen_drive
].i
.disk
.minor
);
1063 if (ioctl(mdfd
, START_ARRAY
, dev
)) {
1064 fprintf(stderr
, Name
": Cannot start array: %s\n",
1074 int assemble_container_content(struct supertype
*st
, int mdfd
,
1075 struct mdinfo
*content
, int runstop
,
1076 char *chosen_name
, int verbose
)
1078 struct mdinfo
*dev
, *sra
;
1079 int working
= 0, preexist
= 0;
1080 struct map_ent
*map
= NULL
;
1082 sysfs_init(content
, mdfd
, 0);
1084 sra
= sysfs_read(mdfd
, 0, GET_VERSION
);
1085 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0)
1086 if (sysfs_set_array(content
, md_get_version(mdfd
)) != 0)
1091 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
1092 if (sysfs_add_disk(content
, dev
) == 0)
1094 else if (errno
== EEXIST
)
1097 /* Nothing new, don't try to start */ ;
1098 else if (runstop
> 0 ||
1099 (working
+ preexist
) >= content
->array
.working_disks
) {
1100 switch(content
->array
.level
) {
1102 case LEVEL_MULTIPATH
:
1104 sysfs_set_str(content
, NULL
, "array_state",
1108 sysfs_set_str(content
, NULL
, "array_state",
1110 /* start mdmon if needed. */
1111 if (!mdmon_running(st
->container_dev
))
1112 start_mdmon(st
->container_dev
);
1113 ping_monitor(devnum2devname(st
->container_dev
));
1116 sysfs_set_safemode(content
, content
->safe_mode_delay
);
1118 fprintf(stderr
, Name
1119 ": Started %s with %d devices",
1120 chosen_name
, working
+ preexist
);
1122 fprintf(stderr
, " (%d new)", working
);
1123 fprintf(stderr
, "\n");
1125 /* FIXME should have an O_EXCL and wait for read-auto */
1128 fprintf(stderr
, Name
1129 ": %s assembled with %d devices but "
1131 chosen_name
, working
);
1132 map_update(&map
, fd2devnum(mdfd
),
1133 content
->text_version
,
1134 content
->uuid
, chosen_name
);