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