]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Create.c
tests: create 00confnames
[thirdparty/mdadm.git] / Create.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
6f02172d 4 * Copyright (C) 2001-2013 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
ca3b6696 25#include "mdadm.h"
682c7051
NB
26#include "md_u.h"
27#include "md_p.h"
60248f74 28#include <ctype.h>
577fd104
LG
29#include <fcntl.h>
30#include <signal.h>
31#include <sys/signalfd.h>
32#include <sys/wait.h>
64c4757e 33
22dc741f
MT
34static int round_size_and_verify(unsigned long long *size, int chunk)
35{
36 if (*size == 0)
37 return 0;
38 *size &= ~(unsigned long long)(chunk - 1);
39 if (*size == 0) {
40 pr_err("Size cannot be smaller than chunk.\n");
41 return 1;
42 }
43 return 0;
44}
45
913f07d1
MT
46/**
47 * default_layout() - Get default layout for level.
48 * @st: metadata requested, could be NULL.
49 * @level: raid level requested.
50 * @verbose: verbose level.
51 *
52 * Try to ask metadata handler first, otherwise use global defaults.
53 *
54 * Return: Layout or &UnSet, return value meaning depends of level used.
55 */
56int default_layout(struct supertype *st, int level, int verbose)
a18a888e
DW
57{
58 int layout = UnSet;
913f07d1
MT
59 mapping_t *layout_map = NULL;
60 char *layout_name = NULL;
a18a888e 61
30f58b22
DW
62 if (st && st->ss->default_geometry)
63 st->ss->default_geometry(st, &level, &layout, NULL);
a18a888e 64
913f07d1
MT
65 if (layout != UnSet)
66 return layout;
a18a888e 67
913f07d1
MT
68 switch (level) {
69 default: /* no layout */
70 layout = 0;
71 break;
72 case 0:
73 layout = RAID0_ORIG_LAYOUT;
74 break;
75 case 10:
76 layout = 0x102; /* near=2, far=1 */
77 layout_name = "n2";
78 break;
79 case 5:
80 case 6:
81 layout_map = r5layout;
82 break;
83 case LEVEL_FAULTY:
84 layout_map = faultylayout;
85 break;
86 }
87
88 if (layout_map) {
89 layout = map_name(layout_map, "default");
5f21d674 90 layout_name = map_num_s(layout_map, layout);
913f07d1
MT
91 }
92 if (layout_name && verbose > 0)
93 pr_err("layout defaults to %s\n", layout_name);
a18a888e
DW
94
95 return layout;
96}
97
577fd104
LG
98static pid_t write_zeroes_fork(int fd, struct shape *s, struct supertype *st,
99 struct mddev_dev *dv)
100
101{
102 const unsigned long long req_size = 1 << 30;
103 unsigned long long offset_bytes, size_bytes, sz;
104 sigset_t sigset;
105 int ret = 0;
106 pid_t pid;
107
108 size_bytes = KIB_TO_BYTES(s->size);
109
110 /*
111 * If size_bytes is zero, this is a zoned raid array where
112 * each disk is of a different size and uses its full
113 * disk. Thus zero the entire disk.
114 */
115 if (!size_bytes && !get_dev_size(fd, dv->devname, &size_bytes))
116 return -1;
117
118 if (dv->data_offset != INVALID_SECTORS)
119 offset_bytes = SEC_TO_BYTES(dv->data_offset);
120 else
121 offset_bytes = SEC_TO_BYTES(st->data_offset);
122
123 pr_info("zeroing data from %lld to %lld on: %s\n",
124 offset_bytes, size_bytes, dv->devname);
125
126 pid = fork();
127 if (pid < 0) {
128 pr_err("Could not fork to zero disks: %s\n", strerror(errno));
129 return pid;
130 } else if (pid != 0) {
131 return pid;
132 }
133
134 sigemptyset(&sigset);
135 sigaddset(&sigset, SIGINT);
136 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
137
138 while (size_bytes) {
139 /*
140 * Split requests to the kernel into 1GB chunks seeing the
141 * fallocate() call is not interruptible and blocking a
142 * ctrl-c for several minutes is not desirable.
143 *
144 * 1GB is chosen as a compromise: the user may still have
145 * to wait several seconds if they ctrl-c on devices that
146 * zero slowly, but will reduce the number of requests
147 * required and thus the overhead on devices that perform
148 * better.
149 */
150 sz = size_bytes;
151 if (sz >= req_size)
152 sz = req_size;
153
154 if (fallocate(fd, FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE,
155 offset_bytes, sz)) {
156 pr_err("zeroing %s failed: %s\n", dv->devname,
157 strerror(errno));
158 ret = 1;
159 break;
160 }
161
162 offset_bytes += sz;
163 size_bytes -= sz;
164 }
165
166 exit(ret);
167}
168
169static int wait_for_zero_forks(int *zero_pids, int count)
170{
171 int wstatus, ret = 0, i, sfd, wait_count = 0;
172 struct signalfd_siginfo fdsi;
173 bool interrupted = false;
174 sigset_t sigset;
175 ssize_t s;
176
177 for (i = 0; i < count; i++)
178 if (zero_pids[i])
179 wait_count++;
180 if (!wait_count)
181 return 0;
182
183 sigemptyset(&sigset);
184 sigaddset(&sigset, SIGINT);
185 sigaddset(&sigset, SIGCHLD);
186 sigprocmask(SIG_BLOCK, &sigset, NULL);
187
188 sfd = signalfd(-1, &sigset, 0);
189 if (sfd < 0) {
190 pr_err("Unable to create signalfd: %s\n", strerror(errno));
191 return 1;
192 }
193
194 while (1) {
195 s = read(sfd, &fdsi, sizeof(fdsi));
196 if (s != sizeof(fdsi)) {
197 pr_err("Invalid signalfd read: %s\n", strerror(errno));
198 close(sfd);
199 return 1;
200 }
201
202 if (fdsi.ssi_signo == SIGINT) {
203 printf("\n");
204 pr_info("Interrupting zeroing processes, please wait...\n");
205 interrupted = true;
206 } else if (fdsi.ssi_signo == SIGCHLD) {
207 if (!--wait_count)
208 break;
209 }
210 }
211
212 close(sfd);
213
214 for (i = 0; i < count; i++) {
215 if (!zero_pids[i])
216 continue;
217
218 waitpid(zero_pids[i], &wstatus, 0);
219 zero_pids[i] = 0;
220 if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus))
221 ret = 1;
222 }
223
224 if (interrupted) {
225 pr_err("zeroing interrupted!\n");
226 return 1;
227 }
228
229 if (ret)
230 pr_err("zeroing failed!\n");
231 else
232 pr_info("zeroing finished\n");
233
234 return ret;
235}
236
8a4ce2c0
LG
237static int add_disk_to_super(int mdfd, struct shape *s, struct context *c,
238 struct supertype *st, struct mddev_dev *dv,
577fd104
LG
239 struct mdinfo *info, int have_container, int major_num,
240 int *zero_pid)
8a4ce2c0
LG
241{
242 dev_t rdev;
243 int fd;
244
245 if (dv->disposition == 'j') {
246 info->disk.raid_disk = MD_DISK_ROLE_JOURNAL;
247 info->disk.state = (1<<MD_DISK_JOURNAL);
248 } else if (info->disk.raid_disk < s->raiddisks) {
249 info->disk.state = (1<<MD_DISK_ACTIVE) |
250 (1<<MD_DISK_SYNC);
251 } else {
252 info->disk.state = 0;
253 }
254
255 if (dv->writemostly == FlagSet) {
256 if (major_num == BITMAP_MAJOR_CLUSTERED) {
257 pr_err("Can not set %s --write-mostly with a clustered bitmap\n",dv->devname);
258 return 1;
259 } else {
260 info->disk.state |= (1<<MD_DISK_WRITEMOSTLY);
261 }
262
263 }
264
265 if (dv->failfast == FlagSet)
266 info->disk.state |= (1<<MD_DISK_FAILFAST);
267
268 if (have_container) {
269 fd = -1;
270 } else {
271 if (st->ss->external && st->container_devnm[0])
272 fd = open(dv->devname, O_RDWR);
273 else
274 fd = open(dv->devname, O_RDWR|O_EXCL);
275
276 if (fd < 0) {
277 pr_err("failed to open %s after earlier success - aborting\n",
278 dv->devname);
279 return 1;
280 }
281 if (!fstat_is_blkdev(fd, dv->devname, &rdev))
282 return 1;
283 info->disk.major = major(rdev);
284 info->disk.minor = minor(rdev);
285 }
286 if (fd >= 0)
287 remove_partitions(fd);
288 if (st->ss->add_to_super(st, &info->disk, fd, dv->devname,
289 dv->data_offset)) {
290 ioctl(mdfd, STOP_ARRAY, NULL);
291 return 1;
292 }
293 st->ss->getinfo_super(st, info, NULL);
294
577fd104
LG
295 if (fd >= 0 && s->write_zeroes) {
296 *zero_pid = write_zeroes_fork(fd, s, st, dv);
297 if (*zero_pid <= 0) {
298 ioctl(mdfd, STOP_ARRAY, NULL);
299 return 1;
300 }
301 }
302
8a4ce2c0
LG
303 if (have_container && c->verbose > 0)
304 pr_err("Using %s for device %d\n",
305 map_dev(info->disk.major, info->disk.minor, 0),
306 info->disk.number);
307
308 if (!have_container) {
309 /* getinfo_super might have lost these ... */
310 info->disk.major = major(rdev);
311 info->disk.minor = minor(rdev);
312 }
313
314 return 0;
315}
316
317static int update_metadata(int mdfd, struct shape *s, struct supertype *st,
318 struct map_ent **map, struct mdinfo *info,
319 char *chosen_name)
320{
321 struct mdinfo info_new;
322 struct map_ent *me = NULL;
323
324 /* check to see if the uuid has changed due to these
325 * metadata changes, and if so update the member array
326 * and container uuid. Note ->write_init_super clears
327 * the subarray cursor such that ->getinfo_super once
328 * again returns container info.
329 */
330 st->ss->getinfo_super(st, &info_new, NULL);
ef6236da 331 if (st->ss->external && !is_container(s->level) &&
8a4ce2c0
LG
332 !same_uuid(info_new.uuid, info->uuid, 0)) {
333 map_update(map, fd2devnm(mdfd),
334 info_new.text_version,
335 info_new.uuid, chosen_name);
336 me = map_by_devnm(map, st->container_devnm);
337 }
338
339 if (st->ss->write_init_super(st)) {
340 st->ss->free_super(st);
341 return 1;
342 }
343
344 /*
345 * Before activating the array, perform extra steps
346 * required to configure the internal write-intent
347 * bitmap.
348 */
349 if (info_new.consistency_policy == CONSISTENCY_POLICY_BITMAP &&
350 st->ss->set_bitmap && st->ss->set_bitmap(st, info)) {
351 st->ss->free_super(st);
352 return 1;
353 }
354
355 /* update parent container uuid */
356 if (me) {
357 char *path = xstrdup(me->path);
358
359 st->ss->getinfo_super(st, &info_new, NULL);
360 map_update(map, st->container_devnm, info_new.text_version,
361 info_new.uuid, path);
362 free(path);
363 }
364
365 flush_metadata_updates(st);
366 st->ss->free_super(st);
367
368 return 0;
369}
370
371static int add_disks(int mdfd, struct mdinfo *info, struct shape *s,
372 struct context *c, struct supertype *st,
373 struct map_ent **map, struct mddev_dev *devlist,
374 int total_slots, int have_container, int insert_point,
375 int major_num, char *chosen_name)
376{
377 struct mddev_dev *moved_disk = NULL;
378 int pass, raid_disk_num, dnum;
577fd104 379 int zero_pids[total_slots];
8a4ce2c0
LG
380 struct mddev_dev *dv;
381 struct mdinfo *infos;
577fd104 382 sigset_t sigset, orig_sigset;
8a4ce2c0
LG
383 int ret = 0;
384
577fd104
LG
385 /*
386 * Block SIGINT so the main thread will always wait for the
387 * zeroing processes when being interrupted. Otherwise the
388 * zeroing processes will finish their work in the background
389 * keeping the disk busy.
390 */
391 sigemptyset(&sigset);
392 sigaddset(&sigset, SIGINT);
393 sigprocmask(SIG_BLOCK, &sigset, &orig_sigset);
394 memset(zero_pids, 0, sizeof(zero_pids));
395
8a4ce2c0
LG
396 infos = xmalloc(sizeof(*infos) * total_slots);
397 enable_fds(total_slots);
398 for (pass = 1; pass <= 2; pass++) {
399 for (dnum = 0, raid_disk_num = 0, dv = devlist; dv;
400 dv = (dv->next) ? (dv->next) : moved_disk, dnum++) {
401 if (dnum >= total_slots)
402 abort();
403 if (dnum == insert_point) {
404 raid_disk_num += 1;
405 moved_disk = dv;
406 continue;
407 }
408 if (strcasecmp(dv->devname, "missing") == 0) {
409 raid_disk_num += 1;
410 continue;
411 }
412 if (have_container)
413 moved_disk = NULL;
414 if (have_container && dnum < total_slots - 1)
415 /* repeatedly use the container */
416 moved_disk = dv;
417
418 switch(pass) {
419 case 1:
420 infos[dnum] = *info;
421 infos[dnum].disk.number = dnum;
422 infos[dnum].disk.raid_disk = raid_disk_num++;
423
424 if (dv->disposition == 'j')
425 raid_disk_num--;
426
427 ret = add_disk_to_super(mdfd, s, c, st, dv,
428 &infos[dnum], have_container,
577fd104 429 major_num, &zero_pids[dnum]);
8a4ce2c0
LG
430 if (ret)
431 goto out;
432
433 break;
434 case 2:
435 infos[dnum].errors = 0;
436
437 ret = add_disk(mdfd, st, info, &infos[dnum]);
438 if (ret) {
439 pr_err("ADD_NEW_DISK for %s failed: %s\n",
440 dv->devname, strerror(errno));
441 if (errno == EINVAL &&
442 info->array.level == 0) {
443 pr_err("Possibly your kernel doesn't support RAID0 layouts.\n");
444 pr_err("Either upgrade, or use --layout=dangerous\n");
445 }
446 goto out;
447 }
448 break;
449 }
450 if (!have_container &&
451 dv == moved_disk && dnum != insert_point) break;
452 }
453
454 if (pass == 1) {
577fd104
LG
455 ret = wait_for_zero_forks(zero_pids, total_slots);
456 if (ret)
457 goto out;
458
8a4ce2c0
LG
459 ret = update_metadata(mdfd, s, st, map, info,
460 chosen_name);
461 if (ret)
462 goto out;
463 }
464 }
465
466out:
577fd104
LG
467 if (ret)
468 wait_for_zero_forks(zero_pids, total_slots);
8a4ce2c0 469 free(infos);
577fd104 470 sigprocmask(SIG_SETMASK, &orig_sigset, NULL);
8a4ce2c0
LG
471 return ret;
472}
473
7f91af49 474int Create(struct supertype *st, char *mddev,
171dccc8 475 char *name, int *uuid,
a655e550 476 int subdevs, struct mddev_dev *devlist,
99cc42f4 477 struct shape *s,
ae5dfc56 478 struct context *c)
64c4757e 479{
682c7051
NB
480 /*
481 * Create a new raid array.
482 *
483 * First check that necessary details are available
484 * (i.e. level, raid-disks)
485 *
486 * Then check each disk to see what might be on it
487 * and report anything interesting.
488 *
489 * If anything looks odd, and runstop not set,
490 * abort.
491 *
492 * SET_ARRAY_INFO and ADD_NEW_DISK, and
82d9eba6 493 * if runstop==run, or raiddisks disks were used,
682c7051
NB
494 * RUN_ARRAY
495 */
7f91af49 496 int mdfd;
98dbf73c 497 unsigned long long minsize = 0, maxsize = 0;
cd29a5c8
NB
498 char *mindisc = NULL;
499 char *maxdisc = NULL;
8a4ce2c0 500 int dnum;
a655e550 501 struct mddev_dev *dv;
0a6bff09 502 dev_t rdev;
98dbf73c 503 int fail = 0, warn = 0;
82d9eba6 504 int first_missing = subdevs * 2;
6fb79233 505 int second_missing = subdevs * 2;
52826846 506 int missing_disks = 0;
82d9eba6 507 int insert_point = subdevs * 2; /* where to insert a missing drive */
d2ca6449 508 int total_slots;
82d9eba6 509 int rv;
c82f047c 510 int bitmap_fd;
5f8097be 511 int have_container = 0;
0e600426 512 int container_fd = -1;
a931db9e 513 int need_mdmon = 0;
f9c25f1d 514 unsigned long long bitmapsize;
8a4ce2c0 515 struct mdinfo info;
b8ac1967 516 int did_default = 0;
a18a888e 517 int do_default_layout = 0;
c21e737b 518 int do_default_chunk = 0;
a04d5763 519 char chosen_name[1024];
ad5bc697 520 struct map_ent *map = NULL;
8592f29d 521 unsigned long long newsize;
5f4cc239 522 mdu_array_info_t inf;
682c7051 523
943eafef 524 int major_num = BITMAP_MAJOR_HI;
82d9485e 525 if (s->bitmap_file && strcmp(s->bitmap_file, "clustered") == 0) {
6d9c7c25 526 major_num = BITMAP_MAJOR_CLUSTERED;
82d9485e
GJ
527 if (c->nodes <= 1) {
528 pr_err("At least 2 nodes are needed for cluster-md\n");
529 return 1;
530 }
531 }
dcec9ee5 532
06c7f68e 533 memset(&info, 0, sizeof(info));
99cc42f4
N
534 if (s->level == UnSet && st && st->ss->default_geometry)
535 st->ss->default_geometry(st, &s->level, NULL, NULL);
536 if (s->level == UnSet) {
e7b84f9d 537 pr_err("a RAID level is needed to create an array.\n");
682c7051
NB
538 return 1;
539 }
99cc42f4 540 if (s->raiddisks < 4 && s->level == 6) {
e7b84f9d 541 pr_err("at least 4 raid-devices needed for level 6\n");
98c6faba
NB
542 return 1;
543 }
99cc42f4 544 if (s->raiddisks > 256 && s->level == 6) {
e7b84f9d 545 pr_err("no more than 256 raid-devices supported for level 6\n");
98c6faba
NB
546 return 1;
547 }
99cc42f4 548 if (s->raiddisks < 2 && s->level >= 4) {
f7889e51 549 pr_err("at least 2 raid-devices needed for level %d\n", s->level);
52826846
NB
550 return 1;
551 }
99cc42f4 552 if (s->level <= 0 && s->sparedisks) {
e7b84f9d 553 pr_err("This level does not support spare devices\n");
570510ba
NB
554 return 1;
555 }
5f8097be
NB
556
557 if (subdevs == 1 && strcmp(devlist->devname, "missing") != 0) {
558 /* If given a single device, it might be a container, and we can
559 * extract a device list from there
560 */
5f8097be
NB
561 int fd;
562
563 memset(&inf, 0, sizeof(inf));
37ea3936 564 fd = open(devlist->devname, O_RDONLY);
5f8097be 565 if (fd >= 0 &&
9cd39f01 566 md_get_array_info(fd, &inf) == 0 && inf.raid_disks == 0) {
5f8097be
NB
567 /* yep, looks like a container */
568 if (st) {
0fb69d1d
N
569 rv = st->ss->load_container(st, fd,
570 devlist->devname);
5f8097be
NB
571 if (rv == 0)
572 have_container = 1;
573 } else {
0fb69d1d 574 st = super_by_fd(fd, NULL);
5f8097be 575 if (st && !(rv = st->ss->
0fb69d1d
N
576 load_container(st, fd,
577 devlist->devname)))
5f8097be
NB
578 have_container = 1;
579 else
580 st = NULL;
581 }
18fde300 582 if (have_container) {
99cc42f4 583 subdevs = s->raiddisks;
18fde300
DW
584 first_missing = subdevs * 2;
585 second_missing = subdevs * 2;
586 insert_point = subdevs * 2;
587 }
5f8097be
NB
588 }
589 if (fd >= 0)
590 close(fd);
5f8097be 591 }
99cc42f4 592 if (st && st->ss->external && s->sparedisks) {
7a862a02 593 pr_err("This metadata type does not support spare disks at create time\n");
ffcfc735
N
594 return 1;
595 }
cc1799c3 596 if (subdevs > s->raiddisks+s->sparedisks+s->journaldisks) {
99cc42f4 597 pr_err("You have listed more devices (%d) than are in the array(%d)!\n", subdevs, s->raiddisks+s->sparedisks);
52826846 598 return 1;
682c7051 599 }
cc1799c3 600 if (!have_container && subdevs < s->raiddisks+s->sparedisks+s->journaldisks) {
e7b84f9d 601 pr_err("You haven't given enough devices (real or missing) to create this array\n");
52826846
NB
602 return 1;
603 }
99cc42f4 604 if (s->bitmap_file && s->level <= 0) {
e7b84f9d 605 pr_err("bitmaps not meaningful with level %s\n",
99cc42f4 606 map_num(pers, s->level)?:"given");
5b28bd56
NB
607 return 1;
608 }
52826846 609
682c7051 610 /* now set some defaults */
b5e64645 611
99cc42f4 612 if (s->layout == UnSet) {
a18a888e 613 do_default_layout = 1;
99cc42f4 614 s->layout = default_layout(st, s->level, c->verbose);
a18a888e 615 }
682c7051 616
99cc42f4 617 if (s->level == 10)
e5329c37 618 /* check layout fits in array*/
99cc42f4 619 if ((s->layout&255) * ((s->layout>>8)&255) > s->raiddisks) {
e7b84f9d 620 pr_err("that layout requires at least %d devices\n",
99cc42f4 621 (s->layout&255) * ((s->layout>>8)&255));
e5329c37
NB
622 return 1;
623 }
624
99cc42f4 625 switch(s->level) {
aa88f531
NB
626 case 4:
627 case 5:
e5329c37 628 case 10:
98c6faba 629 case 6:
aa88f531 630 case 0:
99cc42f4
N
631 if (s->chunk == 0 || s->chunk == UnSet) {
632 s->chunk = UnSet;
c21e737b
CA
633 do_default_chunk = 1;
634 /* chunk will be set later */
bb7295f1 635 }
5f175898
N
636 break;
637 case LEVEL_LINEAR:
638 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
aa88f531 639 break;
570510ba 640 case 1:
08e43379 641 case LEVEL_FAULTY:
570510ba 642 case LEVEL_MULTIPATH:
17f25ca6 643 case LEVEL_CONTAINER:
99cc42f4 644 if (s->chunk) {
5b30a34a
MG
645 pr_err("specifying chunk size is forbidden for this level\n");
646 return 1;
aa88f531
NB
647 }
648 break;
570510ba 649 default:
99cc42f4 650 pr_err("unknown level %d\n", s->level);
570510ba 651 return 1;
682c7051 652 }
22dc741f 653
99cc42f4 654 if (s->size == MAX_SIZE)
d04f65f4 655 /* use '0' to mean 'max' now... */
99cc42f4
N
656 s->size = 0;
657 if (s->size && s->chunk && s->chunk != UnSet)
22dc741f
MT
658 if (round_size_and_verify(&s->size, s->chunk))
659 return 1;
660
99cc42f4
N
661 newsize = s->size * 2;
662 if (st && ! st->ss->validate_geometry(st, s->level, s->layout, s->raiddisks,
af4348dd 663 &s->chunk, s->size*2,
ae5dfc56 664 s->data_offset, NULL,
5308f117 665 &newsize, s->consistency_policy,
98dbf73c 666 c->verbose >= 0))
17f25ca6 667 return 1;
bb7295f1 668
99cc42f4
N
669 if (s->chunk && s->chunk != UnSet) {
670 newsize &= ~(unsigned long long)(s->chunk*2 - 1);
64385908
CA
671 if (do_default_chunk) {
672 /* default chunk was just set */
171dccc8 673 if (c->verbose > 0)
7a862a02 674 pr_err("chunk size defaults to %dK\n", s->chunk);
22dc741f
MT
675 if (round_size_and_verify(&s->size, s->chunk))
676 return 1;
64385908
CA
677 do_default_chunk = 0;
678 }
bb7295f1 679 }
64385908 680
99cc42f4
N
681 if (s->size == 0) {
682 s->size = newsize / 2;
683 if (s->level == 1)
ae6c05ad
N
684 /* If this is ever reshaped to RAID5, we will
685 * need a chunksize. So round it off a bit
686 * now just to be safe
687 */
99cc42f4 688 s->size &= ~(64ULL-1);
ae6c05ad 689
99cc42f4
N
690 if (s->size && c->verbose > 0)
691 pr_err("setting size to %lluK\n", s->size);
8592f29d 692 }
17f25ca6 693
682c7051 694 /* now look at the subdevs */
06c7f68e
NB
695 info.array.active_disks = 0;
696 info.array.working_disks = 0;
c913b90e 697 dnum = 0;
98dbf73c 698 for (dv = devlist; dv; dv = dv->next)
ae5dfc56 699 if (s->data_offset == VARIABLE_OFFSET)
476066a3
N
700 dv->data_offset = INVALID_SECTORS;
701 else
ae5dfc56 702 dv->data_offset = s->data_offset;
476066a3 703
8592f29d 704 for (dv=devlist; dv && !have_container; dv=dv->next, dnum++) {
cd29a5c8 705 char *dname = dv->devname;
17f25ca6 706 unsigned long long freesize;
e9b11fee 707 int dfd;
72ca9bcf 708 char *doff;
e9b11fee 709
98dbf73c 710 if (strcasecmp(dname, "missing") == 0) {
c913b90e
NB
711 if (first_missing > dnum)
712 first_missing = dnum;
6fb79233
NB
713 if (second_missing > dnum && dnum > first_missing)
714 second_missing = dnum;
52826846
NB
715 missing_disks ++;
716 continue;
717 }
ae5dfc56 718 if (s->data_offset == VARIABLE_OFFSET) {
72ca9bcf
N
719 doff = strchr(dname, ':');
720 if (doff) {
721 *doff++ = 0;
722 dv->data_offset = parse_size(doff);
723 } else
724 dv->data_offset = INVALID_SECTORS;
725 } else
ae5dfc56 726 dv->data_offset = s->data_offset;
72ca9bcf 727
58b3c697 728 dfd = open(dname, O_RDONLY);
e9b11fee 729 if (dfd < 0) {
e7b84f9d 730 pr_err("cannot open %s: %s\n",
e9b11fee
N
731 dname, strerror(errno));
732 exit(2);
733 }
0a6bff09 734 if (!fstat_is_blkdev(dfd, dname, NULL)) {
e9b11fee 735 close(dfd);
e9b11fee
N
736 exit(2);
737 }
738 close(dfd);
06c7f68e 739 info.array.working_disks++;
dfd7822c 740 if (dnum < s->raiddisks && dv->disposition != 'j')
06c7f68e 741 info.array.active_disks++;
058574b1 742 if (st == NULL) {
8aec876d 743 struct createinfo *ci = conf_get_create_info();
058574b1
NB
744 if (ci)
745 st = ci->supertype;
746 }
576d6d83
NB
747 if (st == NULL) {
748 /* Need to choose a default metadata, which is different
17f25ca6 749 * depending on geometry of array.
576d6d83
NB
750 */
751 int i;
752 char *name = "default";
98dbf73c 753 for(i = 0; !st && superlist[i]; i++) {
576d6d83 754 st = superlist[i]->match_metadata_desc(name);
ecbd9e81
N
755 if (!st)
756 continue;
a18a888e 757 if (do_default_layout)
99cc42f4 758 s->layout = default_layout(st, s->level, c->verbose);
ecbd9e81 759 switch (st->ss->validate_geometry(
99cc42f4 760 st, s->level, s->layout, s->raiddisks,
af4348dd 761 &s->chunk, s->size*2,
72ca9bcf 762 dv->data_offset, dname,
5308f117
AP
763 &freesize, s->consistency_policy,
764 c->verbose > 0)) {
ecbd9e81
N
765 case -1: /* Not valid, message printed, and not
766 * worth checking any further */
767 exit(2);
768 break;
769 case 0: /* Geometry not valid */
55425f27 770 free(st);
17f25ca6 771 st = NULL;
99cc42f4 772 s->chunk = do_default_chunk ? UnSet : s->chunk;
ecbd9e81
N
773 break;
774 case 1: /* All happy */
775 break;
55425f27 776 }
17f25ca6 777 }
576d6d83
NB
778
779 if (!st) {
58b3c697
N
780 int dfd = open(dname, O_RDONLY|O_EXCL);
781 if (dfd < 0) {
e7b84f9d 782 pr_err("cannot open %s: %s\n",
58b3c697
N
783 dname, strerror(errno));
784 exit(2);
785 }
7a862a02 786 pr_err("device %s not suitable for any style of array\n",
17f25ca6 787 dname);
576d6d83
NB
788 exit(2);
789 }
b8ac1967 790 if (st->ss != &super0 ||
576d6d83 791 st->minor_version != 90)
b8ac1967 792 did_default = 1;
17f25ca6 793 } else {
a18a888e 794 if (do_default_layout)
99cc42f4
N
795 s->layout = default_layout(st, s->level, 0);
796 if (!st->ss->validate_geometry(st, s->level, s->layout,
797 s->raiddisks,
af4348dd 798 &s->chunk, s->size*2,
72ca9bcf 799 dv->data_offset,
af4348dd 800 dname, &freesize,
5308f117 801 s->consistency_policy,
171dccc8 802 c->verbose >= 0)) {
17f25ca6 803
7a862a02 804 pr_err("%s is not suitable for this array.\n",
e7b84f9d 805 dname);
17f25ca6
NB
806 fail = 1;
807 continue;
808 }
682c7051 809 }
82d9eba6 810
cc1799c3 811 if (dv->disposition == 'j')
dfd7822c 812 goto skip_size_check; /* skip write journal for size check */
cc1799c3 813
82d9eba6 814 freesize /= 2; /* convert to K */
99cc42f4 815 if (s->chunk && s->chunk != UnSet) {
d2cd3ffc 816 /* round to chunk size */
99cc42f4 817 freesize = freesize & ~(s->chunk-1);
64385908
CA
818 if (do_default_chunk) {
819 /* default chunk was just set */
171dccc8 820 if (c->verbose > 0)
7a862a02 821 pr_err("chunk size defaults to %dK\n", s->chunk);
22dc741f
MT
822 if (round_size_and_verify(&s->size, s->chunk))
823 return 1;
64385908
CA
824 do_default_chunk = 0;
825 }
d2cd3ffc 826 }
066e92f0
LD
827 if (!freesize) {
828 pr_err("no free space left on %s\n", dname);
829 fail = 1;
830 continue;
831 }
682c7051 832
99cc42f4 833 if (s->size && freesize < s->size) {
7a862a02 834 pr_err("%s is smaller than given size. %lluK < %lluK + metadata\n",
99cc42f4 835 dname, freesize, s->size);
52826846 836 fail = 1;
52826846 837 continue;
682c7051 838 }
cd29a5c8
NB
839 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
840 maxdisc = dname;
52826846 841 maxsize = freesize;
682c7051 842 }
cd29a5c8
NB
843 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
844 mindisc = dname;
52826846 845 minsize = freesize;
682c7051 846 }
dfd7822c 847 skip_size_check:
171dccc8 848 if (c->runstop != 1 || c->verbose >= 0) {
37ea3936 849 int fd = open(dname, O_RDONLY);
98dbf73c 850 if (fd < 0) {
e7b84f9d 851 pr_err("Cannot open %s: %s\n",
b6e63da4 852 dname, strerror(errno));
98dbf73c 853 fail = 1;
b6e63da4
NB
854 continue;
855 }
dab6685f
NB
856 warn |= check_ext2(fd, dname);
857 warn |= check_reiser(fd, dname);
858 warn |= check_raid(fd, dname);
034b203a
TM
859 if (strcmp(st->ss->name, "1.x") == 0 &&
860 st->minor_version >= 1)
861 /* metadata at front */
53ed6ac3 862 warn |= check_partitions(fd, dname, 0, 0);
6f2af6a4 863 else if (s->level == 1 || is_container(s->level) ||
cf622ec1 864 (s->level == 0 && s->raiddisks == 1))
034b203a 865 /* partitions could be meaningful */
99cc42f4 866 warn |= check_partitions(fd, dname, freesize*2, s->size*2);
034b203a
TM
867 else
868 /* partitions cannot be meaningful */
53ed6ac3 869 warn |= check_partitions(fd, dname, 0, 0);
034b203a 870 if (strcmp(st->ss->name, "1.x") == 0 &&
a0962fe9
N
871 st->minor_version >= 1 &&
872 did_default &&
99cc42f4 873 s->level == 1 &&
034b203a
TM
874 (warn & 1024) == 0) {
875 warn |= 1024;
e7b84f9d 876 pr_err("Note: this array has metadata at the start and\n"
a0962fe9 877 " may not be suitable as a boot device. If you plan to\n"
cc86f89c 878 " store '/boot' on this device please ensure that\n"
a0962fe9 879 " your boot-loader understands md/v1.x metadata, or use\n"
cc86f89c 880 " --metadata=0.90\n");
a0962fe9 881 }
b6e63da4 882 close(fd);
dab6685f 883 }
682c7051 884 }
b91ad097 885 if (missing_disks == dnum && !have_container) {
1db03765
XN
886 pr_err("Subdevs can't be all missing\n");
887 return 1;
888 }
99cc42f4 889 if (s->raiddisks + s->sparedisks > st->max_devs) {
7a862a02 890 pr_err("Too many devices: %s metadata only supports %d\n",
acab7bb1
N
891 st->ss->name, st->max_devs);
892 return 1;
893 }
8592f29d 894 if (have_container)
99cc42f4 895 info.array.working_disks = s->raiddisks;
682c7051 896 if (fail) {
e7b84f9d 897 pr_err("create aborted\n");
52826846 898 return 1;
682c7051 899 }
99cc42f4 900 if (s->size == 0) {
5f8097be 901 if (mindisc == NULL && !have_container) {
e7b84f9d 902 pr_err("no size and no drives given - aborting create.\n");
52826846
NB
903 return 1;
904 }
cf622ec1 905 if (s->level > 0 || s->level == LEVEL_MULTIPATH ||
98dbf73c 906 s->level == LEVEL_FAULTY || st->ss->external) {
b5e64645 907 /* size is meaningful */
99cc42f4
N
908 if (!st->ss->validate_geometry(st, s->level, s->layout,
909 s->raiddisks,
910 &s->chunk, minsize*2,
ae5dfc56 911 s->data_offset,
5308f117
AP
912 NULL, NULL,
913 s->consistency_policy, 0)) {
99cc42f4 914 pr_err("devices too large for RAID level %d\n", s->level);
b5e64645
NB
915 return 1;
916 }
99cc42f4
N
917 s->size = minsize;
918 if (s->level == 1)
ae6c05ad
N
919 /* If this is ever reshaped to RAID5, we will
920 * need a chunksize. So round it off a bit
921 * now just to be safe
922 */
99cc42f4 923 s->size &= ~(64ULL-1);
171dccc8 924 if (c->verbose > 0)
99cc42f4 925 pr_err("size set to %lluK\n", s->size);
b5e64645 926 }
682c7051 927 }
748952f7
N
928
929 if (!s->bitmap_file &&
848d71c9 930 !st->ss->external &&
748952f7 931 s->level >= 1 &&
39917e56 932 st->ss->add_internal_bitmap &&
2ce09172 933 s->journaldisks == 0 &&
e97a7cd0
AP
934 (s->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
935 s->consistency_policy != CONSISTENCY_POLICY_PPL) &&
748952f7
N
936 (s->write_behind || s->size > 100*1024*1024ULL)) {
937 if (c->verbose > 0)
938 pr_err("automatically enabling write-intent bitmap on large array\n");
939 s->bitmap_file = "internal";
940 }
941 if (s->bitmap_file && strcmp(s->bitmap_file, "none") == 0)
942 s->bitmap_file = NULL;
943
5308f117
AP
944 if (s->consistency_policy == CONSISTENCY_POLICY_PPL &&
945 !st->ss->write_init_ppl) {
946 pr_err("%s metadata does not support PPL\n", st->ss->name);
947 return 1;
948 }
949
99cc42f4 950 if (!have_container && s->level > 0 && ((maxsize-s->size)*100 > maxsize)) {
171dccc8 951 if (c->runstop != 1 || c->verbose >= 0)
e7b84f9d 952 pr_err("largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
99cc42f4 953 maxdisc, s->size);
52826846 954 warn = 1;
682c7051
NB
955 }
956
9eafa1de 957 if (st->ss->detail_platform && st->ss->detail_platform(0, 1, NULL) != 0) {
171dccc8 958 if (c->runstop != 1 || c->verbose >= 0)
e7b84f9d 959 pr_err("%s unable to enumerate platform support\n"
5615172f
DW
960 " array may not be compatible with hardware/firmware\n",
961 st->ss->name);
962 warn = 1;
963 }
529e2aa5 964 st->nodes = c->nodes;
7716570e 965 st->cluster_name = c->homecluster;
5615172f 966
682c7051 967 if (warn) {
171dccc8 968 if (c->runstop!= 1) {
52826846 969 if (!ask("Continue creating array? ")) {
e7b84f9d 970 pr_err("create aborted.\n");
52826846
NB
971 return 1;
972 }
973 } else {
171dccc8 974 if (c->verbose > 0)
e7b84f9d 975 pr_err("creation continuing despite oddities due to --run\n");
682c7051 976 }
682c7051
NB
977 }
978
66f8bbbe 979 /* If this is raid4/5, we want to configure the last active slot
52826846 980 * as missing, so that a reconstruct happens (faster than re-parity)
98c6faba 981 * FIX: Can we do this for raid6 as well?
52826846 982 */
98dbf73c
JS
983 if (st->ss->external == 0 && s->assume_clean == 0 &&
984 c->force == 0 && first_missing >= s->raiddisks) {
985 switch (s->level) {
66f8bbbe 986 case 4:
98c6faba 987 case 5:
99cc42f4
N
988 insert_point = s->raiddisks-1;
989 s->sparedisks++;
06c7f68e 990 info.array.active_disks--;
98c6faba
NB
991 missing_disks++;
992 break;
993 default:
994 break;
995 }
52826846 996 }
6fb79233
NB
997 /* For raid6, if creating with 1 missing drive, make a good drive
998 * into a spare, else the create will fail
999 */
99cc42f4 1000 if (s->assume_clean == 0 && c->force == 0 && first_missing < s->raiddisks &&
ffcfc735 1001 st->ss->external == 0 &&
99cc42f4
N
1002 second_missing >= s->raiddisks && s->level == 6) {
1003 insert_point = s->raiddisks - 1;
6fb79233
NB
1004 if (insert_point == first_missing)
1005 insert_point--;
99cc42f4 1006 s->sparedisks ++;
6fb79233
NB
1007 info.array.active_disks--;
1008 missing_disks++;
1009 }
570510ba 1010
99cc42f4 1011 if (s->level <= 0 && first_missing < subdevs * 2) {
e7b84f9d 1012 pr_err("This level does not support missing devices\n");
570510ba
NB
1013 return 1;
1014 }
aba69144 1015
7f91af49 1016 /* We need to create the device */
ad5bc697 1017 map_lock(&map);
cd6cbb08 1018 mdfd = create_mddev(mddev, name, c->autof, LOCAL, chosen_name, 1);
e06af9dd
JS
1019 if (mdfd < 0) {
1020 map_unlock(&map);
7f91af49 1021 return 1;
e06af9dd 1022 }
3e9df86a
AK
1023 /* verify if chosen_name is not in use,
1024 * it could be in conflict with already existing device
1025 * e.g. container, array
1026 */
b9ce7ab0
MT
1027 if (strncmp(chosen_name, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0 &&
1028 map_by_name(&map, chosen_name + DEV_MD_DIR_LEN)) {
1029 pr_err("Array name %s is in use already.\n", chosen_name);
3e9df86a
AK
1030 close(mdfd);
1031 map_unlock(&map);
cd6cbb08 1032 udev_unblock();
3e9df86a
AK
1033 return 1;
1034 }
f2be09f1 1035 mddev = chosen_name;
7f91af49 1036
5f4cc239
JS
1037 memset(&inf, 0, sizeof(inf));
1038 md_get_array_info(mdfd, &inf);
1039 if (inf.working_disks != 0) {
1040 pr_err("another array by this name is already running.\n");
e06af9dd 1041 goto abort_locked;
7f91af49 1042 }
a04d5763 1043
682c7051
NB
1044 /* Ok, lets try some ioctls */
1045
99cc42f4
N
1046 info.array.level = s->level;
1047 info.array.size = s->size;
1048 info.array.raid_disks = s->raiddisks;
82b27616
NB
1049 /* The kernel should *know* what md_minor we are dealing
1050 * with, but it chooses to trust me instead. Sigh
1051 */
06c7f68e 1052 info.array.md_minor = 0;
0a6bff09
ZL
1053 if (fstat_is_blkdev(mdfd, mddev, &rdev))
1054 info.array.md_minor = minor(rdev);
06c7f68e 1055 info.array.not_persistent = 0;
6fb79233 1056
cf622ec1
JS
1057 if (((s->level == 4 || s->level == 5) &&
1058 (insert_point < s->raiddisks || first_missing < s->raiddisks)) ||
1059 (s->level == 6 && (insert_point < s->raiddisks ||
1060 second_missing < s->raiddisks)) ||
1061 (s->level <= 0) || s->assume_clean) {
06c7f68e 1062 info.array.state = 1; /* clean, but one+ drive will be missing*/
b7528a20 1063 info.resync_start = MaxSector;
103f2410 1064 } else {
06c7f68e 1065 info.array.state = 0; /* not clean, but no errors */
103f2410
NB
1066 info.resync_start = 0;
1067 }
99cc42f4 1068 if (s->level == 10) {
f9c25f1d
NB
1069 /* for raid10, the bitmap size is the capacity of the array,
1070 * which is array.size * raid_disks / ncopies;
1071 * .. but convert to sectors.
1072 */
99cc42f4
N
1073 int ncopies = ((s->layout>>8) & 255) * (s->layout & 255);
1074 bitmapsize = s->size * s->raiddisks / ncopies * 2;
1075/* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, s->size, s->raiddisks, ncopies);*/
f9c25f1d 1076 } else
99cc42f4 1077 bitmapsize = s->size * 2;
f9c25f1d 1078
82b27616
NB
1079 /* There is lots of redundancy in these disk counts,
1080 * raid_disks is the most meaningful value
1081 * it describes the geometry of the array
1082 * it is constant
1083 * nr_disks is total number of used slots.
1084 * it should be raid_disks+spare_disks
1085 * spare_disks is the number of extra disks present
1086 * see above
1087 * active_disks is the number of working disks in
1088 * active slots. (With raid_disks)
1089 * working_disks is the total number of working disks,
1090 * including spares
1091 * failed_disks is the number of disks marked failed
1092 *
5d500228 1093 * Ideally, the kernel would keep these (except raid_disks)
82b27616
NB
1094 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
1095 * So for now, we assume that all raid and spare
1096 * devices will be given.
1097 */
99cc42f4 1098 info.array.spare_disks=s->sparedisks;
06c7f68e
NB
1099 info.array.failed_disks=missing_disks;
1100 info.array.nr_disks = info.array.working_disks
1101 + info.array.failed_disks;
99cc42f4
N
1102 info.array.layout = s->layout;
1103 info.array.chunk_size = s->chunk*1024;
82d9eba6 1104
b3b33eb5
NB
1105 if (name == NULL || *name == 0) {
1106 /* base name on mddev */
d1e80164
NB
1107 /* /dev/md0 -> 0
1108 * /dev/md_d0 -> d0
eca944fa 1109 * /dev/md_foo -> foo
d1e80164
NB
1110 * /dev/md/1 -> 1
1111 * /dev/md/d1 -> d1
1112 * /dev/md/home -> home
1113 * /dev/mdhome -> home
1114 */
69207ff6 1115 /* FIXME compare this with rules in create_mddev */
b3b33eb5
NB
1116 name = strrchr(mddev, '/');
1117 if (name) {
1118 name++;
98dbf73c
JS
1119 if (strncmp(name, "md_", 3) == 0 &&
1120 strlen(name) > 3 && (name-mddev) == 5 /* /dev/ */)
d1e80164 1121 name += 3;
98dbf73c
JS
1122 else if (strncmp(name, "md", 2) == 0 &&
1123 strlen(name) > 2 && isdigit(name[2]) &&
60248f74 1124 (name-mddev) == 5 /* /dev/ */)
b3b33eb5
NB
1125 name += 2;
1126 }
1127 }
5308f117 1128 if (!st->ss->init_super(st, &info.array, s, name, c->homehost, uuid,
ae5dfc56 1129 s->data_offset))
e06af9dd 1130 goto abort_locked;
682c7051 1131
d2ca6449 1132 total_slots = info.array.nr_disks;
a5d85af7 1133 st->ss->getinfo_super(st, &info, NULL);
dae13137
JS
1134 if (sysfs_init(&info, mdfd, NULL)) {
1135 pr_err("unable to initialize sysfs\n");
1136 goto abort_locked;
1137 }
159c3a1a 1138
171dccc8 1139 if (did_default && c->verbose >= 0) {
3c558363 1140 if (is_subarray(info.text_version)) {
4dd2df09
N
1141 char devnm[32];
1142 char *ep;
f7f1b6a1 1143 struct mdinfo *mdi;
f7f1b6a1 1144
4dd2df09
N
1145 strncpy(devnm, info.text_version+1, 32);
1146 devnm[31] = 0;
1147 ep = strchr(devnm, '/');
1148 if (ep)
1149 *ep = 0;
1150
1151 mdi = sysfs_read(-1, devnm, GET_VERSION);
f7f1b6a1 1152
9364dbfb 1153 pr_info("Creating array inside %s container %s\n",
4dd2df09 1154 mdi?mdi->text_version:"managed", devnm);
f7f1b6a1
NB
1155 sysfs_free(mdi);
1156 } else
9364dbfb
LG
1157 pr_info("Defaulting to version %s metadata\n",
1158 info.text_version);
f7f1b6a1 1159 }
b8ac1967 1160
4dd2df09 1161 map_update(&map, fd2devnm(mdfd), info.text_version,
a04d5763 1162 info.uuid, chosen_name);
19ad4b2c
AP
1163 /* Keep map locked until devices have been added to array
1164 * to stop another mdadm from finding and using those devices.
1165 */
a04d5763 1166
98dbf73c
JS
1167 if (s->bitmap_file && (strcmp(s->bitmap_file, "internal") == 0 ||
1168 strcmp(s->bitmap_file, "clustered") == 0)) {
ebeb3663 1169 if (!st->ss->add_internal_bitmap) {
e7b84f9d 1170 pr_err("internal bitmaps not supported with %s metadata\n",
ebeb3663 1171 st->ss->name);
19ad4b2c 1172 goto abort_locked;
ebeb3663 1173 }
2ec2b7e9
JS
1174 if (st->ss->add_internal_bitmap(st, &s->bitmap_chunk,
1175 c->delay, s->write_behind,
1176 bitmapsize, 1, major_num)) {
e7b84f9d 1177 pr_err("Given bitmap chunk size not supported.\n");
19ad4b2c 1178 goto abort_locked;
55935d51 1179 }
99cc42f4 1180 s->bitmap_file = NULL;
55935d51
NB
1181 }
1182
dae13137
JS
1183 if (sysfs_init(&info, mdfd, NULL)) {
1184 pr_err("unable to initialize sysfs\n");
1185 goto abort_locked;
1186 }
55935d51 1187
4dd2df09 1188 if (st->ss->external && st->container_devnm[0]) {
f35f2525
N
1189 /* member */
1190
1191 /* When creating a member, we need to be careful
1192 * to negotiate with mdmon properly.
1193 * If it is already running, we cannot write to
1194 * the devices and must ask it to do that part.
1195 * If it isn't running, we write to the devices,
1196 * and then start it.
1197 * We hold an exclusive open on the container
1198 * device to make sure mdmon doesn't exit after
1199 * we checked that it is running.
1200 *
1201 * For now, fail if it is already running.
1202 */
4dd2df09 1203 container_fd = open_dev_excl(st->container_devnm);
f35f2525 1204 if (container_fd < 0) {
7a862a02 1205 pr_err("Cannot get exclusive open on container - weird.\n");
19ad4b2c 1206 goto abort_locked;
d03373f1 1207 }
4dd2df09 1208 if (mdmon_running(st->container_devnm)) {
171dccc8 1209 if (c->verbose)
7a862a02 1210 pr_err("reusing mdmon for %s.\n",
4dd2df09 1211 st->container_devnm);
f35f2525
N
1212 st->update_tail = &st->updates;
1213 } else
1214 need_mdmon = 1;
1215 }
1216 rv = set_array_info(mdfd, st, &info);
82d9eba6 1217 if (rv) {
e7b84f9d 1218 pr_err("failed to set array info for %s: %s\n",
52826846 1219 mddev, strerror(errno));
19ad4b2c 1220 goto abort_locked;
682c7051 1221 }
52826846 1222
99cc42f4 1223 if (s->bitmap_file) {
c82f047c 1224 int uuid[4];
55935d51 1225
3da92f27 1226 st->ss->uuid_from_super(st, uuid);
99cc42f4
N
1227 if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid, s->bitmap_chunk,
1228 c->delay, s->write_behind,
f9c25f1d 1229 bitmapsize,
943eafef 1230 major_num)) {
19ad4b2c 1231 goto abort_locked;
c82f047c 1232 }
99cc42f4 1233 bitmap_fd = open(s->bitmap_file, O_RDWR);
c82f047c 1234 if (bitmap_fd < 0) {
ebf3be99 1235 pr_err("weird: %s cannot be opened\n",
99cc42f4 1236 s->bitmap_file);
19ad4b2c 1237 goto abort_locked;
c82f047c
NB
1238 }
1239 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
e7b84f9d 1240 pr_err("Cannot set bitmap file for %s: %s\n",
c82f047c 1241 mddev, strerror(errno));
19ad4b2c 1242 goto abort_locked;
c82f047c
NB
1243 }
1244 }
1245
8a4ce2c0
LG
1246 if (add_disks(mdfd, &info, s, c, st, &map, devlist, total_slots,
1247 have_container, insert_point, major_num, chosen_name))
1248 goto abort_locked;
9b1fb677 1249
19ad4b2c 1250 map_unlock(&map);
5e52ae9e 1251
6f2af6a4 1252 if (is_container(s->level)) {
97590376
N
1253 /* No need to start. But we should signal udev to
1254 * create links */
1255 sysfs_uevent(&info, "change");
171dccc8 1256 if (c->verbose >= 0)
e7b84f9d 1257 pr_err("container %s prepared.\n", mddev);
a7c6e3fb 1258 wait_for(chosen_name, mdfd);
99cc42f4 1259 } else if (c->runstop == 1 || subdevs >= s->raiddisks) {
598f0d58 1260 if (st->ss->external) {
4c821454 1261 int err;
99cc42f4 1262 switch(s->level) {
598f0d58
NB
1263 case LEVEL_LINEAR:
1264 case LEVEL_MULTIPATH:
1265 case 0:
4c821454 1266 err = sysfs_set_str(&info, NULL, "array_state",
171dccc8 1267 c->readonly
72d566f6
N
1268 ? "readonly"
1269 : "active");
a931db9e 1270 need_mdmon = 0;
598f0d58
NB
1271 break;
1272 default:
4c821454
N
1273 err = sysfs_set_str(&info, NULL, "array_state",
1274 "readonly");
598f0d58
NB
1275 break;
1276 }
fb2c0f61 1277 sysfs_set_safemode(&info, info.safe_mode_delay);
4c821454 1278 if (err) {
7a862a02 1279 pr_err("failed to activate array.\n");
4c821454
N
1280 ioctl(mdfd, STOP_ARRAY, NULL);
1281 goto abort;
1282 }
171dccc8 1283 } else if (c->readonly &&
72d566f6
N
1284 sysfs_attribute_available(
1285 &info, NULL, "array_state")) {
1286 if (sysfs_set_str(&info, NULL,
1287 "array_state", "readonly") < 0) {
1288 pr_err("Failed to start array: %s\n",
1289 strerror(errno));
1290 ioctl(mdfd, STOP_ARRAY, NULL);
1291 goto abort;
1292 }
598f0d58 1293 } else {
97590376 1294 /* param is not actually used */
598f0d58
NB
1295 mdu_param_t param;
1296 if (ioctl(mdfd, RUN_ARRAY, &param)) {
e7b84f9d 1297 pr_err("RUN_ARRAY failed: %s\n",
72d566f6 1298 strerror(errno));
329dfc28
N
1299 if (errno == 524 /* ENOTSUP */ &&
1300 info.array.level == 0)
1301 cont_err("Please use --layout=original or --layout=alternate\n");
a252c078 1302 if (info.array.chunk_size & (info.array.chunk_size-1)) {
7a862a02 1303 cont_err("Problem may be that chunk size is not a power of 2\n");
a252c078 1304 }
4eb26970 1305 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1306 goto abort;
598f0d58 1307 }
a21e848a
N
1308 /* if start_ro module parameter is set, array is
1309 * auto-read-only, which is bad as the resync won't
1310 * start. So lets make it read-write now.
1311 */
1312 ioctl(mdfd, RESTART_ARRAY_RW, NULL);
682c7051 1313 }
171dccc8 1314 if (c->verbose >= 0)
9364dbfb 1315 pr_info("array %s started.\n", mddev);
4dd2df09 1316 if (st->ss->external && st->container_devnm[0]) {
8850ee3e 1317 if (need_mdmon)
4dd2df09 1318 start_mdmon(st->container_devnm);
8850ee3e 1319
4dd2df09 1320 ping_monitor(st->container_devnm);
a931db9e
NB
1321 close(container_fd);
1322 }
a7c6e3fb 1323 wait_for(chosen_name, mdfd);
682c7051 1324 } else {
e7b84f9d 1325 pr_err("not starting array - not enough devices.\n");
682c7051 1326 }
cd6cbb08 1327 udev_unblock();
ce559078
MT
1328 close(mdfd);
1329 sysfs_uevent(&info, "change");
682c7051 1330 return 0;
7f91af49
N
1331
1332 abort:
cd6cbb08 1333 udev_unblock();
4eb26970 1334 map_lock(&map);
e06af9dd 1335 abort_locked:
4dd2df09 1336 map_remove(&map, fd2devnm(mdfd));
4eb26970
DW
1337 map_unlock(&map);
1338
7f91af49
N
1339 if (mdfd >= 0)
1340 close(mdfd);
1341 return 1;
64c4757e 1342}