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