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