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