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