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