summaryrefslogtreecommitdiffstats
path: root/Create.c
blob: d94253b1ad2e8881550276d2a43606c3973893b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
/*
 * mdadm - manage Linux "md" devices aka RAID arrays.
 *
 * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
 *
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    Author: Neil Brown
 *    Email: <neilb@suse.de>
 */

#include	"mdadm.h"
#include	"udev.h"
#include	"md_u.h"
#include	"md_p.h"
#include	<ctype.h>
#include	<fcntl.h>
#include	<signal.h>
#include	<sys/signalfd.h>
#include	<sys/wait.h>

#ifndef FALLOC_FL_ZERO_RANGE
#define FALLOC_FL_ZERO_RANGE 16
#endif

static int round_size_and_verify(unsigned long long *size, int chunk)
{
	if (*size == 0)
		return 0;
	*size &= ~(unsigned long long)(chunk - 1);
	if (*size == 0) {
		pr_err("Size cannot be smaller than chunk.\n");
		return 1;
	}
	return 0;
}

/**
 * default_layout() - Get default layout for level.
 * @st: metadata requested, could be NULL.
 * @level: raid level requested.
 * @verbose: verbose level.
 *
 * Try to ask metadata handler first, otherwise use global defaults.
 *
 * Return: Layout or &UnSet, return value meaning depends of level used.
 */
int default_layout(struct supertype *st, int level, int verbose)
{
	int layout = UnSet;
	mapping_t *layout_map = NULL;
	char *layout_name = NULL;

	if (st && st->ss->default_geometry)
		st->ss->default_geometry(st, &level, &layout, NULL);

	if (layout != UnSet)
		return layout;

	switch (level) {
	default: /* no layout */
		layout = 0;
		break;
	case 0:
		layout = RAID0_ORIG_LAYOUT;
		break;
	case 10:
		layout = 0x102; /* near=2, far=1 */
		layout_name = "n2";
		break;
	case 5:
	case 6:
		layout_map = r5layout;
		break;
	case LEVEL_FAULTY:
		layout_map = faultylayout;
		break;
	}

	if (layout_map) {
		layout = map_name(layout_map, "default");
		layout_name = map_num_s(layout_map, layout);
	}
	if (layout_name && verbose > 0)
		pr_err("layout defaults to %s\n", layout_name);

	return layout;
}

static pid_t write_zeroes_fork(int fd, struct shape *s, struct supertype *st,
			       struct mddev_dev *dv)

{
	const unsigned long long req_size = 1 << 30;
	unsigned long long offset_bytes, size_bytes, sz;
	sigset_t sigset;
	int ret = 0;
	pid_t pid;

	size_bytes = KIB_TO_BYTES(s->size);

	/*
	 * If size_bytes is zero, this is a zoned raid array where
	 * each disk is of a different size and uses its full
	 * disk. Thus zero the entire disk.
	 */
	if (!size_bytes && !get_dev_size(fd, dv->devname, &size_bytes))
		return -1;

	if (dv->data_offset != INVALID_SECTORS)
		offset_bytes = SEC_TO_BYTES(dv->data_offset);
	else
		offset_bytes = SEC_TO_BYTES(st->data_offset);

	pr_info("zeroing data from %lld to %lld on: %s\n",
		offset_bytes, size_bytes, dv->devname);

	pid = fork();
	if (pid < 0) {
		pr_err("Could not fork to zero disks: %s\n", strerror(errno));
		return pid;
	} else if (pid != 0) {
		return pid;
	}

	sigemptyset(&sigset);
	sigaddset(&sigset, SIGINT);
	sigprocmask(SIG_UNBLOCK, &sigset, NULL);

	while (size_bytes) {
		/*
		 * Split requests to the kernel into 1GB chunks seeing the
		 * fallocate() call is not interruptible and blocking a
		 * ctrl-c for several minutes is not desirable.
		 *
		 * 1GB is chosen as a compromise: the user may still have
		 * to wait several seconds if they ctrl-c on devices that
		 * zero slowly, but will reduce the number of requests
		 * required and thus the overhead on devices that perform
		 * better.
		 */
		sz = size_bytes;
		if (sz >= req_size)
			sz = req_size;

		if (fallocate(fd, FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE,
			      offset_bytes, sz)) {
			pr_err("zeroing %s failed: %s\n", dv->devname,
			       strerror(errno));
			ret = 1;
			break;
		}

		offset_bytes += sz;
		size_bytes -= sz;
	}

	exit(ret);
}

static int wait_for_zero_forks(int *zero_pids, int count)
{
	int wstatus, ret = 0, i, sfd, wait_count = 0;
	struct signalfd_siginfo fdsi;
	bool interrupted = false;
	sigset_t sigset;
	ssize_t s;

	for (i = 0; i < count; i++)
		if (zero_pids[i])
			wait_count++;
	if (!wait_count)
		return 0;

	sigemptyset(&sigset);
	sigaddset(&sigset, SIGINT);
	sigaddset(&sigset, SIGCHLD);
	sigprocmask(SIG_BLOCK, &sigset, NULL);

	sfd = signalfd(-1, &sigset, 0);
	if (sfd < 0) {
		pr_err("Unable to create signalfd: %s\n", strerror(errno));
		return 1;
	}

	while (1) {
		s = read(sfd, &fdsi, sizeof(fdsi));
		if (s != sizeof(fdsi)) {
			pr_err("Invalid signalfd read: %s\n", strerror(errno));
			close(sfd);
			return 1;
		}

		if (fdsi.ssi_signo == SIGINT) {
			printf("\n");
			pr_info("Interrupting zeroing processes, please wait...\n");
			interrupted = true;
		} else if (fdsi.ssi_signo == SIGCHLD) {
			if (!--wait_count)
				break;
		}
	}

	close(sfd);

	for (i = 0; i < count; i++) {
		if (!zero_pids[i])
			continue;

		waitpid(zero_pids[i], &wstatus, 0);
		zero_pids[i] = 0;
		if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus))
			ret = 1;
	}

	if (interrupted) {
		pr_err("zeroing interrupted!\n");
		return 1;
	}

	if (ret)
		pr_err("zeroing failed!\n");
	else
		pr_info("zeroing finished\n");

	return ret;
}

