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