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