Set home-cluster while creating an array
[thirdparty/mdadm.git] / mdadm.c
1 /*
2  * mdadm - manage Linux "md" devices aka RAID arrays.
3  *
4  * Copyright (C) 2001-2013 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@suse.de>
23  *
24  *    Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
25  *    Paul Clements, SteelEye Technology, Inc.
26  */
27
28 #include "mdadm.h"
29 #include "md_p.h"
30 #include <ctype.h>
31
32 static int scan_assemble(struct supertype *ss,
33                          struct context *c,
34                          struct mddev_ident *ident);
35 static int misc_scan(char devmode, struct context *c);
36 static int stop_scan(int verbose);
37 static int misc_list(struct mddev_dev *devlist,
38                      struct mddev_ident *ident,
39                      char *dump_directory,
40                      struct supertype *ss, struct context *c);
41 const char Name[] = "mdadm";
42
43 int main(int argc, char *argv[])
44 {
45         int mode = 0;
46         int opt;
47         int option_index;
48         int rv;
49         int i;
50
51         unsigned long long array_size = 0;
52         unsigned long long data_offset = INVALID_SECTORS;
53         struct mddev_ident ident;
54         char *configfile = NULL;
55         int devmode = 0;
56         int bitmap_fd = -1;
57         struct mddev_dev *devlist = NULL;
58         struct mddev_dev **devlistend = & devlist;
59         struct mddev_dev *dv;
60         int devs_found = 0;
61         char *symlinks = NULL;
62         int grow_continue = 0;
63         /* autof indicates whether and how to create device node.
64          * bottom 3 bits are style.  Rest (when shifted) are number of parts
65          * 0  - unset
66          * 1  - don't create (no)
67          * 2  - if is_standard, then create (yes)
68          * 3  - create as 'md' - reject is_standard mdp (md)
69          * 4  - create as 'mdp' - reject is_standard md (mdp)
70          * 5  - default to md if not is_standard (md in config file)
71          * 6  - default to mdp if not is_standard (part, or mdp in config file)
72          */
73         struct context c = {
74                 .require_homehost = 1,
75         };
76         struct shape s = {
77                 .level          = UnSet,
78                 .layout         = UnSet,
79                 .bitmap_chunk   = UnSet,
80         };
81
82         char sys_hostname[256];
83         char *mailaddr = NULL;
84         char *program = NULL;
85         int increments = 20;
86         int daemonise = 0;
87         char *pidfile = NULL;
88         int oneshot = 0;
89         int spare_sharing = 1;
90         struct supertype *ss = NULL;
91         int writemostly = 0;
92         char *shortopt = short_options;
93         int dosyslog = 0;
94         int rebuild_map = 0;
95         char *remove_path = NULL;
96         char *udev_filename = NULL;
97         char *dump_directory = NULL;
98
99         int print_help = 0;
100         FILE *outf;
101
102         int mdfd = -1;
103
104         srandom(time(0) ^ getpid());
105
106         ident.uuid_set=0;
107         ident.level = UnSet;
108         ident.raid_disks = UnSet;
109         ident.super_minor= UnSet;
110         ident.devices=0;
111         ident.spare_group = NULL;
112         ident.autof = 0;
113         ident.st = NULL;
114         ident.bitmap_fd = -1;
115         ident.bitmap_file = NULL;
116         ident.name[0] = 0;
117         ident.container = NULL;
118         ident.member = NULL;
119
120         while ((option_index = -1) ,
121                (opt=getopt_long(argc, argv,
122                                 shortopt, long_options,
123                                 &option_index)) != -1) {
124                 int newmode = mode;
125                 /* firstly, some mode-independent options */
126                 switch(opt) {
127                 case HelpOptions:
128                         print_help = 2;
129                         continue;
130                 case 'h':
131                         print_help = 1;
132                         continue;
133
134                 case 'V':
135                         fputs(Version, stderr);
136                         exit(0);
137
138                 case 'v': c.verbose++;
139                         continue;
140
141                 case 'q': c.verbose--;
142                         continue;
143
144                 case 'b':
145                         if (mode == ASSEMBLE || mode == BUILD || mode == CREATE
146                             || mode == GROW || mode == INCREMENTAL
147                             || mode == MANAGE)
148                                 break; /* b means bitmap */
149                 case Brief:
150                         c.brief = 1;
151                         continue;
152
153                 case 'Y': c.export++;
154                         continue;
155
156                 case HomeHost:
157                         if (strcasecmp(optarg, "<ignore>") == 0)
158                                 c.require_homehost = 0;
159                         else
160                                 c.homehost = optarg;
161                         continue;
162
163                 case OffRootOpt:
164                         /* Silently ignore old option */
165                         continue;
166
167                 case Prefer:
168                         if (c.prefer)
169                                 free(c.prefer);
170                         if (asprintf(&c.prefer, "/%s/", optarg) <= 0)
171                                 c.prefer = NULL;
172                         continue;
173
174                 case ':':
175                 case '?':
176                         fputs(Usage, stderr);
177                         exit(2);
178                 }
179                 /* second, figure out the mode.
180                  * Some options force the mode.  Others
181                  * set the mode if it isn't already
182                  */
183
184                 switch(opt) {
185                 case ManageOpt:
186                         newmode = MANAGE;
187                         shortopt = short_bitmap_options;
188                         break;
189                 case 'a':
190                 case Add:
191                 case AddSpare:
192                 case 'r':
193                 case Remove:
194                 case Replace:
195                 case With:
196                 case 'f':
197                 case Fail:
198                 case ReAdd: /* re-add */
199                         if (!mode) {
200                                 newmode = MANAGE;
201                                 shortopt = short_bitmap_options;
202                         }
203                         break;
204
205                 case 'A': newmode = ASSEMBLE;
206                         shortopt = short_bitmap_auto_options;
207                         break;
208                 case 'B': newmode = BUILD;
209                         shortopt = short_bitmap_auto_options;
210                         break;
211                 case 'C': newmode = CREATE;
212                         shortopt = short_bitmap_auto_options;
213                         break;
214                 case 'F': newmode = MONITOR;
215                         break;
216                 case 'G': newmode = GROW;
217                         shortopt = short_bitmap_options;
218                         break;
219                 case 'I': newmode = INCREMENTAL;
220                         shortopt = short_bitmap_auto_options;
221                         break;
222                 case AutoDetect:
223                         newmode = AUTODETECT;
224                         break;
225
226                 case MiscOpt:
227                 case 'D':
228                 case 'E':
229                 case 'X':
230                 case 'Q':
231                 case ExamineBB:
232                 case Dump:
233                 case Restore:
234                 case Action:
235                         newmode = MISC;
236                         break;
237
238                 case 'R':
239                 case 'S':
240                 case 'o':
241                 case 'w':
242                 case 'W':
243                 case WaitOpt:
244                 case Waitclean:
245                 case DetailPlatform:
246                 case KillSubarray:
247                 case UpdateSubarray:
248                 case UdevRules:
249                 case KillOpt:
250                         if (!mode)
251                                 newmode = MISC;
252                         break;
253
254                 case NoSharing:
255                         newmode = MONITOR;
256                         break;
257                 }
258                 if (mode && newmode == mode) {
259                         /* everybody happy ! */
260                 } else if (mode && newmode != mode) {
261                         /* not allowed.. */
262                         pr_err("");
263                         if (option_index >= 0)
264                                 fprintf(stderr, "--%s", long_options[option_index].name);
265                         else
266                                 fprintf(stderr, "-%c", opt);
267                         fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
268                                 map_num(modes, newmode),
269                                 map_num(modes, mode));
270                         exit(2);
271                 } else if (!mode && newmode) {
272                         mode = newmode;
273                         if (mode == MISC && devs_found) {
274                                 pr_err("No action given for %s in --misc mode\n",
275                                         devlist->devname);
276                                 cont_err("Action options must come before device names\n");
277                                 exit(2);
278                         }
279                 } else {
280                         /* special case of -c --help */
281                         if ((opt == 'c' || opt == ConfigFile) &&
282                             ( strncmp(optarg, "--h", 3)==0 ||
283                               strncmp(optarg, "-h", 2)==0)) {
284                                 fputs(Help_config, stdout);
285                                 exit(0);
286                         }
287
288                         /* If first option is a device, don't force the mode yet */
289                         if (opt == 1) {
290                                 if (devs_found == 0) {
291                                         dv = xmalloc(sizeof(*dv));
292                                         dv->devname = optarg;
293                                         dv->disposition = devmode;
294                                         dv->writemostly = writemostly;
295                                         dv->used = 0;
296                                         dv->next = NULL;
297                                         *devlistend = dv;
298                                         devlistend = &dv->next;
299
300                                         devs_found++;
301                                         continue;
302                                 }
303                                 /* No mode yet, and this is the second device ... */
304                                 pr_err("An option must be given to set the mode before a second device\n"
305                                         "       (%s) is listed\n", optarg);
306                                 exit(2);
307                         }
308                         if (option_index >= 0)
309                                 pr_err("--%s", long_options[option_index].name);
310                         else
311                                 pr_err("-%c", opt);
312                         fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
313                         exit(2);
314                 }
315
316                 /* if we just set the mode, then done */
317                 switch(opt) {
318                 case ManageOpt:
319                 case MiscOpt:
320                 case 'A':
321                 case 'B':
322                 case 'C':
323                 case 'F':
324                 case 'G':
325                 case 'I':
326                 case AutoDetect:
327                         continue;
328                 }
329                 if (opt == 1) {
330                         /* an undecorated option - must be a device name.
331                          */
332
333                         if (devs_found > 0 && devmode == DetailPlatform) {
334                                 pr_err("controller may only be specified once. %s ignored\n",
335                                                 optarg);
336                                 continue;
337                         }
338
339                         if (devs_found > 0 && mode == MANAGE && !devmode) {
340                                 pr_err("Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
341                                 exit(2);
342                         }
343                         if (devs_found > 0 && mode == GROW && !devmode) {
344                                 pr_err("Must give -a/--add for devices to add: %s\n", optarg);
345                                 exit(2);
346                         }
347                         dv = xmalloc(sizeof(*dv));
348                         dv->devname = optarg;
349                         dv->disposition = devmode;
350                         dv->writemostly = writemostly;
351                         dv->used = 0;
352                         dv->next = NULL;
353                         *devlistend = dv;
354                         devlistend = &dv->next;
355
356                         devs_found++;
357                         continue;
358                 }
359
360                 /* We've got a mode, and opt is now something else which
361                  * could depend on the mode */
362 #define O(a,b) ((a<<16)|b)
363                 switch (O(mode,opt)) {
364                 case O(GROW,'c'):
365                 case O(GROW,ChunkSize):
366                 case O(CREATE,'c'):
367                 case O(CREATE,ChunkSize):
368                 case O(BUILD,'c'): /* chunk or rounding */
369                 case O(BUILD,ChunkSize): /* chunk or rounding */
370                         if (s.chunk) {
371                                 pr_err("chunk/rounding may only be specified once. Second value is %s.\n", optarg);
372                                 exit(2);
373                         }
374                         s.chunk = parse_size(optarg);
375                         if (s.chunk == INVALID_SECTORS ||
376                             s.chunk < 8 || (s.chunk&1)) {
377                                 pr_err("invalid chunk/rounding value: %s\n",
378                                         optarg);
379                                 exit(2);
380                         }
381                         /* Convert sectors to K */
382                         s.chunk /= 2;
383                         continue;
384
385                 case O(INCREMENTAL, 'e'):
386                 case O(CREATE,'e'):
387                 case O(ASSEMBLE,'e'):
388                 case O(MISC,'e'): /* set metadata (superblock) information */
389                         if (ss) {
390                                 pr_err("metadata information already given\n");
391                                 exit(2);
392                         }
393                         for(i=0; !ss && superlist[i]; i++)
394                                 ss = superlist[i]->match_metadata_desc(optarg);
395
396                         if (!ss) {
397                                 pr_err("unrecognised metadata identifier: %s\n", optarg);
398                                 exit(2);
399                         }
400                         continue;
401
402                 case O(MANAGE,'W'):
403                 case O(MANAGE,WriteMostly):
404                 case O(BUILD,'W'):
405                 case O(BUILD,WriteMostly):
406                 case O(CREATE,'W'):
407                 case O(CREATE,WriteMostly):
408                         /* set write-mostly for following devices */
409                         writemostly = 1;
410                         continue;
411
412                 case O(MANAGE,'w'):
413                         /* clear write-mostly for following devices */
414                         writemostly = 2;
415                         continue;
416
417                 case O(GROW,'z'):
418                 case O(CREATE,'z'):
419                 case O(BUILD,'z'): /* size */
420                         if (s.size > 0) {
421                                 pr_err("size may only be specified once. Second value is %s.\n", optarg);
422                                 exit(2);
423                         }
424                         if (strcmp(optarg, "max")==0)
425                                 s.size = MAX_SIZE;
426                         else {
427                                 s.size = parse_size(optarg);
428                                 if (s.size == INVALID_SECTORS ||
429                                     s.size < 8) {
430                                         pr_err("invalid size: %s\n",
431                                                 optarg);
432                                         exit(2);
433                                 }
434                                 /* convert sectors to K */
435                                 s.size /= 2;
436                         }
437                         continue;
438
439                 case O(GROW,'Z'): /* array size */
440                         if (array_size > 0) {
441                                 pr_err("array-size may only be specified once. Second value is %s.\n", optarg);
442                                 exit(2);
443                         }
444                         if (strcmp(optarg, "max") == 0)
445                                 array_size = MAX_SIZE;
446                         else {
447                                 array_size = parse_size(optarg);
448                                 if (array_size == 0 ||
449                                     array_size == INVALID_SECTORS) {
450                                         pr_err("invalid array size: %s\n",
451                                                 optarg);
452                                         exit(2);
453                                 }
454                         }
455                         continue;
456
457                 case O(CREATE,DataOffset):
458                 case O(GROW,DataOffset):
459                         if (data_offset != INVALID_SECTORS) {
460                                 pr_err("data-offset may only be specified one. Second value is %s.\n", optarg);
461                                 exit(2);
462                         }
463                         if (mode == CREATE &&
464                             strcmp(optarg, "variable") == 0)
465                                 data_offset = VARIABLE_OFFSET;
466                         else
467                                 data_offset = parse_size(optarg);
468                         if (data_offset == INVALID_SECTORS) {
469                                 pr_err("invalid data-offset: %s\n",
470                                         optarg);
471                                 exit(2);
472                         }
473                         continue;
474
475                 case O(GROW,'l'):
476                 case O(CREATE,'l'):
477                 case O(BUILD,'l'): /* set raid level*/
478                         if (s.level != UnSet) {
479                                 pr_err("raid level may only be set once.  Second value is %s.\n", optarg);
480                                 exit(2);
481                         }
482                         s.level = map_name(pers, optarg);
483                         if (s.level == UnSet) {
484                                 pr_err("invalid raid level: %s\n",
485                                         optarg);
486                                 exit(2);
487                         }
488                         if (s.level != 0 && s.level != LEVEL_LINEAR && s.level != 1 &&
489                             s.level != LEVEL_MULTIPATH && s.level != LEVEL_FAULTY &&
490                             s.level != 10 &&
491                             mode == BUILD) {
492                                 pr_err("Raid level %s not permitted with --build.\n",
493                                         optarg);
494                                 exit(2);
495                         }
496                         if (s.sparedisks > 0 && s.level < 1 && s.level >= -1) {
497                                 pr_err("raid level %s is incompatible with spare-devices setting.\n",
498                                         optarg);
499                                 exit(2);
500                         }
501                         ident.level = s.level;
502                         continue;
503
504                 case O(GROW, 'p'): /* new layout */
505                 case O(GROW, Layout):
506                         if (s.layout_str) {
507                                 pr_err("layout may only be sent once.  Second value was %s\n", optarg);
508                                 exit(2);
509                         }
510                         s.layout_str = optarg;
511                         /* 'Grow' will parse the value */
512                         continue;
513
514                 case O(CREATE,'p'): /* raid5 layout */
515                 case O(CREATE,Layout):
516                 case O(BUILD,'p'): /* faulty layout */
517                 case O(BUILD,Layout):
518                         if (s.layout != UnSet) {
519                                 pr_err("layout may only be sent once.  Second value was %s\n", optarg);
520                                 exit(2);
521                         }
522                         switch(s.level) {
523                         default:
524                                 pr_err("layout not meaningful for %s arrays.\n",
525                                         map_num(pers, s.level));
526                                 exit(2);
527                         case UnSet:
528                                 pr_err("raid level must be given before layout.\n");
529                                 exit(2);
530
531                         case 5:
532                                 s.layout = map_name(r5layout, optarg);
533                                 if (s.layout==UnSet) {
534                                         pr_err("layout %s not understood for raid5.\n",
535                                                 optarg);
536                                         exit(2);
537                                 }
538                                 break;
539                         case 6:
540                                 s.layout = map_name(r6layout, optarg);
541                                 if (s.layout==UnSet) {
542                                         pr_err("layout %s not understood for raid6.\n",
543                                                 optarg);
544                                         exit(2);
545                                 }
546                                 break;
547
548                         case 10:
549                                 s.layout = parse_layout_10(optarg);
550                                 if (s.layout < 0) {
551                                         pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg);
552                                         exit(2);
553                                 }
554                                 break;
555                         case LEVEL_FAULTY:
556                                 /* Faulty
557                                  * modeNNN
558                                  */
559                                 s.layout = parse_layout_faulty(optarg);
560                                 if (s.layout == -1) {
561                                         pr_err("layout %s not understood for faulty.\n",
562                                                 optarg);
563                                         exit(2);
564                                 }
565                                 break;
566                         }
567                         continue;
568
569                 case O(CREATE,AssumeClean):
570                 case O(BUILD,AssumeClean): /* assume clean */
571                 case O(GROW,AssumeClean):
572                         s.assume_clean = 1;
573                         continue;
574
575                 case O(GROW,'n'):
576                 case O(CREATE,'n'):
577                 case O(BUILD,'n'): /* number of raid disks */
578                         if (s.raiddisks) {
579                                 pr_err("raid-devices set twice: %d and %s\n",
580                                         s.raiddisks, optarg);
581                                 exit(2);
582                         }
583                         s.raiddisks = parse_num(optarg);
584                         if (s.raiddisks <= 0) {
585                                 pr_err("invalid number of raid devices: %s\n",
586                                         optarg);
587                                 exit(2);
588                         }
589                         ident.raid_disks = s.raiddisks;
590                         continue;
591                 case O(CREATE, Nodes):
592                         c.nodes = parse_num(optarg);
593                         if (c.nodes <= 0) {
594                                 pr_err("invalid number for the number of cluster nodes: %s\n",
595                                         optarg);
596                                 exit(2);
597                         }
598                         continue;
599                 case O(CREATE, ClusterName):
600                         c.homecluster = optarg;
601                         if (strlen(c.homecluster) > 64) {
602                                 pr_err("Cluster name too big.\n");
603                                 exit(ERANGE);
604                         }
605                         continue;
606                 case O(CREATE,'x'): /* number of spare (eXtra) disks */
607                         if (s.sparedisks) {
608                                 pr_err("spare-devices set twice: %d and %s\n",
609                                        s.sparedisks, optarg);
610                                 exit(2);
611                         }
612                         if (s.level != UnSet && s.level <= 0 && s.level >= -1) {
613                                 pr_err("spare-devices setting is incompatible with raid level %d\n",
614                                         s.level);
615                                 exit(2);
616                         }
617                         s.sparedisks = parse_num(optarg);
618                         if (s.sparedisks < 0) {
619                                 pr_err("invalid number of spare-devices: %s\n",
620                                         optarg);
621                                 exit(2);
622                         }
623                         continue;
624
625                 case O(CREATE,'a'):
626                 case O(CREATE,Auto):
627                 case O(BUILD,'a'):
628                 case O(BUILD,Auto):
629                 case O(INCREMENTAL,'a'):
630                 case O(INCREMENTAL,Auto):
631                 case O(ASSEMBLE,'a'):
632                 case O(ASSEMBLE,Auto): /* auto-creation of device node */
633                         c.autof = parse_auto(optarg, "--auto flag", 0);
634                         continue;
635
636                 case O(CREATE,Symlinks):
637                 case O(BUILD,Symlinks):
638                 case O(ASSEMBLE,Symlinks): /* auto creation of symlinks in /dev to /dev/md */
639                         symlinks = optarg;
640                         continue;
641
642                 case O(BUILD,'f'): /* force honouring '-n 1' */
643                 case O(BUILD,Force): /* force honouring '-n 1' */
644                 case O(GROW,'f'): /* ditto */
645                 case O(GROW,Force): /* ditto */
646                 case O(CREATE,'f'): /* force honouring of device list */
647                 case O(CREATE,Force): /* force honouring of device list */
648                 case O(ASSEMBLE,'f'): /* force assembly */
649                 case O(ASSEMBLE,Force): /* force assembly */
650                 case O(MISC,'f'): /* force zero */
651                 case O(MISC,Force): /* force zero */
652                 case O(MANAGE,Force): /* add device which is too large */
653                         c.force=1;
654                         continue;
655                         /* now for the Assemble options */
656                 case O(ASSEMBLE, FreezeReshape):   /* Freeze reshape during
657                                                     * initrd phase */
658                 case O(INCREMENTAL, FreezeReshape):
659                         c.freeze_reshape = 1;
660                         continue;
661                 case O(CREATE,'u'): /* uuid of array */
662                 case O(ASSEMBLE,'u'): /* uuid of array */
663                         if (ident.uuid_set) {
664                                 pr_err("uuid cannot be set twice.  Second value %s.\n", optarg);
665                                 exit(2);
666                         }
667                         if (parse_uuid(optarg, ident.uuid))
668                                 ident.uuid_set = 1;
669                         else {
670                                 pr_err("Bad uuid: %s\n", optarg);
671                                 exit(2);
672                         }
673                         continue;
674
675                 case O(CREATE,'N'):
676                 case O(ASSEMBLE,'N'):
677                 case O(MISC,'N'):
678                         if (ident.name[0]) {
679                                 pr_err("name cannot be set twice.   Second value %s.\n", optarg);
680                                 exit(2);
681                         }
682                         if (mode == MISC && !c.subarray) {
683                                 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
684                                 exit(2);
685                         }
686                         if (strlen(optarg) > 32) {
687                                 pr_err("name '%s' is too long, 32 chars max.\n",
688                                         optarg);
689                                 exit(2);
690                         }
691                         strcpy(ident.name, optarg);
692                         continue;
693
694                 case O(ASSEMBLE,'m'): /* super-minor for array */
695                 case O(ASSEMBLE,SuperMinor):
696                         if (ident.super_minor != UnSet) {
697                                 pr_err("super-minor cannot be set twice.  Second value: %s.\n", optarg);
698                                 exit(2);
699                         }
700                         if (strcmp(optarg, "dev")==0)
701                                 ident.super_minor = -2;
702                         else {
703                                 ident.super_minor = parse_num(optarg);
704                                 if (ident.super_minor < 0) {
705                                         pr_err("Bad super-minor number: %s.\n", optarg);
706                                         exit(2);
707                                 }
708                         }
709                         continue;
710
711                 case O(ASSEMBLE,'o'):
712                 case O(MANAGE,'o'):
713                 case O(CREATE,'o'):
714                         c.readonly = 1;
715                         continue;
716
717                 case O(ASSEMBLE,'U'): /* update the superblock */
718                 case O(MISC,'U'):
719                         if (c.update) {
720                                 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
721                                         c.update, optarg);
722                                 exit(2);
723                         }
724                         if (mode == MISC && !c.subarray) {
725                                 pr_err("Only subarrays can be updated in misc mode\n");
726                                 exit(2);
727                         }
728                         c.update = optarg;
729                         if (strcmp(c.update, "sparc2.2")==0)
730                                 continue;
731                         if (strcmp(c.update, "super-minor") == 0)
732                                 continue;
733                         if (strcmp(c.update, "summaries")==0)
734                                 continue;
735                         if (strcmp(c.update, "resync")==0)
736                                 continue;
737                         if (strcmp(c.update, "uuid")==0)
738                                 continue;
739                         if (strcmp(c.update, "name")==0)
740                                 continue;
741                         if (strcmp(c.update, "homehost")==0)
742                                 continue;
743                         if (strcmp(c.update, "devicesize")==0)
744                                 continue;
745                         if (strcmp(c.update, "no-bitmap")==0)
746                                 continue;
747                         if (strcmp(c.update, "bbl") == 0)
748                                 continue;
749                         if (strcmp(c.update, "no-bbl") == 0)
750                                 continue;
751                         if (strcmp(c.update, "metadata") == 0)
752                                 continue;
753                         if (strcmp(c.update, "revert-reshape") == 0)
754                                 continue;
755                         if (strcmp(c.update, "byteorder")==0) {
756                                 if (ss) {
757                                         pr_err("must not set metadata type with --update=byteorder.\n");
758                                         exit(2);
759                                 }
760                                 for(i=0; !ss && superlist[i]; i++)
761                                         ss = superlist[i]->match_metadata_desc(
762                                                 "0.swap");
763                                 if (!ss) {
764                                         pr_err("INTERNAL ERROR cannot find 0.swap\n");
765                                         exit(2);
766                                 }
767
768                                 continue;
769                         }
770                         if (strcmp(c.update,"?") == 0 ||
771                             strcmp(c.update, "help") == 0) {
772                                 outf = stdout;
773                                 fprintf(outf, "%s: ", Name);
774                         } else {
775                                 outf = stderr;
776                                 fprintf(outf,
777                                         "%s: '--update=%s' is invalid.  ",
778                                         Name, c.update);
779                         }
780                         fprintf(outf, "Valid --update options are:\n"
781                 "     'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
782                 "     'summaries', 'homehost', 'byteorder', 'devicesize',\n"
783                 "     'no-bitmap', 'metadata', 'revert-reshape'\n"
784                 "     'bbl', 'no-bbl'\n"
785                                 );
786                         exit(outf == stdout ? 0 : 2);
787
788                 case O(MANAGE,'U'):
789                         /* update=devicesize is allowed with --re-add */
790                         if (devmode != 'A') {
791                                 pr_err("--update in Manage mode only allowed with --re-add.\n");
792                                 exit(1);
793                         }
794                         if (c.update) {
795                                 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
796                                         c.update, optarg);
797                                 exit(2);
798                         }
799                         c.update = optarg;
800                         if (strcmp(c.update, "devicesize") != 0 &&
801                             strcmp(c.update, "bbl") != 0 &&
802                             strcmp(c.update, "no-bbl") != 0) {
803                                 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be updated with --re-add\n");
804                                 exit(2);
805                         }
806                         continue;
807
808                 case O(INCREMENTAL,NoDegraded):
809                         pr_err("--no-degraded is deprecated in Incremental mode\n");
810                 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
811                         c.runstop = -1; /* --stop isn't allowed for --assemble,
812                                          * so we overload slightly */
813                         continue;
814
815                 case O(ASSEMBLE,'c'):
816                 case O(ASSEMBLE,ConfigFile):
817                 case O(INCREMENTAL, 'c'):
818                 case O(INCREMENTAL, ConfigFile):
819                 case O(MISC, 'c'):
820                 case O(MISC, ConfigFile):
821                 case O(MONITOR,'c'):
822                 case O(MONITOR,ConfigFile):
823                 case O(CREATE,ConfigFile):
824                         if (configfile) {
825                                 pr_err("configfile cannot be set twice.  Second value is %s.\n", optarg);
826                                 exit(2);
827                         }
828                         configfile = optarg;
829                         set_conffile(configfile);
830                         /* FIXME possibly check that config file exists.  Even parse it */
831                         continue;
832                 case O(ASSEMBLE,'s'): /* scan */
833                 case O(MISC,'s'):
834                 case O(MONITOR,'s'):
835                 case O(INCREMENTAL,'s'):
836                         c.scan = 1;
837                         continue;
838
839                 case O(MONITOR,'m'): /* mail address */
840                 case O(MONITOR,EMail):
841                         if (mailaddr)
842                                 pr_err("only specify one mailaddress. %s ignored.\n",
843                                         optarg);
844                         else
845                                 mailaddr = optarg;
846                         continue;
847
848                 case O(MONITOR,'p'): /* alert program */
849                 case O(MONITOR,ProgramOpt): /* alert program */
850                         if (program)
851                                 pr_err("only specify one alter program. %s ignored.\n",
852                                         optarg);
853                         else
854                                 program = optarg;
855                         continue;
856
857                 case O(MONITOR,'r'): /* rebuild increments */
858                 case O(MONITOR,Increment):
859                         increments = atoi(optarg);
860                         if (increments > 99 || increments < 1) {
861                                 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
862                                 exit(2);
863                         }
864                         continue;
865
866                 case O(MONITOR,'d'): /* delay in seconds */
867                 case O(GROW, 'd'):
868                 case O(BUILD,'d'): /* delay for bitmap updates */
869                 case O(CREATE,'d'):
870                         if (c.delay)
871                                 pr_err("only specify delay once. %s ignored.\n",
872                                         optarg);
873                         else {
874                                 c.delay = parse_num(optarg);
875                                 if (c.delay < 1) {
876                                         pr_err("invalid delay: %s\n",
877                                                 optarg);
878                                         exit(2);
879                                 }
880                         }
881                         continue;
882                 case O(MONITOR,'f'): /* daemonise */
883                 case O(MONITOR,Fork):
884                         daemonise = 1;
885                         continue;
886                 case O(MONITOR,'i'): /* pid */
887                         if (pidfile)
888                                 pr_err("only specify one pid file. %s ignored.\n",
889                                         optarg);
890                         else
891                                 pidfile = optarg;
892                         continue;
893                 case O(MONITOR,'1'): /* oneshot */
894                         oneshot = 1;
895                         spare_sharing = 0;
896                         continue;
897                 case O(MONITOR,'t'): /* test */
898                         c.test = 1;
899                         continue;
900                 case O(MONITOR,'y'): /* log messages to syslog */
901                         openlog("mdadm", LOG_PID, SYSLOG_FACILITY);
902                         dosyslog = 1;
903                         continue;
904                 case O(MONITOR, NoSharing):
905                         spare_sharing = 0;
906                         continue;
907
908                         /* now the general management options.  Some are applicable
909                          * to other modes. None have arguments.
910                          */
911                 case O(GROW,'a'):
912                 case O(GROW,Add):
913                 case O(MANAGE,'a'):
914                 case O(MANAGE,Add): /* add a drive */
915                         devmode = 'a';
916                         continue;
917                 case O(MANAGE,AddSpare): /* add drive - never re-add */
918                         devmode = 'S';
919                         continue;
920                 case O(MANAGE,ReAdd):
921                         devmode = 'A';
922                         continue;
923                 case O(MANAGE,'r'): /* remove a drive */
924                 case O(MANAGE,Remove):
925                         devmode = 'r';
926                         continue;
927                 case O(MANAGE,'f'): /* set faulty */
928                 case O(MANAGE,Fail):
929                 case O(INCREMENTAL,'f'):
930                 case O(INCREMENTAL,Remove):
931                 case O(INCREMENTAL,Fail): /* r for incremental is taken, use f
932                                            * even though we will both fail and
933                                            * remove the device */
934                         devmode = 'f';
935                         continue;
936                 case O(MANAGE,Replace):
937                         /* Mark these devices for replacement */
938                         devmode = 'R';
939                         continue;
940                 case O(MANAGE,With):
941                         /* These are the replacements to use */
942                         if (devmode != 'R') {
943                                 pr_err("--with must follow --replace\n");
944                                 exit(2);
945                         }
946                         devmode = 'W';
947                         continue;
948                 case O(INCREMENTAL,'R'):
949                 case O(MANAGE,'R'):
950                 case O(ASSEMBLE,'R'):
951                 case O(BUILD,'R'):
952                 case O(CREATE,'R'): /* Run the array */
953                         if (c.runstop < 0) {
954                                 pr_err("Cannot both Stop and Run an array\n");
955                                 exit(2);
956                         }
957                         c.runstop = 1;
958                         continue;
959                 case O(MANAGE,'S'):
960                         if (c.runstop > 0) {
961                                 pr_err("Cannot both Run and Stop an array\n");
962                                 exit(2);
963                         }
964                         c.runstop = -1;
965                         continue;
966                 case O(MANAGE,'t'):
967                         c.test = 1;
968                         continue;
969
970                 case O(MISC,'Q'):
971                 case O(MISC,'D'):
972                 case O(MISC,'E'):
973                 case O(MISC,KillOpt):
974                 case O(MISC,'R'):
975                 case O(MISC,'S'):
976                 case O(MISC,'X'):
977                 case O(MISC, ExamineBB):
978                 case O(MISC,'o'):
979                 case O(MISC,'w'):
980                 case O(MISC,'W'):
981                 case O(MISC, WaitOpt):
982                 case O(MISC, Waitclean):
983                 case O(MISC, DetailPlatform):
984                 case O(MISC, KillSubarray):
985                 case O(MISC, UpdateSubarray):
986                 case O(MISC, Dump):
987                 case O(MISC, Restore):
988                 case O(MISC ,Action):
989                         if (opt == KillSubarray || opt == UpdateSubarray) {
990                                 if (c.subarray) {
991                                         pr_err("subarray can only be specified once\n");
992                                         exit(2);
993                                 }
994                                 c.subarray = optarg;
995                         }
996                         if (opt == Action) {
997                                 if (c.action) {
998                                         pr_err("Only one --action can be specified\n");
999                                         exit(2);
1000                                 }
1001                                 if (strcmp(optarg, "idle") == 0 ||
1002                                     strcmp(optarg, "frozen") == 0 ||
1003                                     strcmp(optarg, "check") == 0 ||
1004                                     strcmp(optarg, "repair") == 0)
1005                                         c.action = optarg;
1006                                 else {
1007                                         pr_err("action must be one of idle, frozen, check, repair\n");
1008                                         exit(2);
1009                                 }
1010                         }
1011                         if (devmode && devmode != opt &&
1012                             (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
1013                                 pr_err("--examine/-E cannot be given with ");
1014                                 if (devmode == 'E') {
1015                                         if (option_index >= 0)
1016                                                 fprintf(stderr, "--%s\n",
1017                                                         long_options[option_index].name);
1018                                         else
1019                                                 fprintf(stderr, "-%c\n", opt);
1020                                 } else if (isalpha(devmode))
1021                                         fprintf(stderr, "-%c\n", devmode);
1022                                 else
1023                                         fprintf(stderr, "previous option\n");
1024                                 exit(2);
1025                         }
1026                         devmode = opt;
1027                         if (opt == Dump || opt == Restore) {
1028                                 if (dump_directory != NULL) {
1029                                         pr_err("dump/restore directory specified twice: %s and %s\n",
1030                                                dump_directory, optarg);
1031                                         exit(2);
1032                                 }
1033                                 dump_directory = optarg;
1034                         }
1035                         continue;
1036                 case O(MISC, UdevRules):
1037                         if (devmode && devmode != opt) {
1038                                 pr_err("--udev-rules must be the only option.\n");
1039                         } else {
1040                                 if (udev_filename)
1041                                         pr_err("only specify one udev rule filename. %s ignored.\n",
1042                                                 optarg);
1043                                 else
1044                                         udev_filename = optarg;
1045                         }
1046                         devmode = opt;
1047                         continue;
1048                 case O(MISC,'t'):
1049                         c.test = 1;
1050                         continue;
1051
1052                 case O(MISC, Sparc22):
1053                         if (devmode != 'E') {
1054                                 pr_err("--sparc2.2 only allowed with --examine\n");
1055                                 exit(2);
1056                         }
1057                         c.SparcAdjust = 1;
1058                         continue;
1059
1060                 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
1061                 case O(ASSEMBLE,Bitmap):
1062                         if (!optarg) {
1063                                 pr_err("bitmap file needed with -b in --assemble mode\n");
1064                                 exit(2);
1065                         }
1066                         if (strcmp(optarg, "internal")==0) {
1067                                 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1068                                 continue;
1069                         }
1070                         bitmap_fd = open(optarg, O_RDWR);
1071                         if (!*optarg || bitmap_fd < 0) {
1072                                 pr_err("cannot open bitmap file %s: %s\n", optarg, strerror(errno));
1073                                 exit(2);
1074                         }
1075                         ident.bitmap_fd = bitmap_fd; /* for Assemble */
1076                         continue;
1077
1078                 case O(ASSEMBLE, BackupFile):
1079                 case O(GROW, BackupFile):
1080                         /* Specify a file into which grow might place a backup,
1081                          * or from which assemble might recover a backup
1082                          */
1083                         if (c.backup_file) {
1084                                 pr_err("backup file already specified, rejecting %s\n", optarg);
1085                                 exit(2);
1086                         }
1087                         c.backup_file = optarg;
1088                         continue;
1089
1090                 case O(GROW, Continue):
1091                         /* Continue interrupted grow
1092                          */
1093                         grow_continue = 1;
1094                         continue;
1095                 case O(ASSEMBLE, InvalidBackup):
1096                         /* Acknowledge that the backupfile is invalid, but ask
1097                          * to continue anyway
1098                          */
1099                         c.invalid_backup = 1;
1100                         continue;
1101
1102                 case O(BUILD,'b'):
1103                 case O(BUILD,Bitmap):
1104                 case O(CREATE,'b'):
1105                 case O(CREATE,Bitmap): /* here we create the bitmap */
1106                 case O(GROW,'b'):
1107                 case O(GROW,Bitmap):
1108                         if (strcmp(optarg, "internal")== 0 ||
1109                             strcmp(optarg, "none")== 0 ||
1110                             strchr(optarg, '/') != NULL) {
1111                                 s.bitmap_file = optarg;
1112                                 continue;
1113                         }
1114                         if (strcmp(optarg, "clustered")== 0) {
1115                                 s.bitmap_file = optarg;
1116                                 /* Set the default number of cluster nodes
1117                                  * to 4 if not already set by user
1118                                  */
1119                                 if (c.nodes < 1)
1120                                         c.nodes = 4;
1121                                 continue;
1122                         }
1123                         /* probable typo */
1124                         pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1125                                 "       not '%s'\n", optarg);
1126                         exit(2);
1127
1128                 case O(GROW,BitmapChunk):
1129                 case O(BUILD,BitmapChunk):
1130                 case O(CREATE,BitmapChunk): /* bitmap chunksize */
1131                         s.bitmap_chunk = parse_size(optarg);
1132                         if (s.bitmap_chunk == 0 ||
1133                             s.bitmap_chunk == INVALID_SECTORS ||
1134                             s.bitmap_chunk & (s.bitmap_chunk - 1)) {
1135                                 pr_err("invalid bitmap chunksize: %s\n",
1136                                        optarg);
1137                                 exit(2);
1138                         }
1139                         s.bitmap_chunk = s.bitmap_chunk * 512;
1140                         continue;
1141
1142                 case O(GROW, WriteBehind):
1143                 case O(BUILD, WriteBehind):
1144                 case O(CREATE, WriteBehind): /* write-behind mode */
1145                         s.write_behind = DEFAULT_MAX_WRITE_BEHIND;
1146                         if (optarg) {
1147                                 s.write_behind = parse_num(optarg);
1148                                 if (s.write_behind < 0 ||
1149                                     s.write_behind > 16383) {
1150                                         pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
1151                                         exit(2);
1152                                 }
1153                         }
1154                         continue;
1155
1156                 case O(INCREMENTAL, 'r'):
1157                 case O(INCREMENTAL, RebuildMapOpt):
1158                         rebuild_map = 1;
1159                         continue;
1160                 case O(INCREMENTAL, IncrementalPath):
1161                         remove_path = optarg;
1162                         continue;
1163                 }
1164                 /* We have now processed all the valid options. Anything else is
1165                  * an error
1166                  */
1167                 if (option_index > 0)
1168                         pr_err(":option --%s not valid in %s mode\n",
1169                                 long_options[option_index].name,
1170                                 map_num(modes, mode));
1171                 else
1172                         pr_err("option -%c not valid in %s mode\n",
1173                                 opt, map_num(modes, mode));
1174                 exit(2);
1175
1176         }
1177
1178         if (print_help) {
1179                 char *help_text;
1180                 if (print_help == 2)
1181                         help_text = OptionHelp;
1182                 else
1183                         help_text = mode_help[mode];
1184                 if (help_text == NULL)
1185                         help_text = Help;
1186                 fputs(help_text,stdout);
1187                 exit(0);
1188         }
1189
1190         if (!mode && devs_found) {
1191                 mode = MISC;
1192                 devmode = 'Q';
1193                 if (devlist->disposition == 0)
1194                         devlist->disposition = devmode;
1195         }
1196         if (!mode) {
1197                 fputs(Usage, stderr);
1198                 exit(2);
1199         }
1200
1201         if (symlinks) {
1202                 struct createinfo *ci = conf_get_create_info();
1203
1204                 if (strcasecmp(symlinks, "yes") == 0)
1205                         ci->symlinks = 1;
1206                 else if (strcasecmp(symlinks, "no") == 0)
1207                         ci->symlinks = 0;
1208                 else {
1209                         pr_err("option --symlinks must be 'no' or 'yes'\n");
1210                         exit(2);
1211                 }
1212         }
1213         /* Ok, got the option parsing out of the way
1214          * hopefully it's mostly right but there might be some stuff
1215          * missing
1216          *
1217          * That is mosty checked in the per-mode stuff but...
1218          *
1219          * For @,B,C and A without -s, the first device listed must be
1220          * an md device.  We check that here and open it.
1221          */
1222
1223         if (mode == MANAGE || mode == BUILD || mode == CREATE
1224             || mode == GROW
1225             || (mode == ASSEMBLE && ! c.scan)) {
1226                 if (devs_found < 1) {
1227                         pr_err("an md device must be given in this mode\n");
1228                         exit(2);
1229                 }
1230                 if ((int)ident.super_minor == -2 && c.autof) {
1231                         pr_err("--super-minor=dev is incompatible with --auto\n");
1232                         exit(2);
1233                 }
1234                 if (mode == MANAGE || mode == GROW) {
1235                         mdfd = open_mddev(devlist->devname, 1);
1236                         if (mdfd < 0)
1237                                 exit(1);
1238                 } else
1239                         /* non-existent device is OK */
1240                         mdfd = open_mddev(devlist->devname, 0);
1241                 if (mdfd == -2) {
1242                         pr_err("device %s exists but is not an md array.\n", devlist->devname);
1243                         exit(1);
1244                 }
1245                 if ((int)ident.super_minor == -2) {
1246                         struct stat stb;
1247                         if (mdfd < 0) {
1248                                 pr_err("--super-minor=dev given, and listed device %s doesn't exist.\n",
1249                                         devlist->devname);
1250                                 exit(1);
1251                         }
1252                         fstat(mdfd, &stb);
1253                         ident.super_minor = minor(stb.st_rdev);
1254                 }
1255                 if (mdfd >= 0 && mode != MANAGE && mode != GROW) {
1256                         /* We don't really want this open yet, we just might
1257                          * have wanted to check some things
1258                          */
1259                         close(mdfd);
1260                         mdfd = -1;
1261                 }
1262         }
1263
1264         if (s.raiddisks) {
1265                 if (s.raiddisks == 1 &&  !c.force && s.level != LEVEL_FAULTY) {
1266                         pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1267                                 "     a mistake.  If you really mean it you will need to specify --force before\n"
1268                                 "     setting the number of drives.\n");
1269                         exit(2);
1270                 }
1271         }
1272
1273         if (c.homehost == NULL)
1274                 c.homehost = conf_get_homehost(&c.require_homehost);
1275         if (c.homehost == NULL || strcasecmp(c.homehost, "<system>")==0) {
1276                 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
1277                         sys_hostname[sizeof(sys_hostname)-1] = 0;
1278                         c.homehost = sys_hostname;
1279                 }
1280         }
1281         if (c.homehost && (!c.homehost[0] || strcasecmp(c.homehost, "<none>") == 0)) {
1282                 c.homehost = NULL;
1283                 c.require_homehost = 0;
1284         }
1285
1286         if (c.homecluster == NULL && (c.nodes > 0)) {
1287                 c.homecluster = conf_get_homecluster();
1288                 if (c.homecluster == NULL)
1289                         rv = get_cluster_name(&c.homecluster);
1290                 if (rv != 0) {
1291                         pr_err("The md can't get cluster name\n");
1292                         exit(1);
1293                 }
1294         }
1295
1296         if (c.backup_file && data_offset != INVALID_SECTORS) {
1297                 pr_err("--backup-file and --data-offset are incompatible\n");
1298                 exit(2);
1299         }
1300
1301         if ((mode == MISC && devmode == 'E')
1302             || (mode == MONITOR && spare_sharing == 0))
1303                 /* Anyone may try this */;
1304         else if (geteuid() != 0) {
1305                 pr_err("must be super-user to perform this action\n");
1306                 exit(1);
1307         }
1308
1309         ident.autof = c.autof;
1310
1311         if (c.scan && c.verbose < 2)
1312                 /* --scan implied --brief unless -vv */
1313                 c.brief = 1;
1314
1315         rv = 0;
1316         switch(mode) {
1317         case MANAGE:
1318                 /* readonly, add/remove, readwrite, runstop */
1319                 if (c.readonly > 0)
1320                         rv = Manage_ro(devlist->devname, mdfd, c.readonly);
1321                 if (!rv && devs_found>1)
1322                         rv = Manage_subdevs(devlist->devname, mdfd,
1323                                             devlist->next, c.verbose, c.test,
1324                                             c.update, c.force);
1325                 if (!rv && c.readonly < 0)
1326                         rv = Manage_ro(devlist->devname, mdfd, c.readonly);
1327                 if (!rv && c.runstop > 0)
1328                         rv = Manage_run(devlist->devname, mdfd, &c);
1329                 if (!rv && c.runstop < 0)
1330                         rv = Manage_stop(devlist->devname, mdfd, c.verbose, 0);
1331                 break;
1332         case ASSEMBLE:
1333                 if (devs_found == 1 && ident.uuid_set == 0 &&
1334                     ident.super_minor == UnSet && ident.name[0] == 0 && !c.scan ) {
1335                         /* Only a device has been given, so get details from config file */
1336                         struct mddev_ident *array_ident = conf_get_ident(devlist->devname);
1337                         if (array_ident == NULL) {
1338                                 pr_err("%s not identified in config file.\n",
1339                                         devlist->devname);
1340                                 rv |= 1;
1341                                 if (mdfd >= 0)
1342                                         close(mdfd);
1343                         } else {
1344                                 if (array_ident->autof == 0)
1345                                         array_ident->autof = c.autof;
1346                                 rv |= Assemble(ss, devlist->devname, array_ident,
1347                                                NULL, &c);
1348                         }
1349                 } else if (!c.scan)
1350                         rv = Assemble(ss, devlist->devname, &ident,
1351                                       devlist->next, &c);
1352                 else if (devs_found > 0) {
1353                         if (c.update && devs_found > 1) {
1354                                 pr_err("can only update a single array at a time\n");
1355                                 exit(1);
1356                         }
1357                         if (c.backup_file && devs_found > 1) {
1358                                 pr_err("can only assemble a single array when providing a backup file.\n");
1359                                 exit(1);
1360                         }
1361                         for (dv = devlist ; dv ; dv=dv->next) {
1362                                 struct mddev_ident *array_ident = conf_get_ident(dv->devname);
1363                                 if (array_ident == NULL) {
1364                                         pr_err("%s not identified in config file.\n",
1365                                                 dv->devname);
1366                                         rv |= 1;
1367                                         continue;
1368                                 }
1369                                 if (array_ident->autof == 0)
1370                                         array_ident->autof = c.autof;
1371                                 rv |= Assemble(ss, dv->devname, array_ident,
1372                                                NULL, &c);
1373                         }
1374                 } else {
1375                         if (c.update) {
1376                                 pr_err("--update not meaningful with a --scan assembly.\n");
1377                                 exit(1);
1378                         }
1379                         if (c.backup_file) {
1380                                 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1381                                 exit(1);
1382                         }
1383                         rv = scan_assemble(ss, &c, &ident);
1384                 }
1385
1386                 break;
1387         case BUILD:
1388                 if (c.delay == 0)
1389                         c.delay = DEFAULT_BITMAP_DELAY;
1390                 if (s.write_behind && !s.bitmap_file) {
1391                         pr_err("write-behind mode requires a bitmap.\n");
1392                         rv = 1;
1393                         break;
1394                 }
1395                 if (s.raiddisks == 0) {
1396                         pr_err("no raid-devices specified.\n");
1397                         rv = 1;
1398                         break;
1399                 }
1400
1401                 if (s.bitmap_file) {
1402                         if (strcmp(s.bitmap_file, "internal")==0) {
1403                                 pr_err("'internal' bitmaps not supported with --build\n");
1404                                 rv |= 1;
1405                                 break;
1406                         }
1407                 }
1408                 rv = Build(devlist->devname, devlist->next, &s, &c);
1409                 break;
1410         case CREATE:
1411                 if (c.delay == 0)
1412                         c.delay = DEFAULT_BITMAP_DELAY;
1413
1414                 if (c.nodes) {
1415                         if (!s.bitmap_file || strcmp(s.bitmap_file, "clustered") != 0) {
1416                                 pr_err("--nodes argument only compatible with --bitmap=clustered\n");
1417                                 rv = 1;
1418                                 break;
1419                         }
1420
1421                         if (s.level != 1) {
1422                                 pr_err("--bitmap=clustered is currently supported with RAID mirror only\n");
1423                                 rv = 1;
1424                                 break;
1425                         }
1426                 }
1427
1428                 if (s.write_behind && !s.bitmap_file) {
1429                         pr_err("write-behind mode requires a bitmap.\n");
1430                         rv = 1;
1431                         break;
1432                 }
1433                 if (s.raiddisks == 0) {
1434                         pr_err("no raid-devices specified.\n");
1435                         rv = 1;
1436                         break;
1437                 }
1438
1439                 rv = Create(ss, devlist->devname,
1440                             ident.name, ident.uuid_set ? ident.uuid : NULL,
1441                             devs_found-1, devlist->next,
1442                             &s, &c, data_offset);
1443                 break;
1444         case MISC:
1445                 if (devmode == 'E') {
1446                         if (devlist == NULL && !c.scan) {
1447                                 pr_err("No devices to examine\n");
1448                                 exit(2);
1449                         }
1450                         if (devlist == NULL)
1451                                 devlist = conf_get_devs();
1452                         if (devlist == NULL) {
1453                                 pr_err("No devices listed in %s\n", configfile?configfile:DefaultConfFile);
1454                                 exit(1);
1455                         }
1456                         rv = Examine(devlist, &c, ss);
1457                 } else if (devmode == DetailPlatform) {
1458                         rv = Detail_Platform(ss ? ss->ss : NULL, ss ? c.scan : 1,
1459                                              c.verbose, c.export,
1460                                              devlist ? devlist->devname : NULL);
1461                 } else if (devlist == NULL) {
1462                         if (devmode == 'S' && c.scan)
1463                                 rv = stop_scan(c.verbose);
1464                         else if ((devmode == 'D' || devmode == Waitclean) && c.scan)
1465                                 rv = misc_scan(devmode, &c);
1466                         else if (devmode == UdevRules)
1467                                 rv = Write_rules(udev_filename);
1468                         else {
1469                                 pr_err("No devices given.\n");
1470                                 exit(2);
1471                         }
1472                 } else
1473                         rv = misc_list(devlist, &ident, dump_directory, ss, &c);
1474                 break;
1475         case MONITOR:
1476                 if (!devlist && !c.scan) {
1477                         pr_err("Cannot monitor: need --scan or at least one device\n");
1478                         rv = 1;
1479                         break;
1480                 }
1481                 if (pidfile && !daemonise) {
1482                         pr_err("Cannot write a pid file when not in daemon mode\n");
1483                         rv = 1;
1484                         break;
1485                 }
1486                 if (c.delay == 0) {
1487                         if (get_linux_version() > 2006016)
1488                                 /* mdstat responds to poll */
1489                                 c.delay = 1000;
1490                         else
1491                                 c.delay = 60;
1492                 }
1493                 if (c.delay == 0)
1494                         c.delay = 60;
1495                 rv= Monitor(devlist, mailaddr, program,
1496                             &c, daemonise, oneshot,
1497                             dosyslog, pidfile, increments,
1498                             spare_sharing);
1499                 break;
1500
1501         case GROW:
1502                 if (array_size > 0) {
1503                         /* alway impose array size first, independent of
1504                          * anything else
1505                          * Do not allow level or raid_disks changes at the
1506                          * same time as that can be irreversibly destructive.
1507                          */
1508                         struct mdinfo sra;
1509                         int err;
1510                         if (s.raiddisks || s.level != UnSet) {
1511                                 pr_err("cannot change array size in same operation as changing raiddisks or level.\n"
1512                                         "    Change size first, then check that data is still intact.\n");
1513                                 rv = 1;
1514                                 break;
1515                         }
1516                         sysfs_init(&sra, mdfd, NULL);
1517                         if (array_size == MAX_SIZE)
1518                                 err = sysfs_set_str(&sra, NULL, "array_size", "default");
1519                         else
1520                                 err = sysfs_set_num(&sra, NULL, "array_size", array_size / 2);
1521                         if (err < 0) {
1522                                 if (errno == E2BIG)
1523                                         pr_err("--array-size setting is too large.\n");
1524                                 else
1525                                         pr_err("current kernel does not support setting --array-size\n");
1526                                 rv = 1;
1527                                 break;
1528                         }
1529                 }
1530                 if (devs_found > 1 && s.raiddisks == 0 && s.level == UnSet) {
1531                         /* must be '-a'. */
1532                         if (s.size > 0 || s.chunk || s.layout_str != NULL || s.bitmap_file) {
1533                                 pr_err("--add cannot be used with other geometry changes in --grow mode\n");
1534                                 rv = 1;
1535                                 break;
1536                         }
1537                         for (dv=devlist->next; dv ; dv=dv->next) {
1538                                 rv = Grow_Add_device(devlist->devname, mdfd,
1539                                                      dv->devname);
1540                                 if (rv)
1541                                         break;
1542                         }
1543                 } else if (s.bitmap_file) {
1544                         if (s.size > 0 || s.raiddisks || s.chunk ||
1545                             s.layout_str != NULL || devs_found > 1) {
1546                                 pr_err("--bitmap changes cannot be used with other geometry changes in --grow mode\n");
1547                                 rv = 1;
1548                                 break;
1549                         }
1550                         if (c.delay == 0)
1551                                 c.delay = DEFAULT_BITMAP_DELAY;
1552                         rv = Grow_addbitmap(devlist->devname, mdfd, &c, &s);
1553                 } else if (grow_continue)
1554                         rv = Grow_continue_command(devlist->devname,
1555                                                    mdfd, c.backup_file,
1556                                                    c.verbose);
1557                 else if (s.size > 0 || s.raiddisks || s.layout_str != NULL
1558                          || s.chunk != 0 || s.level != UnSet
1559                          || data_offset != INVALID_SECTORS) {
1560                         rv = Grow_reshape(devlist->devname, mdfd,
1561                                           devlist->next,
1562                                           data_offset, &c, &s);
1563                 } else if (array_size == 0)
1564                         pr_err("no changes to --grow\n");
1565                 break;
1566         case INCREMENTAL:
1567                 if (rebuild_map) {
1568                         RebuildMap();
1569                 }
1570                 if (c.scan) {
1571                         rv = 1;
1572                         if (devlist) {
1573                                 pr_err("In --incremental mode, a device cannot be given with --scan.\n");
1574                                 break;
1575                         }
1576                         if (c.runstop <= 0) {
1577                                 pr_err("--incremental --scan meaningless without --run.\n");
1578                                 break;
1579                         }
1580                         if (devmode == 'f') {
1581                                 pr_err("--incremental --scan --fail not supported.\n");
1582                                 break;
1583                         }
1584                         rv = IncrementalScan(&c, NULL);
1585                 }
1586                 if (!devlist) {
1587                         if (!rebuild_map && !c.scan) {
1588                                 pr_err("--incremental requires a device.\n");
1589                                 rv = 1;
1590                         }
1591                         break;
1592                 }
1593                 if (devmode == 'f') {
1594                         if (devlist->next) {
1595                                 pr_err("'--incremental --fail' can only handle one device.\n");
1596                                 rv = 1;
1597                                 break;
1598                         }
1599                         rv = IncrementalRemove(devlist->devname, remove_path,
1600                                                c.verbose);
1601                 } else
1602                         rv = Incremental(devlist, &c, ss);
1603                 break;
1604         case AUTODETECT:
1605                 autodetect();
1606                 break;
1607         }
1608         exit(rv);
1609 }
1610
1611 static int scan_assemble(struct supertype *ss,
1612                          struct context *c,
1613                          struct mddev_ident *ident)
1614 {
1615         struct mddev_ident *a, *array_list =  conf_get_ident(NULL);
1616         struct mddev_dev *devlist = conf_get_devs();
1617         struct map_ent *map = NULL;
1618         int cnt = 0;
1619         int rv = 0;
1620         int failures, successes;
1621
1622         if (conf_verify_devnames(array_list)) {
1623                 pr_err("Duplicate MD device names in conf file were found.\n");
1624                 return 1;
1625         }
1626         if (devlist == NULL) {
1627                 pr_err("No devices listed in conf file were found.\n");
1628                 return 1;
1629         }
1630         for (a = array_list; a ; a = a->next) {
1631                 a->assembled = 0;
1632                 if (a->autof == 0)
1633                         a->autof = c->autof;
1634         }
1635         if (map_lock(&map))
1636                 pr_err("failed to get exclusive lock on mapfile\n");
1637         do {
1638                 failures = 0;
1639                 successes = 0;
1640                 rv = 0;
1641                 for (a = array_list; a ; a = a->next) {
1642                         int r;
1643                         if (a->assembled)
1644                                 continue;
1645                         if (a->devname &&
1646                             strcasecmp(a->devname, "<ignore>") == 0)
1647                                 continue;
1648
1649                         r = Assemble(ss, a->devname,
1650                                      a, NULL, c);
1651                         if (r == 0) {
1652                                 a->assembled = 1;
1653                                 successes++;
1654                         } else
1655                                 failures++;
1656                         rv |= r;
1657                         cnt++;
1658                 }
1659         } while (failures && successes);
1660         if (c->homehost && cnt == 0) {
1661                 /* Maybe we can auto-assemble something.
1662                  * Repeatedly call Assemble in auto-assemble mode
1663                  * until it fails
1664                  */
1665                 int rv2;
1666                 int acnt;
1667                 ident->autof = c->autof;
1668                 do {
1669                         struct mddev_dev *devlist = conf_get_devs();
1670                         acnt = 0;
1671                         do {
1672                                 rv2 = Assemble(ss, NULL,
1673                                                ident,
1674                                                devlist, c);
1675                                 if (rv2==0) {
1676                                         cnt++;
1677                                         acnt++;
1678                                 }
1679                         } while (rv2!=2);
1680                         /* Incase there are stacked devices, we need to go around again */
1681                 } while (acnt);
1682                 if (cnt == 0 && rv == 0) {
1683                         pr_err("No arrays found in config file or automatically\n");
1684                         rv = 1;
1685                 } else if (cnt)
1686                         rv = 0;
1687         } else if (cnt == 0 && rv == 0) {
1688                 pr_err("No arrays found in config file\n");
1689                 rv = 1;
1690         }
1691         map_unlock(&map);
1692         return rv;
1693 }
1694
1695 static int misc_scan(char devmode, struct context *c)
1696 {
1697         /* apply --detail or --wait-clean to
1698          * all devices in /proc/mdstat
1699          */
1700         struct mdstat_ent *ms = mdstat_read(0, 1);
1701         struct mdstat_ent *e;
1702         struct map_ent *map = NULL;
1703         int members;
1704         int rv = 0;
1705
1706         for (members = 0; members <= 1; members++) {
1707                 for (e=ms ; e ; e=e->next) {
1708                         char *name = NULL;
1709                         struct map_ent *me;
1710                         struct stat stb;
1711                         int member = e->metadata_version &&
1712                                 strncmp(e->metadata_version,
1713                                         "external:/", 10) == 0;
1714                         if (members != member)
1715                                 continue;
1716                         me = map_by_devnm(&map, e->devnm);
1717                         if (me && me->path
1718                             && strcmp(me->path, "/unknown") != 0)
1719                                 name = me->path;
1720                         if (name == NULL ||
1721                             stat(name, &stb) != 0)
1722                                 name = get_md_name(e->devnm);
1723
1724                         if (!name) {
1725                                 pr_err("cannot find device file for %s\n",
1726                                         e->dev);
1727                                 continue;
1728                         }
1729                         if (devmode == 'D')
1730                                 rv |= Detail(name, c);
1731                         else
1732                                 rv |= WaitClean(name, -1, c->verbose);
1733                         put_md_name(name);
1734                 }
1735         }
1736         free_mdstat(ms);
1737         return rv;
1738 }
1739
1740 static int stop_scan(int verbose)
1741 {
1742         /* apply --stop to all devices in /proc/mdstat */
1743         /* Due to possible stacking of devices, repeat until
1744          * nothing more can be stopped
1745          */
1746         int progress=1, err;
1747         int last = 0;
1748         int rv = 0;
1749         do {
1750                 struct mdstat_ent *ms = mdstat_read(0, 0);
1751                 struct mdstat_ent *e;
1752
1753                 if (!progress) last = 1;
1754                 progress = 0; err = 0;
1755                 for (e=ms ; e ; e=e->next) {
1756                         char *name = get_md_name(e->devnm);
1757                         int mdfd;
1758
1759                         if (!name) {
1760                                 pr_err("cannot find device file for %s\n",
1761                                         e->dev);
1762                                 continue;
1763                         }
1764                         mdfd = open_mddev(name, 1);
1765                         if (mdfd >= 0) {
1766                                 if (Manage_stop(name, mdfd, verbose, !last))
1767                                         err = 1;
1768                                 else
1769                                         progress = 1;
1770                                 close(mdfd);
1771                         }
1772
1773                         put_md_name(name);
1774                 }
1775                 free_mdstat(ms);
1776         } while (!last && err);
1777         if (err)
1778                 rv |= 1;
1779         return rv;
1780 }
1781
1782 static int misc_list(struct mddev_dev *devlist,
1783                      struct mddev_ident *ident,
1784                      char *dump_directory,
1785                      struct supertype *ss, struct context *c)
1786 {
1787         struct mddev_dev *dv;
1788         int rv = 0;
1789
1790         for (dv=devlist ; dv; dv=(rv & 16) ? NULL : dv->next) {
1791                 int mdfd;
1792
1793                 switch(dv->disposition) {
1794                 case 'D':
1795                         rv |= Detail(dv->devname, c);
1796                         continue;
1797                 case KillOpt: /* Zero superblock */
1798                         if (ss)
1799                                 rv |= Kill(dv->devname, ss, c->force, c->verbose,0);
1800                         else {
1801                                 int v = c->verbose;
1802                                 do {
1803                                         rv |= Kill(dv->devname, NULL, c->force, v, 0);
1804                                         v = -1;
1805                                 } while (rv == 0);
1806                                 rv &= ~2;
1807                         }
1808                         continue;
1809                 case 'Q':
1810                         rv |= Query(dv->devname); continue;
1811                 case 'X':
1812                         rv |= ExamineBitmap(dv->devname, c->brief, ss); continue;
1813                 case ExamineBB:
1814                         rv |= ExamineBadblocks(dv->devname, c->brief, ss); continue;
1815                 case 'W':
1816                 case WaitOpt:
1817                         rv |= Wait(dv->devname); continue;
1818                 case Waitclean:
1819                         rv |= WaitClean(dv->devname, -1, c->verbose); continue;
1820                 case KillSubarray:
1821                         rv |= Kill_subarray(dv->devname, c->subarray, c->verbose);
1822                         continue;
1823                 case UpdateSubarray:
1824                         if (c->update == NULL) {
1825                                 pr_err("-U/--update must be specified with --update-subarray\n");
1826                                 rv |= 1;
1827                                 continue;
1828                         }
1829                         rv |= Update_subarray(dv->devname, c->subarray,
1830                                               c->update, ident, c->verbose);
1831                         continue;
1832                 case Dump:
1833                         rv |= Dump_metadata(dv->devname, dump_directory, c, ss);
1834                         continue;
1835                 case Restore:
1836                         rv |= Restore_metadata(dv->devname, dump_directory, c, ss,
1837                                                (dv == devlist && dv->next == NULL));
1838                         continue;
1839                 case Action:
1840                         rv |= SetAction(dv->devname, c->action);
1841                         continue;
1842                 }
1843                 if (dv->devname[0] == '/')
1844                         mdfd = open_mddev(dv->devname, 1);
1845                 else {
1846                         mdfd = open_dev(dv->devname);
1847                         if (mdfd < 0)
1848                                 pr_err("Cannot open %s\n", dv->devname);
1849                 }
1850                 if (mdfd>=0) {
1851                         switch(dv->disposition) {
1852                         case 'R':
1853                                 c->runstop = 1;
1854                                 rv |= Manage_run(dv->devname, mdfd, c); break;
1855                         case 'S':
1856                                 rv |= Manage_stop(dv->devname, mdfd, c->verbose, 0); break;
1857                         case 'o':
1858                                 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1859                         case 'w':
1860                                 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1861                         }
1862                         close(mdfd);
1863                 } else
1864                         rv |= 1;
1865         }
1866         return rv;
1867 }
1868
1869 int SetAction(char *dev, char *action)
1870 {
1871         int fd = open(dev, O_RDONLY);
1872         struct mdinfo mdi;
1873         if (fd < 0) {
1874                 pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
1875                 return 1;
1876         }
1877         sysfs_init(&mdi, fd, NULL);
1878         close(fd);
1879         if (!mdi.sys_name[0]) {
1880                 pr_err("%s is no an md array\n", dev);
1881                 return 1;
1882         }
1883
1884         if (sysfs_set_str(&mdi, NULL, "sync_action", action) < 0) {
1885                 pr_err("Count not set action for %s to %s: %s\n",
1886                        dev, action, strerror(errno));
1887                 return 1;
1888         }
1889         return 0;
1890 }