2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
30 static int round_size_and_verify(unsigned long long *size
, int chunk
)
34 *size
&= ~(unsigned long long)(chunk
- 1);
36 pr_err("Size cannot be smaller than chunk.\n");
42 static int default_layout(struct supertype
*st
, int level
, int verbose
)
46 if (st
&& st
->ss
->default_geometry
)
47 st
->ss
->default_geometry(st
, &level
, &layout
, NULL
);
51 default: /* no layout */
55 layout
= RAID0_ORIG_LAYOUT
;
58 layout
= 0x102; /* near=2, far=1 */
60 pr_err("layout defaults to n2\n");
64 layout
= map_name(r5layout
, "default");
66 pr_err("layout defaults to %s\n", map_num(r5layout
, layout
));
69 layout
= map_name(faultylayout
, "default");
72 pr_err("layout defaults to %s\n", map_num(faultylayout
, layout
));
79 int Create(struct supertype
*st
, char *mddev
,
80 char *name
, int *uuid
,
81 int subdevs
, struct mddev_dev
*devlist
,
83 struct context
*c
, unsigned long long data_offset
)
86 * Create a new raid array.
88 * First check that necessary details are available
89 * (i.e. level, raid-disks)
91 * Then check each disk to see what might be on it
92 * and report anything interesting.
94 * If anything looks odd, and runstop not set,
97 * SET_ARRAY_INFO and ADD_NEW_DISK, and
98 * if runstop==run, or raiddisks disks were used,
102 unsigned long long minsize
= 0, maxsize
= 0;
103 char *mindisc
= NULL
;
104 char *maxdisc
= NULL
;
105 int dnum
, raid_disk_num
;
106 struct mddev_dev
*dv
;
108 int fail
= 0, warn
= 0;
109 int first_missing
= subdevs
* 2;
110 int second_missing
= subdevs
* 2;
111 int missing_disks
= 0;
112 int insert_point
= subdevs
* 2; /* where to insert a missing drive */
117 int have_container
= 0;
118 int container_fd
= -1;
120 unsigned long long bitmapsize
;
121 struct mdinfo info
, *infos
;
123 int do_default_layout
= 0;
124 int do_default_chunk
= 0;
125 unsigned long safe_mode_delay
= 0;
126 char chosen_name
[1024];
127 struct map_ent
*map
= NULL
;
128 unsigned long long newsize
;
129 mdu_array_info_t inf
;
131 int major_num
= BITMAP_MAJOR_HI
;
132 if (s
->bitmap_file
&& strcmp(s
->bitmap_file
, "clustered") == 0) {
133 major_num
= BITMAP_MAJOR_CLUSTERED
;
135 pr_err("At least 2 nodes are needed for cluster-md\n");
140 memset(&info
, 0, sizeof(info
));
141 if (s
->level
== UnSet
&& st
&& st
->ss
->default_geometry
)
142 st
->ss
->default_geometry(st
, &s
->level
, NULL
, NULL
);
143 if (s
->level
== UnSet
) {
144 pr_err("a RAID level is needed to create an array.\n");
147 if (s
->raiddisks
< 4 && s
->level
== 6) {
148 pr_err("at least 4 raid-devices needed for level 6\n");
151 if (s
->raiddisks
> 256 && s
->level
== 6) {
152 pr_err("no more than 256 raid-devices supported for level 6\n");
155 if (s
->raiddisks
< 2 && s
->level
>= 4) {
156 pr_err("at least 2 raid-devices needed for level 4 or 5\n");
159 if (s
->level
<= 0 && s
->sparedisks
) {
160 pr_err("This level does not support spare devices\n");
164 if (subdevs
== 1 && strcmp(devlist
->devname
, "missing") != 0) {
165 /* If given a single device, it might be a container, and we can
166 * extract a device list from there
170 memset(&inf
, 0, sizeof(inf
));
171 fd
= open(devlist
->devname
, O_RDONLY
);
173 md_get_array_info(fd
, &inf
) == 0 && inf
.raid_disks
== 0) {
174 /* yep, looks like a container */
176 rv
= st
->ss
->load_container(st
, fd
,
181 st
= super_by_fd(fd
, NULL
);
182 if (st
&& !(rv
= st
->ss
->
183 load_container(st
, fd
,
189 if (have_container
) {
190 subdevs
= s
->raiddisks
;
191 first_missing
= subdevs
* 2;
192 second_missing
= subdevs
* 2;
193 insert_point
= subdevs
* 2;
199 if (st
&& st
->ss
->external
&& s
->sparedisks
) {
200 pr_err("This metadata type does not support spare disks at create time\n");
203 if (subdevs
> s
->raiddisks
+s
->sparedisks
+s
->journaldisks
) {
204 pr_err("You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, s
->raiddisks
+s
->sparedisks
);
207 if (!have_container
&& subdevs
< s
->raiddisks
+s
->sparedisks
+s
->journaldisks
) {
208 pr_err("You haven't given enough devices (real or missing) to create this array\n");
211 if (s
->bitmap_file
&& s
->level
<= 0) {
212 pr_err("bitmaps not meaningful with level %s\n",
213 map_num(pers
, s
->level
)?:"given");
217 /* now set some defaults */
219 if (s
->layout
== UnSet
) {
220 do_default_layout
= 1;
221 s
->layout
= default_layout(st
, s
->level
, c
->verbose
);
225 /* check layout fits in array*/
226 if ((s
->layout
&255) * ((s
->layout
>>8)&255) > s
->raiddisks
) {
227 pr_err("that layout requires at least %d devices\n",
228 (s
->layout
&255) * ((s
->layout
>>8)&255));
238 if (s
->chunk
== 0 || s
->chunk
== UnSet
) {
240 do_default_chunk
= 1;
241 /* chunk will be set later */
245 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
246 if (get_linux_version() < 2006016 && s
->chunk
== 0) {
249 pr_err("chunk size defaults to 64K\n");
254 case LEVEL_MULTIPATH
:
255 case LEVEL_CONTAINER
:
259 pr_err("chunk size ignored for this level\n");
263 pr_err("unknown level %d\n", s
->level
);
267 if (s
->size
== MAX_SIZE
)
268 /* use '0' to mean 'max' now... */
270 if (s
->size
&& s
->chunk
&& s
->chunk
!= UnSet
)
271 if (round_size_and_verify(&s
->size
, s
->chunk
))
274 newsize
= s
->size
* 2;
275 if (st
&& ! st
->ss
->validate_geometry(st
, s
->level
, s
->layout
, s
->raiddisks
,
276 &s
->chunk
, s
->size
*2,
278 &newsize
, s
->consistency_policy
,
282 if (s
->chunk
&& s
->chunk
!= UnSet
) {
283 newsize
&= ~(unsigned long long)(s
->chunk
*2 - 1);
284 if (do_default_chunk
) {
285 /* default chunk was just set */
287 pr_err("chunk size defaults to %dK\n", s
->chunk
);
288 if (round_size_and_verify(&s
->size
, s
->chunk
))
290 do_default_chunk
= 0;
295 s
->size
= newsize
/ 2;
297 /* If this is ever reshaped to RAID5, we will
298 * need a chunksize. So round it off a bit
299 * now just to be safe
301 s
->size
&= ~(64ULL-1);
303 if (s
->size
&& c
->verbose
> 0)
304 pr_err("setting size to %lluK\n", s
->size
);
307 /* now look at the subdevs */
308 info
.array
.active_disks
= 0;
309 info
.array
.working_disks
= 0;
311 for (dv
= devlist
; dv
; dv
= dv
->next
)
312 if (data_offset
== VARIABLE_OFFSET
)
313 dv
->data_offset
= INVALID_SECTORS
;
315 dv
->data_offset
= data_offset
;
317 for (dv
=devlist
; dv
&& !have_container
; dv
=dv
->next
, dnum
++) {
318 char *dname
= dv
->devname
;
319 unsigned long long freesize
;
323 if (strcasecmp(dname
, "missing") == 0) {
324 if (first_missing
> dnum
)
325 first_missing
= dnum
;
326 if (second_missing
> dnum
&& dnum
> first_missing
)
327 second_missing
= dnum
;
331 if (data_offset
== VARIABLE_OFFSET
) {
332 doff
= strchr(dname
, ':');
335 dv
->data_offset
= parse_size(doff
);
337 dv
->data_offset
= INVALID_SECTORS
;
339 dv
->data_offset
= data_offset
;
341 dfd
= open(dname
, O_RDONLY
);
343 pr_err("cannot open %s: %s\n",
344 dname
, strerror(errno
));
347 if (!fstat_is_blkdev(dfd
, dname
, NULL
)) {
352 info
.array
.working_disks
++;
353 if (dnum
< s
->raiddisks
&& dv
->disposition
!= 'j')
354 info
.array
.active_disks
++;
356 struct createinfo
*ci
= conf_get_create_info();
361 /* Need to choose a default metadata, which is different
362 * depending on geometry of array.
365 char *name
= "default";
366 for(i
= 0; !st
&& superlist
[i
]; i
++) {
367 st
= superlist
[i
]->match_metadata_desc(name
);
370 if (do_default_layout
)
371 s
->layout
= default_layout(st
, s
->level
, c
->verbose
);
372 switch (st
->ss
->validate_geometry(
373 st
, s
->level
, s
->layout
, s
->raiddisks
,
374 &s
->chunk
, s
->size
*2,
375 dv
->data_offset
, dname
,
376 &freesize
, s
->consistency_policy
,
378 case -1: /* Not valid, message printed, and not
379 * worth checking any further */
382 case 0: /* Geometry not valid */
385 s
->chunk
= do_default_chunk
? UnSet
: s
->chunk
;
387 case 1: /* All happy */
393 int dfd
= open(dname
, O_RDONLY
|O_EXCL
);
395 pr_err("cannot open %s: %s\n",
396 dname
, strerror(errno
));
399 pr_err("device %s not suitable for any style of array\n",
403 if (st
->ss
!= &super0
||
404 st
->minor_version
!= 90)
407 if (do_default_layout
)
408 s
->layout
= default_layout(st
, s
->level
, 0);
409 if (!st
->ss
->validate_geometry(st
, s
->level
, s
->layout
,
411 &s
->chunk
, s
->size
*2,
414 s
->consistency_policy
,
417 pr_err("%s is not suitable for this array.\n",
424 if (dv
->disposition
== 'j')
425 goto skip_size_check
; /* skip write journal for size check */
427 freesize
/= 2; /* convert to K */
428 if (s
->chunk
&& s
->chunk
!= UnSet
) {
429 /* round to chunk size */
430 freesize
= freesize
& ~(s
->chunk
-1);
431 if (do_default_chunk
) {
432 /* default chunk was just set */
434 pr_err("chunk size defaults to %dK\n", s
->chunk
);
435 if (round_size_and_verify(&s
->size
, s
->chunk
))
437 do_default_chunk
= 0;
441 pr_err("no free space left on %s\n", dname
);
446 if (s
->size
&& freesize
< s
->size
) {
447 pr_err("%s is smaller than given size. %lluK < %lluK + metadata\n",
448 dname
, freesize
, s
->size
);
452 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
456 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
461 if (c
->runstop
!= 1 || c
->verbose
>= 0) {
462 int fd
= open(dname
, O_RDONLY
);
464 pr_err("Cannot open %s: %s\n",
465 dname
, strerror(errno
));
469 warn
|= check_ext2(fd
, dname
);
470 warn
|= check_reiser(fd
, dname
);
471 warn
|= check_raid(fd
, dname
);
472 if (strcmp(st
->ss
->name
, "1.x") == 0 &&
473 st
->minor_version
>= 1)
474 /* metadata at front */
475 warn
|= check_partitions(fd
, dname
, 0, 0);
476 else if (s
->level
== 1 || s
->level
== LEVEL_CONTAINER
||
477 (s
->level
== 0 && s
->raiddisks
== 1))
478 /* partitions could be meaningful */
479 warn
|= check_partitions(fd
, dname
, freesize
*2, s
->size
*2);
481 /* partitions cannot be meaningful */
482 warn
|= check_partitions(fd
, dname
, 0, 0);
483 if (strcmp(st
->ss
->name
, "1.x") == 0 &&
484 st
->minor_version
>= 1 &&
487 (warn
& 1024) == 0) {
489 pr_err("Note: this array has metadata at the start and\n"
490 " may not be suitable as a boot device. If you plan to\n"
491 " store '/boot' on this device please ensure that\n"
492 " your boot-loader understands md/v1.x metadata, or use\n"
493 " --metadata=0.90\n");
498 if (missing_disks
== dnum
&& !have_container
) {
499 pr_err("Subdevs can't be all missing\n");
502 if (s
->raiddisks
+ s
->sparedisks
> st
->max_devs
) {
503 pr_err("Too many devices: %s metadata only supports %d\n",
504 st
->ss
->name
, st
->max_devs
);
508 info
.array
.working_disks
= s
->raiddisks
;
510 pr_err("create aborted\n");
514 if (mindisc
== NULL
&& !have_container
) {
515 pr_err("no size and no drives given - aborting create.\n");
518 if (s
->level
> 0 || s
->level
== LEVEL_MULTIPATH
||
519 s
->level
== LEVEL_FAULTY
|| st
->ss
->external
) {
520 /* size is meaningful */
521 if (!st
->ss
->validate_geometry(st
, s
->level
, s
->layout
,
523 &s
->chunk
, minsize
*2,
526 s
->consistency_policy
, 0)) {
527 pr_err("devices too large for RAID level %d\n", s
->level
);
532 /* If this is ever reshaped to RAID5, we will
533 * need a chunksize. So round it off a bit
534 * now just to be safe
536 s
->size
&= ~(64ULL-1);
538 pr_err("size set to %lluK\n", s
->size
);
542 if (!s
->bitmap_file
&&
544 st
->ss
->add_internal_bitmap
&&
545 s
->journaldisks
== 0 &&
546 (s
->consistency_policy
!= CONSISTENCY_POLICY_RESYNC
&&
547 s
->consistency_policy
!= CONSISTENCY_POLICY_PPL
) &&
548 (s
->write_behind
|| s
->size
> 100*1024*1024ULL)) {
550 pr_err("automatically enabling write-intent bitmap on large array\n");
551 s
->bitmap_file
= "internal";
553 if (s
->bitmap_file
&& strcmp(s
->bitmap_file
, "none") == 0)
554 s
->bitmap_file
= NULL
;
556 if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
557 !st
->ss
->write_init_ppl
) {
558 pr_err("%s metadata does not support PPL\n", st
->ss
->name
);
562 if (!have_container
&& s
->level
> 0 && ((maxsize
-s
->size
)*100 > maxsize
)) {
563 if (c
->runstop
!= 1 || c
->verbose
>= 0)
564 pr_err("largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
569 if (st
->ss
->detail_platform
&& st
->ss
->detail_platform(0, 1, NULL
) != 0) {
570 if (c
->runstop
!= 1 || c
->verbose
>= 0)
571 pr_err("%s unable to enumerate platform support\n"
572 " array may not be compatible with hardware/firmware\n",
576 st
->nodes
= c
->nodes
;
577 st
->cluster_name
= c
->homecluster
;
580 if (c
->runstop
!= 1) {
581 if (!ask("Continue creating array? ")) {
582 pr_err("create aborted.\n");
587 pr_err("creation continuing despite oddities due to --run\n");
591 /* If this is raid4/5, we want to configure the last active slot
592 * as missing, so that a reconstruct happens (faster than re-parity)
593 * FIX: Can we do this for raid6 as well?
595 if (st
->ss
->external
== 0 && s
->assume_clean
== 0 &&
596 c
->force
== 0 && first_missing
>= s
->raiddisks
) {
600 insert_point
= s
->raiddisks
-1;
602 info
.array
.active_disks
--;
609 /* For raid6, if creating with 1 missing drive, make a good drive
610 * into a spare, else the create will fail
612 if (s
->assume_clean
== 0 && c
->force
== 0 && first_missing
< s
->raiddisks
&&
613 st
->ss
->external
== 0 &&
614 second_missing
>= s
->raiddisks
&& s
->level
== 6) {
615 insert_point
= s
->raiddisks
- 1;
616 if (insert_point
== first_missing
)
619 info
.array
.active_disks
--;
623 if (s
->level
<= 0 && first_missing
< subdevs
* 2) {
624 pr_err("This level does not support missing devices\n");
628 /* We need to create the device */
630 mdfd
= create_mddev(mddev
, name
, c
->autof
, LOCAL
, chosen_name
, 1);
635 /* verify if chosen_name is not in use,
636 * it could be in conflict with already existing device
637 * e.g. container, array
639 if (strncmp(chosen_name
, "/dev/md/", 8) == 0 &&
640 map_by_name(&map
, chosen_name
+8) != NULL
) {
641 pr_err("Array name %s is in use already.\n",
650 memset(&inf
, 0, sizeof(inf
));
651 md_get_array_info(mdfd
, &inf
);
652 if (inf
.working_disks
!= 0) {
653 pr_err("another array by this name is already running.\n");
657 /* Ok, lets try some ioctls */
659 info
.array
.level
= s
->level
;
660 info
.array
.size
= s
->size
;
661 info
.array
.raid_disks
= s
->raiddisks
;
662 /* The kernel should *know* what md_minor we are dealing
663 * with, but it chooses to trust me instead. Sigh
665 info
.array
.md_minor
= 0;
666 if (fstat_is_blkdev(mdfd
, mddev
, &rdev
))
667 info
.array
.md_minor
= minor(rdev
);
668 info
.array
.not_persistent
= 0;
670 if (((s
->level
== 4 || s
->level
== 5) &&
671 (insert_point
< s
->raiddisks
|| first_missing
< s
->raiddisks
)) ||
672 (s
->level
== 6 && (insert_point
< s
->raiddisks
||
673 second_missing
< s
->raiddisks
)) ||
674 (s
->level
<= 0) || s
->assume_clean
) {
675 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
676 info
.resync_start
= MaxSector
;
678 info
.array
.state
= 0; /* not clean, but no errors */
679 info
.resync_start
= 0;
681 if (s
->level
== 10) {
682 /* for raid10, the bitmap size is the capacity of the array,
683 * which is array.size * raid_disks / ncopies;
684 * .. but convert to sectors.
686 int ncopies
= ((s
->layout
>>8) & 255) * (s
->layout
& 255);
687 bitmapsize
= s
->size
* s
->raiddisks
/ ncopies
* 2;
688 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, s->size, s->raiddisks, ncopies);*/
690 bitmapsize
= s
->size
* 2;
692 /* There is lots of redundancy in these disk counts,
693 * raid_disks is the most meaningful value
694 * it describes the geometry of the array
696 * nr_disks is total number of used slots.
697 * it should be raid_disks+spare_disks
698 * spare_disks is the number of extra disks present
700 * active_disks is the number of working disks in
701 * active slots. (With raid_disks)
702 * working_disks is the total number of working disks,
704 * failed_disks is the number of disks marked failed
706 * Ideally, the kernel would keep these (except raid_disks)
707 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
708 * So for now, we assume that all raid and spare
709 * devices will be given.
711 info
.array
.spare_disks
=s
->sparedisks
;
712 info
.array
.failed_disks
=missing_disks
;
713 info
.array
.nr_disks
= info
.array
.working_disks
714 + info
.array
.failed_disks
;
715 info
.array
.layout
= s
->layout
;
716 info
.array
.chunk_size
= s
->chunk
*1024;
718 if (name
== NULL
|| *name
== 0) {
719 /* base name on mddev */
725 * /dev/md/home -> home
726 * /dev/mdhome -> home
728 /* FIXME compare this with rules in create_mddev */
729 name
= strrchr(mddev
, '/');
732 if (strncmp(name
, "md_", 3) == 0 &&
733 strlen(name
) > 3 && (name
-mddev
) == 5 /* /dev/ */)
735 else if (strncmp(name
, "md", 2) == 0 &&
736 strlen(name
) > 2 && isdigit(name
[2]) &&
737 (name
-mddev
) == 5 /* /dev/ */)
741 if (!st
->ss
->init_super(st
, &info
.array
, s
, name
, c
->homehost
, uuid
,
745 total_slots
= info
.array
.nr_disks
;
746 st
->ss
->getinfo_super(st
, &info
, NULL
);
747 if (sysfs_init(&info
, mdfd
, NULL
)) {
748 pr_err("unable to initialize sysfs\n");
752 if (did_default
&& c
->verbose
>= 0) {
753 if (is_subarray(info
.text_version
)) {
758 strncpy(devnm
, info
.text_version
+1, 32);
760 ep
= strchr(devnm
, '/');
764 mdi
= sysfs_read(-1, devnm
, GET_VERSION
);
766 pr_err("Creating array inside %s container %s\n",
767 mdi
?mdi
->text_version
:"managed", devnm
);
770 pr_err("Defaulting to version %s metadata\n", info
.text_version
);
773 map_update(&map
, fd2devnm(mdfd
), info
.text_version
,
774 info
.uuid
, chosen_name
);
775 /* Keep map locked until devices have been added to array
776 * to stop another mdadm from finding and using those devices.
779 if (s
->bitmap_file
&& (strcmp(s
->bitmap_file
, "internal") == 0 ||
780 strcmp(s
->bitmap_file
, "clustered") == 0)) {
781 if (!st
->ss
->add_internal_bitmap
) {
782 pr_err("internal bitmaps not supported with %s metadata\n",
786 if (st
->ss
->add_internal_bitmap(st
, &s
->bitmap_chunk
,
787 c
->delay
, s
->write_behind
,
788 bitmapsize
, 1, major_num
)) {
789 pr_err("Given bitmap chunk size not supported.\n");
792 s
->bitmap_file
= NULL
;
795 if (sysfs_init(&info
, mdfd
, NULL
)) {
796 pr_err("unable to initialize sysfs\n");
800 if (st
->ss
->external
&& st
->container_devnm
[0]) {
803 /* When creating a member, we need to be careful
804 * to negotiate with mdmon properly.
805 * If it is already running, we cannot write to
806 * the devices and must ask it to do that part.
807 * If it isn't running, we write to the devices,
809 * We hold an exclusive open on the container
810 * device to make sure mdmon doesn't exit after
811 * we checked that it is running.
813 * For now, fail if it is already running.
815 container_fd
= open_dev_excl(st
->container_devnm
);
816 if (container_fd
< 0) {
817 pr_err("Cannot get exclusive open on container - weird.\n");
820 if (mdmon_running(st
->container_devnm
)) {
822 pr_err("reusing mdmon for %s.\n",
823 st
->container_devnm
);
824 st
->update_tail
= &st
->updates
;
828 rv
= set_array_info(mdfd
, st
, &info
);
830 pr_err("failed to set array info for %s: %s\n",
831 mddev
, strerror(errno
));
835 if (s
->bitmap_file
) {
838 st
->ss
->uuid_from_super(st
, uuid
);
839 if (CreateBitmap(s
->bitmap_file
, c
->force
, (char*)uuid
, s
->bitmap_chunk
,
840 c
->delay
, s
->write_behind
,
845 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
847 pr_err("weird: %s cannot be opened\n",
851 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
852 pr_err("Cannot set bitmap file for %s: %s\n",
853 mddev
, strerror(errno
));
858 infos
= xmalloc(sizeof(*infos
) * total_slots
);
859 enable_fds(total_slots
);
860 for (pass
= 1; pass
<= 2; pass
++) {
861 struct mddev_dev
*moved_disk
= NULL
; /* the disk that was moved out of the insert point */
863 for (dnum
= 0, raid_disk_num
= 0, dv
= devlist
; dv
;
864 dv
= (dv
->next
) ? (dv
->next
) : moved_disk
, dnum
++) {
866 struct mdinfo
*inf
= &infos
[dnum
];
868 if (dnum
>= total_slots
)
870 if (dnum
== insert_point
) {
875 if (strcasecmp(dv
->devname
, "missing") == 0) {
881 if (have_container
&& dnum
< info
.array
.raid_disks
- 1)
882 /* repeatedly use the container */
889 inf
->disk
.number
= dnum
;
890 inf
->disk
.raid_disk
= raid_disk_num
++;
892 if (dv
->disposition
== 'j') {
893 inf
->disk
.raid_disk
= MD_DISK_ROLE_JOURNAL
;
894 inf
->disk
.state
= (1<<MD_DISK_JOURNAL
);
896 } else if (inf
->disk
.raid_disk
< s
->raiddisks
)
897 inf
->disk
.state
= (1<<MD_DISK_ACTIVE
) |
902 if (dv
->writemostly
== FlagSet
)
903 inf
->disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
904 if (dv
->failfast
== FlagSet
)
905 inf
->disk
.state
|= (1<<MD_DISK_FAILFAST
);
910 if (st
->ss
->external
&&
911 st
->container_devnm
[0])
912 fd
= open(dv
->devname
, O_RDWR
);
914 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
);
917 pr_err("failed to open %s after earlier success - aborting\n",
921 if (!fstat_is_blkdev(fd
, dv
->devname
, &rdev
))
923 inf
->disk
.major
= major(rdev
);
924 inf
->disk
.minor
= minor(rdev
);
927 remove_partitions(fd
);
928 if (st
->ss
->add_to_super(st
, &inf
->disk
,
931 ioctl(mdfd
, STOP_ARRAY
, NULL
);
934 st
->ss
->getinfo_super(st
, inf
, NULL
);
935 safe_mode_delay
= inf
->safe_mode_delay
;
937 if (have_container
&& c
->verbose
> 0)
938 pr_err("Using %s for device %d\n",
939 map_dev(inf
->disk
.major
,
943 if (!have_container
) {
944 /* getinfo_super might have lost these ... */
945 inf
->disk
.major
= major(rdev
);
946 inf
->disk
.minor
= minor(rdev
);
952 rv
= add_disk(mdfd
, st
, &info
, inf
);
955 pr_err("ADD_NEW_DISK for %s failed: %s\n",
956 dv
->devname
, strerror(errno
));
957 if (errno
== EINVAL
&&
958 info
.array
.level
== 0) {
959 pr_err("Possibly your kernel doesn't support RAID0 layouts.\n");
960 pr_err("Either upgrade, or use --layout=dangerous\n");
966 if (!have_container
&&
967 dv
== moved_disk
&& dnum
!= insert_point
) break;
970 struct mdinfo info_new
;
971 struct map_ent
*me
= NULL
;
973 /* check to see if the uuid has changed due to these
974 * metadata changes, and if so update the member array
975 * and container uuid. Note ->write_init_super clears
976 * the subarray cursor such that ->getinfo_super once
977 * again returns container info.
979 st
->ss
->getinfo_super(st
, &info_new
, NULL
);
980 if (st
->ss
->external
&& s
->level
!= LEVEL_CONTAINER
&&
981 !same_uuid(info_new
.uuid
, info
.uuid
, 0)) {
982 map_update(&map
, fd2devnm(mdfd
),
983 info_new
.text_version
,
984 info_new
.uuid
, chosen_name
);
985 me
= map_by_devnm(&map
, st
->container_devnm
);
988 if (st
->ss
->write_init_super(st
)) {
989 st
->ss
->free_super(st
);
993 /* update parent container uuid */
995 char *path
= xstrdup(me
->path
);
997 st
->ss
->getinfo_super(st
, &info_new
, NULL
);
998 map_update(&map
, st
->container_devnm
,
999 info_new
.text_version
,
1000 info_new
.uuid
, path
);
1004 flush_metadata_updates(st
);
1005 st
->ss
->free_super(st
);
1011 if (s
->level
== LEVEL_CONTAINER
) {
1012 /* No need to start. But we should signal udev to
1014 sysfs_uevent(&info
, "change");
1015 if (c
->verbose
>= 0)
1016 pr_err("container %s prepared.\n", mddev
);
1017 wait_for(chosen_name
, mdfd
);
1018 } else if (c
->runstop
== 1 || subdevs
>= s
->raiddisks
) {
1019 if (st
->ss
->external
) {
1023 case LEVEL_MULTIPATH
:
1025 err
= sysfs_set_str(&info
, NULL
, "array_state",
1032 err
= sysfs_set_str(&info
, NULL
, "array_state",
1036 sysfs_set_safemode(&info
, safe_mode_delay
);
1038 pr_err("failed to activate array.\n");
1039 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1042 } else if (c
->readonly
&&
1043 sysfs_attribute_available(
1044 &info
, NULL
, "array_state")) {
1045 if (sysfs_set_str(&info
, NULL
,
1046 "array_state", "readonly") < 0) {
1047 pr_err("Failed to start array: %s\n",
1049 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1053 /* param is not actually used */
1055 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
1056 pr_err("RUN_ARRAY failed: %s\n",
1058 if (errno
== 524 /* ENOTSUP */ &&
1059 info
.array
.level
== 0)
1060 cont_err("Please use --layout=original or --layout=alternate\n");
1061 if (info
.array
.chunk_size
& (info
.array
.chunk_size
-1)) {
1062 cont_err("Problem may be that chunk size is not a power of 2\n");
1064 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1067 /* if start_ro module parameter is set, array is
1068 * auto-read-only, which is bad as the resync won't
1069 * start. So lets make it read-write now.
1071 ioctl(mdfd
, RESTART_ARRAY_RW
, NULL
);
1073 if (c
->verbose
>= 0)
1074 pr_err("array %s started.\n", mddev
);
1075 if (st
->ss
->external
&& st
->container_devnm
[0]) {
1077 start_mdmon(st
->container_devnm
);
1079 ping_monitor(st
->container_devnm
);
1080 close(container_fd
);
1082 wait_for(chosen_name
, mdfd
);
1084 pr_err("not starting array - not enough devices.\n");
1088 sysfs_uevent(&info
, "change");
1095 map_remove(&map
, fd2devnm(mdfd
));