]> git.ipfire.org Git - thirdparty/mdadm.git/blame_incremental - Create.c
mdadm: Block SIGCHLD processes before starting children
[thirdparty/mdadm.git] / Create.c
... / ...
CommitLineData
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
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
22 * Email: <neilb@suse.de>
23 */
24
25#include "mdadm.h"
26#include "udev.h"
27#include "md_u.h"
28#include "md_p.h"
29#include <ctype.h>
30#include <fcntl.h>
31#include <signal.h>
32#include <sys/signalfd.h>
33#include <sys/wait.h>
34
35#ifndef FALLOC_FL_ZERO_RANGE
36#define FALLOC_FL_ZERO_RANGE 16
37#endif
38
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
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)
62{
63 int layout = UnSet;
64 mapping_t *layout_map = NULL;
65 char *layout_name = NULL;
66
67 if (st && st->ss->default_geometry)
68 st->ss->default_geometry(st, &level, &layout, NULL);
69
70 if (layout != UnSet)
71 return layout;
72
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");
95 layout_name = map_num_s(layout_map, layout);
96 }
97 if (layout_name && verbose > 0)
98 pr_err("layout defaults to %s\n", layout_name);
99
100 return layout;
101}
102
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 pid_t pid;
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
200 while (wait_count) {
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) {
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 }
226 }
227 }
228
229 close(sfd);
230
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
244static int add_disk_to_super(int mdfd, struct shape *s, struct context *c,
245 struct supertype *st, struct mddev_dev *dv,
246 struct mdinfo *info, int have_container, int major_num,
247 int *zero_pid)
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 }
288 if (!fstat_is_blkdev(fd, dv->devname, &rdev)) {
289 close(fd);
290 return 1;
291 }
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);
300 close(fd);
301 return 1;
302 }
303 st->ss->getinfo_super(st, info, NULL);
304
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);
309 close(fd);
310 return 1;
311 }
312 }
313
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);
342 if (st->ss->external && !is_container(s->level) &&
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;
390 int zero_pids[total_slots];
391 struct mddev_dev *dv;
392 struct mdinfo *infos;
393 sigset_t sigset, orig_sigset;
394 int ret = 0;
395
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);
404 sigaddset(&sigset, SIGCHLD);
405 sigprocmask(SIG_BLOCK, &sigset, &orig_sigset);
406 memset(zero_pids, 0, sizeof(zero_pids));
407
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,
441 major_num, &zero_pids[dnum]);
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) {
467 ret = wait_for_zero_forks(zero_pids, total_slots);
468 if (ret)
469 goto out;
470
471 ret = update_metadata(mdfd, s, st, map, info,
472 chosen_name);
473 if (ret)
474 goto out;
475 }
476 }
477
478out:
479 if (ret)
480 wait_for_zero_forks(zero_pids, total_slots);
481 free(infos);
482 sigprocmask(SIG_SETMASK, &orig_sigset, NULL);
483 return ret;
484}
485
486int Create(struct supertype *st, struct mddev_ident *ident, int subdevs,
487 struct mddev_dev *devlist, struct shape *s, struct context *c)
488{
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
502 * if runstop==run, or raiddisks disks were used,
503 * RUN_ARRAY
504 */
505 int mdfd;
506 unsigned long long minsize = 0, maxsize = 0;
507 dev_policy_t *custom_pols = NULL;
508 char *mindisc = NULL;
509 char *maxdisc = NULL;
510 char *name = ident->name;
511 int *uuid = ident->uuid_set == 1 ? ident->uuid : NULL;
512 int dnum;
513 struct mddev_dev *dv;
514 dev_t rdev;
515 int fail = 0, warn = 0;
516 int first_missing = subdevs * 2;
517 int second_missing = subdevs * 2;
518 int missing_disks = 0;
519 int insert_point = subdevs * 2; /* where to insert a missing drive */
520 int total_slots;
521 int rv;
522 int bitmap_fd;
523 int have_container = 0;
524 int container_fd = -1;
525 int need_mdmon = 0;
526 unsigned long long bitmapsize;
527 struct mdinfo info;
528 int did_default = 0;
529 int do_default_layout = 0;
530 int do_default_chunk = 0;
531 char chosen_name[1024];
532 struct map_ent *map = NULL;
533 unsigned long long newsize;
534 mdu_array_info_t inf;
535
536 int major_num = BITMAP_MAJOR_HI;
537 if (s->bitmap_file && strcmp(s->bitmap_file, "clustered") == 0) {
538 major_num = BITMAP_MAJOR_CLUSTERED;
539 if (c->nodes <= 1) {
540 pr_err("At least 2 nodes are needed for cluster-md\n");
541 return 1;
542 }
543 }
544
545 memset(&info, 0, sizeof(info));
546 if (s->level == UnSet && st && st->ss->default_geometry)
547 st->ss->default_geometry(st, &s->level, NULL, NULL);
548 if (s->level == UnSet) {
549 pr_err("a RAID level is needed to create an array.\n");
550 return 1;
551 }
552 if (s->raiddisks < 4 && s->level == 6) {
553 pr_err("at least 4 raid-devices needed for level 6\n");
554 return 1;
555 }
556 if (s->raiddisks > 256 && s->level == 6) {
557 pr_err("no more than 256 raid-devices supported for level 6\n");
558 return 1;
559 }
560 if (s->raiddisks < 2 && s->level >= 4) {
561 pr_err("at least 2 raid-devices needed for level %d\n", s->level);
562 return 1;
563 }
564 if (s->level <= 0 && s->sparedisks) {
565 pr_err("This level does not support spare devices\n");
566 return 1;
567 }
568
569 if (subdevs == 1 && strcmp(devlist->devname, "missing") != 0) {
570 /* If given a single device, it might be a container, and we can
571 * extract a device list from there
572 */
573 int fd;
574
575 memset(&inf, 0, sizeof(inf));
576 fd = open(devlist->devname, O_RDONLY);
577 if (fd >= 0 &&
578 md_get_array_info(fd, &inf) == 0 && inf.raid_disks == 0) {
579 /* yep, looks like a container */
580 if (st) {
581 rv = st->ss->load_container(st, fd,
582 devlist->devname);
583 if (rv == 0)
584 have_container = 1;
585 } else {
586 st = super_by_fd(fd, NULL);
587 if (st && !(rv = st->ss->
588 load_container(st, fd,
589 devlist->devname)))
590 have_container = 1;
591 else
592 st = NULL;
593 }
594 if (have_container) {
595 subdevs = s->raiddisks;
596 first_missing = subdevs * 2;
597 second_missing = subdevs * 2;
598 insert_point = subdevs * 2;
599
600 if (mddev_test_and_add_drive_policies(st, &custom_pols, fd, 1))
601 exit(1);
602 }
603 }
604 if (fd >= 0)
605 close(fd);
606 }
607 if (st && st->ss->external && s->sparedisks) {
608 pr_err("This metadata type does not support spare disks at create time\n");
609 return 1;
610 }
611 if (subdevs > s->raiddisks+s->sparedisks+s->journaldisks) {
612 pr_err("You have listed more devices (%d) than are in the array(%d)!\n", subdevs, s->raiddisks+s->sparedisks);
613 return 1;
614 }
615 if (!have_container && subdevs < s->raiddisks+s->sparedisks+s->journaldisks) {
616 pr_err("You haven't given enough devices (real or missing) to create this array\n");
617 return 1;
618 }
619 if (s->bitmap_file && s->level <= 0) {
620 pr_err("bitmaps not meaningful with level %s\n",
621 map_num(pers, s->level)?:"given");
622 return 1;
623 }
624
625 /* now set some defaults */
626
627 if (s->layout == UnSet) {
628 do_default_layout = 1;
629 s->layout = default_layout(st, s->level, c->verbose);
630 }
631
632 if (s->level == 10)
633 /* check layout fits in array*/
634 if ((s->layout&255) * ((s->layout>>8)&255) > s->raiddisks) {
635 pr_err("that layout requires at least %d devices\n",
636 (s->layout&255) * ((s->layout>>8)&255));
637 return 1;
638 }
639
640 switch(s->level) {
641 case 4:
642 case 5:
643 case 10:
644 case 6:
645 case 0:
646 if (s->chunk == 0 || s->chunk == UnSet) {
647 s->chunk = UnSet;
648 do_default_chunk = 1;
649 /* chunk will be set later */
650 }
651 break;
652 case LEVEL_LINEAR:
653 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
654 break;
655 case 1:
656 case LEVEL_FAULTY:
657 case LEVEL_MULTIPATH:
658 case LEVEL_CONTAINER:
659 if (s->chunk) {
660 pr_err("specifying chunk size is forbidden for this level\n");
661 return 1;
662 }
663 break;
664 default:
665 pr_err("unknown level %d\n", s->level);
666 return 1;
667 }
668
669 if (s->size == MAX_SIZE)
670 /* use '0' to mean 'max' now... */
671 s->size = 0;
672 if (s->size && s->chunk && s->chunk != UnSet)
673 if (round_size_and_verify(&s->size, s->chunk))
674 return 1;
675
676 newsize = s->size * 2;
677 if (st && ! st->ss->validate_geometry(st, s->level, s->layout, s->raiddisks,
678 &s->chunk, s->size*2,
679 s->data_offset, NULL,
680 &newsize, s->consistency_policy,
681 c->verbose >= 0))
682 return 1;
683
684 if (s->chunk && s->chunk != UnSet) {
685 newsize &= ~(unsigned long long)(s->chunk*2 - 1);
686 if (do_default_chunk) {
687 /* default chunk was just set */
688 if (c->verbose > 0)
689 pr_err("chunk size defaults to %dK\n", s->chunk);
690 if (round_size_and_verify(&s->size, s->chunk))
691 return 1;
692 do_default_chunk = 0;
693 }
694 }
695
696 if (s->size == 0) {
697 s->size = newsize / 2;
698 if (s->level == 1)
699 /* If this is ever reshaped to RAID5, we will
700 * need a chunksize. So round it off a bit
701 * now just to be safe
702 */
703 s->size &= ~(64ULL-1);
704
705 if (s->size && c->verbose > 0)
706 pr_err("setting size to %lluK\n", s->size);
707 }
708
709 /* now look at the subdevs */
710 info.array.active_disks = 0;
711 info.array.working_disks = 0;
712 dnum = 0;
713 for (dv = devlist; dv; dv = dv->next)
714 if (s->data_offset == VARIABLE_OFFSET)
715 dv->data_offset = INVALID_SECTORS;
716 else
717 dv->data_offset = s->data_offset;
718
719 for (dv=devlist; dv && !have_container; dv=dv->next, dnum++) {
720 char *dname = dv->devname;
721 unsigned long long freesize;
722 int dfd;
723 char *doff;
724
725 if (strcasecmp(dname, "missing") == 0) {
726 if (first_missing > dnum)
727 first_missing = dnum;
728 if (second_missing > dnum && dnum > first_missing)
729 second_missing = dnum;
730 missing_disks ++;
731 continue;
732 }
733 if (s->data_offset == VARIABLE_OFFSET) {
734 doff = strchr(dname, ':');
735 if (doff) {
736 *doff++ = 0;
737 dv->data_offset = parse_size(doff);
738 } else
739 dv->data_offset = INVALID_SECTORS;
740 } else
741 dv->data_offset = s->data_offset;
742
743 dfd = open(dname, O_RDONLY);
744 if (dfd < 0) {
745 pr_err("cannot open %s: %s\n",
746 dname, strerror(errno));
747 exit(2);
748 }
749 if (!fstat_is_blkdev(dfd, dname, NULL)) {
750 close(dfd);
751 exit(2);
752 }
753
754 info.array.working_disks++;
755 if (dnum < s->raiddisks && dv->disposition != 'j')
756 info.array.active_disks++;
757 if (st == NULL) {
758 struct createinfo *ci = conf_get_create_info();
759 if (ci)
760 st = ci->supertype;
761 }
762 if (st == NULL) {
763 /* Need to choose a default metadata, which is different
764 * depending on geometry of array.
765 */
766 int i;
767 char *name = "default";
768 for(i = 0; !st && superlist[i]; i++) {
769 st = superlist[i]->match_metadata_desc(name);
770 if (!st)
771 continue;
772 if (do_default_layout)
773 s->layout = default_layout(st, s->level, c->verbose);
774 switch (st->ss->validate_geometry(
775 st, s->level, s->layout, s->raiddisks,
776 &s->chunk, s->size*2,
777 dv->data_offset, dname,
778 &freesize, s->consistency_policy,
779 c->verbose > 0)) {
780 case -1: /* Not valid, message printed, and not
781 * worth checking any further */
782 exit(2);
783 break;
784 case 0: /* Geometry not valid */
785 free(st);
786 st = NULL;
787 s->chunk = do_default_chunk ? UnSet : s->chunk;
788 break;
789 case 1: /* All happy */
790 break;
791 }
792 }
793
794 if (!st) {
795 int dfd = open(dname, O_RDONLY|O_EXCL);
796 if (dfd < 0) {
797 pr_err("cannot open %s: %s\n",
798 dname, strerror(errno));
799 exit(2);
800 }
801 pr_err("device %s not suitable for any style of array\n",
802 dname);
803 exit(2);
804 }
805 if (st->ss != &super0 ||
806 st->minor_version != 90)
807 did_default = 1;
808 } else {
809 if (do_default_layout)
810 s->layout = default_layout(st, s->level, 0);
811 if (!st->ss->validate_geometry(st, s->level, s->layout,
812 s->raiddisks,
813 &s->chunk, s->size*2,
814 dv->data_offset,
815 dname, &freesize,
816 s->consistency_policy,
817 c->verbose >= 0)) {
818
819 pr_err("%s is not suitable for this array.\n",
820 dname);
821 fail = 1;
822 continue;
823 }
824 }
825
826 if (drive_test_and_add_policies(st, &custom_pols, dfd, 1))
827 exit(1);
828
829 close(dfd);
830
831 if (dv->disposition == 'j')
832 goto skip_size_check; /* skip write journal for size check */
833
834 freesize /= 2; /* convert to K */
835 if (s->chunk && s->chunk != UnSet) {
836 /* round to chunk size */
837 freesize = freesize & ~(s->chunk-1);
838 if (do_default_chunk) {
839 /* default chunk was just set */
840 if (c->verbose > 0)
841 pr_err("chunk size defaults to %dK\n", s->chunk);
842 if (round_size_and_verify(&s->size, s->chunk))
843 return 1;
844 do_default_chunk = 0;
845 }
846 }
847 if (!freesize) {
848 pr_err("no free space left on %s\n", dname);
849 fail = 1;
850 continue;
851 }
852
853 if (s->size && freesize < s->size) {
854 pr_err("%s is smaller than given size. %lluK < %lluK + metadata\n",
855 dname, freesize, s->size);
856 fail = 1;
857 continue;
858 }
859 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
860 maxdisc = dname;
861 maxsize = freesize;
862 }
863 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
864 mindisc = dname;
865 minsize = freesize;
866 }
867 skip_size_check:
868 if (c->runstop != 1 || c->verbose >= 0) {
869 int fd = open(dname, O_RDONLY);
870 if (fd < 0) {
871 pr_err("Cannot open %s: %s\n",
872 dname, strerror(errno));
873 fail = 1;
874 continue;
875 }
876 warn |= check_ext2(fd, dname);
877 warn |= check_reiser(fd, dname);
878 warn |= check_raid(fd, dname);
879 if (strcmp(st->ss->name, "1.x") == 0 &&
880 st->minor_version >= 1)
881 /* metadata at front */
882 warn |= check_partitions(fd, dname, 0, 0);
883 else if (s->level == 1 || is_container(s->level) ||
884 (s->level == 0 && s->raiddisks == 1))
885 /* partitions could be meaningful */
886 warn |= check_partitions(fd, dname, freesize*2, s->size*2);
887 else
888 /* partitions cannot be meaningful */
889 warn |= check_partitions(fd, dname, 0, 0);
890 if (strcmp(st->ss->name, "1.x") == 0 &&
891 st->minor_version >= 1 &&
892 did_default &&
893 s->level == 1 &&
894 (warn & 1024) == 0) {
895 warn |= 1024;
896 pr_err("Note: this array has metadata at the start and\n"
897 " may not be suitable as a boot device. If you plan to\n"
898 " store '/boot' on this device please ensure that\n"
899 " your boot-loader understands md/v1.x metadata, or use\n"
900 " --metadata=0.90\n");
901 }
902 close(fd);
903 }
904 }
905
906 if (missing_disks == dnum && !have_container) {
907 pr_err("Subdevs can't be all missing\n");
908 return 1;
909 }
910 if (s->raiddisks + s->sparedisks > st->max_devs) {
911 pr_err("Too many devices: %s metadata only supports %d\n",
912 st->ss->name, st->max_devs);
913 return 1;
914 }
915 if (have_container)
916 info.array.working_disks = s->raiddisks;
917 if (fail) {
918 pr_err("create aborted\n");
919 return 1;
920 }
921 if (s->size == 0) {
922 if (mindisc == NULL && !have_container) {
923 pr_err("no size and no drives given - aborting create.\n");
924 return 1;
925 }
926 if (s->level > 0 || s->level == LEVEL_MULTIPATH ||
927 s->level == LEVEL_FAULTY || st->ss->external) {
928 /* size is meaningful */
929 if (!st->ss->validate_geometry(st, s->level, s->layout,
930 s->raiddisks,
931 &s->chunk, minsize*2,
932 s->data_offset,
933 NULL, NULL,
934 s->consistency_policy, 0)) {
935 pr_err("devices too large for RAID level %d\n", s->level);
936 return 1;
937 }
938 s->size = minsize;
939 if (s->level == 1)
940 /* If this is ever reshaped to RAID5, we will
941 * need a chunksize. So round it off a bit
942 * now just to be safe
943 */
944 s->size &= ~(64ULL-1);
945 if (c->verbose > 0)
946 pr_err("size set to %lluK\n", s->size);
947 }
948 }
949
950 if (!s->bitmap_file &&
951 !st->ss->external &&
952 s->level >= 1 &&
953 st->ss->add_internal_bitmap &&
954 s->journaldisks == 0 &&
955 (s->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
956 s->consistency_policy != CONSISTENCY_POLICY_PPL) &&
957 (s->write_behind || s->size > 100*1024*1024ULL)) {
958 if (c->verbose > 0)
959 pr_err("automatically enabling write-intent bitmap on large array\n");
960 s->bitmap_file = "internal";
961 }
962 if (s->bitmap_file && str_is_none(s->bitmap_file) == true)
963 s->bitmap_file = NULL;
964
965 if (s->consistency_policy == CONSISTENCY_POLICY_PPL &&
966 !st->ss->write_init_ppl) {
967 pr_err("%s metadata does not support PPL\n", st->ss->name);
968 return 1;
969 }
970
971 if (st->ss == &super_imsm && s->level == 10 && s->raiddisks > 4) {
972 /* Print no matter runstop was specifed */
973 pr_err("Warning! VROC UEFI driver does not support RAID10 in requested layout.\n");
974 pr_err("Array won't be suitable as boot device.\n");
975 warn = 1;
976 }
977
978 if (!have_container && s->level > 0 && ((maxsize-s->size)*100 > maxsize)) {
979 if (c->runstop != 1 || c->verbose >= 0)
980 pr_err("largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
981 maxdisc, s->size);
982 warn = 1;
983 }
984
985 if (st->ss->detail_platform && st->ss->detail_platform(0, 1, NULL) != 0) {
986 if (c->runstop != 1 || c->verbose >= 0)
987 pr_err("%s unable to enumerate platform support\n"
988 " array may not be compatible with hardware/firmware\n",
989 st->ss->name);
990 warn = 1;
991 }
992 st->nodes = c->nodes;
993 st->cluster_name = c->homecluster;
994
995 if (warn) {
996 if (c->runstop!= 1) {
997 if (!ask("Continue creating array")) {
998 pr_err("create aborted.\n");
999 return 1;
1000 }
1001 } else {
1002 if (c->verbose > 0)
1003 pr_err("creation continuing despite oddities due to --run\n");
1004 }
1005 }
1006
1007 /* If this is raid4/5, we want to configure the last active slot
1008 * as missing, so that a reconstruct happens (faster than re-parity)
1009 * FIX: Can we do this for raid6 as well?
1010 */
1011 if (st->ss->external == 0 && s->assume_clean == 0 &&
1012 c->force == 0 && first_missing >= s->raiddisks) {
1013 switch (s->level) {
1014 case 4:
1015 case 5:
1016 insert_point = s->raiddisks-1;
1017 s->sparedisks++;
1018 info.array.active_disks--;
1019 missing_disks++;
1020 break;
1021 default:
1022 break;
1023 }
1024 }
1025 /* For raid6, if creating with 1 missing drive, make a good drive
1026 * into a spare, else the create will fail
1027 */
1028 if (s->assume_clean == 0 && c->force == 0 && first_missing < s->raiddisks &&
1029 st->ss->external == 0 &&
1030 second_missing >= s->raiddisks && s->level == 6) {
1031 insert_point = s->raiddisks - 1;
1032 if (insert_point == first_missing)
1033 insert_point--;
1034 s->sparedisks ++;
1035 info.array.active_disks--;
1036 missing_disks++;
1037 }
1038
1039 if (s->level <= 0 && first_missing < subdevs * 2) {
1040 pr_err("This level does not support missing devices\n");
1041 return 1;
1042 }
1043
1044 /* We need to create the device */
1045 map_lock(&map);
1046 mdfd = create_mddev(ident->devname, ident->name, c->autof, LOCAL, chosen_name, 1);
1047 if (mdfd < 0) {
1048 map_unlock(&map);
1049 return 1;
1050 }
1051 /* verify if chosen_name is not in use,
1052 * it could be in conflict with already existing device
1053 * e.g. container, array
1054 */
1055 if (strncmp(chosen_name, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0 &&
1056 map_by_name(&map, chosen_name + DEV_MD_DIR_LEN)) {
1057 pr_err("Array name %s is in use already.\n", chosen_name);
1058 close(mdfd);
1059 map_unlock(&map);
1060 udev_unblock();
1061 return 1;
1062 }
1063
1064 memset(&inf, 0, sizeof(inf));
1065 md_get_array_info(mdfd, &inf);
1066 if (inf.working_disks != 0) {
1067 pr_err("another array by this name is already running.\n");
1068 goto abort_locked;
1069 }
1070
1071 /* Ok, lets try some ioctls */
1072
1073 info.array.level = s->level;
1074 info.array.size = s->size;
1075 info.array.raid_disks = s->raiddisks;
1076 /* The kernel should *know* what md_minor we are dealing
1077 * with, but it chooses to trust me instead. Sigh
1078 */
1079 info.array.md_minor = 0;
1080 if (fstat_is_blkdev(mdfd, chosen_name, &rdev))
1081 info.array.md_minor = minor(rdev);
1082 info.array.not_persistent = 0;
1083
1084 if (((s->level == 4 || s->level == 5) &&
1085 (insert_point < s->raiddisks || first_missing < s->raiddisks)) ||
1086 (s->level == 6 && (insert_point < s->raiddisks ||
1087 second_missing < s->raiddisks)) ||
1088 (s->level <= 0) || s->assume_clean) {
1089 info.array.state = 1; /* clean, but one+ drive will be missing*/
1090 info.resync_start = MaxSector;
1091 } else {
1092 info.array.state = 0; /* not clean, but no errors */
1093 info.resync_start = 0;
1094 }
1095 if (s->level == 10) {
1096 /* for raid10, the bitmap size is the capacity of the array,
1097 * which is array.size * raid_disks / ncopies;
1098 * .. but convert to sectors.
1099 */
1100 int ncopies = ((s->layout>>8) & 255) * (s->layout & 255);
1101 bitmapsize = s->size * s->raiddisks / ncopies * 2;
1102/* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, s->size, s->raiddisks, ncopies);*/
1103 } else
1104 bitmapsize = s->size * 2;
1105
1106 /* There is lots of redundancy in these disk counts,
1107 * raid_disks is the most meaningful value
1108 * it describes the geometry of the array
1109 * it is constant
1110 * nr_disks is total number of used slots.
1111 * it should be raid_disks+spare_disks
1112 * spare_disks is the number of extra disks present
1113 * see above
1114 * active_disks is the number of working disks in
1115 * active slots. (With raid_disks)
1116 * working_disks is the total number of working disks,
1117 * including spares
1118 * failed_disks is the number of disks marked failed
1119 *
1120 * Ideally, the kernel would keep these (except raid_disks)
1121 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
1122 * So for now, we assume that all raid and spare
1123 * devices will be given.
1124 */
1125 info.array.spare_disks=s->sparedisks;
1126 info.array.failed_disks=missing_disks;
1127 info.array.nr_disks = info.array.working_disks
1128 + info.array.failed_disks;
1129 info.array.layout = s->layout;
1130 info.array.chunk_size = s->chunk*1024;
1131
1132 if (*name == 0) {
1133 /* base name on devname */
1134 /* /dev/md0 -> 0
1135 * /dev/md_d0 -> d0
1136 * /dev/md_foo -> foo
1137 * /dev/md/1 -> 1
1138 * /dev/md/d1 -> d1
1139 * /dev/md/home -> home
1140 * /dev/mdhome -> home
1141 */
1142 /* FIXME compare this with rules in create_mddev */
1143 name = strrchr(chosen_name, '/');
1144
1145 if (name) {
1146 name++;
1147 if (strncmp(name, "md_", 3) == 0 &&
1148 strlen(name) > 3 && (name - chosen_name) == 5 /* /dev/ */)
1149 name += 3;
1150 else if (strncmp(name, "md", 2) == 0 &&
1151 strlen(name) > 2 && isdigit(name[2]) &&
1152 (name - chosen_name) == 5 /* /dev/ */)
1153 name += 2;
1154 }
1155 }
1156 if (!st->ss->init_super(st, &info.array, s, name, c->homehost, uuid,
1157 s->data_offset))
1158 goto abort_locked;
1159
1160 total_slots = info.array.nr_disks;
1161 st->ss->getinfo_super(st, &info, NULL);
1162 if (sysfs_init(&info, mdfd, NULL)) {
1163 pr_err("unable to initialize sysfs\n");
1164 goto abort_locked;
1165 }
1166
1167 if (did_default) {
1168 if (is_subarray(info.text_version)) {
1169 char devnm[MD_NAME_MAX];
1170 struct mdinfo *mdi;
1171
1172 sysfs_get_container_devnm(&info, devnm);
1173
1174 mdi = sysfs_read(-1, devnm, GET_VERSION | GET_DEVS);
1175 if (!mdi) {
1176 pr_err("Cannot open sysfs for container %s\n", devnm);
1177 goto abort_locked;
1178 }
1179
1180 if (sysfs_test_and_add_drive_policies(st, &custom_pols, mdi, 1))
1181 goto abort_locked;
1182
1183 if (c->verbose >= 0)
1184 pr_info("Creating array inside %s container /dev/%s\n",
1185 mdi->text_version, devnm);
1186
1187 sysfs_free(mdi);
1188 } else if (c->verbose >= 0) {
1189 pr_info("Defaulting to version %s metadata\n", info.text_version);
1190 }
1191 }
1192
1193 map_update(&map, fd2devnm(mdfd), info.text_version,
1194 info.uuid, chosen_name);
1195 /* Keep map locked until devices have been added to array
1196 * to stop another mdadm from finding and using those devices.
1197 */
1198
1199 if (s->bitmap_file && (strcmp(s->bitmap_file, "internal") == 0 ||
1200 strcmp(s->bitmap_file, "clustered") == 0)) {
1201 if (!st->ss->add_internal_bitmap) {
1202 pr_err("internal bitmaps not supported with %s metadata\n",
1203 st->ss->name);
1204 goto abort_locked;
1205 }
1206 if (st->ss->add_internal_bitmap(st, &s->bitmap_chunk,
1207 c->delay, s->write_behind,
1208 bitmapsize, 1, major_num)) {
1209 pr_err("Given bitmap chunk size not supported.\n");
1210 goto abort_locked;
1211 }
1212 s->bitmap_file = NULL;
1213 }
1214
1215 if (sysfs_init(&info, mdfd, NULL)) {
1216 pr_err("unable to initialize sysfs\n");
1217 goto abort_locked;
1218 }
1219
1220 if (st->ss->external && st->container_devnm[0]) {
1221 /* member */
1222
1223 /* When creating a member, we need to be careful
1224 * to negotiate with mdmon properly.
1225 * If it is already running, we cannot write to
1226 * the devices and must ask it to do that part.
1227 * If it isn't running, we write to the devices,
1228 * and then start it.
1229 * We hold an exclusive open on the container
1230 * device to make sure mdmon doesn't exit after
1231 * we checked that it is running.
1232 *
1233 * For now, fail if it is already running.
1234 */
1235 container_fd = open_dev_excl(st->container_devnm);
1236 if (container_fd < 0) {
1237 pr_err("Cannot get exclusive open on container - weird.\n");
1238 goto abort_locked;
1239 }
1240 if (mdmon_running(st->container_devnm)) {
1241 if (c->verbose)
1242 pr_err("reusing mdmon for %s.\n",
1243 st->container_devnm);
1244 st->update_tail = &st->updates;
1245 } else
1246 need_mdmon = 1;
1247 }
1248 rv = set_array_info(mdfd, st, &info);
1249 if (rv) {
1250 pr_err("failed to set array info for %s: %s\n", chosen_name, strerror(errno));
1251 goto abort_locked;
1252 }
1253
1254 if (s->bitmap_file) {
1255 int uuid[4];
1256
1257 st->ss->uuid_from_super(st, uuid);
1258 if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid, s->bitmap_chunk,
1259 c->delay, s->write_behind,
1260 bitmapsize,
1261 major_num)) {
1262 goto abort_locked;
1263 }
1264 bitmap_fd = open(s->bitmap_file, O_RDWR);
1265 if (bitmap_fd < 0) {
1266 pr_err("weird: %s cannot be opened\n",
1267 s->bitmap_file);
1268 goto abort_locked;
1269 }
1270 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
1271 pr_err("Cannot set bitmap file for %s: %s\n", chosen_name, strerror(errno));
1272 goto abort_locked;
1273 }
1274 }
1275
1276 if (add_disks(mdfd, &info, s, c, st, &map, devlist, total_slots,
1277 have_container, insert_point, major_num, chosen_name))
1278 goto abort_locked;
1279
1280 map_unlock(&map);
1281
1282 if (is_container(s->level)) {
1283 /* No need to start. But we should signal udev to
1284 * create links */
1285 sysfs_uevent(&info, "change");
1286 if (c->verbose >= 0)
1287 pr_err("container %s prepared.\n", chosen_name);
1288 wait_for(chosen_name, mdfd);
1289 } else if (c->runstop == 1 || subdevs >= s->raiddisks) {
1290 if (st->ss->external) {
1291 int err;
1292 switch(s->level) {
1293 case LEVEL_LINEAR:
1294 case LEVEL_MULTIPATH:
1295 case 0:
1296 err = sysfs_set_str(&info, NULL, "array_state",
1297 c->readonly
1298 ? "readonly"
1299 : "active");
1300 need_mdmon = 0;
1301 break;
1302 default:
1303 err = sysfs_set_str(&info, NULL, "array_state",
1304 "readonly");
1305 break;
1306 }
1307 sysfs_set_safemode(&info, info.safe_mode_delay);
1308 if (err) {
1309 pr_err("failed to activate array.\n");
1310 ioctl(mdfd, STOP_ARRAY, NULL);
1311 goto abort;
1312 }
1313 } else if (c->readonly &&
1314 sysfs_attribute_available(
1315 &info, NULL, "array_state")) {
1316 if (sysfs_set_str(&info, NULL,
1317 "array_state", "readonly") < 0) {
1318 pr_err("Failed to start array: %s\n",
1319 strerror(errno));
1320 ioctl(mdfd, STOP_ARRAY, NULL);
1321 goto abort;
1322 }
1323 } else {
1324 /* param is not actually used */
1325 mdu_param_t param;
1326 if (ioctl(mdfd, RUN_ARRAY, &param)) {
1327 pr_err("RUN_ARRAY failed: %s\n",
1328 strerror(errno));
1329 if (errno == 524 /* ENOTSUP */ &&
1330 info.array.level == 0)
1331 cont_err("Please use --layout=original or --layout=alternate\n");
1332 if (info.array.chunk_size & (info.array.chunk_size-1)) {
1333 cont_err("Problem may be that chunk size is not a power of 2\n");
1334 }
1335 ioctl(mdfd, STOP_ARRAY, NULL);
1336 goto abort;
1337 }
1338 /* if start_ro module parameter is set, array is
1339 * auto-read-only, which is bad as the resync won't
1340 * start. So lets make it read-write now.
1341 */
1342 ioctl(mdfd, RESTART_ARRAY_RW, NULL);
1343 }
1344 if (c->verbose >= 0)
1345 pr_info("array %s started.\n", chosen_name);
1346 if (st->ss->external && st->container_devnm[0]) {
1347 if (need_mdmon)
1348 start_mdmon(st->container_devnm);
1349
1350 ping_monitor(st->container_devnm);
1351 close(container_fd);
1352 }
1353 wait_for(chosen_name, mdfd);
1354 } else {
1355 pr_err("not starting array - not enough devices.\n");
1356 }
1357 udev_unblock();
1358 close(mdfd);
1359 sysfs_uevent(&info, "change");
1360 dev_policy_free(custom_pols);
1361
1362 return 0;
1363
1364 abort:
1365 udev_unblock();
1366 map_lock(&map);
1367 abort_locked:
1368 map_remove(&map, fd2devnm(mdfd));
1369 map_unlock(&map);
1370
1371 if (mdfd >= 0)
1372 close(mdfd);
1373
1374 dev_policy_free(custom_pols);
1375 return 1;
1376}