]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Create.c
Fixup default definitions of VAR_RUN and ALT_RUN
[thirdparty/mdadm.git] / Create.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
e736b623 4 * Copyright (C) 2001-2009 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
e736b623 22 * Email: <neilb@suse.de>
64c4757e
NB
23 */
24
9a9dab36 25#include "mdadm.h"
682c7051
NB
26#include "md_u.h"
27#include "md_p.h"
60248f74 28#include <ctype.h>
64c4757e 29
a18a888e
DW
30static int default_layout(struct supertype *st, int level, int verbose)
31{
32 int layout = UnSet;
33
34 if (st && st->ss->default_layout)
35 layout = st->ss->default_layout(level);
36
37 if (layout == UnSet)
38 switch(level) {
39 default: /* no layout */
40 layout = 0;
41 break;
42 case 10:
43 layout = 0x102; /* near=2, far=1 */
44 if (verbose > 0)
45 fprintf(stderr,
46 Name ": layout defaults to n1\n");
47 break;
48 case 5:
49 case 6:
50 layout = map_name(r5layout, "default");
51 if (verbose > 0)
52 fprintf(stderr,
53 Name ": layout defaults to %s\n", map_num(r5layout, layout));
54 break;
55 case LEVEL_FAULTY:
56 layout = map_name(faultylayout, "default");
57
58 if (verbose > 0)
59 fprintf(stderr,
60 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
61 break;
62 }
63
64 return layout;
65}
66
67
7f91af49 68int Create(struct supertype *st, char *mddev,
5dd497ee 69 int chunk, int level, int layout, unsigned long long size, int raiddisks, int sparedisks,
3d3dd91e 70 char *name, char *homehost, int *uuid,
cd29a5c8 71 int subdevs, mddev_dev_t devlist,
47d79ef8 72 int runstop, int verbose, int force, int assume_clean,
7f91af49 73 char *bitmap_file, int bitmap_chunk, int write_behind, int delay, int autof)
64c4757e 74{
682c7051
NB
75 /*
76 * Create a new raid array.
77 *
78 * First check that necessary details are available
79 * (i.e. level, raid-disks)
80 *
81 * Then check each disk to see what might be on it
82 * and report anything interesting.
83 *
84 * If anything looks odd, and runstop not set,
85 * abort.
86 *
87 * SET_ARRAY_INFO and ADD_NEW_DISK, and
82d9eba6 88 * if runstop==run, or raiddisks disks were used,
682c7051
NB
89 * RUN_ARRAY
90 */
7f91af49 91 int mdfd;
b5e64645 92 unsigned long long minsize=0, maxsize=0;
cd29a5c8
NB
93 char *mindisc = NULL;
94 char *maxdisc = NULL;
c913b90e 95 int dnum;
cd29a5c8 96 mddev_dev_t dv;
682c7051 97 int fail=0, warn=0;
82b27616 98 struct stat stb;
82d9eba6 99 int first_missing = subdevs * 2;
6fb79233 100 int second_missing = subdevs * 2;
52826846 101 int missing_disks = 0;
82d9eba6 102 int insert_point = subdevs * 2; /* where to insert a missing drive */
d2ca6449 103 int total_slots;
4b1ac34b 104 int pass;
82d9eba6
NB
105 int vers;
106 int rv;
c82f047c 107 int bitmap_fd;
5f8097be 108 int have_container = 0;
0e600426 109 int container_fd = -1;
a931db9e 110 int need_mdmon = 0;
f9c25f1d 111 unsigned long long bitmapsize;
d2ca6449 112 struct mdinfo info, *infos;
b8ac1967 113 int did_default = 0;
a18a888e 114 int do_default_layout = 0;
8ed3e5e1 115 unsigned long safe_mode_delay = 0;
a04d5763 116 char chosen_name[1024];
ad5bc697 117 struct map_ent *map = NULL;
8592f29d 118 unsigned long long newsize;
682c7051 119
943eafef 120 int major_num = BITMAP_MAJOR_HI;
dcec9ee5 121
06c7f68e 122 memset(&info, 0, sizeof(info));
29e766a5 123
a322f70c 124 if (level == UnSet) {
3cfe6247 125 /* "ddf" and "imsm" metadata only supports one level - should possibly
a322f70c
DW
126 * push this into metadata handler??
127 */
3cfe6247 128 if (st && (st->ss == &super_ddf || st->ss == &super_imsm))
a322f70c
DW
129 level = LEVEL_CONTAINER;
130 }
131
98c6faba 132 if (level == UnSet) {
682c7051
NB
133 fprintf(stderr,
134 Name ": a RAID level is needed to create an array.\n");
135 return 1;
136 }
98c6faba
NB
137 if (raiddisks < 4 && level == 6) {
138 fprintf(stderr,
139 Name ": at least 4 raid-devices needed for level 6\n");
140 return 1;
141 }
142 if (raiddisks > 256 && level == 6) {
143 fprintf(stderr,
144 Name ": no more than 256 raid-devices supported for level 6\n");
145 return 1;
146 }
52826846
NB
147 if (raiddisks < 2 && level >= 4) {
148 fprintf(stderr,
98c6faba 149 Name ": at least 2 raid-devices needed for level 4 or 5\n");
52826846
NB
150 return 1;
151 }
570510ba
NB
152 if (level <= 0 && sparedisks) {
153 fprintf(stderr,
154 Name ": This level does not support spare devices\n");
155 return 1;
156 }
5f8097be
NB
157
158 if (subdevs == 1 && strcmp(devlist->devname, "missing") != 0) {
159 /* If given a single device, it might be a container, and we can
160 * extract a device list from there
161 */
162 mdu_array_info_t inf;
163 int fd;
164
165 memset(&inf, 0, sizeof(inf));
37ea3936 166 fd = open(devlist->devname, O_RDONLY);
5f8097be
NB
167 if (fd >= 0 &&
168 ioctl(fd, GET_ARRAY_INFO, &inf) == 0 &&
169 inf.raid_disks == 0) {
170 /* yep, looks like a container */
171 if (st) {
172 rv = st->ss->load_super(st, fd,
173 devlist->devname);
174 if (rv == 0)
175 have_container = 1;
176 } else {
177 st = guess_super(fd);
178 if (st && !(rv = st->ss->
179 load_super(st, fd,
180 devlist->devname)))
181 have_container = 1;
182 else
183 st = NULL;
184 }
18fde300 185 if (have_container) {
8592f29d 186 subdevs = raiddisks;
18fde300
DW
187 first_missing = subdevs * 2;
188 second_missing = subdevs * 2;
189 insert_point = subdevs * 2;
190 }
5f8097be
NB
191 }
192 if (fd >= 0)
193 close(fd);
5f8097be 194 }
ffcfc735
N
195 if (st && st->ss->external && sparedisks) {
196 fprintf(stderr,
197 Name ": This metadata type does not support "
b42f577a 198 "spare disks at create time\n");
ffcfc735
N
199 return 1;
200 }
682c7051 201 if (subdevs > raiddisks+sparedisks) {
b83d95f3 202 fprintf(stderr, Name ": You have listed more devices (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks);
52826846 203 return 1;
682c7051 204 }
5f8097be 205 if (!have_container && subdevs < raiddisks+sparedisks) {
52826846
NB
206 fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n");
207 return 1;
208 }
5b28bd56
NB
209 if (bitmap_file && level <= 0) {
210 fprintf(stderr, Name ": bitmaps not meaningful with level %s\n",
211 map_num(pers, level)?:"given");
212 return 1;
213 }
52826846 214
682c7051 215 /* now set some defaults */
b5e64645 216
a18a888e
DW
217
218 if (layout == UnSet) {
219 do_default_layout = 1;
220 layout = default_layout(st, level, verbose);
221 }
682c7051 222
e5329c37
NB
223 if (level == 10)
224 /* check layout fits in array*/
225 if ((layout&255) * ((layout>>8)&255) > raiddisks) {
226 fprintf(stderr, Name ": that layout requires at least %d devices\n",
227 (layout&255) * ((layout>>8)&255));
228 return 1;
229 }
230
aa88f531
NB
231 switch(level) {
232 case 4:
233 case 5:
e5329c37 234 case 10:
98c6faba 235 case 6:
aa88f531 236 case 0:
aa88f531 237 if (chunk == 0) {
5f175898
N
238 chunk = 512;
239 if (verbose > 0)
240 fprintf(stderr, Name ": chunk size defaults to 512K\n");
241 }
242 break;
243 case LEVEL_LINEAR:
244 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
245 if (get_linux_version() < 2006016 && chunk == 0) {
aa88f531 246 chunk = 64;
dab6685f 247 if (verbose > 0)
aa88f531
NB
248 fprintf(stderr, Name ": chunk size defaults to 64K\n");
249 }
250 break;
570510ba 251 case 1:
08e43379 252 case LEVEL_FAULTY:
570510ba 253 case LEVEL_MULTIPATH:
17f25ca6 254 case LEVEL_CONTAINER:
aa88f531
NB
255 if (chunk) {
256 chunk = 0;
dab6685f 257 if (verbose > 0)
aa88f531
NB
258 fprintf(stderr, Name ": chunk size ignored for this level\n");
259 }
260 break;
570510ba
NB
261 default:
262 fprintf(stderr, Name ": unknown level %d\n", level);
263 return 1;
682c7051 264 }
8592f29d 265
9f3bd60c
N
266 if (size && chunk)
267 size &= ~(unsigned long long)(chunk - 1);
8592f29d 268 newsize = size * 2;
17f25ca6 269 if (st && ! st->ss->validate_geometry(st, level, layout, raiddisks,
8592f29d 270 chunk, size*2, NULL, &newsize, verbose>=0))
17f25ca6 271 return 1;
8592f29d
N
272 if (size == 0) {
273 size = newsize / 2;
274 if (size && verbose > 0)
275 fprintf(stderr, Name ": setting size to %lluK\n",
276 (unsigned long long)size);
277 }
17f25ca6 278
682c7051 279 /* now look at the subdevs */
06c7f68e
NB
280 info.array.active_disks = 0;
281 info.array.working_disks = 0;
c913b90e 282 dnum = 0;
8592f29d 283 for (dv=devlist; dv && !have_container; dv=dv->next, dnum++) {
cd29a5c8 284 char *dname = dv->devname;
17f25ca6 285 unsigned long long freesize;
cd29a5c8 286 if (strcasecmp(dname, "missing")==0) {
c913b90e
NB
287 if (first_missing > dnum)
288 first_missing = dnum;
6fb79233
NB
289 if (second_missing > dnum && dnum > first_missing)
290 second_missing = dnum;
52826846
NB
291 missing_disks ++;
292 continue;
293 }
06c7f68e 294 info.array.working_disks++;
c913b90e 295 if (dnum < raiddisks)
06c7f68e 296 info.array.active_disks++;
058574b1 297 if (st == NULL) {
8aec876d 298 struct createinfo *ci = conf_get_create_info();
058574b1
NB
299 if (ci)
300 st = ci->supertype;
301 }
576d6d83
NB
302 if (st == NULL) {
303 /* Need to choose a default metadata, which is different
17f25ca6 304 * depending on geometry of array.
576d6d83
NB
305 */
306 int i;
307 char *name = "default";
17f25ca6 308 for(i=0; !st && superlist[i]; i++) {
576d6d83 309 st = superlist[i]->match_metadata_desc(name);
a18a888e
DW
310 if (do_default_layout)
311 layout = default_layout(st, level, verbose);
17f25ca6
NB
312 if (st && !st->ss->validate_geometry
313 (st, level, layout, raiddisks,
e46273eb 314 chunk, size*2, dname, &freesize,
2c514b71 315 verbose > 0))
17f25ca6
NB
316 st = NULL;
317 }
576d6d83
NB
318
319 if (!st) {
17f25ca6
NB
320 fprintf(stderr, Name ": device %s not suitable "
321 "for any style of array\n",
322 dname);
576d6d83
NB
323 exit(2);
324 }
b8ac1967 325 if (st->ss != &super0 ||
576d6d83 326 st->minor_version != 90)
b8ac1967 327 did_default = 1;
17f25ca6 328 } else {
a18a888e
DW
329 if (do_default_layout)
330 layout = default_layout(st, level, verbose);
17f25ca6
NB
331 if (!st->ss->validate_geometry(st, level, layout,
332 raiddisks,
e46273eb 333 chunk, size*2, dname,
2c514b71 334 &freesize,
b42f577a 335 verbose >= 0)) {
17f25ca6
NB
336
337 fprintf(stderr,
338 Name ": %s is not suitable for "
339 "this array.\n",
340 dname);
341 fail = 1;
342 continue;
343 }
682c7051 344 }
82d9eba6
NB
345
346 freesize /= 2; /* convert to K */
d2cd3ffc
NB
347 if (chunk) {
348 /* round to chunk size */
349 freesize = freesize & ~(chunk-1);
350 }
682c7051
NB
351
352 if (size && freesize < size) {
42dc2744 353 fprintf(stderr, Name ": %s is smaller than given size."
17f25ca6
NB
354 " %lluK < %lluK + metadata\n",
355 dname, freesize, size);
52826846 356 fail = 1;
52826846 357 continue;
682c7051 358 }
cd29a5c8
NB
359 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
360 maxdisc = dname;
52826846 361 maxsize = freesize;
682c7051 362 }
cd29a5c8
NB
363 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
364 mindisc = dname;
52826846 365 minsize = freesize;
682c7051 366 }
dab6685f 367 if (runstop != 1 || verbose >= 0) {
37ea3936 368 int fd = open(dname, O_RDONLY);
b6e63da4
NB
369 if (fd <0 ) {
370 fprintf(stderr, Name ": Cannot open %s: %s\n",
371 dname, strerror(errno));
372 fail=1;
373 continue;
374 }
dab6685f
NB
375 warn |= check_ext2(fd, dname);
376 warn |= check_reiser(fd, dname);
377 warn |= check_raid(fd, dname);
034b203a
TM
378 if (strcmp(st->ss->name, "1.x") == 0 &&
379 st->minor_version >= 1)
380 /* metadata at front */
381 warn |= check_partitions(fd, dname, 0);
382 else if (level == 1 || level == LEVEL_CONTAINER)
383 /* partitions could be meaningful */
384 warn |= check_partitions(fd, dname, freesize*2);
385 else
386 /* partitions cannot be meaningful */
387 warn |= check_partitions(fd, dname, 0);
388 if (strcmp(st->ss->name, "1.x") == 0 &&
a0962fe9
N
389 st->minor_version >= 1 &&
390 did_default &&
034b203a
TM
391 level == 1 &&
392 (warn & 1024) == 0) {
393 warn |= 1024;
a0962fe9
N
394 fprintf(stderr, Name ": Note: this array has metadata at the start and\n"
395 " may not be suitable as a boot device. If you plan to\n"
cc86f89c 396 " store '/boot' on this device please ensure that\n"
a0962fe9 397 " your boot-loader understands md/v1.x metadata, or use\n"
cc86f89c 398 " --metadata=0.90\n");
a0962fe9 399 }
b6e63da4 400 close(fd);
dab6685f 401 }
682c7051 402 }
8592f29d
N
403 if (have_container)
404 info.array.working_disks = raiddisks;
682c7051 405 if (fail) {
52826846
NB
406 fprintf(stderr, Name ": create aborted\n");
407 return 1;
682c7051
NB
408 }
409 if (size == 0) {
5f8097be 410 if (mindisc == NULL && !have_container) {
52826846
NB
411 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
412 return 1;
413 }
5f8097be
NB
414 if (level > 0 || level == LEVEL_MULTIPATH
415 || level == LEVEL_FAULTY
b8ac1967 416 || st->ss->external ) {
b5e64645 417 /* size is meaningful */
b8ac1967
NB
418 if (!st->ss->validate_geometry(st, level, layout,
419 raiddisks,
e46273eb 420 chunk, minsize*2,
2c514b71 421 NULL, NULL, 0)) {
aba69144 422 fprintf(stderr, Name ": devices too large for RAID level %d\n", level);
b5e64645
NB
423 return 1;
424 }
425 size = minsize;
dab6685f 426 if (verbose > 0)
5dd497ee 427 fprintf(stderr, Name ": size set to %lluK\n", size);
b5e64645 428 }
682c7051 429 }
8592f29d 430 if (!have_container && level > 0 && ((maxsize-size)*100 > maxsize)) {
dab6685f 431 if (runstop != 1 || verbose >= 0)
8592f29d 432 fprintf(stderr, Name ": largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
dab6685f 433 maxdisc, size);
52826846 434 warn = 1;
682c7051
NB
435 }
436
5615172f
DW
437 if (st->ss->detail_platform && st->ss->detail_platform(0, 1) != 0) {
438 if (runstop != 1 || verbose >= 0)
439 fprintf(stderr, Name ": %s unable to enumerate platform support\n"
440 " array may not be compatible with hardware/firmware\n",
441 st->ss->name);
442 warn = 1;
443 }
444
682c7051 445 if (warn) {
52826846
NB
446 if (runstop!= 1) {
447 if (!ask("Continue creating array? ")) {
448 fprintf(stderr, Name ": create aborted.\n");
449 return 1;
450 }
451 } else {
dab6685f 452 if (verbose > 0)
52826846 453 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
682c7051 454 }
682c7051
NB
455 }
456
66f8bbbe 457 /* If this is raid4/5, we want to configure the last active slot
52826846 458 * as missing, so that a reconstruct happens (faster than re-parity)
98c6faba 459 * FIX: Can we do this for raid6 as well?
52826846 460 */
87b47257
N
461 if (st->ss->external == 0 &&
462 assume_clean==0 && force == 0 && first_missing >= raiddisks) {
98c6faba 463 switch ( level ) {
66f8bbbe 464 case 4:
98c6faba
NB
465 case 5:
466 insert_point = raiddisks-1;
467 sparedisks++;
06c7f68e 468 info.array.active_disks--;
98c6faba
NB
469 missing_disks++;
470 break;
471 default:
472 break;
473 }
52826846 474 }
6fb79233
NB
475 /* For raid6, if creating with 1 missing drive, make a good drive
476 * into a spare, else the create will fail
477 */
478 if (assume_clean == 0 && force == 0 && first_missing < raiddisks &&
ffcfc735 479 st->ss->external == 0 &&
6fb79233
NB
480 second_missing >= raiddisks && level == 6) {
481 insert_point = raiddisks - 1;
482 if (insert_point == first_missing)
483 insert_point--;
484 sparedisks ++;
485 info.array.active_disks--;
486 missing_disks++;
487 }
570510ba 488
5f8097be 489 if (level <= 0 && first_missing < subdevs * 2) {
570510ba
NB
490 fprintf(stderr,
491 Name ": This level does not support missing devices\n");
492 return 1;
493 }
aba69144 494
7f91af49 495 /* We need to create the device */
ad5bc697 496 map_lock(&map);
a04d5763 497 mdfd = create_mddev(mddev, name, autof, LOCAL, chosen_name);
7f91af49
N
498 if (mdfd < 0)
499 return 1;
f2be09f1 500 mddev = chosen_name;
7f91af49
N
501
502 vers = md_get_version(mdfd);
503 if (vers < 9000) {
504 fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n");
505 goto abort;
506 } else {
507 mdu_array_info_t inf;
508 memset(&inf, 0, sizeof(inf));
509 ioctl(mdfd, GET_ARRAY_INFO, &inf);
510 if (inf.working_disks != 0) {
511 fprintf(stderr, Name ": another array by this name"
512 " is already running.\n");
513 goto abort;
514 }
515 }
a04d5763 516
682c7051
NB
517 /* Ok, lets try some ioctls */
518
06c7f68e
NB
519 info.array.level = level;
520 info.array.size = size;
521 info.array.raid_disks = raiddisks;
82b27616
NB
522 /* The kernel should *know* what md_minor we are dealing
523 * with, but it chooses to trust me instead. Sigh
524 */
06c7f68e 525 info.array.md_minor = 0;
82b27616 526 if (fstat(mdfd, &stb)==0)
06c7f68e
NB
527 info.array.md_minor = minor(stb.st_rdev);
528 info.array.not_persistent = 0;
6fb79233 529
66f8bbbe 530 if ( ( (level == 4 || level == 5) &&
b5e64645
NB
531 (insert_point < raiddisks || first_missing < raiddisks) )
532 ||
6fb79233
NB
533 ( level == 6 && (insert_point < raiddisks
534 || second_missing < raiddisks))
47d79ef8 535 ||
d024b0a7
NB
536 ( level <= 0 )
537 ||
47d79ef8 538 assume_clean
103f2410 539 ) {
06c7f68e 540 info.array.state = 1; /* clean, but one+ drive will be missing*/
b7528a20 541 info.resync_start = MaxSector;
103f2410 542 } else {
06c7f68e 543 info.array.state = 0; /* not clean, but no errors */
103f2410
NB
544 info.resync_start = 0;
545 }
f9c25f1d
NB
546 if (level == 10) {
547 /* for raid10, the bitmap size is the capacity of the array,
548 * which is array.size * raid_disks / ncopies;
549 * .. but convert to sectors.
550 */
702b557b 551 int ncopies = ((layout>>8) & 255) * (layout & 255);
5dd497ee
NB
552 bitmapsize = (unsigned long long)size * raiddisks / ncopies * 2;
553/* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
f9c25f1d 554 } else
5dd497ee 555 bitmapsize = (unsigned long long)size * 2;
f9c25f1d 556
82b27616
NB
557 /* There is lots of redundancy in these disk counts,
558 * raid_disks is the most meaningful value
559 * it describes the geometry of the array
560 * it is constant
561 * nr_disks is total number of used slots.
562 * it should be raid_disks+spare_disks
563 * spare_disks is the number of extra disks present
564 * see above
565 * active_disks is the number of working disks in
566 * active slots. (With raid_disks)
567 * working_disks is the total number of working disks,
568 * including spares
569 * failed_disks is the number of disks marked failed
570 *
571 * Ideally, the kernel would keep these (except raid_disks)
572 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
573 * So for now, we assume that all raid and spare
574 * devices will be given.
575 */
06c7f68e
NB
576 info.array.spare_disks=sparedisks;
577 info.array.failed_disks=missing_disks;
578 info.array.nr_disks = info.array.working_disks
579 + info.array.failed_disks;
580 info.array.layout = layout;
581 info.array.chunk_size = chunk*1024;
82d9eba6 582
b3b33eb5
NB
583 if (name == NULL || *name == 0) {
584 /* base name on mddev */
d1e80164
NB
585 /* /dev/md0 -> 0
586 * /dev/md_d0 -> d0
587 * /dev/md/1 -> 1
588 * /dev/md/d1 -> d1
589 * /dev/md/home -> home
590 * /dev/mdhome -> home
591 */
69207ff6 592 /* FIXME compare this with rules in create_mddev */
b3b33eb5
NB
593 name = strrchr(mddev, '/');
594 if (name) {
595 name++;
60248f74
NB
596 if (strncmp(name, "md_d", 4)==0 &&
597 strlen(name) > 4 &&
598 isdigit(name[4]) &&
b3b33eb5 599 (name-mddev) == 5 /* /dev/ */)
d1e80164 600 name += 3;
60248f74
NB
601 else if (strncmp(name, "md", 2)==0 &&
602 strlen(name) > 2 &&
603 isdigit(name[2]) &&
604 (name-mddev) == 5 /* /dev/ */)
b3b33eb5
NB
605 name += 2;
606 }
607 }
06c7f68e 608 if (!st->ss->init_super(st, &info.array, size, name, homehost, uuid))
7f91af49 609 goto abort;
682c7051 610
d2ca6449 611 total_slots = info.array.nr_disks;
f35f2525 612 sysfs_init(&info, mdfd, 0);
159c3a1a
NB
613 st->ss->getinfo_super(st, &info);
614
f7f1b6a1 615 if (did_default && verbose >= 0) {
3c558363 616 if (is_subarray(info.text_version)) {
f7f1b6a1
NB
617 int dnum = devname2devnum(info.text_version+1);
618 char *path;
619 int mdp = get_mdp_major();
620 struct mdinfo *mdi;
621 if (dnum > 0)
622 path = map_dev(MD_MAJOR, dnum, 1);
623 else
624 path = map_dev(mdp, (-1-dnum)<< 6, 1);
625
626 mdi = sysfs_read(-1, dnum, GET_VERSION);
627
628 fprintf(stderr, Name ": Creating array inside "
629 "%s container %s\n",
630 mdi?mdi->text_version:"managed", path);
631 sysfs_free(mdi);
632 } else
633 fprintf(stderr, Name ": Defaulting to version"
634 " %s metadata\n", info.text_version);
635 }
b8ac1967 636
ad5bc697 637 map_update(&map, fd2devnum(mdfd), info.text_version,
a04d5763 638 info.uuid, chosen_name);
ad5bc697 639 map_unlock(&map);
a04d5763 640
dcec9ee5 641 if (bitmap_file && vers < 9003) {
943eafef 642 major_num = BITMAP_MAJOR_HOSTENDIAN;
dcec9ee5
NB
643#ifdef __BIG_ENDIAN
644 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
645 " between different architectured. Consider upgrading the Linux kernel.\n");
646#endif
647 }
648
55935d51
NB
649 if (bitmap_file && strcmp(bitmap_file, "internal")==0) {
650 if ((vers%100) < 2) {
651 fprintf(stderr, Name ": internal bitmaps not supported by this kernel.\n");
7f91af49 652 goto abort;
55935d51 653 }
ebeb3663
N
654 if (!st->ss->add_internal_bitmap) {
655 fprintf(stderr, Name ": internal bitmaps not supported with %s metadata\n",
656 st->ss->name);
657 goto abort;
658 }
3da92f27 659 if (!st->ss->add_internal_bitmap(st, &bitmap_chunk,
199171a2 660 delay, write_behind,
943eafef 661 bitmapsize, 1, major_num)) {
55935d51 662 fprintf(stderr, Name ": Given bitmap chunk size not supported.\n");
7f91af49 663 goto abort;
55935d51
NB
664 }
665 bitmap_file = NULL;
666 }
667
668
f35f2525 669 sysfs_init(&info, mdfd, 0);
55935d51 670
f35f2525
N
671 if (st->ss->external && st->subarray[0]) {
672 /* member */
673
674 /* When creating a member, we need to be careful
675 * to negotiate with mdmon properly.
676 * If it is already running, we cannot write to
677 * the devices and must ask it to do that part.
678 * If it isn't running, we write to the devices,
679 * and then start it.
680 * We hold an exclusive open on the container
681 * device to make sure mdmon doesn't exit after
682 * we checked that it is running.
683 *
684 * For now, fail if it is already running.
685 */
686 container_fd = open_dev_excl(st->container_dev);
687 if (container_fd < 0) {
688 fprintf(stderr, Name ": Cannot get exclusive "
689 "open on container - weird.\n");
7f91af49 690 goto abort;
d03373f1 691 }
f35f2525
N
692 if (mdmon_running(st->container_dev)) {
693 if (verbose)
694 fprintf(stderr, Name ": reusing mdmon "
695 "for %s.\n",
696 devnum2devname(st->container_dev));
697 st->update_tail = &st->updates;
698 } else
699 need_mdmon = 1;
700 }
701 rv = set_array_info(mdfd, st, &info);
82d9eba6 702 if (rv) {
f35f2525 703 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
52826846 704 mddev, strerror(errno));
7f91af49 705 goto abort;
682c7051 706 }
52826846 707
c82f047c
NB
708 if (bitmap_file) {
709 int uuid[4];
55935d51 710
3da92f27 711 st->ss->uuid_from_super(st, uuid);
dfd4d8ee 712 if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk,
dcec9ee5 713 delay, write_behind,
f9c25f1d 714 bitmapsize,
943eafef 715 major_num)) {
7f91af49 716 goto abort;
c82f047c
NB
717 }
718 bitmap_fd = open(bitmap_file, O_RDWR);
719 if (bitmap_fd < 0) {
720 fprintf(stderr, Name ": weird: %s cannot be openned\n",
dcec9ee5 721 bitmap_file);
7f91af49 722 goto abort;
c82f047c
NB
723 }
724 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
725 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
726 mddev, strerror(errno));
7f91af49 727 goto abort;
c82f047c
NB
728 }
729 }
730
d2ca6449 731 infos = malloc(sizeof(*infos) * total_slots);
4b1ac34b 732
4b1ac34b
NB
733 for (pass=1; pass <=2 ; pass++) {
734 mddev_dev_t moved_disk = NULL; /* the disk that was moved out of the insert point */
735
736 for (dnum=0, dv = devlist ; dv ;
737 dv=(dv->next)?(dv->next):moved_disk, dnum++) {
738 int fd;
739 struct stat stb;
d2ca6449 740 struct mdinfo *inf = &infos[dnum];
4b1ac34b 741
d2ca6449
NB
742 if (dnum >= total_slots)
743 abort();
4b1ac34b
NB
744 if (dnum == insert_point) {
745 moved_disk = dv;
8592f29d 746 continue;
52826846 747 }
8592f29d 748 if (strcasecmp(dv->devname, "missing")==0)
111d01fc 749 continue;
8592f29d
N
750 if (have_container)
751 moved_disk = NULL;
752 if (have_container && dnum < info.array.raid_disks - 1)
753 /* repeatedly use the container */
754 moved_disk = dv;
111d01fc 755
d2ca6449
NB
756 switch(pass) {
757 case 1:
758 *inf = info;
5f8097be 759
d2ca6449
NB
760 inf->disk.number = dnum;
761 inf->disk.raid_disk = dnum;
762 if (inf->disk.raid_disk < raiddisks)
763 inf->disk.state = (1<<MD_DISK_ACTIVE) |
764 (1<<MD_DISK_SYNC);
765 else
766 inf->disk.state = 0;
767
b01b06bd 768 if (dv->writemostly == 1)
d2ca6449
NB
769 inf->disk.state |= (1<<MD_DISK_WRITEMOSTLY);
770
8592f29d
N
771 if (have_container)
772 fd = -1;
773 else {
774 if (st->ss->external && st->subarray[0])
775 fd = open(dv->devname, O_RDWR);
776 else
777 fd = open(dv->devname, O_RDWR|O_EXCL);
778
779 if (fd < 0) {
780 fprintf(stderr, Name ": failed to open %s "
781 "after earlier success - aborting\n",
782 dv->devname);
783 goto abort;
784 }
785 fstat(fd, &stb);
786 inf->disk.major = major(stb.st_rdev);
787 inf->disk.minor = minor(stb.st_rdev);
d2ca6449 788 }
8592f29d
N
789 if (fd >= 0)
790 remove_partitions(fd);
f20c3968 791 if (st->ss->add_to_super(st, &inf->disk,
208933a7 792 fd, dv->devname))
f20c3968 793 goto abort;
d2ca6449 794 st->ss->getinfo_super(st, inf);
8ed3e5e1 795 safe_mode_delay = inf->safe_mode_delay;
d2ca6449 796
8592f29d
N
797 if (have_container && verbose > 0)
798 fprintf(stderr, Name ": Using %s for device %d\n",
799 map_dev(inf->disk.major,
800 inf->disk.minor,
801 0), dnum);
802
803 if (!have_container) {
804 /* getinfo_super might have lost these ... */
805 inf->disk.major = major(stb.st_rdev);
806 inf->disk.minor = minor(stb.st_rdev);
807 }
4b1ac34b
NB
808 break;
809 case 2:
d2ca6449 810 inf->errors = 0;
2503d23b 811 rv = 0;
4b1ac34b 812
f35f2525 813 rv = add_disk(mdfd, st, &info, inf);
d2ca6449 814
2503d23b 815 if (rv) {
d03373f1
NB
816 fprintf(stderr,
817 Name ": ADD_NEW_DISK for %s "
818 "failed: %s\n",
4b1ac34b 819 dv->devname, strerror(errno));
3da92f27 820 st->ss->free_super(st);
7f91af49 821 goto abort;
4b1ac34b 822 }
4b1ac34b
NB
823 break;
824 }
8592f29d
N
825 if (!have_container &&
826 dv == moved_disk && dnum != insert_point) break;
52826846 827 }
edd8d13c 828 if (pass == 1) {
9b1fb677
DW
829 struct mdinfo info_new;
830 struct map_ent *me = NULL;
831
832 /* check to see if the uuid has changed due to these
833 * metadata changes, and if so update the member array
834 * and container uuid. Note ->write_init_super clears
835 * the subarray cursor such that ->getinfo_super once
836 * again returns container info.
837 */
838 map_lock(&map);
839 st->ss->getinfo_super(st, &info_new);
840 if (st->ss->external && level != LEVEL_CONTAINER &&
841 !same_uuid(info_new.uuid, info.uuid, 0)) {
842 map_update(&map, fd2devnum(mdfd),
843 info_new.text_version,
844 info_new.uuid, chosen_name);
845 me = map_by_devnum(&map, st->container_dev);
846 }
847
111d01fc 848 st->ss->write_init_super(st);
9b1fb677
DW
849
850 /* update parent container uuid */
851 if (me) {
852 char *path = strdup(me->path);
853
854 st->ss->getinfo_super(st, &info_new);
855 map_update(&map, st->container_dev,
856 info_new.text_version,
857 info_new.uuid, path);
858 free(path);
859 }
860 map_unlock(&map);
861
edd8d13c
NB
862 flush_metadata_updates(st);
863 }
5e52ae9e 864 }
d2ca6449 865 free(infos);
3da92f27 866 st->ss->free_super(st);
5e52ae9e 867
97590376
N
868 if (level == LEVEL_CONTAINER) {
869 /* No need to start. But we should signal udev to
870 * create links */
871 sysfs_uevent(&info, "change");
ed034b11
N
872 if (verbose >= 0)
873 fprintf(stderr, Name ": container %s prepared.\n", mddev);
a7c6e3fb 874 wait_for(chosen_name, mdfd);
97590376 875 } else if (runstop == 1 || subdevs >= raiddisks) {
598f0d58
NB
876 if (st->ss->external) {
877 switch(level) {
878 case LEVEL_LINEAR:
879 case LEVEL_MULTIPATH:
880 case 0:
f35f2525 881 sysfs_set_str(&info, NULL, "array_state",
598f0d58 882 "active");
a931db9e 883 need_mdmon = 0;
598f0d58
NB
884 break;
885 default:
f35f2525 886 sysfs_set_str(&info, NULL, "array_state",
598f0d58
NB
887 "readonly");
888 break;
889 }
f35f2525 890 sysfs_set_safemode(&info, safe_mode_delay);
598f0d58 891 } else {
97590376 892 /* param is not actually used */
598f0d58
NB
893 mdu_param_t param;
894 if (ioctl(mdfd, RUN_ARRAY, &param)) {
895 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
896 strerror(errno));
897 Manage_runstop(mddev, mdfd, -1, 0);
7f91af49 898 goto abort;
598f0d58 899 }
682c7051 900 }
dab6685f
NB
901 if (verbose >= 0)
902 fprintf(stderr, Name ": array %s started.\n", mddev);
f7e7067b 903 if (st->ss->external && st->subarray[0]) {
8850ee3e
N
904 if (need_mdmon)
905 start_mdmon(st->container_dev);
906
907 ping_monitor(devnum2devname(st->container_dev));
a931db9e
NB
908 close(container_fd);
909 }
a7c6e3fb 910 wait_for(chosen_name, mdfd);
682c7051 911 } else {
b83d95f3 912 fprintf(stderr, Name ": not starting array - not enough devices.\n");
682c7051 913 }
7f91af49 914 close(mdfd);
682c7051 915 return 0;
7f91af49
N
916
917 abort:
918 if (mdfd >= 0)
919 close(mdfd);
920 return 1;
64c4757e 921}