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