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