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