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