]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Create.c
Compile fixes, particularly moving more stuff under MDASSEMBLE
[thirdparty/mdadm.git] / Create.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
4f589ad0 4 * Copyright (C) 2001-2006 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
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
9a9dab36 30#include "mdadm.h"
682c7051
NB
31#include "md_u.h"
32#include "md_p.h"
60248f74 33#include <ctype.h>
64c4757e 34
82d9eba6 35int Create(struct supertype *st, char *mddev, int mdfd,
5dd497ee 36 int chunk, int level, int layout, unsigned long long size, int raiddisks, int sparedisks,
3d3dd91e 37 char *name, char *homehost, int *uuid,
cd29a5c8 38 int subdevs, mddev_dev_t devlist,
47d79ef8 39 int runstop, int verbose, int force, int assume_clean,
dfd4d8ee 40 char *bitmap_file, int bitmap_chunk, int write_behind, int delay)
64c4757e 41{
682c7051
NB
42 /*
43 * Create a new raid array.
44 *
45 * First check that necessary details are available
46 * (i.e. level, raid-disks)
47 *
48 * Then check each disk to see what might be on it
49 * and report anything interesting.
50 *
51 * If anything looks odd, and runstop not set,
52 * abort.
53 *
54 * SET_ARRAY_INFO and ADD_NEW_DISK, and
82d9eba6 55 * if runstop==run, or raiddisks disks were used,
682c7051
NB
56 * RUN_ARRAY
57 */
b5e64645 58 unsigned long long minsize=0, maxsize=0;
cd29a5c8
NB
59 char *mindisc = NULL;
60 char *maxdisc = NULL;
c913b90e 61 int dnum;
cd29a5c8 62 mddev_dev_t dv;
682c7051 63 int fail=0, warn=0;
82b27616 64 struct stat stb;
82d9eba6 65 int first_missing = subdevs * 2;
6fb79233 66 int second_missing = subdevs * 2;
52826846 67 int missing_disks = 0;
82d9eba6 68 int insert_point = subdevs * 2; /* where to insert a missing drive */
d2ca6449 69 int total_slots;
4b1ac34b 70 int pass;
82d9eba6
NB
71 int vers;
72 int rv;
c82f047c 73 int bitmap_fd;
5f8097be 74 int have_container = 0;
0e600426 75 int container_fd = -1;
a931db9e 76 int need_mdmon = 0;
f9c25f1d 77 unsigned long long bitmapsize;
d03373f1 78 struct mdinfo *sra;
d2ca6449 79 struct mdinfo info, *infos;
b8ac1967 80 int did_default = 0;
8ed3e5e1 81 unsigned long safe_mode_delay = 0;
682c7051 82
943eafef 83 int major_num = BITMAP_MAJOR_HI;
dcec9ee5 84
06c7f68e 85 memset(&info, 0, sizeof(info));
29e766a5 86
82d9eba6
NB
87 vers = md_get_version(mdfd);
88 if (vers < 9000) {
dd0781e5 89 fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n");
52826846 90 return 1;
32e5a4ee
DL
91 } else {
92 mdu_array_info_t inf;
93 memset(&inf, 0, sizeof(inf));
94 ioctl(mdfd, GET_ARRAY_INFO, &inf);
95 if (inf.working_disks != 0) {
96 fprintf(stderr, Name ": another array by this name"
97 " is already running.\n");
98 return 1;
99 }
682c7051 100 }
a322f70c 101 if (level == UnSet) {
3cfe6247 102 /* "ddf" and "imsm" metadata only supports one level - should possibly
a322f70c
DW
103 * push this into metadata handler??
104 */
3cfe6247 105 if (st && (st->ss == &super_ddf || st->ss == &super_imsm))
a322f70c
DW
106 level = LEVEL_CONTAINER;
107 }
108
98c6faba 109 if (level == UnSet) {
682c7051
NB
110 fprintf(stderr,
111 Name ": a RAID level is needed to create an array.\n");
112 return 1;
113 }
98c6faba
NB
114 if (raiddisks < 4 && level == 6) {
115 fprintf(stderr,
116 Name ": at least 4 raid-devices needed for level 6\n");
117 return 1;
118 }
119 if (raiddisks > 256 && level == 6) {
120 fprintf(stderr,
121 Name ": no more than 256 raid-devices supported for level 6\n");
122 return 1;
123 }
52826846
NB
124 if (raiddisks < 2 && level >= 4) {
125 fprintf(stderr,
98c6faba 126 Name ": at least 2 raid-devices needed for level 4 or 5\n");
52826846
NB
127 return 1;
128 }
570510ba
NB
129 if (level <= 0 && sparedisks) {
130 fprintf(stderr,
131 Name ": This level does not support spare devices\n");
132 return 1;
133 }
5f8097be
NB
134
135 if (subdevs == 1 && strcmp(devlist->devname, "missing") != 0) {
136 /* If given a single device, it might be a container, and we can
137 * extract a device list from there
138 */
139 mdu_array_info_t inf;
140 int fd;
141
142 memset(&inf, 0, sizeof(inf));
37ea3936 143 fd = open(devlist->devname, O_RDONLY);
5f8097be
NB
144 if (fd >= 0 &&
145 ioctl(fd, GET_ARRAY_INFO, &inf) == 0 &&
146 inf.raid_disks == 0) {
147 /* yep, looks like a container */
148 if (st) {
149 rv = st->ss->load_super(st, fd,
150 devlist->devname);
151 if (rv == 0)
152 have_container = 1;
153 } else {
154 st = guess_super(fd);
155 if (st && !(rv = st->ss->
156 load_super(st, fd,
157 devlist->devname)))
158 have_container = 1;
159 else
160 st = NULL;
161 }
162 }
163 if (fd >= 0)
164 close(fd);
165 if (have_container) {
166 subdevs = 0;
167 devlist = NULL;
168 }
169 }
682c7051 170 if (subdevs > raiddisks+sparedisks) {
b83d95f3 171 fprintf(stderr, Name ": You have listed more devices (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks);
52826846 172 return 1;
682c7051 173 }
5f8097be 174 if (!have_container && subdevs < raiddisks+sparedisks) {
52826846
NB
175 fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n");
176 return 1;
177 }
5b28bd56
NB
178 if (bitmap_file && level <= 0) {
179 fprintf(stderr, Name ": bitmaps not meaningful with level %s\n",
180 map_num(pers, level)?:"given");
181 return 1;
182 }
52826846 183
682c7051 184 /* now set some defaults */
98c6faba 185 if (layout == UnSet)
682c7051
NB
186 switch(level) {
187 default: /* no layout */
188 layout = 0;
189 break;
e5329c37
NB
190 case 10:
191 layout = 0x102; /* near=2, far=1 */
dab6685f 192 if (verbose > 0)
e5329c37
NB
193 fprintf(stderr,
194 Name ": layout defaults to n1\n");
195 break;
682c7051 196 case 5:
98c6faba 197 case 6:
682c7051 198 layout = map_name(r5layout, "default");
dab6685f 199 if (verbose > 0)
682c7051
NB
200 fprintf(stderr,
201 Name ": layout defaults to %s\n", map_num(r5layout, layout));
202 break;
b5e64645
NB
203 case LEVEL_FAULTY:
204 layout = map_name(faultylayout, "default");
205
dab6685f 206 if (verbose > 0)
b5e64645
NB
207 fprintf(stderr,
208 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
209 break;
682c7051
NB
210 }
211
e5329c37
NB
212 if (level == 10)
213 /* check layout fits in array*/
214 if ((layout&255) * ((layout>>8)&255) > raiddisks) {
215 fprintf(stderr, Name ": that layout requires at least %d devices\n",
216 (layout&255) * ((layout>>8)&255));
217 return 1;
218 }
219
aa88f531
NB
220 switch(level) {
221 case 4:
222 case 5:
e5329c37 223 case 10:
98c6faba 224 case 6:
aa88f531 225 case 0:
570510ba 226 case LEVEL_LINEAR: /* linear */
aa88f531
NB
227 if (chunk == 0) {
228 chunk = 64;
dab6685f 229 if (verbose > 0)
aa88f531
NB
230 fprintf(stderr, Name ": chunk size defaults to 64K\n");
231 }
232 break;
570510ba 233 case 1:
08e43379 234 case LEVEL_FAULTY:
570510ba 235 case LEVEL_MULTIPATH:
17f25ca6 236 case LEVEL_CONTAINER:
aa88f531
NB
237 if (chunk) {
238 chunk = 0;
dab6685f 239 if (verbose > 0)
aa88f531
NB
240 fprintf(stderr, Name ": chunk size ignored for this level\n");
241 }
242 break;
570510ba
NB
243 default:
244 fprintf(stderr, Name ": unknown level %d\n", level);
245 return 1;
682c7051
NB
246 }
247
17f25ca6 248 if (st && ! st->ss->validate_geometry(st, level, layout, raiddisks,
2c514b71 249 chunk, size, NULL, NULL, verbose>=0))
17f25ca6
NB
250 return 1;
251
682c7051 252 /* now look at the subdevs */
06c7f68e
NB
253 info.array.active_disks = 0;
254 info.array.working_disks = 0;
c913b90e
NB
255 dnum = 0;
256 for (dv=devlist; dv; dv=dv->next, dnum++) {
cd29a5c8 257 char *dname = dv->devname;
17f25ca6 258 unsigned long long freesize;
cd29a5c8 259 if (strcasecmp(dname, "missing")==0) {
c913b90e
NB
260 if (first_missing > dnum)
261 first_missing = dnum;
6fb79233
NB
262 if (second_missing > dnum && dnum > first_missing)
263 second_missing = dnum;
52826846
NB
264 missing_disks ++;
265 continue;
266 }
06c7f68e 267 info.array.working_disks++;
c913b90e 268 if (dnum < raiddisks)
06c7f68e 269 info.array.active_disks++;
058574b1 270 if (st == NULL) {
8aec876d 271 struct createinfo *ci = conf_get_create_info();
058574b1
NB
272 if (ci)
273 st = ci->supertype;
274 }
576d6d83
NB
275 if (st == NULL) {
276 /* Need to choose a default metadata, which is different
17f25ca6 277 * depending on geometry of array.
576d6d83
NB
278 */
279 int i;
280 char *name = "default";
17f25ca6 281 for(i=0; !st && superlist[i]; i++) {
576d6d83 282 st = superlist[i]->match_metadata_desc(name);
17f25ca6
NB
283 if (st && !st->ss->validate_geometry
284 (st, level, layout, raiddisks,
2c514b71
NB
285 chunk, size, dname, &freesize,
286 verbose > 0))
17f25ca6
NB
287 st = NULL;
288 }
576d6d83
NB
289
290 if (!st) {
17f25ca6
NB
291 fprintf(stderr, Name ": device %s not suitable "
292 "for any style of array\n",
293 dname);
576d6d83
NB
294 exit(2);
295 }
b8ac1967 296 if (st->ss != &super0 ||
576d6d83 297 st->minor_version != 90)
b8ac1967 298 did_default = 1;
17f25ca6
NB
299 } else {
300 if (!st->ss->validate_geometry(st, level, layout,
301 raiddisks,
302 chunk, size, dname,
2c514b71
NB
303 &freesize,
304 verbose > 0)) {
17f25ca6
NB
305
306 fprintf(stderr,
307 Name ": %s is not suitable for "
308 "this array.\n",
309 dname);
310 fail = 1;
311 continue;
312 }
682c7051 313 }
82d9eba6
NB
314
315 freesize /= 2; /* convert to K */
d2cd3ffc
NB
316 if (chunk) {
317 /* round to chunk size */
318 freesize = freesize & ~(chunk-1);
319 }
682c7051
NB
320
321 if (size && freesize < size) {
52826846 322 fprintf(stderr, Name ": %s is smaller that given size."
17f25ca6
NB
323 " %lluK < %lluK + metadata\n",
324 dname, freesize, size);
52826846 325 fail = 1;
52826846 326 continue;
682c7051 327 }
cd29a5c8
NB
328 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
329 maxdisc = dname;
52826846 330 maxsize = freesize;
682c7051 331 }
cd29a5c8
NB
332 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
333 mindisc = dname;
52826846 334 minsize = freesize;
682c7051 335 }
dab6685f 336 if (runstop != 1 || verbose >= 0) {
37ea3936 337 int fd = open(dname, O_RDONLY);
b6e63da4
NB
338 if (fd <0 ) {
339 fprintf(stderr, Name ": Cannot open %s: %s\n",
340 dname, strerror(errno));
341 fail=1;
342 continue;
343 }
dab6685f
NB
344 warn |= check_ext2(fd, dname);
345 warn |= check_reiser(fd, dname);
346 warn |= check_raid(fd, dname);
b6e63da4 347 close(fd);
dab6685f 348 }
682c7051
NB
349 }
350 if (fail) {
52826846
NB
351 fprintf(stderr, Name ": create aborted\n");
352 return 1;
682c7051
NB
353 }
354 if (size == 0) {
5f8097be 355 if (mindisc == NULL && !have_container) {
52826846
NB
356 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
357 return 1;
358 }
5f8097be
NB
359 if (level > 0 || level == LEVEL_MULTIPATH
360 || level == LEVEL_FAULTY
b8ac1967 361 || st->ss->external ) {
b5e64645 362 /* size is meaningful */
b8ac1967
NB
363 if (!st->ss->validate_geometry(st, level, layout,
364 raiddisks,
365 chunk, minsize,
2c514b71 366 NULL, NULL, 0)) {
aba69144 367 fprintf(stderr, Name ": devices too large for RAID level %d\n", level);
b5e64645
NB
368 return 1;
369 }
370 size = minsize;
dab6685f 371 if (verbose > 0)
5dd497ee 372 fprintf(stderr, Name ": size set to %lluK\n", size);
b5e64645 373 }
682c7051 374 }
b5e64645 375 if (level > 0 && ((maxsize-size)*100 > maxsize)) {
dab6685f 376 if (runstop != 1 || verbose >= 0)
5dd497ee 377 fprintf(stderr, Name ": largest drive (%s) exceed size (%lluK) by more than 1%%\n",
dab6685f 378 maxdisc, size);
52826846 379 warn = 1;
682c7051
NB
380 }
381
382 if (warn) {
52826846
NB
383 if (runstop!= 1) {
384 if (!ask("Continue creating array? ")) {
385 fprintf(stderr, Name ": create aborted.\n");
386 return 1;
387 }
388 } else {
dab6685f 389 if (verbose > 0)
52826846 390 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
682c7051 391 }
682c7051
NB
392 }
393
66f8bbbe 394 /* If this is raid4/5, we want to configure the last active slot
52826846 395 * as missing, so that a reconstruct happens (faster than re-parity)
98c6faba 396 * FIX: Can we do this for raid6 as well?
52826846 397 */
47d79ef8 398 if (assume_clean==0 && force == 0 && first_missing >= raiddisks) {
98c6faba 399 switch ( level ) {
66f8bbbe 400 case 4:
98c6faba
NB
401 case 5:
402 insert_point = raiddisks-1;
403 sparedisks++;
06c7f68e 404 info.array.active_disks--;
98c6faba
NB
405 missing_disks++;
406 break;
407 default:
408 break;
409 }
52826846 410 }
6fb79233
NB
411 /* For raid6, if creating with 1 missing drive, make a good drive
412 * into a spare, else the create will fail
413 */
414 if (assume_clean == 0 && force == 0 && first_missing < raiddisks &&
415 second_missing >= raiddisks && level == 6) {
416 insert_point = raiddisks - 1;
417 if (insert_point == first_missing)
418 insert_point--;
419 sparedisks ++;
420 info.array.active_disks--;
421 missing_disks++;
422 }
570510ba 423
5f8097be 424 if (level <= 0 && first_missing < subdevs * 2) {
570510ba
NB
425 fprintf(stderr,
426 Name ": This level does not support missing devices\n");
427 return 1;
428 }
aba69144 429
682c7051
NB
430 /* Ok, lets try some ioctls */
431
06c7f68e
NB
432 info.array.level = level;
433 info.array.size = size;
434 info.array.raid_disks = raiddisks;
82b27616
NB
435 /* The kernel should *know* what md_minor we are dealing
436 * with, but it chooses to trust me instead. Sigh
437 */
06c7f68e 438 info.array.md_minor = 0;
82b27616 439 if (fstat(mdfd, &stb)==0)
06c7f68e
NB
440 info.array.md_minor = minor(stb.st_rdev);
441 info.array.not_persistent = 0;
6fb79233 442
66f8bbbe 443 if ( ( (level == 4 || level == 5) &&
b5e64645
NB
444 (insert_point < raiddisks || first_missing < raiddisks) )
445 ||
6fb79233
NB
446 ( level == 6 && (insert_point < raiddisks
447 || second_missing < raiddisks))
47d79ef8 448 ||
d024b0a7
NB
449 ( level <= 0 )
450 ||
47d79ef8 451 assume_clean
103f2410 452 ) {
06c7f68e 453 info.array.state = 1; /* clean, but one+ drive will be missing*/
103f2410
NB
454 info.resync_start = ~0ULL;
455 } else {
06c7f68e 456 info.array.state = 0; /* not clean, but no errors */
103f2410
NB
457 info.resync_start = 0;
458 }
f9c25f1d
NB
459 if (level == 10) {
460 /* for raid10, the bitmap size is the capacity of the array,
461 * which is array.size * raid_disks / ncopies;
462 * .. but convert to sectors.
463 */
702b557b 464 int ncopies = ((layout>>8) & 255) * (layout & 255);
5dd497ee
NB
465 bitmapsize = (unsigned long long)size * raiddisks / ncopies * 2;
466/* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
f9c25f1d 467 } else
5dd497ee 468 bitmapsize = (unsigned long long)size * 2;
f9c25f1d 469
82b27616
NB
470 /* There is lots of redundancy in these disk counts,
471 * raid_disks is the most meaningful value
472 * it describes the geometry of the array
473 * it is constant
474 * nr_disks is total number of used slots.
475 * it should be raid_disks+spare_disks
476 * spare_disks is the number of extra disks present
477 * see above
478 * active_disks is the number of working disks in
479 * active slots. (With raid_disks)
480 * working_disks is the total number of working disks,
481 * including spares
482 * failed_disks is the number of disks marked failed
483 *
484 * Ideally, the kernel would keep these (except raid_disks)
485 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
486 * So for now, we assume that all raid and spare
487 * devices will be given.
488 */
06c7f68e
NB
489 info.array.spare_disks=sparedisks;
490 info.array.failed_disks=missing_disks;
491 info.array.nr_disks = info.array.working_disks
492 + info.array.failed_disks;
493 info.array.layout = layout;
494 info.array.chunk_size = chunk*1024;
82d9eba6 495
b3b33eb5
NB
496 if (name == NULL || *name == 0) {
497 /* base name on mddev */
d1e80164
NB
498 /* /dev/md0 -> 0
499 * /dev/md_d0 -> d0
500 * /dev/md/1 -> 1
501 * /dev/md/d1 -> d1
502 * /dev/md/home -> home
503 * /dev/mdhome -> home
504 */
b3b33eb5
NB
505 name = strrchr(mddev, '/');
506 if (name) {
507 name++;
60248f74
NB
508 if (strncmp(name, "md_d", 4)==0 &&
509 strlen(name) > 4 &&
510 isdigit(name[4]) &&
b3b33eb5 511 (name-mddev) == 5 /* /dev/ */)
d1e80164 512 name += 3;
60248f74
NB
513 else if (strncmp(name, "md", 2)==0 &&
514 strlen(name) > 2 &&
515 isdigit(name[2]) &&
516 (name-mddev) == 5 /* /dev/ */)
b3b33eb5
NB
517 name += 2;
518 }
519 }
06c7f68e 520 if (!st->ss->init_super(st, &info.array, size, name, homehost, uuid))
82d9eba6 521 return 1;
682c7051 522
d2ca6449 523 total_slots = info.array.nr_disks;
159c3a1a
NB
524 st->ss->getinfo_super(st, &info);
525
f7f1b6a1 526 if (did_default && verbose >= 0) {
3c558363 527 if (is_subarray(info.text_version)) {
f7f1b6a1
NB
528 int dnum = devname2devnum(info.text_version+1);
529 char *path;
530 int mdp = get_mdp_major();
531 struct mdinfo *mdi;
532 if (dnum > 0)
533 path = map_dev(MD_MAJOR, dnum, 1);
534 else
535 path = map_dev(mdp, (-1-dnum)<< 6, 1);
536
537 mdi = sysfs_read(-1, dnum, GET_VERSION);
538
539 fprintf(stderr, Name ": Creating array inside "
540 "%s container %s\n",
541 mdi?mdi->text_version:"managed", path);
542 sysfs_free(mdi);
543 } else
544 fprintf(stderr, Name ": Defaulting to version"
545 " %s metadata\n", info.text_version);
546 }
b8ac1967 547
dcec9ee5 548 if (bitmap_file && vers < 9003) {
943eafef 549 major_num = BITMAP_MAJOR_HOSTENDIAN;
dcec9ee5
NB
550#ifdef __BIG_ENDIAN
551 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
552 " between different architectured. Consider upgrading the Linux kernel.\n");
553#endif
554 }
555
55935d51
NB
556 if (bitmap_file && strcmp(bitmap_file, "internal")==0) {
557 if ((vers%100) < 2) {
558 fprintf(stderr, Name ": internal bitmaps not supported by this kernel.\n");
559 return 1;
560 }
3da92f27 561 if (!st->ss->add_internal_bitmap(st, &bitmap_chunk,
199171a2 562 delay, write_behind,
943eafef 563 bitmapsize, 1, major_num)) {
55935d51
NB
564 fprintf(stderr, Name ": Given bitmap chunk size not supported.\n");
565 return 1;
566 }
567 bitmap_file = NULL;
568 }
569
570
d03373f1 571 sra = sysfs_read(mdfd, 0, 0);
55935d51 572
d03373f1
NB
573 if (st->ss->external) {
574 char ver[100];
dd15dc4a
NB
575 strcat(strcpy(ver, "external:"),
576 info.text_version);
f7e7067b 577 if (st->ss->external && st->subarray[0]) {
2f6079dc 578 /* member */
dd15dc4a 579
a931db9e
NB
580 /* When creating a member, we need to be careful
581 * to negotiate with mdmon properly.
582 * If it is already running, we cannot write to
583 * the devices and must ask it to do that part.
584 * If it isn't running, we write to the devices,
585 * and then start it.
586 * We hold an exclusive open on the container
587 * device to make sure mdmon doesn't exit after
588 * we checked that it is running.
589 *
590 * For now, fail if it is already running.
591 */
592 container_fd = open_dev_excl(st->container_dev);
593 if (container_fd < 0) {
594 fprintf(stderr, Name ": Cannot get exclusive "
595 "open on container - weird.\n");
596 return 1;
597 }
598 if (mdmon_running(st->container_dev)) {
edd8d13c
NB
599 if (verbose)
600 fprintf(stderr, Name ": reusing mdmon "
601 "for %s.\n",
602 devnum2devname(st->container_dev));
603 st->update_tail = &st->updates;
604 } else
605 need_mdmon = 1;
2f6079dc 606 }
d03373f1
NB
607 if ((vers % 100) < 2 ||
608 sra == NULL ||
609 sysfs_set_str(sra, NULL, "metadata_version",
610 ver) < 0) {
611 fprintf(stderr, Name ": This kernel does not "
612 "support external metadata.\n");
613 return 1;
614 }
2503d23b 615 rv = sysfs_set_array(sra, &info);
d03373f1 616 } else if ((vers % 100) >= 1) { /* can use different versions */
82d9eba6
NB
617 mdu_array_info_t inf;
618 memset(&inf, 0, sizeof(inf));
b8ac1967
NB
619 inf.major_version = info.array.major_version;
620 inf.minor_version = info.array.minor_version;
82d9eba6 621 rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
aba69144 622 } else
82d9eba6
NB
623 rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
624 if (rv) {
52826846
NB
625 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
626 mddev, strerror(errno));
627 return 1;
682c7051 628 }
52826846 629
c82f047c
NB
630 if (bitmap_file) {
631 int uuid[4];
55935d51 632
3da92f27 633 st->ss->uuid_from_super(st, uuid);
dfd4d8ee 634 if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk,
dcec9ee5 635 delay, write_behind,
f9c25f1d 636 bitmapsize,
943eafef 637 major_num)) {
c82f047c
NB
638 return 1;
639 }
640 bitmap_fd = open(bitmap_file, O_RDWR);
641 if (bitmap_fd < 0) {
642 fprintf(stderr, Name ": weird: %s cannot be openned\n",
dcec9ee5 643 bitmap_file);
c82f047c
NB
644 return 1;
645 }
646 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
647 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
648 mddev, strerror(errno));
649 return 1;
650 }
651 }
652
d2ca6449 653 infos = malloc(sizeof(*infos) * total_slots);
4b1ac34b 654
4b1ac34b
NB
655 for (pass=1; pass <=2 ; pass++) {
656 mddev_dev_t moved_disk = NULL; /* the disk that was moved out of the insert point */
657
658 for (dnum=0, dv = devlist ; dv ;
659 dv=(dv->next)?(dv->next):moved_disk, dnum++) {
660 int fd;
661 struct stat stb;
d2ca6449 662 struct mdinfo *inf = &infos[dnum];
4b1ac34b 663
d2ca6449
NB
664 if (dnum >= total_slots)
665 abort();
4b1ac34b
NB
666 if (dnum == insert_point) {
667 moved_disk = dv;
52826846 668 }
4b1ac34b 669 if (dnum == insert_point ||
111d01fc
NB
670 strcasecmp(dv->devname, "missing")==0)
671 continue;
672
d2ca6449
NB
673 switch(pass) {
674 case 1:
675 *inf = info;
5f8097be 676
d2ca6449
NB
677 inf->disk.number = dnum;
678 inf->disk.raid_disk = dnum;
679 if (inf->disk.raid_disk < raiddisks)
680 inf->disk.state = (1<<MD_DISK_ACTIVE) |
681 (1<<MD_DISK_SYNC);
682 else
683 inf->disk.state = 0;
684
685 if (dv->writemostly)
686 inf->disk.state |= (1<<MD_DISK_WRITEMOSTLY);
687
688 if (st->ss->external && st->subarray[0])
37ea3936 689 fd = open(dv->devname, O_RDWR);
d2ca6449 690 else
37ea3936 691 fd = open(dv->devname, O_RDWR|O_EXCL);
d2ca6449
NB
692
693 if (fd < 0) {
694 fprintf(stderr, Name ": failed to open %s "
695 "after earlier success - aborting\n",
696 dv->devname);
697 return 1;
698 }
699 fstat(fd, &stb);
700 inf->disk.major = major(stb.st_rdev);
701 inf->disk.minor = minor(stb.st_rdev);
111d01fc 702
111d01fc 703 remove_partitions(fd);
d2ca6449 704 st->ss->add_to_super(st, &inf->disk,
111d01fc 705 fd, dv->devname);
d2ca6449 706 st->ss->getinfo_super(st, inf);
8ed3e5e1 707 safe_mode_delay = inf->safe_mode_delay;
d2ca6449
NB
708
709 /* getinfo_super might have lost these ... */
710 inf->disk.major = major(stb.st_rdev);
711 inf->disk.minor = minor(stb.st_rdev);
4b1ac34b
NB
712 break;
713 case 2:
d2ca6449 714 inf->errors = 0;
2503d23b 715 rv = 0;
4b1ac34b 716
d2ca6449
NB
717 if (st->ss->external)
718 rv = sysfs_add_disk(sra, inf);
719 else
2503d23b 720 rv = ioctl(mdfd, ADD_NEW_DISK,
d2ca6449
NB
721 &inf->disk);
722
2503d23b 723 if (rv) {
d03373f1
NB
724 fprintf(stderr,
725 Name ": ADD_NEW_DISK for %s "
726 "failed: %s\n",
4b1ac34b 727 dv->devname, strerror(errno));
3da92f27 728 st->ss->free_super(st);
4b1ac34b
NB
729 return 1;
730 }
4b1ac34b
NB
731 break;
732 }
733 if (dv == moved_disk && dnum != insert_point) break;
52826846 734 }
edd8d13c 735 if (pass == 1) {
111d01fc 736 st->ss->write_init_super(st);
edd8d13c
NB
737 flush_metadata_updates(st);
738 }
5e52ae9e 739 }
d2ca6449 740 free(infos);
3da92f27 741 st->ss->free_super(st);
5e52ae9e 742
682c7051 743 /* param is not actually used */
17f25ca6
NB
744 if (level == LEVEL_CONTAINER)
745 /* No need to start */
746 ;
747 else if (runstop == 1 || subdevs >= raiddisks) {
598f0d58
NB
748 if (st->ss->external) {
749 switch(level) {
750 case LEVEL_LINEAR:
751 case LEVEL_MULTIPATH:
752 case 0:
753 sysfs_set_str(sra, NULL, "array_state",
754 "active");
a931db9e 755 need_mdmon = 0;
598f0d58
NB
756 break;
757 default:
758 sysfs_set_str(sra, NULL, "array_state",
759 "readonly");
760 break;
761 }
8ed3e5e1 762 sysfs_set_safemode(sra, safe_mode_delay);
598f0d58
NB
763 } else {
764 mdu_param_t param;
765 if (ioctl(mdfd, RUN_ARRAY, &param)) {
766 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
767 strerror(errno));
768 Manage_runstop(mddev, mdfd, -1, 0);
769 return 1;
770 }
682c7051 771 }
dab6685f
NB
772 if (verbose >= 0)
773 fprintf(stderr, Name ": array %s started.\n", mddev);
f7e7067b 774 if (st->ss->external && st->subarray[0]) {
8850ee3e
N
775 if (need_mdmon)
776 start_mdmon(st->container_dev);
777
778 ping_monitor(devnum2devname(st->container_dev));
a931db9e
NB
779 close(container_fd);
780 }
682c7051 781 } else {
b83d95f3 782 fprintf(stderr, Name ": not starting array - not enough devices.\n");
682c7051
NB
783 }
784 return 0;
64c4757e 785}