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