]> git.ipfire.org Git - thirdparty/mdadm.git/blame - mdadm.c
Enhance raid4 support: --assemble and --monitor wasn't quite happy with 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
f5e166fe 820 case O(GROW,'b'):
c82f047c
NB
821 case O(BUILD,'b'):
822 case O(CREATE,'b'): /* here we create the bitmap */
1e0d770c
NB
823 if (strcmp(optarg, "internal")== 0 ||
824 strcmp(optarg, "none")== 0 ||
825 strchr(optarg, '/') != NULL) {
826 bitmap_file = optarg;
827 continue;
828 }
829 /* probable typo */
830 fprintf(stderr, Name ": bitmap file must contain a '/', or be 'internal', or 'none'\n");
831 exit(2);
c82f047c 832
997aed5d
NB
833 case O(GROW,BitmapChunk):
834 case O(BUILD,BitmapChunk):
835 case O(CREATE,BitmapChunk): /* bitmap chunksize */
c82f047c
NB
836 bitmap_chunk = strtol(optarg, &c, 10);
837 if (!optarg[0] || *c || bitmap_chunk < 0 ||
838 bitmap_chunk & (bitmap_chunk - 1)) {
839 fprintf(stderr, Name ": invalid bitmap chunksize: %s\n",
840 optarg);
841 exit(2);
842 }
843 /* convert K to B, chunk of 0K means 512B */
844 bitmap_chunk = bitmap_chunk ? bitmap_chunk * 1024 : 512;
845 continue;
dfd4d8ee 846
997aed5d
NB
847 case O(BUILD, WriteBehind):
848 case O(CREATE, WriteBehind): /* write-behind mode */
dfd4d8ee
NB
849 write_behind = DEFAULT_MAX_WRITE_BEHIND;
850 if (optarg) {
851 write_behind = strtol(optarg, &c, 10);
852 if (write_behind < 0 || *c ||
853 write_behind > 16383) {
854 fprintf(stderr, Name ": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
855 exit(2);
856 }
857 }
858 continue;
8382f19b
NB
859
860 case O(INCREMENTAL, 'r'):
861 rebuild_map = 1;
862 continue;
52826846
NB
863 }
864 /* We have now processed all the valid options. Anything else is
865 * an error
866 */
06b0d786
NB
867 if (option_index > 0)
868 fprintf(stderr, Name ":option --%s not valid in %s mode\n",
869 long_options[option_index].name,
870 map_num(modes, mode));
871 else
872 fprintf(stderr, Name ": option -%c not valid in %s mode\n",
873 opt, map_num(modes, mode));
64c4757e 874 exit(2);
52826846
NB
875
876 }
877
3d4064cc
NB
878 if (print_help) {
879 char *help_text = Help;
880 if (print_help == 2)
881 help_text = OptionHelp;
882 else
883 switch (mode) {
884 case ASSEMBLE : help_text = Help_assemble; break;
885 case BUILD : help_text = Help_build; break;
886 case CREATE : help_text = Help_create; break;
887 case MANAGE : help_text = Help_manage; break;
888 case MISC : help_text = Help_misc; break;
889 case MONITOR : help_text = Help_monitor; break;
890 case GROW : help_text = Help_grow; break;
8382f19b 891 case INCREMENTAL:help_text= Help_incr; break;
3d4064cc 892 }
0047d254 893 fputs(help_text,stdout);
3d4064cc
NB
894 exit(0);
895 }
896
0320ea45
NB
897 if (!mode && devs_found) {
898 mode = MISC;
899 devmode = 'Q';
900 if (devlist->disposition == 0)
901 devlist->disposition = devmode;
902 }
52826846
NB
903 if (!mode) {
904 fputs(Usage, stderr);
64c4757e 905 exit(2);
64c4757e 906 }
38098016
NB
907
908 if (symlinks) {
909 struct createinfo *ci = conf_get_create_info();
910
911 if (strcasecmp(symlinks, "yes") == 0)
912 ci->symlinks = 1;
913 else if (strcasecmp(symlinks, "no") == 0)
914 ci->symlinks = 0;
915 else {
916 fprintf(stderr, Name ": option --symlinks must be 'no' or 'yes'\n");
917 exit(2);
918 }
919 }
52826846
NB
920 /* Ok, got the option parsing out of the way
921 * hopefully it's mostly right but there might be some stuff
922 * missing
923 *
e0d19036 924 * That is mosty checked in the per-mode stuff but...
52826846
NB
925 *
926 * For @,B,C and A without -s, the first device listed must be an md device
927 * we check that here and open it.
64c4757e 928 */
52826846 929
dd0781e5
NB
930 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
931 (mode == ASSEMBLE && ! scan)) {
52826846
NB
932 if (devs_found < 1) {
933 fprintf(stderr, Name ": an md device must be given in this mode\n");
934 exit(2);
935 }
dd0781e5
NB
936 if ((int)ident.super_minor == -2 && autof) {
937 fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
938 exit(2);
939 }
1d1e104b 940 if (mode == MANAGE || mode == GROW)
d55e3aef 941 autof=1; /* Don't create */
dd0781e5 942 mdfd = open_mddev(devlist->devname, autof);
52826846
NB
943 if (mdfd < 0)
944 exit(1);
98c6faba 945 if ((int)ident.super_minor == -2) {
d013a55e
NB
946 struct stat stb;
947 fstat(mdfd, &stb);
0df46c2a 948 ident.super_minor = minor(stb.st_rdev);
d013a55e 949 }
64c4757e 950 }
52826846 951
e4c4352e
NB
952 if (raiddisks) {
953 if (raiddisks > max_disks) {
6fbba4c9
NB
954 fprintf(stderr, Name ": invalid number of raid devices: %d\n",
955 raiddisks);
e4c4352e
NB
956 exit(2);
957 }
958 if (raiddisks == 1 && !force && level != -5) {
959 fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
960 " a mistake. If you really mean it you will need to specify --force before\n"
961 " setting the number of drives.\n");
962 exit(2);
963 }
964 }
965 if (sparedisks) {
966 if ( sparedisks > max_disks - raiddisks) {
6fbba4c9
NB
967 fprintf(stderr, Name ": invalid number of spare-devices: %d\n",
968 sparedisks);
e4c4352e
NB
969 exit(2);
970 }
971 }
c82f047c 972
997aed5d 973 if (homehost == NULL)
8aec876d 974 homehost = conf_get_homehost();
05697ec1
NB
975 if (homehost && strcmp(homehost, "<system>")==0) {
976 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
977 sys_hostname[sizeof(sys_hostname)-1] = 0;
978 homehost = sys_hostname;
979 }
980 }
997aed5d 981
52826846
NB
982 rv = 0;
983 switch(mode) {
e0d19036 984 case MANAGE:
52826846
NB
985 /* readonly, add/remove, readwrite, runstop */
986 if (readonly>0)
cd29a5c8 987 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 988 if (!rv && devs_found>1)
cd29a5c8 989 rv = Manage_subdevs(devlist->devname, mdfd,
dab6685f 990 devlist->next, verbose-quiet);
52826846 991 if (!rv && readonly < 0)
cd29a5c8 992 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 993 if (!rv && runstop)
ab56093f 994 rv = Manage_runstop(devlist->devname, mdfd, runstop, quiet);
52826846 995 break;
e0d19036 996 case ASSEMBLE:
d013a55e 997 if (devs_found == 1 && ident.uuid_set == 0 &&
947fd4dd 998 ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) {
d013a55e 999 /* Only a device has been given, so get details from config file */
8aec876d 1000 mddev_ident_t array_ident = conf_get_ident(devlist->devname);
b5e64645
NB
1001 if (array_ident == NULL) {
1002 fprintf(stderr, Name ": %s not identified in config file.\n",
1003 devlist->devname);
d013a55e 1004 rv |= 1;
b5e64645 1005 } else {
1337546d
NB
1006 mdfd = open_mddev(devlist->devname,
1007 array_ident->autof ? array_ident->autof : autof);
b5e64645 1008 if (mdfd < 0)
d013a55e 1009 rv |= 1;
b5e64645 1010 else {
8aec876d 1011 rv |= Assemble(ss, devlist->devname, mdfd, array_ident,
06b0d786 1012 NULL, backup_file,
e5eac01f 1013 readonly, runstop, update, homehost, verbose-quiet, force);
b5e64645
NB
1014 close(mdfd);
1015 }
d013a55e
NB
1016 }
1017 } else if (!scan)
8aec876d 1018 rv = Assemble(ss, devlist->devname, mdfd, &ident,
06b0d786 1019 devlist->next, backup_file,
e5eac01f 1020 readonly, runstop, update, homehost, verbose-quiet, force);
5787fa49
NB
1021 else if (devs_found>0) {
1022 if (update && devs_found > 1) {
1023 fprintf(stderr, Name ": can only update a single array at a time\n");
1024 exit(1);
1025 }
06b0d786
NB
1026 if (backup_file && devs_found > 1) {
1027 fprintf(stderr, Name ": can only assemble a single array when providing a backup file.\n");
1028 exit(1);
1029 }
cd29a5c8 1030 for (dv = devlist ; dv ; dv=dv->next) {
8aec876d 1031 mddev_ident_t array_ident = conf_get_ident(dv->devname);
52826846
NB
1032 if (array_ident == NULL) {
1033 fprintf(stderr, Name ": %s not identified in config file.\n",
cd29a5c8 1034 dv->devname);
52826846
NB
1035 rv |= 1;
1036 continue;
1037 }
1337546d
NB
1038 mdfd = open_mddev(dv->devname,
1039 array_ident->autof ?array_ident->autof : autof);
b5e64645
NB
1040 if (mdfd < 0) {
1041 rv |= 1;
1042 continue;
1043 }
8aec876d 1044 rv |= Assemble(ss, dv->devname, mdfd, array_ident,
06b0d786 1045 NULL, backup_file,
e5eac01f 1046 readonly, runstop, update, homehost, verbose-quiet, force);
b5e64645 1047 close(mdfd);
52826846 1048 }
5787fa49 1049 } else {
8aec876d
NB
1050 mddev_ident_t array_list = conf_get_ident(NULL);
1051 mddev_dev_t devlist = conf_get_devs();
da6b5ca9
NB
1052 int cnt = 0;
1053 if (devlist == NULL) {
1054 fprintf(stderr, Name ": No devices listed in conf file were found.\n");
1055 exit(1);
1056 }
1057 if (update) {
1058 fprintf(stderr, Name ": --update not meaningful with a --scan assembly.\n");
1059 exit(1);
1060 }
1061 if (backup_file) {
1062 fprintf(stderr, Name ": --backup_file not meaningful with a --scan assembly.\n");
1063 exit(1);
1064 }
1065 for (; array_list; array_list = array_list->next) {
1066 mdu_array_info_t array;
1067 mdfd = open_mddev(array_list->devname,
1068 array_list->autof ? array_list->autof : autof);
1069 if (mdfd < 0) {
1070 rv |= 1;
1071 continue;
06b0d786 1072 }
da6b5ca9
NB
1073 if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
1074 /* already assembled, skip */
1cac4c1b 1075 cnt++;
da6b5ca9
NB
1076 else {
1077 rv |= Assemble(ss, array_list->devname, mdfd,
8aec876d 1078 array_list,
43f2372a 1079 NULL, NULL,
da6b5ca9
NB
1080 readonly, runstop, NULL, homehost, verbose-quiet, force);
1081 if (rv == 0) cnt++;
06b0d786 1082 }
da6b5ca9
NB
1083 close(mdfd);
1084 }
1085 if (homehost) {
1086 /* Maybe we can auto-assemble something.
1087 * Repeatedly call Assemble in auto-assmble mode
1088 * until it fails
1089 */
1090 int rv2;
d55e3aef
NB
1091 int acnt;
1092 ident.autof = autof;
da6b5ca9 1093 do {
d55e3aef
NB
1094 acnt = 0;
1095 do {
1096 rv2 = Assemble(ss, NULL, -1,
8aec876d 1097 &ident,
43f2372a 1098 NULL, NULL,
d55e3aef
NB
1099 readonly, runstop, NULL, homehost, verbose-quiet, force);
1100 if (rv2==0) {
1101 cnt++;
1102 acnt++;
1103 }
589395d6
NB
1104 if (rv2 == 1)
1105 /* found something so even though assembly failed we
1106 * want to avoid auto-updates
1107 */
1108 auto_update_home = 0;
d55e3aef
NB
1109 } while (rv2!=2);
1110 /* Incase there are stacked devices, we need to go around again */
d55e3aef 1111 } while (acnt);
589395d6
NB
1112 if (cnt == 0 && auto_update_home && homehost) {
1113 /* Nothing found, maybe we need to bootstrap homehost info */
1114 do {
1115 acnt = 0;
1116 do {
1117 rv2 = Assemble(ss, NULL, -1,
8aec876d 1118 &ident,
43f2372a 1119 NULL, NULL,
589395d6
NB
1120 readonly, runstop, "homehost", homehost, verbose-quiet, force);
1121 if (rv2==0) {
1122 cnt++;
1123 acnt++;
1124 }
1125 } while (rv2!=2);
1126 /* Incase there are stacked devices, we need to go around again */
589395d6
NB
1127 } while (acnt);
1128 }
da6b5ca9
NB
1129 if (cnt == 0 && rv == 0) {
1130 fprintf(stderr, Name ": No arrays found in config file or automatically\n");
1131 rv = 1;
52826846 1132 }
da6b5ca9
NB
1133 } else if (cnt == 0 && rv == 0) {
1134 fprintf(stderr, Name ": No arrays found in config file\n");
1135 rv = 1;
1136 }
52826846
NB
1137 }
1138 break;
e0d19036 1139 case BUILD:
c82f047c 1140 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
dfd4d8ee
NB
1141 if (write_behind && !bitmap_file) {
1142 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1143 rv = 1;
1144 break;
1145 }
5b28bd56
NB
1146 if (raiddisks == 0) {
1147 fprintf(stderr, Name ": no raid-disks specified.\n");
1148 rv = 1;
1149 break;
1150 }
dfd4d8ee 1151
c82f047c 1152 if (bitmap_file) {
55935d51
NB
1153 if (strcmp(bitmap_file, "internal")==0) {
1154 fprintf(stderr, Name ": 'internal' bitmaps not supported with --build\n");
1155 rv |= 1;
1156 break;
1157 }
c82f047c
NB
1158 }
1159 rv = Build(devlist->devname, mdfd, chunk, level, layout,
1160 raiddisks, devlist->next, assume_clean,
dab6685f 1161 bitmap_file, bitmap_chunk, write_behind, delay, verbose-quiet);
52826846 1162 break;
e0d19036 1163 case CREATE:
c82f047c 1164 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
dfd4d8ee
NB
1165 if (write_behind && !bitmap_file) {
1166 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1167 rv = 1;
1168 break;
1169 }
5b28bd56
NB
1170 if (raiddisks == 0) {
1171 fprintf(stderr, Name ": no raid-disks specified.\n");
1172 rv = 1;
1173 break;
1174 }
f9ce90ba
NB
1175
1176 rv = Create(ss, devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size,
05697ec1 1177 raiddisks, sparedisks, ident.name, homehost,
3d3dd91e 1178 ident.uuid_set ? ident.uuid : NULL,
47d79ef8 1179 devs_found-1, devlist->next, runstop, verbose-quiet, force, assume_clean,
dfd4d8ee 1180 bitmap_file, bitmap_chunk, write_behind, delay);
52826846 1181 break;
e0d19036 1182 case MISC:
e0d19036
NB
1183 if (devmode == 'E') {
1184 if (devlist == NULL && !scan) {
1185 fprintf(stderr, Name ": No devices to examine\n");
1186 exit(2);
1187 }
1188 if (devlist == NULL)
8aec876d 1189 devlist = conf_get_devs();
e0d19036 1190 if (devlist == NULL) {
c913b90e 1191 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
e0d19036
NB
1192 exit(1);
1193 }
e843d5d7
NB
1194 if (brief && verbose)
1195 brief = 2;
a1cbd7d0 1196 rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief, scan, SparcAdjust, ss, homehost);
e0d19036
NB
1197 } else {
1198 if (devlist == NULL) {
91f068bf
NB
1199 if (devmode=='D' && scan) {
1200 /* apply --detail to all devices in /proc/mdstat */
22a88995 1201 struct mdstat_ent *ms = mdstat_read(0, 1);
e0d19036
NB
1202 struct mdstat_ent *e;
1203 for (e=ms ; e ; e=e->next) {
1204 char *name = get_md_name(e->devnum);
1205
1206 if (!name) {
1207 fprintf(stderr, Name ": cannot find device file for %s\n",
1208 e->dev);
1209 continue;
1210 }
54bad364
KS
1211 rv |= Detail(name, verbose>1?0:verbose+1,
1212 export, test, homehost);
91f068bf
NB
1213 put_md_name(name);
1214 }
1215 } else if (devmode == 'S' && scan) {
1216 /* apply --stop to all devices in /proc/mdstat */
1217 /* Due to possible stacking of devices, repeat until
1218 * nothing more can be stopped
1219 */
1220 int progress=1, err;
1221 int last = 0;
1222 do {
22a88995 1223 struct mdstat_ent *ms = mdstat_read(0, 0);
91f068bf
NB
1224 struct mdstat_ent *e;
1225
1226 if (!progress) last = 1;
1227 progress = 0; err = 0;
1228 for (e=ms ; e ; e=e->next) {
1229 char *name = get_md_name(e->devnum);
1230
1231 if (!name) {
1232 fprintf(stderr, Name ": cannot find device file for %s\n",
1233 e->dev);
1234 continue;
1235 }
d55e3aef 1236 mdfd = open_mddev(name, 1);
43fc1676 1237 if (mdfd >= 0) {
ab56093f 1238 if (Manage_runstop(name, mdfd, -1, quiet?1:last?0:-1))
91f068bf
NB
1239 err = 1;
1240 else
1241 progress = 1;
43fc1676
NB
1242 close(mdfd);
1243 }
91f068bf
NB
1244
1245 put_md_name(name);
e0d19036 1246 }
91f068bf 1247 } while (!last && err);
f9c25f1d 1248 if (err) rv |= 1;
91f068bf 1249 } else {
e0d19036
NB
1250 fprintf(stderr, Name ": No devices given.\n");
1251 exit(2);
1252 }
1253 }
1254 for (dv=devlist ; dv; dv=dv->next) {
1255 switch(dv->disposition) {
1256 case 'D':
54bad364
KS
1257 rv |= Detail(dv->devname,
1258 brief?1+verbose:0,
1259 export, test, homehost);
1260 continue;
e0d19036 1261 case 'K': /* Zero superblock */
6409687b 1262 rv |= Kill(dv->devname, force, quiet); continue;
e0d19036
NB
1263 case 'Q':
1264 rv |= Query(dv->devname); continue;
c82f047c 1265 case 'X':
55935d51 1266 rv |= ExamineBitmap(dv->devname, brief, ss); continue;
b90c0e9a
NB
1267 case 'W':
1268 rv |= Wait(dv->devname); continue;
e0d19036 1269 }
d55e3aef 1270 mdfd = open_mddev(dv->devname, 1);
43fc1676 1271 if (mdfd>=0) {
e0d19036
NB
1272 switch(dv->disposition) {
1273 case 'R':
ab56093f 1274 rv |= Manage_runstop(dv->devname, mdfd, 1, quiet); break;
e0d19036 1275 case 'S':
ab56093f 1276 rv |= Manage_runstop(dv->devname, mdfd, -1, quiet); break;
e0d19036
NB
1277 case 'o':
1278 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1279 case 'w':
1280 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1281 }
43fc1676 1282 close(mdfd);
7f48e210
NB
1283 } else
1284 rv |= 1;
e0d19036 1285 }
cd29a5c8 1286 }
9a9dab36 1287 break;
e0d19036 1288 case MONITOR:
e0d19036
NB
1289 if (!devlist && !scan) {
1290 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
1291 rv = 1;
1292 break;
1293 }
b5e64645
NB
1294 if (pidfile && !daemonise) {
1295 fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
1296 rv = 1;
1297 break;
1298 }
d013a55e 1299 rv= Monitor(devlist, mailaddr, program,
773135f5 1300 delay?delay:60, daemonise, scan, oneshot,
8aec876d 1301 dosyslog, test, pidfile);
9a9dab36 1302 break;
dd0781e5
NB
1303
1304 case GROW:
1305 if (devs_found > 1) {
e5329c37
NB
1306
1307 /* must be '-a'. */
1308 if (size >= 0 || raiddisks) {
1309 fprintf(stderr, Name ": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1310 rv = 1;
1311 break;
1312 }
1313 for (dv=devlist->next; dv ; dv=dv->next) {
1314 rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
1315 if (rv)
1316 break;
1317 }
f5e166fe
NB
1318 } else if ((size >= 0) + (raiddisks != 0) + (layout != UnSet) + (bitmap_file != NULL)> 1) {
1319 fprintf(stderr, Name ": can change at most one of size, raiddisks, bitmap, and layout\n");
dd0781e5
NB
1320 rv = 1;
1321 break;
b5e64645
NB
1322 } else if (layout != UnSet)
1323 rv = Manage_reconfig(devlist->devname, mdfd, layout);
1324 else if (size >= 0 || raiddisks)
06b0d786 1325 rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file,
e86c9dd6 1326 size, level, layout, chunk, raiddisks);
f5e166fe
NB
1327 else if (bitmap_file) {
1328 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1329 rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file,
8fac0577 1330 bitmap_chunk, delay, write_behind, force);
f5e166fe 1331 } else
b5e64645 1332 fprintf(stderr, Name ": no changes to --grow\n");
dd0781e5 1333 break;
8382f19b
NB
1334 case INCREMENTAL:
1335 if (rebuild_map) {
1336 RebuildMap();
1337 }
1338 if (scan) {
1339 if (runstop <= 0) {
1340 fprintf(stderr, Name
1341 ": --incremental --scan meaningless without --run.\n");
1342 break;
1343 }
1344 rv = IncrementalScan(verbose);
1345 }
1346 if (!devlist) {
1347 if (!rebuild_map && !scan) {
1348 fprintf(stderr, Name
1349 ": --incremental requires a device.\n");
1350 rv = 1;
1351 }
1352 break;
1353 }
1354 if (devlist->next) {
1355 fprintf(stderr, Name
1356 ": --incremental can only handle one device.\n");
1357 rv = 1;
1358 break;
1359 }
1360 rv = Incremental(devlist->devname, verbose-quiet, runstop,
1361 ss, homehost, autof);
1f48664b
NB
1362 break;
1363 case AUTODETECT:
1364 autodetect();
1365 break;
64c4757e 1366 }
52826846 1367 exit(rv);
64c4757e 1368}