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