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
&&
545 st
->ss
->add_internal_bitmap
&&
546 s
->journaldisks
== 0 &&
547 (s
->consistency_policy
!= CONSISTENCY_POLICY_RESYNC
&&
548 s
->consistency_policy
!= CONSISTENCY_POLICY_PPL
) &&
549 (s
->write_behind
|| s
->size
> 100*1024*1024ULL)) {
551 pr_err("automatically enabling write-intent bitmap on large array\n");
552 s
->bitmap_file
= "internal";
554 if (s
->bitmap_file
&& strcmp(s
->bitmap_file
, "none") == 0)
555 s
->bitmap_file
= NULL
;
557 if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
558 !st
->ss
->write_init_ppl
) {
559 pr_err("%s metadata does not support PPL\n", st
->ss
->name
);
563 if (!have_container
&& s
->level
> 0 && ((maxsize
-s
->size
)*100 > maxsize
)) {
564 if (c
->runstop
!= 1 || c
->verbose
>= 0)
565 pr_err("largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
570 if (st
->ss
->detail_platform
&& st
->ss
->detail_platform(0, 1, NULL
) != 0) {
571 if (c
->runstop
!= 1 || c
->verbose
>= 0)
572 pr_err("%s unable to enumerate platform support\n"
573 " array may not be compatible with hardware/firmware\n",
577 st
->nodes
= c
->nodes
;
578 st
->cluster_name
= c
->homecluster
;
581 if (c
->runstop
!= 1) {
582 if (!ask("Continue creating array? ")) {
583 pr_err("create aborted.\n");
588 pr_err("creation continuing despite oddities due to --run\n");
592 /* If this is raid4/5, we want to configure the last active slot
593 * as missing, so that a reconstruct happens (faster than re-parity)
594 * FIX: Can we do this for raid6 as well?
596 if (st
->ss
->external
== 0 && s
->assume_clean
== 0 &&
597 c
->force
== 0 && first_missing
>= s
->raiddisks
) {
601 insert_point
= s
->raiddisks
-1;
603 info
.array
.active_disks
--;
610 /* For raid6, if creating with 1 missing drive, make a good drive
611 * into a spare, else the create will fail
613 if (s
->assume_clean
== 0 && c
->force
== 0 && first_missing
< s
->raiddisks
&&
614 st
->ss
->external
== 0 &&
615 second_missing
>= s
->raiddisks
&& s
->level
== 6) {
616 insert_point
= s
->raiddisks
- 1;
617 if (insert_point
== first_missing
)
620 info
.array
.active_disks
--;
624 if (s
->level
<= 0 && first_missing
< subdevs
* 2) {
625 pr_err("This level does not support missing devices\n");
629 /* We need to create the device */
631 mdfd
= create_mddev(mddev
, name
, c
->autof
, LOCAL
, chosen_name
, 1);
636 /* verify if chosen_name is not in use,
637 * it could be in conflict with already existing device
638 * e.g. container, array
640 if (strncmp(chosen_name
, "/dev/md/", 8) == 0 &&
641 map_by_name(&map
, chosen_name
+8) != NULL
) {
642 pr_err("Array name %s is in use already.\n",
651 memset(&inf
, 0, sizeof(inf
));
652 md_get_array_info(mdfd
, &inf
);
653 if (inf
.working_disks
!= 0) {
654 pr_err("another array by this name is already running.\n");
658 /* Ok, lets try some ioctls */
660 info
.array
.level
= s
->level
;
661 info
.array
.size
= s
->size
;
662 info
.array
.raid_disks
= s
->raiddisks
;
663 /* The kernel should *know* what md_minor we are dealing
664 * with, but it chooses to trust me instead. Sigh
666 info
.array
.md_minor
= 0;
667 if (fstat_is_blkdev(mdfd
, mddev
, &rdev
))
668 info
.array
.md_minor
= minor(rdev
);
669 info
.array
.not_persistent
= 0;
671 if (((s
->level
== 4 || s
->level
== 5) &&
672 (insert_point
< s
->raiddisks
|| first_missing
< s
->raiddisks
)) ||
673 (s
->level
== 6 && (insert_point
< s
->raiddisks
||
674 second_missing
< s
->raiddisks
)) ||
675 (s
->level
<= 0) || s
->assume_clean
) {
676 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
677 info
.resync_start
= MaxSector
;
679 info
.array
.state
= 0; /* not clean, but no errors */
680 info
.resync_start
= 0;
682 if (s
->level
== 10) {
683 /* for raid10, the bitmap size is the capacity of the array,
684 * which is array.size * raid_disks / ncopies;
685 * .. but convert to sectors.
687 int ncopies
= ((s
->layout
>>8) & 255) * (s
->layout
& 255);
688 bitmapsize
= s
->size
* s
->raiddisks
/ ncopies
* 2;
689 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, s->size, s->raiddisks, ncopies);*/
691 bitmapsize
= s
->size
* 2;
693 /* There is lots of redundancy in these disk counts,
694 * raid_disks is the most meaningful value
695 * it describes the geometry of the array
697 * nr_disks is total number of used slots.
698 * it should be raid_disks+spare_disks
699 * spare_disks is the number of extra disks present
701 * active_disks is the number of working disks in
702 * active slots. (With raid_disks)
703 * working_disks is the total number of working disks,
705 * failed_disks is the number of disks marked failed
707 * Ideally, the kernel would keep these (except raid_disks)
708 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
709 * So for now, we assume that all raid and spare
710 * devices will be given.
712 info
.array
.spare_disks
=s
->sparedisks
;
713 info
.array
.failed_disks
=missing_disks
;
714 info
.array
.nr_disks
= info
.array
.working_disks
715 + info
.array
.failed_disks
;
716 info
.array
.layout
= s
->layout
;
717 info
.array
.chunk_size
= s
->chunk
*1024;
719 if (name
== NULL
|| *name
== 0) {
720 /* base name on mddev */
726 * /dev/md/home -> home
727 * /dev/mdhome -> home
729 /* FIXME compare this with rules in create_mddev */
730 name
= strrchr(mddev
, '/');
733 if (strncmp(name
, "md_", 3) == 0 &&
734 strlen(name
) > 3 && (name
-mddev
) == 5 /* /dev/ */)
736 else if (strncmp(name
, "md", 2) == 0 &&
737 strlen(name
) > 2 && isdigit(name
[2]) &&
738 (name
-mddev
) == 5 /* /dev/ */)
742 if (!st
->ss
->init_super(st
, &info
.array
, s
, name
, c
->homehost
, uuid
,
746 total_slots
= info
.array
.nr_disks
;
747 st
->ss
->getinfo_super(st
, &info
, NULL
);
748 if (sysfs_init(&info
, mdfd
, NULL
)) {
749 pr_err("unable to initialize sysfs\n");
753 if (did_default
&& c
->verbose
>= 0) {
754 if (is_subarray(info
.text_version
)) {
759 strncpy(devnm
, info
.text_version
+1, 32);
761 ep
= strchr(devnm
, '/');
765 mdi
= sysfs_read(-1, devnm
, GET_VERSION
);
767 pr_err("Creating array inside %s container %s\n",
768 mdi
?mdi
->text_version
:"managed", devnm
);
771 pr_err("Defaulting to version %s metadata\n", info
.text_version
);
774 map_update(&map
, fd2devnm(mdfd
), info
.text_version
,
775 info
.uuid
, chosen_name
);
776 /* Keep map locked until devices have been added to array
777 * to stop another mdadm from finding and using those devices.
780 if (s
->bitmap_file
&& (strcmp(s
->bitmap_file
, "internal") == 0 ||
781 strcmp(s
->bitmap_file
, "clustered") == 0)) {
782 if (!st
->ss
->add_internal_bitmap
) {
783 pr_err("internal bitmaps not supported with %s metadata\n",
787 if (st
->ss
->add_internal_bitmap(st
, &s
->bitmap_chunk
,
788 c
->delay
, s
->write_behind
,
789 bitmapsize
, 1, major_num
)) {
790 pr_err("Given bitmap chunk size not supported.\n");
793 s
->bitmap_file
= NULL
;
796 if (sysfs_init(&info
, mdfd
, NULL
)) {
797 pr_err("unable to initialize sysfs\n");
801 if (st
->ss
->external
&& st
->container_devnm
[0]) {
804 /* When creating a member, we need to be careful
805 * to negotiate with mdmon properly.
806 * If it is already running, we cannot write to
807 * the devices and must ask it to do that part.
808 * If it isn't running, we write to the devices,
810 * We hold an exclusive open on the container
811 * device to make sure mdmon doesn't exit after
812 * we checked that it is running.
814 * For now, fail if it is already running.
816 container_fd
= open_dev_excl(st
->container_devnm
);
817 if (container_fd
< 0) {
818 pr_err("Cannot get exclusive open on container - weird.\n");
821 if (mdmon_running(st
->container_devnm
)) {
823 pr_err("reusing mdmon for %s.\n",
824 st
->container_devnm
);
825 st
->update_tail
= &st
->updates
;
829 rv
= set_array_info(mdfd
, st
, &info
);
831 pr_err("failed to set array info for %s: %s\n",
832 mddev
, strerror(errno
));
836 if (s
->bitmap_file
) {
839 st
->ss
->uuid_from_super(st
, uuid
);
840 if (CreateBitmap(s
->bitmap_file
, c
->force
, (char*)uuid
, s
->bitmap_chunk
,
841 c
->delay
, s
->write_behind
,
846 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
848 pr_err("weird: %s cannot be opened\n",
852 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
853 pr_err("Cannot set bitmap file for %s: %s\n",
854 mddev
, strerror(errno
));
859 infos
= xmalloc(sizeof(*infos
) * total_slots
);
860 enable_fds(total_slots
);
861 for (pass
= 1; pass
<= 2; pass
++) {
862 struct mddev_dev
*moved_disk
= NULL
; /* the disk that was moved out of the insert point */
864 for (dnum
= 0, raid_disk_num
= 0, dv
= devlist
; dv
;
865 dv
= (dv
->next
) ? (dv
->next
) : moved_disk
, dnum
++) {
867 struct mdinfo
*inf
= &infos
[dnum
];
869 if (dnum
>= total_slots
)
871 if (dnum
== insert_point
) {
876 if (strcasecmp(dv
->devname
, "missing") == 0) {
882 if (have_container
&& dnum
< info
.array
.raid_disks
- 1)
883 /* repeatedly use the container */
890 inf
->disk
.number
= dnum
;
891 inf
->disk
.raid_disk
= raid_disk_num
++;
893 if (dv
->disposition
== 'j') {
894 inf
->disk
.raid_disk
= MD_DISK_ROLE_JOURNAL
;
895 inf
->disk
.state
= (1<<MD_DISK_JOURNAL
);
897 } else if (inf
->disk
.raid_disk
< s
->raiddisks
)
898 inf
->disk
.state
= (1<<MD_DISK_ACTIVE
) |
903 if (dv
->writemostly
== FlagSet
)
904 inf
->disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
905 if (dv
->failfast
== FlagSet
)
906 inf
->disk
.state
|= (1<<MD_DISK_FAILFAST
);
911 if (st
->ss
->external
&&
912 st
->container_devnm
[0])
913 fd
= open(dv
->devname
, O_RDWR
);
915 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
);
918 pr_err("failed to open %s after earlier success - aborting\n",
922 if (!fstat_is_blkdev(fd
, dv
->devname
, &rdev
))
924 inf
->disk
.major
= major(rdev
);
925 inf
->disk
.minor
= minor(rdev
);
928 remove_partitions(fd
);
929 if (st
->ss
->add_to_super(st
, &inf
->disk
,
932 ioctl(mdfd
, STOP_ARRAY
, NULL
);
935 st
->ss
->getinfo_super(st
, inf
, NULL
);
936 safe_mode_delay
= inf
->safe_mode_delay
;
938 if (have_container
&& c
->verbose
> 0)
939 pr_err("Using %s for device %d\n",
940 map_dev(inf
->disk
.major
,
944 if (!have_container
) {
945 /* getinfo_super might have lost these ... */
946 inf
->disk
.major
= major(rdev
);
947 inf
->disk
.minor
= minor(rdev
);
953 rv
= add_disk(mdfd
, st
, &info
, inf
);
956 pr_err("ADD_NEW_DISK for %s failed: %s\n",
957 dv
->devname
, strerror(errno
));
958 if (errno
== EINVAL
&&
959 info
.array
.level
== 0) {
960 pr_err("Possibly your kernel doesn't support RAID0 layouts.\n");
961 pr_err("Either upgrade, or use --layout=dangerous\n");
967 if (!have_container
&&
968 dv
== moved_disk
&& dnum
!= insert_point
) break;
971 struct mdinfo info_new
;
972 struct map_ent
*me
= NULL
;
974 /* check to see if the uuid has changed due to these
975 * metadata changes, and if so update the member array
976 * and container uuid. Note ->write_init_super clears
977 * the subarray cursor such that ->getinfo_super once
978 * again returns container info.
980 st
->ss
->getinfo_super(st
, &info_new
, NULL
);
981 if (st
->ss
->external
&& s
->level
!= LEVEL_CONTAINER
&&
982 !same_uuid(info_new
.uuid
, info
.uuid
, 0)) {
983 map_update(&map
, fd2devnm(mdfd
),
984 info_new
.text_version
,
985 info_new
.uuid
, chosen_name
);
986 me
= map_by_devnm(&map
, st
->container_devnm
);
989 if (st
->ss
->write_init_super(st
)) {
990 st
->ss
->free_super(st
);
994 * Before activating the array, perform extra steps
995 * required to configure the internal write-intent
998 if (info_new
.consistency_policy
==
999 CONSISTENCY_POLICY_BITMAP
&&
1000 st
->ss
->set_bitmap
&&
1001 st
->ss
->set_bitmap(st
, &info
)) {
1002 st
->ss
->free_super(st
);
1006 /* update parent container uuid */
1008 char *path
= xstrdup(me
->path
);
1010 st
->ss
->getinfo_super(st
, &info_new
, NULL
);
1011 map_update(&map
, st
->container_devnm
,
1012 info_new
.text_version
,
1013 info_new
.uuid
, path
);
1017 flush_metadata_updates(st
);
1018 st
->ss
->free_super(st
);
1024 if (s
->level
== LEVEL_CONTAINER
) {
1025 /* No need to start. But we should signal udev to
1027 sysfs_uevent(&info
, "change");
1028 if (c
->verbose
>= 0)
1029 pr_err("container %s prepared.\n", mddev
);
1030 wait_for(chosen_name
, mdfd
);
1031 } else if (c
->runstop
== 1 || subdevs
>= s
->raiddisks
) {
1032 if (st
->ss
->external
) {
1036 case LEVEL_MULTIPATH
:
1038 err
= sysfs_set_str(&info
, NULL
, "array_state",
1045 err
= sysfs_set_str(&info
, NULL
, "array_state",
1049 sysfs_set_safemode(&info
, safe_mode_delay
);
1051 pr_err("failed to activate array.\n");
1052 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1055 } else if (c
->readonly
&&
1056 sysfs_attribute_available(
1057 &info
, NULL
, "array_state")) {
1058 if (sysfs_set_str(&info
, NULL
,
1059 "array_state", "readonly") < 0) {
1060 pr_err("Failed to start array: %s\n",
1062 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1066 /* param is not actually used */
1068 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
1069 pr_err("RUN_ARRAY failed: %s\n",
1071 if (errno
== 524 /* ENOTSUP */ &&
1072 info
.array
.level
== 0)
1073 cont_err("Please use --layout=original or --layout=alternate\n");
1074 if (info
.array
.chunk_size
& (info
.array
.chunk_size
-1)) {
1075 cont_err("Problem may be that chunk size is not a power of 2\n");
1077 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1080 /* if start_ro module parameter is set, array is
1081 * auto-read-only, which is bad as the resync won't
1082 * start. So lets make it read-write now.
1084 ioctl(mdfd
, RESTART_ARRAY_RW
, NULL
);
1086 if (c
->verbose
>= 0)
1087 pr_err("array %s started.\n", mddev
);
1088 if (st
->ss
->external
&& st
->container_devnm
[0]) {
1090 start_mdmon(st
->container_devnm
);
1092 ping_monitor(st
->container_devnm
);
1093 close(container_fd
);
1095 wait_for(chosen_name
, mdfd
);
1097 pr_err("not starting array - not enough devices.\n");
1101 sysfs_uevent(&info
, "change");
1108 map_remove(&map
, fd2devnm(mdfd
));