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