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