]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
Create.c: fix uclibc build
[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 "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
39 static 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 */
61 int 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
103 static 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
174 static 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
242 static int add_disk_to_super(int mdfd, struct shape *s, struct context *c,
243 struct supertype *st, struct mddev_dev *dv,
244 struct mdinfo *info, int have_container, int major_num,
245 int *zero_pid)
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 }
286 if (!fstat_is_blkdev(fd, dv->devname, &rdev)) {
287 close(fd);
288 return 1;
289 }
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);
298 close(fd);
299 return 1;
300 }
301 st->ss->getinfo_super(st, info, NULL);
302
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);
307 close(fd);
308 return 1;
309 }
310 }
311
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
326 static 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);
340 if (st->ss->external && !is_container(s->level) &&
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
380 static 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;
388 int zero_pids[total_slots];
389 struct mddev_dev *dv;
390 struct mdinfo *infos;
391 sigset_t sigset, orig_sigset;
392 int ret = 0;
393
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
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,
438 major_num, &zero_pids[dnum]);
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) {
464 ret = wait_for_zero_forks(zero_pids, total_slots);
465 if (ret)
466 goto out;
467
468 ret = update_metadata(mdfd, s, st, map, info,
469 chosen_name);
470 if (ret)
471 goto out;
472 }
473 }
474
475 out:
476 if (ret)
477 wait_for_zero_forks(zero_pids, total_slots);
478 free(infos);
479 sigprocmask(SIG_SETMASK, &orig_sigset, NULL);
480 return ret;
481 }
482
483 int Create(struct supertype *st, struct mddev_ident *ident, int subdevs,
484 struct mddev_dev *devlist, struct shape *s, struct context *c)
485 {
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
499 * if runstop==run, or raiddisks disks were used,
500 * RUN_ARRAY
501 */
502 int mdfd;
503 unsigned long long minsize = 0, maxsize = 0;
504 dev_policy_t *custom_pols = NULL;
505 char *mindisc = NULL;
506 char *maxdisc = NULL;
507 char *name = ident->name;
508 int *uuid = ident->uuid_set == 1 ? ident->uuid : NULL;
509 int dnum;
510 struct mddev_dev *dv;
511 dev_t rdev;
512 int fail = 0, warn = 0;
513 int first_missing = subdevs * 2;
514 int second_missing = subdevs * 2;
515 int missing_disks = 0;
516 int insert_point = subdevs * 2; /* where to insert a missing drive */
517 int total_slots;
518 int rv;
519 int bitmap_fd;
520 int have_container = 0;
521 int container_fd = -1;
522 int need_mdmon = 0;
523 unsigned long long bitmapsize;
524 struct mdinfo info;
525 int did_default = 0;
526 int do_default_layout = 0;
527 int do_default_chunk = 0;
528 char chosen_name[1024];
529 struct map_ent *map = NULL;
530 unsigned long long newsize;
531 mdu_array_info_t inf;
532
533 int major_num = BITMAP_MAJOR_HI;
534 if (s->bitmap_file && strcmp(s->bitmap_file, "clustered") == 0) {
535 major_num = BITMAP_MAJOR_CLUSTERED;
536 if (c->nodes <= 1) {
537 pr_err("At least 2 nodes are needed for cluster-md\n");
538 return 1;
539 }
540 }
541
542 memset(&info, 0, sizeof(info));
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) {
546 pr_err("a RAID level is needed to create an array.\n");
547 return 1;
548 }
549 if (s->raiddisks < 4 && s->level == 6) {
550 pr_err("at least 4 raid-devices needed for level 6\n");
551 return 1;
552 }
553 if (s->raiddisks > 256 && s->level == 6) {
554 pr_err("no more than 256 raid-devices supported for level 6\n");
555 return 1;
556 }
557 if (s->raiddisks < 2 && s->level >= 4) {
558 pr_err("at least 2 raid-devices needed for level %d\n", s->level);
559 return 1;
560 }
561 if (s->level <= 0 && s->sparedisks) {
562 pr_err("This level does not support spare devices\n");
563 return 1;
564 }
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 */
570 int fd;
571
572 memset(&inf, 0, sizeof(inf));
573 fd = open(devlist->devname, O_RDONLY);
574 if (fd >= 0 &&
575 md_get_array_info(fd, &inf) == 0 && inf.raid_disks == 0) {
576 /* yep, looks like a container */
577 if (st) {
578 rv = st->ss->load_container(st, fd,
579 devlist->devname);
580 if (rv == 0)
581 have_container = 1;
582 } else {
583 st = super_by_fd(fd, NULL);
584 if (st && !(rv = st->ss->
585 load_container(st, fd,
586 devlist->devname)))
587 have_container = 1;
588 else
589 st = NULL;
590 }
591 if (have_container) {
592 subdevs = s->raiddisks;
593 first_missing = subdevs * 2;
594 second_missing = subdevs * 2;
595 insert_point = subdevs * 2;
596
597 if (mddev_test_and_add_drive_policies(st, &custom_pols, fd, 1))
598 exit(1);
599 }
600 }
601 if (fd >= 0)
602 close(fd);
603 }
604 if (st && st->ss->external && s->sparedisks) {
605 pr_err("This metadata type does not support spare disks at create time\n");
606 return 1;
607 }
608 if (subdevs > s->raiddisks+s->sparedisks+s->journaldisks) {
609 pr_err("You have listed more devices (%d) than are in the array(%d)!\n", subdevs, s->raiddisks+s->sparedisks);
610 return 1;
611 }
612 if (!have_container && subdevs < s->raiddisks+s->sparedisks+s->journaldisks) {
613 pr_err("You haven't given enough devices (real or missing) to create this array\n");
614 return 1;
615 }
616 if (s->bitmap_file && s->level <= 0) {
617 pr_err("bitmaps not meaningful with level %s\n",
618 map_num(pers, s->level)?:"given");
619 return 1;
620 }
621
622 /* now set some defaults */
623
624 if (s->layout == UnSet) {
625 do_default_layout = 1;
626 s->layout = default_layout(st, s->level, c->verbose);
627 }
628
629 if (s->level == 10)
630 /* check layout fits in array*/
631 if ((s->layout&255) * ((s->layout>>8)&255) > s->raiddisks) {
632 pr_err("that layout requires at least %d devices\n",
633 (s->layout&255) * ((s->layout>>8)&255));
634 return 1;
635 }
636
637 switch(s->level) {
638 case 4:
639 case 5:
640 case 10:
641 case 6:
642 case 0:
643 if (s->chunk == 0 || s->chunk == UnSet) {
644 s->chunk = UnSet;
645 do_default_chunk = 1;
646 /* chunk will be set later */
647 }
648 break;
649 case LEVEL_LINEAR:
650 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
651 break;
652 case 1:
653 case LEVEL_FAULTY:
654 case LEVEL_MULTIPATH:
655 case LEVEL_CONTAINER:
656 if (s->chunk) {
657 pr_err("specifying chunk size is forbidden for this level\n");
658 return 1;
659 }
660 break;
661 default:
662 pr_err("unknown level %d\n", s->level);
663 return 1;
664 }
665
666 if (s->size == MAX_SIZE)
667 /* use '0' to mean 'max' now... */
668 s->size = 0;
669 if (s->size && s->chunk && s->chunk != UnSet)
670 if (round_size_and_verify(&s->size, s->chunk))
671 return 1;
672
673 newsize = s->size * 2;
674 if (st && ! st->ss->validate_geometry(st, s->level, s->layout, s->raiddisks,
675 &s->chunk, s->size*2,
676 s->data_offset, NULL,
677 &newsize, s->consistency_policy,
678 c->verbose >= 0))
679 return 1;
680
681 if (s->chunk && s->chunk != UnSet) {
682 newsize &= ~(unsigned long long)(s->chunk*2 - 1);
683 if (do_default_chunk) {
684 /* default chunk was just set */
685 if (c->verbose > 0)
686 pr_err("chunk size defaults to %dK\n", s->chunk);
687 if (round_size_and_verify(&s->size, s->chunk))
688 return 1;
689 do_default_chunk = 0;
690 }
691 }
692
693 if (s->size == 0) {
694 s->size = newsize / 2;
695 if (s->level == 1)
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 */
700 s->size &= ~(64ULL-1);
701
702 if (s->size && c->verbose > 0)
703 pr_err("setting size to %lluK\n", s->size);
704 }
705
706 /* now look at the subdevs */
707 info.array.active_disks = 0;
708 info.array.working_disks = 0;
709 dnum = 0;
710 for (dv = devlist; dv; dv = dv->next)
711 if (s->data_offset == VARIABLE_OFFSET)
712 dv->data_offset = INVALID_SECTORS;
713 else
714 dv->data_offset = s->data_offset;
715
716 for (dv=devlist; dv && !have_container; dv=dv->next, dnum++) {
717 char *dname = dv->devname;
718 unsigned long long freesize;
719 int dfd;
720 char *doff;
721
722 if (strcasecmp(dname, "missing") == 0) {
723 if (first_missing > dnum)
724 first_missing = dnum;
725 if (second_missing > dnum && dnum > first_missing)
726 second_missing = dnum;
727 missing_disks ++;
728 continue;
729 }
730 if (s->data_offset == VARIABLE_OFFSET) {
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
738 dv->data_offset = s->data_offset;
739
740 dfd = open(dname, O_RDONLY);
741 if (dfd < 0) {
742 pr_err("cannot open %s: %s\n",
743 dname, strerror(errno));
744 exit(2);
745 }
746 if (!fstat_is_blkdev(dfd, dname, NULL)) {
747 close(dfd);
748 exit(2);
749 }
750
751 info.array.working_disks++;
752 if (dnum < s->raiddisks && dv->disposition != 'j')
753 info.array.active_disks++;
754 if (st == NULL) {
755 struct createinfo *ci = conf_get_create_info();
756 if (ci)
757 st = ci->supertype;
758 }
759 if (st == NULL) {
760 /* Need to choose a default metadata, which is different
761 * depending on geometry of array.
762 */
763 int i;
764 char *name = "default";
765 for(i = 0; !st && superlist[i]; i++) {
766 st = superlist[i]->match_metadata_desc(name);
767 if (!st)
768 continue;
769 if (do_default_layout)
770 s->layout = default_layout(st, s->level, c->verbose);
771 switch (st->ss->validate_geometry(
772 st, s->level, s->layout, s->raiddisks,
773 &s->chunk, s->size*2,
774 dv->data_offset, dname,
775 &freesize, s->consistency_policy,
776 c->verbose > 0)) {
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 */
782 free(st);
783 st = NULL;
784 s->chunk = do_default_chunk ? UnSet : s->chunk;
785 break;
786 case 1: /* All happy */
787 break;
788 }
789 }
790
791 if (!st) {
792 int dfd = open(dname, O_RDONLY|O_EXCL);
793 if (dfd < 0) {
794 pr_err("cannot open %s: %s\n",
795 dname, strerror(errno));
796 exit(2);
797 }
798 pr_err("device %s not suitable for any style of array\n",
799 dname);
800 exit(2);
801 }
802 if (st->ss != &super0 ||
803 st->minor_version != 90)
804 did_default = 1;
805 } else {
806 if (do_default_layout)
807 s->layout = default_layout(st, s->level, 0);
808 if (!st->ss->validate_geometry(st, s->level, s->layout,
809 s->raiddisks,
810 &s->chunk, s->size*2,
811 dv->data_offset,
812 dname, &freesize,
813 s->consistency_policy,
814 c->verbose >= 0)) {
815
816 pr_err("%s is not suitable for this array.\n",
817 dname);
818 fail = 1;
819 continue;
820 }
821 }
822
823 if (drive_test_and_add_policies(st, &custom_pols, dfd, 1))
824 exit(1);
825
826 close(dfd);
827
828 if (dv->disposition == 'j')
829 goto skip_size_check; /* skip write journal for size check */
830
831 freesize /= 2; /* convert to K */
832 if (s->chunk && s->chunk != UnSet) {
833 /* round to chunk size */
834 freesize = freesize & ~(s->chunk-1);
835 if (do_default_chunk) {
836 /* default chunk was just set */
837 if (c->verbose > 0)
838 pr_err("chunk size defaults to %dK\n", s->chunk);
839 if (round_size_and_verify(&s->size, s->chunk))
840 return 1;
841 do_default_chunk = 0;
842 }
843 }
844 if (!freesize) {
845 pr_err("no free space left on %s\n", dname);
846 fail = 1;
847 continue;
848 }
849
850 if (s->size && freesize < s->size) {
851 pr_err("%s is smaller than given size. %lluK < %lluK + metadata\n",
852 dname, freesize, s->size);
853 fail = 1;
854 continue;
855 }
856 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
857 maxdisc = dname;
858 maxsize = freesize;
859 }
860 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
861 mindisc = dname;
862 minsize = freesize;
863 }
864 skip_size_check:
865 if (c->runstop != 1 || c->verbose >= 0) {
866 int fd = open(dname, O_RDONLY);
867 if (fd < 0) {
868 pr_err("Cannot open %s: %s\n",
869 dname, strerror(errno));
870 fail = 1;
871 continue;
872 }
873 warn |= check_ext2(fd, dname);
874 warn |= check_reiser(fd, dname);
875 warn |= check_raid(fd, dname);
876 if (strcmp(st->ss->name, "1.x") == 0 &&
877 st->minor_version >= 1)
878 /* metadata at front */
879 warn |= check_partitions(fd, dname, 0, 0);
880 else if (s->level == 1 || is_container(s->level) ||
881 (s->level == 0 && s->raiddisks == 1))
882 /* partitions could be meaningful */
883 warn |= check_partitions(fd, dname, freesize*2, s->size*2);
884 else
885 /* partitions cannot be meaningful */
886 warn |= check_partitions(fd, dname, 0, 0);
887 if (strcmp(st->ss->name, "1.x") == 0 &&
888 st->minor_version >= 1 &&
889 did_default &&
890 s->level == 1 &&
891 (warn & 1024) == 0) {
892 warn |= 1024;
893 pr_err("Note: this array has metadata at the start and\n"
894 " may not be suitable as a boot device. If you plan to\n"
895 " store '/boot' on this device please ensure that\n"
896 " your boot-loader understands md/v1.x metadata, or use\n"
897 " --metadata=0.90\n");
898 }
899 close(fd);
900 }
901 }
902
903 if (missing_disks == dnum && !have_container) {
904 pr_err("Subdevs can't be all missing\n");
905 return 1;
906 }
907 if (s->raiddisks + s->sparedisks > st->max_devs) {
908 pr_err("Too many devices: %s metadata only supports %d\n",
909 st->ss->name, st->max_devs);
910 return 1;
911 }
912 if (have_container)
913 info.array.working_disks = s->raiddisks;
914 if (fail) {
915 pr_err("create aborted\n");
916 return 1;
917 }
918 if (s->size == 0) {
919 if (mindisc == NULL && !have_container) {
920 pr_err("no size and no drives given - aborting create.\n");
921 return 1;
922 }
923 if (s->level > 0 || s->level == LEVEL_MULTIPATH ||
924 s->level == LEVEL_FAULTY || st->ss->external) {
925 /* size is meaningful */
926 if (!st->ss->validate_geometry(st, s->level, s->layout,
927 s->raiddisks,
928 &s->chunk, minsize*2,
929 s->data_offset,
930 NULL, NULL,
931 s->consistency_policy, 0)) {
932 pr_err("devices too large for RAID level %d\n", s->level);
933 return 1;
934 }
935 s->size = minsize;
936 if (s->level == 1)
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 */
941 s->size &= ~(64ULL-1);
942 if (c->verbose > 0)
943 pr_err("size set to %lluK\n", s->size);
944 }
945 }
946
947 if (!s->bitmap_file &&
948 !st->ss->external &&
949 s->level >= 1 &&
950 st->ss->add_internal_bitmap &&
951 s->journaldisks == 0 &&
952 (s->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
953 s->consistency_policy != CONSISTENCY_POLICY_PPL) &&
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 }
959 if (s->bitmap_file && str_is_none(s->bitmap_file) == true)
960 s->bitmap_file = NULL;
961
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
968 if (!have_container && s->level > 0 && ((maxsize-s->size)*100 > maxsize)) {
969 if (c->runstop != 1 || c->verbose >= 0)
970 pr_err("largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
971 maxdisc, s->size);
972 warn = 1;
973 }
974
975 if (st->ss->detail_platform && st->ss->detail_platform(0, 1, NULL) != 0) {
976 if (c->runstop != 1 || c->verbose >= 0)
977 pr_err("%s unable to enumerate platform support\n"
978 " array may not be compatible with hardware/firmware\n",
979 st->ss->name);
980 warn = 1;
981 }
982 st->nodes = c->nodes;
983 st->cluster_name = c->homecluster;
984
985 if (warn) {
986 if (c->runstop!= 1) {
987 if (!ask("Continue creating array? ")) {
988 pr_err("create aborted.\n");
989 return 1;
990 }
991 } else {
992 if (c->verbose > 0)
993 pr_err("creation continuing despite oddities due to --run\n");
994 }
995 }
996
997 /* If this is raid4/5, we want to configure the last active slot
998 * as missing, so that a reconstruct happens (faster than re-parity)
999 * FIX: Can we do this for raid6 as well?
1000 */
1001 if (st->ss->external == 0 && s->assume_clean == 0 &&
1002 c->force == 0 && first_missing >= s->raiddisks) {
1003 switch (s->level) {
1004 case 4:
1005 case 5:
1006 insert_point = s->raiddisks-1;
1007 s->sparedisks++;
1008 info.array.active_disks--;
1009 missing_disks++;
1010 break;
1011 default:
1012 break;
1013 }
1014 }
1015 /* For raid6, if creating with 1 missing drive, make a good drive
1016 * into a spare, else the create will fail
1017 */
1018 if (s->assume_clean == 0 && c->force == 0 && first_missing < s->raiddisks &&
1019 st->ss->external == 0 &&
1020 second_missing >= s->raiddisks && s->level == 6) {
1021 insert_point = s->raiddisks - 1;
1022 if (insert_point == first_missing)
1023 insert_point--;
1024 s->sparedisks ++;
1025 info.array.active_disks--;
1026 missing_disks++;
1027 }
1028
1029 if (s->level <= 0 && first_missing < subdevs * 2) {
1030 pr_err("This level does not support missing devices\n");
1031 return 1;
1032 }
1033
1034 /* We need to create the device */
1035 map_lock(&map);
1036 mdfd = create_mddev(ident->devname, ident->name, c->autof, LOCAL, chosen_name, 1);
1037 if (mdfd < 0) {
1038 map_unlock(&map);
1039 return 1;
1040 }
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 */
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);
1048 close(mdfd);
1049 map_unlock(&map);
1050 udev_unblock();
1051 return 1;
1052 }
1053
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");
1058 goto abort_locked;
1059 }
1060
1061 /* Ok, lets try some ioctls */
1062
1063 info.array.level = s->level;
1064 info.array.size = s->size;
1065 info.array.raid_disks = s->raiddisks;
1066 /* The kernel should *know* what md_minor we are dealing
1067 * with, but it chooses to trust me instead. Sigh
1068 */
1069 info.array.md_minor = 0;
1070 if (fstat_is_blkdev(mdfd, chosen_name, &rdev))
1071 info.array.md_minor = minor(rdev);
1072 info.array.not_persistent = 0;
1073
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) {
1079 info.array.state = 1; /* clean, but one+ drive will be missing*/
1080 info.resync_start = MaxSector;
1081 } else {
1082 info.array.state = 0; /* not clean, but no errors */
1083 info.resync_start = 0;
1084 }
1085 if (s->level == 10) {
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 */
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);*/
1093 } else
1094 bitmapsize = s->size * 2;
1095
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 *
1110 * Ideally, the kernel would keep these (except raid_disks)
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 */
1115 info.array.spare_disks=s->sparedisks;
1116 info.array.failed_disks=missing_disks;
1117 info.array.nr_disks = info.array.working_disks
1118 + info.array.failed_disks;
1119 info.array.layout = s->layout;
1120 info.array.chunk_size = s->chunk*1024;
1121
1122 if (*name == 0) {
1123 /* base name on devname */
1124 /* /dev/md0 -> 0
1125 * /dev/md_d0 -> d0
1126 * /dev/md_foo -> foo
1127 * /dev/md/1 -> 1
1128 * /dev/md/d1 -> d1
1129 * /dev/md/home -> home
1130 * /dev/mdhome -> home
1131 */
1132 /* FIXME compare this with rules in create_mddev */
1133 name = strrchr(chosen_name, '/');
1134
1135 if (name) {
1136 name++;
1137 if (strncmp(name, "md_", 3) == 0 &&
1138 strlen(name) > 3 && (name - chosen_name) == 5 /* /dev/ */)
1139 name += 3;
1140 else if (strncmp(name, "md", 2) == 0 &&
1141 strlen(name) > 2 && isdigit(name[2]) &&
1142 (name - chosen_name) == 5 /* /dev/ */)
1143 name += 2;
1144 }
1145 }
1146 if (!st->ss->init_super(st, &info.array, s, name, c->homehost, uuid,
1147 s->data_offset))
1148 goto abort_locked;
1149
1150 total_slots = info.array.nr_disks;
1151 st->ss->getinfo_super(st, &info, NULL);
1152 if (sysfs_init(&info, mdfd, NULL)) {
1153 pr_err("unable to initialize sysfs\n");
1154 goto abort_locked;
1155 }
1156
1157 if (did_default) {
1158 if (is_subarray(info.text_version)) {
1159 char devnm[MD_NAME_MAX];
1160 struct mdinfo *mdi;
1161
1162 sysfs_get_container_devnm(&info, devnm);
1163
1164 mdi = sysfs_read(-1, devnm, GET_VERSION | GET_DEVS);
1165 if (!mdi) {
1166 pr_err("Cannot open sysfs for container %s\n", devnm);
1167 goto abort_locked;
1168 }
1169
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);
1176
1177 sysfs_free(mdi);
1178 } else if (c->verbose >= 0) {
1179 pr_info("Defaulting to version %s metadata\n", info.text_version);
1180 }
1181 }
1182
1183 map_update(&map, fd2devnm(mdfd), info.text_version,
1184 info.uuid, chosen_name);
1185 /* Keep map locked until devices have been added to array
1186 * to stop another mdadm from finding and using those devices.
1187 */
1188
1189 if (s->bitmap_file && (strcmp(s->bitmap_file, "internal") == 0 ||
1190 strcmp(s->bitmap_file, "clustered") == 0)) {
1191 if (!st->ss->add_internal_bitmap) {
1192 pr_err("internal bitmaps not supported with %s metadata\n",
1193 st->ss->name);
1194 goto abort_locked;
1195 }
1196 if (st->ss->add_internal_bitmap(st, &s->bitmap_chunk,
1197 c->delay, s->write_behind,
1198 bitmapsize, 1, major_num)) {
1199 pr_err("Given bitmap chunk size not supported.\n");
1200 goto abort_locked;
1201 }
1202 s->bitmap_file = NULL;
1203 }
1204
1205 if (sysfs_init(&info, mdfd, NULL)) {
1206 pr_err("unable to initialize sysfs\n");
1207 goto abort_locked;
1208 }
1209
1210 if (st->ss->external && st->container_devnm[0]) {
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 */
1225 container_fd = open_dev_excl(st->container_devnm);
1226 if (container_fd < 0) {
1227 pr_err("Cannot get exclusive open on container - weird.\n");
1228 goto abort_locked;
1229 }
1230 if (mdmon_running(st->container_devnm)) {
1231 if (c->verbose)
1232 pr_err("reusing mdmon for %s.\n",
1233 st->container_devnm);
1234 st->update_tail = &st->updates;
1235 } else
1236 need_mdmon = 1;
1237 }
1238 rv = set_array_info(mdfd, st, &info);
1239 if (rv) {
1240 pr_err("failed to set array info for %s: %s\n", chosen_name, strerror(errno));
1241 goto abort_locked;
1242 }
1243
1244 if (s->bitmap_file) {
1245 int uuid[4];
1246
1247 st->ss->uuid_from_super(st, uuid);
1248 if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid, s->bitmap_chunk,
1249 c->delay, s->write_behind,
1250 bitmapsize,
1251 major_num)) {
1252 goto abort_locked;
1253 }
1254 bitmap_fd = open(s->bitmap_file, O_RDWR);
1255 if (bitmap_fd < 0) {
1256 pr_err("weird: %s cannot be opened\n",
1257 s->bitmap_file);
1258 goto abort_locked;
1259 }
1260 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
1261 pr_err("Cannot set bitmap file for %s: %s\n", chosen_name, strerror(errno));
1262 goto abort_locked;
1263 }
1264 }
1265
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;
1269
1270 map_unlock(&map);
1271
1272 if (is_container(s->level)) {
1273 /* No need to start. But we should signal udev to
1274 * create links */
1275 sysfs_uevent(&info, "change");
1276 if (c->verbose >= 0)
1277 pr_err("container %s prepared.\n", chosen_name);
1278 wait_for(chosen_name, mdfd);
1279 } else if (c->runstop == 1 || subdevs >= s->raiddisks) {
1280 if (st->ss->external) {
1281 int err;
1282 switch(s->level) {
1283 case LEVEL_LINEAR:
1284 case LEVEL_MULTIPATH:
1285 case 0:
1286 err = sysfs_set_str(&info, NULL, "array_state",
1287 c->readonly
1288 ? "readonly"
1289 : "active");
1290 need_mdmon = 0;
1291 break;
1292 default:
1293 err = sysfs_set_str(&info, NULL, "array_state",
1294 "readonly");
1295 break;
1296 }
1297 sysfs_set_safemode(&info, info.safe_mode_delay);
1298 if (err) {
1299 pr_err("failed to activate array.\n");
1300 ioctl(mdfd, STOP_ARRAY, NULL);
1301 goto abort;
1302 }
1303 } else if (c->readonly &&
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 }
1313 } else {
1314 /* param is not actually used */
1315 mdu_param_t param;
1316 if (ioctl(mdfd, RUN_ARRAY, &param)) {
1317 pr_err("RUN_ARRAY failed: %s\n",
1318 strerror(errno));
1319 if (errno == 524 /* ENOTSUP */ &&
1320 info.array.level == 0)
1321 cont_err("Please use --layout=original or --layout=alternate\n");
1322 if (info.array.chunk_size & (info.array.chunk_size-1)) {
1323 cont_err("Problem may be that chunk size is not a power of 2\n");
1324 }
1325 ioctl(mdfd, STOP_ARRAY, NULL);
1326 goto abort;
1327 }
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);
1333 }
1334 if (c->verbose >= 0)
1335 pr_info("array %s started.\n", chosen_name);
1336 if (st->ss->external && st->container_devnm[0]) {
1337 if (need_mdmon)
1338 start_mdmon(st->container_devnm);
1339
1340 ping_monitor(st->container_devnm);
1341 close(container_fd);
1342 }
1343 wait_for(chosen_name, mdfd);
1344 } else {
1345 pr_err("not starting array - not enough devices.\n");
1346 }
1347 udev_unblock();
1348 close(mdfd);
1349 sysfs_uevent(&info, "change");
1350 dev_policy_free(custom_pols);
1351
1352 return 0;
1353
1354 abort:
1355 udev_unblock();
1356 map_lock(&map);
1357 abort_locked:
1358 map_remove(&map, fd2devnm(mdfd));
1359 map_unlock(&map);
1360
1361 if (mdfd >= 0)
1362 close(mdfd);
1363
1364 dev_policy_free(custom_pols);
1365 return 1;
1366 }