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