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