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