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