2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2013 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'
36 if (strcmp(found
, required
)==0)
38 sep
= strchr(found
, ':');
42 if (strncmp(found
, "any:", 4) == 0 ||
43 (homehost
&& strcmp(homehost
, "any") == 0) ||
44 (homehost
&& strlen(homehost
) == l
&&
45 strncmp(found
, homehost
, l
) == 0)) {
46 /* matching homehost */
47 if (strcmp(sep
+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(0, 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 static int ident_matches(struct mddev_ident
*ident
,
79 struct mdinfo
*content
,
80 struct supertype
*tst
,
82 char *update
, char *devname
)
85 if (ident
->uuid_set
&& (!update
|| strcmp(update
, "uuid")!= 0) &&
86 same_uuid(content
->uuid
, ident
->uuid
, tst
->ss
->swapuuid
)==0 &&
87 memcmp(content
->uuid
, uuid_zero
, sizeof(int[4])) != 0) {
89 pr_err("%s has wrong uuid.\n", devname
);
92 if (ident
->name
[0] && (!update
|| strcmp(update
, "name")!= 0) &&
93 name_matches(content
->name
, ident
->name
, homehost
)==0) {
95 pr_err("%s has wrong name.\n", devname
);
98 if (ident
->super_minor
!= UnSet
&&
99 ident
->super_minor
!= content
->array
.md_minor
) {
101 pr_err("%s has wrong super-minor.\n",
105 if (ident
->level
!= UnSet
&&
106 ident
->level
!= content
->array
.level
) {
108 pr_err("%s has wrong raid level.\n",
112 if (ident
->raid_disks
!= UnSet
&&
113 content
->array
.raid_disks
!= 0 && /* metadata doesn't know how many to expect */
114 ident
->raid_disks
!= content
->array
.raid_disks
) {
116 pr_err("%s requires wrong number of drives.\n",
120 if (ident
->member
&& ident
->member
[0]) {
121 /* content->text_version must match */
122 char *s
= strchr(content
->text_version
+1, '/');
125 pr_err("%s is not a container and one is required.\n",
128 } else if (strcmp(ident
->member
, s
+1) != 0) {
130 pr_err("skipping wrong member %s is %s\n",
131 content
->text_version
, devname
);
138 static int select_devices(struct mddev_dev
*devlist
,
139 struct mddev_ident
*ident
,
140 struct supertype
**stp
,
141 struct mdinfo
**contentp
,
143 int inargv
, int auto_assem
)
145 struct mddev_dev
*tmpdev
;
147 struct supertype
*st
= *stp
;
148 struct mdinfo
*content
= NULL
;
149 int report_mismatch
= ((inargv
&& c
->verbose
>= 0) || c
->verbose
> 0);
150 struct domainlist
*domains
= NULL
;
152 tmpdev
= devlist
; num_devs
= 0;
158 tmpdev
->disposition
= 0;
159 tmpdev
= tmpdev
->next
;
162 /* first walk the list of devices to find a consistent set
163 * that match the criterea, if that is possible.
164 * We flag the ones we like with 'used'.
166 for (tmpdev
= devlist
;
168 tmpdev
= tmpdev
? tmpdev
->next
: NULL
) {
169 char *devname
= tmpdev
->devname
;
172 struct supertype
*tst
;
173 struct dev_policy
*pol
= NULL
;
174 int found_container
= 0;
176 if (tmpdev
->used
> 1)
179 if (ident
->container
) {
180 if (ident
->container
[0] == '/' &&
181 !same_dev(ident
->container
, devname
)) {
183 pr_err("%s is not the container required (%s)\n",
184 devname
, ident
->container
);
187 } else if (ident
->devices
&&
188 !match_oneof(ident
->devices
, devname
)) {
189 /* Note that we ignore the "device=" identifier if a
190 * "container=" is given. Checking both is unnecessarily
194 pr_err("%s is not one of %s\n", devname
, ident
->devices
);
200 dfd
= dev_open(devname
, O_RDONLY
);
203 pr_err("cannot open device %s: %s\n",
204 devname
, strerror(errno
));
206 } else if (fstat(dfd
, &stb
)< 0) {
208 pr_err("fstat failed for %s: %s\n",
209 devname
, strerror(errno
));
211 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
212 pr_err("%s is not a block device.\n",
215 } else if (must_be_container(dfd
)) {
217 /* already found some components, this cannot
221 pr_err("%s is a container, but we are looking for components\n",
224 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
225 } if (!tst
&& (tst
= super_by_fd(dfd
, NULL
)) == NULL
) {
227 pr_err("not a recognisable container: %s\n",
231 } else if (!tst
->ss
->load_container
232 || tst
->ss
->load_container(tst
, dfd
, NULL
)) {
234 pr_err("no correct container type: %s\n",
237 } else if (auto_assem
&&
238 !conf_test_metadata(tst
->ss
->name
, (pol
= devid_policy(stb
.st_rdev
)),
239 tst
->ss
->match_home(tst
, c
->homehost
) == 1)) {
241 pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
242 devname
, tst
->ss
->name
);
247 if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
249 pr_err("no recogniseable superblock on %s\n",
252 } else if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
254 pr_err("no RAID superblock on %s\n",
257 } else if (tst
->ss
->compare_super
== NULL
) {
259 pr_err("Cannot assemble %s metadata on %s\n",
260 tst
->ss
->name
, devname
);
262 } else if (auto_assem
&& st
== NULL
&&
263 !conf_test_metadata(tst
->ss
->name
, (pol
= devid_policy(stb
.st_rdev
)),
264 tst
->ss
->match_home(tst
, c
->homehost
) == 1)) {
266 pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
267 devname
, tst
->ss
->name
);
271 if (dfd
>= 0) close(dfd
);
272 if (tmpdev
->used
== 2) {
273 if (auto_assem
|| !inargv
)
274 /* Ignore unrecognised devices during auto-assembly */
276 if (ident
->uuid_set
|| ident
->name
[0] ||
277 ident
->super_minor
!= UnSet
)
278 /* Ignore unrecognised device if looking for
282 pr_err("%s has no superblock - assembly aborted\n",
285 st
->ss
->free_super(st
);
286 dev_policy_free(pol
);
287 domain_free(domains
);
291 if (found_container
) {
292 /* tmpdev is a container. We need to be either
293 * looking for a member, or auto-assembling
295 /* should be safe to try an exclusive open now, we
296 * have rejected anything that some other mdadm might
299 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
302 pr_err("%s is busy - skipping\n", devname
);
307 if (ident
->container
&& ident
->container
[0] != '/') {
312 tst
->ss
->getinfo_super(tst
, content
, NULL
);
314 if (!parse_uuid(ident
->container
, uuid
) ||
315 !same_uuid(content
->uuid
, uuid
, tst
->ss
->swapuuid
)) {
317 pr_err("%s has wrong UUID to be required container\n",
322 /* It is worth looking inside this container.
325 pr_err("looking in container %s\n",
328 for (content
= tst
->ss
->container_content(tst
, NULL
);
330 content
= content
->next
) {
332 if (!ident_matches(ident
, content
, tst
,
333 c
->homehost
, c
->update
,
334 report_mismatch
? devname
: NULL
))
335 /* message already printed */;
336 else if (is_member_busy(content
->text_version
)) {
338 pr_err("member %s in %s is already assembled\n",
339 content
->text_version
,
341 } else if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
342 /* do not assemble arrays with unsupported configurations */
343 pr_err("Cannot activate member %s in %s.\n",
344 content
->text_version
,
351 goto loop
; /* empty container */
354 st
= tst
; tst
= NULL
;
355 if (!auto_assem
&& inargv
&& tmpdev
->next
!= NULL
) {
356 pr_err("%s is a container, but is not only device given: confused and aborting\n",
358 st
->ss
->free_super(st
);
359 dev_policy_free(pol
);
360 domain_free(domains
);
364 pr_err("found match on member %s in %s\n",
365 content
->text_version
, devname
);
367 /* make sure we finished the loop */
372 tst
->ss
->getinfo_super(tst
, content
, NULL
);
374 if (!ident_matches(ident
, content
, tst
,
375 c
->homehost
, c
->update
,
376 report_mismatch
? devname
: NULL
))
380 /* Never auto-assemble things that conflict
381 * with mdadm.conf in some way
383 struct mddev_ident
*match
;
386 match
= conf_match(tst
, content
, devname
,
387 report_mismatch
? c
->verbose
: -1,
389 if (!match
&& rv
== 2)
391 if (match
&& match
->devname
&&
392 strcasecmp(match
->devname
, "<ignore>") == 0) {
394 pr_err("%s is a member of an explicitly ignored array\n",
398 if (match
&& !ident_matches(match
, content
, tst
,
399 c
->homehost
, c
->update
,
400 report_mismatch
? devname
: NULL
))
401 /* Array exists in mdadm.conf but some
402 * details don't match, so reject it
407 /* should be safe to try an exclusive open now, we
408 * have rejected anything that some other mdadm might
411 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
414 pr_err("%s is busy - skipping\n", devname
);
421 if (st
->minor_version
== -1)
422 st
->minor_version
= tst
->minor_version
;
424 if (memcmp(content
->uuid
, uuid_zero
,
425 sizeof(int[4])) == 0) {
426 /* this is a floating spare. It cannot define
427 * an array unless there are no more arrays of
428 * this type to be found. It can be included
429 * in an array of this type though.
435 if (st
->ss
!= tst
->ss
||
436 st
->minor_version
!= tst
->minor_version
||
437 st
->ss
->compare_super(st
, tst
) != 0) {
438 /* Some mismatch. If exactly one array matches this host,
439 * we can resolve on that one.
440 * Or, if we are auto assembling, we just ignore the second
446 int first
= st
->ss
->match_home(st
, c
->homehost
);
447 int last
= tst
->ss
->match_home(tst
, c
->homehost
);
449 (first
== 1 || last
== 1)) {
450 /* We can do something */
451 if (first
) {/* just ignore this one */
453 pr_err("%s misses out due to wrong homehost\n",
456 } else { /* reject all those sofar */
457 struct mddev_dev
*td
;
459 pr_err("%s overrides previous devices due to good homehost\n",
461 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
469 pr_err("superblock on %s doesn't match others - assembly aborted\n",
471 tst
->ss
->free_super(tst
);
472 st
->ss
->free_super(st
);
473 dev_policy_free(pol
);
474 domain_free(domains
);
480 /* Collect domain information from members only */
481 if (tmpdev
&& tmpdev
->used
== 1) {
483 pol
= devid_policy(stb
.st_rdev
);
484 domain_merge(&domains
, pol
, tst
?tst
->ss
->name
:NULL
);
486 dev_policy_free(pol
);
489 tst
->ss
->free_super(tst
);
492 /* Check if we found some imsm spares but no members */
495 memcmp(uuid_zero
, ident
->uuid
,sizeof(uuid_zero
)) == 0)) &&
497 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
498 if (tmpdev
->used
!= 3)
504 /* we need sb from one of the spares */
505 int dfd
= dev_open(tmpdev
->devname
, O_RDONLY
);
507 st
->ss
->load_super(st
, dfd
, NULL
))
514 /* Now reject spares that don't match domains of identified members */
515 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
517 if (tmpdev
->used
!= 3)
519 if (stat(tmpdev
->devname
, &stb
)< 0) {
520 pr_err("fstat failed for %s: %s\n",
521 tmpdev
->devname
, strerror(errno
));
524 struct dev_policy
*pol
= devid_policy(stb
.st_rdev
);
525 int dt
= domain_test(domains
, pol
, NULL
);
526 if (inargv
&& dt
!= 0)
527 /* take this spare as domains match
528 * if there are any */
530 else if (!inargv
&& dt
== 1)
531 /* device wasn't explicitly listed, so need
532 * explicit domain match - which we have */
535 /* if domains don't match mark as unused */
537 dev_policy_free(pol
);
540 domain_free(domains
);
542 if (st
&& st
->sb
&& content
== *contentp
)
543 st
->ss
->getinfo_super(st
, content
, NULL
);
551 int uptodate
; /* set once we decide that this device is as
552 * recent as everything else in the array.
554 int included
; /* set if the device is already in the array
555 * due to a previous '-I'
560 static int load_devices(struct devs
*devices
, char *devmap
,
561 struct mddev_ident
*ident
, struct supertype
**stp
,
562 struct mddev_dev
*devlist
, struct context
*c
,
563 struct mdinfo
*content
,
564 int mdfd
, char *mddev
,
565 int *most_recentp
, int *bestcntp
, int **bestp
,
568 struct mddev_dev
*tmpdev
;
574 int most_recent
= -1;
577 struct supertype
*st
= *stp
;
579 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) {
580 char *devname
= tmpdev
->devname
;
582 struct supertype
*tst
;
586 if (tmpdev
->used
!= 1)
588 /* looks like a good enough match to update the super block if needed */
591 /* prepare useful information in info structures */
596 if (strcmp(c
->update
, "uuid")==0 &&
599 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
600 read(rfd
, ident
->uuid
, 16) != 16) {
601 *(__u32
*)(ident
->uuid
) = random();
602 *(__u32
*)(ident
->uuid
+1) = random();
603 *(__u32
*)(ident
->uuid
+2) = random();
604 *(__u32
*)(ident
->uuid
+3) = random();
606 if (rfd
>= 0) close(rfd
);
608 dfd
= dev_open(devname
,
609 tmpdev
->disposition
== 'I'
610 ? O_RDWR
: (O_RDWR
|O_EXCL
));
613 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
614 pr_err("cannot re-read metadata from %s - aborting\n",
621 tst
->ss
->free_super(tst
);
626 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
628 memcpy(content
->uuid
, ident
->uuid
, 16);
629 strcpy(content
->name
, ident
->name
);
630 content
->array
.md_minor
= minor(stb2
.st_rdev
);
632 if (strcmp(c
->update
, "byteorder") == 0)
635 err
= tst
->ss
->update_super(tst
, content
, c
->update
,
641 pr_err("--update=%s not understood for %s metadata\n",
642 c
->update
, tst
->ss
->name
);
643 tst
->ss
->free_super(tst
);
652 if (strcmp(c
->update
, "uuid")==0 &&
655 memcpy(ident
->uuid
, content
->uuid
, 16);
657 if (tst
->ss
->store_super(tst
, dfd
))
658 pr_err("Could not re-write superblock on %s.\n",
661 if (strcmp(c
->update
, "uuid")==0 &&
662 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
663 if (bitmap_update_uuid(ident
->bitmap_fd
,
665 tst
->ss
->swapuuid
) != 0)
666 pr_err("Could not update uuid on external bitmap.\n");
673 dfd
= dev_open(devname
,
674 tmpdev
->disposition
== 'I'
675 ? O_RDWR
: (O_RDWR
|O_EXCL
));
678 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
679 pr_err("cannot re-read metadata from %s - aborting\n",
686 tst
->ss
->free_super(tst
);
691 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
698 pr_err("%s is identified as a member of %s, slot %d%s.\n",
699 devname
, mddev
, content
->disk
.raid_disk
,
700 (content
->disk
.state
& (1<<MD_DISK_REPLACEMENT
)) ? " replacement":"");
701 devices
[devcnt
].devname
= devname
;
702 devices
[devcnt
].uptodate
= 0;
703 devices
[devcnt
].included
= (tmpdev
->disposition
== 'I');
704 devices
[devcnt
].i
= *content
;
705 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
706 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
708 if (devices
[devcnt
].i
.disk
.state
== 6) {
709 if (most_recent
< 0 ||
710 devices
[devcnt
].i
.events
711 > devices
[most_recent
].i
.events
) {
712 struct supertype
*tmp
= tst
;
715 most_recent
= devcnt
;
718 tst
->ss
->free_super(tst
);
721 if (content
->array
.level
== LEVEL_MULTIPATH
)
722 /* with multipath, the raid_disk from the superblock is meaningless */
725 i
= devices
[devcnt
].i
.disk
.raid_disk
;
727 if (nextspare
< content
->array
.raid_disks
*2)
728 nextspare
= content
->array
.raid_disks
*2;
731 /* i is raid_disk - double it so there is room for
734 if (devices
[devcnt
].i
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))
736 if (i
>= content
->array
.raid_disks
*2 &&
742 int newbestcnt
= i
+10;
743 int *newbest
= xmalloc(sizeof(int)*newbestcnt
);
745 for (c
=0; c
< newbestcnt
; c
++)
747 newbest
[c
] = best
[c
];
752 bestcnt
= newbestcnt
;
755 devices
[best
[i
]].i
.events
756 == devices
[devcnt
].i
.events
757 && (devices
[best
[i
]].i
.disk
.minor
758 != devices
[devcnt
].i
.disk
.minor
)
760 && content
->array
.level
!= LEVEL_MULTIPATH
) {
761 /* two different devices with identical superblock.
762 * Could be a mis-detection caused by overlapping
763 * partitions. fail-safe.
765 pr_err("WARNING %s and %s appear to have very similar superblocks.\n"
766 " If they are really different, please --zero the superblock on one\n"
767 " If they are the same or overlap, please remove one from %s.\n",
768 devices
[best
[i
]].devname
, devname
,
769 inargv
? "the list" :
770 "the\n DEVICE list in mdadm.conf"
779 || (devices
[best
[i
]].i
.events
780 < devices
[devcnt
].i
.events
))
785 if (most_recent
>= 0)
786 *most_recentp
= most_recent
;
793 static int force_array(struct mdinfo
*content
,
794 struct devs
*devices
,
795 int *best
, int bestcnt
, char *avail
,
797 struct supertype
*st
,
801 while (!enough(content
->array
.level
, content
->array
.raid_disks
,
802 content
->array
.layout
, 1,
805 (content
->reshape_active
&& content
->delta_disks
> 0 &&
806 !enough(content
->array
.level
, (content
->array
.raid_disks
807 - content
->delta_disks
),
808 content
->new_layout
, 1,
811 /* Choose the newest best drive which is
812 * not up-to-date, update the superblock
816 struct supertype
*tst
;
817 unsigned long long current_events
;
818 int chosen_drive
= -1;
822 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
827 if (devices
[j
].uptodate
)
829 if (devices
[j
].i
.recovery_start
!= MaxSector
) {
831 if (!devices
[j
].i
.reshape_active
||
832 devices
[j
].i
.delta_disks
<= 0)
834 /* When increasing number of devices, an
835 * added device also appears to be
836 * recovering. It is safe to include it
837 * as long as it won't be a source of
839 * For now, just allow for last data
840 * devices in RAID4 or last devices in RAID4/5/6.
842 delta
= devices
[j
].i
.delta_disks
;
843 if (devices
[j
].i
.array
.level
>= 4 &&
844 devices
[j
].i
.array
.level
<= 6 &&
845 i
/2 >= content
->array
.raid_disks
- delta
)
847 else if (devices
[j
].i
.array
.level
== 4 &&
848 i
/2 >= content
->array
.raid_disks
- delta
- 1)
853 if (chosen_drive
< 0 ||
855 > devices
[chosen_drive
].i
.events
)
858 if (chosen_drive
< 0)
860 current_events
= devices
[chosen_drive
].i
.events
;
863 pr_err("forcing event count in %s(%d) from %d upto %d\n",
864 devices
[chosen_drive
].devname
,
865 devices
[chosen_drive
].i
.disk
.raid_disk
,
866 (int)(devices
[chosen_drive
].i
.events
),
867 (int)(devices
[most_recent
].i
.events
));
868 fd
= dev_open(devices
[chosen_drive
].devname
,
869 devices
[chosen_drive
].included
? O_RDWR
872 pr_err("Couldn't open %s for write - not updating\n",
873 devices
[chosen_drive
].devname
);
874 devices
[chosen_drive
].i
.events
= 0;
878 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
880 pr_err("RAID superblock disappeared from %s - not updating.\n",
881 devices
[chosen_drive
].devname
);
882 devices
[chosen_drive
].i
.events
= 0;
885 content
->events
= devices
[most_recent
].i
.events
;
886 tst
->ss
->update_super(tst
, content
, "force-one",
887 devices
[chosen_drive
].devname
, c
->verbose
,
890 if (tst
->ss
->store_super(tst
, fd
)) {
892 pr_err("Could not re-write superblock on %s\n",
893 devices
[chosen_drive
].devname
);
894 devices
[chosen_drive
].i
.events
= 0;
895 tst
->ss
->free_super(tst
);
899 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
900 devices
[chosen_drive
].uptodate
= 1;
901 avail
[chosen_drive
] = 1;
903 tst
->ss
->free_super(tst
);
905 /* If there are any other drives of the same vintage,
906 * add them in as well. We can't lose and we might gain
909 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
913 !devices
[j
].uptodate
&&
914 devices
[j
].i
.recovery_start
== MaxSector
&&
915 devices
[j
].i
.events
== current_events
) {
924 static int start_array(int mdfd
,
926 struct mdinfo
*content
,
927 struct supertype
*st
,
928 struct mddev_ident
*ident
,
929 int *best
, int bestcnt
,
931 struct devs
*devices
,
933 unsigned int sparecnt
,
934 unsigned int rebuilding_cnt
,
936 int clean
, char *avail
,
937 int start_partial_ok
,
944 unsigned int req_cnt
;
946 rv
= set_array_info(mdfd
, st
, content
);
948 pr_err("failed to set array info for %s: %s\n",
949 mddev
, strerror(errno
));
952 if (ident
->bitmap_fd
>= 0) {
953 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
954 pr_err("SET_BITMAP_FILE failed.\n");
957 } else if (ident
->bitmap_file
) {
958 /* From config file */
959 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
961 pr_err("Could not open bitmap file %s\n",
965 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
966 pr_err("Failed to set bitmapfile for %s\n", mddev
);
973 /* First, add the raid disks, but add the chosen one last */
974 for (i
=0; i
<= bestcnt
; i
++) {
978 if (j
== chosen_drive
)
983 if (j
>= 0 && !devices
[j
].included
) {
984 int dfd
= dev_open(devices
[j
].devname
,
987 remove_partitions(dfd
);
990 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
993 pr_err("failed to add %s to %s: %s\n",
997 if (i
< content
->array
.raid_disks
* 2
1002 } else if (c
->verbose
> 0)
1003 pr_err("added %s to %s as %d%s%s\n",
1004 devices
[j
].devname
, mddev
,
1005 devices
[j
].i
.disk
.raid_disk
,
1006 devices
[j
].uptodate
?"":
1007 " (possibly out of date)",
1008 (devices
[j
].i
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))?" replacement":"");
1009 } else if (j
>= 0) {
1011 pr_err("%s is already in %s as %d\n",
1012 devices
[j
].devname
, mddev
,
1013 devices
[j
].i
.disk
.raid_disk
);
1014 } else if (c
->verbose
> 0 && i
< content
->array
.raid_disks
*2
1016 pr_err("no uptodate device for slot %d of %s\n",
1020 if (content
->array
.level
== LEVEL_CONTAINER
) {
1021 if (c
->verbose
>= 0) {
1022 pr_err("Container %s has been assembled with %d drive%s",
1023 mddev
, okcnt
+sparecnt
, okcnt
+sparecnt
==1?"":"s");
1024 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1025 fprintf(stderr
, " (out of %d)",
1026 content
->array
.raid_disks
);
1027 fprintf(stderr
, "\n");
1030 if (st
->ss
->validate_container
) {
1031 struct mdinfo
*devices_list
;
1032 struct mdinfo
*info_devices
= xmalloc(sizeof(struct mdinfo
)*(okcnt
+sparecnt
));
1034 devices_list
= NULL
;
1035 for (count
= 0; count
< okcnt
+sparecnt
; count
++) {
1036 info_devices
[count
] = devices
[count
].i
;
1037 info_devices
[count
].next
= devices_list
;
1038 devices_list
= &info_devices
[count
];
1040 if (st
->ss
->validate_container(devices_list
))
1041 pr_err("Mismatch detected!\n");
1045 st
->ss
->free_super(st
);
1046 sysfs_uevent(content
, "change");
1047 if (err_ok
&& okcnt
< (unsigned)content
->array
.raid_disks
)
1048 /* Was partial, is still partial, so signal an error
1049 * to ensure we don't retry */
1054 /* Get number of in-sync devices according to the superblock.
1055 * We must have this number to start the array without -s or -R
1057 req_cnt
= content
->array
.working_disks
;
1059 if (c
->runstop
== 1 ||
1061 ( enough(content
->array
.level
, content
->array
.raid_disks
,
1062 content
->array
.layout
, clean
, avail
) &&
1063 (okcnt
+ rebuilding_cnt
>= req_cnt
|| start_partial_ok
)
1065 /* This array is good-to-go.
1066 * If a reshape is in progress then we might need to
1067 * continue monitoring it. In that case we start
1068 * it read-only and let the grow code make it writable.
1072 if (content
->reshape_active
&&
1073 !(content
->reshape_active
& RESHAPE_NO_BACKUP
) &&
1074 content
->delta_disks
<= 0) {
1075 if (!c
->backup_file
) {
1076 pr_err("%s: Need a backup file to complete reshape of this array.\n",
1078 pr_err("Please provided one with \"--backup-file=...\"\n");
1080 strcmp(c
->update
, "revert-reshape") == 0)
1081 pr_err("(Don't specify --update=revert-reshape again, that part succeeded.)\n");
1084 rv
= sysfs_set_str(content
, NULL
,
1085 "array_state", "readonly");
1087 rv
= Grow_continue(mdfd
, st
, content
,
1090 } else if (c
->readonly
&&
1091 sysfs_attribute_available(
1092 content
, NULL
, "array_state")) {
1093 rv
= sysfs_set_str(content
, NULL
,
1094 "array_state", "readonly");
1097 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1098 reopen_mddev(mdfd
); /* drop O_EXCL */
1100 if (c
->verbose
>= 0) {
1101 pr_err("%s has been started with %d drive%s",
1102 mddev
, okcnt
, okcnt
==1?"":"s");
1103 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1104 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1106 fprintf(stderr
, "%s %d rebuilding", sparecnt
?",":" and", rebuilding_cnt
);
1108 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1109 fprintf(stderr
, ".\n");
1111 if (content
->reshape_active
&&
1112 content
->array
.level
>= 4 &&
1113 content
->array
.level
<= 6) {
1114 /* might need to increase the size
1115 * of the stripe cache - default is 256
1117 int chunk_size
= content
->array
.chunk_size
;
1118 if (content
->reshape_active
&&
1119 content
->new_chunk
> chunk_size
)
1120 chunk_size
= content
->new_chunk
;
1121 if (256 < 4 * ((chunk_size
+4065)/4096)) {
1122 struct mdinfo
*sra
= sysfs_read(mdfd
, NULL
, 0);
1124 sysfs_set_num(sra
, NULL
,
1125 "stripe_cache_size",
1126 (4 * chunk_size
/ 4096) + 1);
1130 if (okcnt
< (unsigned)content
->array
.raid_disks
) {
1131 /* If any devices did not get added
1132 * because the kernel rejected them based
1133 * on event count, try adding them
1134 * again providing the action policy is
1135 * 're-add' or greater. The bitmap
1136 * might allow them to be included, or
1137 * they will become spares.
1139 for (i
= 0; i
< bestcnt
; i
++) {
1141 if (j
>= 0 && !devices
[j
].uptodate
) {
1142 if (!disk_action_allows(&devices
[j
].i
, st
->ss
->name
, act_re_add
))
1144 rv
= add_disk(mdfd
, st
, content
,
1146 if (rv
== 0 && c
->verbose
>= 0)
1147 pr_err("%s has been re-added.\n",
1148 devices
[j
].devname
);
1152 if (content
->array
.level
== 6 &&
1153 okcnt
+ 1 == (unsigned)content
->array
.raid_disks
&&
1155 struct mdinfo
*sra
= sysfs_read(mdfd
, NULL
, 0);
1157 sysfs_set_str(sra
, NULL
,
1158 "sync_action", "repair");
1163 pr_err("failed to RUN_ARRAY %s: %s\n",
1164 mddev
, strerror(errno
));
1166 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1167 content
->array
.layout
, 1, avail
))
1168 pr_err("Not enough devices to start the array.\n");
1169 else if (!enough(content
->array
.level
,
1170 content
->array
.raid_disks
,
1171 content
->array
.layout
, clean
,
1173 pr_err("Not enough devices to start the array while not clean - consider --force.\n");
1177 if (c
->runstop
== -1) {
1178 pr_err("%s assembled from %d drive%s",
1179 mddev
, okcnt
, okcnt
==1?"":"s");
1180 if (okcnt
!= (unsigned)content
->array
.raid_disks
)
1181 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1182 fprintf(stderr
, ", but not started.\n");
1185 if (c
->verbose
>= -1) {
1186 pr_err("%s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
1188 fprintf(stderr
, "%s %d rebuilding", sparecnt
?",":" and", rebuilding_cnt
);
1190 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1191 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1192 content
->array
.layout
, 1, avail
))
1193 fprintf(stderr
, " - not enough to start the array.\n");
1194 else if (!enough(content
->array
.level
,
1195 content
->array
.raid_disks
,
1196 content
->array
.layout
, clean
,
1198 fprintf(stderr
, " - not enough to start the array while not clean - consider --force.\n");
1200 if (req_cnt
== (unsigned)content
->array
.raid_disks
)
1201 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1203 fprintf(stderr
, " - need %d to start", req_cnt
);
1204 fprintf(stderr
, " (use --run to insist).\n");
1210 int Assemble(struct supertype
*st
, char *mddev
,
1211 struct mddev_ident
*ident
,
1212 struct mddev_dev
*devlist
,
1216 * The task of Assemble is to find a collection of
1217 * devices that should (according to their superblocks)
1218 * form an array, and to give this collection to the MD driver.
1219 * In Linux-2.4 and later, this involves submitting a
1220 * SET_ARRAY_INFO ioctl with no arg - to prepare
1221 * the array - and then submit a number of
1222 * ADD_NEW_DISK ioctls to add disks into
1223 * the array. Finally RUN_ARRAY might
1224 * be submitted to start the array.
1226 * Much of the work of Assemble is in finding and/or
1227 * checking the disks to make sure they look right.
1229 * If mddev is not set, then scan must be set and we
1230 * read through the config file for dev+uuid mapping
1231 * We recurse, setting mddev, for each device that
1233 * - has a valid uuid (or any uuid if !uuidset)
1235 * If mddev is set, we try to determine state of md.
1236 * check version - must be at least 0.90.0
1237 * check kernel version. must be at least 2.4.
1238 * If not, we can possibly fall back on START_ARRAY
1239 * Try to GET_ARRAY_INFO.
1240 * If possible, give up
1241 * If not, try to STOP_ARRAY just to make sure
1243 * If !uuidset and scan, look in conf-file for uuid
1244 * If not found, give up
1245 * If !devlist and scan and uuidset, get list of devs from conf-file
1248 * Check superblock - discard if bad
1249 * Check uuid (set if we don't have one) - discard if no match
1250 * Check superblock similarity if we have a superblock - discard if different
1251 * Record events, devicenum
1252 * This should give us a list of devices for the array
1253 * We should collect the most recent event number
1255 * Count disks with recent enough event count
1256 * While force && !enough disks
1257 * Choose newest rejected disks, update event count
1258 * mark clean and rewrite superblock
1261 * foreach device with recent events : ADD_NEW_DISK
1262 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
1264 * Check the device numbers in superblock are right
1265 * update superblock if any changes
1272 int auto_assem
= (mddev
== NULL
&& !ident
->uuid_set
&&
1273 ident
->super_minor
== UnSet
&& ident
->name
[0] == 0
1274 && (ident
->container
== NULL
|| ident
->member
== NULL
));
1275 struct devs
*devices
;
1277 int *best
= NULL
; /* indexed by raid_disk */
1280 unsigned int okcnt
, sparecnt
, rebuilding_cnt
, replcnt
;
1283 int most_recent
= 0;
1287 int start_partial_ok
= (c
->runstop
>= 0) &&
1288 (c
->force
|| devlist
==NULL
|| auto_assem
);
1290 struct mddev_dev
*tmpdev
;
1292 struct mdinfo
*content
= NULL
;
1293 struct mdinfo
*pre_exist
= NULL
;
1296 char chosen_name
[1024];
1297 struct map_ent
*map
= NULL
;
1301 * If any subdevs are listed, then any that don't
1302 * match ident are discarded. Remainder must all match and
1304 * If no subdevs, then we scan all devices in the config file, but
1305 * there must be something in the identity
1309 ident
->uuid_set
== 0 &&
1310 (ident
->super_minor
< 0 || ident
->super_minor
== UnSet
) &&
1311 ident
->name
[0] == 0 &&
1312 (ident
->container
== NULL
|| ident
->member
== NULL
) &&
1313 ident
->devices
== NULL
) {
1314 pr_err("No identity information available for %s - cannot assemble.\n",
1315 mddev
? mddev
: "further assembly");
1319 if (devlist
== NULL
)
1320 devlist
= conf_get_devs();
1325 /* We come back here when doing auto-assembly and attempting some
1326 * set of devices failed. Those are now marked as ->used==2 and
1327 * we ignore them and try again
1329 if (!st
&& ident
->st
)
1332 pr_err("looking for devices for %s\n",
1333 mddev
? mddev
: "further assembly");
1337 st
->ignore_hw_compat
= 1;
1338 num_devs
= select_devices(devlist
, ident
, &st
, &content
, c
,
1339 inargv
, auto_assem
);
1343 if (!st
|| !st
->sb
|| !content
)
1346 /* We have a full set of devices - we now need to find the
1348 * However there is a risk that we are racing with "mdadm -I"
1349 * and the array is already partially assembled - we will have
1350 * rejected any devices already in this address.
1351 * So we take a lock on the map file - to prevent further races -
1352 * and look for the uuid in there. If found and the array is
1353 * active, we abort. If found and the array is not active
1354 * we commit to that md device and add all the contained devices
1355 * to our list. We flag them so that we don't try to re-add,
1356 * but can remove if they turn out to not be wanted.
1359 pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
1360 if (c
->update
&& strcmp(c
->update
,"uuid") == 0)
1363 mp
= map_by_uuid(&map
, content
->uuid
);
1366 /* array already exists. */
1367 pre_exist
= sysfs_read(-1, mp
->devnm
, GET_LEVEL
|GET_DEVS
);
1368 if (pre_exist
->array
.level
!= UnSet
) {
1369 pr_err("Found some drive for an array that is already active: %s\n",
1371 pr_err("giving up.\n");
1374 for (dv
= pre_exist
->devs
; dv
; dv
= dv
->next
) {
1375 /* We want to add this device to our list,
1376 * but it could already be there if "mdadm -I"
1377 * started *after* we checked for O_EXCL.
1378 * If we add it to the top of the list
1379 * it will be preferred over later copies.
1381 struct mddev_dev
*newdev
;
1382 char *devname
= map_dev(dv
->disk
.major
,
1387 newdev
= xmalloc(sizeof(*newdev
));
1388 newdev
->devname
= devname
;
1389 newdev
->disposition
= 'I';
1391 newdev
->next
= devlist
;
1395 strcpy(chosen_name
, mp
->path
);
1396 if (c
->verbose
> 0 || mddev
== NULL
||
1397 strcmp(mddev
, chosen_name
) != 0)
1398 pr_err("Merging with already-assembled %s\n",
1400 mdfd
= open_dev_excl(mp
->devnm
);
1402 int trustworthy
= FOREIGN
;
1403 name
= content
->name
;
1404 switch (st
->ss
->match_home(st
, c
->homehost
)
1405 ?: st
->ss
->match_home(st
, "any")) {
1407 trustworthy
= LOCAL
;
1408 name
= strchr(content
->name
, ':');
1412 name
= content
->name
;
1416 /* If the array is listed in mdadm.conf or on
1417 * command line, then we trust the name
1418 * even if the array doesn't look local
1420 trustworthy
= LOCAL
;
1423 content
->array
.level
== LEVEL_CONTAINER
) {
1424 name
= content
->text_version
;
1425 trustworthy
= METADATA
;
1428 if (name
[0] && trustworthy
!= LOCAL
&&
1429 ! c
->require_homehost
&&
1430 conf_name_is_free(name
))
1431 trustworthy
= LOCAL
;
1433 if (trustworthy
== LOCAL
&&
1435 /* Ignore 'host:' prefix of name */
1436 name
= strchr(name
, ':')+1;
1438 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
1442 st
->ss
->free_super(st
);
1447 mddev
= chosen_name
;
1448 if (get_linux_version() < 2004000 ||
1449 md_get_version(mdfd
) < 9000) {
1450 pr_err("Assemble requires Linux 2.4 or later, and\n"
1451 " md driver version 0.90.0 or later.\n"
1452 " Upgrade your kernel or try --build\n");
1456 if (pre_exist
== NULL
) {
1457 if (mddev_busy(fd2devnm(mdfd
))) {
1458 pr_err("%s already active, cannot restart it!\n",
1460 for (tmpdev
= devlist
;
1461 tmpdev
&& tmpdev
->used
!= 1;
1462 tmpdev
= tmpdev
->next
)
1464 if (tmpdev
&& auto_assem
)
1465 pr_err("%s needed for %s...\n",
1466 mddev
, tmpdev
->devname
);
1469 st
->ss
->free_super(st
);
1474 /* just incase it was started but has no content */
1475 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1479 if (content
!= &info
) {
1480 /* This is a member of a container. Try starting the array. */
1482 err
= assemble_container_content(st
, mdfd
, content
, c
,
1488 /* Ok, no bad inconsistancy, we can try updating etc */
1489 devices
= xcalloc(num_devs
, sizeof(*devices
));
1490 devmap
= xcalloc(num_devs
, content
->array
.raid_disks
);
1491 devcnt
= load_devices(devices
, devmap
, ident
, &st
, devlist
,
1492 c
, content
, mdfd
, mddev
,
1493 &most_recent
, &bestcnt
, &best
, inargv
);
1498 pr_err("no devices found for %s\n",
1501 st
->ss
->free_super(st
);
1508 if (c
->update
&& strcmp(c
->update
, "byteorder")==0)
1509 st
->minor_version
= 90;
1511 st
->ss
->getinfo_super(st
, content
, NULL
);
1512 clean
= content
->array
.state
& 1;
1514 /* now we have some devices that might be suitable.
1517 avail
= xcalloc(content
->array
.raid_disks
, 1);
1522 for (i
=0; i
< bestcnt
; i
++) {
1524 int event_margin
= 1; /* always allow a difference of '1'
1525 * like the kernel does
1527 if (j
< 0) continue;
1528 /* note: we ignore error flags in multipath arrays
1529 * as they don't make sense
1531 if (content
->array
.level
!= LEVEL_MULTIPATH
)
1532 if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_ACTIVE
))) {
1533 if (!(devices
[j
].i
.disk
.state
1534 & (1<<MD_DISK_FAULTY
))) {
1535 devices
[j
].uptodate
= 1;
1540 /* If this device thinks that 'most_recent' has failed, then
1541 * we must reject this device.
1543 if (j
!= most_recent
&& !c
->force
&&
1544 content
->array
.raid_disks
> 0 &&
1545 devices
[most_recent
].i
.disk
.raid_disk
>= 0 &&
1546 devmap
[j
* content
->array
.raid_disks
+ devices
[most_recent
].i
.disk
.raid_disk
] == 0) {
1547 if (c
->verbose
> -1)
1548 pr_err("ignoring %s as it reports %s as failed\n",
1549 devices
[j
].devname
, devices
[most_recent
].devname
);
1553 /* Require event counter to be same as, or just less than,
1554 * most recent. If it is bigger, it must be a stray spare and
1555 * should be ignored.
1557 if (devices
[j
].i
.events
+event_margin
>=
1558 devices
[most_recent
].i
.events
&&
1559 devices
[j
].i
.events
<=
1560 devices
[most_recent
].i
.events
1562 devices
[j
].uptodate
= 1;
1563 if (i
< content
->array
.raid_disks
* 2) {
1564 if (devices
[j
].i
.recovery_start
== MaxSector
||
1565 (content
->reshape_active
&&
1566 i
>= content
->array
.raid_disks
- content
->delta_disks
)) {
1580 int force_ok
= force_array(content
, devices
, best
, bestcnt
,
1581 avail
, most_recent
, st
, c
);
1586 /* Now we want to look at the superblock which the kernel will base things on
1587 * and compare the devices that we think are working with the devices that the
1588 * superblock thinks are working.
1589 * If there are differences and --force is given, then update this chosen
1593 st
->ss
->free_super(st
);
1594 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
+=2) {
1600 if (!devices
[j
].uptodate
)
1602 if (devices
[j
].i
.events
< devices
[most_recent
].i
.events
)
1605 if ((fd
=dev_open(devices
[j
].devname
,
1606 devices
[j
].included
? O_RDONLY
1607 : (O_RDONLY
|O_EXCL
)))< 0) {
1608 pr_err("Cannot open %s: %s\n",
1609 devices
[j
].devname
, strerror(errno
));
1614 if (st
->ss
->load_super(st
,fd
, NULL
)) {
1616 pr_err("RAID superblock has disappeared from %s\n",
1617 devices
[j
].devname
);
1624 if (st
->sb
== NULL
) {
1625 pr_err("No suitable drives found for %s\n", mddev
);
1630 st
->ss
->getinfo_super(st
, content
, NULL
);
1632 sysfs_init(content
, mdfd
, NULL
);
1634 for (i
=0; i
<bestcnt
; i
++) {
1636 unsigned int desired_state
;
1638 if (i
>= content
->array
.raid_disks
* 2)
1641 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_REPLACEMENT
);
1643 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
1647 if (!devices
[j
].uptodate
)
1650 devices
[j
].i
.disk
.state
= desired_state
;
1651 if (!(devices
[j
].i
.array
.state
& 1))
1654 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
1655 c
->verbose
, 0, NULL
)) {
1657 if (c
->verbose
>= 0)
1658 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
1659 j
, mddev
, devices
[j
].devname
);
1662 if (c
->verbose
>= -1)
1663 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
1664 i
, mddev
, devices
[j
].devname
);
1668 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
1669 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1674 if (c
->force
&& !clean
&&
1675 !enough(content
->array
.level
, content
->array
.raid_disks
,
1676 content
->array
.layout
, clean
,
1678 change
+= st
->ss
->update_super(st
, content
, "force-array",
1679 devices
[chosen_drive
].devname
, c
->verbose
,
1687 fd
= dev_open(devices
[chosen_drive
].devname
,
1688 devices
[chosen_drive
].included
?
1689 O_RDWR
: (O_RDWR
|O_EXCL
));
1691 pr_err("Could not open %s for write - cannot Assemble array.\n",
1692 devices
[chosen_drive
].devname
);
1697 if (st
->ss
->store_super(st
, fd
)) {
1699 pr_err("Could not re-write superblock on %s\n",
1700 devices
[chosen_drive
].devname
);
1705 if (c
->verbose
>= 0)
1706 pr_err("Marking array %s as 'clean'\n",
1711 /* If we are in the middle of a reshape we may need to restore saved data
1712 * that was moved aside due to the reshape overwriting live data
1713 * The code of doing this lives in Grow.c
1716 if (content
->reshape_active
&&
1717 !(content
->reshape_active
& RESHAPE_NO_BACKUP
)) {
1719 int *fdlist
= xmalloc(sizeof(int)* bestcnt
);
1721 pr_err("%s has an active reshape - checking if critical section needs to be restored\n",
1723 if (!c
->backup_file
)
1724 c
->backup_file
= locate_backup(content
->sys_name
);
1725 enable_fds(bestcnt
/2);
1726 for (i
= 0; i
< bestcnt
/2; i
++) {
1729 fdlist
[i
] = dev_open(devices
[j
].devname
,
1731 ? O_RDWR
: (O_RDWR
|O_EXCL
));
1732 if (fdlist
[i
] < 0) {
1733 pr_err("Could not open %s for write - cannot Assemble array.\n",
1734 devices
[j
].devname
);
1742 if (st
->ss
->external
&& st
->ss
->recover_backup
)
1743 err
= st
->ss
->recover_backup(st
, content
);
1745 err
= Grow_restart(st
, content
, fdlist
, bestcnt
/2,
1746 c
->backup_file
, c
->verbose
> 0);
1747 if (err
&& c
->invalid_backup
) {
1749 pr_err("continuing without restoring backup\n");
1755 if (fdlist
[i
]>=0) close(fdlist
[i
]);
1759 pr_err("Failed to restore critical section for reshape, sorry.\n");
1760 if (c
->backup_file
== NULL
)
1761 cont_err("Possibly you needed to specify the --backup-file\n");
1769 /* Almost ready to actually *do* something */
1770 /* First, fill in the map, so that udev can find our name
1771 * as soon as we become active.
1773 if (c
->update
&& strcmp(c
->update
, "metadata")==0) {
1774 content
->array
.major_version
= 1;
1775 content
->array
.minor_version
= 0;
1776 strcpy(content
->text_version
, "1.0");
1779 map_update(&map
, fd2devnm(mdfd
), content
->text_version
,
1780 content
->uuid
, chosen_name
);
1782 rv
= start_array(mdfd
, mddev
, content
,
1783 st
, ident
, best
, bestcnt
,
1784 chosen_drive
, devices
, okcnt
, sparecnt
,
1787 clean
, avail
, start_partial_ok
,
1790 if (rv
== 1 && !pre_exist
)
1791 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1795 wait_for(chosen_name
, mdfd
);
1799 /* There is a nasty race with 'mdadm --monitor'.
1800 * If it opens this device before we close it,
1801 * it gets an incomplete open on which IO
1802 * doesn't work and the capacity is
1804 * If we reopen (to check for layered devices)
1805 * before --monitor closes, we loose.
1807 * So: wait upto 1 second for there to be
1808 * a non-zero capacity.
1810 while (usecs
< 1000) {
1811 mdfd
= open(mddev
, O_RDONLY
);
1813 unsigned long long size
;
1814 if (get_dev_size(mdfd
, NULL
, &size
) &&
1826 /* '2' means 'OK, but not started yet' */
1827 return rv
== 2 ? 0 : rv
;
1831 int assemble_container_content(struct supertype
*st
, int mdfd
,
1832 struct mdinfo
*content
, struct context
*c
,
1833 char *chosen_name
, int *result
)
1835 struct mdinfo
*dev
, *sra
, *dev2
;
1836 int working
= 0, preexist
= 0;
1838 struct map_ent
*map
= NULL
;
1844 sysfs_init(content
, mdfd
, NULL
);
1846 sra
= sysfs_read(mdfd
, NULL
, GET_VERSION
|GET_DEVS
);
1847 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0) {
1848 if (content
->array
.major_version
== -1 &&
1849 content
->array
.minor_version
== -2 &&
1851 content
->text_version
[0] == '/')
1852 content
->text_version
[0] = '-';
1853 if (sysfs_set_array(content
, md_get_version(mdfd
)) != 0) {
1860 /* There are two types of reshape: container wide or sub-array specific
1861 * Check if metadata requests blocking container wide reshapes
1863 start_reshape
= (content
->reshape_active
&&
1864 !((content
->reshape_active
== CONTAINER_RESHAPE
) &&
1865 (content
->array
.state
& (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))));
1867 /* Block subarray here if it is under reshape now
1868 * Do not allow for any changes in this array
1870 if (st
->ss
->external
&& content
->recovery_blocked
&& start_reshape
)
1871 block_subarray(content
);
1873 for (dev2
= sra
->devs
; dev2
; dev2
= dev2
->next
) {
1874 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
1875 if (dev2
->disk
.major
== dev
->disk
.major
&&
1876 dev2
->disk
.minor
== dev
->disk
.minor
)
1880 /* Don't want this one any more */
1881 if (sysfs_set_str(sra
, dev2
, "slot", "none") < 0 &&
1883 pr_err("Cannot remove old device %s: not updating %s\n", dev2
->sys_name
, sra
->sys_name
);
1887 sysfs_set_str(sra
, dev2
, "state", "remove");
1889 old_raid_disks
= content
->array
.raid_disks
- content
->delta_disks
;
1890 avail
= xcalloc(content
->array
.raid_disks
, 1);
1891 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
1892 if (dev
->disk
.raid_disk
>= 0)
1893 avail
[dev
->disk
.raid_disk
] = 1;
1894 if (sysfs_add_disk(content
, dev
, 1) == 0) {
1895 if (dev
->disk
.raid_disk
>= old_raid_disks
&&
1896 content
->reshape_active
)
1900 } else if (errno
== EEXIST
)
1904 if (working
+ expansion
== 0 && c
->runstop
<= 0) {
1906 return 1;/* Nothing new, don't try to start */
1908 map_update(&map
, fd2devnm(mdfd
),
1909 content
->text_version
,
1910 content
->uuid
, chosen_name
);
1913 if (enough(content
->array
.level
, content
->array
.raid_disks
,
1914 content
->array
.layout
, content
->array
.state
& 1, avail
) == 0) {
1915 if (c
->export
&& result
)
1917 else if (c
->verbose
>= 0) {
1918 pr_err("%s assembled with %d device%s",
1919 chosen_name
, preexist
+ working
,
1920 preexist
+ working
== 1 ? "":"s");
1922 fprintf(stderr
, " (%d new)", working
);
1923 fprintf(stderr
, " but not started\n");
1930 if (c
->runstop
<= 0 &&
1931 (working
+ preexist
+ expansion
) <
1932 content
->array
.working_disks
) {
1933 if (c
->export
&& result
)
1934 *result
|= INCR_UNSAFE
;
1935 else if (c
->verbose
>= 0) {
1936 pr_err("%s assembled with %d device%s",
1937 chosen_name
, preexist
+ working
,
1938 preexist
+ working
== 1 ? "":"s");
1940 fprintf(stderr
, " (%d new)", working
);
1941 fprintf(stderr
, " but not safe to start\n");
1947 if (start_reshape
) {
1948 int spare
= content
->array
.raid_disks
+ expansion
;
1949 if (restore_backup(st
, content
,
1951 spare
, &c
->backup_file
, c
->verbose
) == 1)
1954 err
= sysfs_set_str(content
, NULL
,
1955 "array_state", "readonly");
1959 if (st
->ss
->external
) {
1960 if (!mdmon_running(st
->container_devnm
))
1961 start_mdmon(st
->container_devnm
);
1962 ping_monitor(st
->container_devnm
);
1963 if (mdmon_running(st
->container_devnm
) &&
1964 st
->update_tail
== NULL
)
1965 st
->update_tail
= &st
->updates
;
1968 err
= Grow_continue(mdfd
, st
, content
, c
->backup_file
,
1969 0, c
->freeze_reshape
);
1970 } else switch(content
->array
.level
) {
1972 case LEVEL_MULTIPATH
:
1974 err
= sysfs_set_str(content
, NULL
, "array_state",
1975 c
->readonly
? "readonly" : "active");
1978 err
= sysfs_set_str(content
, NULL
, "array_state",
1980 /* start mdmon if needed. */
1982 if (!mdmon_running(st
->container_devnm
))
1983 start_mdmon(st
->container_devnm
);
1984 ping_monitor(st
->container_devnm
);
1989 sysfs_set_safemode(content
, content
->safe_mode_delay
);
1991 /* Block subarray here if it is not reshaped now
1992 * It has be blocked a little later to allow mdmon to switch in
1995 if (st
->ss
->external
&& content
->recovery_blocked
&&
1997 block_subarray(content
);
1999 if (c
->export
&& result
) {
2003 *result
|= INCR_YES
;
2004 } else if (c
->verbose
>= 0) {
2006 pr_err("array %s now has %d device%s",
2007 chosen_name
, working
+ preexist
,
2008 working
+ preexist
== 1 ? "":"s");
2010 pr_err("Started %s with %d device%s",
2011 chosen_name
, working
+ preexist
,
2012 working
+ preexist
== 1 ? "":"s");
2014 fprintf(stderr
, " (%d new)", working
);
2016 fprintf(stderr
, " ( + %d for expansion)",
2018 fprintf(stderr
, "\n");
2021 wait_for(chosen_name
, mdfd
);
2023 /* FIXME should have an O_EXCL and wait for read-auto */