2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2016 Neil Brown <neilb@suse.com>
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 mapping_t assemble_statuses
[] = {
29 { "but cannot be started", INCR_NO
},
30 { "but not safe to start", INCR_UNSAFE
},
31 { "and started", INCR_YES
},
32 { NULL
, INCR_ALREADY
}
37 * struct assembly_array_info - General, meaningful information for assembly.
39 * @new_cnt: Count of drives known to be members, recently added.
40 * @preexist_cnt: Count of member drives in pre-assembled array.
41 * @exp_cnt: Count of known expansion targets.
43 * FIXME: @exp_new_cnt for recently added expansion targets.
45 struct assembly_array_info
{
53 * set_array_assembly_status() - generate status of assembly for an array.
54 * @c: Global settings.
55 * @result: Pointer to status mask.
56 * @status: Status to be set/printed.
57 * @arr: Array information.
59 * Print status message to user or set it in @result if it is not NULL.
61 static void set_array_assembly_status(struct context
*c
,
62 int *result
, int status
,
63 struct assembly_array_info
*arr
)
65 int raid_disks
= arr
->preexist_cnt
+ arr
->new_cnt
;
66 char *status_msg
= map_num_s(assemble_statuses
, status
);
68 if (c
->export
&& result
)
71 if (c
->export
|| c
->verbose
< 0)
74 pr_err("%s has been assembled with %d device%s", arr
->name
,
75 raid_disks
, raid_disks
== 1 ? "":"s");
76 if (arr
->preexist_cnt
> 0)
77 fprintf(stderr
, " (%d new)", arr
->new_cnt
);
79 fprintf(stderr
, " ( + %d for expansion)", arr
->exp_cnt
);
80 fprintf(stderr
, " %s.\n", status_msg
);
83 static int name_matches(char *found
, char *required
, char *homehost
, int require_homehost
)
85 /* See if the name found matches the required name, possibly
86 * prefixed with 'homehost'
91 if (strcmp(found
, required
)==0)
93 sep
= strchr(found
, ':');
97 if (strncmp(found
, "any:", 4) == 0 ||
98 (homehost
&& strcmp(homehost
, "any") == 0) ||
100 (homehost
&& strlen(homehost
) == l
&&
101 strncmp(found
, homehost
, l
) == 0)) {
102 /* matching homehost */
103 if (strcmp(sep
+1, required
) == 0)
109 static int is_member_busy(char *metadata_version
)
111 /* check if the given member array is active */
112 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
113 struct mdstat_ent
*ent
;
116 for (ent
= mdstat
; ent
; ent
= ent
->next
) {
117 if (ent
->metadata_version
== NULL
)
119 if (strncmp(ent
->metadata_version
, "external:", 9) != 0)
121 if (!is_subarray(&ent
->metadata_version
[9]))
123 /* Skip first char - it can be '/' or '-' */
124 if (strcmp(&ent
->metadata_version
[10], metadata_version
+1) == 0) {
134 static int ident_matches(struct mddev_ident
*ident
,
135 struct mdinfo
*content
,
136 struct supertype
*tst
,
137 char *homehost
, int require_homehost
,
138 char *update
, char *devname
)
141 if (ident
->uuid_set
&& (!update
|| strcmp(update
, "uuid")!= 0) &&
142 same_uuid(content
->uuid
, ident
->uuid
, tst
->ss
->swapuuid
)==0 &&
143 memcmp(content
->uuid
, uuid_zero
, sizeof(int[4])) != 0) {
145 pr_err("%s has wrong uuid.\n", devname
);
148 if (ident
->name
[0] && (!update
|| strcmp(update
, "name")!= 0) &&
149 name_matches(content
->name
, ident
->name
, homehost
, require_homehost
)==0) {
151 pr_err("%s has wrong name.\n", devname
);
154 if (ident
->super_minor
!= UnSet
&&
155 ident
->super_minor
!= content
->array
.md_minor
) {
157 pr_err("%s has wrong super-minor.\n",
161 if (ident
->level
!= UnSet
&&
162 ident
->level
!= content
->array
.level
) {
164 pr_err("%s has wrong raid level.\n",
168 if (ident
->raid_disks
!= UnSet
&&
169 content
->array
.raid_disks
!= 0 && /* metadata doesn't know how many to expect */
170 ident
->raid_disks
!= content
->array
.raid_disks
) {
172 pr_err("%s requires wrong number of drives.\n",
176 if (ident
->member
&& ident
->member
[0]) {
177 /* content->text_version must match */
178 char *s
= strchr(content
->text_version
+1, '/');
181 pr_err("%s is not a container and one is required.\n",
184 } else if (strcmp(ident
->member
, s
+1) != 0) {
186 pr_err("skipping wrong member %s is %s\n",
187 content
->text_version
, devname
);
194 static int select_devices(struct mddev_dev
*devlist
,
195 struct mddev_ident
*ident
,
196 struct supertype
**stp
,
197 struct mdinfo
**contentp
,
199 int inargv
, int auto_assem
)
201 struct mddev_dev
*tmpdev
;
203 struct supertype
*st
= *stp
;
204 struct mdinfo
*content
= NULL
;
205 int report_mismatch
= ((inargv
&& c
->verbose
>= 0) || c
->verbose
> 0);
206 struct domainlist
*domains
= NULL
;
209 tmpdev
= devlist
; num_devs
= 0;
215 tmpdev
->disposition
= 0;
216 tmpdev
= tmpdev
->next
;
219 /* first walk the list of devices to find a consistent set
220 * that match the criterea, if that is possible.
221 * We flag the ones we like with 'used'.
223 for (tmpdev
= devlist
;
225 tmpdev
= tmpdev
? tmpdev
->next
: NULL
) {
226 char *devname
= tmpdev
->devname
;
228 struct supertype
*tst
;
229 struct dev_policy
*pol
= NULL
;
230 int found_container
= 0;
232 if (tmpdev
->used
> 1)
235 if (ident
->container
) {
236 if (ident
->container
[0] == '/' &&
237 !same_dev(ident
->container
, devname
)) {
239 pr_err("%s is not the container required (%s)\n",
240 devname
, ident
->container
);
243 } else if (ident
->devices
&&
244 !match_oneof(ident
->devices
, devname
)) {
245 /* Note that we ignore the "device=" identifier if a
246 * "container=" is given. Checking both is unnecessarily
250 pr_err("%s is not one of %s\n", devname
, ident
->devices
);
256 dfd
= dev_open(devname
, O_RDONLY
);
259 pr_err("cannot open device %s: %s\n",
260 devname
, strerror(errno
));
262 } else if (!fstat_is_blkdev(dfd
, devname
, &rdev
)) {
264 } else if (must_be_container(dfd
)) {
266 /* already found some components, this cannot
270 pr_err("%s is a container, but we are looking for components\n",
273 } if (!tst
&& (tst
= super_by_fd(dfd
, NULL
)) == NULL
) {
275 pr_err("not a recognisable container: %s\n",
278 } else if (!tst
->ss
->load_container
||
279 tst
->ss
->load_container(tst
, dfd
, NULL
)) {
281 pr_err("no correct container type: %s\n",
284 } else if (auto_assem
&&
285 !conf_test_metadata(tst
->ss
->name
,
286 (pol
= devid_policy(rdev
)),
287 tst
->ss
->match_home(tst
, c
->homehost
) == 1)) {
289 pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
290 devname
, tst
->ss
->name
);
295 if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
297 pr_err("no recogniseable superblock on %s\n",
300 } else if ((tst
->ignore_hw_compat
= 0),
301 tst
->ss
->load_super(tst
, dfd
,
302 report_mismatch
? devname
: NULL
)) {
304 pr_err("no RAID superblock on %s\n",
307 } else if (tst
->ss
->compare_super
== NULL
) {
309 pr_err("Cannot assemble %s metadata on %s\n",
310 tst
->ss
->name
, devname
);
312 } else if (auto_assem
&& st
== NULL
&&
313 !conf_test_metadata(tst
->ss
->name
,
314 (pol
= devid_policy(rdev
)),
315 tst
->ss
->match_home(tst
, c
->homehost
) == 1)) {
317 pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
318 devname
, tst
->ss
->name
);
322 if (dfd
>= 0) close(dfd
);
323 if (tmpdev
->used
== 2) {
324 if (auto_assem
|| !inargv
)
325 /* Ignore unrecognised devices during auto-assembly */
327 if (ident
->name
[0] ||
328 ident
->super_minor
!= UnSet
)
329 /* Ignore unrecognised device if looking for
333 /* ignore unrecognized device if looking for
338 pr_err("%s has no superblock - assembly aborted\n",
341 st
->ss
->free_super(st
);
342 dev_policy_free(pol
);
343 domain_free(domains
);
345 tst
->ss
->free_super(tst
);
349 if (found_container
) {
350 /* tmpdev is a container. We need to be either
351 * looking for a member, or auto-assembling
353 /* should be safe to try an exclusive open now, we
354 * have rejected anything that some other mdadm might
357 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
360 pr_err("%s is busy - skipping\n", devname
);
365 if (ident
->container
&& ident
->container
[0] != '/') {
370 tst
->ss
->getinfo_super(tst
, content
, NULL
);
372 if (!parse_uuid(ident
->container
, uuid
) ||
373 !same_uuid(content
->uuid
, uuid
, tst
->ss
->swapuuid
)) {
375 pr_err("%s has wrong UUID to be required container\n",
380 /* It is worth looking inside this container.
383 pr_err("looking in container %s\n",
386 for (content
= tst
->ss
->container_content(tst
, NULL
);
388 content
= content
->next
) {
390 if (!ident_matches(ident
, content
, tst
,
391 c
->homehost
, c
->require_homehost
,
393 report_mismatch
? devname
: NULL
))
394 /* message already printed */;
395 else if (is_member_busy(content
->text_version
)) {
397 pr_err("member %s in %s is already assembled\n",
398 content
->text_version
,
400 } else if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
401 /* do not assemble arrays with unsupported configurations */
402 pr_err("Cannot activate member %s in %s.\n",
403 content
->text_version
,
410 goto loop
; /* empty container */
413 st
= tst
; tst
= NULL
;
414 if (!auto_assem
&& inargv
&& tmpdev
->next
!= NULL
) {
415 pr_err("%s is a container, but is not only device given: confused and aborting\n",
417 st
->ss
->free_super(st
);
418 dev_policy_free(pol
);
419 domain_free(domains
);
423 pr_err("found match on member %s in %s\n",
424 content
->text_version
, devname
);
426 /* make sure we finished the loop */
431 tst
->ss
->getinfo_super(tst
, content
, NULL
);
433 if (!ident_matches(ident
, content
, tst
,
434 c
->homehost
, c
->require_homehost
,
436 report_mismatch
? devname
: NULL
))
440 /* Never auto-assemble things that conflict
441 * with mdadm.conf in some way
443 struct mddev_ident
*match
;
446 match
= conf_match(tst
, content
, devname
,
447 report_mismatch
? c
->verbose
: -1,
449 if (!match
&& rv
== 2)
451 if (match
&& match
->devname
&&
452 strcasecmp(match
->devname
, "<ignore>") == 0) {
454 pr_err("%s is a member of an explicitly ignored array\n",
458 if (match
&& !ident_matches(match
, content
, tst
,
459 c
->homehost
, c
->require_homehost
,
461 report_mismatch
? devname
: NULL
))
462 /* Array exists in mdadm.conf but some
463 * details don't match, so reject it
468 /* should be safe to try an exclusive open now, we
469 * have rejected anything that some other mdadm might
472 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
475 pr_err("%s is busy - skipping\n", devname
);
482 if (st
->minor_version
== -1)
483 st
->minor_version
= tst
->minor_version
;
485 if (memcmp(content
->uuid
, uuid_zero
,
486 sizeof(int[4])) == 0) {
487 /* this is a floating spare. It cannot define
488 * an array unless there are no more arrays of
489 * this type to be found. It can be included
490 * in an array of this type though.
496 if (st
->ss
!= tst
->ss
||
497 st
->minor_version
!= tst
->minor_version
||
498 st
->ss
->compare_super(st
, tst
, 1) != 0) {
499 /* Some mismatch. If exactly one array matches this host,
500 * we can resolve on that one.
501 * Or, if we are auto assembling, we just ignore the second
507 int first
= st
->ss
->match_home(st
, c
->homehost
);
508 int last
= tst
->ss
->match_home(tst
, c
->homehost
);
510 (first
== 1 || last
== 1)) {
511 /* We can do something */
512 if (first
) {/* just ignore this one */
514 pr_err("%s misses out due to wrong homehost\n",
517 } else { /* reject all those sofar */
518 struct mddev_dev
*td
;
520 pr_err("%s overrides previous devices due to good homehost\n",
522 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
530 pr_err("superblock on %s doesn't match others - assembly aborted\n",
532 tst
->ss
->free_super(tst
);
533 st
->ss
->free_super(st
);
534 dev_policy_free(pol
);
535 domain_free(domains
);
541 /* Collect domain information from members only */
542 if (tmpdev
&& tmpdev
->used
== 1) {
544 pol
= devid_policy(rdev
);
545 domain_merge(&domains
, pol
, tst
?tst
->ss
->name
:NULL
);
547 dev_policy_free(pol
);
550 tst
->ss
->free_super(tst
);
553 /* Check if we found some imsm spares but no members */
556 memcmp(uuid_zero
, ident
->uuid
,sizeof(uuid_zero
)) == 0)) &&
558 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
559 if (tmpdev
->used
!= 3)
565 /* we need sb from one of the spares */
566 int dfd
= dev_open(tmpdev
->devname
, O_RDONLY
);
568 st
->ss
->load_super(st
, dfd
, NULL
))
574 /* Now reject spares that don't match domains of identified members */
575 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
576 if (tmpdev
->used
!= 3)
578 if (!stat_is_blkdev(tmpdev
->devname
, &rdev
)) {
581 struct dev_policy
*pol
= devid_policy(rdev
);
582 int dt
= domain_test(domains
, pol
, NULL
);
583 if (inargv
&& dt
!= 0)
584 /* take this spare as domains match
585 * if there are any */
587 else if (!inargv
&& dt
== 1)
588 /* device wasn't explicitly listed, so need
589 * explicit domain match - which we have */
592 /* if domains don't match mark as unused */
594 dev_policy_free(pol
);
597 domain_free(domains
);
599 if (st
&& st
->sb
&& content
== *contentp
)
600 st
->ss
->getinfo_super(st
, content
, NULL
);
608 int uptodate
; /* set once we decide that this device is as
609 * recent as everything else in the array.
611 int included
; /* set if the device is already in the array
612 * due to a previous '-I'
617 static int load_devices(struct devs
*devices
, char *devmap
,
618 struct mddev_ident
*ident
, struct supertype
**stp
,
619 struct mddev_dev
*devlist
, struct context
*c
,
620 struct mdinfo
*content
,
621 int mdfd
, char *mddev
,
622 int *most_recentp
, int *bestcntp
, int **bestp
,
625 struct mddev_dev
*tmpdev
;
629 int most_recent
= -1;
632 struct supertype
*st
= *stp
;
634 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) {
635 char *devname
= tmpdev
->devname
;
637 struct supertype
*tst
;
642 if (tmpdev
->used
!= 1)
644 /* looks like a good enough match to update the super block if needed */
646 /* prepare useful information in info structures */
651 if (strcmp(c
->update
, "uuid") == 0 && !ident
->uuid_set
)
652 random_uuid((__u8
*)ident
->uuid
);
654 if (strcmp(c
->update
, "ppl") == 0 &&
655 ident
->bitmap_fd
>= 0) {
656 pr_err("PPL is not compatible with bitmap\n");
663 dfd
= dev_open(devname
,
664 tmpdev
->disposition
== 'I'
665 ? O_RDWR
: (O_RDWR
|O_EXCL
));
668 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
669 pr_err("cannot re-read metadata from %s - aborting\n",
676 tst
->ss
->free_super(tst
);
681 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
683 memcpy(content
->uuid
, ident
->uuid
, 16);
684 strcpy(content
->name
, ident
->name
);
685 content
->array
.md_minor
= minor(stb2
.st_rdev
);
687 if (strcmp(c
->update
, "byteorder") == 0)
689 else if (strcmp(c
->update
, "home-cluster") == 0) {
690 tst
->cluster_name
= c
->homecluster
;
691 err
= tst
->ss
->write_bitmap(tst
, dfd
, NameUpdate
);
692 } else if (strcmp(c
->update
, "nodes") == 0) {
693 tst
->nodes
= c
->nodes
;
694 err
= tst
->ss
->write_bitmap(tst
, dfd
, NodeNumUpdate
);
695 } else if (strcmp(c
->update
, "revert-reshape") == 0 &&
697 err
= tst
->ss
->update_super(tst
, content
,
698 "revert-reshape-nobackup",
703 err
= tst
->ss
->update_super(tst
, content
, c
->update
,
709 pr_err("--update=%s not understood for %s metadata\n",
710 c
->update
, tst
->ss
->name
);
711 tst
->ss
->free_super(tst
);
720 if (strcmp(c
->update
, "uuid")==0 &&
723 memcpy(ident
->uuid
, content
->uuid
, 16);
725 if (tst
->ss
->store_super(tst
, dfd
))
726 pr_err("Could not re-write superblock on %s.\n",
729 if (strcmp(c
->update
, "uuid")==0 &&
730 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
731 if (bitmap_update_uuid(ident
->bitmap_fd
,
733 tst
->ss
->swapuuid
) != 0)
734 pr_err("Could not update uuid on external bitmap.\n");
739 dfd
= dev_open(devname
,
740 tmpdev
->disposition
== 'I'
741 ? O_RDWR
: (O_RDWR
|O_EXCL
));
744 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
745 pr_err("cannot re-read metadata from %s - aborting\n",
752 tst
->ss
->free_super(tst
);
757 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
764 pr_err("%s is identified as a member of %s, slot %d%s.\n",
765 devname
, mddev
, content
->disk
.raid_disk
,
766 (content
->disk
.state
& (1<<MD_DISK_REPLACEMENT
)) ? " replacement":"");
767 devices
[devcnt
].devname
= devname
;
768 devices
[devcnt
].uptodate
= 0;
769 devices
[devcnt
].included
= (tmpdev
->disposition
== 'I');
770 devices
[devcnt
].i
= *content
;
771 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
772 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
774 disk_state
= devices
[devcnt
].i
.disk
.state
& ~((1<<MD_DISK_FAILFAST
) |
775 (1<<MD_DISK_WRITEMOSTLY
));
776 if (disk_state
== ((1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
))) {
777 if (most_recent
< 0 ||
778 devices
[devcnt
].i
.events
779 > devices
[most_recent
].i
.events
) {
780 struct supertype
*tmp
= tst
;
783 most_recent
= devcnt
;
786 tst
->ss
->free_super(tst
);
789 if (content
->array
.level
== LEVEL_MULTIPATH
)
790 /* with multipath, the raid_disk from the superblock is meaningless */
793 i
= devices
[devcnt
].i
.disk
.raid_disk
;
794 if (i
+1 == 0 || i
== MD_DISK_ROLE_JOURNAL
) {
795 if (nextspare
< content
->array
.raid_disks
*2)
796 nextspare
= content
->array
.raid_disks
*2;
799 /* i is raid_disk - double it so there is room for
802 if (devices
[devcnt
].i
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))
804 if (i
>= content
->array
.raid_disks
*2 &&
810 int newbestcnt
= i
+10;
811 int *newbest
= xmalloc(sizeof(int)*newbestcnt
);
813 for (c
=0; c
< newbestcnt
; c
++)
815 newbest
[c
] = best
[c
];
820 bestcnt
= newbestcnt
;
823 devices
[best
[i
]].i
.events
==
824 devices
[devcnt
].i
.events
&&
825 (devices
[best
[i
]].i
.disk
.minor
!=
826 devices
[devcnt
].i
.disk
.minor
) &&
828 content
->array
.level
!= LEVEL_MULTIPATH
) {
829 /* two different devices with identical superblock.
830 * Could be a mis-detection caused by overlapping
831 * partitions. fail-safe.
833 pr_err("WARNING %s and %s appear to have very similar superblocks.\n"
834 " If they are really different, please --zero the superblock on one\n"
835 " If they are the same or overlap, please remove one from %s.\n",
836 devices
[best
[i
]].devname
, devname
,
837 inargv
? "the list" :
838 "the\n DEVICE list in mdadm.conf"
846 if (best
[i
] == -1 || (devices
[best
[i
]].i
.events
847 < devices
[devcnt
].i
.events
))
849 else if (st
->ss
== &super_imsm
)
854 if (most_recent
>= 0)
855 *most_recentp
= most_recent
;
862 static int force_array(struct mdinfo
*content
,
863 struct devs
*devices
,
864 int *best
, int bestcnt
, char *avail
,
866 struct supertype
*st
,
870 while (!enough(content
->array
.level
, content
->array
.raid_disks
,
871 content
->array
.layout
, 1,
873 (content
->reshape_active
&& content
->delta_disks
> 0 &&
874 !enough(content
->array
.level
, (content
->array
.raid_disks
875 - content
->delta_disks
),
876 content
->new_layout
, 1, avail
))) {
877 /* Choose the newest best drive which is
878 * not up-to-date, update the superblock
882 struct supertype
*tst
;
883 unsigned long long current_events
;
884 int chosen_drive
= -1;
888 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
893 if (devices
[j
].uptodate
)
895 if (devices
[j
].i
.recovery_start
!= MaxSector
) {
897 if (!devices
[j
].i
.reshape_active
||
898 devices
[j
].i
.delta_disks
<= 0)
900 /* When increasing number of devices, an
901 * added device also appears to be
902 * recovering. It is safe to include it
903 * as long as it won't be a source of
905 * For now, just allow for last data
906 * devices in RAID4 or last devices in RAID4/5/6.
908 delta
= devices
[j
].i
.delta_disks
;
909 if (devices
[j
].i
.array
.level
>= 4 &&
910 devices
[j
].i
.array
.level
<= 6 &&
911 i
/2 >= content
->array
.raid_disks
- delta
)
913 else if (devices
[j
].i
.array
.level
== 4 &&
914 i
/2 >= content
->array
.raid_disks
- delta
- 1)
918 } else if (devices
[j
].i
.reshape_active
!=
919 content
->reshape_active
||
920 (devices
[j
].i
.reshape_active
&&
921 devices
[j
].i
.reshape_progress
!=
922 content
->reshape_progress
))
923 /* Here, it may be a source of data. If two
924 * devices claim different progresses, it
925 * means that reshape boundaries differ for
926 * their own devices. Kernel will only treat
927 * the first one as reshape progress and
928 * go on. It may cause disaster, so avoid it.
931 if (chosen_drive
< 0 ||
933 > devices
[chosen_drive
].i
.events
)
936 if (chosen_drive
< 0)
938 current_events
= devices
[chosen_drive
].i
.events
;
941 pr_err("forcing event count in %s(%d) from %d up to %d\n",
942 devices
[chosen_drive
].devname
,
943 devices
[chosen_drive
].i
.disk
.raid_disk
,
944 (int)(devices
[chosen_drive
].i
.events
),
945 (int)(devices
[most_recent
].i
.events
));
946 fd
= dev_open(devices
[chosen_drive
].devname
,
947 devices
[chosen_drive
].included
? O_RDWR
950 pr_err("Couldn't open %s for write - not updating\n",
951 devices
[chosen_drive
].devname
);
952 devices
[chosen_drive
].i
.events
= 0;
956 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
958 pr_err("RAID superblock disappeared from %s - not updating.\n",
959 devices
[chosen_drive
].devname
);
960 devices
[chosen_drive
].i
.events
= 0;
963 content
->events
= devices
[most_recent
].i
.events
;
964 tst
->ss
->update_super(tst
, content
, "force-one",
965 devices
[chosen_drive
].devname
, c
->verbose
,
968 if (tst
->ss
->store_super(tst
, fd
)) {
970 pr_err("Could not re-write superblock on %s\n",
971 devices
[chosen_drive
].devname
);
972 devices
[chosen_drive
].i
.events
= 0;
973 tst
->ss
->free_super(tst
);
977 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
978 devices
[chosen_drive
].uptodate
= 1;
979 avail
[chosen_drive
] = 1;
981 tst
->ss
->free_super(tst
);
982 /* If there are any other drives of the same vintage,
983 * add them in as well. We can't lose and we might gain
986 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
990 !devices
[j
].uptodate
&&
991 devices
[j
].i
.recovery_start
== MaxSector
&&
992 devices
[j
].i
.events
== current_events
&&
993 ((!devices
[j
].i
.reshape_active
&&
994 !content
->reshape_active
) ||
995 (devices
[j
].i
.reshape_active
==
996 content
->reshape_active
&&
997 devices
[j
].i
.reshape_progress
==
998 content
->reshape_progress
))) {
1007 static int start_array(int mdfd
,
1009 struct mdinfo
*content
,
1010 struct supertype
*st
,
1011 struct mddev_ident
*ident
,
1012 int *best
, int bestcnt
,
1014 struct devs
*devices
,
1016 unsigned int sparecnt
,
1017 unsigned int rebuilding_cnt
,
1018 unsigned int journalcnt
,
1020 int clean
, char *avail
,
1021 int start_partial_ok
,
1028 unsigned int req_cnt
;
1030 if (content
->journal_device_required
&& (content
->journal_clean
== 0)) {
1032 pr_err("Not safe to assemble with missing or stale journal device, consider --force.\n");
1035 pr_err("Journal is missing or stale, starting array read only.\n");
1039 if (content
->consistency_policy
== CONSISTENCY_POLICY_PPL
)
1042 rv
= set_array_info(mdfd
, st
, content
);
1043 if (rv
&& !err_ok
) {
1044 pr_err("failed to set array info for %s: %s\n",
1045 mddev
, strerror(errno
));
1048 if (ident
->bitmap_fd
>= 0) {
1049 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
1050 pr_err("SET_BITMAP_FILE failed.\n");
1053 } else if (ident
->bitmap_file
) {
1054 /* From config file */
1055 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
1057 pr_err("Could not open bitmap file %s\n",
1058 ident
->bitmap_file
);
1061 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
1062 pr_err("Failed to set bitmapfile for %s\n", mddev
);
1069 /* First, add the raid disks, but add the chosen one last */
1070 for (i
= 0; i
<= bestcnt
; i
++) {
1074 if (j
== chosen_drive
)
1079 if (j
>= 0 && !devices
[j
].included
) {
1082 dfd
= dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
1084 remove_partitions(dfd
);
1087 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
1090 pr_err("failed to add %s to %s: %s\n",
1091 devices
[j
].devname
, mddev
,
1093 if (errno
== EINVAL
&& content
->array
.level
== 0 &&
1094 content
->array
.layout
!= 0) {
1095 cont_err("Possibly your kernel doesn't support RAID0 layouts.\n");
1096 cont_err("Please upgrade.\n");
1098 if (i
< content
->array
.raid_disks
* 2 ||
1103 } else if (c
->verbose
> 0) {
1104 pr_err("added %s to %s as %d%s%s\n",
1105 devices
[j
].devname
, mddev
,
1106 devices
[j
].i
.disk
.raid_disk
,
1107 devices
[j
].uptodate
?"":
1108 " (possibly out of date)",
1109 (devices
[j
].i
.disk
.state
&
1110 (1<<MD_DISK_REPLACEMENT
)) ?
1113 } else if (j
>= 0) {
1115 pr_err("%s is already in %s as %d\n",
1116 devices
[j
].devname
, mddev
,
1117 devices
[j
].i
.disk
.raid_disk
);
1118 } else if (c
->verbose
> 0 &&
1119 i
< content
->array
.raid_disks
* 2 && (i
& 1) == 0)
1120 pr_err("no uptodate device for slot %d of %s\n",
1124 if (content
->array
.level
== LEVEL_CONTAINER
) {
1125 sysfs_rules_apply(mddev
, content
);
1126 if (c
->verbose
>= 0) {
1127 pr_err("Container %s has been assembled with %d drive%s",
1128 mddev
, okcnt
+ sparecnt
+ journalcnt
,
1129 okcnt
+ sparecnt
+ journalcnt
== 1 ? "" : "s");
1130 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1131 fprintf(stderr
, " (out of %d)\n",
1132 content
->array
.raid_disks
);
1134 fprintf(stderr
, "\n");
1137 if (st
->ss
->validate_container
) {
1138 struct mdinfo
*devices_list
;
1139 struct mdinfo
*info_devices
;
1142 devices_list
= NULL
;
1143 info_devices
= xmalloc(sizeof(struct mdinfo
) *
1144 (okcnt
+ sparecnt
));
1145 for (count
= 0; count
< okcnt
+ sparecnt
; count
++) {
1146 info_devices
[count
] = devices
[count
].i
;
1147 info_devices
[count
].next
= devices_list
;
1148 devices_list
= &info_devices
[count
];
1150 if (st
->ss
->validate_container(devices_list
))
1151 pr_err("Mismatch detected!\n");
1155 st
->ss
->free_super(st
);
1156 sysfs_uevent(content
, "change");
1157 if (err_ok
&& okcnt
< (unsigned)content
->array
.raid_disks
)
1158 /* Was partial, is still partial, so signal an error
1159 * to ensure we don't retry */
1164 /* Get number of in-sync devices according to the superblock.
1165 * We must have this number to start the array without -s or -R
1167 req_cnt
= content
->array
.working_disks
;
1169 if (c
->runstop
== 1 ||
1171 (enough(content
->array
.level
, content
->array
.raid_disks
,
1172 content
->array
.layout
, clean
, avail
) &&
1173 (okcnt
+ rebuilding_cnt
>= req_cnt
|| start_partial_ok
)))) {
1174 /* This array is good-to-go.
1175 * If a reshape is in progress then we might need to
1176 * continue monitoring it. In that case we start
1177 * it read-only and let the grow code make it writable.
1181 if (content
->reshape_active
&&
1182 !(content
->reshape_active
& RESHAPE_NO_BACKUP
) &&
1183 content
->delta_disks
<= 0) {
1184 if (!c
->backup_file
) {
1185 pr_err("%s: Need a backup file to complete reshape of this array.\n",
1187 pr_err("Please provided one with \"--backup-file=...\"\n");
1189 strcmp(c
->update
, "revert-reshape") == 0)
1190 pr_err("(Don't specify --update=revert-reshape again, that part succeeded.)\n");
1193 rv
= sysfs_set_str(content
, NULL
,
1194 "array_state", "readonly");
1196 rv
= Grow_continue(mdfd
, st
, content
,
1199 } else if (c
->readonly
&&
1200 sysfs_attribute_available(content
, NULL
,
1202 rv
= sysfs_set_str(content
, NULL
,
1203 "array_state", "readonly");
1205 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1206 reopen_mddev(mdfd
); /* drop O_EXCL */
1208 sysfs_rules_apply(mddev
, content
);
1209 if (c
->verbose
>= 0) {
1210 pr_err("%s has been started with %d drive%s",
1211 mddev
, okcnt
, okcnt
==1?"":"s");
1212 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1213 fprintf(stderr
, " (out of %d)",
1214 content
->array
.raid_disks
);
1216 fprintf(stderr
, "%s %d rebuilding",
1217 sparecnt
?",":" and",
1220 fprintf(stderr
, " and %d spare%s",
1222 sparecnt
== 1 ? "" : "s");
1223 if (content
->journal_clean
)
1224 fprintf(stderr
, " and %d journal",
1226 fprintf(stderr
, ".\n");
1228 if (content
->reshape_active
&&
1229 content
->array
.level
>= 4 &&
1230 content
->array
.level
<= 6) {
1231 /* might need to increase the size
1232 * of the stripe cache - default is 256
1234 int chunk_size
= content
->array
.chunk_size
;
1236 if (content
->reshape_active
&&
1237 content
->new_chunk
> chunk_size
)
1238 chunk_size
= content
->new_chunk
;
1239 if (256 < 4 * ((chunk_size
+4065)/4096)) {
1242 sra
= sysfs_read(mdfd
, NULL
, 0);
1244 sysfs_set_num(sra
, NULL
,
1245 "stripe_cache_size",
1246 (4 * chunk_size
/ 4096) + 1);
1250 if (okcnt
< (unsigned)content
->array
.raid_disks
) {
1251 /* If any devices did not get added
1252 * because the kernel rejected them based
1253 * on event count, try adding them
1254 * again providing the action policy is
1255 * 're-add' or greater. The bitmap
1256 * might allow them to be included, or
1257 * they will become spares.
1259 for (i
= 0; i
< bestcnt
; i
++) {
1261 if (j
>= 0 && !devices
[j
].uptodate
) {
1262 if (!disk_action_allows(&devices
[j
].i
, st
->ss
->name
, act_re_add
))
1264 rv
= add_disk(mdfd
, st
, content
,
1266 if (rv
== 0 && c
->verbose
>= 0)
1267 pr_err("%s has been re-added.\n",
1268 devices
[j
].devname
);
1272 if (content
->array
.level
== 6 &&
1273 okcnt
+ 1 == (unsigned)content
->array
.raid_disks
&&
1277 sra
= sysfs_read(mdfd
, NULL
, 0);
1279 sysfs_set_str(sra
, NULL
,
1280 "sync_action", "repair");
1285 pr_err("failed to RUN_ARRAY %s: %s\n", mddev
, strerror(errno
));
1286 if (errno
== 524 /* ENOTSUP */ &&
1287 content
->array
.level
== 0 && content
->array
.layout
== 0)
1288 cont_err("Please use --update=layout-original or --update=layout-alternate\n");
1290 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1291 content
->array
.layout
, 1, avail
))
1292 pr_err("Not enough devices to start the array.\n");
1293 else if (!enough(content
->array
.level
,
1294 content
->array
.raid_disks
,
1295 content
->array
.layout
, clean
, avail
))
1296 pr_err("Not enough devices to start the array while not clean - consider --force.\n");
1300 if (c
->runstop
== -1) {
1301 pr_err("%s assembled from %d drive%s",
1302 mddev
, okcnt
, okcnt
== 1 ? "" : "s");
1303 if (okcnt
!= (unsigned)content
->array
.raid_disks
)
1304 fprintf(stderr
, " (out of %d)",
1305 content
->array
.raid_disks
);
1306 fprintf(stderr
, ", but not started.\n");
1309 if (c
->verbose
>= -1) {
1310 pr_err("%s assembled from %d drive%s",
1311 mddev
, okcnt
, okcnt
== 1 ? "" : "s");
1313 fprintf(stderr
, "%s %d rebuilding",
1314 sparecnt
? "," : " and", rebuilding_cnt
);
1316 fprintf(stderr
, " and %d spare%s", sparecnt
,
1317 sparecnt
== 1 ? "" : "s");
1318 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1319 content
->array
.layout
, 1, avail
))
1320 fprintf(stderr
, " - not enough to start the array.\n");
1321 else if (!enough(content
->array
.level
,
1322 content
->array
.raid_disks
,
1323 content
->array
.layout
, clean
, avail
))
1324 fprintf(stderr
, " - not enough to start the array while not clean - consider --force.\n");
1326 if (req_cnt
== (unsigned)content
->array
.raid_disks
)
1327 fprintf(stderr
, " - need all %d to start it",
1330 fprintf(stderr
, " - need %d to start", req_cnt
);
1331 fprintf(stderr
, " (use --run to insist).\n");
1337 int Assemble(struct supertype
*st
, char *mddev
,
1338 struct mddev_ident
*ident
,
1339 struct mddev_dev
*devlist
,
1343 * The task of Assemble is to find a collection of
1344 * devices that should (according to their superblocks)
1345 * form an array, and to give this collection to the MD driver.
1346 * In Linux-2.4 and later, this involves submitting a
1347 * SET_ARRAY_INFO ioctl with no arg - to prepare
1348 * the array - and then submit a number of
1349 * ADD_NEW_DISK ioctls to add disks into
1350 * the array. Finally RUN_ARRAY might
1351 * be submitted to start the array.
1353 * Much of the work of Assemble is in finding and/or
1354 * checking the disks to make sure they look right.
1356 * If mddev is not set, then scan must be set and we
1357 * read through the config file for dev+uuid mapping
1358 * We recurse, setting mddev, for each device that
1360 * - has a valid uuid (or any uuid if !uuidset)
1362 * If mddev is set, we try to determine state of md.
1363 * check version - must be at least 0.90.0
1364 * check kernel version. must be at least 2.4.
1365 * If not, we can possibly fall back on START_ARRAY
1366 * Try to GET_ARRAY_INFO.
1367 * If possible, give up
1368 * If not, try to STOP_ARRAY just to make sure
1370 * If !uuidset and scan, look in conf-file for uuid
1371 * If not found, give up
1372 * If !devlist and scan and uuidset, get list of devs from conf-file
1375 * Check superblock - discard if bad
1376 * Check uuid (set if we don't have one) - discard if no match
1377 * Check superblock similarity if we have a superblock - discard if different
1378 * Record events, devicenum
1379 * This should give us a list of devices for the array
1380 * We should collect the most recent event number
1382 * Count disks with recent enough event count
1383 * While force && !enough disks
1384 * Choose newest rejected disks, update event count
1385 * mark clean and rewrite superblock
1388 * foreach device with recent events : ADD_NEW_DISK
1389 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
1391 * Check the device numbers in superblock are right
1392 * update superblock if any changes
1399 int auto_assem
= (mddev
== NULL
&& !ident
->uuid_set
&&
1400 ident
->super_minor
== UnSet
&& ident
->name
[0] == 0 &&
1401 (ident
->container
== NULL
|| ident
->member
== NULL
));
1402 struct devs
*devices
= NULL
;
1404 int *best
= NULL
; /* indexed by raid_disk */
1407 unsigned int okcnt
, sparecnt
, rebuilding_cnt
, replcnt
, journalcnt
;
1408 int journal_clean
= 0;
1411 int most_recent
= 0;
1415 int start_partial_ok
= (c
->runstop
>= 0) &&
1416 (c
->force
|| devlist
==NULL
|| auto_assem
);
1418 struct mddev_dev
*tmpdev
;
1420 struct mdinfo
*content
= NULL
;
1421 struct mdinfo
*pre_exist
= NULL
;
1424 char chosen_name
[1024];
1425 struct map_ent
*map
= NULL
;
1429 * If any subdevs are listed, then any that don't
1430 * match ident are discarded. Remainder must all match and
1432 * If no subdevs, then we scan all devices in the config file, but
1433 * there must be something in the identity
1437 ident
->uuid_set
== 0 &&
1438 (ident
->super_minor
< 0 || ident
->super_minor
== UnSet
) &&
1439 ident
->name
[0] == 0 &&
1440 (ident
->container
== NULL
|| ident
->member
== NULL
) &&
1441 ident
->devices
== NULL
) {
1442 pr_err("No identity information available for %s - cannot assemble.\n",
1443 mddev
? mddev
: "further assembly");
1447 if (devlist
== NULL
)
1448 devlist
= conf_get_devs();
1453 /* We come back here when doing auto-assembly and attempting some
1454 * set of devices failed. Those are now marked as ->used==2 and
1455 * we ignore them and try again
1457 if (!st
&& ident
->st
)
1460 pr_err("looking for devices for %s\n",
1461 mddev
? mddev
: "further assembly");
1465 st
->ignore_hw_compat
= 1;
1466 num_devs
= select_devices(devlist
, ident
, &st
, &content
, c
,
1467 inargv
, auto_assem
);
1471 if (!st
|| !st
->sb
|| !content
)
1474 /* We have a full set of devices - we now need to find the
1476 * However there is a risk that we are racing with "mdadm -I"
1477 * and the array is already partially assembled - we will have
1478 * rejected any devices already in this address.
1479 * So we take a lock on the map file - to prevent further races -
1480 * and look for the uuid in there. If found and the array is
1481 * active, we abort. If found and the array is not active
1482 * we commit to that md device and add all the contained devices
1483 * to our list. We flag them so that we don't try to re-add,
1484 * but can remove if they turn out to not be wanted.
1487 pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
1488 if (c
->update
&& strcmp(c
->update
,"uuid") == 0)
1491 mp
= map_by_uuid(&map
, content
->uuid
);
1494 /* array already exists. */
1495 pre_exist
= sysfs_read(-1, mp
->devnm
, GET_LEVEL
|GET_DEVS
);
1496 if (pre_exist
->array
.level
!= UnSet
) {
1497 pr_err("Found some drive for an array that is already active: %s\n",
1499 pr_err("giving up.\n");
1502 for (dv
= pre_exist
->devs
; dv
; dv
= dv
->next
) {
1503 /* We want to add this device to our list,
1504 * but it could already be there if "mdadm -I"
1505 * started *after* we checked for O_EXCL.
1506 * If we add it to the top of the list
1507 * it will be preferred over later copies.
1509 struct mddev_dev
*newdev
;
1510 char *devname
= map_dev(dv
->disk
.major
,
1515 newdev
= xmalloc(sizeof(*newdev
));
1516 newdev
->devname
= devname
;
1517 newdev
->disposition
= 'I';
1519 newdev
->next
= devlist
;
1523 strcpy(chosen_name
, mp
->path
);
1524 if (c
->verbose
> 0 || mddev
== NULL
||
1525 strcmp(mddev
, chosen_name
) != 0)
1526 pr_err("Merging with already-assembled %s\n",
1528 mdfd
= open_dev_excl(mp
->devnm
);
1530 int trustworthy
= FOREIGN
;
1531 name
= content
->name
;
1532 switch (st
->ss
->match_home(st
, c
->homehost
)
1533 ?: st
->ss
->match_home(st
, "any")) {
1535 trustworthy
= LOCAL
;
1536 name
= strchr(content
->name
, ':');
1540 name
= content
->name
;
1543 if (mddev
&& map_by_name(&map
, mddev
) != NULL
) {
1544 pr_err("Cannot create device with %s because is in use\n", mddev
);
1548 /* If the array is listed in mdadm.conf or on
1549 * command line, then we trust the name
1550 * even if the array doesn't look local
1552 trustworthy
= LOCAL
;
1555 content
->array
.level
== LEVEL_CONTAINER
) {
1556 name
= content
->text_version
;
1557 trustworthy
= METADATA
;
1560 if (name
[0] && trustworthy
!= LOCAL
&&
1561 ! c
->require_homehost
&&
1562 conf_name_is_free(name
))
1563 trustworthy
= LOCAL
;
1565 if (trustworthy
== LOCAL
&&
1567 /* Ignore 'host:' prefix of name */
1568 name
= strchr(name
, ':')+1;
1570 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
1574 st
->ss
->free_super(st
);
1579 mddev
= chosen_name
;
1580 if (pre_exist
== NULL
) {
1581 if (mddev_busy(fd2devnm(mdfd
))) {
1582 pr_err("%s already active, cannot restart it!\n",
1584 for (tmpdev
= devlist
;
1585 tmpdev
&& tmpdev
->used
!= 1;
1586 tmpdev
= tmpdev
->next
)
1588 if (tmpdev
&& auto_assem
)
1589 pr_err("%s needed for %s...\n",
1590 mddev
, tmpdev
->devname
);
1593 st
->ss
->free_super(st
);
1598 /* just incase it was started but has no content */
1599 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1602 if (content
!= &info
) {
1603 /* This is a member of a container. Try starting the array. */
1605 err
= assemble_container_content(st
, mdfd
, content
, c
,
1611 /* Ok, no bad inconsistancy, we can try updating etc */
1612 devices
= xcalloc(num_devs
, sizeof(*devices
));
1613 devmap
= xcalloc(num_devs
, content
->array
.raid_disks
);
1614 devcnt
= load_devices(devices
, devmap
, ident
, &st
, devlist
,
1615 c
, content
, mdfd
, mddev
,
1616 &most_recent
, &bestcnt
, &best
, inargv
);
1620 * devices is already freed in load_devices, so set devices
1621 * to NULL to avoid double free devices.
1628 pr_err("no devices found for %s\n",
1631 st
->ss
->free_super(st
);
1636 if (c
->update
&& strcmp(c
->update
, "byteorder")==0)
1637 st
->minor_version
= 90;
1639 st
->ss
->getinfo_super(st
, content
, NULL
);
1640 clean
= content
->array
.state
& 1;
1642 /* now we have some devices that might be suitable.
1645 avail
= xcalloc(content
->array
.raid_disks
, 1);
1651 for (i
=0; i
< bestcnt
; i
++) {
1653 int event_margin
= 1; /* always allow a difference of '1'
1654 * like the kernel does
1656 if (j
< 0) continue;
1657 /* note: we ignore error flags in multipath arrays
1658 * as they don't make sense
1660 if (content
->array
.level
!= LEVEL_MULTIPATH
) {
1661 if (devices
[j
].i
.disk
.state
& (1<<MD_DISK_JOURNAL
)) {
1662 if (content
->journal_device_required
)
1664 else /* unexpected journal, mark as faulty */
1665 devices
[j
].i
.disk
.state
|= (1<<MD_DISK_FAULTY
);
1666 } else if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_ACTIVE
))) {
1667 if (!(devices
[j
].i
.disk
.state
1668 & (1<<MD_DISK_FAULTY
))) {
1669 devices
[j
].uptodate
= 1;
1675 /* If this device thinks that 'most_recent' has failed, then
1676 * we must reject this device.
1678 if (j
!= most_recent
&& !c
->force
&&
1679 content
->array
.raid_disks
> 0 &&
1680 devices
[most_recent
].i
.disk
.raid_disk
>= 0 &&
1681 devmap
[j
* content
->array
.raid_disks
+ devices
[most_recent
].i
.disk
.raid_disk
] == 0) {
1682 if (c
->verbose
> -1)
1683 pr_err("ignoring %s as it reports %s as failed\n",
1684 devices
[j
].devname
, devices
[most_recent
].devname
);
1688 /* Require event counter to be same as, or just less than,
1689 * most recent. If it is bigger, it must be a stray spare and
1690 * should be ignored.
1692 if (devices
[j
].i
.events
+event_margin
>=
1693 devices
[most_recent
].i
.events
&&
1694 devices
[j
].i
.events
<=
1695 devices
[most_recent
].i
.events
1697 devices
[j
].uptodate
= 1;
1698 if (devices
[j
].i
.disk
.state
& (1<<MD_DISK_JOURNAL
))
1700 if (i
< content
->array
.raid_disks
* 2) {
1701 if (devices
[j
].i
.recovery_start
== MaxSector
||
1702 (content
->reshape_active
&&
1703 i
>= content
->array
.raid_disks
- content
->delta_disks
)) {
1711 } else if (devices
[j
].i
.disk
.raid_disk
!= MD_DISK_ROLE_JOURNAL
)
1717 int force_ok
= force_array(content
, devices
, best
, bestcnt
,
1718 avail
, most_recent
, st
, c
);
1723 /* Now we want to look at the superblock which the kernel will base things on
1724 * and compare the devices that we think are working with the devices that the
1725 * superblock thinks are working.
1726 * If there are differences and --force is given, then update this chosen
1730 st
->ss
->free_super(st
);
1731 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
+=2) {
1737 if (!devices
[j
].uptodate
)
1739 if (devices
[j
].i
.events
< devices
[most_recent
].i
.events
)
1742 if ((fd
=dev_open(devices
[j
].devname
,
1743 devices
[j
].included
? O_RDONLY
1744 : (O_RDONLY
|O_EXCL
)))< 0) {
1745 pr_err("Cannot open %s: %s\n",
1746 devices
[j
].devname
, strerror(errno
));
1749 if (st
->ss
->load_super(st
,fd
, NULL
)) {
1751 pr_err("RAID superblock has disappeared from %s\n",
1752 devices
[j
].devname
);
1757 if (st
->sb
== NULL
) {
1758 pr_err("No suitable drives found for %s\n", mddev
);
1761 st
->ss
->getinfo_super(st
, content
, NULL
);
1762 if (sysfs_init(content
, mdfd
, NULL
)) {
1763 pr_err("Unable to initialize sysfs\n");
1767 /* after reload context, store journal_clean in context */
1768 content
->journal_clean
= journal_clean
;
1769 for (i
=0; i
<bestcnt
; i
++) {
1771 unsigned int desired_state
;
1775 if (devices
[j
].i
.disk
.raid_disk
== MD_DISK_ROLE_JOURNAL
)
1776 desired_state
= (1<<MD_DISK_JOURNAL
);
1777 else if (i
>= content
->array
.raid_disks
* 2)
1780 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_REPLACEMENT
);
1782 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
1784 desired_state
|= devices
[j
].i
.disk
.state
& ((1<<MD_DISK_FAILFAST
) |
1785 (1<<MD_DISK_WRITEMOSTLY
));
1787 if (!devices
[j
].uptodate
)
1790 devices
[j
].i
.disk
.state
= desired_state
;
1791 if (!(devices
[j
].i
.array
.state
& 1))
1794 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
1795 c
->verbose
, 0, NULL
)) {
1797 if (c
->verbose
>= 0)
1798 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
1799 j
, mddev
, devices
[j
].devname
);
1802 if (c
->verbose
>= -1)
1803 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
1804 i
, mddev
, devices
[j
].devname
);
1808 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
1809 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1814 if (c
->force
&& !clean
&&
1815 !enough(content
->array
.level
, content
->array
.raid_disks
,
1816 content
->array
.layout
, clean
,
1818 change
+= st
->ss
->update_super(st
, content
, "force-array",
1819 devices
[chosen_drive
].devname
, c
->verbose
,
1827 fd
= dev_open(devices
[chosen_drive
].devname
,
1828 devices
[chosen_drive
].included
?
1829 O_RDWR
: (O_RDWR
|O_EXCL
));
1831 pr_err("Could not open %s for write - cannot Assemble array.\n",
1832 devices
[chosen_drive
].devname
);
1835 if (st
->ss
->store_super(st
, fd
)) {
1837 pr_err("Could not re-write superblock on %s\n",
1838 devices
[chosen_drive
].devname
);
1841 if (c
->verbose
>= 0)
1842 pr_err("Marking array %s as 'clean'\n",
1847 /* If we are in the middle of a reshape we may need to restore saved data
1848 * that was moved aside due to the reshape overwriting live data
1849 * The code of doing this lives in Grow.c
1851 if (content
->reshape_active
&&
1852 !(content
->reshape_active
& RESHAPE_NO_BACKUP
)) {
1854 int *fdlist
= xmalloc(sizeof(int)* bestcnt
);
1856 pr_err("%s has an active reshape - checking if critical section needs to be restored\n",
1858 if (!c
->backup_file
)
1859 c
->backup_file
= locate_backup(content
->sys_name
);
1860 enable_fds(bestcnt
/2);
1861 for (i
= 0; i
< bestcnt
/2; i
++) {
1864 fdlist
[i
] = dev_open(devices
[j
].devname
,
1866 ? O_RDWR
: (O_RDWR
|O_EXCL
));
1867 if (fdlist
[i
] < 0) {
1868 pr_err("Could not open %s for write - cannot Assemble array.\n",
1869 devices
[j
].devname
);
1877 if (st
->ss
->external
&& st
->ss
->recover_backup
)
1878 err
= st
->ss
->recover_backup(st
, content
);
1880 err
= Grow_restart(st
, content
, fdlist
, bestcnt
/2,
1881 c
->backup_file
, c
->verbose
> 0);
1882 if (err
&& c
->invalid_backup
) {
1884 pr_err("continuing without restoring backup\n");
1890 if (fdlist
[i
]>=0) close(fdlist
[i
]);
1894 pr_err("Failed to restore critical section for reshape, sorry.\n");
1895 if (c
->backup_file
== NULL
)
1896 cont_err("Possibly you needed to specify the --backup-file\n");
1901 /* Almost ready to actually *do* something */
1902 /* First, fill in the map, so that udev can find our name
1903 * as soon as we become active.
1905 if (c
->update
&& strcmp(c
->update
, "metadata")==0) {
1906 content
->array
.major_version
= 1;
1907 content
->array
.minor_version
= 0;
1908 strcpy(content
->text_version
, "1.0");
1911 map_update(&map
, fd2devnm(mdfd
), content
->text_version
,
1912 content
->uuid
, chosen_name
);
1914 rv
= start_array(mdfd
, mddev
, content
,
1915 st
, ident
, best
, bestcnt
,
1916 chosen_drive
, devices
, okcnt
, sparecnt
,
1917 rebuilding_cnt
, journalcnt
,
1919 clean
, avail
, start_partial_ok
,
1922 if (rv
== 1 && !pre_exist
)
1923 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1928 wait_for(chosen_name
, mdfd
);
1932 /* There is a nasty race with 'mdadm --monitor'.
1933 * If it opens this device before we close it,
1934 * it gets an incomplete open on which IO
1935 * doesn't work and the capacity is
1937 * If we reopen (to check for layered devices)
1938 * before --monitor closes, we loose.
1940 * So: wait upto 1 second for there to be
1941 * a non-zero capacity.
1943 while (usecs
< 1000) {
1944 mdfd
= open(mddev
, O_RDONLY
);
1946 unsigned long long size
;
1947 if (get_dev_size(mdfd
, NULL
, &size
) &&
1956 } else if (mdfd
>= 0)
1959 /* '2' means 'OK, but not started yet' */
1964 return rv
== 2 ? 0 : rv
;
1967 int assemble_container_content(struct supertype
*st
, int mdfd
,
1968 struct mdinfo
*content
, struct context
*c
,
1969 char *chosen_name
, int *result
)
1971 struct mdinfo
*dev
, *sra
, *dev2
;
1972 struct assembly_array_info array
= {chosen_name
, 0, 0, 0};
1977 int is_raid456
, is_clean
, all_disks
;
1979 if (sysfs_init(content
, mdfd
, NULL
)) {
1980 pr_err("Unable to initialize sysfs\n");
1984 sra
= sysfs_read(mdfd
, NULL
, GET_VERSION
|GET_DEVS
);
1985 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0) {
1986 if (content
->array
.major_version
== -1 &&
1987 content
->array
.minor_version
== -2 &&
1989 content
->text_version
[0] == '/')
1990 content
->text_version
[0] = '-';
1991 if (sysfs_set_array(content
, 9003) != 0) {
1997 /* There are two types of reshape: container wide or sub-array specific
1998 * Check if metadata requests blocking container wide reshapes
2000 start_reshape
= (content
->reshape_active
&&
2001 !((content
->reshape_active
== CONTAINER_RESHAPE
) &&
2002 (content
->array
.state
& (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))));
2004 /* Block subarray here if it is under reshape now
2005 * Do not allow for any changes in this array
2007 if (st
->ss
->external
&& content
->recovery_blocked
&& start_reshape
)
2008 block_subarray(content
);
2010 for (dev2
= sra
->devs
; dev2
; dev2
= dev2
->next
) {
2011 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
2012 if (dev2
->disk
.major
== dev
->disk
.major
&&
2013 dev2
->disk
.minor
== dev
->disk
.minor
)
2017 /* Don't want this one any more */
2018 if (sysfs_set_str(sra
, dev2
, "slot", "none") < 0 &&
2020 pr_err("Cannot remove old device %s: not updating %s\n", dev2
->sys_name
, sra
->sys_name
);
2024 sysfs_set_str(sra
, dev2
, "state", "remove");
2026 old_raid_disks
= content
->array
.raid_disks
- content
->delta_disks
;
2027 avail
= xcalloc(content
->array
.raid_disks
, 1);
2028 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
2029 if (dev
->disk
.raid_disk
>= 0)
2030 avail
[dev
->disk
.raid_disk
] = 1;
2031 if (sysfs_add_disk(content
, dev
, 1) == 0) {
2032 if (dev
->disk
.raid_disk
>= old_raid_disks
&&
2033 content
->reshape_active
)
2037 } else if (errno
== EEXIST
)
2038 array
.preexist_cnt
++;
2042 all_disks
= array
.new_cnt
+ array
.exp_cnt
+ array
.preexist_cnt
;
2044 map_update(NULL
, fd2devnm(mdfd
), content
->text_version
,
2045 content
->uuid
, chosen_name
);
2047 if (content
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
2048 st
->ss
->validate_ppl
) {
2049 content
->array
.state
|= 1;
2052 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
2057 ret
= st
->ss
->validate_ppl(st
, content
, dev
);
2067 pr_err("%s contains invalid PPL - consider --force or --update-subarray with --update=no-ppl\n",
2069 content
->array
.state
&= ~1;
2070 avail
[dev
->disk
.raid_disk
] = 0;
2074 /* have --force - overwrite the invalid ppl */
2075 devpath
= map_dev(dev
->disk
.major
, dev
->disk
.minor
, 0);
2076 dfd
= dev_open(devpath
, O_RDWR
);
2078 pr_err("Failed to open %s\n", devpath
);
2083 err
= st
->ss
->write_init_ppl(st
, content
, dfd
);
2094 } else if (c
->force
) {
2095 /* Set the array as 'clean' so that we can proceed with starting
2096 * it even if we don't have all devices. Mdmon doesn't care
2097 * if the dirty flag is set in metadata, it will start managing
2099 * This is really important for raid456 (RWH case), other levels
2100 * are started anyway.
2102 content
->array
.state
|= 1;
2105 is_raid456
= (content
->array
.level
>= 4 && content
->array
.level
<= 6);
2106 is_clean
= content
->array
.state
& 1;
2108 if (enough(content
->array
.level
, content
->array
.raid_disks
,
2109 content
->array
.layout
, is_clean
, avail
) == 0) {
2110 set_array_assembly_status(c
, result
, INCR_NO
, &array
);
2112 if (c
->verbose
>= 0 && is_raid456
&& !is_clean
)
2113 pr_err("Consider --force to start dirty degraded array\n");
2120 if (c
->runstop
<= 0 && all_disks
< content
->array
.working_disks
) {
2122 set_array_assembly_status(c
, result
, INCR_UNSAFE
, &array
);
2124 if (c
->verbose
>= 0 && c
->force
)
2125 pr_err("Consider --run to start array as degraded.\n");
2129 if (is_raid456
&& content
->resync_start
!= MaxSector
&& c
->force
&&
2130 all_disks
< content
->array
.raid_disks
) {
2132 content
->resync_start
= MaxSector
;
2133 err
= sysfs_set_num(content
, NULL
, "resync_start", MaxSector
);
2137 pr_err("%s array state forced to clean. It may cause data corruption.\n",
2142 * Before activating the array, perform extra steps required
2143 * to configure the internal write-intent bitmap.
2145 if (content
->consistency_policy
== CONSISTENCY_POLICY_BITMAP
&&
2147 st
->ss
->set_bitmap(st
, content
);
2149 if (start_reshape
) {
2150 int spare
= content
->array
.raid_disks
+ array
.exp_cnt
;
2151 if (restore_backup(st
, content
,
2153 spare
, &c
->backup_file
, c
->verbose
) == 1)
2156 if (content
->reshape_progress
== 0) {
2157 /* If reshape progress is 0 - we are assembling the
2158 * array that was stopped, before reshape has started.
2159 * Array needs to be started as active, Grow_continue()
2160 * will start the reshape.
2162 sysfs_set_num(content
, NULL
, "reshape_position",
2164 err
= sysfs_set_str(content
, NULL
,
2165 "array_state", "active");
2166 sysfs_set_num(content
, NULL
, "reshape_position", 0);
2168 err
= sysfs_set_str(content
, NULL
,
2169 "array_state", "readonly");
2175 if (st
->ss
->external
) {
2176 if (!mdmon_running(st
->container_devnm
))
2177 start_mdmon(st
->container_devnm
);
2178 ping_monitor(st
->container_devnm
);
2179 if (mdmon_running(st
->container_devnm
) &&
2180 st
->update_tail
== NULL
)
2181 st
->update_tail
= &st
->updates
;
2184 err
= Grow_continue(mdfd
, st
, content
, c
->backup_file
,
2185 0, c
->freeze_reshape
);
2186 } else switch(content
->array
.level
) {
2188 case LEVEL_MULTIPATH
:
2190 err
= sysfs_set_str(content
, NULL
, "array_state",
2191 c
->readonly
? "readonly" : "active");
2194 err
= sysfs_set_str(content
, NULL
, "array_state",
2196 /* start mdmon if needed. */
2198 if (!mdmon_running(st
->container_devnm
))
2199 start_mdmon(st
->container_devnm
);
2200 ping_monitor(st
->container_devnm
);
2205 sysfs_set_safemode(content
, content
->safe_mode_delay
);
2207 /* Block subarray here if it is not reshaped now
2208 * It has be blocked a little later to allow mdmon to switch in
2211 if (st
->ss
->external
&& content
->recovery_blocked
&&
2213 block_subarray(content
);
2216 set_array_assembly_status(c
, result
, INCR_NO
, &array
);
2218 set_array_assembly_status(c
, result
, INCR_YES
, &array
);
2219 wait_for(chosen_name
, mdfd
);
2220 sysfs_rules_apply(chosen_name
, content
);
2224 /* FIXME should have an O_EXCL and wait for read-auto */