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