Create: support --readonly flag.
[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,'o'):
711 case O(MANAGE,'o'):
712 case O(CREATE,'o'):
713 readonly = 1;
714 continue;
715
716 case O(ASSEMBLE,'U'): /* update the superblock */
717 case O(MISC,'U'):
718 if (update) {
719 pr_err("Can only update one aspect"
720 " of superblock, both %s and %s given.\n",
721 update, optarg);
722 exit(2);
723 }
724 if (mode == MISC && !subarray) {
725 pr_err("Only subarrays can be"
726 " updated in misc mode\n");
727 exit(2);
728 }
729 update = optarg;
730 if (strcmp(update, "sparc2.2")==0)
731 continue;
732 if (strcmp(update, "super-minor") == 0)
733 continue;
734 if (strcmp(update, "summaries")==0)
735 continue;
736 if (strcmp(update, "resync")==0)
737 continue;
738 if (strcmp(update, "uuid")==0)
739 continue;
740 if (strcmp(update, "name")==0)
741 continue;
742 if (strcmp(update, "homehost")==0)
743 continue;
744 if (strcmp(update, "devicesize")==0)
745 continue;
746 if (strcmp(update, "no-bitmap")==0)
747 continue;
748 if (strcmp(update, "byteorder")==0) {
749 if (ss) {
750 pr_err("must not set metadata"
751 " type with --update=byteorder.\n");
752 exit(2);
753 }
754 for(i=0; !ss && superlist[i]; i++)
755 ss = superlist[i]->match_metadata_desc(
756 "0.swap");
757 if (!ss) {
758 pr_err("INTERNAL ERROR"
759 " cannot find 0.swap\n");
760 exit(2);
761 }
762
763 continue;
764 }
765 if (strcmp(update,"?") == 0 ||
766 strcmp(update, "help") == 0) {
767 outf = stdout;
768 fprintf(outf, Name ": ");
769 } else {
770 outf = stderr;
771 fprintf(outf,
772 Name ": '--update=%s' is invalid. ",
773 update);
774 }
775 fprintf(outf, "Valid --update options are:\n"
776 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
777 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
778 " 'no-bitmap'\n");
779 exit(outf == stdout ? 0 : 2);
780
781 case O(MANAGE,'U'):
782 /* update=devicesize is allowed with --re-add */
783 if (devmode != 'A') {
784 pr_err("--update in Manage mode only"
785 " allowed with --re-add.\n");
786 exit(1);
787 }
788 if (update) {
789 pr_err("Can only update one aspect"
790 " of superblock, both %s and %s given.\n",
791 update, optarg);
792 exit(2);
793 }
794 update = optarg;
795 if (strcmp(update, "devicesize") != 0) {
796 pr_err("only 'devicesize' can be"
797 " updated with --re-add\n");
798 exit(2);
799 }
800 continue;
801
802 case O(INCREMENTAL,NoDegraded):
803 pr_err("--no-degraded is deprecated in Incremental mode\n");
804 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
805 runstop = -1; /* --stop isn't allowed for --assemble,
806 * so we overload slightly */
807 continue;
808
809 case O(ASSEMBLE,'c'):
810 case O(ASSEMBLE,ConfigFile):
811 case O(INCREMENTAL, 'c'):
812 case O(INCREMENTAL, ConfigFile):
813 case O(MISC, 'c'):
814 case O(MISC, ConfigFile):
815 case O(MONITOR,'c'):
816 case O(MONITOR,ConfigFile):
817 if (configfile) {
818 pr_err("configfile cannot be set twice. "
819 "Second value is %s.\n", optarg);
820 exit(2);
821 }
822 configfile = optarg;
823 set_conffile(configfile);
824 /* FIXME possibly check that config file exists. Even parse it */
825 continue;
826 case O(ASSEMBLE,'s'): /* scan */
827 case O(MISC,'s'):
828 case O(MONITOR,'s'):
829 case O(INCREMENTAL,'s'):
830 scan = 1;
831 continue;
832
833 case O(MONITOR,'m'): /* mail address */
834 case O(MONITOR,EMail):
835 if (mailaddr)
836 pr_err("only specify one mailaddress. %s ignored.\n",
837 optarg);
838 else
839 mailaddr = optarg;
840 continue;
841
842 case O(MONITOR,'p'): /* alert program */
843 case O(MONITOR,ProgramOpt): /* alert program */
844 if (program)
845 pr_err("only specify one alter program. %s ignored.\n",
846 optarg);
847 else
848 program = optarg;
849 continue;
850
851 case O(MONITOR,'r'): /* rebuild increments */
852 case O(MONITOR,Increment):
853 increments = atoi(optarg);
854 if (increments > 99 || increments < 1) {
855 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
856 exit(2);
857 }
858 continue;
859
860 case O(MONITOR,'d'): /* delay in seconds */
861 case O(GROW, 'd'):
862 case O(BUILD,'d'): /* delay for bitmap updates */
863 case O(CREATE,'d'):
864 if (delay)
865 pr_err("only specify delay once. %s ignored.\n",
866 optarg);
867 else {
868 delay = strtol(optarg, &c, 10);
869 if (!optarg[0] || *c || delay<1) {
870 pr_err("invalid delay: %s\n",
871 optarg);
872 exit(2);
873 }
874 }
875 continue;
876 case O(MONITOR,'f'): /* daemonise */
877 case O(MONITOR,Fork):
878 daemonise = 1;
879 continue;
880 case O(MONITOR,'i'): /* pid */
881 if (pidfile)
882 pr_err("only specify one pid file. %s ignored.\n",
883 optarg);
884 else
885 pidfile = optarg;
886 continue;
887 case O(MONITOR,'1'): /* oneshot */
888 oneshot = 1;
889 spare_sharing = 0;
890 continue;
891 case O(MONITOR,'t'): /* test */
892 test = 1;
893 continue;
894 case O(MONITOR,'y'): /* log messages to syslog */
895 openlog("mdadm", LOG_PID, SYSLOG_FACILITY);
896 dosyslog = 1;
897 continue;
898 case O(MONITOR, NoSharing):
899 spare_sharing = 0;
900 continue;
901
902 /* now the general management options. Some are applicable
903 * to other modes. None have arguments.
904 */
905 case O(GROW,'a'):
906 case O(GROW,Add):
907 case O(MANAGE,'a'):
908 case O(MANAGE,Add): /* add a drive */
909 devmode = 'a';
910 continue;
911 case O(MANAGE,ReAdd):
912 devmode = 'A';
913 continue;
914 case O(MANAGE,'r'): /* remove a drive */
915 case O(MANAGE,Remove):
916 devmode = 'r';
917 continue;
918 case O(MANAGE,'f'): /* set faulty */
919 case O(MANAGE,Fail):
920 case O(INCREMENTAL,'f'):
921 case O(INCREMENTAL,Remove):
922 case O(INCREMENTAL,Fail): /* r for incremental is taken, use f
923 * even though we will both fail and
924 * remove the device */
925 devmode = 'f';
926 continue;
927 case O(INCREMENTAL,'R'):
928 case O(MANAGE,'R'):
929 case O(ASSEMBLE,'R'):
930 case O(BUILD,'R'):
931 case O(CREATE,'R'): /* Run the array */
932 if (runstop < 0) {
933 pr_err("Cannot both Stop and Run an array\n");
934 exit(2);
935 }
936 runstop = 1;
937 continue;
938 case O(MANAGE,'S'):
939 if (runstop > 0) {
940 pr_err("Cannot both Run and Stop an array\n");
941 exit(2);
942 }
943 runstop = -1;
944 continue;
945 case O(MANAGE,'t'):
946 test = 1;
947 continue;
948
949 case O(MISC,'Q'):
950 case O(MISC,'D'):
951 case O(MISC,'E'):
952 case O(MISC,KillOpt):
953 case O(MISC,'R'):
954 case O(MISC,'S'):
955 case O(MISC,'X'):
956 case O(MISC,'o'):
957 case O(MISC,'w'):
958 case O(MISC,'W'):
959 case O(MISC, WaitOpt):
960 case O(MISC, Waitclean):
961 case O(MISC, DetailPlatform):
962 case O(MISC, KillSubarray):
963 case O(MISC, UpdateSubarray):
964 if (opt == KillSubarray || opt == UpdateSubarray) {
965 if (subarray) {
966 pr_err("subarray can only"
967 " be specified once\n");
968 exit(2);
969 }
970 subarray = optarg;
971 }
972 if (devmode && devmode != opt &&
973 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
974 pr_err("--examine/-E cannot be given with ");
975 if (devmode == 'E') {
976 if (option_index >= 0)
977 fprintf(stderr, "--%s\n",
978 long_options[option_index].name);
979 else
980 fprintf(stderr, "-%c\n", opt);
981 } else if (isalpha(devmode))
982 fprintf(stderr, "-%c\n", devmode);
983 else
984 fprintf(stderr, "previous option\n");
985 exit(2);
986 }
987 devmode = opt;
988 continue;
989 case O(MISC, UdevRules):
990 if (devmode && devmode != opt) {
991 pr_err("--udev-rules must"
992 " be the only option.\n");
993 } else {
994 if (udev_filename)
995 pr_err("only specify one udev "
996 "rule filename. %s ignored.\n",
997 optarg);
998 else
999 udev_filename = optarg;
1000 }
1001 devmode = opt;
1002 continue;
1003 case O(MISC,'t'):
1004 test = 1;
1005 continue;
1006
1007 case O(MISC, Sparc22):
1008 if (devmode != 'E') {
1009 pr_err("--sparc2.2 only allowed with --examine\n");
1010 exit(2);
1011 }
1012 SparcAdjust = 1;
1013 continue;
1014
1015 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
1016 case O(ASSEMBLE,Bitmap):
1017 if (!optarg) {
1018 pr_err("bitmap file needed with -b in --assemble mode\n");
1019 exit(2);
1020 }
1021 if (strcmp(optarg, "internal")==0) {
1022 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1023 continue;
1024 }
1025 bitmap_fd = open(optarg, O_RDWR);
1026 if (!*optarg || bitmap_fd < 0) {
1027 pr_err("cannot open bitmap file %s: %s\n", optarg, strerror(errno));
1028 exit(2);
1029 }
1030 ident.bitmap_fd = bitmap_fd; /* for Assemble */
1031 continue;
1032
1033 case O(ASSEMBLE, BackupFile):
1034 case O(GROW, BackupFile):
1035 /* Specify a file into which grow might place a backup,
1036 * or from which assemble might recover a backup
1037 */
1038 if (backup_file) {
1039 pr_err("backup file already specified, rejecting %s\n", optarg);
1040 exit(2);
1041 }
1042 backup_file = optarg;
1043 continue;
1044
1045 case O(GROW, Continue):
1046 /* Continue interrupted grow
1047 */
1048 grow_continue = 1;
1049 continue;
1050 case O(ASSEMBLE, InvalidBackup):
1051 /* Acknowledge that the backupfile is invalid, but ask
1052 * to continue anyway
1053 */
1054 invalid_backup = 1;
1055 continue;
1056
1057 case O(BUILD,'b'):
1058 case O(BUILD,Bitmap):
1059 case O(CREATE,'b'):
1060 case O(CREATE,Bitmap): /* here we create the bitmap */
1061 if (strcmp(optarg, "none") == 0) {
1062 pr_err("'--bitmap none' only"
1063 " supported for --grow\n");
1064 exit(2);
1065 }
1066 /* FALL THROUGH */
1067 case O(GROW,'b'):
1068 case O(GROW,Bitmap):
1069 if (strcmp(optarg, "internal")== 0 ||
1070 strcmp(optarg, "none")== 0 ||
1071 strchr(optarg, '/') != NULL) {
1072 bitmap_file = optarg;
1073 continue;
1074 }
1075 /* probable typo */
1076 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1077 " not '%s'\n", optarg);
1078 exit(2);
1079
1080 case O(GROW,BitmapChunk):
1081 case O(BUILD,BitmapChunk):
1082 case O(CREATE,BitmapChunk): /* bitmap chunksize */
1083 bitmap_chunk = parse_size(optarg);
1084 if (bitmap_chunk <= 0 ||
1085 bitmap_chunk & (bitmap_chunk - 1)) {
1086 pr_err("invalid bitmap chunksize: %s\n",
1087 optarg);
1088 exit(2);
1089 }
1090 bitmap_chunk = bitmap_chunk * 512;
1091 continue;
1092
1093 case O(GROW, WriteBehind):
1094 case O(BUILD, WriteBehind):
1095 case O(CREATE, WriteBehind): /* write-behind mode */
1096 write_behind = DEFAULT_MAX_WRITE_BEHIND;
1097 if (optarg) {
1098 write_behind = strtol(optarg, &c, 10);
1099 if (write_behind < 0 || *c ||
1100 write_behind > 16383) {
1101 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
1102 exit(2);
1103 }
1104 }
1105 continue;
1106
1107 case O(INCREMENTAL, 'r'):
1108 case O(INCREMENTAL, RebuildMapOpt):
1109 rebuild_map = 1;
1110 continue;
1111 case O(INCREMENTAL, IncrementalPath):
1112 remove_path = optarg;
1113 continue;
1114 }
1115 /* We have now processed all the valid options. Anything else is
1116 * an error
1117 */
1118 if (option_index > 0)
1119 pr_err(":option --%s not valid in %s mode\n",
1120 long_options[option_index].name,
1121 map_num(modes, mode));
1122 else
1123 pr_err("option -%c not valid in %s mode\n",
1124 opt, map_num(modes, mode));
1125 exit(2);
1126
1127 }
1128
1129 if (print_help) {
1130 char *help_text;
1131 if (print_help == 2)
1132 help_text = OptionHelp;
1133 else
1134 help_text = mode_help[mode];
1135 if (help_text == NULL)
1136 help_text = Help;
1137 fputs(help_text,stdout);
1138 exit(0);
1139 }
1140
1141 if (!mode && devs_found) {
1142 mode = MISC;
1143 devmode = 'Q';
1144 if (devlist->disposition == 0)
1145 devlist->disposition = devmode;
1146 }
1147 if (!mode) {
1148 fputs(Usage, stderr);
1149 exit(2);
1150 }
1151
1152 if (symlinks) {
1153 struct createinfo *ci = conf_get_create_info();
1154
1155 if (strcasecmp(symlinks, "yes") == 0)
1156 ci->symlinks = 1;
1157 else if (strcasecmp(symlinks, "no") == 0)
1158 ci->symlinks = 0;
1159 else {
1160 pr_err("option --symlinks must be 'no' or 'yes'\n");
1161 exit(2);
1162 }
1163 }
1164 /* Ok, got the option parsing out of the way
1165 * hopefully it's mostly right but there might be some stuff
1166 * missing
1167 *
1168 * That is mosty checked in the per-mode stuff but...
1169 *
1170 * For @,B,C and A without -s, the first device listed must be
1171 * an md device. We check that here and open it.
1172 */
1173
1174 if (mode == MANAGE || mode == BUILD || mode == CREATE
1175 || mode == GROW
1176 || (mode == ASSEMBLE && ! scan)) {
1177 if (devs_found < 1) {
1178 pr_err("an md device must be given in this mode\n");
1179 exit(2);
1180 }
1181 if ((int)ident.super_minor == -2 && autof) {
1182 pr_err("--super-minor=dev is incompatible with --auto\n");
1183 exit(2);
1184 }
1185 if (mode == MANAGE || mode == GROW) {
1186 mdfd = open_mddev(devlist->devname, 1);
1187 if (mdfd < 0)
1188 exit(1);
1189 } else
1190 /* non-existent device is OK */
1191 mdfd = open_mddev(devlist->devname, 0);
1192 if (mdfd == -2) {
1193 pr_err("device %s exists but is not an "
1194 "md array.\n", devlist->devname);
1195 exit(1);
1196 }
1197 if ((int)ident.super_minor == -2) {
1198 struct stat stb;
1199 if (mdfd < 0) {
1200 pr_err("--super-minor=dev given, and "
1201 "listed device %s doesn't exist.\n",
1202 devlist->devname);
1203 exit(1);
1204 }
1205 fstat(mdfd, &stb);
1206 ident.super_minor = minor(stb.st_rdev);
1207 }
1208 if (mdfd >= 0 && mode != MANAGE && mode != GROW) {
1209 /* We don't really want this open yet, we just might
1210 * have wanted to check some things
1211 */
1212 close(mdfd);
1213 mdfd = -1;
1214 }
1215 }
1216
1217 if (raiddisks) {
1218 if (raiddisks == 1 && !force && level != LEVEL_FAULTY) {
1219 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1220 " a mistake. If you really mean it you will need to specify --force before\n"
1221 " setting the number of drives.\n");
1222 exit(2);
1223 }
1224 }
1225
1226 if (homehost == NULL)
1227 homehost = conf_get_homehost(&require_homehost);
1228 if (homehost == NULL || strcasecmp(homehost, "<system>")==0) {
1229 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
1230 sys_hostname[sizeof(sys_hostname)-1] = 0;
1231 homehost = sys_hostname;
1232 }
1233 }
1234 if (homehost && (!homehost[0] || strcasecmp(homehost, "<none>") == 0)) {
1235 homehost = NULL;
1236 require_homehost = 0;
1237 }
1238
1239 if ((mode == MISC && devmode == 'E')
1240 || (mode == MONITOR && spare_sharing == 0))
1241 /* Anyone may try this */;
1242 else if (geteuid() != 0) {
1243 pr_err("must be super-user to perform this action\n");
1244 exit(1);
1245 }
1246
1247 ident.autof = autof;
1248
1249 rv = 0;
1250 switch(mode) {
1251 case MANAGE:
1252 /* readonly, add/remove, readwrite, runstop */
1253 if (readonly>0)
1254 rv = Manage_ro(devlist->devname, mdfd, readonly);
1255 if (!rv && devs_found>1)
1256 rv = Manage_subdevs(devlist->devname, mdfd,
1257 devlist->next, verbose-quiet, test,
1258 update, force);
1259 if (!rv && readonly < 0)
1260 rv = Manage_ro(devlist->devname, mdfd, readonly);
1261 if (!rv && runstop)
1262 rv = Manage_runstop(devlist->devname, mdfd, runstop, quiet);
1263 break;
1264 case ASSEMBLE:
1265 if (devs_found == 1 && ident.uuid_set == 0 &&
1266 ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) {
1267 /* Only a device has been given, so get details from config file */
1268 struct mddev_ident *array_ident = conf_get_ident(devlist->devname);
1269 if (array_ident == NULL) {
1270 pr_err("%s not identified in config file.\n",
1271 devlist->devname);
1272 rv |= 1;
1273 if (mdfd >= 0)
1274 close(mdfd);
1275 } else {
1276 if (array_ident->autof == 0)
1277 array_ident->autof = autof;
1278 rv |= Assemble(ss, devlist->devname, array_ident,
1279 NULL, backup_file, invalid_backup,
1280 readonly, runstop, update,
1281 homehost, require_homehost,
1282 verbose-quiet, force,
1283 freeze_reshape);
1284 }
1285 } else if (!scan)
1286 rv = Assemble(ss, devlist->devname, &ident,
1287 devlist->next, backup_file, invalid_backup,
1288 readonly, runstop, update,
1289 homehost, require_homehost,
1290 verbose-quiet, force,
1291 freeze_reshape);
1292 else if (devs_found > 0) {
1293 if (update && devs_found > 1) {
1294 pr_err("can only update a single array at a time\n");
1295 exit(1);
1296 }
1297 if (backup_file && devs_found > 1) {
1298 pr_err("can only assemble a single array when providing a backup file.\n");
1299 exit(1);
1300 }
1301 for (dv = devlist ; dv ; dv=dv->next) {
1302 struct mddev_ident *array_ident = conf_get_ident(dv->devname);
1303 if (array_ident == NULL) {
1304 pr_err("%s not identified in config file.\n",
1305 dv->devname);
1306 rv |= 1;
1307 continue;
1308 }
1309 if (array_ident->autof == 0)
1310 array_ident->autof = autof;
1311 rv |= Assemble(ss, dv->devname, array_ident,
1312 NULL, backup_file, invalid_backup,
1313 readonly, runstop, update,
1314 homehost, require_homehost,
1315 verbose-quiet, force,
1316 freeze_reshape);
1317 }
1318 } else {
1319 if (update) {
1320 pr_err("--update not meaningful with a --scan assembly.\n");
1321 exit(1);
1322 }
1323 if (backup_file) {
1324 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1325 exit(1);
1326 }
1327 rv = scan_assemble(autof, ss, readonly, runstop,
1328 &ident, homehost,
1329 require_homehost,
1330 verbose - quiet,
1331 force, freeze_reshape);
1332 }
1333
1334 break;
1335 case BUILD:
1336 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1337 if (write_behind && !bitmap_file) {
1338 pr_err("write-behind mode requires a bitmap.\n");
1339 rv = 1;
1340 break;
1341 }
1342 if (raiddisks == 0) {
1343 pr_err("no raid-devices specified.\n");
1344 rv = 1;
1345 break;
1346 }
1347
1348 if (bitmap_file) {
1349 if (strcmp(bitmap_file, "internal")==0) {
1350 pr_err("'internal' bitmaps not supported with --build\n");
1351 rv |= 1;
1352 break;
1353 }
1354 }
1355 rv = Build(devlist->devname, chunk, level, layout,
1356 raiddisks, devlist->next, assume_clean,
1357 bitmap_file, bitmap_chunk, write_behind,
1358 delay, verbose-quiet, autof, size);
1359 break;
1360 case CREATE:
1361 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1362 if (write_behind && !bitmap_file) {
1363 pr_err("write-behind mode requires a bitmap.\n");
1364 rv = 1;
1365 break;
1366 }
1367 if (raiddisks == 0) {
1368 pr_err("no raid-devices specified.\n");
1369 rv = 1;
1370 break;
1371 }
1372
1373 rv = Create(ss, devlist->devname, chunk, level, layout, size<0 ? 0 : size,
1374 raiddisks, sparedisks, ident.name, homehost,
1375 ident.uuid_set ? ident.uuid : NULL,
1376 devs_found-1, devlist->next, runstop,
1377 readonly, verbose-quiet, force, assume_clean,
1378 bitmap_file, bitmap_chunk, write_behind, delay, autof);
1379 break;
1380 case MISC:
1381 if (devmode == 'E') {
1382 if (devlist == NULL && !scan) {
1383 pr_err("No devices to examine\n");
1384 exit(2);
1385 }
1386 if (devlist == NULL)
1387 devlist = conf_get_devs();
1388 if (devlist == NULL) {
1389 pr_err("No devices listed in %s\n", configfile?configfile:DefaultConfFile);
1390 exit(1);
1391 }
1392 if (brief && verbose)
1393 brief = 2;
1394 rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief,
1395 export, scan,
1396 SparcAdjust, ss, homehost);
1397 } else if (devmode == DetailPlatform) {
1398 rv = Detail_Platform(ss ? ss->ss : NULL, ss ? scan : 1, verbose);
1399 } else if (devlist == NULL) {
1400 if (devmode == 'S' && scan)
1401 rv = stop_scan(quiet);
1402 else if ((devmode == 'D' || devmode == Waitclean) && scan)
1403 rv = misc_scan(devmode, verbose, export,
1404 test, homehost, prefer);
1405 else if (devmode == UdevRules)
1406 rv = Write_rules(udev_filename);
1407 else {
1408 pr_err("No devices given.\n");
1409 exit(2);
1410 }
1411 } else
1412 rv = misc_list(devlist, brief, verbose, export, test,
1413 homehost, prefer, subarray, update,
1414 &ident,
1415 ss, force, quiet);
1416 break;
1417 case MONITOR:
1418 if (!devlist && !scan) {
1419 pr_err("Cannot monitor: need --scan or at least one device\n");
1420 rv = 1;
1421 break;
1422 }
1423 if (pidfile && !daemonise) {
1424 pr_err("Cannot write a pid file when not in daemon mode\n");
1425 rv = 1;
1426 break;
1427 }
1428 if (delay == 0) {
1429 if (get_linux_version() > 2006016)
1430 /* mdstat responds to poll */
1431 delay = 1000;
1432 else
1433 delay = 60;
1434 }
1435 rv= Monitor(devlist, mailaddr, program,
1436 delay?delay:60, daemonise, scan, oneshot,
1437 dosyslog, test, pidfile, increments,
1438 spare_sharing, prefer);
1439 break;
1440
1441 case GROW:
1442 if (array_size >= 0) {
1443 /* alway impose array size first, independent of
1444 * anything else
1445 * Do not allow level or raid_disks changes at the
1446 * same time as that can be irreversibly destructive.
1447 */
1448 struct mdinfo sra;
1449 int err;
1450 if (raiddisks || level != UnSet) {
1451 pr_err("cannot change array size in same operation "
1452 "as changing raiddisks or level.\n"
1453 " Change size first, then check that data is still intact.\n");
1454 rv = 1;
1455 break;
1456 }
1457 sysfs_init(&sra, mdfd, 0);
1458 if (array_size == 0)
1459 err = sysfs_set_str(&sra, NULL, "array_size", "default");
1460 else
1461 err = sysfs_set_num(&sra, NULL, "array_size", array_size / 2);
1462 if (err < 0) {
1463 if (errno == E2BIG)
1464 pr_err("--array-size setting"
1465 " is too large.\n");
1466 else
1467 pr_err("current kernel does"
1468 " not support setting --array-size\n");
1469 rv = 1;
1470 break;
1471 }
1472 }
1473 if (devs_found > 1 && raiddisks == 0) {
1474 /* must be '-a'. */
1475 if (size >= 0 || chunk || layout_str != NULL || bitmap_file) {
1476 pr_err("--add cannot be used with "
1477 "other geometry changes in --grow mode\n");
1478 rv = 1;
1479 break;
1480 }
1481 for (dv=devlist->next; dv ; dv=dv->next) {
1482 rv = Grow_Add_device(devlist->devname, mdfd,
1483 dv->devname);
1484 if (rv)
1485 break;
1486 }
1487 } else if (bitmap_file) {
1488 if (size >= 0 || raiddisks || chunk ||
1489 layout_str != NULL || devs_found > 1) {
1490 pr_err("--bitmap changes cannot be "
1491 "used with other geometry changes "
1492 "in --grow mode\n");
1493 rv = 1;
1494 break;
1495 }
1496 if (delay == 0)
1497 delay = DEFAULT_BITMAP_DELAY;
1498 rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file,
1499 bitmap_chunk, delay, write_behind, force);
1500 } else if (grow_continue)
1501 rv = Grow_continue_command(devlist->devname,
1502 mdfd, backup_file,
1503 verbose);
1504 else if (size >= 0 || raiddisks != 0 || layout_str != NULL
1505 || chunk != 0 || level != UnSet) {
1506 rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file,
1507 size, level, layout_str, chunk, raiddisks,
1508 devlist->next,
1509 assume_clean, force);
1510 } else if (array_size < 0)
1511 pr_err("no changes to --grow\n");
1512 break;
1513 case INCREMENTAL:
1514 if (rebuild_map) {
1515 RebuildMap();
1516 }
1517 if (scan) {
1518 if (runstop <= 0) {
1519 pr_err("--incremental --scan meaningless without --run.\n");
1520 break;
1521 }
1522 if (devmode == 'f') {
1523 pr_err("--incremental --scan --fail not supported.\n");
1524 break;
1525 }
1526 rv = IncrementalScan(verbose);
1527 }
1528 if (!devlist) {
1529 if (!rebuild_map && !scan) {
1530 pr_err("--incremental requires a device.\n");
1531 rv = 1;
1532 }
1533 break;
1534 }
1535 if (devlist->next) {
1536 pr_err("--incremental can only handle one device.\n");
1537 rv = 1;
1538 break;
1539 }
1540 if (devmode == 'f')
1541 rv = IncrementalRemove(devlist->devname, remove_path,
1542 verbose-quiet);
1543 else
1544 rv = Incremental(devlist->devname, verbose-quiet,
1545 runstop, ss, homehost,
1546 require_homehost, autof,
1547 freeze_reshape);
1548 break;
1549 case AUTODETECT:
1550 autodetect();
1551 break;
1552 }
1553 exit(rv);
1554 }
1555
1556 static int scan_assemble(int autof, struct supertype *ss,
1557 int readonly, int runstop,
1558 struct mddev_ident *ident,
1559 char *homehost, int require_homehost,
1560 int verbose, int force,
1561 int freeze_reshape)
1562 {
1563 struct mddev_ident *a, *array_list = conf_get_ident(NULL);
1564 struct mddev_dev *devlist = conf_get_devs();
1565 struct map_ent *map = NULL;
1566 int cnt = 0;
1567 int rv = 0;
1568 int failures, successes;
1569
1570 if (conf_verify_devnames(array_list)) {
1571 pr_err("Duplicate MD device names in "
1572 "conf file were found.\n");
1573 return 1;
1574 }
1575 if (devlist == NULL) {
1576 pr_err("No devices listed in conf file were found.\n");
1577 return 1;
1578 }
1579 for (a = array_list; a ; a = a->next) {
1580 a->assembled = 0;
1581 if (a->autof == 0)
1582 a->autof = autof;
1583 }
1584 if (map_lock(&map))
1585 pr_err("%s: failed to get "
1586 "exclusive lock on mapfile\n",
1587 __func__);
1588 do {
1589 failures = 0;
1590 successes = 0;
1591 rv = 0;
1592 for (a = array_list; a ; a = a->next) {
1593 int r;
1594 if (a->assembled)
1595 continue;
1596 if (a->devname &&
1597 strcasecmp(a->devname, "<ignore>") == 0)
1598 continue;
1599
1600 r = Assemble(ss, a->devname,
1601 a,
1602 NULL, NULL, 0,
1603 readonly, runstop, NULL,
1604 homehost, require_homehost,
1605 verbose, force,
1606 freeze_reshape);
1607 if (r == 0) {
1608 a->assembled = 1;
1609 successes++;
1610 } else
1611 failures++;
1612 rv |= r;
1613 cnt++;
1614 }
1615 } while (failures && successes);
1616 if (homehost && cnt == 0) {
1617 /* Maybe we can auto-assemble something.
1618 * Repeatedly call Assemble in auto-assemble mode
1619 * until it fails
1620 */
1621 int rv2;
1622 int acnt;
1623 ident->autof = autof;
1624 do {
1625 struct mddev_dev *devlist = conf_get_devs();
1626 acnt = 0;
1627 do {
1628 rv2 = Assemble(ss, NULL,
1629 ident,
1630 devlist, NULL, 0,
1631 readonly,
1632 runstop, NULL,
1633 homehost,
1634 require_homehost,
1635 verbose,
1636 force,
1637 freeze_reshape);
1638 if (rv2==0) {
1639 cnt++;
1640 acnt++;
1641 }
1642 } while (rv2!=2);
1643 /* Incase there are stacked devices, we need to go around again */
1644 } while (acnt);
1645 if (cnt == 0 && rv == 0) {
1646 pr_err("No arrays found in config file or automatically\n");
1647 rv = 1;
1648 } else if (cnt)
1649 rv = 0;
1650 } else if (cnt == 0 && rv == 0) {
1651 pr_err("No arrays found in config file\n");
1652 rv = 1;
1653 }
1654 map_unlock(&map);
1655 return rv;
1656 }
1657
1658 static int misc_scan(char devmode, int verbose, int export, int test,
1659 char *homehost, char *prefer)
1660 {
1661 /* apply --detail or --wait-clean to
1662 * all devices in /proc/mdstat
1663 */
1664 struct mdstat_ent *ms = mdstat_read(0, 1);
1665 struct mdstat_ent *e;
1666 struct map_ent *map = NULL;
1667 int members;
1668 int v = verbose>1?0:verbose+1;
1669 int rv = 0;
1670
1671 for (members = 0; members <= 1; members++) {
1672 for (e=ms ; e ; e=e->next) {
1673 char *name;
1674 struct map_ent *me;
1675 int member = e->metadata_version &&
1676 strncmp(e->metadata_version,
1677 "external:/", 10) == 0;
1678 if (members != member)
1679 continue;
1680 me = map_by_devnum(&map, e->devnum);
1681 if (me && me->path
1682 && strcmp(me->path, "/unknown") != 0)
1683 name = me->path;
1684 else
1685 name = get_md_name(e->devnum);
1686
1687 if (!name) {
1688 pr_err("cannot find device file for %s\n",
1689 e->dev);
1690 continue;
1691 }
1692 if (devmode == 'D')
1693 rv |= Detail(name, v,
1694 export, test,
1695 homehost, prefer);
1696 else
1697 rv |= WaitClean(name, -1, v);
1698 put_md_name(name);
1699 }
1700 }
1701 free_mdstat(ms);
1702 return rv;
1703 }
1704
1705 static int stop_scan(int quiet)
1706 {
1707 /* apply --stop to all devices in /proc/mdstat */
1708 /* Due to possible stacking of devices, repeat until
1709 * nothing more can be stopped
1710 */
1711 int progress=1, err;
1712 int last = 0;
1713 int rv = 0;
1714 do {
1715 struct mdstat_ent *ms = mdstat_read(0, 0);
1716 struct mdstat_ent *e;
1717
1718 if (!progress) last = 1;
1719 progress = 0; err = 0;
1720 for (e=ms ; e ; e=e->next) {
1721 char *name = get_md_name(e->devnum);
1722 int mdfd;
1723
1724 if (!name) {
1725 pr_err("cannot find device file for %s\n",
1726 e->dev);
1727 continue;
1728 }
1729 mdfd = open_mddev(name, 1);
1730 if (mdfd >= 0) {
1731 if (Manage_runstop(name, mdfd, -1, quiet?1:last?0:-1))
1732 err = 1;
1733 else
1734 progress = 1;
1735 close(mdfd);
1736 }
1737
1738 put_md_name(name);
1739 }
1740 free_mdstat(ms);
1741 } while (!last && err);
1742 if (err)
1743 rv |= 1;
1744 return rv;
1745 }
1746
1747 static int misc_list(struct mddev_dev *devlist,
1748 int brief, int verbose, int export, int test,
1749 char *homehost, char *prefer, char *subarray,
1750 char *update, struct mddev_ident *ident,
1751 struct supertype *ss, int force, int quiet)
1752 {
1753 struct mddev_dev *dv;
1754 int rv = 0;
1755
1756 for (dv=devlist ; dv; dv=dv->next) {
1757 int mdfd;
1758
1759 switch(dv->disposition) {
1760 case 'D':
1761 rv |= Detail(dv->devname,
1762 brief?1+verbose:0,
1763 export, test, homehost, prefer);
1764 continue;
1765 case KillOpt: /* Zero superblock */
1766 if (ss)
1767 rv |= Kill(dv->devname, ss, force, quiet,0);
1768 else {
1769 int q = quiet;
1770 do {
1771 rv |= Kill(dv->devname, NULL, force, q, 0);
1772 q = 1;
1773 } while (rv == 0);
1774 rv &= ~2;
1775 }
1776 continue;
1777 case 'Q':
1778 rv |= Query(dv->devname); continue;
1779 case 'X':
1780 rv |= ExamineBitmap(dv->devname, brief, ss); continue;
1781 case 'W':
1782 case WaitOpt:
1783 rv |= Wait(dv->devname); continue;
1784 case Waitclean:
1785 rv |= WaitClean(dv->devname, -1, verbose-quiet); continue;
1786 case KillSubarray:
1787 rv |= Kill_subarray(dv->devname, subarray, quiet);
1788 continue;
1789 case UpdateSubarray:
1790 if (update == NULL) {
1791 pr_err("-U/--update must be specified with --update-subarray\n");
1792 rv |= 1;
1793 continue;
1794 }
1795 rv |= Update_subarray(dv->devname, subarray,
1796 update, ident, quiet);
1797 continue;
1798 }
1799 mdfd = open_mddev(dv->devname, 1);
1800 if (mdfd>=0) {
1801 switch(dv->disposition) {
1802 case 'R':
1803 rv |= Manage_runstop(dv->devname, mdfd, 1, quiet); break;
1804 case 'S':
1805 rv |= Manage_runstop(dv->devname, mdfd, -1, quiet); break;
1806 case 'o':
1807 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1808 case 'w':
1809 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1810 }
1811 close(mdfd);
1812 } else
1813 rv |= 1;
1814 }
1815 return rv;
1816 }