static int add_disk_to_super(int mdfd, struct shape *s, struct context *c,
		struct supertype *st, struct mddev_dev *dv,
		struct mdinfo *info, int have_container, int major_num,
		int *zero_pid)
{
	dev_t rdev;
	int fd;

	if (dv->disposition == 'j') {
		info->disk.raid_disk = MD_DISK_ROLE_JOURNAL;
		info->disk.state = (1<<MD_DISK_JOURNAL);
	} else if (info->disk.raid_disk < s->raiddisks) {
		info->disk.state = (1<<MD_DISK_ACTIVE) |
			(1<<MD_DISK_SYNC);
	} else {
		info->disk.state = 0;
	}

	if (dv->writemostly == FlagSet) {
		if (major_num == BITMAP_MAJOR_CLUSTERED) {
			pr_err("Can not set %s --write-mostly with a clustered bitmap\n",dv->devname);
			return 1;
		} else {
			info->disk.state |= (1<<MD_DISK_WRITEMOSTLY);
		}

	}

	if (dv->failfast == FlagSet)
		info->disk.state |= (1<<MD_DISK_FAILFAST);

	if (have_container) {
		fd = -1;
	} else {
		if (st->ss->external && st->container_devnm[0])
			fd = open(dv->devname, O_RDWR);
		else
			fd = open(dv->devname, O_RDWR|O_EXCL);

		if (fd < 0) {
			pr_err("failed to open %s after earlier success - aborting\n",
			       dv->devname);
			return 1;
		}
		if (!fstat_is_blkdev(fd, dv->devname, &rdev)) {
			close(fd);
			return 1;
		}
		info->disk.major = major(rdev);
		info->disk.minor = minor(rdev);
	}
	if (fd >= 0)
		remove_partitions(fd);
	if (st->ss->add_to_super(st, &info->disk, fd, dv->devname,
				 dv->data_offset)) {
		ioctl(mdfd, STOP_ARRAY, NULL);
		close(fd);
		return 1;
	}
	st->ss->getinfo_super(st, info, NULL);

	if (fd >= 0 && s->write_zeroes) {
		*zero_pid = write_zeroes_fork(fd, s, st, dv);
		if (*zero_pid <= 0) {
			ioctl(mdfd, STOP_ARRAY, NULL);
			close(fd);
			return 1;
		}
	}

	if (have_container && c->verbose > 0)
		pr_err("Using %s for device %d\n",
		       map_dev(info->disk.major, info->disk.minor, 0),
		       info->disk.number);

	if (!have_container) {
		/* getinfo_super might have lost these ... */
		info->disk.major = major(rdev);
		info->disk.minor = minor(rdev);
	}

	return 0;
}

static int update_metadata(int mdfd, struct shape *s, struct supertype *st,
			   struct map_ent **map, struct mdinfo *info,
			   char *chosen_name)
{
	struct mdinfo info_new;
	struct map_ent *me = NULL;

	/* check to see if the uuid has changed due to these
	 * metadata changes, and if so update the member array
	 * and container uuid.  Note ->write_init_super clears
	 * the subarray cursor such that ->getinfo_super once
	 * again returns container info.
	 */
	st->ss->getinfo_super(st, &info_new, NULL);
	if (st->ss->external && !is_container(s->level) &&
	    !same_uuid(info_new.uuid, info->uuid, 0)) {
		map_update(map, fd2devnm(mdfd),
			   info_new.text_version,
			   info_new.uuid, chosen_name);
		me = map_by_devnm(map, st->container_devnm);
	}

	if (st->ss->write_init_super(st)) {
		st->ss->free_super(st);
		return 1;
	}

	/*
	 * Before activating the array, perform extra steps
	 * required to configure the internal write-intent
	 * bitmap.
	 */
	if (info_new.consistency_policy == CONSISTENCY_POLICY_BITMAP &&
	    st->ss->set_bitmap && st->ss->set_bitmap(st, info)) {
		st->ss->free_super(st);
		return 1;
	}

	/* update parent container uuid */
	if (me) {
		char *path = xstrdup(me->path);

		st->ss->getinfo_super(st, &info_new, NULL);
		map_update(map, st->container_devnm, info_new.text_version,
			   info_new.uuid, path);
		free(path);
	}

	flush_metadata_updates(st);
	st->ss->free_super(st);

	return 0;
}

