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