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