]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Create.c
util: make env checking more generic
[thirdparty/mdadm.git] / Create.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
4f589ad0 4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
64c4757e
NB
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@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
9a9dab36 30#include "mdadm.h"
682c7051
NB
31#include "md_u.h"
32#include "md_p.h"
60248f74 33#include <ctype.h>
64c4757e 34
7f91af49 35int Create(struct supertype *st, char *mddev,
5dd497ee 36 int chunk, int level, int layout, unsigned long long size, int raiddisks, int sparedisks,
3d3dd91e 37 char *name, char *homehost, int *uuid,
cd29a5c8 38 int subdevs, mddev_dev_t devlist,
47d79ef8 39 int runstop, int verbose, int force, int assume_clean,
7f91af49 40 char *bitmap_file, int bitmap_chunk, int write_behind, int delay, int autof)
64c4757e 41{
682c7051
NB
42 /*
43 * Create a new raid array.
44 *
45 * First check that necessary details are available
46 * (i.e. level, raid-disks)
47 *
48 * Then check each disk to see what might be on it
49 * and report anything interesting.
50 *
51 * If anything looks odd, and runstop not set,
52 * abort.
53 *
54 * SET_ARRAY_INFO and ADD_NEW_DISK, and
82d9eba6 55 * if runstop==run, or raiddisks disks were used,
682c7051
NB
56 * RUN_ARRAY
57 */
7f91af49 58 int mdfd;
b5e64645 59 unsigned long long minsize=0, maxsize=0;
cd29a5c8
NB
60 char *mindisc = NULL;
61 char *maxdisc = NULL;
c913b90e 62 int dnum;
cd29a5c8 63 mddev_dev_t dv;
682c7051 64 int fail=0, warn=0;
82b27616 65 struct stat stb;
82d9eba6 66 int first_missing = subdevs * 2;
6fb79233 67 int second_missing = subdevs * 2;
52826846 68 int missing_disks = 0;
82d9eba6 69 int insert_point = subdevs * 2; /* where to insert a missing drive */
d2ca6449 70 int total_slots;
4b1ac34b 71 int pass;
82d9eba6
NB
72 int vers;
73 int rv;
c82f047c 74 int bitmap_fd;
5f8097be 75 int have_container = 0;
0e600426 76 int container_fd = -1;
a931db9e 77 int need_mdmon = 0;
f9c25f1d 78 unsigned long long bitmapsize;
d2ca6449 79 struct mdinfo info, *infos;
b8ac1967 80 int did_default = 0;
8ed3e5e1 81 unsigned long safe_mode_delay = 0;
682c7051 82
943eafef 83 int major_num = BITMAP_MAJOR_HI;
dcec9ee5 84
06c7f68e 85 memset(&info, 0, sizeof(info));
29e766a5 86
a322f70c 87 if (level == UnSet) {
3cfe6247 88 /* "ddf" and "imsm" metadata only supports one level - should possibly
a322f70c
DW
89 * push this into metadata handler??
90 */
3cfe6247 91 if (st && (st->ss == &super_ddf || st->ss == &super_imsm))
a322f70c
DW
92 level = LEVEL_CONTAINER;
93 }
94
98c6faba 95 if (level == UnSet) {
682c7051
NB
96 fprintf(stderr,
97 Name ": a RAID level is needed to create an array.\n");
98 return 1;
99 }
98c6faba
NB
100 if (raiddisks < 4 && level == 6) {
101 fprintf(stderr,
102 Name ": at least 4 raid-devices needed for level 6\n");
103 return 1;
104 }
105 if (raiddisks > 256 && level == 6) {
106 fprintf(stderr,
107 Name ": no more than 256 raid-devices supported for level 6\n");
108 return 1;
109 }
52826846
NB
110 if (raiddisks < 2 && level >= 4) {
111 fprintf(stderr,
98c6faba 112 Name ": at least 2 raid-devices needed for level 4 or 5\n");
52826846
NB
113 return 1;
114 }
570510ba
NB
115 if (level <= 0 && sparedisks) {
116 fprintf(stderr,
117 Name ": This level does not support spare devices\n");
118 return 1;
119 }
5f8097be
NB
120
121 if (subdevs == 1 && strcmp(devlist->devname, "missing") != 0) {
122 /* If given a single device, it might be a container, and we can
123 * extract a device list from there
124 */
125 mdu_array_info_t inf;
126 int fd;
127
128 memset(&inf, 0, sizeof(inf));
37ea3936 129 fd = open(devlist->devname, O_RDONLY);
5f8097be
NB
130 if (fd >= 0 &&
131 ioctl(fd, GET_ARRAY_INFO, &inf) == 0 &&
132 inf.raid_disks == 0) {
133 /* yep, looks like a container */
134 if (st) {
135 rv = st->ss->load_super(st, fd,
136 devlist->devname);
137 if (rv == 0)
138 have_container = 1;
139 } else {
140 st = guess_super(fd);
141 if (st && !(rv = st->ss->
142 load_super(st, fd,
143 devlist->devname)))
144 have_container = 1;
145 else
146 st = NULL;
147 }
148 }
149 if (fd >= 0)
150 close(fd);
151 if (have_container) {
152 subdevs = 0;
153 devlist = NULL;
154 }
155 }
ffcfc735
N
156 if (st && st->ss->external && sparedisks) {
157 fprintf(stderr,
158 Name ": This metadata type does not support "
159 "spare disks are create time\n");
160 return 1;
161 }
682c7051 162 if (subdevs > raiddisks+sparedisks) {
b83d95f3 163 fprintf(stderr, Name ": You have listed more devices (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks);
52826846 164 return 1;
682c7051 165 }
5f8097be 166 if (!have_container && subdevs < raiddisks+sparedisks) {
52826846
NB
167 fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n");
168 return 1;
169 }
5b28bd56
NB
170 if (bitmap_file && level <= 0) {
171 fprintf(stderr, Name ": bitmaps not meaningful with level %s\n",
172 map_num(pers, level)?:"given");
173 return 1;
174 }
52826846 175
682c7051 176 /* now set some defaults */
98c6faba 177 if (layout == UnSet)
682c7051
NB
178 switch(level) {
179 default: /* no layout */
180 layout = 0;
181 break;
e5329c37
NB
182 case 10:
183 layout = 0x102; /* near=2, far=1 */
dab6685f 184 if (verbose > 0)
e5329c37
NB
185 fprintf(stderr,
186 Name ": layout defaults to n1\n");
187 break;
682c7051 188 case 5:
98c6faba 189 case 6:
682c7051 190 layout = map_name(r5layout, "default");
dab6685f 191 if (verbose > 0)
682c7051
NB
192 fprintf(stderr,
193 Name ": layout defaults to %s\n", map_num(r5layout, layout));
194 break;
b5e64645
NB
195 case LEVEL_FAULTY:
196 layout = map_name(faultylayout, "default");
197
dab6685f 198 if (verbose > 0)
b5e64645
NB
199 fprintf(stderr,
200 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
201 break;
682c7051
NB
202 }
203
e5329c37
NB
204 if (level == 10)
205 /* check layout fits in array*/
206 if ((layout&255) * ((layout>>8)&255) > raiddisks) {
207 fprintf(stderr, Name ": that layout requires at least %d devices\n",
208 (layout&255) * ((layout>>8)&255));
209 return 1;
210 }
211
aa88f531
NB
212 switch(level) {
213 case 4:
214 case 5:
e5329c37 215 case 10:
98c6faba 216 case 6:
aa88f531 217 case 0:
570510ba 218 case LEVEL_LINEAR: /* linear */
aa88f531
NB
219 if (chunk == 0) {
220 chunk = 64;
dab6685f 221 if (verbose > 0)
aa88f531
NB
222 fprintf(stderr, Name ": chunk size defaults to 64K\n");
223 }
224 break;
570510ba 225 case 1:
08e43379 226 case LEVEL_FAULTY:
570510ba 227 case LEVEL_MULTIPATH:
17f25ca6 228 case LEVEL_CONTAINER:
aa88f531
NB
229 if (chunk) {
230 chunk = 0;
dab6685f 231 if (verbose > 0)
aa88f531
NB
232 fprintf(stderr, Name ": chunk size ignored for this level\n");
233 }
234 break;
570510ba
NB
235 default:
236 fprintf(stderr, Name ": unknown level %d\n", level);
237 return 1;
682c7051
NB
238 }
239
17f25ca6 240 if (st && ! st->ss->validate_geometry(st, level, layout, raiddisks,
2c514b71 241 chunk, size, NULL, NULL, verbose>=0))
17f25ca6
NB
242 return 1;
243
682c7051 244 /* now look at the subdevs */
06c7f68e
NB
245 info.array.active_disks = 0;
246 info.array.working_disks = 0;
c913b90e
NB
247 dnum = 0;
248 for (dv=devlist; dv; dv=dv->next, dnum++) {
cd29a5c8 249 char *dname = dv->devname;
17f25ca6 250 unsigned long long freesize;
cd29a5c8 251 if (strcasecmp(dname, "missing")==0) {
c913b90e
NB
252 if (first_missing > dnum)
253 first_missing = dnum;
6fb79233
NB
254 if (second_missing > dnum && dnum > first_missing)
255 second_missing = dnum;
52826846
NB
256 missing_disks ++;
257 continue;
258 }
06c7f68e 259 info.array.working_disks++;
c913b90e 260 if (dnum < raiddisks)
06c7f68e 261 info.array.active_disks++;
058574b1 262 if (st == NULL) {
8aec876d 263 struct createinfo *ci = conf_get_create_info();
058574b1
NB
264 if (ci)
265 st = ci->supertype;
266 }
576d6d83
NB
267 if (st == NULL) {
268 /* Need to choose a default metadata, which is different
17f25ca6 269 * depending on geometry of array.
576d6d83
NB
270 */
271 int i;
272 char *name = "default";
17f25ca6 273 for(i=0; !st && superlist[i]; i++) {
576d6d83 274 st = superlist[i]->match_metadata_desc(name);
17f25ca6
NB
275 if (st && !st->ss->validate_geometry
276 (st, level, layout, raiddisks,
2c514b71
NB
277 chunk, size, dname, &freesize,
278 verbose > 0))
17f25ca6
NB
279 st = NULL;
280 }
576d6d83
NB
281
282 if (!st) {
17f25ca6
NB
283 fprintf(stderr, Name ": device %s not suitable "
284 "for any style of array\n",
285 dname);
576d6d83
NB
286 exit(2);
287 }
b8ac1967 288 if (st->ss != &super0 ||
576d6d83 289 st->minor_version != 90)
b8ac1967 290 did_default = 1;
17f25ca6
NB
291 } else {
292 if (!st->ss->validate_geometry(st, level, layout,
293 raiddisks,
294 chunk, size, dname,
2c514b71
NB
295 &freesize,
296 verbose > 0)) {
17f25ca6
NB
297
298 fprintf(stderr,
299 Name ": %s is not suitable for "
300 "this array.\n",
301 dname);
302 fail = 1;
303 continue;
304 }
682c7051 305 }
82d9eba6
NB
306
307 freesize /= 2; /* convert to K */
d2cd3ffc
NB
308 if (chunk) {
309 /* round to chunk size */
310 freesize = freesize & ~(chunk-1);
311 }
682c7051
NB
312
313 if (size && freesize < size) {
52826846 314 fprintf(stderr, Name ": %s is smaller that given size."
17f25ca6
NB
315 " %lluK < %lluK + metadata\n",
316 dname, freesize, size);
52826846 317 fail = 1;
52826846 318 continue;
682c7051 319 }
cd29a5c8
NB
320 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
321 maxdisc = dname;
52826846 322 maxsize = freesize;
682c7051 323 }
cd29a5c8
NB
324 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
325 mindisc = dname;
52826846 326 minsize = freesize;
682c7051 327 }
dab6685f 328 if (runstop != 1 || verbose >= 0) {
37ea3936 329 int fd = open(dname, O_RDONLY);
b6e63da4
NB
330 if (fd <0 ) {
331 fprintf(stderr, Name ": Cannot open %s: %s\n",
332 dname, strerror(errno));
333 fail=1;
334 continue;
335 }
dab6685f
NB
336 warn |= check_ext2(fd, dname);
337 warn |= check_reiser(fd, dname);
338 warn |= check_raid(fd, dname);
b6e63da4 339 close(fd);
dab6685f 340 }
682c7051
NB
341 }
342 if (fail) {
52826846
NB
343 fprintf(stderr, Name ": create aborted\n");
344 return 1;
682c7051
NB
345 }
346 if (size == 0) {
5f8097be 347 if (mindisc == NULL && !have_container) {
52826846
NB
348 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
349 return 1;
350 }
5f8097be
NB
351 if (level > 0 || level == LEVEL_MULTIPATH
352 || level == LEVEL_FAULTY
b8ac1967 353 || st->ss->external ) {
b5e64645 354 /* size is meaningful */
b8ac1967
NB
355 if (!st->ss->validate_geometry(st, level, layout,
356 raiddisks,
357 chunk, minsize,
2c514b71 358 NULL, NULL, 0)) {
aba69144 359 fprintf(stderr, Name ": devices too large for RAID level %d\n", level);
b5e64645
NB
360 return 1;
361 }
362 size = minsize;
dab6685f 363 if (verbose > 0)
5dd497ee 364 fprintf(stderr, Name ": size set to %lluK\n", size);
b5e64645 365 }
682c7051 366 }
b5e64645 367 if (level > 0 && ((maxsize-size)*100 > maxsize)) {
dab6685f 368 if (runstop != 1 || verbose >= 0)
5dd497ee 369 fprintf(stderr, Name ": largest drive (%s) exceed size (%lluK) by more than 1%%\n",
dab6685f 370 maxdisc, size);
52826846 371 warn = 1;
682c7051
NB
372 }
373
374 if (warn) {
52826846
NB
375 if (runstop!= 1) {
376 if (!ask("Continue creating array? ")) {
377 fprintf(stderr, Name ": create aborted.\n");
378 return 1;
379 }
380 } else {
dab6685f 381 if (verbose > 0)
52826846 382 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
682c7051 383 }
682c7051
NB
384 }
385
66f8bbbe 386 /* If this is raid4/5, we want to configure the last active slot
52826846 387 * as missing, so that a reconstruct happens (faster than re-parity)
98c6faba 388 * FIX: Can we do this for raid6 as well?
52826846 389 */
47d79ef8 390 if (assume_clean==0 && force == 0 && first_missing >= raiddisks) {
98c6faba 391 switch ( level ) {
66f8bbbe 392 case 4:
98c6faba
NB
393 case 5:
394 insert_point = raiddisks-1;
395 sparedisks++;
06c7f68e 396 info.array.active_disks--;
98c6faba
NB
397 missing_disks++;
398 break;
399 default:
400 break;
401 }
52826846 402 }
6fb79233
NB
403 /* For raid6, if creating with 1 missing drive, make a good drive
404 * into a spare, else the create will fail
405 */
406 if (assume_clean == 0 && force == 0 && first_missing < raiddisks &&
ffcfc735 407 st->ss->external == 0 &&
6fb79233
NB
408 second_missing >= raiddisks && level == 6) {
409 insert_point = raiddisks - 1;
410 if (insert_point == first_missing)
411 insert_point--;
412 sparedisks ++;
413 info.array.active_disks--;
414 missing_disks++;
415 }
570510ba 416
5f8097be 417 if (level <= 0 && first_missing < subdevs * 2) {
570510ba
NB
418 fprintf(stderr,
419 Name ": This level does not support missing devices\n");
420 return 1;
421 }
aba69144 422
7f91af49
N
423 /* We need to create the device */
424 mdfd = create_mddev(mddev, autof);
425 if (mdfd < 0)
426 return 1;
427
428 vers = md_get_version(mdfd);
429 if (vers < 9000) {
430 fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n");
431 goto abort;
432 } else {
433 mdu_array_info_t inf;
434 memset(&inf, 0, sizeof(inf));
435 ioctl(mdfd, GET_ARRAY_INFO, &inf);
436 if (inf.working_disks != 0) {
437 fprintf(stderr, Name ": another array by this name"
438 " is already running.\n");
439 goto abort;
440 }
441 }
682c7051
NB
442 /* Ok, lets try some ioctls */
443
06c7f68e
NB
444 info.array.level = level;
445 info.array.size = size;
446 info.array.raid_disks = raiddisks;
82b27616
NB
447 /* The kernel should *know* what md_minor we are dealing
448 * with, but it chooses to trust me instead. Sigh
449 */
06c7f68e 450 info.array.md_minor = 0;
82b27616 451 if (fstat(mdfd, &stb)==0)
06c7f68e
NB
452 info.array.md_minor = minor(stb.st_rdev);
453 info.array.not_persistent = 0;
6fb79233 454
66f8bbbe 455 if ( ( (level == 4 || level == 5) &&
b5e64645
NB
456 (insert_point < raiddisks || first_missing < raiddisks) )
457 ||
6fb79233
NB
458 ( level == 6 && (insert_point < raiddisks
459 || second_missing < raiddisks))
47d79ef8 460 ||
d024b0a7
NB
461 ( level <= 0 )
462 ||
47d79ef8 463 assume_clean
103f2410 464 ) {
06c7f68e 465 info.array.state = 1; /* clean, but one+ drive will be missing*/
103f2410
NB
466 info.resync_start = ~0ULL;
467 } else {
06c7f68e 468 info.array.state = 0; /* not clean, but no errors */
103f2410
NB
469 info.resync_start = 0;
470 }
f9c25f1d
NB
471 if (level == 10) {
472 /* for raid10, the bitmap size is the capacity of the array,
473 * which is array.size * raid_disks / ncopies;
474 * .. but convert to sectors.
475 */
702b557b 476 int ncopies = ((layout>>8) & 255) * (layout & 255);
5dd497ee
NB
477 bitmapsize = (unsigned long long)size * raiddisks / ncopies * 2;
478/* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
f9c25f1d 479 } else
5dd497ee 480 bitmapsize = (unsigned long long)size * 2;
f9c25f1d 481
82b27616
NB
482 /* There is lots of redundancy in these disk counts,
483 * raid_disks is the most meaningful value
484 * it describes the geometry of the array
485 * it is constant
486 * nr_disks is total number of used slots.
487 * it should be raid_disks+spare_disks
488 * spare_disks is the number of extra disks present
489 * see above
490 * active_disks is the number of working disks in
491 * active slots. (With raid_disks)
492 * working_disks is the total number of working disks,
493 * including spares
494 * failed_disks is the number of disks marked failed
495 *
496 * Ideally, the kernel would keep these (except raid_disks)
497 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
498 * So for now, we assume that all raid and spare
499 * devices will be given.
500 */
06c7f68e
NB
501 info.array.spare_disks=sparedisks;
502 info.array.failed_disks=missing_disks;
503 info.array.nr_disks = info.array.working_disks
504 + info.array.failed_disks;
505 info.array.layout = layout;
506 info.array.chunk_size = chunk*1024;
82d9eba6 507
b3b33eb5
NB
508 if (name == NULL || *name == 0) {
509 /* base name on mddev */
d1e80164
NB
510 /* /dev/md0 -> 0
511 * /dev/md_d0 -> d0
512 * /dev/md/1 -> 1
513 * /dev/md/d1 -> d1
514 * /dev/md/home -> home
515 * /dev/mdhome -> home
516 */
b3b33eb5
NB
517 name = strrchr(mddev, '/');
518 if (name) {
519 name++;
60248f74
NB
520 if (strncmp(name, "md_d", 4)==0 &&
521 strlen(name) > 4 &&
522 isdigit(name[4]) &&
b3b33eb5 523 (name-mddev) == 5 /* /dev/ */)
d1e80164 524 name += 3;
60248f74
NB
525 else if (strncmp(name, "md", 2)==0 &&
526 strlen(name) > 2 &&
527 isdigit(name[2]) &&
528 (name-mddev) == 5 /* /dev/ */)
b3b33eb5
NB
529 name += 2;
530 }
531 }
06c7f68e 532 if (!st->ss->init_super(st, &info.array, size, name, homehost, uuid))
7f91af49 533 goto abort;
682c7051 534
d2ca6449 535 total_slots = info.array.nr_disks;
f35f2525 536 sysfs_init(&info, mdfd, 0);
159c3a1a
NB
537 st->ss->getinfo_super(st, &info);
538
f7f1b6a1 539 if (did_default && verbose >= 0) {
3c558363 540 if (is_subarray(info.text_version)) {
f7f1b6a1
NB
541 int dnum = devname2devnum(info.text_version+1);
542 char *path;
543 int mdp = get_mdp_major();
544 struct mdinfo *mdi;
545 if (dnum > 0)
546 path = map_dev(MD_MAJOR, dnum, 1);
547 else
548 path = map_dev(mdp, (-1-dnum)<< 6, 1);
549
550 mdi = sysfs_read(-1, dnum, GET_VERSION);
551
552 fprintf(stderr, Name ": Creating array inside "
553 "%s container %s\n",
554 mdi?mdi->text_version:"managed", path);
555 sysfs_free(mdi);
556 } else
557 fprintf(stderr, Name ": Defaulting to version"
558 " %s metadata\n", info.text_version);
559 }
b8ac1967 560
dcec9ee5 561 if (bitmap_file && vers < 9003) {
943eafef 562 major_num = BITMAP_MAJOR_HOSTENDIAN;
dcec9ee5
NB
563#ifdef __BIG_ENDIAN
564 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
565 " between different architectured. Consider upgrading the Linux kernel.\n");
566#endif
567 }
568
55935d51
NB
569 if (bitmap_file && strcmp(bitmap_file, "internal")==0) {
570 if ((vers%100) < 2) {
571 fprintf(stderr, Name ": internal bitmaps not supported by this kernel.\n");
7f91af49 572 goto abort;
55935d51 573 }
3da92f27 574 if (!st->ss->add_internal_bitmap(st, &bitmap_chunk,
199171a2 575 delay, write_behind,
943eafef 576 bitmapsize, 1, major_num)) {
55935d51 577 fprintf(stderr, Name ": Given bitmap chunk size not supported.\n");
7f91af49 578 goto abort;
55935d51
NB
579 }
580 bitmap_file = NULL;
581 }
582
583
f35f2525 584 sysfs_init(&info, mdfd, 0);
55935d51 585
f35f2525
N
586 if (st->ss->external && st->subarray[0]) {
587 /* member */
588
589 /* When creating a member, we need to be careful
590 * to negotiate with mdmon properly.
591 * If it is already running, we cannot write to
592 * the devices and must ask it to do that part.
593 * If it isn't running, we write to the devices,
594 * and then start it.
595 * We hold an exclusive open on the container
596 * device to make sure mdmon doesn't exit after
597 * we checked that it is running.
598 *
599 * For now, fail if it is already running.
600 */
601 container_fd = open_dev_excl(st->container_dev);
602 if (container_fd < 0) {
603 fprintf(stderr, Name ": Cannot get exclusive "
604 "open on container - weird.\n");
7f91af49 605 goto abort;
d03373f1 606 }
f35f2525
N
607 if (mdmon_running(st->container_dev)) {
608 if (verbose)
609 fprintf(stderr, Name ": reusing mdmon "
610 "for %s.\n",
611 devnum2devname(st->container_dev));
612 st->update_tail = &st->updates;
613 } else
614 need_mdmon = 1;
615 }
616 rv = set_array_info(mdfd, st, &info);
82d9eba6 617 if (rv) {
f35f2525 618 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
52826846 619 mddev, strerror(errno));
7f91af49 620 goto abort;
682c7051 621 }
52826846 622
c82f047c
NB
623 if (bitmap_file) {
624 int uuid[4];
55935d51 625
3da92f27 626 st->ss->uuid_from_super(st, uuid);
dfd4d8ee 627 if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk,
dcec9ee5 628 delay, write_behind,
f9c25f1d 629 bitmapsize,
943eafef 630 major_num)) {
7f91af49 631 goto abort;
c82f047c
NB
632 }
633 bitmap_fd = open(bitmap_file, O_RDWR);
634 if (bitmap_fd < 0) {
635 fprintf(stderr, Name ": weird: %s cannot be openned\n",
dcec9ee5 636 bitmap_file);
7f91af49 637 goto abort;
c82f047c
NB
638 }
639 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
640 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
641 mddev, strerror(errno));
7f91af49 642 goto abort;
c82f047c
NB
643 }
644 }
645
d2ca6449 646 infos = malloc(sizeof(*infos) * total_slots);
4b1ac34b 647
4b1ac34b
NB
648 for (pass=1; pass <=2 ; pass++) {
649 mddev_dev_t moved_disk = NULL; /* the disk that was moved out of the insert point */
650
651 for (dnum=0, dv = devlist ; dv ;
652 dv=(dv->next)?(dv->next):moved_disk, dnum++) {
653 int fd;
654 struct stat stb;
d2ca6449 655 struct mdinfo *inf = &infos[dnum];
4b1ac34b 656
d2ca6449
NB
657 if (dnum >= total_slots)
658 abort();
4b1ac34b
NB
659 if (dnum == insert_point) {
660 moved_disk = dv;
52826846 661 }
4b1ac34b 662 if (dnum == insert_point ||
111d01fc
NB
663 strcasecmp(dv->devname, "missing")==0)
664 continue;
665
d2ca6449
NB
666 switch(pass) {
667 case 1:
668 *inf = info;
5f8097be 669
d2ca6449
NB
670 inf->disk.number = dnum;
671 inf->disk.raid_disk = dnum;
672 if (inf->disk.raid_disk < raiddisks)
673 inf->disk.state = (1<<MD_DISK_ACTIVE) |
674 (1<<MD_DISK_SYNC);
675 else
676 inf->disk.state = 0;
677
b01b06bd 678 if (dv->writemostly == 1)
d2ca6449
NB
679 inf->disk.state |= (1<<MD_DISK_WRITEMOSTLY);
680
681 if (st->ss->external && st->subarray[0])
37ea3936 682 fd = open(dv->devname, O_RDWR);
d2ca6449 683 else
37ea3936 684 fd = open(dv->devname, O_RDWR|O_EXCL);
d2ca6449
NB
685
686 if (fd < 0) {
687 fprintf(stderr, Name ": failed to open %s "
688 "after earlier success - aborting\n",
689 dv->devname);
7f91af49 690 goto abort;
d2ca6449
NB
691 }
692 fstat(fd, &stb);
693 inf->disk.major = major(stb.st_rdev);
694 inf->disk.minor = minor(stb.st_rdev);
111d01fc 695
111d01fc 696 remove_partitions(fd);
d2ca6449 697 st->ss->add_to_super(st, &inf->disk,
111d01fc 698 fd, dv->devname);
d2ca6449 699 st->ss->getinfo_super(st, inf);
8ed3e5e1 700 safe_mode_delay = inf->safe_mode_delay;
d2ca6449
NB
701
702 /* getinfo_super might have lost these ... */
703 inf->disk.major = major(stb.st_rdev);
704 inf->disk.minor = minor(stb.st_rdev);
4b1ac34b
NB
705 break;
706 case 2:
d2ca6449 707 inf->errors = 0;
2503d23b 708 rv = 0;
4b1ac34b 709
f35f2525 710 rv = add_disk(mdfd, st, &info, inf);
d2ca6449 711
2503d23b 712 if (rv) {
d03373f1
NB
713 fprintf(stderr,
714 Name ": ADD_NEW_DISK for %s "
715 "failed: %s\n",
4b1ac34b 716 dv->devname, strerror(errno));
3da92f27 717 st->ss->free_super(st);
7f91af49 718 goto abort;
4b1ac34b 719 }
4b1ac34b
NB
720 break;
721 }
722 if (dv == moved_disk && dnum != insert_point) break;
52826846 723 }
edd8d13c 724 if (pass == 1) {
111d01fc 725 st->ss->write_init_super(st);
edd8d13c
NB
726 flush_metadata_updates(st);
727 }
5e52ae9e 728 }
d2ca6449 729 free(infos);
3da92f27 730 st->ss->free_super(st);
5e52ae9e 731
682c7051 732 /* param is not actually used */
17f25ca6
NB
733 if (level == LEVEL_CONTAINER)
734 /* No need to start */
735 ;
736 else if (runstop == 1 || subdevs >= raiddisks) {
598f0d58
NB
737 if (st->ss->external) {
738 switch(level) {
739 case LEVEL_LINEAR:
740 case LEVEL_MULTIPATH:
741 case 0:
f35f2525 742 sysfs_set_str(&info, NULL, "array_state",
598f0d58 743 "active");
a931db9e 744 need_mdmon = 0;
598f0d58
NB
745 break;
746 default:
f35f2525 747 sysfs_set_str(&info, NULL, "array_state",
598f0d58
NB
748 "readonly");
749 break;
750 }
f35f2525 751 sysfs_set_safemode(&info, safe_mode_delay);
598f0d58
NB
752 } else {
753 mdu_param_t param;
754 if (ioctl(mdfd, RUN_ARRAY, &param)) {
755 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
756 strerror(errno));
757 Manage_runstop(mddev, mdfd, -1, 0);
7f91af49 758 goto abort;
598f0d58 759 }
682c7051 760 }
dab6685f
NB
761 if (verbose >= 0)
762 fprintf(stderr, Name ": array %s started.\n", mddev);
f7e7067b 763 if (st->ss->external && st->subarray[0]) {
8850ee3e
N
764 if (need_mdmon)
765 start_mdmon(st->container_dev);
766
767 ping_monitor(devnum2devname(st->container_dev));
a931db9e
NB
768 close(container_fd);
769 }
682c7051 770 } else {
b83d95f3 771 fprintf(stderr, Name ": not starting array - not enough devices.\n");
682c7051 772 }
7f91af49 773 close(mdfd);
682c7051 774 return 0;
7f91af49
N
775
776 abort:
777 if (mdfd >= 0)
778 close(mdfd);
779 return 1;
64c4757e 780}