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