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");
43 * default_layout() - Get default layout for level.
44 * @st: metadata requested, could be NULL.
45 * @level: raid level requested.
46 * @verbose: verbose level.
48 * Try to ask metadata handler first, otherwise use global defaults.
50 * Return: Layout or &UnSet, return value meaning depends of level used.
52 int default_layout(struct supertype
*st
, int level
, int verbose
)
55 mapping_t
*layout_map
= NULL
;
56 char *layout_name
= NULL
;
58 if (st
&& st
->ss
->default_geometry
)
59 st
->ss
->default_geometry(st
, &level
, &layout
, NULL
);
65 default: /* no layout */
69 layout
= RAID0_ORIG_LAYOUT
;
72 layout
= 0x102; /* near=2, far=1 */
77 layout_map
= r5layout
;
80 layout_map
= faultylayout
;
85 layout
= map_name(layout_map
, "default");
86 layout_name
= map_num_s(layout_map
, layout
);
88 if (layout_name
&& verbose
> 0)
89 pr_err("layout defaults to %s\n", layout_name
);
94 static int add_disk_to_super(int mdfd
, struct shape
*s
, struct context
*c
,
95 struct supertype
*st
, struct mddev_dev
*dv
,
96 struct mdinfo
*info
, int have_container
, int major_num
)
101 if (dv
->disposition
== 'j') {
102 info
->disk
.raid_disk
= MD_DISK_ROLE_JOURNAL
;
103 info
->disk
.state
= (1<<MD_DISK_JOURNAL
);
104 } else if (info
->disk
.raid_disk
< s
->raiddisks
) {
105 info
->disk
.state
= (1<<MD_DISK_ACTIVE
) |
108 info
->disk
.state
= 0;
111 if (dv
->writemostly
== FlagSet
) {
112 if (major_num
== BITMAP_MAJOR_CLUSTERED
) {
113 pr_err("Can not set %s --write-mostly with a clustered bitmap\n",dv
->devname
);
116 info
->disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
121 if (dv
->failfast
== FlagSet
)
122 info
->disk
.state
|= (1<<MD_DISK_FAILFAST
);
124 if (have_container
) {
127 if (st
->ss
->external
&& st
->container_devnm
[0])
128 fd
= open(dv
->devname
, O_RDWR
);
130 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
);
133 pr_err("failed to open %s after earlier success - aborting\n",
137 if (!fstat_is_blkdev(fd
, dv
->devname
, &rdev
))
139 info
->disk
.major
= major(rdev
);
140 info
->disk
.minor
= minor(rdev
);
143 remove_partitions(fd
);
144 if (st
->ss
->add_to_super(st
, &info
->disk
, fd
, dv
->devname
,
146 ioctl(mdfd
, STOP_ARRAY
, NULL
);
149 st
->ss
->getinfo_super(st
, info
, NULL
);
151 if (have_container
&& c
->verbose
> 0)
152 pr_err("Using %s for device %d\n",
153 map_dev(info
->disk
.major
, info
->disk
.minor
, 0),
156 if (!have_container
) {
157 /* getinfo_super might have lost these ... */
158 info
->disk
.major
= major(rdev
);
159 info
->disk
.minor
= minor(rdev
);
165 static int update_metadata(int mdfd
, struct shape
*s
, struct supertype
*st
,
166 struct map_ent
**map
, struct mdinfo
*info
,
169 struct mdinfo info_new
;
170 struct map_ent
*me
= NULL
;
172 /* check to see if the uuid has changed due to these
173 * metadata changes, and if so update the member array
174 * and container uuid. Note ->write_init_super clears
175 * the subarray cursor such that ->getinfo_super once
176 * again returns container info.
178 st
->ss
->getinfo_super(st
, &info_new
, NULL
);
179 if (st
->ss
->external
&& is_container(s
->level
) &&
180 !same_uuid(info_new
.uuid
, info
->uuid
, 0)) {
181 map_update(map
, fd2devnm(mdfd
),
182 info_new
.text_version
,
183 info_new
.uuid
, chosen_name
);
184 me
= map_by_devnm(map
, st
->container_devnm
);
187 if (st
->ss
->write_init_super(st
)) {
188 st
->ss
->free_super(st
);
193 * Before activating the array, perform extra steps
194 * required to configure the internal write-intent
197 if (info_new
.consistency_policy
== CONSISTENCY_POLICY_BITMAP
&&
198 st
->ss
->set_bitmap
&& st
->ss
->set_bitmap(st
, info
)) {
199 st
->ss
->free_super(st
);
203 /* update parent container uuid */
205 char *path
= xstrdup(me
->path
);
207 st
->ss
->getinfo_super(st
, &info_new
, NULL
);
208 map_update(map
, st
->container_devnm
, info_new
.text_version
,
209 info_new
.uuid
, path
);
213 flush_metadata_updates(st
);
214 st
->ss
->free_super(st
);
219 static int add_disks(int mdfd
, struct mdinfo
*info
, struct shape
*s
,
220 struct context
*c
, struct supertype
*st
,
221 struct map_ent
**map
, struct mddev_dev
*devlist
,
222 int total_slots
, int have_container
, int insert_point
,
223 int major_num
, char *chosen_name
)
225 struct mddev_dev
*moved_disk
= NULL
;
226 int pass
, raid_disk_num
, dnum
;
227 struct mddev_dev
*dv
;
228 struct mdinfo
*infos
;
231 infos
= xmalloc(sizeof(*infos
) * total_slots
);
232 enable_fds(total_slots
);
233 for (pass
= 1; pass
<= 2; pass
++) {
234 for (dnum
= 0, raid_disk_num
= 0, dv
= devlist
; dv
;
235 dv
= (dv
->next
) ? (dv
->next
) : moved_disk
, dnum
++) {
236 if (dnum
>= total_slots
)
238 if (dnum
== insert_point
) {
243 if (strcasecmp(dv
->devname
, "missing") == 0) {
249 if (have_container
&& dnum
< total_slots
- 1)
250 /* repeatedly use the container */
256 infos
[dnum
].disk
.number
= dnum
;
257 infos
[dnum
].disk
.raid_disk
= raid_disk_num
++;
259 if (dv
->disposition
== 'j')
262 ret
= add_disk_to_super(mdfd
, s
, c
, st
, dv
,
263 &infos
[dnum
], have_container
,
270 infos
[dnum
].errors
= 0;
272 ret
= add_disk(mdfd
, st
, info
, &infos
[dnum
]);
274 pr_err("ADD_NEW_DISK for %s failed: %s\n",
275 dv
->devname
, strerror(errno
));
276 if (errno
== EINVAL
&&
277 info
->array
.level
== 0) {
278 pr_err("Possibly your kernel doesn't support RAID0 layouts.\n");
279 pr_err("Either upgrade, or use --layout=dangerous\n");
285 if (!have_container
&&
286 dv
== moved_disk
&& dnum
!= insert_point
) break;
290 ret
= update_metadata(mdfd
, s
, st
, map
, info
,
302 int Create(struct supertype
*st
, char *mddev
,
303 char *name
, int *uuid
,
304 int subdevs
, struct mddev_dev
*devlist
,
309 * Create a new raid array.
311 * First check that necessary details are available
312 * (i.e. level, raid-disks)
314 * Then check each disk to see what might be on it
315 * and report anything interesting.
317 * If anything looks odd, and runstop not set,
320 * SET_ARRAY_INFO and ADD_NEW_DISK, and
321 * if runstop==run, or raiddisks disks were used,
325 unsigned long long minsize
= 0, maxsize
= 0;
326 char *mindisc
= NULL
;
327 char *maxdisc
= NULL
;
329 struct mddev_dev
*dv
;
331 int fail
= 0, warn
= 0;
332 int first_missing
= subdevs
* 2;
333 int second_missing
= subdevs
* 2;
334 int missing_disks
= 0;
335 int insert_point
= subdevs
* 2; /* where to insert a missing drive */
339 int have_container
= 0;
340 int container_fd
= -1;
342 unsigned long long bitmapsize
;
345 int do_default_layout
= 0;
346 int do_default_chunk
= 0;
347 char chosen_name
[1024];
348 struct map_ent
*map
= NULL
;
349 unsigned long long newsize
;
350 mdu_array_info_t inf
;
352 int major_num
= BITMAP_MAJOR_HI
;
353 if (s
->bitmap_file
&& strcmp(s
->bitmap_file
, "clustered") == 0) {
354 major_num
= BITMAP_MAJOR_CLUSTERED
;
356 pr_err("At least 2 nodes are needed for cluster-md\n");
361 memset(&info
, 0, sizeof(info
));
362 if (s
->level
== UnSet
&& st
&& st
->ss
->default_geometry
)
363 st
->ss
->default_geometry(st
, &s
->level
, NULL
, NULL
);
364 if (s
->level
== UnSet
) {
365 pr_err("a RAID level is needed to create an array.\n");
368 if (s
->raiddisks
< 4 && s
->level
== 6) {
369 pr_err("at least 4 raid-devices needed for level 6\n");
372 if (s
->raiddisks
> 256 && s
->level
== 6) {
373 pr_err("no more than 256 raid-devices supported for level 6\n");
376 if (s
->raiddisks
< 2 && s
->level
>= 4) {
377 pr_err("at least 2 raid-devices needed for level %d\n", s
->level
);
380 if (s
->level
<= 0 && s
->sparedisks
) {
381 pr_err("This level does not support spare devices\n");
385 if (subdevs
== 1 && strcmp(devlist
->devname
, "missing") != 0) {
386 /* If given a single device, it might be a container, and we can
387 * extract a device list from there
391 memset(&inf
, 0, sizeof(inf
));
392 fd
= open(devlist
->devname
, O_RDONLY
);
394 md_get_array_info(fd
, &inf
) == 0 && inf
.raid_disks
== 0) {
395 /* yep, looks like a container */
397 rv
= st
->ss
->load_container(st
, fd
,
402 st
= super_by_fd(fd
, NULL
);
403 if (st
&& !(rv
= st
->ss
->
404 load_container(st
, fd
,
410 if (have_container
) {
411 subdevs
= s
->raiddisks
;
412 first_missing
= subdevs
* 2;
413 second_missing
= subdevs
* 2;
414 insert_point
= subdevs
* 2;
420 if (st
&& st
->ss
->external
&& s
->sparedisks
) {
421 pr_err("This metadata type does not support spare disks at create time\n");
424 if (subdevs
> s
->raiddisks
+s
->sparedisks
+s
->journaldisks
) {
425 pr_err("You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, s
->raiddisks
+s
->sparedisks
);
428 if (!have_container
&& subdevs
< s
->raiddisks
+s
->sparedisks
+s
->journaldisks
) {
429 pr_err("You haven't given enough devices (real or missing) to create this array\n");
432 if (s
->bitmap_file
&& s
->level
<= 0) {
433 pr_err("bitmaps not meaningful with level %s\n",
434 map_num(pers
, s
->level
)?:"given");
438 /* now set some defaults */
440 if (s
->layout
== UnSet
) {
441 do_default_layout
= 1;
442 s
->layout
= default_layout(st
, s
->level
, c
->verbose
);
446 /* check layout fits in array*/
447 if ((s
->layout
&255) * ((s
->layout
>>8)&255) > s
->raiddisks
) {
448 pr_err("that layout requires at least %d devices\n",
449 (s
->layout
&255) * ((s
->layout
>>8)&255));
459 if (s
->chunk
== 0 || s
->chunk
== UnSet
) {
461 do_default_chunk
= 1;
462 /* chunk will be set later */
466 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
467 if (get_linux_version() < 2006016 && s
->chunk
== 0) {
470 pr_err("chunk size defaults to 64K\n");
475 case LEVEL_MULTIPATH
:
476 case LEVEL_CONTAINER
:
478 pr_err("specifying chunk size is forbidden for this level\n");
483 pr_err("unknown level %d\n", s
->level
);
487 if (s
->size
== MAX_SIZE
)
488 /* use '0' to mean 'max' now... */
490 if (s
->size
&& s
->chunk
&& s
->chunk
!= UnSet
)
491 if (round_size_and_verify(&s
->size
, s
->chunk
))
494 newsize
= s
->size
* 2;
495 if (st
&& ! st
->ss
->validate_geometry(st
, s
->level
, s
->layout
, s
->raiddisks
,
496 &s
->chunk
, s
->size
*2,
497 s
->data_offset
, NULL
,
498 &newsize
, s
->consistency_policy
,
502 if (s
->chunk
&& s
->chunk
!= UnSet
) {
503 newsize
&= ~(unsigned long long)(s
->chunk
*2 - 1);
504 if (do_default_chunk
) {
505 /* default chunk was just set */
507 pr_err("chunk size defaults to %dK\n", s
->chunk
);
508 if (round_size_and_verify(&s
->size
, s
->chunk
))
510 do_default_chunk
= 0;
515 s
->size
= newsize
/ 2;
517 /* If this is ever reshaped to RAID5, we will
518 * need a chunksize. So round it off a bit
519 * now just to be safe
521 s
->size
&= ~(64ULL-1);
523 if (s
->size
&& c
->verbose
> 0)
524 pr_err("setting size to %lluK\n", s
->size
);
527 /* now look at the subdevs */
528 info
.array
.active_disks
= 0;
529 info
.array
.working_disks
= 0;
531 for (dv
= devlist
; dv
; dv
= dv
->next
)
532 if (s
->data_offset
== VARIABLE_OFFSET
)
533 dv
->data_offset
= INVALID_SECTORS
;
535 dv
->data_offset
= s
->data_offset
;
537 for (dv
=devlist
; dv
&& !have_container
; dv
=dv
->next
, dnum
++) {
538 char *dname
= dv
->devname
;
539 unsigned long long freesize
;
543 if (strcasecmp(dname
, "missing") == 0) {
544 if (first_missing
> dnum
)
545 first_missing
= dnum
;
546 if (second_missing
> dnum
&& dnum
> first_missing
)
547 second_missing
= dnum
;
551 if (s
->data_offset
== VARIABLE_OFFSET
) {
552 doff
= strchr(dname
, ':');
555 dv
->data_offset
= parse_size(doff
);
557 dv
->data_offset
= INVALID_SECTORS
;
559 dv
->data_offset
= s
->data_offset
;
561 dfd
= open(dname
, O_RDONLY
);
563 pr_err("cannot open %s: %s\n",
564 dname
, strerror(errno
));
567 if (!fstat_is_blkdev(dfd
, dname
, NULL
)) {
572 info
.array
.working_disks
++;
573 if (dnum
< s
->raiddisks
&& dv
->disposition
!= 'j')
574 info
.array
.active_disks
++;
576 struct createinfo
*ci
= conf_get_create_info();
581 /* Need to choose a default metadata, which is different
582 * depending on geometry of array.
585 char *name
= "default";
586 for(i
= 0; !st
&& superlist
[i
]; i
++) {
587 st
= superlist
[i
]->match_metadata_desc(name
);
590 if (do_default_layout
)
591 s
->layout
= default_layout(st
, s
->level
, c
->verbose
);
592 switch (st
->ss
->validate_geometry(
593 st
, s
->level
, s
->layout
, s
->raiddisks
,
594 &s
->chunk
, s
->size
*2,
595 dv
->data_offset
, dname
,
596 &freesize
, s
->consistency_policy
,
598 case -1: /* Not valid, message printed, and not
599 * worth checking any further */
602 case 0: /* Geometry not valid */
605 s
->chunk
= do_default_chunk
? UnSet
: s
->chunk
;
607 case 1: /* All happy */
613 int dfd
= open(dname
, O_RDONLY
|O_EXCL
);
615 pr_err("cannot open %s: %s\n",
616 dname
, strerror(errno
));
619 pr_err("device %s not suitable for any style of array\n",
623 if (st
->ss
!= &super0
||
624 st
->minor_version
!= 90)
627 if (do_default_layout
)
628 s
->layout
= default_layout(st
, s
->level
, 0);
629 if (!st
->ss
->validate_geometry(st
, s
->level
, s
->layout
,
631 &s
->chunk
, s
->size
*2,
634 s
->consistency_policy
,
637 pr_err("%s is not suitable for this array.\n",
644 if (dv
->disposition
== 'j')
645 goto skip_size_check
; /* skip write journal for size check */
647 freesize
/= 2; /* convert to K */
648 if (s
->chunk
&& s
->chunk
!= UnSet
) {
649 /* round to chunk size */
650 freesize
= freesize
& ~(s
->chunk
-1);
651 if (do_default_chunk
) {
652 /* default chunk was just set */
654 pr_err("chunk size defaults to %dK\n", s
->chunk
);
655 if (round_size_and_verify(&s
->size
, s
->chunk
))
657 do_default_chunk
= 0;
661 pr_err("no free space left on %s\n", dname
);
666 if (s
->size
&& freesize
< s
->size
) {
667 pr_err("%s is smaller than given size. %lluK < %lluK + metadata\n",
668 dname
, freesize
, s
->size
);
672 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
676 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
681 if (c
->runstop
!= 1 || c
->verbose
>= 0) {
682 int fd
= open(dname
, O_RDONLY
);
684 pr_err("Cannot open %s: %s\n",
685 dname
, strerror(errno
));
689 warn
|= check_ext2(fd
, dname
);
690 warn
|= check_reiser(fd
, dname
);
691 warn
|= check_raid(fd
, dname
);
692 if (strcmp(st
->ss
->name
, "1.x") == 0 &&
693 st
->minor_version
>= 1)
694 /* metadata at front */
695 warn
|= check_partitions(fd
, dname
, 0, 0);
696 else if (s
->level
== 1 || is_container(s
->level
) ||
697 (s
->level
== 0 && s
->raiddisks
== 1))
698 /* partitions could be meaningful */
699 warn
|= check_partitions(fd
, dname
, freesize
*2, s
->size
*2);
701 /* partitions cannot be meaningful */
702 warn
|= check_partitions(fd
, dname
, 0, 0);
703 if (strcmp(st
->ss
->name
, "1.x") == 0 &&
704 st
->minor_version
>= 1 &&
707 (warn
& 1024) == 0) {
709 pr_err("Note: this array has metadata at the start and\n"
710 " may not be suitable as a boot device. If you plan to\n"
711 " store '/boot' on this device please ensure that\n"
712 " your boot-loader understands md/v1.x metadata, or use\n"
713 " --metadata=0.90\n");
718 if (missing_disks
== dnum
&& !have_container
) {
719 pr_err("Subdevs can't be all missing\n");
722 if (s
->raiddisks
+ s
->sparedisks
> st
->max_devs
) {
723 pr_err("Too many devices: %s metadata only supports %d\n",
724 st
->ss
->name
, st
->max_devs
);
728 info
.array
.working_disks
= s
->raiddisks
;
730 pr_err("create aborted\n");
734 if (mindisc
== NULL
&& !have_container
) {
735 pr_err("no size and no drives given - aborting create.\n");
738 if (s
->level
> 0 || s
->level
== LEVEL_MULTIPATH
||
739 s
->level
== LEVEL_FAULTY
|| st
->ss
->external
) {
740 /* size is meaningful */
741 if (!st
->ss
->validate_geometry(st
, s
->level
, s
->layout
,
743 &s
->chunk
, minsize
*2,
746 s
->consistency_policy
, 0)) {
747 pr_err("devices too large for RAID level %d\n", s
->level
);
752 /* If this is ever reshaped to RAID5, we will
753 * need a chunksize. So round it off a bit
754 * now just to be safe
756 s
->size
&= ~(64ULL-1);
758 pr_err("size set to %lluK\n", s
->size
);
762 if (!s
->bitmap_file
&&
765 st
->ss
->add_internal_bitmap
&&
766 s
->journaldisks
== 0 &&
767 (s
->consistency_policy
!= CONSISTENCY_POLICY_RESYNC
&&
768 s
->consistency_policy
!= CONSISTENCY_POLICY_PPL
) &&
769 (s
->write_behind
|| s
->size
> 100*1024*1024ULL)) {
771 pr_err("automatically enabling write-intent bitmap on large array\n");
772 s
->bitmap_file
= "internal";
774 if (s
->bitmap_file
&& strcmp(s
->bitmap_file
, "none") == 0)
775 s
->bitmap_file
= NULL
;
777 if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
778 !st
->ss
->write_init_ppl
) {
779 pr_err("%s metadata does not support PPL\n", st
->ss
->name
);
783 if (!have_container
&& s
->level
> 0 && ((maxsize
-s
->size
)*100 > maxsize
)) {
784 if (c
->runstop
!= 1 || c
->verbose
>= 0)
785 pr_err("largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
790 if (st
->ss
->detail_platform
&& st
->ss
->detail_platform(0, 1, NULL
) != 0) {
791 if (c
->runstop
!= 1 || c
->verbose
>= 0)
792 pr_err("%s unable to enumerate platform support\n"
793 " array may not be compatible with hardware/firmware\n",
797 st
->nodes
= c
->nodes
;
798 st
->cluster_name
= c
->homecluster
;
801 if (c
->runstop
!= 1) {
802 if (!ask("Continue creating array? ")) {
803 pr_err("create aborted.\n");
808 pr_err("creation continuing despite oddities due to --run\n");
812 /* If this is raid4/5, we want to configure the last active slot
813 * as missing, so that a reconstruct happens (faster than re-parity)
814 * FIX: Can we do this for raid6 as well?
816 if (st
->ss
->external
== 0 && s
->assume_clean
== 0 &&
817 c
->force
== 0 && first_missing
>= s
->raiddisks
) {
821 insert_point
= s
->raiddisks
-1;
823 info
.array
.active_disks
--;
830 /* For raid6, if creating with 1 missing drive, make a good drive
831 * into a spare, else the create will fail
833 if (s
->assume_clean
== 0 && c
->force
== 0 && first_missing
< s
->raiddisks
&&
834 st
->ss
->external
== 0 &&
835 second_missing
>= s
->raiddisks
&& s
->level
== 6) {
836 insert_point
= s
->raiddisks
- 1;
837 if (insert_point
== first_missing
)
840 info
.array
.active_disks
--;
844 if (s
->level
<= 0 && first_missing
< subdevs
* 2) {
845 pr_err("This level does not support missing devices\n");
849 /* We need to create the device */
851 mdfd
= create_mddev(mddev
, name
, c
->autof
, LOCAL
, chosen_name
, 1);
856 /* verify if chosen_name is not in use,
857 * it could be in conflict with already existing device
858 * e.g. container, array
860 if (strncmp(chosen_name
, "/dev/md/", 8) == 0 &&
861 map_by_name(&map
, chosen_name
+8) != NULL
) {
862 pr_err("Array name %s is in use already.\n",
871 memset(&inf
, 0, sizeof(inf
));
872 md_get_array_info(mdfd
, &inf
);
873 if (inf
.working_disks
!= 0) {
874 pr_err("another array by this name is already running.\n");
878 /* Ok, lets try some ioctls */
880 info
.array
.level
= s
->level
;
881 info
.array
.size
= s
->size
;
882 info
.array
.raid_disks
= s
->raiddisks
;
883 /* The kernel should *know* what md_minor we are dealing
884 * with, but it chooses to trust me instead. Sigh
886 info
.array
.md_minor
= 0;
887 if (fstat_is_blkdev(mdfd
, mddev
, &rdev
))
888 info
.array
.md_minor
= minor(rdev
);
889 info
.array
.not_persistent
= 0;
891 if (((s
->level
== 4 || s
->level
== 5) &&
892 (insert_point
< s
->raiddisks
|| first_missing
< s
->raiddisks
)) ||
893 (s
->level
== 6 && (insert_point
< s
->raiddisks
||
894 second_missing
< s
->raiddisks
)) ||
895 (s
->level
<= 0) || s
->assume_clean
) {
896 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
897 info
.resync_start
= MaxSector
;
899 info
.array
.state
= 0; /* not clean, but no errors */
900 info
.resync_start
= 0;
902 if (s
->level
== 10) {
903 /* for raid10, the bitmap size is the capacity of the array,
904 * which is array.size * raid_disks / ncopies;
905 * .. but convert to sectors.
907 int ncopies
= ((s
->layout
>>8) & 255) * (s
->layout
& 255);
908 bitmapsize
= s
->size
* s
->raiddisks
/ ncopies
* 2;
909 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, s->size, s->raiddisks, ncopies);*/
911 bitmapsize
= s
->size
* 2;
913 /* There is lots of redundancy in these disk counts,
914 * raid_disks is the most meaningful value
915 * it describes the geometry of the array
917 * nr_disks is total number of used slots.
918 * it should be raid_disks+spare_disks
919 * spare_disks is the number of extra disks present
921 * active_disks is the number of working disks in
922 * active slots. (With raid_disks)
923 * working_disks is the total number of working disks,
925 * failed_disks is the number of disks marked failed
927 * Ideally, the kernel would keep these (except raid_disks)
928 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
929 * So for now, we assume that all raid and spare
930 * devices will be given.
932 info
.array
.spare_disks
=s
->sparedisks
;
933 info
.array
.failed_disks
=missing_disks
;
934 info
.array
.nr_disks
= info
.array
.working_disks
935 + info
.array
.failed_disks
;
936 info
.array
.layout
= s
->layout
;
937 info
.array
.chunk_size
= s
->chunk
*1024;
939 if (name
== NULL
|| *name
== 0) {
940 /* base name on mddev */
946 * /dev/md/home -> home
947 * /dev/mdhome -> home
949 /* FIXME compare this with rules in create_mddev */
950 name
= strrchr(mddev
, '/');
953 if (strncmp(name
, "md_", 3) == 0 &&
954 strlen(name
) > 3 && (name
-mddev
) == 5 /* /dev/ */)
956 else if (strncmp(name
, "md", 2) == 0 &&
957 strlen(name
) > 2 && isdigit(name
[2]) &&
958 (name
-mddev
) == 5 /* /dev/ */)
962 if (!st
->ss
->init_super(st
, &info
.array
, s
, name
, c
->homehost
, uuid
,
966 total_slots
= info
.array
.nr_disks
;
967 st
->ss
->getinfo_super(st
, &info
, NULL
);
968 if (sysfs_init(&info
, mdfd
, NULL
)) {
969 pr_err("unable to initialize sysfs\n");
973 if (did_default
&& c
->verbose
>= 0) {
974 if (is_subarray(info
.text_version
)) {
979 strncpy(devnm
, info
.text_version
+1, 32);
981 ep
= strchr(devnm
, '/');
985 mdi
= sysfs_read(-1, devnm
, GET_VERSION
);
987 pr_info("Creating array inside %s container %s\n",
988 mdi
?mdi
->text_version
:"managed", devnm
);
991 pr_info("Defaulting to version %s metadata\n",
995 map_update(&map
, fd2devnm(mdfd
), info
.text_version
,
996 info
.uuid
, chosen_name
);
997 /* Keep map locked until devices have been added to array
998 * to stop another mdadm from finding and using those devices.
1001 if (s
->bitmap_file
&& (strcmp(s
->bitmap_file
, "internal") == 0 ||
1002 strcmp(s
->bitmap_file
, "clustered") == 0)) {
1003 if (!st
->ss
->add_internal_bitmap
) {
1004 pr_err("internal bitmaps not supported with %s metadata\n",
1008 if (st
->ss
->add_internal_bitmap(st
, &s
->bitmap_chunk
,
1009 c
->delay
, s
->write_behind
,
1010 bitmapsize
, 1, major_num
)) {
1011 pr_err("Given bitmap chunk size not supported.\n");
1014 s
->bitmap_file
= NULL
;
1017 if (sysfs_init(&info
, mdfd
, NULL
)) {
1018 pr_err("unable to initialize sysfs\n");
1022 if (st
->ss
->external
&& st
->container_devnm
[0]) {
1025 /* When creating a member, we need to be careful
1026 * to negotiate with mdmon properly.
1027 * If it is already running, we cannot write to
1028 * the devices and must ask it to do that part.
1029 * If it isn't running, we write to the devices,
1030 * and then start it.
1031 * We hold an exclusive open on the container
1032 * device to make sure mdmon doesn't exit after
1033 * we checked that it is running.
1035 * For now, fail if it is already running.
1037 container_fd
= open_dev_excl(st
->container_devnm
);
1038 if (container_fd
< 0) {
1039 pr_err("Cannot get exclusive open on container - weird.\n");
1042 if (mdmon_running(st
->container_devnm
)) {
1044 pr_err("reusing mdmon for %s.\n",
1045 st
->container_devnm
);
1046 st
->update_tail
= &st
->updates
;
1050 rv
= set_array_info(mdfd
, st
, &info
);
1052 pr_err("failed to set array info for %s: %s\n",
1053 mddev
, strerror(errno
));
1057 if (s
->bitmap_file
) {
1060 st
->ss
->uuid_from_super(st
, uuid
);
1061 if (CreateBitmap(s
->bitmap_file
, c
->force
, (char*)uuid
, s
->bitmap_chunk
,
1062 c
->delay
, s
->write_behind
,
1067 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
1068 if (bitmap_fd
< 0) {
1069 pr_err("weird: %s cannot be opened\n",
1073 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
1074 pr_err("Cannot set bitmap file for %s: %s\n",
1075 mddev
, strerror(errno
));
1080 if (add_disks(mdfd
, &info
, s
, c
, st
, &map
, devlist
, total_slots
,
1081 have_container
, insert_point
, major_num
, chosen_name
))
1086 if (is_container(s
->level
)) {
1087 /* No need to start. But we should signal udev to
1089 sysfs_uevent(&info
, "change");
1090 if (c
->verbose
>= 0)
1091 pr_err("container %s prepared.\n", mddev
);
1092 wait_for(chosen_name
, mdfd
);
1093 } else if (c
->runstop
== 1 || subdevs
>= s
->raiddisks
) {
1094 if (st
->ss
->external
) {
1098 case LEVEL_MULTIPATH
:
1100 err
= sysfs_set_str(&info
, NULL
, "array_state",
1107 err
= sysfs_set_str(&info
, NULL
, "array_state",
1111 sysfs_set_safemode(&info
, info
.safe_mode_delay
);
1113 pr_err("failed to activate array.\n");
1114 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1117 } else if (c
->readonly
&&
1118 sysfs_attribute_available(
1119 &info
, NULL
, "array_state")) {
1120 if (sysfs_set_str(&info
, NULL
,
1121 "array_state", "readonly") < 0) {
1122 pr_err("Failed to start array: %s\n",
1124 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1128 /* param is not actually used */
1130 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
1131 pr_err("RUN_ARRAY failed: %s\n",
1133 if (errno
== 524 /* ENOTSUP */ &&
1134 info
.array
.level
== 0)
1135 cont_err("Please use --layout=original or --layout=alternate\n");
1136 if (info
.array
.chunk_size
& (info
.array
.chunk_size
-1)) {
1137 cont_err("Problem may be that chunk size is not a power of 2\n");
1139 ioctl(mdfd
, STOP_ARRAY
, NULL
);
1142 /* if start_ro module parameter is set, array is
1143 * auto-read-only, which is bad as the resync won't
1144 * start. So lets make it read-write now.
1146 ioctl(mdfd
, RESTART_ARRAY_RW
, NULL
);
1148 if (c
->verbose
>= 0)
1149 pr_info("array %s started.\n", mddev
);
1150 if (st
->ss
->external
&& st
->container_devnm
[0]) {
1152 start_mdmon(st
->container_devnm
);
1154 ping_monitor(st
->container_devnm
);
1155 close(container_fd
);
1157 wait_for(chosen_name
, mdfd
);
1159 pr_err("not starting array - not enough devices.\n");
1163 sysfs_uevent(&info
, "change");
1170 map_remove(&map
, fd2devnm(mdfd
));