Add a test.
[thirdparty/mdadm.git] / mdadm.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
cd29a5c8 4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
64c4757e
NB
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
c82f047c 28 *
dfd4d8ee 29 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
c82f047c 30 * Paul Clements, SteelEye Technology, Inc.
64c4757e
NB
31 */
32
9a9dab36 33#include "mdadm.h"
64c4757e 34#include "md_p.h"
dd0781e5 35#include <ctype.h>
64c4757e 36
dd0781e5 37
52826846
NB
38int main(int argc, char *argv[])
39{
e0d19036 40 int mode = 0;
52826846 41 int opt;
e0d19036 42 int option_index;
52826846
NB
43 char *help_text;
44 char *c;
45 int rv;
f9ce90ba 46 int i;
52826846
NB
47
48 int chunk = 0;
dd0781e5 49 int size = -1;
98c6faba
NB
50 int level = UnSet;
51 int layout = UnSet;
52826846 52 int raiddisks = 0;
ea329559 53 int max_disks = MD_SB_DISKS;
52826846
NB
54 int sparedisks = 0;
55 struct mddev_ident_s ident;
56 char *configfile = NULL;
cd29a5c8 57 char *cp;
5787fa49 58 char *update = NULL;
52826846
NB
59 int scan = 0;
60 char devmode = 0;
61 int runstop = 0;
62 int readonly = 0;
dfd4d8ee 63 int write_behind = 0;
c82f047c
NB
64 int bitmap_fd = -1;
65 char *bitmap_file = NULL;
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;
cd29a5c8 73 int brief = 0;
52826846 74 int force = 0;
feb716e9 75 int test = 0;
dd0781e5 76 int assume_clean = 0;
8d80900b
NB
77 int autof = 0; /* -2 means create device based on name:
78 * if it ends mdN, then non-partitioned array N
79 * if it ends dN, then partitions array N
80 * -1 means create non-partitioned, choose N
81 * 1 or more to create partitioned
82 * If -1 or 1 and name is a 'standard' name, then
83 * insist on a match of type and number.
84 */
52826846
NB
85
86 char *mailaddr = NULL;
87 char *program = NULL;
88 int delay = 0;
d013a55e 89 int daemonise = 0;
b5e64645 90 char *pidfile = NULL;
aa88f531 91 int oneshot = 0;
82d9eba6 92 struct supertype *ss = NULL;
dfd4d8ee 93 int writemostly = 0;
52826846 94
e5329c37
NB
95 int copies;
96
52826846
NB
97 int mdfd = -1;
98
dfe47e00
NB
99 srandom(time(0) ^ getpid());
100
52826846 101 ident.uuid_set=0;
98c6faba
NB
102 ident.level = UnSet;
103 ident.raid_disks = UnSet;
104 ident.super_minor= UnSet;
52826846 105 ident.devices=0;
a75f2fbc
NB
106 ident.spare_group = NULL;
107 ident.autof = 0;
a825febc 108 ident.st = NULL;
c82f047c 109 ident.bitmap_fd = -1;
947fd4dd 110 ident.name[0] = 0;
52826846 111
e0d19036
NB
112 while ((option_index = -1) ,
113 (opt=getopt_long(argc, argv,
52826846 114 short_options, long_options,
e0d19036
NB
115 &option_index)) != -1) {
116 int newmode = mode;
117 /* firstly, so mode-independant options */
52826846 118 switch(opt) {
52826846
NB
119 case 'h':
120 help_text = Help;
56eedc1a
NB
121 if (option_index > 0 &&
122 strcmp(long_options[option_index].name, "help-options")==0)
123 help_text = OptionHelp;
124 else
125 switch (mode) {
126 case ASSEMBLE : help_text = Help_assemble; break;
127 case BUILD : help_text = Help_build; break;
128 case CREATE : help_text = Help_create; break;
129 case MANAGE : help_text = Help_manage; break;
130 case MISC : help_text = Help_misc; break;
131 case MONITOR : help_text = Help_monitor; break;
dd0781e5 132 case GROW : help_text = Help_grow; break;
56eedc1a 133 }
52826846
NB
134 fputs(help_text,stderr);
135 exit(0);
136
137 case 'V':
138 fputs(Version, stderr);
139 exit(0);
140
22892d56 141 case 'v': verbose++;
52826846
NB
142 continue;
143
c82f047c 144 case 'b':
f5e166fe 145 if (mode == ASSEMBLE || mode == BUILD || mode == CREATE || mode == GROW)
c82f047c
NB
146 break; /* b means bitmap */
147 brief = 1;
cd29a5c8
NB
148 continue;
149
e0d19036
NB
150 case ':':
151 case '?':
152 fputs(Usage, stderr);
153 exit(2);
154 }
155 /* second, figure out the mode.
156 * Some options force the mode. Others
157 * set the mode if it isn't already
158 */
159
160 switch(opt) {
161 case '@': /* just incase they say --manage */
162 newmode = MANAGE; break;
163 case 'a':
164 case 'r':
165 case 'f':
0320ea45
NB
166 if (!mode) newmode = MANAGE;
167 break;
e0d19036
NB
168
169 case 'A': newmode = ASSEMBLE; break;
170 case 'B': newmode = BUILD; break;
171 case 'C': newmode = CREATE; break;
172 case 'F': newmode = MONITOR;break;
dd0781e5 173 case 'G': newmode = GROW; break;
e0d19036
NB
174
175 case '#':
176 case 'D':
177 case 'E':
c82f047c 178 case 'X':
e0d19036
NB
179 case 'Q': newmode = MISC; break;
180 case 'R':
181 case 'S':
182 case 'o':
183 case 'w':
184 case 'K': if (!mode) newmode = MISC; break;
185 }
186 if (mode && newmode == mode) {
187 /* everybody happy ! */
188 } else if (mode && newmode != mode) {
189 /* not allowed.. */
190 fprintf(stderr, Name ": ");
191 if (option_index >= 0)
192 fprintf(stderr, "--%s", long_options[option_index].name);
193 else
194 fprintf(stderr, "-%c", opt);
e6b64cd0 195 fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
e0d19036
NB
196 map_num(modes, newmode),
197 map_num(modes, mode));
198 exit(2);
199 } else if (!mode && newmode) {
200 mode = newmode;
201 } else {
202 /* special case of -c --help */
203 if (opt == 'c' &&
204 ( strncmp(optarg, "--h", 3)==0 ||
205 strncmp(optarg, "-h", 2)==0)) {
206 fputs(Help_config, stderr);
207 exit(0);
cd29a5c8 208 }
0320ea45
NB
209
210 /* If first option is a device, don't force the mode yet */
211 if (opt == 1) {
212 if (devs_found == 0) {
213 dv = malloc(sizeof(*dv));
214 if (dv == NULL) {
215 fprintf(stderr, Name ": malloc failed\n");
216 exit(3);
217 }
218 dv->devname = optarg;
219 dv->disposition = devmode;
dfd4d8ee 220 dv->writemostly = writemostly;
0320ea45
NB
221 dv->next = NULL;
222 *devlistend = dv;
223 devlistend = &dv->next;
224
225 devs_found++;
226 continue;
227 }
228 /* No mode yet, and this is the second device ... */
229 fprintf(stderr, Name ": An option must be given to set the mode before a second device is listed\n");
230 exit(2);
231 }
e0d19036 232 if (option_index >= 0)
0320ea45 233 fprintf(stderr, Name ": --%s", long_options[option_index].name);
e0d19036 234 else
0320ea45
NB
235 fprintf(stderr, Name ": -%c", opt);
236 fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
e0d19036
NB
237 exit(2);
238 }
239
240 /* if we just set the mode, then done */
241 switch(opt) {
242 case '@':
243 case '#':
244 case 'A':
245 case 'B':
246 case 'C':
247 case 'F':
dd0781e5 248 case 'G':
e0d19036
NB
249 continue;
250 }
251 if (opt == 1) {
252 /* an undecorated option - must be a device name.
253 */
cd29a5c8 254 if (devs_found > 0 && mode == '@' && !devmode) {
dd0781e5 255 fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
52826846
NB
256 exit(2);
257 }
e5329c37
NB
258 if (devs_found > 0 && mode == 'G' && !devmode) {
259 fprintf(stderr, Name ": Must give one of -a for devices do add: %s\n", optarg);
260 exit(2);
261 }
cd29a5c8
NB
262 dv = malloc(sizeof(*dv));
263 if (dv == NULL) {
264 fprintf(stderr, Name ": malloc failed\n");
265 exit(3);
266 }
267 dv->devname = optarg;
268 dv->disposition = devmode;
dfd4d8ee 269 dv->writemostly = writemostly;
cd29a5c8
NB
270 dv->next = NULL;
271 *devlistend = dv;
272 devlistend = &dv->next;
273
52826846
NB
274 devs_found++;
275 continue;
682c7051 276 }
682c7051 277
52826846
NB
278 /* We've got a mode, and opt is now something else which
279 * could depend on the mode */
280#define O(a,b) ((a<<8)|b)
281 switch (O(mode,opt)) {
e0d19036
NB
282 case O(CREATE,'c'):
283 case O(BUILD,'c'): /* chunk or rounding */
52826846
NB
284 if (chunk) {
285 fprintf(stderr, Name ": chunk/rounding may only be specified once. "
286 "Second value is %s.\n", optarg);
287 exit(2);
288 }
289 chunk = strtol(optarg, &c, 10);
290 if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) {
291 fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
292 optarg);
293 exit(2);
294 }
295 continue;
64c4757e 296
f9ce90ba
NB
297 case O(CREATE,'e'):
298 case O(ASSEMBLE,'e'):
299 case O(MISC,'e'): /* set metadata (superblock) information */
300 if (ss) {
301 fprintf(stderr, Name ": metadata information already given\n");
302 exit(2);
303 }
82d9eba6
NB
304 for(i=0; !ss && superlist[i]; i++)
305 ss = superlist[i]->match_metadata_desc(optarg);
306
f9ce90ba
NB
307 if (!ss) {
308 fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg);
309 exit(2);
310 }
ea329559 311 max_disks = ss->max_devs;
f9ce90ba
NB
312 continue;
313
dfd4d8ee
NB
314 case O(MANAGE,'W'):
315 case O(BUILD,'W'):
316 case O(CREATE,'W'):
317 /* set write-mostly for following devices */
318 writemostly = 1;
319 continue;
320
dd0781e5 321 case O(GROW,'z'):
e0d19036 322 case O(CREATE,'z'): /* size */
dd0781e5 323 if (size >= 0) {
52826846
NB
324 fprintf(stderr, Name ": size may only be specified once. "
325 "Second value is %s.\n", optarg);
326 exit(2);
327 }
dd0781e5
NB
328 if (strcmp(optarg, "max")==0)
329 size = 0;
330 else {
331 size = strtol(optarg, &c, 10);
332 if (!optarg[0] || *c || size < 4) {
333 fprintf(stderr, Name ": invalid size: %s\n",
334 optarg);
335 exit(2);
336 }
52826846
NB
337 }
338 continue;
64c4757e 339
b5e64645 340 case O(GROW,'l'): /* hack - needed to understand layout */
e0d19036
NB
341 case O(CREATE,'l'):
342 case O(BUILD,'l'): /* set raid level*/
98c6faba 343 if (level != UnSet) {
52826846
NB
344 fprintf(stderr, Name ": raid level may only be set once. "
345 "Second value is %s.\n", optarg);
346 exit(2);
347 }
348 level = map_name(pers, optarg);
98c6faba 349 if (level == UnSet) {
52826846
NB
350 fprintf(stderr, Name ": invalid raid level: %s\n",
351 optarg);
352 exit(2);
353 }
b5e64645 354 if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) {
52826846
NB
355 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
356 optarg);
357 exit(2);
358 }
e0d19036 359 if (sparedisks > 0 && level < 1 && level >= -1) {
b83d95f3 360 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
52826846
NB
361 optarg);
362 exit(2);
363 }
cd29a5c8 364 ident.level = level;
52826846 365 continue;
64c4757e 366
e0d19036 367 case O(CREATE,'p'): /* raid5 layout */
b5e64645
NB
368 case O(BUILD,'p'): /* faulty layout */
369 case O(GROW, 'p'): /* faulty reconfig */
e5329c37 370 if (layout != UnSet) {
52826846
NB
371 fprintf(stderr,Name ": layout may only be sent once. "
372 "Second value was %s\n", optarg);
373 exit(2);
374 }
375 switch(level) {
376 default:
56eb10c0 377 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
52826846
NB
378 map_num(pers, level));
379 exit(2);
98c6faba 380 case UnSet:
52826846
NB
381 fprintf(stderr, Name ": raid level must be given before layout.\n");
382 exit(2);
383
384 case 5:
98c6faba 385 case 6:
52826846 386 layout = map_name(r5layout, optarg);
98c6faba 387 if (layout==UnSet) {
52826846
NB
388 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
389 optarg);
390 exit(2);
391 }
392 break;
e5329c37
NB
393
394 case 10:
395 /* 'f' or 'n' followed by a number <= raid_disks */
396 if ((optarg[0] != 'n' && optarg[0] != 'f') ||
397 (copies = strtoul(optarg+1, &cp, 10)) < 1 ||
398 copies > 200 ||
399 *cp) {
400 fprintf(stderr, Name ": layout for raid10 must be 'nNN' or 'fNN' where NN is a number, not %s\n", optarg);
401 exit(2);
402 }
403 if (optarg[0] == 'n')
404 layout = 256 + copies;
405 else
406 layout = 1 + (copies<<8);
407 break;
b5e64645
NB
408 case -5: /* Faulty
409 * modeNNN
410 */
411
412 {
413 int ln = strcspn(optarg, "0123456789");
414 char *m = strdup(optarg);
415 int mode;
416 m[ln] = 0;
417 mode = map_name(faultylayout, m);
418 if (mode == UnSet) {
419 fprintf(stderr, Name ": layout %s not understood for faulty.\n",
420 optarg);
421 exit(2);
422 }
423 layout = mode | (atoi(optarg+ln)<< ModeShift);
424 }
52826846
NB
425 }
426 continue;
427
dd0781e5
NB
428 case O(CREATE,3):
429 case O(BUILD,3): /* assume clean */
430 assume_clean = 1;
431 continue;
432
433 case O(GROW,'n'):
e0d19036
NB
434 case O(CREATE,'n'):
435 case O(BUILD,'n'): /* number of raid disks */
52826846 436 if (raiddisks) {
b83d95f3 437 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
52826846
NB
438 raiddisks, optarg);
439 exit(2);
440 }
441 raiddisks = strtol(optarg, &c, 10);
e4c4352e 442 if (!optarg[0] || *c || raiddisks<=0) {
b83d95f3 443 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
52826846
NB
444 optarg);
445 exit(2);
446 }
cd29a5c8 447 ident.raid_disks = raiddisks;
52826846
NB
448 continue;
449
e0d19036 450 case O(CREATE,'x'): /* number of spare (eXtra) discs */
52826846 451 if (sparedisks) {
b83d95f3 452 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
52826846
NB
453 sparedisks, optarg);
454 exit(2);
455 }
98c6faba 456 if (level != UnSet && level <= 0 && level >= -1) {
b83d95f3 457 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
52826846
NB
458 level);
459 exit(2);
460 }
461 sparedisks = strtol(optarg, &c, 10);
e4c4352e 462 if (!optarg[0] || *c || sparedisks < 0) {
b83d95f3 463 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
52826846
NB
464 optarg);
465 exit(2);
466 }
467 continue;
dd0781e5
NB
468
469 case O(CREATE,'a'):
470 case O(BUILD,'a'):
471 case O(ASSEMBLE,'a'): /* auto-creation of device node */
472 if (optarg == NULL)
8d80900b 473 autof = -2;
dd0781e5
NB
474 else if (strcasecmp(optarg,"no")==0)
475 autof = 0;
8d80900b
NB
476 else if (strcasecmp(optarg,"yes")==0)
477 autof = -2;
478 else if (strcasecmp(optarg,"md")==0)
dd0781e5
NB
479 autof = -1;
480 else {
481 /* There might be digits, and maybe a hypen, at the end */
482 char *e = optarg + strlen(optarg);
483 int num = 4;
484 int len;
485 while (e > optarg && isdigit(e[-1]))
486 e--;
487 if (*e) {
488 num = atoi(e);
489 if (num <= 0) num = 1;
490 }
491 if (e > optarg && e[-1] == '-')
492 e--;
493 len = e - optarg;
494 if ((len == 3 && strncasecmp(optarg,"mdp",3)==0) ||
495 (len == 1 && strncasecmp(optarg,"p",1)==0) ||
496 (len >= 4 && strncasecmp(optarg,"part",4)==0))
497 autof = num;
498 else {
499 fprintf(stderr, Name ": --auto flag arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
500 " optionally followed by a number.\n",
501 optarg);
502 exit(2);
503 }
504 }
505 continue;
506
aa88f531 507 case O(BUILD,'f'): /* force honouring '-n 1' */
bd72c2b2 508 case O(GROW,'f'): /* ditto */
e0d19036
NB
509 case O(CREATE,'f'): /* force honouring of device list */
510 case O(ASSEMBLE,'f'): /* force assembly */
511 case O(MISC,'f'): /* force zero */
52826846
NB
512 force=1;
513 continue;
514
515 /* now for the Assemble options */
e0d19036 516 case O(ASSEMBLE,'u'): /* uuid of array */
52826846 517 if (ident.uuid_set) {
cd29a5c8 518 fprintf(stderr, Name ": uuid cannot be set twice. "
52826846
NB
519 "Second value %s.\n", optarg);
520 exit(2);
521 }
522 if (parse_uuid(optarg, ident.uuid))
523 ident.uuid_set = 1;
524 else {
525 fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
526 exit(2);
527 }
528 continue;
529
947fd4dd
NB
530 case O(CREATE,'N'):
531 case O(ASSEMBLE,'N'):
532 if (ident.name[0]) {
533 fprintf(stderr, Name ": name cannot be set twice. "
534 "Second value %s.\n", optarg);
535 exit(2);
536 }
537 if (strlen(optarg) > 32) {
538 fprintf(stderr, Name ": name '%s' is too long, 32 chars max.\n",
539 optarg);
540 exit(2);
541 }
542 strcpy(ident.name, optarg);
543 continue;
544
e0d19036 545 case O(ASSEMBLE,'m'): /* super-minor for array */
98c6faba 546 if (ident.super_minor != UnSet) {
cd29a5c8
NB
547 fprintf(stderr, Name ": super-minor cannot be set twice. "
548 "Second value: %s.\n", optarg);
549 exit(2);
550 }
d013a55e
NB
551 if (strcmp(optarg, "dev")==0)
552 ident.super_minor = -2;
553 else {
554 ident.super_minor = strtoul(optarg, &cp, 10);
555 if (!optarg[0] || *cp) {
556 fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
557 exit(2);
558 }
cd29a5c8
NB
559 }
560 continue;
561
5787fa49
NB
562 case O(ASSEMBLE,'U'): /* update the superblock */
563 if (update) {
564 fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n",
565 update, optarg);
566 exit(2);
567 }
568 update = optarg;
e5329c37
NB
569 if (strcmp(update, "sparc2.2")==0)
570 continue;
5787fa49
NB
571 if (strcmp(update, "super-minor") == 0)
572 continue;
feb716e9
NB
573 if (strcmp(update, "summaries")==0)
574 continue;
e5329c37
NB
575 if (strcmp(update, "resync")==0)
576 continue;
586ed405
NB
577 if (strcmp(update, "byteorder")==0) {
578 if (ss) {
579 fprintf(stderr, Name ": must not set metadata type with --update=byteorder.\n");
580 exit(2);
581 }
582 for(i=0; !ss && superlist[i]; i++)
583 ss = superlist[i]->match_metadata_desc("0.swap");
584 if (!ss) {
585 fprintf(stderr, Name ": INTERNAL ERROR cannot find 0.swap\n");
586 exit(2);
587 }
588
589 continue;
590 }
e5329c37 591 fprintf(stderr, Name ": '--update %s' invalid. Only 'sparc2.2', 'super-minor', 'resync' or 'summaries' supported\n",update);
5787fa49
NB
592 exit(2);
593
e0d19036 594 case O(ASSEMBLE,'c'): /* config file */
2d465520 595 case O(MISC, 'c'):
e0d19036 596 case O(MONITOR,'c'):
52826846
NB
597 if (configfile) {
598 fprintf(stderr, Name ": configfile cannot be set twice. "
599 "Second value is %s.\n", optarg);
600 exit(2);
601 }
602 configfile = optarg;
603 /* FIXME possibly check that config file exists. Even parse it */
604 continue;
e0d19036
NB
605 case O(ASSEMBLE,'s'): /* scan */
606 case O(MISC,'s'):
607 case O(MONITOR,'s'):
52826846
NB
608 scan = 1;
609 continue;
610
e0d19036 611 case O(MONITOR,'m'): /* mail address */
52826846
NB
612 if (mailaddr)
613 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
614 optarg);
615 else
616 mailaddr = optarg;
617 continue;
618
e0d19036 619 case O(MONITOR,'p'): /* alert program */
52826846
NB
620 if (program)
621 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
622 optarg);
623 else
624 program = optarg;
625 continue;
64c4757e 626
e0d19036 627 case O(MONITOR,'d'): /* delay in seconds */
f5e166fe 628 case O(GROW, 'd'):
c82f047c
NB
629 case O(BUILD,'d'): /* delay for bitmap updates */
630 case O(CREATE,'d'):
52826846
NB
631 if (delay)
632 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
633 optarg);
634 else {
635 delay = strtol(optarg, &c, 10);
636 if (!optarg[0] || *c || delay<1) {
637 fprintf(stderr, Name ": invalid delay: %s\n",
638 optarg);
639 exit(2);
640 }
641 }
642 continue;
d013a55e
NB
643 case O(MONITOR,'f'): /* daemonise */
644 daemonise = 1;
645 continue;
b5e64645
NB
646 case O(MONITOR,'i'): /* pid */
647 if (pidfile)
648 fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
649 optarg);
650 else
651 pidfile = optarg;
652 continue;
aa88f531
NB
653 case O(MONITOR,'1'): /* oneshot */
654 oneshot = 1;
655 continue;
98c6faba
NB
656 case O(MONITOR,'t'): /* test */
657 test = 1;
658 continue;
52826846
NB
659
660 /* now the general management options. Some are applicable
661 * to other modes. None have arguments.
662 */
e5329c37 663 case O(GROW,'a'):
dd0781e5 664 case O(MANAGE,'a'): /* add a drive */
52826846
NB
665 devmode = 'a';
666 continue;
e0d19036 667 case O(MANAGE,'r'): /* remove a drive */
52826846
NB
668 devmode = 'r';
669 continue;
e0d19036 670 case O(MANAGE,'f'): /* set faulty */
52826846
NB
671 devmode = 'f';
672 continue;
e0d19036
NB
673 case O(MANAGE,'R'):
674 case O(ASSEMBLE,'R'):
675 case O(BUILD,'R'):
676 case O(CREATE,'R'): /* Run the array */
52826846
NB
677 if (runstop < 0) {
678 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
679 exit(2);
680 }
681 runstop = 1;
682 continue;
e0d19036 683 case O(MANAGE,'S'):
52826846
NB
684 if (runstop > 0) {
685 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
686 exit(2);
687 }
688 runstop = -1;
689 continue;
690
e0d19036 691 case O(MANAGE,'o'):
52826846
NB
692 if (readonly < 0) {
693 fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
694 exit(2);
695 }
696 readonly = 1;
697 continue;
e0d19036 698 case O(MANAGE,'w'):
52826846 699 if (readonly > 0) {
e0d19036 700 fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n");
52826846
NB
701 exit(2);
702 }
703 readonly = -1;
704 continue;
e0d19036
NB
705
706 case O(MISC,'Q'):
707 case O(MISC,'D'):
708 case O(MISC,'E'):
709 case O(MISC,'K'):
710 case O(MISC,'R'):
711 case O(MISC,'S'):
c82f047c 712 case O(MISC,'X'):
e0d19036
NB
713 case O(MISC,'o'):
714 case O(MISC,'w'):
715 if (devmode && devmode != opt &&
716 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
717 fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n",
718 devmode =='E'?opt:devmode);
719 exit(2);
720 }
721 devmode = opt;
722 continue;
feb716e9
NB
723 case O(MISC,'t'):
724 test = 1;
725 continue;
e0d19036 726
bd526cee
NB
727 case O(MISC, 22):
728 if (devmode != 'E') {
729 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
730 exit(2);
731 }
732 SparcAdjust = 1;
733 continue;
c82f047c
NB
734
735 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
736 if (!optarg) {
737 fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n");
738 exit(2);
739 }
55935d51
NB
740 if (strcmp(optarg, "internal")==0) {
741 fprintf(stderr, Name ": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
742 continue;
743 }
c82f047c
NB
744 bitmap_fd = open(optarg, O_RDWR);
745 if (!*optarg || bitmap_fd < 0) {
746 fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno));
747 exit(2);
748 }
749 ident.bitmap_fd = bitmap_fd; /* for Assemble */
750 continue;
f5e166fe
NB
751
752 case O(GROW,'b'):
c82f047c
NB
753 case O(BUILD,'b'):
754 case O(CREATE,'b'): /* here we create the bitmap */
755 bitmap_file = optarg;
756 continue;
757
f5e166fe 758 case O(GROW,4):
c82f047c
NB
759 case O(BUILD,4):
760 case O(CREATE,4): /* bitmap chunksize */
761 bitmap_chunk = strtol(optarg, &c, 10);
762 if (!optarg[0] || *c || bitmap_chunk < 0 ||
763 bitmap_chunk & (bitmap_chunk - 1)) {
764 fprintf(stderr, Name ": invalid bitmap chunksize: %s\n",
765 optarg);
766 exit(2);
767 }
768 /* convert K to B, chunk of 0K means 512B */
769 bitmap_chunk = bitmap_chunk ? bitmap_chunk * 1024 : 512;
770 continue;
dfd4d8ee
NB
771
772 case O(BUILD, 5):
773 case O(CREATE, 5): /* write-behind mode */
774 write_behind = DEFAULT_MAX_WRITE_BEHIND;
775 if (optarg) {
776 write_behind = strtol(optarg, &c, 10);
777 if (write_behind < 0 || *c ||
778 write_behind > 16383) {
779 fprintf(stderr, Name ": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
780 exit(2);
781 }
782 }
783 continue;
52826846
NB
784 }
785 /* We have now processed all the valid options. Anything else is
786 * an error
787 */
e0d19036
NB
788 fprintf(stderr, Name ": option %c not valid in %s mode\n",
789 opt, map_num(modes, mode));
64c4757e 790 exit(2);
52826846
NB
791
792 }
793
0320ea45
NB
794 if (!mode && devs_found) {
795 mode = MISC;
796 devmode = 'Q';
797 if (devlist->disposition == 0)
798 devlist->disposition = devmode;
799 }
52826846
NB
800 if (!mode) {
801 fputs(Usage, stderr);
64c4757e 802 exit(2);
64c4757e 803 }
52826846
NB
804 /* Ok, got the option parsing out of the way
805 * hopefully it's mostly right but there might be some stuff
806 * missing
807 *
e0d19036 808 * That is mosty checked in the per-mode stuff but...
52826846
NB
809 *
810 * For @,B,C and A without -s, the first device listed must be an md device
811 * we check that here and open it.
64c4757e 812 */
52826846 813
dd0781e5
NB
814 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
815 (mode == ASSEMBLE && ! scan)) {
52826846
NB
816 if (devs_found < 1) {
817 fprintf(stderr, Name ": an md device must be given in this mode\n");
818 exit(2);
819 }
dd0781e5
NB
820 if ((int)ident.super_minor == -2 && autof) {
821 fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
822 exit(2);
823 }
824 mdfd = open_mddev(devlist->devname, autof);
52826846
NB
825 if (mdfd < 0)
826 exit(1);
98c6faba 827 if ((int)ident.super_minor == -2) {
d013a55e
NB
828 struct stat stb;
829 fstat(mdfd, &stb);
0df46c2a 830 ident.super_minor = minor(stb.st_rdev);
d013a55e 831 }
64c4757e 832 }
52826846 833
e4c4352e
NB
834 if (raiddisks) {
835 if (raiddisks > max_disks) {
6fbba4c9
NB
836 fprintf(stderr, Name ": invalid number of raid devices: %d\n",
837 raiddisks);
e4c4352e
NB
838 exit(2);
839 }
840 if (raiddisks == 1 && !force && level != -5) {
841 fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
842 " a mistake. If you really mean it you will need to specify --force before\n"
843 " setting the number of drives.\n");
844 exit(2);
845 }
846 }
847 if (sparedisks) {
848 if ( sparedisks > max_disks - raiddisks) {
6fbba4c9
NB
849 fprintf(stderr, Name ": invalid number of spare-devices: %d\n",
850 sparedisks);
e4c4352e
NB
851 exit(2);
852 }
853 }
c82f047c 854
52826846
NB
855 rv = 0;
856 switch(mode) {
e0d19036 857 case MANAGE:
52826846
NB
858 /* readonly, add/remove, readwrite, runstop */
859 if (readonly>0)
cd29a5c8 860 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 861 if (!rv && devs_found>1)
cd29a5c8
NB
862 rv = Manage_subdevs(devlist->devname, mdfd,
863 devlist->next);
52826846 864 if (!rv && readonly < 0)
cd29a5c8 865 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 866 if (!rv && runstop)
91f068bf 867 rv = Manage_runstop(devlist->devname, mdfd, runstop, 0);
52826846 868 break;
e0d19036 869 case ASSEMBLE:
d013a55e 870 if (devs_found == 1 && ident.uuid_set == 0 &&
947fd4dd 871 ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) {
d013a55e
NB
872 /* Only a device has been given, so get details from config file */
873 mddev_ident_t array_ident = conf_get_ident(configfile, devlist->devname);
b5e64645
NB
874 if (array_ident == NULL) {
875 fprintf(stderr, Name ": %s not identified in config file.\n",
876 devlist->devname);
d013a55e 877 rv |= 1;
b5e64645 878 } else {
1337546d
NB
879 mdfd = open_mddev(devlist->devname,
880 array_ident->autof ? array_ident->autof : autof);
b5e64645 881 if (mdfd < 0)
d013a55e 882 rv |= 1;
b5e64645 883 else {
f9ce90ba 884 rv |= Assemble(ss, devlist->devname, mdfd, array_ident, configfile,
d013a55e
NB
885 NULL,
886 readonly, runstop, update, verbose, force);
b5e64645
NB
887 close(mdfd);
888 }
d013a55e
NB
889 }
890 } else if (!scan)
f9ce90ba 891 rv = Assemble(ss, devlist->devname, mdfd, &ident, configfile,
cd29a5c8 892 devlist->next,
5787fa49
NB
893 readonly, runstop, update, verbose, force);
894 else if (devs_found>0) {
895 if (update && devs_found > 1) {
896 fprintf(stderr, Name ": can only update a single array at a time\n");
897 exit(1);
898 }
cd29a5c8
NB
899 for (dv = devlist ; dv ; dv=dv->next) {
900 mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname);
52826846
NB
901 if (array_ident == NULL) {
902 fprintf(stderr, Name ": %s not identified in config file.\n",
cd29a5c8 903 dv->devname);
52826846
NB
904 rv |= 1;
905 continue;
906 }
1337546d
NB
907 mdfd = open_mddev(dv->devname,
908 array_ident->autof ?array_ident->autof : autof);
b5e64645
NB
909 if (mdfd < 0) {
910 rv |= 1;
911 continue;
912 }
f9ce90ba 913 rv |= Assemble(ss, dv->devname, mdfd, array_ident, configfile,
cd29a5c8 914 NULL,
5787fa49 915 readonly, runstop, update, verbose, force);
b5e64645 916 close(mdfd);
52826846 917 }
5787fa49 918 } else {
52826846
NB
919 mddev_ident_t array_list = conf_get_ident(configfile, NULL);
920 if (!array_list) {
921 fprintf(stderr, Name ": No arrays found in config file\n");
922 rv = 1;
923 } else
924 for (; array_list; array_list = array_list->next) {
cd29a5c8 925 mdu_array_info_t array;
1337546d
NB
926 mdfd = open_mddev(array_list->devname,
927 array_list->autof ? array_list->autof : autof);
52826846
NB
928 if (mdfd < 0) {
929 rv |= 1;
930 continue;
931 }
cd29a5c8
NB
932 if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
933 /* already assembled, skip */
43fc1676
NB
934 ;
935 else
f9ce90ba 936 rv |= Assemble(ss, array_list->devname, mdfd,
43fc1676
NB
937 array_list, configfile,
938 NULL,
939 readonly, runstop, NULL, verbose, force);
940 close(mdfd);
52826846
NB
941 }
942 }
943 break;
e0d19036 944 case BUILD:
c82f047c
NB
945 if (bitmap_chunk == UnSet) bitmap_chunk = DEFAULT_BITMAP_CHUNK;
946 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
dfd4d8ee
NB
947 if (write_behind && !bitmap_file) {
948 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
949 rv = 1;
950 break;
951 }
952
c82f047c 953 if (bitmap_file) {
55935d51
NB
954 if (strcmp(bitmap_file, "internal")==0) {
955 fprintf(stderr, Name ": 'internal' bitmaps not supported with --build\n");
956 rv |= 1;
957 break;
958 }
c82f047c
NB
959 bitmap_fd = open(bitmap_file, O_RDWR,0);
960 if (bitmap_fd < 0 && errno != ENOENT) {
961 perror(Name ": cannot create bitmap file");
962 rv |= 1;
963 break;
964 }
965 if (bitmap_fd < 0) {
966 bitmap_fd = CreateBitmap(bitmap_file, force, NULL,
dfd4d8ee 967 bitmap_chunk, delay, write_behind, size);
c82f047c
NB
968 }
969 }
970 rv = Build(devlist->devname, mdfd, chunk, level, layout,
971 raiddisks, devlist->next, assume_clean,
dfd4d8ee 972 bitmap_file, bitmap_chunk, write_behind, delay);
52826846 973 break;
e0d19036 974 case CREATE:
c82f047c 975 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
dfd4d8ee
NB
976 if (write_behind && !bitmap_file) {
977 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
978 rv = 1;
979 break;
980 }
f9ce90ba 981 if (ss == NULL) {
82d9eba6
NB
982 for(i=0; !ss && superlist[i]; i++)
983 ss = superlist[i]->match_metadata_desc("default");
f9ce90ba
NB
984 }
985 if (!ss) {
986 fprintf(stderr, Name ": internal error - no default metadata style\n");
987 exit(2);
988 }
989
990 rv = Create(ss, devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size,
947fd4dd 991 raiddisks, sparedisks, ident.name,
c82f047c 992 devs_found-1, devlist->next, runstop, verbose, force,
dfd4d8ee 993 bitmap_file, bitmap_chunk, write_behind, delay);
52826846 994 break;
e0d19036
NB
995 case MISC:
996
997 if (devmode == 'E') {
998 if (devlist == NULL && !scan) {
999 fprintf(stderr, Name ": No devices to examine\n");
1000 exit(2);
1001 }
1002 if (devlist == NULL)
1003 devlist = conf_get_devs(configfile);
1004 if (devlist == NULL) {
c913b90e 1005 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
e0d19036
NB
1006 exit(1);
1007 }
22892d56 1008 rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief, scan, SparcAdjust, ss);
e0d19036
NB
1009 } else {
1010 if (devlist == NULL) {
91f068bf
NB
1011 if (devmode=='D' && scan) {
1012 /* apply --detail to all devices in /proc/mdstat */
dd0781e5 1013 struct mdstat_ent *ms = mdstat_read(0);
e0d19036
NB
1014 struct mdstat_ent *e;
1015 for (e=ms ; e ; e=e->next) {
1016 char *name = get_md_name(e->devnum);
1017
1018 if (!name) {
1019 fprintf(stderr, Name ": cannot find device file for %s\n",
1020 e->dev);
1021 continue;
1022 }
22892d56 1023 rv |= Detail(name, verbose>1?0:verbose+1, test);
91f068bf
NB
1024 put_md_name(name);
1025 }
1026 } else if (devmode == 'S' && scan) {
1027 /* apply --stop to all devices in /proc/mdstat */
1028 /* Due to possible stacking of devices, repeat until
1029 * nothing more can be stopped
1030 */
1031 int progress=1, err;
1032 int last = 0;
1033 do {
1034 struct mdstat_ent *ms = mdstat_read(0);
1035 struct mdstat_ent *e;
1036
1037 if (!progress) last = 1;
1038 progress = 0; err = 0;
1039 for (e=ms ; e ; e=e->next) {
1040 char *name = get_md_name(e->devnum);
1041
1042 if (!name) {
1043 fprintf(stderr, Name ": cannot find device file for %s\n",
1044 e->dev);
1045 continue;
1046 }
dd0781e5 1047 mdfd = open_mddev(name, 0);
43fc1676 1048 if (mdfd >= 0) {
91f068bf
NB
1049 if (Manage_runstop(name, mdfd, -1, !last))
1050 err = 1;
1051 else
1052 progress = 1;
43fc1676
NB
1053 close(mdfd);
1054 }
91f068bf
NB
1055
1056 put_md_name(name);
e0d19036 1057 }
91f068bf
NB
1058 } while (!last && err);
1059 } else {
e0d19036
NB
1060 fprintf(stderr, Name ": No devices given.\n");
1061 exit(2);
1062 }
1063 }
1064 for (dv=devlist ; dv; dv=dv->next) {
1065 switch(dv->disposition) {
1066 case 'D':
22892d56 1067 rv |= Detail(dv->devname, brief?1+verbose:0, test); continue;
e0d19036
NB
1068 case 'K': /* Zero superblock */
1069 rv |= Kill(dv->devname, force); continue;
1070 case 'Q':
1071 rv |= Query(dv->devname); continue;
c82f047c 1072 case 'X':
55935d51 1073 rv |= ExamineBitmap(dv->devname, brief, ss); continue;
e0d19036 1074 }
dd0781e5 1075 mdfd = open_mddev(dv->devname, 0);
43fc1676 1076 if (mdfd>=0) {
e0d19036
NB
1077 switch(dv->disposition) {
1078 case 'R':
91f068bf 1079 rv |= Manage_runstop(dv->devname, mdfd, 1, 0); break;
e0d19036 1080 case 'S':
91f068bf 1081 rv |= Manage_runstop(dv->devname, mdfd, -1, 0); break;
e0d19036
NB
1082 case 'o':
1083 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1084 case 'w':
1085 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1086 }
43fc1676
NB
1087 close(mdfd);
1088 }
e0d19036 1089 }
cd29a5c8 1090 }
9a9dab36 1091 break;
e0d19036 1092 case MONITOR:
e0d19036
NB
1093 if (!devlist && !scan) {
1094 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
1095 rv = 1;
1096 break;
1097 }
b5e64645
NB
1098 if (pidfile && !daemonise) {
1099 fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
1100 rv = 1;
1101 break;
1102 }
d013a55e 1103 rv= Monitor(devlist, mailaddr, program,
b5e64645 1104 delay?delay:60, daemonise, scan, oneshot, configfile, test, pidfile);
9a9dab36 1105 break;
dd0781e5
NB
1106
1107 case GROW:
1108 if (devs_found > 1) {
e5329c37
NB
1109
1110 /* must be '-a'. */
1111 if (size >= 0 || raiddisks) {
1112 fprintf(stderr, Name ": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1113 rv = 1;
1114 break;
1115 }
1116 for (dv=devlist->next; dv ; dv=dv->next) {
1117 rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
1118 if (rv)
1119 break;
1120 }
f5e166fe
NB
1121 } else if ((size >= 0) + (raiddisks != 0) + (layout != UnSet) + (bitmap_file != NULL)> 1) {
1122 fprintf(stderr, Name ": can change at most one of size, raiddisks, bitmap, and layout\n");
dd0781e5
NB
1123 rv = 1;
1124 break;
b5e64645
NB
1125 } else if (layout != UnSet)
1126 rv = Manage_reconfig(devlist->devname, mdfd, layout);
1127 else if (size >= 0 || raiddisks)
e5329c37 1128 rv = Manage_resize(devlist->devname, mdfd, size, raiddisks);
f5e166fe
NB
1129 else if (bitmap_file) {
1130 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1131 rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file,
dfd4d8ee 1132 bitmap_chunk, delay, write_behind);
f5e166fe 1133 } else
b5e64645 1134 fprintf(stderr, Name ": no changes to --grow\n");
dd0781e5 1135 break;
64c4757e 1136 }
52826846 1137 exit(rv);
64c4757e 1138}