static int add_disks(int mdfd, struct mdinfo *info, struct shape *s,
		     struct context *c, struct supertype *st,
		     struct map_ent **map, struct mddev_dev *devlist,
		     int total_slots, int have_container, int insert_point,
		     int major_num, char *chosen_name)
{
	struct mddev_dev *moved_disk = NULL;
	int pass, raid_disk_num, dnum;
	int zero_pids[total_slots];
	struct mddev_dev *dv;
	struct mdinfo *infos;
	sigset_t sigset, orig_sigset;
	int ret = 0;

	/*
	 * Block SIGINT so the main thread will always wait for the
	 * zeroing processes when being interrupted. Otherwise the
	 * zeroing processes will finish their work in the background
	 * keeping the disk busy.
	 */
	sigemptyset(&sigset);
	sigaddset(&sigset, SIGINT);
	sigprocmask(SIG_BLOCK, &sigset, &orig_sigset);
	memset(zero_pids, 0, sizeof(zero_pids));

	infos = xmalloc(sizeof(*infos) * total_slots);
	enable_fds(total_slots);
	for (pass = 1; pass <= 2; pass++) {
		for (dnum = 0, raid_disk_num = 0, dv = devlist; dv;
		     dv = (dv->next) ? (dv->next) : moved_disk, dnum++) {
			if (dnum >= total_slots)
				abort();
			if (dnum == insert_point) {
				raid_disk_num += 1;
				moved_disk = dv;
				continue;
			}
			if (strcasecmp(dv->devname, "missing") == 0) {
				raid_disk_num += 1;
				continue;
			}
			if (have_container)
				moved_disk = NULL;
			if (have_container && dnum < total_slots - 1)
				/* repeatedly use the container */
				moved_disk = dv;

			switch(pass) {
			case 1:
				infos[dnum] = *info;
				infos[dnum].disk.number = dnum;
				infos[dnum].disk.raid_disk = raid_disk_num++;

				if (dv->disposition == 'j')
					raid_disk_num--;

				ret = add_disk_to_super(mdfd, s, c, st, dv,
						&infos[dnum], have_container,
						major_num, &zero_pids[dnum]);
				if (ret)
					goto out;

				break;
			case 2:
				infos[dnum].errors = 0;

				ret = add_disk(mdfd, st, info, &infos[dnum]);
				if (ret) {
					pr_err("ADD_NEW_DISK for %s failed: %s\n",
					       dv->devname, strerror(errno));
					if (errno == EINVAL &&
					    info->array.level == 0) {
						pr_err("Possibly your kernel doesn't support RAID0 layouts.\n");
						pr_err("Either upgrade, or use --layout=dangerous\n");
					}
					goto out;
				}
				break;
			}
			if (!have_container &&
			    dv == moved_disk && dnum != insert_point) break;
		}

		if (pass == 1) {
			ret = wait_for_zero_forks(zero_pids, total_slots);
			if (ret)
				goto out;

			ret = update_metadata(mdfd, s, st, map, info,
					      chosen_name);
			if (ret)
				goto out;
		}
	}

out:
	if (ret)
		wait_for_zero_forks(zero_pids, total_slots);
	free(infos);
	sigprocmask(SIG_SETMASK, &orig_sigset, NULL);
	return ret;
}

int Create(struct supertype *st, struct mddev_ident *ident, int subdevs,
	   struct mddev_dev *devlist, struct shape *s, struct context *c)
{
	/*
	 * Create a new raid array.
	 *
	 * First check that necessary details are available
	 * (i.e. level, raid-disks)
	 *
	 * Then check each disk to see what might be on it
	 * and report anything interesting.
	 *
	 * If anything looks odd, and runstop not set,
	 * abort.
	 *
	 * SET_ARRAY_INFO and ADD_NEW_DISK, and
	 * if runstop==run, or raiddisks disks were used,
	 * RUN_ARRAY
	 */
	int mdfd;
	unsigned long long minsize = 0, maxsize = 0;
	dev_policy_t *custom_pols = NULL;
	char *mindisc = NULL;
	char *maxdisc = NULL;
	char *name = ident->name;
	int *uuid = ident->uuid_set == 1 ? ident->uuid : NULL;
	int dnum;
	struct mddev_dev *dv;
	dev_t rdev;
	int fail = 0, warn = 0;
	int first_missing = subdevs * 2;
	int second_missing = subdevs * 2;
	int missing_disks = 0;
	int insert_point = subdevs * 2; /* where to insert a missing drive */
	int total_slots;
	int rv;
	int bitmap_fd;
	int have_container = 0;
	int container_fd = -1;
	int need_mdmon = 0;
	unsigned long long bitmapsize;
	struct mdinfo info;
	int did_default = 0;
	int do_default_layout = 0;
	int do_default_chunk = 0;
	char chosen_name[1024];
	struct map_ent *map = NULL;
	unsigned long long newsize;
	mdu_array_info_t inf;

	int major_num = BITMAP_MAJOR_HI;
	if (s->bitmap_file && strcmp(s->bitmap_file, "clustered") == 0) {
		major_num = BITMAP_MAJOR_CLUSTERED;
		if (c->nodes <= 1) {
			pr_err("At least 2 nodes are needed for cluster-md\n");
			return 1;
		}
	}

