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