]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Create.c
Check partition tables when creating array.
[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"
396 " store '/' or '/boot' on this device please ensure that\n"
397 " your boot-loader understands md/v1.x metadata, or use\n"
398 " --metadata=1.0\n");
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*/
103f2410
NB
541 info.resync_start = ~0ULL;
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 }
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
f35f2525
N
666 if (st->ss->external && st->subarray[0]) {
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
NB
728 for (pass=1; pass <=2 ; pass++) {
729 mddev_dev_t moved_disk = NULL; /* the disk that was moved out of the insert point */
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 {
769 if (st->ss->external && st->subarray[0])
770 fd = open(dv->devname, O_RDWR);
771 else
772 fd = open(dv->devname, O_RDWR|O_EXCL);
773
774 if (fd < 0) {
775 fprintf(stderr, Name ": failed to open %s "
776 "after earlier success - aborting\n",
777 dv->devname);
778 goto abort;
779 }
780 fstat(fd, &stb);
781 inf->disk.major = major(stb.st_rdev);
782 inf->disk.minor = minor(stb.st_rdev);
d2ca6449 783 }
8592f29d
N
784 if (fd >= 0)
785 remove_partitions(fd);
f20c3968 786 if (st->ss->add_to_super(st, &inf->disk,
208933a7 787 fd, dv->devname))
f20c3968 788 goto abort;
d2ca6449 789 st->ss->getinfo_super(st, inf);
8ed3e5e1 790 safe_mode_delay = inf->safe_mode_delay;
d2ca6449 791
8592f29d
N
792 if (have_container && verbose > 0)
793 fprintf(stderr, Name ": Using %s for device %d\n",
794 map_dev(inf->disk.major,
795 inf->disk.minor,
796 0), dnum);
797
798 if (!have_container) {
799 /* getinfo_super might have lost these ... */
800 inf->disk.major = major(stb.st_rdev);
801 inf->disk.minor = minor(stb.st_rdev);
802 }
4b1ac34b
NB
803 break;
804 case 2:
d2ca6449 805 inf->errors = 0;
2503d23b 806 rv = 0;
4b1ac34b 807
f35f2525 808 rv = add_disk(mdfd, st, &info, inf);
d2ca6449 809
2503d23b 810 if (rv) {
d03373f1
NB
811 fprintf(stderr,
812 Name ": ADD_NEW_DISK for %s "
813 "failed: %s\n",
4b1ac34b 814 dv->devname, strerror(errno));
3da92f27 815 st->ss->free_super(st);
7f91af49 816 goto abort;
4b1ac34b 817 }
4b1ac34b
NB
818 break;
819 }
8592f29d
N
820 if (!have_container &&
821 dv == moved_disk && dnum != insert_point) break;
52826846 822 }
edd8d13c 823 if (pass == 1) {
9b1fb677
DW
824 struct mdinfo info_new;
825 struct map_ent *me = NULL;
826
827 /* check to see if the uuid has changed due to these
828 * metadata changes, and if so update the member array
829 * and container uuid. Note ->write_init_super clears
830 * the subarray cursor such that ->getinfo_super once
831 * again returns container info.
832 */
833 map_lock(&map);
834 st->ss->getinfo_super(st, &info_new);
835 if (st->ss->external && level != LEVEL_CONTAINER &&
836 !same_uuid(info_new.uuid, info.uuid, 0)) {
837 map_update(&map, fd2devnum(mdfd),
838 info_new.text_version,
839 info_new.uuid, chosen_name);
840 me = map_by_devnum(&map, st->container_dev);
841 }
842
111d01fc 843 st->ss->write_init_super(st);
9b1fb677
DW
844
845 /* update parent container uuid */
846 if (me) {
847 char *path = strdup(me->path);
848
849 st->ss->getinfo_super(st, &info_new);
850 map_update(&map, st->container_dev,
851 info_new.text_version,
852 info_new.uuid, path);
853 free(path);
854 }
855 map_unlock(&map);
856
edd8d13c
NB
857 flush_metadata_updates(st);
858 }
5e52ae9e 859 }
d2ca6449 860 free(infos);
3da92f27 861 st->ss->free_super(st);
5e52ae9e 862
97590376
N
863 if (level == LEVEL_CONTAINER) {
864 /* No need to start. But we should signal udev to
865 * create links */
866 sysfs_uevent(&info, "change");
ed034b11
N
867 if (verbose >= 0)
868 fprintf(stderr, Name ": container %s prepared.\n", mddev);
a7c6e3fb 869 wait_for(chosen_name, mdfd);
97590376 870 } else if (runstop == 1 || subdevs >= raiddisks) {
598f0d58
NB
871 if (st->ss->external) {
872 switch(level) {
873 case LEVEL_LINEAR:
874 case LEVEL_MULTIPATH:
875 case 0:
f35f2525 876 sysfs_set_str(&info, NULL, "array_state",
598f0d58 877 "active");
a931db9e 878 need_mdmon = 0;
598f0d58
NB
879 break;
880 default:
f35f2525 881 sysfs_set_str(&info, NULL, "array_state",
598f0d58
NB
882 "readonly");
883 break;
884 }
f35f2525 885 sysfs_set_safemode(&info, safe_mode_delay);
598f0d58 886 } else {
97590376 887 /* param is not actually used */
598f0d58
NB
888 mdu_param_t param;
889 if (ioctl(mdfd, RUN_ARRAY, &param)) {
890 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
891 strerror(errno));
892 Manage_runstop(mddev, mdfd, -1, 0);
7f91af49 893 goto abort;
598f0d58 894 }
682c7051 895 }
dab6685f
NB
896 if (verbose >= 0)
897 fprintf(stderr, Name ": array %s started.\n", mddev);
f7e7067b 898 if (st->ss->external && st->subarray[0]) {
8850ee3e
N
899 if (need_mdmon)
900 start_mdmon(st->container_dev);
901
902 ping_monitor(devnum2devname(st->container_dev));
a931db9e
NB
903 close(container_fd);
904 }
a7c6e3fb 905 wait_for(chosen_name, mdfd);
682c7051 906 } else {
b83d95f3 907 fprintf(stderr, Name ": not starting array - not enough devices.\n");
682c7051 908 }
7f91af49 909 close(mdfd);
682c7051 910 return 0;
7f91af49
N
911
912 abort:
913 if (mdfd >= 0)
914 close(mdfd);
915 return 1;
64c4757e 916}