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