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