]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Create.c
fix: memory leak in Create
[thirdparty/mdadm.git] / Create.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
e736b623 4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
64c4757e
NB
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
e736b623 22 * Email: <neilb@suse.de>
64c4757e
NB
23 */
24
9a9dab36 25#include "mdadm.h"
682c7051
NB
26#include "md_u.h"
27#include "md_p.h"
60248f74 28#include <ctype.h>
64c4757e 29
a18a888e
DW
30static int default_layout(struct supertype *st, int level, int verbose)
31{
32 int layout = UnSet;
33
30f58b22
DW
34 if (st && st->ss->default_geometry)
35 st->ss->default_geometry(st, &level, &layout, NULL);
a18a888e
DW
36
37 if (layout == UnSet)
38 switch(level) {
39 default: /* no layout */
40 layout = 0;
41 break;
42 case 10:
43 layout = 0x102; /* near=2, far=1 */
44 if (verbose > 0)
45 fprintf(stderr,
ccaeea03 46 Name ": layout defaults to n2\n");
a18a888e
DW
47 break;
48 case 5:
49 case 6:
50 layout = map_name(r5layout, "default");
51 if (verbose > 0)
52 fprintf(stderr,
53 Name ": layout defaults to %s\n", map_num(r5layout, layout));
54 break;
55 case LEVEL_FAULTY:
56 layout = map_name(faultylayout, "default");
57
58 if (verbose > 0)
59 fprintf(stderr,
60 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
61 break;
62 }
63
64 return layout;
65}
66
67
7f91af49 68int Create(struct supertype *st, char *mddev,
acab7bb1
N
69 int chunk, int level, int layout, unsigned long long size,
70 int raiddisks, int sparedisks,
3d3dd91e 71 char *name, char *homehost, int *uuid,
a655e550 72 int subdevs, struct mddev_dev *devlist,
47d79ef8 73 int runstop, int verbose, int force, int assume_clean,
acab7bb1
N
74 char *bitmap_file, int bitmap_chunk, int write_behind,
75 int delay, int autof)
64c4757e 76{
682c7051
NB
77 /*
78 * Create a new raid array.
79 *
80 * First check that necessary details are available
81 * (i.e. level, raid-disks)
82 *
83 * Then check each disk to see what might be on it
84 * and report anything interesting.
85 *
86 * If anything looks odd, and runstop not set,
87 * abort.
88 *
89 * SET_ARRAY_INFO and ADD_NEW_DISK, and
82d9eba6 90 * if runstop==run, or raiddisks disks were used,
682c7051
NB
91 * RUN_ARRAY
92 */
7f91af49 93 int mdfd;
b5e64645 94 unsigned long long minsize=0, maxsize=0;
cd29a5c8
NB
95 char *mindisc = NULL;
96 char *maxdisc = NULL;
c913b90e 97 int dnum;
a655e550 98 struct mddev_dev *dv;
682c7051 99 int fail=0, warn=0;
82b27616 100 struct stat stb;
82d9eba6 101 int first_missing = subdevs * 2;
6fb79233 102 int second_missing = subdevs * 2;
52826846 103 int missing_disks = 0;
82d9eba6 104 int insert_point = subdevs * 2; /* where to insert a missing drive */
d2ca6449 105 int total_slots;
4b1ac34b 106 int pass;
82d9eba6
NB
107 int vers;
108 int rv;
c82f047c 109 int bitmap_fd;
5f8097be 110 int have_container = 0;
0e600426 111 int container_fd = -1;
a931db9e 112 int need_mdmon = 0;
f9c25f1d 113 unsigned long long bitmapsize;
d2ca6449 114 struct mdinfo info, *infos;
b8ac1967 115 int did_default = 0;
a18a888e 116 int do_default_layout = 0;
8ed3e5e1 117 unsigned long safe_mode_delay = 0;
a04d5763 118 char chosen_name[1024];
ad5bc697 119 struct map_ent *map = NULL;
8592f29d 120 unsigned long long newsize;
682c7051 121
943eafef 122 int major_num = BITMAP_MAJOR_HI;
dcec9ee5 123
06c7f68e 124 memset(&info, 0, sizeof(info));
30f58b22
DW
125 if (level == UnSet && st && st->ss->default_geometry)
126 st->ss->default_geometry(st, &level, NULL, NULL);
98c6faba 127 if (level == UnSet) {
682c7051
NB
128 fprintf(stderr,
129 Name ": a RAID level is needed to create an array.\n");
130 return 1;
131 }
98c6faba
NB
132 if (raiddisks < 4 && level == 6) {
133 fprintf(stderr,
134 Name ": at least 4 raid-devices needed for level 6\n");
135 return 1;
136 }
137 if (raiddisks > 256 && level == 6) {
138 fprintf(stderr,
139 Name ": no more than 256 raid-devices supported for level 6\n");
140 return 1;
141 }
52826846
NB
142 if (raiddisks < 2 && level >= 4) {
143 fprintf(stderr,
98c6faba 144 Name ": at least 2 raid-devices needed for level 4 or 5\n");
52826846
NB
145 return 1;
146 }
570510ba
NB
147 if (level <= 0 && sparedisks) {
148 fprintf(stderr,
149 Name ": This level does not support spare devices\n");
150 return 1;
151 }
5f8097be
NB
152
153 if (subdevs == 1 && strcmp(devlist->devname, "missing") != 0) {
154 /* If given a single device, it might be a container, and we can
155 * extract a device list from there
156 */
157 mdu_array_info_t inf;
158 int fd;
159
160 memset(&inf, 0, sizeof(inf));
37ea3936 161 fd = open(devlist->devname, O_RDONLY);
5f8097be
NB
162 if (fd >= 0 &&
163 ioctl(fd, GET_ARRAY_INFO, &inf) == 0 &&
164 inf.raid_disks == 0) {
165 /* yep, looks like a container */
166 if (st) {
0fb69d1d
N
167 rv = st->ss->load_container(st, fd,
168 devlist->devname);
5f8097be
NB
169 if (rv == 0)
170 have_container = 1;
171 } else {
0fb69d1d 172 st = super_by_fd(fd, NULL);
5f8097be 173 if (st && !(rv = st->ss->
0fb69d1d
N
174 load_container(st, fd,
175 devlist->devname)))
5f8097be
NB
176 have_container = 1;
177 else
178 st = NULL;
179 }
18fde300 180 if (have_container) {
8592f29d 181 subdevs = raiddisks;
18fde300
DW
182 first_missing = subdevs * 2;
183 second_missing = subdevs * 2;
184 insert_point = subdevs * 2;
185 }
5f8097be
NB
186 }
187 if (fd >= 0)
188 close(fd);
5f8097be 189 }
ffcfc735
N
190 if (st && st->ss->external && sparedisks) {
191 fprintf(stderr,
192 Name ": This metadata type does not support "
b42f577a 193 "spare disks at create time\n");
ffcfc735
N
194 return 1;
195 }
682c7051 196 if (subdevs > raiddisks+sparedisks) {
b83d95f3 197 fprintf(stderr, Name ": You have listed more devices (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks);
52826846 198 return 1;
682c7051 199 }
5f8097be 200 if (!have_container && subdevs < raiddisks+sparedisks) {
52826846
NB
201 fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n");
202 return 1;
203 }
5b28bd56
NB
204 if (bitmap_file && level <= 0) {
205 fprintf(stderr, Name ": bitmaps not meaningful with level %s\n",
206 map_num(pers, level)?:"given");
207 return 1;
208 }
52826846 209
682c7051 210 /* now set some defaults */
b5e64645 211
a18a888e
DW
212
213 if (layout == UnSet) {
214 do_default_layout = 1;
215 layout = default_layout(st, level, verbose);
216 }
682c7051 217
e5329c37
NB
218 if (level == 10)
219 /* check layout fits in array*/
220 if ((layout&255) * ((layout>>8)&255) > raiddisks) {
221 fprintf(stderr, Name ": that layout requires at least %d devices\n",
222 (layout&255) * ((layout>>8)&255));
223 return 1;
224 }
225
aa88f531
NB
226 switch(level) {
227 case 4:
228 case 5:
e5329c37 229 case 10:
98c6faba 230 case 6:
aa88f531 231 case 0:
aa88f531 232 if (chunk == 0) {
30f58b22
DW
233 if (st && st->ss->default_geometry)
234 st->ss->default_geometry(st, NULL, NULL, &chunk);
0bd16cf2 235 chunk = chunk ? : 512;
5f175898 236 if (verbose > 0)
0bd16cf2 237 fprintf(stderr, Name ": chunk size defaults to %dK\n", chunk);
5f175898
N
238 }
239 break;
240 case LEVEL_LINEAR:
241 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
242 if (get_linux_version() < 2006016 && chunk == 0) {
aa88f531 243 chunk = 64;
dab6685f 244 if (verbose > 0)
aa88f531
NB
245 fprintf(stderr, Name ": chunk size defaults to 64K\n");
246 }
247 break;
570510ba 248 case 1:
08e43379 249 case LEVEL_FAULTY:
570510ba 250 case LEVEL_MULTIPATH:
17f25ca6 251 case LEVEL_CONTAINER:
aa88f531
NB
252 if (chunk) {
253 chunk = 0;
dab6685f 254 if (verbose > 0)
aa88f531
NB
255 fprintf(stderr, Name ": chunk size ignored for this level\n");
256 }
257 break;
570510ba
NB
258 default:
259 fprintf(stderr, Name ": unknown level %d\n", level);
260 return 1;
682c7051 261 }
8592f29d 262
9f3bd60c
N
263 if (size && chunk)
264 size &= ~(unsigned long long)(chunk - 1);
8592f29d 265 newsize = size * 2;
17f25ca6 266 if (st && ! st->ss->validate_geometry(st, level, layout, raiddisks,
8592f29d 267 chunk, size*2, NULL, &newsize, verbose>=0))
17f25ca6 268 return 1;
8592f29d
N
269 if (size == 0) {
270 size = newsize / 2;
271 if (size && verbose > 0)
272 fprintf(stderr, Name ": setting size to %lluK\n",
273 (unsigned long long)size);
274 }
17f25ca6 275
682c7051 276 /* now look at the subdevs */
06c7f68e
NB
277 info.array.active_disks = 0;
278 info.array.working_disks = 0;
c913b90e 279 dnum = 0;
8592f29d 280 for (dv=devlist; dv && !have_container; dv=dv->next, dnum++) {
cd29a5c8 281 char *dname = dv->devname;
17f25ca6 282 unsigned long long freesize;
cd29a5c8 283 if (strcasecmp(dname, "missing")==0) {
c913b90e
NB
284 if (first_missing > dnum)
285 first_missing = dnum;
6fb79233
NB
286 if (second_missing > dnum && dnum > first_missing)
287 second_missing = dnum;
52826846
NB
288 missing_disks ++;
289 continue;
290 }
06c7f68e 291 info.array.working_disks++;
c913b90e 292 if (dnum < raiddisks)
06c7f68e 293 info.array.active_disks++;
058574b1 294 if (st == NULL) {
8aec876d 295 struct createinfo *ci = conf_get_create_info();
058574b1
NB
296 if (ci)
297 st = ci->supertype;
298 }
576d6d83
NB
299 if (st == NULL) {
300 /* Need to choose a default metadata, which is different
17f25ca6 301 * depending on geometry of array.
576d6d83
NB
302 */
303 int i;
304 char *name = "default";
17f25ca6 305 for(i=0; !st && superlist[i]; i++) {
576d6d83 306 st = superlist[i]->match_metadata_desc(name);
a18a888e
DW
307 if (do_default_layout)
308 layout = default_layout(st, level, verbose);
17f25ca6
NB
309 if (st && !st->ss->validate_geometry
310 (st, level, layout, raiddisks,
e46273eb 311 chunk, size*2, dname, &freesize,
55425f27
CA
312 verbose > 0)) {
313 free(st);
17f25ca6 314 st = NULL;
55425f27 315 }
17f25ca6 316 }
576d6d83
NB
317
318 if (!st) {
17f25ca6
NB
319 fprintf(stderr, Name ": device %s not suitable "
320 "for any style of array\n",
321 dname);
576d6d83
NB
322 exit(2);
323 }
b8ac1967 324 if (st->ss != &super0 ||
576d6d83 325 st->minor_version != 90)
b8ac1967 326 did_default = 1;
17f25ca6 327 } else {
a18a888e
DW
328 if (do_default_layout)
329 layout = default_layout(st, level, verbose);
17f25ca6
NB
330 if (!st->ss->validate_geometry(st, level, layout,
331 raiddisks,
e46273eb 332 chunk, size*2, dname,
2c514b71 333 &freesize,
b42f577a 334 verbose >= 0)) {
17f25ca6
NB
335
336 fprintf(stderr,
337 Name ": %s is not suitable for "
338 "this array.\n",
339 dname);
340 fail = 1;
341 continue;
342 }
682c7051 343 }
82d9eba6
NB
344
345 freesize /= 2; /* convert to K */
d2cd3ffc
NB
346 if (chunk) {
347 /* round to chunk size */
348 freesize = freesize & ~(chunk-1);
349 }
682c7051
NB
350
351 if (size && freesize < size) {
42dc2744 352 fprintf(stderr, Name ": %s is smaller than given size."
17f25ca6
NB
353 " %lluK < %lluK + metadata\n",
354 dname, freesize, size);
52826846 355 fail = 1;
52826846 356 continue;
682c7051 357 }
cd29a5c8
NB
358 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
359 maxdisc = dname;
52826846 360 maxsize = freesize;
682c7051 361 }
cd29a5c8
NB
362 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
363 mindisc = dname;
52826846 364 minsize = freesize;
682c7051 365 }
dab6685f 366 if (runstop != 1 || verbose >= 0) {
37ea3936 367 int fd = open(dname, O_RDONLY);
b6e63da4
NB
368 if (fd <0 ) {
369 fprintf(stderr, Name ": Cannot open %s: %s\n",
370 dname, strerror(errno));
371 fail=1;
372 continue;
373 }
dab6685f
NB
374 warn |= check_ext2(fd, dname);
375 warn |= check_reiser(fd, dname);
376 warn |= check_raid(fd, dname);
034b203a
TM
377 if (strcmp(st->ss->name, "1.x") == 0 &&
378 st->minor_version >= 1)
379 /* metadata at front */
380 warn |= check_partitions(fd, dname, 0);
381 else if (level == 1 || level == LEVEL_CONTAINER)
382 /* partitions could be meaningful */
383 warn |= check_partitions(fd, dname, freesize*2);
384 else
385 /* partitions cannot be meaningful */
386 warn |= check_partitions(fd, dname, 0);
387 if (strcmp(st->ss->name, "1.x") == 0 &&
a0962fe9
N
388 st->minor_version >= 1 &&
389 did_default &&
034b203a
TM
390 level == 1 &&
391 (warn & 1024) == 0) {
392 warn |= 1024;
a0962fe9
N
393 fprintf(stderr, Name ": Note: this array has metadata at the start and\n"
394 " may not be suitable as a boot device. If you plan to\n"
cc86f89c 395 " store '/boot' on this device please ensure that\n"
a0962fe9 396 " your boot-loader understands md/v1.x metadata, or use\n"
cc86f89c 397 " --metadata=0.90\n");
a0962fe9 398 }
b6e63da4 399 close(fd);
dab6685f 400 }
682c7051 401 }
acab7bb1
N
402 if (raiddisks + sparedisks > st->max_devs) {
403 fprintf(stderr, Name ": Too many devices:"
404 " %s metadata only supports %d\n",
405 st->ss->name, st->max_devs);
406 return 1;
407 }
8592f29d
N
408 if (have_container)
409 info.array.working_disks = raiddisks;
682c7051 410 if (fail) {
52826846
NB
411 fprintf(stderr, Name ": create aborted\n");
412 return 1;
682c7051
NB
413 }
414 if (size == 0) {
5f8097be 415 if (mindisc == NULL && !have_container) {
52826846
NB
416 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
417 return 1;
418 }
5f8097be
NB
419 if (level > 0 || level == LEVEL_MULTIPATH
420 || level == LEVEL_FAULTY
b8ac1967 421 || st->ss->external ) {
b5e64645 422 /* size is meaningful */
b8ac1967
NB
423 if (!st->ss->validate_geometry(st, level, layout,
424 raiddisks,
e46273eb 425 chunk, minsize*2,
2c514b71 426 NULL, NULL, 0)) {
aba69144 427 fprintf(stderr, Name ": devices too large for RAID level %d\n", level);
b5e64645
NB
428 return 1;
429 }
430 size = minsize;
dab6685f 431 if (verbose > 0)
5dd497ee 432 fprintf(stderr, Name ": size set to %lluK\n", size);
b5e64645 433 }
682c7051 434 }
8592f29d 435 if (!have_container && level > 0 && ((maxsize-size)*100 > maxsize)) {
dab6685f 436 if (runstop != 1 || verbose >= 0)
8592f29d 437 fprintf(stderr, Name ": largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
dab6685f 438 maxdisc, size);
52826846 439 warn = 1;
682c7051
NB
440 }
441
5615172f
DW
442 if (st->ss->detail_platform && st->ss->detail_platform(0, 1) != 0) {
443 if (runstop != 1 || verbose >= 0)
444 fprintf(stderr, Name ": %s unable to enumerate platform support\n"
445 " array may not be compatible with hardware/firmware\n",
446 st->ss->name);
447 warn = 1;
448 }
449
682c7051 450 if (warn) {
52826846
NB
451 if (runstop!= 1) {
452 if (!ask("Continue creating array? ")) {
453 fprintf(stderr, Name ": create aborted.\n");
454 return 1;
455 }
456 } else {
dab6685f 457 if (verbose > 0)
52826846 458 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
682c7051 459 }
682c7051
NB
460 }
461
66f8bbbe 462 /* If this is raid4/5, we want to configure the last active slot
52826846 463 * as missing, so that a reconstruct happens (faster than re-parity)
98c6faba 464 * FIX: Can we do this for raid6 as well?
52826846 465 */
87b47257
N
466 if (st->ss->external == 0 &&
467 assume_clean==0 && force == 0 && first_missing >= raiddisks) {
98c6faba 468 switch ( level ) {
66f8bbbe 469 case 4:
98c6faba
NB
470 case 5:
471 insert_point = raiddisks-1;
472 sparedisks++;
06c7f68e 473 info.array.active_disks--;
98c6faba
NB
474 missing_disks++;
475 break;
476 default:
477 break;
478 }
52826846 479 }
6fb79233
NB
480 /* For raid6, if creating with 1 missing drive, make a good drive
481 * into a spare, else the create will fail
482 */
483 if (assume_clean == 0 && force == 0 && first_missing < raiddisks &&
ffcfc735 484 st->ss->external == 0 &&
6fb79233
NB
485 second_missing >= raiddisks && level == 6) {
486 insert_point = raiddisks - 1;
487 if (insert_point == first_missing)
488 insert_point--;
489 sparedisks ++;
490 info.array.active_disks--;
491 missing_disks++;
492 }
570510ba 493
5f8097be 494 if (level <= 0 && first_missing < subdevs * 2) {
570510ba
NB
495 fprintf(stderr,
496 Name ": This level does not support missing devices\n");
497 return 1;
498 }
aba69144 499
7f91af49 500 /* We need to create the device */
ad5bc697 501 map_lock(&map);
a04d5763 502 mdfd = create_mddev(mddev, name, autof, LOCAL, chosen_name);
7f91af49
N
503 if (mdfd < 0)
504 return 1;
f2be09f1 505 mddev = chosen_name;
7f91af49
N
506
507 vers = md_get_version(mdfd);
508 if (vers < 9000) {
509 fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n");
510 goto abort;
511 } else {
512 mdu_array_info_t inf;
513 memset(&inf, 0, sizeof(inf));
514 ioctl(mdfd, GET_ARRAY_INFO, &inf);
515 if (inf.working_disks != 0) {
516 fprintf(stderr, Name ": another array by this name"
517 " is already running.\n");
518 goto abort;
519 }
520 }
a04d5763 521
682c7051
NB
522 /* Ok, lets try some ioctls */
523
06c7f68e
NB
524 info.array.level = level;
525 info.array.size = size;
526 info.array.raid_disks = raiddisks;
82b27616
NB
527 /* The kernel should *know* what md_minor we are dealing
528 * with, but it chooses to trust me instead. Sigh
529 */
06c7f68e 530 info.array.md_minor = 0;
82b27616 531 if (fstat(mdfd, &stb)==0)
06c7f68e
NB
532 info.array.md_minor = minor(stb.st_rdev);
533 info.array.not_persistent = 0;
6fb79233 534
66f8bbbe 535 if ( ( (level == 4 || level == 5) &&
b5e64645
NB
536 (insert_point < raiddisks || first_missing < raiddisks) )
537 ||
6fb79233
NB
538 ( level == 6 && (insert_point < raiddisks
539 || second_missing < raiddisks))
47d79ef8 540 ||
d024b0a7
NB
541 ( level <= 0 )
542 ||
47d79ef8 543 assume_clean
103f2410 544 ) {
06c7f68e 545 info.array.state = 1; /* clean, but one+ drive will be missing*/
b7528a20 546 info.resync_start = MaxSector;
103f2410 547 } else {
06c7f68e 548 info.array.state = 0; /* not clean, but no errors */
103f2410
NB
549 info.resync_start = 0;
550 }
f9c25f1d
NB
551 if (level == 10) {
552 /* for raid10, the bitmap size is the capacity of the array,
553 * which is array.size * raid_disks / ncopies;
554 * .. but convert to sectors.
555 */
702b557b 556 int ncopies = ((layout>>8) & 255) * (layout & 255);
5dd497ee
NB
557 bitmapsize = (unsigned long long)size * raiddisks / ncopies * 2;
558/* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
f9c25f1d 559 } else
5dd497ee 560 bitmapsize = (unsigned long long)size * 2;
f9c25f1d 561
82b27616
NB
562 /* There is lots of redundancy in these disk counts,
563 * raid_disks is the most meaningful value
564 * it describes the geometry of the array
565 * it is constant
566 * nr_disks is total number of used slots.
567 * it should be raid_disks+spare_disks
568 * spare_disks is the number of extra disks present
569 * see above
570 * active_disks is the number of working disks in
571 * active slots. (With raid_disks)
572 * working_disks is the total number of working disks,
573 * including spares
574 * failed_disks is the number of disks marked failed
575 *
576 * Ideally, the kernel would keep these (except raid_disks)
577 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
578 * So for now, we assume that all raid and spare
579 * devices will be given.
580 */
06c7f68e
NB
581 info.array.spare_disks=sparedisks;
582 info.array.failed_disks=missing_disks;
583 info.array.nr_disks = info.array.working_disks
584 + info.array.failed_disks;
585 info.array.layout = layout;
586 info.array.chunk_size = chunk*1024;
82d9eba6 587
b3b33eb5
NB
588 if (name == NULL || *name == 0) {
589 /* base name on mddev */
d1e80164
NB
590 /* /dev/md0 -> 0
591 * /dev/md_d0 -> d0
592 * /dev/md/1 -> 1
593 * /dev/md/d1 -> d1
594 * /dev/md/home -> home
595 * /dev/mdhome -> home
596 */
69207ff6 597 /* FIXME compare this with rules in create_mddev */
b3b33eb5
NB
598 name = strrchr(mddev, '/');
599 if (name) {
600 name++;
60248f74
NB
601 if (strncmp(name, "md_d", 4)==0 &&
602 strlen(name) > 4 &&
603 isdigit(name[4]) &&
b3b33eb5 604 (name-mddev) == 5 /* /dev/ */)
d1e80164 605 name += 3;
60248f74
NB
606 else if (strncmp(name, "md", 2)==0 &&
607 strlen(name) > 2 &&
608 isdigit(name[2]) &&
609 (name-mddev) == 5 /* /dev/ */)
b3b33eb5
NB
610 name += 2;
611 }
612 }
06c7f68e 613 if (!st->ss->init_super(st, &info.array, size, name, homehost, uuid))
7f91af49 614 goto abort;
682c7051 615
d2ca6449 616 total_slots = info.array.nr_disks;
f35f2525 617 sysfs_init(&info, mdfd, 0);
a5d85af7 618 st->ss->getinfo_super(st, &info, NULL);
159c3a1a 619
f7f1b6a1 620 if (did_default && verbose >= 0) {
3c558363 621 if (is_subarray(info.text_version)) {
f7f1b6a1
NB
622 int dnum = devname2devnum(info.text_version+1);
623 char *path;
624 int mdp = get_mdp_major();
625 struct mdinfo *mdi;
626 if (dnum > 0)
627 path = map_dev(MD_MAJOR, dnum, 1);
628 else
629 path = map_dev(mdp, (-1-dnum)<< 6, 1);
630
631 mdi = sysfs_read(-1, dnum, GET_VERSION);
632
633 fprintf(stderr, Name ": Creating array inside "
634 "%s container %s\n",
635 mdi?mdi->text_version:"managed", path);
636 sysfs_free(mdi);
637 } else
638 fprintf(stderr, Name ": Defaulting to version"
639 " %s metadata\n", info.text_version);
640 }
b8ac1967 641
ad5bc697 642 map_update(&map, fd2devnum(mdfd), info.text_version,
a04d5763 643 info.uuid, chosen_name);
ad5bc697 644 map_unlock(&map);
a04d5763 645
dcec9ee5 646 if (bitmap_file && vers < 9003) {
943eafef 647 major_num = BITMAP_MAJOR_HOSTENDIAN;
dcec9ee5
NB
648#ifdef __BIG_ENDIAN
649 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
650 " between different architectured. Consider upgrading the Linux kernel.\n");
651#endif
652 }
653
55935d51
NB
654 if (bitmap_file && strcmp(bitmap_file, "internal")==0) {
655 if ((vers%100) < 2) {
656 fprintf(stderr, Name ": internal bitmaps not supported by this kernel.\n");
7f91af49 657 goto abort;
55935d51 658 }
ebeb3663
N
659 if (!st->ss->add_internal_bitmap) {
660 fprintf(stderr, Name ": internal bitmaps not supported with %s metadata\n",
661 st->ss->name);
662 goto abort;
663 }
3da92f27 664 if (!st->ss->add_internal_bitmap(st, &bitmap_chunk,
199171a2 665 delay, write_behind,
943eafef 666 bitmapsize, 1, major_num)) {
55935d51 667 fprintf(stderr, Name ": Given bitmap chunk size not supported.\n");
7f91af49 668 goto abort;
55935d51
NB
669 }
670 bitmap_file = NULL;
671 }
672
673
f35f2525 674 sysfs_init(&info, mdfd, 0);
55935d51 675
d1d599ea 676 if (st->ss->external && st->container_dev != NoMdDev) {
f35f2525
N
677 /* member */
678
679 /* When creating a member, we need to be careful
680 * to negotiate with mdmon properly.
681 * If it is already running, we cannot write to
682 * the devices and must ask it to do that part.
683 * If it isn't running, we write to the devices,
684 * and then start it.
685 * We hold an exclusive open on the container
686 * device to make sure mdmon doesn't exit after
687 * we checked that it is running.
688 *
689 * For now, fail if it is already running.
690 */
691 container_fd = open_dev_excl(st->container_dev);
692 if (container_fd < 0) {
693 fprintf(stderr, Name ": Cannot get exclusive "
694 "open on container - weird.\n");
7f91af49 695 goto abort;
d03373f1 696 }
f35f2525
N
697 if (mdmon_running(st->container_dev)) {
698 if (verbose)
699 fprintf(stderr, Name ": reusing mdmon "
700 "for %s.\n",
701 devnum2devname(st->container_dev));
702 st->update_tail = &st->updates;
703 } else
704 need_mdmon = 1;
705 }
706 rv = set_array_info(mdfd, st, &info);
82d9eba6 707 if (rv) {
f35f2525 708 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
52826846 709 mddev, strerror(errno));
7f91af49 710 goto abort;
682c7051 711 }
52826846 712
c82f047c
NB
713 if (bitmap_file) {
714 int uuid[4];
55935d51 715
3da92f27 716 st->ss->uuid_from_super(st, uuid);
dfd4d8ee 717 if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk,
dcec9ee5 718 delay, write_behind,
f9c25f1d 719 bitmapsize,
943eafef 720 major_num)) {
7f91af49 721 goto abort;
c82f047c
NB
722 }
723 bitmap_fd = open(bitmap_file, O_RDWR);
724 if (bitmap_fd < 0) {
725 fprintf(stderr, Name ": weird: %s cannot be openned\n",
dcec9ee5 726 bitmap_file);
7f91af49 727 goto abort;
c82f047c
NB
728 }
729 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
730 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
731 mddev, strerror(errno));
7f91af49 732 goto abort;
c82f047c
NB
733 }
734 }
735
d2ca6449 736 infos = malloc(sizeof(*infos) * total_slots);
4b1ac34b 737
4b1ac34b 738 for (pass=1; pass <=2 ; pass++) {
a655e550 739 struct mddev_dev *moved_disk = NULL; /* the disk that was moved out of the insert point */
4b1ac34b
NB
740
741 for (dnum=0, dv = devlist ; dv ;
742 dv=(dv->next)?(dv->next):moved_disk, dnum++) {
743 int fd;
744 struct stat stb;
d2ca6449 745 struct mdinfo *inf = &infos[dnum];
4b1ac34b 746
d2ca6449
NB
747 if (dnum >= total_slots)
748 abort();
4b1ac34b
NB
749 if (dnum == insert_point) {
750 moved_disk = dv;
8592f29d 751 continue;
52826846 752 }
8592f29d 753 if (strcasecmp(dv->devname, "missing")==0)
111d01fc 754 continue;
8592f29d
N
755 if (have_container)
756 moved_disk = NULL;
757 if (have_container && dnum < info.array.raid_disks - 1)
758 /* repeatedly use the container */
759 moved_disk = dv;
111d01fc 760
d2ca6449
NB
761 switch(pass) {
762 case 1:
763 *inf = info;
5f8097be 764
d2ca6449
NB
765 inf->disk.number = dnum;
766 inf->disk.raid_disk = dnum;
767 if (inf->disk.raid_disk < raiddisks)
768 inf->disk.state = (1<<MD_DISK_ACTIVE) |
769 (1<<MD_DISK_SYNC);
770 else
771 inf->disk.state = 0;
772
b01b06bd 773 if (dv->writemostly == 1)
d2ca6449
NB
774 inf->disk.state |= (1<<MD_DISK_WRITEMOSTLY);
775
8592f29d
N
776 if (have_container)
777 fd = -1;
778 else {
d1d599ea
N
779 if (st->ss->external &&
780 st->container_dev != NoMdDev)
8592f29d
N
781 fd = open(dv->devname, O_RDWR);
782 else
783 fd = open(dv->devname, O_RDWR|O_EXCL);
784
785 if (fd < 0) {
786 fprintf(stderr, Name ": failed to open %s "
787 "after earlier success - aborting\n",
788 dv->devname);
789 goto abort;
790 }
791 fstat(fd, &stb);
792 inf->disk.major = major(stb.st_rdev);
793 inf->disk.minor = minor(stb.st_rdev);
d2ca6449 794 }
8592f29d
N
795 if (fd >= 0)
796 remove_partitions(fd);
f20c3968 797 if (st->ss->add_to_super(st, &inf->disk,
4eb26970
DW
798 fd, dv->devname)) {
799 ioctl(mdfd, STOP_ARRAY, NULL);
f20c3968 800 goto abort;
4eb26970 801 }
a5d85af7 802 st->ss->getinfo_super(st, inf, NULL);
8ed3e5e1 803 safe_mode_delay = inf->safe_mode_delay;
d2ca6449 804
8592f29d
N
805 if (have_container && verbose > 0)
806 fprintf(stderr, Name ": Using %s for device %d\n",
807 map_dev(inf->disk.major,
808 inf->disk.minor,
809 0), dnum);
810
811 if (!have_container) {
812 /* getinfo_super might have lost these ... */
813 inf->disk.major = major(stb.st_rdev);
814 inf->disk.minor = minor(stb.st_rdev);
815 }
4b1ac34b
NB
816 break;
817 case 2:
d2ca6449 818 inf->errors = 0;
2503d23b 819 rv = 0;
4b1ac34b 820
f35f2525 821 rv = add_disk(mdfd, st, &info, inf);
d2ca6449 822
2503d23b 823 if (rv) {
d03373f1
NB
824 fprintf(stderr,
825 Name ": ADD_NEW_DISK for %s "
826 "failed: %s\n",
4b1ac34b 827 dv->devname, strerror(errno));
7f91af49 828 goto abort;
4b1ac34b 829 }
4b1ac34b
NB
830 break;
831 }
8592f29d
N
832 if (!have_container &&
833 dv == moved_disk && dnum != insert_point) break;
52826846 834 }
edd8d13c 835 if (pass == 1) {
9b1fb677
DW
836 struct mdinfo info_new;
837 struct map_ent *me = NULL;
838
839 /* check to see if the uuid has changed due to these
840 * metadata changes, and if so update the member array
841 * and container uuid. Note ->write_init_super clears
842 * the subarray cursor such that ->getinfo_super once
843 * again returns container info.
844 */
845 map_lock(&map);
a5d85af7 846 st->ss->getinfo_super(st, &info_new, NULL);
9b1fb677
DW
847 if (st->ss->external && level != LEVEL_CONTAINER &&
848 !same_uuid(info_new.uuid, info.uuid, 0)) {
849 map_update(&map, fd2devnum(mdfd),
850 info_new.text_version,
851 info_new.uuid, chosen_name);
852 me = map_by_devnum(&map, st->container_dev);
853 }
854
111d01fc 855 st->ss->write_init_super(st);
9b1fb677
DW
856
857 /* update parent container uuid */
858 if (me) {
859 char *path = strdup(me->path);
860
a5d85af7 861 st->ss->getinfo_super(st, &info_new, NULL);
9b1fb677
DW
862 map_update(&map, st->container_dev,
863 info_new.text_version,
864 info_new.uuid, path);
865 free(path);
866 }
867 map_unlock(&map);
868
edd8d13c 869 flush_metadata_updates(st);
6946681d 870 st->ss->free_super(st);
edd8d13c 871 }
5e52ae9e 872 }
d2ca6449 873 free(infos);
5e52ae9e 874
97590376
N
875 if (level == LEVEL_CONTAINER) {
876 /* No need to start. But we should signal udev to
877 * create links */
878 sysfs_uevent(&info, "change");
ed034b11
N
879 if (verbose >= 0)
880 fprintf(stderr, Name ": container %s prepared.\n", mddev);
a7c6e3fb 881 wait_for(chosen_name, mdfd);
97590376 882 } else if (runstop == 1 || subdevs >= raiddisks) {
598f0d58 883 if (st->ss->external) {
4c821454 884 int err;
598f0d58
NB
885 switch(level) {
886 case LEVEL_LINEAR:
887 case LEVEL_MULTIPATH:
888 case 0:
4c821454
N
889 err = sysfs_set_str(&info, NULL, "array_state",
890 "active");
a931db9e 891 need_mdmon = 0;
598f0d58
NB
892 break;
893 default:
4c821454
N
894 err = sysfs_set_str(&info, NULL, "array_state",
895 "readonly");
598f0d58
NB
896 break;
897 }
f35f2525 898 sysfs_set_safemode(&info, safe_mode_delay);
4c821454
N
899 if (err) {
900 fprintf(stderr, Name ": failed to"
901 " activate array.\n");
902 ioctl(mdfd, STOP_ARRAY, NULL);
903 goto abort;
904 }
598f0d58 905 } else {
97590376 906 /* param is not actually used */
598f0d58
NB
907 mdu_param_t param;
908 if (ioctl(mdfd, RUN_ARRAY, &param)) {
909 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
910 strerror(errno));
4eb26970 911 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 912 goto abort;
598f0d58 913 }
682c7051 914 }
dab6685f
NB
915 if (verbose >= 0)
916 fprintf(stderr, Name ": array %s started.\n", mddev);
d1d599ea 917 if (st->ss->external && st->container_dev != NoMdDev) {
8850ee3e
N
918 if (need_mdmon)
919 start_mdmon(st->container_dev);
920
921 ping_monitor(devnum2devname(st->container_dev));
a931db9e
NB
922 close(container_fd);
923 }
a7c6e3fb 924 wait_for(chosen_name, mdfd);
682c7051 925 } else {
b83d95f3 926 fprintf(stderr, Name ": not starting array - not enough devices.\n");
682c7051 927 }
7f91af49 928 close(mdfd);
682c7051 929 return 0;
7f91af49
N
930
931 abort:
4eb26970
DW
932 map_lock(&map);
933 map_remove(&map, fd2devnum(mdfd));
934 map_unlock(&map);
935
7f91af49
N
936 if (mdfd >= 0)
937 close(mdfd);
938 return 1;
64c4757e 939}