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