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