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