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