	memset(&info, 0, sizeof(info));
	if (s->level == UnSet && st && st->ss->default_geometry)
		st->ss->default_geometry(st, &s->level, NULL, NULL);
	if (s->level == UnSet) {
		pr_err("a RAID level is needed to create an array.\n");
		return 1;
	}
	if (s->raiddisks < 4 && s->level == 6) {
		pr_err("at least 4 raid-devices needed for level 6\n");
		return 1;
	}
	if (s->raiddisks > 256 && s->level == 6) {
		pr_err("no more than 256 raid-devices supported for level 6\n");
		return 1;
	}
	if (s->raiddisks < 2 && s->level >= 4) {
		pr_err("at least 2 raid-devices needed for level %d\n", s->level);
		return 1;
	}
	if (s->level <= 0 && s->sparedisks) {
		pr_err("This level does not support spare devices\n");
		return 1;
	}

	if (subdevs == 1 && strcmp(devlist->devname, "missing") != 0) {
		/* If given a single device, it might be a container, and we can
		 * extract a device list from there
		 */
		int fd;

		memset(&inf, 0, sizeof(inf));
		fd = open(devlist->devname, O_RDONLY);
		if (fd >= 0 &&
		    md_get_array_info(fd, &inf) == 0 && inf.raid_disks == 0) {
			/* yep, looks like a container */
			if (st) {
				rv = st->ss->load_container(st, fd,
							    devlist->devname);
				if (rv == 0)
					have_container = 1;
			} else {
				st = super_by_fd(fd, NULL);
				if (st && !(rv = st->ss->
					    load_container(st, fd,
							   devlist->devname)))
					have_container = 1;
				else
					st = NULL;
			}
			if (have_container) {
				subdevs = s->raiddisks;
				first_missing = subdevs * 2;
				second_missing = subdevs * 2;
				insert_point = subdevs * 2;

				if (mddev_test_and_add_drive_policies(st, &custom_pols, fd, 1))
					exit(1);
			}
		}
		if (fd >= 0)
			close(fd);
	}
	if (st && st->ss->external && s->sparedisks) {
		pr_err("This metadata type does not support spare disks at create time\n");
		return 1;
	}
	if (subdevs > s->raiddisks+s->sparedisks+s->journaldisks) {
		pr_err("You have listed more devices (%d) than are in the array(%d)!\n", subdevs, s->raiddisks+s->sparedisks);
		return 1;
	}
	if (!have_container && subdevs < s->raiddisks+s->sparedisks+s->journaldisks) {
		pr_err("You haven't given enough devices (real or missing) to create this array\n");
		return 1;
	}
	if (s->bitmap_file && s->level <= 0) {
		pr_err("bitmaps not meaningful with level %s\n",
			map_num(pers, s->level)?:"given");
		return 1;
	}

	/* now set some defaults */

	if (s->layout == UnSet) {
		do_default_layout = 1;
		s->layout = default_layout(st, s->level, c->verbose);
	}

	if (s->level == 10)
		/* check layout fits in array*/
		if ((s->layout&255) * ((s->layout>>8)&255) > s->raiddisks) {
			pr_err("that layout requires at least %d devices\n",
				(s->layout&255) * ((s->layout>>8)&255));
			return 1;
		}

