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 /* ignore unrecognized device if looking for
340 pr_err("%s has no superblock - assembly aborted\n",
343 st
->ss
->free_super(st
);
344 dev_policy_free(pol
);
345 domain_free(domains
);
347 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
);
425 pr_err("found match on member %s in %s\n",
426 content
->text_version
, devname
);
428 /* make sure we finished the loop */
433 tst
->ss
->getinfo_super(tst
, content
, NULL
);
435 if (!ident_matches(ident
, content
, tst
,
436 c
->homehost
, c
->require_homehost
,
438 report_mismatch
? devname
: NULL
))
442 /* Never auto-assemble things that conflict
443 * with mdadm.conf in some way
445 struct mddev_ident
*match
;
448 match
= conf_match(tst
, content
, devname
,
449 report_mismatch
? c
->verbose
: -1,
451 if (!match
&& rv
== 2)
453 if (match
&& match
->devname
&&
454 strcasecmp(match
->devname
, "<ignore>") == 0) {
456 pr_err("%s is a member of an explicitly ignored array\n",
460 if (match
&& !ident_matches(match
, content
, tst
,
461 c
->homehost
, c
->require_homehost
,
463 report_mismatch
? devname
: NULL
))
464 /* Array exists in mdadm.conf but some
465 * details don't match, so reject it
470 /* should be safe to try an exclusive open now, we
471 * have rejected anything that some other mdadm might
474 dfd
= dev_open(devname
, O_RDONLY
| O_EXCL
);
477 pr_err("%s is busy - skipping\n", devname
);
484 if (st
->minor_version
== -1)
485 st
->minor_version
= tst
->minor_version
;
487 if (memcmp(content
->uuid
, uuid_zero
,
488 sizeof(int[4])) == 0) {
489 /* this is a floating spare. It cannot define
490 * an array unless there are no more arrays of
491 * this type to be found. It can be included
492 * in an array of this type though.
498 if (st
->ss
!= tst
->ss
||
499 st
->minor_version
!= tst
->minor_version
||
500 st
->ss
->compare_super(st
, tst
, 1) != 0) {
501 /* Some mismatch. If exactly one array matches this host,
502 * we can resolve on that one.
503 * Or, if we are auto assembling, we just ignore the second
509 int first
= st
->ss
->match_home(st
, c
->homehost
);
510 int last
= tst
->ss
->match_home(tst
, c
->homehost
);
512 (first
== 1 || last
== 1)) {
513 /* We can do something */
514 if (first
) {/* just ignore this one */
516 pr_err("%s misses out due to wrong homehost\n",
519 } else { /* reject all those sofar */
520 struct mddev_dev
*td
;
522 pr_err("%s overrides previous devices due to good homehost\n",
524 for (td
=devlist
; td
!= tmpdev
; td
=td
->next
)
532 pr_err("superblock on %s doesn't match others - assembly aborted\n",
534 tst
->ss
->free_super(tst
);
535 st
->ss
->free_super(st
);
536 dev_policy_free(pol
);
537 domain_free(domains
);
543 /* Collect domain information from members only */
544 if (tmpdev
&& tmpdev
->used
== 1) {
546 pol
= devid_policy(rdev
);
547 domain_merge(&domains
, pol
, tst
?tst
->ss
->name
:NULL
);
549 dev_policy_free(pol
);
552 tst
->ss
->free_super(tst
);
555 /* Check if we found some imsm spares but no members */
558 memcmp(uuid_zero
, ident
->uuid
,sizeof(uuid_zero
)) == 0)) &&
560 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
561 if (tmpdev
->used
!= 3)
567 /* we need sb from one of the spares */
568 int dfd
= dev_open(tmpdev
->devname
, O_RDONLY
);
570 st
->ss
->load_super(st
, dfd
, NULL
))
576 /* Now reject spares that don't match domains of identified members */
577 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
578 if (tmpdev
->used
!= 3)
580 if (!stat_is_blkdev(tmpdev
->devname
, &rdev
)) {
583 struct dev_policy
*pol
= devid_policy(rdev
);
584 int dt
= domain_test(domains
, pol
, NULL
);
585 if (inargv
&& dt
!= 0)
586 /* take this spare as domains match
587 * if there are any */
589 else if (!inargv
&& dt
== 1)
590 /* device wasn't explicitly listed, so need
591 * explicit domain match - which we have */
594 /* if domains don't match mark as unused */
596 dev_policy_free(pol
);
599 domain_free(domains
);
601 if (st
&& st
->sb
&& content
== *contentp
)
602 st
->ss
->getinfo_super(st
, content
, NULL
);
610 int uptodate
; /* set once we decide that this device is as
611 * recent as everything else in the array.
613 int included
; /* set if the device is already in the array
614 * due to a previous '-I'
619 static int load_devices(struct devs
*devices
, char *devmap
,
620 struct mddev_ident
*ident
, struct supertype
**stp
,
621 struct mddev_dev
*devlist
, struct context
*c
,
622 struct mdinfo
*content
,
623 int mdfd
, char *mddev
,
624 int *most_recentp
, int *bestcntp
, int **bestp
,
627 struct mddev_dev
*tmpdev
;
631 int most_recent
= -1;
634 struct supertype
*st
= *stp
;
636 for (tmpdev
= devlist
; tmpdev
; tmpdev
=tmpdev
->next
) {
637 char *devname
= tmpdev
->devname
;
639 struct supertype
*tst
;
644 if (tmpdev
->used
!= 1)
646 /* looks like a good enough match to update the super block if needed */
648 /* prepare useful information in info structures */
653 if (strcmp(c
->update
, "uuid") == 0 && !ident
->uuid_set
)
654 random_uuid((__u8
*)ident
->uuid
);
656 if (strcmp(c
->update
, "ppl") == 0 &&
657 ident
->bitmap_fd
>= 0) {
658 pr_err("PPL is not compatible with bitmap\n");
665 dfd
= dev_open(devname
,
666 tmpdev
->disposition
== 'I'
667 ? O_RDWR
: (O_RDWR
|O_EXCL
));
670 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
671 pr_err("cannot re-read metadata from %s - aborting\n",
678 tst
->ss
->free_super(tst
);
683 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
685 memcpy(content
->uuid
, ident
->uuid
, 16);
686 strcpy(content
->name
, ident
->name
);
687 content
->array
.md_minor
= minor(stb2
.st_rdev
);
689 if (strcmp(c
->update
, "byteorder") == 0)
691 else if (strcmp(c
->update
, "home-cluster") == 0) {
692 tst
->cluster_name
= c
->homecluster
;
693 err
= tst
->ss
->write_bitmap(tst
, dfd
, NameUpdate
);
694 } else if (strcmp(c
->update
, "nodes") == 0) {
695 tst
->nodes
= c
->nodes
;
696 err
= tst
->ss
->write_bitmap(tst
, dfd
, NodeNumUpdate
);
697 } else if (strcmp(c
->update
, "revert-reshape") == 0 &&
699 err
= tst
->ss
->update_super(tst
, content
,
700 "revert-reshape-nobackup",
705 err
= tst
->ss
->update_super(tst
, content
, c
->update
,
711 pr_err("--update=%s not understood for %s metadata\n",
712 c
->update
, tst
->ss
->name
);
713 tst
->ss
->free_super(tst
);
722 if (strcmp(c
->update
, "uuid")==0 &&
725 memcpy(ident
->uuid
, content
->uuid
, 16);
727 if (tst
->ss
->store_super(tst
, dfd
))
728 pr_err("Could not re-write superblock on %s.\n",
731 if (strcmp(c
->update
, "uuid")==0 &&
732 ident
->bitmap_fd
>= 0 && !bitmap_done
) {
733 if (bitmap_update_uuid(ident
->bitmap_fd
,
735 tst
->ss
->swapuuid
) != 0)
736 pr_err("Could not update uuid on external bitmap.\n");
741 dfd
= dev_open(devname
,
742 tmpdev
->disposition
== 'I'
743 ? O_RDWR
: (O_RDWR
|O_EXCL
));
746 if (dfd
< 0 || tst
->ss
->load_super(tst
, dfd
, NULL
) != 0) {
747 pr_err("cannot re-read metadata from %s - aborting\n",
754 tst
->ss
->free_super(tst
);
759 tst
->ss
->getinfo_super(tst
, content
, devmap
+ devcnt
* content
->array
.raid_disks
);
766 pr_err("%s is identified as a member of %s, slot %d%s.\n",
767 devname
, mddev
, content
->disk
.raid_disk
,
768 (content
->disk
.state
& (1<<MD_DISK_REPLACEMENT
)) ? " replacement":"");
769 devices
[devcnt
].devname
= devname
;
770 devices
[devcnt
].uptodate
= 0;
771 devices
[devcnt
].included
= (tmpdev
->disposition
== 'I');
772 devices
[devcnt
].i
= *content
;
773 devices
[devcnt
].i
.disk
.major
= major(stb
.st_rdev
);
774 devices
[devcnt
].i
.disk
.minor
= minor(stb
.st_rdev
);
776 disk_state
= devices
[devcnt
].i
.disk
.state
& ~((1<<MD_DISK_FAILFAST
) |
777 (1<<MD_DISK_WRITEMOSTLY
));
778 if (disk_state
== ((1<<MD_DISK_ACTIVE
) | (1<<MD_DISK_SYNC
))) {
779 if (most_recent
< 0 ||
780 devices
[devcnt
].i
.events
781 > devices
[most_recent
].i
.events
) {
782 struct supertype
*tmp
= tst
;
785 most_recent
= devcnt
;
788 tst
->ss
->free_super(tst
);
791 if (content
->array
.level
== LEVEL_MULTIPATH
)
792 /* with multipath, the raid_disk from the superblock is meaningless */
795 i
= devices
[devcnt
].i
.disk
.raid_disk
;
796 if (i
+1 == 0 || i
== MD_DISK_ROLE_JOURNAL
) {
797 if (nextspare
< content
->array
.raid_disks
*2)
798 nextspare
= content
->array
.raid_disks
*2;
801 /* i is raid_disk - double it so there is room for
804 if (devices
[devcnt
].i
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))
806 if (i
>= content
->array
.raid_disks
*2 &&
812 int newbestcnt
= i
+10;
813 int *newbest
= xmalloc(sizeof(int)*newbestcnt
);
815 for (c
=0; c
< newbestcnt
; c
++)
817 newbest
[c
] = best
[c
];
822 bestcnt
= newbestcnt
;
825 devices
[best
[i
]].i
.events
==
826 devices
[devcnt
].i
.events
&&
827 (devices
[best
[i
]].i
.disk
.minor
!=
828 devices
[devcnt
].i
.disk
.minor
) &&
830 content
->array
.level
!= LEVEL_MULTIPATH
) {
831 /* two different devices with identical superblock.
832 * Could be a mis-detection caused by overlapping
833 * partitions. fail-safe.
835 pr_err("WARNING %s and %s appear to have very similar superblocks.\n"
836 " If they are really different, please --zero the superblock on one\n"
837 " If they are the same or overlap, please remove one from %s.\n",
838 devices
[best
[i
]].devname
, devname
,
839 inargv
? "the list" :
840 "the\n DEVICE list in mdadm.conf"
848 if (best
[i
] == -1 || (devices
[best
[i
]].i
.events
849 < devices
[devcnt
].i
.events
))
851 else if (st
->ss
== &super_imsm
)
856 if (most_recent
>= 0)
857 *most_recentp
= most_recent
;
864 static int force_array(struct mdinfo
*content
,
865 struct devs
*devices
,
866 int *best
, int bestcnt
, char *avail
,
868 struct supertype
*st
,
872 while (!enough(content
->array
.level
, content
->array
.raid_disks
,
873 content
->array
.layout
, 1,
875 (content
->reshape_active
&& content
->delta_disks
> 0 &&
876 !enough(content
->array
.level
, (content
->array
.raid_disks
877 - content
->delta_disks
),
878 content
->new_layout
, 1, avail
))) {
879 /* Choose the newest best drive which is
880 * not up-to-date, update the superblock
884 struct supertype
*tst
;
885 unsigned long long current_events
;
886 int chosen_drive
= -1;
890 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
895 if (devices
[j
].uptodate
)
897 if (devices
[j
].i
.recovery_start
!= MaxSector
) {
899 if (!devices
[j
].i
.reshape_active
||
900 devices
[j
].i
.delta_disks
<= 0)
902 /* When increasing number of devices, an
903 * added device also appears to be
904 * recovering. It is safe to include it
905 * as long as it won't be a source of
907 * For now, just allow for last data
908 * devices in RAID4 or last devices in RAID4/5/6.
910 delta
= devices
[j
].i
.delta_disks
;
911 if (devices
[j
].i
.array
.level
>= 4 &&
912 devices
[j
].i
.array
.level
<= 6 &&
913 i
/2 >= content
->array
.raid_disks
- delta
)
915 else if (devices
[j
].i
.array
.level
== 4 &&
916 i
/2 >= content
->array
.raid_disks
- delta
- 1)
920 } else if (devices
[j
].i
.reshape_active
!=
921 content
->reshape_active
||
922 (devices
[j
].i
.reshape_active
&&
923 devices
[j
].i
.reshape_progress
!=
924 content
->reshape_progress
))
925 /* Here, it may be a source of data. If two
926 * devices claim different progresses, it
927 * means that reshape boundaries differ for
928 * their own devices. Kernel will only treat
929 * the first one as reshape progress and
930 * go on. It may cause disaster, so avoid it.
933 if (chosen_drive
< 0 ||
935 > devices
[chosen_drive
].i
.events
)
938 if (chosen_drive
< 0)
940 current_events
= devices
[chosen_drive
].i
.events
;
943 pr_err("forcing event count in %s(%d) from %d up to %d\n",
944 devices
[chosen_drive
].devname
,
945 devices
[chosen_drive
].i
.disk
.raid_disk
,
946 (int)(devices
[chosen_drive
].i
.events
),
947 (int)(devices
[most_recent
].i
.events
));
948 fd
= dev_open(devices
[chosen_drive
].devname
,
949 devices
[chosen_drive
].included
? O_RDWR
952 pr_err("Couldn't open %s for write - not updating\n",
953 devices
[chosen_drive
].devname
);
954 devices
[chosen_drive
].i
.events
= 0;
958 if (tst
->ss
->load_super(tst
,fd
, NULL
)) {
960 pr_err("RAID superblock disappeared from %s - not updating.\n",
961 devices
[chosen_drive
].devname
);
962 devices
[chosen_drive
].i
.events
= 0;
965 content
->events
= devices
[most_recent
].i
.events
;
966 tst
->ss
->update_super(tst
, content
, "force-one",
967 devices
[chosen_drive
].devname
, c
->verbose
,
970 if (tst
->ss
->store_super(tst
, fd
)) {
972 pr_err("Could not re-write superblock on %s\n",
973 devices
[chosen_drive
].devname
);
974 devices
[chosen_drive
].i
.events
= 0;
975 tst
->ss
->free_super(tst
);
979 devices
[chosen_drive
].i
.events
= devices
[most_recent
].i
.events
;
980 devices
[chosen_drive
].uptodate
= 1;
981 avail
[chosen_drive
] = 1;
983 tst
->ss
->free_super(tst
);
984 /* If there are any other drives of the same vintage,
985 * add them in as well. We can't lose and we might gain
988 i
< content
->array
.raid_disks
* 2 && i
< bestcnt
;
992 !devices
[j
].uptodate
&&
993 devices
[j
].i
.recovery_start
== MaxSector
&&
994 devices
[j
].i
.events
== current_events
&&
995 ((!devices
[j
].i
.reshape_active
&&
996 !content
->reshape_active
) ||
997 (devices
[j
].i
.reshape_active
==
998 content
->reshape_active
&&
999 devices
[j
].i
.reshape_progress
==
1000 content
->reshape_progress
))) {
1009 static int start_array(int mdfd
,
1011 struct mdinfo
*content
,
1012 struct supertype
*st
,
1013 struct mddev_ident
*ident
,
1014 int *best
, int bestcnt
,
1016 struct devs
*devices
,
1018 unsigned int sparecnt
,
1019 unsigned int rebuilding_cnt
,
1020 unsigned int journalcnt
,
1022 int clean
, char *avail
,
1023 int start_partial_ok
,
1030 unsigned int req_cnt
;
1032 if (content
->journal_device_required
&& (content
->journal_clean
== 0)) {
1034 pr_err("Not safe to assemble with missing or stale journal device, consider --force.\n");
1037 pr_err("Journal is missing or stale, starting array read only.\n");
1041 if (content
->consistency_policy
== CONSISTENCY_POLICY_PPL
)
1044 rv
= set_array_info(mdfd
, st
, content
);
1045 if (rv
&& !err_ok
) {
1046 pr_err("failed to set array info for %s: %s\n",
1047 mddev
, strerror(errno
));
1050 if (ident
->bitmap_fd
>= 0) {
1051 if (ioctl(mdfd
, SET_BITMAP_FILE
, ident
->bitmap_fd
) != 0) {
1052 pr_err("SET_BITMAP_FILE failed.\n");
1055 } else if (ident
->bitmap_file
) {
1056 /* From config file */
1057 int bmfd
= open(ident
->bitmap_file
, O_RDWR
);
1059 pr_err("Could not open bitmap file %s\n",
1060 ident
->bitmap_file
);
1063 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
1064 pr_err("Failed to set bitmapfile for %s\n", mddev
);
1071 /* First, add the raid disks, but add the chosen one last */
1072 for (i
= 0; i
<= bestcnt
; i
++) {
1076 if (j
== chosen_drive
)
1081 if (j
>= 0 && !devices
[j
].included
) {
1084 dfd
= dev_open(devices
[j
].devname
, O_RDWR
|O_EXCL
);
1086 remove_partitions(dfd
);
1089 rv
= add_disk(mdfd
, st
, content
, &devices
[j
].i
);
1092 pr_err("failed to add %s to %s: %s\n",
1093 devices
[j
].devname
, mddev
,
1095 if (errno
== EINVAL
&& content
->array
.level
== 0 &&
1096 content
->array
.layout
!= 0) {
1097 cont_err("Possibly your kernel doesn't support RAID0 layouts.\n");
1098 cont_err("Please upgrade.\n");
1100 if (i
< content
->array
.raid_disks
* 2 ||
1105 } else if (c
->verbose
> 0) {
1106 pr_err("added %s to %s as %d%s%s\n",
1107 devices
[j
].devname
, mddev
,
1108 devices
[j
].i
.disk
.raid_disk
,
1109 devices
[j
].uptodate
?"":
1110 " (possibly out of date)",
1111 (devices
[j
].i
.disk
.state
&
1112 (1<<MD_DISK_REPLACEMENT
)) ?
1115 } else if (j
>= 0) {
1117 pr_err("%s is already in %s as %d\n",
1118 devices
[j
].devname
, mddev
,
1119 devices
[j
].i
.disk
.raid_disk
);
1120 } else if (c
->verbose
> 0 &&
1121 i
< content
->array
.raid_disks
* 2 && (i
& 1) == 0)
1122 pr_err("no uptodate device for slot %d of %s\n",
1126 if (content
->array
.level
== LEVEL_CONTAINER
) {
1127 sysfs_rules_apply(mddev
, content
);
1128 if (c
->verbose
>= 0) {
1129 pr_err("Container %s has been assembled with %d drive%s",
1130 mddev
, okcnt
+ sparecnt
+ journalcnt
,
1131 okcnt
+ sparecnt
+ journalcnt
== 1 ? "" : "s");
1132 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1133 fprintf(stderr
, " (out of %d)\n",
1134 content
->array
.raid_disks
);
1136 fprintf(stderr
, "\n");
1139 if (st
->ss
->validate_container
) {
1140 struct mdinfo
*devices_list
;
1141 struct mdinfo
*info_devices
;
1144 devices_list
= NULL
;
1145 info_devices
= xmalloc(sizeof(struct mdinfo
) *
1146 (okcnt
+ sparecnt
));
1147 for (count
= 0; count
< okcnt
+ sparecnt
; count
++) {
1148 info_devices
[count
] = devices
[count
].i
;
1149 info_devices
[count
].next
= devices_list
;
1150 devices_list
= &info_devices
[count
];
1152 if (st
->ss
->validate_container(devices_list
))
1153 pr_err("Mismatch detected!\n");
1157 st
->ss
->free_super(st
);
1158 sysfs_uevent(content
, "change");
1159 if (err_ok
&& okcnt
< (unsigned)content
->array
.raid_disks
)
1160 /* Was partial, is still partial, so signal an error
1161 * to ensure we don't retry */
1166 /* Get number of in-sync devices according to the superblock.
1167 * We must have this number to start the array without -s or -R
1169 req_cnt
= content
->array
.working_disks
;
1171 if (c
->runstop
== 1 ||
1173 (enough(content
->array
.level
, content
->array
.raid_disks
,
1174 content
->array
.layout
, clean
, avail
) &&
1175 (okcnt
+ rebuilding_cnt
>= req_cnt
|| start_partial_ok
)))) {
1176 /* This array is good-to-go.
1177 * If a reshape is in progress then we might need to
1178 * continue monitoring it. In that case we start
1179 * it read-only and let the grow code make it writable.
1183 if (content
->reshape_active
&&
1184 !(content
->reshape_active
& RESHAPE_NO_BACKUP
) &&
1185 content
->delta_disks
<= 0) {
1186 if (!c
->backup_file
) {
1187 pr_err("%s: Need a backup file to complete reshape of this array.\n",
1189 pr_err("Please provided one with \"--backup-file=...\"\n");
1191 strcmp(c
->update
, "revert-reshape") == 0)
1192 pr_err("(Don't specify --update=revert-reshape again, that part succeeded.)\n");
1195 rv
= sysfs_set_str(content
, NULL
,
1196 "array_state", "readonly");
1198 rv
= Grow_continue(mdfd
, st
, content
,
1201 } else if (c
->readonly
&&
1202 sysfs_attribute_available(content
, NULL
,
1204 rv
= sysfs_set_str(content
, NULL
,
1205 "array_state", "readonly");
1207 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
1208 reopen_mddev(mdfd
); /* drop O_EXCL */
1210 sysfs_rules_apply(mddev
, content
);
1211 if (c
->verbose
>= 0) {
1212 pr_err("%s has been started with %d drive%s",
1213 mddev
, okcnt
, okcnt
==1?"":"s");
1214 if (okcnt
< (unsigned)content
->array
.raid_disks
)
1215 fprintf(stderr
, " (out of %d)",
1216 content
->array
.raid_disks
);
1218 fprintf(stderr
, "%s %d rebuilding",
1219 sparecnt
?",":" and",
1222 fprintf(stderr
, " and %d spare%s",
1224 sparecnt
== 1 ? "" : "s");
1225 if (content
->journal_clean
)
1226 fprintf(stderr
, " and %d journal",
1228 fprintf(stderr
, ".\n");
1230 if (content
->reshape_active
&&
1231 content
->array
.level
>= 4 &&
1232 content
->array
.level
<= 6) {
1233 /* might need to increase the size
1234 * of the stripe cache - default is 256
1236 int chunk_size
= content
->array
.chunk_size
;
1238 if (content
->reshape_active
&&
1239 content
->new_chunk
> chunk_size
)
1240 chunk_size
= content
->new_chunk
;
1241 if (256 < 4 * ((chunk_size
+4065)/4096)) {
1244 sra
= sysfs_read(mdfd
, NULL
, 0);
1246 sysfs_set_num(sra
, NULL
,
1247 "stripe_cache_size",
1248 (4 * chunk_size
/ 4096) + 1);
1252 if (okcnt
< (unsigned)content
->array
.raid_disks
) {
1253 /* If any devices did not get added
1254 * because the kernel rejected them based
1255 * on event count, try adding them
1256 * again providing the action policy is
1257 * 're-add' or greater. The bitmap
1258 * might allow them to be included, or
1259 * they will become spares.
1261 for (i
= 0; i
< bestcnt
; i
++) {
1263 if (j
>= 0 && !devices
[j
].uptodate
) {
1264 if (!disk_action_allows(&devices
[j
].i
, st
->ss
->name
, act_re_add
))
1266 rv
= add_disk(mdfd
, st
, content
,
1268 if (rv
== 0 && c
->verbose
>= 0)
1269 pr_err("%s has been re-added.\n",
1270 devices
[j
].devname
);
1274 if (content
->array
.level
== 6 &&
1275 okcnt
+ 1 == (unsigned)content
->array
.raid_disks
&&
1279 sra
= sysfs_read(mdfd
, NULL
, 0);
1281 sysfs_set_str(sra
, NULL
,
1282 "sync_action", "repair");
1287 pr_err("failed to RUN_ARRAY %s: %s\n", mddev
, strerror(errno
));
1288 if (errno
== 524 /* ENOTSUP */ &&
1289 content
->array
.level
== 0 && content
->array
.layout
== 0)
1290 cont_err("Please use --update=layout-original or --update=layout-alternate\n");
1292 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1293 content
->array
.layout
, 1, avail
))
1294 pr_err("Not enough devices to start the array.\n");
1295 else if (!enough(content
->array
.level
,
1296 content
->array
.raid_disks
,
1297 content
->array
.layout
, clean
, avail
))
1298 pr_err("Not enough devices to start the array while not clean - consider --force.\n");
1302 if (c
->runstop
== -1) {
1303 pr_err("%s assembled from %d drive%s",
1304 mddev
, okcnt
, okcnt
== 1 ? "" : "s");
1305 if (okcnt
!= (unsigned)content
->array
.raid_disks
)
1306 fprintf(stderr
, " (out of %d)",
1307 content
->array
.raid_disks
);
1308 fprintf(stderr
, ", but not started.\n");
1311 if (c
->verbose
>= -1) {
1312 pr_err("%s assembled from %d drive%s",
1313 mddev
, okcnt
, okcnt
== 1 ? "" : "s");
1315 fprintf(stderr
, "%s %d rebuilding",
1316 sparecnt
? "," : " and", rebuilding_cnt
);
1318 fprintf(stderr
, " and %d spare%s", sparecnt
,
1319 sparecnt
== 1 ? "" : "s");
1320 if (!enough(content
->array
.level
, content
->array
.raid_disks
,
1321 content
->array
.layout
, 1, avail
))
1322 fprintf(stderr
, " - not enough to start the array.\n");
1323 else if (!enough(content
->array
.level
,
1324 content
->array
.raid_disks
,
1325 content
->array
.layout
, clean
, avail
))
1326 fprintf(stderr
, " - not enough to start the array while not clean - consider --force.\n");
1328 if (req_cnt
== (unsigned)content
->array
.raid_disks
)
1329 fprintf(stderr
, " - need all %d to start it",
1332 fprintf(stderr
, " - need %d to start", req_cnt
);
1333 fprintf(stderr
, " (use --run to insist).\n");
1339 int Assemble(struct supertype
*st
, char *mddev
,
1340 struct mddev_ident
*ident
,
1341 struct mddev_dev
*devlist
,
1345 * The task of Assemble is to find a collection of
1346 * devices that should (according to their superblocks)
1347 * form an array, and to give this collection to the MD driver.
1348 * In Linux-2.4 and later, this involves submitting a
1349 * SET_ARRAY_INFO ioctl with no arg - to prepare
1350 * the array - and then submit a number of
1351 * ADD_NEW_DISK ioctls to add disks into
1352 * the array. Finally RUN_ARRAY might
1353 * be submitted to start the array.
1355 * Much of the work of Assemble is in finding and/or
1356 * checking the disks to make sure they look right.
1358 * If mddev is not set, then scan must be set and we
1359 * read through the config file for dev+uuid mapping
1360 * We recurse, setting mddev, for each device that
1362 * - has a valid uuid (or any uuid if !uuidset)
1364 * If mddev is set, we try to determine state of md.
1365 * check version - must be at least 0.90.0
1366 * check kernel version. must be at least 2.4.
1367 * If not, we can possibly fall back on START_ARRAY
1368 * Try to GET_ARRAY_INFO.
1369 * If possible, give up
1370 * If not, try to STOP_ARRAY just to make sure
1372 * If !uuidset and scan, look in conf-file for uuid
1373 * If not found, give up
1374 * If !devlist and scan and uuidset, get list of devs from conf-file
1377 * Check superblock - discard if bad
1378 * Check uuid (set if we don't have one) - discard if no match
1379 * Check superblock similarity if we have a superblock - discard if different
1380 * Record events, devicenum
1381 * This should give us a list of devices for the array
1382 * We should collect the most recent event number
1384 * Count disks with recent enough event count
1385 * While force && !enough disks
1386 * Choose newest rejected disks, update event count
1387 * mark clean and rewrite superblock
1390 * foreach device with recent events : ADD_NEW_DISK
1391 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
1393 * Check the device numbers in superblock are right
1394 * update superblock if any changes
1401 int auto_assem
= (mddev
== NULL
&& !ident
->uuid_set
&&
1402 ident
->super_minor
== UnSet
&& ident
->name
[0] == 0 &&
1403 (ident
->container
== NULL
|| ident
->member
== NULL
));
1404 struct devs
*devices
= NULL
;
1406 int *best
= NULL
; /* indexed by raid_disk */
1409 unsigned int okcnt
, sparecnt
, rebuilding_cnt
, replcnt
, journalcnt
;
1410 int journal_clean
= 0;
1413 int most_recent
= 0;
1417 int start_partial_ok
= (c
->runstop
>= 0) &&
1418 (c
->force
|| devlist
==NULL
|| auto_assem
);
1420 struct mddev_dev
*tmpdev
;
1422 struct mdinfo
*content
= NULL
;
1423 struct mdinfo
*pre_exist
= NULL
;
1426 char chosen_name
[1024];
1427 struct map_ent
*map
= NULL
;
1431 * If any subdevs are listed, then any that don't
1432 * match ident are discarded. Remainder must all match and
1434 * If no subdevs, then we scan all devices in the config file, but
1435 * there must be something in the identity
1439 ident
->uuid_set
== 0 &&
1440 (ident
->super_minor
< 0 || ident
->super_minor
== UnSet
) &&
1441 ident
->name
[0] == 0 &&
1442 (ident
->container
== NULL
|| ident
->member
== NULL
) &&
1443 ident
->devices
== NULL
) {
1444 pr_err("No identity information available for %s - cannot assemble.\n",
1445 mddev
? mddev
: "further assembly");
1449 if (devlist
== NULL
)
1450 devlist
= conf_get_devs();
1455 /* We come back here when doing auto-assembly and attempting some
1456 * set of devices failed. Those are now marked as ->used==2 and
1457 * we ignore them and try again
1459 if (!st
&& ident
->st
)
1462 pr_err("looking for devices for %s\n",
1463 mddev
? mddev
: "further assembly");
1467 st
->ignore_hw_compat
= 1;
1468 num_devs
= select_devices(devlist
, ident
, &st
, &content
, c
,
1469 inargv
, auto_assem
);
1473 if (!st
|| !st
->sb
|| !content
)
1476 /* We have a full set of devices - we now need to find the
1478 * However there is a risk that we are racing with "mdadm -I"
1479 * and the array is already partially assembled - we will have
1480 * rejected any devices already in this address.
1481 * So we take a lock on the map file - to prevent further races -
1482 * and look for the uuid in there. If found and the array is
1483 * active, we abort. If found and the array is not active
1484 * we commit to that md device and add all the contained devices
1485 * to our list. We flag them so that we don't try to re-add,
1486 * but can remove if they turn out to not be wanted.
1489 pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
1490 if (c
->update
&& strcmp(c
->update
,"uuid") == 0)
1493 mp
= map_by_uuid(&map
, content
->uuid
);
1496 /* array already exists. */
1497 pre_exist
= sysfs_read(-1, mp
->devnm
, GET_LEVEL
|GET_DEVS
);
1498 if (pre_exist
->array
.level
!= UnSet
) {
1499 pr_err("Found some drive for an array that is already active: %s\n",
1501 pr_err("giving up.\n");
1504 for (dv
= pre_exist
->devs
; dv
; dv
= dv
->next
) {
1505 /* We want to add this device to our list,
1506 * but it could already be there if "mdadm -I"
1507 * started *after* we checked for O_EXCL.
1508 * If we add it to the top of the list
1509 * it will be preferred over later copies.
1511 struct mddev_dev
*newdev
;
1512 char *devname
= map_dev(dv
->disk
.major
,
1517 newdev
= xmalloc(sizeof(*newdev
));
1518 newdev
->devname
= devname
;
1519 newdev
->disposition
= 'I';
1521 newdev
->next
= devlist
;
1525 strcpy(chosen_name
, mp
->path
);
1526 if (c
->verbose
> 0 || mddev
== NULL
||
1527 strcmp(mddev
, chosen_name
) != 0)
1528 pr_err("Merging with already-assembled %s\n",
1530 mdfd
= open_dev_excl(mp
->devnm
);
1532 int trustworthy
= FOREIGN
;
1533 name
= content
->name
;
1534 switch (st
->ss
->match_home(st
, c
->homehost
)
1535 ?: st
->ss
->match_home(st
, "any")) {
1537 trustworthy
= LOCAL
;
1538 name
= strchr(content
->name
, ':');
1542 name
= content
->name
;
1545 if (mddev
&& map_by_name(&map
, mddev
) != NULL
) {
1546 pr_err("Cannot create device with %s because is in use\n", mddev
);
1550 /* If the array is listed in mdadm.conf or on
1551 * command line, then we trust the name
1552 * even if the array doesn't look local
1554 trustworthy
= LOCAL
;
1557 content
->array
.level
== LEVEL_CONTAINER
) {
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
&& strcmp(c
->update
, "byteorder")==0)
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
, "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 (!(super
.disks
[i
].i
.disk
.state
& (1 << MD_DISK_FAULTY
))) {
1811 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1816 if (c
->force
&& !clean
&&
1817 !enough(content
->array
.level
, content
->array
.raid_disks
,
1818 content
->array
.layout
, clean
,
1820 change
+= st
->ss
->update_super(st
, content
, "force-array",
1821 devices
[chosen_drive
].devname
, c
->verbose
,
1829 fd
= dev_open(devices
[chosen_drive
].devname
,
1830 devices
[chosen_drive
].included
?
1831 O_RDWR
: (O_RDWR
|O_EXCL
));
1833 pr_err("Could not open %s for write - cannot Assemble array.\n",
1834 devices
[chosen_drive
].devname
);
1837 if (st
->ss
->store_super(st
, fd
)) {
1839 pr_err("Could not re-write superblock on %s\n",
1840 devices
[chosen_drive
].devname
);
1843 if (c
->verbose
>= 0)
1844 pr_err("Marking array %s as 'clean'\n",
1849 /* If we are in the middle of a reshape we may need to restore saved data
1850 * that was moved aside due to the reshape overwriting live data
1851 * The code of doing this lives in Grow.c
1853 if (content
->reshape_active
&&
1854 !(content
->reshape_active
& RESHAPE_NO_BACKUP
)) {
1856 int *fdlist
= xmalloc(sizeof(int)* bestcnt
);
1858 pr_err("%s has an active reshape - checking if critical section needs to be restored\n",
1860 if (!c
->backup_file
)
1861 c
->backup_file
= locate_backup(content
->sys_name
);
1862 enable_fds(bestcnt
/2);
1863 for (i
= 0; i
< bestcnt
/2; i
++) {
1866 fdlist
[i
] = dev_open(devices
[j
].devname
,
1868 ? O_RDWR
: (O_RDWR
|O_EXCL
));
1869 if (fdlist
[i
] < 0) {
1870 pr_err("Could not open %s for write - cannot Assemble array.\n",
1871 devices
[j
].devname
);
1879 if (st
->ss
->external
&& st
->ss
->recover_backup
)
1880 err
= st
->ss
->recover_backup(st
, content
);
1882 err
= Grow_restart(st
, content
, fdlist
, bestcnt
/2,
1883 c
->backup_file
, c
->verbose
> 0);
1884 if (err
&& c
->invalid_backup
) {
1886 pr_err("continuing without restoring backup\n");
1892 if (fdlist
[i
]>=0) close(fdlist
[i
]);
1896 pr_err("Failed to restore critical section for reshape, sorry.\n");
1897 if (c
->backup_file
== NULL
)
1898 cont_err("Possibly you needed to specify the --backup-file\n");
1903 /* Almost ready to actually *do* something */
1904 /* First, fill in the map, so that udev can find our name
1905 * as soon as we become active.
1907 if (c
->update
&& strcmp(c
->update
, "metadata")==0) {
1908 content
->array
.major_version
= 1;
1909 content
->array
.minor_version
= 0;
1910 strcpy(content
->text_version
, "1.0");
1913 map_update(&map
, fd2devnm(mdfd
), content
->text_version
,
1914 content
->uuid
, chosen_name
);
1916 rv
= start_array(mdfd
, mddev
, content
,
1917 st
, ident
, best
, bestcnt
,
1918 chosen_drive
, devices
, okcnt
, sparecnt
,
1919 rebuilding_cnt
, journalcnt
,
1921 clean
, avail
, start_partial_ok
,
1924 if (rv
== 1 && !pre_exist
)
1925 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1930 wait_for(chosen_name
, mdfd
);
1934 /* There is a nasty race with 'mdadm --monitor'.
1935 * If it opens this device before we close it,
1936 * it gets an incomplete open on which IO
1937 * doesn't work and the capacity is
1939 * If we reopen (to check for layered devices)
1940 * before --monitor closes, we loose.
1942 * So: wait upto 1 second for there to be
1943 * a non-zero capacity.
1945 while (usecs
< 1000) {
1946 mdfd
= open(mddev
, O_RDONLY
);
1948 unsigned long long size
;
1949 if (get_dev_size(mdfd
, NULL
, &size
) &&
1958 } else if (mdfd
>= 0)
1961 /* '2' means 'OK, but not started yet' */
1966 return rv
== 2 ? 0 : rv
;
1969 int assemble_container_content(struct supertype
*st
, int mdfd
,
1970 struct mdinfo
*content
, struct context
*c
,
1971 char *chosen_name
, int *result
)
1973 struct mdinfo
*dev
, *sra
, *dev2
;
1974 struct assembly_array_info array
= {chosen_name
, 0, 0, 0};
1979 int is_raid456
, is_clean
, all_disks
;
1981 if (sysfs_init(content
, mdfd
, NULL
)) {
1982 pr_err("Unable to initialize sysfs\n");
1986 sra
= sysfs_read(mdfd
, NULL
, GET_VERSION
|GET_DEVS
);
1987 if (sra
== NULL
|| strcmp(sra
->text_version
, content
->text_version
) != 0) {
1988 if (content
->array
.major_version
== -1 &&
1989 content
->array
.minor_version
== -2 &&
1991 content
->text_version
[0] == '/')
1992 content
->text_version
[0] = '-';
1993 if (sysfs_set_array(content
, 9003) != 0) {
1999 /* There are two types of reshape: container wide or sub-array specific
2000 * Check if metadata requests blocking container wide reshapes
2002 start_reshape
= (content
->reshape_active
&&
2003 !((content
->reshape_active
== CONTAINER_RESHAPE
) &&
2004 (content
->array
.state
& (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))));
2006 /* Block subarray here if it is under reshape now
2007 * Do not allow for any changes in this array
2009 if (st
->ss
->external
&& content
->recovery_blocked
&& start_reshape
)
2010 block_subarray(content
);
2012 for (dev2
= sra
->devs
; dev2
; dev2
= dev2
->next
) {
2013 for (dev
= content
->devs
; dev
; dev
= dev
->next
)
2014 if (dev2
->disk
.major
== dev
->disk
.major
&&
2015 dev2
->disk
.minor
== dev
->disk
.minor
)
2019 /* Don't want this one any more */
2020 if (sysfs_set_str(sra
, dev2
, "slot", "none") < 0 &&
2022 pr_err("Cannot remove old device %s: not updating %s\n", dev2
->sys_name
, sra
->sys_name
);
2026 sysfs_set_str(sra
, dev2
, "state", "remove");
2028 old_raid_disks
= content
->array
.raid_disks
- content
->delta_disks
;
2029 avail
= xcalloc(content
->array
.raid_disks
, 1);
2030 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
2031 if (dev
->disk
.raid_disk
>= 0)
2032 avail
[dev
->disk
.raid_disk
] = 1;
2033 if (sysfs_add_disk(content
, dev
, 1) == 0) {
2034 if (dev
->disk
.raid_disk
>= old_raid_disks
&&
2035 content
->reshape_active
)
2039 } else if (errno
== EEXIST
)
2040 array
.preexist_cnt
++;
2044 all_disks
= array
.new_cnt
+ array
.exp_cnt
+ array
.preexist_cnt
;
2046 map_update(NULL
, fd2devnm(mdfd
), content
->text_version
,
2047 content
->uuid
, chosen_name
);
2049 if (content
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
2050 st
->ss
->validate_ppl
) {
2051 content
->array
.state
|= 1;
2054 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
2059 ret
= st
->ss
->validate_ppl(st
, content
, dev
);
2069 pr_err("%s contains invalid PPL - consider --force or --update-subarray with --update=no-ppl\n",
2071 content
->array
.state
&= ~1;
2072 avail
[dev
->disk
.raid_disk
] = 0;
2076 /* have --force - overwrite the invalid ppl */
2077 devpath
= map_dev(dev
->disk
.major
, dev
->disk
.minor
, 0);
2078 dfd
= dev_open(devpath
, O_RDWR
);
2080 pr_err("Failed to open %s\n", devpath
);
2085 err
= st
->ss
->write_init_ppl(st
, content
, dfd
);
2096 } else if (c
->force
) {
2097 /* Set the array as 'clean' so that we can proceed with starting
2098 * it even if we don't have all devices. Mdmon doesn't care
2099 * if the dirty flag is set in metadata, it will start managing
2101 * This is really important for raid456 (RWH case), other levels
2102 * are started anyway.
2104 content
->array
.state
|= 1;
2107 is_raid456
= (content
->array
.level
>= 4 && content
->array
.level
<= 6);
2108 is_clean
= content
->array
.state
& 1;
2110 if (enough(content
->array
.level
, content
->array
.raid_disks
,
2111 content
->array
.layout
, is_clean
, avail
) == 0) {
2112 set_array_assembly_status(c
, result
, INCR_NO
, &array
);
2114 if (c
->verbose
>= 0 && is_raid456
&& !is_clean
)
2115 pr_err("Consider --force to start dirty degraded array\n");
2122 if (c
->runstop
<= 0 && all_disks
< content
->array
.working_disks
) {
2124 set_array_assembly_status(c
, result
, INCR_UNSAFE
, &array
);
2126 if (c
->verbose
>= 0 && c
->force
)
2127 pr_err("Consider --run to start array as degraded.\n");
2131 if (is_raid456
&& content
->resync_start
!= MaxSector
&& c
->force
&&
2132 all_disks
< content
->array
.raid_disks
) {
2134 content
->resync_start
= MaxSector
;
2135 err
= sysfs_set_num(content
, NULL
, "resync_start", MaxSector
);
2139 pr_err("%s array state forced to clean. It may cause data corruption.\n",
2144 * Before activating the array, perform extra steps required
2145 * to configure the internal write-intent bitmap.
2147 if (content
->consistency_policy
== CONSISTENCY_POLICY_BITMAP
&&
2149 st
->ss
->set_bitmap(st
, content
);
2151 if (start_reshape
) {
2152 int spare
= content
->array
.raid_disks
+ array
.exp_cnt
;
2153 if (restore_backup(st
, content
,
2155 spare
, &c
->backup_file
, c
->verbose
) == 1)
2158 if (content
->reshape_progress
== 0) {
2159 /* If reshape progress is 0 - we are assembling the
2160 * array that was stopped, before reshape has started.
2161 * Array needs to be started as active, Grow_continue()
2162 * will start the reshape.
2164 sysfs_set_num(content
, NULL
, "reshape_position",
2166 err
= sysfs_set_str(content
, NULL
,
2167 "array_state", "active");
2168 sysfs_set_num(content
, NULL
, "reshape_position", 0);
2170 err
= sysfs_set_str(content
, NULL
,
2171 "array_state", "readonly");
2177 if (st
->ss
->external
) {
2178 if (!mdmon_running(st
->container_devnm
))
2179 start_mdmon(st
->container_devnm
);
2180 ping_monitor(st
->container_devnm
);
2181 if (mdmon_running(st
->container_devnm
) &&
2182 st
->update_tail
== NULL
)
2183 st
->update_tail
= &st
->updates
;
2186 err
= Grow_continue(mdfd
, st
, content
, c
->backup_file
,
2187 0, c
->freeze_reshape
);
2188 } else switch(content
->array
.level
) {
2190 case LEVEL_MULTIPATH
:
2192 err
= sysfs_set_str(content
, NULL
, "array_state",
2193 c
->readonly
? "readonly" : "active");
2196 err
= sysfs_set_str(content
, NULL
, "array_state",
2198 /* start mdmon if needed. */
2200 if (!mdmon_running(st
->container_devnm
))
2201 start_mdmon(st
->container_devnm
);
2202 ping_monitor(st
->container_devnm
);
2207 sysfs_set_safemode(content
, content
->safe_mode_delay
);
2209 /* Block subarray here if it is not reshaped now
2210 * It has be blocked a little later to allow mdmon to switch in
2213 if (st
->ss
->external
&& content
->recovery_blocked
&&
2215 block_subarray(content
);
2218 set_array_assembly_status(c
, result
, INCR_NO
, &array
);
2220 set_array_assembly_status(c
, result
, INCR_YES
, &array
);
2221 wait_for(chosen_name
, mdfd
);
2222 sysfs_rules_apply(chosen_name
, content
);
2226 /* FIXME should have an O_EXCL and wait for read-auto */