Assemble: allow members of containers to be assembled and auto-assembled.
[thirdparty/mdadm.git] / mdadm.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  *    Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
30  *    Paul Clements, SteelEye Technology, Inc.
31  */
32
33 #include "mdadm.h"
34 #include "md_p.h"
35 #include <ctype.h>
36
37
38 int main(int argc, char *argv[])
39 {
40         int mode = 0;
41         int opt;
42         int option_index;
43         char *c;
44         int rv;
45         int i;
46
47         int chunk = 0;
48         long long size = -1;
49         int level = UnSet;
50         int layout = UnSet;
51         int raiddisks = 0;
52         int max_disks = MD_SB_DISKS; /* just a default */
53         int sparedisks = 0;
54         struct mddev_ident_s ident;
55         char *configfile = NULL;
56         char *cp;
57         char *update = NULL;
58         int scan = 0;
59         char devmode = 0;
60         int runstop = 0;
61         int readonly = 0;
62         int write_behind = 0;
63         int bitmap_fd = -1;
64         char *bitmap_file = NULL;
65         char *backup_file = NULL;
66         int bitmap_chunk = UnSet;
67         int SparcAdjust = 0;
68         mddev_dev_t devlist = NULL;
69         mddev_dev_t *devlistend = & devlist;
70         mddev_dev_t dv;
71         int devs_found = 0;
72         int verbose = 0;
73         int quiet = 0;
74         int brief = 0;
75         int force = 0;
76         int test = 0;
77         int export = 0;
78         int assume_clean = 0;
79         char *symlinks = NULL;
80         /* autof indicates whether and how to create device node.
81          * bottom 3 bits are style.  Rest (when shifted) are number of parts
82          * 0  - unset
83          * 1  - don't create (no)
84          * 2  - if is_standard, then create (yes)
85          * 3  - create as 'md' - reject is_standard mdp (md)
86          * 4  - create as 'mdp' - reject is_standard md (mdp)
87          * 5  - default to md if not is_standard (md in config file)
88          * 6  - default to mdp if not is_standard (part, or mdp in config file)
89          */
90         int autof = 0;
91
92         char *homehost = NULL;
93         char sys_hostname[256];
94         char *mailaddr = NULL;
95         char *program = NULL;
96         int delay = 0;
97         int daemonise = 0;
98         char *pidfile = NULL;
99         int oneshot = 0;
100         struct supertype *ss = NULL;
101         int writemostly = 0;
102         int re_add = 0;
103         char *shortopt = short_options;
104         int dosyslog = 0;
105         int rebuild_map = 0;
106         int auto_update_home = 0;
107
108         int copies;
109         int print_help = 0;
110         FILE *outf;
111
112         int mdfd = -1;
113
114         srandom(time(0) ^ getpid());
115
116         ident.uuid_set=0;
117         ident.level = UnSet;
118         ident.raid_disks = UnSet;
119         ident.super_minor= UnSet;
120         ident.devices=0;
121         ident.spare_group = NULL;
122         ident.autof = 0;
123         ident.st = NULL;
124         ident.bitmap_fd = -1;
125         ident.bitmap_file = NULL;
126         ident.name[0] = 0;
127         ident.container = NULL;
128         ident.member = NULL;
129
130         while ((option_index = -1) ,
131                (opt=getopt_long(argc, argv,
132                                 shortopt, long_options,
133                                 &option_index)) != -1) {
134                 int newmode = mode;
135                 /* firstly, some mode-independant options */
136                 switch(opt) {
137                 case 'h':
138                         if (option_index > 0 &&
139                             strcmp(long_options[option_index].name, "help-options")==0)
140                                 print_help = 2;
141                         else
142                                 print_help = 1;
143                         continue;
144
145                 case 'V':
146                         fputs(Version, stderr);
147                         exit(0);
148
149                 case 'v': verbose++;
150                         continue;
151
152                 case 'q': quiet++;
153                         continue;
154
155                 case 'b':
156                         if (mode == ASSEMBLE || mode == BUILD || mode == CREATE || mode == GROW)
157                                 break; /* b means bitmap */
158                         brief = 1;
159                         if (optarg) {
160                                 fprintf(stderr, Name ": -b cannot have any extra immediately after it, sorry.\n");
161                                 exit(2);
162                         }
163                         continue;
164
165                 case 'Y': export++;
166                         continue;
167
168                 case HomeHost:
169                         homehost = optarg;
170                         continue;
171
172                 case ':':
173                 case '?':
174                         fputs(Usage, stderr);
175                         exit(2);
176                 }
177                 /* second, figure out the mode.
178                  * Some options force the mode.  Others
179                  * set the mode if it isn't already
180                  */
181
182                 switch(opt) {
183                 case '@': /* just incase they say --manage */
184                         newmode = MANAGE;
185                         shortopt = short_bitmap_options;
186                         break;
187                 case 'a':
188                 case 'r':
189                 case 'f':
190                 case ReAdd: /* re-add */
191                         if (!mode) {
192                                 newmode = MANAGE;
193                                 shortopt = short_bitmap_options;
194                         }
195                         break;
196
197                 case 'A': newmode = ASSEMBLE; shortopt = short_bitmap_auto_options; break;
198                 case 'B': newmode = BUILD; shortopt = short_bitmap_auto_options; break;
199                 case 'C': newmode = CREATE; shortopt = short_bitmap_auto_options; break;
200                 case 'F': newmode = MONITOR;break;
201                 case 'G': newmode = GROW;
202                         shortopt = short_bitmap_options;
203                         break;
204                 case 'I': newmode = INCREMENTAL;
205                         shortopt = short_bitmap_auto_options; break;
206                 case AutoDetect:
207                         newmode = AUTODETECT; break;
208
209                 case '#':
210                 case 'D':
211                 case 'E':
212                 case 'X':
213                 case 'Q': newmode = MISC; break;
214                 case 'R':
215                 case 'S':
216                 case 'o':
217                 case 'w':
218                 case 'W':
219                 case Waitclean:
220                 case 'K': if (!mode) newmode = MISC; break;
221                 }
222                 if (mode && newmode == mode) {
223                         /* everybody happy ! */
224                 } else if (mode && newmode != mode) {
225                         /* not allowed.. */
226                         fprintf(stderr, Name ": ");
227                         if (option_index >= 0)
228                                 fprintf(stderr, "--%s", long_options[option_index].name);
229                         else
230                                 fprintf(stderr, "-%c", opt);
231                         fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
232                                 map_num(modes, newmode),
233                                 map_num(modes, mode));
234                         exit(2);
235                 } else if (!mode && newmode) {
236                         mode = newmode;
237                 } else {
238                         /* special case of -c --help */
239                         if (opt == 'c' &&
240                             ( strncmp(optarg, "--h", 3)==0 ||
241                               strncmp(optarg, "-h", 2)==0)) {
242                                 fputs(Help_config, stdout);
243                                 exit(0);
244                         }
245
246                         /* If first option is a device, don't force the mode yet */
247                         if (opt == 1) {
248                                 if (devs_found == 0) {
249                                         dv = malloc(sizeof(*dv));
250                                         if (dv == NULL) {
251                                                 fprintf(stderr, Name ": malloc failed\n");
252                                                 exit(3);
253                                         }
254                                         dv->devname = optarg;
255                                         dv->disposition = devmode;
256                                         dv->writemostly = writemostly;
257                                         dv->re_add = re_add;
258                                         dv->used = 0;
259                                         dv->content = NULL;
260                                         dv->next = NULL;
261                                         *devlistend = dv;
262                                         devlistend = &dv->next;
263
264                                         devs_found++;
265                                         continue;
266                                 }
267                                 /* No mode yet, and this is the second device ... */
268                                 fprintf(stderr, Name ": An option must be given to set the mode before a second device is listed\n");
269                                 exit(2);
270                         }
271                         if (option_index >= 0)
272                                 fprintf(stderr, Name ": --%s", long_options[option_index].name);
273                         else
274                                 fprintf(stderr, Name ": -%c", opt);
275                         fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
276                         exit(2);
277                 }
278
279                 /* if we just set the mode, then done */
280                 switch(opt) {
281                 case '@':
282                 case '#':
283                 case 'A':
284                 case 'B':
285                 case 'C':
286                 case 'F':
287                 case 'G':
288                 case 'I':
289                 case AutoDetect:
290                         continue;
291                 }
292                 if (opt == 1) {
293                         /* an undecorated option - must be a device name.
294                          */
295                         if (devs_found > 0 && mode == '@' && !devmode) {
296                                 fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
297                                 exit(2);
298                         }
299                         if (devs_found > 0 && mode == 'G' && !devmode) {
300                                 fprintf(stderr, Name ": Must give one of -a for devices do add: %s\n", optarg);
301                                 exit(2);
302                         }
303                         dv = malloc(sizeof(*dv));
304                         if (dv == NULL) {
305                                 fprintf(stderr, Name ": malloc failed\n");
306                                 exit(3);
307                         }
308                         dv->devname = optarg;
309                         dv->disposition = devmode;
310                         dv->writemostly = writemostly;
311                         dv->re_add = re_add;
312                         dv->used = 0;
313                         dv->content = NULL;
314                         dv->next = NULL;
315                         *devlistend = dv;
316                         devlistend = &dv->next;
317
318                         devs_found++;
319                         continue;
320                 }
321
322                 /* We've got a mode, and opt is now something else which
323                  * could depend on the mode */
324 #define O(a,b) ((a<<8)|b)
325                 switch (O(mode,opt)) {
326                 case O(CREATE,'c'):
327                 case O(BUILD,'c'): /* chunk or rounding */
328                         if (chunk) {
329                                 fprintf(stderr, Name ": chunk/rounding may only be specified once. "
330                                         "Second value is %s.\n", optarg);
331                                 exit(2);
332                         }
333                         chunk = strtol(optarg, &c, 10);
334                         if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) {
335                                 fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
336                                         optarg);
337                                 exit(2);
338                         }
339                         continue;
340
341                 case O(ASSEMBLE,AutoHomeHost):
342                         auto_update_home = 1;
343                         continue;
344                 case O(INCREMENTAL, 'e'):
345                 case O(CREATE,'e'):
346                 case O(ASSEMBLE,'e'):
347                 case O(MISC,'e'): /* set metadata (superblock) information */
348                         if (ss) {
349                                 fprintf(stderr, Name ": metadata information already given\n");
350                                 exit(2);
351                         }
352                         for(i=0; !ss && superlist[i]; i++)
353                                 ss = superlist[i]->match_metadata_desc(optarg);
354
355                         if (!ss) {
356                                 fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg);
357                                 exit(2);
358                         }
359                         max_disks = ss->max_devs;
360                         continue;
361
362                 case O(MANAGE,'W'):
363                 case O(BUILD,'W'):
364                 case O(CREATE,'W'):
365                         /* set write-mostly for following devices */
366                         writemostly = 1;
367                         continue;
368
369                 case O(MANAGE,'w'):
370                         /* clear write-mostly for following devices */
371                         writemostly = 2;
372                         continue;
373
374
375                 case O(GROW,'z'):
376                 case O(CREATE,'z'): /* size */
377                         if (size >= 0) {
378                                 fprintf(stderr, Name ": size may only be specified once. "
379                                         "Second value is %s.\n", optarg);
380                                 exit(2);
381                         }
382                         if (strcmp(optarg, "max")==0)
383                                 size = 0;
384                         else {
385                                 size = strtoll(optarg, &c, 10);
386                                 if (!optarg[0] || *c || size < 4) {
387                                         fprintf(stderr, Name ": invalid size: %s\n",
388                                                 optarg);
389                                         exit(2);
390                                 }
391                         }
392                         continue;
393
394                 case O(GROW,'l'): /* hack - needed to understand layout */
395                 case O(CREATE,'l'):
396                 case O(BUILD,'l'): /* set raid level*/
397                         if (level != UnSet) {
398                                 fprintf(stderr, Name ": raid level may only be set once.  "
399                                         "Second value is %s.\n", optarg);
400                                 exit(2);
401                         }
402                         level = map_name(pers, optarg);
403                         if (level == UnSet) {
404                                 fprintf(stderr, Name ": invalid raid level: %s\n",
405                                         optarg);
406                                 exit(2);
407                         }
408                         if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) {
409                                 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
410                                         optarg);
411                                 exit(2);
412                         }
413                         if (sparedisks > 0 && level < 1 && level >= -1) {
414                                 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
415                                         optarg);
416                                 exit(2);
417                         }
418                         ident.level = level;
419                         continue;
420
421                 case O(CREATE,'p'): /* raid5 layout */
422                 case O(BUILD,'p'): /* faulty layout */
423                 case O(GROW, 'p'): /* faulty reconfig */
424                         if (layout != UnSet) {
425                                 fprintf(stderr,Name ": layout may only be sent once.  "
426                                         "Second value was %s\n", optarg);
427                                 exit(2);
428                         }
429                         switch(level) {
430                         default:
431                                 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
432                                         map_num(pers, level));
433                                 exit(2);
434                         case UnSet:
435                                 fprintf(stderr, Name ": raid level must be given before layout.\n");
436                                 exit(2);
437
438                         case 5:
439                         case 6:
440                                 layout = map_name(r5layout, optarg);
441                                 if (layout==UnSet) {
442                                         fprintf(stderr, Name ": layout %s not understood for raid5.\n",
443                                                 optarg);
444                                         exit(2);
445                                 }
446                                 break;
447
448                         case 10:
449                                 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
450                                 if ((optarg[0] !=  'n' && optarg[0] != 'f' && optarg[0] != 'o') ||
451                                     (copies = strtoul(optarg+1, &cp, 10)) < 1 ||
452                                     copies > 200 ||
453                                     *cp) {
454                                         fprintf(stderr, Name ": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg);
455                                         exit(2);
456                                 }
457                                 if (optarg[0] == 'n')
458                                         layout = 256 + copies;
459                                 else if (optarg[0] == 'o')
460                                         layout = 0x10000 + (copies<<8) + 1;
461                                 else
462                                         layout = 1 + (copies<<8);
463                                 break;
464                         case -5: /* Faulty
465                                   * modeNNN
466                                   */
467
468                         {
469                                 int ln = strcspn(optarg, "0123456789");
470                                 char *m = strdup(optarg);
471                                 int mode;
472                                 m[ln] = 0;
473                                 mode = map_name(faultylayout, m);
474                                 if (mode == UnSet) {
475                                         fprintf(stderr, Name ": layout %s not understood for faulty.\n",
476                                                 optarg);
477                                         exit(2);
478                                 }
479                                 layout = mode | (atoi(optarg+ln)<< ModeShift);
480                         }
481                         }
482                         continue;
483
484                 case O(CREATE,AssumeClean):
485                 case O(BUILD,AssumeClean): /* assume clean */
486                         assume_clean = 1;
487                         continue;
488
489                 case O(GROW,'n'):
490                 case O(CREATE,'n'):
491                 case O(BUILD,'n'): /* number of raid disks */
492                         if (raiddisks) {
493                                 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
494                                         raiddisks, optarg);
495                                 exit(2);
496                         }
497                         raiddisks = strtol(optarg, &c, 10);
498                         if (!optarg[0] || *c || raiddisks<=0) {
499                                 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
500                                         optarg);
501                                 exit(2);
502                         }
503                         ident.raid_disks = raiddisks;
504                         continue;
505
506                 case O(CREATE,'x'): /* number of spare (eXtra) discs */
507                         if (sparedisks) {
508                                 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
509                                         sparedisks, optarg);
510                                 exit(2);
511                         }
512                         if (level != UnSet && level <= 0 && level >= -1) {
513                                 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
514                                         level);
515                                 exit(2);
516                         }
517                         sparedisks = strtol(optarg, &c, 10);
518                         if (!optarg[0] || *c || sparedisks < 0) {
519                                 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
520                                         optarg);
521                                 exit(2);
522                         }
523                         continue;
524
525                 case O(CREATE,'a'):
526                 case O(BUILD,'a'):
527                 case O(INCREMENTAL,'a'):
528                 case O(ASSEMBLE,'a'): /* auto-creation of device node */
529                         autof = parse_auto(optarg, "--auto flag", 0);
530                         continue;
531
532                 case O(CREATE,Symlinks):
533                 case O(BUILD,Symlinks):
534                 case O(ASSEMBLE,Symlinks): /* auto creation of symlinks in /dev to /dev/md */
535                         symlinks = optarg;
536                         continue;
537
538                 case O(BUILD,'f'): /* force honouring '-n 1' */
539                 case O(GROW,'f'): /* ditto */
540                 case O(CREATE,'f'): /* force honouring of device list */
541                 case O(ASSEMBLE,'f'): /* force assembly */
542                 case O(MISC,'f'): /* force zero */
543                         force=1;
544                         continue;
545
546                         /* now for the Assemble options */
547                 case O(CREATE,'u'): /* uuid of array */
548                 case O(ASSEMBLE,'u'): /* uuid of array */
549                         if (ident.uuid_set) {
550                                 fprintf(stderr, Name ": uuid cannot be set twice.  "
551                                         "Second value %s.\n", optarg);
552                                 exit(2);
553                         }
554                         if (parse_uuid(optarg, ident.uuid))
555                                 ident.uuid_set = 1;
556                         else {
557                                 fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
558                                 exit(2);
559                         }
560                         continue;
561
562                 case O(CREATE,'N'):
563                 case O(ASSEMBLE,'N'):
564                         if (ident.name[0]) {
565                                 fprintf(stderr, Name ": name cannot be set twice.   "
566                                         "Second value %s.\n", optarg);
567                                 exit(2);
568                         }
569                         if (strlen(optarg) > 32) {
570                                 fprintf(stderr, Name ": name '%s' is too long, 32 chars max.\n",
571                                         optarg);
572                                 exit(2);
573                         }
574                         strcpy(ident.name, optarg);
575                         continue;
576
577                 case O(ASSEMBLE,'m'): /* super-minor for array */
578                         if (ident.super_minor != UnSet) {
579                                 fprintf(stderr, Name ": super-minor cannot be set twice.  "
580                                         "Second value: %s.\n", optarg);
581                                 exit(2);
582                         }
583                         if (strcmp(optarg, "dev")==0)
584                                 ident.super_minor = -2;
585                         else {
586                                 ident.super_minor = strtoul(optarg, &cp, 10);
587                                 if (!optarg[0] || *cp) {
588                                         fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
589                                         exit(2);
590                                 }
591                         }
592                         continue;
593
594                 case O(ASSEMBLE,'U'): /* update the superblock */
595                         if (update) {
596                                 fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n",
597                                         update, optarg);
598                                 exit(2);
599                         }
600                         update = optarg;
601                         if (strcmp(update, "sparc2.2")==0)
602                                 continue;
603                         if (strcmp(update, "super-minor") == 0)
604                                 continue;
605                         if (strcmp(update, "summaries")==0)
606                                 continue;
607                         if (strcmp(update, "resync")==0)
608                                 continue;
609                         if (strcmp(update, "uuid")==0)
610                                 continue;
611                         if (strcmp(update, "name")==0)
612                                 continue;
613                         if (strcmp(update, "homehost")==0)
614                                 continue;
615                         if (strcmp(update, "devicesize")==0)
616                                 continue;
617                         if (strcmp(update, "byteorder")==0) {
618                                 if (ss) {
619                                         fprintf(stderr, Name ": must not set metadata type with --update=byteorder.\n");
620                                         exit(2);
621                                 }
622                                 for(i=0; !ss && superlist[i]; i++)
623                                         ss = superlist[i]->match_metadata_desc("0.swap");
624                                 if (!ss) {
625                                         fprintf(stderr, Name ": INTERNAL ERROR cannot find 0.swap\n");
626                                         exit(2);
627                                 }
628
629                                 continue;
630                         }
631                         if (strcmp(update,"?") == 0 ||
632                             strcmp(update, "help") == 0) {
633                                 outf = stdout;
634                                 fprintf(outf, Name ": ");
635                         } else {
636                                 outf = stderr;
637                                 fprintf(outf,
638                                         Name ": '--update=%s' is invalid.  ",
639                                         update);
640                         }
641                         fprintf(outf, "Valid --update options are:\n"
642                 "     'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
643                 "     'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
644                         exit(outf == stdout ? 0 : 2);
645
646                 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
647                         runstop = -1; /* --stop isn't allowed for --assemble,
648                                        * so we overload slightly */
649                         continue;
650
651                 case O(ASSEMBLE,'c'): /* config file */
652                 case O(INCREMENTAL, 'c'):
653                 case O(MISC, 'c'):
654                 case O(MONITOR,'c'):
655                         if (configfile) {
656                                 fprintf(stderr, Name ": configfile cannot be set twice.  "
657                                         "Second value is %s.\n", optarg);
658                                 exit(2);
659                         }
660                         configfile = optarg;
661                         set_conffile(configfile);
662                         /* FIXME possibly check that config file exists.  Even parse it */
663                         continue;
664                 case O(ASSEMBLE,'s'): /* scan */
665                 case O(MISC,'s'):
666                 case O(MONITOR,'s'):
667                 case O(INCREMENTAL,'s'):
668                         scan = 1;
669                         continue;
670
671                 case O(MONITOR,'m'): /* mail address */
672                         if (mailaddr)
673                                 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
674                                         optarg);
675                         else
676                                 mailaddr = optarg;
677                         continue;
678
679                 case O(MONITOR,'p'): /* alert program */
680                         if (program)
681                                 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
682                                         optarg);
683                         else
684                                 program = optarg;
685                         continue;
686
687                 case O(MONITOR,'d'): /* delay in seconds */
688                 case O(GROW, 'd'):
689                 case O(BUILD,'d'): /* delay for bitmap updates */
690                 case O(CREATE,'d'):
691                         if (delay)
692                                 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
693                                         optarg);
694                         else {
695                                 delay = strtol(optarg, &c, 10);
696                                 if (!optarg[0] || *c || delay<1) {
697                                         fprintf(stderr, Name ": invalid delay: %s\n",
698                                                 optarg);
699                                         exit(2);
700                                 }
701                         }
702                         continue;
703                 case O(MONITOR,'f'): /* daemonise */
704                         daemonise = 1;
705                         continue;
706                 case O(MONITOR,'i'): /* pid */
707                         if (pidfile)
708                                 fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
709                                         optarg);
710                         else
711                                 pidfile = optarg;
712                         continue;
713                 case O(MONITOR,'1'): /* oneshot */
714                         oneshot = 1;
715                         continue;
716                 case O(MONITOR,'t'): /* test */
717                         test = 1;
718                         continue;
719                 case O(MONITOR,'y'): /* log messages to syslog */
720                         openlog("mdadm", LOG_PID, SYSLOG_FACILITY);
721                         dosyslog = 1;
722                         continue;
723
724                         /* now the general management options.  Some are applicable
725                          * to other modes. None have arguments.
726                          */
727                 case O(GROW,'a'):
728                 case O(MANAGE,'a'): /* add a drive */
729                         devmode = 'a';
730                         re_add = 0;
731                         continue;
732                 case O(MANAGE,ReAdd):
733                         devmode = 'a';
734                         re_add = 1;
735                         continue;
736                 case O(MANAGE,'r'): /* remove a drive */
737                         devmode = 'r';
738                         continue;
739                 case O(MANAGE,'f'): /* set faulty */
740                         devmode = 'f';
741                         continue;
742                 case O(INCREMENTAL,'R'):
743                 case O(MANAGE,'R'):
744                 case O(ASSEMBLE,'R'):
745                 case O(BUILD,'R'):
746                 case O(CREATE,'R'): /* Run the array */
747                         if (runstop < 0) {
748                                 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
749                                 exit(2);
750                         }
751                         runstop = 1;
752                         continue;
753                 case O(MANAGE,'S'):
754                         if (runstop > 0) {
755                                 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
756                                 exit(2);
757                         }
758                         runstop = -1;
759                         continue;
760
761                 case O(MISC,'Q'):
762                 case O(MISC,'D'):
763                 case O(MISC,'E'):
764                 case O(MISC,'K'):
765                 case O(MISC,'R'):
766                 case O(MISC,'S'):
767                 case O(MISC,'X'):
768                 case O(MISC,'o'):
769                 case O(MISC,'w'):
770                 case O(MISC,'W'):
771                 case O(MISC, Waitclean):
772                         if (devmode && devmode != opt &&
773                             (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
774                                 fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n",
775                                         devmode =='E'?opt:devmode);
776                                 exit(2);
777                         }
778                         devmode = opt;
779                         continue;
780                 case O(MISC,'t'):
781                         test = 1;
782                         continue;
783
784                 case O(MISC, Sparc22):
785                         if (devmode != 'E') {
786                                 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
787                                 exit(2);
788                         }
789                         SparcAdjust = 1;
790                         continue;
791
792                 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
793                         if (!optarg) {
794                                 fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n");
795                                 exit(2);
796                         }
797                         if (strcmp(optarg, "internal")==0) {
798                                 fprintf(stderr, Name ": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
799                                 continue;
800                         }
801                         bitmap_fd = open(optarg, O_RDWR);
802                         if (!*optarg || bitmap_fd < 0) {
803                                 fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno));
804                                 exit(2);
805                         }
806                         ident.bitmap_fd = bitmap_fd; /* for Assemble */
807                         continue;
808
809                 case O(ASSEMBLE, BackupFile):
810                 case O(GROW, BackupFile):
811                         /* Specify a file into which grow might place a backup,
812                          * or from which assemble might recover a backup
813                          */
814                         if (backup_file) {
815                                 fprintf(stderr, Name ": backup file already specified, rejecting %s\n", optarg);
816                                 exit(2);
817                         }
818                         backup_file = optarg;
819                         continue;
820
821                 case O(BUILD,'b'):
822                 case O(CREATE,'b'): /* here we create the bitmap */
823                         if (strcmp(optarg, "none") == 0) {
824                                 fprintf(stderr, Name ": '--bitmap none' only"
825                                         " support for --grow\n");
826                                 exit(2);
827                         }
828                         /* FALL THROUGH */
829                 case O(GROW,'b'):
830                         if (strcmp(optarg, "internal")== 0 ||
831                             strcmp(optarg, "none")== 0 ||
832                             strchr(optarg, '/') != NULL) {
833                                 bitmap_file = optarg;
834                                 continue;
835                         }
836                         /* probable typo */
837                         fprintf(stderr, Name ": bitmap file must contain a '/', or be 'internal', or 'none'\n");
838                         exit(2);
839
840                 case O(GROW,BitmapChunk):
841                 case O(BUILD,BitmapChunk):
842                 case O(CREATE,BitmapChunk): /* bitmap chunksize */
843                         bitmap_chunk = strtol(optarg, &c, 10);
844                         if (!optarg[0] || *c || bitmap_chunk < 0 ||
845                                         bitmap_chunk & (bitmap_chunk - 1)) {
846                                 fprintf(stderr, Name ": invalid bitmap chunksize: %s\n",
847                                                 optarg);
848                                 exit(2);
849                         }
850                         /* convert K to B, chunk of 0K means 512B */
851                         bitmap_chunk = bitmap_chunk ? bitmap_chunk * 1024 : 512;
852                         continue;
853
854                 case O(GROW, WriteBehind):
855                 case O(BUILD, WriteBehind):
856                 case O(CREATE, WriteBehind): /* write-behind mode */
857                         write_behind = DEFAULT_MAX_WRITE_BEHIND;
858                         if (optarg) {
859                                 write_behind = strtol(optarg, &c, 10);
860                                 if (write_behind < 0 || *c ||
861                                     write_behind > 16383) {
862                                         fprintf(stderr, Name ": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
863                                         exit(2);
864                                 }
865                         }
866                         continue;
867
868                 case O(INCREMENTAL, 'r'):
869                         rebuild_map = 1;
870                         continue;
871                 }
872                 /* We have now processed all the valid options. Anything else is
873                  * an error
874                  */
875                 if (option_index > 0)
876                         fprintf(stderr, Name ":option --%s not valid in %s mode\n",
877                                 long_options[option_index].name,
878                                 map_num(modes, mode));
879                 else
880                         fprintf(stderr, Name ": option -%c not valid in %s mode\n",
881                                 opt, map_num(modes, mode));
882                 exit(2);
883
884         }
885
886         if (print_help) {
887                 char *help_text = Help;
888                 if (print_help == 2)
889                         help_text = OptionHelp;
890                 else
891                         switch (mode) {
892                         case ASSEMBLE : help_text = Help_assemble; break;
893                         case BUILD    : help_text = Help_build; break;
894                         case CREATE   : help_text = Help_create; break;
895                         case MANAGE   : help_text = Help_manage; break;
896                         case MISC     : help_text = Help_misc; break;
897                         case MONITOR  : help_text = Help_monitor; break;
898                         case GROW     : help_text = Help_grow; break;
899                         case INCREMENTAL:help_text= Help_incr; break;
900                         }
901                 fputs(help_text,stdout);
902                 exit(0);
903         }
904
905         if (!mode && devs_found) {
906                 mode = MISC;
907                 devmode = 'Q';
908                 if (devlist->disposition == 0)
909                         devlist->disposition = devmode;
910         }
911         if (!mode) {
912                 fputs(Usage, stderr);
913                 exit(2);
914         }
915
916         if (symlinks) {
917                 struct createinfo *ci = conf_get_create_info();
918
919                 if (strcasecmp(symlinks, "yes") == 0)
920                         ci->symlinks = 1;
921                 else if (strcasecmp(symlinks, "no") == 0)
922                         ci->symlinks = 0;
923                 else {
924                         fprintf(stderr, Name ": option --symlinks must be 'no' or 'yes'\n");
925                         exit(2);
926                 }
927         }
928         /* Ok, got the option parsing out of the way
929          * hopefully it's mostly right but there might be some stuff
930          * missing
931          *
932          * That is mosty checked in the per-mode stuff but...
933          *
934          * For @,B,C  and A without -s, the first device listed must be an md device
935          * we check that here and open it.
936          */
937
938         if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
939             (mode == ASSEMBLE && ! scan)) {
940                 if (devs_found < 1) {
941                         fprintf(stderr, Name ": an md device must be given in this mode\n");
942                         exit(2);
943                 }
944                 if ((int)ident.super_minor == -2 && autof) {
945                         fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
946                         exit(2);
947                 }
948                 if (mode == MANAGE || mode == GROW) {
949                         mdfd = open_mddev(devlist->devname, 1);
950                         if (mdfd < 0)
951                                 exit(1);
952                 } else
953                         /* non-existent device is OK */
954                         mdfd = open_mddev(devlist->devname, 0);
955                 if (mdfd == -2) {
956                         fprintf(stderr, Name ": device %s exists but is not an "
957                                 "md array.\n", devlist->devname);
958                         exit(1);
959                 }
960                 if ((int)ident.super_minor == -2) {
961                         struct stat stb;
962                         if (mdfd < 0) {
963                                 fprintf(stderr, Name ": --super-minor=dev given, and "
964                                         "listed device %s doesn't exist.\n",
965                                         devlist->devname);
966                                 exit(1);
967                         }
968                         fstat(mdfd, &stb);
969                         ident.super_minor = minor(stb.st_rdev);
970                 }
971                 if (mdfd >= 0 && mode != MANAGE && mode != GROW) {
972                         /* We don't really want this open yet, we just might
973                          * have wanted to check some things
974                          */
975                         close(mdfd);
976                         mdfd = -1;
977                 }
978         }
979
980         if (raiddisks) {
981                 if (raiddisks > max_disks) {
982                         fprintf(stderr, Name ": invalid number of raid devices: %d\n",
983                                 raiddisks);
984                         exit(2);
985                 }
986                 if (raiddisks == 1 &&  !force && level != -5) {
987                         fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
988                                 "     a mistake.  If you really mean it you will need to specify --force before\n"
989                                 "     setting the number of drives.\n");
990                         exit(2);
991                 }
992         }
993         if (sparedisks) {
994                 if ( sparedisks > max_disks - raiddisks) {
995                         fprintf(stderr, Name ": invalid number of spare-devices: %d\n",
996                                 sparedisks);
997                         exit(2);
998                 }
999         }
1000
1001         if (homehost == NULL)
1002                 homehost = conf_get_homehost();
1003         if (homehost == NULL || strcmp(homehost, "<system>")==0) {
1004                 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
1005                         sys_hostname[sizeof(sys_hostname)-1] = 0;
1006                         homehost = sys_hostname;
1007                 }
1008         }
1009
1010         ident.autof = autof;
1011
1012         rv = 0;
1013         switch(mode) {
1014         case MANAGE:
1015                 /* readonly, add/remove, readwrite, runstop */
1016                 if (readonly>0)
1017                         rv = Manage_ro(devlist->devname, mdfd, readonly);
1018                 if (!rv && devs_found>1)
1019                         rv = Manage_subdevs(devlist->devname, mdfd,
1020                                             devlist->next, verbose-quiet);
1021                 if (!rv && readonly < 0)
1022                         rv = Manage_ro(devlist->devname, mdfd, readonly);
1023                 if (!rv && runstop)
1024                         rv = Manage_runstop(devlist->devname, mdfd, runstop, quiet);
1025                 break;
1026         case ASSEMBLE:
1027                 if (devs_found == 1 && ident.uuid_set == 0 &&
1028                     ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) {
1029                         /* Only a device has been given, so get details from config file */
1030                         mddev_ident_t array_ident = conf_get_ident(devlist->devname);
1031                         if (array_ident == NULL) {
1032                                 fprintf(stderr, Name ": %s not identified in config file.\n",
1033                                         devlist->devname);
1034                                 rv |= 1;
1035                                 if (mdfd >= 0)
1036                                         close(mdfd);
1037                         } else {
1038                                 if (array_ident->autof == 0)
1039                                         array_ident->autof = autof;
1040                                 rv |= Assemble(ss, devlist->devname, array_ident,
1041                                                NULL, backup_file,
1042                                                readonly, runstop, update, homehost, verbose-quiet, force);
1043                         }
1044                 } else if (!scan)
1045                         rv = Assemble(ss, devlist->devname, &ident,
1046                                       devlist->next, backup_file,
1047                                       readonly, runstop, update, homehost, verbose-quiet, force);
1048                 else if (devs_found>0) {
1049                         if (update && devs_found > 1) {
1050                                 fprintf(stderr, Name ": can only update a single array at a time\n");
1051                                 exit(1);
1052                         }
1053                         if (backup_file && devs_found > 1) {
1054                                 fprintf(stderr, Name ": can only assemble a single array when providing a backup file.\n");
1055                                 exit(1);
1056                         }
1057                         for (dv = devlist ; dv ; dv=dv->next) {
1058                                 mddev_ident_t array_ident = conf_get_ident(dv->devname);
1059                                 if (array_ident == NULL) {
1060                                         fprintf(stderr, Name ": %s not identified in config file.\n",
1061                                                 dv->devname);
1062                                         rv |= 1;
1063                                         continue;
1064                                 }
1065                                 if (array_ident->autof == 0)
1066                                         array_ident->autof = autof;
1067                                 rv |= Assemble(ss, dv->devname, array_ident,
1068                                                NULL, backup_file,
1069                                                readonly, runstop, update, homehost, verbose-quiet, force);
1070                         }
1071                 } else {
1072                         mddev_ident_t array_list =  conf_get_ident(NULL);
1073                         mddev_dev_t devlist = conf_get_devs();
1074                         int cnt = 0;
1075                         if (devlist == NULL) {
1076                                 fprintf(stderr, Name ": No devices listed in conf file were found.\n");
1077                                 exit(1);
1078                         }
1079                         if (update) {
1080                                 fprintf(stderr, Name ": --update not meaningful with a --scan assembly.\n");
1081                                 exit(1);
1082                         }
1083                         if (backup_file) {
1084                                 fprintf(stderr, Name ": --backup_file not meaningful with a --scan assembly.\n");
1085                                 exit(1);
1086                         }
1087                         for (; array_list; array_list = array_list->next) {
1088                                 if (array_list->autof == 0)
1089                                         array_list->autof = autof;
1090                                 
1091                                 rv |= Assemble(ss, array_list->devname,
1092                                                array_list,
1093                                                NULL, NULL,
1094                                                readonly, runstop, NULL, homehost, verbose-quiet, force);
1095                                 if (rv == 0) cnt++;
1096                         }
1097                         if (homehost) {
1098                                 /* Maybe we can auto-assemble something.
1099                                  * Repeatedly call Assemble in auto-assemble mode
1100                                  * until it fails
1101                                  */
1102                                 int rv2;
1103                                 int acnt;
1104                                 ident.autof = autof;
1105                                 do {
1106                                         mddev_dev_t devlist = conf_get_devs();
1107                                         acnt = 0;
1108                                         do {
1109                                                 rv2 = Assemble(ss, NULL,
1110                                                                &ident,
1111                                                                devlist, NULL,
1112                                                                readonly, runstop, NULL, homehost, verbose-quiet, force);
1113                                                 if (rv2==0) {
1114                                                         cnt++;
1115                                                         acnt++;
1116                                                 }
1117                                                 if (rv2 == 1)
1118                                                         /* found something so even though assembly failed  we
1119                                                          * want to avoid auto-updates
1120                                                          */
1121                                                         auto_update_home = 0;
1122                                         } while (rv2!=2);
1123                                         /* Incase there are stacked devices, we need to go around again */
1124                                 } while (acnt);
1125                                 if (cnt == 0 && auto_update_home && homehost) {
1126                                         /* Nothing found, maybe we need to bootstrap homehost info */
1127                                         do {
1128                                                 acnt = 0;
1129                                                 do {
1130                                                         rv2 = Assemble(ss, NULL,
1131                                                                        &ident,
1132                                                                        NULL, NULL,
1133                                                                        readonly, runstop, "homehost", homehost, verbose-quiet, force);
1134                                                         if (rv2==0) {
1135                                                                 cnt++;
1136                                                                 acnt++;
1137                                                         }
1138                                                 } while (rv2!=2);
1139                                                 /* Incase there are stacked devices, we need to go around again */
1140                                         } while (acnt);
1141                                 }
1142                                 if (cnt == 0 && rv == 0) {
1143                                         fprintf(stderr, Name ": No arrays found in config file or automatically\n");
1144                                         rv = 1;
1145                                 } else if (cnt)
1146                                         rv = 0;
1147                         } else if (cnt == 0 && rv == 0) {
1148                                 fprintf(stderr, Name ": No arrays found in config file\n");
1149                                 rv = 1;
1150                         }
1151                 }
1152                 break;
1153         case BUILD:
1154                 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1155                 if (write_behind && !bitmap_file) {
1156                         fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1157                         rv = 1;
1158                         break;
1159                 }
1160                 if (raiddisks == 0) {
1161                         fprintf(stderr, Name ": no raid-devices specified.\n");
1162                         rv = 1;
1163                         break;
1164                 }
1165
1166                 if (bitmap_file) {
1167                         if (strcmp(bitmap_file, "internal")==0) {
1168                                 fprintf(stderr, Name ": 'internal' bitmaps not supported with --build\n");
1169                                 rv |= 1;
1170                                 break;
1171                         }
1172                 }
1173                 rv = Build(devlist->devname, chunk, level, layout,
1174                            raiddisks, devlist->next, assume_clean,
1175                            bitmap_file, bitmap_chunk, write_behind,
1176                            delay, verbose-quiet, autof);
1177                 break;
1178         case CREATE:
1179                 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1180                 if (write_behind && !bitmap_file) {
1181                         fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1182                         rv = 1;
1183                         break;
1184                 }
1185                 if (raiddisks == 0) {
1186                         fprintf(stderr, Name ": no raid-devices specified.\n");
1187                         rv = 1;
1188                         break;
1189                 }
1190
1191                 rv = Create(ss, devlist->devname, chunk, level, layout, size<0 ? 0 : size,
1192                             raiddisks, sparedisks, ident.name, homehost,
1193                             ident.uuid_set ? ident.uuid : NULL,
1194                             devs_found-1, devlist->next, runstop, verbose-quiet, force, assume_clean,
1195                             bitmap_file, bitmap_chunk, write_behind, delay, autof);
1196                 break;
1197         case MISC:
1198                 if (devmode == 'E') {
1199                         if (devlist == NULL && !scan) {
1200                                 fprintf(stderr, Name ": No devices to examine\n");
1201                                 exit(2);
1202                         }
1203                         if (devlist == NULL)
1204                                 devlist = conf_get_devs();
1205                         if (devlist == NULL) {
1206                                 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
1207                                 exit(1);
1208                         }
1209                         if (brief && verbose)
1210                                 brief = 2;
1211                         rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief,
1212                                      export, scan,
1213                                      SparcAdjust, ss, homehost);
1214                 } else {
1215                         if (devlist == NULL) {
1216                                 if (devmode=='D' && scan) {
1217                                         /* apply --detail to all devices in /proc/mdstat */
1218                                         struct mdstat_ent *ms = mdstat_read(0, 1);
1219                                         struct mdstat_ent *e;
1220                                         for (e=ms ; e ; e=e->next) {
1221                                                 char *name = get_md_name(e->devnum);
1222
1223                                                 if (!name) {
1224                                                         fprintf(stderr, Name ": cannot find device file for %s\n",
1225                                                                 e->dev);
1226                                                         continue;
1227                                                 }
1228                                                 rv |= Detail(name, verbose>1?0:verbose+1,
1229                                                              export, test, homehost);
1230                                                 put_md_name(name);
1231                                         }
1232                                 } else  if (devmode == 'S' && scan) {
1233                                         /* apply --stop to all devices in /proc/mdstat */
1234                                         /* Due to possible stacking of devices, repeat until
1235                                          * nothing more can be stopped
1236                                          */
1237                                         int progress=1, err;
1238                                         int last = 0;
1239                                         do {
1240                                                 struct mdstat_ent *ms = mdstat_read(0, 0);
1241                                                 struct mdstat_ent *e;
1242
1243                                                 if (!progress) last = 1;
1244                                                 progress = 0; err = 0;
1245                                                 for (e=ms ; e ; e=e->next) {
1246                                                         char *name = get_md_name(e->devnum);
1247
1248                                                         if (!name) {
1249                                                                 fprintf(stderr, Name ": cannot find device file for %s\n",
1250                                                                         e->dev);
1251                                                                 continue;
1252                                                         }
1253                                                         mdfd = open_mddev(name, 1);
1254                                                         if (mdfd >= 0) {
1255                                                                 if (Manage_runstop(name, mdfd, -1, quiet?1:last?0:-1))
1256                                                                         err = 1;
1257                                                                 else
1258                                                                         progress = 1;
1259                                                                 close(mdfd);
1260                                                         }
1261
1262                                                         put_md_name(name);
1263                                                 }
1264                                         } while (!last && err);
1265                                         if (err) rv |= 1;
1266                                 } else {
1267                                         fprintf(stderr, Name ": No devices given.\n");
1268                                         exit(2);
1269                                 }
1270                         }
1271                         for (dv=devlist ; dv; dv=dv->next) {
1272                                 switch(dv->disposition) {
1273                                 case 'D':
1274                                         rv |= Detail(dv->devname,
1275                                                      brief?1+verbose:0,
1276                                                      export, test, homehost);
1277                                         continue;
1278                                 case 'K': /* Zero superblock */
1279                                         rv |= Kill(dv->devname, force, quiet,0);
1280                                         continue;
1281                                 case 'Q':
1282                                         rv |= Query(dv->devname); continue;
1283                                 case 'X':
1284                                         rv |= ExamineBitmap(dv->devname, brief, ss); continue;
1285                                 case 'W':
1286                                         rv |= Wait(dv->devname); continue;
1287                                 case Waitclean:
1288                                         rv |= WaitClean(dv->devname, verbose-quiet); continue;
1289                                 }
1290                                 mdfd = open_mddev(dv->devname, 1);
1291                                 if (mdfd>=0) {
1292                                         switch(dv->disposition) {
1293                                         case 'R':
1294                                                 rv |= Manage_runstop(dv->devname, mdfd, 1, quiet); break;
1295                                         case 'S':
1296                                                 rv |= Manage_runstop(dv->devname, mdfd, -1, quiet); break;
1297                                         case 'o':
1298                                                 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1299                                         case 'w':
1300                                                 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1301                                         }
1302                                         close(mdfd);
1303                                 } else
1304                                         rv |= 1;
1305                         }
1306                 }
1307                 break;
1308         case MONITOR:
1309                 if (!devlist && !scan) {
1310                         fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
1311                         rv = 1;
1312                         break;
1313                 }
1314                 if (pidfile && !daemonise) {
1315                         fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
1316                         rv = 1;
1317                         break;
1318                 }
1319                 rv= Monitor(devlist, mailaddr, program,
1320                             delay?delay:60, daemonise, scan, oneshot,
1321                             dosyslog, test, pidfile);
1322                 break;
1323
1324         case GROW:
1325                 if (devs_found > 1) {
1326
1327                         /* must be '-a'. */
1328                         if (size >= 0 || raiddisks) {
1329                                 fprintf(stderr, Name ": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1330                                 rv = 1;
1331                                 break;
1332                         }
1333                         for (dv=devlist->next; dv ; dv=dv->next) {
1334                                 rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
1335                                 if (rv)
1336                                         break;
1337                         }
1338                 } else if ((size >= 0) + (raiddisks != 0) +  (layout != UnSet) + (bitmap_file != NULL)> 1) {
1339                         fprintf(stderr, Name ": can change at most one of size, raiddisks, bitmap, and layout\n");
1340                         rv = 1;
1341                         break;
1342                 } else if (layout != UnSet)
1343                         rv = Manage_reconfig(devlist->devname, mdfd, layout);
1344                 else if (size >= 0 || raiddisks)
1345                         rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file,
1346                                           size, level, layout, chunk, raiddisks);
1347                 else if (bitmap_file) {
1348                         if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1349                         rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file,
1350                                             bitmap_chunk, delay, write_behind, force);
1351                 } else
1352                         fprintf(stderr, Name ": no changes to --grow\n");
1353                 break;
1354         case INCREMENTAL:
1355                 if (rebuild_map) {
1356                         RebuildMap();
1357                 }
1358                 if (scan) {
1359                         if (runstop <= 0) {
1360                                 fprintf(stderr, Name
1361                          ": --incremental --scan meaningless without --run.\n");
1362                                 break;
1363                         }
1364                         rv = IncrementalScan(verbose);
1365                 }
1366                 if (!devlist) {
1367                         if (!rebuild_map && !scan) {
1368                                 fprintf(stderr, Name
1369                                         ": --incremental requires a device.\n");
1370                                 rv = 1;
1371                         }
1372                         break;
1373                 }
1374                 if (devlist->next) {
1375                         fprintf(stderr, Name
1376                                ": --incremental can only handle one device.\n");
1377                         rv = 1;
1378                         break;
1379                 }
1380                 rv = Incremental(devlist->devname, verbose-quiet, runstop,
1381                                  ss, homehost, autof);
1382                 break;
1383         case AUTODETECT:
1384                 autodetect();
1385                 break;
1386         }
1387         exit(rv);
1388 }