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