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