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