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