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