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