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