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