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