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