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 static int round_size_and_verify(unsigned long long *size
, int chunk
)
39 *size
&= ~(unsigned long long)(chunk
- 1);
41 pr_err("Size cannot be smaller than chunk.\n");
48 * default_layout() - Get default layout for level.
49 * @st: metadata requested, could be NULL.
50 * @level: raid level requested.
51 * @verbose: verbose level.
53 * Try to ask metadata handler first, otherwise use global defaults.
55 * Return: Layout or &UnSet, return value meaning depends of level used.
57 int default_layout(struct supertype
*st
, int level
, int verbose
)
60 mapping_t
*layout_map
= NULL
;
61 char *layout_name
= NULL
;
63 if (st
&& st
->ss
->default_geometry
)
64 st
->ss
->default_geometry(st
, &level
, &layout
, NULL
);
70 default: /* no layout */
74 layout
= RAID0_ORIG_LAYOUT
;
77 layout
= 0x102; /* near=2, far=1 */
82 layout_map
= r5layout
;
85 layout_map
= faultylayout
;
90 layout
= map_name(layout_map
, "default");
91 layout_name
= map_num_s(layout_map
, layout
);
93 if (layout_name
&& verbose
> 0)
94 pr_err("layout defaults to %s\n", layout_name
);
99 static pid_t
write_zeroes_fork(int fd
, struct shape
*s
, struct supertype
*st
,
100 struct mddev_dev
*dv
)
103 const unsigned long long req_size
= 1 << 30;
104 unsigned long long offset_bytes
, size_bytes
, sz
;
109 size_bytes
= KIB_TO_BYTES(s
->size
);
112 * If size_bytes is zero, this is a zoned raid array where
113 * each disk is of a different size and uses its full
114 * disk. Thus zero the entire disk.
116 if (!size_bytes
&& !get_dev_size(fd
, dv
->devname
, &size_bytes
))
119 if (dv
->data_offset
!= INVALID_SECTORS
)
120 offset_bytes
= SEC_TO_BYTES(dv
->data_offset
);
122 offset_bytes
= SEC_TO_BYTES(st
->data_offset
);
124 pr_info("zeroing data from %lld to %lld on: %s\n",
125 offset_bytes
, size_bytes
, dv
->devname
);
129 pr_err("Could not fork to zero disks: %s\n", strerror(errno
));
131 } else if (pid
!= 0) {
135 sigemptyset(&sigset
);
136 sigaddset(&sigset
, SIGINT
);
137 sigprocmask(SIG_UNBLOCK
, &sigset
, NULL
);
141 * Split requests to the kernel into 1GB chunks seeing the
142 * fallocate() call is not interruptible and blocking a
143 * ctrl-c for several minutes is not desirable.
145 * 1GB is chosen as a compromise: the user may still have
146 * to wait several seconds if they ctrl-c on devices that
147 * zero slowly, but will reduce the number of requests
148 * required and thus the overhead on devices that perform
155 if (fallocate(fd
, FALLOC_FL_ZERO_RANGE
| FALLOC_FL_KEEP_SIZE
,
157 pr_err("zeroing %s failed: %s\n", dv
->devname
,
170 static int wait_for_zero_forks(int *zero_pids
, int count
)
172 int wstatus
, ret
= 0, i
, sfd
, wait_count
= 0;
173 struct signalfd_siginfo fdsi
;
174 bool interrupted
= false;
178 for (i
= 0; i
< count
; i
++)
184 sigemptyset(&sigset
);
185 sigaddset(&sigset
, SIGINT
);
186 sigaddset(&sigset
, SIGCHLD
);
187 sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
189 sfd
= signalfd(-1, &sigset
, 0);
191 pr_err("Unable to create signalfd: %s\n", strerror(errno
));
196 s
= read(sfd
, &fdsi
, sizeof(fdsi
));
197 if (s
!= sizeof(fdsi
)) {
198 pr_err("Invalid signalfd read: %s\n", strerror(errno
));
203 if (fdsi
.ssi_signo
== SIGINT
) {
205 pr_info("Interrupting zeroing processes, please wait...\n");
207 } else if (fdsi
.ssi_signo
== SIGCHLD
) {
215 for (i
= 0; i
< count
; i
++) {
219 waitpid(zero_pids
[i
], &wstatus
, 0);
221 if (!WIFEXITED(wstatus
) || WEXITSTATUS(wstatus
))
226 pr_err("zeroing interrupted!\n");
231 pr_err("zeroing failed!\n");
233 pr_info("zeroing finished\n");
238 static int add_disk_to_super(int mdfd
, struct shape
*s
, struct context
*c
,
239 struct supertype
*st
, struct mddev_dev
*dv
,
240 struct mdinfo
*info
, int have_container
, int major_num
,
246 if (dv
->disposition
== 'j') {
247 info
->disk
.raid_disk
= MD_DISK_ROLE_JOURNAL
;
248 info
->disk
.state
= (1<<MD_DISK_JOURNAL
);
249 } else if (info
->disk
.raid_disk
< s
->raiddisks
) {
250 info
->disk
.state
= (1<<MD_DISK_ACTIVE
) |
253 info
->disk
.state
= 0;
256 if (dv
->writemostly
== FlagSet
) {
257 if (major_num
== BITMAP_MAJOR_CLUSTERED
) {
258 pr_err("Can not set %s --write-mostly with a clustered bitmap\n",dv
->devname
);
261 info
->disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
266 if (dv
->failfast
== FlagSet
)
267 info
->disk
.state
|= (1<<MD_DISK_FAILFAST
);
269 if (have_container
) {
272 if (st
->ss
->external
&& st
->container_devnm
[0])
273 fd
= open(dv
->devname
, O_RDWR
);
275 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
);
278 pr_err("failed to open %s after earlier success - aborting\n",
282 if (!fstat_is_blkdev(fd
, dv
->devname
, &rdev
)) {
286 info
->disk
.major
= major(rdev
);
287 info
->disk
.minor
= minor(rdev
);
290 remove_partitions(fd
);
291 if (st
->ss
->add_to_super(st
, &info
->disk
, fd
, dv
->devname
,
293 ioctl(mdfd
, STOP_ARRAY
, NULL
);
297 st
->ss
->getinfo_super(st
, info
, NULL
);
299 if (fd
>= 0 && s
->write_zeroes
) {
300 *zero_pid
= write_zeroes_fork(fd
, s
, st
, dv
);
301 if (*zero_pid
<= 0) {
302 ioctl(mdfd
, STOP_ARRAY
, NULL
);
308 if (have_container
&& c
->verbose
> 0)
309 pr_err("Using %s for device %d\n",
310 map_dev(info
->disk
.major
, info
->disk
.minor
, 0),
313 if (!have_container
) {
314 /* getinfo_super might have lost these ... */
315 info
->disk
.major
= major(rdev
);
316 info
->disk
.minor
= minor(rdev
);
322 static int update_metadata(int mdfd
, struct shape
*s
, struct supertype
*st
,
323 struct map_ent
**map
, struct mdinfo
*info
,
326 struct mdinfo info_new
;
327 struct map_ent
*me
= NULL
;
329 /* check to see if the uuid has changed due to these
330 * metadata changes, and if so update the member array
331 * and container uuid. Note ->write_init_super clears
332 * the subarray cursor such that ->getinfo_super once
333 * again returns container info.
335 st
->ss
->getinfo_super(st
, &info_new
, NULL
);
336 if (st
->ss
->external
&& !is_container(s
->level
) &&
337 !same_uuid(info_new
.uuid
, info
->uuid
, 0)) {
338 map_update(map
, fd2devnm(mdfd
),
339 info_new
.text_version
,
340 info_new
.uuid
, chosen_name
);
341 me
= map_by_devnm(map
, st
->container_devnm
);
344 if (st
->ss
->write_init_super(st
)) {
345 st
->ss
->free_super(st
);
350 * Before activating the array, perform extra steps
351 * required to configure the internal write-intent
354 if (info_new
.consistency_policy
== CONSISTENCY_POLICY_BITMAP
&&
355 st
->ss
->set_bitmap
&& st
->ss
->set_bitmap(st
, info
)) {
356 st
->ss
->free_super(st
);
360 /* update parent container uuid */
362 char *path
= xstrdup(me
->path
);
364 st
->ss
->getinfo_super(st
, &info_new
, NULL
);
365 map_update(map
, st
->container_devnm
, info_new
.text_version
,
366 info_new
.uuid
, path
);
370 flush_metadata_updates(st
);
371 st
->ss
->free_super(st
);
376 static int add_disks(int mdfd
, struct mdinfo
*info
, struct shape
*s
,
377 struct context
*c
, struct supertype
*st
,
378 struct map_ent
**map
, struct mddev_dev
*devlist
,
379 int total_slots
, int have_container
, int insert_point
,
380 int major_num
, char *chosen_name
)
382 struct mddev_dev
*moved_disk
= NULL
;
383 int pass
, raid_disk_num
, dnum
;
384 int zero_pids
[total_slots
];
385 struct mddev_dev
*dv
;
386 struct mdinfo
*infos
;
387 sigset_t sigset
, orig_sigset
;
391 * Block SIGINT so the main thread will always wait for the
392 * zeroing processes when being interrupted. Otherwise the
393 * zeroing processes will finish their work in the background
394 * keeping the disk busy.
396 sigemptyset(&sigset
);
397 sigaddset(&sigset
, SIGINT
);
398 sigprocmask(SIG_BLOCK
, &sigset
, &orig_sigset
);
399 memset(zero_pids
, 0, sizeof(zero_pids
));
401 infos
= xmalloc(sizeof(*infos
) * total_slots
);
402 enable_fds(total_slots
);
403 for (pass
= 1; pass
<= 2; pass
++) {
404 for (dnum
= 0, raid_disk_num
= 0, dv
= devlist
; dv
;
405 dv
= (dv
->next
) ? (dv
->next
) : moved_disk
, dnum
++) {
406 if (dnum
>= total_slots
)
408 if (dnum
== insert_point
) {
413 if (strcasecmp(dv
->devname
, "missing") == 0) {
419 if (have_container
&& dnum
< total_slots
- 1)
420 /* repeatedly use the container */
426 infos
[dnum
].disk
.number
= dnum
;
427 infos
[dnum
].disk
.raid_disk
= raid_disk_num
++;
429 if (dv
->disposition
== 'j')
432 ret
= add_disk_to_super(mdfd
, s
, c
, st
, dv
,
433 &infos
[dnum
], have_container
,
434 major_num
, &zero_pids
[dnum
]);
440 infos
[dnum
].errors
= 0;
442 ret
= add_disk(mdfd
, st
, info
, &infos
[dnum
]);
444 pr_err("ADD_NEW_DISK for %s failed: %s\n",
445 dv
->devname
, strerror(errno
));
446 if (errno
== EINVAL
&&
447 info
->array
.level
== 0) {
448 pr_err("Possibly your kernel doesn't support RAID0 layouts.\n");
449 pr_err("Either upgrade, or use --layout=dangerous\n");
455 if (!have_container
&&
456 dv
== moved_disk
&& dnum
!= insert_point
) break;
460 ret
= wait_for_zero_forks(zero_pids
, total_slots
);
464 ret
= update_metadata(mdfd
, s
, st
, map
, info
,
473 wait_for_zero_forks(zero_pids
, total_slots
);
475 sigprocmask(SIG_SETMASK
, &orig_sigset
, NULL
);
479 int Create(struct supertype
*st
, struct mddev_ident
*ident
, int subdevs
,
480 struct mddev_dev
*devlist
, struct shape
*s
, struct context
*c
)
483 * Create a new raid array.
485 * First check that necessary details are available
486 * (i.e. level, raid-disks)
488 * Then check each disk to see what might be on it
489 * and report anything interesting.
491 * If anything looks odd, and runstop not set,
494 * SET_ARRAY_INFO and ADD_NEW_DISK, and
495 * if runstop==run, or raiddisks disks were used,
499 unsigned long long minsize
= 0, maxsize
= 0;
500 char *mindisc
= NULL
;
501 char *maxdisc
= NULL
;
502 char *name
= ident
->name
;
503 int *uuid
= ident
->uuid_set
== 1 ? ident
->uuid
: NULL
;
505 struct mddev_dev
*dv
;
507 int fail
= 0, warn
= 0;
508 int first_missing
= subdevs
* 2;
509 int second_missing
= subdevs
* 2;
510 int missing_disks
= 0;
511 int insert_point
= subdevs
* 2; /* where to insert a missing drive */
515 int have_container
= 0;
516 int container_fd
= -1;
518 unsigned long long bitmapsize
;
521 int do_default_layout
= 0;
522 int do_default_chunk
= 0;
523 char chosen_name
[1024];
524 struct map_ent
*map
= NULL
;
525 unsigned long long newsize
;
526 mdu_array_info_t inf
;
528 int major_num
= BITMAP_MAJOR_HI
;
529 if (s
->bitmap_file
&& strcmp(s
->bitmap_file
, "clustered") == 0) {
530 major_num
= BITMAP_MAJOR_CLUSTERED
;
532 pr_err("At least 2 nodes are needed for cluster-md\n");
537 memset(&info
, 0, sizeof(info
));
538 if (s
->level
== UnSet
&& st
&& st
->ss
->default_geometry
)
539 st
->ss
->default_geometry(st
, &s
->level
, NULL
, NULL
);
540 if (s
->level
== UnSet
) {
541 pr_err("a RAID level is needed to create an array.\n");
544 if (s
->raiddisks
< 4 && s
->level
== 6) {
545 pr_err("at least 4 raid-devices needed for level 6\n");
548 if (s
->raiddisks
> 256 && s
->level
== 6) {
549 pr_err("no more than 256 raid-devices supported for level 6\n");
552 if (s
->raiddisks
< 2 && s
->level
>= 4) {
553 pr_err("at least 2 raid-devices needed for level %d\n", s
->level
);
556 if (s
->level
<= 0 && s
->sparedisks
) {
557 pr_err("This level does not support spare devices\n");
561 if (subdevs
== 1 && strcmp(devlist
->devname
, "missing") != 0) {
562 /* If given a single device, it might be a container, and we can
563 * extract a device list from there
567 memset(&inf
, 0, sizeof(inf
));
568 fd
= open(devlist
->devname
, O_RDONLY
);
570 md_get_array_info(fd
, &inf
) == 0 && inf
.raid_disks
== 0) {
571 /* yep, looks like a container */
573 rv
= st
->ss
->load_container(st
, fd
,
578 st
= super_by_fd(fd
, NULL
);
579 if (st
&& !(rv
= st
->ss
->
580 load_container(st
, fd
,
586 if (have_container
) {
587 subdevs
= s
->raiddisks
;
588 first_missing
= subdevs
* 2;
589 second_missing
= subdevs
* 2;
590 insert_point
= subdevs
* 2;
596 if (st
&& st
->ss
->external
&& s
->sparedisks
) {
597 pr_err("This metadata type does not support spare disks at create time\n");
600 if (subdevs
> s
->raiddisks
+s
->sparedisks
+s
->journaldisks
) {
601 pr_err("You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, s
->raiddisks
+s
->sparedisks
);
604 if (!have_container
&& subdevs
< s
->raiddisks
+s
->sparedisks
+s
->journaldisks
) {
605 pr_err("You haven't given enough devices (real or missing) to create this array\n");
608 if (s
->bitmap_file
&& s
->level
<= 0) {
609 pr_err("bitmaps not meaningful with level %s\n",
610 map_num(pers
, s
->level
)?:"given");
614 /* now set some defaults */
616 if (s
->layout
== UnSet
) {
617 do_default_layout
= 1;
618 s
->layout
= default_layout(st
, s
->level
, c
->verbose
);
622 /* check layout fits in array*/
623 if ((s
->layout
&255) * ((s
->layout
>>8)&255) > s
->raiddisks
) {
624 pr_err("that layout requires at least %d devices\n",
625 (s
->layout
&255) * ((s
->layout
>>8)&255));
635 if (s
->chunk
== 0 || s
->chunk
== UnSet
) {
637 do_default_chunk
= 1;
638 /* chunk will be set later */
642 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
646 case LEVEL_MULTIPATH
:
647 case LEVEL_CONTAINER
:
649 pr_err("specifying chunk size is forbidden for this level\n");
654 pr_err("unknown level %d\n", s
->level
);
658 if (s
->size
== MAX_SIZE
)
659 /* use '0' to mean 'max' now... */
661 if (s
->size
&& s
->chunk
&& s
->chunk
!= UnSet
)
662 if (round_size_and_verify(&s
->size
, s
->chunk
))
665 newsize
= s
->size
* 2;
666 if (st
&& ! st
->ss
->validate_geometry(st
, s
->level
, s
->layout
, s
->raiddisks
,
667 &s
->chunk
, s
->size
*2,
668 s
->data_offset
, NULL
,
669 &newsize
, s
->consistency_policy
,
673 if (s
->chunk
&& s
->chunk
!= UnSet
) {
674 newsize
&= ~(unsigned long long)(s
->chunk
*2 - 1);
675 if (do_default_chunk
) {
676 /* default chunk was just set */
678 pr_err("chunk size defaults to %dK\n", s
->chunk
);
679 if (round_size_and_verify(&s
->size
, s
->chunk
))
681 do_default_chunk
= 0;
686 s
->size
= newsize
/ 2;
688 /* If this is ever reshaped to RAID5, we will
689 * need a chunksize. So round it off a bit
690 * now just to be safe
692 s
->size
&= ~(64ULL-1);
694 if (s
->size
&& c
->verbose
> 0)
695 pr_err("setting size to %lluK\n", s
->size
);
698 /* now look at the subdevs */
699 info
.array
.active_disks
= 0;
700 info
.array
.working_disks
= 0;
702 for (dv
= devlist
; dv
; dv
= dv
->next
)
703 if (s
->data_offset
== VARIABLE_OFFSET
)
704 dv
->data_offset
= INVALID_SECTORS
;
706 dv
->data_offset
= s
->data_offset
;
708 for (dv
=devlist
; dv
&& !have_container
; dv
=dv
->next
, dnum
++) {
709 char *dname
= dv
->devname
;
710 unsigned long long freesize
;
714 if (strcasecmp(dname
, "missing") == 0) {
715 if (first_missing
> dnum
)
716 first_missing
= dnum
;
717 if (second_missing
> dnum
&& dnum
> first_missing
)
718 second_missing
= dnum
;
722 if (s
->data_offset
== VARIABLE_OFFSET
) {
723 doff
= strchr(dname
, ':');
726 dv
->data_offset
= parse_size(doff
);
728 dv
->data_offset
= INVALID_SECTORS
;
730 dv
->data_offset
= s
->data_offset
;
732 dfd
= open(dname
, O_RDONLY
);
734 pr_err("cannot open %s: %s\n",
735 dname
, strerror(errno
));
738 if (!fstat_is_blkdev(dfd
, dname
, NULL
)) {
743 info
.array
.working_disks
++;
744 if (dnum
< s
->raiddisks
&& dv
->disposition
!= 'j')
745 info
.array
.active_disks
++;
747 struct createinfo
*ci
= conf_get_create_info();
752 /* Need to choose a default metadata, which is different
753 * depending on geometry of array.
756 char *name
= "default";
757 for(i
= 0; !st
&& superlist
[i
]; i
++) {
758 st
= superlist
[i
]->match_metadata_desc(name
);
761 if (do_default_layout
)
762 s
->layout
= default_layout(st
, s
->level
, c
->verbose
);
763 switch (st
->ss
->validate_geometry(
764 st
, s
->level
, s
->layout
, s
->raiddisks
,
765 &s
->chunk
, s
->size
*2,
766 dv
->data_offset
, dname
,
767 &freesize
, s
->consistency_policy
,
769 case -1: /* Not valid, message printed, and not
770 * worth checking any further */
773 case 0: /* Geometry not valid */
776 s
->chunk
= do_default_chunk
? UnSet
: s
->chunk
;
778 case 1: /* All happy */
784 int dfd
= open(dname
, O_RDONLY
|O_EXCL
);
786 pr_err("cannot open %s: %s\n",
787 dname
, strerror(errno
));
790 pr_err("device %s not suitable for any style of array\n",
794 if (st
->ss
!= &super0
||
795 st
->minor_version
!= 90)
798 if (do_default_layout
)
799 s
->layout
= default_layout(st
, s
->level
, 0);
800 if (!st
->ss
->validate_geometry(st
, s
->level
, s
->layout
,
802 &s
->chunk
, s
->size
*2,
805 s
->consistency_policy
,
808 pr_err("%s is not suitable for this array.\n",
815 if (dv
->disposition
== 'j')
816 goto skip_size_check
; /* skip write journal for size check */
818 freesize
/= 2; /* convert to K */
819 if (s
->chunk
&& s
->chunk
!= UnSet
) {
820 /* round to chunk size */
821 freesize
= freesize
& ~(s
->chunk
-1);
822 if (do_default_chunk
) {
823 /* default chunk was just set */
825 pr_err("chunk size defaults to %dK\n", s
->chunk
);
826 if (round_size_and_verify(&s
->size
, s
->chunk
))
828 do_default_chunk
= 0;
832 pr_err("no free space left on %s\n", dname
);
837 if (s
->size
&& freesize
< s
->size
) {
838 pr_err("%s is smaller than given size. %lluK < %lluK + metadata\n",
839 dname
, freesize
, s
->size
);
843 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
847 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
852 if (c
->runstop
!= 1 || c
->verbose
>= 0) {
853 int fd
= open(dname
, O_RDONLY
);
855 pr_err("Cannot open %s: %s\n",
856 dname
, strerror(errno
));
860 warn
|= check_ext2(fd
, dname
);
861 warn
|= check_reiser(fd
, dname
);
862 warn
|= check_raid(fd
, dname
);
863 if (strcmp(st
->ss
->name
, "1.x") == 0 &&
864 st
->minor_version
>= 1)
865 /* metadata at front */
866 warn
|= check_partitions(fd
, dname
, 0, 0);
867 else if (s
->level
== 1 || is_container(s
->level
) ||
868 (s
->level
== 0 && s
->raiddisks
== 1))
869 /* partitions could be meaningful */
870 warn
|= check_partitions(fd
, dname
, freesize
*2, s
->size
*2);
872 /* partitions cannot be meaningful */
873 warn
|= check_partitions(fd
, dname
, 0, 0);
874 if (strcmp(st
->ss
->name
, "1.x") == 0 &&
875 st
->minor_version
>= 1 &&
878 (warn
& 1024) == 0) {
880 pr_err("Note: this array has metadata at the start and\n"
881 " may not be suitable as a boot device. If you plan to\n"
882 " store '/boot' on this device please ensure that\n"
883 " your boot-loader understands md/v1.x metadata, or use\n"
884 " --metadata=0.90\n");
889 if (missing_disks
== dnum
&& !have_container
) {
890 pr_err("Subdevs can't be all missing\n");
893 if (s
->raiddisks
+ s
->sparedisks
> st
->max_devs
) {
894 pr_err("Too many devices: %s metadata only supports %d\n",
895 st
->ss
->name
, st
->max_devs
);
899 info
.array
.working_disks
= s
->raiddisks
;
901 pr_err("create aborted\n");
905 if (mindisc
== NULL
&& !have_container
) {
906 pr_err("no size and no drives given - aborting create.\n");
909 if (s
->level
> 0 || s
->level
== LEVEL_MULTIPATH
||
910 s
->level
== LEVEL_FAULTY
|| st
->ss
->external
) {
911 /* size is meaningful */
912 if (!st
->ss
->validate_geometry(st
, s
->level
, s
->layout
,
914 &s
->chunk
, minsize
*2,
917 s
->consistency_policy
, 0)) {
918 pr_err("devices too large for RAID level %d\n", s
->level
);
923 /* If this is ever reshaped to RAID5, we will
924 * need a chunksize. So round it off a bit
925 * now just to be safe
927 s
->size
&= ~(64ULL-1);
929 pr_err("size set to %lluK\n", s
->size
);
933 if (!s
->bitmap_file
&&
936 st
->ss
->add_internal_bitmap
&&
937 s
->journaldisks
== 0 &&
938 (s
->consistency_policy
!= CONSISTENCY_POLICY_RESYNC
&&
939 s
->consistency_policy
!= CONSISTENCY_POLICY_PPL
) &&
940 (s
->write_behind
|| s
->size
> 100*1024*1024ULL)) {
942 pr_err("automatically enabling write-intent bitmap on large array\n");
943 s
->bitmap_file
= "internal";
945 if (s
->bitmap_file
&& str_is_none(s
->bitmap_file
) == true)
946 s
->bitmap_file
= NULL
;
948 if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
949 !st
->ss
->write_init_ppl
) {
950 pr_err("%s metadata does not support PPL\n", st
->ss
->name
);
954 if (!have_container
&& s
->level
> 0 && ((maxsize
-s
->size
)*100 > maxsize
)) {
955 if (c
->runstop
!= 1 || c
->verbose
>= 0)
956 pr_err("largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
961 if (st
->ss
->detail_platform
&& st
->ss
->detail_platform(0, 1, NULL
) != 0) {
962 if (c
->runstop
!= 1 || c
->verbose
>= 0)
963 pr_err("%s unable to enumerate platform support\n"
964 " array may not be compatible with hardware/firmware\n",
968 st
->nodes
= c
->nodes
;
969 st
->cluster_name
= c
->homecluster
;
972 if (c
->runstop
!= 1) {
973 if (!ask("Continue creating array? ")) {
974 pr_err("create aborted.\n");
979 pr_err("creation continuing despite oddities due to --run\n");
983 /* If this is raid4/5, we want to configure the last active slot
984 * as missing, so that a reconstruct happens (faster than re-parity)
985 * FIX: Can we do this for raid6 as well?
987 if (st
->ss
->external
== 0 && s
->assume_clean
== 0 &&
988 c
->force
== 0 && first_missing
>= s
->raiddisks
) {
992 insert_point
= s
->raiddisks
-1;
994 info
.array
.active_disks
--;
1001 /* For raid6, if creating with 1 missing drive, make a good drive
1002 * into a spare, else the create will fail
1004 if (s
->assume_clean
== 0 && c
->force
== 0 && first_missing
< s
->raiddisks
&&
1005 st
->ss
->external
== 0 &&
1006 second_missing
>= s
->raiddisks
&& s
->level
== 6) {
1007 insert_point
= s
->raiddisks
- 1;
1008 if (insert_point
== first_missing
)
1011 info
.array
.active_disks
--;
1015 if (s
->level
<= 0 && first_missing
< subdevs
* 2) {
1016 pr_err("This level does not support missing devices\n");
1020 /* We need to create the device */
1022 mdfd
= create_mddev(ident
->devname
, ident
->name
, c
->autof
, LOCAL
, chosen_name
, 1);
1027 /* verify if chosen_name is not in use,
1028 * it could be in conflict with already existing device
1029 * e.g. container, array
1031 if (strncmp(chosen_name
, DEV_MD_DIR
, DEV_MD_DIR_LEN
) == 0 &&
1032 map_by_name(&map
, chosen_name
+ DEV_MD_DIR_LEN
)) {
1033 pr_err("Array name %s is in use already.\n", chosen_name
);
1040 memset(&inf
, 0, sizeof(inf
));
1041 md_get_array_info(mdfd
, &inf
);
1042 if (inf
.working_disks
!= 0) {
1043 pr_err("another array by this name is already running.\n");
1047 /* Ok, lets try some ioctls */
1049 info
.array
.level
= s
->level
;
1050 info
.array
.size
= s
->size
;
1051 info
.array
.raid_disks
= s
->raiddisks
;
1052 /* The kernel should *know* what md_minor we are dealing
1053 * with, but it chooses to trust me instead. Sigh
1055 info
.array
.md_minor
= 0;
1056 if (fstat_is_blkdev(mdfd
, chosen_name
, &rdev
))
1057 info
.array
.md_minor
= minor(rdev
);
1058 info
.array
.not_persistent
= 0;
1060 if (((s
->level
== 4 || s
->level
== 5) &&
1061 (insert_point
< s
->raiddisks
|| first_missing
< s
->raiddisks
)) ||
1062 (s
->level
== 6 && (insert_point
< s
->raiddisks
||
1063 second_missing
< s
->raiddisks
)) ||
1064 (s
->level
<= 0) || s
->assume_clean
) {
1065 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
1066 info
.resync_start
= MaxSector
;
1068 info
.array
.state
= 0; /* not clean, but no errors */
1069 info
.resync_start
= 0;
1071 if (s
->level
== 10) {
1072 /* for raid10, the bitmap size is the capacity of the array,
1073 * which is array.size * raid_disks / ncopies;
1074 * .. but convert to sectors.
1076 int ncopies
= ((s
->layout
>>8) & 255) * (s
->layout
& 255);
1077 bitmapsize
= s
->size
* s
->raiddisks
/ ncopies
* 2;
1078 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, s->size, s->raiddisks, ncopies);*/
1080 bitmapsize
= s
->size
* 2;
1082 /* There is lots of redundancy in these disk counts,
1083 * raid_disks is the most meaningful value
1084 * it describes the geometry of the array
1086 * nr_disks is total number of used slots.
1087 * it should be raid_disks+spare_disks
1088 * spare_disks is the number of extra disks present
1090 * active_disks is the number of working disks in
1091 * active slots. (With raid_disks)
1092 * working_disks is the total number of working disks,
1094 * failed_disks is the number of disks marked failed
1096 * Ideally, the kernel would keep these (except raid_disks)
1097 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
1098 * So for now, we assume that all raid and spare
1099 * devices will be given.
1101 info
.array
.spare_disks
=s
->sparedisks
;
1102 info
.array
.failed_disks
=missing_disks
;
1103 info
.array
.nr_disks
= info
.array
.working_disks
1104 + info
.array
.failed_disks
;
1105 info
.array
.layout
= s
->layout
;
1106 info
.array
.chunk_size
= s
->chunk
*1024;
1109 /* base name on devname */
1112 * /dev/md_foo -> foo
1115 * /dev/md/home -> home
1116 * /dev/mdhome -> home
1118 /* FIXME compare this with rules in create_mddev */
1119 name
= strrchr(chosen_name
, '/');
1123 if (strncmp(name
, "md_", 3) == 0 &&
1124 strlen(name
) > 3 && (name
- chosen_name
) == 5 /* /dev/ */)
1126 else if (strncmp(name
, "md", 2) == 0 &&
1127 strlen(name
) > 2 && isdigit(name
[2]) &&
1128 (name
- chosen_name
) == 5 /* /dev/ */)
1132 if (!st
->ss
->init_super(st
, &info
.array
, s
, name
, c
->homehost
, uuid
,
1136 total_slots
= info
.array
.nr_disks
;
1137 st
->ss
->getinfo_super(st
, &info
, NULL
);
1138 if (sysfs_init(&info
, mdfd
, NULL
)) {
1139 pr_err("unable to initialize sysfs\n");
1143 if (did_default
&& c
->verbose
>= 0) {
1144 if (is_subarray(info
.text_version
)) {
1145 char devnm
[MD_NAME_MAX
];
1148 sysfs_get_container_devnm(&info
, devnm
);
1150 mdi
= sysfs_read(-1, devnm
, GET_VERSION
);
1152 pr_err("Cannot open sysfs for container %s\n", devnm
);
1156 pr_info("Creating array inside %s container /dev/%s\n", mdi
->text_version
,
1161 pr_info("Defaulting to version %s metadata\n", info
.text_version
);
1164 map_update(&map
, fd2devnm(mdfd
), info
.text_version
,
1165 info
.uuid
, chosen_name
);
1166 /* Keep map locked until devices have been added to array
1167 * to stop another mdadm from finding and using those devices.
1170 if (s
->bitmap_file
&& (strcmp(s
->bitmap_file
, "internal") == 0 ||
1171 strcmp(s
->bitmap_file
, "clustered") == 0)) {
1172 if (!st
->ss
->add_internal_bitmap
) {
1173 pr_err("internal bitmaps not supported with %s metadata\n",
1177 if (st
->ss
->add_internal_bitmap(st
, &s
->bitmap_chunk
,
1178 c
->delay
, s
->write_behind
,
1179 bitmapsize
, 1, major_num
)) {
1180 pr_err("Given bitmap chunk size not supported.\n");
1183 s
->bitmap_file
= NULL
;
1186 if (sysfs_init(&info
, mdfd
, NULL
)) {
1187 pr_err("unable to initialize sysfs\n");
1191 if (st
->ss
->external
&& st
->container_devnm
[0]) {
1194 /* When creating a member, we need to be careful
1195 * to negotiate with mdmon properly.
1196 * If it is already running, we cannot write to
1197 * the devices and must ask it to do that part.
1198 * If it isn't running, we write to the devices,
1199 * and then start it.
1200 * We hold an exclusive open on the container
1201 * device to make sure mdmon doesn't exit after
1202 * we checked that it is running.
1204 * For now, fail if it is already running.
1206 container_fd
= open_dev_excl(st
->container_devnm
);
1207 if (container_fd
< 0) {
1208 pr_err("Cannot get exclusive open on container - weird.\n");
1211 if (mdmon_running(st
->container_devnm
)) {
1213 pr_err("reusing mdmon for %s.\n",
1214 st
->container_devnm
);
1215 st
->update_tail
= &st
->updates
;
1219 rv
= set_array_info(mdfd
, st
, &info
);
1221 pr_err("failed to set array info for %s: %s\n", chosen_name
, strerror(errno
));
1225 if (s
->bitmap_file
) {
1228 st
->ss
->uuid_from_super(st
, uuid
);
1229 if (CreateBitmap(s
->bitmap_file
, c
->force
, (char*)uuid
, s
->bitmap_chunk
,
1230 c
->delay
, s
->write_behind
,
1235 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
1236 if (bitmap_fd
< 0) {
1237 pr_err("weird: %s cannot be opened\n",
1241 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
1242 pr_err("Cannot set bitmap file for %s: %s\n", chosen_name
, strerror(errno
));
1247 if (add_disks(mdfd
, &info
, s
, c
, st
, &map
, devlist
, total_slots
,
1248 have_container
, insert_point
, major_num
, chosen_name
))
1253 if (is_container(s
->level
)) {
1254 /* No need to start. But we should signal udev to
1256 sysfs_uevent(&info
, "change");
1257 if (c
->verbose
>= 0)
1258 pr_err("container %s prepared.\n", chosen_name
);
1259 wait_for(chosen_name
, mdfd
);
1260 } else if (c
->runstop
== 1 || subdevs
>= s
->raiddisks
) {
1261 if (st
->ss
->external
) {
1265 case LEVEL_MULTIPATH
:
1267 err
= sysfs_set_str(&info
, NULL
, "array_state",
1274 err
= sysfs_set_str(&info
, NULL
, "array_state",
1278 sysfs_set_safemode(&info
, info
.safe_mode_delay
);
1280 pr_err("failed to activate array.\n");
1281 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1284 } else if (c
->readonly
&&
1285 sysfs_attribute_available(
1286 &info
, NULL
, "array_state")) {
1287 if (sysfs_set_str(&info
, NULL
,
1288 "array_state", "readonly") < 0) {
1289 pr_err("Failed to start array: %s\n",
1291 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1295 /* param is not actually used */
1297 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
1298 pr_err("RUN_ARRAY failed: %s\n",
1300 if (errno
== 524 /* ENOTSUP */ &&
1301 info
.array
.level
== 0)
1302 cont_err("Please use --layout=original or --layout=alternate\n");
1303 if (info
.array
.chunk_size
& (info
.array
.chunk_size
-1)) {
1304 cont_err("Problem may be that chunk size is not a power of 2\n");
1306 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1309 /* if start_ro module parameter is set, array is
1310 * auto-read-only, which is bad as the resync won't
1311 * start. So lets make it read-write now.
1313 ioctl(mdfd
, RESTART_ARRAY_RW
, NULL
);
1315 if (c
->verbose
>= 0)
1316 pr_info("array %s started.\n", chosen_name
);
1317 if (st
->ss
->external
&& st
->container_devnm
[0]) {
1319 start_mdmon(st
->container_devnm
);
1321 ping_monitor(st
->container_devnm
);
1322 close(container_fd
);
1324 wait_for(chosen_name
, mdfd
);
1326 pr_err("not starting array - not enough devices.\n");
1330 sysfs_uevent(&info
, "change");
1337 map_remove(&map
, fd2devnm(mdfd
));