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