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