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