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