]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
imsm: Remove --dump/--restore implementation
[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->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
546 s->consistency_policy != CONSISTENCY_POLICY_PPL) &&
547 (s->write_behind || s->size > 100*1024*1024ULL)) {
548 if (c->verbose > 0)
549 pr_err("automatically enabling write-intent bitmap on large array\n");
550 s->bitmap_file = "internal";
551 }
552 if (s->bitmap_file && strcmp(s->bitmap_file, "none") == 0)
553 s->bitmap_file = NULL;
554
555 if (s->consistency_policy == CONSISTENCY_POLICY_PPL &&
556 !st->ss->write_init_ppl) {
557 pr_err("%s metadata does not support PPL\n", st->ss->name);
558 return 1;
559 }
560
561 if (!have_container && s->level > 0 && ((maxsize-s->size)*100 > maxsize)) {
562 if (c->runstop != 1 || c->verbose >= 0)
563 pr_err("largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
564 maxdisc, s->size);
565 warn = 1;
566 }
567
568 if (st->ss->detail_platform && st->ss->detail_platform(0, 1, NULL) != 0) {
569 if (c->runstop != 1 || c->verbose >= 0)
570 pr_err("%s unable to enumerate platform support\n"
571 " array may not be compatible with hardware/firmware\n",
572 st->ss->name);
573 warn = 1;
574 }
575 st->nodes = c->nodes;
576 st->cluster_name = c->homecluster;
577
578 if (warn) {
579 if (c->runstop!= 1) {
580 if (!ask("Continue creating array? ")) {
581 pr_err("create aborted.\n");
582 return 1;
583 }
584 } else {
585 if (c->verbose > 0)
586 pr_err("creation continuing despite oddities due to --run\n");
587 }
588 }
589
590 /* If this is raid4/5, we want to configure the last active slot
591 * as missing, so that a reconstruct happens (faster than re-parity)
592 * FIX: Can we do this for raid6 as well?
593 */
594 if (st->ss->external == 0 && s->assume_clean == 0 &&
595 c->force == 0 && first_missing >= s->raiddisks) {
596 switch (s->level) {
597 case 4:
598 case 5:
599 insert_point = s->raiddisks-1;
600 s->sparedisks++;
601 info.array.active_disks--;
602 missing_disks++;
603 break;
604 default:
605 break;
606 }
607 }
608 /* For raid6, if creating with 1 missing drive, make a good drive
609 * into a spare, else the create will fail
610 */
611 if (s->assume_clean == 0 && c->force == 0 && first_missing < s->raiddisks &&
612 st->ss->external == 0 &&
613 second_missing >= s->raiddisks && s->level == 6) {
614 insert_point = s->raiddisks - 1;
615 if (insert_point == first_missing)
616 insert_point--;
617 s->sparedisks ++;
618 info.array.active_disks--;
619 missing_disks++;
620 }
621
622 if (s->level <= 0 && first_missing < subdevs * 2) {
623 pr_err("This level does not support missing devices\n");
624 return 1;
625 }
626
627 /* We need to create the device */
628 map_lock(&map);
629 mdfd = create_mddev(mddev, name, c->autof, LOCAL, chosen_name, 1);
630 if (mdfd < 0) {
631 map_unlock(&map);
632 return 1;
633 }
634 /* verify if chosen_name is not in use,
635 * it could be in conflict with already existing device
636 * e.g. container, array
637 */
638 if (strncmp(chosen_name, "/dev/md/", 8) == 0 &&
639 map_by_name(&map, chosen_name+8) != NULL) {
640 pr_err("Array name %s is in use already.\n",
641 chosen_name);
642 close(mdfd);
643 map_unlock(&map);
644 udev_unblock();
645 return 1;
646 }
647 mddev = chosen_name;
648
649 memset(&inf, 0, sizeof(inf));
650 md_get_array_info(mdfd, &inf);
651 if (inf.working_disks != 0) {
652 pr_err("another array by this name is already running.\n");
653 goto abort_locked;
654 }
655
656 /* Ok, lets try some ioctls */
657
658 info.array.level = s->level;
659 info.array.size = s->size;
660 info.array.raid_disks = s->raiddisks;
661 /* The kernel should *know* what md_minor we are dealing
662 * with, but it chooses to trust me instead. Sigh
663 */
664 info.array.md_minor = 0;
665 if (fstat_is_blkdev(mdfd, mddev, &rdev))
666 info.array.md_minor = minor(rdev);
667 info.array.not_persistent = 0;
668
669 if (((s->level == 4 || s->level == 5) &&
670 (insert_point < s->raiddisks || first_missing < s->raiddisks)) ||
671 (s->level == 6 && (insert_point < s->raiddisks ||
672 second_missing < s->raiddisks)) ||
673 (s->level <= 0) || s->assume_clean) {
674 info.array.state = 1; /* clean, but one+ drive will be missing*/
675 info.resync_start = MaxSector;
676 } else {
677 info.array.state = 0; /* not clean, but no errors */
678 info.resync_start = 0;
679 }
680 if (s->level == 10) {
681 /* for raid10, the bitmap size is the capacity of the array,
682 * which is array.size * raid_disks / ncopies;
683 * .. but convert to sectors.
684 */
685 int ncopies = ((s->layout>>8) & 255) * (s->layout & 255);
686 bitmapsize = s->size * s->raiddisks / ncopies * 2;
687 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, s->size, s->raiddisks, ncopies);*/
688 } else
689 bitmapsize = s->size * 2;
690
691 /* There is lots of redundancy in these disk counts,
692 * raid_disks is the most meaningful value
693 * it describes the geometry of the array
694 * it is constant
695 * nr_disks is total number of used slots.
696 * it should be raid_disks+spare_disks
697 * spare_disks is the number of extra disks present
698 * see above
699 * active_disks is the number of working disks in
700 * active slots. (With raid_disks)
701 * working_disks is the total number of working disks,
702 * including spares
703 * failed_disks is the number of disks marked failed
704 *
705 * Ideally, the kernel would keep these (except raid_disks)
706 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
707 * So for now, we assume that all raid and spare
708 * devices will be given.
709 */
710 info.array.spare_disks=s->sparedisks;
711 info.array.failed_disks=missing_disks;
712 info.array.nr_disks = info.array.working_disks
713 + info.array.failed_disks;
714 info.array.layout = s->layout;
715 info.array.chunk_size = s->chunk*1024;
716
717 if (name == NULL || *name == 0) {
718 /* base name on mddev */
719 /* /dev/md0 -> 0
720 * /dev/md_d0 -> d0
721 * /dev/md_foo -> foo
722 * /dev/md/1 -> 1
723 * /dev/md/d1 -> d1
724 * /dev/md/home -> home
725 * /dev/mdhome -> home
726 */
727 /* FIXME compare this with rules in create_mddev */
728 name = strrchr(mddev, '/');
729 if (name) {
730 name++;
731 if (strncmp(name, "md_", 3) == 0 &&
732 strlen(name) > 3 && (name-mddev) == 5 /* /dev/ */)
733 name += 3;
734 else if (strncmp(name, "md", 2) == 0 &&
735 strlen(name) > 2 && isdigit(name[2]) &&
736 (name-mddev) == 5 /* /dev/ */)
737 name += 2;
738 }
739 }
740 if (!st->ss->init_super(st, &info.array, s, name, c->homehost, uuid,
741 data_offset))
742 goto abort_locked;
743
744 total_slots = info.array.nr_disks;
745 st->ss->getinfo_super(st, &info, NULL);
746 if (sysfs_init(&info, mdfd, NULL)) {
747 pr_err("unable to initialize sysfs\n");
748 goto abort_locked;
749 }
750
751 if (did_default && c->verbose >= 0) {
752 if (is_subarray(info.text_version)) {
753 char devnm[32];
754 char *ep;
755 struct mdinfo *mdi;
756
757 strncpy(devnm, info.text_version+1, 32);
758 devnm[31] = 0;
759 ep = strchr(devnm, '/');
760 if (ep)
761 *ep = 0;
762
763 mdi = sysfs_read(-1, devnm, GET_VERSION);
764
765 pr_err("Creating array inside %s container %s\n",
766 mdi?mdi->text_version:"managed", devnm);
767 sysfs_free(mdi);
768 } else
769 pr_err("Defaulting to version %s metadata\n", info.text_version);
770 }
771
772 map_update(&map, fd2devnm(mdfd), info.text_version,
773 info.uuid, chosen_name);
774 /* Keep map locked until devices have been added to array
775 * to stop another mdadm from finding and using those devices.
776 */
777
778 if (s->bitmap_file && (strcmp(s->bitmap_file, "internal") == 0 ||
779 strcmp(s->bitmap_file, "clustered") == 0)) {
780 if (!st->ss->add_internal_bitmap) {
781 pr_err("internal bitmaps not supported with %s metadata\n",
782 st->ss->name);
783 goto abort_locked;
784 }
785 if (st->ss->add_internal_bitmap(st, &s->bitmap_chunk,
786 c->delay, s->write_behind,
787 bitmapsize, 1, major_num)) {
788 pr_err("Given bitmap chunk size not supported.\n");
789 goto abort_locked;
790 }
791 s->bitmap_file = NULL;
792 }
793
794 if (sysfs_init(&info, mdfd, NULL)) {
795 pr_err("unable to initialize sysfs\n");
796 goto abort_locked;
797 }
798
799 if (st->ss->external && st->container_devnm[0]) {
800 /* member */
801
802 /* When creating a member, we need to be careful
803 * to negotiate with mdmon properly.
804 * If it is already running, we cannot write to
805 * the devices and must ask it to do that part.
806 * If it isn't running, we write to the devices,
807 * and then start it.
808 * We hold an exclusive open on the container
809 * device to make sure mdmon doesn't exit after
810 * we checked that it is running.
811 *
812 * For now, fail if it is already running.
813 */
814 container_fd = open_dev_excl(st->container_devnm);
815 if (container_fd < 0) {
816 pr_err("Cannot get exclusive open on container - weird.\n");
817 goto abort_locked;
818 }
819 if (mdmon_running(st->container_devnm)) {
820 if (c->verbose)
821 pr_err("reusing mdmon for %s.\n",
822 st->container_devnm);
823 st->update_tail = &st->updates;
824 } else
825 need_mdmon = 1;
826 }
827 rv = set_array_info(mdfd, st, &info);
828 if (rv) {
829 pr_err("failed to set array info for %s: %s\n",
830 mddev, strerror(errno));
831 goto abort_locked;
832 }
833
834 if (s->bitmap_file) {
835 int uuid[4];
836
837 st->ss->uuid_from_super(st, uuid);
838 if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid, s->bitmap_chunk,
839 c->delay, s->write_behind,
840 bitmapsize,
841 major_num)) {
842 goto abort_locked;
843 }
844 bitmap_fd = open(s->bitmap_file, O_RDWR);
845 if (bitmap_fd < 0) {
846 pr_err("weird: %s cannot be opened\n",
847 s->bitmap_file);
848 goto abort_locked;
849 }
850 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
851 pr_err("Cannot set bitmap file for %s: %s\n",
852 mddev, strerror(errno));
853 goto abort_locked;
854 }
855 }
856
857 infos = xmalloc(sizeof(*infos) * total_slots);
858 enable_fds(total_slots);
859 for (pass = 1; pass <= 2; pass++) {
860 struct mddev_dev *moved_disk = NULL; /* the disk that was moved out of the insert point */
861
862 for (dnum = 0, raid_disk_num = 0, dv = devlist; dv;
863 dv = (dv->next) ? (dv->next) : moved_disk, dnum++) {
864 int fd;
865 struct mdinfo *inf = &infos[dnum];
866
867 if (dnum >= total_slots)
868 abort();
869 if (dnum == insert_point) {
870 raid_disk_num += 1;
871 moved_disk = dv;
872 continue;
873 }
874 if (strcasecmp(dv->devname, "missing") == 0) {
875 raid_disk_num += 1;
876 continue;
877 }
878 if (have_container)
879 moved_disk = NULL;
880 if (have_container && dnum < info.array.raid_disks - 1)
881 /* repeatedly use the container */
882 moved_disk = dv;
883
884 switch(pass) {
885 case 1:
886 *inf = info;
887
888 inf->disk.number = dnum;
889 inf->disk.raid_disk = raid_disk_num++;
890
891 if (dv->disposition == 'j') {
892 inf->disk.raid_disk = MD_DISK_ROLE_JOURNAL;
893 inf->disk.state = (1<<MD_DISK_JOURNAL);
894 raid_disk_num--;
895 } else if (inf->disk.raid_disk < s->raiddisks)
896 inf->disk.state = (1<<MD_DISK_ACTIVE) |
897 (1<<MD_DISK_SYNC);
898 else
899 inf->disk.state = 0;
900
901 if (dv->writemostly == FlagSet)
902 inf->disk.state |= (1<<MD_DISK_WRITEMOSTLY);
903 if (dv->failfast == FlagSet)
904 inf->disk.state |= (1<<MD_DISK_FAILFAST);
905
906 if (have_container)
907 fd = -1;
908 else {
909 if (st->ss->external &&
910 st->container_devnm[0])
911 fd = open(dv->devname, O_RDWR);
912 else
913 fd = open(dv->devname, O_RDWR|O_EXCL);
914
915 if (fd < 0) {
916 pr_err("failed to open %s after earlier success - aborting\n",
917 dv->devname);
918 goto abort_locked;
919 }
920 if (!fstat_is_blkdev(fd, dv->devname, &rdev))
921 return 1;
922 inf->disk.major = major(rdev);
923 inf->disk.minor = minor(rdev);
924 }
925 if (fd >= 0)
926 remove_partitions(fd);
927 if (st->ss->add_to_super(st, &inf->disk,
928 fd, dv->devname,
929 dv->data_offset)) {
930 ioctl(mdfd, STOP_ARRAY, NULL);
931 goto abort_locked;
932 }
933 st->ss->getinfo_super(st, inf, NULL);
934 safe_mode_delay = inf->safe_mode_delay;
935
936 if (have_container && c->verbose > 0)
937 pr_err("Using %s for device %d\n",
938 map_dev(inf->disk.major,
939 inf->disk.minor,
940 0), dnum);
941
942 if (!have_container) {
943 /* getinfo_super might have lost these ... */
944 inf->disk.major = major(rdev);
945 inf->disk.minor = minor(rdev);
946 }
947 break;
948 case 2:
949 inf->errors = 0;
950
951 rv = add_disk(mdfd, st, &info, inf);
952
953 if (rv) {
954 pr_err("ADD_NEW_DISK for %s failed: %s\n",
955 dv->devname, strerror(errno));
956 if (errno == EINVAL &&
957 info.array.level == 0) {
958 pr_err("Possibly your kernel doesn't support RAID0 layouts.\n");
959 pr_err("Either upgrade, or use --layout=dangerous\n");
960 }
961 goto abort_locked;
962 }
963 break;
964 }
965 if (!have_container &&
966 dv == moved_disk && dnum != insert_point) break;
967 }
968 if (pass == 1) {
969 struct mdinfo info_new;
970 struct map_ent *me = NULL;
971
972 /* check to see if the uuid has changed due to these
973 * metadata changes, and if so update the member array
974 * and container uuid. Note ->write_init_super clears
975 * the subarray cursor such that ->getinfo_super once
976 * again returns container info.
977 */
978 st->ss->getinfo_super(st, &info_new, NULL);
979 if (st->ss->external && s->level != LEVEL_CONTAINER &&
980 !same_uuid(info_new.uuid, info.uuid, 0)) {
981 map_update(&map, fd2devnm(mdfd),
982 info_new.text_version,
983 info_new.uuid, chosen_name);
984 me = map_by_devnm(&map, st->container_devnm);
985 }
986
987 if (st->ss->write_init_super(st)) {
988 st->ss->free_super(st);
989 goto abort_locked;
990 }
991
992 /* update parent container uuid */
993 if (me) {
994 char *path = xstrdup(me->path);
995
996 st->ss->getinfo_super(st, &info_new, NULL);
997 map_update(&map, st->container_devnm,
998 info_new.text_version,
999 info_new.uuid, path);
1000 free(path);
1001 }
1002
1003 flush_metadata_updates(st);
1004 st->ss->free_super(st);
1005 }
1006 }
1007 map_unlock(&map);
1008 free(infos);
1009
1010 if (s->level == LEVEL_CONTAINER) {
1011 /* No need to start. But we should signal udev to
1012 * create links */
1013 sysfs_uevent(&info, "change");
1014 if (c->verbose >= 0)
1015 pr_err("container %s prepared.\n", mddev);
1016 wait_for(chosen_name, mdfd);
1017 } else if (c->runstop == 1 || subdevs >= s->raiddisks) {
1018 if (st->ss->external) {
1019 int err;
1020 switch(s->level) {
1021 case LEVEL_LINEAR:
1022 case LEVEL_MULTIPATH:
1023 case 0:
1024 err = sysfs_set_str(&info, NULL, "array_state",
1025 c->readonly
1026 ? "readonly"
1027 : "active");
1028 need_mdmon = 0;
1029 break;
1030 default:
1031 err = sysfs_set_str(&info, NULL, "array_state",
1032 "readonly");
1033 break;
1034 }
1035 sysfs_set_safemode(&info, safe_mode_delay);
1036 if (err) {
1037 pr_err("failed to activate array.\n");
1038 ioctl(mdfd, STOP_ARRAY, NULL);
1039 goto abort;
1040 }
1041 } else if (c->readonly &&
1042 sysfs_attribute_available(
1043 &info, NULL, "array_state")) {
1044 if (sysfs_set_str(&info, NULL,
1045 "array_state", "readonly") < 0) {
1046 pr_err("Failed to start array: %s\n",
1047 strerror(errno));
1048 ioctl(mdfd, STOP_ARRAY, NULL);
1049 goto abort;
1050 }
1051 } else {
1052 /* param is not actually used */
1053 mdu_param_t param;
1054 if (ioctl(mdfd, RUN_ARRAY, &param)) {
1055 pr_err("RUN_ARRAY failed: %s\n",
1056 strerror(errno));
1057 if (errno == 524 /* ENOTSUP */ &&
1058 info.array.level == 0)
1059 cont_err("Please use --layout=original or --layout=alternate\n");
1060 if (info.array.chunk_size & (info.array.chunk_size-1)) {
1061 cont_err("Problem may be that chunk size is not a power of 2\n");
1062 }
1063 ioctl(mdfd, STOP_ARRAY, NULL);
1064 goto abort;
1065 }
1066 /* if start_ro module parameter is set, array is
1067 * auto-read-only, which is bad as the resync won't
1068 * start. So lets make it read-write now.
1069 */
1070 ioctl(mdfd, RESTART_ARRAY_RW, NULL);
1071 }
1072 if (c->verbose >= 0)
1073 pr_err("array %s started.\n", mddev);
1074 if (st->ss->external && st->container_devnm[0]) {
1075 if (need_mdmon)
1076 start_mdmon(st->container_devnm);
1077
1078 ping_monitor(st->container_devnm);
1079 close(container_fd);
1080 }
1081 wait_for(chosen_name, mdfd);
1082 } else {
1083 pr_err("not starting array - not enough devices.\n");
1084 }
1085 close(mdfd);
1086 /* Give udev a moment to process the Change event caused
1087 * by the close.
1088 */
1089 usleep(100*1000);
1090 udev_unblock();
1091 return 0;
1092
1093 abort:
1094 udev_unblock();
1095 map_lock(&map);
1096 abort_locked:
1097 map_remove(&map, fd2devnm(mdfd));
1098 map_unlock(&map);
1099
1100 if (mdfd >= 0)
1101 close(mdfd);
1102 return 1;
1103 }