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>
32 #include <sys/signalfd.h>
35 #ifndef FALLOC_FL_ZERO_RANGE
36 #define FALLOC_FL_ZERO_RANGE 16
39 static int round_size_and_verify(unsigned long long *size
, int chunk
)
43 *size
&= ~(unsigned long long)(chunk
- 1);
45 pr_err("Size cannot be smaller than chunk.\n");
52 * default_layout() - Get default layout for level.
53 * @st: metadata requested, could be NULL.
54 * @level: raid level requested.
55 * @verbose: verbose level.
57 * Try to ask metadata handler first, otherwise use global defaults.
59 * Return: Layout or &UnSet, return value meaning depends of level used.
61 int default_layout(struct supertype
*st
, int level
, int verbose
)
64 mapping_t
*layout_map
= NULL
;
65 char *layout_name
= NULL
;
67 if (st
&& st
->ss
->default_geometry
)
68 st
->ss
->default_geometry(st
, &level
, &layout
, NULL
);
74 default: /* no layout */
78 /* Leave unset - metadata handlers choose default */
81 layout
= 0x102; /* near=2, far=1 */
86 layout_map
= r5layout
;
89 layout_map
= faultylayout
;
94 layout
= map_name(layout_map
, "default");
95 layout_name
= map_num_s(layout_map
, layout
);
97 if (layout_name
&& verbose
> 0)
98 pr_err("layout defaults to %s\n", layout_name
);
103 static pid_t
write_zeroes_fork(int fd
, struct shape
*s
, struct supertype
*st
,
104 struct mddev_dev
*dv
)
107 const unsigned long long req_size
= 1 << 30;
108 unsigned long long offset_bytes
, size_bytes
, sz
;
113 size_bytes
= KIB_TO_BYTES(s
->size
);
116 * If size_bytes is zero, this is a zoned raid array where
117 * each disk is of a different size and uses its full
118 * disk. Thus zero the entire disk.
120 if (!size_bytes
&& !get_dev_size(fd
, dv
->devname
, &size_bytes
))
123 if (dv
->data_offset
!= INVALID_SECTORS
)
124 offset_bytes
= SEC_TO_BYTES(dv
->data_offset
);
126 offset_bytes
= SEC_TO_BYTES(st
->data_offset
);
128 pr_info("zeroing data from %lld to %lld on: %s\n",
129 offset_bytes
, size_bytes
, dv
->devname
);
133 pr_err("Could not fork to zero disks: %s\n", strerror(errno
));
135 } else if (pid
!= 0) {
139 sigemptyset(&sigset
);
140 sigaddset(&sigset
, SIGINT
);
141 sigprocmask(SIG_UNBLOCK
, &sigset
, NULL
);
145 * Split requests to the kernel into 1GB chunks seeing the
146 * fallocate() call is not interruptible and blocking a
147 * ctrl-c for several minutes is not desirable.
149 * 1GB is chosen as a compromise: the user may still have
150 * to wait several seconds if they ctrl-c on devices that
151 * zero slowly, but will reduce the number of requests
152 * required and thus the overhead on devices that perform
159 if (fallocate(fd
, FALLOC_FL_ZERO_RANGE
| FALLOC_FL_KEEP_SIZE
,
161 pr_err("zeroing %s failed: %s\n", dv
->devname
,
174 static int wait_for_zero_forks(int *zero_pids
, int count
)
176 int wstatus
, ret
= 0, i
, sfd
, wait_count
= 0;
177 struct signalfd_siginfo fdsi
;
178 bool interrupted
= false;
183 for (i
= 0; i
< count
; i
++)
189 sigemptyset(&sigset
);
190 sigaddset(&sigset
, SIGINT
);
191 sigaddset(&sigset
, SIGCHLD
);
192 sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
194 sfd
= signalfd(-1, &sigset
, 0);
196 pr_err("Unable to create signalfd: %s\n", strerror(errno
));
201 s
= read(sfd
, &fdsi
, sizeof(fdsi
));
202 if (s
!= sizeof(fdsi
)) {
203 pr_err("Invalid signalfd read: %s\n", strerror(errno
));
208 if (fdsi
.ssi_signo
== SIGINT
) {
210 pr_info("Interrupting zeroing processes, please wait...\n");
212 } else if (fdsi
.ssi_signo
== SIGCHLD
) {
213 for (i
= 0; i
< count
; i
++) {
217 pid
= waitpid(zero_pids
[i
], &wstatus
, WNOHANG
);
222 if (!WIFEXITED(wstatus
) || WEXITSTATUS(wstatus
))
232 pr_err("zeroing interrupted!\n");
237 pr_err("zeroing failed!\n");
239 pr_info("zeroing finished\n");
244 static int add_disk_to_super(int mdfd
, struct shape
*s
, struct context
*c
,
245 struct supertype
*st
, struct mddev_dev
*dv
,
246 struct mdinfo
*info
, int have_container
, int major_num
,
252 if (dv
->disposition
== 'j') {
253 info
->disk
.raid_disk
= MD_DISK_ROLE_JOURNAL
;
254 info
->disk
.state
= (1<<MD_DISK_JOURNAL
);
255 } else if (info
->disk
.raid_disk
< s
->raiddisks
) {
256 info
->disk
.state
= (1<<MD_DISK_ACTIVE
) |
259 info
->disk
.state
= 0;
262 if (dv
->writemostly
== FlagSet
) {
263 if (major_num
== BITMAP_MAJOR_CLUSTERED
) {
264 pr_err("Can not set %s --write-mostly with a clustered bitmap\n",dv
->devname
);
267 info
->disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
272 if (dv
->failfast
== FlagSet
)
273 info
->disk
.state
|= (1<<MD_DISK_FAILFAST
);
275 if (have_container
) {
278 if (st
->ss
->external
&& st
->container_devnm
[0])
279 fd
= open(dv
->devname
, O_RDWR
);
281 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
);
284 pr_err("failed to open %s after earlier success - aborting\n",
288 if (!fstat_is_blkdev(fd
, dv
->devname
, &rdev
)) {
292 info
->disk
.major
= major(rdev
);
293 info
->disk
.minor
= minor(rdev
);
296 remove_partitions(fd
);
297 if (st
->ss
->add_to_super(st
, &info
->disk
, fd
, dv
->devname
,
299 ioctl(mdfd
, STOP_ARRAY
, NULL
);
303 st
->ss
->getinfo_super(st
, info
, NULL
);
305 if (fd
>= 0 && s
->write_zeroes
) {
306 *zero_pid
= write_zeroes_fork(fd
, s
, st
, dv
);
307 if (*zero_pid
<= 0) {
308 ioctl(mdfd
, STOP_ARRAY
, NULL
);
314 if (have_container
&& c
->verbose
> 0)
315 pr_err("Using %s for device %d\n",
316 map_dev(info
->disk
.major
, info
->disk
.minor
, 0),
319 if (!have_container
) {
320 /* getinfo_super might have lost these ... */
321 info
->disk
.major
= major(rdev
);
322 info
->disk
.minor
= minor(rdev
);
328 static int update_metadata(int mdfd
, struct shape
*s
, struct supertype
*st
,
329 struct map_ent
**map
, struct mdinfo
*info
,
332 struct mdinfo info_new
;
333 struct map_ent
*me
= NULL
;
335 /* check to see if the uuid has changed due to these
336 * metadata changes, and if so update the member array
337 * and container uuid. Note ->write_init_super clears
338 * the subarray cursor such that ->getinfo_super once
339 * again returns container info.
341 st
->ss
->getinfo_super(st
, &info_new
, NULL
);
342 if (st
->ss
->external
&& !is_container(s
->level
) &&
343 !same_uuid(info_new
.uuid
, info
->uuid
, 0)) {
344 map_update(map
, fd2devnm(mdfd
),
345 info_new
.text_version
,
346 info_new
.uuid
, chosen_name
);
347 me
= map_by_devnm(map
, st
->container_devnm
);
350 if (st
->ss
->write_init_super(st
)) {
351 st
->ss
->free_super(st
);
356 * Before activating the array, perform extra steps
357 * required to configure the internal write-intent
360 if (info_new
.consistency_policy
== CONSISTENCY_POLICY_BITMAP
&&
361 st
->ss
->set_bitmap
&& st
->ss
->set_bitmap(st
, info
)) {
362 st
->ss
->free_super(st
);
366 /* update parent container uuid */
368 char *path
= xstrdup(me
->path
);
370 st
->ss
->getinfo_super(st
, &info_new
, NULL
);
371 map_update(map
, st
->container_devnm
, info_new
.text_version
,
372 info_new
.uuid
, path
);
376 flush_metadata_updates(st
);
377 st
->ss
->free_super(st
);
382 static int add_disks(int mdfd
, struct mdinfo
*info
, struct shape
*s
,
383 struct context
*c
, struct supertype
*st
,
384 struct map_ent
**map
, struct mddev_dev
*devlist
,
385 int total_slots
, int have_container
, int insert_point
,
386 int major_num
, char *chosen_name
)
388 struct mddev_dev
*moved_disk
= NULL
;
389 int pass
, raid_disk_num
, dnum
;
390 int zero_pids
[total_slots
];
391 struct mddev_dev
*dv
;
392 struct mdinfo
*infos
;
393 sigset_t sigset
, orig_sigset
;
397 * Block SIGINT so the main thread will always wait for the
398 * zeroing processes when being interrupted. Otherwise the
399 * zeroing processes will finish their work in the background
400 * keeping the disk busy.
402 sigemptyset(&sigset
);
403 sigaddset(&sigset
, SIGINT
);
404 sigaddset(&sigset
, SIGCHLD
);
405 sigprocmask(SIG_BLOCK
, &sigset
, &orig_sigset
);
406 memset(zero_pids
, 0, sizeof(zero_pids
));
408 infos
= xmalloc(sizeof(*infos
) * total_slots
);
409 enable_fds(total_slots
);
410 for (pass
= 1; pass
<= 2; pass
++) {
411 for (dnum
= 0, raid_disk_num
= 0, dv
= devlist
; dv
;
412 dv
= (dv
->next
) ? (dv
->next
) : moved_disk
, dnum
++) {
413 if (dnum
>= total_slots
)
415 if (dnum
== insert_point
) {
420 if (strcasecmp(dv
->devname
, "missing") == 0) {
426 if (have_container
&& dnum
< total_slots
- 1)
427 /* repeatedly use the container */
433 infos
[dnum
].disk
.number
= dnum
;
434 infos
[dnum
].disk
.raid_disk
= raid_disk_num
++;
436 if (dv
->disposition
== 'j')
439 ret
= add_disk_to_super(mdfd
, s
, c
, st
, dv
,
440 &infos
[dnum
], have_container
,
441 major_num
, &zero_pids
[dnum
]);
447 infos
[dnum
].errors
= 0;
449 ret
= add_disk(mdfd
, st
, info
, &infos
[dnum
]);
451 pr_err("ADD_NEW_DISK for %s failed: %s\n",
452 dv
->devname
, strerror(errno
));
453 if (errno
== EINVAL
&&
454 info
->array
.level
== 0) {
455 pr_err("Possibly your kernel doesn't support RAID0 layouts.\n");
456 pr_err("Either upgrade, or use --layout=dangerous\n");
462 if (!have_container
&&
463 dv
== moved_disk
&& dnum
!= insert_point
) break;
467 ret
= wait_for_zero_forks(zero_pids
, total_slots
);
471 ret
= update_metadata(mdfd
, s
, st
, map
, info
,
480 wait_for_zero_forks(zero_pids
, total_slots
);
482 sigprocmask(SIG_SETMASK
, &orig_sigset
, NULL
);
486 int Create(struct supertype
*st
, struct mddev_ident
*ident
, int subdevs
,
487 struct mddev_dev
*devlist
, struct shape
*s
, struct context
*c
)
490 * Create a new raid array.
492 * First check that necessary details are available
493 * (i.e. level, raid-disks)
495 * Then check each disk to see what might be on it
496 * and report anything interesting.
498 * If anything looks odd, and runstop not set,
501 * SET_ARRAY_INFO and ADD_NEW_DISK, and
502 * if runstop==run, or raiddisks disks were used,
506 unsigned long long minsize
= 0, maxsize
= 0;
507 dev_policy_t
*custom_pols
= NULL
;
508 char *mindisc
= NULL
;
509 char *maxdisc
= NULL
;
510 char *name
= ident
->name
;
511 int *uuid
= ident
->uuid_set
== 1 ? ident
->uuid
: NULL
;
513 struct mddev_dev
*dv
;
515 int fail
= 0, warn
= 0;
516 int first_missing
= subdevs
* 2;
517 int second_missing
= subdevs
* 2;
518 int missing_disks
= 0;
519 int insert_point
= subdevs
* 2; /* where to insert a missing drive */
522 int have_container
= 0;
523 int container_fd
= -1;
525 unsigned long long bitmapsize
;
528 int do_default_layout
= 0;
529 int do_default_chunk
= 0;
530 char chosen_name
[1024];
531 struct map_ent
*map
= NULL
;
532 unsigned long long newsize
;
533 mdu_array_info_t inf
;
534 int major_num
= BITMAP_MAJOR_HI
;
536 if (s
->btype
== BitmapCluster
) {
537 major_num
= BITMAP_MAJOR_CLUSTERED
;
539 pr_err("At least 2 nodes are needed for cluster-md\n");
544 memset(&info
, 0, sizeof(info
));
545 if (s
->level
== UnSet
&& st
&& st
->ss
->default_geometry
)
546 st
->ss
->default_geometry(st
, &s
->level
, NULL
, NULL
);
547 if (s
->level
== UnSet
) {
548 pr_err("a RAID level is needed to create an array.\n");
551 if (s
->raiddisks
< 4 && s
->level
== 6) {
552 pr_err("at least 4 raid-devices needed for level 6\n");
555 if (s
->raiddisks
> 256 && s
->level
== 6) {
556 pr_err("no more than 256 raid-devices supported for level 6\n");
559 if (s
->raiddisks
< 2 && s
->level
>= 4) {
560 pr_err("at least 2 raid-devices needed for level %d\n", s
->level
);
563 if (s
->level
<= 0 && s
->sparedisks
) {
564 pr_err("This level does not support spare devices\n");
568 if (subdevs
== 1 && strcmp(devlist
->devname
, "missing") != 0) {
569 /* If given a single device, it might be a container, and we can
570 * extract a device list from there
574 memset(&inf
, 0, sizeof(inf
));
575 fd
= open(devlist
->devname
, O_RDONLY
);
577 md_get_array_info(fd
, &inf
) == 0 && inf
.raid_disks
== 0) {
578 /* yep, looks like a container */
580 rv
= st
->ss
->load_container(st
, fd
,
585 st
= super_by_fd(fd
, NULL
);
586 if (st
&& !(rv
= st
->ss
->
587 load_container(st
, fd
,
593 if (have_container
) {
594 subdevs
= s
->raiddisks
;
595 first_missing
= subdevs
* 2;
596 second_missing
= subdevs
* 2;
597 insert_point
= subdevs
* 2;
599 if (mddev_test_and_add_drive_policies(st
, &custom_pols
, fd
, 1))
606 if (st
&& st
->ss
->external
&& s
->sparedisks
) {
607 pr_err("This metadata type does not support spare disks at create time\n");
610 if (subdevs
> s
->raiddisks
+s
->sparedisks
+s
->journaldisks
) {
611 pr_err("You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, s
->raiddisks
+s
->sparedisks
);
614 if (!have_container
&& subdevs
< s
->raiddisks
+s
->sparedisks
+s
->journaldisks
) {
615 pr_err("You haven't given enough devices (real or missing) to create this array\n");
618 if (s
->btype
!= BitmapNone
&& s
->level
<= 0) {
619 pr_err("bitmaps not meaningful with level %s\n",
620 map_num(pers
, s
->level
)?:"given");
624 /* now set some defaults */
626 if (s
->layout
== UnSet
) {
627 do_default_layout
= 1;
628 s
->layout
= default_layout(st
, s
->level
, c
->verbose
);
632 /* check layout fits in array*/
633 if ((s
->layout
&255) * ((s
->layout
>>8)&255) > s
->raiddisks
) {
634 pr_err("that layout requires at least %d devices\n",
635 (s
->layout
&255) * ((s
->layout
>>8)&255));
645 if (s
->chunk
== 0 || s
->chunk
== UnSet
) {
647 do_default_chunk
= 1;
648 /* chunk will be set later */
652 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
656 case LEVEL_MULTIPATH
:
657 case LEVEL_CONTAINER
:
659 pr_err("specifying chunk size is forbidden for this level\n");
664 pr_err("unknown level %d\n", s
->level
);
668 if (s
->size
== MAX_SIZE
)
669 /* use '0' to mean 'max' now... */
671 if (s
->size
&& s
->chunk
&& s
->chunk
!= UnSet
)
672 if (round_size_and_verify(&s
->size
, s
->chunk
))
675 newsize
= s
->size
* 2;
676 if (st
&& ! st
->ss
->validate_geometry(st
, s
->level
, s
->layout
, s
->raiddisks
,
677 &s
->chunk
, s
->size
*2,
678 s
->data_offset
, NULL
,
679 &newsize
, s
->consistency_policy
,
683 if (s
->chunk
&& s
->chunk
!= UnSet
) {
684 newsize
&= ~(unsigned long long)(s
->chunk
*2 - 1);
685 if (do_default_chunk
) {
686 /* default chunk was just set */
688 pr_err("chunk size defaults to %dK\n", s
->chunk
);
689 if (round_size_and_verify(&s
->size
, s
->chunk
))
691 do_default_chunk
= 0;
696 s
->size
= newsize
/ 2;
698 /* If this is ever reshaped to RAID5, we will
699 * need a chunksize. So round it off a bit
700 * now just to be safe
702 s
->size
&= ~(64ULL-1);
704 if (s
->size
&& c
->verbose
> 0)
705 pr_err("setting size to %lluK\n", s
->size
);
708 /* now look at the subdevs */
709 info
.array
.active_disks
= 0;
710 info
.array
.working_disks
= 0;
712 for (dv
= devlist
; dv
; dv
= dv
->next
)
713 if (s
->data_offset
== VARIABLE_OFFSET
)
714 dv
->data_offset
= INVALID_SECTORS
;
716 dv
->data_offset
= s
->data_offset
;
718 for (dv
=devlist
; dv
&& !have_container
; dv
=dv
->next
, dnum
++) {
719 char *dname
= dv
->devname
;
720 unsigned long long freesize
;
724 if (strcasecmp(dname
, "missing") == 0) {
725 if (first_missing
> dnum
)
726 first_missing
= dnum
;
727 if (second_missing
> dnum
&& dnum
> first_missing
)
728 second_missing
= dnum
;
732 if (s
->data_offset
== VARIABLE_OFFSET
) {
733 doff
= strchr(dname
, ':');
736 dv
->data_offset
= parse_size(doff
);
738 dv
->data_offset
= INVALID_SECTORS
;
740 dv
->data_offset
= s
->data_offset
;
742 dfd
= open(dname
, O_RDONLY
);
744 pr_err("cannot open %s: %s\n",
745 dname
, strerror(errno
));
748 if (!fstat_is_blkdev(dfd
, dname
, NULL
)) {
753 info
.array
.working_disks
++;
754 if (dnum
< s
->raiddisks
&& dv
->disposition
!= 'j')
755 info
.array
.active_disks
++;
757 struct createinfo
*ci
= conf_get_create_info();
762 /* Need to choose a default metadata, which is different
763 * depending on geometry of array.
766 char *name
= "default";
767 for(i
= 0; !st
&& superlist
[i
]; i
++) {
768 st
= superlist
[i
]->match_metadata_desc(name
);
771 if (do_default_layout
)
772 s
->layout
= default_layout(st
, s
->level
, c
->verbose
);
773 switch (st
->ss
->validate_geometry(
774 st
, s
->level
, s
->layout
, s
->raiddisks
,
775 &s
->chunk
, s
->size
*2,
776 dv
->data_offset
, dname
,
777 &freesize
, s
->consistency_policy
,
779 case -1: /* Not valid, message printed, and not
780 * worth checking any further */
783 case 0: /* Geometry not valid */
786 s
->chunk
= do_default_chunk
? UnSet
: s
->chunk
;
788 case 1: /* All happy */
794 int dfd
= open(dname
, O_RDONLY
|O_EXCL
);
796 pr_err("cannot open %s: %s\n",
797 dname
, strerror(errno
));
800 pr_err("device %s not suitable for any style of array\n",
804 if (st
->ss
!= &super0
||
805 st
->minor_version
!= 90)
808 if (do_default_layout
)
809 s
->layout
= default_layout(st
, s
->level
, 0);
810 if (!st
->ss
->validate_geometry(st
, s
->level
, s
->layout
,
812 &s
->chunk
, s
->size
*2,
815 s
->consistency_policy
,
818 pr_err("%s is not suitable for this array.\n",
825 if (drive_test_and_add_policies(st
, &custom_pols
, dfd
, 1))
830 if (dv
->disposition
== 'j')
831 goto skip_size_check
; /* skip write journal for size check */
833 freesize
/= 2; /* convert to K */
834 if (s
->chunk
&& s
->chunk
!= UnSet
) {
835 /* round to chunk size */
836 freesize
= freesize
& ~(s
->chunk
-1);
837 if (do_default_chunk
) {
838 /* default chunk was just set */
840 pr_err("chunk size defaults to %dK\n", s
->chunk
);
841 if (round_size_and_verify(&s
->size
, s
->chunk
))
843 do_default_chunk
= 0;
847 pr_err("no free space left on %s\n", dname
);
852 if (s
->size
&& freesize
< s
->size
) {
853 pr_err("%s is smaller than given size. %lluK < %lluK + metadata\n",
854 dname
, freesize
, s
->size
);
858 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
862 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
867 if (c
->runstop
!= 1 || c
->verbose
>= 0) {
868 int fd
= open(dname
, O_RDONLY
);
870 pr_err("Cannot open %s: %s\n",
871 dname
, strerror(errno
));
875 warn
|= check_ext2(fd
, dname
);
876 warn
|= check_reiser(fd
, dname
);
877 warn
|= check_raid(fd
, dname
);
878 if (strcmp(st
->ss
->name
, "1.x") == 0 &&
879 st
->minor_version
>= 1)
880 /* metadata at front */
881 warn
|= check_partitions(fd
, dname
, 0, 0);
882 else if (s
->level
== 1 || is_container(s
->level
) ||
883 (s
->level
== 0 && s
->raiddisks
== 1))
884 /* partitions could be meaningful */
885 warn
|= check_partitions(fd
, dname
, freesize
*2, s
->size
*2);
887 /* partitions cannot be meaningful */
888 warn
|= check_partitions(fd
, dname
, 0, 0);
889 if (strcmp(st
->ss
->name
, "1.x") == 0 &&
890 st
->minor_version
>= 1 &&
893 (warn
& 1024) == 0) {
895 pr_err("Note: this array has metadata at the start and\n"
896 " may not be suitable as a boot device. If you plan to\n"
897 " store '/boot' on this device please ensure that\n"
898 " your boot-loader understands md/v1.x metadata, or use\n"
899 " --metadata=0.90\n");
905 if (missing_disks
== dnum
&& !have_container
) {
906 pr_err("Subdevs can't be all missing\n");
909 if (s
->raiddisks
+ s
->sparedisks
> st
->max_devs
) {
910 pr_err("Too many devices: %s metadata only supports %d\n",
911 st
->ss
->name
, st
->max_devs
);
915 info
.array
.working_disks
= s
->raiddisks
;
917 pr_err("create aborted\n");
921 if (mindisc
== NULL
&& !have_container
) {
922 pr_err("no size and no drives given - aborting create.\n");
925 if (s
->level
> 0 || s
->level
== LEVEL_MULTIPATH
||
926 s
->level
== LEVEL_FAULTY
|| st
->ss
->external
) {
927 /* size is meaningful */
928 if (!st
->ss
->validate_geometry(st
, s
->level
, s
->layout
,
930 &s
->chunk
, minsize
*2,
933 s
->consistency_policy
, 0)) {
934 pr_err("devices too large for RAID level %d\n", s
->level
);
939 /* If this is ever reshaped to RAID5, we will
940 * need a chunksize. So round it off a bit
941 * now just to be safe
943 s
->size
&= ~(64ULL-1);
945 pr_err("size set to %lluK\n", s
->size
);
949 if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
950 !st
->ss
->write_init_ppl
) {
951 pr_err("%s metadata does not support PPL\n", st
->ss
->name
);
955 if (st
->ss
== &super_imsm
&& s
->level
== 10 && s
->raiddisks
> 4) {
956 /* Print no matter runstop was specifed */
957 pr_err("Warning! VROC UEFI driver does not support RAID10 in requested layout.\n");
958 pr_err("Array won't be suitable as boot device.\n");
962 if (!have_container
&& s
->level
> 0 && ((maxsize
-s
->size
)*100 > maxsize
)) {
963 if (c
->runstop
!= 1 || c
->verbose
>= 0)
964 pr_err("largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
969 if (st
->ss
->detail_platform
&& st
->ss
->detail_platform(0, 1, NULL
) != 0) {
970 if (c
->runstop
!= 1 || c
->verbose
>= 0)
971 pr_err("%s unable to enumerate platform support\n"
972 " array may not be compatible with hardware/firmware\n",
976 st
->nodes
= c
->nodes
;
977 st
->cluster_name
= c
->homecluster
;
980 if (c
->runstop
!= 1) {
981 if (!ask("Continue creating array")) {
982 pr_err("create aborted.\n");
987 pr_err("creation continuing despite oddities due to --run\n");
991 /* If this is raid4/5, we want to configure the last active slot
992 * as missing, so that a reconstruct happens (faster than re-parity)
993 * FIX: Can we do this for raid6 as well?
995 if (st
->ss
->external
== 0 && s
->assume_clean
== 0 &&
996 c
->force
== 0 && first_missing
>= s
->raiddisks
) {
1000 insert_point
= s
->raiddisks
-1;
1002 info
.array
.active_disks
--;
1009 /* For raid6, if creating with 1 missing drive, make a good drive
1010 * into a spare, else the create will fail
1012 if (s
->assume_clean
== 0 && c
->force
== 0 && first_missing
< s
->raiddisks
&&
1013 st
->ss
->external
== 0 &&
1014 second_missing
>= s
->raiddisks
&& s
->level
== 6) {
1015 insert_point
= s
->raiddisks
- 1;
1016 if (insert_point
== first_missing
)
1019 info
.array
.active_disks
--;
1023 if (s
->level
<= 0 && first_missing
< subdevs
* 2) {
1024 pr_err("This level does not support missing devices\n");
1028 /* We need to create the device */
1030 mdfd
= create_mddev(ident
->devname
, ident
->name
, LOCAL
, chosen_name
, 1);
1035 /* verify if chosen_name is not in use,
1036 * it could be in conflict with already existing device
1037 * e.g. container, array
1039 if (strncmp(chosen_name
, DEV_MD_DIR
, DEV_MD_DIR_LEN
) == 0 &&
1040 map_by_name(&map
, chosen_name
+ DEV_MD_DIR_LEN
)) {
1041 pr_err("Array name %s is in use already.\n", chosen_name
);
1048 memset(&inf
, 0, sizeof(inf
));
1049 md_get_array_info(mdfd
, &inf
);
1050 if (inf
.working_disks
!= 0) {
1051 pr_err("another array by this name is already running.\n");
1055 /* Ok, lets try some ioctls */
1057 info
.array
.level
= s
->level
;
1058 info
.array
.size
= s
->size
;
1059 info
.array
.raid_disks
= s
->raiddisks
;
1060 /* The kernel should *know* what md_minor we are dealing
1061 * with, but it chooses to trust me instead. Sigh
1063 info
.array
.md_minor
= 0;
1064 if (fstat_is_blkdev(mdfd
, chosen_name
, &rdev
))
1065 info
.array
.md_minor
= minor(rdev
);
1066 info
.array
.not_persistent
= 0;
1068 if (((s
->level
== 4 || s
->level
== 5) &&
1069 (insert_point
< s
->raiddisks
|| first_missing
< s
->raiddisks
)) ||
1070 (s
->level
== 6 && (insert_point
< s
->raiddisks
||
1071 second_missing
< s
->raiddisks
)) ||
1072 (s
->level
<= 0) || s
->assume_clean
) {
1073 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
1074 info
.resync_start
= MaxSector
;
1076 info
.array
.state
= 0; /* not clean, but no errors */
1077 info
.resync_start
= 0;
1079 if (s
->level
== 10) {
1080 /* for raid10, the bitmap size is the capacity of the array,
1081 * which is array.size * raid_disks / ncopies;
1082 * .. but convert to sectors.
1084 int ncopies
= ((s
->layout
>>8) & 255) * (s
->layout
& 255);
1085 bitmapsize
= s
->size
* s
->raiddisks
/ ncopies
* 2;
1086 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, s->size, s->raiddisks, ncopies);*/
1088 bitmapsize
= s
->size
* 2;
1090 /* There is lots of redundancy in these disk counts,
1091 * raid_disks is the most meaningful value
1092 * it describes the geometry of the array
1094 * nr_disks is total number of used slots.
1095 * it should be raid_disks+spare_disks
1096 * spare_disks is the number of extra disks present
1098 * active_disks is the number of working disks in
1099 * active slots. (With raid_disks)
1100 * working_disks is the total number of working disks,
1102 * failed_disks is the number of disks marked failed
1104 * Ideally, the kernel would keep these (except raid_disks)
1105 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
1106 * So for now, we assume that all raid and spare
1107 * devices will be given.
1109 info
.array
.spare_disks
=s
->sparedisks
;
1110 info
.array
.failed_disks
=missing_disks
;
1111 info
.array
.nr_disks
= info
.array
.working_disks
1112 + info
.array
.failed_disks
;
1113 info
.array
.layout
= s
->layout
;
1114 info
.array
.chunk_size
= s
->chunk
*1024;
1117 /* base name on devname */
1120 * /dev/md_foo -> foo
1123 * /dev/md/home -> home
1124 * /dev/mdhome -> home
1126 /* FIXME compare this with rules in create_mddev */
1127 name
= strrchr(chosen_name
, '/');
1131 if (strncmp(name
, "md_", 3) == 0 &&
1132 strlen(name
) > 3 && (name
- chosen_name
) == 5 /* /dev/ */)
1134 else if (strncmp(name
, "md", 2) == 0 &&
1135 strlen(name
) > 2 && isdigit(name
[2]) &&
1136 (name
- chosen_name
) == 5 /* /dev/ */)
1140 if (!st
->ss
->init_super(st
, &info
.array
, s
, name
, c
->homehost
, uuid
,
1144 total_slots
= info
.array
.nr_disks
;
1145 st
->ss
->getinfo_super(st
, &info
, NULL
);
1146 if (sysfs_init(&info
, mdfd
, NULL
)) {
1147 pr_err("unable to initialize sysfs\n");
1152 if (is_subarray(info
.text_version
)) {
1153 char devnm
[MD_NAME_MAX
];
1156 sysfs_get_container_devnm(&info
, devnm
);
1158 mdi
= sysfs_read(-1, devnm
, GET_VERSION
| GET_DEVS
);
1160 pr_err("Cannot open sysfs for container %s\n", devnm
);
1164 if (sysfs_test_and_add_drive_policies(st
, &custom_pols
, mdi
, 1))
1167 if (c
->verbose
>= 0)
1168 pr_info("Creating array inside %s container /dev/%s\n",
1169 mdi
->text_version
, devnm
);
1172 } else if (c
->verbose
>= 0) {
1173 pr_info("Defaulting to version %s metadata\n", info
.text_version
);
1177 map_update(&map
, fd2devnm(mdfd
), info
.text_version
,
1178 info
.uuid
, chosen_name
);
1179 /* Keep map locked until devices have been added to array
1180 * to stop another mdadm from finding and using those devices.
1183 if (s
->btype
== BitmapInternal
|| s
->btype
== BitmapCluster
) {
1184 if (!st
->ss
->add_internal_bitmap
) {
1185 pr_err("internal bitmaps not supported with %s metadata\n",
1189 if (st
->ss
->add_internal_bitmap(st
, &s
->bitmap_chunk
,
1190 c
->delay
, s
->write_behind
,
1191 bitmapsize
, 1, major_num
)) {
1192 pr_err("Given bitmap chunk size not supported.\n");
1197 if (sysfs_init(&info
, mdfd
, NULL
)) {
1198 pr_err("unable to initialize sysfs\n");
1202 if (st
->ss
->external
&& st
->container_devnm
[0]) {
1205 /* When creating a member, we need to be careful
1206 * to negotiate with mdmon properly.
1207 * If it is already running, we cannot write to
1208 * the devices and must ask it to do that part.
1209 * If it isn't running, we write to the devices,
1210 * and then start it.
1211 * We hold an exclusive open on the container
1212 * device to make sure mdmon doesn't exit after
1213 * we checked that it is running.
1215 * For now, fail if it is already running.
1217 container_fd
= open_dev_excl(st
->container_devnm
);
1218 if (container_fd
< 0) {
1219 pr_err("Cannot get exclusive open on container - weird.\n");
1222 if (mdmon_running(st
->container_devnm
)) {
1224 pr_err("reusing mdmon for %s.\n",
1225 st
->container_devnm
);
1226 st
->update_tail
= &st
->updates
;
1230 rv
= set_array_info(mdfd
, st
, &info
);
1232 pr_err("failed to set array info for %s: %s\n", chosen_name
, strerror(errno
));
1236 if (add_disks(mdfd
, &info
, s
, c
, st
, &map
, devlist
, total_slots
,
1237 have_container
, insert_point
, major_num
, chosen_name
))
1242 if (is_container(s
->level
)) {
1243 /* No need to start. But we should signal udev to
1245 sysfs_uevent(&info
, "change");
1246 if (c
->verbose
>= 0)
1247 pr_err("container %s prepared.\n", chosen_name
);
1248 wait_for(chosen_name
, mdfd
);
1249 } else if (c
->runstop
== 1 || subdevs
>= s
->raiddisks
) {
1250 if (st
->ss
->external
) {
1254 case LEVEL_MULTIPATH
:
1256 err
= sysfs_set_str(&info
, NULL
, "array_state",
1263 err
= sysfs_set_str(&info
, NULL
, "array_state",
1267 sysfs_set_safemode(&info
, info
.safe_mode_delay
);
1269 pr_err("failed to activate array.\n");
1270 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1273 } else if (c
->readonly
&&
1274 sysfs_attribute_available(
1275 &info
, NULL
, "array_state")) {
1276 if (sysfs_set_str(&info
, NULL
,
1277 "array_state", "readonly") < 0) {
1278 pr_err("Failed to start array: %s\n",
1280 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1284 /* param is not actually used */
1286 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
1287 pr_err("RUN_ARRAY failed: %s\n",
1289 if (errno
== 524 /* ENOTSUP */ &&
1290 info
.array
.level
== 0)
1291 cont_err("Please use --layout=original or --layout=alternate\n");
1292 if (info
.array
.chunk_size
& (info
.array
.chunk_size
-1)) {
1293 cont_err("Problem may be that chunk size is not a power of 2\n");
1295 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1298 /* if start_ro module parameter is set, array is
1299 * auto-read-only, which is bad as the resync won't
1300 * start. So lets make it read-write now.
1302 ioctl(mdfd
, RESTART_ARRAY_RW
, NULL
);
1304 if (c
->verbose
>= 0)
1305 pr_info("array %s started.\n", chosen_name
);
1306 if (st
->ss
->external
&& st
->container_devnm
[0]) {
1308 start_mdmon(st
->container_devnm
);
1309 if (wait_for_mdmon_control_socket(st
->container_devnm
) != MDADM_STATUS_SUCCESS
)
1312 ping_monitor(st
->container_devnm
);
1313 close(container_fd
);
1315 wait_for(chosen_name
, mdfd
);
1317 pr_err("not starting array - not enough devices.\n");
1321 sysfs_uevent(&info
, "change");
1322 dev_policy_free(custom_pols
);
1330 map_remove(&map
, fd2devnm(mdfd
));
1334 close_fd(&container_fd
);
1336 dev_policy_free(custom_pols
);