]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Assemble.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
28 static int name_matches(char *found
, char *required
, char *homehost
)
30 /* See if the name found matches the required name, possibly
31 * prefixed with 'homehost'
35 strncpy(fnd
, found
, 32);
37 if (strcmp(found
, required
)==0)
40 int l
= strlen(homehost
);
41 if (l
< 32 && fnd
[l
] == ':' &&
42 strcmp(fnd
+l
+1, required
)==0)
48 static int is_member_busy(char *metadata_version
)
50 /* check if the given member array is active */
51 struct mdstat_ent
*mdstat
= mdstat_read(1, 0);
52 struct mdstat_ent
*ent
;
55 for (ent
= mdstat
; ent
; ent
= ent
->next
) {
56 if (ent
->metadata_version
== NULL
)
58 if (strncmp(ent
->metadata_version
, "external:", 9) != 0)
60 if (!is_subarray(&ent
->metadata_version
[9]))
62 /* Skip first char - it can be '/' or '-' */
63 if (strcmp(&ent
->metadata_version
[10], metadata_version
+1) == 0) {
73 int Assemble(struct supertype
*st
, char *mddev
,
75 mddev_dev_t devlist
, char *backup_file
,
76 int readonly
, int runstop
,
77 char *update
, char *homehost
, int require_homehost
,
78 int verbose
, int force
)
81 * The task of Assemble is to find a collection of
82 * devices that should (according to their superblocks)
83 * form an array, and to give this collection to the MD driver.
84 * In Linux-2.4 and later, this involves submitting a
85 * SET_ARRAY_INFO ioctl with no arg - to prepare
86 * the array - and then submit a number of
87 * ADD_NEW_DISK ioctls to add disks into
88 * the array. Finally RUN_ARRAY might
89 * be submitted to start the array.
91 * Much of the work of Assemble is in finding and/or
92 * checking the disks to make sure they look right.
94 * If mddev is not set, then scan must be set and we
95 * read through the config file for dev+uuid mapping
96 * We recurse, setting mddev, for each device that
98 * - has a valid uuid (or any uuid if !uuidset)
100 * If mddev is set, we try to determine state of md.
101 * check version - must be at least 0.90.0
102 * check kernel version. must be at least 2.4.
103 * If not, we can possibly fall back on START_ARRAY
104 * Try to GET_ARRAY_INFO.
105 * If possible, give up
106 * If not, try to STOP_ARRAY just to make sure
108 * If !uuidset and scan, look in conf-file for uuid
109 * If not found, give up
110 * If !devlist and scan and uuidset, get list of devs from conf-file
113 * Check superblock - discard if bad
114 * Check uuid (set if we don't have one) - discard if no match
115 * Check superblock similarity if we have a superblock - discard if different
116 * Record events, devicenum
117 * This should give us a list of devices for the array
118 * We should collect the most recent event number
120 * Count disks with recent enough event count
121 * While force && !enough disks
122 * Choose newest rejected disks, update event count
123 * mark clean and rewrite superblock
126 * foreach device with recent events : ADD_NEW_DISK
127 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
129 * Check the device numbers in superblock are right
130 * update superblock if any changes
136 int auto_assem
= (mddev
== NULL
&& !ident
->uuid_set
&&
137 ident
->super_minor
== UnSet
&& ident
->name
[0] == 0
138 && (ident
->container
== NULL
|| ident
->member
== NULL
));
140 int vers
= vers
; /* Keep gcc quite - it really is initialised */
143 int uptodate
; /* set once we decide that this device is as
144 * recent as everything else in the array.
148 int *best
= NULL
; /* indexed by raid_disk */
151 unsigned int okcnt
, sparecnt
, rebuilding_cnt
;
152 unsigned int req_cnt
;
158 int report_missmatch
;
160 int start_partial_ok
= (runstop
>= 0) &&
161 (force
|| devlist
==NULL
|| auto_assem
);
162 unsigned int num_devs
;
165 struct mdinfo
*content
= NULL
;
170 char chosen_name
[1024];
172 if (get_linux_version() < 2004000)
176 * If any subdevs are listed, then any that don't
177 * match ident are discarded. Remainder must all match and
179 * If no subdevs, then we scan all devices in the config file, but
180 * there must be something in the identity
184 ident
->uuid_set
== 0 &&
185 (ident
->super_minor
< 0 || ident
->super_minor
== UnSet
) &&
186 ident
->name
[0] == 0 &&
187 (ident
->container
== NULL
|| ident
->member
== NULL
) &&
188 ident
->devices
== NULL
) {
189 fprintf(stderr
, Name
": No identity information available for %s - cannot assemble.\n",
190 mddev
? mddev
: "further assembly");
195 devlist
= conf_get_devs();
199 report_missmatch
= ((inargv
&& verbose
>= 0) || verbose
> 0);
201 /* We come back here when doing auto-assembly and attempting some
202 * set of devices failed. Those are now marked as ->used==2 and
203 * we ignore them and try again
206 tmpdev
= devlist
; num_devs
= 0;
212 tmpdev
= tmpdev
->next
;
214 devices
= malloc(num_devs
* sizeof(*devices
));
216 if (!st
&& ident
->st
) st
= ident
->st
;
219 fprintf(stderr
, Name
": looking for devices for %s\n",
220 mddev
? mddev
: "further assembly");
222 /* first walk the list of devices to find a consistent set
223 * that match the criterea, if that is possible.
224 * We flag the ones we like with 'used'.
226 for (tmpdev
= devlist
;
228 tmpdev
= tmpdev
->next
) {
229 char *devname
= tmpdev
->devname
;
232 struct supertype
*tst
= dup_super(st
);
234 if (tmpdev
->used
> 1) continue;
236 if (ident
->devices
&&
237 !match_oneof(ident
->devices
, devname
)) {
238 if (report_missmatch
)
239 fprintf(stderr
, Name
": %s is not one of %s\n", devname
, ident
->devices
);
243 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
245 if (report_missmatch
)
246 fprintf(stderr
, Name
": cannot open device %s: %s\n",
247 devname
, strerror(errno
));
249 } else if (fstat(dfd
, &stb
)< 0) {
251 fprintf(stderr
, Name
": fstat failed for %s: %s\n",
252 devname
, strerror(errno
));
254 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
255 fprintf(stderr
, Name
": %s is not a block device.\n",
258 } else if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
259 if (report_missmatch
)
260 fprintf(stderr
, Name
": no recogniseable superblock on %s\n",
263 } else if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
264 if (report_missmatch
)
265 fprintf(stderr
, Name
": no RAID superblock on %s\n",
267 } else if (tst
->ss
->compare_super
== NULL
) {
268 if (report_missmatch
)
269 fprintf(stderr
, Name
": Cannot assemble %s metadata on %s\n",
270 tst
->ss
->name
, devname
);
271 tst
->ss
->free_super(tst
);
273 } else if (auto_assem
&& st
== NULL
&&
274 !conf_test_metadata(tst
->ss
->name
,
275 tst
->ss
->match_home(tst
, homehost
) == 1)) {
276 if (report_missmatch
)
277 fprintf(stderr
, Name
": %s has metadata type %s for which "
278 "auto-assembly is disabled\n",
279 devname
, tst
->ss
->name
);
280 tst
->ss
->free_super(tst
);
284 memset(content
, 0, sizeof(*content
));
285 tst
->ss
->getinfo_super(tst
, content
);
287 if (dfd
>= 0) close(dfd
);
289 if (tst
&& tst
->sb
&& tst
->ss
->container_content
290 && tst
->loaded_container
) {
291 /* tmpdev is a container. We need to be either
292 * looking for a member, or auto-assembling
295 /* already found some components, this cannot
298 if (report_missmatch
)
299 fprintf(stderr
, Name
": %s is a container, but we are looking for components\n",
304 if (ident
->container
) {
305 if (ident
->container
[0] == '/' &&
306 !same_dev(ident
->container
, devname
)) {
307 if (report_missmatch
)
308 fprintf(stderr
, Name
": %s is not the container required (%s)\n",
309 devname
, ident
->container
);
312 if (ident
->container
[0] != '/') {
315 if (!parse_uuid(ident
->container
, uuid
) ||
316 !same_uuid(content
->uuid
, uuid
, tst
->ss
->swapuuid
)) {
317 if (report_missmatch
)
318 fprintf(stderr
, Name
": %s has wrong UUID to be required container\n",
324 /* It is worth looking inside this container.
327 fprintf(stderr
, Name
": looking in container %s\n",
331 content
= tmpdev
->content
;
333 content
= tst
->ss
->container_content(tst
);
335 goto loop
; /* empty container */
337 tmpdev
->content
= content
->next
;
338 if (tmpdev
->content
== NULL
)
341 } else if (ident
->container
|| ident
->member
) {
342 /* No chance of this matching if we don't have
344 if (report_missmatch
)
345 fprintf(stderr
, Name
"%s is not a container, and one is required.\n",
350 if (ident
->uuid_set
&& (!update
|| strcmp(update
, "uuid")!= 0) &&
352 same_uuid(content
->uuid
, ident
->uuid
, tst
->ss
->swapuuid
)==0)) {
353 if (report_missmatch
)
354 fprintf(stderr
, Name
": %s has wrong uuid.\n",
358 if (ident
->name
[0] && (!update
|| strcmp(update
, "name")!= 0) &&
360 name_matches(content
->name
, ident
->name
, homehost
)==0)) {
361 if (report_missmatch
)
362 fprintf(stderr
, Name
": %s has wrong name.\n",
366 if (ident
->super_minor
!= UnSet
&&
368 ident
->super_minor
!= content
->array
.md_minor
)) {
369 if (report_missmatch
)
370 fprintf(stderr
, Name
": %s has wrong super-minor.\n",
374 if (ident
->level
!= UnSet
&&
376 ident
->level
!= content
->array
.level
)) {
377 if (report_missmatch
)
378 fprintf(stderr
, Name
": %s has wrong raid level.\n",
382 if (ident
->raid_disks
!= UnSet
&&
384 ident
->raid_disks
!= content
->array
.raid_disks
)) {
385 if (report_missmatch
)
386 fprintf(stderr
, Name
": %s requires wrong number of drives.\n",
391 if (tst
== NULL
|| tst
->sb
== NULL
)
394 /* If we are this far, then we are nearly commited to this device.
395 * If the super_block doesn't exist, or doesn't match others,
396 * then we probably cannot continue
397 * However if one of the arrays is for the homehost, and
398 * the other isn't that can disambiguate.
401 if (!tst
|| !tst
->sb
) {
402 fprintf(stderr
, Name
": %s has no superblock - assembly aborted\n",
405 st
->ss
->free_super(st
);
409 if (tst
&& tst
->sb
&& tst
->ss
->container_content
410 && tst
->loaded_container
) {
411 /* we have the one container we need, don't keep
412 * looking. If the chosen member is active, skip.
414 if (is_member_busy(content
->text_version
)) {
415 if (report_missmatch
)
416 fprintf(stderr
, Name
": member %s in %s is already assembled\n",
417 content
->text_version
,
422 tst
->ss
->free_super(tst
);
429 if (ident
->member
&& ident
->member
[0]) {
430 char *s
= strchr(content
->text_version
+1, '/');
432 fprintf(stderr
, Name
": badly formatted version: %s\n",
433 content
->text_version
);
436 if (strcmp(ident
->member
, s
+1) != 0) {
437 if (report_missmatch
)
439 Name
": skipping wrong member %s\n",
440 content
->text_version
);
444 st
= tst
; tst
= NULL
;
445 if (!auto_assem
&& inargv
&& tmpdev
->next
!= NULL
) {
446 fprintf(stderr
, Name
": %s is a container, but is not "
447 "only device given: confused and aborting\n",
449 st
->ss
->free_super(st
);
453 fprintf(stderr
, Name
": found match on member %s in %s\n",
454 content
->text_version
, devname
);
459 if (st
->minor_version
== -1)
460 st
->minor_version
= tst
->minor_version
;
461 if (st
->ss
!= tst
->ss
||
462 st
->minor_version
!= tst
->minor_version
||
463 st
->ss
->compare_super(st
, tst
) != 0) {
464 /* Some mismatch. If exactly one array matches this host,
465 * we can resolve on that one.
466 * Or, if we are auto assembling, we just ignore the second
472 int first
= st
->ss
->match_home(st
, homehost
);
473 int last
= tst
->ss
->match_home(tst
, homehost
);
475 (first
== 1 || last
== 1)) {
476 /* We can do something */
477 if (first
) {/* just ignore this one */
478 if (report_missmatch
)
479 fprintf(stderr
, Name
": %s misses out due to wrong homehost\n",
482 } else { /* reject all those sofar */
484 if (report_missmatch
)
485 fprintf(stderr
, Name
": %s overrides previous devices due to good homehost\n",
487 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
495 fprintf(stderr
, Name
": superblock on %s doesn't match others - assembly aborted\n",
497 tst
->ss
->free_super(tst
);
498 st
->ss
->free_super(st
);
508 tst
->ss
->free_super(tst
);
511 if (!st
|| !st
->sb
|| !content
)
514 /* Now need to open the array device. Use create_mddev */
515 if (content
== &info
)
516 st
->ss
->getinfo_super(st
, content
);
518 trustworthy
= FOREIGN
;
519 name
= content
->name
;
520 switch (st
->ss
->match_home(st
, homehost
)
521 ?: st
->ss
->match_home(st
, "any")) {
524 name
= strchr(content
->name
, ':');
528 name
= content
->name
;
532 /* If the array is listed in mdadm.conf or on
533 * command line, then we trust the name
534 * even if the array doesn't look local
539 content
->array
.level
== LEVEL_CONTAINER
) {
540 name
= content
->text_version
;
541 trustworthy
= METADATA
;
544 if (name
[0] && trustworthy
!= LOCAL
&&
545 ! require_homehost
&&
546 conf_name_is_free(name
))
549 if (trustworthy
== LOCAL
&&
551 /* Ignore 'host:' prefix of name */
552 name
= strchr(name
, ':')+1;
554 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
557 st
->ss
->free_super(st
);
564 vers
= md_get_version(mdfd
);
566 fprintf(stderr
, Name
": Assemble requires driver version 0.90.0 or later.\n"
567 " Upgrade your kernel or try --build\n");
571 if (mddev_busy(fd2devnum(mdfd
))) {
572 fprintf(stderr
, Name
": %s already active, cannot restart it!\n",
574 for (tmpdev
= devlist
;
575 tmpdev
&& tmpdev
->used
!= 1;
576 tmpdev
= tmpdev
->next
)
578 if (tmpdev
&& auto_assem
)
579 fprintf(stderr
, Name
": %s needed for %s...\n",
580 mddev
, tmpdev
->devname
);
583 st
->ss
->free_super(st
);
589 ioctl(mdfd
, STOP_ARRAY
, NULL
); /* just incase it was started but has no content */
592 if (content
!= &info
) {
593 /* This is a member of a container. Try starting the array. */
594 return assemble_container_content(st
, mdfd
, content
, runstop
,
595 chosen_name
, verbose
);
598 /* Ok, no bad inconsistancy, we can try updating etc */
600 content
->update_private
= NULL
;
601 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) if (tmpdev
->used
== 1) {
602 char *devname
= tmpdev
->devname
;
604 /* looks like a good enough match to update the super block if needed */
608 /* prepare useful information in info structures */
610 struct supertype
*tst
;
613 if (strcmp(update
, "uuid")==0 &&
616 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
617 read(rfd
, ident
->uuid
, 16) != 16) {
618 *(__u32
*)(ident
->uuid
) = random();
619 *(__u32
*)(ident
->uuid
+1) = random();
620 *(__u32
*)(ident
->uuid
+2) = random();
621 *(__u32
*)(ident
->uuid
+3) = random();
623 if (rfd
>= 0) close(rfd
);
625 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
627 remove_partitions(dfd
);
630 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
631 fprintf(stderr
, Name
": cannot re-read metadata from %s - aborting\n",
638 tst
->ss
->getinfo_super(tst
, content
);
640 memcpy(content
->uuid
, ident
->uuid
, 16);
641 strcpy(content
->name
, ident
->name
);
642 content
->array
.md_minor
= minor(stb2
.st_rdev
);
644 tst
->ss
->update_super(tst
, content
, update
,
646 ident
->uuid_set
, homehost
);
647 if (strcmp(update
, "uuid")==0 &&
650 memcpy(ident
->uuid
, content
->uuid
, 16);
653 fprintf(stderr
, Name
": Cannot open %s for superblock update\n",
655 else if (tst
->ss
->store_super(tst
, dfd
))
656 fprintf(stderr
, Name
": Could not re-write superblock on %s.\n",
661 if (strcmp(update
, "uuid")==0 &&
662 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
663 if (bitmap_update_uuid(ident
->bitmap_fd
,
665 tst
->ss
->swapuuid
) != 0)
666 fprintf(stderr
, Name
": Could not update uuid on external bitmap.\n");
670 tst
->ss
->free_super(tst
);
674 struct supertype
*tst
= dup_super(st
);
676 dfd
= dev_open(devname
, O_RDWR
|O_EXCL
);
678 remove_partitions(dfd
);
680 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
681 fprintf(stderr
, Name
": cannot re-read metadata from %s - aborting\n",
688 tst
->ss
->getinfo_super(tst
, content
);
689 tst
->ss
->free_super(tst
);
696 fprintf(stderr
, Name
": %s is identified as a member of %s, slot %d.\n",
697 devname
, mddev
, content
->disk
.raid_disk
);
698 devices
[devcnt
].devname
= devname
;
699 devices
[devcnt
].uptodate
= 0;
700 devices
[devcnt
].i
= *content
;
701 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
702 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
703 if (most_recent
< devcnt
) {
704 if (devices
[devcnt
].i
.events
705 > devices
[most_recent
].i
.events
)
706 most_recent
= devcnt
;
708 if (content
->array
.level
== LEVEL_MULTIPATH
)
709 /* with multipath, the raid_disk from the superblock is meaningless */
712 i
= devices
[devcnt
].i
.disk
.raid_disk
;
714 if (nextspare
< content
->array
.raid_disks
)
715 nextspare
= content
->array
.raid_disks
;
718 if (i
>= content
->array
.raid_disks
&&
724 int newbestcnt
= i
+10;
725 int *newbest
= malloc(sizeof(int)*newbestcnt
);
727 for (c
=0; c
< newbestcnt
; c
++)
729 newbest
[c
] = best
[c
];
734 bestcnt
= newbestcnt
;
737 devices
[best
[i
]].i
.events
738 == devices
[devcnt
].i
.events
739 && (devices
[best
[i
]].i
.disk
.minor
740 != devices
[devcnt
].i
.disk
.minor
)
742 && content
->array
.level
!= LEVEL_MULTIPATH
) {
743 /* two different devices with identical superblock.
744 * Could be a mis-detection caused by overlapping
745 * partitions. fail-safe.
747 fprintf(stderr
, Name
": WARNING %s and %s appear"
748 " to have very similar superblocks.\n"
749 " If they are really different, "
750 "please --zero the superblock on one\n"
751 " If they are the same or overlap,"
752 " please remove one from %s.\n",
753 devices
[best
[i
]].devname
, devname
,
754 inargv
? "the list" :
755 "the\n DEVICE list in mdadm.conf"
761 || (devices
[best
[i
]].i
.events
762 < devices
[devcnt
].i
.events
))
767 free(content
->update_private
);
768 content
->update_private
= NULL
;
771 fprintf(stderr
, Name
": no devices found for %s\n",
774 st
->ss
->free_super(st
);
779 if (update
&& strcmp(update
, "byteorder")==0)
780 st
->minor_version
= 90;
782 st
->ss
->getinfo_super(st
, content
);
783 clean
= content
->array
.state
& 1;
785 /* now we have some devices that might be suitable.
788 avail
= malloc(content
->array
.raid_disks
);
789 memset(avail
, 0, content
->array
.raid_disks
);
793 for (i
=0; i
< bestcnt
; i
++) {
795 int event_margin
= 1; /* always allow a difference of '1'
796 * like the kernel does
799 /* note: we ignore error flags in multipath arrays
800 * as they don't make sense
802 if (content
->array
.level
!= LEVEL_MULTIPATH
)
803 if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_ACTIVE
))) {
804 if (!(devices
[j
].i
.disk
.state
805 & (1<<MD_DISK_FAULTY
)))
809 if (devices
[j
].i
.events
+event_margin
>=
810 devices
[most_recent
].i
.events
) {
811 devices
[j
].uptodate
= 1;
812 if (i
< content
->array
.raid_disks
) {
813 if (devices
[j
].i
.recovery_start
== MaxSector
) {
822 while (force
&& !enough(content
->array
.level
, content
->array
.raid_disks
,
823 content
->array
.layout
, 1,
825 /* Choose the newest best drive which is
826 * not up-to-date, update the superblock
830 struct supertype
*tst
;
831 unsigned long long current_events
;
833 for (i
= 0; i
< content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
836 !devices
[j
].uptodate
&&
837 devices
[j
].i
.recovery_start
== MaxSector
&&
840 > devices
[chosen_drive
].i
.events
))
843 if (chosen_drive
< 0)
845 current_events
= devices
[chosen_drive
].i
.events
;
848 fprintf(stderr
, Name
": forcing event count in %s(%d) from %d upto %d\n",
849 devices
[chosen_drive
].devname
,
850 devices
[chosen_drive
].i
.disk
.raid_disk
,
851 (int)(devices
[chosen_drive
].i
.events
),
852 (int)(devices
[most_recent
].i
.events
));
853 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
855 fprintf(stderr
, Name
": Couldn't open %s for write - not updating\n",
856 devices
[chosen_drive
].devname
);
857 devices
[chosen_drive
].i
.events
= 0;
861 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
863 fprintf(stderr
, Name
": RAID superblock disappeared from %s - not updating.\n",
864 devices
[chosen_drive
].devname
);
865 devices
[chosen_drive
].i
.events
= 0;
868 content
->events
= devices
[most_recent
].i
.events
;
869 tst
->ss
->update_super(tst
, content
, "force-one",
870 devices
[chosen_drive
].devname
, verbose
,
873 if (tst
->ss
->store_super(tst
, fd
)) {
875 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
876 devices
[chosen_drive
].devname
);
877 devices
[chosen_drive
].i
.events
= 0;
878 tst
->ss
->free_super(tst
);
882 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
883 devices
[chosen_drive
].uptodate
= 1;
884 avail
[chosen_drive
] = 1;
886 tst
->ss
->free_super(tst
);
888 /* If there are any other drives of the same vintage,
889 * add them in as well. We can't lose and we might gain
891 for (i
= 0; i
< content
->array
.raid_disks
&& i
< bestcnt
; i
++) {
894 !devices
[j
].uptodate
&&
895 devices
[j
].i
.events
== current_events
) {
902 /* Now we want to look at the superblock which the kernel will base things on
903 * and compare the devices that we think are working with the devices that the
904 * superblock thinks are working.
905 * If there are differences and --force is given, then update this chosen
909 st
->ss
->free_super(st
);
910 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
++) {
916 if (!devices
[j
].uptodate
)
919 if ((fd
=dev_open(devices
[j
].devname
, O_RDONLY
|O_EXCL
))< 0) {
920 fprintf(stderr
, Name
": Cannot open %s: %s\n",
921 devices
[j
].devname
, strerror(errno
));
925 if (st
->ss
->load_super(st
,fd
, NULL
)) {
927 fprintf(stderr
, Name
": RAID superblock has disappeared from %s\n",
934 if (st
->sb
== NULL
) {
935 fprintf(stderr
, Name
": No suitable drives found for %s\n", mddev
);
939 st
->ss
->getinfo_super(st
, content
);
941 sysfs_init(content
, mdfd
, 0);
943 for (i
=0; i
<bestcnt
; i
++) {
945 unsigned int desired_state
;
947 if (i
< content
->array
.raid_disks
)
948 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
954 if (!devices
[j
].uptodate
)
957 devices
[j
].i
.disk
.state
= desired_state
;
958 if (!(devices
[j
].i
.array
.state
& 1))
961 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
965 fprintf(stderr
, Name
": "
966 "clearing FAULTY flag for device %d in %s for %s\n",
967 j
, mddev
, devices
[j
].devname
);
971 fprintf(stderr
, Name
": "
972 "device %d in %s has wrong state in superblock, but %s seems ok\n",
973 i
, mddev
, devices
[j
].devname
);
977 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
978 fprintf(stderr
, Name
": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
983 if (force
&& !clean
&&
984 !enough(content
->array
.level
, content
->array
.raid_disks
,
985 content
->array
.layout
, clean
,
987 change
+= st
->ss
->update_super(st
, content
, "force-array",
988 devices
[chosen_drive
].devname
, verbose
,
995 fd
= dev_open(devices
[chosen_drive
].devname
, O_RDWR
|O_EXCL
);
997 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
998 devices
[chosen_drive
].devname
);
1002 if (st
->ss
->store_super(st
, fd
)) {
1004 fprintf(stderr
, Name
": Could not re-write superblock on %s\n",
1005 devices
[chosen_drive
].devname
);
1012 /* If we are in the middle of a reshape we may need to restore saved data
1013 * that was moved aside due to the reshape overwriting live data
1014 * The code of doing this lives in Grow.c
1017 if (content
->reshape_active
) {
1019 int *fdlist
= malloc(sizeof(int)* bestcnt
);
1021 fprintf(stderr
, Name
":%s has an active reshape - checking "
1022 "if critical section needs to be restored\n",
1024 for (i
=0; i
<bestcnt
; i
++) {
1027 fdlist
[i
] = dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
1028 if (fdlist
[i
] < 0) {
1029 fprintf(stderr
, Name
": Could not open %s for write - cannot Assemble array.\n",
1030 devices
[j
].devname
);
1038 err
= Grow_restart(st
, content
, fdlist
, bestcnt
, backup_file
, verbose
> 0);
1041 if (fdlist
[i
]>=0) close(fdlist
[i
]);
1044 fprintf(stderr
, Name
": Failed to restore critical section for reshape, sorry.\n");
1045 if (backup_file
== NULL
)
1046 fprintf(stderr
," Possibly you needed to specify the --backup-file\n");
1052 /* count number of in-sync devices according to the superblock.
1053 * We must have this number to start the array without -s or -R
1055 req_cnt
= content
->array
.working_disks
;
1057 /* Almost ready to actually *do* something */
1061 /* First, fill in the map, so that udev can find our name
1062 * as soon as we become active.
1064 map_update(NULL
, fd2devnum(mdfd
), content
->text_version
,
1065 content
->uuid
, chosen_name
);
1067 rv
= set_array_info(mdfd
, st
, content
);
1069 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
1070 mddev
, strerror(errno
));
1071 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1075 if (ident
->bitmap_fd
>= 0) {
1076 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
1077 fprintf(stderr
, Name
": SET_BITMAP_FILE failed.\n");
1078 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1082 } else if (ident
->bitmap_file
) {
1083 /* From config file */
1084 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
1086 fprintf(stderr
, Name
": Could not open bitmap file %s\n",
1087 ident
->bitmap_file
);
1088 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1092 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
1093 fprintf(stderr
, Name
": Failed to set bitmapfile for %s\n", mddev
);
1095 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1102 /* First, add the raid disks, but add the chosen one last */
1103 for (i
=0; i
<= bestcnt
; i
++) {
1107 if (j
== chosen_drive
)
1112 if (j
>= 0 /* && devices[j].uptodate */) {
1113 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
1116 fprintf(stderr
, Name
": failed to add "
1121 if (i
< content
->array
.raid_disks
1126 } else if (verbose
> 0)
1127 fprintf(stderr
, Name
": added %s "
1129 devices
[j
].devname
, mddev
,
1130 devices
[j
].i
.disk
.raid_disk
);
1131 } else if (verbose
> 0 && i
< content
->array
.raid_disks
)
1132 fprintf(stderr
, Name
": no uptodate device for "
1137 if (content
->array
.level
== LEVEL_CONTAINER
) {
1139 fprintf(stderr
, Name
": Container %s has been "
1140 "assembled with %d drive%s",
1141 mddev
, okcnt
+sparecnt
, okcnt
+sparecnt
==1?"":"s");
1142 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1143 fprintf(stderr
, " (out of %d)",
1144 content
->array
.raid_disks
);
1145 fprintf(stderr
, "\n");
1147 sysfs_uevent(content
, "change");
1148 wait_for(chosen_name
, mdfd
);
1155 ( enough(content
->array
.level
, content
->array
.raid_disks
,
1156 content
->array
.layout
, clean
, avail
, okcnt
) &&
1157 (okcnt
+ rebuilding_cnt
>= req_cnt
|| start_partial_ok
)
1159 /* This array is good-to-go.
1160 * If a reshape is in progress then we might need to
1161 * continue monitoring it. In that case we start
1162 * it read-only and let the grow code make it writable.
1166 if (content
->reshape_active
&&
1167 content
->delta_disks
<= 0)
1168 rv
= Grow_continue(mdfd
, st
, content
, backup_file
);
1171 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1174 fprintf(stderr
, Name
": %s has been started with %d drive%s",
1175 mddev
, okcnt
, okcnt
==1?"":"s");
1176 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1177 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1179 fprintf(stderr
, "%s %d rebuilding", sparecnt
?",":" and", rebuilding_cnt
);
1181 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1182 fprintf(stderr
, ".\n");
1184 if (content
->reshape_active
&&
1185 content
->array
.level
>= 4 &&
1186 content
->array
.level
<= 6) {
1187 /* might need to increase the size
1188 * of the stripe cache - default is 256
1190 if (256 < 4 * (content
->array
.chunk_size
/4096)) {
1191 struct mdinfo
*sra
= sysfs_read(mdfd
, 0, 0);
1193 sysfs_set_num(sra
, NULL
,
1194 "stripe_cache_size",
1195 (4 * content
->array
.chunk_size
/ 4096) + 1);
1198 if (okcnt
< (unsigned)content
->array
.raid_disks
) {
1199 /* If any devices did not get added
1200 * because the kernel rejected them based
1201 * on event count, try adding them
1202 * again providing the action policy is
1203 * 're-add' or greater. The bitmap
1204 * might allow them to be included, or
1205 * they will become spares.
1207 for (i
= 0; i
<= bestcnt
; i
++) {
1209 if (j
>= 0 && !devices
[j
].uptodate
) {
1210 if (!disk_action_allows(&devices
[j
].i
, st
->ss
->name
, act_re_add
))
1212 rv
= add_disk(mdfd
, st
, content
,
1214 if (rv
== 0 && verbose
>= 0)
1216 Name
": %s has been re-added.\n",
1217 devices
[j
].devname
);
1221 wait_for(mddev
, mdfd
);
1225 /* There is a nasty race with 'mdadm --monitor'.
1226 * If it opens this device before we close it,
1227 * it gets an incomplete open on which IO
1228 * doesn't work and the capacity is
1230 * If we reopen (to check for layered devices)
1231 * before --monitor closes, we loose.
1233 * So: wait upto 1 second for there to be
1234 * a non-zero capacity.
1236 while (usecs
< 1000) {
1237 mdfd
= open(mddev
, O_RDONLY
);
1239 unsigned long long size
;
1240 if (get_dev_size(mdfd
, NULL
, &size
) &&
1251 fprintf(stderr
, Name
": failed to RUN_ARRAY %s: %s\n",
1252 mddev
, strerror(errno
));
1254 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1255 content
->array
.layout
, 1, avail
, okcnt
))
1256 fprintf(stderr
, Name
": Not enough devices to "
1257 "start the array.\n");
1258 else if (!enough(content
->array
.level
,
1259 content
->array
.raid_disks
,
1260 content
->array
.layout
, clean
,
1262 fprintf(stderr
, Name
": Not enough devices to "
1263 "start the array while not clean "
1264 "- consider --force.\n");
1267 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1271 if (runstop
== -1) {
1272 fprintf(stderr
, Name
": %s assembled from %d drive%s",
1273 mddev
, okcnt
, okcnt
==1?"":"s");
1274 if (okcnt
!= (unsigned)content
->array
.raid_disks
)
1275 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1276 fprintf(stderr
, ", but not started.\n");
1280 if (verbose
>= -1) {
1281 fprintf(stderr
, Name
": %s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
1283 fprintf(stderr
, "%s %d rebuilding", sparecnt
?", ":" and ", rebuilding_cnt
);
1285 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1286 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1287 content
->array
.layout
, 1, avail
, okcnt
))
1288 fprintf(stderr
, " - not enough to start the array.\n");
1289 else if (!enough(content
->array
.level
,
1290 content
->array
.raid_disks
,
1291 content
->array
.layout
, clean
,
1293 fprintf(stderr
, " - not enough to start the "
1294 "array while not clean - consider "
1297 if (req_cnt
== (unsigned)content
->array
.raid_disks
)
1298 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1300 fprintf(stderr
, " - need %d of %d to start", req_cnt
, content
->array
.raid_disks
);
1301 fprintf(stderr
, " (use --run to insist).\n");
1305 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1309 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1310 * been updated to point to the current locations of devices.
1311 * so we can just start the array
1314 dev
= makedev(devices
[chosen_drive
].i
.disk
.major
,
1315 devices
[chosen_drive
].i
.disk
.minor
);
1316 if (ioctl(mdfd
, START_ARRAY
, dev
)) {
1317 fprintf(stderr
, Name
": Cannot start array: %s\n",
1327 int assemble_container_content(struct supertype
*st
, int mdfd
,
1328 struct mdinfo
*content
, int runstop
,
1329 char *chosen_name
, int verbose
)
1331 struct mdinfo
*dev
, *sra
;
1332 int working
= 0, preexist
= 0;
1333 struct map_ent
*map
= NULL
;
1335 sysfs_init(content
, mdfd
, 0);
1337 sra
= sysfs_read(mdfd
, 0, GET_VERSION
);
1338 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0)
1339 if (sysfs_set_array(content
, md_get_version(mdfd
)) != 0) {
1346 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
1347 if (sysfs_add_disk(content
, dev
, 1) == 0)
1349 else if (errno
== EEXIST
)
1353 return 1;/* Nothing new, don't try to start */
1356 map_update(&map
, fd2devnum(mdfd
),
1357 content
->text_version
,
1358 content
->uuid
, chosen_name
);
1361 (working
+ preexist
) >= content
->array
.working_disks
) {
1364 switch(content
->array
.level
) {
1366 case LEVEL_MULTIPATH
:
1368 err
= sysfs_set_str(content
, NULL
, "array_state",
1372 err
= sysfs_set_str(content
, NULL
, "array_state",
1374 /* start mdmon if needed. */
1376 if (!mdmon_running(st
->container_dev
))
1377 start_mdmon(st
->container_dev
);
1378 ping_monitor(devnum2devname(st
->container_dev
));
1383 sysfs_set_safemode(content
, content
->safe_mode_delay
);
1386 fprintf(stderr
, Name
1387 ": array %s now has %d devices",
1388 chosen_name
, working
+ preexist
);
1390 fprintf(stderr
, Name
1391 ": Started %s with %d devices",
1392 chosen_name
, working
+ preexist
);
1394 fprintf(stderr
, " (%d new)", working
);
1395 fprintf(stderr
, "\n");
1398 wait_for(chosen_name
, mdfd
);
1401 /* FIXME should have an O_EXCL and wait for read-auto */
1404 fprintf(stderr
, Name
1405 ": %s assembled with %d devices but "
1407 chosen_name
, working
);