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