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
[])
51 unsigned long long array_size
= 0;
52 struct mddev_ident ident
;
53 char *configfile
= NULL
;
56 struct mddev_dev
*devlist
= NULL
;
57 struct mddev_dev
**devlistend
= & devlist
;
60 char *symlinks
= NULL
;
61 int grow_continue
= 0;
62 /* autof indicates whether and how to create device node.
63 * bottom 3 bits are style. Rest (when shifted) are number of parts
65 * 1 - don't create (no)
66 * 2 - if is_standard, then create (yes)
67 * 3 - create as 'md' - reject is_standard mdp (md)
68 * 4 - create as 'mdp' - reject is_standard md (mdp)
69 * 5 - default to md if not is_standard (md in config file)
70 * 6 - default to mdp if not is_standard (part, or mdp in config file)
73 .require_homehost
= 1,
78 .bitmap_chunk
= UnSet
,
81 char sys_hostname
[256];
82 char *mailaddr
= NULL
;
88 int spare_sharing
= 1;
89 struct supertype
*ss
= NULL
;
91 char *shortopt
= short_options
;
94 char *remove_path
= NULL
;
95 char *udev_filename
= NULL
;
102 srandom(time(0) ^ getpid());
106 ident
.raid_disks
= UnSet
;
107 ident
.super_minor
= UnSet
;
109 ident
.spare_group
= NULL
;
112 ident
.bitmap_fd
= -1;
113 ident
.bitmap_file
= NULL
;
115 ident
.container
= NULL
;
118 while ((option_index
= -1) ,
119 (opt
=getopt_long(argc
, argv
,
120 shortopt
, long_options
,
121 &option_index
)) != -1) {
123 /* firstly, some mode-independent options */
133 fputs(Version
, stderr
);
136 case 'v': c
.verbose
++;
139 case 'q': c
.verbose
--;
143 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
144 || mode
== GROW
|| mode
== INCREMENTAL
146 break; /* b means bitmap */
151 case 'Y': c
.export
++;
155 if (strcasecmp(optarg
, "<ignore>") == 0)
156 c
.require_homehost
= 0;
162 * --offroot sets first char of argv[0] to @. This is used
163 * by systemd to signal that the task was launched from
164 * initrd/initramfs and should be preserved during shutdown
174 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
180 fputs(Usage
, stderr
);
183 /* second, figure out the mode.
184 * Some options force the mode. Others
185 * set the mode if it isn't already
191 shortopt
= short_bitmap_options
;
199 case ReAdd
: /* re-add */
202 shortopt
= short_bitmap_options
;
206 case 'A': newmode
= ASSEMBLE
;
207 shortopt
= short_bitmap_auto_options
;
209 case 'B': newmode
= BUILD
;
210 shortopt
= short_bitmap_auto_options
;
212 case 'C': newmode
= CREATE
;
213 shortopt
= short_bitmap_auto_options
;
215 case 'F': newmode
= MONITOR
;
217 case 'G': newmode
= GROW
;
218 shortopt
= short_bitmap_options
;
220 case 'I': newmode
= INCREMENTAL
;
221 shortopt
= short_bitmap_auto_options
;
224 newmode
= AUTODETECT
;
255 if (mode
&& newmode
== mode
) {
256 /* everybody happy ! */
257 } else if (mode
&& newmode
!= mode
) {
260 if (option_index
>= 0)
261 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
263 fprintf(stderr
, "-%c", opt
);
264 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
265 map_num(modes
, newmode
),
266 map_num(modes
, mode
));
268 } else if (!mode
&& newmode
) {
270 if (mode
== MISC
&& devs_found
) {
271 pr_err("No action given for %s in --misc mode\n",
273 fprintf(stderr
," Action options must come before device names\n");
277 /* special case of -c --help */
278 if ((opt
== 'c' || opt
== ConfigFile
) &&
279 ( strncmp(optarg
, "--h", 3)==0 ||
280 strncmp(optarg
, "-h", 2)==0)) {
281 fputs(Help_config
, stdout
);
285 /* If first option is a device, don't force the mode yet */
287 if (devs_found
== 0) {
288 dv
= xmalloc(sizeof(*dv
));
289 dv
->devname
= optarg
;
290 dv
->disposition
= devmode
;
291 dv
->writemostly
= writemostly
;
295 devlistend
= &dv
->next
;
300 /* No mode yet, and this is the second device ... */
301 pr_err("An option must be given to set the mode before a second device\n"
302 " (%s) is listed\n", optarg
);
305 if (option_index
>= 0)
306 pr_err("--%s", long_options
[option_index
].name
);
309 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
313 /* if we just set the mode, then done */
327 /* an undecorated option - must be a device name.
329 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
330 pr_err("Must give one of -a/-r/-f"
331 " for subsequent devices at %s\n", optarg
);
334 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
335 pr_err("Must give -a/--add for"
336 " devices to add: %s\n", optarg
);
339 dv
= xmalloc(sizeof(*dv
));
340 dv
->devname
= optarg
;
341 dv
->disposition
= devmode
;
342 dv
->writemostly
= writemostly
;
346 devlistend
= &dv
->next
;
352 /* We've got a mode, and opt is now something else which
353 * could depend on the mode */
354 #define O(a,b) ((a<<16)|b)
355 switch (O(mode
,opt
)) {
357 case O(GROW
,ChunkSize
):
359 case O(CREATE
,ChunkSize
):
360 case O(BUILD
,'c'): /* chunk or rounding */
361 case O(BUILD
,ChunkSize
): /* chunk or rounding */
363 pr_err("chunk/rounding may only be specified once. "
364 "Second value is %s.\n", optarg
);
367 s
.chunk
= parse_size(optarg
);
368 if (s
.chunk
< 8 || (s
.chunk
&1)) {
369 pr_err("invalid chunk/rounding value: %s\n",
373 /* Convert sectors to K */
377 case O(INCREMENTAL
, 'e'):
379 case O(ASSEMBLE
,'e'):
380 case O(MISC
,'e'): /* set metadata (superblock) information */
382 pr_err("metadata information already given\n");
385 for(i
=0; !ss
&& superlist
[i
]; i
++)
386 ss
= superlist
[i
]->match_metadata_desc(optarg
);
389 pr_err("unrecognised metadata identifier: %s\n", optarg
);
395 case O(MANAGE
,WriteMostly
):
397 case O(BUILD
,WriteMostly
):
399 case O(CREATE
,WriteMostly
):
400 /* set write-mostly for following devices */
405 /* clear write-mostly for following devices */
412 case O(BUILD
,'z'): /* size */
414 pr_err("size may only be specified once. "
415 "Second value is %s.\n", optarg
);
418 if (strcmp(optarg
, "max")==0)
421 s
.size
= parse_size(optarg
);
423 pr_err("invalid size: %s\n",
427 /* convert sectors to K */
432 case O(GROW
,'Z'): /* array size */
433 if (array_size
> 0) {
434 pr_err("array-size may only be specified once. "
435 "Second value is %s.\n", optarg
);
438 if (strcmp(optarg
, "max") == 0)
439 array_size
= MAX_SIZE
;
441 array_size
= parse_size(optarg
);
442 if (array_size
<= 0) {
443 pr_err("invalid array size: %s\n",
452 case O(BUILD
,'l'): /* set raid level*/
453 if (s
.level
!= UnSet
) {
454 pr_err("raid level may only be set once. "
455 "Second value is %s.\n", optarg
);
458 s
.level
= map_name(pers
, optarg
);
459 if (s
.level
== UnSet
) {
460 pr_err("invalid raid level: %s\n",
464 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
465 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
468 pr_err("Raid level %s not permitted with --build.\n",
472 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
473 pr_err("raid level %s is incompatible with spare-devices setting.\n",
477 ident
.level
= s
.level
;
480 case O(GROW
, 'p'): /* new layout */
481 case O(GROW
, Layout
):
483 pr_err("layout may only be sent once. "
484 "Second value was %s\n", optarg
);
487 s
.layout_str
= optarg
;
488 /* 'Grow' will parse the value */
491 case O(CREATE
,'p'): /* raid5 layout */
492 case O(CREATE
,Layout
):
493 case O(BUILD
,'p'): /* faulty layout */
494 case O(BUILD
,Layout
):
495 if (s
.layout
!= UnSet
) {
496 pr_err("layout may only be sent once. "
497 "Second value was %s\n", optarg
);
502 pr_err("layout not meaningful for %s arrays.\n",
503 map_num(pers
, s
.level
));
506 pr_err("raid level must be given before layout.\n");
510 s
.layout
= map_name(r5layout
, optarg
);
511 if (s
.layout
==UnSet
) {
512 pr_err("layout %s not understood for raid5.\n",
518 s
.layout
= map_name(r6layout
, optarg
);
519 if (s
.layout
==UnSet
) {
520 pr_err("layout %s not understood for raid6.\n",
527 s
.layout
= parse_layout_10(optarg
);
529 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
537 s
.layout
= parse_layout_faulty(optarg
);
538 if (s
.layout
== -1) {
539 pr_err("layout %s not understood for faulty.\n",
547 case O(CREATE
,AssumeClean
):
548 case O(BUILD
,AssumeClean
): /* assume clean */
549 case O(GROW
,AssumeClean
):
555 case O(BUILD
,'n'): /* number of raid disks */
557 pr_err("raid-devices set twice: %d and %s\n",
558 s
.raiddisks
, optarg
);
561 s
.raiddisks
= parse_num(optarg
);
562 if (s
.raiddisks
<= 0) {
563 pr_err("invalid number of raid devices: %s\n",
567 ident
.raid_disks
= s
.raiddisks
;
570 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
572 pr_err("spare-devices set twice: %d and %s\n",
573 s
.sparedisks
, optarg
);
576 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
577 pr_err("spare-devices setting is incompatible with raid level %d\n",
581 s
.sparedisks
= parse_num(optarg
);
582 if (s
.sparedisks
< 0) {
583 pr_err("invalid number of spare-devices: %s\n",
593 case O(INCREMENTAL
,'a'):
594 case O(INCREMENTAL
,Auto
):
595 case O(ASSEMBLE
,'a'):
596 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
597 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
600 case O(CREATE
,Symlinks
):
601 case O(BUILD
,Symlinks
):
602 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
606 case O(BUILD
,'f'): /* force honouring '-n 1' */
607 case O(BUILD
,Force
): /* force honouring '-n 1' */
608 case O(GROW
,'f'): /* ditto */
609 case O(GROW
,Force
): /* ditto */
610 case O(CREATE
,'f'): /* force honouring of device list */
611 case O(CREATE
,Force
): /* force honouring of device list */
612 case O(ASSEMBLE
,'f'): /* force assembly */
613 case O(ASSEMBLE
,Force
): /* force assembly */
614 case O(MISC
,'f'): /* force zero */
615 case O(MISC
,Force
): /* force zero */
616 case O(MANAGE
,Force
): /* add device which is too large */
619 /* now for the Assemble options */
620 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
622 case O(INCREMENTAL
, FreezeReshape
):
623 c
.freeze_reshape
= 1;
625 case O(CREATE
,'u'): /* uuid of array */
626 case O(ASSEMBLE
,'u'): /* uuid of array */
627 if (ident
.uuid_set
) {
628 pr_err("uuid cannot be set twice. "
629 "Second value %s.\n", optarg
);
632 if (parse_uuid(optarg
, ident
.uuid
))
635 pr_err("Bad uuid: %s\n", optarg
);
641 case O(ASSEMBLE
,'N'):
644 pr_err("name cannot be set twice. "
645 "Second value %s.\n", optarg
);
648 if (mode
== MISC
&& !c
.subarray
) {
649 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
652 if (strlen(optarg
) > 32) {
653 pr_err("name '%s' is too long, 32 chars max.\n",
657 strcpy(ident
.name
, optarg
);
660 case O(ASSEMBLE
,'m'): /* super-minor for array */
661 case O(ASSEMBLE
,SuperMinor
):
662 if (ident
.super_minor
!= UnSet
) {
663 pr_err("super-minor cannot be set twice. "
664 "Second value: %s.\n", optarg
);
667 if (strcmp(optarg
, "dev")==0)
668 ident
.super_minor
= -2;
670 ident
.super_minor
= parse_num(optarg
);
671 if (ident
.super_minor
< 0) {
672 pr_err("Bad super-minor number: %s.\n", optarg
);
678 case O(ASSEMBLE
,'o'):
684 case O(ASSEMBLE
,'U'): /* update the superblock */
687 pr_err("Can only update one aspect"
688 " of superblock, both %s and %s given.\n",
692 if (mode
== MISC
&& !c
.subarray
) {
693 pr_err("Only subarrays can be"
694 " updated in misc mode\n");
698 if (strcmp(c
.update
, "sparc2.2")==0)
700 if (strcmp(c
.update
, "super-minor") == 0)
702 if (strcmp(c
.update
, "summaries")==0)
704 if (strcmp(c
.update
, "resync")==0)
706 if (strcmp(c
.update
, "uuid")==0)
708 if (strcmp(c
.update
, "name")==0)
710 if (strcmp(c
.update
, "homehost")==0)
712 if (strcmp(c
.update
, "devicesize")==0)
714 if (strcmp(c
.update
, "no-bitmap")==0)
716 if (strcmp(c
.update
, "byteorder")==0) {
718 pr_err("must not set metadata"
719 " type with --update=byteorder.\n");
722 for(i
=0; !ss
&& superlist
[i
]; i
++)
723 ss
= superlist
[i
]->match_metadata_desc(
726 pr_err("INTERNAL ERROR"
727 " cannot find 0.swap\n");
733 if (strcmp(c
.update
,"?") == 0 ||
734 strcmp(c
.update
, "help") == 0) {
736 fprintf(outf
, Name
": ");
740 Name
": '--update=%s' is invalid. ",
743 fprintf(outf
, "Valid --update options are:\n"
744 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
745 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
747 exit(outf
== stdout
? 0 : 2);
750 /* update=devicesize is allowed with --re-add */
751 if (devmode
!= 'A') {
752 pr_err("--update in Manage mode only"
753 " allowed with --re-add.\n");
757 pr_err("Can only update one aspect"
758 " of superblock, both %s and %s given.\n",
763 if (strcmp(c
.update
, "devicesize") != 0) {
764 pr_err("only 'devicesize' can be"
765 " updated with --re-add\n");
770 case O(INCREMENTAL
,NoDegraded
):
771 pr_err("--no-degraded is deprecated in Incremental mode\n");
772 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
773 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
774 * so we overload slightly */
777 case O(ASSEMBLE
,'c'):
778 case O(ASSEMBLE
,ConfigFile
):
779 case O(INCREMENTAL
, 'c'):
780 case O(INCREMENTAL
, ConfigFile
):
782 case O(MISC
, ConfigFile
):
784 case O(MONITOR
,ConfigFile
):
786 pr_err("configfile cannot be set twice. "
787 "Second value is %s.\n", optarg
);
791 set_conffile(configfile
);
792 /* FIXME possibly check that config file exists. Even parse it */
794 case O(ASSEMBLE
,'s'): /* scan */
797 case O(INCREMENTAL
,'s'):
801 case O(MONITOR
,'m'): /* mail address */
802 case O(MONITOR
,EMail
):
804 pr_err("only specify one mailaddress. %s ignored.\n",
810 case O(MONITOR
,'p'): /* alert program */
811 case O(MONITOR
,ProgramOpt
): /* alert program */
813 pr_err("only specify one alter program. %s ignored.\n",
819 case O(MONITOR
,'r'): /* rebuild increments */
820 case O(MONITOR
,Increment
):
821 increments
= atoi(optarg
);
822 if (increments
> 99 || increments
< 1) {
823 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
828 case O(MONITOR
,'d'): /* delay in seconds */
830 case O(BUILD
,'d'): /* delay for bitmap updates */
833 pr_err("only specify delay once. %s ignored.\n",
836 c
.delay
= parse_num(optarg
);
838 pr_err("invalid delay: %s\n",
844 case O(MONITOR
,'f'): /* daemonise */
845 case O(MONITOR
,Fork
):
848 case O(MONITOR
,'i'): /* pid */
850 pr_err("only specify one pid file. %s ignored.\n",
855 case O(MONITOR
,'1'): /* oneshot */
859 case O(MONITOR
,'t'): /* test */
862 case O(MONITOR
,'y'): /* log messages to syslog */
863 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
866 case O(MONITOR
, NoSharing
):
870 /* now the general management options. Some are applicable
871 * to other modes. None have arguments.
876 case O(MANAGE
,Add
): /* add a drive */
879 case O(MANAGE
,ReAdd
):
882 case O(MANAGE
,'r'): /* remove a drive */
883 case O(MANAGE
,Remove
):
886 case O(MANAGE
,'f'): /* set faulty */
888 case O(INCREMENTAL
,'f'):
889 case O(INCREMENTAL
,Remove
):
890 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
891 * even though we will both fail and
892 * remove the device */
895 case O(INCREMENTAL
,'R'):
897 case O(ASSEMBLE
,'R'):
899 case O(CREATE
,'R'): /* Run the array */
901 pr_err("Cannot both Stop and Run an array\n");
908 pr_err("Cannot both Run and Stop an array\n");
920 case O(MISC
,KillOpt
):
927 case O(MISC
, WaitOpt
):
928 case O(MISC
, Waitclean
):
929 case O(MISC
, DetailPlatform
):
930 case O(MISC
, KillSubarray
):
931 case O(MISC
, UpdateSubarray
):
932 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
934 pr_err("subarray can only"
935 " be specified once\n");
940 if (devmode
&& devmode
!= opt
&&
941 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
942 pr_err("--examine/-E cannot be given with ");
943 if (devmode
== 'E') {
944 if (option_index
>= 0)
945 fprintf(stderr
, "--%s\n",
946 long_options
[option_index
].name
);
948 fprintf(stderr
, "-%c\n", opt
);
949 } else if (isalpha(devmode
))
950 fprintf(stderr
, "-%c\n", devmode
);
952 fprintf(stderr
, "previous option\n");
957 case O(MISC
, UdevRules
):
958 if (devmode
&& devmode
!= opt
) {
959 pr_err("--udev-rules must"
960 " be the only option.\n");
963 pr_err("only specify one udev "
964 "rule filename. %s ignored.\n",
967 udev_filename
= optarg
;
975 case O(MISC
, Sparc22
):
976 if (devmode
!= 'E') {
977 pr_err("--sparc2.2 only allowed with --examine\n");
983 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
984 case O(ASSEMBLE
,Bitmap
):
986 pr_err("bitmap file needed with -b in --assemble mode\n");
989 if (strcmp(optarg
, "internal")==0) {
990 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
993 bitmap_fd
= open(optarg
, O_RDWR
);
994 if (!*optarg
|| bitmap_fd
< 0) {
995 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
998 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1001 case O(ASSEMBLE
, BackupFile
):
1002 case O(GROW
, BackupFile
):
1003 /* Specify a file into which grow might place a backup,
1004 * or from which assemble might recover a backup
1006 if (c
.backup_file
) {
1007 pr_err("backup file already specified, rejecting %s\n", optarg
);
1010 c
.backup_file
= optarg
;
1013 case O(GROW
, Continue
):
1014 /* Continue interrupted grow
1018 case O(ASSEMBLE
, InvalidBackup
):
1019 /* Acknowledge that the backupfile is invalid, but ask
1020 * to continue anyway
1022 c
.invalid_backup
= 1;
1026 case O(BUILD
,Bitmap
):
1028 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1029 if (strcmp(optarg
, "none") == 0) {
1030 pr_err("'--bitmap none' only"
1031 " supported for --grow\n");
1036 case O(GROW
,Bitmap
):
1037 if (strcmp(optarg
, "internal")== 0 ||
1038 strcmp(optarg
, "none")== 0 ||
1039 strchr(optarg
, '/') != NULL
) {
1040 s
.bitmap_file
= optarg
;
1044 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1045 " not '%s'\n", optarg
);
1048 case O(GROW
,BitmapChunk
):
1049 case O(BUILD
,BitmapChunk
):
1050 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1051 s
.bitmap_chunk
= parse_size(optarg
);
1052 if (s
.bitmap_chunk
<= 0 ||
1053 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1054 pr_err("invalid bitmap chunksize: %s\n",
1058 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1061 case O(GROW
, WriteBehind
):
1062 case O(BUILD
, WriteBehind
):
1063 case O(CREATE
, WriteBehind
): /* write-behind mode */
1064 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1066 s
.write_behind
= parse_num(optarg
);
1067 if (s
.write_behind
< 0 ||
1068 s
.write_behind
> 16383) {
1069 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1075 case O(INCREMENTAL
, 'r'):
1076 case O(INCREMENTAL
, RebuildMapOpt
):
1079 case O(INCREMENTAL
, IncrementalPath
):
1080 remove_path
= optarg
;
1083 /* We have now processed all the valid options. Anything else is
1086 if (option_index
> 0)
1087 pr_err(":option --%s not valid in %s mode\n",
1088 long_options
[option_index
].name
,
1089 map_num(modes
, mode
));
1091 pr_err("option -%c not valid in %s mode\n",
1092 opt
, map_num(modes
, mode
));
1099 if (print_help
== 2)
1100 help_text
= OptionHelp
;
1102 help_text
= mode_help
[mode
];
1103 if (help_text
== NULL
)
1105 fputs(help_text
,stdout
);
1109 if (!mode
&& devs_found
) {
1112 if (devlist
->disposition
== 0)
1113 devlist
->disposition
= devmode
;
1116 fputs(Usage
, stderr
);
1121 struct createinfo
*ci
= conf_get_create_info();
1123 if (strcasecmp(symlinks
, "yes") == 0)
1125 else if (strcasecmp(symlinks
, "no") == 0)
1128 pr_err("option --symlinks must be 'no' or 'yes'\n");
1132 /* Ok, got the option parsing out of the way
1133 * hopefully it's mostly right but there might be some stuff
1136 * That is mosty checked in the per-mode stuff but...
1138 * For @,B,C and A without -s, the first device listed must be
1139 * an md device. We check that here and open it.
1142 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1144 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1145 if (devs_found
< 1) {
1146 pr_err("an md device must be given in this mode\n");
1149 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1150 pr_err("--super-minor=dev is incompatible with --auto\n");
1153 if (mode
== MANAGE
|| mode
== GROW
) {
1154 mdfd
= open_mddev(devlist
->devname
, 1);
1158 /* non-existent device is OK */
1159 mdfd
= open_mddev(devlist
->devname
, 0);
1161 pr_err("device %s exists but is not an "
1162 "md array.\n", devlist
->devname
);
1165 if ((int)ident
.super_minor
== -2) {
1168 pr_err("--super-minor=dev given, and "
1169 "listed device %s doesn't exist.\n",
1174 ident
.super_minor
= minor(stb
.st_rdev
);
1176 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1177 /* We don't really want this open yet, we just might
1178 * have wanted to check some things
1186 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1187 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1188 " a mistake. If you really mean it you will need to specify --force before\n"
1189 " setting the number of drives.\n");
1194 if (c
.homehost
== NULL
)
1195 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1196 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1197 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1198 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1199 c
.homehost
= sys_hostname
;
1202 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1204 c
.require_homehost
= 0;
1207 if ((mode
== MISC
&& devmode
== 'E')
1208 || (mode
== MONITOR
&& spare_sharing
== 0))
1209 /* Anyone may try this */;
1210 else if (geteuid() != 0) {
1211 pr_err("must be super-user to perform this action\n");
1215 ident
.autof
= c
.autof
;
1217 if (c
.scan
&& c
.verbose
< 2)
1218 /* --scan implied --brief unless -vv */
1224 /* readonly, add/remove, readwrite, runstop */
1226 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1227 if (!rv
&& devs_found
>1)
1228 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1229 devlist
->next
, c
.verbose
, c
.test
,
1231 if (!rv
&& c
.readonly
< 0)
1232 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1233 if (!rv
&& c
.runstop
)
1234 rv
= Manage_runstop(devlist
->devname
, mdfd
, c
.runstop
, c
.verbose
, 0);
1237 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1238 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1239 /* Only a device has been given, so get details from config file */
1240 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1241 if (array_ident
== NULL
) {
1242 pr_err("%s not identified in config file.\n",
1248 if (array_ident
->autof
== 0)
1249 array_ident
->autof
= c
.autof
;
1250 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1254 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1256 else if (devs_found
> 0) {
1257 if (c
.update
&& devs_found
> 1) {
1258 pr_err("can only update a single array at a time\n");
1261 if (c
.backup_file
&& devs_found
> 1) {
1262 pr_err("can only assemble a single array when providing a backup file.\n");
1265 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1266 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1267 if (array_ident
== NULL
) {
1268 pr_err("%s not identified in config file.\n",
1273 if (array_ident
->autof
== 0)
1274 array_ident
->autof
= c
.autof
;
1275 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1280 pr_err("--update not meaningful with a --scan assembly.\n");
1283 if (c
.backup_file
) {
1284 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1287 rv
= scan_assemble(ss
, &c
, &ident
);
1293 c
.delay
= DEFAULT_BITMAP_DELAY
;
1294 if (s
.write_behind
&& !s
.bitmap_file
) {
1295 pr_err("write-behind mode requires a bitmap.\n");
1299 if (s
.raiddisks
== 0) {
1300 pr_err("no raid-devices specified.\n");
1305 if (s
.bitmap_file
) {
1306 if (strcmp(s
.bitmap_file
, "internal")==0) {
1307 pr_err("'internal' bitmaps not supported with --build\n");
1312 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1316 c
.delay
= DEFAULT_BITMAP_DELAY
;
1317 if (s
.write_behind
&& !s
.bitmap_file
) {
1318 pr_err("write-behind mode requires a bitmap.\n");
1322 if (s
.raiddisks
== 0) {
1323 pr_err("no raid-devices specified.\n");
1328 rv
= Create(ss
, devlist
->devname
,
1329 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1330 devs_found
-1, devlist
->next
,
1334 if (devmode
== 'E') {
1335 if (devlist
== NULL
&& !c
.scan
) {
1336 pr_err("No devices to examine\n");
1339 if (devlist
== NULL
)
1340 devlist
= conf_get_devs();
1341 if (devlist
== NULL
) {
1342 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1345 rv
= Examine(devlist
, &c
, ss
);
1346 } else if (devmode
== DetailPlatform
) {
1347 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1, c
.verbose
);
1348 } else if (devlist
== NULL
) {
1349 if (devmode
== 'S' && c
.scan
)
1350 rv
= stop_scan(c
.verbose
);
1351 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1352 rv
= misc_scan(devmode
, &c
);
1353 else if (devmode
== UdevRules
)
1354 rv
= Write_rules(udev_filename
);
1356 pr_err("No devices given.\n");
1360 rv
= misc_list(devlist
, &ident
, ss
, &c
);
1363 if (!devlist
&& !c
.scan
) {
1364 pr_err("Cannot monitor: need --scan or at least one device\n");
1368 if (pidfile
&& !daemonise
) {
1369 pr_err("Cannot write a pid file when not in daemon mode\n");
1374 if (get_linux_version() > 2006016)
1375 /* mdstat responds to poll */
1382 rv
= Monitor(devlist
, mailaddr
, program
,
1383 &c
, daemonise
, oneshot
,
1384 dosyslog
, pidfile
, increments
,
1389 if (array_size
> 0) {
1390 /* alway impose array size first, independent of
1392 * Do not allow level or raid_disks changes at the
1393 * same time as that can be irreversibly destructive.
1397 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1398 pr_err("cannot change array size in same operation "
1399 "as changing raiddisks or level.\n"
1400 " Change size first, then check that data is still intact.\n");
1404 sysfs_init(&sra
, mdfd
, 0);
1405 if (array_size
== MAX_SIZE
)
1406 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1408 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1411 pr_err("--array-size setting"
1412 " is too large.\n");
1414 pr_err("current kernel does"
1415 " not support setting --array-size\n");
1420 if (devs_found
> 1 && s
.raiddisks
== 0) {
1422 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1423 pr_err("--add cannot be used with "
1424 "other geometry changes in --grow mode\n");
1428 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1429 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1434 } else if (s
.bitmap_file
) {
1435 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1436 s
.layout_str
!= NULL
|| devs_found
> 1) {
1437 pr_err("--bitmap changes cannot be "
1438 "used with other geometry changes "
1439 "in --grow mode\n");
1444 c
.delay
= DEFAULT_BITMAP_DELAY
;
1445 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, s
.bitmap_file
,
1446 s
.bitmap_chunk
, c
.delay
, s
.write_behind
, c
.force
);
1447 } else if (grow_continue
)
1448 rv
= Grow_continue_command(devlist
->devname
,
1449 mdfd
, c
.backup_file
,
1451 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1452 || s
.chunk
!= 0 || s
.level
!= UnSet
) {
1453 rv
= Grow_reshape(devlist
->devname
, mdfd
, c
.verbose
, c
.backup_file
,
1454 s
.size
, s
.level
, s
.layout_str
, s
.chunk
, s
.raiddisks
,
1456 s
.assume_clean
, c
.force
);
1457 } else if (array_size
== 0)
1458 pr_err("no changes to --grow\n");
1465 if (c
.runstop
<= 0) {
1466 pr_err("--incremental --scan meaningless without --run.\n");
1469 if (devmode
== 'f') {
1470 pr_err("--incremental --scan --fail not supported.\n");
1473 rv
= IncrementalScan(c
.verbose
);
1476 if (!rebuild_map
&& !c
.scan
) {
1477 pr_err("--incremental requires a device.\n");
1482 if (devlist
->next
) {
1483 pr_err("--incremental can only handle one device.\n");
1488 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1491 rv
= Incremental(devlist
->devname
, &c
, ss
);
1500 static int scan_assemble(struct supertype
*ss
,
1502 struct mddev_ident
*ident
)
1504 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1505 struct mddev_dev
*devlist
= conf_get_devs();
1506 struct map_ent
*map
= NULL
;
1509 int failures
, successes
;
1511 if (conf_verify_devnames(array_list
)) {
1512 pr_err("Duplicate MD device names in "
1513 "conf file were found.\n");
1516 if (devlist
== NULL
) {
1517 pr_err("No devices listed in conf file were found.\n");
1520 for (a
= array_list
; a
; a
= a
->next
) {
1523 a
->autof
= c
->autof
;
1526 pr_err("%s: failed to get "
1527 "exclusive lock on mapfile\n",
1533 for (a
= array_list
; a
; a
= a
->next
) {
1538 strcasecmp(a
->devname
, "<ignore>") == 0)
1541 r
= Assemble(ss
, a
->devname
,
1551 } while (failures
&& successes
);
1552 if (c
->homehost
&& cnt
== 0) {
1553 /* Maybe we can auto-assemble something.
1554 * Repeatedly call Assemble in auto-assemble mode
1559 ident
->autof
= c
->autof
;
1561 struct mddev_dev
*devlist
= conf_get_devs();
1564 rv2
= Assemble(ss
, NULL
,
1572 /* Incase there are stacked devices, we need to go around again */
1574 if (cnt
== 0 && rv
== 0) {
1575 pr_err("No arrays found in config file or automatically\n");
1579 } else if (cnt
== 0 && rv
== 0) {
1580 pr_err("No arrays found in config file\n");
1587 static int misc_scan(char devmode
, struct context
*c
)
1589 /* apply --detail or --wait-clean to
1590 * all devices in /proc/mdstat
1592 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1593 struct mdstat_ent
*e
;
1594 struct map_ent
*map
= NULL
;
1598 for (members
= 0; members
<= 1; members
++) {
1599 for (e
=ms
; e
; e
=e
->next
) {
1602 int member
= e
->metadata_version
&&
1603 strncmp(e
->metadata_version
,
1604 "external:/", 10) == 0;
1605 if (members
!= member
)
1607 me
= map_by_devnum(&map
, e
->devnum
);
1609 && strcmp(me
->path
, "/unknown") != 0)
1612 name
= get_md_name(e
->devnum
);
1615 pr_err("cannot find device file for %s\n",
1620 rv
|= Detail(name
, c
);
1622 rv
|= WaitClean(name
, -1, c
->verbose
);
1630 static int stop_scan(int verbose
)
1632 /* apply --stop to all devices in /proc/mdstat */
1633 /* Due to possible stacking of devices, repeat until
1634 * nothing more can be stopped
1636 int progress
=1, err
;
1640 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1641 struct mdstat_ent
*e
;
1643 if (!progress
) last
= 1;
1644 progress
= 0; err
= 0;
1645 for (e
=ms
; e
; e
=e
->next
) {
1646 char *name
= get_md_name(e
->devnum
);
1650 pr_err("cannot find device file for %s\n",
1654 mdfd
= open_mddev(name
, 1);
1656 if (Manage_runstop(name
, mdfd
, -1, verbose
, !last
))
1666 } while (!last
&& err
);
1672 static int misc_list(struct mddev_dev
*devlist
,
1673 struct mddev_ident
*ident
,
1674 struct supertype
*ss
, struct context
*c
)
1676 struct mddev_dev
*dv
;
1679 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1682 switch(dv
->disposition
) {
1684 rv
|= Detail(dv
->devname
, c
);
1686 case KillOpt
: /* Zero superblock */
1688 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1692 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1699 rv
|= Query(dv
->devname
); continue;
1701 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1704 rv
|= Wait(dv
->devname
); continue;
1706 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1708 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1710 case UpdateSubarray
:
1711 if (c
->update
== NULL
) {
1712 pr_err("-U/--update must be specified with --update-subarray\n");
1716 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1717 c
->update
, ident
, c
->verbose
);
1720 mdfd
= open_mddev(dv
->devname
, 1);
1722 switch(dv
->disposition
) {
1724 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, c
->verbose
, 0); break;
1726 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, c
->verbose
, 0); break;
1728 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1730 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;