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