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