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