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 enum update_opt update
, char *devname
)
141 if (ident
->uuid_set
&& update
!= UOPT_UUID
&&
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
!= UOPT_NAME
&&
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
);
351 if (found_container
) {
352 /* tmpdev is a container. We need to be either
353 * looking for a member, or auto-assembling
355 /* should be safe to try an exclusive open now, we
356 * have rejected anything that some other mdadm might
359 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
362 pr_err("%s is busy - skipping\n", devname
);
367 if (ident
->container
&& ident
->container
[0] != '/') {
372 tst
->ss
->getinfo_super(tst
, content
, NULL
);
374 if (!parse_uuid(ident
->container
, uuid
) ||
375 !same_uuid(content
->uuid
, uuid
, tst
->ss
->swapuuid
)) {
377 pr_err("%s has wrong UUID to be required container\n",
382 /* It is worth looking inside this container.
385 pr_err("looking in container %s\n",
388 for (content
= tst
->ss
->container_content(tst
, NULL
);
390 content
= content
->next
) {
392 if (!ident_matches(ident
, content
, tst
,
393 c
->homehost
, c
->require_homehost
,
395 report_mismatch
? devname
: NULL
))
396 /* message already printed */;
397 else if (is_member_busy(content
->text_version
)) {
399 pr_err("member %s in %s is already assembled\n",
400 content
->text_version
,
402 } else if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
403 /* do not assemble arrays with unsupported configurations */
404 pr_err("Cannot activate member %s in %s.\n",
405 content
->text_version
,
412 goto loop
; /* empty container */
415 st
= tst
; tst
= NULL
;
416 if (!auto_assem
&& inargv
&& tmpdev
->next
!= NULL
) {
417 pr_err("%s is a container, but is not only device given: confused and aborting\n",
419 st
->ss
->free_super(st
);
420 dev_policy_free(pol
);
421 domain_free(domains
);
426 pr_err("found match on member %s in %s\n",
427 content
->text_version
, devname
);
429 /* make sure we finished the loop */
434 tst
->ss
->getinfo_super(tst
, content
, NULL
);
436 if (!ident_matches(ident
, content
, tst
,
437 c
->homehost
, c
->require_homehost
,
439 report_mismatch
? devname
: NULL
))
443 /* Never auto-assemble things that conflict
444 * with mdadm.conf in some way
446 struct mddev_ident
*match
;
449 match
= conf_match(tst
, content
, devname
,
450 report_mismatch
? c
->verbose
: -1,
452 if (!match
&& rv
== 2)
454 if (match
&& match
->devname
&&
455 strcasecmp(match
->devname
, "<ignore>") == 0) {
457 pr_err("%s is a member of an explicitly ignored array\n",
461 if (match
&& !ident_matches(match
, content
, tst
,
462 c
->homehost
, c
->require_homehost
,
464 report_mismatch
? devname
: NULL
))
465 /* Array exists in mdadm.conf but some
466 * details don't match, so reject it
471 /* should be safe to try an exclusive open now, we
472 * have rejected anything that some other mdadm might
475 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
478 pr_err("%s is busy - skipping\n", devname
);
485 if (st
->minor_version
== -1)
486 st
->minor_version
= tst
->minor_version
;
488 if (memcmp(content
->uuid
, uuid_zero
,
489 sizeof(int[4])) == 0) {
490 /* this is a floating spare. It cannot define
491 * an array unless there are no more arrays of
492 * this type to be found. It can be included
493 * in an array of this type though.
499 if (st
->ss
!= tst
->ss
||
500 st
->minor_version
!= tst
->minor_version
||
501 st
->ss
->compare_super(st
, tst
, 1) != 0) {
502 /* Some mismatch. If exactly one array matches this host,
503 * we can resolve on that one.
504 * Or, if we are auto assembling, we just ignore the second
510 int first
= st
->ss
->match_home(st
, c
->homehost
);
511 int last
= tst
->ss
->match_home(tst
, c
->homehost
);
513 (first
== 1 || last
== 1)) {
514 /* We can do something */
515 if (first
) {/* just ignore this one */
517 pr_err("%s misses out due to wrong homehost\n",
520 } else { /* reject all those sofar */
521 struct mddev_dev
*td
;
523 pr_err("%s overrides previous devices due to good homehost\n",
525 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
533 pr_err("superblock on %s doesn't match others - assembly aborted\n",
535 tst
->ss
->free_super(tst
);
536 st
->ss
->free_super(st
);
537 dev_policy_free(pol
);
538 domain_free(domains
);
545 /* Collect domain information from members only */
546 if (tmpdev
&& tmpdev
->used
== 1) {
548 pol
= devid_policy(rdev
);
549 domain_merge(&domains
, pol
, tst
?tst
->ss
->name
:NULL
);
551 dev_policy_free(pol
);
554 tst
->ss
->free_super(tst
);
559 /* Check if we found some imsm spares but no members */
562 memcmp(uuid_zero
, ident
->uuid
,sizeof(uuid_zero
)) == 0)) &&
564 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
565 if (tmpdev
->used
!= 3)
571 /* we need sb from one of the spares */
572 int dfd
= dev_open(tmpdev
->devname
, O_RDONLY
);
574 st
->ss
->load_super(st
, dfd
, NULL
))
580 /* Now reject spares that don't match domains of identified members */
581 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
582 if (tmpdev
->used
!= 3)
584 if (!stat_is_blkdev(tmpdev
->devname
, &rdev
)) {
587 struct dev_policy
*pol
= devid_policy(rdev
);
588 int dt
= domain_test(domains
, pol
, NULL
);
589 if (inargv
&& dt
!= 0)
590 /* take this spare as domains match
591 * if there are any */
593 else if (!inargv
&& dt
== 1)
594 /* device wasn't explicitly listed, so need
595 * explicit domain match - which we have */
598 /* if domains don't match mark as unused */
600 dev_policy_free(pol
);
603 domain_free(domains
);
605 if (st
&& st
->sb
&& content
== *contentp
)
606 st
->ss
->getinfo_super(st
, content
, NULL
);
614 int uptodate
; /* set once we decide that this device is as
615 * recent as everything else in the array.
617 int included
; /* set if the device is already in the array
618 * due to a previous '-I'
623 static int load_devices(struct devs
*devices
, char *devmap
,
624 struct mddev_ident
*ident
, struct supertype
**stp
,
625 struct mddev_dev
*devlist
, struct context
*c
,
626 struct mdinfo
*content
,
627 int mdfd
, char *mddev
,
628 int *most_recentp
, int *bestcntp
, int **bestp
,
631 struct mddev_dev
*tmpdev
;
635 int most_recent
= -1;
638 struct supertype
*st
= *stp
;
640 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) {
641 char *devname
= tmpdev
->devname
;
643 struct supertype
*tst
;
648 if (tmpdev
->used
!= 1)
650 /* looks like a good enough match to update the super block if needed */
652 /* prepare useful information in info structures */
657 if (c
->update
== UOPT_UUID
&& !ident
->uuid_set
)
658 random_uuid((__u8
*)ident
->uuid
);
660 if (c
->update
== UOPT_PPL
&& ident
->bitmap_fd
>= 0) {
661 pr_err("PPL is not compatible with bitmap\n");
668 dfd
= dev_open(devname
,
669 tmpdev
->disposition
== 'I'
670 ? O_RDWR
: (O_RDWR
|O_EXCL
));
673 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
674 pr_err("cannot re-read metadata from %s - aborting\n",
681 tst
->ss
->free_super(tst
);
686 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
688 memcpy(content
->uuid
, ident
->uuid
, 16);
689 strcpy(content
->name
, ident
->name
);
690 content
->array
.md_minor
= minor(stb2
.st_rdev
);
692 if (c
->update
== UOPT_BYTEORDER
)
694 else if (c
->update
== UOPT_HOME_CLUSTER
) {
695 tst
->cluster_name
= c
->homecluster
;
696 err
= tst
->ss
->write_bitmap(tst
, dfd
, NameUpdate
);
697 } else if (c
->update
== UOPT_NODES
) {
698 tst
->nodes
= c
->nodes
;
699 err
= tst
->ss
->write_bitmap(tst
, dfd
, NodeNumUpdate
);
700 } else if (c
->update
== UOPT_REVERT_RESHAPE
&& c
->invalid_backup
)
701 err
= tst
->ss
->update_super(tst
, content
,
702 UOPT_SPEC_REVERT_RESHAPE_NOBACKUP
,
707 err
= tst
->ss
->update_super(tst
, content
,
714 pr_err("--update=%s not understood for %s metadata\n",
715 map_num(update_options
, c
->update
), tst
->ss
->name
);
716 tst
->ss
->free_super(tst
);
725 if (c
->update
== UOPT_UUID
&&
728 memcpy(ident
->uuid
, content
->uuid
, 16);
730 if (tst
->ss
->store_super(tst
, dfd
))
731 pr_err("Could not re-write superblock on %s.\n",
734 if (c
->update
== UOPT_UUID
&&
735 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
736 if (bitmap_update_uuid(ident
->bitmap_fd
,
738 tst
->ss
->swapuuid
) != 0)
739 pr_err("Could not update uuid on external bitmap.\n");
744 dfd
= dev_open(devname
,
745 tmpdev
->disposition
== 'I'
746 ? O_RDWR
: (O_RDWR
|O_EXCL
));
749 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
750 pr_err("cannot re-read metadata from %s - aborting\n",
757 tst
->ss
->free_super(tst
);
762 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
769 pr_err("%s is identified as a member of %s, slot %d%s.\n",
770 devname
, mddev
, content
->disk
.raid_disk
,
771 (content
->disk
.state
& (1<<MD_DISK_REPLACEMENT
)) ? " replacement":"");
772 devices
[devcnt
].devname
= devname
;
773 devices
[devcnt
].uptodate
= 0;
774 devices
[devcnt
].included
= (tmpdev
->disposition
== 'I');
775 devices
[devcnt
].i
= *content
;
776 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
777 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
779 disk_state
= devices
[devcnt
].i
.disk
.state
& ~((1<<MD_DISK_FAILFAST
) |
780 (1<<MD_DISK_WRITEMOSTLY
));
781 if (disk_state
== ((1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
))) {
782 if (most_recent
< 0 ||
783 devices
[devcnt
].i
.events
784 > devices
[most_recent
].i
.events
) {
785 struct supertype
*tmp
= tst
;
788 most_recent
= devcnt
;
791 tst
->ss
->free_super(tst
);
794 if (content
->array
.level
== LEVEL_MULTIPATH
)
795 /* with multipath, the raid_disk from the superblock is meaningless */
798 i
= devices
[devcnt
].i
.disk
.raid_disk
;
799 if (i
+1 == 0 || i
== MD_DISK_ROLE_JOURNAL
) {
800 if (nextspare
< content
->array
.raid_disks
*2)
801 nextspare
= content
->array
.raid_disks
*2;
804 /* i is raid_disk - double it so there is room for
807 if (devices
[devcnt
].i
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))
809 if (i
>= content
->array
.raid_disks
*2 &&
815 int newbestcnt
= i
+10;
816 int *newbest
= xmalloc(sizeof(int)*newbestcnt
);
818 for (c
=0; c
< newbestcnt
; c
++)
820 newbest
[c
] = best
[c
];
825 bestcnt
= newbestcnt
;
828 devices
[best
[i
]].i
.events
==
829 devices
[devcnt
].i
.events
&&
830 (devices
[best
[i
]].i
.disk
.minor
!=
831 devices
[devcnt
].i
.disk
.minor
) &&
833 content
->array
.level
!= LEVEL_MULTIPATH
) {
834 /* two different devices with identical superblock.
835 * Could be a mis-detection caused by overlapping
836 * partitions. fail-safe.
838 pr_err("WARNING %s and %s appear to have very similar superblocks.\n"
839 " If they are really different, please --zero the superblock on one\n"
840 " If they are the same or overlap, please remove one from %s.\n",
841 devices
[best
[i
]].devname
, devname
,
842 inargv
? "the list" :
843 "the\n DEVICE list in mdadm.conf"
852 if (best
[i
] == -1 || (devices
[best
[i
]].i
.events
853 < devices
[devcnt
].i
.events
))
855 else if (st
->ss
== &super_imsm
)
860 if (most_recent
>= 0)
861 *most_recentp
= most_recent
;
868 static int force_array(struct mdinfo
*content
,
869 struct devs
*devices
,
870 int *best
, int bestcnt
, char *avail
,
872 struct supertype
*st
,
876 while (!enough(content
->array
.level
, content
->array
.raid_disks
,
877 content
->array
.layout
, 1,
879 (content
->reshape_active
&& content
->delta_disks
> 0 &&
880 !enough(content
->array
.level
, (content
->array
.raid_disks
881 - content
->delta_disks
),
882 content
->new_layout
, 1, avail
))) {
883 /* Choose the newest best drive which is
884 * not up-to-date, update the superblock
888 struct supertype
*tst
;
889 unsigned long long current_events
;
890 int chosen_drive
= -1;
894 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
899 if (devices
[j
].uptodate
)
901 if (devices
[j
].i
.recovery_start
!= MaxSector
) {
903 if (!devices
[j
].i
.reshape_active
||
904 devices
[j
].i
.delta_disks
<= 0)
906 /* When increasing number of devices, an
907 * added device also appears to be
908 * recovering. It is safe to include it
909 * as long as it won't be a source of
911 * For now, just allow for last data
912 * devices in RAID4 or last devices in RAID4/5/6.
914 delta
= devices
[j
].i
.delta_disks
;
915 if (is_level456(devices
[j
].i
.array
.level
) &&
916 i
/2 >= content
->array
.raid_disks
- delta
)
918 else if (devices
[j
].i
.array
.level
== 4 &&
919 i
/2 >= content
->array
.raid_disks
- delta
- 1)
923 } else if (devices
[j
].i
.reshape_active
!=
924 content
->reshape_active
||
925 (devices
[j
].i
.reshape_active
&&
926 devices
[j
].i
.reshape_progress
!=
927 content
->reshape_progress
))
928 /* Here, it may be a source of data. If two
929 * devices claim different progresses, it
930 * means that reshape boundaries differ for
931 * their own devices. Kernel will only treat
932 * the first one as reshape progress and
933 * go on. It may cause disaster, so avoid it.
936 if (chosen_drive
< 0 ||
938 > devices
[chosen_drive
].i
.events
)
941 if (chosen_drive
< 0)
943 current_events
= devices
[chosen_drive
].i
.events
;
946 pr_err("forcing event count in %s(%d) from %d up to %d\n",
947 devices
[chosen_drive
].devname
,
948 devices
[chosen_drive
].i
.disk
.raid_disk
,
949 (int)(devices
[chosen_drive
].i
.events
),
950 (int)(devices
[most_recent
].i
.events
));
951 fd
= dev_open(devices
[chosen_drive
].devname
,
952 devices
[chosen_drive
].included
? O_RDWR
955 pr_err("Couldn't open %s for write - not updating\n",
956 devices
[chosen_drive
].devname
);
957 devices
[chosen_drive
].i
.events
= 0;
961 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
963 pr_err("RAID superblock disappeared from %s - not updating.\n",
964 devices
[chosen_drive
].devname
);
965 devices
[chosen_drive
].i
.events
= 0;
968 content
->events
= devices
[most_recent
].i
.events
;
969 tst
->ss
->update_super(tst
, content
, UOPT_SPEC_FORCE_ONE
,
970 devices
[chosen_drive
].devname
, c
->verbose
,
973 if (tst
->ss
->store_super(tst
, fd
)) {
975 pr_err("Could not re-write superblock on %s\n",
976 devices
[chosen_drive
].devname
);
977 devices
[chosen_drive
].i
.events
= 0;
978 tst
->ss
->free_super(tst
);
982 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
983 devices
[chosen_drive
].uptodate
= 1;
984 avail
[chosen_drive
] = 1;
986 tst
->ss
->free_super(tst
);
987 /* If there are any other drives of the same vintage,
988 * add them in as well. We can't lose and we might gain
991 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
995 !devices
[j
].uptodate
&&
996 devices
[j
].i
.recovery_start
== MaxSector
&&
997 devices
[j
].i
.events
== current_events
&&
998 ((!devices
[j
].i
.reshape_active
&&
999 !content
->reshape_active
) ||
1000 (devices
[j
].i
.reshape_active
==
1001 content
->reshape_active
&&
1002 devices
[j
].i
.reshape_progress
==
1003 content
->reshape_progress
))) {
1012 static int start_array(int mdfd
,
1014 struct mdinfo
*content
,
1015 struct supertype
*st
,
1016 struct mddev_ident
*ident
,
1017 int *best
, int bestcnt
,
1019 struct devs
*devices
,
1021 unsigned int sparecnt
,
1022 unsigned int rebuilding_cnt
,
1023 unsigned int journalcnt
,
1025 int clean
, char *avail
,
1026 int start_partial_ok
,
1033 unsigned int req_cnt
;
1035 if (content
->journal_device_required
&& (content
->journal_clean
== 0)) {
1037 pr_err("Not safe to assemble with missing or stale journal device, consider --force.\n");
1040 pr_err("Journal is missing or stale, starting array read only.\n");
1044 if (content
->consistency_policy
== CONSISTENCY_POLICY_PPL
)
1047 rv
= set_array_info(mdfd
, st
, content
);
1048 if (rv
&& !err_ok
) {
1049 pr_err("failed to set array info for %s: %s\n",
1050 mddev
, strerror(errno
));
1053 if (ident
->bitmap_fd
>= 0) {
1054 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
1055 pr_err("SET_BITMAP_FILE failed.\n");
1058 } else if (ident
->bitmap_file
) {
1059 /* From config file */
1060 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
1062 pr_err("Could not open bitmap file %s\n",
1063 ident
->bitmap_file
);
1066 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
1067 pr_err("Failed to set bitmapfile for %s\n", mddev
);
1074 /* First, add the raid disks, but add the chosen one last */
1075 for (i
= 0; i
<= bestcnt
; i
++) {
1079 if (j
== chosen_drive
)
1084 if (j
>= 0 && !devices
[j
].included
) {
1087 dfd
= dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
1089 remove_partitions(dfd
);
1092 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
1095 pr_err("failed to add %s to %s: %s\n",
1096 devices
[j
].devname
, mddev
,
1098 if (errno
== EINVAL
&& content
->array
.level
== 0 &&
1099 content
->array
.layout
!= 0) {
1100 cont_err("Possibly your kernel doesn't support RAID0 layouts.\n");
1101 cont_err("Please upgrade.\n");
1103 if (i
< content
->array
.raid_disks
* 2 ||
1108 } else if (c
->verbose
> 0) {
1109 pr_err("added %s to %s as %d%s%s\n",
1110 devices
[j
].devname
, mddev
,
1111 devices
[j
].i
.disk
.raid_disk
,
1112 devices
[j
].uptodate
?"":
1113 " (possibly out of date)",
1114 (devices
[j
].i
.disk
.state
&
1115 (1<<MD_DISK_REPLACEMENT
)) ?
1118 } else if (j
>= 0) {
1120 pr_err("%s is already in %s as %d\n",
1121 devices
[j
].devname
, mddev
,
1122 devices
[j
].i
.disk
.raid_disk
);
1123 } else if (c
->verbose
> 0 &&
1124 i
< content
->array
.raid_disks
* 2 && (i
& 1) == 0)
1125 pr_err("no uptodate device for slot %d of %s\n",
1129 if (is_container(content
->array
.level
)) {
1130 sysfs_rules_apply(mddev
, content
);
1131 if (c
->verbose
>= 0) {
1132 pr_err("Container %s has been assembled with %d drive%s",
1133 mddev
, okcnt
+ sparecnt
+ journalcnt
,
1134 okcnt
+ sparecnt
+ journalcnt
== 1 ? "" : "s");
1135 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1136 fprintf(stderr
, " (out of %d)\n",
1137 content
->array
.raid_disks
);
1139 fprintf(stderr
, "\n");
1142 if (st
->ss
->validate_container
) {
1143 struct mdinfo
*devices_list
;
1144 struct mdinfo
*info_devices
;
1147 devices_list
= NULL
;
1148 info_devices
= xmalloc(sizeof(struct mdinfo
) *
1149 (okcnt
+ sparecnt
));
1150 for (count
= 0; count
< okcnt
+ sparecnt
; count
++) {
1151 info_devices
[count
] = devices
[count
].i
;
1152 info_devices
[count
].next
= devices_list
;
1153 devices_list
= &info_devices
[count
];
1155 if (st
->ss
->validate_container(devices_list
))
1156 pr_err("Mismatch detected!\n");
1160 st
->ss
->free_super(st
);
1161 sysfs_uevent(content
, "change");
1162 if (err_ok
&& okcnt
< (unsigned)content
->array
.raid_disks
)
1163 /* Was partial, is still partial, so signal an error
1164 * to ensure we don't retry */
1169 /* Get number of in-sync devices according to the superblock.
1170 * We must have this number to start the array without -s or -R
1172 req_cnt
= content
->array
.working_disks
;
1174 if (c
->runstop
== 1 ||
1176 (enough(content
->array
.level
, content
->array
.raid_disks
,
1177 content
->array
.layout
, clean
, avail
) &&
1178 (okcnt
+ rebuilding_cnt
>= req_cnt
|| start_partial_ok
)))) {
1179 /* This array is good-to-go.
1180 * If a reshape is in progress then we might need to
1181 * continue monitoring it. In that case we start
1182 * it read-only and let the grow code make it writable.
1186 if (content
->reshape_active
&&
1187 !(content
->reshape_active
& RESHAPE_NO_BACKUP
) &&
1188 content
->delta_disks
<= 0) {
1189 if (!c
->backup_file
) {
1190 pr_err("%s: Need a backup file to complete reshape of this array.\n",
1192 pr_err("Please provided one with \"--backup-file=...\"\n");
1193 if (c
->update
== UOPT_REVERT_RESHAPE
)
1194 pr_err("(Don't specify --update=revert-reshape again, that part succeeded.)\n");
1197 rv
= sysfs_set_str(content
, NULL
,
1198 "array_state", "readonly");
1200 rv
= Grow_continue(mdfd
, st
, content
,
1203 } else if (c
->readonly
&&
1204 sysfs_attribute_available(content
, NULL
,
1206 rv
= sysfs_set_str(content
, NULL
,
1207 "array_state", "readonly");
1209 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1210 reopen_mddev(mdfd
); /* drop O_EXCL */
1212 sysfs_rules_apply(mddev
, content
);
1213 if (c
->verbose
>= 0) {
1214 pr_err("%s has been started with %d drive%s",
1215 mddev
, okcnt
, okcnt
==1?"":"s");
1216 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1217 fprintf(stderr
, " (out of %d)",
1218 content
->array
.raid_disks
);
1220 fprintf(stderr
, "%s %d rebuilding",
1221 sparecnt
?",":" and",
1224 fprintf(stderr
, " and %d spare%s",
1226 sparecnt
== 1 ? "" : "s");
1227 if (content
->journal_clean
)
1228 fprintf(stderr
, " and %d journal",
1230 fprintf(stderr
, ".\n");
1232 if (content
->reshape_active
&&
1233 is_level456(content
->array
.level
)) {
1234 /* might need to increase the size
1235 * of the stripe cache - default is 256
1237 int chunk_size
= content
->array
.chunk_size
;
1239 if (content
->reshape_active
&&
1240 content
->new_chunk
> chunk_size
)
1241 chunk_size
= content
->new_chunk
;
1242 if (256 < 4 * ((chunk_size
+4065)/4096)) {
1245 sra
= sysfs_read(mdfd
, NULL
, 0);
1247 sysfs_set_num(sra
, NULL
,
1248 "stripe_cache_size",
1249 (4 * chunk_size
/ 4096) + 1);
1253 if (okcnt
< (unsigned)content
->array
.raid_disks
) {
1254 /* If any devices did not get added
1255 * because the kernel rejected them based
1256 * on event count, try adding them
1257 * again providing the action policy is
1258 * 're-add' or greater. The bitmap
1259 * might allow them to be included, or
1260 * they will become spares.
1262 for (i
= 0; i
< bestcnt
; i
++) {
1264 if (j
>= 0 && !devices
[j
].uptodate
) {
1265 if (!disk_action_allows(&devices
[j
].i
, st
->ss
->name
, act_re_add
))
1267 rv
= add_disk(mdfd
, st
, content
,
1269 if (rv
== 0 && c
->verbose
>= 0)
1270 pr_err("%s has been re-added.\n",
1271 devices
[j
].devname
);
1275 if (content
->array
.level
== 6 &&
1276 okcnt
+ 1 == (unsigned)content
->array
.raid_disks
&&
1280 sra
= sysfs_read(mdfd
, NULL
, 0);
1282 sysfs_set_str(sra
, NULL
,
1283 "sync_action", "repair");
1288 pr_err("failed to RUN_ARRAY %s: %s\n", mddev
, strerror(errno
));
1289 if (errno
== 524 /* ENOTSUP */ &&
1290 content
->array
.level
== 0 && content
->array
.layout
== 0)
1291 cont_err("Please use --update=layout-original or --update=layout-alternate\n");
1293 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1294 content
->array
.layout
, 1, avail
))
1295 pr_err("Not enough devices to start the array.\n");
1296 else if (!enough(content
->array
.level
,
1297 content
->array
.raid_disks
,
1298 content
->array
.layout
, clean
, avail
))
1299 pr_err("Not enough devices to start the array while not clean - consider --force.\n");
1303 if (c
->runstop
== -1) {
1304 pr_err("%s assembled from %d drive%s",
1305 mddev
, okcnt
, okcnt
== 1 ? "" : "s");
1306 if (okcnt
!= (unsigned)content
->array
.raid_disks
)
1307 fprintf(stderr
, " (out of %d)",
1308 content
->array
.raid_disks
);
1309 fprintf(stderr
, ", but not started.\n");
1312 if (c
->verbose
>= -1) {
1313 pr_err("%s assembled from %d drive%s",
1314 mddev
, okcnt
, okcnt
== 1 ? "" : "s");
1316 fprintf(stderr
, "%s %d rebuilding",
1317 sparecnt
? "," : " and", rebuilding_cnt
);
1319 fprintf(stderr
, " and %d spare%s", sparecnt
,
1320 sparecnt
== 1 ? "" : "s");
1321 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1322 content
->array
.layout
, 1, avail
))
1323 fprintf(stderr
, " - not enough to start the array.\n");
1324 else if (!enough(content
->array
.level
,
1325 content
->array
.raid_disks
,
1326 content
->array
.layout
, clean
, avail
))
1327 fprintf(stderr
, " - not enough to start the array while not clean - consider --force.\n");
1329 if (req_cnt
== (unsigned)content
->array
.raid_disks
)
1330 fprintf(stderr
, " - need all %d to start it",
1333 fprintf(stderr
, " - need %d to start", req_cnt
);
1334 fprintf(stderr
, " (use --run to insist).\n");
1340 int Assemble(struct supertype
*st
, char *mddev
,
1341 struct mddev_ident
*ident
,
1342 struct mddev_dev
*devlist
,
1346 * The task of Assemble is to find a collection of
1347 * devices that should (according to their superblocks)
1348 * form an array, and to give this collection to the MD driver.
1349 * In Linux-2.4 and later, this involves submitting a
1350 * SET_ARRAY_INFO ioctl with no arg - to prepare
1351 * the array - and then submit a number of
1352 * ADD_NEW_DISK ioctls to add disks into
1353 * the array. Finally RUN_ARRAY might
1354 * be submitted to start the array.
1356 * Much of the work of Assemble is in finding and/or
1357 * checking the disks to make sure they look right.
1359 * If mddev is not set, then scan must be set and we
1360 * read through the config file for dev+uuid mapping
1361 * We recurse, setting mddev, for each device that
1363 * - has a valid uuid (or any uuid if !uuidset)
1365 * If mddev is set, we try to determine state of md.
1366 * check version - must be at least 0.90.0
1367 * check kernel version. must be at least 2.4.
1368 * If not, we can possibly fall back on START_ARRAY
1369 * Try to GET_ARRAY_INFO.
1370 * If possible, give up
1371 * If not, try to STOP_ARRAY just to make sure
1373 * If !uuidset and scan, look in conf-file for uuid
1374 * If not found, give up
1375 * If !devlist and scan and uuidset, get list of devs from conf-file
1378 * Check superblock - discard if bad
1379 * Check uuid (set if we don't have one) - discard if no match
1380 * Check superblock similarity if we have a superblock - discard if different
1381 * Record events, devicenum
1382 * This should give us a list of devices for the array
1383 * We should collect the most recent event number
1385 * Count disks with recent enough event count
1386 * While force && !enough disks
1387 * Choose newest rejected disks, update event count
1388 * mark clean and rewrite superblock
1391 * foreach device with recent events : ADD_NEW_DISK
1392 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
1394 * Check the device numbers in superblock are right
1395 * update superblock if any changes
1402 int auto_assem
= (mddev
== NULL
&& !ident
->uuid_set
&&
1403 ident
->super_minor
== UnSet
&& ident
->name
[0] == 0 &&
1404 (ident
->container
== NULL
|| ident
->member
== NULL
));
1405 struct devs
*devices
= NULL
;
1407 int *best
= NULL
; /* indexed by raid_disk */
1410 unsigned int okcnt
, sparecnt
, rebuilding_cnt
, replcnt
, journalcnt
;
1411 int journal_clean
= 0;
1414 int most_recent
= 0;
1418 int start_partial_ok
= (c
->runstop
>= 0) &&
1419 (c
->force
|| devlist
==NULL
|| auto_assem
);
1421 struct mddev_dev
*tmpdev
;
1423 struct mdinfo
*content
= NULL
;
1424 struct mdinfo
*pre_exist
= NULL
;
1427 char chosen_name
[1024];
1428 struct map_ent
*map
= NULL
;
1432 * If any subdevs are listed, then any that don't
1433 * match ident are discarded. Remainder must all match and
1435 * If no subdevs, then we scan all devices in the config file, but
1436 * there must be something in the identity
1440 ident
->uuid_set
== 0 &&
1441 (ident
->super_minor
< 0 || ident
->super_minor
== UnSet
) &&
1442 ident
->name
[0] == 0 &&
1443 (ident
->container
== NULL
|| ident
->member
== NULL
) &&
1444 ident
->devices
== NULL
) {
1445 pr_err("No identity information available for %s - cannot assemble.\n",
1446 mddev
? mddev
: "further assembly");
1450 if (devlist
== NULL
)
1451 devlist
= conf_get_devs();
1456 /* We come back here when doing auto-assembly and attempting some
1457 * set of devices failed. Those are now marked as ->used==2 and
1458 * we ignore them and try again
1460 if (!st
&& ident
->st
)
1463 pr_err("looking for devices for %s\n",
1464 mddev
? mddev
: "further assembly");
1468 st
->ignore_hw_compat
= 1;
1469 num_devs
= select_devices(devlist
, ident
, &st
, &content
, c
,
1470 inargv
, auto_assem
);
1474 if (!st
|| !st
->sb
|| !content
)
1477 /* We have a full set of devices - we now need to find the
1479 * However there is a risk that we are racing with "mdadm -I"
1480 * and the array is already partially assembled - we will have
1481 * rejected any devices already in this address.
1482 * So we take a lock on the map file - to prevent further races -
1483 * and look for the uuid in there. If found and the array is
1484 * active, we abort. If found and the array is not active
1485 * we commit to that md device and add all the contained devices
1486 * to our list. We flag them so that we don't try to re-add,
1487 * but can remove if they turn out to not be wanted.
1490 pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
1491 if (c
->update
== UOPT_UUID
)
1494 mp
= map_by_uuid(&map
, content
->uuid
);
1497 /* array already exists. */
1498 pre_exist
= sysfs_read(-1, mp
->devnm
, GET_LEVEL
|GET_DEVS
);
1499 if (pre_exist
->array
.level
!= UnSet
) {
1500 pr_err("Found some drive for an array that is already active: %s\n",
1502 pr_err("giving up.\n");
1505 for (dv
= pre_exist
->devs
; dv
; dv
= dv
->next
) {
1506 /* We want to add this device to our list,
1507 * but it could already be there if "mdadm -I"
1508 * started *after* we checked for O_EXCL.
1509 * If we add it to the top of the list
1510 * it will be preferred over later copies.
1512 struct mddev_dev
*newdev
;
1513 char *devname
= map_dev(dv
->disk
.major
,
1518 newdev
= xmalloc(sizeof(*newdev
));
1519 newdev
->devname
= devname
;
1520 newdev
->disposition
= 'I';
1522 newdev
->next
= devlist
;
1526 strcpy(chosen_name
, mp
->path
);
1527 if (c
->verbose
> 0 || mddev
== NULL
||
1528 strcmp(mddev
, chosen_name
) != 0)
1529 pr_err("Merging with already-assembled %s\n",
1531 mdfd
= open_dev_excl(mp
->devnm
);
1533 int trustworthy
= FOREIGN
;
1534 name
= content
->name
;
1535 switch (st
->ss
->match_home(st
, c
->homehost
)
1536 ?: st
->ss
->match_home(st
, "any")) {
1538 trustworthy
= LOCAL
;
1539 name
= strchr(content
->name
, ':');
1543 name
= content
->name
;
1546 if (mddev
&& map_by_name(&map
, mddev
) != NULL
) {
1547 pr_err("Cannot create device with %s because is in use\n", mddev
);
1551 /* If the array is listed in mdadm.conf or on
1552 * command line, then we trust the name
1553 * even if the array doesn't look local
1555 trustworthy
= LOCAL
;
1557 if (!name
[0] && is_container(content
->array
.level
)) {
1558 name
= content
->text_version
;
1559 trustworthy
= METADATA
;
1562 if (name
[0] && trustworthy
!= LOCAL
&&
1563 ! c
->require_homehost
&&
1564 conf_name_is_free(name
))
1565 trustworthy
= LOCAL
;
1567 if (trustworthy
== LOCAL
&&
1569 /* Ignore 'host:' prefix of name */
1570 name
= strchr(name
, ':')+1;
1572 mdfd
= create_mddev(mddev
, name
, ident
->autof
, trustworthy
,
1576 st
->ss
->free_super(st
);
1581 mddev
= chosen_name
;
1582 if (pre_exist
== NULL
) {
1583 if (mddev_busy(fd2devnm(mdfd
))) {
1584 pr_err("%s already active, cannot restart it!\n",
1586 for (tmpdev
= devlist
;
1587 tmpdev
&& tmpdev
->used
!= 1;
1588 tmpdev
= tmpdev
->next
)
1590 if (tmpdev
&& auto_assem
)
1591 pr_err("%s needed for %s...\n",
1592 mddev
, tmpdev
->devname
);
1595 st
->ss
->free_super(st
);
1600 /* just incase it was started but has no content */
1601 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1604 if (content
!= &info
) {
1605 /* This is a member of a container. Try starting the array. */
1607 err
= assemble_container_content(st
, mdfd
, content
, c
,
1613 /* Ok, no bad inconsistancy, we can try updating etc */
1614 devices
= xcalloc(num_devs
, sizeof(*devices
));
1615 devmap
= xcalloc(num_devs
, content
->array
.raid_disks
);
1616 devcnt
= load_devices(devices
, devmap
, ident
, &st
, devlist
,
1617 c
, content
, mdfd
, mddev
,
1618 &most_recent
, &bestcnt
, &best
, inargv
);
1622 * devices is already freed in load_devices, so set devices
1623 * to NULL to avoid double free devices.
1630 pr_err("no devices found for %s\n",
1633 st
->ss
->free_super(st
);
1638 if (c
->update
== UOPT_BYTEORDER
)
1639 st
->minor_version
= 90;
1641 st
->ss
->getinfo_super(st
, content
, NULL
);
1642 clean
= content
->array
.state
& 1;
1644 /* now we have some devices that might be suitable.
1647 avail
= xcalloc(content
->array
.raid_disks
, 1);
1653 for (i
=0; i
< bestcnt
; i
++) {
1655 int event_margin
= 1; /* always allow a difference of '1'
1656 * like the kernel does
1658 if (j
< 0) continue;
1659 /* note: we ignore error flags in multipath arrays
1660 * as they don't make sense
1662 if (content
->array
.level
!= LEVEL_MULTIPATH
) {
1663 if (devices
[j
].i
.disk
.state
& (1<<MD_DISK_JOURNAL
)) {
1664 if (content
->journal_device_required
)
1666 else /* unexpected journal, mark as faulty */
1667 devices
[j
].i
.disk
.state
|= (1<<MD_DISK_FAULTY
);
1668 } else if (!(devices
[j
].i
.disk
.state
& (1<<MD_DISK_ACTIVE
))) {
1669 if (!(devices
[j
].i
.disk
.state
1670 & (1<<MD_DISK_FAULTY
))) {
1671 devices
[j
].uptodate
= 1;
1677 /* If this device thinks that 'most_recent' has failed, then
1678 * we must reject this device.
1680 if (j
!= most_recent
&& !c
->force
&&
1681 content
->array
.raid_disks
> 0 &&
1682 devices
[most_recent
].i
.disk
.raid_disk
>= 0 &&
1683 devmap
[j
* content
->array
.raid_disks
+ devices
[most_recent
].i
.disk
.raid_disk
] == 0) {
1684 if (c
->verbose
> -1)
1685 pr_err("ignoring %s as it reports %s as failed\n",
1686 devices
[j
].devname
, devices
[most_recent
].devname
);
1690 /* Require event counter to be same as, or just less than,
1691 * most recent. If it is bigger, it must be a stray spare and
1692 * should be ignored.
1694 if (devices
[j
].i
.events
+event_margin
>=
1695 devices
[most_recent
].i
.events
&&
1696 devices
[j
].i
.events
<=
1697 devices
[most_recent
].i
.events
1699 devices
[j
].uptodate
= 1;
1700 if (devices
[j
].i
.disk
.state
& (1<<MD_DISK_JOURNAL
))
1702 if (i
< content
->array
.raid_disks
* 2) {
1703 if (devices
[j
].i
.recovery_start
== MaxSector
||
1704 (content
->reshape_active
&&
1705 i
>= content
->array
.raid_disks
- content
->delta_disks
)) {
1713 } else if (devices
[j
].i
.disk
.raid_disk
!= MD_DISK_ROLE_JOURNAL
)
1719 int force_ok
= force_array(content
, devices
, best
, bestcnt
,
1720 avail
, most_recent
, st
, c
);
1725 /* Now we want to look at the superblock which the kernel will base things on
1726 * and compare the devices that we think are working with the devices that the
1727 * superblock thinks are working.
1728 * If there are differences and --force is given, then update this chosen
1732 st
->ss
->free_super(st
);
1733 for (i
=0; chosen_drive
< 0 && i
<bestcnt
; i
+=2) {
1739 if (!devices
[j
].uptodate
)
1741 if (devices
[j
].i
.events
< devices
[most_recent
].i
.events
)
1744 if ((fd
=dev_open(devices
[j
].devname
,
1745 devices
[j
].included
? O_RDONLY
1746 : (O_RDONLY
|O_EXCL
)))< 0) {
1747 pr_err("Cannot open %s: %s\n",
1748 devices
[j
].devname
, strerror(errno
));
1751 if (st
->ss
->load_super(st
,fd
, NULL
)) {
1753 pr_err("RAID superblock has disappeared from %s\n",
1754 devices
[j
].devname
);
1759 if (st
->sb
== NULL
) {
1760 pr_err("No suitable drives found for %s\n", mddev
);
1763 st
->ss
->getinfo_super(st
, content
, NULL
);
1764 if (sysfs_init(content
, mdfd
, NULL
)) {
1765 pr_err("Unable to initialize sysfs\n");
1769 /* after reload context, store journal_clean in context */
1770 content
->journal_clean
= journal_clean
;
1771 for (i
=0; i
<bestcnt
; i
++) {
1773 unsigned int desired_state
;
1777 if (devices
[j
].i
.disk
.raid_disk
== MD_DISK_ROLE_JOURNAL
)
1778 desired_state
= (1<<MD_DISK_JOURNAL
);
1779 else if (i
>= content
->array
.raid_disks
* 2)
1782 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_REPLACEMENT
);
1784 desired_state
= (1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
);
1786 desired_state
|= devices
[j
].i
.disk
.state
& ((1<<MD_DISK_FAILFAST
) |
1787 (1<<MD_DISK_WRITEMOSTLY
));
1789 if (!devices
[j
].uptodate
)
1792 devices
[j
].i
.disk
.state
= desired_state
;
1793 if (!(devices
[j
].i
.array
.state
& 1))
1796 if (st
->ss
->update_super(st
, &devices
[j
].i
, UOPT_SPEC_ASSEMBLE
, NULL
,
1797 c
->verbose
, 0, NULL
)) {
1799 if (c
->verbose
>= 0)
1800 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
1801 j
, mddev
, devices
[j
].devname
);
1804 if (c
->verbose
>= -1)
1805 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
1806 i
, mddev
, devices
[j
].devname
);
1810 if (c
->force
&& !clean
&& !is_container(content
->array
.level
) &&
1811 !enough(content
->array
.level
, content
->array
.raid_disks
,
1812 content
->array
.layout
, clean
, avail
)) {
1813 change
+= st
->ss
->update_super(st
, content
, UOPT_SPEC_FORCE_ARRAY
,
1814 devices
[chosen_drive
].devname
, c
->verbose
,
1822 fd
= dev_open(devices
[chosen_drive
].devname
,
1823 devices
[chosen_drive
].included
?
1824 O_RDWR
: (O_RDWR
|O_EXCL
));
1826 pr_err("Could not open %s for write - cannot Assemble array.\n",
1827 devices
[chosen_drive
].devname
);
1830 if (st
->ss
->store_super(st
, fd
)) {
1832 pr_err("Could not re-write superblock on %s\n",
1833 devices
[chosen_drive
].devname
);
1836 if (c
->verbose
>= 0)
1837 pr_err("Marking array %s as 'clean'\n",
1842 /* If we are in the middle of a reshape we may need to restore saved data
1843 * that was moved aside due to the reshape overwriting live data
1844 * The code of doing this lives in Grow.c
1846 if (content
->reshape_active
&&
1847 !(content
->reshape_active
& RESHAPE_NO_BACKUP
)) {
1849 int *fdlist
= xmalloc(sizeof(int)* bestcnt
);
1851 pr_err("%s has an active reshape - checking if critical section needs to be restored\n",
1853 if (!c
->backup_file
)
1854 c
->backup_file
= locate_backup(content
->sys_name
);
1855 enable_fds(bestcnt
/2);
1856 for (i
= 0; i
< bestcnt
/2; i
++) {
1859 fdlist
[i
] = dev_open(devices
[j
].devname
,
1861 ? O_RDWR
: (O_RDWR
|O_EXCL
));
1862 if (fdlist
[i
] < 0) {
1863 pr_err("Could not open %s for write - cannot Assemble array.\n",
1864 devices
[j
].devname
);
1872 if (st
->ss
->external
&& st
->ss
->recover_backup
)
1873 err
= st
->ss
->recover_backup(st
, content
);
1875 err
= Grow_restart(st
, content
, fdlist
, bestcnt
/2,
1876 c
->backup_file
, c
->verbose
> 0);
1877 if (err
&& c
->invalid_backup
) {
1879 pr_err("continuing without restoring backup\n");
1885 if (fdlist
[i
]>=0) close(fdlist
[i
]);
1889 pr_err("Failed to restore critical section for reshape, sorry.\n");
1890 if (c
->backup_file
== NULL
)
1891 cont_err("Possibly you needed to specify the --backup-file\n");
1896 /* Almost ready to actually *do* something */
1897 /* First, fill in the map, so that udev can find our name
1898 * as soon as we become active.
1900 if (c
->update
== UOPT_METADATA
) {
1901 content
->array
.major_version
= 1;
1902 content
->array
.minor_version
= 0;
1903 strcpy(content
->text_version
, "1.0");
1906 map_update(&map
, fd2devnm(mdfd
), content
->text_version
,
1907 content
->uuid
, chosen_name
);
1909 rv
= start_array(mdfd
, mddev
, content
,
1910 st
, ident
, best
, bestcnt
,
1911 chosen_drive
, devices
, okcnt
, sparecnt
,
1912 rebuilding_cnt
, journalcnt
,
1914 clean
, avail
, start_partial_ok
,
1917 if (rv
== 1 && !pre_exist
)
1918 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1923 wait_for(chosen_name
, mdfd
);
1927 /* There is a nasty race with 'mdadm --monitor'.
1928 * If it opens this device before we close it,
1929 * it gets an incomplete open on which IO
1930 * doesn't work and the capacity is
1932 * If we reopen (to check for layered devices)
1933 * before --monitor closes, we loose.
1935 * So: wait upto 1 second for there to be
1936 * a non-zero capacity.
1938 while (usecs
< 1000) {
1939 mdfd
= open(mddev
, O_RDONLY
);
1941 unsigned long long size
;
1942 if (get_dev_size(mdfd
, NULL
, &size
) &&
1947 sleep_for(0, USEC_TO_NSEC(usecs
), true);
1951 } else if (mdfd
>= 0)
1955 /* '2' means 'OK, but not started yet' */
1960 return rv
== 2 ? 0 : rv
;
1963 int assemble_container_content(struct supertype
*st
, int mdfd
,
1964 struct mdinfo
*content
, struct context
*c
,
1965 char *chosen_name
, int *result
)
1967 struct mdinfo
*dev
, *sra
, *dev2
;
1968 struct assembly_array_info array
= {chosen_name
, 0, 0, 0};
1973 int is_clean
, all_disks
;
1976 if (sysfs_init(content
, mdfd
, NULL
)) {
1977 pr_err("Unable to initialize sysfs\n");
1981 sra
= sysfs_read(mdfd
, NULL
, GET_VERSION
|GET_DEVS
);
1983 pr_err("Failed to read sysfs parameters\n");
1987 /* Fill sysfs properties only if they are not set. Determine it by checking text_version
1988 * and ignoring special character on the first place.
1990 if (strcmp(sra
->text_version
+ 1, content
->text_version
+ 1) != 0) {
1991 if (sysfs_set_array(content
) != 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", STR_COMMON_NONE
) < 0 && errno
== EBUSY
) {
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
= is_level456(content
->array
.level
);
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 */