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