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