]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
Check all member devices in enough_fd
[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 int do_default_chunk = 0;
118 unsigned long safe_mode_delay = 0;
119 char chosen_name[1024];
120 struct map_ent *map = NULL;
121 unsigned long long newsize;
122
123 int major_num = BITMAP_MAJOR_HI;
124
125 memset(&info, 0, sizeof(info));
126 if (level == UnSet && st && st->ss->default_geometry)
127 st->ss->default_geometry(st, &level, NULL, NULL);
128 if (level == UnSet) {
129 fprintf(stderr,
130 Name ": a RAID level is needed to create an array.\n");
131 return 1;
132 }
133 if (raiddisks < 4 && level == 6) {
134 fprintf(stderr,
135 Name ": at least 4 raid-devices needed for level 6\n");
136 return 1;
137 }
138 if (raiddisks > 256 && level == 6) {
139 fprintf(stderr,
140 Name ": no more than 256 raid-devices supported for level 6\n");
141 return 1;
142 }
143 if (raiddisks < 2 && level >= 4) {
144 fprintf(stderr,
145 Name ": at least 2 raid-devices needed for level 4 or 5\n");
146 return 1;
147 }
148 if (level <= 0 && sparedisks) {
149 fprintf(stderr,
150 Name ": This level does not support spare devices\n");
151 return 1;
152 }
153
154 if (subdevs == 1 && strcmp(devlist->devname, "missing") != 0) {
155 /* If given a single device, it might be a container, and we can
156 * extract a device list from there
157 */
158 mdu_array_info_t inf;
159 int fd;
160
161 memset(&inf, 0, sizeof(inf));
162 fd = open(devlist->devname, O_RDONLY);
163 if (fd >= 0 &&
164 ioctl(fd, GET_ARRAY_INFO, &inf) == 0 &&
165 inf.raid_disks == 0) {
166 /* yep, looks like a container */
167 if (st) {
168 rv = st->ss->load_container(st, fd,
169 devlist->devname);
170 if (rv == 0)
171 have_container = 1;
172 } else {
173 st = super_by_fd(fd, NULL);
174 if (st && !(rv = st->ss->
175 load_container(st, fd,
176 devlist->devname)))
177 have_container = 1;
178 else
179 st = NULL;
180 }
181 if (have_container) {
182 subdevs = raiddisks;
183 first_missing = subdevs * 2;
184 second_missing = subdevs * 2;
185 insert_point = subdevs * 2;
186 }
187 }
188 if (fd >= 0)
189 close(fd);
190 }
191 if (st && st->ss->external && sparedisks) {
192 fprintf(stderr,
193 Name ": This metadata type does not support "
194 "spare disks at create time\n");
195 return 1;
196 }
197 if (subdevs > raiddisks+sparedisks) {
198 fprintf(stderr, Name ": You have listed more devices (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks);
199 return 1;
200 }
201 if (!have_container && subdevs < raiddisks+sparedisks) {
202 fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n");
203 return 1;
204 }
205 if (bitmap_file && level <= 0) {
206 fprintf(stderr, Name ": bitmaps not meaningful with level %s\n",
207 map_num(pers, level)?:"given");
208 return 1;
209 }
210
211 /* now set some defaults */
212
213
214 if (layout == UnSet) {
215 do_default_layout = 1;
216 layout = default_layout(st, level, verbose);
217 }
218
219 if (level == 10)
220 /* check layout fits in array*/
221 if ((layout&255) * ((layout>>8)&255) > raiddisks) {
222 fprintf(stderr, Name ": that layout requires at least %d devices\n",
223 (layout&255) * ((layout>>8)&255));
224 return 1;
225 }
226
227 switch(level) {
228 case 4:
229 case 5:
230 case 10:
231 case 6:
232 case 0:
233 if (chunk == 0 || chunk == UnSet) {
234 chunk = UnSet;
235 do_default_chunk = 1;
236 /* chunk will be set later */
237 }
238 break;
239 case LEVEL_LINEAR:
240 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
241 if (get_linux_version() < 2006016 && chunk == 0) {
242 chunk = 64;
243 if (verbose > 0)
244 fprintf(stderr, Name ": chunk size defaults to 64K\n");
245 }
246 break;
247 case 1:
248 case LEVEL_FAULTY:
249 case LEVEL_MULTIPATH:
250 case LEVEL_CONTAINER:
251 if (chunk) {
252 chunk = 0;
253 if (verbose > 0)
254 fprintf(stderr, Name ": chunk size ignored for this level\n");
255 }
256 break;
257 default:
258 fprintf(stderr, Name ": unknown level %d\n", level);
259 return 1;
260 }
261
262 if (size && chunk && chunk != UnSet)
263 size &= ~(unsigned long long)(chunk - 1);
264 newsize = size * 2;
265 if (st && ! st->ss->validate_geometry(st, level, layout, raiddisks,
266 &chunk, size*2, NULL, &newsize, verbose>=0))
267 return 1;
268
269 if (chunk && chunk != UnSet) {
270 newsize &= ~(unsigned long long)(chunk*2 - 1);
271 if (do_default_chunk) {
272 /* default chunk was just set */
273 if (verbose > 0)
274 fprintf(stderr, Name ": chunk size "
275 "defaults to %dK\n", chunk);
276 size &= ~(unsigned long long)(chunk - 1);
277 do_default_chunk = 0;
278 }
279 }
280
281 if (size == 0) {
282 size = newsize / 2;
283 if (size && verbose > 0)
284 fprintf(stderr, Name ": setting size to %lluK\n",
285 (unsigned long long)size);
286 }
287
288 /* now look at the subdevs */
289 info.array.active_disks = 0;
290 info.array.working_disks = 0;
291 dnum = 0;
292 for (dv=devlist; dv && !have_container; dv=dv->next, dnum++) {
293 char *dname = dv->devname;
294 unsigned long long freesize;
295 int dfd;
296
297 if (strcasecmp(dname, "missing")==0) {
298 if (first_missing > dnum)
299 first_missing = dnum;
300 if (second_missing > dnum && dnum > first_missing)
301 second_missing = dnum;
302 missing_disks ++;
303 continue;
304 }
305 dfd = open(dname, O_RDONLY|O_EXCL);
306 if (dfd < 0) {
307 fprintf(stderr, Name ": cannot open %s: %s\n",
308 dname, strerror(errno));
309 exit(2);
310 }
311 if (fstat(dfd, &stb) != 0 ||
312 (stb.st_mode & S_IFMT) != S_IFBLK) {
313 close(dfd);
314 fprintf(stderr, Name ": %s is not a block device\n",
315 dname);
316 exit(2);
317 }
318 close(dfd);
319 info.array.working_disks++;
320 if (dnum < raiddisks)
321 info.array.active_disks++;
322 if (st == NULL) {
323 struct createinfo *ci = conf_get_create_info();
324 if (ci)
325 st = ci->supertype;
326 }
327 if (st == NULL) {
328 /* Need to choose a default metadata, which is different
329 * depending on geometry of array.
330 */
331 int i;
332 char *name = "default";
333 for(i=0; !st && superlist[i]; i++) {
334 st = superlist[i]->match_metadata_desc(name);
335 if (do_default_layout)
336 layout = default_layout(st, level, verbose);
337 if (st && !st->ss->validate_geometry
338 (st, level, layout, raiddisks,
339 &chunk, size*2, dname, &freesize,
340 verbose > 0)) {
341 free(st);
342 st = NULL;
343 chunk = do_default_chunk ? UnSet : chunk;
344 }
345 }
346
347 if (!st) {
348 fprintf(stderr, Name ": device %s not suitable "
349 "for any style of array\n",
350 dname);
351 exit(2);
352 }
353 if (st->ss != &super0 ||
354 st->minor_version != 90)
355 did_default = 1;
356 } else {
357 if (do_default_layout)
358 layout = default_layout(st, level, verbose);
359 if (!st->ss->validate_geometry(st, level, layout,
360 raiddisks,
361 &chunk, size*2, dname,
362 &freesize,
363 verbose >= 0)) {
364
365 fprintf(stderr,
366 Name ": %s is not suitable for "
367 "this array.\n",
368 dname);
369 fail = 1;
370 continue;
371 }
372 }
373
374 freesize /= 2; /* convert to K */
375 if (chunk && chunk != UnSet) {
376 /* round to chunk size */
377 freesize = freesize & ~(chunk-1);
378 if (do_default_chunk) {
379 /* default chunk was just set */
380 if (verbose > 0)
381 fprintf(stderr, Name ": chunk size "
382 "defaults to %dK\n", chunk);
383 size &= ~(unsigned long long)(chunk - 1);
384 do_default_chunk = 0;
385 }
386 }
387
388 if (size && freesize < size) {
389 fprintf(stderr, Name ": %s is smaller than given size."
390 " %lluK < %lluK + metadata\n",
391 dname, freesize, size);
392 fail = 1;
393 continue;
394 }
395 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
396 maxdisc = dname;
397 maxsize = freesize;
398 }
399 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
400 mindisc = dname;
401 minsize = freesize;
402 }
403 if (runstop != 1 || verbose >= 0) {
404 int fd = open(dname, O_RDONLY);
405 if (fd <0 ) {
406 fprintf(stderr, Name ": Cannot open %s: %s\n",
407 dname, strerror(errno));
408 fail=1;
409 continue;
410 }
411 warn |= check_ext2(fd, dname);
412 warn |= check_reiser(fd, dname);
413 warn |= check_raid(fd, dname);
414 if (strcmp(st->ss->name, "1.x") == 0 &&
415 st->minor_version >= 1)
416 /* metadata at front */
417 warn |= check_partitions(fd, dname, 0, 0);
418 else if (level == 1 || level == LEVEL_CONTAINER
419 || (level == 0 && raiddisks == 1))
420 /* partitions could be meaningful */
421 warn |= check_partitions(fd, dname, freesize*2, size*2);
422 else
423 /* partitions cannot be meaningful */
424 warn |= check_partitions(fd, dname, 0, 0);
425 if (strcmp(st->ss->name, "1.x") == 0 &&
426 st->minor_version >= 1 &&
427 did_default &&
428 level == 1 &&
429 (warn & 1024) == 0) {
430 warn |= 1024;
431 fprintf(stderr, Name ": Note: this array has metadata at the start and\n"
432 " may not be suitable as a boot device. If you plan to\n"
433 " store '/boot' on this device please ensure that\n"
434 " your boot-loader understands md/v1.x metadata, or use\n"
435 " --metadata=0.90\n");
436 }
437 close(fd);
438 }
439 }
440 if (raiddisks + sparedisks > st->max_devs) {
441 fprintf(stderr, Name ": Too many devices:"
442 " %s metadata only supports %d\n",
443 st->ss->name, st->max_devs);
444 return 1;
445 }
446 if (have_container)
447 info.array.working_disks = raiddisks;
448 if (fail) {
449 fprintf(stderr, Name ": create aborted\n");
450 return 1;
451 }
452 if (size == 0) {
453 if (mindisc == NULL && !have_container) {
454 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
455 return 1;
456 }
457 if (level > 0 || level == LEVEL_MULTIPATH
458 || level == LEVEL_FAULTY
459 || st->ss->external ) {
460 /* size is meaningful */
461 if (!st->ss->validate_geometry(st, level, layout,
462 raiddisks,
463 &chunk, minsize*2,
464 NULL, NULL, 0)) {
465 fprintf(stderr, Name ": devices too large for RAID level %d\n", level);
466 return 1;
467 }
468 size = minsize;
469 if (verbose > 0)
470 fprintf(stderr, Name ": size set to %lluK\n", size);
471 }
472 }
473 if (!have_container && level > 0 && ((maxsize-size)*100 > maxsize)) {
474 if (runstop != 1 || verbose >= 0)
475 fprintf(stderr, Name ": largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
476 maxdisc, size);
477 warn = 1;
478 }
479
480 if (st->ss->detail_platform && st->ss->detail_platform(0, 1) != 0) {
481 if (runstop != 1 || verbose >= 0)
482 fprintf(stderr, Name ": %s unable to enumerate platform support\n"
483 " array may not be compatible with hardware/firmware\n",
484 st->ss->name);
485 warn = 1;
486 }
487
488 if (warn) {
489 if (runstop!= 1) {
490 if (!ask("Continue creating array? ")) {
491 fprintf(stderr, Name ": create aborted.\n");
492 return 1;
493 }
494 } else {
495 if (verbose > 0)
496 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
497 }
498 }
499
500 /* If this is raid4/5, we want to configure the last active slot
501 * as missing, so that a reconstruct happens (faster than re-parity)
502 * FIX: Can we do this for raid6 as well?
503 */
504 if (st->ss->external == 0 &&
505 assume_clean==0 && force == 0 && first_missing >= raiddisks) {
506 switch ( level ) {
507 case 4:
508 case 5:
509 insert_point = raiddisks-1;
510 sparedisks++;
511 info.array.active_disks--;
512 missing_disks++;
513 break;
514 default:
515 break;
516 }
517 }
518 /* For raid6, if creating with 1 missing drive, make a good drive
519 * into a spare, else the create will fail
520 */
521 if (assume_clean == 0 && force == 0 && first_missing < raiddisks &&
522 st->ss->external == 0 &&
523 second_missing >= raiddisks && level == 6) {
524 insert_point = raiddisks - 1;
525 if (insert_point == first_missing)
526 insert_point--;
527 sparedisks ++;
528 info.array.active_disks--;
529 missing_disks++;
530 }
531
532 if (level <= 0 && first_missing < subdevs * 2) {
533 fprintf(stderr,
534 Name ": This level does not support missing devices\n");
535 return 1;
536 }
537
538 /* We need to create the device */
539 map_lock(&map);
540 mdfd = create_mddev(mddev, name, autof, LOCAL, chosen_name);
541 if (mdfd < 0)
542 return 1;
543 mddev = chosen_name;
544
545 vers = md_get_version(mdfd);
546 if (vers < 9000) {
547 fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n");
548 goto abort;
549 } else {
550 mdu_array_info_t inf;
551 memset(&inf, 0, sizeof(inf));
552 ioctl(mdfd, GET_ARRAY_INFO, &inf);
553 if (inf.working_disks != 0) {
554 fprintf(stderr, Name ": another array by this name"
555 " is already running.\n");
556 goto abort;
557 }
558 }
559
560 /* Ok, lets try some ioctls */
561
562 info.array.level = level;
563 info.array.size = size;
564 info.array.raid_disks = raiddisks;
565 /* The kernel should *know* what md_minor we are dealing
566 * with, but it chooses to trust me instead. Sigh
567 */
568 info.array.md_minor = 0;
569 if (fstat(mdfd, &stb)==0)
570 info.array.md_minor = minor(stb.st_rdev);
571 info.array.not_persistent = 0;
572
573 if ( ( (level == 4 || level == 5) &&
574 (insert_point < raiddisks || first_missing < raiddisks) )
575 ||
576 ( level == 6 && (insert_point < raiddisks
577 || second_missing < raiddisks))
578 ||
579 ( level <= 0 )
580 ||
581 assume_clean
582 ) {
583 info.array.state = 1; /* clean, but one+ drive will be missing*/
584 info.resync_start = MaxSector;
585 } else {
586 info.array.state = 0; /* not clean, but no errors */
587 info.resync_start = 0;
588 }
589 if (level == 10) {
590 /* for raid10, the bitmap size is the capacity of the array,
591 * which is array.size * raid_disks / ncopies;
592 * .. but convert to sectors.
593 */
594 int ncopies = ((layout>>8) & 255) * (layout & 255);
595 bitmapsize = (unsigned long long)size * raiddisks / ncopies * 2;
596 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
597 } else
598 bitmapsize = (unsigned long long)size * 2;
599
600 /* There is lots of redundancy in these disk counts,
601 * raid_disks is the most meaningful value
602 * it describes the geometry of the array
603 * it is constant
604 * nr_disks is total number of used slots.
605 * it should be raid_disks+spare_disks
606 * spare_disks is the number of extra disks present
607 * see above
608 * active_disks is the number of working disks in
609 * active slots. (With raid_disks)
610 * working_disks is the total number of working disks,
611 * including spares
612 * failed_disks is the number of disks marked failed
613 *
614 * Ideally, the kernel would keep these (except raid_disks)
615 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
616 * So for now, we assume that all raid and spare
617 * devices will be given.
618 */
619 info.array.spare_disks=sparedisks;
620 info.array.failed_disks=missing_disks;
621 info.array.nr_disks = info.array.working_disks
622 + info.array.failed_disks;
623 info.array.layout = layout;
624 info.array.chunk_size = chunk*1024;
625
626 if (name == NULL || *name == 0) {
627 /* base name on mddev */
628 /* /dev/md0 -> 0
629 * /dev/md_d0 -> d0
630 * /dev/md/1 -> 1
631 * /dev/md/d1 -> d1
632 * /dev/md/home -> home
633 * /dev/mdhome -> home
634 */
635 /* FIXME compare this with rules in create_mddev */
636 name = strrchr(mddev, '/');
637 if (name) {
638 name++;
639 if (strncmp(name, "md_d", 4)==0 &&
640 strlen(name) > 4 &&
641 isdigit(name[4]) &&
642 (name-mddev) == 5 /* /dev/ */)
643 name += 3;
644 else if (strncmp(name, "md", 2)==0 &&
645 strlen(name) > 2 &&
646 isdigit(name[2]) &&
647 (name-mddev) == 5 /* /dev/ */)
648 name += 2;
649 }
650 }
651 if (!st->ss->init_super(st, &info.array, size, name, homehost, uuid))
652 goto abort;
653
654 total_slots = info.array.nr_disks;
655 sysfs_init(&info, mdfd, 0);
656 st->ss->getinfo_super(st, &info, NULL);
657
658 if (did_default && verbose >= 0) {
659 if (is_subarray(info.text_version)) {
660 int dnum = devname2devnum(info.text_version+1);
661 char *path;
662 int mdp = get_mdp_major();
663 struct mdinfo *mdi;
664 if (dnum > 0)
665 path = map_dev(MD_MAJOR, dnum, 1);
666 else
667 path = map_dev(mdp, (-1-dnum)<< 6, 1);
668
669 mdi = sysfs_read(-1, dnum, GET_VERSION);
670
671 fprintf(stderr, Name ": Creating array inside "
672 "%s container %s\n",
673 mdi?mdi->text_version:"managed", path);
674 sysfs_free(mdi);
675 } else
676 fprintf(stderr, Name ": Defaulting to version"
677 " %s metadata\n", info.text_version);
678 }
679
680 map_update(&map, fd2devnum(mdfd), info.text_version,
681 info.uuid, chosen_name);
682 map_unlock(&map);
683
684 if (bitmap_file && vers < 9003) {
685 major_num = BITMAP_MAJOR_HOSTENDIAN;
686 #ifdef __BIG_ENDIAN
687 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
688 " between different architectured. Consider upgrading the Linux kernel.\n");
689 #endif
690 }
691
692 if (bitmap_file && strcmp(bitmap_file, "internal")==0) {
693 if ((vers%100) < 2) {
694 fprintf(stderr, Name ": internal bitmaps not supported by this kernel.\n");
695 goto abort;
696 }
697 if (!st->ss->add_internal_bitmap) {
698 fprintf(stderr, Name ": internal bitmaps not supported with %s metadata\n",
699 st->ss->name);
700 goto abort;
701 }
702 if (!st->ss->add_internal_bitmap(st, &bitmap_chunk,
703 delay, write_behind,
704 bitmapsize, 1, major_num)) {
705 fprintf(stderr, Name ": Given bitmap chunk size not supported.\n");
706 goto abort;
707 }
708 bitmap_file = NULL;
709 }
710
711
712 sysfs_init(&info, mdfd, 0);
713
714 if (st->ss->external && st->container_dev != NoMdDev) {
715 /* member */
716
717 /* When creating a member, we need to be careful
718 * to negotiate with mdmon properly.
719 * If it is already running, we cannot write to
720 * the devices and must ask it to do that part.
721 * If it isn't running, we write to the devices,
722 * and then start it.
723 * We hold an exclusive open on the container
724 * device to make sure mdmon doesn't exit after
725 * we checked that it is running.
726 *
727 * For now, fail if it is already running.
728 */
729 container_fd = open_dev_excl(st->container_dev);
730 if (container_fd < 0) {
731 fprintf(stderr, Name ": Cannot get exclusive "
732 "open on container - weird.\n");
733 goto abort;
734 }
735 if (mdmon_running(st->container_dev)) {
736 if (verbose)
737 fprintf(stderr, Name ": reusing mdmon "
738 "for %s.\n",
739 devnum2devname(st->container_dev));
740 st->update_tail = &st->updates;
741 } else
742 need_mdmon = 1;
743 }
744 rv = set_array_info(mdfd, st, &info);
745 if (rv) {
746 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
747 mddev, strerror(errno));
748 goto abort;
749 }
750
751 if (bitmap_file) {
752 int uuid[4];
753
754 st->ss->uuid_from_super(st, uuid);
755 if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk,
756 delay, write_behind,
757 bitmapsize,
758 major_num)) {
759 goto abort;
760 }
761 bitmap_fd = open(bitmap_file, O_RDWR);
762 if (bitmap_fd < 0) {
763 fprintf(stderr, Name ": weird: %s cannot be openned\n",
764 bitmap_file);
765 goto abort;
766 }
767 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
768 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
769 mddev, strerror(errno));
770 goto abort;
771 }
772 }
773
774 infos = malloc(sizeof(*infos) * total_slots);
775
776 for (pass=1; pass <=2 ; pass++) {
777 struct mddev_dev *moved_disk = NULL; /* the disk that was moved out of the insert point */
778
779 for (dnum=0, dv = devlist ; dv ;
780 dv=(dv->next)?(dv->next):moved_disk, dnum++) {
781 int fd;
782 struct stat stb;
783 struct mdinfo *inf = &infos[dnum];
784
785 if (dnum >= total_slots)
786 abort();
787 if (dnum == insert_point) {
788 moved_disk = dv;
789 continue;
790 }
791 if (strcasecmp(dv->devname, "missing")==0)
792 continue;
793 if (have_container)
794 moved_disk = NULL;
795 if (have_container && dnum < info.array.raid_disks - 1)
796 /* repeatedly use the container */
797 moved_disk = dv;
798
799 switch(pass) {
800 case 1:
801 *inf = info;
802
803 inf->disk.number = dnum;
804 inf->disk.raid_disk = dnum;
805 if (inf->disk.raid_disk < raiddisks)
806 inf->disk.state = (1<<MD_DISK_ACTIVE) |
807 (1<<MD_DISK_SYNC);
808 else
809 inf->disk.state = 0;
810
811 if (dv->writemostly == 1)
812 inf->disk.state |= (1<<MD_DISK_WRITEMOSTLY);
813
814 if (have_container)
815 fd = -1;
816 else {
817 if (st->ss->external &&
818 st->container_dev != NoMdDev)
819 fd = open(dv->devname, O_RDWR);
820 else
821 fd = open(dv->devname, O_RDWR|O_EXCL);
822
823 if (fd < 0) {
824 fprintf(stderr, Name ": failed to open %s "
825 "after earlier success - aborting\n",
826 dv->devname);
827 goto abort;
828 }
829 fstat(fd, &stb);
830 inf->disk.major = major(stb.st_rdev);
831 inf->disk.minor = minor(stb.st_rdev);
832 }
833 if (fd >= 0)
834 remove_partitions(fd);
835 if (st->ss->add_to_super(st, &inf->disk,
836 fd, dv->devname)) {
837 ioctl(mdfd, STOP_ARRAY, NULL);
838 goto abort;
839 }
840 st->ss->getinfo_super(st, inf, NULL);
841 safe_mode_delay = inf->safe_mode_delay;
842
843 if (have_container && verbose > 0)
844 fprintf(stderr, Name ": Using %s for device %d\n",
845 map_dev(inf->disk.major,
846 inf->disk.minor,
847 0), dnum);
848
849 if (!have_container) {
850 /* getinfo_super might have lost these ... */
851 inf->disk.major = major(stb.st_rdev);
852 inf->disk.minor = minor(stb.st_rdev);
853 }
854 break;
855 case 2:
856 inf->errors = 0;
857 rv = 0;
858
859 rv = add_disk(mdfd, st, &info, inf);
860
861 if (rv) {
862 fprintf(stderr,
863 Name ": ADD_NEW_DISK for %s "
864 "failed: %s\n",
865 dv->devname, strerror(errno));
866 goto abort;
867 }
868 break;
869 }
870 if (!have_container &&
871 dv == moved_disk && dnum != insert_point) break;
872 }
873 if (pass == 1) {
874 struct mdinfo info_new;
875 struct map_ent *me = NULL;
876
877 /* check to see if the uuid has changed due to these
878 * metadata changes, and if so update the member array
879 * and container uuid. Note ->write_init_super clears
880 * the subarray cursor such that ->getinfo_super once
881 * again returns container info.
882 */
883 map_lock(&map);
884 st->ss->getinfo_super(st, &info_new, NULL);
885 if (st->ss->external && level != LEVEL_CONTAINER &&
886 !same_uuid(info_new.uuid, info.uuid, 0)) {
887 map_update(&map, fd2devnum(mdfd),
888 info_new.text_version,
889 info_new.uuid, chosen_name);
890 me = map_by_devnum(&map, st->container_dev);
891 }
892
893 if (st->ss->write_init_super(st)) {
894 fprintf(stderr,
895 Name ": Failed to write metadata to %s\n",
896 dv->devname);
897 st->ss->free_super(st);
898 goto abort;
899 }
900
901 /* update parent container uuid */
902 if (me) {
903 char *path = strdup(me->path);
904
905 st->ss->getinfo_super(st, &info_new, NULL);
906 map_update(&map, st->container_dev,
907 info_new.text_version,
908 info_new.uuid, path);
909 free(path);
910 }
911 map_unlock(&map);
912
913 flush_metadata_updates(st);
914 st->ss->free_super(st);
915 }
916 }
917 free(infos);
918
919 if (level == LEVEL_CONTAINER) {
920 /* No need to start. But we should signal udev to
921 * create links */
922 sysfs_uevent(&info, "change");
923 if (verbose >= 0)
924 fprintf(stderr, Name ": container %s prepared.\n", mddev);
925 wait_for(chosen_name, mdfd);
926 } else if (runstop == 1 || subdevs >= raiddisks) {
927 if (st->ss->external) {
928 int err;
929 switch(level) {
930 case LEVEL_LINEAR:
931 case LEVEL_MULTIPATH:
932 case 0:
933 err = sysfs_set_str(&info, NULL, "array_state",
934 "active");
935 need_mdmon = 0;
936 break;
937 default:
938 err = sysfs_set_str(&info, NULL, "array_state",
939 "readonly");
940 break;
941 }
942 sysfs_set_safemode(&info, safe_mode_delay);
943 if (err) {
944 fprintf(stderr, Name ": failed to"
945 " activate array.\n");
946 ioctl(mdfd, STOP_ARRAY, NULL);
947 goto abort;
948 }
949 } else {
950 /* param is not actually used */
951 mdu_param_t param;
952 if (ioctl(mdfd, RUN_ARRAY, &param)) {
953 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
954 strerror(errno));
955 if (info.array.chunk_size & (info.array.chunk_size-1)) {
956 fprintf(stderr, " : Problem may be that "
957 "chunk size is not a power of 2\n");
958 }
959 ioctl(mdfd, STOP_ARRAY, NULL);
960 goto abort;
961 }
962 }
963 if (verbose >= 0)
964 fprintf(stderr, Name ": array %s started.\n", mddev);
965 if (st->ss->external && st->container_dev != NoMdDev) {
966 if (need_mdmon)
967 start_mdmon(st->container_dev);
968
969 ping_monitor_by_id(st->container_dev);
970 close(container_fd);
971 }
972 wait_for(chosen_name, mdfd);
973 } else {
974 fprintf(stderr, Name ": not starting array - not enough devices.\n");
975 }
976 close(mdfd);
977 return 0;
978
979 abort:
980 map_lock(&map);
981 map_remove(&map, fd2devnum(mdfd));
982 map_unlock(&map);
983
984 if (mdfd >= 0)
985 close(mdfd);
986 return 1;
987 }