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