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