	switch(s->level) {
	case 4:
	case 5:
	case 10:
	case 6:
	case 0:
		if (s->chunk == 0 || s->chunk == UnSet) {
			s->chunk = UnSet;
			do_default_chunk = 1;
			/* chunk will be set later */
		}
		break;
	case LEVEL_LINEAR:
		/* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
		break;
	case 1:
	case LEVEL_FAULTY:
	case LEVEL_MULTIPATH:
	case LEVEL_CONTAINER:
		if (s->chunk) {
			pr_err("specifying chunk size is forbidden for this level\n");
			return 1;
		}
		break;
	default:
		pr_err("unknown level %d\n", s->level);
		return 1;
	}

	if (s->size == MAX_SIZE)
		/* use '0' to mean 'max' now... */
		s->size = 0;
	if (s->size && s->chunk && s->chunk != UnSet)
		if (round_size_and_verify(&s->size, s->chunk))
			return 1;

	newsize = s->size * 2;
	if (st && ! st->ss->validate_geometry(st, s->level, s->layout, s->raiddisks,
					      &s->chunk, s->size*2,
					      s->data_offset, NULL,
					      &newsize, s->consistency_policy,
					      c->verbose >= 0))
		return 1;

	if (s->chunk && s->chunk != UnSet) {
		newsize &= ~(unsigned long long)(s->chunk*2 - 1);
		if (do_default_chunk) {
			/* default chunk was just set */
			if (c->verbose > 0)
				pr_err("chunk size defaults to %dK\n", s->chunk);
			if (round_size_and_verify(&s->size, s->chunk))
				return 1;
			do_default_chunk = 0;
		}
	}

	if (s->size == 0) {
		s->size = newsize / 2;
		if (s->level == 1)
			/* If this is ever reshaped to RAID5, we will
			 * need a chunksize.  So round it off a bit
			 * now just to be safe
			 */
			s->size &= ~(64ULL-1);

		if (s->size && c->verbose > 0)
			pr_err("setting size to %lluK\n", s->size);
	}

	/* now look at the subdevs */
	info.array.active_disks = 0;
	info.array.working_disks = 0;
	dnum = 0;
	for (dv = devlist; dv; dv = dv->next)
		if (s->data_offset == VARIABLE_OFFSET)
			dv->data_offset = INVALID_SECTORS;
		else
			dv->data_offset = s->data_offset;

	for (dv=devlist; dv && !have_container; dv=dv->next, dnum++) {
		char *dname = dv->devname;
		unsigned long long freesize;
		int dfd;
		char *doff;

		if (strcasecmp(dname, "missing") == 0) {
			if (first_missing > dnum)
				first_missing = dnum;
			if (second_missing > dnum && dnum > first_missing)
				second_missing = dnum;
			missing_disks ++;
			continue;
		}
		if (s->data_offset == VARIABLE_OFFSET) {
			doff = strchr(dname, ':');
			if (doff) {
				*doff++ = 0;
				dv->data_offset = parse_size(doff);
			} else
				dv->data_offset = INVALID_SECTORS;
		} else
			dv->data_offset = s->data_offset;

		dfd = open(dname, O_RDONLY);
		if (dfd < 0) {
			pr_err("cannot open %s: %s\n",
				dname, strerror(errno));
			exit(2);
		}
		if (!fstat_is_blkdev(dfd, dname, NULL)) {
			close(dfd);
			exit(2);
		}

		info.array.working_disks++;
		if (dnum < s->raiddisks && dv->disposition != 'j')
			info.array.active_disks++;
		if (st == NULL) {
			struct createinfo *ci = conf_get_create_info();
			if (ci)
				st = ci->supertype;
		}
		if (st == NULL) {
			/* Need to choose a default metadata, which is different
			 * depending on geometry of array.
			 */
			int i;
			char *name = "default";
			for(i = 0; !st && superlist[i]; i++) {
				st = superlist[i]->match_metadata_desc(name);
				if (!st)
					continue;
				if (do_default_layout)
					s->layout = default_layout(st, s->level, c->verbose);
				switch (st->ss->validate_geometry(
						st, s->level, s->layout, s->raiddisks,
						&s->chunk, s->size*2,
						dv->data_offset, dname,
						&freesize, s->consistency_policy,
						c->verbose > 0)) {
				case -1: /* Not valid, message printed, and not
					  * worth checking any further */
					exit(2);
					break;
				case 0: /* Geometry not valid */
					free(st);
					st = NULL;
					s->chunk = do_default_chunk ? UnSet : s->chunk;
					break;
				case 1:	/* All happy */
					break;
				}
			}

			if (!st) {
				int dfd = open(dname, O_RDONLY|O_EXCL);
				if (dfd < 0) {
					pr_err("cannot open %s: %s\n",
						dname, strerror(errno));
					exit(2);
				}
				pr_err("device %s not suitable for any style of array\n",
					dname);
				exit(2);
			}
			if (st->ss != &super0 ||
			    st->minor_version != 90)
				did_default = 1;
		} else {
			if (do_default_layout)
				s->layout = default_layout(st, s->level, 0);
			if (!st->ss->validate_geometry(st, s->level, s->layout,
						       s->raiddisks,
						       &s->chunk, s->size*2,
						       dv->data_offset,
						       dname, &freesize,
						       s->consistency_policy,
						       c->verbose >= 0)) {

				pr_err("%s is not suitable for this array.\n",
				       dname);
				fail = 1;
				continue;
			}
		}

		if (drive_test_and_add_policies(st, &custom_pols, dfd, 1))
			exit(1);

		close(dfd);

		if (dv->disposition == 'j')
			goto skip_size_check;  /* skip write journal for size check */

		freesize /= 2; /* convert to K */
		if (s->chunk && s->chunk != UnSet) {
			/* round to chunk size */
			freesize = freesize & ~(s->chunk-1);
			if (do_default_chunk) {
				/* default chunk was just set */
				if (c->verbose > 0)
					pr_err("chunk size defaults to %dK\n", s->chunk);
				if (round_size_and_verify(&s->size, s->chunk))
					return 1;
				do_default_chunk = 0;
			}
		}
		if (!freesize) {
			pr_err("no free space left on %s\n", dname);
			fail = 1;
			continue;
		}

		if (s->size && freesize < s->size) {
			pr_err("%s is smaller than given size. %lluK < %lluK + metadata\n",
				dname, freesize, s->size);
			fail = 1;
			continue;
		}
		if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
			maxdisc = dname;
			maxsize = freesize;
		}
		if (mindisc ==NULL || (mindisc && freesize < minsize)) {
			mindisc = dname;
			minsize = freesize;
		}
	skip_size_check:
		if (c->runstop != 1 || c->verbose >= 0) {
			int fd = open(dname, O_RDONLY);
			if (fd < 0) {
				pr_err("Cannot open %s: %s\n",
					dname, strerror(errno));
				fail = 1;
				continue;
			}
			warn |= check_ext2(fd, dname);
			warn |= check_reiser(fd, dname);
			warn |= check_raid(fd, dname);
			if (strcmp(st->ss->name, "1.x") == 0 &&
			    st->minor_version >= 1)
				/* metadata at front */
				warn |= check_partitions(fd, dname, 0, 0);
			else if (s->level == 1 || is_container(s->level) ||
				 (s->level == 0 && s->raiddisks == 1))
				/* partitions could be meaningful */
				warn |= check_partitions(fd, dname, freesize*2, s->size*2);
			else
				/* partitions cannot be meaningful */
				warn |= check_partitions(fd, dname, 0, 0);
			if (strcmp(st->ss->name, "1.x") == 0 &&
			    st->minor_version >= 1 &&
			    did_default &&
			    s->level == 1 &&
			    (warn & 1024) == 0) {
				warn |= 1024;
				pr_err("Note: this array has metadata at the start and\n"
					"    may not be suitable as a boot device.  If you plan to\n"
					"    store '/boot' on this device please ensure that\n"
					"    your boot-loader understands md/v1.x metadata, or use\n"
					"    --metadata=0.90\n");
			}
			close(fd);
		}
	}

