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