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
, struct context
*c
);
37 static int stop_scan(int verbose
);
38 static int misc_list(struct mddev_dev
*devlist
,
39 struct mddev_ident
*ident
,
40 struct supertype
*ss
, struct context
*c
);
43 int main(int argc
, char *argv
[])
52 unsigned long long size
= 0;
53 long long array_size
= -1;
56 char *layout_str
= NULL
;
59 struct mddev_ident ident
;
60 char *configfile
= NULL
;
64 char *bitmap_file
= NULL
;
65 int bitmap_chunk
= UnSet
;
66 struct mddev_dev
*devlist
= NULL
;
67 struct mddev_dev
**devlistend
= & devlist
;
71 char *symlinks
= NULL
;
72 int grow_continue
= 0;
73 /* autof indicates whether and how to create device node.
74 * bottom 3 bits are style. Rest (when shifted) are number of parts
76 * 1 - don't create (no)
77 * 2 - if is_standard, then create (yes)
78 * 3 - create as 'md' - reject is_standard mdp (md)
79 * 4 - create as 'mdp' - reject is_standard md (mdp)
80 * 5 - default to md if not is_standard (md in config file)
81 * 6 - default to mdp if not is_standard (part, or mdp in config file)
84 .require_homehost
= 1,
87 char sys_hostname
[256];
88 char *mailaddr
= NULL
;
94 int spare_sharing
= 1;
95 struct supertype
*ss
= NULL
;
97 char *shortopt
= short_options
;
100 char *remove_path
= NULL
;
101 char *udev_filename
= NULL
;
108 srandom(time(0) ^ getpid());
112 ident
.raid_disks
= UnSet
;
113 ident
.super_minor
= UnSet
;
115 ident
.spare_group
= NULL
;
118 ident
.bitmap_fd
= -1;
119 ident
.bitmap_file
= NULL
;
121 ident
.container
= NULL
;
124 while ((option_index
= -1) ,
125 (opt
=getopt_long(argc
, argv
,
126 shortopt
, long_options
,
127 &option_index
)) != -1) {
129 /* firstly, some mode-independent options */
139 fputs(Version
, stderr
);
142 case 'v': c
.verbose
++;
145 case 'q': c
.verbose
--;
149 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
150 || mode
== GROW
|| mode
== INCREMENTAL
152 break; /* b means bitmap */
157 case 'Y': c
.export
++;
161 if (strcasecmp(optarg
, "<ignore>") == 0)
162 c
.require_homehost
= 0;
168 * --offroot sets first char of argv[0] to @. This is used
169 * by systemd to signal that the task was launched from
170 * initrd/initramfs and should be preserved during shutdown
180 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
186 fputs(Usage
, stderr
);
189 /* second, figure out the mode.
190 * Some options force the mode. Others
191 * set the mode if it isn't already
197 shortopt
= short_bitmap_options
;
205 case ReAdd
: /* re-add */
208 shortopt
= short_bitmap_options
;
212 case 'A': newmode
= ASSEMBLE
;
213 shortopt
= short_bitmap_auto_options
;
215 case 'B': newmode
= BUILD
;
216 shortopt
= short_bitmap_auto_options
;
218 case 'C': newmode
= CREATE
;
219 shortopt
= short_bitmap_auto_options
;
221 case 'F': newmode
= MONITOR
;
223 case 'G': newmode
= GROW
;
224 shortopt
= short_bitmap_options
;
226 case 'I': newmode
= INCREMENTAL
;
227 shortopt
= short_bitmap_auto_options
;
230 newmode
= AUTODETECT
;
261 if (mode
&& newmode
== mode
) {
262 /* everybody happy ! */
263 } else if (mode
&& newmode
!= mode
) {
266 if (option_index
>= 0)
267 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
269 fprintf(stderr
, "-%c", opt
);
270 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
271 map_num(modes
, newmode
),
272 map_num(modes
, mode
));
274 } else if (!mode
&& newmode
) {
276 if (mode
== MISC
&& devs_found
) {
277 pr_err("No action given for %s in --misc mode\n",
279 fprintf(stderr
," Action options must come before device names\n");
283 /* special case of -c --help */
284 if ((opt
== 'c' || opt
== ConfigFile
) &&
285 ( strncmp(optarg
, "--h", 3)==0 ||
286 strncmp(optarg
, "-h", 2)==0)) {
287 fputs(Help_config
, stdout
);
291 /* If first option is a device, don't force the mode yet */
293 if (devs_found
== 0) {
294 dv
= xmalloc(sizeof(*dv
));
295 dv
->devname
= optarg
;
296 dv
->disposition
= devmode
;
297 dv
->writemostly
= writemostly
;
301 devlistend
= &dv
->next
;
306 /* No mode yet, and this is the second device ... */
307 pr_err("An option must be given to set the mode before a second device\n"
308 " (%s) is listed\n", optarg
);
311 if (option_index
>= 0)
312 pr_err("--%s", long_options
[option_index
].name
);
315 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
319 /* if we just set the mode, then done */
333 /* an undecorated option - must be a device name.
335 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
336 pr_err("Must give one of -a/-r/-f"
337 " for subsequent devices at %s\n", optarg
);
340 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
341 pr_err("Must give -a/--add for"
342 " devices to add: %s\n", optarg
);
345 dv
= xmalloc(sizeof(*dv
));
346 dv
->devname
= optarg
;
347 dv
->disposition
= devmode
;
348 dv
->writemostly
= writemostly
;
352 devlistend
= &dv
->next
;
358 /* We've got a mode, and opt is now something else which
359 * could depend on the mode */
360 #define O(a,b) ((a<<16)|b)
361 switch (O(mode
,opt
)) {
363 case O(GROW
,ChunkSize
):
365 case O(CREATE
,ChunkSize
):
366 case O(BUILD
,'c'): /* chunk or rounding */
367 case O(BUILD
,ChunkSize
): /* chunk or rounding */
369 pr_err("chunk/rounding may only be specified once. "
370 "Second value is %s.\n", optarg
);
373 chunk
= parse_size(optarg
);
374 if (chunk
< 8 || (chunk
&1)) {
375 pr_err("invalid chunk/rounding value: %s\n",
379 /* Convert sectors to K */
383 case O(INCREMENTAL
, 'e'):
385 case O(ASSEMBLE
,'e'):
386 case O(MISC
,'e'): /* set metadata (superblock) information */
388 pr_err("metadata information already given\n");
391 for(i
=0; !ss
&& superlist
[i
]; i
++)
392 ss
= superlist
[i
]->match_metadata_desc(optarg
);
395 pr_err("unrecognised metadata identifier: %s\n", optarg
);
401 case O(MANAGE
,WriteMostly
):
403 case O(BUILD
,WriteMostly
):
405 case O(CREATE
,WriteMostly
):
406 /* set write-mostly for following devices */
411 /* clear write-mostly for following devices */
418 case O(BUILD
,'z'): /* size */
420 pr_err("size may only be specified once. "
421 "Second value is %s.\n", optarg
);
424 if (strcmp(optarg
, "max")==0)
427 size
= parse_size(optarg
);
429 pr_err("invalid size: %s\n",
433 /* convert sectors to K */
438 case O(GROW
,'Z'): /* array size */
439 if (array_size
>= 0) {
440 pr_err("array-size may only be specified once. "
441 "Second value is %s.\n", optarg
);
444 if (strcmp(optarg
, "max") == 0)
447 array_size
= parse_size(optarg
);
448 if (array_size
<= 0) {
449 pr_err("invalid array size: %s\n",
458 case O(BUILD
,'l'): /* set raid level*/
459 if (level
!= UnSet
) {
460 pr_err("raid level may only be set once. "
461 "Second value is %s.\n", optarg
);
464 level
= map_name(pers
, optarg
);
465 if (level
== UnSet
) {
466 pr_err("invalid raid level: %s\n",
470 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
471 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
474 pr_err("Raid level %s not permitted with --build.\n",
478 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
479 pr_err("raid level %s is incompatible with spare-devices setting.\n",
486 case O(GROW
, 'p'): /* new layout */
487 case O(GROW
, Layout
):
489 pr_err("layout may only be sent once. "
490 "Second value was %s\n", optarg
);
494 /* 'Grow' will parse the value */
497 case O(CREATE
,'p'): /* raid5 layout */
498 case O(CREATE
,Layout
):
499 case O(BUILD
,'p'): /* faulty layout */
500 case O(BUILD
,Layout
):
501 if (layout
!= UnSet
) {
502 pr_err("layout may only be sent once. "
503 "Second value was %s\n", optarg
);
508 pr_err("layout not meaningful for %s arrays.\n",
509 map_num(pers
, level
));
512 pr_err("raid level must be given before layout.\n");
516 layout
= map_name(r5layout
, optarg
);
518 pr_err("layout %s not understood for raid5.\n",
524 layout
= map_name(r6layout
, optarg
);
526 pr_err("layout %s not understood for raid6.\n",
533 layout
= parse_layout_10(optarg
);
535 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
543 layout
= parse_layout_faulty(optarg
);
545 pr_err("layout %s not understood for faulty.\n",
553 case O(CREATE
,AssumeClean
):
554 case O(BUILD
,AssumeClean
): /* assume clean */
555 case O(GROW
,AssumeClean
):
561 case O(BUILD
,'n'): /* number of raid disks */
563 pr_err("raid-devices set twice: %d and %s\n",
567 raiddisks
= parse_num(optarg
);
568 if (raiddisks
<= 0) {
569 pr_err("invalid number of raid devices: %s\n",
573 ident
.raid_disks
= raiddisks
;
576 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
578 pr_err("spare-devices set twice: %d and %s\n",
582 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
583 pr_err("spare-devices setting is incompatible with raid level %d\n",
587 sparedisks
= parse_num(optarg
);
588 if (sparedisks
< 0) {
589 pr_err("invalid number of spare-devices: %s\n",
599 case O(INCREMENTAL
,'a'):
600 case O(INCREMENTAL
,Auto
):
601 case O(ASSEMBLE
,'a'):
602 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
603 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
606 case O(CREATE
,Symlinks
):
607 case O(BUILD
,Symlinks
):
608 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
612 case O(BUILD
,'f'): /* force honouring '-n 1' */
613 case O(BUILD
,Force
): /* force honouring '-n 1' */
614 case O(GROW
,'f'): /* ditto */
615 case O(GROW
,Force
): /* ditto */
616 case O(CREATE
,'f'): /* force honouring of device list */
617 case O(CREATE
,Force
): /* force honouring of device list */
618 case O(ASSEMBLE
,'f'): /* force assembly */
619 case O(ASSEMBLE
,Force
): /* force assembly */
620 case O(MISC
,'f'): /* force zero */
621 case O(MISC
,Force
): /* force zero */
622 case O(MANAGE
,Force
): /* add device which is too large */
625 /* now for the Assemble options */
626 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
628 case O(INCREMENTAL
, FreezeReshape
):
629 c
.freeze_reshape
= 1;
631 case O(CREATE
,'u'): /* uuid of array */
632 case O(ASSEMBLE
,'u'): /* uuid of array */
633 if (ident
.uuid_set
) {
634 pr_err("uuid cannot be set twice. "
635 "Second value %s.\n", optarg
);
638 if (parse_uuid(optarg
, ident
.uuid
))
641 pr_err("Bad uuid: %s\n", optarg
);
647 case O(ASSEMBLE
,'N'):
650 pr_err("name cannot be set twice. "
651 "Second value %s.\n", optarg
);
654 if (mode
== MISC
&& !c
.subarray
) {
655 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
658 if (strlen(optarg
) > 32) {
659 pr_err("name '%s' is too long, 32 chars max.\n",
663 strcpy(ident
.name
, optarg
);
666 case O(ASSEMBLE
,'m'): /* super-minor for array */
667 case O(ASSEMBLE
,SuperMinor
):
668 if (ident
.super_minor
!= UnSet
) {
669 pr_err("super-minor cannot be set twice. "
670 "Second value: %s.\n", optarg
);
673 if (strcmp(optarg
, "dev")==0)
674 ident
.super_minor
= -2;
676 ident
.super_minor
= parse_num(optarg
);
677 if (ident
.super_minor
< 0) {
678 pr_err("Bad super-minor number: %s.\n", optarg
);
684 case O(ASSEMBLE
,'o'):
690 case O(ASSEMBLE
,'U'): /* update the superblock */
693 pr_err("Can only update one aspect"
694 " of superblock, both %s and %s given.\n",
698 if (mode
== MISC
&& !c
.subarray
) {
699 pr_err("Only subarrays can be"
700 " updated in misc mode\n");
704 if (strcmp(c
.update
, "sparc2.2")==0)
706 if (strcmp(c
.update
, "super-minor") == 0)
708 if (strcmp(c
.update
, "summaries")==0)
710 if (strcmp(c
.update
, "resync")==0)
712 if (strcmp(c
.update
, "uuid")==0)
714 if (strcmp(c
.update
, "name")==0)
716 if (strcmp(c
.update
, "homehost")==0)
718 if (strcmp(c
.update
, "devicesize")==0)
720 if (strcmp(c
.update
, "no-bitmap")==0)
722 if (strcmp(c
.update
, "byteorder")==0) {
724 pr_err("must not set metadata"
725 " type with --update=byteorder.\n");
728 for(i
=0; !ss
&& superlist
[i
]; i
++)
729 ss
= superlist
[i
]->match_metadata_desc(
732 pr_err("INTERNAL ERROR"
733 " cannot find 0.swap\n");
739 if (strcmp(c
.update
,"?") == 0 ||
740 strcmp(c
.update
, "help") == 0) {
742 fprintf(outf
, Name
": ");
746 Name
": '--update=%s' is invalid. ",
749 fprintf(outf
, "Valid --update options are:\n"
750 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
751 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
753 exit(outf
== stdout
? 0 : 2);
756 /* update=devicesize is allowed with --re-add */
757 if (devmode
!= 'A') {
758 pr_err("--update in Manage mode only"
759 " allowed with --re-add.\n");
763 pr_err("Can only update one aspect"
764 " of superblock, both %s and %s given.\n",
769 if (strcmp(c
.update
, "devicesize") != 0) {
770 pr_err("only 'devicesize' can be"
771 " updated with --re-add\n");
776 case O(INCREMENTAL
,NoDegraded
):
777 pr_err("--no-degraded is deprecated in Incremental mode\n");
778 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
779 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
780 * so we overload slightly */
783 case O(ASSEMBLE
,'c'):
784 case O(ASSEMBLE
,ConfigFile
):
785 case O(INCREMENTAL
, 'c'):
786 case O(INCREMENTAL
, ConfigFile
):
788 case O(MISC
, ConfigFile
):
790 case O(MONITOR
,ConfigFile
):
792 pr_err("configfile cannot be set twice. "
793 "Second value is %s.\n", optarg
);
797 set_conffile(configfile
);
798 /* FIXME possibly check that config file exists. Even parse it */
800 case O(ASSEMBLE
,'s'): /* scan */
803 case O(INCREMENTAL
,'s'):
807 case O(MONITOR
,'m'): /* mail address */
808 case O(MONITOR
,EMail
):
810 pr_err("only specify one mailaddress. %s ignored.\n",
816 case O(MONITOR
,'p'): /* alert program */
817 case O(MONITOR
,ProgramOpt
): /* alert program */
819 pr_err("only specify one alter program. %s ignored.\n",
825 case O(MONITOR
,'r'): /* rebuild increments */
826 case O(MONITOR
,Increment
):
827 increments
= atoi(optarg
);
828 if (increments
> 99 || increments
< 1) {
829 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
834 case O(MONITOR
,'d'): /* delay in seconds */
836 case O(BUILD
,'d'): /* delay for bitmap updates */
839 pr_err("only specify delay once. %s ignored.\n",
842 c
.delay
= parse_num(optarg
);
844 pr_err("invalid delay: %s\n",
850 case O(MONITOR
,'f'): /* daemonise */
851 case O(MONITOR
,Fork
):
854 case O(MONITOR
,'i'): /* pid */
856 pr_err("only specify one pid file. %s ignored.\n",
861 case O(MONITOR
,'1'): /* oneshot */
865 case O(MONITOR
,'t'): /* test */
868 case O(MONITOR
,'y'): /* log messages to syslog */
869 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
872 case O(MONITOR
, NoSharing
):
876 /* now the general management options. Some are applicable
877 * to other modes. None have arguments.
882 case O(MANAGE
,Add
): /* add a drive */
885 case O(MANAGE
,ReAdd
):
888 case O(MANAGE
,'r'): /* remove a drive */
889 case O(MANAGE
,Remove
):
892 case O(MANAGE
,'f'): /* set faulty */
894 case O(INCREMENTAL
,'f'):
895 case O(INCREMENTAL
,Remove
):
896 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
897 * even though we will both fail and
898 * remove the device */
901 case O(INCREMENTAL
,'R'):
903 case O(ASSEMBLE
,'R'):
905 case O(CREATE
,'R'): /* Run the array */
907 pr_err("Cannot both Stop and Run an array\n");
914 pr_err("Cannot both Run and Stop an array\n");
926 case O(MISC
,KillOpt
):
933 case O(MISC
, WaitOpt
):
934 case O(MISC
, Waitclean
):
935 case O(MISC
, DetailPlatform
):
936 case O(MISC
, KillSubarray
):
937 case O(MISC
, UpdateSubarray
):
938 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
940 pr_err("subarray can only"
941 " be specified once\n");
946 if (devmode
&& devmode
!= opt
&&
947 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
948 pr_err("--examine/-E cannot be given with ");
949 if (devmode
== 'E') {
950 if (option_index
>= 0)
951 fprintf(stderr
, "--%s\n",
952 long_options
[option_index
].name
);
954 fprintf(stderr
, "-%c\n", opt
);
955 } else if (isalpha(devmode
))
956 fprintf(stderr
, "-%c\n", devmode
);
958 fprintf(stderr
, "previous option\n");
963 case O(MISC
, UdevRules
):
964 if (devmode
&& devmode
!= opt
) {
965 pr_err("--udev-rules must"
966 " be the only option.\n");
969 pr_err("only specify one udev "
970 "rule filename. %s ignored.\n",
973 udev_filename
= optarg
;
981 case O(MISC
, Sparc22
):
982 if (devmode
!= 'E') {
983 pr_err("--sparc2.2 only allowed with --examine\n");
989 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
990 case O(ASSEMBLE
,Bitmap
):
992 pr_err("bitmap file needed with -b in --assemble mode\n");
995 if (strcmp(optarg
, "internal")==0) {
996 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
999 bitmap_fd
= open(optarg
, O_RDWR
);
1000 if (!*optarg
|| bitmap_fd
< 0) {
1001 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1004 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1007 case O(ASSEMBLE
, BackupFile
):
1008 case O(GROW
, BackupFile
):
1009 /* Specify a file into which grow might place a backup,
1010 * or from which assemble might recover a backup
1012 if (c
.backup_file
) {
1013 pr_err("backup file already specified, rejecting %s\n", optarg
);
1016 c
.backup_file
= optarg
;
1019 case O(GROW
, Continue
):
1020 /* Continue interrupted grow
1024 case O(ASSEMBLE
, InvalidBackup
):
1025 /* Acknowledge that the backupfile is invalid, but ask
1026 * to continue anyway
1028 c
.invalid_backup
= 1;
1032 case O(BUILD
,Bitmap
):
1034 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1035 if (strcmp(optarg
, "none") == 0) {
1036 pr_err("'--bitmap none' only"
1037 " supported for --grow\n");
1042 case O(GROW
,Bitmap
):
1043 if (strcmp(optarg
, "internal")== 0 ||
1044 strcmp(optarg
, "none")== 0 ||
1045 strchr(optarg
, '/') != NULL
) {
1046 bitmap_file
= optarg
;
1050 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1051 " not '%s'\n", optarg
);
1054 case O(GROW
,BitmapChunk
):
1055 case O(BUILD
,BitmapChunk
):
1056 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1057 bitmap_chunk
= parse_size(optarg
);
1058 if (bitmap_chunk
<= 0 ||
1059 bitmap_chunk
& (bitmap_chunk
- 1)) {
1060 pr_err("invalid bitmap chunksize: %s\n",
1064 bitmap_chunk
= bitmap_chunk
* 512;
1067 case O(GROW
, WriteBehind
):
1068 case O(BUILD
, WriteBehind
):
1069 case O(CREATE
, WriteBehind
): /* write-behind mode */
1070 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1072 write_behind
= parse_num(optarg
);
1073 if (write_behind
< 0 ||
1074 write_behind
> 16383) {
1075 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1081 case O(INCREMENTAL
, 'r'):
1082 case O(INCREMENTAL
, RebuildMapOpt
):
1085 case O(INCREMENTAL
, IncrementalPath
):
1086 remove_path
= optarg
;
1089 /* We have now processed all the valid options. Anything else is
1092 if (option_index
> 0)
1093 pr_err(":option --%s not valid in %s mode\n",
1094 long_options
[option_index
].name
,
1095 map_num(modes
, mode
));
1097 pr_err("option -%c not valid in %s mode\n",
1098 opt
, map_num(modes
, mode
));
1105 if (print_help
== 2)
1106 help_text
= OptionHelp
;
1108 help_text
= mode_help
[mode
];
1109 if (help_text
== NULL
)
1111 fputs(help_text
,stdout
);
1115 if (!mode
&& devs_found
) {
1118 if (devlist
->disposition
== 0)
1119 devlist
->disposition
= devmode
;
1122 fputs(Usage
, stderr
);
1127 struct createinfo
*ci
= conf_get_create_info();
1129 if (strcasecmp(symlinks
, "yes") == 0)
1131 else if (strcasecmp(symlinks
, "no") == 0)
1134 pr_err("option --symlinks must be 'no' or 'yes'\n");
1138 /* Ok, got the option parsing out of the way
1139 * hopefully it's mostly right but there might be some stuff
1142 * That is mosty checked in the per-mode stuff but...
1144 * For @,B,C and A without -s, the first device listed must be
1145 * an md device. We check that here and open it.
1148 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1150 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1151 if (devs_found
< 1) {
1152 pr_err("an md device must be given in this mode\n");
1155 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1156 pr_err("--super-minor=dev is incompatible with --auto\n");
1159 if (mode
== MANAGE
|| mode
== GROW
) {
1160 mdfd
= open_mddev(devlist
->devname
, 1);
1164 /* non-existent device is OK */
1165 mdfd
= open_mddev(devlist
->devname
, 0);
1167 pr_err("device %s exists but is not an "
1168 "md array.\n", devlist
->devname
);
1171 if ((int)ident
.super_minor
== -2) {
1174 pr_err("--super-minor=dev given, and "
1175 "listed device %s doesn't exist.\n",
1180 ident
.super_minor
= minor(stb
.st_rdev
);
1182 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1183 /* We don't really want this open yet, we just might
1184 * have wanted to check some things
1192 if (raiddisks
== 1 && !c
.force
&& level
!= LEVEL_FAULTY
) {
1193 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1194 " a mistake. If you really mean it you will need to specify --force before\n"
1195 " setting the number of drives.\n");
1200 if (c
.homehost
== NULL
)
1201 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1202 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1203 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1204 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1205 c
.homehost
= sys_hostname
;
1208 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1210 c
.require_homehost
= 0;
1213 if ((mode
== MISC
&& devmode
== 'E')
1214 || (mode
== MONITOR
&& spare_sharing
== 0))
1215 /* Anyone may try this */;
1216 else if (geteuid() != 0) {
1217 pr_err("must be super-user to perform this action\n");
1221 ident
.autof
= c
.autof
;
1223 if (c
.scan
&& c
.verbose
< 2)
1224 /* --scan implied --brief unless -vv */
1230 /* readonly, add/remove, readwrite, runstop */
1232 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1233 if (!rv
&& devs_found
>1)
1234 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1235 devlist
->next
, c
.verbose
, c
.test
,
1237 if (!rv
&& c
.readonly
< 0)
1238 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1239 if (!rv
&& c
.runstop
)
1240 rv
= Manage_runstop(devlist
->devname
, mdfd
, c
.runstop
, c
.verbose
, 0);
1243 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1244 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1245 /* Only a device has been given, so get details from config file */
1246 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1247 if (array_ident
== NULL
) {
1248 pr_err("%s not identified in config file.\n",
1254 if (array_ident
->autof
== 0)
1255 array_ident
->autof
= c
.autof
;
1256 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1260 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1262 else if (devs_found
> 0) {
1263 if (c
.update
&& devs_found
> 1) {
1264 pr_err("can only update a single array at a time\n");
1267 if (c
.backup_file
&& devs_found
> 1) {
1268 pr_err("can only assemble a single array when providing a backup file.\n");
1271 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1272 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1273 if (array_ident
== NULL
) {
1274 pr_err("%s not identified in config file.\n",
1279 if (array_ident
->autof
== 0)
1280 array_ident
->autof
= c
.autof
;
1281 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1286 pr_err("--update not meaningful with a --scan assembly.\n");
1289 if (c
.backup_file
) {
1290 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1293 rv
= scan_assemble(ss
, &c
, &ident
);
1299 c
.delay
= DEFAULT_BITMAP_DELAY
;
1300 if (write_behind
&& !bitmap_file
) {
1301 pr_err("write-behind mode requires a bitmap.\n");
1305 if (raiddisks
== 0) {
1306 pr_err("no raid-devices specified.\n");
1312 if (strcmp(bitmap_file
, "internal")==0) {
1313 pr_err("'internal' bitmaps not supported with --build\n");
1318 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1319 raiddisks
, devlist
->next
, assume_clean
,
1320 bitmap_file
, bitmap_chunk
, write_behind
,
1325 c
.delay
= DEFAULT_BITMAP_DELAY
;
1326 if (write_behind
&& !bitmap_file
) {
1327 pr_err("write-behind mode requires a bitmap.\n");
1331 if (raiddisks
== 0) {
1332 pr_err("no raid-devices specified.\n");
1337 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
,
1338 raiddisks
, sparedisks
, ident
.name
,
1339 ident
.uuid_set
? ident
.uuid
: NULL
,
1340 devs_found
-1, devlist
->next
,
1342 bitmap_file
, bitmap_chunk
, write_behind
, &c
);
1345 if (devmode
== 'E') {
1346 if (devlist
== NULL
&& !c
.scan
) {
1347 pr_err("No devices to examine\n");
1350 if (devlist
== NULL
)
1351 devlist
= conf_get_devs();
1352 if (devlist
== NULL
) {
1353 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1356 rv
= Examine(devlist
, &c
, ss
);
1357 } else if (devmode
== DetailPlatform
) {
1358 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1, c
.verbose
);
1359 } else if (devlist
== NULL
) {
1360 if (devmode
== 'S' && c
.scan
)
1361 rv
= stop_scan(c
.verbose
);
1362 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1363 rv
= misc_scan(devmode
, &c
);
1364 else if (devmode
== UdevRules
)
1365 rv
= Write_rules(udev_filename
);
1367 pr_err("No devices given.\n");
1371 rv
= misc_list(devlist
, &ident
, ss
, &c
);
1374 if (!devlist
&& !c
.scan
) {
1375 pr_err("Cannot monitor: need --scan or at least one device\n");
1379 if (pidfile
&& !daemonise
) {
1380 pr_err("Cannot write a pid file when not in daemon mode\n");
1385 if (get_linux_version() > 2006016)
1386 /* mdstat responds to poll */
1393 rv
= Monitor(devlist
, mailaddr
, program
,
1394 &c
, daemonise
, oneshot
,
1395 dosyslog
, pidfile
, increments
,
1400 if (array_size
>= 0) {
1401 /* alway impose array size first, independent of
1403 * Do not allow level or raid_disks changes at the
1404 * same time as that can be irreversibly destructive.
1408 if (raiddisks
|| level
!= UnSet
) {
1409 pr_err("cannot change array size in same operation "
1410 "as changing raiddisks or level.\n"
1411 " Change size first, then check that data is still intact.\n");
1415 sysfs_init(&sra
, mdfd
, 0);
1416 if (array_size
== 0)
1417 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1419 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1422 pr_err("--array-size setting"
1423 " is too large.\n");
1425 pr_err("current kernel does"
1426 " not support setting --array-size\n");
1431 if (devs_found
> 1 && raiddisks
== 0) {
1433 if (size
> 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1434 pr_err("--add cannot be used with "
1435 "other geometry changes in --grow mode\n");
1439 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1440 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1445 } else if (bitmap_file
) {
1446 if (size
> 0 || raiddisks
|| chunk
||
1447 layout_str
!= NULL
|| devs_found
> 1) {
1448 pr_err("--bitmap changes cannot be "
1449 "used with other geometry changes "
1450 "in --grow mode\n");
1455 c
.delay
= DEFAULT_BITMAP_DELAY
;
1456 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1457 bitmap_chunk
, c
.delay
, write_behind
, c
.force
);
1458 } else if (grow_continue
)
1459 rv
= Grow_continue_command(devlist
->devname
,
1460 mdfd
, c
.backup_file
,
1462 else if (size
> 0 || raiddisks
!= 0 || layout_str
!= NULL
1463 || chunk
!= 0 || level
!= UnSet
) {
1464 rv
= Grow_reshape(devlist
->devname
, mdfd
, c
.verbose
, c
.backup_file
,
1465 size
, level
, layout_str
, chunk
, raiddisks
,
1467 assume_clean
, c
.force
);
1468 } else if (array_size
< 0)
1469 pr_err("no changes to --grow\n");
1476 if (c
.runstop
<= 0) {
1477 pr_err("--incremental --scan meaningless without --run.\n");
1480 if (devmode
== 'f') {
1481 pr_err("--incremental --scan --fail not supported.\n");
1484 rv
= IncrementalScan(c
.verbose
);
1487 if (!rebuild_map
&& !c
.scan
) {
1488 pr_err("--incremental requires a device.\n");
1493 if (devlist
->next
) {
1494 pr_err("--incremental can only handle one device.\n");
1499 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1502 rv
= Incremental(devlist
->devname
, &c
, ss
);
1511 static int scan_assemble(struct supertype
*ss
,
1513 struct mddev_ident
*ident
)
1515 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1516 struct mddev_dev
*devlist
= conf_get_devs();
1517 struct map_ent
*map
= NULL
;
1520 int failures
, successes
;
1522 if (conf_verify_devnames(array_list
)) {
1523 pr_err("Duplicate MD device names in "
1524 "conf file were found.\n");
1527 if (devlist
== NULL
) {
1528 pr_err("No devices listed in conf file were found.\n");
1531 for (a
= array_list
; a
; a
= a
->next
) {
1534 a
->autof
= c
->autof
;
1537 pr_err("%s: failed to get "
1538 "exclusive lock on mapfile\n",
1544 for (a
= array_list
; a
; a
= a
->next
) {
1549 strcasecmp(a
->devname
, "<ignore>") == 0)
1552 r
= Assemble(ss
, a
->devname
,
1562 } while (failures
&& successes
);
1563 if (c
->homehost
&& cnt
== 0) {
1564 /* Maybe we can auto-assemble something.
1565 * Repeatedly call Assemble in auto-assemble mode
1570 ident
->autof
= c
->autof
;
1572 struct mddev_dev
*devlist
= conf_get_devs();
1575 rv2
= Assemble(ss
, NULL
,
1583 /* Incase there are stacked devices, we need to go around again */
1585 if (cnt
== 0 && rv
== 0) {
1586 pr_err("No arrays found in config file or automatically\n");
1590 } else if (cnt
== 0 && rv
== 0) {
1591 pr_err("No arrays found in config file\n");
1598 static int misc_scan(char devmode
, struct context
*c
)
1600 /* apply --detail or --wait-clean to
1601 * all devices in /proc/mdstat
1603 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1604 struct mdstat_ent
*e
;
1605 struct map_ent
*map
= NULL
;
1609 for (members
= 0; members
<= 1; members
++) {
1610 for (e
=ms
; e
; e
=e
->next
) {
1613 int member
= e
->metadata_version
&&
1614 strncmp(e
->metadata_version
,
1615 "external:/", 10) == 0;
1616 if (members
!= member
)
1618 me
= map_by_devnum(&map
, e
->devnum
);
1620 && strcmp(me
->path
, "/unknown") != 0)
1623 name
= get_md_name(e
->devnum
);
1626 pr_err("cannot find device file for %s\n",
1631 rv
|= Detail(name
, c
);
1633 rv
|= WaitClean(name
, -1, c
->verbose
);
1641 static int stop_scan(int verbose
)
1643 /* apply --stop to all devices in /proc/mdstat */
1644 /* Due to possible stacking of devices, repeat until
1645 * nothing more can be stopped
1647 int progress
=1, err
;
1651 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1652 struct mdstat_ent
*e
;
1654 if (!progress
) last
= 1;
1655 progress
= 0; err
= 0;
1656 for (e
=ms
; e
; e
=e
->next
) {
1657 char *name
= get_md_name(e
->devnum
);
1661 pr_err("cannot find device file for %s\n",
1665 mdfd
= open_mddev(name
, 1);
1667 if (Manage_runstop(name
, mdfd
, -1, verbose
, !last
))
1677 } while (!last
&& err
);
1683 static int misc_list(struct mddev_dev
*devlist
,
1684 struct mddev_ident
*ident
,
1685 struct supertype
*ss
, struct context
*c
)
1687 struct mddev_dev
*dv
;
1690 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1693 switch(dv
->disposition
) {
1695 rv
|= Detail(dv
->devname
, c
);
1697 case KillOpt
: /* Zero superblock */
1699 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1703 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1710 rv
|= Query(dv
->devname
); continue;
1712 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1715 rv
|= Wait(dv
->devname
); continue;
1717 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1719 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1721 case UpdateSubarray
:
1722 if (c
->update
== NULL
) {
1723 pr_err("-U/--update must be specified with --update-subarray\n");
1727 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1728 c
->update
, ident
, c
->verbose
);
1731 mdfd
= open_mddev(dv
->devname
, 1);
1733 switch(dv
->disposition
) {
1735 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, c
->verbose
, 0); break;
1737 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, c
->verbose
, 0); break;
1739 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1741 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;