	if (missing_disks == dnum && !have_container) {
		pr_err("Subdevs can't be all missing\n");
		return 1;
	}
	if (s->raiddisks + s->sparedisks > st->max_devs) {
		pr_err("Too many devices: %s metadata only supports %d\n",
			st->ss->name, st->max_devs);
		return 1;
	}
	if (have_container)
		info.array.working_disks = s->raiddisks;
	if (fail) {
		pr_err("create aborted\n");
		return 1;
	}
	if (s->size == 0) {
		if (mindisc == NULL && !have_container) {
			pr_err("no size and no drives given - aborting create.\n");
			return 1;
		}
		if (s->level > 0 || s->level == LEVEL_MULTIPATH ||
		    s->level == LEVEL_FAULTY || st->ss->external) {
			/* size is meaningful */
			if (!st->ss->validate_geometry(st, s->level, s->layout,
						       s->raiddisks,
						       &s->chunk, minsize*2,
						       s->data_offset,
						       NULL, NULL,
						       s->consistency_policy, 0)) {
				pr_err("devices too large for RAID level %d\n", s->level);
				return 1;
			}
			s->size = minsize;
			if (s->level == 1)
				/* If this is ever reshaped to RAID5, we will
				 * need a chunksize.  So round it off a bit
				 * now just to be safe
				 */
				s->size &= ~(64ULL-1);
			if (c->verbose > 0)
				pr_err("size set to %lluK\n", s->size);
		}
	}

	if (!s->bitmap_file &&
	    !st->ss->external &&
	    s->level >= 1 &&
	    st->ss->add_internal_bitmap &&
	    s->journaldisks == 0 &&
	    (s->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
	     s->consistency_policy != CONSISTENCY_POLICY_PPL) &&
	    (s->write_behind || s->size > 100*1024*1024ULL)) {
		if (c->verbose > 0)
			pr_err("automatically enabling write-intent bitmap on large array\n");
		s->bitmap_file = "internal";
	}
	if (s->bitmap_file && str_is_none(s->bitmap_file) == true)
		s->bitmap_file = NULL;

	if (s->consistency_policy == CONSISTENCY_POLICY_PPL &&
	    !st->ss->write_init_ppl) {
		pr_err("%s metadata does not support PPL\n", st->ss->name);
		return 1;
	}

	if (!have_container && s->level > 0 && ((maxsize-s->size)*100 > maxsize)) {
		if (c->runstop != 1 || c->verbose >= 0)
			pr_err("largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
				maxdisc, s->size);
		warn = 1;
	}

	if (st->ss->detail_platform && st->ss->detail_platform(0, 1, NULL) != 0) {
		if (c->runstop != 1 || c->verbose >= 0)
			pr_err("%s unable to enumerate platform support\n"
				"    array may not be compatible with hardware/firmware\n",
				st->ss->name);
		warn = 1;
	}
	st->nodes = c->nodes;
	st->cluster_name = c->homecluster;

	if (warn) {
		if (c->runstop!= 1) {
			if (!ask("Continue creating array? ")) {
				pr_err("create aborted.\n");
				return 1;
			}
		} else {
			if (c->verbose > 0)
				pr_err("creation continuing despite oddities due to --run\n");
		}
	}

	/* If this is raid4/5, we want to configure the last active slot
	 * as missing, so that a reconstruct happens (faster than re-parity)
	 * FIX: Can we do this for raid6 as well?
	 */
	if (st->ss->external == 0 && s->assume_clean == 0 &&
	    c->force == 0 && first_missing >= s->raiddisks) {
		switch (s->level) {
		case 4:
		case 5:
			insert_point = s->raiddisks-1;
			s->sparedisks++;
			info.array.active_disks--;
			missing_disks++;
			break;
		default:
			break;
		}
	}
	/* For raid6, if creating with 1 missing drive, make a good drive
	 * into a spare, else the create will fail
	 */
	if (s->assume_clean == 0 && c->force == 0 && first_missing < s->raiddisks &&
	    st->ss->external == 0 &&
	    second_missing >= s->raiddisks && s->level == 6) {
		insert_point = s->raiddisks - 1;
		if (insert_point == first_missing)
			insert_point--;
		s->sparedisks ++;
		info.array.active_disks--;
		missing_disks++;
	}

	if (s->level <= 0 && first_missing < subdevs * 2) {
		pr_err("This level does not support missing devices\n");
		return 1;
	}

