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