]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
7c6979ac2047ad68e55035585aef7c3e8f937143
[thirdparty/mdadm.git] / Create.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
5 *
6 *
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.
11 *
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.
16 *
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
20 *
21 * Author: Neil Brown
22 * Email: <neilb@suse.de>
23 */
24
25 #include "mdadm.h"
26 #include "md_u.h"
27 #include "md_p.h"
28 #include <ctype.h>
29
30 static int default_layout(struct supertype *st, int level, int verbose)
31 {
32 int layout = UnSet;
33
34 if (st && st->ss->default_geometry)
35 st->ss->default_geometry(st, &level, &layout, NULL);
36
37 if (layout == UnSet)
38 switch(level) {
39 default: /* no layout */
40 layout = 0;
41 break;
42 case 10:
43 layout = 0x102; /* near=2, far=1 */
44 if (verbose > 0)
45 fprintf(stderr,
46 Name ": layout defaults to n2\n");
47 break;
48 case 5:
49 case 6:
50 layout = map_name(r5layout, "default");
51 if (verbose > 0)
52 fprintf(stderr,
53 Name ": layout defaults to %s\n", map_num(r5layout, layout));
54 break;
55 case LEVEL_FAULTY:
56 layout = map_name(faultylayout, "default");
57
58 if (verbose > 0)
59 fprintf(stderr,
60 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
61 break;
62 }
63
64 return layout;
65 }
66
67
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,
75 int delay, int autof)
76 {
77 /*
78 * Create a new raid array.
79 *
80 * First check that necessary details are available
81 * (i.e. level, raid-disks)
82 *
83 * Then check each disk to see what might be on it
84 * and report anything interesting.
85 *
86 * If anything looks odd, and runstop not set,
87 * abort.
88 *
89 * SET_ARRAY_INFO and ADD_NEW_DISK, and
90 * if runstop==run, or raiddisks disks were used,
91 * RUN_ARRAY
92 */
93 int mdfd;
94 unsigned long long minsize=0, maxsize=0;
95 char *mindisc = NULL;
96 char *maxdisc = NULL;
97 int dnum;
98 struct mddev_dev *dv;
99 int fail=0, warn=0;
100 struct stat stb;
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 */
105 int total_slots;
106 int pass;
107 int vers;
108 int rv;
109 int bitmap_fd;
110 int have_container = 0;
111 int container_fd = -1;
112 int need_mdmon = 0;
113 unsigned long long bitmapsize;
114 struct mdinfo info, *infos;
115 int did_default = 0;
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;
121
122 int major_num = BITMAP_MAJOR_HI;
123
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) {
128 fprintf(stderr,
129 Name ": a RAID level is needed to create an array.\n");
130 return 1;
131 }
132 if (raiddisks < 4 && level == 6) {
133 fprintf(stderr,
134 Name ": at least 4 raid-devices needed for level 6\n");
135 return 1;
136 }
137 if (raiddisks > 256 && level == 6) {
138 fprintf(stderr,
139 Name ": no more than 256 raid-devices supported for level 6\n");
140 return 1;
141 }
142 if (raiddisks < 2 && level >= 4) {
143 fprintf(stderr,
144 Name ": at least 2 raid-devices needed for level 4 or 5\n");
145 return 1;
146 }
147 if (level <= 0 && sparedisks) {
148 fprintf(stderr,
149 Name ": This level does not support spare devices\n");
150 return 1;
151 }
152
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
156 */
157 mdu_array_info_t inf;
158 int fd;
159
160 memset(&inf, 0, sizeof(inf));
161 fd = open(devlist->devname, O_RDONLY);
162 if (fd >= 0 &&
163 ioctl(fd, GET_ARRAY_INFO, &inf) == 0 &&
164 inf.raid_disks == 0) {
165 /* yep, looks like a container */
166 if (st) {
167 rv = st->ss->load_container(st, fd,
168 devlist->devname);
169 if (rv == 0)
170 have_container = 1;
171 } else {
172 st = super_by_fd(fd, NULL);
173 if (st && !(rv = st->ss->
174 load_container(st, fd,
175 devlist->devname)))
176 have_container = 1;
177 else
178 st = NULL;
179 }
180 if (have_container) {
181 subdevs = raiddisks;
182 first_missing = subdevs * 2;
183 second_missing = subdevs * 2;
184 insert_point = subdevs * 2;
185 }
186 }
187 if (fd >= 0)
188 close(fd);
189 }
190 if (st && st->ss->external && sparedisks) {
191 fprintf(stderr,
192 Name ": This metadata type does not support "
193 "spare disks at create time\n");
194 return 1;
195 }
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);
198 return 1;
199 }
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");
202 return 1;
203 }
204 if (bitmap_file && level <= 0) {
205 fprintf(stderr, Name ": bitmaps not meaningful with level %s\n",
206 map_num(pers, level)?:"given");
207 return 1;
208 }
209
210 /* now set some defaults */
211
212
213 if (layout == UnSet) {
214 do_default_layout = 1;
215 layout = default_layout(st, level, verbose);
216 }
217
218 if (level == 10)
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));
223 return 1;
224 }
225
226 switch(level) {
227 case 4:
228 case 5:
229 case 10:
230 case 6:
231 case 0:
232 if (chunk == 0) {
233 if (st && st->ss->default_geometry)
234 st->ss->default_geometry(st, NULL, NULL, &chunk);
235 chunk = chunk ? : 512;
236 if (verbose > 0)
237 fprintf(stderr, Name ": chunk size defaults to %dK\n", chunk);
238 }
239 break;
240 case LEVEL_LINEAR:
241 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
242 if (get_linux_version() < 2006016 && chunk == 0) {
243 chunk = 64;
244 if (verbose > 0)
245 fprintf(stderr, Name ": chunk size defaults to 64K\n");
246 }
247 break;
248 case 1:
249 case LEVEL_FAULTY:
250 case LEVEL_MULTIPATH:
251 case LEVEL_CONTAINER:
252 if (chunk) {
253 chunk = 0;
254 if (verbose > 0)
255 fprintf(stderr, Name ": chunk size ignored for this level\n");
256 }
257 break;
258 default:
259 fprintf(stderr, Name ": unknown level %d\n", level);
260 return 1;
261 }
262
263 if (size && chunk)
264 size &= ~(unsigned long long)(chunk - 1);
265 newsize = size * 2;
266 if (st && ! st->ss->validate_geometry(st, level, layout, raiddisks,
267 chunk, size*2, NULL, &newsize, verbose>=0))
268 return 1;
269 if (size == 0) {
270 size = newsize / 2;
271 if (size && verbose > 0)
272 fprintf(stderr, Name ": setting size to %lluK\n",
273 (unsigned long long)size);
274 }
275
276 /* now look at the subdevs */
277 info.array.active_disks = 0;
278 info.array.working_disks = 0;
279 dnum = 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;
288 missing_disks ++;
289 continue;
290 }
291 info.array.working_disks++;
292 if (dnum < raiddisks)
293 info.array.active_disks++;
294 if (st == NULL) {
295 struct createinfo *ci = conf_get_create_info();
296 if (ci)
297 st = ci->supertype;
298 }
299 if (st == NULL) {
300 /* Need to choose a default metadata, which is different
301 * depending on geometry of array.
302 */
303 int i;
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,
312 verbose > 0))
313 st = NULL;
314 }
315
316 if (!st) {
317 fprintf(stderr, Name ": device %s not suitable "
318 "for any style of array\n",
319 dname);
320 exit(2);
321 }
322 if (st->ss != &super0 ||
323 st->minor_version != 90)
324 did_default = 1;
325 } else {
326 if (do_default_layout)
327 layout = default_layout(st, level, verbose);
328 if (!st->ss->validate_geometry(st, level, layout,
329 raiddisks,
330 chunk, size*2, dname,
331 &freesize,
332 verbose >= 0)) {
333
334 fprintf(stderr,
335 Name ": %s is not suitable for "
336 "this array.\n",
337 dname);
338 fail = 1;
339 continue;
340 }
341 }
342
343 freesize /= 2; /* convert to K */
344 if (chunk) {
345 /* round to chunk size */
346 freesize = freesize & ~(chunk-1);
347 }
348
349 if (size && freesize < size) {
350 fprintf(stderr, Name ": %s is smaller than given size."
351 " %lluK < %lluK + metadata\n",
352 dname, freesize, size);
353 fail = 1;
354 continue;
355 }
356 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
357 maxdisc = dname;
358 maxsize = freesize;
359 }
360 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
361 mindisc = dname;
362 minsize = freesize;
363 }
364 if (runstop != 1 || verbose >= 0) {
365 int fd = open(dname, O_RDONLY);
366 if (fd <0 ) {
367 fprintf(stderr, Name ": Cannot open %s: %s\n",
368 dname, strerror(errno));
369 fail=1;
370 continue;
371 }
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);
382 else
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 &&
387 did_default &&
388 level == 1 &&
389 (warn & 1024) == 0) {
390 warn |= 1024;
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");
396 }
397 close(fd);
398 }
399 }
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);
404 return 1;
405 }
406 if (have_container)
407 info.array.working_disks = raiddisks;
408 if (fail) {
409 fprintf(stderr, Name ": create aborted\n");
410 return 1;
411 }
412 if (size == 0) {
413 if (mindisc == NULL && !have_container) {
414 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
415 return 1;
416 }
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,
422 raiddisks,
423 chunk, minsize*2,
424 NULL, NULL, 0)) {
425 fprintf(stderr, Name ": devices too large for RAID level %d\n", level);
426 return 1;
427 }
428 size = minsize;
429 if (verbose > 0)
430 fprintf(stderr, Name ": size set to %lluK\n", size);
431 }
432 }
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",
436 maxdisc, size);
437 warn = 1;
438 }
439
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",
444 st->ss->name);
445 warn = 1;
446 }
447
448 if (warn) {
449 if (runstop!= 1) {
450 if (!ask("Continue creating array? ")) {
451 fprintf(stderr, Name ": create aborted.\n");
452 return 1;
453 }
454 } else {
455 if (verbose > 0)
456 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
457 }
458 }
459
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?
463 */
464 if (st->ss->external == 0 &&
465 assume_clean==0 && force == 0 && first_missing >= raiddisks) {
466 switch ( level ) {
467 case 4:
468 case 5:
469 insert_point = raiddisks-1;
470 sparedisks++;
471 info.array.active_disks--;
472 missing_disks++;
473 break;
474 default:
475 break;
476 }
477 }
478 /* For raid6, if creating with 1 missing drive, make a good drive
479 * into a spare, else the create will fail
480 */
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)
486 insert_point--;
487 sparedisks ++;
488 info.array.active_disks--;
489 missing_disks++;
490 }
491
492 if (level <= 0 && first_missing < subdevs * 2) {
493 fprintf(stderr,
494 Name ": This level does not support missing devices\n");
495 return 1;
496 }
497
498 /* We need to create the device */
499 map_lock(&map);
500 mdfd = create_mddev(mddev, name, autof, LOCAL, chosen_name);
501 if (mdfd < 0)
502 return 1;
503 mddev = chosen_name;
504
505 vers = md_get_version(mdfd);
506 if (vers < 9000) {
507 fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n");
508 goto abort;
509 } else {
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");
516 goto abort;
517 }
518 }
519
520 /* Ok, lets try some ioctls */
521
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
527 */
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;
532
533 if ( ( (level == 4 || level == 5) &&
534 (insert_point < raiddisks || first_missing < raiddisks) )
535 ||
536 ( level == 6 && (insert_point < raiddisks
537 || second_missing < raiddisks))
538 ||
539 ( level <= 0 )
540 ||
541 assume_clean
542 ) {
543 info.array.state = 1; /* clean, but one+ drive will be missing*/
544 info.resync_start = MaxSector;
545 } else {
546 info.array.state = 0; /* not clean, but no errors */
547 info.resync_start = 0;
548 }
549 if (level == 10) {
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.
553 */
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);*/
557 } else
558 bitmapsize = (unsigned long long)size * 2;
559
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
563 * it is constant
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
567 * see above
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,
571 * including spares
572 * failed_disks is the number of disks marked failed
573 *
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.
578 */
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;
585
586 if (name == NULL || *name == 0) {
587 /* base name on mddev */
588 /* /dev/md0 -> 0
589 * /dev/md_d0 -> d0
590 * /dev/md/1 -> 1
591 * /dev/md/d1 -> d1
592 * /dev/md/home -> home
593 * /dev/mdhome -> home
594 */
595 /* FIXME compare this with rules in create_mddev */
596 name = strrchr(mddev, '/');
597 if (name) {
598 name++;
599 if (strncmp(name, "md_d", 4)==0 &&
600 strlen(name) > 4 &&
601 isdigit(name[4]) &&
602 (name-mddev) == 5 /* /dev/ */)
603 name += 3;
604 else if (strncmp(name, "md", 2)==0 &&
605 strlen(name) > 2 &&
606 isdigit(name[2]) &&
607 (name-mddev) == 5 /* /dev/ */)
608 name += 2;
609 }
610 }
611 if (!st->ss->init_super(st, &info.array, size, name, homehost, uuid))
612 goto abort;
613
614 total_slots = info.array.nr_disks;
615 sysfs_init(&info, mdfd, 0);
616 st->ss->getinfo_super(st, &info, NULL);
617
618 if (did_default && verbose >= 0) {
619 if (is_subarray(info.text_version)) {
620 int dnum = devname2devnum(info.text_version+1);
621 char *path;
622 int mdp = get_mdp_major();
623 struct mdinfo *mdi;
624 if (dnum > 0)
625 path = map_dev(MD_MAJOR, dnum, 1);
626 else
627 path = map_dev(mdp, (-1-dnum)<< 6, 1);
628
629 mdi = sysfs_read(-1, dnum, GET_VERSION);
630
631 fprintf(stderr, Name ": Creating array inside "
632 "%s container %s\n",
633 mdi?mdi->text_version:"managed", path);
634 sysfs_free(mdi);
635 } else
636 fprintf(stderr, Name ": Defaulting to version"
637 " %s metadata\n", info.text_version);
638 }
639
640 map_update(&map, fd2devnum(mdfd), info.text_version,
641 info.uuid, chosen_name);
642 map_unlock(&map);
643
644 if (bitmap_file && vers < 9003) {
645 major_num = BITMAP_MAJOR_HOSTENDIAN;
646 #ifdef __BIG_ENDIAN
647 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
648 " between different architectured. Consider upgrading the Linux kernel.\n");
649 #endif
650 }
651
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");
655 goto abort;
656 }
657 if (!st->ss->add_internal_bitmap) {
658 fprintf(stderr, Name ": internal bitmaps not supported with %s metadata\n",
659 st->ss->name);
660 goto abort;
661 }
662 if (!st->ss->add_internal_bitmap(st, &bitmap_chunk,
663 delay, write_behind,
664 bitmapsize, 1, major_num)) {
665 fprintf(stderr, Name ": Given bitmap chunk size not supported.\n");
666 goto abort;
667 }
668 bitmap_file = NULL;
669 }
670
671
672 sysfs_init(&info, mdfd, 0);
673
674 if (st->ss->external && st->container_dev != NoMdDev) {
675 /* member */
676
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,
682 * and then start it.
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.
686 *
687 * For now, fail if it is already running.
688 */
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");
693 goto abort;
694 }
695 if (mdmon_running(st->container_dev)) {
696 if (verbose)
697 fprintf(stderr, Name ": reusing mdmon "
698 "for %s.\n",
699 devnum2devname(st->container_dev));
700 st->update_tail = &st->updates;
701 } else
702 need_mdmon = 1;
703 }
704 rv = set_array_info(mdfd, st, &info);
705 if (rv) {
706 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
707 mddev, strerror(errno));
708 goto abort;
709 }
710
711 if (bitmap_file) {
712 int uuid[4];
713
714 st->ss->uuid_from_super(st, uuid);
715 if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk,
716 delay, write_behind,
717 bitmapsize,
718 major_num)) {
719 goto abort;
720 }
721 bitmap_fd = open(bitmap_file, O_RDWR);
722 if (bitmap_fd < 0) {
723 fprintf(stderr, Name ": weird: %s cannot be openned\n",
724 bitmap_file);
725 goto abort;
726 }
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));
730 goto abort;
731 }
732 }
733
734 infos = malloc(sizeof(*infos) * total_slots);
735
736 for (pass=1; pass <=2 ; pass++) {
737 struct mddev_dev *moved_disk = NULL; /* the disk that was moved out of the insert point */
738
739 for (dnum=0, dv = devlist ; dv ;
740 dv=(dv->next)?(dv->next):moved_disk, dnum++) {
741 int fd;
742 struct stat stb;
743 struct mdinfo *inf = &infos[dnum];
744
745 if (dnum >= total_slots)
746 abort();
747 if (dnum == insert_point) {
748 moved_disk = dv;
749 continue;
750 }
751 if (strcasecmp(dv->devname, "missing")==0)
752 continue;
753 if (have_container)
754 moved_disk = NULL;
755 if (have_container && dnum < info.array.raid_disks - 1)
756 /* repeatedly use the container */
757 moved_disk = dv;
758
759 switch(pass) {
760 case 1:
761 *inf = info;
762
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) |
767 (1<<MD_DISK_SYNC);
768 else
769 inf->disk.state = 0;
770
771 if (dv->writemostly == 1)
772 inf->disk.state |= (1<<MD_DISK_WRITEMOSTLY);
773
774 if (have_container)
775 fd = -1;
776 else {
777 if (st->ss->external &&
778 st->container_dev != NoMdDev)
779 fd = open(dv->devname, O_RDWR);
780 else
781 fd = open(dv->devname, O_RDWR|O_EXCL);
782
783 if (fd < 0) {
784 fprintf(stderr, Name ": failed to open %s "
785 "after earlier success - aborting\n",
786 dv->devname);
787 goto abort;
788 }
789 fstat(fd, &stb);
790 inf->disk.major = major(stb.st_rdev);
791 inf->disk.minor = minor(stb.st_rdev);
792 }
793 if (fd >= 0)
794 remove_partitions(fd);
795 if (st->ss->add_to_super(st, &inf->disk,
796 fd, dv->devname)) {
797 ioctl(mdfd, STOP_ARRAY, NULL);
798 goto abort;
799 }
800 st->ss->getinfo_super(st, inf, NULL);
801 safe_mode_delay = inf->safe_mode_delay;
802
803 if (have_container && verbose > 0)
804 fprintf(stderr, Name ": Using %s for device %d\n",
805 map_dev(inf->disk.major,
806 inf->disk.minor,
807 0), dnum);
808
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);
813 }
814 break;
815 case 2:
816 inf->errors = 0;
817 rv = 0;
818
819 rv = add_disk(mdfd, st, &info, inf);
820
821 if (rv) {
822 fprintf(stderr,
823 Name ": ADD_NEW_DISK for %s "
824 "failed: %s\n",
825 dv->devname, strerror(errno));
826 st->ss->free_super(st);
827 goto abort;
828 }
829 break;
830 }
831 if (!have_container &&
832 dv == moved_disk && dnum != insert_point) break;
833 }
834 if (pass == 1) {
835 struct mdinfo info_new;
836 struct map_ent *me = NULL;
837
838 /* check to see if the uuid has changed due to these
839 * metadata changes, and if so update the member array
840 * and container uuid. Note ->write_init_super clears
841 * the subarray cursor such that ->getinfo_super once
842 * again returns container info.
843 */
844 map_lock(&map);
845 st->ss->getinfo_super(st, &info_new, NULL);
846 if (st->ss->external && level != LEVEL_CONTAINER &&
847 !same_uuid(info_new.uuid, info.uuid, 0)) {
848 map_update(&map, fd2devnum(mdfd),
849 info_new.text_version,
850 info_new.uuid, chosen_name);
851 me = map_by_devnum(&map, st->container_dev);
852 }
853
854 st->ss->write_init_super(st);
855
856 /* update parent container uuid */
857 if (me) {
858 char *path = strdup(me->path);
859
860 st->ss->getinfo_super(st, &info_new, NULL);
861 map_update(&map, st->container_dev,
862 info_new.text_version,
863 info_new.uuid, path);
864 free(path);
865 }
866 map_unlock(&map);
867
868 flush_metadata_updates(st);
869 }
870 }
871 free(infos);
872 st->ss->free_super(st);
873
874 if (level == LEVEL_CONTAINER) {
875 /* No need to start. But we should signal udev to
876 * create links */
877 sysfs_uevent(&info, "change");
878 if (verbose >= 0)
879 fprintf(stderr, Name ": container %s prepared.\n", mddev);
880 wait_for(chosen_name, mdfd);
881 } else if (runstop == 1 || subdevs >= raiddisks) {
882 if (st->ss->external) {
883 int err;
884 switch(level) {
885 case LEVEL_LINEAR:
886 case LEVEL_MULTIPATH:
887 case 0:
888 err = sysfs_set_str(&info, NULL, "array_state",
889 "active");
890 need_mdmon = 0;
891 break;
892 default:
893 err = sysfs_set_str(&info, NULL, "array_state",
894 "readonly");
895 break;
896 }
897 sysfs_set_safemode(&info, safe_mode_delay);
898 if (err) {
899 fprintf(stderr, Name ": failed to"
900 " activate array.\n");
901 ioctl(mdfd, STOP_ARRAY, NULL);
902 goto abort;
903 }
904 } else {
905 /* param is not actually used */
906 mdu_param_t param;
907 if (ioctl(mdfd, RUN_ARRAY, &param)) {
908 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
909 strerror(errno));
910 ioctl(mdfd, STOP_ARRAY, NULL);
911 goto abort;
912 }
913 }
914 if (verbose >= 0)
915 fprintf(stderr, Name ": array %s started.\n", mddev);
916 if (st->ss->external && st->container_dev != NoMdDev) {
917 if (need_mdmon)
918 start_mdmon(st->container_dev);
919
920 ping_monitor(devnum2devname(st->container_dev));
921 close(container_fd);
922 }
923 wait_for(chosen_name, mdfd);
924 } else {
925 fprintf(stderr, Name ": not starting array - not enough devices.\n");
926 }
927 close(mdfd);
928 return 0;
929
930 abort:
931 map_lock(&map);
932 map_remove(&map, fd2devnum(mdfd));
933 map_unlock(&map);
934
935 if (mdfd >= 0)
936 close(mdfd);
937 return 1;
938 }