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 auto-assembly is disabled\n",
237 devname
, tst
->ss
->name
);
242 if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
244 pr_err("no recogniseable superblock on %s\n",
247 } else if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
249 pr_err("no RAID superblock on %s\n",
252 } else if (tst
->ss
->compare_super
== NULL
) {
254 pr_err("Cannot assemble %s metadata on %s\n",
255 tst
->ss
->name
, devname
);
257 } else if (auto_assem
&& st
== NULL
&&
258 !conf_test_metadata(tst
->ss
->name
, (pol
= devid_policy(stb
.st_rdev
)),
259 tst
->ss
->match_home(tst
, c
->homehost
) == 1)) {
261 pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
262 devname
, tst
->ss
->name
);
266 if (dfd
>= 0) close(dfd
);
267 if (tmpdev
->used
== 2) {
268 if (auto_assem
|| !inargv
)
269 /* Ignore unrecognised devices during auto-assembly */
271 if (ident
->uuid_set
|| ident
->name
[0] ||
272 ident
->super_minor
!= UnSet
)
273 /* Ignore unrecognised device if looking for
277 pr_err("%s has no superblock - assembly aborted\n",
280 st
->ss
->free_super(st
);
281 dev_policy_free(pol
);
282 domain_free(domains
);
286 if (found_container
) {
287 /* tmpdev is a container. We need to be either
288 * looking for a member, or auto-assembling
290 /* should be safe to try an exclusive open now, we
291 * have rejected anything that some other mdadm might
294 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
297 pr_err("%s is busy - skipping\n", devname
);
302 if (ident
->container
&& ident
->container
[0] != '/') {
307 tst
->ss
->getinfo_super(tst
, content
, NULL
);
309 if (!parse_uuid(ident
->container
, uuid
) ||
310 !same_uuid(content
->uuid
, uuid
, tst
->ss
->swapuuid
)) {
312 pr_err("%s has wrong UUID to be required container\n",
317 /* It is worth looking inside this container.
320 pr_err("looking in container %s\n",
323 for (content
= tst
->ss
->container_content(tst
, NULL
);
325 content
= content
->next
) {
327 if (!ident_matches(ident
, content
, tst
,
328 c
->homehost
, c
->update
,
329 report_mismatch
? devname
: NULL
))
330 /* message already printed */;
331 else if (is_member_busy(content
->text_version
)) {
333 pr_err("member %s in %s is already assembled\n",
334 content
->text_version
,
336 } else if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
337 /* do not assemble arrays with unsupported configurations */
338 pr_err("Cannot activate member %s in %s.\n",
339 content
->text_version
,
346 goto loop
; /* empty container */
349 st
= tst
; tst
= NULL
;
350 if (!auto_assem
&& inargv
&& tmpdev
->next
!= NULL
) {
351 pr_err("%s is a container, but is not only device given: confused and aborting\n",
353 st
->ss
->free_super(st
);
354 dev_policy_free(pol
);
355 domain_free(domains
);
359 pr_err("found match on member %s in %s\n",
360 content
->text_version
, devname
);
362 /* make sure we finished the loop */
367 tst
->ss
->getinfo_super(tst
, content
, NULL
);
369 if (!ident_matches(ident
, content
, tst
,
370 c
->homehost
, c
->update
,
371 report_mismatch
? devname
: NULL
))
375 /* Never auto-assemble things that conflict
376 * with mdadm.conf in some way
378 struct mddev_ident
*match
;
381 match
= conf_match(tst
, content
, devname
,
382 report_mismatch
? c
->verbose
: -1,
384 if (!match
&& rv
== 2)
386 if (match
&& match
->devname
&&
387 strcasecmp(match
->devname
, "<ignore>") == 0) {
389 pr_err("%s is a member of an explicitly ignored array\n",
393 if (match
&& !ident_matches(match
, content
, tst
,
394 c
->homehost
, c
->update
,
395 report_mismatch
? devname
: NULL
))
396 /* Array exists in mdadm.conf but some
397 * details don't match, so reject it
402 /* should be safe to try an exclusive open now, we
403 * have rejected anything that some other mdadm might
406 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
409 pr_err("%s is busy - skipping\n", devname
);
416 if (st
->minor_version
== -1)
417 st
->minor_version
= tst
->minor_version
;
419 if (memcmp(content
->uuid
, uuid_zero
,
420 sizeof(int[4])) == 0) {
421 /* this is a floating spare. It cannot define
422 * an array unless there are no more arrays of
423 * this type to be found. It can be included
424 * in an array of this type though.
430 if (st
->ss
!= tst
->ss
||
431 st
->minor_version
!= tst
->minor_version
||
432 st
->ss
->compare_super(st
, tst
) != 0) {
433 /* Some mismatch. If exactly one array matches this host,
434 * we can resolve on that one.
435 * Or, if we are auto assembling, we just ignore the second
441 int first
= st
->ss
->match_home(st
, c
->homehost
);
442 int last
= tst
->ss
->match_home(tst
, c
->homehost
);
444 (first
== 1 || last
== 1)) {
445 /* We can do something */
446 if (first
) {/* just ignore this one */
448 pr_err("%s misses out due to wrong homehost\n",
451 } else { /* reject all those sofar */
452 struct mddev_dev
*td
;
454 pr_err("%s overrides previous devices due to good homehost\n",
456 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
464 pr_err("superblock on %s doesn't match others - assembly aborted\n",
466 tst
->ss
->free_super(tst
);
467 st
->ss
->free_super(st
);
468 dev_policy_free(pol
);
469 domain_free(domains
);
475 /* Collect domain information from members only */
476 if (tmpdev
&& tmpdev
->used
== 1) {
478 pol
= devid_policy(stb
.st_rdev
);
479 domain_merge(&domains
, pol
, tst
?tst
->ss
->name
:NULL
);
481 dev_policy_free(pol
);
484 tst
->ss
->free_super(tst
);
487 /* Check if we found some imsm spares but no members */
490 memcmp(uuid_zero
, ident
->uuid
,sizeof(uuid_zero
)) == 0)) &&
492 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
493 if (tmpdev
->used
!= 3)
499 /* we need sb from one of the spares */
500 int dfd
= dev_open(tmpdev
->devname
, O_RDONLY
);
502 st
->ss
->load_super(st
, dfd
, NULL
))
509 /* Now reject spares that don't match domains of identified members */
510 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
512 if (tmpdev
->used
!= 3)
514 if (stat(tmpdev
->devname
, &stb
)< 0) {
515 pr_err("fstat failed for %s: %s\n",
516 tmpdev
->devname
, strerror(errno
));
519 struct dev_policy
*pol
= devid_policy(stb
.st_rdev
);
520 int dt
= domain_test(domains
, pol
, NULL
);
521 if (inargv
&& dt
!= 0)
522 /* take this spare as domains match
523 * if there are any */
525 else if (!inargv
&& dt
== 1)
526 /* device wasn't explicitly listed, so need
527 * explicit domain match - which we have */
530 /* if domains don't match mark as unused */
532 dev_policy_free(pol
);
535 domain_free(domains
);
537 if (st
&& st
->sb
&& content
== *contentp
)
538 st
->ss
->getinfo_super(st
, content
, NULL
);
546 int uptodate
; /* set once we decide that this device is as
547 * recent as everything else in the array.
549 int included
; /* set if the device is already in the array
550 * due to a previous '-I'
555 static int load_devices(struct devs
*devices
, char *devmap
,
556 struct mddev_ident
*ident
, struct supertype
**stp
,
557 struct mddev_dev
*devlist
, struct context
*c
,
558 struct mdinfo
*content
,
559 int mdfd
, char *mddev
,
560 int *most_recentp
, int *bestcntp
, int **bestp
,
563 struct mddev_dev
*tmpdev
;
569 int most_recent
= -1;
572 struct supertype
*st
= *stp
;
574 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) {
575 char *devname
= tmpdev
->devname
;
577 struct supertype
*tst
;
581 if (tmpdev
->used
!= 1)
583 /* looks like a good enough match to update the super block if needed */
586 /* prepare useful information in info structures */
591 if (strcmp(c
->update
, "uuid")==0 &&
594 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
595 read(rfd
, ident
->uuid
, 16) != 16) {
596 *(__u32
*)(ident
->uuid
) = random();
597 *(__u32
*)(ident
->uuid
+1) = random();
598 *(__u32
*)(ident
->uuid
+2) = random();
599 *(__u32
*)(ident
->uuid
+3) = random();
601 if (rfd
>= 0) close(rfd
);
603 dfd
= dev_open(devname
,
604 tmpdev
->disposition
== 'I'
605 ? O_RDWR
: (O_RDWR
|O_EXCL
));
608 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
609 pr_err("cannot re-read metadata from %s - aborting\n",
616 tst
->ss
->free_super(tst
);
621 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
623 memcpy(content
->uuid
, ident
->uuid
, 16);
624 strcpy(content
->name
, ident
->name
);
625 content
->array
.md_minor
= minor(stb2
.st_rdev
);
627 if (strcmp(c
->update
, "byteorder") == 0)
630 err
= tst
->ss
->update_super(tst
, content
, c
->update
,
636 pr_err("--update=%s not understood for %s metadata\n",
637 c
->update
, tst
->ss
->name
);
638 tst
->ss
->free_super(tst
);
647 if (strcmp(c
->update
, "uuid")==0 &&
650 memcpy(ident
->uuid
, content
->uuid
, 16);
652 if (tst
->ss
->store_super(tst
, dfd
))
653 pr_err("Could not re-write superblock on %s.\n",
656 if (strcmp(c
->update
, "uuid")==0 &&
657 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
658 if (bitmap_update_uuid(ident
->bitmap_fd
,
660 tst
->ss
->swapuuid
) != 0)
661 pr_err("Could not update uuid on external bitmap.\n");
668 dfd
= dev_open(devname
,
669 tmpdev
->disposition
== 'I'
670 ? O_RDWR
: (O_RDWR
|O_EXCL
));
673 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
674 pr_err("cannot re-read metadata from %s - aborting\n",
681 tst
->ss
->free_super(tst
);
686 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
693 pr_err("%s is identified as a member of %s, slot %d%s.\n",
694 devname
, mddev
, content
->disk
.raid_disk
,
695 (content
->disk
.state
& (1<<MD_DISK_REPLACEMENT
)) ? " replacement":"");
696 devices
[devcnt
].devname
= devname
;
697 devices
[devcnt
].uptodate
= 0;
698 devices
[devcnt
].included
= (tmpdev
->disposition
== 'I');
699 devices
[devcnt
].i
= *content
;
700 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
701 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
703 if (devices
[devcnt
].i
.disk
.state
== 6) {
704 if (most_recent
< 0 ||
705 devices
[devcnt
].i
.events
706 > devices
[most_recent
].i
.events
) {
707 struct supertype
*tmp
= tst
;
710 most_recent
= devcnt
;
713 tst
->ss
->free_super(tst
);
716 if (content
->array
.level
== LEVEL_MULTIPATH
)
717 /* with multipath, the raid_disk from the superblock is meaningless */
720 i
= devices
[devcnt
].i
.disk
.raid_disk
;
722 if (nextspare
< content
->array
.raid_disks
*2)
723 nextspare
= content
->array
.raid_disks
*2;
726 /* i is raid_disk - double it so there is room for
729 if (devices
[devcnt
].i
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))
731 if (i
>= content
->array
.raid_disks
*2 &&
737 int newbestcnt
= i
+10;
738 int *newbest
= xmalloc(sizeof(int)*newbestcnt
);
740 for (c
=0; c
< newbestcnt
; c
++)
742 newbest
[c
] = best
[c
];
747 bestcnt
= newbestcnt
;
750 devices
[best
[i
]].i
.events
751 == devices
[devcnt
].i
.events
752 && (devices
[best
[i
]].i
.disk
.minor
753 != devices
[devcnt
].i
.disk
.minor
)
755 && content
->array
.level
!= LEVEL_MULTIPATH
) {
756 /* two different devices with identical superblock.
757 * Could be a mis-detection caused by overlapping
758 * partitions. fail-safe.
760 pr_err("WARNING %s and %s appear to have very similar superblocks.\n"
761 " If they are really different, please --zero the superblock on one\n"
762 " If they are the same or overlap, please remove one from %s.\n",
763 devices
[best
[i
]].devname
, devname
,
764 inargv
? "the list" :
765 "the\n DEVICE list in mdadm.conf"
774 || (devices
[best
[i
]].i
.events
775 < devices
[devcnt
].i
.events
))
780 if (most_recent
>= 0)
781 *most_recentp
= most_recent
;
788 static int force_array(struct mdinfo
*content
,
789 struct devs
*devices
,
790 int *best
, int bestcnt
, char *avail
,
792 struct supertype
*st
,
796 while (!enough(content
->array
.level
, content
->array
.raid_disks
,
797 content
->array
.layout
, 1,
800 (content
->reshape_active
&& content
->delta_disks
> 0 &&
801 !enough(content
->array
.level
, (content
->array
.raid_disks
802 - content
->delta_disks
),
803 content
->new_layout
, 1,
806 /* Choose the newest best drive which is
807 * not up-to-date, update the superblock
811 struct supertype
*tst
;
812 unsigned long long current_events
;
813 int chosen_drive
= -1;
817 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
822 if (devices
[j
].uptodate
)
824 if (devices
[j
].i
.recovery_start
!= MaxSector
) {
826 if (!devices
[j
].i
.reshape_active
||
827 devices
[j
].i
.delta_disks
<= 0)
829 /* When increasing number of devices, an
830 * added device also appears to be
831 * recovering. It is safe to include it
832 * as long as it won't be a source of
834 * For now, just allow for last data
835 * devices in RAID4 or last devices in RAID4/5/6.
837 delta
= devices
[j
].i
.delta_disks
;
838 if (devices
[j
].i
.array
.level
>= 4 &&
839 devices
[j
].i
.array
.level
<= 6 &&
840 i
/2 >= content
->array
.raid_disks
- delta
)
842 else if (devices
[j
].i
.array
.level
== 4 &&
843 i
/2 >= content
->array
.raid_disks
- delta
- 1)
848 if (chosen_drive
< 0 ||
850 > devices
[chosen_drive
].i
.events
)
853 if (chosen_drive
< 0)
855 current_events
= devices
[chosen_drive
].i
.events
;
858 pr_err("forcing event count in %s(%d) from %d upto %d\n",
859 devices
[chosen_drive
].devname
,
860 devices
[chosen_drive
].i
.disk
.raid_disk
,
861 (int)(devices
[chosen_drive
].i
.events
),
862 (int)(devices
[most_recent
].i
.events
));
863 fd
= dev_open(devices
[chosen_drive
].devname
,
864 devices
[chosen_drive
].included
? O_RDWR
867 pr_err("Couldn't open %s for write - not updating\n",
868 devices
[chosen_drive
].devname
);
869 devices
[chosen_drive
].i
.events
= 0;
873 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
875 pr_err("RAID superblock disappeared from %s - not updating.\n",
876 devices
[chosen_drive
].devname
);
877 devices
[chosen_drive
].i
.events
= 0;
880 content
->events
= devices
[most_recent
].i
.events
;
881 tst
->ss
->update_super(tst
, content
, "force-one",
882 devices
[chosen_drive
].devname
, c
->verbose
,
885 if (tst
->ss
->store_super(tst
, fd
)) {
887 pr_err("Could not re-write superblock on %s\n",
888 devices
[chosen_drive
].devname
);
889 devices
[chosen_drive
].i
.events
= 0;
890 tst
->ss
->free_super(tst
);
894 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
895 devices
[chosen_drive
].uptodate
= 1;
896 avail
[chosen_drive
] = 1;
898 tst
->ss
->free_super(tst
);
900 /* If there are any other drives of the same vintage,
901 * add them in as well. We can't lose and we might gain
904 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
908 !devices
[j
].uptodate
&&
909 devices
[j
].i
.recovery_start
== MaxSector
&&
910 devices
[j
].i
.events
== current_events
) {
919 static int start_array(int mdfd
,
921 struct mdinfo
*content
,
922 struct supertype
*st
,
923 struct mddev_ident
*ident
,
924 int *best
, int bestcnt
,
926 struct devs
*devices
,
928 unsigned int sparecnt
,
929 unsigned int rebuilding_cnt
,
931 int clean
, char *avail
,
932 int start_partial_ok
,
939 unsigned int req_cnt
;
941 rv
= set_array_info(mdfd
, st
, content
);
943 pr_err("failed to set array info for %s: %s\n",
944 mddev
, strerror(errno
));
947 if (ident
->bitmap_fd
>= 0) {
948 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
949 pr_err("SET_BITMAP_FILE failed.\n");
952 } else if (ident
->bitmap_file
) {
953 /* From config file */
954 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
956 pr_err("Could not open bitmap file %s\n",
960 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
961 pr_err("Failed to set bitmapfile for %s\n", mddev
);
968 /* First, add the raid disks, but add the chosen one last */
969 for (i
=0; i
<= bestcnt
; i
++) {
973 if (j
== chosen_drive
)
978 if (j
>= 0 && !devices
[j
].included
) {
979 int dfd
= dev_open(devices
[j
].devname
,
982 remove_partitions(dfd
);
985 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
988 pr_err("failed to add %s to %s: %s\n",
992 if (i
< content
->array
.raid_disks
* 2
997 } else if (c
->verbose
> 0)
998 pr_err("added %s to %s as %d%s%s\n",
999 devices
[j
].devname
, mddev
,
1000 devices
[j
].i
.disk
.raid_disk
,
1001 devices
[j
].uptodate
?"":
1002 " (possibly out of date)",
1003 (devices
[j
].i
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))?" replacement":"");
1004 } else if (j
>= 0) {
1006 pr_err("%s is already in %s as %d\n",
1007 devices
[j
].devname
, mddev
,
1008 devices
[j
].i
.disk
.raid_disk
);
1009 } else if (c
->verbose
> 0 && i
< content
->array
.raid_disks
*2
1011 pr_err("no uptodate device for slot %d of %s\n",
1015 if (content
->array
.level
== LEVEL_CONTAINER
) {
1016 if (c
->verbose
>= 0) {
1017 pr_err("Container %s has been assembled with %d drive%s",
1018 mddev
, okcnt
+sparecnt
, okcnt
+sparecnt
==1?"":"s");
1019 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1020 fprintf(stderr
, " (out of %d)",
1021 content
->array
.raid_disks
);
1022 fprintf(stderr
, "\n");
1025 if (st
->ss
->validate_container
) {
1026 struct mdinfo
*devices_list
;
1027 struct mdinfo
*info_devices
= xmalloc(sizeof(struct mdinfo
)*(okcnt
+sparecnt
));
1029 devices_list
= NULL
;
1030 for (count
= 0; count
< okcnt
+sparecnt
; count
++) {
1031 info_devices
[count
] = devices
[count
].i
;
1032 info_devices
[count
].next
= devices_list
;
1033 devices_list
= &info_devices
[count
];
1035 if (st
->ss
->validate_container(devices_list
))
1036 pr_err("Mismatch detected!\n");
1040 st
->ss
->free_super(st
);
1041 sysfs_uevent(content
, "change");
1042 if (err_ok
&& okcnt
< (unsigned)content
->array
.raid_disks
)
1043 /* Was partial, is still partial, so signal an error
1044 * to ensure we don't retry */
1049 /* Get number of in-sync devices according to the superblock.
1050 * We must have this number to start the array without -s or -R
1052 req_cnt
= content
->array
.working_disks
;
1054 if (c
->runstop
== 1 ||
1056 ( enough(content
->array
.level
, content
->array
.raid_disks
,
1057 content
->array
.layout
, clean
, avail
) &&
1058 (okcnt
+ rebuilding_cnt
>= req_cnt
|| start_partial_ok
)
1060 /* This array is good-to-go.
1061 * If a reshape is in progress then we might need to
1062 * continue monitoring it. In that case we start
1063 * it read-only and let the grow code make it writable.
1067 if (content
->reshape_active
&&
1068 !(content
->reshape_active
& RESHAPE_NO_BACKUP
) &&
1069 content
->delta_disks
<= 0) {
1070 if (!c
->backup_file
) {
1071 pr_err("%s: Need a backup file to complete reshape of this array.\n",
1073 pr_err("Please provided one with \"--backup-file=...\"\n");
1075 strcmp(c
->update
, "revert-reshape") == 0)
1076 pr_err("(Don't specify --update=revert-reshape again, that part succeeded.)\n");
1079 rv
= sysfs_set_str(content
, NULL
,
1080 "array_state", "readonly");
1082 rv
= Grow_continue(mdfd
, st
, content
,
1085 } else if (c
->readonly
&&
1086 sysfs_attribute_available(
1087 content
, NULL
, "array_state")) {
1088 rv
= sysfs_set_str(content
, NULL
,
1089 "array_state", "readonly");
1092 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1093 reopen_mddev(mdfd
); /* drop O_EXCL */
1095 if (c
->verbose
>= 0) {
1096 pr_err("%s has been started with %d drive%s",
1097 mddev
, okcnt
, okcnt
==1?"":"s");
1098 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1099 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1101 fprintf(stderr
, "%s %d rebuilding", sparecnt
?",":" and", rebuilding_cnt
);
1103 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1104 fprintf(stderr
, ".\n");
1106 if (content
->reshape_active
&&
1107 content
->array
.level
>= 4 &&
1108 content
->array
.level
<= 6) {
1109 /* might need to increase the size
1110 * of the stripe cache - default is 256
1112 int chunk_size
= content
->array
.chunk_size
;
1113 if (content
->reshape_active
&&
1114 content
->new_chunk
> chunk_size
)
1115 chunk_size
= content
->new_chunk
;
1116 if (256 < 4 * ((chunk_size
+4065)/4096)) {
1117 struct mdinfo
*sra
= sysfs_read(mdfd
, NULL
, 0);
1119 sysfs_set_num(sra
, NULL
,
1120 "stripe_cache_size",
1121 (4 * chunk_size
/ 4096) + 1);
1125 if (okcnt
< (unsigned)content
->array
.raid_disks
) {
1126 /* If any devices did not get added
1127 * because the kernel rejected them based
1128 * on event count, try adding them
1129 * again providing the action policy is
1130 * 're-add' or greater. The bitmap
1131 * might allow them to be included, or
1132 * they will become spares.
1134 for (i
= 0; i
< bestcnt
; i
++) {
1136 if (j
>= 0 && !devices
[j
].uptodate
) {
1137 if (!disk_action_allows(&devices
[j
].i
, st
->ss
->name
, act_re_add
))
1139 rv
= add_disk(mdfd
, st
, content
,
1141 if (rv
== 0 && c
->verbose
>= 0)
1142 pr_err("%s has been re-added.\n",
1143 devices
[j
].devname
);
1147 if (content
->array
.level
== 6 &&
1148 okcnt
+ 1 == (unsigned)content
->array
.raid_disks
&&
1150 struct mdinfo
*sra
= sysfs_read(mdfd
, NULL
, 0);
1152 sysfs_set_str(sra
, NULL
,
1153 "sync_action", "repair");
1158 pr_err("failed to RUN_ARRAY %s: %s\n",
1159 mddev
, strerror(errno
));
1161 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1162 content
->array
.layout
, 1, avail
))
1163 pr_err("Not enough devices to start the array.\n");
1164 else if (!enough(content
->array
.level
,
1165 content
->array
.raid_disks
,
1166 content
->array
.layout
, clean
,
1168 pr_err("Not enough devices to start the array while not clean - consider --force.\n");
1172 if (c
->runstop
== -1) {
1173 pr_err("%s assembled from %d drive%s",
1174 mddev
, okcnt
, okcnt
==1?"":"s");
1175 if (okcnt
!= (unsigned)content
->array
.raid_disks
)
1176 fprintf(stderr
, " (out of %d)", content
->array
.raid_disks
);
1177 fprintf(stderr
, ", but not started.\n");
1180 if (c
->verbose
>= -1) {
1181 pr_err("%s assembled from %d drive%s", mddev
, okcnt
, okcnt
==1?"":"s");
1183 fprintf(stderr
, "%s %d rebuilding", sparecnt
?",":" and", rebuilding_cnt
);
1185 fprintf(stderr
, " and %d spare%s", sparecnt
, sparecnt
==1?"":"s");
1186 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1187 content
->array
.layout
, 1, avail
))
1188 fprintf(stderr
, " - not enough to start the array.\n");
1189 else if (!enough(content
->array
.level
,
1190 content
->array
.raid_disks
,
1191 content
->array
.layout
, clean
,
1193 fprintf(stderr
, " - not enough to start the array while not clean - consider --force.\n");
1195 if (req_cnt
== (unsigned)content
->array
.raid_disks
)
1196 fprintf(stderr
, " - need all %d to start it", req_cnt
);
1198 fprintf(stderr
, " - need %d to start", req_cnt
);
1199 fprintf(stderr
, " (use --run to insist).\n");
1205 int Assemble(struct supertype
*st
, char *mddev
,
1206 struct mddev_ident
*ident
,
1207 struct mddev_dev
*devlist
,
1211 * The task of Assemble is to find a collection of
1212 * devices that should (according to their superblocks)
1213 * form an array, and to give this collection to the MD driver.
1214 * In Linux-2.4 and later, this involves submitting a
1215 * SET_ARRAY_INFO ioctl with no arg - to prepare
1216 * the array - and then submit a number of
1217 * ADD_NEW_DISK ioctls to add disks into
1218 * the array. Finally RUN_ARRAY might
1219 * be submitted to start the array.
1221 * Much of the work of Assemble is in finding and/or
1222 * checking the disks to make sure they look right.
1224 * If mddev is not set, then scan must be set and we
1225 * read through the config file for dev+uuid mapping
1226 * We recurse, setting mddev, for each device that
1228 * - has a valid uuid (or any uuid if !uuidset)
1230 * If mddev is set, we try to determine state of md.
1231 * check version - must be at least 0.90.0
1232 * check kernel version. must be at least 2.4.
1233 * If not, we can possibly fall back on START_ARRAY
1234 * Try to GET_ARRAY_INFO.
1235 * If possible, give up
1236 * If not, try to STOP_ARRAY just to make sure
1238 * If !uuidset and scan, look in conf-file for uuid
1239 * If not found, give up
1240 * If !devlist and scan and uuidset, get list of devs from conf-file
1243 * Check superblock - discard if bad
1244 * Check uuid (set if we don't have one) - discard if no match
1245 * Check superblock similarity if we have a superblock - discard if different
1246 * Record events, devicenum
1247 * This should give us a list of devices for the array
1248 * We should collect the most recent event number
1250 * Count disks with recent enough event count
1251 * While force && !enough disks
1252 * Choose newest rejected disks, update event count
1253 * mark clean and rewrite superblock
1256 * foreach device with recent events : ADD_NEW_DISK
1257 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
1259 * Check the device numbers in superblock are right
1260 * update superblock if any changes
1267 int auto_assem
= (mddev
== NULL
&& !ident
->uuid_set
&&
1268 ident
->super_minor
== UnSet
&& ident
->name
[0] == 0
1269 && (ident
->container
== NULL
|| ident
->member
== NULL
));
1270 struct devs
*devices
;
1272 int *best
= NULL
; /* indexed by raid_disk */
1275 unsigned int okcnt
, sparecnt
, rebuilding_cnt
, replcnt
;
1278 int most_recent
= 0;
1282 int start_partial_ok
= (c
->runstop
>= 0) &&
1283 (c
->force
|| devlist
==NULL
|| auto_assem
);
1285 struct mddev_dev
*tmpdev
;
1287 struct mdinfo
*content
= NULL
;
1288 struct mdinfo
*pre_exist
= NULL
;
1291 char chosen_name
[1024];
1292 struct map_ent
*map
= NULL
;
1296 * If any subdevs are listed, then any that don't
1297 * match ident are discarded. Remainder must all match and
1299 * If no subdevs, then we scan all devices in the config file, but
1300 * there must be something in the identity
1304 ident
->uuid_set
== 0 &&
1305 (ident
->super_minor
< 0 || ident
->super_minor
== UnSet
) &&
1306 ident
->name
[0] == 0 &&
1307 (ident
->container
== NULL
|| ident
->member
== NULL
) &&
1308 ident
->devices
== NULL
) {
1309 pr_err("No identity information available for %s - cannot assemble.\n",
1310 mddev
? mddev
: "further assembly");
1314 if (devlist
== NULL
)
1315 devlist
= conf_get_devs();
1320 /* We come back here when doing auto-assembly and attempting some
1321 * set of devices failed. Those are now marked as ->used==2 and
1322 * we ignore them and try again
1324 if (!st
&& ident
->st
)
1327 pr_err("looking for devices for %s\n",
1328 mddev
? mddev
: "further assembly");
1332 st
->ignore_hw_compat
= 1;
1333 num_devs
= select_devices(devlist
, ident
, &st
, &content
, c
,
1334 inargv
, auto_assem
);
1338 if (!st
|| !st
->sb
|| !content
)
1341 /* We have a full set of devices - we now need to find the
1343 * However there is a risk that we are racing with "mdadm -I"
1344 * and the array is already partially assembled - we will have
1345 * rejected any devices already in this address.
1346 * So we take a lock on the map file - to prevent further races -
1347 * and look for the uuid in there. If found and the array is
1348 * active, we abort. If found and the array is not active
1349 * we commit to that md device and add all the contained devices
1350 * to our list. We flag them so that we don't try to re-add,
1351 * but can remove if they turn out to not be wanted.
1354 pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
1355 if (c
->update
&& strcmp(c
->update
,"uuid") == 0)
1358 mp
= map_by_uuid(&map
, content
->uuid
);
1361 /* array already exists. */
1362 pre_exist
= sysfs_read(-1, mp
->devnm
, GET_LEVEL
|GET_DEVS
);
1363 if (pre_exist
->array
.level
!= UnSet
) {
1364 pr_err("Found some drive for an array that is already active: %s\n",
1366 pr_err("giving up.\n");
1369 for (dv
= pre_exist
->devs
; dv
; dv
= dv
->next
) {
1370 /* We want to add this device to our list,
1371 * but it could already be there if "mdadm -I"
1372 * started *after* we checked for O_EXCL.
1373 * If we add it to the top of the list
1374 * it will be preferred over later copies.
1376 struct mddev_dev
*newdev
;
1377 char *devname
= map_dev(dv
->disk
.major
,
1382 newdev
= xmalloc(sizeof(*newdev
));
1383 newdev
->devname
= devname
;
1384 newdev
->disposition
= 'I';
1386 newdev
->next
= devlist
;
1390 strcpy(chosen_name
, mp
->path
);
1391 if (c
->verbose
> 0 || mddev
== NULL
||
1392 strcmp(mddev
, chosen_name
) != 0)
1393 pr_err("Merging with already-assembled %s\n",
1395 mdfd
= open_dev_excl(mp
->devnm
);
1397 int trustworthy
= FOREIGN
;
1398 name
= content
->name
;
1399 switch (st
->ss
->match_home(st
, c
->homehost
)
1400 ?: st
->ss
->match_home(st
, "any")) {
1402 trustworthy
= LOCAL
;
1403 name
= strchr(content
->name
, ':');
1407 name
= content
->name
;
1411 /* If the array is listed in mdadm.conf or on
1412 * command line, then we trust the name
1413 * even if the array doesn't look local
1415 trustworthy
= LOCAL
;
1418 content
->array
.level
== LEVEL_CONTAINER
) {
1419 name
= content
->text_version
;
1420 trustworthy
= METADATA
;
1423 if (name
[0] && trustworthy
!= LOCAL
&&
1424 ! c
->require_homehost
&&
1425 conf_name_is_free(name
))
1426 trustworthy
= LOCAL
;
1428 if (trustworthy
== LOCAL
&&
1430 /* Ignore 'host:' prefix of name */
1431 name
= strchr(name
, ':')+1;
1433 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
1437 st
->ss
->free_super(st
);
1442 mddev
= chosen_name
;
1443 if (get_linux_version() < 2004000 ||
1444 md_get_version(mdfd
) < 9000) {
1445 pr_err("Assemble requires Linux 2.4 or later, and\n"
1446 " md driver version 0.90.0 or later.\n"
1447 " Upgrade your kernel or try --build\n");
1451 if (pre_exist
== NULL
) {
1452 if (mddev_busy(fd2devnm(mdfd
))) {
1453 pr_err("%s already active, cannot restart it!\n",
1455 for (tmpdev
= devlist
;
1456 tmpdev
&& tmpdev
->used
!= 1;
1457 tmpdev
= tmpdev
->next
)
1459 if (tmpdev
&& auto_assem
)
1460 pr_err("%s needed for %s...\n",
1461 mddev
, tmpdev
->devname
);
1464 st
->ss
->free_super(st
);
1469 /* just incase it was started but has no content */
1470 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1474 if (content
!= &info
) {
1475 /* This is a member of a container. Try starting the array. */
1477 err
= assemble_container_content(st
, mdfd
, content
, c
,
1483 /* Ok, no bad inconsistancy, we can try updating etc */
1484 devices
= xcalloc(num_devs
, sizeof(*devices
));
1485 devmap
= xcalloc(num_devs
, content
->array
.raid_disks
);
1486 devcnt
= load_devices(devices
, devmap
, ident
, &st
, devlist
,
1487 c
, content
, mdfd
, mddev
,
1488 &most_recent
, &bestcnt
, &best
, inargv
);
1493 pr_err("no devices found for %s\n",
1496 st
->ss
->free_super(st
);
1503 if (c
->update
&& strcmp(c
->update
, "byteorder")==0)
1504 st
->minor_version
= 90;
1506 st
->ss
->getinfo_super(st
, content
, NULL
);
1507 clean
= content
->array
.state
& 1;
1509 /* now we have some devices that might be suitable.
1512 avail
= xcalloc(content
->array
.raid_disks
, 1);
1517 for (i
=0; i
< bestcnt
; i
++) {
1519 int event_margin
= 1; /* always allow a difference of '1'
1520 * like the kernel does
1522 if (j
< 0) continue;
1523 /* note: we ignore error flags in multipath arrays
1524 * as they don't make sense
1526 if (content
->array
.level
!= LEVEL_MULTIPATH
)
1527 if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_ACTIVE
))) {
1528 if (!(devices
[j
].i
.disk
.state
1529 & (1<<MD_DISK_FAULTY
))) {
1530 devices
[j
].uptodate
= 1;
1535 /* If this device thinks that 'most_recent' has failed, then
1536 * we must reject this device.
1538 if (j
!= most_recent
&& !c
->force
&&
1539 content
->array
.raid_disks
> 0 &&
1540 devices
[most_recent
].i
.disk
.raid_disk
>= 0 &&
1541 devmap
[j
* content
->array
.raid_disks
+ devices
[most_recent
].i
.disk
.raid_disk
] == 0) {
1542 if (c
->verbose
> -1)
1543 pr_err("ignoring %s as it reports %s as failed\n",
1544 devices
[j
].devname
, devices
[most_recent
].devname
);
1548 /* Require event counter to be same as, or just less than,
1549 * most recent. If it is bigger, it must be a stray spare and
1550 * should be ignored.
1552 if (devices
[j
].i
.events
+event_margin
>=
1553 devices
[most_recent
].i
.events
&&
1554 devices
[j
].i
.events
<=
1555 devices
[most_recent
].i
.events
1557 devices
[j
].uptodate
= 1;
1558 if (i
< content
->array
.raid_disks
* 2) {
1559 if (devices
[j
].i
.recovery_start
== MaxSector
||
1560 (content
->reshape_active
&&
1561 i
>= content
->array
.raid_disks
- content
->delta_disks
)) {
1575 int force_ok
= force_array(content
, devices
, best
, bestcnt
,
1576 avail
, most_recent
, st
, c
);
1581 /* Now we want to look at the superblock which the kernel will base things on
1582 * and compare the devices that we think are working with the devices that the
1583 * superblock thinks are working.
1584 * If there are differences and --force is given, then update this chosen
1588 st
->ss
->free_super(st
);
1589 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
+=2) {
1595 if (!devices
[j
].uptodate
)
1597 if (devices
[j
].i
.events
< devices
[most_recent
].i
.events
)
1600 if ((fd
=dev_open(devices
[j
].devname
,
1601 devices
[j
].included
? O_RDONLY
1602 : (O_RDONLY
|O_EXCL
)))< 0) {
1603 pr_err("Cannot open %s: %s\n",
1604 devices
[j
].devname
, strerror(errno
));
1609 if (st
->ss
->load_super(st
,fd
, NULL
)) {
1611 pr_err("RAID superblock has disappeared from %s\n",
1612 devices
[j
].devname
);
1619 if (st
->sb
== NULL
) {
1620 pr_err("No suitable drives found for %s\n", mddev
);
1625 st
->ss
->getinfo_super(st
, content
, NULL
);
1627 sysfs_init(content
, mdfd
, NULL
);
1629 for (i
=0; i
<bestcnt
; i
++) {
1631 unsigned int desired_state
;
1633 if (i
>= content
->array
.raid_disks
* 2)
1636 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_REPLACEMENT
);
1638 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
1642 if (!devices
[j
].uptodate
)
1645 devices
[j
].i
.disk
.state
= desired_state
;
1646 if (!(devices
[j
].i
.array
.state
& 1))
1649 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
1650 c
->verbose
, 0, NULL
)) {
1652 if (c
->verbose
>= 0)
1653 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
1654 j
, mddev
, devices
[j
].devname
);
1657 if (c
->verbose
>= -1)
1658 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
1659 i
, mddev
, devices
[j
].devname
);
1663 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
1664 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1669 if (c
->force
&& !clean
&&
1670 !enough(content
->array
.level
, content
->array
.raid_disks
,
1671 content
->array
.layout
, clean
,
1673 change
+= st
->ss
->update_super(st
, content
, "force-array",
1674 devices
[chosen_drive
].devname
, c
->verbose
,
1682 fd
= dev_open(devices
[chosen_drive
].devname
,
1683 devices
[chosen_drive
].included
?
1684 O_RDWR
: (O_RDWR
|O_EXCL
));
1686 pr_err("Could not open %s for write - cannot Assemble array.\n",
1687 devices
[chosen_drive
].devname
);
1692 if (st
->ss
->store_super(st
, fd
)) {
1694 pr_err("Could not re-write superblock on %s\n",
1695 devices
[chosen_drive
].devname
);
1700 if (c
->verbose
>= 0)
1701 pr_err("Marking array %s as 'clean'\n",
1706 /* If we are in the middle of a reshape we may need to restore saved data
1707 * that was moved aside due to the reshape overwriting live data
1708 * The code of doing this lives in Grow.c
1711 if (content
->reshape_active
&&
1712 !(content
->reshape_active
& RESHAPE_NO_BACKUP
)) {
1714 int *fdlist
= xmalloc(sizeof(int)* bestcnt
);
1716 pr_err("%s has an active reshape - checking if critical section needs to be restored\n",
1718 if (!c
->backup_file
)
1719 c
->backup_file
= locate_backup(content
->sys_name
);
1720 enable_fds(bestcnt
/2);
1721 for (i
= 0; i
< bestcnt
/2; i
++) {
1724 fdlist
[i
] = dev_open(devices
[j
].devname
,
1726 ? O_RDWR
: (O_RDWR
|O_EXCL
));
1727 if (fdlist
[i
] < 0) {
1728 pr_err("Could not open %s for write - cannot Assemble array.\n",
1729 devices
[j
].devname
);
1737 if (st
->ss
->external
&& st
->ss
->recover_backup
)
1738 err
= st
->ss
->recover_backup(st
, content
);
1740 err
= Grow_restart(st
, content
, fdlist
, bestcnt
/2,
1741 c
->backup_file
, c
->verbose
> 0);
1742 if (err
&& c
->invalid_backup
) {
1744 pr_err("continuing without restoring backup\n");
1750 if (fdlist
[i
]>=0) close(fdlist
[i
]);
1754 pr_err("Failed to restore critical section for reshape, sorry.\n");
1755 if (c
->backup_file
== NULL
)
1756 cont_err("Possibly you needed to specify the --backup-file\n");
1764 /* Almost ready to actually *do* something */
1765 /* First, fill in the map, so that udev can find our name
1766 * as soon as we become active.
1768 if (c
->update
&& strcmp(c
->update
, "metadata")==0) {
1769 content
->array
.major_version
= 1;
1770 content
->array
.minor_version
= 0;
1771 strcpy(content
->text_version
, "1.0");
1774 map_update(&map
, fd2devnm(mdfd
), content
->text_version
,
1775 content
->uuid
, chosen_name
);
1777 rv
= start_array(mdfd
, mddev
, content
,
1778 st
, ident
, best
, bestcnt
,
1779 chosen_drive
, devices
, okcnt
, sparecnt
,
1782 clean
, avail
, start_partial_ok
,
1785 if (rv
== 1 && !pre_exist
)
1786 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1790 wait_for(chosen_name
, mdfd
);
1794 /* There is a nasty race with 'mdadm --monitor'.
1795 * If it opens this device before we close it,
1796 * it gets an incomplete open on which IO
1797 * doesn't work and the capacity is
1799 * If we reopen (to check for layered devices)
1800 * before --monitor closes, we loose.
1802 * So: wait upto 1 second for there to be
1803 * a non-zero capacity.
1805 while (usecs
< 1000) {
1806 mdfd
= open(mddev
, O_RDONLY
);
1808 unsigned long long size
;
1809 if (get_dev_size(mdfd
, NULL
, &size
) &&
1821 /* '2' means 'OK, but not started yet' */
1822 return rv
== 2 ? 0 : rv
;
1826 int assemble_container_content(struct supertype
*st
, int mdfd
,
1827 struct mdinfo
*content
, struct context
*c
,
1828 char *chosen_name
, int *result
)
1830 struct mdinfo
*dev
, *sra
, *dev2
;
1831 int working
= 0, preexist
= 0;
1833 struct map_ent
*map
= NULL
;
1839 sysfs_init(content
, mdfd
, NULL
);
1841 sra
= sysfs_read(mdfd
, NULL
, GET_VERSION
|GET_DEVS
);
1842 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0) {
1843 if (content
->array
.major_version
== -1 &&
1844 content
->array
.minor_version
== -2 &&
1846 content
->text_version
[0] == '/')
1847 content
->text_version
[0] = '-';
1848 if (sysfs_set_array(content
, md_get_version(mdfd
)) != 0) {
1855 /* There are two types of reshape: container wide or sub-array specific
1856 * Check if metadata requests blocking container wide reshapes
1858 start_reshape
= (content
->reshape_active
&&
1859 !((content
->reshape_active
== CONTAINER_RESHAPE
) &&
1860 (content
->array
.state
& (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))));
1862 /* Block subarray here if it is under reshape now
1863 * Do not allow for any changes in this array
1865 if (st
->ss
->external
&& content
->recovery_blocked
&& start_reshape
)
1866 block_subarray(content
);
1868 for (dev2
= sra
->devs
; dev2
; dev2
= dev2
->next
) {
1869 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
1870 if (dev2
->disk
.major
== dev
->disk
.major
&&
1871 dev2
->disk
.minor
== dev
->disk
.minor
)
1875 /* Don't want this one any more */
1876 if (sysfs_set_str(sra
, dev2
, "slot", "none") < 0 &&
1878 pr_err("Cannot remove old device %s: not updating %s\n", dev2
->sys_name
, sra
->sys_name
);
1882 sysfs_set_str(sra
, dev2
, "state", "remove");
1884 old_raid_disks
= content
->array
.raid_disks
- content
->delta_disks
;
1885 avail
= xcalloc(content
->array
.raid_disks
, 1);
1886 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
1887 if (dev
->disk
.raid_disk
>= 0)
1888 avail
[dev
->disk
.raid_disk
] = 1;
1889 if (sysfs_add_disk(content
, dev
, 1) == 0) {
1890 if (dev
->disk
.raid_disk
>= old_raid_disks
&&
1891 content
->reshape_active
)
1895 } else if (errno
== EEXIST
)
1899 if (working
+ expansion
== 0 && c
->runstop
<= 0) {
1901 return 1;/* Nothing new, don't try to start */
1903 map_update(&map
, fd2devnm(mdfd
),
1904 content
->text_version
,
1905 content
->uuid
, chosen_name
);
1908 if (enough(content
->array
.level
, content
->array
.raid_disks
,
1909 content
->array
.layout
, content
->array
.state
& 1, avail
) == 0) {
1910 if (c
->export
&& result
)
1912 else if (c
->verbose
>= 0) {
1913 pr_err("%s assembled with %d device%s",
1914 chosen_name
, preexist
+ working
,
1915 preexist
+ working
== 1 ? "":"s");
1917 fprintf(stderr
, " (%d new)", working
);
1918 fprintf(stderr
, " but not started\n");
1925 if (c
->runstop
<= 0 &&
1926 (working
+ preexist
+ expansion
) <
1927 content
->array
.working_disks
) {
1928 if (c
->export
&& result
)
1929 *result
|= INCR_UNSAFE
;
1930 else if (c
->verbose
>= 0) {
1931 pr_err("%s assembled with %d device%s",
1932 chosen_name
, preexist
+ working
,
1933 preexist
+ working
== 1 ? "":"s");
1935 fprintf(stderr
, " (%d new)", working
);
1936 fprintf(stderr
, " but not safe to start\n");
1942 if (start_reshape
) {
1943 int spare
= content
->array
.raid_disks
+ expansion
;
1944 if (restore_backup(st
, content
,
1946 spare
, &c
->backup_file
, c
->verbose
) == 1)
1949 err
= sysfs_set_str(content
, NULL
,
1950 "array_state", "readonly");
1954 if (st
->ss
->external
) {
1955 if (!mdmon_running(st
->container_devnm
))
1956 start_mdmon(st
->container_devnm
);
1957 ping_monitor(st
->container_devnm
);
1958 if (mdmon_running(st
->container_devnm
) &&
1959 st
->update_tail
== NULL
)
1960 st
->update_tail
= &st
->updates
;
1963 err
= Grow_continue(mdfd
, st
, content
, c
->backup_file
,
1964 0, c
->freeze_reshape
);
1965 } else switch(content
->array
.level
) {
1967 case LEVEL_MULTIPATH
:
1969 err
= sysfs_set_str(content
, NULL
, "array_state",
1970 c
->readonly
? "readonly" : "active");
1973 err
= sysfs_set_str(content
, NULL
, "array_state",
1975 /* start mdmon if needed. */
1977 if (!mdmon_running(st
->container_devnm
))
1978 start_mdmon(st
->container_devnm
);
1979 ping_monitor(st
->container_devnm
);
1984 sysfs_set_safemode(content
, content
->safe_mode_delay
);
1986 /* Block subarray here if it is not reshaped now
1987 * It has be blocked a little later to allow mdmon to switch in
1990 if (st
->ss
->external
&& content
->recovery_blocked
&&
1992 block_subarray(content
);
1994 if (c
->export
&& result
) {
1998 *result
|= INCR_YES
;
1999 } else if (c
->verbose
>= 0) {
2001 pr_err("array %s now has %d device%s",
2002 chosen_name
, working
+ preexist
,
2003 working
+ preexist
== 1 ? "":"s");
2005 pr_err("Started %s with %d device%s",
2006 chosen_name
, working
+ preexist
,
2007 working
+ preexist
== 1 ? "":"s");
2009 fprintf(stderr
, " (%d new)", working
);
2011 fprintf(stderr
, " ( + %d for expansion)",
2013 fprintf(stderr
, "\n");
2016 wait_for(chosen_name
, mdfd
);
2018 /* FIXME should have an O_EXCL and wait for read-auto */