]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdadm.c
Create/grow: improve checks on number of devices.
[thirdparty/mdadm.git] / mdadm.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 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@suse.de>
23 *
24 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
25 * Paul Clements, SteelEye Technology, Inc.
26 */
27
28 #include "mdadm.h"
29 #include "md_p.h"
30 #include <ctype.h>
31
32
33 int main(int argc, char *argv[])
34 {
35 int mode = 0;
36 int opt;
37 int option_index;
38 char *c;
39 int rv;
40 int i;
41
42 int chunk = 0;
43 long long size = -1;
44 long long array_size = -1;
45 int level = UnSet;
46 int layout = UnSet;
47 char *layout_str = NULL;
48 int raiddisks = 0;
49 int sparedisks = 0;
50 struct mddev_ident_s ident;
51 char *configfile = NULL;
52 char *cp;
53 char *update = NULL;
54 int scan = 0;
55 char devmode = 0;
56 int runstop = 0;
57 int readonly = 0;
58 int write_behind = 0;
59 int bitmap_fd = -1;
60 char *bitmap_file = NULL;
61 char *backup_file = NULL;
62 int bitmap_chunk = UnSet;
63 int SparcAdjust = 0;
64 mddev_dev_t devlist = NULL;
65 mddev_dev_t *devlistend = & devlist;
66 mddev_dev_t dv;
67 int devs_found = 0;
68 int verbose = 0;
69 int quiet = 0;
70 int brief = 0;
71 int force = 0;
72 int test = 0;
73 int export = 0;
74 int assume_clean = 0;
75 char *symlinks = NULL;
76 /* autof indicates whether and how to create device node.
77 * bottom 3 bits are style. Rest (when shifted) are number of parts
78 * 0 - unset
79 * 1 - don't create (no)
80 * 2 - if is_standard, then create (yes)
81 * 3 - create as 'md' - reject is_standard mdp (md)
82 * 4 - create as 'mdp' - reject is_standard md (mdp)
83 * 5 - default to md if not is_standard (md in config file)
84 * 6 - default to mdp if not is_standard (part, or mdp in config file)
85 */
86 int autof = 0;
87
88 char *homehost = NULL;
89 char sys_hostname[256];
90 int require_homehost = 1;
91 char *mailaddr = NULL;
92 char *program = NULL;
93 int increments = 20;
94 int delay = 0;
95 int daemonise = 0;
96 char *pidfile = NULL;
97 int oneshot = 0;
98 struct supertype *ss = NULL;
99 int writemostly = 0;
100 int re_add = 0;
101 char *shortopt = short_options;
102 int dosyslog = 0;
103 int rebuild_map = 0;
104 int auto_update_home = 0;
105 char *subarray = NULL;
106
107 int print_help = 0;
108 FILE *outf;
109
110 int mdfd = -1;
111
112 srandom(time(0) ^ getpid());
113
114 ident.uuid_set=0;
115 ident.level = UnSet;
116 ident.raid_disks = UnSet;
117 ident.super_minor= UnSet;
118 ident.devices=0;
119 ident.spare_group = NULL;
120 ident.autof = 0;
121 ident.st = NULL;
122 ident.bitmap_fd = -1;
123 ident.bitmap_file = NULL;
124 ident.name[0] = 0;
125 ident.container = NULL;
126 ident.member = NULL;
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-independent 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 mode == INCREMENTAL || mode == MANAGE)
156 break; /* b means bitmap */
157 brief = 1;
158 continue;
159
160 case 'Y': export++;
161 continue;
162
163 case HomeHost:
164 if (strcasecmp(optarg, "<ignore>") == 0)
165 require_homehost = 0;
166 else
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 DetailPlatform:
219 case KillSubarray:
220 case UpdateSubarray:
221 if (opt == KillSubarray || opt == UpdateSubarray) {
222 if (subarray) {
223 fprintf(stderr, Name ": subarray can only be specified once\n");
224 exit(2);
225 }
226 subarray = optarg;
227 }
228 case 'K': if (!mode) newmode = MISC; break;
229 }
230 if (mode && newmode == mode) {
231 /* everybody happy ! */
232 } else if (mode && newmode != mode) {
233 /* not allowed.. */
234 fprintf(stderr, Name ": ");
235 if (option_index >= 0)
236 fprintf(stderr, "--%s", long_options[option_index].name);
237 else
238 fprintf(stderr, "-%c", opt);
239 fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
240 map_num(modes, newmode),
241 map_num(modes, mode));
242 exit(2);
243 } else if (!mode && newmode) {
244 mode = newmode;
245 } else {
246 /* special case of -c --help */
247 if (opt == 'c' &&
248 ( strncmp(optarg, "--h", 3)==0 ||
249 strncmp(optarg, "-h", 2)==0)) {
250 fputs(Help_config, stdout);
251 exit(0);
252 }
253
254 /* If first option is a device, don't force the mode yet */
255 if (opt == 1) {
256 if (devs_found == 0) {
257 dv = malloc(sizeof(*dv));
258 if (dv == NULL) {
259 fprintf(stderr, Name ": malloc failed\n");
260 exit(3);
261 }
262 dv->devname = optarg;
263 dv->disposition = devmode;
264 dv->writemostly = writemostly;
265 dv->re_add = re_add;
266 dv->used = 0;
267 dv->content = NULL;
268 dv->next = NULL;
269 *devlistend = dv;
270 devlistend = &dv->next;
271
272 devs_found++;
273 continue;
274 }
275 /* No mode yet, and this is the second device ... */
276 fprintf(stderr, Name ": An option must be given to set the mode before a second device\n"
277 " (%s) is listed\n", optarg);
278 exit(2);
279 }
280 if (option_index >= 0)
281 fprintf(stderr, Name ": --%s", long_options[option_index].name);
282 else
283 fprintf(stderr, Name ": -%c", opt);
284 fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
285 exit(2);
286 }
287
288 /* if we just set the mode, then done */
289 switch(opt) {
290 case '@':
291 case '#':
292 case 'A':
293 case 'B':
294 case 'C':
295 case 'F':
296 case 'G':
297 case 'I':
298 case AutoDetect:
299 continue;
300 }
301 if (opt == 1) {
302 /* an undecorated option - must be a device name.
303 */
304 if (devs_found > 0 && mode == '@' && !devmode) {
305 fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
306 exit(2);
307 }
308 if (devs_found > 0 && mode == 'G' && !devmode) {
309 fprintf(stderr, Name ": Must give one of -a for devices do add: %s\n", optarg);
310 exit(2);
311 }
312 dv = malloc(sizeof(*dv));
313 if (dv == NULL) {
314 fprintf(stderr, Name ": malloc failed\n");
315 exit(3);
316 }
317 dv->devname = optarg;
318 dv->disposition = devmode;
319 dv->writemostly = writemostly;
320 dv->re_add = re_add;
321 dv->used = 0;
322 dv->content = NULL;
323 dv->next = NULL;
324 *devlistend = dv;
325 devlistend = &dv->next;
326
327 devs_found++;
328 continue;
329 }
330
331 /* We've got a mode, and opt is now something else which
332 * could depend on the mode */
333 #define O(a,b) ((a<<8)|b)
334 switch (O(mode,opt)) {
335 case O(GROW,'c'):
336 case O(CREATE,'c'):
337 case O(BUILD,'c'): /* chunk or rounding */
338 if (chunk) {
339 fprintf(stderr, Name ": chunk/rounding may only be specified once. "
340 "Second value is %s.\n", optarg);
341 exit(2);
342 }
343 chunk = strtol(optarg, &c, 10);
344 if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) {
345 fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
346 optarg);
347 exit(2);
348 }
349 continue;
350
351 #if 0
352 case O(ASSEMBLE,AutoHomeHost):
353 auto_update_home = 1;
354 continue;
355 #endif
356 case O(INCREMENTAL, 'e'):
357 case O(CREATE,'e'):
358 case O(ASSEMBLE,'e'):
359 case O(MISC,'e'): /* set metadata (superblock) information */
360 if (ss) {
361 fprintf(stderr, Name ": metadata information already given\n");
362 exit(2);
363 }
364 for(i=0; !ss && superlist[i]; i++)
365 ss = superlist[i]->match_metadata_desc(optarg);
366
367 if (!ss) {
368 fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg);
369 exit(2);
370 }
371 continue;
372
373 case O(MANAGE,'W'):
374 case O(BUILD,'W'):
375 case O(CREATE,'W'):
376 /* set write-mostly for following devices */
377 writemostly = 1;
378 continue;
379
380 case O(MANAGE,'w'):
381 /* clear write-mostly for following devices */
382 writemostly = 2;
383 continue;
384
385
386 case O(GROW,'z'):
387 case O(CREATE,'z'):
388 case O(BUILD,'z'): /* size */
389 if (size >= 0) {
390 fprintf(stderr, Name ": size may only be specified once. "
391 "Second value is %s.\n", optarg);
392 exit(2);
393 }
394 if (strcmp(optarg, "max")==0)
395 size = 0;
396 else {
397 size = parse_size(optarg);
398 if (size < 8) {
399 fprintf(stderr, Name ": invalid size: %s\n",
400 optarg);
401 exit(2);
402 }
403 /* convert sectors to K */
404 size /= 2;
405 }
406 continue;
407
408 case O(GROW,'Z'): /* array size */
409 if (array_size >= 0) {
410 fprintf(stderr, Name ": array-size may only be specified once. "
411 "Second value is %s.\n", optarg);
412 exit(2);
413 }
414 if (strcmp(optarg, "max") == 0)
415 array_size = 0;
416 else {
417 array_size = parse_size(optarg);
418 if (array_size <= 0) {
419 fprintf(stderr, Name ": invalid array size: %s\n",
420 optarg);
421 exit(2);
422 }
423 }
424 continue;
425
426 case O(GROW,'l'):
427 case O(CREATE,'l'):
428 case O(BUILD,'l'): /* set raid level*/
429 if (level != UnSet) {
430 fprintf(stderr, Name ": raid level may only be set once. "
431 "Second value is %s.\n", optarg);
432 exit(2);
433 }
434 level = map_name(pers, optarg);
435 if (level == UnSet) {
436 fprintf(stderr, Name ": invalid raid level: %s\n",
437 optarg);
438 exit(2);
439 }
440 if (level != 0 && level != LEVEL_LINEAR && level != 1 &&
441 level != LEVEL_MULTIPATH && level != LEVEL_FAULTY &&
442 level != 10 &&
443 mode == BUILD) {
444 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
445 optarg);
446 exit(2);
447 }
448 if (sparedisks > 0 && level < 1 && level >= -1) {
449 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
450 optarg);
451 exit(2);
452 }
453 ident.level = level;
454 continue;
455
456 case O(GROW, 'p'): /* new layout */
457 if (layout_str) {
458 fprintf(stderr,Name ": layout may only be sent once. "
459 "Second value was %s\n", optarg);
460 exit(2);
461 }
462 layout_str = optarg;
463 /* 'Grow' will parse the value */
464 continue;
465
466 case O(CREATE,'p'): /* raid5 layout */
467 case O(BUILD,'p'): /* faulty layout */
468 if (layout != UnSet) {
469 fprintf(stderr,Name ": layout may only be sent once. "
470 "Second value was %s\n", optarg);
471 exit(2);
472 }
473 switch(level) {
474 default:
475 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
476 map_num(pers, level));
477 exit(2);
478 case UnSet:
479 fprintf(stderr, Name ": raid level must be given before layout.\n");
480 exit(2);
481
482 case 5:
483 layout = map_name(r5layout, optarg);
484 if (layout==UnSet) {
485 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
486 optarg);
487 exit(2);
488 }
489 break;
490 case 6:
491 layout = map_name(r6layout, optarg);
492 if (layout==UnSet) {
493 fprintf(stderr, Name ": layout %s not understood for raid6.\n",
494 optarg);
495 exit(2);
496 }
497 break;
498
499 case 10:
500 layout = parse_layout_10(optarg);
501 if (layout < 0) {
502 fprintf(stderr, Name ": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg);
503 exit(2);
504 }
505 break;
506 case LEVEL_FAULTY:
507 /* Faulty
508 * modeNNN
509 */
510 layout = parse_layout_faulty(optarg);
511 if (layout == -1) {
512 fprintf(stderr, Name ": layout %s not understood for faulty.\n",
513 optarg);
514 exit(2);
515 }
516 break;
517 }
518 continue;
519
520 case O(CREATE,AssumeClean):
521 case O(BUILD,AssumeClean): /* assume clean */
522 assume_clean = 1;
523 continue;
524
525 case O(GROW,'n'):
526 case O(CREATE,'n'):
527 case O(BUILD,'n'): /* number of raid disks */
528 if (raiddisks) {
529 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
530 raiddisks, optarg);
531 exit(2);
532 }
533 raiddisks = strtol(optarg, &c, 10);
534 if (!optarg[0] || *c || raiddisks<=0) {
535 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
536 optarg);
537 exit(2);
538 }
539 ident.raid_disks = raiddisks;
540 continue;
541
542 case O(CREATE,'x'): /* number of spare (eXtra) discs */
543 if (sparedisks) {
544 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
545 sparedisks, optarg);
546 exit(2);
547 }
548 if (level != UnSet && level <= 0 && level >= -1) {
549 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
550 level);
551 exit(2);
552 }
553 sparedisks = strtol(optarg, &c, 10);
554 if (!optarg[0] || *c || sparedisks < 0) {
555 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
556 optarg);
557 exit(2);
558 }
559 continue;
560
561 case O(CREATE,'a'):
562 case O(BUILD,'a'):
563 case O(INCREMENTAL,'a'):
564 case O(ASSEMBLE,'a'): /* auto-creation of device node */
565 autof = parse_auto(optarg, "--auto flag", 0);
566 continue;
567
568 case O(CREATE,Symlinks):
569 case O(BUILD,Symlinks):
570 case O(ASSEMBLE,Symlinks): /* auto creation of symlinks in /dev to /dev/md */
571 symlinks = optarg;
572 continue;
573
574 case O(BUILD,'f'): /* force honouring '-n 1' */
575 case O(GROW,'f'): /* ditto */
576 case O(CREATE,'f'): /* force honouring of device list */
577 case O(ASSEMBLE,'f'): /* force assembly */
578 case O(MISC,'f'): /* force zero */
579 force=1;
580 continue;
581
582 /* now for the Assemble options */
583 case O(CREATE,'u'): /* uuid of array */
584 case O(ASSEMBLE,'u'): /* uuid of array */
585 if (ident.uuid_set) {
586 fprintf(stderr, Name ": uuid cannot be set twice. "
587 "Second value %s.\n", optarg);
588 exit(2);
589 }
590 if (parse_uuid(optarg, ident.uuid))
591 ident.uuid_set = 1;
592 else {
593 fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
594 exit(2);
595 }
596 continue;
597
598 case O(CREATE,'N'):
599 case O(ASSEMBLE,'N'):
600 case O(MISC,'N'):
601 if (ident.name[0]) {
602 fprintf(stderr, Name ": name cannot be set twice. "
603 "Second value %s.\n", optarg);
604 exit(2);
605 }
606 if (mode == MISC && !subarray) {
607 fprintf(stderr, Name ": -N/--name only valid with --update-subarray in misc mode\n");
608 exit(2);
609 }
610 if (strlen(optarg) > 32) {
611 fprintf(stderr, Name ": name '%s' is too long, 32 chars max.\n",
612 optarg);
613 exit(2);
614 }
615 strcpy(ident.name, optarg);
616 continue;
617
618 case O(ASSEMBLE,'m'): /* super-minor for array */
619 if (ident.super_minor != UnSet) {
620 fprintf(stderr, Name ": super-minor cannot be set twice. "
621 "Second value: %s.\n", optarg);
622 exit(2);
623 }
624 if (strcmp(optarg, "dev")==0)
625 ident.super_minor = -2;
626 else {
627 ident.super_minor = strtoul(optarg, &cp, 10);
628 if (!optarg[0] || *cp) {
629 fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
630 exit(2);
631 }
632 }
633 continue;
634
635 case O(ASSEMBLE,'U'): /* update the superblock */
636 case O(MISC,'U'):
637 if (update) {
638 fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n",
639 update, optarg);
640 exit(2);
641 }
642 if (mode == MISC && !subarray) {
643 fprintf(stderr, Name ": Only subarrays can be updated in misc mode\n");
644 exit(2);
645 }
646 update = optarg;
647 if (strcmp(update, "sparc2.2")==0)
648 continue;
649 if (strcmp(update, "super-minor") == 0)
650 continue;
651 if (strcmp(update, "summaries")==0)
652 continue;
653 if (strcmp(update, "resync")==0)
654 continue;
655 if (strcmp(update, "uuid")==0)
656 continue;
657 if (strcmp(update, "name")==0)
658 continue;
659 if (strcmp(update, "homehost")==0)
660 continue;
661 if (strcmp(update, "devicesize")==0)
662 continue;
663 if (strcmp(update, "byteorder")==0) {
664 if (ss) {
665 fprintf(stderr, Name ": must not set metadata type with --update=byteorder.\n");
666 exit(2);
667 }
668 for(i=0; !ss && superlist[i]; i++)
669 ss = superlist[i]->match_metadata_desc("0.swap");
670 if (!ss) {
671 fprintf(stderr, Name ": INTERNAL ERROR cannot find 0.swap\n");
672 exit(2);
673 }
674
675 continue;
676 }
677 if (strcmp(update,"?") == 0 ||
678 strcmp(update, "help") == 0) {
679 outf = stdout;
680 fprintf(outf, Name ": ");
681 } else {
682 outf = stderr;
683 fprintf(outf,
684 Name ": '--update=%s' is invalid. ",
685 update);
686 }
687 fprintf(outf, "Valid --update options are:\n"
688 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
689 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
690 exit(outf == stdout ? 0 : 2);
691
692 case O(INCREMENTAL,NoDegraded):
693 fprintf(stderr, Name ": --no-degraded is deprecated in Incremental mode\n");
694 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
695 runstop = -1; /* --stop isn't allowed for --assemble,
696 * so we overload slightly */
697 continue;
698
699 case O(ASSEMBLE,'c'): /* config file */
700 case O(INCREMENTAL, 'c'):
701 case O(MISC, 'c'):
702 case O(MONITOR,'c'):
703 if (configfile) {
704 fprintf(stderr, Name ": configfile cannot be set twice. "
705 "Second value is %s.\n", optarg);
706 exit(2);
707 }
708 configfile = optarg;
709 set_conffile(configfile);
710 /* FIXME possibly check that config file exists. Even parse it */
711 continue;
712 case O(ASSEMBLE,'s'): /* scan */
713 case O(MISC,'s'):
714 case O(MONITOR,'s'):
715 case O(INCREMENTAL,'s'):
716 scan = 1;
717 continue;
718
719 case O(MONITOR,'m'): /* mail address */
720 if (mailaddr)
721 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
722 optarg);
723 else
724 mailaddr = optarg;
725 continue;
726
727 case O(MONITOR,'p'): /* alert program */
728 if (program)
729 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
730 optarg);
731 else
732 program = optarg;
733 continue;
734
735 case O(MONITOR,'r'): /* rebuild increments */
736 increments = atoi(optarg);
737 if (increments>99 || increments<1) {
738 fprintf(stderr, Name ": please specify positive integer between 1 and 99 as rebuild increments.\n");
739 exit(2);
740 }
741 continue;
742
743 case O(MONITOR,'d'): /* delay in seconds */
744 case O(GROW, 'd'):
745 case O(BUILD,'d'): /* delay for bitmap updates */
746 case O(CREATE,'d'):
747 if (delay)
748 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
749 optarg);
750 else {
751 delay = strtol(optarg, &c, 10);
752 if (!optarg[0] || *c || delay<1) {
753 fprintf(stderr, Name ": invalid delay: %s\n",
754 optarg);
755 exit(2);
756 }
757 }
758 continue;
759 case O(MONITOR,'f'): /* daemonise */
760 daemonise = 1;
761 continue;
762 case O(MONITOR,'i'): /* pid */
763 if (pidfile)
764 fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
765 optarg);
766 else
767 pidfile = optarg;
768 continue;
769 case O(MONITOR,'1'): /* oneshot */
770 oneshot = 1;
771 continue;
772 case O(MONITOR,'t'): /* test */
773 test = 1;
774 continue;
775 case O(MONITOR,'y'): /* log messages to syslog */
776 openlog("mdadm", LOG_PID, SYSLOG_FACILITY);
777 dosyslog = 1;
778 continue;
779
780 /* now the general management options. Some are applicable
781 * to other modes. None have arguments.
782 */
783 case O(GROW,'a'):
784 case O(MANAGE,'a'): /* add a drive */
785 devmode = 'a';
786 re_add = 0;
787 continue;
788 case O(MANAGE,ReAdd):
789 devmode = 'a';
790 re_add = 1;
791 continue;
792 case O(MANAGE,'r'): /* remove a drive */
793 devmode = 'r';
794 continue;
795 case O(MANAGE,'f'): /* set faulty */
796 case O(INCREMENTAL,'f'): /* r for incremental is taken, use f
797 * even though we will both fail and
798 * remove the device */
799 devmode = 'f';
800 continue;
801 case O(INCREMENTAL,'R'):
802 case O(MANAGE,'R'):
803 case O(ASSEMBLE,'R'):
804 case O(BUILD,'R'):
805 case O(CREATE,'R'): /* Run the array */
806 if (runstop < 0) {
807 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
808 exit(2);
809 }
810 runstop = 1;
811 continue;
812 case O(MANAGE,'S'):
813 if (runstop > 0) {
814 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
815 exit(2);
816 }
817 runstop = -1;
818 continue;
819 case O(MANAGE,'t'):
820 test = 1;
821 continue;
822
823 case O(MISC,'Q'):
824 case O(MISC,'D'):
825 case O(MISC,'E'):
826 case O(MISC,'K'):
827 case O(MISC,'R'):
828 case O(MISC,'S'):
829 case O(MISC,'X'):
830 case O(MISC,'o'):
831 case O(MISC,'w'):
832 case O(MISC,'W'):
833 case O(MISC, Waitclean):
834 case O(MISC, DetailPlatform):
835 case O(MISC, KillSubarray):
836 case O(MISC, UpdateSubarray):
837 if (devmode && devmode != opt &&
838 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
839 fprintf(stderr, Name ": --examine/-E cannot be given with ");
840 if (devmode == 'E') {
841 if (option_index >= 0)
842 fprintf(stderr, "--%s\n",
843 long_options[option_index].name);
844 else
845 fprintf(stderr, "-%c\n", opt);
846 } else if (isalpha(devmode))
847 fprintf(stderr, "-%c\n", devmode);
848 else
849 fprintf(stderr, "previous option\n");
850 exit(2);
851 }
852 devmode = opt;
853 continue;
854 case O(MISC,'t'):
855 test = 1;
856 continue;
857
858 case O(MISC, Sparc22):
859 if (devmode != 'E') {
860 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
861 exit(2);
862 }
863 SparcAdjust = 1;
864 continue;
865
866 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
867 if (!optarg) {
868 fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n");
869 exit(2);
870 }
871 if (strcmp(optarg, "internal")==0) {
872 fprintf(stderr, Name ": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
873 continue;
874 }
875 bitmap_fd = open(optarg, O_RDWR);
876 if (!*optarg || bitmap_fd < 0) {
877 fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno));
878 exit(2);
879 }
880 ident.bitmap_fd = bitmap_fd; /* for Assemble */
881 continue;
882
883 case O(ASSEMBLE, BackupFile):
884 case O(GROW, BackupFile):
885 /* Specify a file into which grow might place a backup,
886 * or from which assemble might recover a backup
887 */
888 if (backup_file) {
889 fprintf(stderr, Name ": backup file already specified, rejecting %s\n", optarg);
890 exit(2);
891 }
892 backup_file = optarg;
893 continue;
894
895 case O(BUILD,'b'):
896 case O(CREATE,'b'): /* here we create the bitmap */
897 if (strcmp(optarg, "none") == 0) {
898 fprintf(stderr, Name ": '--bitmap none' only"
899 " support for --grow\n");
900 exit(2);
901 }
902 /* FALL THROUGH */
903 case O(GROW,'b'):
904 if (strcmp(optarg, "internal")== 0 ||
905 strcmp(optarg, "none")== 0 ||
906 strchr(optarg, '/') != NULL) {
907 bitmap_file = optarg;
908 continue;
909 }
910 /* probable typo */
911 fprintf(stderr, Name ": bitmap file must contain a '/', or be 'internal', or 'none'\n"
912 " not '%s'\n", optarg);
913 exit(2);
914
915 case O(GROW,BitmapChunk):
916 case O(BUILD,BitmapChunk):
917 case O(CREATE,BitmapChunk): /* bitmap chunksize */
918 bitmap_chunk = strtol(optarg, &c, 10);
919 if (!optarg[0] || *c || bitmap_chunk < 0 ||
920 bitmap_chunk & (bitmap_chunk - 1)) {
921 fprintf(stderr, Name ": invalid bitmap chunksize: %s\n",
922 optarg);
923 exit(2);
924 }
925 /* convert K to B, chunk of 0K means 512B */
926 bitmap_chunk = bitmap_chunk ? bitmap_chunk * 1024 : 512;
927 continue;
928
929 case O(GROW, WriteBehind):
930 case O(BUILD, WriteBehind):
931 case O(CREATE, WriteBehind): /* write-behind mode */
932 write_behind = DEFAULT_MAX_WRITE_BEHIND;
933 if (optarg) {
934 write_behind = strtol(optarg, &c, 10);
935 if (write_behind < 0 || *c ||
936 write_behind > 16383) {
937 fprintf(stderr, Name ": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
938 exit(2);
939 }
940 }
941 continue;
942
943 case O(INCREMENTAL, 'r'):
944 rebuild_map = 1;
945 continue;
946 }
947 /* We have now processed all the valid options. Anything else is
948 * an error
949 */
950 if (option_index > 0)
951 fprintf(stderr, Name ":option --%s not valid in %s mode\n",
952 long_options[option_index].name,
953 map_num(modes, mode));
954 else
955 fprintf(stderr, Name ": option -%c not valid in %s mode\n",
956 opt, map_num(modes, mode));
957 exit(2);
958
959 }
960
961 if (print_help) {
962 char *help_text = Help;
963 if (print_help == 2)
964 help_text = OptionHelp;
965 else
966 switch (mode) {
967 case ASSEMBLE : help_text = Help_assemble; break;
968 case BUILD : help_text = Help_build; break;
969 case CREATE : help_text = Help_create; break;
970 case MANAGE : help_text = Help_manage; break;
971 case MISC : help_text = Help_misc; break;
972 case MONITOR : help_text = Help_monitor; break;
973 case GROW : help_text = Help_grow; break;
974 case INCREMENTAL:help_text= Help_incr; break;
975 }
976 fputs(help_text,stdout);
977 exit(0);
978 }
979
980 if (!mode && devs_found) {
981 mode = MISC;
982 devmode = 'Q';
983 if (devlist->disposition == 0)
984 devlist->disposition = devmode;
985 }
986 if (!mode) {
987 fputs(Usage, stderr);
988 exit(2);
989 }
990
991 if (symlinks) {
992 struct createinfo *ci = conf_get_create_info();
993
994 if (strcasecmp(symlinks, "yes") == 0)
995 ci->symlinks = 1;
996 else if (strcasecmp(symlinks, "no") == 0)
997 ci->symlinks = 0;
998 else {
999 fprintf(stderr, Name ": option --symlinks must be 'no' or 'yes'\n");
1000 exit(2);
1001 }
1002 }
1003 /* Ok, got the option parsing out of the way
1004 * hopefully it's mostly right but there might be some stuff
1005 * missing
1006 *
1007 * That is mosty checked in the per-mode stuff but...
1008 *
1009 * For @,B,C and A without -s, the first device listed must be an md device
1010 * we check that here and open it.
1011 */
1012
1013 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
1014 (mode == ASSEMBLE && ! scan)) {
1015 if (devs_found < 1) {
1016 fprintf(stderr, Name ": an md device must be given in this mode\n");
1017 exit(2);
1018 }
1019 if ((int)ident.super_minor == -2 && autof) {
1020 fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
1021 exit(2);
1022 }
1023 if (mode == MANAGE || mode == GROW) {
1024 mdfd = open_mddev(devlist->devname, 1);
1025 if (mdfd < 0)
1026 exit(1);
1027 } else
1028 /* non-existent device is OK */
1029 mdfd = open_mddev(devlist->devname, 0);
1030 if (mdfd == -2) {
1031 fprintf(stderr, Name ": device %s exists but is not an "
1032 "md array.\n", devlist->devname);
1033 exit(1);
1034 }
1035 if ((int)ident.super_minor == -2) {
1036 struct stat stb;
1037 if (mdfd < 0) {
1038 fprintf(stderr, Name ": --super-minor=dev given, and "
1039 "listed device %s doesn't exist.\n",
1040 devlist->devname);
1041 exit(1);
1042 }
1043 fstat(mdfd, &stb);
1044 ident.super_minor = minor(stb.st_rdev);
1045 }
1046 if (mdfd >= 0 && mode != MANAGE && mode != GROW) {
1047 /* We don't really want this open yet, we just might
1048 * have wanted to check some things
1049 */
1050 close(mdfd);
1051 mdfd = -1;
1052 }
1053 }
1054
1055 if (raiddisks) {
1056 if (raiddisks == 1 && !force && level != -5) {
1057 fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
1058 " a mistake. If you really mean it you will need to specify --force before\n"
1059 " setting the number of drives.\n");
1060 exit(2);
1061 }
1062 }
1063
1064 if (homehost == NULL)
1065 homehost = conf_get_homehost(&require_homehost);
1066 if (homehost == NULL || strcmp(homehost, "<system>")==0) {
1067 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
1068 sys_hostname[sizeof(sys_hostname)-1] = 0;
1069 homehost = sys_hostname;
1070 }
1071 }
1072
1073 if ((mode != MISC || devmode != 'E') &&
1074 geteuid() != 0) {
1075 fprintf(stderr, Name ": must be super-user to perform this action\n");
1076 exit(1);
1077 }
1078
1079 ident.autof = autof;
1080
1081 rv = 0;
1082 switch(mode) {
1083 case MANAGE:
1084 /* readonly, add/remove, readwrite, runstop */
1085 if (readonly>0)
1086 rv = Manage_ro(devlist->devname, mdfd, readonly);
1087 if (!rv && devs_found>1)
1088 rv = Manage_subdevs(devlist->devname, mdfd,
1089 devlist->next, verbose-quiet, test);
1090 if (!rv && readonly < 0)
1091 rv = Manage_ro(devlist->devname, mdfd, readonly);
1092 if (!rv && runstop)
1093 rv = Manage_runstop(devlist->devname, mdfd, runstop, quiet);
1094 break;
1095 case ASSEMBLE:
1096 if (devs_found == 1 && ident.uuid_set == 0 &&
1097 ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) {
1098 /* Only a device has been given, so get details from config file */
1099 mddev_ident_t array_ident = conf_get_ident(devlist->devname);
1100 if (array_ident == NULL) {
1101 fprintf(stderr, Name ": %s not identified in config file.\n",
1102 devlist->devname);
1103 rv |= 1;
1104 if (mdfd >= 0)
1105 close(mdfd);
1106 } else {
1107 if (array_ident->autof == 0)
1108 array_ident->autof = autof;
1109 rv |= Assemble(ss, devlist->devname, array_ident,
1110 NULL, backup_file,
1111 readonly, runstop, update,
1112 homehost, require_homehost,
1113 verbose-quiet, force);
1114 }
1115 } else if (!scan)
1116 rv = Assemble(ss, devlist->devname, &ident,
1117 devlist->next, backup_file,
1118 readonly, runstop, update,
1119 homehost, require_homehost,
1120 verbose-quiet, force);
1121 else if (devs_found>0) {
1122 if (update && devs_found > 1) {
1123 fprintf(stderr, Name ": can only update a single array at a time\n");
1124 exit(1);
1125 }
1126 if (backup_file && devs_found > 1) {
1127 fprintf(stderr, Name ": can only assemble a single array when providing a backup file.\n");
1128 exit(1);
1129 }
1130 for (dv = devlist ; dv ; dv=dv->next) {
1131 mddev_ident_t array_ident = conf_get_ident(dv->devname);
1132 if (array_ident == NULL) {
1133 fprintf(stderr, Name ": %s not identified in config file.\n",
1134 dv->devname);
1135 rv |= 1;
1136 continue;
1137 }
1138 if (array_ident->autof == 0)
1139 array_ident->autof = autof;
1140 rv |= Assemble(ss, dv->devname, array_ident,
1141 NULL, backup_file,
1142 readonly, runstop, update,
1143 homehost, require_homehost,
1144 verbose-quiet, force);
1145 }
1146 } else {
1147 mddev_ident_t a, array_list = conf_get_ident(NULL);
1148 mddev_dev_t devlist = conf_get_devs();
1149 int cnt = 0;
1150 int failures, successes;
1151 if (devlist == NULL) {
1152 fprintf(stderr, Name ": No devices listed in conf file were found.\n");
1153 exit(1);
1154 }
1155 if (update) {
1156 fprintf(stderr, Name ": --update not meaningful with a --scan assembly.\n");
1157 exit(1);
1158 }
1159 if (backup_file) {
1160 fprintf(stderr, Name ": --backup_file not meaningful with a --scan assembly.\n");
1161 exit(1);
1162 }
1163 for (a = array_list; a ; a = a->next) {
1164 a->assembled = 0;
1165 if (a->autof == 0)
1166 a->autof = autof;
1167 }
1168 do {
1169 failures = 0;
1170 successes = 0;
1171 rv = 0;
1172 for (a = array_list; a ; a = a->next) {
1173 int r;
1174 if (a->assembled)
1175 continue;
1176 if (a->devname &&
1177 strcasecmp(a->devname, "<ignore>") == 0)
1178 continue;
1179
1180 r = Assemble(ss, a->devname,
1181 a,
1182 NULL, NULL,
1183 readonly, runstop, NULL,
1184 homehost, require_homehost,
1185 verbose-quiet, force);
1186 if (r == 0) {
1187 a->assembled = 1;
1188 successes++;
1189 } else
1190 failures++;
1191 rv |= r;
1192 cnt++;
1193 }
1194 } while (failures && successes);
1195 if (homehost && cnt == 0) {
1196 /* Maybe we can auto-assemble something.
1197 * Repeatedly call Assemble in auto-assemble mode
1198 * until it fails
1199 */
1200 int rv2;
1201 int acnt;
1202 ident.autof = autof;
1203 do {
1204 mddev_dev_t devlist = conf_get_devs();
1205 acnt = 0;
1206 do {
1207 rv2 = Assemble(ss, NULL,
1208 &ident,
1209 devlist, NULL,
1210 readonly, runstop, NULL,
1211 homehost, require_homehost,
1212 verbose-quiet, force);
1213 if (rv2==0) {
1214 cnt++;
1215 acnt++;
1216 }
1217 if (rv2 == 1)
1218 /* found something so even though assembly failed we
1219 * want to avoid auto-updates
1220 */
1221 auto_update_home = 0;
1222 } while (rv2!=2);
1223 /* Incase there are stacked devices, we need to go around again */
1224 } while (acnt);
1225 #if 0
1226 if (cnt == 0 && auto_update_home && homehost) {
1227 /* Nothing found, maybe we need to bootstrap homehost info */
1228 do {
1229 acnt = 0;
1230 do {
1231 rv2 = Assemble(ss, NULL,
1232 &ident,
1233 NULL, NULL,
1234 readonly, runstop, "homehost",
1235 homehost, require_homehost,
1236 verbose-quiet, force);
1237 if (rv2==0) {
1238 cnt++;
1239 acnt++;
1240 }
1241 } while (rv2!=2);
1242 /* Incase there are stacked devices, we need to go around again */
1243 } while (acnt);
1244 }
1245 #endif
1246 if (cnt == 0 && rv == 0) {
1247 fprintf(stderr, Name ": No arrays found in config file or automatically\n");
1248 rv = 1;
1249 } else if (cnt)
1250 rv = 0;
1251 } else if (cnt == 0 && rv == 0) {
1252 fprintf(stderr, Name ": No arrays found in config file\n");
1253 rv = 1;
1254 }
1255 }
1256 break;
1257 case BUILD:
1258 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1259 if (write_behind && !bitmap_file) {
1260 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1261 rv = 1;
1262 break;
1263 }
1264 if (raiddisks == 0) {
1265 fprintf(stderr, Name ": no raid-devices specified.\n");
1266 rv = 1;
1267 break;
1268 }
1269
1270 if (bitmap_file) {
1271 if (strcmp(bitmap_file, "internal")==0) {
1272 fprintf(stderr, Name ": 'internal' bitmaps not supported with --build\n");
1273 rv |= 1;
1274 break;
1275 }
1276 }
1277 rv = Build(devlist->devname, chunk, level, layout,
1278 raiddisks, devlist->next, assume_clean,
1279 bitmap_file, bitmap_chunk, write_behind,
1280 delay, verbose-quiet, autof, size);
1281 break;
1282 case CREATE:
1283 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1284 if (write_behind && !bitmap_file) {
1285 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1286 rv = 1;
1287 break;
1288 }
1289 if (raiddisks == 0) {
1290 fprintf(stderr, Name ": no raid-devices specified.\n");
1291 rv = 1;
1292 break;
1293 }
1294
1295 rv = Create(ss, devlist->devname, chunk, level, layout, size<0 ? 0 : size,
1296 raiddisks, sparedisks, ident.name, homehost,
1297 ident.uuid_set ? ident.uuid : NULL,
1298 devs_found-1, devlist->next, runstop, verbose-quiet, force, assume_clean,
1299 bitmap_file, bitmap_chunk, write_behind, delay, autof);
1300 break;
1301 case MISC:
1302 if (devmode == 'E') {
1303 if (devlist == NULL && !scan) {
1304 fprintf(stderr, Name ": No devices to examine\n");
1305 exit(2);
1306 }
1307 if (devlist == NULL)
1308 devlist = conf_get_devs();
1309 if (devlist == NULL) {
1310 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
1311 exit(1);
1312 }
1313 if (brief && verbose)
1314 brief = 2;
1315 rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief,
1316 export, scan,
1317 SparcAdjust, ss, homehost);
1318 } else if (devmode == DetailPlatform) {
1319 rv = Detail_Platform(ss ? ss->ss : NULL, ss ? scan : 1, verbose);
1320 } else {
1321 if (devlist == NULL) {
1322 if ((devmode=='D' || devmode == Waitclean) && scan) {
1323 /* apply --detail or --wait-clean to
1324 * all devices in /proc/mdstat
1325 */
1326 struct mdstat_ent *ms = mdstat_read(0, 1);
1327 struct mdstat_ent *e;
1328 struct map_ent *map = NULL;
1329 int members;
1330 int v = verbose>1?0:verbose+1;
1331
1332 for (members = 0; members <= 1; members++) {
1333 for (e=ms ; e ; e=e->next) {
1334 char *name;
1335 struct map_ent *me;
1336 int member = e->metadata_version &&
1337 strncmp(e->metadata_version,
1338 "external:/", 10) == 0;
1339 if (members != member)
1340 continue;
1341 me = map_by_devnum(&map, e->devnum);
1342 if (me && me->path
1343 && strcmp(me->path, "/unknown") != 0)
1344 name = me->path;
1345 else
1346 name = get_md_name(e->devnum);
1347
1348 if (!name) {
1349 fprintf(stderr, Name ": cannot find device file for %s\n",
1350 e->dev);
1351 continue;
1352 }
1353 if (devmode == 'D')
1354 rv |= Detail(name, v,
1355 export, test,
1356 homehost);
1357 else
1358 rv |= WaitClean(name, -1, v);
1359 put_md_name(name);
1360 }
1361 }
1362 free_mdstat(ms);
1363 } else if (devmode == 'S' && scan) {
1364 /* apply --stop to all devices in /proc/mdstat */
1365 /* Due to possible stacking of devices, repeat until
1366 * nothing more can be stopped
1367 */
1368 int progress=1, err;
1369 int last = 0;
1370 do {
1371 struct mdstat_ent *ms = mdstat_read(0, 0);
1372 struct mdstat_ent *e;
1373
1374 if (!progress) last = 1;
1375 progress = 0; err = 0;
1376 for (e=ms ; e ; e=e->next) {
1377 char *name = get_md_name(e->devnum);
1378
1379 if (!name) {
1380 fprintf(stderr, Name ": cannot find device file for %s\n",
1381 e->dev);
1382 continue;
1383 }
1384 mdfd = open_mddev(name, 1);
1385 if (mdfd >= 0) {
1386 if (Manage_runstop(name, mdfd, -1, quiet?1:last?0:-1))
1387 err = 1;
1388 else
1389 progress = 1;
1390 close(mdfd);
1391 }
1392
1393 put_md_name(name);
1394 }
1395 free_mdstat(ms);
1396 } while (!last && err);
1397 if (err) rv |= 1;
1398 } else {
1399 fprintf(stderr, Name ": No devices given.\n");
1400 exit(2);
1401 }
1402 }
1403 for (dv=devlist ; dv; dv=dv->next) {
1404 switch(dv->disposition) {
1405 case 'D':
1406 rv |= Detail(dv->devname,
1407 brief?1+verbose:0,
1408 export, test, homehost);
1409 continue;
1410 case 'K': /* Zero superblock */
1411 if (ss)
1412 rv |= Kill(dv->devname, ss, force, quiet,0);
1413 else {
1414 int q = quiet;
1415 do {
1416 rv |= Kill(dv->devname, NULL, force, q, 0);
1417 q = 1;
1418 } while (rv == 0);
1419 rv &= ~2;
1420 }
1421 continue;
1422 case 'Q':
1423 rv |= Query(dv->devname); continue;
1424 case 'X':
1425 rv |= ExamineBitmap(dv->devname, brief, ss); continue;
1426 case 'W':
1427 rv |= Wait(dv->devname); continue;
1428 case Waitclean:
1429 rv |= WaitClean(dv->devname, -1, verbose-quiet); continue;
1430 case KillSubarray:
1431 rv |= Kill_subarray(dv->devname, subarray, quiet);
1432 continue;
1433 case UpdateSubarray:
1434 if (update == NULL) {
1435 fprintf(stderr,
1436 Name ": -U/--update must be specified with --update-subarray\n");
1437 rv |= 1;
1438 continue;
1439 }
1440 rv |= Update_subarray(dv->devname, subarray, update, &ident, quiet);
1441 continue;
1442 }
1443 mdfd = open_mddev(dv->devname, 1);
1444 if (mdfd>=0) {
1445 switch(dv->disposition) {
1446 case 'R':
1447 rv |= Manage_runstop(dv->devname, mdfd, 1, quiet); break;
1448 case 'S':
1449 rv |= Manage_runstop(dv->devname, mdfd, -1, quiet); break;
1450 case 'o':
1451 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1452 case 'w':
1453 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1454 }
1455 close(mdfd);
1456 } else
1457 rv |= 1;
1458 }
1459 }
1460 break;
1461 case MONITOR:
1462 if (!devlist && !scan) {
1463 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
1464 rv = 1;
1465 break;
1466 }
1467 if (pidfile && !daemonise) {
1468 fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
1469 rv = 1;
1470 break;
1471 }
1472 if (delay == 0) {
1473 if (get_linux_version() > 20616)
1474 /* mdstat responds to poll */
1475 delay = 1000;
1476 else
1477 delay = 60;
1478 }
1479 rv= Monitor(devlist, mailaddr, program,
1480 delay?delay:60, daemonise, scan, oneshot,
1481 dosyslog, test, pidfile, increments);
1482 break;
1483
1484 case GROW:
1485 if (array_size >= 0) {
1486 /* alway impose array size first, independent of
1487 * anything else
1488 * Do not allow level or raid_disks changes at the
1489 * same time as that can be irreversibly destructive.
1490 */
1491 struct mdinfo sra;
1492 int err;
1493 if (raiddisks || level != UnSet) {
1494 fprintf(stderr, Name ": cannot change array size in same operation "
1495 "as changing raiddisks or level.\n"
1496 " Change size first, then check that data is still intact.\n");
1497 rv = 1;
1498 break;
1499 }
1500 sysfs_init(&sra, mdfd, 0);
1501 if (array_size == 0)
1502 err = sysfs_set_str(&sra, NULL, "array_size", "default");
1503 else
1504 err = sysfs_set_num(&sra, NULL, "array_size", array_size / 2);
1505 if (err < 0) {
1506 if (errno == E2BIG)
1507 fprintf(stderr, Name ": --array-size setting"
1508 " is too large.\n");
1509 else
1510 fprintf(stderr, Name ": current kernel does"
1511 " not support setting --array-size\n");
1512 rv = 1;
1513 break;
1514 }
1515 }
1516 if (devs_found > 1) {
1517
1518 /* must be '-a'. */
1519 if (size >= 0 || raiddisks || chunk || layout_str != NULL || bitmap_file) {
1520 fprintf(stderr, Name ": --add cannot be used with other geometry changes in --grow mode\n");
1521 rv = 1;
1522 break;
1523 }
1524 for (dv=devlist->next; dv ; dv=dv->next) {
1525 rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
1526 if (rv)
1527 break;
1528 }
1529 } else if (bitmap_file) {
1530 if (size >= 0 || raiddisks || chunk || layout_str != NULL) {
1531 fprintf(stderr, Name ": --bitmap changes cannot be used with other geometry changes in --grow mode\n");
1532 rv = 1;
1533 break;
1534 }
1535 if (delay == 0)
1536 delay = DEFAULT_BITMAP_DELAY;
1537 rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file,
1538 bitmap_chunk, delay, write_behind, force);
1539 } else if (size >= 0 || raiddisks != 0 || layout_str != NULL
1540 || chunk != 0 || level != UnSet) {
1541 rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file,
1542 size, level, layout_str, chunk, raiddisks);
1543 } else if (array_size < 0)
1544 fprintf(stderr, Name ": no changes to --grow\n");
1545 break;
1546 case INCREMENTAL:
1547 if (rebuild_map) {
1548 RebuildMap();
1549 }
1550 if (scan) {
1551 if (runstop <= 0) {
1552 fprintf(stderr, Name
1553 ": --incremental --scan meaningless without --run.\n");
1554 break;
1555 }
1556 if (devmode == 'f') {
1557 fprintf(stderr, Name
1558 ": --incremental --scan --fail not supported.\n");
1559 break;
1560 }
1561 rv = IncrementalScan(verbose);
1562 }
1563 if (!devlist) {
1564 if (!rebuild_map && !scan) {
1565 fprintf(stderr, Name
1566 ": --incremental requires a device.\n");
1567 rv = 1;
1568 }
1569 break;
1570 }
1571 if (devlist->next) {
1572 fprintf(stderr, Name
1573 ": --incremental can only handle one device.\n");
1574 rv = 1;
1575 break;
1576 }
1577 if (devmode == 'f') {
1578 rv = IncrementalRemove(devlist->devname, verbose-quiet);
1579 break;
1580 }
1581 rv = Incremental(devlist->devname, verbose-quiet, runstop,
1582 ss, homehost, require_homehost, autof);
1583 break;
1584 case AUTODETECT:
1585 autodetect();
1586 break;
1587 }
1588 exit(rv);
1589 }