]>
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 static int is_member_busy(char *metadata_version
)
55 /* check if the given member array is active */
56 struct mdstat_ent
*mdstat
= mdstat_read(1, 0);
57 struct mdstat_ent
*ent
;
60 for (ent
= mdstat
; ent
; ent
= ent
->next
) {
61 if (ent
->metadata_version
== NULL
)
63 if (strncmp(ent
->metadata_version
, "external:", 9) != 0)
65 if (!is_subarray(&ent
->metadata_version
[9]))
67 /* Skip first char - it can be '/' or '-' */
68 if (strcmp(&ent
->metadata_version
[10], metadata_version
+1) == 0) {
78 int Assemble(struct supertype
*st
, char *mddev
,
80 mddev_dev_t devlist
, char *backup_file
,
81 int readonly
, int runstop
,
82 char *update
, char *homehost
,
83 int verbose
, int force
)
86 * The task of Assemble is to find a collection of
87 * devices that should (according to their superblocks)
88 * form an array, and to give this collection to the MD driver.
89 * In Linux-2.4 and later, this involves submitting a
90 * SET_ARRAY_INFO ioctl with no arg - to prepare
91 * the array - and then submit a number of
92 * ADD_NEW_DISK ioctls to add disks into
93 * the array. Finally RUN_ARRAY might
94 * be submitted to start the array.
96 * Much of the work of Assemble is in finding and/or
97 * checking the disks to make sure they look right.
99 * If mddev is not set, then scan must be set and we
100 * read through the config file for dev+uuid mapping
101 * We recurse, setting mddev, for each device that
103 * - has a valid uuid (or any uuid if !uuidset)
105 * If mddev is set, we try to determine state of md.
106 * check version - must be at least 0.90.0
107 * check kernel version. must be at least 2.4.
108 * If not, we can possibly fall back on START_ARRAY
109 * Try to GET_ARRAY_INFO.
110 * If possible, give up
111 * If not, try to STOP_ARRAY just to make sure
113 * If !uuidset and scan, look in conf-file for uuid
114 * If not found, give up
115 * If !devlist and scan and uuidset, get list of devs from conf-file
118 * Check superblock - discard if bad
119 * Check uuid (set if we don't have one) - discard if no match
120 * Check superblock similarity if we have a superblock - discard if different
121 * Record events, devicenum
122 * This should give us a list of devices for the array
123 * We should collect the most recent event number
125 * Count disks with recent enough event count
126 * While force && !enough disks
127 * Choose newest rejected disks, update event count
128 * mark clean and rewrite superblock
131 * foreach device with recent events : ADD_NEW_DISK
132 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
134 * Check the device numbers in superblock are right
135 * update superblock if any changes
141 int auto_assem
= (mddev
== NULL
&& !ident
->uuid_set
&&
142 ident
->super_minor
== UnSet
&& ident
->name
[0] == 0
143 && ident
->container
== NULL
&& ident
->member
== NULL
);
145 int vers
= vers
; /* Keep gcc quite - it really is initialised */
148 int uptodate
; /* set once we decide that this device is as
149 * recent as everything else in the array.
153 int *best
= NULL
; /* indexed by raid_disk */
154 unsigned int bestcnt
= 0;
156 unsigned int okcnt
, sparecnt
;
157 unsigned int req_cnt
;
163 int report_missmatch
;
165 int start_partial_ok
= (runstop
>= 0) &&
166 (force
|| devlist
==NULL
|| auto_assem
);
167 unsigned int num_devs
;
170 struct mdinfo
*content
= NULL
;
175 char chosen_name
[1024];
177 if (get_linux_version() < 2004000)
181 * If any subdevs are listed, then any that don't
182 * match ident are discarded. Remainder must all match and
184 * If no subdevs, then we scan all devices in the config file, but
185 * there must be something in the identity
189 ident
->uuid_set
== 0 &&
190 ident
->super_minor
< 0 &&
191 ident
->devices
== NULL
) {
192 fprintf(stderr
, Name
": No identity information available for %s - cannot assemble.\n",
193 mddev
? mddev
: "further assembly");
198 devlist
= conf_get_devs();
202 report_missmatch
= ((inargv
&& verbose
>= 0) || verbose
> 0);
204 /* We come back here when doing auto-assembly and attempting some
205 * set of devices failed. Those are now marked as ->used==2 and
206 * we ignore them and try again
209 tmpdev
= devlist
; num_devs
= 0;
215 tmpdev
= tmpdev
->next
;
217 devices
= malloc(num_devs
* sizeof(*devices
));
219 if (!st
&& ident
->st
) st
= ident
->st
;
222 fprintf(stderr
, Name
": looking for devices for %s\n",
223 mddev
? mddev
: "further assembly");
225 /* first walk the list of devices to find a consistent set
226 * that match the criterea, if that is possible.
227 * We flag the ones we like with 'used'.
229 for (tmpdev
= devlist
;
231 tmpdev
= tmpdev
->next
) {
232 char *devname
= tmpdev
->devname
;
235 struct supertype
*tst
= dup_super(st
);
237 if (tmpdev
->used
> 1) continue;
239 if (ident
->devices
&&
240 !match_oneof(ident
->devices
, devname
)) {
241 if (report_missmatch
)
242 fprintf(stderr
, Name
": %s is not one of %s\n", devname
, ident
->devices
);
246 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
248 if (report_missmatch
)
249 fprintf(stderr
, Name
": cannot open device %s: %s\n",
250 devname
, strerror(errno
));
252 } else if (fstat(dfd
, &stb
)< 0) {
254 fprintf(stderr
, Name
": fstat failed for %s: %s\n",
255 devname
, strerror(errno
));
257 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
258 fprintf(stderr
, Name
": %s is not a block device.\n",
261 } else if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
262 if (report_missmatch
)
263 fprintf(stderr
, Name
": no recogniseable superblock on %s\n",
266 } else if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
267 if (report_missmatch
)
268 fprintf( stderr
, Name
": no RAID superblock on %s\n",
272 memset(content
, 0, sizeof(*content
));
273 tst
->ss
->getinfo_super(tst
, content
);
275 if (dfd
>= 0) close(dfd
);
277 if (tst
&& tst
->sb
&& tst
->ss
->container_content
278 && tst
->loaded_container
) {
279 /* tmpdev is a container. We need to be either
280 * looking for a member, or auto-assembling
283 /* already found some components, this cannot
286 if (report_missmatch
)
287 fprintf(stderr
, Name
": %s is a container, but we are looking for components\n",
292 if (ident
->container
) {
293 if (ident
->container
[0] == '/' &&
294 !same_dev(ident
->container
, devname
)) {
295 if (report_missmatch
)
296 fprintf(stderr
, Name
": %s is not the container required (%s)\n",
297 devname
, ident
->container
);
300 if (ident
->container
[0] != '/') {
303 if (!parse_uuid(ident
->container
, uuid
) ||
304 !same_uuid(content
->uuid
, uuid
, tst
->ss
->swapuuid
)) {
305 if (report_missmatch
)
306 fprintf(stderr
, Name
": %s has wrong UUID to be required container\n",
312 /* It is worth looking inside this container.
316 content
= tmpdev
->content
;
318 content
= tst
->ss
->container_content(tst
);
320 tmpdev
->content
= content
->next
;
321 if (tmpdev
->content
== NULL
)
324 } else if (ident
->container
|| ident
->member
) {
325 /* No chance of this matching if we don't have
327 if (report_missmatch
)
328 fprintf(stderr
, Name
"%s is not a container, and one is required.\n",
333 if (ident
->uuid_set
&& (!update
|| strcmp(update
, "uuid")!= 0) &&
335 same_uuid(content
->uuid
, ident
->uuid
, tst
->ss
->swapuuid
)==0)) {
336 if (report_missmatch
)
337 fprintf(stderr
, Name
": %s has wrong uuid.\n",
341 if (ident
->name
[0] && (!update
|| strcmp(update
, "name")!= 0) &&
343 name_matches(content
->name
, ident
->name
, homehost
)==0)) {
344 if (report_missmatch
)
345 fprintf(stderr
, Name
": %s has wrong name.\n",
349 if (ident
->super_minor
!= UnSet
&&
351 ident
->super_minor
!= content
->array
.md_minor
)) {
352 if (report_missmatch
)
353 fprintf(stderr
, Name
": %s has wrong super-minor.\n",
357 if (ident
->level
!= UnSet
&&
359 ident
->level
!= content
->array
.level
)) {
360 if (report_missmatch
)
361 fprintf(stderr
, Name
": %s has wrong raid level.\n",
365 if (ident
->raid_disks
!= UnSet
&&
367 ident
->raid_disks
!= content
->array
.raid_disks
)) {
368 if (report_missmatch
)
369 fprintf(stderr
, Name
": %s requires wrong number of drives.\n",
374 if (tst
== NULL
|| tst
->sb
== NULL
)
377 /* If we are this far, then we are nearly commited to this device.
378 * If the super_block doesn't exist, or doesn't match others,
379 * then we probably cannot continue
380 * However if one of the arrays is for the homehost, and
381 * the other isn't that can disambiguate.
384 if (!tst
|| !tst
->sb
) {
385 fprintf(stderr
, Name
": %s has no superblock - assembly aborted\n",
388 st
->ss
->free_super(st
);
392 if (tst
&& tst
->sb
&& tst
->ss
->container_content
393 && tst
->loaded_container
) {
394 /* we have the one container we need, don't keep
395 * looking. If the chosen member is active, skip.
397 if (is_member_busy(content
->text_version
)) {
398 if (report_missmatch
)
399 fprintf(stderr
, Name
": member %s in %s is already assembled\n",
400 content
->text_version
,
402 tst
->ss
->free_super(tst
);
409 st
= tst
; tst
= NULL
;
410 if (!auto_assem
&& tmpdev
->next
!= NULL
) {
411 fprintf(stderr
, Name
": %s is a container, but is not "
412 "only device given: confused and aborting\n",
414 st
->ss
->free_super(st
);
421 if (st
->minor_version
== -1)
422 st
->minor_version
= tst
->minor_version
;
423 if (st
->ss
!= tst
->ss
||
424 st
->minor_version
!= tst
->minor_version
||
425 st
->ss
->compare_super(st
, tst
) != 0) {
426 /* Some mismatch. If exactly one array matches this host,
427 * we can resolve on that one.
428 * Or, if we are auto assembling, we just ignore the second
434 int first
= st
->ss
->match_home(st
, homehost
);
435 int last
= tst
->ss
->match_home(tst
, homehost
);
437 (first
== 1 || last
== 1)) {
438 /* We can do something */
439 if (first
) {/* just ignore this one */
440 if (report_missmatch
)
441 fprintf(stderr
, Name
": %s misses out due to wrong homehost\n",
444 } else { /* reject all those sofar */
446 if (report_missmatch
)
447 fprintf(stderr
, Name
": %s overrides previous devices due to good homehost\n",
449 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
457 fprintf(stderr
, Name
": superblock on %s doesn't match others - assembly aborted\n",
459 tst
->ss
->free_super(tst
);
460 st
->ss
->free_super(st
);
470 tst
->ss
->free_super(tst
);
473 if (!st
|| !st
->sb
|| !content
)
476 /* Now need to open array the device. Use create_mddev */
477 if (content
== &info
)
478 st
->ss
->getinfo_super(st
, content
);
480 trustworthy
= FOREIGN
;
481 switch (st
->ss
->match_home(st
, homehost
)) {
483 trustworthy
= FOREIGN
;
484 name
= content
->name
;
488 name
= strchr(content
->name
, ':');
492 name
= content
->name
;
495 trustworthy
= FOREIGN
;
498 if (!auto_assem
&& trustworthy
== FOREIGN
)
499 /* If the array is listed in mdadm or on
500 * command line, then we trust the name
501 * even if the array doesn't look local
505 if (content
->name
[0] == 0 &&
506 content
->array
.level
== LEVEL_CONTAINER
) {
507 name
= content
->text_version
;
508 trustworthy
= METADATA
;
510 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
513 st
->ss
->free_super(st
);
520 vers
= md_get_version(mdfd
);
522 fprintf(stderr
, Name
": Assemble requires driver version 0.90.0 or later.\n"
523 " Upgrade your kernel or try --build\n");
527 if (mddev_busy(fd2devnum(mdfd
))) {
528 fprintf(stderr
, Name
": %s already active, cannot restart it!\n",
530 for (tmpdev
= devlist
;
531 tmpdev
&& tmpdev
->used
!= 1;
532 tmpdev
= tmpdev
->next
)
534 if (tmpdev
&& auto_assem
)
535 fprintf(stderr
, Name
": %s needed for %s...\n",
536 mddev
, tmpdev
->devname
);
539 st
->ss
->free_super(st
);
545 ioctl(mdfd
, STOP_ARRAY
, NULL
); /* just incase it was started but has no content */
548 if (content
!= &info
) {
549 /* This is a member of a container. Try starting the array. */
550 return assemble_container_content(st
, mdfd
, content
, runstop
,
551 chosen_name
, verbose
);
554 /* Ok, no bad inconsistancy, we can try updating etc */
556 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) if (tmpdev
->used
== 1) {
557 char *devname
= tmpdev
->devname
;
559 /* looks like a good enough match to update the super block if needed */
563 /* prepare useful information in info structures */
565 struct supertype
*tst
;
568 if (strcmp(update
, "uuid")==0 &&
571 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
572 read(rfd
, ident
->uuid
, 16) != 16) {
573 *(__u32
*)(ident
->uuid
) = random();
574 *(__u32
*)(ident
->uuid
+1) = random();
575 *(__u32
*)(ident
->uuid
+2) = random();
576 *(__u32
*)(ident
->uuid
+3) = random();
578 if (rfd
>= 0) close(rfd
);
580 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
582 remove_partitions(dfd
);
585 tst
->ss
->load_super(tst
, dfd
, NULL
);
586 tst
->ss
->getinfo_super(tst
, content
);
588 memcpy(content
->uuid
, ident
->uuid
, 16);
589 strcpy(content
->name
, ident
->name
);
590 content
->array
.md_minor
= minor(stb2
.st_rdev
);
592 tst
->ss
->update_super(tst
, content
, update
,
594 ident
->uuid_set
, homehost
);
595 if (strcmp(update
, "uuid")==0 &&
598 memcpy(ident
->uuid
, content
->uuid
, 16);
601 fprintf(stderr
, Name
": Cannot open %s for superblock update\n",
603 else if (tst
->ss
->store_super(tst
, dfd
))
604 fprintf(stderr
, Name
": Could not re-write superblock on %s.\n",
609 if (strcmp(update
, "uuid")==0 &&
610 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
611 if (bitmap_update_uuid(ident
->bitmap_fd
,
613 tst
->ss
->swapuuid
) != 0)
614 fprintf(stderr
, Name
": Could not update uuid on external bitmap.\n");
618 tst
->ss
->free_super(tst
);
622 struct supertype
*tst
= dup_super(st
);
624 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
626 remove_partitions(dfd
);
628 tst
->ss
->load_super(tst
, dfd
, NULL
);
629 tst
->ss
->getinfo_super(tst
, content
);
630 tst
->ss
->free_super(tst
);
637 fprintf(stderr
, Name
": %s is identified as a member of %s, slot %d.\n",
638 devname
, mddev
, content
->disk
.raid_disk
);
639 devices
[devcnt
].devname
= devname
;
640 devices
[devcnt
].uptodate
= 0;
641 devices
[devcnt
].i
= *content
;
642 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
643 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
644 if (most_recent
< devcnt
) {
645 if (devices
[devcnt
].i
.events
646 > devices
[most_recent
].i
.events
)
647 most_recent
= devcnt
;
649 if (content
->array
.level
== -4)
650 /* with multipath, the raid_disk from the superblock is meaningless */
653 i
= devices
[devcnt
].i
.disk
.raid_disk
;
655 if (nextspare
< content
->array
.raid_disks
)
656 nextspare
= content
->array
.raid_disks
;
659 if (i
>= content
->array
.raid_disks
&&
665 unsigned int newbestcnt
= i
+10;
666 int *newbest
= malloc(sizeof(int)*newbestcnt
);
668 for (c
=0; c
< newbestcnt
; c
++)
670 newbest
[c
] = best
[c
];
675 bestcnt
= newbestcnt
;
678 devices
[best
[i
]].i
.events
679 == devices
[devcnt
].i
.events
680 && (devices
[best
[i
]].i
.disk
.minor
681 != devices
[devcnt
].i
.disk
.minor
)
683 && content
->array
.level
!= LEVEL_MULTIPATH
) {
684 /* two different devices with identical superblock.
685 * Could be a mis-detection caused by overlapping
686 * partitions. fail-safe.
688 fprintf(stderr
, Name
": WARNING %s and %s appear"
689 " to have very similar superblocks.\n"
690 " If they are really different, "
691 "please --zero the superblock on one\n"
692 " If they are the same or overlap,"
693 " please remove one from %s.\n",
694 devices
[best
[i
]].devname
, devname
,
695 inargv
? "the list" :
696 "the\n DEVICE list in mdadm.conf"
702 || (devices
[best
[i
]].i
.events
703 < devices
[devcnt
].i
.events
))
710 fprintf(stderr
, Name
": no devices found for %s\n",
713 st
->ss
->free_super(st
);
718 if (update
&& strcmp(update
, "byteorder")==0)
719 st
->minor_version
= 90;
721 st
->ss
->getinfo_super(st
, content
);
722 clean
= content
->array
.state
& 1;
724 /* now we have some devices that might be suitable.
727 avail
= malloc(content
->array
.raid_disks
);
728 memset(avail
, 0, content
->array
.raid_disks
);
731 for (i
=0; i
< bestcnt
;i
++) {
733 int event_margin
= 1; /* always allow a difference of '1'
734 * like the kernel does
737 /* note: we ignore error flags in multipath arrays
738 * as they don't make sense
740 if (content
->array
.level
!= -4)
741 if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_SYNC
))) {
742 if (!(devices
[j
].i
.disk
.state
743 & (1<<MD_DISK_FAULTY
)))
747 if (devices
[j
].i
.events
+event_margin
>=
748 devices
[most_recent
].i
.events
) {
749 devices
[j
].uptodate
= 1;
750 if (i
< content
->array
.raid_disks
) {
757 while (force
&& !enough(content
->array
.level
, content
->array
.raid_disks
,
758 content
->array
.layout
, 1,
760 /* Choose the newest best drive which is
761 * not up-to-date, update the superblock
765 struct supertype
*tst
;
766 long long current_events
;
768 for (i
=0; i
<content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
771 !devices
[j
].uptodate
&&
774 > devices
[chosen_drive
].i
.events
))
777 if (chosen_drive
< 0)
779 current_events
= devices
[chosen_drive
].i
.events
;
782 fprintf(stderr
, Name
": forcing event count in %s(%d) from %d upto %d\n",
783 devices
[chosen_drive
].devname
,
784 devices
[chosen_drive
].i
.disk
.raid_disk
,
785 (int)(devices
[chosen_drive
].i
.events
),
786 (int)(devices
[most_recent
].i
.events
));
787 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
789 fprintf(stderr
, Name
": Couldn't open %s for write - not updating\n",
790 devices
[chosen_drive
].devname
);
791 devices
[chosen_drive
].i
.events
= 0;
795 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
797 fprintf(stderr
, Name
": RAID superblock disappeared from %s - not updating.\n",
798 devices
[chosen_drive
].devname
);
799 devices
[chosen_drive
].i
.events
= 0;
802 content
->events
= devices
[most_recent
].i
.events
;
803 tst
->ss
->update_super(tst
, content
, "force-one",
804 devices
[chosen_drive
].devname
, verbose
,
807 if (tst
->ss
->store_super(tst
, fd
)) {
809 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
810 devices
[chosen_drive
].devname
);
811 devices
[chosen_drive
].i
.events
= 0;
812 tst
->ss
->free_super(tst
);
816 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
817 devices
[chosen_drive
].uptodate
= 1;
818 avail
[chosen_drive
] = 1;
820 tst
->ss
->free_super(tst
);
822 /* If there are any other drives of the same vintage,
823 * add them in as well. We can't lose and we might gain
825 for (i
=0; i
<content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
828 !devices
[j
].uptodate
&&
829 devices
[j
].i
.events
== current_events
) {
836 /* Now we want to look at the superblock which the kernel will base things on
837 * and compare the devices that we think are working with the devices that the
838 * superblock thinks are working.
839 * If there are differences and --force is given, then update this chosen
843 st
->ss
->free_super(st
);
844 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
++) {
850 if (!devices
[j
].uptodate
)
853 if ((fd
=dev_open(devices
[j
].devname
, O_RDONLY
|O_EXCL
))< 0) {
854 fprintf(stderr
, Name
": Cannot open %s: %s\n",
855 devices
[j
].devname
, strerror(errno
));
859 if (st
->ss
->load_super(st
,fd
, NULL
)) {
861 fprintf(stderr
, Name
": RAID superblock has disappeared from %s\n",
868 if (st
->sb
== NULL
) {
869 fprintf(stderr
, Name
": No suitable drives found for %s\n", mddev
);
873 st
->ss
->getinfo_super(st
, content
);
875 sysfs_init(content
, mdfd
, 0);
877 for (i
=0; i
<bestcnt
; i
++) {
879 unsigned int desired_state
;
881 if (i
< content
->array
.raid_disks
)
882 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
888 if (!devices
[j
].uptodate
)
891 devices
[j
].i
.disk
.state
= desired_state
;
892 if (!(devices
[j
].i
.array
.state
& 1))
895 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
899 fprintf(stderr
, Name
": "
900 "clearing FAULTY flag for device %d in %s for %s\n",
901 j
, mddev
, devices
[j
].devname
);
905 fprintf(stderr
, Name
": "
906 "device %d in %s has wrong state in superblock, but %s seems ok\n",
907 i
, mddev
, devices
[j
].devname
);
911 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
912 fprintf(stderr
, Name
": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
917 if (force
&& !clean
&&
918 !enough(content
->array
.level
, content
->array
.raid_disks
,
919 content
->array
.layout
, clean
,
921 change
+= st
->ss
->update_super(st
, content
, "force-array",
922 devices
[chosen_drive
].devname
, verbose
,
929 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
931 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
932 devices
[chosen_drive
].devname
);
936 if (st
->ss
->store_super(st
, fd
)) {
938 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
939 devices
[chosen_drive
].devname
);
946 /* If we are in the middle of a reshape we may need to restore saved data
947 * that was moved aside due to the reshape overwriting live data
948 * The code of doing this lives in Grow.c
951 if (content
->reshape_active
) {
953 int *fdlist
= malloc(sizeof(int)* bestcnt
);
954 for (i
=0; i
<bestcnt
; i
++) {
957 fdlist
[i
] = dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
959 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
968 err
= Grow_restart(st
, content
, fdlist
, bestcnt
, backup_file
);
971 if (fdlist
[i
]>=0) close(fdlist
[i
]);
974 fprintf(stderr
, Name
": Failed to restore critical section for reshape, sorry.\n");
980 /* count number of in-sync devices according to the superblock.
981 * We must have this number to start the array without -s or -R
983 req_cnt
= content
->array
.working_disks
;
985 /* Almost ready to actually *do* something */
989 /* First, fill in the map, so that udev can find our name
990 * as soon as we become active.
992 map_update(NULL
, fd2devnum(mdfd
), content
->text_version
,
993 content
->uuid
, chosen_name
);
995 rv
= set_array_info(mdfd
, st
, content
);
997 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
998 mddev
, strerror(errno
));
1002 if (ident
->bitmap_fd
>= 0) {
1003 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
1004 fprintf(stderr
, Name
": SET_BITMAP_FILE failed.\n");
1008 } else if (ident
->bitmap_file
) {
1009 /* From config file */
1010 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
1012 fprintf(stderr
, Name
": Could not open bitmap file %s\n",
1013 ident
->bitmap_file
);
1017 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
1018 fprintf(stderr
, Name
": Failed to set bitmapfile for %s\n", mddev
);
1026 /* First, add the raid disks, but add the chosen one last */
1027 for (i
=0; i
<= bestcnt
; i
++) {
1031 if (j
== chosen_drive
)
1036 if (j
>= 0 /* && devices[j].uptodate */) {
1037 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
1040 fprintf(stderr
, Name
": failed to add "
1045 if (i
< content
->array
.raid_disks
1050 } else if (verbose
> 0)
1051 fprintf(stderr
, Name
": added %s "
1053 devices
[j
].devname
, mddev
,
1054 devices
[j
].i
.disk
.raid_disk
);
1055 } else if (verbose
> 0 && i
< content
->array
.raid_disks
)
1056 fprintf(stderr
, Name
": no uptodate device for "
1061 if (content
->array
.level
== LEVEL_CONTAINER
) {
1063 fprintf(stderr
, Name
": Container %s has been "
1064 "assembled with %d drive%s",
1065 mddev
, okcnt
+sparecnt
, okcnt
+sparecnt
==1?"":"s");
1066 if (okcnt
< content
->array
.raid_disks
)
1067 fprintf(stderr
, " (out of %d)",
1068 content
->array
.raid_disks
);
1069 fprintf(stderr
, "\n");
1071 sysfs_uevent(content
, "change");
1072 wait_for(chosen_name
);
1079 ( enough(content
->array
.level
, content
->array
.raid_disks
,
1080 content
->array
.layout
, clean
, avail
, okcnt
) &&
1081 (okcnt
>= req_cnt
|| start_partial_ok
)
1083 if (ioctl(mdfd
, RUN_ARRAY
, NULL
)==0) {
1085 fprintf(stderr
, Name
": %s has been started with %d drive%s",
1086 mddev
, okcnt
, okcnt
==1?"":"s");
1087 if (okcnt
< content
->array
.raid_disks
)
1088 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1090 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1091 fprintf(stderr
, ".\n");
1093 if (content
->reshape_active
&&
1094 content
->array
.level
>= 4 &&
1095 content
->array
.level
<= 6) {
1096 /* might need to increase the size
1097 * of the stripe cache - default is 256
1099 if (256 < 4 * (content
->array
.chunk_size
/4096)) {
1100 struct mdinfo
*sra
= sysfs_read(mdfd
, 0, 0);
1102 sysfs_set_num(sra
, NULL
,
1103 "stripe_cache_size",
1104 (4 * content
->array
.chunk_size
/ 4096) + 1);
1111 /* There is a nasty race with 'mdadm --monitor'.
1112 * If it opens this device before we close it,
1113 * it gets an incomplete open on which IO
1114 * doesn't work and the capacity is
1116 * If we reopen (to check for layered devices)
1117 * before --monitor closes, we loose.
1119 * So: wait upto 1 second for there to be
1120 * a non-zero capacity.
1122 while (usecs
< 1000) {
1123 mdfd
= open(mddev
, O_RDONLY
);
1125 unsigned long long size
;
1126 if (get_dev_size(mdfd
, NULL
, &size
) &&
1137 fprintf(stderr
, Name
": failed to RUN_ARRAY %s: %s\n",
1138 mddev
, strerror(errno
));
1140 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1141 content
->array
.layout
, 1, avail
, okcnt
))
1142 fprintf(stderr
, Name
": Not enough devices to "
1143 "start the array.\n");
1144 else if (!enough(content
->array
.level
,
1145 content
->array
.raid_disks
,
1146 content
->array
.layout
, clean
,
1148 fprintf(stderr
, Name
": Not enough devices to "
1149 "start the array while not clean "
1150 "- consider --force.\n");
1153 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1157 if (runstop
== -1) {
1158 fprintf(stderr
, Name
": %s assembled from %d drive%s",
1159 mddev
, okcnt
, okcnt
==1?"":"s");
1160 if (okcnt
!= content
->array
.raid_disks
)
1161 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1162 fprintf(stderr
, ", but not started.\n");
1166 if (verbose
>= -1) {
1167 fprintf(stderr
, Name
": %s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
1169 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1170 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1171 content
->array
.layout
, 1, avail
, okcnt
))
1172 fprintf(stderr
, " - not enough to start the array.\n");
1173 else if (!enough(content
->array
.level
,
1174 content
->array
.raid_disks
,
1175 content
->array
.layout
, clean
,
1177 fprintf(stderr
, " - not enough to start the "
1178 "array while not clean - consider "
1181 if (req_cnt
== content
->array
.raid_disks
)
1182 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1184 fprintf(stderr
, " - need %d of %d to start", req_cnt
, content
->array
.raid_disks
);
1185 fprintf(stderr
, " (use --run to insist).\n");
1189 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1193 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1194 * been updated to point to the current locations of devices.
1195 * so we can just start the array
1198 dev
= makedev(devices
[chosen_drive
].i
.disk
.major
,
1199 devices
[chosen_drive
].i
.disk
.minor
);
1200 if (ioctl(mdfd
, START_ARRAY
, dev
)) {
1201 fprintf(stderr
, Name
": Cannot start array: %s\n",
1211 int assemble_container_content(struct supertype
*st
, int mdfd
,
1212 struct mdinfo
*content
, int runstop
,
1213 char *chosen_name
, int verbose
)
1215 struct mdinfo
*dev
, *sra
;
1216 int working
= 0, preexist
= 0;
1217 struct map_ent
*map
= NULL
;
1219 sysfs_init(content
, mdfd
, 0);
1221 sra
= sysfs_read(mdfd
, 0, GET_VERSION
);
1222 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0)
1223 if (sysfs_set_array(content
, md_get_version(mdfd
)) != 0) {
1230 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
1231 if (sysfs_add_disk(content
, dev
) == 0)
1233 else if (errno
== EEXIST
)
1237 return 1;/* Nothing new, don't try to start */
1238 } else if (runstop
> 0 ||
1239 (working
+ preexist
) >= content
->array
.working_disks
) {
1241 map_update(&map
, fd2devnum(mdfd
),
1242 content
->text_version
,
1243 content
->uuid
, chosen_name
);
1244 switch(content
->array
.level
) {
1246 case LEVEL_MULTIPATH
:
1248 sysfs_set_str(content
, NULL
, "array_state",
1252 sysfs_set_str(content
, NULL
, "array_state",
1254 /* start mdmon if needed. */
1255 if (!mdmon_running(st
->container_dev
))
1256 start_mdmon(st
->container_dev
);
1257 ping_monitor(devnum2devname(st
->container_dev
));
1260 sysfs_set_safemode(content
, content
->safe_mode_delay
);
1262 fprintf(stderr
, Name
1263 ": Started %s with %d devices",
1264 chosen_name
, working
+ preexist
);
1266 fprintf(stderr
, " (%d new)", working
);
1267 fprintf(stderr
, "\n");
1269 wait_for(chosen_name
);
1272 /* FIXME should have an O_EXCL and wait for read-auto */
1275 fprintf(stderr
, Name
1276 ": %s assembled with %d devices but "
1278 chosen_name
, working
);