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