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