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