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