]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdadm.c
Discourage large devices from being added to 0.90 arrays.
[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 case O(MANAGE,Force): /* add device which is too large */
613 force=1;
614 continue;
615
616 /* now for the Assemble options */
617 case O(CREATE,'u'): /* uuid of array */
618 case O(ASSEMBLE,'u'): /* uuid of array */
619 if (ident.uuid_set) {
620 fprintf(stderr, Name ": uuid cannot be set twice. "
621 "Second value %s.\n", optarg);
622 exit(2);
623 }
624 if (parse_uuid(optarg, ident.uuid))
625 ident.uuid_set = 1;
626 else {
627 fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
628 exit(2);
629 }
630 continue;
631
632 case O(CREATE,'N'):
633 case O(ASSEMBLE,'N'):
634 case O(MISC,'N'):
635 if (ident.name[0]) {
636 fprintf(stderr, Name ": name cannot be set twice. "
637 "Second value %s.\n", optarg);
638 exit(2);
639 }
640 if (mode == MISC && !subarray) {
641 fprintf(stderr, Name ": -N/--name only valid with --update-subarray in misc mode\n");
642 exit(2);
643 }
644 if (strlen(optarg) > 32) {
645 fprintf(stderr, Name ": name '%s' is too long, 32 chars max.\n",
646 optarg);
647 exit(2);
648 }
649 strcpy(ident.name, optarg);
650 continue;
651
652 case O(ASSEMBLE,'m'): /* super-minor for array */
653 case O(ASSEMBLE,SuperMinor):
654 if (ident.super_minor != UnSet) {
655 fprintf(stderr, Name ": super-minor cannot be set twice. "
656 "Second value: %s.\n", optarg);
657 exit(2);
658 }
659 if (strcmp(optarg, "dev")==0)
660 ident.super_minor = -2;
661 else {
662 ident.super_minor = strtoul(optarg, &cp, 10);
663 if (!optarg[0] || *cp) {
664 fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
665 exit(2);
666 }
667 }
668 continue;
669
670 case O(ASSEMBLE,'U'): /* update the superblock */
671 case O(MISC,'U'):
672 if (update) {
673 fprintf(stderr, Name ": Can only update one aspect"
674 " of superblock, both %s and %s given.\n",
675 update, optarg);
676 exit(2);
677 }
678 if (mode == MISC && !subarray) {
679 fprintf(stderr, Name ": Only subarrays can be"
680 " updated in misc mode\n");
681 exit(2);
682 }
683 update = optarg;
684 if (strcmp(update, "sparc2.2")==0)
685 continue;
686 if (strcmp(update, "super-minor") == 0)
687 continue;
688 if (strcmp(update, "summaries")==0)
689 continue;
690 if (strcmp(update, "resync")==0)
691 continue;
692 if (strcmp(update, "uuid")==0)
693 continue;
694 if (strcmp(update, "name")==0)
695 continue;
696 if (strcmp(update, "homehost")==0)
697 continue;
698 if (strcmp(update, "devicesize")==0)
699 continue;
700 if (strcmp(update, "no-bitmap")==0)
701 continue;
702 if (strcmp(update, "byteorder")==0) {
703 if (ss) {
704 fprintf(stderr,
705 Name ": must not set metadata"
706 " type with --update=byteorder.\n");
707 exit(2);
708 }
709 for(i=0; !ss && superlist[i]; i++)
710 ss = superlist[i]->match_metadata_desc(
711 "0.swap");
712 if (!ss) {
713 fprintf(stderr, Name ": INTERNAL ERROR"
714 " cannot find 0.swap\n");
715 exit(2);
716 }
717
718 continue;
719 }
720 if (strcmp(update,"?") == 0 ||
721 strcmp(update, "help") == 0) {
722 outf = stdout;
723 fprintf(outf, Name ": ");
724 } else {
725 outf = stderr;
726 fprintf(outf,
727 Name ": '--update=%s' is invalid. ",
728 update);
729 }
730 fprintf(outf, "Valid --update options are:\n"
731 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
732 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
733 " 'no-bitmap'\n");
734 exit(outf == stdout ? 0 : 2);
735
736 case O(MANAGE,'U'):
737 /* update=devicesize is allowed with --re-add */
738 if (devmode != 'a' || re_add != 1) {
739 fprintf(stderr, Name "--update in Manage mode only"
740 " allowed with --re-add.\n");
741 exit(1);
742 }
743 if (update) {
744 fprintf(stderr, Name ": Can only update one aspect"
745 " of superblock, both %s and %s given.\n",
746 update, optarg);
747 exit(2);
748 }
749 update = optarg;
750 if (strcmp(update, "devicesize") != 0) {
751 fprintf(stderr, Name ": only 'devicesize' can be"
752 " updated with --re-add\n");
753 exit(2);
754 }
755 continue;
756
757 case O(INCREMENTAL,NoDegraded):
758 fprintf(stderr, Name ": --no-degraded is deprecated in Incremental mode\n");
759 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
760 runstop = -1; /* --stop isn't allowed for --assemble,
761 * so we overload slightly */
762 continue;
763
764 case O(ASSEMBLE,'c'):
765 case O(ASSEMBLE,ConfigFile):
766 case O(INCREMENTAL, 'c'):
767 case O(INCREMENTAL, ConfigFile):
768 case O(MISC, 'c'):
769 case O(MISC, ConfigFile):
770 case O(MONITOR,'c'):
771 case O(MONITOR,ConfigFile):
772 if (configfile) {
773 fprintf(stderr, Name ": configfile cannot be set twice. "
774 "Second value is %s.\n", optarg);
775 exit(2);
776 }
777 configfile = optarg;
778 set_conffile(configfile);
779 /* FIXME possibly check that config file exists. Even parse it */
780 continue;
781 case O(ASSEMBLE,'s'): /* scan */
782 case O(MISC,'s'):
783 case O(MONITOR,'s'):
784 case O(INCREMENTAL,'s'):
785 scan = 1;
786 continue;
787
788 case O(MONITOR,'m'): /* mail address */
789 case O(MONITOR,EMail):
790 if (mailaddr)
791 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
792 optarg);
793 else
794 mailaddr = optarg;
795 continue;
796
797 case O(MONITOR,'p'): /* alert program */
798 case O(MONITOR,ProgramOpt): /* alert program */
799 if (program)
800 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
801 optarg);
802 else
803 program = optarg;
804 continue;
805
806 case O(MONITOR,'r'): /* rebuild increments */
807 case O(MONITOR,Increment):
808 increments = atoi(optarg);
809 if (increments>99 || increments<1) {
810 fprintf(stderr, Name ": please specify positive integer between 1 and 99 as rebuild increments.\n");
811 exit(2);
812 }
813 continue;
814
815 case O(MONITOR,'d'): /* delay in seconds */
816 case O(GROW, 'd'):
817 case O(BUILD,'d'): /* delay for bitmap updates */
818 case O(CREATE,'d'):
819 if (delay)
820 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
821 optarg);
822 else {
823 delay = strtol(optarg, &c, 10);
824 if (!optarg[0] || *c || delay<1) {
825 fprintf(stderr, Name ": invalid delay: %s\n",
826 optarg);
827 exit(2);
828 }
829 }
830 continue;
831 case O(MONITOR,'f'): /* daemonise */
832 case O(MONITOR,Fork):
833 daemonise = 1;
834 continue;
835 case O(MONITOR,'i'): /* pid */
836 if (pidfile)
837 fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
838 optarg);
839 else
840 pidfile = optarg;
841 continue;
842 case O(MONITOR,'1'): /* oneshot */
843 oneshot = 1;
844 continue;
845 case O(MONITOR,'t'): /* test */
846 test = 1;
847 continue;
848 case O(MONITOR,'y'): /* log messages to syslog */
849 openlog("mdadm", LOG_PID, SYSLOG_FACILITY);
850 dosyslog = 1;
851 continue;
852 case O(MONITOR, NoSharing):
853 spare_sharing = 0;
854 continue;
855 /* now the general management options. Some are applicable
856 * to other modes. None have arguments.
857 */
858 case O(GROW,'a'):
859 case O(GROW,Add):
860 case O(MANAGE,'a'):
861 case O(MANAGE,Add): /* add a drive */
862 devmode = 'a';
863 re_add = 0;
864 continue;
865 case O(MANAGE,ReAdd):
866 devmode = 'a';
867 re_add = 1;
868 continue;
869 case O(MANAGE,'r'): /* remove a drive */
870 case O(MANAGE,Remove):
871 devmode = 'r';
872 continue;
873 case O(MANAGE,'f'): /* set faulty */
874 case O(MANAGE,Fail):
875 case O(INCREMENTAL,'f'):
876 case O(INCREMENTAL,Remove):
877 case O(INCREMENTAL,Fail): /* r for incremental is taken, use f
878 * even though we will both fail and
879 * remove the device */
880 devmode = 'f';
881 continue;
882 case O(INCREMENTAL,'R'):
883 case O(MANAGE,'R'):
884 case O(ASSEMBLE,'R'):
885 case O(BUILD,'R'):
886 case O(CREATE,'R'): /* Run the array */
887 if (runstop < 0) {
888 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
889 exit(2);
890 }
891 runstop = 1;
892 continue;
893 case O(MANAGE,'S'):
894 if (runstop > 0) {
895 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
896 exit(2);
897 }
898 runstop = -1;
899 continue;
900 case O(MANAGE,'t'):
901 test = 1;
902 continue;
903
904 case O(MISC,'Q'):
905 case O(MISC,'D'):
906 case O(MISC,'E'):
907 case O(MISC,'K'):
908 case O(MISC,'R'):
909 case O(MISC,'S'):
910 case O(MISC,'X'):
911 case O(MISC,'o'):
912 case O(MISC,'w'):
913 case O(MISC,'W'):
914 case O(MISC, WaitOpt):
915 case O(MISC, Waitclean):
916 case O(MISC, DetailPlatform):
917 case O(MISC, KillSubarray):
918 case O(MISC, UpdateSubarray):
919 if (devmode && devmode != opt &&
920 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
921 fprintf(stderr, Name ": --examine/-E cannot be given with ");
922 if (devmode == 'E') {
923 if (option_index >= 0)
924 fprintf(stderr, "--%s\n",
925 long_options[option_index].name);
926 else
927 fprintf(stderr, "-%c\n", opt);
928 } else if (isalpha(devmode))
929 fprintf(stderr, "-%c\n", devmode);
930 else
931 fprintf(stderr, "previous option\n");
932 exit(2);
933 }
934 devmode = opt;
935 continue;
936 case O(MISC, UdevRules):
937 if (devmode && devmode != opt) {
938 fprintf(stderr, Name ": --udev-rules must"
939 " be the only option.\n");
940 } else {
941 if (udev_filename)
942 fprintf(stderr, Name ": only specify one udev "
943 "rule filename. %s ignored.\n",
944 optarg);
945 else
946 udev_filename = optarg;
947 }
948 devmode = opt;
949 continue;
950 case O(MISC,'t'):
951 test = 1;
952 continue;
953
954 case O(MISC, Sparc22):
955 if (devmode != 'E') {
956 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
957 exit(2);
958 }
959 SparcAdjust = 1;
960 continue;
961
962 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
963 case O(ASSEMBLE,Bitmap):
964 if (!optarg) {
965 fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n");
966 exit(2);
967 }
968 if (strcmp(optarg, "internal")==0) {
969 fprintf(stderr, Name ": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
970 continue;
971 }
972 bitmap_fd = open(optarg, O_RDWR);
973 if (!*optarg || bitmap_fd < 0) {
974 fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno));
975 exit(2);
976 }
977 ident.bitmap_fd = bitmap_fd; /* for Assemble */
978 continue;
979
980 case O(ASSEMBLE, BackupFile):
981 case O(GROW, BackupFile):
982 /* Specify a file into which grow might place a backup,
983 * or from which assemble might recover a backup
984 */
985 if (backup_file) {
986 fprintf(stderr, Name ": backup file already specified, rejecting %s\n", optarg);
987 exit(2);
988 }
989 backup_file = optarg;
990 continue;
991
992 case O(ASSEMBLE, InvalidBackup):
993 /* Acknowledge that the backupfile is invalid, but ask
994 * to continue anyway
995 */
996 invalid_backup = 1;
997 continue;
998
999 case O(BUILD,'b'):
1000 case O(BUILD,Bitmap):
1001 case O(CREATE,'b'):
1002 case O(CREATE,Bitmap): /* here we create the bitmap */
1003 if (strcmp(optarg, "none") == 0) {
1004 fprintf(stderr, Name ": '--bitmap none' only"
1005 " support for --grow\n");
1006 exit(2);
1007 }
1008 /* FALL THROUGH */
1009 case O(GROW,'b'):
1010 case O(GROW,Bitmap):
1011 if (strcmp(optarg, "internal")== 0 ||
1012 strcmp(optarg, "none")== 0 ||
1013 strchr(optarg, '/') != NULL) {
1014 bitmap_file = optarg;
1015 continue;
1016 }
1017 /* probable typo */
1018 fprintf(stderr, Name ": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1019 " not '%s'\n", optarg);
1020 exit(2);
1021
1022 case O(GROW,BitmapChunk):
1023 case O(BUILD,BitmapChunk):
1024 case O(CREATE,BitmapChunk): /* bitmap chunksize */
1025 bitmap_chunk = parse_size(optarg);
1026 if (bitmap_chunk < 0 ||
1027 bitmap_chunk & (bitmap_chunk - 1)) {
1028 fprintf(stderr,
1029 Name ": invalid bitmap chunksize: %s\n",
1030 optarg);
1031 exit(2);
1032 }
1033 /* convert sectors to B, chunk of 0 means 512B */
1034 bitmap_chunk = bitmap_chunk ? bitmap_chunk * 512 : 512;
1035 continue;
1036
1037 case O(GROW, WriteBehind):
1038 case O(BUILD, WriteBehind):
1039 case O(CREATE, WriteBehind): /* write-behind mode */
1040 write_behind = DEFAULT_MAX_WRITE_BEHIND;
1041 if (optarg) {
1042 write_behind = strtol(optarg, &c, 10);
1043 if (write_behind < 0 || *c ||
1044 write_behind > 16383) {
1045 fprintf(stderr, Name ": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
1046 exit(2);
1047 }
1048 }
1049 continue;
1050
1051 case O(INCREMENTAL, 'r'):
1052 case O(INCREMENTAL, RebuildMapOpt):
1053 rebuild_map = 1;
1054 continue;
1055 case O(INCREMENTAL, IncrementalPath):
1056 remove_path = optarg;
1057 continue;
1058 }
1059 /* We have now processed all the valid options. Anything else is
1060 * an error
1061 */
1062 if (option_index > 0)
1063 fprintf(stderr, Name ":option --%s not valid in %s mode\n",
1064 long_options[option_index].name,
1065 map_num(modes, mode));
1066 else
1067 fprintf(stderr, Name ": option -%c not valid in %s mode\n",
1068 opt, map_num(modes, mode));
1069 exit(2);
1070
1071 }
1072
1073 if (print_help) {
1074 char *help_text = Help;
1075 if (print_help == 2)
1076 help_text = OptionHelp;
1077 else
1078 switch (mode) {
1079 case ASSEMBLE : help_text = Help_assemble; break;
1080 case BUILD : help_text = Help_build; break;
1081 case CREATE : help_text = Help_create; break;
1082 case MANAGE : help_text = Help_manage; break;
1083 case MISC : help_text = Help_misc; break;
1084 case MONITOR : help_text = Help_monitor; break;
1085 case GROW : help_text = Help_grow; break;
1086 case INCREMENTAL:help_text= Help_incr; break;
1087 }
1088 fputs(help_text,stdout);
1089 exit(0);
1090 }
1091
1092 if (!mode && devs_found) {
1093 mode = MISC;
1094 devmode = 'Q';
1095 if (devlist->disposition == 0)
1096 devlist->disposition = devmode;
1097 }
1098 if (!mode) {
1099 fputs(Usage, stderr);
1100 exit(2);
1101 }
1102
1103 if (symlinks) {
1104 struct createinfo *ci = conf_get_create_info();
1105
1106 if (strcasecmp(symlinks, "yes") == 0)
1107 ci->symlinks = 1;
1108 else if (strcasecmp(symlinks, "no") == 0)
1109 ci->symlinks = 0;
1110 else {
1111 fprintf(stderr, Name ": option --symlinks must be 'no' or 'yes'\n");
1112 exit(2);
1113 }
1114 }
1115 /* Ok, got the option parsing out of the way
1116 * hopefully it's mostly right but there might be some stuff
1117 * missing
1118 *
1119 * That is mosty checked in the per-mode stuff but...
1120 *
1121 * For @,B,C and A without -s, the first device listed must be an md device
1122 * we check that here and open it.
1123 */
1124
1125 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
1126 (mode == ASSEMBLE && ! scan)) {
1127 if (devs_found < 1) {
1128 fprintf(stderr, Name ": an md device must be given in this mode\n");
1129 exit(2);
1130 }
1131 if ((int)ident.super_minor == -2 && autof) {
1132 fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
1133 exit(2);
1134 }
1135 if (mode == MANAGE || mode == GROW) {
1136 mdfd = open_mddev(devlist->devname, 1);
1137 if (mdfd < 0)
1138 exit(1);
1139 } else
1140 /* non-existent device is OK */
1141 mdfd = open_mddev(devlist->devname, 0);
1142 if (mdfd == -2) {
1143 fprintf(stderr, Name ": device %s exists but is not an "
1144 "md array.\n", devlist->devname);
1145 exit(1);
1146 }
1147 if ((int)ident.super_minor == -2) {
1148 struct stat stb;
1149 if (mdfd < 0) {
1150 fprintf(stderr, Name ": --super-minor=dev given, and "
1151 "listed device %s doesn't exist.\n",
1152 devlist->devname);
1153 exit(1);
1154 }
1155 fstat(mdfd, &stb);
1156 ident.super_minor = minor(stb.st_rdev);
1157 }
1158 if (mdfd >= 0 && mode != MANAGE && mode != GROW) {
1159 /* We don't really want this open yet, we just might
1160 * have wanted to check some things
1161 */
1162 close(mdfd);
1163 mdfd = -1;
1164 }
1165 }
1166
1167 if (raiddisks) {
1168 if (raiddisks == 1 && !force && level != -5) {
1169 fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
1170 " a mistake. If you really mean it you will need to specify --force before\n"
1171 " setting the number of drives.\n");
1172 exit(2);
1173 }
1174 }
1175
1176 if (homehost == NULL)
1177 homehost = conf_get_homehost(&require_homehost);
1178 if (homehost == NULL || strcasecmp(homehost, "<system>")==0) {
1179 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
1180 sys_hostname[sizeof(sys_hostname)-1] = 0;
1181 homehost = sys_hostname;
1182 }
1183 }
1184 if (homehost && (!homehost[0] || strcasecmp(homehost, "<none>") == 0)) {
1185 homehost = NULL;
1186 require_homehost = 0;
1187 }
1188
1189 if ((mode != MISC || devmode != 'E') &&
1190 geteuid() != 0) {
1191 fprintf(stderr, Name ": must be super-user to perform this action\n");
1192 exit(1);
1193 }
1194
1195 ident.autof = autof;
1196
1197 rv = 0;
1198 switch(mode) {
1199 case MANAGE:
1200 /* readonly, add/remove, readwrite, runstop */
1201 if (readonly>0)
1202 rv = Manage_ro(devlist->devname, mdfd, readonly);
1203 if (!rv && devs_found>1)
1204 rv = Manage_subdevs(devlist->devname, mdfd,
1205 devlist->next, verbose-quiet, test,
1206 update, force);
1207 if (!rv && readonly < 0)
1208 rv = Manage_ro(devlist->devname, mdfd, readonly);
1209 if (!rv && runstop)
1210 rv = Manage_runstop(devlist->devname, mdfd, runstop, quiet);
1211 break;
1212 case ASSEMBLE:
1213 if (devs_found == 1 && ident.uuid_set == 0 &&
1214 ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) {
1215 /* Only a device has been given, so get details from config file */
1216 struct mddev_ident *array_ident = conf_get_ident(devlist->devname);
1217 if (array_ident == NULL) {
1218 fprintf(stderr, Name ": %s not identified in config file.\n",
1219 devlist->devname);
1220 rv |= 1;
1221 if (mdfd >= 0)
1222 close(mdfd);
1223 } else {
1224 if (array_ident->autof == 0)
1225 array_ident->autof = autof;
1226 rv |= Assemble(ss, devlist->devname, array_ident,
1227 NULL, backup_file, invalid_backup,
1228 readonly, runstop, update,
1229 homehost, require_homehost,
1230 verbose-quiet, force);
1231 }
1232 } else if (!scan)
1233 rv = Assemble(ss, devlist->devname, &ident,
1234 devlist->next, backup_file, invalid_backup,
1235 readonly, runstop, update,
1236 homehost, require_homehost,
1237 verbose-quiet, force);
1238 else if (devs_found>0) {
1239 if (update && devs_found > 1) {
1240 fprintf(stderr, Name ": can only update a single array at a time\n");
1241 exit(1);
1242 }
1243 if (backup_file && devs_found > 1) {
1244 fprintf(stderr, Name ": can only assemble a single array when providing a backup file.\n");
1245 exit(1);
1246 }
1247 for (dv = devlist ; dv ; dv=dv->next) {
1248 struct mddev_ident *array_ident = conf_get_ident(dv->devname);
1249 if (array_ident == NULL) {
1250 fprintf(stderr, Name ": %s not identified in config file.\n",
1251 dv->devname);
1252 rv |= 1;
1253 continue;
1254 }
1255 if (array_ident->autof == 0)
1256 array_ident->autof = autof;
1257 rv |= Assemble(ss, dv->devname, array_ident,
1258 NULL, backup_file, invalid_backup,
1259 readonly, runstop, update,
1260 homehost, require_homehost,
1261 verbose-quiet, force);
1262 }
1263 } else {
1264 struct mddev_ident *a, *array_list = conf_get_ident(NULL);
1265 struct mddev_dev *devlist = conf_get_devs();
1266 int cnt = 0;
1267 int failures, successes;
1268 if (devlist == NULL) {
1269 fprintf(stderr, Name ": No devices listed in conf file were found.\n");
1270 exit(1);
1271 }
1272 if (update) {
1273 fprintf(stderr, Name ": --update not meaningful with a --scan assembly.\n");
1274 exit(1);
1275 }
1276 if (backup_file) {
1277 fprintf(stderr, Name ": --backup_file not meaningful with a --scan assembly.\n");
1278 exit(1);
1279 }
1280 for (a = array_list; a ; a = a->next) {
1281 a->assembled = 0;
1282 if (a->autof == 0)
1283 a->autof = autof;
1284 }
1285 do {
1286 failures = 0;
1287 successes = 0;
1288 rv = 0;
1289 for (a = array_list; a ; a = a->next) {
1290 int r;
1291 if (a->assembled)
1292 continue;
1293 if (a->devname &&
1294 strcasecmp(a->devname, "<ignore>") == 0)
1295 continue;
1296
1297 r = Assemble(ss, a->devname,
1298 a,
1299 NULL, NULL, 0,
1300 readonly, runstop, NULL,
1301 homehost, require_homehost,
1302 verbose-quiet, force);
1303 if (r == 0) {
1304 a->assembled = 1;
1305 successes++;
1306 } else
1307 failures++;
1308 rv |= r;
1309 cnt++;
1310 }
1311 } while (failures && successes);
1312 if (homehost && cnt == 0) {
1313 /* Maybe we can auto-assemble something.
1314 * Repeatedly call Assemble in auto-assemble mode
1315 * until it fails
1316 */
1317 int rv2;
1318 int acnt;
1319 ident.autof = autof;
1320 do {
1321 struct mddev_dev *devlist = conf_get_devs();
1322 acnt = 0;
1323 do {
1324 rv2 = Assemble(ss, NULL,
1325 &ident,
1326 devlist, NULL, 0,
1327 readonly, runstop, NULL,
1328 homehost, require_homehost,
1329 verbose-quiet, force);
1330 if (rv2==0) {
1331 cnt++;
1332 acnt++;
1333 }
1334 } while (rv2!=2);
1335 /* Incase there are stacked devices, we need to go around again */
1336 } while (acnt);
1337 if (cnt == 0 && rv == 0) {
1338 fprintf(stderr, Name ": No arrays found in config file or automatically\n");
1339 rv = 1;
1340 } else if (cnt)
1341 rv = 0;
1342 } else if (cnt == 0 && rv == 0) {
1343 fprintf(stderr, Name ": No arrays found in config file\n");
1344 rv = 1;
1345 }
1346 }
1347 break;
1348 case BUILD:
1349 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1350 if (write_behind && !bitmap_file) {
1351 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1352 rv = 1;
1353 break;
1354 }
1355 if (raiddisks == 0) {
1356 fprintf(stderr, Name ": no raid-devices specified.\n");
1357 rv = 1;
1358 break;
1359 }
1360
1361 if (bitmap_file) {
1362 if (strcmp(bitmap_file, "internal")==0) {
1363 fprintf(stderr, Name ": 'internal' bitmaps not supported with --build\n");
1364 rv |= 1;
1365 break;
1366 }
1367 }
1368 rv = Build(devlist->devname, chunk, level, layout,
1369 raiddisks, devlist->next, assume_clean,
1370 bitmap_file, bitmap_chunk, write_behind,
1371 delay, verbose-quiet, autof, size);
1372 break;
1373 case CREATE:
1374 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1375 if (write_behind && !bitmap_file) {
1376 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1377 rv = 1;
1378 break;
1379 }
1380 if (raiddisks == 0) {
1381 fprintf(stderr, Name ": no raid-devices specified.\n");
1382 rv = 1;
1383 break;
1384 }
1385
1386 rv = Create(ss, devlist->devname, chunk, level, layout, size<0 ? 0 : size,
1387 raiddisks, sparedisks, ident.name, homehost,
1388 ident.uuid_set ? ident.uuid : NULL,
1389 devs_found-1, devlist->next, runstop, verbose-quiet, force, assume_clean,
1390 bitmap_file, bitmap_chunk, write_behind, delay, autof);
1391 break;
1392 case MISC:
1393 if (devmode == 'E') {
1394 if (devlist == NULL && !scan) {
1395 fprintf(stderr, Name ": No devices to examine\n");
1396 exit(2);
1397 }
1398 if (devlist == NULL)
1399 devlist = conf_get_devs();
1400 if (devlist == NULL) {
1401 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
1402 exit(1);
1403 }
1404 if (brief && verbose)
1405 brief = 2;
1406 rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief,
1407 export, scan,
1408 SparcAdjust, ss, homehost);
1409 } else if (devmode == DetailPlatform) {
1410 rv = Detail_Platform(ss ? ss->ss : NULL, ss ? scan : 1, verbose);
1411 } else {
1412 if (devlist == NULL) {
1413 if ((devmode=='D' || devmode == Waitclean) && scan) {
1414 /* apply --detail or --wait-clean to
1415 * all devices in /proc/mdstat
1416 */
1417 struct mdstat_ent *ms = mdstat_read(0, 1);
1418 struct mdstat_ent *e;
1419 struct map_ent *map = NULL;
1420 int members;
1421 int v = verbose>1?0:verbose+1;
1422
1423 for (members = 0; members <= 1; members++) {
1424 for (e=ms ; e ; e=e->next) {
1425 char *name;
1426 struct map_ent *me;
1427 int member = e->metadata_version &&
1428 strncmp(e->metadata_version,
1429 "external:/", 10) == 0;
1430 if (members != member)
1431 continue;
1432 me = map_by_devnum(&map, e->devnum);
1433 if (me && me->path
1434 && strcmp(me->path, "/unknown") != 0)
1435 name = me->path;
1436 else
1437 name = get_md_name(e->devnum);
1438
1439 if (!name) {
1440 fprintf(stderr, Name ": cannot find device file for %s\n",
1441 e->dev);
1442 continue;
1443 }
1444 if (devmode == 'D')
1445 rv |= Detail(name, v,
1446 export, test,
1447 homehost);
1448 else
1449 rv |= WaitClean(name, -1, v);
1450 put_md_name(name);
1451 }
1452 }
1453 free_mdstat(ms);
1454 } else if (devmode == 'S' && scan) {
1455 /* apply --stop to all devices in /proc/mdstat */
1456 /* Due to possible stacking of devices, repeat until
1457 * nothing more can be stopped
1458 */
1459 int progress=1, err;
1460 int last = 0;
1461 do {
1462 struct mdstat_ent *ms = mdstat_read(0, 0);
1463 struct mdstat_ent *e;
1464
1465 if (!progress) last = 1;
1466 progress = 0; err = 0;
1467 for (e=ms ; e ; e=e->next) {
1468 char *name = get_md_name(e->devnum);
1469
1470 if (!name) {
1471 fprintf(stderr, Name ": cannot find device file for %s\n",
1472 e->dev);
1473 continue;
1474 }
1475 mdfd = open_mddev(name, 1);
1476 if (mdfd >= 0) {
1477 if (Manage_runstop(name, mdfd, -1, quiet?1:last?0:-1))
1478 err = 1;
1479 else
1480 progress = 1;
1481 close(mdfd);
1482 }
1483
1484 put_md_name(name);
1485 }
1486 free_mdstat(ms);
1487 } while (!last && err);
1488 if (err) rv |= 1;
1489 } else if (devmode == UdevRules) {
1490 rv = Write_rules(udev_filename);
1491 } else {
1492 fprintf(stderr, Name ": No devices given.\n");
1493 exit(2);
1494 }
1495 }
1496 for (dv=devlist ; dv; dv=dv->next) {
1497 switch(dv->disposition) {
1498 case 'D':
1499 rv |= Detail(dv->devname,
1500 brief?1+verbose:0,
1501 export, test, homehost);
1502 continue;
1503 case 'K': /* Zero superblock */
1504 if (ss)
1505 rv |= Kill(dv->devname, ss, force, quiet,0);
1506 else {
1507 int q = quiet;
1508 do {
1509 rv |= Kill(dv->devname, NULL, force, q, 0);
1510 q = 1;
1511 } while (rv == 0);
1512 rv &= ~2;
1513 }
1514 continue;
1515 case 'Q':
1516 rv |= Query(dv->devname); continue;
1517 case 'X':
1518 rv |= ExamineBitmap(dv->devname, brief, ss); continue;
1519 case 'W':
1520 case WaitOpt:
1521 rv |= Wait(dv->devname); continue;
1522 case Waitclean:
1523 rv |= WaitClean(dv->devname, -1, verbose-quiet); continue;
1524 case KillSubarray:
1525 rv |= Kill_subarray(dv->devname, subarray, quiet);
1526 continue;
1527 case UpdateSubarray:
1528 if (update == NULL) {
1529 fprintf(stderr,
1530 Name ": -U/--update must be specified with --update-subarray\n");
1531 rv |= 1;
1532 continue;
1533 }
1534 rv |= Update_subarray(dv->devname, subarray, update, &ident, quiet);
1535 continue;
1536 }
1537 mdfd = open_mddev(dv->devname, 1);
1538 if (mdfd>=0) {
1539 switch(dv->disposition) {
1540 case 'R':
1541 rv |= Manage_runstop(dv->devname, mdfd, 1, quiet); break;
1542 case 'S':
1543 rv |= Manage_runstop(dv->devname, mdfd, -1, quiet); break;
1544 case 'o':
1545 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1546 case 'w':
1547 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1548 }
1549 close(mdfd);
1550 } else
1551 rv |= 1;
1552 }
1553 }
1554 break;
1555 case MONITOR:
1556 if (!devlist && !scan) {
1557 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
1558 rv = 1;
1559 break;
1560 }
1561 if (pidfile && !daemonise) {
1562 fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
1563 rv = 1;
1564 break;
1565 }
1566 if (delay == 0) {
1567 if (get_linux_version() > 2006016)
1568 /* mdstat responds to poll */
1569 delay = 1000;
1570 else
1571 delay = 60;
1572 }
1573 rv= Monitor(devlist, mailaddr, program,
1574 delay?delay:60, daemonise, scan, oneshot,
1575 dosyslog, test, pidfile, increments, spare_sharing);
1576 break;
1577
1578 case GROW:
1579 if (array_size >= 0) {
1580 /* alway impose array size first, independent of
1581 * anything else
1582 * Do not allow level or raid_disks changes at the
1583 * same time as that can be irreversibly destructive.
1584 */
1585 struct mdinfo sra;
1586 int err;
1587 if (raiddisks || level != UnSet) {
1588 fprintf(stderr, Name ": cannot change array size in same operation "
1589 "as changing raiddisks or level.\n"
1590 " Change size first, then check that data is still intact.\n");
1591 rv = 1;
1592 break;
1593 }
1594 sysfs_init(&sra, mdfd, 0);
1595 if (array_size == 0)
1596 err = sysfs_set_str(&sra, NULL, "array_size", "default");
1597 else
1598 err = sysfs_set_num(&sra, NULL, "array_size", array_size / 2);
1599 if (err < 0) {
1600 if (errno == E2BIG)
1601 fprintf(stderr, Name ": --array-size setting"
1602 " is too large.\n");
1603 else
1604 fprintf(stderr, Name ": current kernel does"
1605 " not support setting --array-size\n");
1606 rv = 1;
1607 break;
1608 }
1609 }
1610 if (devs_found > 1 && raiddisks == 0) {
1611 /* must be '-a'. */
1612 if (size >= 0 || chunk || layout_str != NULL || bitmap_file) {
1613 fprintf(stderr, Name ": --add cannot be used with "
1614 "other geometry changes in --grow mode\n");
1615 rv = 1;
1616 break;
1617 }
1618 for (dv=devlist->next; dv ; dv=dv->next) {
1619 rv = Grow_Add_device(devlist->devname, mdfd,
1620 dv->devname);
1621 if (rv)
1622 break;
1623 }
1624 } else if (bitmap_file) {
1625 if (size >= 0 || raiddisks || chunk ||
1626 layout_str != NULL || devs_found > 1) {
1627 fprintf(stderr, Name ": --bitmap changes cannot be "
1628 "used with other geometry changes "
1629 "in --grow mode\n");
1630 rv = 1;
1631 break;
1632 }
1633 if (delay == 0)
1634 delay = DEFAULT_BITMAP_DELAY;
1635 rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file,
1636 bitmap_chunk, delay, write_behind, force);
1637 } else if (size >= 0 || raiddisks != 0 || layout_str != NULL
1638 || chunk != 0 || level != UnSet) {
1639 rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file,
1640 size, level, layout_str, chunk, raiddisks,
1641 devlist->next,
1642 assume_clean, force);
1643 } else if (array_size < 0)
1644 fprintf(stderr, Name ": no changes to --grow\n");
1645 break;
1646 case INCREMENTAL:
1647 if (rebuild_map) {
1648 RebuildMap();
1649 }
1650 if (scan) {
1651 if (runstop <= 0) {
1652 fprintf(stderr, Name
1653 ": --incremental --scan meaningless without --run.\n");
1654 break;
1655 }
1656 if (devmode == 'f') {
1657 fprintf(stderr, Name
1658 ": --incremental --scan --fail not supported.\n");
1659 break;
1660 }
1661 rv = IncrementalScan(verbose);
1662 }
1663 if (!devlist) {
1664 if (!rebuild_map && !scan) {
1665 fprintf(stderr, Name
1666 ": --incremental requires a device.\n");
1667 rv = 1;
1668 }
1669 break;
1670 }
1671 if (devlist->next) {
1672 fprintf(stderr, Name
1673 ": --incremental can only handle one device.\n");
1674 rv = 1;
1675 break;
1676 }
1677 if (devmode == 'f')
1678 rv = IncrementalRemove(devlist->devname, remove_path,
1679 verbose-quiet);
1680 else
1681 rv = Incremental(devlist->devname, verbose-quiet,
1682 runstop, ss, homehost,
1683 require_homehost, autof);
1684 break;
1685 case AUTODETECT:
1686 autodetect();
1687 break;
1688 }
1689 exit(rv);
1690 }