]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Create.c
Create: Fix checking for container in update_metadata
[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 */
99cc42f4
N
639 if (get_linux_version() < 2006016 && s->chunk == 0) {
640 s->chunk = 64;
171dccc8 641 if (c->verbose > 0)
e7b84f9d 642 pr_err("chunk size defaults to 64K\n");
aa88f531
NB
643 }
644 break;
570510ba 645 case 1:
08e43379 646 case LEVEL_FAULTY:
570510ba 647 case LEVEL_MULTIPATH:
17f25ca6 648 case LEVEL_CONTAINER:
99cc42f4 649 if (s->chunk) {
5b30a34a
MG
650 pr_err("specifying chunk size is forbidden for this level\n");
651 return 1;
aa88f531
NB
652 }
653 break;
570510ba 654 default:
99cc42f4 655 pr_err("unknown level %d\n", s->level);
570510ba 656 return 1;
682c7051 657 }
22dc741f 658
99cc42f4 659 if (s->size == MAX_SIZE)
d04f65f4 660 /* use '0' to mean 'max' now... */
99cc42f4
N
661 s->size = 0;
662 if (s->size && s->chunk && s->chunk != UnSet)
22dc741f
MT
663 if (round_size_and_verify(&s->size, s->chunk))
664 return 1;
665
99cc42f4
N
666 newsize = s->size * 2;
667 if (st && ! st->ss->validate_geometry(st, s->level, s->layout, s->raiddisks,
af4348dd 668 &s->chunk, s->size*2,
ae5dfc56 669 s->data_offset, NULL,
5308f117 670 &newsize, s->consistency_policy,
98dbf73c 671 c->verbose >= 0))
17f25ca6 672 return 1;
bb7295f1 673
99cc42f4
N
674 if (s->chunk && s->chunk != UnSet) {
675 newsize &= ~(unsigned long long)(s->chunk*2 - 1);
64385908
CA
676 if (do_default_chunk) {
677 /* default chunk was just set */
171dccc8 678 if (c->verbose > 0)
7a862a02 679 pr_err("chunk size defaults to %dK\n", s->chunk);
22dc741f
MT
680 if (round_size_and_verify(&s->size, s->chunk))
681 return 1;
64385908
CA
682 do_default_chunk = 0;
683 }
bb7295f1 684 }
64385908 685
99cc42f4
N
686 if (s->size == 0) {
687 s->size = newsize / 2;
688 if (s->level == 1)
ae6c05ad
N
689 /* If this is ever reshaped to RAID5, we will
690 * need a chunksize. So round it off a bit
691 * now just to be safe
692 */
99cc42f4 693 s->size &= ~(64ULL-1);
ae6c05ad 694
99cc42f4
N
695 if (s->size && c->verbose > 0)
696 pr_err("setting size to %lluK\n", s->size);
8592f29d 697 }
17f25ca6 698
682c7051 699 /* now look at the subdevs */
06c7f68e
NB
700 info.array.active_disks = 0;
701 info.array.working_disks = 0;
c913b90e 702 dnum = 0;
98dbf73c 703 for (dv = devlist; dv; dv = dv->next)
ae5dfc56 704 if (s->data_offset == VARIABLE_OFFSET)
476066a3
N
705 dv->data_offset = INVALID_SECTORS;
706 else
ae5dfc56 707 dv->data_offset = s->data_offset;
476066a3 708
8592f29d 709 for (dv=devlist; dv && !have_container; dv=dv->next, dnum++) {
cd29a5c8 710 char *dname = dv->devname;
17f25ca6 711 unsigned long long freesize;
e9b11fee 712 int dfd;
72ca9bcf 713 char *doff;
e9b11fee 714
98dbf73c 715 if (strcasecmp(dname, "missing") == 0) {
c913b90e
NB
716 if (first_missing > dnum)
717 first_missing = dnum;
6fb79233
NB
718 if (second_missing > dnum && dnum > first_missing)
719 second_missing = dnum;
52826846
NB
720 missing_disks ++;
721 continue;
722 }
ae5dfc56 723 if (s->data_offset == VARIABLE_OFFSET) {
72ca9bcf
N
724 doff = strchr(dname, ':');
725 if (doff) {
726 *doff++ = 0;
727 dv->data_offset = parse_size(doff);
728 } else
729 dv->data_offset = INVALID_SECTORS;
730 } else
ae5dfc56 731 dv->data_offset = s->data_offset;
72ca9bcf 732
58b3c697 733 dfd = open(dname, O_RDONLY);
e9b11fee 734 if (dfd < 0) {
e7b84f9d 735 pr_err("cannot open %s: %s\n",
e9b11fee
N
736 dname, strerror(errno));
737 exit(2);
738 }
0a6bff09 739 if (!fstat_is_blkdev(dfd, dname, NULL)) {
e9b11fee 740 close(dfd);
e9b11fee
N
741 exit(2);
742 }
743 close(dfd);
06c7f68e 744 info.array.working_disks++;
dfd7822c 745 if (dnum < s->raiddisks && dv->disposition != 'j')
06c7f68e 746 info.array.active_disks++;
058574b1 747 if (st == NULL) {
8aec876d 748 struct createinfo *ci = conf_get_create_info();
058574b1
NB
749 if (ci)
750 st = ci->supertype;
751 }
576d6d83
NB
752 if (st == NULL) {
753 /* Need to choose a default metadata, which is different
17f25ca6 754 * depending on geometry of array.
576d6d83
NB
755 */
756 int i;
757 char *name = "default";
98dbf73c 758 for(i = 0; !st && superlist[i]; i++) {
576d6d83 759 st = superlist[i]->match_metadata_desc(name);
ecbd9e81
N
760 if (!st)
761 continue;
a18a888e 762 if (do_default_layout)
99cc42f4 763 s->layout = default_layout(st, s->level, c->verbose);
ecbd9e81 764 switch (st->ss->validate_geometry(
99cc42f4 765 st, s->level, s->layout, s->raiddisks,
af4348dd 766 &s->chunk, s->size*2,
72ca9bcf 767 dv->data_offset, dname,
5308f117
AP
768 &freesize, s->consistency_policy,
769 c->verbose > 0)) {
ecbd9e81
N
770 case -1: /* Not valid, message printed, and not
771 * worth checking any further */
772 exit(2);
773 break;
774 case 0: /* Geometry not valid */
55425f27 775 free(st);
17f25ca6 776 st = NULL;
99cc42f4 777 s->chunk = do_default_chunk ? UnSet : s->chunk;
ecbd9e81
N
778 break;
779 case 1: /* All happy */
780 break;
55425f27 781 }
17f25ca6 782 }
576d6d83
NB
783
784 if (!st) {
58b3c697
N
785 int dfd = open(dname, O_RDONLY|O_EXCL);
786 if (dfd < 0) {
e7b84f9d 787 pr_err("cannot open %s: %s\n",
58b3c697
N
788 dname, strerror(errno));
789 exit(2);
790 }
7a862a02 791 pr_err("device %s not suitable for any style of array\n",
17f25ca6 792 dname);
576d6d83
NB
793 exit(2);
794 }
b8ac1967 795 if (st->ss != &super0 ||
576d6d83 796 st->minor_version != 90)
b8ac1967 797 did_default = 1;
17f25ca6 798 } else {
a18a888e 799 if (do_default_layout)
99cc42f4
N
800 s->layout = default_layout(st, s->level, 0);
801 if (!st->ss->validate_geometry(st, s->level, s->layout,
802 s->raiddisks,
af4348dd 803 &s->chunk, s->size*2,
72ca9bcf 804 dv->data_offset,
af4348dd 805 dname, &freesize,
5308f117 806 s->consistency_policy,
171dccc8 807 c->verbose >= 0)) {
17f25ca6 808
7a862a02 809 pr_err("%s is not suitable for this array.\n",
e7b84f9d 810 dname);
17f25ca6
NB
811 fail = 1;
812 continue;
813 }
682c7051 814 }
82d9eba6 815
cc1799c3 816 if (dv->disposition == 'j')
dfd7822c 817 goto skip_size_check; /* skip write journal for size check */
cc1799c3 818
82d9eba6 819 freesize /= 2; /* convert to K */
99cc42f4 820 if (s->chunk && s->chunk != UnSet) {
d2cd3ffc 821 /* round to chunk size */
99cc42f4 822 freesize = freesize & ~(s->chunk-1);
64385908
CA
823 if (do_default_chunk) {
824 /* default chunk was just set */
171dccc8 825 if (c->verbose > 0)
7a862a02 826 pr_err("chunk size defaults to %dK\n", s->chunk);
22dc741f
MT
827 if (round_size_and_verify(&s->size, s->chunk))
828 return 1;
64385908
CA
829 do_default_chunk = 0;
830 }
d2cd3ffc 831 }
066e92f0
LD
832 if (!freesize) {
833 pr_err("no free space left on %s\n", dname);
834 fail = 1;
835 continue;
836 }
682c7051 837
99cc42f4 838 if (s->size && freesize < s->size) {
7a862a02 839 pr_err("%s is smaller than given size. %lluK < %lluK + metadata\n",
99cc42f4 840 dname, freesize, s->size);
52826846 841 fail = 1;
52826846 842 continue;
682c7051 843 }
cd29a5c8
NB
844 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
845 maxdisc = dname;
52826846 846 maxsize = freesize;
682c7051 847 }
cd29a5c8
NB
848 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
849 mindisc = dname;
52826846 850 minsize = freesize;
682c7051 851 }
dfd7822c 852 skip_size_check:
171dccc8 853 if (c->runstop != 1 || c->verbose >= 0) {
37ea3936 854 int fd = open(dname, O_RDONLY);
98dbf73c 855 if (fd < 0) {
e7b84f9d 856 pr_err("Cannot open %s: %s\n",
b6e63da4 857 dname, strerror(errno));
98dbf73c 858 fail = 1;
b6e63da4
NB
859 continue;
860 }
dab6685f
NB
861 warn |= check_ext2(fd, dname);
862 warn |= check_reiser(fd, dname);
863 warn |= check_raid(fd, dname);
034b203a
TM
864 if (strcmp(st->ss->name, "1.x") == 0 &&
865 st->minor_version >= 1)
866 /* metadata at front */
53ed6ac3 867 warn |= check_partitions(fd, dname, 0, 0);
6f2af6a4 868 else if (s->level == 1 || is_container(s->level) ||
cf622ec1 869 (s->level == 0 && s->raiddisks == 1))
034b203a 870 /* partitions could be meaningful */
99cc42f4 871 warn |= check_partitions(fd, dname, freesize*2, s->size*2);
034b203a
TM
872 else
873 /* partitions cannot be meaningful */
53ed6ac3 874 warn |= check_partitions(fd, dname, 0, 0);
034b203a 875 if (strcmp(st->ss->name, "1.x") == 0 &&
a0962fe9
N
876 st->minor_version >= 1 &&
877 did_default &&
99cc42f4 878 s->level == 1 &&
034b203a
TM
879 (warn & 1024) == 0) {
880 warn |= 1024;
e7b84f9d 881 pr_err("Note: this array has metadata at the start and\n"
a0962fe9 882 " may not be suitable as a boot device. If you plan to\n"
cc86f89c 883 " store '/boot' on this device please ensure that\n"
a0962fe9 884 " your boot-loader understands md/v1.x metadata, or use\n"
cc86f89c 885 " --metadata=0.90\n");
a0962fe9 886 }
b6e63da4 887 close(fd);
dab6685f 888 }
682c7051 889 }
b91ad097 890 if (missing_disks == dnum && !have_container) {
1db03765
XN
891 pr_err("Subdevs can't be all missing\n");
892 return 1;
893 }
99cc42f4 894 if (s->raiddisks + s->sparedisks > st->max_devs) {
7a862a02 895 pr_err("Too many devices: %s metadata only supports %d\n",
acab7bb1
N
896 st->ss->name, st->max_devs);
897 return 1;
898 }
8592f29d 899 if (have_container)
99cc42f4 900 info.array.working_disks = s->raiddisks;
682c7051 901 if (fail) {
e7b84f9d 902 pr_err("create aborted\n");
52826846 903 return 1;
682c7051 904 }
99cc42f4 905 if (s->size == 0) {
5f8097be 906 if (mindisc == NULL && !have_container) {
e7b84f9d 907 pr_err("no size and no drives given - aborting create.\n");
52826846
NB
908 return 1;
909 }
cf622ec1 910 if (s->level > 0 || s->level == LEVEL_MULTIPATH ||
98dbf73c 911 s->level == LEVEL_FAULTY || st->ss->external) {
b5e64645 912 /* size is meaningful */
99cc42f4
N
913 if (!st->ss->validate_geometry(st, s->level, s->layout,
914 s->raiddisks,
915 &s->chunk, minsize*2,
ae5dfc56 916 s->data_offset,
5308f117
AP
917 NULL, NULL,
918 s->consistency_policy, 0)) {
99cc42f4 919 pr_err("devices too large for RAID level %d\n", s->level);
b5e64645
NB
920 return 1;
921 }
99cc42f4
N
922 s->size = minsize;
923 if (s->level == 1)
ae6c05ad
N
924 /* If this is ever reshaped to RAID5, we will
925 * need a chunksize. So round it off a bit
926 * now just to be safe
927 */
99cc42f4 928 s->size &= ~(64ULL-1);
171dccc8 929 if (c->verbose > 0)
99cc42f4 930 pr_err("size set to %lluK\n", s->size);
b5e64645 931 }
682c7051 932 }
748952f7
N
933
934 if (!s->bitmap_file &&
848d71c9 935 !st->ss->external &&
748952f7 936 s->level >= 1 &&
39917e56 937 st->ss->add_internal_bitmap &&
2ce09172 938 s->journaldisks == 0 &&
e97a7cd0
AP
939 (s->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
940 s->consistency_policy != CONSISTENCY_POLICY_PPL) &&
748952f7
N
941 (s->write_behind || s->size > 100*1024*1024ULL)) {
942 if (c->verbose > 0)
943 pr_err("automatically enabling write-intent bitmap on large array\n");
944 s->bitmap_file = "internal";
945 }
946 if (s->bitmap_file && strcmp(s->bitmap_file, "none") == 0)
947 s->bitmap_file = NULL;
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
99cc42f4 955 if (!have_container && s->level > 0 && ((maxsize-s->size)*100 > maxsize)) {
171dccc8 956 if (c->runstop != 1 || c->verbose >= 0)
e7b84f9d 957 pr_err("largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
99cc42f4 958 maxdisc, s->size);
52826846 959 warn = 1;
682c7051
NB
960 }
961
9eafa1de 962 if (st->ss->detail_platform && st->ss->detail_platform(0, 1, NULL) != 0) {
171dccc8 963 if (c->runstop != 1 || c->verbose >= 0)
e7b84f9d 964 pr_err("%s unable to enumerate platform support\n"
5615172f
DW
965 " array may not be compatible with hardware/firmware\n",
966 st->ss->name);
967 warn = 1;
968 }
529e2aa5 969 st->nodes = c->nodes;
7716570e 970 st->cluster_name = c->homecluster;
5615172f 971
682c7051 972 if (warn) {
171dccc8 973 if (c->runstop!= 1) {
52826846 974 if (!ask("Continue creating array? ")) {
e7b84f9d 975 pr_err("create aborted.\n");
52826846
NB
976 return 1;
977 }
978 } else {
171dccc8 979 if (c->verbose > 0)
e7b84f9d 980 pr_err("creation continuing despite oddities due to --run\n");
682c7051 981 }
682c7051
NB
982 }
983
66f8bbbe 984 /* If this is raid4/5, we want to configure the last active slot
52826846 985 * as missing, so that a reconstruct happens (faster than re-parity)
98c6faba 986 * FIX: Can we do this for raid6 as well?
52826846 987 */
98dbf73c
JS
988 if (st->ss->external == 0 && s->assume_clean == 0 &&
989 c->force == 0 && first_missing >= s->raiddisks) {
990 switch (s->level) {
66f8bbbe 991 case 4:
98c6faba 992 case 5:
99cc42f4
N
993 insert_point = s->raiddisks-1;
994 s->sparedisks++;
06c7f68e 995 info.array.active_disks--;
98c6faba
NB
996 missing_disks++;
997 break;
998 default:
999 break;
1000 }
52826846 1001 }
6fb79233
NB
1002 /* For raid6, if creating with 1 missing drive, make a good drive
1003 * into a spare, else the create will fail
1004 */
99cc42f4 1005 if (s->assume_clean == 0 && c->force == 0 && first_missing < s->raiddisks &&
ffcfc735 1006 st->ss->external == 0 &&
99cc42f4
N
1007 second_missing >= s->raiddisks && s->level == 6) {
1008 insert_point = s->raiddisks - 1;
6fb79233
NB
1009 if (insert_point == first_missing)
1010 insert_point--;
99cc42f4 1011 s->sparedisks ++;
6fb79233
NB
1012 info.array.active_disks--;
1013 missing_disks++;
1014 }
570510ba 1015
99cc42f4 1016 if (s->level <= 0 && first_missing < subdevs * 2) {
e7b84f9d 1017 pr_err("This level does not support missing devices\n");
570510ba
NB
1018 return 1;
1019 }
aba69144 1020
7f91af49 1021 /* We need to create the device */
ad5bc697 1022 map_lock(&map);
cd6cbb08 1023 mdfd = create_mddev(mddev, name, c->autof, LOCAL, chosen_name, 1);
e06af9dd
JS
1024 if (mdfd < 0) {
1025 map_unlock(&map);
7f91af49 1026 return 1;
e06af9dd 1027 }
3e9df86a
AK
1028 /* verify if chosen_name is not in use,
1029 * it could be in conflict with already existing device
1030 * e.g. container, array
1031 */
cf622ec1
JS
1032 if (strncmp(chosen_name, "/dev/md/", 8) == 0 &&
1033 map_by_name(&map, chosen_name+8) != NULL) {
e7b84f9d 1034 pr_err("Array name %s is in use already.\n",
3e9df86a
AK
1035 chosen_name);
1036 close(mdfd);
1037 map_unlock(&map);
cd6cbb08 1038 udev_unblock();
3e9df86a
AK
1039 return 1;
1040 }
f2be09f1 1041 mddev = chosen_name;
7f91af49 1042
5f4cc239
JS
1043 memset(&inf, 0, sizeof(inf));
1044 md_get_array_info(mdfd, &inf);
1045 if (inf.working_disks != 0) {
1046 pr_err("another array by this name is already running.\n");
e06af9dd 1047 goto abort_locked;
7f91af49 1048 }
a04d5763 1049
682c7051
NB
1050 /* Ok, lets try some ioctls */
1051
99cc42f4
N
1052 info.array.level = s->level;
1053 info.array.size = s->size;
1054 info.array.raid_disks = s->raiddisks;
82b27616
NB
1055 /* The kernel should *know* what md_minor we are dealing
1056 * with, but it chooses to trust me instead. Sigh
1057 */
06c7f68e 1058 info.array.md_minor = 0;
0a6bff09
ZL
1059 if (fstat_is_blkdev(mdfd, mddev, &rdev))
1060 info.array.md_minor = minor(rdev);
06c7f68e 1061 info.array.not_persistent = 0;
6fb79233 1062
cf622ec1
JS
1063 if (((s->level == 4 || s->level == 5) &&
1064 (insert_point < s->raiddisks || first_missing < s->raiddisks)) ||
1065 (s->level == 6 && (insert_point < s->raiddisks ||
1066 second_missing < s->raiddisks)) ||
1067 (s->level <= 0) || s->assume_clean) {
06c7f68e 1068 info.array.state = 1; /* clean, but one+ drive will be missing*/
b7528a20 1069 info.resync_start = MaxSector;
103f2410 1070 } else {
06c7f68e 1071 info.array.state = 0; /* not clean, but no errors */
103f2410
NB
1072 info.resync_start = 0;
1073 }
99cc42f4 1074 if (s->level == 10) {
f9c25f1d
NB
1075 /* for raid10, the bitmap size is the capacity of the array,
1076 * which is array.size * raid_disks / ncopies;
1077 * .. but convert to sectors.
1078 */
99cc42f4
N
1079 int ncopies = ((s->layout>>8) & 255) * (s->layout & 255);
1080 bitmapsize = s->size * s->raiddisks / ncopies * 2;
1081/* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, s->size, s->raiddisks, ncopies);*/
f9c25f1d 1082 } else
99cc42f4 1083 bitmapsize = s->size * 2;
f9c25f1d 1084
82b27616
NB
1085 /* There is lots of redundancy in these disk counts,
1086 * raid_disks is the most meaningful value
1087 * it describes the geometry of the array
1088 * it is constant
1089 * nr_disks is total number of used slots.
1090 * it should be raid_disks+spare_disks
1091 * spare_disks is the number of extra disks present
1092 * see above
1093 * active_disks is the number of working disks in
1094 * active slots. (With raid_disks)
1095 * working_disks is the total number of working disks,
1096 * including spares
1097 * failed_disks is the number of disks marked failed
1098 *
5d500228 1099 * Ideally, the kernel would keep these (except raid_disks)
82b27616
NB
1100 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
1101 * So for now, we assume that all raid and spare
1102 * devices will be given.
1103 */
99cc42f4 1104 info.array.spare_disks=s->sparedisks;
06c7f68e
NB
1105 info.array.failed_disks=missing_disks;
1106 info.array.nr_disks = info.array.working_disks
1107 + info.array.failed_disks;
99cc42f4
N
1108 info.array.layout = s->layout;
1109 info.array.chunk_size = s->chunk*1024;
82d9eba6 1110
b3b33eb5
NB
1111 if (name == NULL || *name == 0) {
1112 /* base name on mddev */
d1e80164
NB
1113 /* /dev/md0 -> 0
1114 * /dev/md_d0 -> d0
eca944fa 1115 * /dev/md_foo -> foo
d1e80164
NB
1116 * /dev/md/1 -> 1
1117 * /dev/md/d1 -> d1
1118 * /dev/md/home -> home
1119 * /dev/mdhome -> home
1120 */
69207ff6 1121 /* FIXME compare this with rules in create_mddev */
b3b33eb5
NB
1122 name = strrchr(mddev, '/');
1123 if (name) {
1124 name++;
98dbf73c
JS
1125 if (strncmp(name, "md_", 3) == 0 &&
1126 strlen(name) > 3 && (name-mddev) == 5 /* /dev/ */)
d1e80164 1127 name += 3;
98dbf73c
JS
1128 else if (strncmp(name, "md", 2) == 0 &&
1129 strlen(name) > 2 && isdigit(name[2]) &&
60248f74 1130 (name-mddev) == 5 /* /dev/ */)
b3b33eb5
NB
1131 name += 2;
1132 }
1133 }
5308f117 1134 if (!st->ss->init_super(st, &info.array, s, name, c->homehost, uuid,
ae5dfc56 1135 s->data_offset))
e06af9dd 1136 goto abort_locked;
682c7051 1137
d2ca6449 1138 total_slots = info.array.nr_disks;
a5d85af7 1139 st->ss->getinfo_super(st, &info, NULL);
dae13137
JS
1140 if (sysfs_init(&info, mdfd, NULL)) {
1141 pr_err("unable to initialize sysfs\n");
1142 goto abort_locked;
1143 }
159c3a1a 1144
171dccc8 1145 if (did_default && c->verbose >= 0) {
3c558363 1146 if (is_subarray(info.text_version)) {
4dd2df09
N
1147 char devnm[32];
1148 char *ep;
f7f1b6a1 1149 struct mdinfo *mdi;
f7f1b6a1 1150
4dd2df09
N
1151 strncpy(devnm, info.text_version+1, 32);
1152 devnm[31] = 0;
1153 ep = strchr(devnm, '/');
1154 if (ep)
1155 *ep = 0;
1156
1157 mdi = sysfs_read(-1, devnm, GET_VERSION);
f7f1b6a1 1158
9364dbfb 1159 pr_info("Creating array inside %s container %s\n",
4dd2df09 1160 mdi?mdi->text_version:"managed", devnm);
f7f1b6a1
NB
1161 sysfs_free(mdi);
1162 } else
9364dbfb
LG
1163 pr_info("Defaulting to version %s metadata\n",
1164 info.text_version);
f7f1b6a1 1165 }
b8ac1967 1166
4dd2df09 1167 map_update(&map, fd2devnm(mdfd), info.text_version,
a04d5763 1168 info.uuid, chosen_name);
19ad4b2c
AP
1169 /* Keep map locked until devices have been added to array
1170 * to stop another mdadm from finding and using those devices.
1171 */
a04d5763 1172
98dbf73c
JS
1173 if (s->bitmap_file && (strcmp(s->bitmap_file, "internal") == 0 ||
1174 strcmp(s->bitmap_file, "clustered") == 0)) {
ebeb3663 1175 if (!st->ss->add_internal_bitmap) {
e7b84f9d 1176 pr_err("internal bitmaps not supported with %s metadata\n",
ebeb3663 1177 st->ss->name);
19ad4b2c 1178 goto abort_locked;
ebeb3663 1179 }
2ec2b7e9
JS
1180 if (st->ss->add_internal_bitmap(st, &s->bitmap_chunk,
1181 c->delay, s->write_behind,
1182 bitmapsize, 1, major_num)) {
e7b84f9d 1183 pr_err("Given bitmap chunk size not supported.\n");
19ad4b2c 1184 goto abort_locked;
55935d51 1185 }
99cc42f4 1186 s->bitmap_file = NULL;
55935d51
NB
1187 }
1188
dae13137
JS
1189 if (sysfs_init(&info, mdfd, NULL)) {
1190 pr_err("unable to initialize sysfs\n");
1191 goto abort_locked;
1192 }
55935d51 1193
4dd2df09 1194 if (st->ss->external && st->container_devnm[0]) {
f35f2525
N
1195 /* member */
1196
1197 /* When creating a member, we need to be careful
1198 * to negotiate with mdmon properly.
1199 * If it is already running, we cannot write to
1200 * the devices and must ask it to do that part.
1201 * If it isn't running, we write to the devices,
1202 * and then start it.
1203 * We hold an exclusive open on the container
1204 * device to make sure mdmon doesn't exit after
1205 * we checked that it is running.
1206 *
1207 * For now, fail if it is already running.
1208 */
4dd2df09 1209 container_fd = open_dev_excl(st->container_devnm);
f35f2525 1210 if (container_fd < 0) {
7a862a02 1211 pr_err("Cannot get exclusive open on container - weird.\n");
19ad4b2c 1212 goto abort_locked;
d03373f1 1213 }
4dd2df09 1214 if (mdmon_running(st->container_devnm)) {
171dccc8 1215 if (c->verbose)
7a862a02 1216 pr_err("reusing mdmon for %s.\n",
4dd2df09 1217 st->container_devnm);
f35f2525
N
1218 st->update_tail = &st->updates;
1219 } else
1220 need_mdmon = 1;
1221 }
1222 rv = set_array_info(mdfd, st, &info);
82d9eba6 1223 if (rv) {
e7b84f9d 1224 pr_err("failed to set array info for %s: %s\n",
52826846 1225 mddev, strerror(errno));
19ad4b2c 1226 goto abort_locked;
682c7051 1227 }
52826846 1228
99cc42f4 1229 if (s->bitmap_file) {
c82f047c 1230 int uuid[4];
55935d51 1231
3da92f27 1232 st->ss->uuid_from_super(st, uuid);
99cc42f4
N
1233 if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid, s->bitmap_chunk,
1234 c->delay, s->write_behind,
f9c25f1d 1235 bitmapsize,
943eafef 1236 major_num)) {
19ad4b2c 1237 goto abort_locked;
c82f047c 1238 }
99cc42f4 1239 bitmap_fd = open(s->bitmap_file, O_RDWR);
c82f047c 1240 if (bitmap_fd < 0) {
ebf3be99 1241 pr_err("weird: %s cannot be opened\n",
99cc42f4 1242 s->bitmap_file);
19ad4b2c 1243 goto abort_locked;
c82f047c
NB
1244 }
1245 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
e7b84f9d 1246 pr_err("Cannot set bitmap file for %s: %s\n",
c82f047c 1247 mddev, strerror(errno));
19ad4b2c 1248 goto abort_locked;
c82f047c
NB
1249 }
1250 }
1251
8a4ce2c0
LG
1252 if (add_disks(mdfd, &info, s, c, st, &map, devlist, total_slots,
1253 have_container, insert_point, major_num, chosen_name))
1254 goto abort_locked;
9b1fb677 1255
19ad4b2c 1256 map_unlock(&map);
5e52ae9e 1257
6f2af6a4 1258 if (is_container(s->level)) {
97590376
N
1259 /* No need to start. But we should signal udev to
1260 * create links */
1261 sysfs_uevent(&info, "change");
171dccc8 1262 if (c->verbose >= 0)
e7b84f9d 1263 pr_err("container %s prepared.\n", mddev);
a7c6e3fb 1264 wait_for(chosen_name, mdfd);
99cc42f4 1265 } else if (c->runstop == 1 || subdevs >= s->raiddisks) {
598f0d58 1266 if (st->ss->external) {
4c821454 1267 int err;
99cc42f4 1268 switch(s->level) {
598f0d58
NB
1269 case LEVEL_LINEAR:
1270 case LEVEL_MULTIPATH:
1271 case 0:
4c821454 1272 err = sysfs_set_str(&info, NULL, "array_state",
171dccc8 1273 c->readonly
72d566f6
N
1274 ? "readonly"
1275 : "active");
a931db9e 1276 need_mdmon = 0;
598f0d58
NB
1277 break;
1278 default:
4c821454
N
1279 err = sysfs_set_str(&info, NULL, "array_state",
1280 "readonly");
598f0d58
NB
1281 break;
1282 }
fb2c0f61 1283 sysfs_set_safemode(&info, info.safe_mode_delay);
4c821454 1284 if (err) {
7a862a02 1285 pr_err("failed to activate array.\n");
4c821454
N
1286 ioctl(mdfd, STOP_ARRAY, NULL);
1287 goto abort;
1288 }
171dccc8 1289 } else if (c->readonly &&
72d566f6
N
1290 sysfs_attribute_available(
1291 &info, NULL, "array_state")) {
1292 if (sysfs_set_str(&info, NULL,
1293 "array_state", "readonly") < 0) {
1294 pr_err("Failed to start array: %s\n",
1295 strerror(errno));
1296 ioctl(mdfd, STOP_ARRAY, NULL);
1297 goto abort;
1298 }
598f0d58 1299 } else {
97590376 1300 /* param is not actually used */
598f0d58
NB
1301 mdu_param_t param;
1302 if (ioctl(mdfd, RUN_ARRAY, &param)) {
e7b84f9d 1303 pr_err("RUN_ARRAY failed: %s\n",
72d566f6 1304 strerror(errno));
329dfc28
N
1305 if (errno == 524 /* ENOTSUP */ &&
1306 info.array.level == 0)
1307 cont_err("Please use --layout=original or --layout=alternate\n");
a252c078 1308 if (info.array.chunk_size & (info.array.chunk_size-1)) {
7a862a02 1309 cont_err("Problem may be that chunk size is not a power of 2\n");
a252c078 1310 }
4eb26970 1311 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1312 goto abort;
598f0d58 1313 }
a21e848a
N
1314 /* if start_ro module parameter is set, array is
1315 * auto-read-only, which is bad as the resync won't
1316 * start. So lets make it read-write now.
1317 */
1318 ioctl(mdfd, RESTART_ARRAY_RW, NULL);
682c7051 1319 }
171dccc8 1320 if (c->verbose >= 0)
9364dbfb 1321 pr_info("array %s started.\n", mddev);
4dd2df09 1322 if (st->ss->external && st->container_devnm[0]) {
8850ee3e 1323 if (need_mdmon)
4dd2df09 1324 start_mdmon(st->container_devnm);
8850ee3e 1325
4dd2df09 1326 ping_monitor(st->container_devnm);
a931db9e
NB
1327 close(container_fd);
1328 }
a7c6e3fb 1329 wait_for(chosen_name, mdfd);
682c7051 1330 } else {
e7b84f9d 1331 pr_err("not starting array - not enough devices.\n");
682c7051 1332 }
cd6cbb08 1333 udev_unblock();
ce559078
MT
1334 close(mdfd);
1335 sysfs_uevent(&info, "change");
682c7051 1336 return 0;
7f91af49
N
1337
1338 abort:
cd6cbb08 1339 udev_unblock();
4eb26970 1340 map_lock(&map);
e06af9dd 1341 abort_locked:
4dd2df09 1342 map_remove(&map, fd2devnm(mdfd));
4eb26970
DW
1343 map_unlock(&map);
1344
7f91af49
N
1345 if (mdfd >= 0)
1346 close(mdfd);
1347 return 1;
64c4757e 1348}