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