2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2012 Neil Brown <neilb@suse.de>
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.
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.
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
22 * Email: <neilb@suse.de>
24 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
25 * Paul Clements, SteelEye Technology, Inc.
33 static int scan_assemble(struct supertype
*ss
,
35 struct mddev_ident
*ident
);
36 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
37 char *homehost
, char *prefer
);
38 static int stop_scan(int verbose
);
39 static int misc_list(struct mddev_dev
*devlist
,
40 int brief
, int verbose
, int export
, int test
,
41 char *homehost
, char *prefer
, char *subarray
,
42 char *update
, struct mddev_ident
*ident
,
43 struct supertype
*ss
, int force
);
46 int main(int argc
, char *argv
[])
56 long long array_size
= -1;
59 char *layout_str
= NULL
;
62 struct mddev_ident ident
;
63 char *configfile
= NULL
;
67 char *bitmap_file
= NULL
;
68 int bitmap_chunk
= UnSet
;
69 struct mddev_dev
*devlist
= NULL
;
70 struct mddev_dev
**devlistend
= & devlist
;
74 char *symlinks
= NULL
;
75 int grow_continue
= 0;
76 /* autof indicates whether and how to create device node.
77 * bottom 3 bits are style. Rest (when shifted) are number of parts
79 * 1 - don't create (no)
80 * 2 - if is_standard, then create (yes)
81 * 3 - create as 'md' - reject is_standard mdp (md)
82 * 4 - create as 'mdp' - reject is_standard md (mdp)
83 * 5 - default to md if not is_standard (md in config file)
84 * 6 - default to mdp if not is_standard (part, or mdp in config file)
87 .require_homehost
= 1,
90 char sys_hostname
[256];
91 char *mailaddr
= NULL
;
97 int spare_sharing
= 1;
98 struct supertype
*ss
= NULL
;
100 char *shortopt
= short_options
;
103 char *remove_path
= NULL
;
104 char *udev_filename
= NULL
;
111 srandom(time(0) ^ getpid());
115 ident
.raid_disks
= UnSet
;
116 ident
.super_minor
= UnSet
;
118 ident
.spare_group
= NULL
;
121 ident
.bitmap_fd
= -1;
122 ident
.bitmap_file
= NULL
;
124 ident
.container
= NULL
;
127 while ((option_index
= -1) ,
128 (opt
=getopt_long(argc
, argv
,
129 shortopt
, long_options
,
130 &option_index
)) != -1) {
132 /* firstly, some mode-independent options */
142 fputs(Version
, stderr
);
145 case 'v': c
.verbose
++;
148 case 'q': c
.verbose
--;
152 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
153 || mode
== GROW
|| mode
== INCREMENTAL
155 break; /* b means bitmap */
160 case 'Y': c
.export
++;
164 if (strcasecmp(optarg
, "<ignore>") == 0)
165 c
.require_homehost
= 0;
171 * --offroot sets first char of argv[0] to @. This is used
172 * by systemd to signal that the task was launched from
173 * initrd/initramfs and should be preserved during shutdown
183 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
189 fputs(Usage
, stderr
);
192 /* second, figure out the mode.
193 * Some options force the mode. Others
194 * set the mode if it isn't already
200 shortopt
= short_bitmap_options
;
208 case ReAdd
: /* re-add */
211 shortopt
= short_bitmap_options
;
215 case 'A': newmode
= ASSEMBLE
;
216 shortopt
= short_bitmap_auto_options
;
218 case 'B': newmode
= BUILD
;
219 shortopt
= short_bitmap_auto_options
;
221 case 'C': newmode
= CREATE
;
222 shortopt
= short_bitmap_auto_options
;
224 case 'F': newmode
= MONITOR
;
226 case 'G': newmode
= GROW
;
227 shortopt
= short_bitmap_options
;
229 case 'I': newmode
= INCREMENTAL
;
230 shortopt
= short_bitmap_auto_options
;
233 newmode
= AUTODETECT
;
264 if (mode
&& newmode
== mode
) {
265 /* everybody happy ! */
266 } else if (mode
&& newmode
!= mode
) {
269 if (option_index
>= 0)
270 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
272 fprintf(stderr
, "-%c", opt
);
273 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
274 map_num(modes
, newmode
),
275 map_num(modes
, mode
));
277 } else if (!mode
&& newmode
) {
279 if (mode
== MISC
&& devs_found
) {
280 pr_err("No action given for %s in --misc mode\n",
282 fprintf(stderr
," Action options must come before device names\n");
286 /* special case of -c --help */
287 if ((opt
== 'c' || opt
== ConfigFile
) &&
288 ( strncmp(optarg
, "--h", 3)==0 ||
289 strncmp(optarg
, "-h", 2)==0)) {
290 fputs(Help_config
, stdout
);
294 /* If first option is a device, don't force the mode yet */
296 if (devs_found
== 0) {
297 dv
= xmalloc(sizeof(*dv
));
298 dv
->devname
= optarg
;
299 dv
->disposition
= devmode
;
300 dv
->writemostly
= writemostly
;
304 devlistend
= &dv
->next
;
309 /* No mode yet, and this is the second device ... */
310 pr_err("An option must be given to set the mode before a second device\n"
311 " (%s) is listed\n", optarg
);
314 if (option_index
>= 0)
315 pr_err("--%s", long_options
[option_index
].name
);
318 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
322 /* if we just set the mode, then done */
336 /* an undecorated option - must be a device name.
338 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
339 pr_err("Must give one of -a/-r/-f"
340 " for subsequent devices at %s\n", optarg
);
343 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
344 pr_err("Must give -a/--add for"
345 " devices to add: %s\n", optarg
);
348 dv
= xmalloc(sizeof(*dv
));
349 dv
->devname
= optarg
;
350 dv
->disposition
= devmode
;
351 dv
->writemostly
= writemostly
;
355 devlistend
= &dv
->next
;
361 /* We've got a mode, and opt is now something else which
362 * could depend on the mode */
363 #define O(a,b) ((a<<16)|b)
364 switch (O(mode
,opt
)) {
366 case O(GROW
,ChunkSize
):
368 case O(CREATE
,ChunkSize
):
369 case O(BUILD
,'c'): /* chunk or rounding */
370 case O(BUILD
,ChunkSize
): /* chunk or rounding */
372 pr_err("chunk/rounding may only be specified once. "
373 "Second value is %s.\n", optarg
);
376 chunk
= parse_size(optarg
);
377 if (chunk
< 8 || (chunk
&1)) {
378 pr_err("invalid chunk/rounding value: %s\n",
382 /* Convert sectors to K */
386 case O(INCREMENTAL
, 'e'):
388 case O(ASSEMBLE
,'e'):
389 case O(MISC
,'e'): /* set metadata (superblock) information */
391 pr_err("metadata information already given\n");
394 for(i
=0; !ss
&& superlist
[i
]; i
++)
395 ss
= superlist
[i
]->match_metadata_desc(optarg
);
398 pr_err("unrecognised metadata identifier: %s\n", optarg
);
404 case O(MANAGE
,WriteMostly
):
406 case O(BUILD
,WriteMostly
):
408 case O(CREATE
,WriteMostly
):
409 /* set write-mostly for following devices */
414 /* clear write-mostly for following devices */
421 case O(BUILD
,'z'): /* size */
423 pr_err("size may only be specified once. "
424 "Second value is %s.\n", optarg
);
427 if (strcmp(optarg
, "max")==0)
430 size
= parse_size(optarg
);
432 pr_err("invalid size: %s\n",
436 /* convert sectors to K */
441 case O(GROW
,'Z'): /* array size */
442 if (array_size
>= 0) {
443 pr_err("array-size may only be specified once. "
444 "Second value is %s.\n", optarg
);
447 if (strcmp(optarg
, "max") == 0)
450 array_size
= parse_size(optarg
);
451 if (array_size
<= 0) {
452 pr_err("invalid array size: %s\n",
461 case O(BUILD
,'l'): /* set raid level*/
462 if (level
!= UnSet
) {
463 pr_err("raid level may only be set once. "
464 "Second value is %s.\n", optarg
);
467 level
= map_name(pers
, optarg
);
468 if (level
== UnSet
) {
469 pr_err("invalid raid level: %s\n",
473 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
474 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
477 pr_err("Raid level %s not permitted with --build.\n",
481 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
482 pr_err("raid level %s is incompatible with spare-devices setting.\n",
489 case O(GROW
, 'p'): /* new layout */
490 case O(GROW
, Layout
):
492 pr_err("layout may only be sent once. "
493 "Second value was %s\n", optarg
);
497 /* 'Grow' will parse the value */
500 case O(CREATE
,'p'): /* raid5 layout */
501 case O(CREATE
,Layout
):
502 case O(BUILD
,'p'): /* faulty layout */
503 case O(BUILD
,Layout
):
504 if (layout
!= UnSet
) {
505 pr_err("layout may only be sent once. "
506 "Second value was %s\n", optarg
);
511 pr_err("layout not meaningful for %s arrays.\n",
512 map_num(pers
, level
));
515 pr_err("raid level must be given before layout.\n");
519 layout
= map_name(r5layout
, optarg
);
521 pr_err("layout %s not understood for raid5.\n",
527 layout
= map_name(r6layout
, optarg
);
529 pr_err("layout %s not understood for raid6.\n",
536 layout
= parse_layout_10(optarg
);
538 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
546 layout
= parse_layout_faulty(optarg
);
548 pr_err("layout %s not understood for faulty.\n",
556 case O(CREATE
,AssumeClean
):
557 case O(BUILD
,AssumeClean
): /* assume clean */
558 case O(GROW
,AssumeClean
):
564 case O(BUILD
,'n'): /* number of raid disks */
566 pr_err("raid-devices set twice: %d and %s\n",
570 raiddisks
= parse_num(optarg
);
571 if (raiddisks
<= 0) {
572 pr_err("invalid number of raid devices: %s\n",
576 ident
.raid_disks
= raiddisks
;
579 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
581 pr_err("spare-devices set twice: %d and %s\n",
585 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
586 pr_err("spare-devices setting is incompatible with raid level %d\n",
590 sparedisks
= parse_num(optarg
);
591 if (sparedisks
< 0) {
592 pr_err("invalid number of spare-devices: %s\n",
602 case O(INCREMENTAL
,'a'):
603 case O(INCREMENTAL
,Auto
):
604 case O(ASSEMBLE
,'a'):
605 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
606 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
609 case O(CREATE
,Symlinks
):
610 case O(BUILD
,Symlinks
):
611 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
615 case O(BUILD
,'f'): /* force honouring '-n 1' */
616 case O(BUILD
,Force
): /* force honouring '-n 1' */
617 case O(GROW
,'f'): /* ditto */
618 case O(GROW
,Force
): /* ditto */
619 case O(CREATE
,'f'): /* force honouring of device list */
620 case O(CREATE
,Force
): /* force honouring of device list */
621 case O(ASSEMBLE
,'f'): /* force assembly */
622 case O(ASSEMBLE
,Force
): /* force assembly */
623 case O(MISC
,'f'): /* force zero */
624 case O(MISC
,Force
): /* force zero */
625 case O(MANAGE
,Force
): /* add device which is too large */
628 /* now for the Assemble options */
629 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
631 case O(INCREMENTAL
, FreezeReshape
):
632 c
.freeze_reshape
= 1;
634 case O(CREATE
,'u'): /* uuid of array */
635 case O(ASSEMBLE
,'u'): /* uuid of array */
636 if (ident
.uuid_set
) {
637 pr_err("uuid cannot be set twice. "
638 "Second value %s.\n", optarg
);
641 if (parse_uuid(optarg
, ident
.uuid
))
644 pr_err("Bad uuid: %s\n", optarg
);
650 case O(ASSEMBLE
,'N'):
653 pr_err("name cannot be set twice. "
654 "Second value %s.\n", optarg
);
657 if (mode
== MISC
&& !c
.subarray
) {
658 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
661 if (strlen(optarg
) > 32) {
662 pr_err("name '%s' is too long, 32 chars max.\n",
666 strcpy(ident
.name
, optarg
);
669 case O(ASSEMBLE
,'m'): /* super-minor for array */
670 case O(ASSEMBLE
,SuperMinor
):
671 if (ident
.super_minor
!= UnSet
) {
672 pr_err("super-minor cannot be set twice. "
673 "Second value: %s.\n", optarg
);
676 if (strcmp(optarg
, "dev")==0)
677 ident
.super_minor
= -2;
679 ident
.super_minor
= parse_num(optarg
);
680 if (ident
.super_minor
< 0) {
681 pr_err("Bad super-minor number: %s.\n", optarg
);
687 case O(ASSEMBLE
,'o'):
693 case O(ASSEMBLE
,'U'): /* update the superblock */
696 pr_err("Can only update one aspect"
697 " of superblock, both %s and %s given.\n",
701 if (mode
== MISC
&& !c
.subarray
) {
702 pr_err("Only subarrays can be"
703 " updated in misc mode\n");
707 if (strcmp(c
.update
, "sparc2.2")==0)
709 if (strcmp(c
.update
, "super-minor") == 0)
711 if (strcmp(c
.update
, "summaries")==0)
713 if (strcmp(c
.update
, "resync")==0)
715 if (strcmp(c
.update
, "uuid")==0)
717 if (strcmp(c
.update
, "name")==0)
719 if (strcmp(c
.update
, "homehost")==0)
721 if (strcmp(c
.update
, "devicesize")==0)
723 if (strcmp(c
.update
, "no-bitmap")==0)
725 if (strcmp(c
.update
, "byteorder")==0) {
727 pr_err("must not set metadata"
728 " type with --update=byteorder.\n");
731 for(i
=0; !ss
&& superlist
[i
]; i
++)
732 ss
= superlist
[i
]->match_metadata_desc(
735 pr_err("INTERNAL ERROR"
736 " cannot find 0.swap\n");
742 if (strcmp(c
.update
,"?") == 0 ||
743 strcmp(c
.update
, "help") == 0) {
745 fprintf(outf
, Name
": ");
749 Name
": '--update=%s' is invalid. ",
752 fprintf(outf
, "Valid --update options are:\n"
753 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
754 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
756 exit(outf
== stdout
? 0 : 2);
759 /* update=devicesize is allowed with --re-add */
760 if (devmode
!= 'A') {
761 pr_err("--update in Manage mode only"
762 " allowed with --re-add.\n");
766 pr_err("Can only update one aspect"
767 " of superblock, both %s and %s given.\n",
772 if (strcmp(c
.update
, "devicesize") != 0) {
773 pr_err("only 'devicesize' can be"
774 " updated with --re-add\n");
779 case O(INCREMENTAL
,NoDegraded
):
780 pr_err("--no-degraded is deprecated in Incremental mode\n");
781 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
782 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
783 * so we overload slightly */
786 case O(ASSEMBLE
,'c'):
787 case O(ASSEMBLE
,ConfigFile
):
788 case O(INCREMENTAL
, 'c'):
789 case O(INCREMENTAL
, ConfigFile
):
791 case O(MISC
, ConfigFile
):
793 case O(MONITOR
,ConfigFile
):
795 pr_err("configfile cannot be set twice. "
796 "Second value is %s.\n", optarg
);
800 set_conffile(configfile
);
801 /* FIXME possibly check that config file exists. Even parse it */
803 case O(ASSEMBLE
,'s'): /* scan */
806 case O(INCREMENTAL
,'s'):
810 case O(MONITOR
,'m'): /* mail address */
811 case O(MONITOR
,EMail
):
813 pr_err("only specify one mailaddress. %s ignored.\n",
819 case O(MONITOR
,'p'): /* alert program */
820 case O(MONITOR
,ProgramOpt
): /* alert program */
822 pr_err("only specify one alter program. %s ignored.\n",
828 case O(MONITOR
,'r'): /* rebuild increments */
829 case O(MONITOR
,Increment
):
830 increments
= atoi(optarg
);
831 if (increments
> 99 || increments
< 1) {
832 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
837 case O(MONITOR
,'d'): /* delay in seconds */
839 case O(BUILD
,'d'): /* delay for bitmap updates */
842 pr_err("only specify delay once. %s ignored.\n",
845 c
.delay
= parse_num(optarg
);
847 pr_err("invalid delay: %s\n",
853 case O(MONITOR
,'f'): /* daemonise */
854 case O(MONITOR
,Fork
):
857 case O(MONITOR
,'i'): /* pid */
859 pr_err("only specify one pid file. %s ignored.\n",
864 case O(MONITOR
,'1'): /* oneshot */
868 case O(MONITOR
,'t'): /* test */
871 case O(MONITOR
,'y'): /* log messages to syslog */
872 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
875 case O(MONITOR
, NoSharing
):
879 /* now the general management options. Some are applicable
880 * to other modes. None have arguments.
885 case O(MANAGE
,Add
): /* add a drive */
888 case O(MANAGE
,ReAdd
):
891 case O(MANAGE
,'r'): /* remove a drive */
892 case O(MANAGE
,Remove
):
895 case O(MANAGE
,'f'): /* set faulty */
897 case O(INCREMENTAL
,'f'):
898 case O(INCREMENTAL
,Remove
):
899 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
900 * even though we will both fail and
901 * remove the device */
904 case O(INCREMENTAL
,'R'):
906 case O(ASSEMBLE
,'R'):
908 case O(CREATE
,'R'): /* Run the array */
910 pr_err("Cannot both Stop and Run an array\n");
917 pr_err("Cannot both Run and Stop an array\n");
929 case O(MISC
,KillOpt
):
936 case O(MISC
, WaitOpt
):
937 case O(MISC
, Waitclean
):
938 case O(MISC
, DetailPlatform
):
939 case O(MISC
, KillSubarray
):
940 case O(MISC
, UpdateSubarray
):
941 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
943 pr_err("subarray can only"
944 " be specified once\n");
949 if (devmode
&& devmode
!= opt
&&
950 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
951 pr_err("--examine/-E cannot be given with ");
952 if (devmode
== 'E') {
953 if (option_index
>= 0)
954 fprintf(stderr
, "--%s\n",
955 long_options
[option_index
].name
);
957 fprintf(stderr
, "-%c\n", opt
);
958 } else if (isalpha(devmode
))
959 fprintf(stderr
, "-%c\n", devmode
);
961 fprintf(stderr
, "previous option\n");
966 case O(MISC
, UdevRules
):
967 if (devmode
&& devmode
!= opt
) {
968 pr_err("--udev-rules must"
969 " be the only option.\n");
972 pr_err("only specify one udev "
973 "rule filename. %s ignored.\n",
976 udev_filename
= optarg
;
984 case O(MISC
, Sparc22
):
985 if (devmode
!= 'E') {
986 pr_err("--sparc2.2 only allowed with --examine\n");
992 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
993 case O(ASSEMBLE
,Bitmap
):
995 pr_err("bitmap file needed with -b in --assemble mode\n");
998 if (strcmp(optarg
, "internal")==0) {
999 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1002 bitmap_fd
= open(optarg
, O_RDWR
);
1003 if (!*optarg
|| bitmap_fd
< 0) {
1004 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1007 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1010 case O(ASSEMBLE
, BackupFile
):
1011 case O(GROW
, BackupFile
):
1012 /* Specify a file into which grow might place a backup,
1013 * or from which assemble might recover a backup
1015 if (c
.backup_file
) {
1016 pr_err("backup file already specified, rejecting %s\n", optarg
);
1019 c
.backup_file
= optarg
;
1022 case O(GROW
, Continue
):
1023 /* Continue interrupted grow
1027 case O(ASSEMBLE
, InvalidBackup
):
1028 /* Acknowledge that the backupfile is invalid, but ask
1029 * to continue anyway
1031 c
.invalid_backup
= 1;
1035 case O(BUILD
,Bitmap
):
1037 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1038 if (strcmp(optarg
, "none") == 0) {
1039 pr_err("'--bitmap none' only"
1040 " supported for --grow\n");
1045 case O(GROW
,Bitmap
):
1046 if (strcmp(optarg
, "internal")== 0 ||
1047 strcmp(optarg
, "none")== 0 ||
1048 strchr(optarg
, '/') != NULL
) {
1049 bitmap_file
= optarg
;
1053 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1054 " not '%s'\n", optarg
);
1057 case O(GROW
,BitmapChunk
):
1058 case O(BUILD
,BitmapChunk
):
1059 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1060 bitmap_chunk
= parse_size(optarg
);
1061 if (bitmap_chunk
<= 0 ||
1062 bitmap_chunk
& (bitmap_chunk
- 1)) {
1063 pr_err("invalid bitmap chunksize: %s\n",
1067 bitmap_chunk
= bitmap_chunk
* 512;
1070 case O(GROW
, WriteBehind
):
1071 case O(BUILD
, WriteBehind
):
1072 case O(CREATE
, WriteBehind
): /* write-behind mode */
1073 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1075 write_behind
= parse_num(optarg
);
1076 if (write_behind
< 0 ||
1077 write_behind
> 16383) {
1078 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1084 case O(INCREMENTAL
, 'r'):
1085 case O(INCREMENTAL
, RebuildMapOpt
):
1088 case O(INCREMENTAL
, IncrementalPath
):
1089 remove_path
= optarg
;
1092 /* We have now processed all the valid options. Anything else is
1095 if (option_index
> 0)
1096 pr_err(":option --%s not valid in %s mode\n",
1097 long_options
[option_index
].name
,
1098 map_num(modes
, mode
));
1100 pr_err("option -%c not valid in %s mode\n",
1101 opt
, map_num(modes
, mode
));
1108 if (print_help
== 2)
1109 help_text
= OptionHelp
;
1111 help_text
= mode_help
[mode
];
1112 if (help_text
== NULL
)
1114 fputs(help_text
,stdout
);
1118 if (!mode
&& devs_found
) {
1121 if (devlist
->disposition
== 0)
1122 devlist
->disposition
= devmode
;
1125 fputs(Usage
, stderr
);
1130 struct createinfo
*ci
= conf_get_create_info();
1132 if (strcasecmp(symlinks
, "yes") == 0)
1134 else if (strcasecmp(symlinks
, "no") == 0)
1137 pr_err("option --symlinks must be 'no' or 'yes'\n");
1141 /* Ok, got the option parsing out of the way
1142 * hopefully it's mostly right but there might be some stuff
1145 * That is mosty checked in the per-mode stuff but...
1147 * For @,B,C and A without -s, the first device listed must be
1148 * an md device. We check that here and open it.
1151 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1153 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1154 if (devs_found
< 1) {
1155 pr_err("an md device must be given in this mode\n");
1158 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1159 pr_err("--super-minor=dev is incompatible with --auto\n");
1162 if (mode
== MANAGE
|| mode
== GROW
) {
1163 mdfd
= open_mddev(devlist
->devname
, 1);
1167 /* non-existent device is OK */
1168 mdfd
= open_mddev(devlist
->devname
, 0);
1170 pr_err("device %s exists but is not an "
1171 "md array.\n", devlist
->devname
);
1174 if ((int)ident
.super_minor
== -2) {
1177 pr_err("--super-minor=dev given, and "
1178 "listed device %s doesn't exist.\n",
1183 ident
.super_minor
= minor(stb
.st_rdev
);
1185 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1186 /* We don't really want this open yet, we just might
1187 * have wanted to check some things
1195 if (raiddisks
== 1 && !c
.force
&& level
!= LEVEL_FAULTY
) {
1196 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1197 " a mistake. If you really mean it you will need to specify --force before\n"
1198 " setting the number of drives.\n");
1203 if (c
.homehost
== NULL
)
1204 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1205 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1206 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1207 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1208 c
.homehost
= sys_hostname
;
1211 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1213 c
.require_homehost
= 0;
1216 if ((mode
== MISC
&& devmode
== 'E')
1217 || (mode
== MONITOR
&& spare_sharing
== 0))
1218 /* Anyone may try this */;
1219 else if (geteuid() != 0) {
1220 pr_err("must be super-user to perform this action\n");
1224 ident
.autof
= c
.autof
;
1229 /* readonly, add/remove, readwrite, runstop */
1231 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1232 if (!rv
&& devs_found
>1)
1233 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1234 devlist
->next
, c
.verbose
, c
.test
,
1236 if (!rv
&& c
.readonly
< 0)
1237 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1238 if (!rv
&& c
.runstop
)
1239 rv
= Manage_runstop(devlist
->devname
, mdfd
, c
.runstop
, c
.verbose
, 0);
1242 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1243 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1244 /* Only a device has been given, so get details from config file */
1245 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1246 if (array_ident
== NULL
) {
1247 pr_err("%s not identified in config file.\n",
1253 if (array_ident
->autof
== 0)
1254 array_ident
->autof
= c
.autof
;
1255 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1259 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1261 else if (devs_found
> 0) {
1262 if (c
.update
&& devs_found
> 1) {
1263 pr_err("can only update a single array at a time\n");
1266 if (c
.backup_file
&& devs_found
> 1) {
1267 pr_err("can only assemble a single array when providing a backup file.\n");
1270 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1271 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1272 if (array_ident
== NULL
) {
1273 pr_err("%s not identified in config file.\n",
1278 if (array_ident
->autof
== 0)
1279 array_ident
->autof
= c
.autof
;
1280 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1285 pr_err("--update not meaningful with a --scan assembly.\n");
1288 if (c
.backup_file
) {
1289 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1292 rv
= scan_assemble(ss
, &c
, &ident
);
1298 c
.delay
= DEFAULT_BITMAP_DELAY
;
1299 if (write_behind
&& !bitmap_file
) {
1300 pr_err("write-behind mode requires a bitmap.\n");
1304 if (raiddisks
== 0) {
1305 pr_err("no raid-devices specified.\n");
1311 if (strcmp(bitmap_file
, "internal")==0) {
1312 pr_err("'internal' bitmaps not supported with --build\n");
1317 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1318 raiddisks
, devlist
->next
, assume_clean
,
1319 bitmap_file
, bitmap_chunk
, write_behind
,
1320 c
.delay
, c
.verbose
, c
.autof
, size
);
1324 c
.delay
= DEFAULT_BITMAP_DELAY
;
1325 if (write_behind
&& !bitmap_file
) {
1326 pr_err("write-behind mode requires a bitmap.\n");
1330 if (raiddisks
== 0) {
1331 pr_err("no raid-devices specified.\n");
1336 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1337 raiddisks
, sparedisks
, ident
.name
, c
.homehost
,
1338 ident
.uuid_set
? ident
.uuid
: NULL
,
1339 devs_found
-1, devlist
->next
, c
.runstop
,
1340 c
.readonly
, c
.verbose
, c
.force
, assume_clean
,
1341 bitmap_file
, bitmap_chunk
, write_behind
, c
.delay
, c
.autof
);
1344 if (devmode
== 'E') {
1345 if (devlist
== NULL
&& !c
.scan
) {
1346 pr_err("No devices to examine\n");
1349 if (devlist
== NULL
)
1350 devlist
= conf_get_devs();
1351 if (devlist
== NULL
) {
1352 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1355 if (c
.brief
&& c
.verbose
> 0)
1357 rv
= Examine(devlist
, c
.scan
?(c
.verbose
>1?0:c
.verbose
):c
.brief
,
1359 c
.SparcAdjust
, ss
, c
.homehost
);
1360 } else if (devmode
== DetailPlatform
) {
1361 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1, c
.verbose
);
1362 } else if (devlist
== NULL
) {
1363 if (devmode
== 'S' && c
.scan
)
1364 rv
= stop_scan(c
.verbose
);
1365 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1366 rv
= misc_scan(devmode
, c
.verbose
, c
.export
,
1367 c
.test
, c
.homehost
, c
.prefer
);
1368 else if (devmode
== UdevRules
)
1369 rv
= Write_rules(udev_filename
);
1371 pr_err("No devices given.\n");
1375 rv
= misc_list(devlist
, c
.brief
, c
.verbose
, c
.export
, c
.test
,
1376 c
.homehost
, c
.prefer
, c
.subarray
, c
.update
,
1381 if (!devlist
&& !c
.scan
) {
1382 pr_err("Cannot monitor: need --scan or at least one device\n");
1386 if (pidfile
&& !daemonise
) {
1387 pr_err("Cannot write a pid file when not in daemon mode\n");
1392 if (get_linux_version() > 2006016)
1393 /* mdstat responds to poll */
1398 rv
= Monitor(devlist
, mailaddr
, program
,
1399 c
.delay
?c
.delay
:60, daemonise
, c
.scan
, oneshot
,
1400 dosyslog
, c
.test
, pidfile
, increments
,
1401 spare_sharing
, c
.prefer
);
1405 if (array_size
>= 0) {
1406 /* alway impose array size first, independent of
1408 * Do not allow level or raid_disks changes at the
1409 * same time as that can be irreversibly destructive.
1413 if (raiddisks
|| level
!= UnSet
) {
1414 pr_err("cannot change array size in same operation "
1415 "as changing raiddisks or level.\n"
1416 " Change size first, then check that data is still intact.\n");
1420 sysfs_init(&sra
, mdfd
, 0);
1421 if (array_size
== 0)
1422 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1424 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1427 pr_err("--array-size setting"
1428 " is too large.\n");
1430 pr_err("current kernel does"
1431 " not support setting --array-size\n");
1436 if (devs_found
> 1 && raiddisks
== 0) {
1438 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1439 pr_err("--add cannot be used with "
1440 "other geometry changes in --grow mode\n");
1444 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1445 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1450 } else if (bitmap_file
) {
1451 if (size
>= 0 || raiddisks
|| chunk
||
1452 layout_str
!= NULL
|| devs_found
> 1) {
1453 pr_err("--bitmap changes cannot be "
1454 "used with other geometry changes "
1455 "in --grow mode\n");
1460 c
.delay
= DEFAULT_BITMAP_DELAY
;
1461 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1462 bitmap_chunk
, c
.delay
, write_behind
, c
.force
);
1463 } else if (grow_continue
)
1464 rv
= Grow_continue_command(devlist
->devname
,
1465 mdfd
, c
.backup_file
,
1467 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1468 || chunk
!= 0 || level
!= UnSet
) {
1469 rv
= Grow_reshape(devlist
->devname
, mdfd
, c
.verbose
, c
.backup_file
,
1470 size
, level
, layout_str
, chunk
, raiddisks
,
1472 assume_clean
, c
.force
);
1473 } else if (array_size
< 0)
1474 pr_err("no changes to --grow\n");
1481 if (c
.runstop
<= 0) {
1482 pr_err("--incremental --scan meaningless without --run.\n");
1485 if (devmode
== 'f') {
1486 pr_err("--incremental --scan --fail not supported.\n");
1489 rv
= IncrementalScan(c
.verbose
);
1492 if (!rebuild_map
&& !c
.scan
) {
1493 pr_err("--incremental requires a device.\n");
1498 if (devlist
->next
) {
1499 pr_err("--incremental can only handle one device.\n");
1504 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1507 rv
= Incremental(devlist
->devname
, c
.verbose
,
1508 c
.runstop
, ss
, c
.homehost
,
1509 c
.require_homehost
, c
.autof
,
1519 static int scan_assemble(struct supertype
*ss
,
1521 struct mddev_ident
*ident
)
1523 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1524 struct mddev_dev
*devlist
= conf_get_devs();
1525 struct map_ent
*map
= NULL
;
1528 int failures
, successes
;
1530 if (conf_verify_devnames(array_list
)) {
1531 pr_err("Duplicate MD device names in "
1532 "conf file were found.\n");
1535 if (devlist
== NULL
) {
1536 pr_err("No devices listed in conf file were found.\n");
1539 for (a
= array_list
; a
; a
= a
->next
) {
1542 a
->autof
= c
->autof
;
1545 pr_err("%s: failed to get "
1546 "exclusive lock on mapfile\n",
1552 for (a
= array_list
; a
; a
= a
->next
) {
1557 strcasecmp(a
->devname
, "<ignore>") == 0)
1560 r
= Assemble(ss
, a
->devname
,
1570 } while (failures
&& successes
);
1571 if (c
->homehost
&& cnt
== 0) {
1572 /* Maybe we can auto-assemble something.
1573 * Repeatedly call Assemble in auto-assemble mode
1578 ident
->autof
= c
->autof
;
1580 struct mddev_dev
*devlist
= conf_get_devs();
1583 rv2
= Assemble(ss
, NULL
,
1591 /* Incase there are stacked devices, we need to go around again */
1593 if (cnt
== 0 && rv
== 0) {
1594 pr_err("No arrays found in config file or automatically\n");
1598 } else if (cnt
== 0 && rv
== 0) {
1599 pr_err("No arrays found in config file\n");
1606 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
1607 char *homehost
, char *prefer
)
1609 /* apply --detail or --wait-clean to
1610 * all devices in /proc/mdstat
1612 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1613 struct mdstat_ent
*e
;
1614 struct map_ent
*map
= NULL
;
1616 int v
= verbose
>1?0:verbose
+1;
1619 for (members
= 0; members
<= 1; members
++) {
1620 for (e
=ms
; e
; e
=e
->next
) {
1623 int member
= e
->metadata_version
&&
1624 strncmp(e
->metadata_version
,
1625 "external:/", 10) == 0;
1626 if (members
!= member
)
1628 me
= map_by_devnum(&map
, e
->devnum
);
1630 && strcmp(me
->path
, "/unknown") != 0)
1633 name
= get_md_name(e
->devnum
);
1636 pr_err("cannot find device file for %s\n",
1641 rv
|= Detail(name
, v
,
1645 rv
|= WaitClean(name
, -1, v
);
1653 static int stop_scan(int verbose
)
1655 /* apply --stop to all devices in /proc/mdstat */
1656 /* Due to possible stacking of devices, repeat until
1657 * nothing more can be stopped
1659 int progress
=1, err
;
1663 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1664 struct mdstat_ent
*e
;
1666 if (!progress
) last
= 1;
1667 progress
= 0; err
= 0;
1668 for (e
=ms
; e
; e
=e
->next
) {
1669 char *name
= get_md_name(e
->devnum
);
1673 pr_err("cannot find device file for %s\n",
1677 mdfd
= open_mddev(name
, 1);
1679 if (Manage_runstop(name
, mdfd
, -1, verbose
, !last
))
1689 } while (!last
&& err
);
1695 static int misc_list(struct mddev_dev
*devlist
,
1696 int brief
, int verbose
, int export
, int test
,
1697 char *homehost
, char *prefer
, char *subarray
,
1698 char *update
, struct mddev_ident
*ident
,
1699 struct supertype
*ss
, int force
)
1701 struct mddev_dev
*dv
;
1704 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1707 switch(dv
->disposition
) {
1709 rv
|= Detail(dv
->devname
,
1711 export
, test
, homehost
, prefer
);
1713 case KillOpt
: /* Zero superblock */
1715 rv
|= Kill(dv
->devname
, ss
, force
, verbose
,0);
1719 rv
|= Kill(dv
->devname
, NULL
, force
, v
, 0);
1726 rv
|= Query(dv
->devname
); continue;
1728 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1731 rv
|= Wait(dv
->devname
); continue;
1733 rv
|= WaitClean(dv
->devname
, -1, verbose
); continue;
1735 rv
|= Kill_subarray(dv
->devname
, subarray
, verbose
);
1737 case UpdateSubarray
:
1738 if (update
== NULL
) {
1739 pr_err("-U/--update must be specified with --update-subarray\n");
1743 rv
|= Update_subarray(dv
->devname
, subarray
,
1744 update
, ident
, verbose
);
1747 mdfd
= open_mddev(dv
->devname
, 1);
1749 switch(dv
->disposition
) {
1751 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, verbose
, 0); break;
1753 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, verbose
, 0); break;
1755 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1757 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;