]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
mdadm: Introduce pr_info()
[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
30 static int round_size_and_verify(unsigned long long *size, int chunk)
31 {
32 if (*size == 0)
33 return 0;
34 *size &= ~(unsigned long long)(chunk - 1);
35 if (*size == 0) {
36 pr_err("Size cannot be smaller than chunk.\n");
37 return 1;
38 }
39 return 0;
40 }
41
42 /**
43 * default_layout() - Get default layout for level.
44 * @st: metadata requested, could be NULL.
45 * @level: raid level requested.
46 * @verbose: verbose level.
47 *
48 * Try to ask metadata handler first, otherwise use global defaults.
49 *
50 * Return: Layout or &UnSet, return value meaning depends of level used.
51 */
52 int default_layout(struct supertype *st, int level, int verbose)
53 {
54 int layout = UnSet;
55 mapping_t *layout_map = NULL;
56 char *layout_name = NULL;
57
58 if (st && st->ss->default_geometry)
59 st->ss->default_geometry(st, &level, &layout, NULL);
60
61 if (layout != UnSet)
62 return layout;
63
64 switch (level) {
65 default: /* no layout */
66 layout = 0;
67 break;
68 case 0:
69 layout = RAID0_ORIG_LAYOUT;
70 break;
71 case 10:
72 layout = 0x102; /* near=2, far=1 */
73 layout_name = "n2";
74 break;
75 case 5:
76 case 6:
77 layout_map = r5layout;
78 break;
79 case LEVEL_FAULTY:
80 layout_map = faultylayout;
81 break;
82 }
83
84 if (layout_map) {
85 layout = map_name(layout_map, "default");
86 layout_name = map_num_s(layout_map, layout);
87 }
88 if (layout_name && verbose > 0)
89 pr_err("layout defaults to %s\n", layout_name);
90
91 return layout;
92 }
93
94 static int add_disk_to_super(int mdfd, struct shape *s, struct context *c,
95 struct supertype *st, struct mddev_dev *dv,
96 struct mdinfo *info, int have_container, int major_num)
97 {
98 dev_t rdev;
99 int fd;
100
101 if (dv->disposition == 'j') {
102 info->disk.raid_disk = MD_DISK_ROLE_JOURNAL;
103 info->disk.state = (1<<MD_DISK_JOURNAL);
104 } else if (info->disk.raid_disk < s->raiddisks) {
105 info->disk.state = (1<<MD_DISK_ACTIVE) |
106 (1<<MD_DISK_SYNC);
107 } else {
108 info->disk.state = 0;
109 }
110
111 if (dv->writemostly == FlagSet) {
112 if (major_num == BITMAP_MAJOR_CLUSTERED) {
113 pr_err("Can not set %s --write-mostly with a clustered bitmap\n",dv->devname);
114 return 1;
115 } else {
116 info->disk.state |= (1<<MD_DISK_WRITEMOSTLY);
117 }
118
119 }
120
121 if (dv->failfast == FlagSet)
122 info->disk.state |= (1<<MD_DISK_FAILFAST);
123
124 if (have_container) {
125 fd = -1;
126 } else {
127 if (st->ss->external && st->container_devnm[0])
128 fd = open(dv->devname, O_RDWR);
129 else
130 fd = open(dv->devname, O_RDWR|O_EXCL);
131
132 if (fd < 0) {
133 pr_err("failed to open %s after earlier success - aborting\n",
134 dv->devname);
135 return 1;
136 }
137 if (!fstat_is_blkdev(fd, dv->devname, &rdev))
138 return 1;
139 info->disk.major = major(rdev);
140 info->disk.minor = minor(rdev);
141 }
142 if (fd >= 0)
143 remove_partitions(fd);
144 if (st->ss->add_to_super(st, &info->disk, fd, dv->devname,
145 dv->data_offset)) {
146 ioctl(mdfd, STOP_ARRAY, NULL);
147 return 1;
148 }
149 st->ss->getinfo_super(st, info, NULL);
150
151 if (have_container && c->verbose > 0)
152 pr_err("Using %s for device %d\n",
153 map_dev(info->disk.major, info->disk.minor, 0),
154 info->disk.number);
155
156 if (!have_container) {
157 /* getinfo_super might have lost these ... */
158 info->disk.major = major(rdev);
159 info->disk.minor = minor(rdev);
160 }
161
162 return 0;
163 }
164
165 static int update_metadata(int mdfd, struct shape *s, struct supertype *st,
166 struct map_ent **map, struct mdinfo *info,
167 char *chosen_name)
168 {
169 struct mdinfo info_new;
170 struct map_ent *me = NULL;
171
172 /* check to see if the uuid has changed due to these
173 * metadata changes, and if so update the member array
174 * and container uuid. Note ->write_init_super clears
175 * the subarray cursor such that ->getinfo_super once
176 * again returns container info.
177 */
178 st->ss->getinfo_super(st, &info_new, NULL);
179 if (st->ss->external && is_container(s->level) &&
180 !same_uuid(info_new.uuid, info->uuid, 0)) {
181 map_update(map, fd2devnm(mdfd),
182 info_new.text_version,
183 info_new.uuid, chosen_name);
184 me = map_by_devnm(map, st->container_devnm);
185 }
186
187 if (st->ss->write_init_super(st)) {
188 st->ss->free_super(st);
189 return 1;
190 }
191
192 /*
193 * Before activating the array, perform extra steps
194 * required to configure the internal write-intent
195 * bitmap.
196 */
197 if (info_new.consistency_policy == CONSISTENCY_POLICY_BITMAP &&
198 st->ss->set_bitmap && st->ss->set_bitmap(st, info)) {
199 st->ss->free_super(st);
200 return 1;
201 }
202
203 /* update parent container uuid */
204 if (me) {
205 char *path = xstrdup(me->path);
206
207 st->ss->getinfo_super(st, &info_new, NULL);
208 map_update(map, st->container_devnm, info_new.text_version,
209 info_new.uuid, path);
210 free(path);
211 }
212
213 flush_metadata_updates(st);
214 st->ss->free_super(st);
215
216 return 0;
217 }
218
219 static int add_disks(int mdfd, struct mdinfo *info, struct shape *s,
220 struct context *c, struct supertype *st,
221 struct map_ent **map, struct mddev_dev *devlist,
222 int total_slots, int have_container, int insert_point,
223 int major_num, char *chosen_name)
224 {
225 struct mddev_dev *moved_disk = NULL;
226 int pass, raid_disk_num, dnum;
227 struct mddev_dev *dv;
228 struct mdinfo *infos;
229 int ret = 0;
230
231 infos = xmalloc(sizeof(*infos) * total_slots);
232 enable_fds(total_slots);
233 for (pass = 1; pass <= 2; pass++) {
234 for (dnum = 0, raid_disk_num = 0, dv = devlist; dv;
235 dv = (dv->next) ? (dv->next) : moved_disk, dnum++) {
236 if (dnum >= total_slots)
237 abort();
238 if (dnum == insert_point) {
239 raid_disk_num += 1;
240 moved_disk = dv;
241 continue;
242 }
243 if (strcasecmp(dv->devname, "missing") == 0) {
244 raid_disk_num += 1;
245 continue;
246 }
247 if (have_container)
248 moved_disk = NULL;
249 if (have_container && dnum < total_slots - 1)
250 /* repeatedly use the container */
251 moved_disk = dv;
252
253 switch(pass) {
254 case 1:
255 infos[dnum] = *info;
256 infos[dnum].disk.number = dnum;
257 infos[dnum].disk.raid_disk = raid_disk_num++;
258
259 if (dv->disposition == 'j')
260 raid_disk_num--;
261
262 ret = add_disk_to_super(mdfd, s, c, st, dv,
263 &infos[dnum], have_container,
264 major_num);
265 if (ret)
266 goto out;
267
268 break;
269 case 2:
270 infos[dnum].errors = 0;
271
272 ret = add_disk(mdfd, st, info, &infos[dnum]);
273 if (ret) {
274 pr_err("ADD_NEW_DISK for %s failed: %s\n",
275 dv->devname, strerror(errno));
276 if (errno == EINVAL &&
277 info->array.level == 0) {
278 pr_err("Possibly your kernel doesn't support RAID0 layouts.\n");
279 pr_err("Either upgrade, or use --layout=dangerous\n");
280 }
281 goto out;
282 }
283 break;
284 }
285 if (!have_container &&
286 dv == moved_disk && dnum != insert_point) break;
287 }
288
289 if (pass == 1) {
290 ret = update_metadata(mdfd, s, st, map, info,
291 chosen_name);
292 if (ret)
293 goto out;
294 }
295 }
296
297 out:
298 free(infos);
299 return ret;
300 }
301
302 int Create(struct supertype *st, char *mddev,
303 char *name, int *uuid,
304 int subdevs, struct mddev_dev *devlist,
305 struct shape *s,
306 struct context *c)
307 {
308 /*
309 * Create a new raid array.
310 *
311 * First check that necessary details are available
312 * (i.e. level, raid-disks)
313 *
314 * Then check each disk to see what might be on it
315 * and report anything interesting.
316 *
317 * If anything looks odd, and runstop not set,
318 * abort.
319 *
320 * SET_ARRAY_INFO and ADD_NEW_DISK, and
321 * if runstop==run, or raiddisks disks were used,
322 * RUN_ARRAY
323 */
324 int mdfd;
325 unsigned long long minsize = 0, maxsize = 0;
326 char *mindisc = NULL;
327 char *maxdisc = NULL;
328 int dnum;
329 struct mddev_dev *dv;
330 dev_t rdev;
331 int fail = 0, warn = 0;
332 int first_missing = subdevs * 2;
333 int second_missing = subdevs * 2;
334 int missing_disks = 0;
335 int insert_point = subdevs * 2; /* where to insert a missing drive */
336 int total_slots;
337 int rv;
338 int bitmap_fd;
339 int have_container = 0;
340 int container_fd = -1;
341 int need_mdmon = 0;
342 unsigned long long bitmapsize;
343 struct mdinfo info;
344 int did_default = 0;
345 int do_default_layout = 0;
346 int do_default_chunk = 0;
347 char chosen_name[1024];
348 struct map_ent *map = NULL;
349 unsigned long long newsize;
350 mdu_array_info_t inf;
351
352 int major_num = BITMAP_MAJOR_HI;
353 if (s->bitmap_file && strcmp(s->bitmap_file, "clustered") == 0) {
354 major_num = BITMAP_MAJOR_CLUSTERED;
355 if (c->nodes <= 1) {
356 pr_err("At least 2 nodes are needed for cluster-md\n");
357 return 1;
358 }
359 }
360
361 memset(&info, 0, sizeof(info));
362 if (s->level == UnSet && st && st->ss->default_geometry)
363 st->ss->default_geometry(st, &s->level, NULL, NULL);
364 if (s->level == UnSet) {
365 pr_err("a RAID level is needed to create an array.\n");
366 return 1;
367 }
368 if (s->raiddisks < 4 && s->level == 6) {
369 pr_err("at least 4 raid-devices needed for level 6\n");
370 return 1;
371 }
372 if (s->raiddisks > 256 && s->level == 6) {
373 pr_err("no more than 256 raid-devices supported for level 6\n");
374 return 1;
375 }
376 if (s->raiddisks < 2 && s->level >= 4) {
377 pr_err("at least 2 raid-devices needed for level %d\n", s->level);
378 return 1;
379 }
380 if (s->level <= 0 && s->sparedisks) {
381 pr_err("This level does not support spare devices\n");
382 return 1;
383 }
384
385 if (subdevs == 1 && strcmp(devlist->devname, "missing") != 0) {
386 /* If given a single device, it might be a container, and we can
387 * extract a device list from there
388 */
389 int fd;
390
391 memset(&inf, 0, sizeof(inf));
392 fd = open(devlist->devname, O_RDONLY);
393 if (fd >= 0 &&
394 md_get_array_info(fd, &inf) == 0 && inf.raid_disks == 0) {
395 /* yep, looks like a container */
396 if (st) {
397 rv = st->ss->load_container(st, fd,
398 devlist->devname);
399 if (rv == 0)
400 have_container = 1;
401 } else {
402 st = super_by_fd(fd, NULL);
403 if (st && !(rv = st->ss->
404 load_container(st, fd,
405 devlist->devname)))
406 have_container = 1;
407 else
408 st = NULL;
409 }
410 if (have_container) {
411 subdevs = s->raiddisks;
412 first_missing = subdevs * 2;
413 second_missing = subdevs * 2;
414 insert_point = subdevs * 2;
415 }
416 }
417 if (fd >= 0)
418 close(fd);
419 }
420 if (st && st->ss->external && s->sparedisks) {
421 pr_err("This metadata type does not support spare disks at create time\n");
422 return 1;
423 }
424 if (subdevs > s->raiddisks+s->sparedisks+s->journaldisks) {
425 pr_err("You have listed more devices (%d) than are in the array(%d)!\n", subdevs, s->raiddisks+s->sparedisks);
426 return 1;
427 }
428 if (!have_container && subdevs < s->raiddisks+s->sparedisks+s->journaldisks) {
429 pr_err("You haven't given enough devices (real or missing) to create this array\n");
430 return 1;
431 }
432 if (s->bitmap_file && s->level <= 0) {
433 pr_err("bitmaps not meaningful with level %s\n",
434 map_num(pers, s->level)?:"given");
435 return 1;
436 }
437
438 /* now set some defaults */
439
440 if (s->layout == UnSet) {
441 do_default_layout = 1;
442 s->layout = default_layout(st, s->level, c->verbose);
443 }
444
445 if (s->level == 10)
446 /* check layout fits in array*/
447 if ((s->layout&255) * ((s->layout>>8)&255) > s->raiddisks) {
448 pr_err("that layout requires at least %d devices\n",
449 (s->layout&255) * ((s->layout>>8)&255));
450 return 1;
451 }
452
453 switch(s->level) {
454 case 4:
455 case 5:
456 case 10:
457 case 6:
458 case 0:
459 if (s->chunk == 0 || s->chunk == UnSet) {
460 s->chunk = UnSet;
461 do_default_chunk = 1;
462 /* chunk will be set later */
463 }
464 break;
465 case LEVEL_LINEAR:
466 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
467 if (get_linux_version() < 2006016 && s->chunk == 0) {
468 s->chunk = 64;
469 if (c->verbose > 0)
470 pr_err("chunk size defaults to 64K\n");
471 }
472 break;
473 case 1:
474 case LEVEL_FAULTY:
475 case LEVEL_MULTIPATH:
476 case LEVEL_CONTAINER:
477 if (s->chunk) {
478 pr_err("specifying chunk size is forbidden for this level\n");
479 return 1;
480 }
481 break;
482 default:
483 pr_err("unknown level %d\n", s->level);
484 return 1;
485 }
486
487 if (s->size == MAX_SIZE)
488 /* use '0' to mean 'max' now... */
489 s->size = 0;
490 if (s->size && s->chunk && s->chunk != UnSet)
491 if (round_size_and_verify(&s->size, s->chunk))
492 return 1;
493
494 newsize = s->size * 2;
495 if (st && ! st->ss->validate_geometry(st, s->level, s->layout, s->raiddisks,
496 &s->chunk, s->size*2,
497 s->data_offset, NULL,
498 &newsize, s->consistency_policy,
499 c->verbose >= 0))
500 return 1;
501
502 if (s->chunk && s->chunk != UnSet) {
503 newsize &= ~(unsigned long long)(s->chunk*2 - 1);
504 if (do_default_chunk) {
505 /* default chunk was just set */
506 if (c->verbose > 0)
507 pr_err("chunk size defaults to %dK\n", s->chunk);
508 if (round_size_and_verify(&s->size, s->chunk))
509 return 1;
510 do_default_chunk = 0;
511 }
512 }
513
514 if (s->size == 0) {
515 s->size = newsize / 2;
516 if (s->level == 1)
517 /* If this is ever reshaped to RAID5, we will
518 * need a chunksize. So round it off a bit
519 * now just to be safe
520 */
521 s->size &= ~(64ULL-1);
522
523 if (s->size && c->verbose > 0)
524 pr_err("setting size to %lluK\n", s->size);
525 }
526
527 /* now look at the subdevs */
528 info.array.active_disks = 0;
529 info.array.working_disks = 0;
530 dnum = 0;
531 for (dv = devlist; dv; dv = dv->next)
532 if (s->data_offset == VARIABLE_OFFSET)
533 dv->data_offset = INVALID_SECTORS;
534 else
535 dv->data_offset = s->data_offset;
536
537 for (dv=devlist; dv && !have_container; dv=dv->next, dnum++) {
538 char *dname = dv->devname;
539 unsigned long long freesize;
540 int dfd;
541 char *doff;
542
543 if (strcasecmp(dname, "missing") == 0) {
544 if (first_missing > dnum)
545 first_missing = dnum;
546 if (second_missing > dnum && dnum > first_missing)
547 second_missing = dnum;
548 missing_disks ++;
549 continue;
550 }
551 if (s->data_offset == VARIABLE_OFFSET) {
552 doff = strchr(dname, ':');
553 if (doff) {
554 *doff++ = 0;
555 dv->data_offset = parse_size(doff);
556 } else
557 dv->data_offset = INVALID_SECTORS;
558 } else
559 dv->data_offset = s->data_offset;
560
561 dfd = open(dname, O_RDONLY);
562 if (dfd < 0) {
563 pr_err("cannot open %s: %s\n",
564 dname, strerror(errno));
565 exit(2);
566 }
567 if (!fstat_is_blkdev(dfd, dname, NULL)) {
568 close(dfd);
569 exit(2);
570 }
571 close(dfd);
572 info.array.working_disks++;
573 if (dnum < s->raiddisks && dv->disposition != 'j')
574 info.array.active_disks++;
575 if (st == NULL) {
576 struct createinfo *ci = conf_get_create_info();
577 if (ci)
578 st = ci->supertype;
579 }
580 if (st == NULL) {
581 /* Need to choose a default metadata, which is different
582 * depending on geometry of array.
583 */
584 int i;
585 char *name = "default";
586 for(i = 0; !st && superlist[i]; i++) {
587 st = superlist[i]->match_metadata_desc(name);
588 if (!st)
589 continue;
590 if (do_default_layout)
591 s->layout = default_layout(st, s->level, c->verbose);
592 switch (st->ss->validate_geometry(
593 st, s->level, s->layout, s->raiddisks,
594 &s->chunk, s->size*2,
595 dv->data_offset, dname,
596 &freesize, s->consistency_policy,
597 c->verbose > 0)) {
598 case -1: /* Not valid, message printed, and not
599 * worth checking any further */
600 exit(2);
601 break;
602 case 0: /* Geometry not valid */
603 free(st);
604 st = NULL;
605 s->chunk = do_default_chunk ? UnSet : s->chunk;
606 break;
607 case 1: /* All happy */
608 break;
609 }
610 }
611
612 if (!st) {
613 int dfd = open(dname, O_RDONLY|O_EXCL);
614 if (dfd < 0) {
615 pr_err("cannot open %s: %s\n",
616 dname, strerror(errno));
617 exit(2);
618 }
619 pr_err("device %s not suitable for any style of array\n",
620 dname);
621 exit(2);
622 }
623 if (st->ss != &super0 ||
624 st->minor_version != 90)
625 did_default = 1;
626 } else {
627 if (do_default_layout)
628 s->layout = default_layout(st, s->level, 0);
629 if (!st->ss->validate_geometry(st, s->level, s->layout,
630 s->raiddisks,
631 &s->chunk, s->size*2,
632 dv->data_offset,
633 dname, &freesize,
634 s->consistency_policy,
635 c->verbose >= 0)) {
636
637 pr_err("%s is not suitable for this array.\n",
638 dname);
639 fail = 1;
640 continue;
641 }
642 }
643
644 if (dv->disposition == 'j')
645 goto skip_size_check; /* skip write journal for size check */
646
647 freesize /= 2; /* convert to K */
648 if (s->chunk && s->chunk != UnSet) {
649 /* round to chunk size */
650 freesize = freesize & ~(s->chunk-1);
651 if (do_default_chunk) {
652 /* default chunk was just set */
653 if (c->verbose > 0)
654 pr_err("chunk size defaults to %dK\n", s->chunk);
655 if (round_size_and_verify(&s->size, s->chunk))
656 return 1;
657 do_default_chunk = 0;
658 }
659 }
660 if (!freesize) {
661 pr_err("no free space left on %s\n", dname);
662 fail = 1;
663 continue;
664 }
665
666 if (s->size && freesize < s->size) {
667 pr_err("%s is smaller than given size. %lluK < %lluK + metadata\n",
668 dname, freesize, s->size);
669 fail = 1;
670 continue;
671 }
672 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
673 maxdisc = dname;
674 maxsize = freesize;
675 }
676 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
677 mindisc = dname;
678 minsize = freesize;
679 }
680 skip_size_check:
681 if (c->runstop != 1 || c->verbose >= 0) {
682 int fd = open(dname, O_RDONLY);
683 if (fd < 0) {
684 pr_err("Cannot open %s: %s\n",
685 dname, strerror(errno));
686 fail = 1;
687 continue;
688 }
689 warn |= check_ext2(fd, dname);
690 warn |= check_reiser(fd, dname);
691 warn |= check_raid(fd, dname);
692 if (strcmp(st->ss->name, "1.x") == 0 &&
693 st->minor_version >= 1)
694 /* metadata at front */
695 warn |= check_partitions(fd, dname, 0, 0);
696 else if (s->level == 1 || is_container(s->level) ||
697 (s->level == 0 && s->raiddisks == 1))
698 /* partitions could be meaningful */
699 warn |= check_partitions(fd, dname, freesize*2, s->size*2);
700 else
701 /* partitions cannot be meaningful */
702 warn |= check_partitions(fd, dname, 0, 0);
703 if (strcmp(st->ss->name, "1.x") == 0 &&
704 st->minor_version >= 1 &&
705 did_default &&
706 s->level == 1 &&
707 (warn & 1024) == 0) {
708 warn |= 1024;
709 pr_err("Note: this array has metadata at the start and\n"
710 " may not be suitable as a boot device. If you plan to\n"
711 " store '/boot' on this device please ensure that\n"
712 " your boot-loader understands md/v1.x metadata, or use\n"
713 " --metadata=0.90\n");
714 }
715 close(fd);
716 }
717 }
718 if (missing_disks == dnum && !have_container) {
719 pr_err("Subdevs can't be all missing\n");
720 return 1;
721 }
722 if (s->raiddisks + s->sparedisks > st->max_devs) {
723 pr_err("Too many devices: %s metadata only supports %d\n",
724 st->ss->name, st->max_devs);
725 return 1;
726 }
727 if (have_container)
728 info.array.working_disks = s->raiddisks;
729 if (fail) {
730 pr_err("create aborted\n");
731 return 1;
732 }
733 if (s->size == 0) {
734 if (mindisc == NULL && !have_container) {
735 pr_err("no size and no drives given - aborting create.\n");
736 return 1;
737 }
738 if (s->level > 0 || s->level == LEVEL_MULTIPATH ||
739 s->level == LEVEL_FAULTY || st->ss->external) {
740 /* size is meaningful */
741 if (!st->ss->validate_geometry(st, s->level, s->layout,
742 s->raiddisks,
743 &s->chunk, minsize*2,
744 s->data_offset,
745 NULL, NULL,
746 s->consistency_policy, 0)) {
747 pr_err("devices too large for RAID level %d\n", s->level);
748 return 1;
749 }
750 s->size = minsize;
751 if (s->level == 1)
752 /* If this is ever reshaped to RAID5, we will
753 * need a chunksize. So round it off a bit
754 * now just to be safe
755 */
756 s->size &= ~(64ULL-1);
757 if (c->verbose > 0)
758 pr_err("size set to %lluK\n", s->size);
759 }
760 }
761
762 if (!s->bitmap_file &&
763 !st->ss->external &&
764 s->level >= 1 &&
765 st->ss->add_internal_bitmap &&
766 s->journaldisks == 0 &&
767 (s->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
768 s->consistency_policy != CONSISTENCY_POLICY_PPL) &&
769 (s->write_behind || s->size > 100*1024*1024ULL)) {
770 if (c->verbose > 0)
771 pr_err("automatically enabling write-intent bitmap on large array\n");
772 s->bitmap_file = "internal";
773 }
774 if (s->bitmap_file && strcmp(s->bitmap_file, "none") == 0)
775 s->bitmap_file = NULL;
776
777 if (s->consistency_policy == CONSISTENCY_POLICY_PPL &&
778 !st->ss->write_init_ppl) {
779 pr_err("%s metadata does not support PPL\n", st->ss->name);
780 return 1;
781 }
782
783 if (!have_container && s->level > 0 && ((maxsize-s->size)*100 > maxsize)) {
784 if (c->runstop != 1 || c->verbose >= 0)
785 pr_err("largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
786 maxdisc, s->size);
787 warn = 1;
788 }
789
790 if (st->ss->detail_platform && st->ss->detail_platform(0, 1, NULL) != 0) {
791 if (c->runstop != 1 || c->verbose >= 0)
792 pr_err("%s unable to enumerate platform support\n"
793 " array may not be compatible with hardware/firmware\n",
794 st->ss->name);
795 warn = 1;
796 }
797 st->nodes = c->nodes;
798 st->cluster_name = c->homecluster;
799
800 if (warn) {
801 if (c->runstop!= 1) {
802 if (!ask("Continue creating array? ")) {
803 pr_err("create aborted.\n");
804 return 1;
805 }
806 } else {
807 if (c->verbose > 0)
808 pr_err("creation continuing despite oddities due to --run\n");
809 }
810 }
811
812 /* If this is raid4/5, we want to configure the last active slot
813 * as missing, so that a reconstruct happens (faster than re-parity)
814 * FIX: Can we do this for raid6 as well?
815 */
816 if (st->ss->external == 0 && s->assume_clean == 0 &&
817 c->force == 0 && first_missing >= s->raiddisks) {
818 switch (s->level) {
819 case 4:
820 case 5:
821 insert_point = s->raiddisks-1;
822 s->sparedisks++;
823 info.array.active_disks--;
824 missing_disks++;
825 break;
826 default:
827 break;
828 }
829 }
830 /* For raid6, if creating with 1 missing drive, make a good drive
831 * into a spare, else the create will fail
832 */
833 if (s->assume_clean == 0 && c->force == 0 && first_missing < s->raiddisks &&
834 st->ss->external == 0 &&
835 second_missing >= s->raiddisks && s->level == 6) {
836 insert_point = s->raiddisks - 1;
837 if (insert_point == first_missing)
838 insert_point--;
839 s->sparedisks ++;
840 info.array.active_disks--;
841 missing_disks++;
842 }
843
844 if (s->level <= 0 && first_missing < subdevs * 2) {
845 pr_err("This level does not support missing devices\n");
846 return 1;
847 }
848
849 /* We need to create the device */
850 map_lock(&map);
851 mdfd = create_mddev(mddev, name, c->autof, LOCAL, chosen_name, 1);
852 if (mdfd < 0) {
853 map_unlock(&map);
854 return 1;
855 }
856 /* verify if chosen_name is not in use,
857 * it could be in conflict with already existing device
858 * e.g. container, array
859 */
860 if (strncmp(chosen_name, "/dev/md/", 8) == 0 &&
861 map_by_name(&map, chosen_name+8) != NULL) {
862 pr_err("Array name %s is in use already.\n",
863 chosen_name);
864 close(mdfd);
865 map_unlock(&map);
866 udev_unblock();
867 return 1;
868 }
869 mddev = chosen_name;
870
871 memset(&inf, 0, sizeof(inf));
872 md_get_array_info(mdfd, &inf);
873 if (inf.working_disks != 0) {
874 pr_err("another array by this name is already running.\n");
875 goto abort_locked;
876 }
877
878 /* Ok, lets try some ioctls */
879
880 info.array.level = s->level;
881 info.array.size = s->size;
882 info.array.raid_disks = s->raiddisks;
883 /* The kernel should *know* what md_minor we are dealing
884 * with, but it chooses to trust me instead. Sigh
885 */
886 info.array.md_minor = 0;
887 if (fstat_is_blkdev(mdfd, mddev, &rdev))
888 info.array.md_minor = minor(rdev);
889 info.array.not_persistent = 0;
890
891 if (((s->level == 4 || s->level == 5) &&
892 (insert_point < s->raiddisks || first_missing < s->raiddisks)) ||
893 (s->level == 6 && (insert_point < s->raiddisks ||
894 second_missing < s->raiddisks)) ||
895 (s->level <= 0) || s->assume_clean) {
896 info.array.state = 1; /* clean, but one+ drive will be missing*/
897 info.resync_start = MaxSector;
898 } else {
899 info.array.state = 0; /* not clean, but no errors */
900 info.resync_start = 0;
901 }
902 if (s->level == 10) {
903 /* for raid10, the bitmap size is the capacity of the array,
904 * which is array.size * raid_disks / ncopies;
905 * .. but convert to sectors.
906 */
907 int ncopies = ((s->layout>>8) & 255) * (s->layout & 255);
908 bitmapsize = s->size * s->raiddisks / ncopies * 2;
909 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, s->size, s->raiddisks, ncopies);*/
910 } else
911 bitmapsize = s->size * 2;
912
913 /* There is lots of redundancy in these disk counts,
914 * raid_disks is the most meaningful value
915 * it describes the geometry of the array
916 * it is constant
917 * nr_disks is total number of used slots.
918 * it should be raid_disks+spare_disks
919 * spare_disks is the number of extra disks present
920 * see above
921 * active_disks is the number of working disks in
922 * active slots. (With raid_disks)
923 * working_disks is the total number of working disks,
924 * including spares
925 * failed_disks is the number of disks marked failed
926 *
927 * Ideally, the kernel would keep these (except raid_disks)
928 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
929 * So for now, we assume that all raid and spare
930 * devices will be given.
931 */
932 info.array.spare_disks=s->sparedisks;
933 info.array.failed_disks=missing_disks;
934 info.array.nr_disks = info.array.working_disks
935 + info.array.failed_disks;
936 info.array.layout = s->layout;
937 info.array.chunk_size = s->chunk*1024;
938
939 if (name == NULL || *name == 0) {
940 /* base name on mddev */
941 /* /dev/md0 -> 0
942 * /dev/md_d0 -> d0
943 * /dev/md_foo -> foo
944 * /dev/md/1 -> 1
945 * /dev/md/d1 -> d1
946 * /dev/md/home -> home
947 * /dev/mdhome -> home
948 */
949 /* FIXME compare this with rules in create_mddev */
950 name = strrchr(mddev, '/');
951 if (name) {
952 name++;
953 if (strncmp(name, "md_", 3) == 0 &&
954 strlen(name) > 3 && (name-mddev) == 5 /* /dev/ */)
955 name += 3;
956 else if (strncmp(name, "md", 2) == 0 &&
957 strlen(name) > 2 && isdigit(name[2]) &&
958 (name-mddev) == 5 /* /dev/ */)
959 name += 2;
960 }
961 }
962 if (!st->ss->init_super(st, &info.array, s, name, c->homehost, uuid,
963 s->data_offset))
964 goto abort_locked;
965
966 total_slots = info.array.nr_disks;
967 st->ss->getinfo_super(st, &info, NULL);
968 if (sysfs_init(&info, mdfd, NULL)) {
969 pr_err("unable to initialize sysfs\n");
970 goto abort_locked;
971 }
972
973 if (did_default && c->verbose >= 0) {
974 if (is_subarray(info.text_version)) {
975 char devnm[32];
976 char *ep;
977 struct mdinfo *mdi;
978
979 strncpy(devnm, info.text_version+1, 32);
980 devnm[31] = 0;
981 ep = strchr(devnm, '/');
982 if (ep)
983 *ep = 0;
984
985 mdi = sysfs_read(-1, devnm, GET_VERSION);
986
987 pr_info("Creating array inside %s container %s\n",
988 mdi?mdi->text_version:"managed", devnm);
989 sysfs_free(mdi);
990 } else
991 pr_info("Defaulting to version %s metadata\n",
992 info.text_version);
993 }
994
995 map_update(&map, fd2devnm(mdfd), info.text_version,
996 info.uuid, chosen_name);
997 /* Keep map locked until devices have been added to array
998 * to stop another mdadm from finding and using those devices.
999 */
1000
1001 if (s->bitmap_file && (strcmp(s->bitmap_file, "internal") == 0 ||
1002 strcmp(s->bitmap_file, "clustered") == 0)) {
1003 if (!st->ss->add_internal_bitmap) {
1004 pr_err("internal bitmaps not supported with %s metadata\n",
1005 st->ss->name);
1006 goto abort_locked;
1007 }
1008 if (st->ss->add_internal_bitmap(st, &s->bitmap_chunk,
1009 c->delay, s->write_behind,
1010 bitmapsize, 1, major_num)) {
1011 pr_err("Given bitmap chunk size not supported.\n");
1012 goto abort_locked;
1013 }
1014 s->bitmap_file = NULL;
1015 }
1016
1017 if (sysfs_init(&info, mdfd, NULL)) {
1018 pr_err("unable to initialize sysfs\n");
1019 goto abort_locked;
1020 }
1021
1022 if (st->ss->external && st->container_devnm[0]) {
1023 /* member */
1024
1025 /* When creating a member, we need to be careful
1026 * to negotiate with mdmon properly.
1027 * If it is already running, we cannot write to
1028 * the devices and must ask it to do that part.
1029 * If it isn't running, we write to the devices,
1030 * and then start it.
1031 * We hold an exclusive open on the container
1032 * device to make sure mdmon doesn't exit after
1033 * we checked that it is running.
1034 *
1035 * For now, fail if it is already running.
1036 */
1037 container_fd = open_dev_excl(st->container_devnm);
1038 if (container_fd < 0) {
1039 pr_err("Cannot get exclusive open on container - weird.\n");
1040 goto abort_locked;
1041 }
1042 if (mdmon_running(st->container_devnm)) {
1043 if (c->verbose)
1044 pr_err("reusing mdmon for %s.\n",
1045 st->container_devnm);
1046 st->update_tail = &st->updates;
1047 } else
1048 need_mdmon = 1;
1049 }
1050 rv = set_array_info(mdfd, st, &info);
1051 if (rv) {
1052 pr_err("failed to set array info for %s: %s\n",
1053 mddev, strerror(errno));
1054 goto abort_locked;
1055 }
1056
1057 if (s->bitmap_file) {
1058 int uuid[4];
1059
1060 st->ss->uuid_from_super(st, uuid);
1061 if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid, s->bitmap_chunk,
1062 c->delay, s->write_behind,
1063 bitmapsize,
1064 major_num)) {
1065 goto abort_locked;
1066 }
1067 bitmap_fd = open(s->bitmap_file, O_RDWR);
1068 if (bitmap_fd < 0) {
1069 pr_err("weird: %s cannot be opened\n",
1070 s->bitmap_file);
1071 goto abort_locked;
1072 }
1073 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
1074 pr_err("Cannot set bitmap file for %s: %s\n",
1075 mddev, strerror(errno));
1076 goto abort_locked;
1077 }
1078 }
1079
1080 if (add_disks(mdfd, &info, s, c, st, &map, devlist, total_slots,
1081 have_container, insert_point, major_num, chosen_name))
1082 goto abort_locked;
1083
1084 map_unlock(&map);
1085
1086 if (is_container(s->level)) {
1087 /* No need to start. But we should signal udev to
1088 * create links */
1089 sysfs_uevent(&info, "change");
1090 if (c->verbose >= 0)
1091 pr_err("container %s prepared.\n", mddev);
1092 wait_for(chosen_name, mdfd);
1093 } else if (c->runstop == 1 || subdevs >= s->raiddisks) {
1094 if (st->ss->external) {
1095 int err;
1096 switch(s->level) {
1097 case LEVEL_LINEAR:
1098 case LEVEL_MULTIPATH:
1099 case 0:
1100 err = sysfs_set_str(&info, NULL, "array_state",
1101 c->readonly
1102 ? "readonly"
1103 : "active");
1104 need_mdmon = 0;
1105 break;
1106 default:
1107 err = sysfs_set_str(&info, NULL, "array_state",
1108 "readonly");
1109 break;
1110 }
1111 sysfs_set_safemode(&info, info.safe_mode_delay);
1112 if (err) {
1113 pr_err("failed to activate array.\n");
1114 ioctl(mdfd, STOP_ARRAY, NULL);
1115 goto abort;
1116 }
1117 } else if (c->readonly &&
1118 sysfs_attribute_available(
1119 &info, NULL, "array_state")) {
1120 if (sysfs_set_str(&info, NULL,
1121 "array_state", "readonly") < 0) {
1122 pr_err("Failed to start array: %s\n",
1123 strerror(errno));
1124 ioctl(mdfd, STOP_ARRAY, NULL);
1125 goto abort;
1126 }
1127 } else {
1128 /* param is not actually used */
1129 mdu_param_t param;
1130 if (ioctl(mdfd, RUN_ARRAY, &param)) {
1131 pr_err("RUN_ARRAY failed: %s\n",
1132 strerror(errno));
1133 if (errno == 524 /* ENOTSUP */ &&
1134 info.array.level == 0)
1135 cont_err("Please use --layout=original or --layout=alternate\n");
1136 if (info.array.chunk_size & (info.array.chunk_size-1)) {
1137 cont_err("Problem may be that chunk size is not a power of 2\n");
1138 }
1139 ioctl(mdfd, STOP_ARRAY, NULL);
1140 goto abort;
1141 }
1142 /* if start_ro module parameter is set, array is
1143 * auto-read-only, which is bad as the resync won't
1144 * start. So lets make it read-write now.
1145 */
1146 ioctl(mdfd, RESTART_ARRAY_RW, NULL);
1147 }
1148 if (c->verbose >= 0)
1149 pr_info("array %s started.\n", mddev);
1150 if (st->ss->external && st->container_devnm[0]) {
1151 if (need_mdmon)
1152 start_mdmon(st->container_devnm);
1153
1154 ping_monitor(st->container_devnm);
1155 close(container_fd);
1156 }
1157 wait_for(chosen_name, mdfd);
1158 } else {
1159 pr_err("not starting array - not enough devices.\n");
1160 }
1161 udev_unblock();
1162 close(mdfd);
1163 sysfs_uevent(&info, "change");
1164 return 0;
1165
1166 abort:
1167 udev_unblock();
1168 map_lock(&map);
1169 abort_locked:
1170 map_remove(&map, fd2devnm(mdfd));
1171 map_unlock(&map);
1172
1173 if (mdfd >= 0)
1174 close(mdfd);
1175 return 1;
1176 }