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