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(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
);
81 fprintf(stderr
, " %s", status_msg
);
82 fprintf(stderr
, ".\n");
85 static int name_matches(char *found
, char *required
, char *homehost
, int require_homehost
)
87 /* See if the name found matches the required name, possibly
88 * prefixed with 'homehost'
93 if (strcmp(found
, required
)==0)
95 sep
= strchr(found
, ':');
99 if (strncmp(found
, "any:", 4) == 0 ||
100 (homehost
&& strcmp(homehost
, "any") == 0) ||
102 (homehost
&& strlen(homehost
) == l
&&
103 strncmp(found
, homehost
, l
) == 0)) {
104 /* matching homehost */
105 if (strcmp(sep
+1, required
) == 0)
111 static int is_member_busy(char *metadata_version
)
113 /* check if the given member array is active */
114 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
115 struct mdstat_ent
*ent
;
118 for (ent
= mdstat
; ent
; ent
= ent
->next
) {
119 if (ent
->metadata_version
== NULL
)
121 if (strncmp(ent
->metadata_version
, "external:", 9) != 0)
123 if (!is_subarray(&ent
->metadata_version
[9]))
125 /* Skip first char - it can be '/' or '-' */
126 if (strcmp(&ent
->metadata_version
[10], metadata_version
+1) == 0) {
136 static int ident_matches(struct mddev_ident
*ident
,
137 struct mdinfo
*content
,
138 struct supertype
*tst
,
139 char *homehost
, int require_homehost
,
140 char *update
, char *devname
)
143 if (ident
->uuid_set
&& (!update
|| strcmp(update
, "uuid")!= 0) &&
144 same_uuid(content
->uuid
, ident
->uuid
, tst
->ss
->swapuuid
)==0 &&
145 memcmp(content
->uuid
, uuid_zero
, sizeof(int[4])) != 0) {
147 pr_err("%s has wrong uuid.\n", devname
);
150 if (ident
->name
[0] && (!update
|| strcmp(update
, "name")!= 0) &&
151 name_matches(content
->name
, ident
->name
, homehost
, require_homehost
)==0) {
153 pr_err("%s has wrong name.\n", devname
);
156 if (ident
->super_minor
!= UnSet
&&
157 ident
->super_minor
!= content
->array
.md_minor
) {
159 pr_err("%s has wrong super-minor.\n",
163 if (ident
->level
!= UnSet
&&
164 ident
->level
!= content
->array
.level
) {
166 pr_err("%s has wrong raid level.\n",
170 if (ident
->raid_disks
!= UnSet
&&
171 content
->array
.raid_disks
!= 0 && /* metadata doesn't know how many to expect */
172 ident
->raid_disks
!= content
->array
.raid_disks
) {
174 pr_err("%s requires wrong number of drives.\n",
178 if (ident
->member
&& ident
->member
[0]) {
179 /* content->text_version must match */
180 char *s
= strchr(content
->text_version
+1, '/');
183 pr_err("%s is not a container and one is required.\n",
186 } else if (strcmp(ident
->member
, s
+1) != 0) {
188 pr_err("skipping wrong member %s is %s\n",
189 content
->text_version
, devname
);
196 static int select_devices(struct mddev_dev
*devlist
,
197 struct mddev_ident
*ident
,
198 struct supertype
**stp
,
199 struct mdinfo
**contentp
,
201 int inargv
, int auto_assem
)
203 struct mddev_dev
*tmpdev
;
205 struct supertype
*st
= *stp
;
206 struct mdinfo
*content
= NULL
;
207 int report_mismatch
= ((inargv
&& c
->verbose
>= 0) || c
->verbose
> 0);
208 struct domainlist
*domains
= NULL
;
211 tmpdev
= devlist
; num_devs
= 0;
217 tmpdev
->disposition
= 0;
218 tmpdev
= tmpdev
->next
;
221 /* first walk the list of devices to find a consistent set
222 * that match the criterea, if that is possible.
223 * We flag the ones we like with 'used'.
225 for (tmpdev
= devlist
;
227 tmpdev
= tmpdev
? tmpdev
->next
: NULL
) {
228 char *devname
= tmpdev
->devname
;
230 struct supertype
*tst
;
231 struct dev_policy
*pol
= NULL
;
232 int found_container
= 0;
234 if (tmpdev
->used
> 1)
237 if (ident
->container
) {
238 if (ident
->container
[0] == '/' &&
239 !same_dev(ident
->container
, devname
)) {
241 pr_err("%s is not the container required (%s)\n",
242 devname
, ident
->container
);
245 } else if (ident
->devices
&&
246 !match_oneof(ident
->devices
, devname
)) {
247 /* Note that we ignore the "device=" identifier if a
248 * "container=" is given. Checking both is unnecessarily
252 pr_err("%s is not one of %s\n", devname
, ident
->devices
);
258 dfd
= dev_open(devname
, O_RDONLY
);
261 pr_err("cannot open device %s: %s\n",
262 devname
, strerror(errno
));
264 } else if (!fstat_is_blkdev(dfd
, devname
, &rdev
)) {
266 } else if (must_be_container(dfd
)) {
268 /* already found some components, this cannot
272 pr_err("%s is a container, but we are looking for components\n",
275 } if (!tst
&& (tst
= super_by_fd(dfd
, NULL
)) == NULL
) {
277 pr_err("not a recognisable container: %s\n",
280 } else if (!tst
->ss
->load_container
||
281 tst
->ss
->load_container(tst
, dfd
, NULL
)) {
283 pr_err("no correct container type: %s\n",
286 } else if (auto_assem
&&
287 !conf_test_metadata(tst
->ss
->name
,
288 (pol
= devid_policy(rdev
)),
289 tst
->ss
->match_home(tst
, c
->homehost
) == 1)) {
291 pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
292 devname
, tst
->ss
->name
);
297 if (!tst
&& (tst
= guess_super(dfd
)) == NULL
) {
299 pr_err("no recogniseable superblock on %s\n",
302 } else if ((tst
->ignore_hw_compat
= 0),
303 tst
->ss
->load_super(tst
, dfd
,
304 report_mismatch
? devname
: NULL
)) {
306 pr_err("no RAID superblock on %s\n",
309 } else if (tst
->ss
->compare_super
== NULL
) {
311 pr_err("Cannot assemble %s metadata on %s\n",
312 tst
->ss
->name
, devname
);
314 } else if (auto_assem
&& st
== NULL
&&
315 !conf_test_metadata(tst
->ss
->name
,
316 (pol
= devid_policy(rdev
)),
317 tst
->ss
->match_home(tst
, c
->homehost
) == 1)) {
319 pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
320 devname
, tst
->ss
->name
);
324 if (dfd
>= 0) close(dfd
);
325 if (tmpdev
->used
== 2) {
326 if (auto_assem
|| !inargv
)
327 /* Ignore unrecognised devices during auto-assembly */
329 if (ident
->name
[0] ||
330 ident
->super_minor
!= UnSet
)
331 /* Ignore unrecognised device if looking for
335 pr_err("%s has no superblock - assembly aborted\n",
338 st
->ss
->free_super(st
);
339 dev_policy_free(pol
);
340 domain_free(domains
);
342 tst
->ss
->free_super(tst
);
346 if (found_container
) {
347 /* tmpdev is a container. We need to be either
348 * looking for a member, or auto-assembling
350 /* should be safe to try an exclusive open now, we
351 * have rejected anything that some other mdadm might
354 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
357 pr_err("%s is busy - skipping\n", devname
);
362 if (ident
->container
&& ident
->container
[0] != '/') {
367 tst
->ss
->getinfo_super(tst
, content
, NULL
);
369 if (!parse_uuid(ident
->container
, uuid
) ||
370 !same_uuid(content
->uuid
, uuid
, tst
->ss
->swapuuid
)) {
372 pr_err("%s has wrong UUID to be required container\n",
377 /* It is worth looking inside this container.
380 pr_err("looking in container %s\n",
383 for (content
= tst
->ss
->container_content(tst
, NULL
);
385 content
= content
->next
) {
387 if (!ident_matches(ident
, content
, tst
,
388 c
->homehost
, c
->require_homehost
,
390 report_mismatch
? devname
: NULL
))
391 /* message already printed */;
392 else if (is_member_busy(content
->text_version
)) {
394 pr_err("member %s in %s is already assembled\n",
395 content
->text_version
,
397 } else if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
398 /* do not assemble arrays with unsupported configurations */
399 pr_err("Cannot activate member %s in %s.\n",
400 content
->text_version
,
407 goto loop
; /* empty container */
410 st
= tst
; tst
= NULL
;
411 if (!auto_assem
&& inargv
&& tmpdev
->next
!= NULL
) {
412 pr_err("%s is a container, but is not only device given: confused and aborting\n",
414 st
->ss
->free_super(st
);
415 dev_policy_free(pol
);
416 domain_free(domains
);
420 pr_err("found match on member %s in %s\n",
421 content
->text_version
, devname
);
423 /* make sure we finished the loop */
428 tst
->ss
->getinfo_super(tst
, content
, NULL
);
430 if (!ident_matches(ident
, content
, tst
,
431 c
->homehost
, c
->require_homehost
,
433 report_mismatch
? devname
: NULL
))
437 /* Never auto-assemble things that conflict
438 * with mdadm.conf in some way
440 struct mddev_ident
*match
;
443 match
= conf_match(tst
, content
, devname
,
444 report_mismatch
? c
->verbose
: -1,
446 if (!match
&& rv
== 2)
448 if (match
&& match
->devname
&&
449 strcasecmp(match
->devname
, "<ignore>") == 0) {
451 pr_err("%s is a member of an explicitly ignored array\n",
455 if (match
&& !ident_matches(match
, content
, tst
,
456 c
->homehost
, c
->require_homehost
,
458 report_mismatch
? devname
: NULL
))
459 /* Array exists in mdadm.conf but some
460 * details don't match, so reject it
465 /* should be safe to try an exclusive open now, we
466 * have rejected anything that some other mdadm might
469 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
472 pr_err("%s is busy - skipping\n", devname
);
479 if (st
->minor_version
== -1)
480 st
->minor_version
= tst
->minor_version
;
482 if (memcmp(content
->uuid
, uuid_zero
,
483 sizeof(int[4])) == 0) {
484 /* this is a floating spare. It cannot define
485 * an array unless there are no more arrays of
486 * this type to be found. It can be included
487 * in an array of this type though.
493 if (st
->ss
!= tst
->ss
||
494 st
->minor_version
!= tst
->minor_version
||
495 st
->ss
->compare_super(st
, tst
, 1) != 0) {
496 /* Some mismatch. If exactly one array matches this host,
497 * we can resolve on that one.
498 * Or, if we are auto assembling, we just ignore the second
504 int first
= st
->ss
->match_home(st
, c
->homehost
);
505 int last
= tst
->ss
->match_home(tst
, c
->homehost
);
507 (first
== 1 || last
== 1)) {
508 /* We can do something */
509 if (first
) {/* just ignore this one */
511 pr_err("%s misses out due to wrong homehost\n",
514 } else { /* reject all those sofar */
515 struct mddev_dev
*td
;
517 pr_err("%s overrides previous devices due to good homehost\n",
519 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
527 pr_err("superblock on %s doesn't match others - assembly aborted\n",
529 tst
->ss
->free_super(tst
);
530 st
->ss
->free_super(st
);
531 dev_policy_free(pol
);
532 domain_free(domains
);
538 /* Collect domain information from members only */
539 if (tmpdev
&& tmpdev
->used
== 1) {
541 pol
= devid_policy(rdev
);
542 domain_merge(&domains
, pol
, tst
?tst
->ss
->name
:NULL
);
544 dev_policy_free(pol
);
547 tst
->ss
->free_super(tst
);
550 /* Check if we found some imsm spares but no members */
553 memcmp(uuid_zero
, ident
->uuid
,sizeof(uuid_zero
)) == 0)) &&
555 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
556 if (tmpdev
->used
!= 3)
562 /* we need sb from one of the spares */
563 int dfd
= dev_open(tmpdev
->devname
, O_RDONLY
);
565 st
->ss
->load_super(st
, dfd
, NULL
))
572 /* Now reject spares that don't match domains of identified members */
573 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
574 if (tmpdev
->used
!= 3)
576 if (!stat_is_blkdev(tmpdev
->devname
, &rdev
)) {
579 struct dev_policy
*pol
= devid_policy(rdev
);
580 int dt
= domain_test(domains
, pol
, NULL
);
581 if (inargv
&& dt
!= 0)
582 /* take this spare as domains match
583 * if there are any */
585 else if (!inargv
&& dt
== 1)
586 /* device wasn't explicitly listed, so need
587 * explicit domain match - which we have */
590 /* if domains don't match mark as unused */
592 dev_policy_free(pol
);
595 domain_free(domains
);
597 if (st
&& st
->sb
&& content
== *contentp
)
598 st
->ss
->getinfo_super(st
, content
, NULL
);
606 int uptodate
; /* set once we decide that this device is as
607 * recent as everything else in the array.
609 int included
; /* set if the device is already in the array
610 * due to a previous '-I'
615 static int load_devices(struct devs
*devices
, char *devmap
,
616 struct mddev_ident
*ident
, struct supertype
**stp
,
617 struct mddev_dev
*devlist
, struct context
*c
,
618 struct mdinfo
*content
,
619 int mdfd
, char *mddev
,
620 int *most_recentp
, int *bestcntp
, int **bestp
,
623 struct mddev_dev
*tmpdev
;
627 int most_recent
= -1;
630 struct supertype
*st
= *stp
;
632 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) {
633 char *devname
= tmpdev
->devname
;
635 struct supertype
*tst
;
640 if (tmpdev
->used
!= 1)
642 /* looks like a good enough match to update the super block if needed */
644 /* prepare useful information in info structures */
649 if (strcmp(c
->update
, "uuid") == 0 && !ident
->uuid_set
)
650 random_uuid((__u8
*)ident
->uuid
);
652 if (strcmp(c
->update
, "ppl") == 0 &&
653 ident
->bitmap_fd
>= 0) {
654 pr_err("PPL is not compatible with bitmap\n");
661 dfd
= dev_open(devname
,
662 tmpdev
->disposition
== 'I'
663 ? O_RDWR
: (O_RDWR
|O_EXCL
));
666 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
667 pr_err("cannot re-read metadata from %s - aborting\n",
674 tst
->ss
->free_super(tst
);
679 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
681 memcpy(content
->uuid
, ident
->uuid
, 16);
682 strcpy(content
->name
, ident
->name
);
683 content
->array
.md_minor
= minor(stb2
.st_rdev
);
685 if (strcmp(c
->update
, "byteorder") == 0)
687 else if (strcmp(c
->update
, "home-cluster") == 0) {
688 tst
->cluster_name
= c
->homecluster
;
689 err
= tst
->ss
->write_bitmap(tst
, dfd
, NameUpdate
);
690 } else if (strcmp(c
->update
, "nodes") == 0) {
691 tst
->nodes
= c
->nodes
;
692 err
= tst
->ss
->write_bitmap(tst
, dfd
, NodeNumUpdate
);
693 } else if (strcmp(c
->update
, "revert-reshape") == 0 &&
695 err
= tst
->ss
->update_super(tst
, content
,
696 "revert-reshape-nobackup",
701 err
= tst
->ss
->update_super(tst
, content
, c
->update
,
707 pr_err("--update=%s not understood for %s metadata\n",
708 c
->update
, tst
->ss
->name
);
709 tst
->ss
->free_super(tst
);
718 if (strcmp(c
->update
, "uuid")==0 &&
721 memcpy(ident
->uuid
, content
->uuid
, 16);
723 if (tst
->ss
->store_super(tst
, dfd
))
724 pr_err("Could not re-write superblock on %s.\n",
727 if (strcmp(c
->update
, "uuid")==0 &&
728 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
729 if (bitmap_update_uuid(ident
->bitmap_fd
,
731 tst
->ss
->swapuuid
) != 0)
732 pr_err("Could not update uuid on external bitmap.\n");
737 dfd
= dev_open(devname
,
738 tmpdev
->disposition
== 'I'
739 ? O_RDWR
: (O_RDWR
|O_EXCL
));
742 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
743 pr_err("cannot re-read metadata from %s - aborting\n",
750 tst
->ss
->free_super(tst
);
755 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
762 pr_err("%s is identified as a member of %s, slot %d%s.\n",
763 devname
, mddev
, content
->disk
.raid_disk
,
764 (content
->disk
.state
& (1<<MD_DISK_REPLACEMENT
)) ? " replacement":"");
765 devices
[devcnt
].devname
= devname
;
766 devices
[devcnt
].uptodate
= 0;
767 devices
[devcnt
].included
= (tmpdev
->disposition
== 'I');
768 devices
[devcnt
].i
= *content
;
769 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
770 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
772 disk_state
= devices
[devcnt
].i
.disk
.state
& ~((1<<MD_DISK_FAILFAST
) |
773 (1<<MD_DISK_WRITEMOSTLY
));
774 if (disk_state
== ((1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
))) {
775 if (most_recent
< 0 ||
776 devices
[devcnt
].i
.events
777 > devices
[most_recent
].i
.events
) {
778 struct supertype
*tmp
= tst
;
781 most_recent
= devcnt
;
784 tst
->ss
->free_super(tst
);
787 if (content
->array
.level
== LEVEL_MULTIPATH
)
788 /* with multipath, the raid_disk from the superblock is meaningless */
791 i
= devices
[devcnt
].i
.disk
.raid_disk
;
792 if (i
+1 == 0 || i
== MD_DISK_ROLE_JOURNAL
) {
793 if (nextspare
< content
->array
.raid_disks
*2)
794 nextspare
= content
->array
.raid_disks
*2;
797 /* i is raid_disk - double it so there is room for
800 if (devices
[devcnt
].i
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))
802 if (i
>= content
->array
.raid_disks
*2 &&
808 int newbestcnt
= i
+10;
809 int *newbest
= xmalloc(sizeof(int)*newbestcnt
);
811 for (c
=0; c
< newbestcnt
; c
++)
813 newbest
[c
] = best
[c
];
818 bestcnt
= newbestcnt
;
821 devices
[best
[i
]].i
.events
==
822 devices
[devcnt
].i
.events
&&
823 (devices
[best
[i
]].i
.disk
.minor
!=
824 devices
[devcnt
].i
.disk
.minor
) &&
826 content
->array
.level
!= LEVEL_MULTIPATH
) {
827 /* two different devices with identical superblock.
828 * Could be a mis-detection caused by overlapping
829 * partitions. fail-safe.
831 pr_err("WARNING %s and %s appear to have very similar superblocks.\n"
832 " If they are really different, please --zero the superblock on one\n"
833 " If they are the same or overlap, please remove one from %s.\n",
834 devices
[best
[i
]].devname
, devname
,
835 inargv
? "the list" :
836 "the\n DEVICE list in mdadm.conf"
844 if (best
[i
] == -1 || (devices
[best
[i
]].i
.events
845 < devices
[devcnt
].i
.events
))
847 else if (st
->ss
== &super_imsm
)
852 if (most_recent
>= 0)
853 *most_recentp
= most_recent
;
860 static int force_array(struct mdinfo
*content
,
861 struct devs
*devices
,
862 int *best
, int bestcnt
, char *avail
,
864 struct supertype
*st
,
868 while (!enough(content
->array
.level
, content
->array
.raid_disks
,
869 content
->array
.layout
, 1,
871 (content
->reshape_active
&& content
->delta_disks
> 0 &&
872 !enough(content
->array
.level
, (content
->array
.raid_disks
873 - content
->delta_disks
),
874 content
->new_layout
, 1, avail
))) {
875 /* Choose the newest best drive which is
876 * not up-to-date, update the superblock
880 struct supertype
*tst
;
881 unsigned long long current_events
;
882 int chosen_drive
= -1;
886 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
891 if (devices
[j
].uptodate
)
893 if (devices
[j
].i
.recovery_start
!= MaxSector
) {
895 if (!devices
[j
].i
.reshape_active
||
896 devices
[j
].i
.delta_disks
<= 0)
898 /* When increasing number of devices, an
899 * added device also appears to be
900 * recovering. It is safe to include it
901 * as long as it won't be a source of
903 * For now, just allow for last data
904 * devices in RAID4 or last devices in RAID4/5/6.
906 delta
= devices
[j
].i
.delta_disks
;
907 if (devices
[j
].i
.array
.level
>= 4 &&
908 devices
[j
].i
.array
.level
<= 6 &&
909 i
/2 >= content
->array
.raid_disks
- delta
)
911 else if (devices
[j
].i
.array
.level
== 4 &&
912 i
/2 >= content
->array
.raid_disks
- delta
- 1)
916 } else if (devices
[j
].i
.reshape_active
!=
917 content
->reshape_active
||
918 (devices
[j
].i
.reshape_active
&&
919 devices
[j
].i
.reshape_progress
!=
920 content
->reshape_progress
))
921 /* Here, it may be a source of data. If two
922 * devices claim different progresses, it
923 * means that reshape boundaries differ for
924 * their own devices. Kernel will only treat
925 * the first one as reshape progress and
926 * go on. It may cause disaster, so avoid it.
929 if (chosen_drive
< 0 ||
931 > devices
[chosen_drive
].i
.events
)
934 if (chosen_drive
< 0)
936 current_events
= devices
[chosen_drive
].i
.events
;
939 pr_err("forcing event count in %s(%d) from %d up to %d\n",
940 devices
[chosen_drive
].devname
,
941 devices
[chosen_drive
].i
.disk
.raid_disk
,
942 (int)(devices
[chosen_drive
].i
.events
),
943 (int)(devices
[most_recent
].i
.events
));
944 fd
= dev_open(devices
[chosen_drive
].devname
,
945 devices
[chosen_drive
].included
? O_RDWR
948 pr_err("Couldn't open %s for write - not updating\n",
949 devices
[chosen_drive
].devname
);
950 devices
[chosen_drive
].i
.events
= 0;
954 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
956 pr_err("RAID superblock disappeared from %s - not updating.\n",
957 devices
[chosen_drive
].devname
);
958 devices
[chosen_drive
].i
.events
= 0;
961 content
->events
= devices
[most_recent
].i
.events
;
962 tst
->ss
->update_super(tst
, content
, "force-one",
963 devices
[chosen_drive
].devname
, c
->verbose
,
966 if (tst
->ss
->store_super(tst
, fd
)) {
968 pr_err("Could not re-write superblock on %s\n",
969 devices
[chosen_drive
].devname
);
970 devices
[chosen_drive
].i
.events
= 0;
971 tst
->ss
->free_super(tst
);
975 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
976 devices
[chosen_drive
].uptodate
= 1;
977 avail
[chosen_drive
] = 1;
979 tst
->ss
->free_super(tst
);
980 /* If there are any other drives of the same vintage,
981 * add them in as well. We can't lose and we might gain
984 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
988 !devices
[j
].uptodate
&&
989 devices
[j
].i
.recovery_start
== MaxSector
&&
990 devices
[j
].i
.events
== current_events
&&
991 ((!devices
[j
].i
.reshape_active
&&
992 !content
->reshape_active
) ||
993 (devices
[j
].i
.reshape_active
==
994 content
->reshape_active
&&
995 devices
[j
].i
.reshape_progress
==
996 content
->reshape_progress
))) {
1005 static int start_array(int mdfd
,
1007 struct mdinfo
*content
,
1008 struct supertype
*st
,
1009 struct mddev_ident
*ident
,
1010 int *best
, int bestcnt
,
1012 struct devs
*devices
,
1014 unsigned int sparecnt
,
1015 unsigned int rebuilding_cnt
,
1016 unsigned int journalcnt
,
1018 int clean
, char *avail
,
1019 int start_partial_ok
,
1026 unsigned int req_cnt
;
1028 if (content
->journal_device_required
&& (content
->journal_clean
== 0)) {
1030 pr_err("Not safe to assemble with missing or stale journal device, consider --force.\n");
1033 pr_err("Journal is missing or stale, starting array read only.\n");
1037 if (content
->consistency_policy
== CONSISTENCY_POLICY_PPL
)
1040 rv
= set_array_info(mdfd
, st
, content
);
1041 if (rv
&& !err_ok
) {
1042 pr_err("failed to set array info for %s: %s\n",
1043 mddev
, strerror(errno
));
1046 if (ident
->bitmap_fd
>= 0) {
1047 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
1048 pr_err("SET_BITMAP_FILE failed.\n");
1051 } else if (ident
->bitmap_file
) {
1052 /* From config file */
1053 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
1055 pr_err("Could not open bitmap file %s\n",
1056 ident
->bitmap_file
);
1059 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
1060 pr_err("Failed to set bitmapfile for %s\n", mddev
);
1067 /* First, add the raid disks, but add the chosen one last */
1068 for (i
= 0; i
<= bestcnt
; i
++) {
1072 if (j
== chosen_drive
)
1077 if (j
>= 0 && !devices
[j
].included
) {
1080 dfd
= dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
1082 remove_partitions(dfd
);
1085 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
1088 pr_err("failed to add %s to %s: %s\n",
1089 devices
[j
].devname
, mddev
,
1091 if (errno
== EINVAL
&& content
->array
.level
== 0 &&
1092 content
->array
.layout
!= 0) {
1093 cont_err("Possibly your kernel doesn't support RAID0 layouts.\n");
1094 cont_err("Please upgrade.\n");
1096 if (i
< content
->array
.raid_disks
* 2 ||
1101 } else if (c
->verbose
> 0) {
1102 pr_err("added %s to %s as %d%s%s\n",
1103 devices
[j
].devname
, mddev
,
1104 devices
[j
].i
.disk
.raid_disk
,
1105 devices
[j
].uptodate
?"":
1106 " (possibly out of date)",
1107 (devices
[j
].i
.disk
.state
&
1108 (1<<MD_DISK_REPLACEMENT
)) ?
1111 } else if (j
>= 0) {
1113 pr_err("%s is already in %s as %d\n",
1114 devices
[j
].devname
, mddev
,
1115 devices
[j
].i
.disk
.raid_disk
);
1116 } else if (c
->verbose
> 0 &&
1117 i
< content
->array
.raid_disks
* 2 && (i
& 1) == 0)
1118 pr_err("no uptodate device for slot %d of %s\n",
1122 if (content
->array
.level
== LEVEL_CONTAINER
) {
1123 if (c
->verbose
>= 0) {
1124 pr_err("Container %s has been assembled with %d drive%s",
1125 mddev
, okcnt
+ sparecnt
+ journalcnt
,
1126 okcnt
+ sparecnt
+ journalcnt
== 1 ? "" : "s");
1127 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1128 fprintf(stderr
, " (out of %d)\n",
1129 content
->array
.raid_disks
);
1131 fprintf(stderr
, "\n");
1132 sysfs_rules_apply(mddev
, content
);
1136 if (st
->ss
->validate_container
) {
1137 struct mdinfo
*devices_list
;
1138 struct mdinfo
*info_devices
;
1141 devices_list
= NULL
;
1142 info_devices
= xmalloc(sizeof(struct mdinfo
) *
1143 (okcnt
+ sparecnt
));
1144 for (count
= 0; count
< okcnt
+ sparecnt
; count
++) {
1145 info_devices
[count
] = devices
[count
].i
;
1146 info_devices
[count
].next
= devices_list
;
1147 devices_list
= &info_devices
[count
];
1149 if (st
->ss
->validate_container(devices_list
))
1150 pr_err("Mismatch detected!\n");
1154 st
->ss
->free_super(st
);
1155 sysfs_uevent(content
, "change");
1156 if (err_ok
&& okcnt
< (unsigned)content
->array
.raid_disks
)
1157 /* Was partial, is still partial, so signal an error
1158 * to ensure we don't retry */
1163 /* Get number of in-sync devices according to the superblock.
1164 * We must have this number to start the array without -s or -R
1166 req_cnt
= content
->array
.working_disks
;
1168 if (c
->runstop
== 1 ||
1170 (enough(content
->array
.level
, content
->array
.raid_disks
,
1171 content
->array
.layout
, clean
, avail
) &&
1172 (okcnt
+ rebuilding_cnt
>= req_cnt
|| start_partial_ok
)))) {
1173 /* This array is good-to-go.
1174 * If a reshape is in progress then we might need to
1175 * continue monitoring it. In that case we start
1176 * it read-only and let the grow code make it writable.
1180 if (content
->reshape_active
&&
1181 !(content
->reshape_active
& RESHAPE_NO_BACKUP
) &&
1182 content
->delta_disks
<= 0) {
1183 if (!c
->backup_file
) {
1184 pr_err("%s: Need a backup file to complete reshape of this array.\n",
1186 pr_err("Please provided one with \"--backup-file=...\"\n");
1188 strcmp(c
->update
, "revert-reshape") == 0)
1189 pr_err("(Don't specify --update=revert-reshape again, that part succeeded.)\n");
1192 rv
= sysfs_set_str(content
, NULL
,
1193 "array_state", "readonly");
1195 rv
= Grow_continue(mdfd
, st
, content
,
1198 } else if (c
->readonly
&&
1199 sysfs_attribute_available(content
, NULL
,
1201 rv
= sysfs_set_str(content
, NULL
,
1202 "array_state", "readonly");
1204 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1205 reopen_mddev(mdfd
); /* drop O_EXCL */
1207 sysfs_rules_apply(mddev
, content
);
1208 if (c
->verbose
>= 0) {
1209 pr_err("%s has been started with %d drive%s",
1210 mddev
, okcnt
, okcnt
==1?"":"s");
1211 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1212 fprintf(stderr
, " (out of %d)",
1213 content
->array
.raid_disks
);
1215 fprintf(stderr
, "%s %d rebuilding",
1216 sparecnt
?",":" and",
1219 fprintf(stderr
, " and %d spare%s",
1221 sparecnt
== 1 ? "" : "s");
1222 if (content
->journal_clean
)
1223 fprintf(stderr
, " and %d journal",
1225 fprintf(stderr
, ".\n");
1227 if (content
->reshape_active
&&
1228 content
->array
.level
>= 4 &&
1229 content
->array
.level
<= 6) {
1230 /* might need to increase the size
1231 * of the stripe cache - default is 256
1233 int chunk_size
= content
->array
.chunk_size
;
1235 if (content
->reshape_active
&&
1236 content
->new_chunk
> chunk_size
)
1237 chunk_size
= content
->new_chunk
;
1238 if (256 < 4 * ((chunk_size
+4065)/4096)) {
1241 sra
= sysfs_read(mdfd
, NULL
, 0);
1243 sysfs_set_num(sra
, NULL
,
1244 "stripe_cache_size",
1245 (4 * chunk_size
/ 4096) + 1);
1249 if (okcnt
< (unsigned)content
->array
.raid_disks
) {
1250 /* If any devices did not get added
1251 * because the kernel rejected them based
1252 * on event count, try adding them
1253 * again providing the action policy is
1254 * 're-add' or greater. The bitmap
1255 * might allow them to be included, or
1256 * they will become spares.
1258 for (i
= 0; i
< bestcnt
; i
++) {
1260 if (j
>= 0 && !devices
[j
].uptodate
) {
1261 if (!disk_action_allows(&devices
[j
].i
, st
->ss
->name
, act_re_add
))
1263 rv
= add_disk(mdfd
, st
, content
,
1265 if (rv
== 0 && c
->verbose
>= 0)
1266 pr_err("%s has been re-added.\n",
1267 devices
[j
].devname
);
1271 if (content
->array
.level
== 6 &&
1272 okcnt
+ 1 == (unsigned)content
->array
.raid_disks
&&
1276 sra
= sysfs_read(mdfd
, NULL
, 0);
1278 sysfs_set_str(sra
, NULL
,
1279 "sync_action", "repair");
1284 pr_err("failed to RUN_ARRAY %s: %s\n", mddev
, strerror(errno
));
1285 if (errno
== 524 /* ENOTSUP */ &&
1286 content
->array
.level
== 0 && content
->array
.layout
== 0)
1287 cont_err("Please use --update=layout-original or --update=layout-alternate\n");
1289 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1290 content
->array
.layout
, 1, avail
))
1291 pr_err("Not enough devices to start the array.\n");
1292 else if (!enough(content
->array
.level
,
1293 content
->array
.raid_disks
,
1294 content
->array
.layout
, clean
, avail
))
1295 pr_err("Not enough devices to start the array while not clean - consider --force.\n");
1299 if (c
->runstop
== -1) {
1300 pr_err("%s assembled from %d drive%s",
1301 mddev
, okcnt
, okcnt
== 1 ? "" : "s");
1302 if (okcnt
!= (unsigned)content
->array
.raid_disks
)
1303 fprintf(stderr
, " (out of %d)",
1304 content
->array
.raid_disks
);
1305 fprintf(stderr
, ", but not started.\n");
1308 if (c
->verbose
>= -1) {
1309 pr_err("%s assembled from %d drive%s",
1310 mddev
, okcnt
, okcnt
== 1 ? "" : "s");
1312 fprintf(stderr
, "%s %d rebuilding",
1313 sparecnt
? "," : " and", rebuilding_cnt
);
1315 fprintf(stderr
, " and %d spare%s", sparecnt
,
1316 sparecnt
== 1 ? "" : "s");
1317 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1318 content
->array
.layout
, 1, avail
))
1319 fprintf(stderr
, " - not enough to start the array.\n");
1320 else if (!enough(content
->array
.level
,
1321 content
->array
.raid_disks
,
1322 content
->array
.layout
, clean
, avail
))
1323 fprintf(stderr
, " - not enough to start the array while not clean - consider --force.\n");
1325 if (req_cnt
== (unsigned)content
->array
.raid_disks
)
1326 fprintf(stderr
, " - need all %d to start it",
1329 fprintf(stderr
, " - need %d to start", req_cnt
);
1330 fprintf(stderr
, " (use --run to insist).\n");
1336 int Assemble(struct supertype
*st
, char *mddev
,
1337 struct mddev_ident
*ident
,
1338 struct mddev_dev
*devlist
,
1342 * The task of Assemble is to find a collection of
1343 * devices that should (according to their superblocks)
1344 * form an array, and to give this collection to the MD driver.
1345 * In Linux-2.4 and later, this involves submitting a
1346 * SET_ARRAY_INFO ioctl with no arg - to prepare
1347 * the array - and then submit a number of
1348 * ADD_NEW_DISK ioctls to add disks into
1349 * the array. Finally RUN_ARRAY might
1350 * be submitted to start the array.
1352 * Much of the work of Assemble is in finding and/or
1353 * checking the disks to make sure they look right.
1355 * If mddev is not set, then scan must be set and we
1356 * read through the config file for dev+uuid mapping
1357 * We recurse, setting mddev, for each device that
1359 * - has a valid uuid (or any uuid if !uuidset)
1361 * If mddev is set, we try to determine state of md.
1362 * check version - must be at least 0.90.0
1363 * check kernel version. must be at least 2.4.
1364 * If not, we can possibly fall back on START_ARRAY
1365 * Try to GET_ARRAY_INFO.
1366 * If possible, give up
1367 * If not, try to STOP_ARRAY just to make sure
1369 * If !uuidset and scan, look in conf-file for uuid
1370 * If not found, give up
1371 * If !devlist and scan and uuidset, get list of devs from conf-file
1374 * Check superblock - discard if bad
1375 * Check uuid (set if we don't have one) - discard if no match
1376 * Check superblock similarity if we have a superblock - discard if different
1377 * Record events, devicenum
1378 * This should give us a list of devices for the array
1379 * We should collect the most recent event number
1381 * Count disks with recent enough event count
1382 * While force && !enough disks
1383 * Choose newest rejected disks, update event count
1384 * mark clean and rewrite superblock
1387 * foreach device with recent events : ADD_NEW_DISK
1388 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
1390 * Check the device numbers in superblock are right
1391 * update superblock if any changes
1398 int auto_assem
= (mddev
== NULL
&& !ident
->uuid_set
&&
1399 ident
->super_minor
== UnSet
&& ident
->name
[0] == 0 &&
1400 (ident
->container
== NULL
|| ident
->member
== NULL
));
1401 struct devs
*devices
= NULL
;
1403 int *best
= NULL
; /* indexed by raid_disk */
1406 unsigned int okcnt
, sparecnt
, rebuilding_cnt
, replcnt
, journalcnt
;
1407 int journal_clean
= 0;
1410 int most_recent
= 0;
1414 int start_partial_ok
= (c
->runstop
>= 0) &&
1415 (c
->force
|| devlist
==NULL
|| auto_assem
);
1417 struct mddev_dev
*tmpdev
;
1419 struct mdinfo
*content
= NULL
;
1420 struct mdinfo
*pre_exist
= NULL
;
1423 char chosen_name
[1024];
1424 struct map_ent
*map
= NULL
;
1428 * If any subdevs are listed, then any that don't
1429 * match ident are discarded. Remainder must all match and
1431 * If no subdevs, then we scan all devices in the config file, but
1432 * there must be something in the identity
1436 ident
->uuid_set
== 0 &&
1437 (ident
->super_minor
< 0 || ident
->super_minor
== UnSet
) &&
1438 ident
->name
[0] == 0 &&
1439 (ident
->container
== NULL
|| ident
->member
== NULL
) &&
1440 ident
->devices
== NULL
) {
1441 pr_err("No identity information available for %s - cannot assemble.\n",
1442 mddev
? mddev
: "further assembly");
1446 if (devlist
== NULL
)
1447 devlist
= conf_get_devs();
1452 /* We come back here when doing auto-assembly and attempting some
1453 * set of devices failed. Those are now marked as ->used==2 and
1454 * we ignore them and try again
1456 if (!st
&& ident
->st
)
1459 pr_err("looking for devices for %s\n",
1460 mddev
? mddev
: "further assembly");
1464 st
->ignore_hw_compat
= 1;
1465 num_devs
= select_devices(devlist
, ident
, &st
, &content
, c
,
1466 inargv
, auto_assem
);
1470 if (!st
|| !st
->sb
|| !content
)
1473 /* We have a full set of devices - we now need to find the
1475 * However there is a risk that we are racing with "mdadm -I"
1476 * and the array is already partially assembled - we will have
1477 * rejected any devices already in this address.
1478 * So we take a lock on the map file - to prevent further races -
1479 * and look for the uuid in there. If found and the array is
1480 * active, we abort. If found and the array is not active
1481 * we commit to that md device and add all the contained devices
1482 * to our list. We flag them so that we don't try to re-add,
1483 * but can remove if they turn out to not be wanted.
1486 pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
1487 if (c
->update
&& strcmp(c
->update
,"uuid") == 0)
1490 mp
= map_by_uuid(&map
, content
->uuid
);
1493 /* array already exists. */
1494 pre_exist
= sysfs_read(-1, mp
->devnm
, GET_LEVEL
|GET_DEVS
);
1495 if (pre_exist
->array
.level
!= UnSet
) {
1496 pr_err("Found some drive for an array that is already active: %s\n",
1498 pr_err("giving up.\n");
1501 for (dv
= pre_exist
->devs
; dv
; dv
= dv
->next
) {
1502 /* We want to add this device to our list,
1503 * but it could already be there if "mdadm -I"
1504 * started *after* we checked for O_EXCL.
1505 * If we add it to the top of the list
1506 * it will be preferred over later copies.
1508 struct mddev_dev
*newdev
;
1509 char *devname
= map_dev(dv
->disk
.major
,
1514 newdev
= xmalloc(sizeof(*newdev
));
1515 newdev
->devname
= devname
;
1516 newdev
->disposition
= 'I';
1518 newdev
->next
= devlist
;
1522 strcpy(chosen_name
, mp
->path
);
1523 if (c
->verbose
> 0 || mddev
== NULL
||
1524 strcmp(mddev
, chosen_name
) != 0)
1525 pr_err("Merging with already-assembled %s\n",
1527 mdfd
= open_dev_excl(mp
->devnm
);
1529 int trustworthy
= FOREIGN
;
1530 name
= content
->name
;
1531 switch (st
->ss
->match_home(st
, c
->homehost
)
1532 ?: st
->ss
->match_home(st
, "any")) {
1534 trustworthy
= LOCAL
;
1535 name
= strchr(content
->name
, ':');
1539 name
= content
->name
;
1542 if (mddev
&& map_by_name(&map
, mddev
) != NULL
) {
1543 pr_err("Cannot create device with %s because is in use\n", mddev
);
1547 /* If the array is listed in mdadm.conf or on
1548 * command line, then we trust the name
1549 * even if the array doesn't look local
1551 trustworthy
= LOCAL
;
1554 content
->array
.level
== LEVEL_CONTAINER
) {
1555 name
= content
->text_version
;
1556 trustworthy
= METADATA
;
1559 if (name
[0] && trustworthy
!= LOCAL
&&
1560 ! c
->require_homehost
&&
1561 conf_name_is_free(name
))
1562 trustworthy
= LOCAL
;
1564 if (trustworthy
== LOCAL
&&
1566 /* Ignore 'host:' prefix of name */
1567 name
= strchr(name
, ':')+1;
1569 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
1573 st
->ss
->free_super(st
);
1578 mddev
= chosen_name
;
1579 if (pre_exist
== NULL
) {
1580 if (mddev_busy(fd2devnm(mdfd
))) {
1581 pr_err("%s already active, cannot restart it!\n",
1583 for (tmpdev
= devlist
;
1584 tmpdev
&& tmpdev
->used
!= 1;
1585 tmpdev
= tmpdev
->next
)
1587 if (tmpdev
&& auto_assem
)
1588 pr_err("%s needed for %s...\n",
1589 mddev
, tmpdev
->devname
);
1592 st
->ss
->free_super(st
);
1597 /* just incase it was started but has no content */
1598 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1601 if (content
!= &info
) {
1602 /* This is a member of a container. Try starting the array. */
1604 err
= assemble_container_content(st
, mdfd
, content
, c
,
1610 /* Ok, no bad inconsistancy, we can try updating etc */
1611 devices
= xcalloc(num_devs
, sizeof(*devices
));
1612 devmap
= xcalloc(num_devs
, content
->array
.raid_disks
);
1613 devcnt
= load_devices(devices
, devmap
, ident
, &st
, devlist
,
1614 c
, content
, mdfd
, mddev
,
1615 &most_recent
, &bestcnt
, &best
, inargv
);
1619 * devices is already freed in load_devices, so set devices
1620 * to NULL to avoid double free devices.
1627 pr_err("no devices found for %s\n",
1630 st
->ss
->free_super(st
);
1635 if (c
->update
&& strcmp(c
->update
, "byteorder")==0)
1636 st
->minor_version
= 90;
1638 st
->ss
->getinfo_super(st
, content
, NULL
);
1639 clean
= content
->array
.state
& 1;
1641 /* now we have some devices that might be suitable.
1644 avail
= xcalloc(content
->array
.raid_disks
, 1);
1650 for (i
=0; i
< bestcnt
; i
++) {
1652 int event_margin
= 1; /* always allow a difference of '1'
1653 * like the kernel does
1655 if (j
< 0) continue;
1656 /* note: we ignore error flags in multipath arrays
1657 * as they don't make sense
1659 if (content
->array
.level
!= LEVEL_MULTIPATH
) {
1660 if (devices
[j
].i
.disk
.state
& (1<<MD_DISK_JOURNAL
)) {
1661 if (content
->journal_device_required
)
1663 else /* unexpected journal, mark as faulty */
1664 devices
[j
].i
.disk
.state
|= (1<<MD_DISK_FAULTY
);
1665 } else if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_ACTIVE
))) {
1666 if (!(devices
[j
].i
.disk
.state
1667 & (1<<MD_DISK_FAULTY
))) {
1668 devices
[j
].uptodate
= 1;
1674 /* If this device thinks that 'most_recent' has failed, then
1675 * we must reject this device.
1677 if (j
!= most_recent
&& !c
->force
&&
1678 content
->array
.raid_disks
> 0 &&
1679 devices
[most_recent
].i
.disk
.raid_disk
>= 0 &&
1680 devmap
[j
* content
->array
.raid_disks
+ devices
[most_recent
].i
.disk
.raid_disk
] == 0) {
1681 if (c
->verbose
> -1)
1682 pr_err("ignoring %s as it reports %s as failed\n",
1683 devices
[j
].devname
, devices
[most_recent
].devname
);
1687 /* Require event counter to be same as, or just less than,
1688 * most recent. If it is bigger, it must be a stray spare and
1689 * should be ignored.
1691 if (devices
[j
].i
.events
+event_margin
>=
1692 devices
[most_recent
].i
.events
&&
1693 devices
[j
].i
.events
<=
1694 devices
[most_recent
].i
.events
1696 devices
[j
].uptodate
= 1;
1697 if (devices
[j
].i
.disk
.state
& (1<<MD_DISK_JOURNAL
))
1699 if (i
< content
->array
.raid_disks
* 2) {
1700 if (devices
[j
].i
.recovery_start
== MaxSector
||
1701 (content
->reshape_active
&&
1702 i
>= content
->array
.raid_disks
- content
->delta_disks
)) {
1710 } else if (devices
[j
].i
.disk
.raid_disk
!= MD_DISK_ROLE_JOURNAL
)
1716 int force_ok
= force_array(content
, devices
, best
, bestcnt
,
1717 avail
, most_recent
, st
, c
);
1722 /* Now we want to look at the superblock which the kernel will base things on
1723 * and compare the devices that we think are working with the devices that the
1724 * superblock thinks are working.
1725 * If there are differences and --force is given, then update this chosen
1729 st
->ss
->free_super(st
);
1730 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
+=2) {
1736 if (!devices
[j
].uptodate
)
1738 if (devices
[j
].i
.events
< devices
[most_recent
].i
.events
)
1741 if ((fd
=dev_open(devices
[j
].devname
,
1742 devices
[j
].included
? O_RDONLY
1743 : (O_RDONLY
|O_EXCL
)))< 0) {
1744 pr_err("Cannot open %s: %s\n",
1745 devices
[j
].devname
, strerror(errno
));
1748 if (st
->ss
->load_super(st
,fd
, NULL
)) {
1750 pr_err("RAID superblock has disappeared from %s\n",
1751 devices
[j
].devname
);
1756 if (st
->sb
== NULL
) {
1757 pr_err("No suitable drives found for %s\n", mddev
);
1760 st
->ss
->getinfo_super(st
, content
, NULL
);
1761 if (sysfs_init(content
, mdfd
, NULL
)) {
1762 pr_err("Unable to initialize sysfs\n");
1766 /* after reload context, store journal_clean in context */
1767 content
->journal_clean
= journal_clean
;
1768 for (i
=0; i
<bestcnt
; i
++) {
1770 unsigned int desired_state
;
1774 if (devices
[j
].i
.disk
.raid_disk
== MD_DISK_ROLE_JOURNAL
)
1775 desired_state
= (1<<MD_DISK_JOURNAL
);
1776 else if (i
>= content
->array
.raid_disks
* 2)
1779 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_REPLACEMENT
);
1781 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
1783 desired_state
|= devices
[j
].i
.disk
.state
& ((1<<MD_DISK_FAILFAST
) |
1784 (1<<MD_DISK_WRITEMOSTLY
));
1786 if (!devices
[j
].uptodate
)
1789 devices
[j
].i
.disk
.state
= desired_state
;
1790 if (!(devices
[j
].i
.array
.state
& 1))
1793 if (st
->ss
->update_super(st
, &devices
[j
].i
, "assemble", NULL
,
1794 c
->verbose
, 0, NULL
)) {
1796 if (c
->verbose
>= 0)
1797 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
1798 j
, mddev
, devices
[j
].devname
);
1801 if (c
->verbose
>= -1)
1802 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
1803 i
, mddev
, devices
[j
].devname
);
1807 if (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
1808 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1813 if (c
->force
&& !clean
&&
1814 !enough(content
->array
.level
, content
->array
.raid_disks
,
1815 content
->array
.layout
, clean
,
1817 change
+= st
->ss
->update_super(st
, content
, "force-array",
1818 devices
[chosen_drive
].devname
, c
->verbose
,
1826 fd
= dev_open(devices
[chosen_drive
].devname
,
1827 devices
[chosen_drive
].included
?
1828 O_RDWR
: (O_RDWR
|O_EXCL
));
1830 pr_err("Could not open %s for write - cannot Assemble array.\n",
1831 devices
[chosen_drive
].devname
);
1834 if (st
->ss
->store_super(st
, fd
)) {
1836 pr_err("Could not re-write superblock on %s\n",
1837 devices
[chosen_drive
].devname
);
1840 if (c
->verbose
>= 0)
1841 pr_err("Marking array %s as 'clean'\n",
1846 /* If we are in the middle of a reshape we may need to restore saved data
1847 * that was moved aside due to the reshape overwriting live data
1848 * The code of doing this lives in Grow.c
1850 if (content
->reshape_active
&&
1851 !(content
->reshape_active
& RESHAPE_NO_BACKUP
)) {
1853 int *fdlist
= xmalloc(sizeof(int)* bestcnt
);
1855 pr_err("%s has an active reshape - checking if critical section needs to be restored\n",
1857 if (!c
->backup_file
)
1858 c
->backup_file
= locate_backup(content
->sys_name
);
1859 enable_fds(bestcnt
/2);
1860 for (i
= 0; i
< bestcnt
/2; i
++) {
1863 fdlist
[i
] = dev_open(devices
[j
].devname
,
1865 ? O_RDWR
: (O_RDWR
|O_EXCL
));
1866 if (fdlist
[i
] < 0) {
1867 pr_err("Could not open %s for write - cannot Assemble array.\n",
1868 devices
[j
].devname
);
1876 if (st
->ss
->external
&& st
->ss
->recover_backup
)
1877 err
= st
->ss
->recover_backup(st
, content
);
1879 err
= Grow_restart(st
, content
, fdlist
, bestcnt
/2,
1880 c
->backup_file
, c
->verbose
> 0);
1881 if (err
&& c
->invalid_backup
) {
1883 pr_err("continuing without restoring backup\n");
1889 if (fdlist
[i
]>=0) close(fdlist
[i
]);
1893 pr_err("Failed to restore critical section for reshape, sorry.\n");
1894 if (c
->backup_file
== NULL
)
1895 cont_err("Possibly you needed to specify the --backup-file\n");
1900 /* Almost ready to actually *do* something */
1901 /* First, fill in the map, so that udev can find our name
1902 * as soon as we become active.
1904 if (c
->update
&& strcmp(c
->update
, "metadata")==0) {
1905 content
->array
.major_version
= 1;
1906 content
->array
.minor_version
= 0;
1907 strcpy(content
->text_version
, "1.0");
1910 map_update(&map
, fd2devnm(mdfd
), content
->text_version
,
1911 content
->uuid
, chosen_name
);
1913 rv
= start_array(mdfd
, mddev
, content
,
1914 st
, ident
, best
, bestcnt
,
1915 chosen_drive
, devices
, okcnt
, sparecnt
,
1916 rebuilding_cnt
, journalcnt
,
1918 clean
, avail
, start_partial_ok
,
1921 if (rv
== 1 && !pre_exist
)
1922 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1927 wait_for(chosen_name
, mdfd
);
1931 /* There is a nasty race with 'mdadm --monitor'.
1932 * If it opens this device before we close it,
1933 * it gets an incomplete open on which IO
1934 * doesn't work and the capacity is
1936 * If we reopen (to check for layered devices)
1937 * before --monitor closes, we loose.
1939 * So: wait upto 1 second for there to be
1940 * a non-zero capacity.
1942 while (usecs
< 1000) {
1943 mdfd
= open(mddev
, O_RDONLY
);
1945 unsigned long long size
;
1946 if (get_dev_size(mdfd
, NULL
, &size
) &&
1955 } else if (mdfd
>= 0)
1958 /* '2' means 'OK, but not started yet' */
1963 return rv
== 2 ? 0 : rv
;
1966 int assemble_container_content(struct supertype
*st
, int mdfd
,
1967 struct mdinfo
*content
, struct context
*c
,
1968 char *chosen_name
, int *result
)
1970 struct mdinfo
*dev
, *sra
, *dev2
;
1971 struct assembly_array_info array
= {chosen_name
, 0, 0, 0};
1976 int is_raid456
, is_clean
, all_disks
;
1978 if (sysfs_init(content
, mdfd
, NULL
)) {
1979 pr_err("Unable to initialize sysfs\n");
1983 sra
= sysfs_read(mdfd
, NULL
, GET_VERSION
|GET_DEVS
);
1984 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0) {
1985 if (content
->array
.major_version
== -1 &&
1986 content
->array
.minor_version
== -2 &&
1988 content
->text_version
[0] == '/')
1989 content
->text_version
[0] = '-';
1990 if (sysfs_set_array(content
, 9003) != 0) {
1996 /* There are two types of reshape: container wide or sub-array specific
1997 * Check if metadata requests blocking container wide reshapes
1999 start_reshape
= (content
->reshape_active
&&
2000 !((content
->reshape_active
== CONTAINER_RESHAPE
) &&
2001 (content
->array
.state
& (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))));
2003 /* Block subarray here if it is under reshape now
2004 * Do not allow for any changes in this array
2006 if (st
->ss
->external
&& content
->recovery_blocked
&& start_reshape
)
2007 block_subarray(content
);
2009 for (dev2
= sra
->devs
; dev2
; dev2
= dev2
->next
) {
2010 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
2011 if (dev2
->disk
.major
== dev
->disk
.major
&&
2012 dev2
->disk
.minor
== dev
->disk
.minor
)
2016 /* Don't want this one any more */
2017 if (sysfs_set_str(sra
, dev2
, "slot", "none") < 0 &&
2019 pr_err("Cannot remove old device %s: not updating %s\n", dev2
->sys_name
, sra
->sys_name
);
2023 sysfs_set_str(sra
, dev2
, "state", "remove");
2025 old_raid_disks
= content
->array
.raid_disks
- content
->delta_disks
;
2026 avail
= xcalloc(content
->array
.raid_disks
, 1);
2027 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
2028 if (dev
->disk
.raid_disk
>= 0)
2029 avail
[dev
->disk
.raid_disk
] = 1;
2030 if (sysfs_add_disk(content
, dev
, 1) == 0) {
2031 if (dev
->disk
.raid_disk
>= old_raid_disks
&&
2032 content
->reshape_active
)
2036 } else if (errno
== EEXIST
)
2037 array
.preexist_cnt
++;
2041 all_disks
= array
.new_cnt
+ array
.exp_cnt
+ array
.preexist_cnt
;
2043 map_update(NULL
, fd2devnm(mdfd
), content
->text_version
,
2044 content
->uuid
, chosen_name
);
2046 if (content
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
2047 st
->ss
->validate_ppl
) {
2048 content
->array
.state
|= 1;
2051 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
2056 ret
= st
->ss
->validate_ppl(st
, content
, dev
);
2066 pr_err("%s contains invalid PPL - consider --force or --update-subarray with --update=no-ppl\n",
2068 content
->array
.state
&= ~1;
2069 avail
[dev
->disk
.raid_disk
] = 0;
2073 /* have --force - overwrite the invalid ppl */
2074 devpath
= map_dev(dev
->disk
.major
, dev
->disk
.minor
, 0);
2075 dfd
= dev_open(devpath
, O_RDWR
);
2077 pr_err("Failed to open %s\n", devpath
);
2082 err
= st
->ss
->write_init_ppl(st
, content
, dfd
);
2093 } else if (c
->force
) {
2094 /* Set the array as 'clean' so that we can proceed with starting
2095 * it even if we don't have all devices. Mdmon doesn't care
2096 * if the dirty flag is set in metadata, it will start managing
2098 * This is really important for raid456 (RWH case), other levels
2099 * are started anyway.
2101 content
->array
.state
|= 1;
2104 is_raid456
= (content
->array
.level
>= 4 && content
->array
.level
<= 6);
2105 is_clean
= content
->array
.state
& 1;
2107 if (enough(content
->array
.level
, content
->array
.raid_disks
,
2108 content
->array
.layout
, is_clean
, avail
) == 0) {
2109 set_array_assembly_status(c
, result
, INCR_NO
, &array
);
2111 if (c
->verbose
>= 0 && is_raid456
&& !is_clean
)
2112 pr_err("Consider --force to start dirty degraded array\n");
2119 if (c
->runstop
<= 0 && all_disks
< content
->array
.working_disks
) {
2121 set_array_assembly_status(c
, result
, INCR_UNSAFE
, &array
);
2123 if (c
->verbose
>= 0 && c
->force
)
2124 pr_err("Consider --run to start array as degraded.\n");
2128 if (is_raid456
&& content
->resync_start
!= MaxSector
&& c
->force
&&
2129 all_disks
< content
->array
.raid_disks
) {
2131 content
->resync_start
= MaxSector
;
2132 err
= sysfs_set_num(content
, NULL
, "resync_start", MaxSector
);
2136 pr_err("%s array state forced to clean. It may cause data corruption.\n",
2141 * Before activating the array, perform extra steps required
2142 * to configure the internal write-intent bitmap.
2144 if (content
->consistency_policy
== CONSISTENCY_POLICY_BITMAP
&&
2146 st
->ss
->set_bitmap(st
, content
);
2148 if (start_reshape
) {
2149 int spare
= content
->array
.raid_disks
+ array
.exp_cnt
;
2150 if (restore_backup(st
, content
,
2152 spare
, &c
->backup_file
, c
->verbose
) == 1)
2155 if (content
->reshape_progress
== 0) {
2156 /* If reshape progress is 0 - we are assembling the
2157 * array that was stopped, before reshape has started.
2158 * Array needs to be started as active, Grow_continue()
2159 * will start the reshape.
2161 sysfs_set_num(content
, NULL
, "reshape_position",
2163 err
= sysfs_set_str(content
, NULL
,
2164 "array_state", "active");
2165 sysfs_set_num(content
, NULL
, "reshape_position", 0);
2167 err
= sysfs_set_str(content
, NULL
,
2168 "array_state", "readonly");
2174 if (st
->ss
->external
) {
2175 if (!mdmon_running(st
->container_devnm
))
2176 start_mdmon(st
->container_devnm
);
2177 ping_monitor(st
->container_devnm
);
2178 if (mdmon_running(st
->container_devnm
) &&
2179 st
->update_tail
== NULL
)
2180 st
->update_tail
= &st
->updates
;
2183 err
= Grow_continue(mdfd
, st
, content
, c
->backup_file
,
2184 0, c
->freeze_reshape
);
2185 } else switch(content
->array
.level
) {
2187 case LEVEL_MULTIPATH
:
2189 err
= sysfs_set_str(content
, NULL
, "array_state",
2190 c
->readonly
? "readonly" : "active");
2193 err
= sysfs_set_str(content
, NULL
, "array_state",
2195 /* start mdmon if needed. */
2197 if (!mdmon_running(st
->container_devnm
))
2198 start_mdmon(st
->container_devnm
);
2199 ping_monitor(st
->container_devnm
);
2204 sysfs_set_safemode(content
, content
->safe_mode_delay
);
2206 /* Block subarray here if it is not reshaped now
2207 * It has be blocked a little later to allow mdmon to switch in
2210 if (st
->ss
->external
&& content
->recovery_blocked
&&
2212 block_subarray(content
);
2215 set_array_assembly_status(c
, result
, INCR_NO
, &array
);
2217 set_array_assembly_status(c
, result
, INCR_YES
, &array
);
2218 wait_for(chosen_name
, mdfd
);
2219 sysfs_rules_apply(chosen_name
, content
);
2223 /* FIXME should have an O_EXCL and wait for read-auto */