]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdadm.c
Use O_EXCL when opening component devices to be assembled into an array
[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; /* -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 */
77
78 char *mailaddr = NULL;
79 char *program = NULL;
80 int delay = 0;
81 int daemonise = 0;
82 char *pidfile = NULL;
83 int oneshot = 0;
84
85 int copies;
86
87 int mdfd = -1;
88
89 ident.uuid_set=0;
90 ident.level = UnSet;
91 ident.raid_disks = UnSet;
92 ident.super_minor= UnSet;
93 ident.devices=0;
94 ident.spare_group = NULL;
95 ident.autof = 0;
96
97 while ((option_index = -1) ,
98 (opt=getopt_long(argc, argv,
99 short_options, long_options,
100 &option_index)) != -1) {
101 int newmode = mode;
102 /* firstly, so mode-independant options */
103 switch(opt) {
104 case 'h':
105 help_text = Help;
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;
117 case GROW : help_text = Help_grow; break;
118 }
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
129 case 'b': brief = 1;
130 continue;
131
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 if (!mode) newmode = MANAGE;
149 break;
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;
155 case 'G': newmode = GROW; break;
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);
176 fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
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);
189 }
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 }
212 if (option_index >= 0)
213 fprintf(stderr, Name ": --%s", long_options[option_index].name);
214 else
215 fprintf(stderr, Name ": -%c", opt);
216 fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
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':
228 case 'G':
229 continue;
230 }
231 if (opt == 1) {
232 /* an undecorated option - must be a device name.
233 */
234 if (devs_found > 0 && mode == '@' && !devmode) {
235 fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
236 exit(2);
237 }
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 }
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
253 devs_found++;
254 continue;
255 }
256
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)) {
261 case O(CREATE,'c'):
262 case O(BUILD,'c'): /* chunk or rounding */
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;
275
276 case O(GROW,'z'):
277 case O(CREATE,'z'): /* size */
278 if (size >= 0) {
279 fprintf(stderr, Name ": size may only be specified once. "
280 "Second value is %s.\n", optarg);
281 exit(2);
282 }
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 }
292 }
293 continue;
294
295 case O(GROW,'l'): /* hack - needed to understand layout */
296 case O(CREATE,'l'):
297 case O(BUILD,'l'): /* set raid level*/
298 if (level != UnSet) {
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);
304 if (level == UnSet) {
305 fprintf(stderr, Name ": invalid raid level: %s\n",
306 optarg);
307 exit(2);
308 }
309 if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) {
310 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
311 optarg);
312 exit(2);
313 }
314 if (sparedisks > 0 && level < 1 && level >= -1) {
315 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
316 optarg);
317 exit(2);
318 }
319 ident.level = level;
320 continue;
321
322 case O(CREATE,'p'): /* raid5 layout */
323 case O(BUILD,'p'): /* faulty layout */
324 case O(GROW, 'p'): /* faulty reconfig */
325 if (layout != UnSet) {
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:
332 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
333 map_num(pers, level));
334 exit(2);
335 case UnSet:
336 fprintf(stderr, Name ": raid level must be given before layout.\n");
337 exit(2);
338
339 case 5:
340 case 6:
341 layout = map_name(r5layout, optarg);
342 if (layout==UnSet) {
343 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
344 optarg);
345 exit(2);
346 }
347 break;
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;
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 }
380 }
381 continue;
382
383 case O(CREATE,3):
384 case O(BUILD,3): /* assume clean */
385 assume_clean = 1;
386 continue;
387
388 case O(GROW,'n'):
389 case O(CREATE,'n'):
390 case O(BUILD,'n'): /* number of raid disks */
391 if (raiddisks) {
392 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
393 raiddisks, optarg);
394 exit(2);
395 }
396 raiddisks = strtol(optarg, &c, 10);
397 if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) {
398 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
399 optarg);
400 exit(2);
401 }
402 if (raiddisks == 1 && !force && level != -5) {
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 }
408 ident.raid_disks = raiddisks;
409 continue;
410
411 case O(CREATE,'x'): /* number of spare (eXtra) discs */
412 if (sparedisks) {
413 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
414 sparedisks, optarg);
415 exit(2);
416 }
417 if (level != UnSet && level <= 0 && level >= -1) {
418 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
419 level);
420 exit(2);
421 }
422 sparedisks = strtol(optarg, &c, 10);
423 if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) {
424 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
425 optarg);
426 exit(2);
427 }
428 continue;
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)
434 autof = -2;
435 else if (strcasecmp(optarg,"no")==0)
436 autof = 0;
437 else if (strcasecmp(optarg,"yes")==0)
438 autof = -2;
439 else if (strcasecmp(optarg,"md")==0)
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
468 case O(BUILD,'f'): /* force honouring '-n 1' */
469 case O(CREATE,'f'): /* force honouring of device list */
470 case O(ASSEMBLE,'f'): /* force assembly */
471 case O(MISC,'f'): /* force zero */
472 force=1;
473 continue;
474
475 /* now for the Assemble options */
476 case O(ASSEMBLE,'u'): /* uuid of array */
477 if (ident.uuid_set) {
478 fprintf(stderr, Name ": uuid cannot be set twice. "
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
490 case O(ASSEMBLE,'m'): /* super-minor for array */
491 if (ident.super_minor != UnSet) {
492 fprintf(stderr, Name ": super-minor cannot be set twice. "
493 "Second value: %s.\n", optarg);
494 exit(2);
495 }
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 }
504 }
505 continue;
506
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;
514 if (strcmp(update, "sparc2.2")==0)
515 continue;
516 if (strcmp(update, "super-minor") == 0)
517 continue;
518 if (strcmp(update, "summaries")==0)
519 continue;
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);
523 exit(2);
524
525 case O(ASSEMBLE,'c'): /* config file */
526 case O(MISC, 'c'):
527 case O(MONITOR,'c'):
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;
536 case O(ASSEMBLE,'s'): /* scan */
537 case O(MISC,'s'):
538 case O(MONITOR,'s'):
539 scan = 1;
540 continue;
541
542 case O(MONITOR,'m'): /* mail address */
543 if (mailaddr)
544 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
545 optarg);
546 else
547 mailaddr = optarg;
548 continue;
549
550 case O(MONITOR,'p'): /* alert program */
551 if (program)
552 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
553 optarg);
554 else
555 program = optarg;
556 continue;
557
558 case O(MONITOR,'d'): /* delay in seconds */
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;
571 case O(MONITOR,'f'): /* daemonise */
572 daemonise = 1;
573 continue;
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;
581 case O(MONITOR,'1'): /* oneshot */
582 oneshot = 1;
583 continue;
584 case O(MONITOR,'t'): /* test */
585 test = 1;
586 continue;
587
588 /* now the general management options. Some are applicable
589 * to other modes. None have arguments.
590 */
591 case O(GROW,'a'):
592 case O(MANAGE,'a'): /* add a drive */
593 devmode = 'a';
594 continue;
595 case O(MANAGE,'r'): /* remove a drive */
596 devmode = 'r';
597 continue;
598 case O(MANAGE,'f'): /* set faulty */
599 devmode = 'f';
600 continue;
601 case O(MANAGE,'R'):
602 case O(ASSEMBLE,'R'):
603 case O(BUILD,'R'):
604 case O(CREATE,'R'): /* Run the array */
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;
611 case O(MANAGE,'S'):
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
619 case O(MANAGE,'o'):
620 if (readonly < 0) {
621 fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
622 exit(2);
623 }
624 readonly = 1;
625 continue;
626 case O(MANAGE,'w'):
627 if (readonly > 0) {
628 fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n");
629 exit(2);
630 }
631 readonly = -1;
632 continue;
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;
650 case O(MISC,'t'):
651 test = 1;
652 continue;
653
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;
661 }
662 /* We have now processed all the valid options. Anything else is
663 * an error
664 */
665 fprintf(stderr, Name ": option %c not valid in %s mode\n",
666 opt, map_num(modes, mode));
667 exit(2);
668
669 }
670
671 if (!mode && devs_found) {
672 mode = MISC;
673 devmode = 'Q';
674 if (devlist->disposition == 0)
675 devlist->disposition = devmode;
676 }
677 if (!mode) {
678 fputs(Usage, stderr);
679 exit(2);
680 }
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 *
685 * That is mosty checked in the per-mode stuff but...
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.
689 */
690
691 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
692 (mode == ASSEMBLE && ! scan)) {
693 if (devs_found < 1) {
694 fprintf(stderr, Name ": an md device must be given in this mode\n");
695 exit(2);
696 }
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);
702 if (mdfd < 0)
703 exit(1);
704 if ((int)ident.super_minor == -2) {
705 struct stat stb;
706 fstat(mdfd, &stb);
707 ident.super_minor = minor(stb.st_rdev);
708 }
709 }
710
711 rv = 0;
712 switch(mode) {
713 case MANAGE:
714 /* readonly, add/remove, readwrite, runstop */
715 if (readonly>0)
716 rv = Manage_ro(devlist->devname, mdfd, readonly);
717 if (!rv && devs_found>1)
718 rv = Manage_subdevs(devlist->devname, mdfd,
719 devlist->next);
720 if (!rv && readonly < 0)
721 rv = Manage_ro(devlist->devname, mdfd, readonly);
722 if (!rv && runstop)
723 rv = Manage_runstop(devlist->devname, mdfd, runstop);
724 break;
725 case ASSEMBLE:
726 if (devs_found == 1 && ident.uuid_set == 0 &&
727 ident.super_minor == UnSet && !scan ) {
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);
730 if (array_ident == NULL) {
731 fprintf(stderr, Name ": %s not identified in config file.\n",
732 devlist->devname);
733 rv |= 1;
734 } else {
735 mdfd = open_mddev(devlist->devname, array_ident->autof);
736 if (mdfd < 0)
737 rv |= 1;
738 else {
739 rv |= Assemble(devlist->devname, mdfd, array_ident, configfile,
740 NULL,
741 readonly, runstop, update, verbose, force);
742 close(mdfd);
743 }
744 }
745 } else if (!scan)
746 rv = Assemble(devlist->devname, mdfd, &ident, configfile,
747 devlist->next,
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 }
754 for (dv = devlist ; dv ; dv=dv->next) {
755 mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname);
756 if (array_ident == NULL) {
757 fprintf(stderr, Name ": %s not identified in config file.\n",
758 dv->devname);
759 rv |= 1;
760 continue;
761 }
762 mdfd = open_mddev(dv->devname, array_ident->autof);
763 if (mdfd < 0) {
764 rv |= 1;
765 continue;
766 }
767 rv |= Assemble(dv->devname, mdfd, array_ident, configfile,
768 NULL,
769 readonly, runstop, update, verbose, force);
770 close(mdfd);
771 }
772 } else {
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) {
779 mdu_array_info_t array;
780 mdfd = open_mddev(array_list->devname, array_list->autof);
781 if (mdfd < 0) {
782 rv |= 1;
783 continue;
784 }
785 if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
786 /* already assembled, skip */
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);
794 }
795 }
796 break;
797 case BUILD:
798 rv = Build(devlist->devname, mdfd, chunk, level, layout, raiddisks, devlist->next, assume_clean);
799 break;
800 case CREATE:
801 rv = Create(devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size,
802 raiddisks, sparedisks,
803 devs_found-1, devlist->next, runstop, verbose, force);
804 break;
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) {
815 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
816 exit(1);
817 }
818 rv = Examine(devlist, scan?!verbose:brief, scan, SparcAdjust);
819 } else {
820 if (devlist == NULL) {
821 if ((devmode == 'S' ||devmode=='D') && scan) {
822 /* apply to all devices in /proc/mdstat */
823 struct mdstat_ent *ms = mdstat_read(0);
824 struct mdstat_ent *e;
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 }
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')
844 rv |= Detail(name, !verbose, test);
845 else if (devmode=='S') {
846 mdfd = open_mddev(name, 0);
847 if (mdfd >= 0) {
848 rv |= Manage_runstop(name, mdfd, -1);
849 close(mdfd);
850 }
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':
862 rv |= Detail(dv->devname, brief, test); continue;
863 case 'K': /* Zero superblock */
864 rv |= Kill(dv->devname, force); continue;
865 case 'Q':
866 rv |= Query(dv->devname); continue;
867 }
868 mdfd = open_mddev(dv->devname, 0);
869 if (mdfd>=0) {
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 }
880 close(mdfd);
881 }
882 }
883 }
884 break;
885 case MONITOR:
886 if (!devlist && !scan) {
887 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
888 rv = 1;
889 break;
890 }
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 }
896 rv= Monitor(devlist, mailaddr, program,
897 delay?delay:60, daemonise, scan, oneshot, configfile, test, pidfile);
898 break;
899
900 case GROW:
901 if (devs_found > 1) {
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 }
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");
916 rv = 1;
917 break;
918 } else if (layout != UnSet)
919 rv = Manage_reconfig(devlist->devname, mdfd, layout);
920 else if (size >= 0 || raiddisks)
921 rv = Manage_resize(devlist->devname, mdfd, size, raiddisks);
922 else
923 fprintf(stderr, Name ": no changes to --grow\n");
924 break;
925 }
926 exit(rv);
927 }