]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Create.c
imsm: fix spare promotion
[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);
a0962fe9
N
378 if (st && strcmp(st->ss->name, "1.x") == 0 &&
379 st->minor_version >= 1 &&
380 did_default &&
381 level == 1) {
382 warn = 1;
383 fprintf(stderr, Name ": Note: this array has metadata at the start and\n"
384 " may not be suitable as a boot device. If you plan to\n"
385 " store '/' or '/boot' on this device please ensure that\n"
386 " your boot-loader understands md/v1.x metadata, or use\n"
387 " --metadata=1.0\n");
388 }
b6e63da4 389 close(fd);
dab6685f 390 }
682c7051 391 }
8592f29d
N
392 if (have_container)
393 info.array.working_disks = raiddisks;
682c7051 394 if (fail) {
52826846
NB
395 fprintf(stderr, Name ": create aborted\n");
396 return 1;
682c7051
NB
397 }
398 if (size == 0) {
5f8097be 399 if (mindisc == NULL && !have_container) {
52826846
NB
400 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
401 return 1;
402 }
5f8097be
NB
403 if (level > 0 || level == LEVEL_MULTIPATH
404 || level == LEVEL_FAULTY
b8ac1967 405 || st->ss->external ) {
b5e64645 406 /* size is meaningful */
b8ac1967
NB
407 if (!st->ss->validate_geometry(st, level, layout,
408 raiddisks,
e46273eb 409 chunk, minsize*2,
2c514b71 410 NULL, NULL, 0)) {
aba69144 411 fprintf(stderr, Name ": devices too large for RAID level %d\n", level);
b5e64645
NB
412 return 1;
413 }
414 size = minsize;
dab6685f 415 if (verbose > 0)
5dd497ee 416 fprintf(stderr, Name ": size set to %lluK\n", size);
b5e64645 417 }
682c7051 418 }
8592f29d 419 if (!have_container && level > 0 && ((maxsize-size)*100 > maxsize)) {
dab6685f 420 if (runstop != 1 || verbose >= 0)
8592f29d 421 fprintf(stderr, Name ": largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
dab6685f 422 maxdisc, size);
52826846 423 warn = 1;
682c7051
NB
424 }
425
5615172f
DW
426 if (st->ss->detail_platform && st->ss->detail_platform(0, 1) != 0) {
427 if (runstop != 1 || verbose >= 0)
428 fprintf(stderr, Name ": %s unable to enumerate platform support\n"
429 " array may not be compatible with hardware/firmware\n",
430 st->ss->name);
431 warn = 1;
432 }
433
682c7051 434 if (warn) {
52826846
NB
435 if (runstop!= 1) {
436 if (!ask("Continue creating array? ")) {
437 fprintf(stderr, Name ": create aborted.\n");
438 return 1;
439 }
440 } else {
dab6685f 441 if (verbose > 0)
52826846 442 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
682c7051 443 }
682c7051
NB
444 }
445
66f8bbbe 446 /* If this is raid4/5, we want to configure the last active slot
52826846 447 * as missing, so that a reconstruct happens (faster than re-parity)
98c6faba 448 * FIX: Can we do this for raid6 as well?
52826846 449 */
87b47257
N
450 if (st->ss->external == 0 &&
451 assume_clean==0 && force == 0 && first_missing >= raiddisks) {
98c6faba 452 switch ( level ) {
66f8bbbe 453 case 4:
98c6faba
NB
454 case 5:
455 insert_point = raiddisks-1;
456 sparedisks++;
06c7f68e 457 info.array.active_disks--;
98c6faba
NB
458 missing_disks++;
459 break;
460 default:
461 break;
462 }
52826846 463 }
6fb79233
NB
464 /* For raid6, if creating with 1 missing drive, make a good drive
465 * into a spare, else the create will fail
466 */
467 if (assume_clean == 0 && force == 0 && first_missing < raiddisks &&
ffcfc735 468 st->ss->external == 0 &&
6fb79233
NB
469 second_missing >= raiddisks && level == 6) {
470 insert_point = raiddisks - 1;
471 if (insert_point == first_missing)
472 insert_point--;
473 sparedisks ++;
474 info.array.active_disks--;
475 missing_disks++;
476 }
570510ba 477
5f8097be 478 if (level <= 0 && first_missing < subdevs * 2) {
570510ba
NB
479 fprintf(stderr,
480 Name ": This level does not support missing devices\n");
481 return 1;
482 }
aba69144 483
7f91af49 484 /* We need to create the device */
ad5bc697 485 map_lock(&map);
a04d5763 486 mdfd = create_mddev(mddev, name, autof, LOCAL, chosen_name);
7f91af49
N
487 if (mdfd < 0)
488 return 1;
f2be09f1 489 mddev = chosen_name;
7f91af49
N
490
491 vers = md_get_version(mdfd);
492 if (vers < 9000) {
493 fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n");
494 goto abort;
495 } else {
496 mdu_array_info_t inf;
497 memset(&inf, 0, sizeof(inf));
498 ioctl(mdfd, GET_ARRAY_INFO, &inf);
499 if (inf.working_disks != 0) {
500 fprintf(stderr, Name ": another array by this name"
501 " is already running.\n");
502 goto abort;
503 }
504 }
a04d5763 505
682c7051
NB
506 /* Ok, lets try some ioctls */
507
06c7f68e
NB
508 info.array.level = level;
509 info.array.size = size;
510 info.array.raid_disks = raiddisks;
82b27616
NB
511 /* The kernel should *know* what md_minor we are dealing
512 * with, but it chooses to trust me instead. Sigh
513 */
06c7f68e 514 info.array.md_minor = 0;
82b27616 515 if (fstat(mdfd, &stb)==0)
06c7f68e
NB
516 info.array.md_minor = minor(stb.st_rdev);
517 info.array.not_persistent = 0;
6fb79233 518
66f8bbbe 519 if ( ( (level == 4 || level == 5) &&
b5e64645
NB
520 (insert_point < raiddisks || first_missing < raiddisks) )
521 ||
6fb79233
NB
522 ( level == 6 && (insert_point < raiddisks
523 || second_missing < raiddisks))
47d79ef8 524 ||
d024b0a7
NB
525 ( level <= 0 )
526 ||
47d79ef8 527 assume_clean
103f2410 528 ) {
06c7f68e 529 info.array.state = 1; /* clean, but one+ drive will be missing*/
103f2410
NB
530 info.resync_start = ~0ULL;
531 } else {
06c7f68e 532 info.array.state = 0; /* not clean, but no errors */
103f2410
NB
533 info.resync_start = 0;
534 }
f9c25f1d
NB
535 if (level == 10) {
536 /* for raid10, the bitmap size is the capacity of the array,
537 * which is array.size * raid_disks / ncopies;
538 * .. but convert to sectors.
539 */
702b557b 540 int ncopies = ((layout>>8) & 255) * (layout & 255);
5dd497ee
NB
541 bitmapsize = (unsigned long long)size * raiddisks / ncopies * 2;
542/* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
f9c25f1d 543 } else
5dd497ee 544 bitmapsize = (unsigned long long)size * 2;
f9c25f1d 545
82b27616
NB
546 /* There is lots of redundancy in these disk counts,
547 * raid_disks is the most meaningful value
548 * it describes the geometry of the array
549 * it is constant
550 * nr_disks is total number of used slots.
551 * it should be raid_disks+spare_disks
552 * spare_disks is the number of extra disks present
553 * see above
554 * active_disks is the number of working disks in
555 * active slots. (With raid_disks)
556 * working_disks is the total number of working disks,
557 * including spares
558 * failed_disks is the number of disks marked failed
559 *
560 * Ideally, the kernel would keep these (except raid_disks)
561 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
562 * So for now, we assume that all raid and spare
563 * devices will be given.
564 */
06c7f68e
NB
565 info.array.spare_disks=sparedisks;
566 info.array.failed_disks=missing_disks;
567 info.array.nr_disks = info.array.working_disks
568 + info.array.failed_disks;
569 info.array.layout = layout;
570 info.array.chunk_size = chunk*1024;
82d9eba6 571
b3b33eb5
NB
572 if (name == NULL || *name == 0) {
573 /* base name on mddev */
d1e80164
NB
574 /* /dev/md0 -> 0
575 * /dev/md_d0 -> d0
576 * /dev/md/1 -> 1
577 * /dev/md/d1 -> d1
578 * /dev/md/home -> home
579 * /dev/mdhome -> home
580 */
69207ff6 581 /* FIXME compare this with rules in create_mddev */
b3b33eb5
NB
582 name = strrchr(mddev, '/');
583 if (name) {
584 name++;
60248f74
NB
585 if (strncmp(name, "md_d", 4)==0 &&
586 strlen(name) > 4 &&
587 isdigit(name[4]) &&
b3b33eb5 588 (name-mddev) == 5 /* /dev/ */)
d1e80164 589 name += 3;
60248f74
NB
590 else if (strncmp(name, "md", 2)==0 &&
591 strlen(name) > 2 &&
592 isdigit(name[2]) &&
593 (name-mddev) == 5 /* /dev/ */)
b3b33eb5
NB
594 name += 2;
595 }
596 }
06c7f68e 597 if (!st->ss->init_super(st, &info.array, size, name, homehost, uuid))
7f91af49 598 goto abort;
682c7051 599
d2ca6449 600 total_slots = info.array.nr_disks;
f35f2525 601 sysfs_init(&info, mdfd, 0);
159c3a1a
NB
602 st->ss->getinfo_super(st, &info);
603
f7f1b6a1 604 if (did_default && verbose >= 0) {
3c558363 605 if (is_subarray(info.text_version)) {
f7f1b6a1
NB
606 int dnum = devname2devnum(info.text_version+1);
607 char *path;
608 int mdp = get_mdp_major();
609 struct mdinfo *mdi;
610 if (dnum > 0)
611 path = map_dev(MD_MAJOR, dnum, 1);
612 else
613 path = map_dev(mdp, (-1-dnum)<< 6, 1);
614
615 mdi = sysfs_read(-1, dnum, GET_VERSION);
616
617 fprintf(stderr, Name ": Creating array inside "
618 "%s container %s\n",
619 mdi?mdi->text_version:"managed", path);
620 sysfs_free(mdi);
621 } else
622 fprintf(stderr, Name ": Defaulting to version"
623 " %s metadata\n", info.text_version);
624 }
b8ac1967 625
ad5bc697 626 map_update(&map, fd2devnum(mdfd), info.text_version,
a04d5763 627 info.uuid, chosen_name);
ad5bc697 628 map_unlock(&map);
a04d5763 629
dcec9ee5 630 if (bitmap_file && vers < 9003) {
943eafef 631 major_num = BITMAP_MAJOR_HOSTENDIAN;
dcec9ee5
NB
632#ifdef __BIG_ENDIAN
633 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
634 " between different architectured. Consider upgrading the Linux kernel.\n");
635#endif
636 }
637
55935d51
NB
638 if (bitmap_file && strcmp(bitmap_file, "internal")==0) {
639 if ((vers%100) < 2) {
640 fprintf(stderr, Name ": internal bitmaps not supported by this kernel.\n");
7f91af49 641 goto abort;
55935d51 642 }
3da92f27 643 if (!st->ss->add_internal_bitmap(st, &bitmap_chunk,
199171a2 644 delay, write_behind,
943eafef 645 bitmapsize, 1, major_num)) {
55935d51 646 fprintf(stderr, Name ": Given bitmap chunk size not supported.\n");
7f91af49 647 goto abort;
55935d51
NB
648 }
649 bitmap_file = NULL;
650 }
651
652
f35f2525 653 sysfs_init(&info, mdfd, 0);
55935d51 654
f35f2525
N
655 if (st->ss->external && st->subarray[0]) {
656 /* member */
657
658 /* When creating a member, we need to be careful
659 * to negotiate with mdmon properly.
660 * If it is already running, we cannot write to
661 * the devices and must ask it to do that part.
662 * If it isn't running, we write to the devices,
663 * and then start it.
664 * We hold an exclusive open on the container
665 * device to make sure mdmon doesn't exit after
666 * we checked that it is running.
667 *
668 * For now, fail if it is already running.
669 */
670 container_fd = open_dev_excl(st->container_dev);
671 if (container_fd < 0) {
672 fprintf(stderr, Name ": Cannot get exclusive "
673 "open on container - weird.\n");
7f91af49 674 goto abort;
d03373f1 675 }
f35f2525
N
676 if (mdmon_running(st->container_dev)) {
677 if (verbose)
678 fprintf(stderr, Name ": reusing mdmon "
679 "for %s.\n",
680 devnum2devname(st->container_dev));
681 st->update_tail = &st->updates;
682 } else
683 need_mdmon = 1;
684 }
685 rv = set_array_info(mdfd, st, &info);
82d9eba6 686 if (rv) {
f35f2525 687 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
52826846 688 mddev, strerror(errno));
7f91af49 689 goto abort;
682c7051 690 }
52826846 691
c82f047c
NB
692 if (bitmap_file) {
693 int uuid[4];
55935d51 694
3da92f27 695 st->ss->uuid_from_super(st, uuid);
dfd4d8ee 696 if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk,
dcec9ee5 697 delay, write_behind,
f9c25f1d 698 bitmapsize,
943eafef 699 major_num)) {
7f91af49 700 goto abort;
c82f047c
NB
701 }
702 bitmap_fd = open(bitmap_file, O_RDWR);
703 if (bitmap_fd < 0) {
704 fprintf(stderr, Name ": weird: %s cannot be openned\n",
dcec9ee5 705 bitmap_file);
7f91af49 706 goto abort;
c82f047c
NB
707 }
708 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
709 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
710 mddev, strerror(errno));
7f91af49 711 goto abort;
c82f047c
NB
712 }
713 }
714
d2ca6449 715 infos = malloc(sizeof(*infos) * total_slots);
4b1ac34b 716
4b1ac34b
NB
717 for (pass=1; pass <=2 ; pass++) {
718 mddev_dev_t moved_disk = NULL; /* the disk that was moved out of the insert point */
719
720 for (dnum=0, dv = devlist ; dv ;
721 dv=(dv->next)?(dv->next):moved_disk, dnum++) {
722 int fd;
723 struct stat stb;
d2ca6449 724 struct mdinfo *inf = &infos[dnum];
4b1ac34b 725
d2ca6449
NB
726 if (dnum >= total_slots)
727 abort();
4b1ac34b
NB
728 if (dnum == insert_point) {
729 moved_disk = dv;
8592f29d 730 continue;
52826846 731 }
8592f29d 732 if (strcasecmp(dv->devname, "missing")==0)
111d01fc 733 continue;
8592f29d
N
734 if (have_container)
735 moved_disk = NULL;
736 if (have_container && dnum < info.array.raid_disks - 1)
737 /* repeatedly use the container */
738 moved_disk = dv;
111d01fc 739
d2ca6449
NB
740 switch(pass) {
741 case 1:
742 *inf = info;
5f8097be 743
d2ca6449
NB
744 inf->disk.number = dnum;
745 inf->disk.raid_disk = dnum;
746 if (inf->disk.raid_disk < raiddisks)
747 inf->disk.state = (1<<MD_DISK_ACTIVE) |
748 (1<<MD_DISK_SYNC);
749 else
750 inf->disk.state = 0;
751
b01b06bd 752 if (dv->writemostly == 1)
d2ca6449
NB
753 inf->disk.state |= (1<<MD_DISK_WRITEMOSTLY);
754
8592f29d
N
755 if (have_container)
756 fd = -1;
757 else {
758 if (st->ss->external && st->subarray[0])
759 fd = open(dv->devname, O_RDWR);
760 else
761 fd = open(dv->devname, O_RDWR|O_EXCL);
762
763 if (fd < 0) {
764 fprintf(stderr, Name ": failed to open %s "
765 "after earlier success - aborting\n",
766 dv->devname);
767 goto abort;
768 }
769 fstat(fd, &stb);
770 inf->disk.major = major(stb.st_rdev);
771 inf->disk.minor = minor(stb.st_rdev);
d2ca6449 772 }
8592f29d
N
773 if (fd >= 0)
774 remove_partitions(fd);
f20c3968 775 if (st->ss->add_to_super(st, &inf->disk,
208933a7 776 fd, dv->devname))
f20c3968 777 goto abort;
d2ca6449 778 st->ss->getinfo_super(st, inf);
8ed3e5e1 779 safe_mode_delay = inf->safe_mode_delay;
d2ca6449 780
8592f29d
N
781 if (have_container && verbose > 0)
782 fprintf(stderr, Name ": Using %s for device %d\n",
783 map_dev(inf->disk.major,
784 inf->disk.minor,
785 0), dnum);
786
787 if (!have_container) {
788 /* getinfo_super might have lost these ... */
789 inf->disk.major = major(stb.st_rdev);
790 inf->disk.minor = minor(stb.st_rdev);
791 }
4b1ac34b
NB
792 break;
793 case 2:
d2ca6449 794 inf->errors = 0;
2503d23b 795 rv = 0;
4b1ac34b 796
f35f2525 797 rv = add_disk(mdfd, st, &info, inf);
d2ca6449 798
2503d23b 799 if (rv) {
d03373f1
NB
800 fprintf(stderr,
801 Name ": ADD_NEW_DISK for %s "
802 "failed: %s\n",
4b1ac34b 803 dv->devname, strerror(errno));
3da92f27 804 st->ss->free_super(st);
7f91af49 805 goto abort;
4b1ac34b 806 }
4b1ac34b
NB
807 break;
808 }
8592f29d
N
809 if (!have_container &&
810 dv == moved_disk && dnum != insert_point) break;
52826846 811 }
edd8d13c 812 if (pass == 1) {
9b1fb677
DW
813 struct mdinfo info_new;
814 struct map_ent *me = NULL;
815
816 /* check to see if the uuid has changed due to these
817 * metadata changes, and if so update the member array
818 * and container uuid. Note ->write_init_super clears
819 * the subarray cursor such that ->getinfo_super once
820 * again returns container info.
821 */
822 map_lock(&map);
823 st->ss->getinfo_super(st, &info_new);
824 if (st->ss->external && level != LEVEL_CONTAINER &&
825 !same_uuid(info_new.uuid, info.uuid, 0)) {
826 map_update(&map, fd2devnum(mdfd),
827 info_new.text_version,
828 info_new.uuid, chosen_name);
829 me = map_by_devnum(&map, st->container_dev);
830 }
831
111d01fc 832 st->ss->write_init_super(st);
9b1fb677
DW
833
834 /* update parent container uuid */
835 if (me) {
836 char *path = strdup(me->path);
837
838 st->ss->getinfo_super(st, &info_new);
839 map_update(&map, st->container_dev,
840 info_new.text_version,
841 info_new.uuid, path);
842 free(path);
843 }
844 map_unlock(&map);
845
edd8d13c
NB
846 flush_metadata_updates(st);
847 }
5e52ae9e 848 }
d2ca6449 849 free(infos);
3da92f27 850 st->ss->free_super(st);
5e52ae9e 851
97590376
N
852 if (level == LEVEL_CONTAINER) {
853 /* No need to start. But we should signal udev to
854 * create links */
855 sysfs_uevent(&info, "change");
ed034b11
N
856 if (verbose >= 0)
857 fprintf(stderr, Name ": container %s prepared.\n", mddev);
a7c6e3fb 858 wait_for(chosen_name, mdfd);
97590376 859 } else if (runstop == 1 || subdevs >= raiddisks) {
598f0d58
NB
860 if (st->ss->external) {
861 switch(level) {
862 case LEVEL_LINEAR:
863 case LEVEL_MULTIPATH:
864 case 0:
f35f2525 865 sysfs_set_str(&info, NULL, "array_state",
598f0d58 866 "active");
a931db9e 867 need_mdmon = 0;
598f0d58
NB
868 break;
869 default:
f35f2525 870 sysfs_set_str(&info, NULL, "array_state",
598f0d58
NB
871 "readonly");
872 break;
873 }
f35f2525 874 sysfs_set_safemode(&info, safe_mode_delay);
598f0d58 875 } else {
97590376 876 /* param is not actually used */
598f0d58
NB
877 mdu_param_t param;
878 if (ioctl(mdfd, RUN_ARRAY, &param)) {
879 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
880 strerror(errno));
881 Manage_runstop(mddev, mdfd, -1, 0);
7f91af49 882 goto abort;
598f0d58 883 }
682c7051 884 }
dab6685f
NB
885 if (verbose >= 0)
886 fprintf(stderr, Name ": array %s started.\n", mddev);
f7e7067b 887 if (st->ss->external && st->subarray[0]) {
8850ee3e
N
888 if (need_mdmon)
889 start_mdmon(st->container_dev);
890
891 ping_monitor(devnum2devname(st->container_dev));
a931db9e
NB
892 close(container_fd);
893 }
a7c6e3fb 894 wait_for(chosen_name, mdfd);
682c7051 895 } else {
b83d95f3 896 fprintf(stderr, Name ": not starting array - not enough devices.\n");
682c7051 897 }
7f91af49 898 close(mdfd);
682c7051 899 return 0;
7f91af49
N
900
901 abort:
902 if (mdfd >= 0)
903 close(mdfd);
904 return 1;
64c4757e 905}