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