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