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