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'
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(0, 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 static int ident_matches(struct mddev_ident
*ident
,
74 struct mdinfo
*content
,
75 struct supertype
*tst
,
77 char *update
, char *devname
)
80 if (ident
->uuid_set
&& (!update
|| strcmp(update
, "uuid")!= 0) &&
81 same_uuid(content
->uuid
, ident
->uuid
, tst
->ss
->swapuuid
)==0 &&
82 memcmp(content
->uuid
, uuid_zero
, sizeof(int[4])) != 0) {
84 pr_err("%s has wrong uuid.\n", devname
);
87 if (ident
->name
[0] && (!update
|| strcmp(update
, "name")!= 0) &&
88 name_matches(content
->name
, ident
->name
, homehost
)==0) {
90 pr_err("%s has wrong name.\n", devname
);
93 if (ident
->super_minor
!= UnSet
&&
94 ident
->super_minor
!= content
->array
.md_minor
) {
96 pr_err("%s has wrong super-minor.\n",
100 if (ident
->level
!= UnSet
&&
101 ident
->level
!= content
->array
.level
) {
103 pr_err("%s has wrong raid level.\n",
107 if (ident
->raid_disks
!= UnSet
&&
108 content
->array
.raid_disks
!= 0 && /* metadata doesn't know how many to expect */
109 ident
->raid_disks
!= content
->array
.raid_disks
) {
111 pr_err("%s requires wrong number of drives.\n",
115 if (ident
->member
&& ident
->member
[0]) {
116 /* content->text_version must match */
117 char *s
= strchr(content
->text_version
+1, '/');
120 pr_err("%s is not a container and one is required.\n",
123 } else if (strcmp(ident
->member
, s
+1) != 0) {
125 pr_err("skipping wrong member %s is %s\n",
126 content
->text_version
, devname
);
133 static int select_devices(struct mddev_dev
*devlist
,
134 struct mddev_ident
*ident
,
135 struct supertype
**stp
,
136 struct mdinfo
**contentp
,
138 int inargv
, int auto_assem
)
140 struct mddev_dev
*tmpdev
;
142 struct supertype
*st
= *stp
;
143 struct mdinfo
*content
= NULL
;
144 int report_mismatch
= ((inargv
&& c
->verbose
>= 0) || c
->verbose
> 0);
145 struct domainlist
*domains
= NULL
;
147 tmpdev
= devlist
; num_devs
= 0;
153 tmpdev
->disposition
= 0;
154 tmpdev
= tmpdev
->next
;
157 /* first walk the list of devices to find a consistent set
158 * that match the criterea, if that is possible.
159 * We flag the ones we like with 'used'.
161 for (tmpdev
= devlist
;
163 tmpdev
= tmpdev
? tmpdev
->next
: NULL
) {
164 char *devname
= tmpdev
->devname
;
167 struct supertype
*tst
;
168 struct dev_policy
*pol
= NULL
;
169 int found_container
= 0;
171 if (tmpdev
->used
> 1)
174 if (ident
->container
) {
175 if (ident
->container
[0] == '/' &&
176 !same_dev(ident
->container
, devname
)) {
178 pr_err("%s is not the container required (%s)\n",
179 devname
, ident
->container
);
182 } else if (ident
->devices
&&
183 !match_oneof(ident
->devices
, devname
)) {
184 /* Note that we ignore the "device=" identifier if a
185 * "container=" is given. Checking both is unnecessarily
189 pr_err("%s is not one of %s\n", devname
, ident
->devices
);
195 dfd
= dev_open(devname
, O_RDONLY
);
198 pr_err("cannot open device %s: %s\n",
199 devname
, strerror(errno
));
201 } else if (fstat(dfd
, &stb
)< 0) {
203 pr_err("fstat failed for %s: %s\n",
204 devname
, strerror(errno
));
206 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
207 pr_err("%s is not a block device.\n",
210 } else if (must_be_container(dfd
)) {
212 /* already found some components, this cannot
216 pr_err("%s is a container, but we are looking for components\n",
219 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
220 } if (!tst
&& (tst
= super_by_fd(dfd
, NULL
)) == NULL
) {
222 pr_err("not a recognisable container: %s\n",
226 } else if (!tst
->ss
->load_container
227 || tst
->ss
->load_container(tst
, dfd
, NULL
)) {
229 pr_err("no correct container type: %s\n",
232 } else if (auto_assem
&&
233 !conf_test_metadata(tst
->ss
->name
, (pol
= devid_policy(stb
.st_rdev
)),
234 tst
->ss
->match_home(tst
, c
->homehost
) == 1)) {
236 pr_err("%s has metadata type %s for which "
237 "auto-assembly is disabled\n",
238 devname
, tst
->ss
->name
);
243 if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
245 pr_err("no recogniseable superblock on %s\n",
248 } else if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
250 pr_err("no RAID superblock on %s\n",
253 } else if (tst
->ss
->compare_super
== NULL
) {
255 pr_err("Cannot assemble %s metadata on %s\n",
256 tst
->ss
->name
, devname
);
258 } else if (auto_assem
&& st
== NULL
&&
259 !conf_test_metadata(tst
->ss
->name
, (pol
= devid_policy(stb
.st_rdev
)),
260 tst
->ss
->match_home(tst
, c
->homehost
) == 1)) {
262 pr_err("%s has metadata type %s for which "
263 "auto-assembly is disabled\n",
264 devname
, tst
->ss
->name
);
268 if (dfd
>= 0) close(dfd
);
269 if (tmpdev
->used
== 2) {
270 if (auto_assem
|| !inargv
)
271 /* Ignore unrecognised devices during auto-assembly */
273 if (ident
->uuid_set
|| ident
->name
[0] ||
274 ident
->super_minor
!= UnSet
)
275 /* Ignore unrecognised device if looking for
279 pr_err("%s has no superblock - assembly aborted\n",
282 st
->ss
->free_super(st
);
283 dev_policy_free(pol
);
284 domain_free(domains
);
288 if (found_container
) {
289 /* tmpdev is a container. We need to be either
290 * looking for a member, or auto-assembling
292 /* should be safe to try an exclusive open now, we
293 * have rejected anything that some other mdadm might
296 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
299 pr_err("%s is busy - skipping\n", devname
);
304 if (ident
->container
&& ident
->container
[0] != '/') {
309 tst
->ss
->getinfo_super(tst
, content
, NULL
);
311 if (!parse_uuid(ident
->container
, uuid
) ||
312 !same_uuid(content
->uuid
, uuid
, tst
->ss
->swapuuid
)) {
314 pr_err("%s has wrong UUID to be required container\n",
319 /* It is worth looking inside this container.
322 pr_err("looking in container %s\n",
325 for (content
= tst
->ss
->container_content(tst
, NULL
);
327 content
= content
->next
) {
329 if (!ident_matches(ident
, content
, tst
,
330 c
->homehost
, c
->update
,
331 report_mismatch
? devname
: NULL
))
332 /* message already printed */;
333 else if (is_member_busy(content
->text_version
)) {
335 pr_err("member %s in %s is already assembled\n",
336 content
->text_version
,
338 } else if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
339 /* do not assemble arrays with unsupported configurations */
340 pr_err("Cannot activate member %s in %s.\n",
341 content
->text_version
,
348 goto loop
; /* empty container */
351 st
= tst
; tst
= NULL
;
352 if (!auto_assem
&& inargv
&& tmpdev
->next
!= NULL
) {
353 pr_err("%s is a container, but is not "
354 "only device given: confused and aborting\n",
356 st
->ss
->free_super(st
);
357 dev_policy_free(pol
);
358 domain_free(domains
);
362 pr_err("found match on member %s in %s\n",
363 content
->text_version
, devname
);
365 /* make sure we finished the loop */
370 struct mddev_ident
*match
;
373 tst
->ss
->getinfo_super(tst
, content
, NULL
);
375 if (!ident_matches(ident
, content
, tst
,
376 c
->homehost
, c
->update
,
377 report_mismatch
? devname
: NULL
))
380 match
= conf_match(tst
, content
, devname
,
381 report_mismatch
? c
->verbose
: -1,
383 if (!match
&& rv
== 2)
385 if (match
&& match
->devname
&&
386 strcasecmp(match
->devname
, "<ignore>") == 0) {
388 pr_err("%s is a member of an explicitly ignored array\n",
392 if (match
&& !ident_matches(match
, content
, tst
,
393 c
->homehost
, c
->update
,
394 report_mismatch
? devname
: NULL
))
395 /* Array exists in mdadm.conf but some
396 * details don't match, so reject it
400 /* should be safe to try an exclusive open now, we
401 * have rejected anything that some other mdadm might
404 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
407 pr_err("%s is busy - skipping\n", devname
);
414 if (st
->minor_version
== -1)
415 st
->minor_version
= tst
->minor_version
;
417 if (memcmp(content
->uuid
, uuid_zero
,
418 sizeof(int[4])) == 0) {
419 /* this is a floating spare. It cannot define
420 * an array unless there are no more arrays of
421 * this type to be found. It can be included
422 * in an array of this type though.
428 if (st
->ss
!= tst
->ss
||
429 st
->minor_version
!= tst
->minor_version
||
430 st
->ss
->compare_super(st
, tst
) != 0) {
431 /* Some mismatch. If exactly one array matches this host,
432 * we can resolve on that one.
433 * Or, if we are auto assembling, we just ignore the second
439 int first
= st
->ss
->match_home(st
, c
->homehost
);
440 int last
= tst
->ss
->match_home(tst
, c
->homehost
);
442 (first
== 1 || last
== 1)) {
443 /* We can do something */
444 if (first
) {/* just ignore this one */
446 pr_err("%s misses out due to wrong homehost\n",
449 } else { /* reject all those sofar */
450 struct mddev_dev
*td
;
452 pr_err("%s overrides previous devices due to good homehost\n",
454 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
462 pr_err("superblock on %s doesn't match others - assembly aborted\n",
464 tst
->ss
->free_super(tst
);
465 st
->ss
->free_super(st
);
466 dev_policy_free(pol
);
467 domain_free(domains
);
473 /* Collect domain information from members only */
474 if (tmpdev
&& tmpdev
->used
== 1) {
476 pol
= devid_policy(stb
.st_rdev
);
477 domain_merge(&domains
, pol
, tst
?tst
->ss
->name
:NULL
);
479 dev_policy_free(pol
);
482 tst
->ss
->free_super(tst
);
485 /* Check if we found some imsm spares but no members */
488 memcmp(uuid_zero
, ident
->uuid
,sizeof(uuid_zero
)) == 0)) &&
490 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
491 if (tmpdev
->used
!= 3)
497 /* we need sb from one of the spares */
498 int dfd
= dev_open(tmpdev
->devname
, O_RDONLY
);
500 st
->ss
->load_super(st
, dfd
, NULL
))
507 /* Now reject spares that don't match domains of identified members */
508 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
510 if (tmpdev
->used
!= 3)
512 if (stat(tmpdev
->devname
, &stb
)< 0) {
513 pr_err("fstat failed for %s: %s\n",
514 tmpdev
->devname
, strerror(errno
));
517 struct dev_policy
*pol
= devid_policy(stb
.st_rdev
);
518 int dt
= domain_test(domains
, pol
, NULL
);
519 if (inargv
&& dt
!= 0)
520 /* take this spare as domains match
521 * if there are any */
523 else if (!inargv
&& dt
== 1)
524 /* device wasn't explicitly listed, so need
525 * explicit domain match - which we have */
528 /* if domains don't match mark as unused */
530 dev_policy_free(pol
);
533 domain_free(domains
);
535 if (st
&& st
->sb
&& content
== *contentp
)
536 st
->ss
->getinfo_super(st
, content
, NULL
);
544 int uptodate
; /* set once we decide that this device is as
545 * recent as everything else in the array.
547 int included
; /* set if the device is already in the array
548 * due to a previous '-I'
553 static int load_devices(struct devs
*devices
, char *devmap
,
554 struct mddev_ident
*ident
, struct supertype
*st
,
555 struct mddev_dev
*devlist
, struct context
*c
,
556 struct mdinfo
*content
,
557 int mdfd
, char *mddev
,
558 int *most_recentp
, int *bestcntp
, int **bestp
,
561 struct mddev_dev
*tmpdev
;
567 int most_recent
= -1;
571 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) {
572 char *devname
= tmpdev
->devname
;
576 if (tmpdev
->used
!= 1)
578 /* looks like a good enough match to update the super block if needed */
582 /* prepare useful information in info structures */
584 struct supertype
*tst
;
588 if (strcmp(c
->update
, "uuid")==0 &&
591 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
592 read(rfd
, ident
->uuid
, 16) != 16) {
593 *(__u32
*)(ident
->uuid
) = random();
594 *(__u32
*)(ident
->uuid
+1) = random();
595 *(__u32
*)(ident
->uuid
+2) = random();
596 *(__u32
*)(ident
->uuid
+3) = random();
598 if (rfd
>= 0) close(rfd
);
600 dfd
= dev_open(devname
,
601 tmpdev
->disposition
== 'I'
602 ? O_RDWR
: (O_RDWR
|O_EXCL
));
605 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
606 pr_err("cannot re-read metadata from %s - aborting\n",
615 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
617 memcpy(content
->uuid
, ident
->uuid
, 16);
618 strcpy(content
->name
, ident
->name
);
619 content
->array
.md_minor
= minor(stb2
.st_rdev
);
621 if (strcmp(c
->update
, "byteorder") == 0)
624 err
= tst
->ss
->update_super(tst
, content
, c
->update
,
630 pr_err("--update=%s not understood"
631 " for %s metadata\n",
632 c
->update
, tst
->ss
->name
);
633 tst
->ss
->free_super(tst
);
641 if (strcmp(c
->update
, "uuid")==0 &&
644 memcpy(ident
->uuid
, content
->uuid
, 16);
646 if (tst
->ss
->store_super(tst
, dfd
))
647 pr_err("Could not re-write superblock on %s.\n",
651 if (strcmp(c
->update
, "uuid")==0 &&
652 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
653 if (bitmap_update_uuid(ident
->bitmap_fd
,
655 tst
->ss
->swapuuid
) != 0)
656 pr_err("Could not update uuid on external bitmap.\n");
660 tst
->ss
->free_super(tst
);
664 struct supertype
*tst
= dup_super(st
);
666 dfd
= dev_open(devname
,
667 tmpdev
->disposition
== 'I'
668 ? O_RDWR
: (O_RDWR
|O_EXCL
));
670 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
671 pr_err("cannot re-read metadata from %s - aborting\n",
680 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
681 tst
->ss
->free_super(tst
);
688 pr_err("%s is identified as a member of %s, slot %d%s.\n",
689 devname
, mddev
, content
->disk
.raid_disk
,
690 (content
->disk
.state
& (1<<MD_DISK_REPLACEMENT
)) ? " replacement":"");
691 devices
[devcnt
].devname
= devname
;
692 devices
[devcnt
].uptodate
= 0;
693 devices
[devcnt
].included
= (tmpdev
->disposition
== 'I');
694 devices
[devcnt
].i
= *content
;
695 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
696 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
698 if (devices
[devcnt
].i
.disk
.state
== 6) {
699 if (most_recent
< 0 ||
700 devices
[devcnt
].i
.events
701 > devices
[most_recent
].i
.events
)
702 most_recent
= devcnt
;
705 if (content
->array
.level
== LEVEL_MULTIPATH
)
706 /* with multipath, the raid_disk from the superblock is meaningless */
709 i
= devices
[devcnt
].i
.disk
.raid_disk
;
711 if (nextspare
< content
->array
.raid_disks
*2)
712 nextspare
= content
->array
.raid_disks
*2;
715 /* i is raid_disk - double it so there is room for
718 if (devices
[devcnt
].i
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))
720 if (i
>= content
->array
.raid_disks
*2 &&
726 int newbestcnt
= i
+10;
727 int *newbest
= xmalloc(sizeof(int)*newbestcnt
);
729 for (c
=0; c
< newbestcnt
; c
++)
731 newbest
[c
] = best
[c
];
736 bestcnt
= newbestcnt
;
739 devices
[best
[i
]].i
.events
740 == devices
[devcnt
].i
.events
741 && (devices
[best
[i
]].i
.disk
.minor
742 != devices
[devcnt
].i
.disk
.minor
)
744 && content
->array
.level
!= LEVEL_MULTIPATH
) {
745 /* two different devices with identical superblock.
746 * Could be a mis-detection caused by overlapping
747 * partitions. fail-safe.
749 pr_err("WARNING %s and %s appear"
750 " to have very similar superblocks.\n"
751 " If they are really different, "
752 "please --zero the superblock on one\n"
753 " If they are the same or overlap,"
754 " please remove one from %s.\n",
755 devices
[best
[i
]].devname
, devname
,
756 inargv
? "the list" :
757 "the\n DEVICE list in mdadm.conf"
765 || (devices
[best
[i
]].i
.events
766 < devices
[devcnt
].i
.events
))
771 if (most_recent
>= 0)
772 *most_recentp
= most_recent
;
778 static int force_array(struct mdinfo
*content
,
779 struct devs
*devices
,
780 int *best
, int bestcnt
, char *avail
,
782 struct supertype
*st
,
786 while (!enough(content
->array
.level
, content
->array
.raid_disks
,
787 content
->array
.layout
, 1,
790 (content
->reshape_active
&& content
->delta_disks
> 0 &&
791 !enough(content
->array
.level
, (content
->array
.raid_disks
792 - content
->delta_disks
),
793 content
->new_layout
, 1,
796 /* Choose the newest best drive which is
797 * not up-to-date, update the superblock
801 struct supertype
*tst
;
802 unsigned long long current_events
;
803 int chosen_drive
= -1;
807 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
811 !devices
[j
].uptodate
&&
812 devices
[j
].i
.recovery_start
== MaxSector
&&
815 > devices
[chosen_drive
].i
.events
))
818 if (chosen_drive
< 0)
820 current_events
= devices
[chosen_drive
].i
.events
;
823 pr_err("forcing event count in %s(%d) from %d upto %d\n",
824 devices
[chosen_drive
].devname
,
825 devices
[chosen_drive
].i
.disk
.raid_disk
,
826 (int)(devices
[chosen_drive
].i
.events
),
827 (int)(devices
[most_recent
].i
.events
));
828 fd
= dev_open(devices
[chosen_drive
].devname
,
829 devices
[chosen_drive
].included
? O_RDWR
832 pr_err("Couldn't open %s for write - not updating\n",
833 devices
[chosen_drive
].devname
);
834 devices
[chosen_drive
].i
.events
= 0;
838 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
840 pr_err("RAID superblock disappeared from %s - not updating.\n",
841 devices
[chosen_drive
].devname
);
842 devices
[chosen_drive
].i
.events
= 0;
845 content
->events
= devices
[most_recent
].i
.events
;
846 tst
->ss
->update_super(tst
, content
, "force-one",
847 devices
[chosen_drive
].devname
, c
->verbose
,
850 if (tst
->ss
->store_super(tst
, fd
)) {
852 pr_err("Could not re-write superblock on %s\n",
853 devices
[chosen_drive
].devname
);
854 devices
[chosen_drive
].i
.events
= 0;
855 tst
->ss
->free_super(tst
);
859 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
860 devices
[chosen_drive
].uptodate
= 1;
861 avail
[chosen_drive
] = 1;
863 tst
->ss
->free_super(tst
);
865 /* If there are any other drives of the same vintage,
866 * add them in as well. We can't lose and we might gain
869 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
873 !devices
[j
].uptodate
&&
874 devices
[j
].i
.recovery_start
== MaxSector
&&
875 devices
[j
].i
.events
== current_events
) {
884 static int start_array(int mdfd
,
886 struct mdinfo
*content
,
887 struct supertype
*st
,
888 struct mddev_ident
*ident
,
889 int *best
, int bestcnt
,
891 struct devs
*devices
,
893 unsigned int sparecnt
,
894 unsigned int rebuilding_cnt
,
896 int clean
, char *avail
,
897 int start_partial_ok
,
904 unsigned int req_cnt
;
906 rv
= set_array_info(mdfd
, st
, content
);
908 pr_err("failed to set array info for %s: %s\n",
909 mddev
, strerror(errno
));
912 if (ident
->bitmap_fd
>= 0) {
913 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
914 pr_err("SET_BITMAP_FILE failed.\n");
917 } else if (ident
->bitmap_file
) {
918 /* From config file */
919 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
921 pr_err("Could not open bitmap file %s\n",
925 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
926 pr_err("Failed to set bitmapfile for %s\n", mddev
);
933 /* First, add the raid disks, but add the chosen one last */
934 for (i
=0; i
<= bestcnt
; i
++) {
938 if (j
== chosen_drive
)
943 if (j
>= 0 && !devices
[j
].included
) {
944 int dfd
= dev_open(devices
[j
].devname
,
947 remove_partitions(dfd
);
950 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
953 pr_err("failed to add "
958 if (i
< content
->array
.raid_disks
* 2
963 } else if (c
->verbose
> 0)
964 pr_err("added %s to %s as %d%s%s\n",
965 devices
[j
].devname
, mddev
,
966 devices
[j
].i
.disk
.raid_disk
,
967 devices
[j
].uptodate
?"":
968 " (possibly out of date)",
969 (devices
[j
].i
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))?" replacement":"");
972 pr_err("%s is already in %s as %d\n",
973 devices
[j
].devname
, mddev
,
974 devices
[j
].i
.disk
.raid_disk
);
975 } else if (c
->verbose
> 0 && i
< content
->array
.raid_disks
*2
977 pr_err("no uptodate device for slot %d of %s\n",
981 if (content
->array
.level
== LEVEL_CONTAINER
) {
982 if (c
->verbose
>= 0) {
983 pr_err("Container %s has been "
984 "assembled with %d drive%s",
985 mddev
, okcnt
+sparecnt
, okcnt
+sparecnt
==1?"":"s");
986 if (okcnt
< (unsigned)content
->array
.raid_disks
)
987 fprintf(stderr
, " (out of %d)",
988 content
->array
.raid_disks
);
989 fprintf(stderr
, "\n");
991 st
->ss
->free_super(st
);
992 sysfs_uevent(content
, "change");
996 /* Get number of in-sync devices according to the superblock.
997 * We must have this number to start the array without -s or -R
999 req_cnt
= content
->array
.working_disks
;
1001 if (c
->runstop
== 1 ||
1003 ( enough(content
->array
.level
, content
->array
.raid_disks
,
1004 content
->array
.layout
, clean
, avail
) &&
1005 (okcnt
+ rebuilding_cnt
>= req_cnt
|| start_partial_ok
)
1007 /* This array is good-to-go.
1008 * If a reshape is in progress then we might need to
1009 * continue monitoring it. In that case we start
1010 * it read-only and let the grow code make it writable.
1014 if (content
->reshape_active
&&
1015 !(content
->reshape_active
& RESHAPE_NO_BACKUP
) &&
1016 content
->delta_disks
<= 0) {
1017 if (!c
->backup_file
) {
1018 pr_err("%s: Need a backup file to complete reshape of this array.\n",
1020 pr_err("Please provided one with \"--backup-file=...\"\n");
1022 strcmp(c
->update
, "revert-reshape") == 0)
1023 pr_err("(Don't specify --update=revert-reshape again, that part succeeded.)\n");
1026 rv
= sysfs_set_str(content
, NULL
,
1027 "array_state", "readonly");
1029 rv
= Grow_continue(mdfd
, st
, content
,
1032 } else if (c
->readonly
&&
1033 sysfs_attribute_available(
1034 content
, NULL
, "array_state")) {
1035 rv
= sysfs_set_str(content
, NULL
,
1036 "array_state", "readonly");
1039 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1041 if (c
->verbose
>= 0) {
1042 pr_err("%s has been started with %d drive%s",
1043 mddev
, okcnt
, okcnt
==1?"":"s");
1044 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1045 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1047 fprintf(stderr
, "%s %d rebuilding", sparecnt
?",":" and", rebuilding_cnt
);
1049 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1050 fprintf(stderr
, ".\n");
1052 if (content
->reshape_active
&&
1053 content
->array
.level
>= 4 &&
1054 content
->array
.level
<= 6) {
1055 /* might need to increase the size
1056 * of the stripe cache - default is 256
1058 if (256 < 4 * (content
->array
.chunk_size
/4096)) {
1059 struct mdinfo
*sra
= sysfs_read(mdfd
, NULL
, 0);
1061 sysfs_set_num(sra
, NULL
,
1062 "stripe_cache_size",
1063 (4 * content
->array
.chunk_size
/ 4096) + 1);
1067 if (okcnt
< (unsigned)content
->array
.raid_disks
) {
1068 /* If any devices did not get added
1069 * because the kernel rejected them based
1070 * on event count, try adding them
1071 * again providing the action policy is
1072 * 're-add' or greater. The bitmap
1073 * might allow them to be included, or
1074 * they will become spares.
1076 for (i
= 0; i
< bestcnt
; i
++) {
1078 if (j
>= 0 && !devices
[j
].uptodate
) {
1079 if (!disk_action_allows(&devices
[j
].i
, st
->ss
->name
, act_re_add
))
1081 rv
= add_disk(mdfd
, st
, content
,
1083 if (rv
== 0 && c
->verbose
>= 0)
1084 pr_err("%s has been re-added.\n",
1085 devices
[j
].devname
);
1089 if (content
->array
.level
== 6 &&
1090 okcnt
+ 1 == (unsigned)content
->array
.raid_disks
&&
1092 struct mdinfo
*sra
= sysfs_read(mdfd
, NULL
, 0);
1094 sysfs_set_str(sra
, NULL
,
1095 "sync_action", "repair");
1100 pr_err("failed to RUN_ARRAY %s: %s\n",
1101 mddev
, strerror(errno
));
1103 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1104 content
->array
.layout
, 1, avail
))
1105 pr_err("Not enough devices to "
1106 "start the array.\n");
1107 else if (!enough(content
->array
.level
,
1108 content
->array
.raid_disks
,
1109 content
->array
.layout
, clean
,
1111 pr_err("Not enough devices to "
1112 "start the array while not clean "
1113 "- consider --force.\n");
1117 if (c
->runstop
== -1) {
1118 pr_err("%s assembled from %d drive%s",
1119 mddev
, okcnt
, okcnt
==1?"":"s");
1120 if (okcnt
!= (unsigned)content
->array
.raid_disks
)
1121 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1122 fprintf(stderr
, ", but not started.\n");
1125 if (c
->verbose
>= -1) {
1126 pr_err("%s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
1128 fprintf(stderr
, "%s %d rebuilding", sparecnt
?",":" and", rebuilding_cnt
);
1130 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1131 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1132 content
->array
.layout
, 1, avail
))
1133 fprintf(stderr
, " - not enough to start the array.\n");
1134 else if (!enough(content
->array
.level
,
1135 content
->array
.raid_disks
,
1136 content
->array
.layout
, clean
,
1138 fprintf(stderr
, " - not enough to start the "
1139 "array while not clean - consider "
1142 if (req_cnt
== (unsigned)content
->array
.raid_disks
)
1143 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1145 fprintf(stderr
, " - need %d to start", req_cnt
);
1146 fprintf(stderr
, " (use --run to insist).\n");
1152 int Assemble(struct supertype
*st
, char *mddev
,
1153 struct mddev_ident
*ident
,
1154 struct mddev_dev
*devlist
,
1158 * The task of Assemble is to find a collection of
1159 * devices that should (according to their superblocks)
1160 * form an array, and to give this collection to the MD driver.
1161 * In Linux-2.4 and later, this involves submitting a
1162 * SET_ARRAY_INFO ioctl with no arg - to prepare
1163 * the array - and then submit a number of
1164 * ADD_NEW_DISK ioctls to add disks into
1165 * the array. Finally RUN_ARRAY might
1166 * be submitted to start the array.
1168 * Much of the work of Assemble is in finding and/or
1169 * checking the disks to make sure they look right.
1171 * If mddev is not set, then scan must be set and we
1172 * read through the config file for dev+uuid mapping
1173 * We recurse, setting mddev, for each device that
1175 * - has a valid uuid (or any uuid if !uuidset)
1177 * If mddev is set, we try to determine state of md.
1178 * check version - must be at least 0.90.0
1179 * check kernel version. must be at least 2.4.
1180 * If not, we can possibly fall back on START_ARRAY
1181 * Try to GET_ARRAY_INFO.
1182 * If possible, give up
1183 * If not, try to STOP_ARRAY just to make sure
1185 * If !uuidset and scan, look in conf-file for uuid
1186 * If not found, give up
1187 * If !devlist and scan and uuidset, get list of devs from conf-file
1190 * Check superblock - discard if bad
1191 * Check uuid (set if we don't have one) - discard if no match
1192 * Check superblock similarity if we have a superblock - discard if different
1193 * Record events, devicenum
1194 * This should give us a list of devices for the array
1195 * We should collect the most recent event number
1197 * Count disks with recent enough event count
1198 * While force && !enough disks
1199 * Choose newest rejected disks, update event count
1200 * mark clean and rewrite superblock
1203 * foreach device with recent events : ADD_NEW_DISK
1204 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
1206 * Check the device numbers in superblock are right
1207 * update superblock if any changes
1214 int auto_assem
= (mddev
== NULL
&& !ident
->uuid_set
&&
1215 ident
->super_minor
== UnSet
&& ident
->name
[0] == 0
1216 && (ident
->container
== NULL
|| ident
->member
== NULL
));
1217 struct devs
*devices
;
1219 int *best
= NULL
; /* indexed by raid_disk */
1222 unsigned int okcnt
, sparecnt
, rebuilding_cnt
, replcnt
;
1225 int most_recent
= 0;
1229 int start_partial_ok
= (c
->runstop
>= 0) &&
1230 (c
->force
|| devlist
==NULL
|| auto_assem
);
1232 struct mddev_dev
*tmpdev
;
1234 struct mdinfo
*content
= NULL
;
1235 struct mdinfo
*pre_exist
= NULL
;
1238 char chosen_name
[1024];
1239 struct map_ent
*map
= NULL
;
1243 * If any subdevs are listed, then any that don't
1244 * match ident are discarded. Remainder must all match and
1246 * If no subdevs, then we scan all devices in the config file, but
1247 * there must be something in the identity
1251 ident
->uuid_set
== 0 &&
1252 (ident
->super_minor
< 0 || ident
->super_minor
== UnSet
) &&
1253 ident
->name
[0] == 0 &&
1254 (ident
->container
== NULL
|| ident
->member
== NULL
) &&
1255 ident
->devices
== NULL
) {
1256 pr_err("No identity information available for %s - cannot assemble.\n",
1257 mddev
? mddev
: "further assembly");
1261 if (devlist
== NULL
)
1262 devlist
= conf_get_devs();
1267 /* We come back here when doing auto-assembly and attempting some
1268 * set of devices failed. Those are now marked as ->used==2 and
1269 * we ignore them and try again
1271 if (!st
&& ident
->st
)
1274 pr_err("looking for devices for %s\n",
1275 mddev
? mddev
: "further assembly");
1279 st
->ignore_hw_compat
= 1;
1280 num_devs
= select_devices(devlist
, ident
, &st
, &content
, c
,
1281 inargv
, auto_assem
);
1285 if (!st
|| !st
->sb
|| !content
)
1288 /* We have a full set of devices - we now need to find the
1290 * However there is a risk that we are racing with "mdadm -I"
1291 * and the array is already partially assembled - we will have
1292 * rejected any devices already in this address.
1293 * So we take a lock on the map file - to prevent further races -
1294 * and look for the uuid in there. If found and the array is
1295 * active, we abort. If found and the array is not active
1296 * we commit to that md device and add all the contained devices
1297 * to our list. We flag them so that we don't try to re-add,
1298 * but can remove if they turn out to not be wanted.
1301 pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
1302 mp
= map_by_uuid(&map
, content
->uuid
);
1305 /* array already exists. */
1306 pre_exist
= sysfs_read(-1, mp
->devnm
, GET_LEVEL
|GET_DEVS
);
1307 if (pre_exist
->array
.level
!= UnSet
) {
1308 pr_err("Found some drive for an array that is already active: %s\n",
1310 pr_err("giving up.\n");
1313 for (dv
= pre_exist
->devs
; dv
; dv
= dv
->next
) {
1314 /* We want to add this device to our list,
1315 * but it could already be there if "mdadm -I"
1316 * started *after* we checked for O_EXCL.
1317 * If we add it to the top of the list
1318 * it will be preferred over later copies.
1320 struct mddev_dev
*newdev
;
1321 char *devname
= map_dev(dv
->disk
.major
,
1326 newdev
= xmalloc(sizeof(*newdev
));
1327 newdev
->devname
= devname
;
1328 newdev
->disposition
= 'I';
1330 newdev
->next
= devlist
;
1334 strcpy(chosen_name
, mp
->path
);
1335 if (c
->verbose
> 0 || mddev
== NULL
||
1336 strcmp(mddev
, chosen_name
) != 0)
1337 pr_err("Merging with already-assembled %s\n",
1339 mdfd
= open_dev_excl(mp
->devnm
);
1341 int trustworthy
= FOREIGN
;
1342 name
= content
->name
;
1343 switch (st
->ss
->match_home(st
, c
->homehost
)
1344 ?: st
->ss
->match_home(st
, "any")) {
1346 trustworthy
= LOCAL
;
1347 name
= strchr(content
->name
, ':');
1351 name
= content
->name
;
1355 /* If the array is listed in mdadm.conf or on
1356 * command line, then we trust the name
1357 * even if the array doesn't look local
1359 trustworthy
= LOCAL
;
1362 content
->array
.level
== LEVEL_CONTAINER
) {
1363 name
= content
->text_version
;
1364 trustworthy
= METADATA
;
1367 if (name
[0] && trustworthy
!= LOCAL
&&
1368 ! c
->require_homehost
&&
1369 conf_name_is_free(name
))
1370 trustworthy
= LOCAL
;
1372 if (trustworthy
== LOCAL
&&
1374 /* Ignore 'host:' prefix of name */
1375 name
= strchr(name
, ':')+1;
1377 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
1381 st
->ss
->free_super(st
);
1386 mddev
= chosen_name
;
1387 if (get_linux_version() < 2004000 ||
1388 md_get_version(mdfd
) < 9000) {
1389 pr_err("Assemble requires Linux 2.4 or later, and\n"
1390 " md driver version 0.90.0 or later.\n"
1391 " Upgrade your kernel or try --build\n");
1395 if (pre_exist
== NULL
) {
1396 if (mddev_busy(fd2devnm(mdfd
))) {
1397 pr_err("%s already active, cannot restart it!\n",
1399 for (tmpdev
= devlist
;
1400 tmpdev
&& tmpdev
->used
!= 1;
1401 tmpdev
= tmpdev
->next
)
1403 if (tmpdev
&& auto_assem
)
1404 pr_err("%s needed for %s...\n",
1405 mddev
, tmpdev
->devname
);
1408 st
->ss
->free_super(st
);
1413 /* just incase it was started but has no content */
1414 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1418 if (content
!= &info
) {
1419 /* This is a member of a container. Try starting the array. */
1421 err
= assemble_container_content(st
, mdfd
, content
, c
,
1427 /* Ok, no bad inconsistancy, we can try updating etc */
1428 devices
= xcalloc(num_devs
, sizeof(*devices
));
1429 devmap
= xcalloc(num_devs
, content
->array
.raid_disks
);
1430 devcnt
= load_devices(devices
, devmap
, ident
, st
, devlist
,
1431 c
, content
, mdfd
, mddev
,
1432 &most_recent
, &bestcnt
, &best
, inargv
);
1437 pr_err("no devices found for %s\n",
1440 st
->ss
->free_super(st
);
1447 if (c
->update
&& strcmp(c
->update
, "byteorder")==0)
1448 st
->minor_version
= 90;
1450 st
->ss
->getinfo_super(st
, content
, NULL
);
1451 clean
= content
->array
.state
& 1;
1453 /* now we have some devices that might be suitable.
1456 avail
= xcalloc(content
->array
.raid_disks
, 1);
1461 for (i
=0; i
< bestcnt
; i
++) {
1463 int event_margin
= 1; /* always allow a difference of '1'
1464 * like the kernel does
1466 if (j
< 0) continue;
1467 /* note: we ignore error flags in multipath arrays
1468 * as they don't make sense
1470 if (content
->array
.level
!= LEVEL_MULTIPATH
)
1471 if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_ACTIVE
))) {
1472 if (!(devices
[j
].i
.disk
.state
1473 & (1<<MD_DISK_FAULTY
))) {
1474 devices
[j
].uptodate
= 1;
1479 /* If this device thinks that 'most_recent' has failed, then
1480 * we must reject this device.
1482 if (j
!= most_recent
&& !c
->force
&&
1483 content
->array
.raid_disks
> 0 &&
1484 devices
[most_recent
].i
.disk
.raid_disk
>= 0 &&
1485 devmap
[j
* content
->array
.raid_disks
+ devices
[most_recent
].i
.disk
.raid_disk
] == 0) {
1486 if (c
->verbose
> -1)
1487 pr_err("ignoring %s as it reports %s as failed\n",
1488 devices
[j
].devname
, devices
[most_recent
].devname
);
1492 /* Require event counter to be same as, or just less than,
1493 * most recent. If it is bigger, it must be a stray spare and
1494 * should be ignored.
1496 if (devices
[j
].i
.events
+event_margin
>=
1497 devices
[most_recent
].i
.events
&&
1498 devices
[j
].i
.events
<=
1499 devices
[most_recent
].i
.events
1501 devices
[j
].uptodate
= 1;
1502 if (i
< content
->array
.raid_disks
* 2) {
1503 if (devices
[j
].i
.recovery_start
== MaxSector
||
1504 (content
->reshape_active
&&
1505 ((i
>= content
->array
.raid_disks
- content
->delta_disks
) ||
1506 (i
>= content
->array
.raid_disks
- content
->delta_disks
- 1
1507 && content
->array
.level
== 4)))) {
1521 int force_ok
= force_array(content
, devices
, best
, bestcnt
,
1522 avail
, most_recent
, st
, c
);
1527 /* Now we want to look at the superblock which the kernel will base things on
1528 * and compare the devices that we think are working with the devices that the
1529 * superblock thinks are working.
1530 * If there are differences and --force is given, then update this chosen
1534 st
->ss
->free_super(st
);
1535 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
+=2) {
1541 if (!devices
[j
].uptodate
)
1543 if (devices
[j
].i
.events
< devices
[most_recent
].i
.events
)
1546 if ((fd
=dev_open(devices
[j
].devname
,
1547 devices
[j
].included
? O_RDONLY
1548 : (O_RDONLY
|O_EXCL
)))< 0) {
1549 pr_err("Cannot open %s: %s\n",
1550 devices
[j
].devname
, strerror(errno
));
1555 if (st
->ss
->load_super(st
,fd
, NULL
)) {
1557 pr_err("RAID superblock has disappeared from %s\n",
1558 devices
[j
].devname
);
1565 if (st
->sb
== NULL
) {
1566 pr_err("No suitable drives found for %s\n", mddev
);
1571 st
->ss
->getinfo_super(st
, content
, NULL
);
1573 sysfs_init(content
, mdfd
, NULL
);
1575 for (i
=0; i
<bestcnt
; i
++) {
1577 unsigned int desired_state
;
1579 if (i
>= content
->array
.raid_disks
* 2)
1582 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_REPLACEMENT
);
1584 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
1588 if (!devices
[j
].uptodate
)
1591 devices
[j
].i
.disk
.state
= desired_state
;
1592 if (!(devices
[j
].i
.array
.state
& 1))
1595 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
1596 c
->verbose
, 0, NULL
)) {
1598 if (c
->verbose
>= 0)
1599 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
1600 j
, mddev
, devices
[j
].devname
);
1603 if (c
->verbose
>= -1)
1604 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
1605 i
, mddev
, devices
[j
].devname
);
1609 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
1610 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1615 if (c
->force
&& !clean
&&
1616 !enough(content
->array
.level
, content
->array
.raid_disks
,
1617 content
->array
.layout
, clean
,
1619 change
+= st
->ss
->update_super(st
, content
, "force-array",
1620 devices
[chosen_drive
].devname
, c
->verbose
,
1628 fd
= dev_open(devices
[chosen_drive
].devname
,
1629 devices
[chosen_drive
].included
?
1630 O_RDWR
: (O_RDWR
|O_EXCL
));
1632 pr_err("Could not open %s for write - cannot Assemble array.\n",
1633 devices
[chosen_drive
].devname
);
1638 if (st
->ss
->store_super(st
, fd
)) {
1640 pr_err("Could not re-write superblock on %s\n",
1641 devices
[chosen_drive
].devname
);
1646 if (c
->verbose
>= 0)
1647 pr_err("Marking array %s as 'clean'\n",
1652 /* If we are in the middle of a reshape we may need to restore saved data
1653 * that was moved aside due to the reshape overwriting live data
1654 * The code of doing this lives in Grow.c
1657 if (content
->reshape_active
&&
1658 !(content
->reshape_active
& RESHAPE_NO_BACKUP
)) {
1660 int *fdlist
= xmalloc(sizeof(int)* bestcnt
);
1662 pr_err(":%s has an active reshape - checking "
1663 "if critical section needs to be restored\n",
1665 enable_fds(bestcnt
/2);
1666 for (i
= 0; i
< bestcnt
/2; i
++) {
1669 fdlist
[i
] = dev_open(devices
[j
].devname
,
1671 ? O_RDWR
: (O_RDWR
|O_EXCL
));
1672 if (fdlist
[i
] < 0) {
1673 pr_err("Could not open %s for write - cannot Assemble array.\n",
1674 devices
[j
].devname
);
1682 if (st
->ss
->external
&& st
->ss
->recover_backup
)
1683 err
= st
->ss
->recover_backup(st
, content
);
1685 err
= Grow_restart(st
, content
, fdlist
, bestcnt
/2,
1686 c
->backup_file
, c
->verbose
> 0);
1687 if (err
&& c
->invalid_backup
) {
1690 " without restoring backup\n");
1696 if (fdlist
[i
]>=0) close(fdlist
[i
]);
1700 pr_err("Failed to restore critical section for reshape, sorry.\n");
1701 if (c
->backup_file
== NULL
)
1702 cont_err("Possibly you needed to specify the --backup-file\n");
1710 /* Almost ready to actually *do* something */
1711 /* First, fill in the map, so that udev can find our name
1712 * as soon as we become active.
1714 if (c
->update
&& strcmp(c
->update
, "metadata")==0) {
1715 content
->array
.major_version
= 1;
1716 content
->array
.minor_version
= 0;
1717 strcpy(content
->text_version
, "1.0");
1720 map_update(&map
, fd2devnm(mdfd
), content
->text_version
,
1721 content
->uuid
, chosen_name
);
1723 rv
= start_array(mdfd
, mddev
, content
,
1724 st
, ident
, best
, bestcnt
,
1725 chosen_drive
, devices
, okcnt
, sparecnt
,
1728 clean
, avail
, start_partial_ok
,
1731 if (rv
== 1 && !pre_exist
)
1732 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1736 wait_for(chosen_name
, mdfd
);
1740 /* There is a nasty race with 'mdadm --monitor'.
1741 * If it opens this device before we close it,
1742 * it gets an incomplete open on which IO
1743 * doesn't work and the capacity is
1745 * If we reopen (to check for layered devices)
1746 * before --monitor closes, we loose.
1748 * So: wait upto 1 second for there to be
1749 * a non-zero capacity.
1751 while (usecs
< 1000) {
1752 mdfd
= open(mddev
, O_RDONLY
);
1754 unsigned long long size
;
1755 if (get_dev_size(mdfd
, NULL
, &size
) &&
1767 /* '2' means 'OK, but not started yet' */
1768 return rv
== 2 ? 0 : rv
;
1772 int assemble_container_content(struct supertype
*st
, int mdfd
,
1773 struct mdinfo
*content
, struct context
*c
,
1774 char *chosen_name
, int *result
)
1776 struct mdinfo
*dev
, *sra
;
1777 int working
= 0, preexist
= 0;
1779 struct map_ent
*map
= NULL
;
1785 sysfs_init(content
, mdfd
, NULL
);
1787 sra
= sysfs_read(mdfd
, NULL
, GET_VERSION
);
1788 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0) {
1789 if (content
->array
.major_version
== -1 &&
1790 content
->array
.minor_version
== -2 &&
1792 content
->text_version
[0] == '/')
1793 content
->text_version
[0] = '-';
1794 if (sysfs_set_array(content
, md_get_version(mdfd
)) != 0) {
1801 /* There are two types of reshape: container wide or sub-array specific
1802 * Check if metadata requests blocking container wide reshapes
1804 start_reshape
= (content
->reshape_active
&&
1805 !((content
->reshape_active
== CONTAINER_RESHAPE
) &&
1806 (content
->array
.state
& (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))));
1808 /* Block subarray here if it is under reshape now
1809 * Do not allow for any changes in this array
1811 if (st
->ss
->external
&& content
->recovery_blocked
&& start_reshape
)
1812 block_subarray(content
);
1816 old_raid_disks
= content
->array
.raid_disks
- content
->delta_disks
;
1817 avail
= xcalloc(content
->array
.raid_disks
, 1);
1818 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
1819 if (dev
->disk
.raid_disk
>= 0)
1820 avail
[dev
->disk
.raid_disk
] = 1;
1821 if (sysfs_add_disk(content
, dev
, 1) == 0) {
1822 if (dev
->disk
.raid_disk
>= old_raid_disks
&&
1823 content
->reshape_active
)
1827 } else if (errno
== EEXIST
)
1830 if (working
+ expansion
== 0 && c
->runstop
<= 0) {
1832 return 1;/* Nothing new, don't try to start */
1834 map_update(&map
, fd2devnm(mdfd
),
1835 content
->text_version
,
1836 content
->uuid
, chosen_name
);
1839 if (enough(content
->array
.level
, content
->array
.raid_disks
,
1840 content
->array
.layout
, content
->array
.state
& 1, avail
) == 0) {
1841 if (c
->export
&& result
)
1843 else if (c
->verbose
>= 0) {
1844 pr_err("%s assembled with %d device%s",
1845 chosen_name
, preexist
+ working
,
1846 preexist
+ working
== 1 ? "":"s");
1848 fprintf(stderr
, " (%d new)", working
);
1849 fprintf(stderr
, " but not started\n");
1856 if (c
->runstop
<= 0 &&
1857 (working
+ preexist
+ expansion
) <
1858 content
->array
.working_disks
) {
1859 if (c
->export
&& result
)
1860 *result
|= INCR_UNSAFE
;
1861 else if (c
->verbose
>= 0) {
1862 pr_err("%s assembled with %d device%s",
1863 chosen_name
, preexist
+ working
,
1864 preexist
+ working
== 1 ? "":"s");
1866 fprintf(stderr
, " (%d new)", working
);
1867 fprintf(stderr
, " but not safe to start\n");
1873 if (start_reshape
) {
1874 int spare
= content
->array
.raid_disks
+ expansion
;
1875 if (restore_backup(st
, content
,
1877 spare
, c
->backup_file
, c
->verbose
) == 1)
1880 err
= sysfs_set_str(content
, NULL
,
1881 "array_state", "readonly");
1885 if (st
->ss
->external
) {
1886 if (!mdmon_running(st
->container_devnm
))
1887 start_mdmon(st
->container_devnm
);
1888 ping_monitor(st
->container_devnm
);
1889 if (mdmon_running(st
->container_devnm
) &&
1890 st
->update_tail
== NULL
)
1891 st
->update_tail
= &st
->updates
;
1894 err
= Grow_continue(mdfd
, st
, content
, c
->backup_file
,
1896 } else switch(content
->array
.level
) {
1898 case LEVEL_MULTIPATH
:
1900 err
= sysfs_set_str(content
, NULL
, "array_state",
1901 c
->readonly
? "readonly" : "active");
1904 err
= sysfs_set_str(content
, NULL
, "array_state",
1906 /* start mdmon if needed. */
1908 if (!mdmon_running(st
->container_devnm
))
1909 start_mdmon(st
->container_devnm
);
1910 ping_monitor(st
->container_devnm
);
1915 sysfs_set_safemode(content
, content
->safe_mode_delay
);
1917 /* Block subarray here if it is not reshaped now
1918 * It has be blocked a little later to allow mdmon to switch in
1921 if (st
->ss
->external
&& content
->recovery_blocked
&&
1923 block_subarray(content
);
1925 if (c
->export
&& result
) {
1929 *result
|= INCR_YES
;
1930 } else if (c
->verbose
>= 0) {
1932 pr_err("array %s now has %d device%s",
1933 chosen_name
, working
+ preexist
,
1934 working
+ preexist
== 1 ? "":"s");
1936 pr_err("Started %s with %d device%s",
1937 chosen_name
, working
+ preexist
,
1938 working
+ preexist
== 1 ? "":"s");
1940 fprintf(stderr
, " (%d new)", working
);
1942 fprintf(stderr
, " ( + %d for expansion)",
1944 fprintf(stderr
, "\n");
1947 wait_for(chosen_name
, mdfd
);
1949 /* FIXME should have an O_EXCL and wait for read-auto */