	/* We need to create the device */
	map_lock(&map);
	mdfd = create_mddev(ident->devname, ident->name, c->autof, LOCAL, chosen_name, 1);
	if (mdfd < 0) {
		map_unlock(&map);
		return 1;
	}
	/* verify if chosen_name is not in use,
	 * it could be in conflict with already existing device
	 * e.g. container, array
	 */
	if (strncmp(chosen_name, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0 &&
	    map_by_name(&map, chosen_name + DEV_MD_DIR_LEN)) {
		pr_err("Array name %s is in use already.\n", chosen_name);
		close(mdfd);
		map_unlock(&map);
		udev_unblock();
		return 1;
	}

	memset(&inf, 0, sizeof(inf));
	md_get_array_info(mdfd, &inf);
	if (inf.working_disks != 0) {
		pr_err("another array by this name is already running.\n");
		goto abort_locked;
	}

	/* Ok, lets try some ioctls */

	info.array.level = s->level;
	info.array.size = s->size;
	info.array.raid_disks = s->raiddisks;
	/* The kernel should *know* what md_minor we are dealing
	 * with, but it chooses to trust me instead. Sigh
	 */
	info.array.md_minor = 0;
	if (fstat_is_blkdev(mdfd, chosen_name, &rdev))
		info.array.md_minor = minor(rdev);
	info.array.not_persistent = 0;

	if (((s->level == 4 || s->level == 5) &&
	     (insert_point < s->raiddisks || first_missing < s->raiddisks)) ||
	    (s->level == 6 && (insert_point < s->raiddisks ||
			       second_missing < s->raiddisks)) ||
	    (s->level <= 0) || s->assume_clean) {
		info.array.state = 1; /* clean, but one+ drive will be missing*/
		info.resync_start = MaxSector;
	} else {
		info.array.state = 0; /* not clean, but no errors */
		info.resync_start = 0;
	}
	if (s->level == 10) {
		/* for raid10, the bitmap size is the capacity of the array,
		 * which is array.size * raid_disks / ncopies;
		 * .. but convert to sectors.
		 */
		int ncopies = ((s->layout>>8) & 255) * (s->layout & 255);
		bitmapsize = s->size * s->raiddisks / ncopies * 2;
/*		printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, s->size, s->raiddisks, ncopies);*/
	} else
		bitmapsize = s->size * 2;

	/* There is lots of redundancy in these disk counts,
	 * raid_disks is the most meaningful value
	 *          it describes the geometry of the array
	 *          it is constant
	 * nr_disks is total number of used slots.
	 *          it should be raid_disks+spare_disks
	 * spare_disks is the number of extra disks present
	 *          see above
	 * active_disks is the number of working disks in
	 *          active slots. (With raid_disks)
	 * working_disks is the total number of working disks,
	 *          including spares
	 * failed_disks is the number of disks marked failed
	 *
	 * Ideally, the kernel would keep these (except raid_disks)
	 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
	 * So for now, we assume that all raid and spare
	 * devices will be given.
	 */
	info.array.spare_disks=s->sparedisks;
	info.array.failed_disks=missing_disks;
	info.array.nr_disks = info.array.working_disks
		+ info.array.failed_disks;
	info.array.layout = s->layout;
	info.array.chunk_size = s->chunk*1024;

	if (*name == 0) {
		/* base name on devname */
		/*  /dev/md0 -> 0
		 *  /dev/md_d0 -> d0
		 *  /dev/md_foo -> foo
		 *  /dev/md/1 -> 1
		 *  /dev/md/d1 -> d1
		 *  /dev/md/home -> home
		 *  /dev/mdhome -> home
		 */
		/* FIXME compare this with rules in create_mddev */
		name = strrchr(chosen_name, '/');

		if (name) {
			name++;
			if (strncmp(name, "md_", 3) == 0 &&
			    strlen(name) > 3 && (name - chosen_name) == 5 /* /dev/ */)
				name += 3;
			else if (strncmp(name, "md", 2) == 0 &&
				 strlen(name) > 2 && isdigit(name[2]) &&
				 (name - chosen_name) == 5 /* /dev/ */)
				name += 2;
		}
	}
	if (!st->ss->init_super(st, &info.array, s, name, c->homehost, uuid,
				s->data_offset))
		goto abort_locked;

	total_slots = info.array.nr_disks;
	st->ss->getinfo_super(st, &info, NULL);
	if (sysfs_init(&info, mdfd, NULL)) {
		pr_err("unable to initialize sysfs\n");
		goto abort_locked;
	}

	if (did_default) {
		if (is_subarray(info.text_version)) {
			char devnm[MD_NAME_MAX];
			struct mdinfo *mdi;

			sysfs_get_container_devnm(&info, devnm);

			mdi = sysfs_read(-1, devnm, GET_VERSION | GET_DEVS);
			if (!mdi) {
				pr_err("Cannot open sysfs for container %s\n", devnm);
				goto abort_locked;
			}

			if (sysfs_test_and_add_drive_policies(st, &custom_pols, mdi, 1))
				goto abort_locked;

			if (c->verbose >= 0)
				pr_info("Creating array inside %s container /dev/%s\n",
					mdi->text_version, devnm);

			sysfs_free(mdi);
		} else if (c->verbose >= 0) {
			pr_info("Defaulting to version %s metadata\n", info.text_version);
		}
	}

	map_update(&map, fd2devnm(mdfd), info.text_version,
		   info.uuid, chosen_name);
	/* Keep map locked until devices have been added to array
	 * to stop another mdadm from finding and using those devices.
	 */

	if (s->bitmap_file && (strcmp(s->bitmap_file, "internal") == 0 ||
			       strcmp(s->bitmap_file, "clustered") == 0)) {
		if (!st->ss->add_internal_bitmap) {
			pr_err("internal bitmaps not supported with %s metadata\n",
				st->ss->name);
			goto abort_locked;
		}
		if (st->ss->add_internal_bitmap(st, &s->bitmap_chunk,
						c->delay, s->write_behind,
						bitmapsize, 1, major_num)) {
			pr_err("Given bitmap chunk size not supported.\n");
			goto abort_locked;
		}
		s->bitmap_file = NULL;
	}

	if (sysfs_init(&info, mdfd, NULL)) {
		pr_err("unable to initialize sysfs\n");
		goto abort_locked;
	}

	if (st->ss->external && st->container_devnm[0]) {
		/* member */

		/* When creating a member, we need to be careful
		 * to negotiate with mdmon properly.
		 * If it is already running, we cannot write to
		 * the devices and must ask it to do that part.
		 * If it isn't running, we write to the devices,
		 * and then start it.
		 * We hold an exclusive open on the container
		 * device to make sure mdmon doesn't exit after
		 * we checked that it is running.
		 *
		 * For now, fail if it is already running.
		 */
		container_fd = open_dev_excl(st->container_devnm);
		if (container_fd < 0) {
			pr_err("Cannot get exclusive open on container - weird.\n");
			goto abort_locked;
		}
		if (mdmon_running(st->container_devnm)) {
			if (c->verbose)
				pr_err("reusing mdmon for %s.\n",
					st->container_devnm);
			st->update_tail = &st->updates;
		} else
			need_mdmon = 1;
	}
	rv = set_array_info(mdfd, st, &info);
	if (rv) {
		pr_err("failed to set array info for %s: %s\n", chosen_name, strerror(errno));
		goto abort_locked;
	}

	if (s->bitmap_file) {
		int uuid[4];

		st->ss->uuid_from_super(st, uuid);
		if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid, s->bitmap_chunk,
				 c->delay, s->write_behind,
				 bitmapsize,
				 major_num)) {
			goto abort_locked;
		}
		bitmap_fd = open(s->bitmap_file, O_RDWR);
		if (bitmap_fd < 0) {
			pr_err("weird: %s cannot be opened\n",
				s->bitmap_file);
			goto abort_locked;
		}
		if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
			pr_err("Cannot set bitmap file for %s: %s\n", chosen_name, strerror(errno));
			goto abort_locked;
		}
	}

	if (add_disks(mdfd, &info, s, c, st, &map, devlist, total_slots,
		      have_container, insert_point, major_num, chosen_name))
		goto abort_locked;

	map_unlock(&map);

	if (is_container(s->level)) {
		/* No need to start.  But we should signal udev to
		 * create links */
		sysfs_uevent(&info, "change");
		if (c->verbose >= 0)
			pr_err("container %s prepared.\n", chosen_name);
		wait_for(chosen_name, mdfd);
	} else if (c->runstop == 1 || subdevs >= s->raiddisks) {
		if (st->ss->external) {
			int err;
			switch(s->level) {
			case LEVEL_LINEAR:
			case LEVEL_MULTIPATH:
			case 0:
				err = sysfs_set_str(&info, NULL, "array_state",
						    c->readonly
						    ? "readonly"
						    : "active");
				need_mdmon = 0;
				break;
			default:
				err = sysfs_set_str(&info, NULL, "array_state",
						    "readonly");
				break;
			}
			sysfs_set_safemode(&info, info.safe_mode_delay);
			if (err) {
				pr_err("failed to activate array.\n");
				ioctl(mdfd, STOP_ARRAY, NULL);
				goto abort;
			}
		} else if (c->readonly &&
			   sysfs_attribute_available(
				   &info, NULL, "array_state")) {
			if (sysfs_set_str(&info, NULL,
					  "array_state", "readonly") < 0) {
				pr_err("Failed to start array: %s\n",
				       strerror(errno));
				ioctl(mdfd, STOP_ARRAY, NULL);
				goto abort;
			}
		} else {
			/* param is not actually used */
			mdu_param_t param;
			if (ioctl(mdfd, RUN_ARRAY, &param)) {
				pr_err("RUN_ARRAY failed: %s\n",
				       strerror(errno));
				if (errno == 524 /* ENOTSUP */ &&
				    info.array.level == 0)
					cont_err("Please use --layout=original or --layout=alternate\n");
				if (info.array.chunk_size & (info.array.chunk_size-1)) {
					cont_err("Problem may be that chunk size is not a power of 2\n");
				}
				ioctl(mdfd, STOP_ARRAY, NULL);
				goto abort;
			}
			/* if start_ro module parameter is set, array is
			 * auto-read-only, which is bad as the resync won't
			 * start.  So lets make it read-write now.
			 */
			ioctl(mdfd, RESTART_ARRAY_RW, NULL);
		}
		if (c->verbose >= 0)
			pr_info("array %s started.\n", chosen_name);
		if (st->ss->external && st->container_devnm[0]) {
			if (need_mdmon)
				start_mdmon(st->container_devnm);

			ping_monitor(st->container_devnm);
			close(container_fd);
		}
		wait_for(chosen_name, mdfd);
	} else {
		pr_err("not starting array - not enough devices.\n");
	}
	udev_unblock();
	close(mdfd);
	sysfs_uevent(&info, "change");
	dev_policy_free(custom_pols);

	return 0;

 abort:
	udev_unblock();
	map_lock(&map);
 abort_locked:
	map_remove(&map, fd2devnm(mdfd));
	map_unlock(&map);

	if (mdfd >= 0)
		close(mdfd);

	dev_policy_free(custom_pols);
	return 1;
}