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