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