2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 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 default_layout(struct supertype *st, int level, int verbose)
34 if (st && st->ss->default_geometry)
35 st->ss->default_geometry(st, &level, &layout, NULL);
39 default: /* no layout */
43 layout = 0x102; /* near=2, far=1 */
46 Name ": layout defaults to n2\n");
50 layout = map_name(r5layout, "default");
53 Name ": layout defaults to %s\n", map_num(r5layout, layout));
56 layout = map_name(faultylayout, "default");
60 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
68 int Create(struct supertype *st, char *mddev,
69 int chunk, int level, int layout, unsigned long long size,
70 int raiddisks, int sparedisks,
71 char *name, char *homehost, int *uuid,
72 int subdevs, struct mddev_dev *devlist,
73 int runstop, int verbose, int force, int assume_clean,
74 char *bitmap_file, int bitmap_chunk, int write_behind,
78 * Create a new raid array.
80 * First check that necessary details are available
81 * (i.e. level, raid-disks)
83 * Then check each disk to see what might be on it
84 * and report anything interesting.
86 * If anything looks odd, and runstop not set,
89 * SET_ARRAY_INFO and ADD_NEW_DISK, and
90 * if runstop==run, or raiddisks disks were used,
94 unsigned long long minsize=0, maxsize=0;
101 int first_missing = subdevs * 2;
102 int second_missing = subdevs * 2;
103 int missing_disks = 0;
104 int insert_point = subdevs * 2; /* where to insert a missing drive */
110 int have_container = 0;
111 int container_fd = -1;
113 unsigned long long bitmapsize;
114 struct mdinfo info, *infos;
116 int do_default_layout = 0;
117 unsigned long safe_mode_delay = 0;
118 char chosen_name[1024];
119 struct map_ent *map = NULL;
120 unsigned long long newsize;
122 int major_num = BITMAP_MAJOR_HI;
124 memset(&info, 0, sizeof(info));
125 if (level == UnSet && st && st->ss->default_geometry)
126 st->ss->default_geometry(st, &level, NULL, NULL);
127 if (level == UnSet) {
129 Name ": a RAID level is needed to create an array.\n");
132 if (raiddisks < 4 && level == 6) {
134 Name ": at least 4 raid-devices needed for level 6\n");
137 if (raiddisks > 256 && level == 6) {
139 Name ": no more than 256 raid-devices supported for level 6\n");
142 if (raiddisks < 2 && level >= 4) {
144 Name ": at least 2 raid-devices needed for level 4 or 5\n");
147 if (level <= 0 && sparedisks) {
149 Name ": This level does not support spare devices\n");
153 if (subdevs == 1 && strcmp(devlist->devname, "missing") != 0) {
154 /* If given a single device, it might be a container, and we can
155 * extract a device list from there
157 mdu_array_info_t inf;
160 memset(&inf, 0, sizeof(inf));
161 fd = open(devlist->devname, O_RDONLY);
163 ioctl(fd, GET_ARRAY_INFO, &inf) == 0 &&
164 inf.raid_disks == 0) {
165 /* yep, looks like a container */
167 rv = st->ss->load_container(st, fd,
172 st = super_by_fd(fd, NULL);
173 if (st && !(rv = st->ss->
174 load_container(st, fd,
180 if (have_container) {
182 first_missing = subdevs * 2;
183 second_missing = subdevs * 2;
184 insert_point = subdevs * 2;
190 if (st && st->ss->external && sparedisks) {
192 Name ": This metadata type does not support "
193 "spare disks at create time\n");
196 if (subdevs > raiddisks+sparedisks) {
197 fprintf(stderr, Name ": You have listed more devices (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks);
200 if (!have_container && subdevs < raiddisks+sparedisks) {
201 fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n");
204 if (bitmap_file && level <= 0) {
205 fprintf(stderr, Name ": bitmaps not meaningful with level %s\n",
206 map_num(pers, level)?:"given");
210 /* now set some defaults */
213 if (layout == UnSet) {
214 do_default_layout = 1;
215 layout = default_layout(st, level, verbose);
219 /* check layout fits in array*/
220 if ((layout&255) * ((layout>>8)&255) > raiddisks) {
221 fprintf(stderr, Name ": that layout requires at least %d devices\n",
222 (layout&255) * ((layout>>8)&255));
233 if (st && st->ss->default_geometry)
234 st->ss->default_geometry(st, NULL, NULL, &chunk);
235 chunk = chunk ? : 512;
237 fprintf(stderr, Name ": chunk size defaults to %dK\n", chunk);
241 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
242 if (get_linux_version() < 2006016 && chunk == 0) {
245 fprintf(stderr, Name ": chunk size defaults to 64K\n");
250 case LEVEL_MULTIPATH:
251 case LEVEL_CONTAINER:
255 fprintf(stderr, Name ": chunk size ignored for this level\n");
259 fprintf(stderr, Name ": unknown level %d\n", level);
264 size &= ~(unsigned long long)(chunk - 1);
266 if (st && ! st->ss->validate_geometry(st, level, layout, raiddisks,
267 chunk, size*2, NULL, &newsize, verbose>=0))
271 if (size && verbose > 0)
272 fprintf(stderr, Name ": setting size to %lluK\n",
273 (unsigned long long)size);
276 /* now look at the subdevs */
277 info.array.active_disks = 0;
278 info.array.working_disks = 0;
280 for (dv=devlist; dv && !have_container; dv=dv->next, dnum++) {
281 char *dname = dv->devname;
282 unsigned long long freesize;
283 if (strcasecmp(dname, "missing")==0) {
284 if (first_missing > dnum)
285 first_missing = dnum;
286 if (second_missing > dnum && dnum > first_missing)
287 second_missing = dnum;
291 info.array.working_disks++;
292 if (dnum < raiddisks)
293 info.array.active_disks++;
295 struct createinfo *ci = conf_get_create_info();
300 /* Need to choose a default metadata, which is different
301 * depending on geometry of array.
304 char *name = "default";
305 for(i=0; !st && superlist[i]; i++) {
306 st = superlist[i]->match_metadata_desc(name);
307 if (do_default_layout)
308 layout = default_layout(st, level, verbose);
309 if (st && !st->ss->validate_geometry
310 (st, level, layout, raiddisks,
311 chunk, size*2, dname, &freesize,
317 fprintf(stderr, Name ": device %s not suitable "
318 "for any style of array\n",
322 if (st->ss != &super0 ||
323 st->minor_version != 90)
326 if (do_default_layout)
327 layout = default_layout(st, level, verbose);
328 if (!st->ss->validate_geometry(st, level, layout,
330 chunk, size*2, dname,
335 Name ": %s is not suitable for "
343 freesize /= 2; /* convert to K */
345 /* round to chunk size */
346 freesize = freesize & ~(chunk-1);
349 if (size && freesize < size) {
350 fprintf(stderr, Name ": %s is smaller than given size."
351 " %lluK < %lluK + metadata\n",
352 dname, freesize, size);
356 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
360 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
364 if (runstop != 1 || verbose >= 0) {
365 int fd = open(dname, O_RDONLY);
367 fprintf(stderr, Name ": Cannot open %s: %s\n",
368 dname, strerror(errno));
372 warn |= check_ext2(fd, dname);
373 warn |= check_reiser(fd, dname);
374 warn |= check_raid(fd, dname);
375 if (strcmp(st->ss->name, "1.x") == 0 &&
376 st->minor_version >= 1)
377 /* metadata at front */
378 warn |= check_partitions(fd, dname, 0);
379 else if (level == 1 || level == LEVEL_CONTAINER)
380 /* partitions could be meaningful */
381 warn |= check_partitions(fd, dname, freesize*2);
383 /* partitions cannot be meaningful */
384 warn |= check_partitions(fd, dname, 0);
385 if (strcmp(st->ss->name, "1.x") == 0 &&
386 st->minor_version >= 1 &&
389 (warn & 1024) == 0) {
391 fprintf(stderr, Name ": Note: this array has metadata at the start and\n"
392 " may not be suitable as a boot device. If you plan to\n"
393 " store '/boot' on this device please ensure that\n"
394 " your boot-loader understands md/v1.x metadata, or use\n"
395 " --metadata=0.90\n");
400 if (raiddisks + sparedisks > st->max_devs) {
401 fprintf(stderr, Name ": Too many devices:"
402 " %s metadata only supports %d\n",
403 st->ss->name, st->max_devs);
407 info.array.working_disks = raiddisks;
409 fprintf(stderr, Name ": create aborted\n");
413 if (mindisc == NULL && !have_container) {
414 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
417 if (level > 0 || level == LEVEL_MULTIPATH
418 || level == LEVEL_FAULTY
419 || st->ss->external ) {
420 /* size is meaningful */
421 if (!st->ss->validate_geometry(st, level, layout,
425 fprintf(stderr, Name ": devices too large for RAID level %d\n", level);
430 fprintf(stderr, Name ": size set to %lluK\n", size);
433 if (!have_container && level > 0 && ((maxsize-size)*100 > maxsize)) {
434 if (runstop != 1 || verbose >= 0)
435 fprintf(stderr, Name ": largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
440 if (st->ss->detail_platform && st->ss->detail_platform(0, 1) != 0) {
441 if (runstop != 1 || verbose >= 0)
442 fprintf(stderr, Name ": %s unable to enumerate platform support\n"
443 " array may not be compatible with hardware/firmware\n",
450 if (!ask("Continue creating array? ")) {
451 fprintf(stderr, Name ": create aborted.\n");
456 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
460 /* If this is raid4/5, we want to configure the last active slot
461 * as missing, so that a reconstruct happens (faster than re-parity)
462 * FIX: Can we do this for raid6 as well?
464 if (st->ss->external == 0 &&
465 assume_clean==0 && force == 0 && first_missing >= raiddisks) {
469 insert_point = raiddisks-1;
471 info.array.active_disks--;
478 /* For raid6, if creating with 1 missing drive, make a good drive
479 * into a spare, else the create will fail
481 if (assume_clean == 0 && force == 0 && first_missing < raiddisks &&
482 st->ss->external == 0 &&
483 second_missing >= raiddisks && level == 6) {
484 insert_point = raiddisks - 1;
485 if (insert_point == first_missing)
488 info.array.active_disks--;
492 if (level <= 0 && first_missing < subdevs * 2) {
494 Name ": This level does not support missing devices\n");
498 /* We need to create the device */
500 mdfd = create_mddev(mddev, name, autof, LOCAL, chosen_name);
505 vers = md_get_version(mdfd);
507 fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n");
510 mdu_array_info_t inf;
511 memset(&inf, 0, sizeof(inf));
512 ioctl(mdfd, GET_ARRAY_INFO, &inf);
513 if (inf.working_disks != 0) {
514 fprintf(stderr, Name ": another array by this name"
515 " is already running.\n");
520 /* Ok, lets try some ioctls */
522 info.array.level = level;
523 info.array.size = size;
524 info.array.raid_disks = raiddisks;
525 /* The kernel should *know* what md_minor we are dealing
526 * with, but it chooses to trust me instead. Sigh
528 info.array.md_minor = 0;
529 if (fstat(mdfd, &stb)==0)
530 info.array.md_minor = minor(stb.st_rdev);
531 info.array.not_persistent = 0;
533 if ( ( (level == 4 || level == 5) &&
534 (insert_point < raiddisks || first_missing < raiddisks) )
536 ( level == 6 && (insert_point < raiddisks
537 || second_missing < raiddisks))
543 info.array.state = 1; /* clean, but one+ drive will be missing*/
544 info.resync_start = MaxSector;
546 info.array.state = 0; /* not clean, but no errors */
547 info.resync_start = 0;
550 /* for raid10, the bitmap size is the capacity of the array,
551 * which is array.size * raid_disks / ncopies;
552 * .. but convert to sectors.
554 int ncopies = ((layout>>8) & 255) * (layout & 255);
555 bitmapsize = (unsigned long long)size * raiddisks / ncopies * 2;
556 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
558 bitmapsize = (unsigned long long)size * 2;
560 /* There is lots of redundancy in these disk counts,
561 * raid_disks is the most meaningful value
562 * it describes the geometry of the array
564 * nr_disks is total number of used slots.
565 * it should be raid_disks+spare_disks
566 * spare_disks is the number of extra disks present
568 * active_disks is the number of working disks in
569 * active slots. (With raid_disks)
570 * working_disks is the total number of working disks,
572 * failed_disks is the number of disks marked failed
574 * Ideally, the kernel would keep these (except raid_disks)
575 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
576 * So for now, we assume that all raid and spare
577 * devices will be given.
579 info.array.spare_disks=sparedisks;
580 info.array.failed_disks=missing_disks;
581 info.array.nr_disks = info.array.working_disks
582 + info.array.failed_disks;
583 info.array.layout = layout;
584 info.array.chunk_size = chunk*1024;
586 if (name == NULL || *name == 0) {
587 /* base name on mddev */
592 * /dev/md/home -> home
593 * /dev/mdhome -> home
595 /* FIXME compare this with rules in create_mddev */
596 name = strrchr(mddev, '/');
599 if (strncmp(name, "md_d", 4)==0 &&
602 (name-mddev) == 5 /* /dev/ */)
604 else if (strncmp(name, "md", 2)==0 &&
607 (name-mddev) == 5 /* /dev/ */)
611 if (!st->ss->init_super(st, &info.array, size, name, homehost, uuid))
614 total_slots = info.array.nr_disks;
615 sysfs_init(&info, mdfd, 0);
616 st->ss->getinfo_super(st, &info, NULL);
618 if (did_default && verbose >= 0) {
619 if (is_subarray(info.text_version)) {
620 int dnum = devname2devnum(info.text_version+1);
622 int mdp = get_mdp_major();
625 path = map_dev(MD_MAJOR, dnum, 1);
627 path = map_dev(mdp, (-1-dnum)<< 6, 1);
629 mdi = sysfs_read(-1, dnum, GET_VERSION);
631 fprintf(stderr, Name ": Creating array inside "
633 mdi?mdi->text_version:"managed", path);
636 fprintf(stderr, Name ": Defaulting to version"
637 " %s metadata\n", info.text_version);
640 map_update(&map, fd2devnum(mdfd), info.text_version,
641 info.uuid, chosen_name);
644 if (bitmap_file && vers < 9003) {
645 major_num = BITMAP_MAJOR_HOSTENDIAN;
647 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
648 " between different architectured. Consider upgrading the Linux kernel.\n");
652 if (bitmap_file && strcmp(bitmap_file, "internal")==0) {
653 if ((vers%100) < 2) {
654 fprintf(stderr, Name ": internal bitmaps not supported by this kernel.\n");
657 if (!st->ss->add_internal_bitmap) {
658 fprintf(stderr, Name ": internal bitmaps not supported with %s metadata\n",
662 if (!st->ss->add_internal_bitmap(st, &bitmap_chunk,
664 bitmapsize, 1, major_num)) {
665 fprintf(stderr, Name ": Given bitmap chunk size not supported.\n");
672 sysfs_init(&info, mdfd, 0);
674 if (st->ss->external && st->container_dev != NoMdDev) {
677 /* When creating a member, we need to be careful
678 * to negotiate with mdmon properly.
679 * If it is already running, we cannot write to
680 * the devices and must ask it to do that part.
681 * If it isn't running, we write to the devices,
683 * We hold an exclusive open on the container
684 * device to make sure mdmon doesn't exit after
685 * we checked that it is running.
687 * For now, fail if it is already running.
689 container_fd = open_dev_excl(st->container_dev);
690 if (container_fd < 0) {
691 fprintf(stderr, Name ": Cannot get exclusive "
692 "open on container - weird.\n");
695 if (mdmon_running(st->container_dev)) {
697 fprintf(stderr, Name ": reusing mdmon "
699 devnum2devname(st->container_dev));
700 st->update_tail = &st->updates;
704 rv = set_array_info(mdfd, st, &info);
706 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
707 mddev, strerror(errno));
714 st->ss->uuid_from_super(st, uuid);
715 if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk,
721 bitmap_fd = open(bitmap_file, O_RDWR);
723 fprintf(stderr, Name ": weird: %s cannot be openned\n",
727 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
728 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
729 mddev, strerror(errno));
734 infos = malloc(sizeof(*infos) * total_slots);
736 for (pass=1; pass <=2 ; pass++) {
737 struct mddev_dev *moved_disk = NULL; /* the disk that was moved out of the insert point */
739 for (dnum=0, dv = devlist ; dv ;
740 dv=(dv->next)?(dv->next):moved_disk, dnum++) {
743 struct mdinfo *inf = &infos[dnum];
745 if (dnum >= total_slots)
747 if (dnum == insert_point) {
751 if (strcasecmp(dv->devname, "missing")==0)
755 if (have_container && dnum < info.array.raid_disks - 1)
756 /* repeatedly use the container */
763 inf->disk.number = dnum;
764 inf->disk.raid_disk = dnum;
765 if (inf->disk.raid_disk < raiddisks)
766 inf->disk.state = (1<<MD_DISK_ACTIVE) |
771 if (dv->writemostly == 1)
772 inf->disk.state |= (1<<MD_DISK_WRITEMOSTLY);
777 if (st->ss->external &&
778 st->container_dev != NoMdDev)
779 fd = open(dv->devname, O_RDWR);
781 fd = open(dv->devname, O_RDWR|O_EXCL);
784 fprintf(stderr, Name ": failed to open %s "
785 "after earlier success - aborting\n",
790 inf->disk.major = major(stb.st_rdev);
791 inf->disk.minor = minor(stb.st_rdev);
794 remove_partitions(fd);
795 if (st->ss->add_to_super(st, &inf->disk,
797 ioctl(mdfd, STOP_ARRAY, NULL);
800 st->ss->getinfo_super(st, inf, NULL);
801 safe_mode_delay = inf->safe_mode_delay;
803 if (have_container && verbose > 0)
804 fprintf(stderr, Name ": Using %s for device %d\n",
805 map_dev(inf->disk.major,
809 if (!have_container) {
810 /* getinfo_super might have lost these ... */
811 inf->disk.major = major(stb.st_rdev);
812 inf->disk.minor = minor(stb.st_rdev);
819 rv = add_disk(mdfd, st, &info, inf);
823 Name ": ADD_NEW_DISK for %s "
825 dv->devname, strerror(errno));
830 if (!have_container &&
831 dv == moved_disk && dnum != insert_point) break;
834 struct mdinfo info_new;
835 struct map_ent *me = NULL;
837 /* check to see if the uuid has changed due to these
838 * metadata changes, and if so update the member array
839 * and container uuid. Note ->write_init_super clears
840 * the subarray cursor such that ->getinfo_super once
841 * again returns container info.
844 st->ss->getinfo_super(st, &info_new, NULL);
845 if (st->ss->external && level != LEVEL_CONTAINER &&
846 !same_uuid(info_new.uuid, info.uuid, 0)) {
847 map_update(&map, fd2devnum(mdfd),
848 info_new.text_version,
849 info_new.uuid, chosen_name);
850 me = map_by_devnum(&map, st->container_dev);
853 st->ss->write_init_super(st);
855 /* update parent container uuid */
857 char *path = strdup(me->path);
859 st->ss->getinfo_super(st, &info_new, NULL);
860 map_update(&map, st->container_dev,
861 info_new.text_version,
862 info_new.uuid, path);
867 flush_metadata_updates(st);
868 st->ss->free_super(st);
873 if (level == LEVEL_CONTAINER) {
874 /* No need to start. But we should signal udev to
876 sysfs_uevent(&info, "change");
878 fprintf(stderr, Name ": container %s prepared.\n", mddev);
879 wait_for(chosen_name, mdfd);
880 } else if (runstop == 1 || subdevs >= raiddisks) {
881 if (st->ss->external) {
885 case LEVEL_MULTIPATH:
887 err = sysfs_set_str(&info, NULL, "array_state",
892 err = sysfs_set_str(&info, NULL, "array_state",
896 sysfs_set_safemode(&info, safe_mode_delay);
898 fprintf(stderr, Name ": failed to"
899 " activate array.\n");
900 ioctl(mdfd, STOP_ARRAY, NULL);
904 /* param is not actually used */
906 if (ioctl(mdfd, RUN_ARRAY, ¶m)) {
907 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
909 ioctl(mdfd, STOP_ARRAY, NULL);
914 fprintf(stderr, Name ": array %s started.\n", mddev);
915 if (st->ss->external && st->container_dev != NoMdDev) {
917 start_mdmon(st->container_dev);
919 ping_monitor(devnum2devname(st->container_dev));
922 wait_for(chosen_name, mdfd);
924 fprintf(stderr, Name ": not starting array - not enough devices.\n");
931 map_remove(&map, fd2devnum(mdfd));