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 unsigned long long data_offset
= INVALID_SECTORS
;
53 struct mddev_ident ident
;
54 char *configfile
= NULL
;
57 struct mddev_dev
*devlist
= NULL
;
58 struct mddev_dev
**devlistend
= & devlist
;
61 char *symlinks
= NULL
;
62 int grow_continue
= 0;
63 /* autof indicates whether and how to create device node.
64 * bottom 3 bits are style. Rest (when shifted) are number of parts
66 * 1 - don't create (no)
67 * 2 - if is_standard, then create (yes)
68 * 3 - create as 'md' - reject is_standard mdp (md)
69 * 4 - create as 'mdp' - reject is_standard md (mdp)
70 * 5 - default to md if not is_standard (md in config file)
71 * 6 - default to mdp if not is_standard (part, or mdp in config file)
74 .require_homehost
= 1,
79 .bitmap_chunk
= UnSet
,
82 char sys_hostname
[256];
83 char *mailaddr
= NULL
;
89 int spare_sharing
= 1;
90 struct supertype
*ss
= NULL
;
92 char *shortopt
= short_options
;
95 char *remove_path
= NULL
;
96 char *udev_filename
= NULL
;
103 srandom(time(0) ^ getpid());
107 ident
.raid_disks
= UnSet
;
108 ident
.super_minor
= UnSet
;
110 ident
.spare_group
= NULL
;
113 ident
.bitmap_fd
= -1;
114 ident
.bitmap_file
= NULL
;
116 ident
.container
= NULL
;
119 while ((option_index
= -1) ,
120 (opt
=getopt_long(argc
, argv
,
121 shortopt
, long_options
,
122 &option_index
)) != -1) {
124 /* firstly, some mode-independent options */
134 fputs(Version
, stderr
);
137 case 'v': c
.verbose
++;
140 case 'q': c
.verbose
--;
144 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
145 || mode
== GROW
|| mode
== INCREMENTAL
147 break; /* b means bitmap */
152 case 'Y': c
.export
++;
156 if (strcasecmp(optarg
, "<ignore>") == 0)
157 c
.require_homehost
= 0;
163 * --offroot sets first char of argv[0] to @. This is used
164 * by systemd to signal that the task was launched from
165 * initrd/initramfs and should be preserved during shutdown
175 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
181 fputs(Usage
, stderr
);
184 /* second, figure out the mode.
185 * Some options force the mode. Others
186 * set the mode if it isn't already
192 shortopt
= short_bitmap_options
;
200 case ReAdd
: /* re-add */
203 shortopt
= short_bitmap_options
;
207 case 'A': newmode
= ASSEMBLE
;
208 shortopt
= short_bitmap_auto_options
;
210 case 'B': newmode
= BUILD
;
211 shortopt
= short_bitmap_auto_options
;
213 case 'C': newmode
= CREATE
;
214 shortopt
= short_bitmap_auto_options
;
216 case 'F': newmode
= MONITOR
;
218 case 'G': newmode
= GROW
;
219 shortopt
= short_bitmap_options
;
221 case 'I': newmode
= INCREMENTAL
;
222 shortopt
= short_bitmap_auto_options
;
225 newmode
= AUTODETECT
;
256 if (mode
&& newmode
== mode
) {
257 /* everybody happy ! */
258 } else if (mode
&& newmode
!= mode
) {
261 if (option_index
>= 0)
262 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
264 fprintf(stderr
, "-%c", opt
);
265 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
266 map_num(modes
, newmode
),
267 map_num(modes
, mode
));
269 } else if (!mode
&& newmode
) {
271 if (mode
== MISC
&& devs_found
) {
272 pr_err("No action given for %s in --misc mode\n",
274 fprintf(stderr
," Action options must come before device names\n");
278 /* special case of -c --help */
279 if ((opt
== 'c' || opt
== ConfigFile
) &&
280 ( strncmp(optarg
, "--h", 3)==0 ||
281 strncmp(optarg
, "-h", 2)==0)) {
282 fputs(Help_config
, stdout
);
286 /* If first option is a device, don't force the mode yet */
288 if (devs_found
== 0) {
289 dv
= xmalloc(sizeof(*dv
));
290 dv
->devname
= optarg
;
291 dv
->disposition
= devmode
;
292 dv
->writemostly
= writemostly
;
296 devlistend
= &dv
->next
;
301 /* No mode yet, and this is the second device ... */
302 pr_err("An option must be given to set the mode before a second device\n"
303 " (%s) is listed\n", optarg
);
306 if (option_index
>= 0)
307 pr_err("--%s", long_options
[option_index
].name
);
310 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
314 /* if we just set the mode, then done */
328 /* an undecorated option - must be a device name.
331 if (devs_found
> 0 && devmode
== DetailPlatform
) {
332 pr_err("controller may only be specified once. %s ignored\n",
337 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
338 pr_err("Must give one of -a/-r/-f"
339 " for subsequent devices at %s\n", optarg
);
342 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
343 pr_err("Must give -a/--add for"
344 " devices to add: %s\n", optarg
);
347 dv
= xmalloc(sizeof(*dv
));
348 dv
->devname
= optarg
;
349 dv
->disposition
= devmode
;
350 dv
->writemostly
= writemostly
;
354 devlistend
= &dv
->next
;
360 /* We've got a mode, and opt is now something else which
361 * could depend on the mode */
362 #define O(a,b) ((a<<16)|b)
363 switch (O(mode
,opt
)) {
365 case O(GROW
,ChunkSize
):
367 case O(CREATE
,ChunkSize
):
368 case O(BUILD
,'c'): /* chunk or rounding */
369 case O(BUILD
,ChunkSize
): /* chunk or rounding */
371 pr_err("chunk/rounding may only be specified once. "
372 "Second value is %s.\n", optarg
);
375 s
.chunk
= parse_size(optarg
);
376 if (s
.chunk
== INVALID_SECTORS
||
377 s
.chunk
< 8 || (s
.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 s
.size
= parse_size(optarg
);
431 if (s
.size
== INVALID_SECTORS
||
433 pr_err("invalid size: %s\n",
437 /* convert sectors to K */
442 case O(GROW
,'Z'): /* array size */
443 if (array_size
> 0) {
444 pr_err("array-size may only be specified once. "
445 "Second value is %s.\n", optarg
);
448 if (strcmp(optarg
, "max") == 0)
449 array_size
= MAX_SIZE
;
451 array_size
= parse_size(optarg
);
452 if (array_size
== 0 ||
453 array_size
== INVALID_SECTORS
) {
454 pr_err("invalid array size: %s\n",
461 case O(CREATE
,DataOffset
):
462 case O(GROW
,DataOffset
):
463 if (data_offset
!= INVALID_SECTORS
) {
464 fprintf(stderr
, Name
": data-offset may only be specified one. "
465 "Second value is %s.\n", optarg
);
468 data_offset
= parse_size(optarg
);
469 if (data_offset
== INVALID_SECTORS
) {
470 fprintf(stderr
, Name
": invalid data-offset: %s\n",
478 case O(BUILD
,'l'): /* set raid level*/
479 if (s
.level
!= UnSet
) {
480 pr_err("raid level may only be set once. "
481 "Second value is %s.\n", optarg
);
484 s
.level
= map_name(pers
, optarg
);
485 if (s
.level
== UnSet
) {
486 pr_err("invalid raid level: %s\n",
490 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
491 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
494 pr_err("Raid level %s not permitted with --build.\n",
498 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
499 pr_err("raid level %s is incompatible with spare-devices setting.\n",
503 ident
.level
= s
.level
;
506 case O(GROW
, 'p'): /* new layout */
507 case O(GROW
, Layout
):
509 pr_err("layout may only be sent once. "
510 "Second value was %s\n", optarg
);
513 s
.layout_str
= optarg
;
514 /* 'Grow' will parse the value */
517 case O(CREATE
,'p'): /* raid5 layout */
518 case O(CREATE
,Layout
):
519 case O(BUILD
,'p'): /* faulty layout */
520 case O(BUILD
,Layout
):
521 if (s
.layout
!= UnSet
) {
522 pr_err("layout may only be sent once. "
523 "Second value was %s\n", optarg
);
528 pr_err("layout not meaningful for %s arrays.\n",
529 map_num(pers
, s
.level
));
532 pr_err("raid level must be given before layout.\n");
536 s
.layout
= map_name(r5layout
, optarg
);
537 if (s
.layout
==UnSet
) {
538 pr_err("layout %s not understood for raid5.\n",
544 s
.layout
= map_name(r6layout
, optarg
);
545 if (s
.layout
==UnSet
) {
546 pr_err("layout %s not understood for raid6.\n",
553 s
.layout
= parse_layout_10(optarg
);
555 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
563 s
.layout
= parse_layout_faulty(optarg
);
564 if (s
.layout
== -1) {
565 pr_err("layout %s not understood for faulty.\n",
573 case O(CREATE
,AssumeClean
):
574 case O(BUILD
,AssumeClean
): /* assume clean */
575 case O(GROW
,AssumeClean
):
581 case O(BUILD
,'n'): /* number of raid disks */
583 pr_err("raid-devices set twice: %d and %s\n",
584 s
.raiddisks
, optarg
);
587 s
.raiddisks
= parse_num(optarg
);
588 if (s
.raiddisks
<= 0) {
589 pr_err("invalid number of raid devices: %s\n",
593 ident
.raid_disks
= s
.raiddisks
;
596 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
598 pr_err("spare-devices set twice: %d and %s\n",
599 s
.sparedisks
, optarg
);
602 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
603 pr_err("spare-devices setting is incompatible with raid level %d\n",
607 s
.sparedisks
= parse_num(optarg
);
608 if (s
.sparedisks
< 0) {
609 pr_err("invalid number of spare-devices: %s\n",
619 case O(INCREMENTAL
,'a'):
620 case O(INCREMENTAL
,Auto
):
621 case O(ASSEMBLE
,'a'):
622 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
623 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
626 case O(CREATE
,Symlinks
):
627 case O(BUILD
,Symlinks
):
628 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
632 case O(BUILD
,'f'): /* force honouring '-n 1' */
633 case O(BUILD
,Force
): /* force honouring '-n 1' */
634 case O(GROW
,'f'): /* ditto */
635 case O(GROW
,Force
): /* ditto */
636 case O(CREATE
,'f'): /* force honouring of device list */
637 case O(CREATE
,Force
): /* force honouring of device list */
638 case O(ASSEMBLE
,'f'): /* force assembly */
639 case O(ASSEMBLE
,Force
): /* force assembly */
640 case O(MISC
,'f'): /* force zero */
641 case O(MISC
,Force
): /* force zero */
642 case O(MANAGE
,Force
): /* add device which is too large */
645 /* now for the Assemble options */
646 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
648 case O(INCREMENTAL
, FreezeReshape
):
649 c
.freeze_reshape
= 1;
651 case O(CREATE
,'u'): /* uuid of array */
652 case O(ASSEMBLE
,'u'): /* uuid of array */
653 if (ident
.uuid_set
) {
654 pr_err("uuid cannot be set twice. "
655 "Second value %s.\n", optarg
);
658 if (parse_uuid(optarg
, ident
.uuid
))
661 pr_err("Bad uuid: %s\n", optarg
);
667 case O(ASSEMBLE
,'N'):
670 pr_err("name cannot be set twice. "
671 "Second value %s.\n", optarg
);
674 if (mode
== MISC
&& !c
.subarray
) {
675 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
678 if (strlen(optarg
) > 32) {
679 pr_err("name '%s' is too long, 32 chars max.\n",
683 strcpy(ident
.name
, optarg
);
686 case O(ASSEMBLE
,'m'): /* super-minor for array */
687 case O(ASSEMBLE
,SuperMinor
):
688 if (ident
.super_minor
!= UnSet
) {
689 pr_err("super-minor cannot be set twice. "
690 "Second value: %s.\n", optarg
);
693 if (strcmp(optarg
, "dev")==0)
694 ident
.super_minor
= -2;
696 ident
.super_minor
= parse_num(optarg
);
697 if (ident
.super_minor
< 0) {
698 pr_err("Bad super-minor number: %s.\n", optarg
);
704 case O(ASSEMBLE
,'o'):
710 case O(ASSEMBLE
,'U'): /* update the superblock */
713 pr_err("Can only update one aspect"
714 " of superblock, both %s and %s given.\n",
718 if (mode
== MISC
&& !c
.subarray
) {
719 pr_err("Only subarrays can be"
720 " updated in misc mode\n");
724 if (strcmp(c
.update
, "sparc2.2")==0)
726 if (strcmp(c
.update
, "super-minor") == 0)
728 if (strcmp(c
.update
, "summaries")==0)
730 if (strcmp(c
.update
, "resync")==0)
732 if (strcmp(c
.update
, "uuid")==0)
734 if (strcmp(c
.update
, "name")==0)
736 if (strcmp(c
.update
, "homehost")==0)
738 if (strcmp(c
.update
, "devicesize")==0)
740 if (strcmp(c
.update
, "no-bitmap")==0)
742 if (strcmp(c
.update
, "bbl") == 0)
744 if (strcmp(c
.update
, "no-bbl") == 0)
746 if (strcmp(c
.update
, "byteorder")==0) {
748 pr_err("must not set metadata"
749 " type with --update=byteorder.\n");
752 for(i
=0; !ss
&& superlist
[i
]; i
++)
753 ss
= superlist
[i
]->match_metadata_desc(
756 pr_err("INTERNAL ERROR"
757 " cannot find 0.swap\n");
763 if (strcmp(c
.update
,"?") == 0 ||
764 strcmp(c
.update
, "help") == 0) {
766 fprintf(outf
, Name
": ");
770 Name
": '--update=%s' is invalid. ",
773 fprintf(outf
, "Valid --update options are:\n"
774 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
775 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
777 exit(outf
== stdout
? 0 : 2);
780 /* update=devicesize is allowed with --re-add */
781 if (devmode
!= 'A') {
782 pr_err("--update in Manage mode only"
783 " allowed with --re-add.\n");
787 pr_err("Can only update one aspect"
788 " of superblock, both %s and %s given.\n",
793 if (strcmp(c
.update
, "devicesize") != 0 &&
794 strcmp(c
.update
, "bbl") != 0 &&
795 strcmp(c
.update
, "no-bbl") != 0) {
796 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be"
797 " updated with --re-add\n");
802 case O(INCREMENTAL
,NoDegraded
):
803 pr_err("--no-degraded is deprecated in Incremental mode\n");
804 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
805 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
806 * so we overload slightly */
809 case O(ASSEMBLE
,'c'):
810 case O(ASSEMBLE
,ConfigFile
):
811 case O(INCREMENTAL
, 'c'):
812 case O(INCREMENTAL
, ConfigFile
):
814 case O(MISC
, ConfigFile
):
816 case O(MONITOR
,ConfigFile
):
818 pr_err("configfile cannot be set twice. "
819 "Second value is %s.\n", optarg
);
823 set_conffile(configfile
);
824 /* FIXME possibly check that config file exists. Even parse it */
826 case O(ASSEMBLE
,'s'): /* scan */
829 case O(INCREMENTAL
,'s'):
833 case O(MONITOR
,'m'): /* mail address */
834 case O(MONITOR
,EMail
):
836 pr_err("only specify one mailaddress. %s ignored.\n",
842 case O(MONITOR
,'p'): /* alert program */
843 case O(MONITOR
,ProgramOpt
): /* alert program */
845 pr_err("only specify one alter program. %s ignored.\n",
851 case O(MONITOR
,'r'): /* rebuild increments */
852 case O(MONITOR
,Increment
):
853 increments
= atoi(optarg
);
854 if (increments
> 99 || increments
< 1) {
855 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
860 case O(MONITOR
,'d'): /* delay in seconds */
862 case O(BUILD
,'d'): /* delay for bitmap updates */
865 pr_err("only specify delay once. %s ignored.\n",
868 c
.delay
= parse_num(optarg
);
870 pr_err("invalid delay: %s\n",
876 case O(MONITOR
,'f'): /* daemonise */
877 case O(MONITOR
,Fork
):
880 case O(MONITOR
,'i'): /* pid */
882 pr_err("only specify one pid file. %s ignored.\n",
887 case O(MONITOR
,'1'): /* oneshot */
891 case O(MONITOR
,'t'): /* test */
894 case O(MONITOR
,'y'): /* log messages to syslog */
895 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
898 case O(MONITOR
, NoSharing
):
902 /* now the general management options. Some are applicable
903 * to other modes. None have arguments.
908 case O(MANAGE
,Add
): /* add a drive */
911 case O(MANAGE
,ReAdd
):
914 case O(MANAGE
,'r'): /* remove a drive */
915 case O(MANAGE
,Remove
):
918 case O(MANAGE
,'f'): /* set faulty */
920 case O(INCREMENTAL
,'f'):
921 case O(INCREMENTAL
,Remove
):
922 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
923 * even though we will both fail and
924 * remove the device */
927 case O(INCREMENTAL
,'R'):
929 case O(ASSEMBLE
,'R'):
931 case O(CREATE
,'R'): /* Run the array */
933 pr_err("Cannot both Stop and Run an array\n");
940 pr_err("Cannot both Run and Stop an array\n");
952 case O(MISC
,KillOpt
):
959 case O(MISC
, WaitOpt
):
960 case O(MISC
, Waitclean
):
961 case O(MISC
, DetailPlatform
):
962 case O(MISC
, KillSubarray
):
963 case O(MISC
, UpdateSubarray
):
964 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
966 pr_err("subarray can only"
967 " be specified once\n");
972 if (devmode
&& devmode
!= opt
&&
973 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
974 pr_err("--examine/-E cannot be given with ");
975 if (devmode
== 'E') {
976 if (option_index
>= 0)
977 fprintf(stderr
, "--%s\n",
978 long_options
[option_index
].name
);
980 fprintf(stderr
, "-%c\n", opt
);
981 } else if (isalpha(devmode
))
982 fprintf(stderr
, "-%c\n", devmode
);
984 fprintf(stderr
, "previous option\n");
989 case O(MISC
, UdevRules
):
990 if (devmode
&& devmode
!= opt
) {
991 pr_err("--udev-rules must"
992 " be the only option.\n");
995 pr_err("only specify one udev "
996 "rule filename. %s ignored.\n",
999 udev_filename
= optarg
;
1007 case O(MISC
, Sparc22
):
1008 if (devmode
!= 'E') {
1009 pr_err("--sparc2.2 only allowed with --examine\n");
1015 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1016 case O(ASSEMBLE
,Bitmap
):
1018 pr_err("bitmap file needed with -b in --assemble mode\n");
1021 if (strcmp(optarg
, "internal")==0) {
1022 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1025 bitmap_fd
= open(optarg
, O_RDWR
);
1026 if (!*optarg
|| bitmap_fd
< 0) {
1027 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1030 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1033 case O(ASSEMBLE
, BackupFile
):
1034 case O(GROW
, BackupFile
):
1035 /* Specify a file into which grow might place a backup,
1036 * or from which assemble might recover a backup
1038 if (c
.backup_file
) {
1039 pr_err("backup file already specified, rejecting %s\n", optarg
);
1042 c
.backup_file
= optarg
;
1045 case O(GROW
, Continue
):
1046 /* Continue interrupted grow
1050 case O(ASSEMBLE
, InvalidBackup
):
1051 /* Acknowledge that the backupfile is invalid, but ask
1052 * to continue anyway
1054 c
.invalid_backup
= 1;
1058 case O(BUILD
,Bitmap
):
1060 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1061 if (strcmp(optarg
, "none") == 0) {
1062 pr_err("'--bitmap none' only"
1063 " supported for --grow\n");
1068 case O(GROW
,Bitmap
):
1069 if (strcmp(optarg
, "internal")== 0 ||
1070 strcmp(optarg
, "none")== 0 ||
1071 strchr(optarg
, '/') != NULL
) {
1072 s
.bitmap_file
= optarg
;
1076 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1077 " not '%s'\n", optarg
);
1080 case O(GROW
,BitmapChunk
):
1081 case O(BUILD
,BitmapChunk
):
1082 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1083 s
.bitmap_chunk
= parse_size(optarg
);
1084 if (s
.bitmap_chunk
== 0 ||
1085 s
.bitmap_chunk
== INVALID_SECTORS
||
1086 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1087 pr_err("invalid bitmap chunksize: %s\n",
1091 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1094 case O(GROW
, WriteBehind
):
1095 case O(BUILD
, WriteBehind
):
1096 case O(CREATE
, WriteBehind
): /* write-behind mode */
1097 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1099 s
.write_behind
= parse_num(optarg
);
1100 if (s
.write_behind
< 0 ||
1101 s
.write_behind
> 16383) {
1102 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1108 case O(INCREMENTAL
, 'r'):
1109 case O(INCREMENTAL
, RebuildMapOpt
):
1112 case O(INCREMENTAL
, IncrementalPath
):
1113 remove_path
= optarg
;
1116 /* We have now processed all the valid options. Anything else is
1119 if (option_index
> 0)
1120 pr_err(":option --%s not valid in %s mode\n",
1121 long_options
[option_index
].name
,
1122 map_num(modes
, mode
));
1124 pr_err("option -%c not valid in %s mode\n",
1125 opt
, map_num(modes
, mode
));
1132 if (print_help
== 2)
1133 help_text
= OptionHelp
;
1135 help_text
= mode_help
[mode
];
1136 if (help_text
== NULL
)
1138 fputs(help_text
,stdout
);
1142 if (!mode
&& devs_found
) {
1145 if (devlist
->disposition
== 0)
1146 devlist
->disposition
= devmode
;
1149 fputs(Usage
, stderr
);
1154 struct createinfo
*ci
= conf_get_create_info();
1156 if (strcasecmp(symlinks
, "yes") == 0)
1158 else if (strcasecmp(symlinks
, "no") == 0)
1161 pr_err("option --symlinks must be 'no' or 'yes'\n");
1165 /* Ok, got the option parsing out of the way
1166 * hopefully it's mostly right but there might be some stuff
1169 * That is mosty checked in the per-mode stuff but...
1171 * For @,B,C and A without -s, the first device listed must be
1172 * an md device. We check that here and open it.
1175 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1177 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1178 if (devs_found
< 1) {
1179 pr_err("an md device must be given in this mode\n");
1182 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1183 pr_err("--super-minor=dev is incompatible with --auto\n");
1186 if (mode
== MANAGE
|| mode
== GROW
) {
1187 mdfd
= open_mddev(devlist
->devname
, 1);
1191 /* non-existent device is OK */
1192 mdfd
= open_mddev(devlist
->devname
, 0);
1194 pr_err("device %s exists but is not an "
1195 "md array.\n", devlist
->devname
);
1198 if ((int)ident
.super_minor
== -2) {
1201 pr_err("--super-minor=dev given, and "
1202 "listed device %s doesn't exist.\n",
1207 ident
.super_minor
= minor(stb
.st_rdev
);
1209 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1210 /* We don't really want this open yet, we just might
1211 * have wanted to check some things
1219 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1220 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1221 " a mistake. If you really mean it you will need to specify --force before\n"
1222 " setting the number of drives.\n");
1227 if (c
.homehost
== NULL
)
1228 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1229 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1230 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1231 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1232 c
.homehost
= sys_hostname
;
1235 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1237 c
.require_homehost
= 0;
1240 if ((mode
== MISC
&& devmode
== 'E')
1241 || (mode
== MONITOR
&& spare_sharing
== 0))
1242 /* Anyone may try this */;
1243 else if (geteuid() != 0) {
1244 pr_err("must be super-user to perform this action\n");
1248 ident
.autof
= c
.autof
;
1250 if (c
.scan
&& c
.verbose
< 2)
1251 /* --scan implied --brief unless -vv */
1257 /* readonly, add/remove, readwrite, runstop */
1259 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1260 if (!rv
&& devs_found
>1)
1261 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1262 devlist
->next
, c
.verbose
, c
.test
,
1264 if (!rv
&& c
.readonly
< 0)
1265 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1266 if (!rv
&& c
.runstop
)
1267 rv
= Manage_runstop(devlist
->devname
, mdfd
, c
.runstop
, c
.verbose
, 0);
1270 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1271 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1272 /* Only a device has been given, so get details from config file */
1273 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1274 if (array_ident
== NULL
) {
1275 pr_err("%s not identified in config file.\n",
1281 if (array_ident
->autof
== 0)
1282 array_ident
->autof
= c
.autof
;
1283 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1287 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1289 else if (devs_found
> 0) {
1290 if (c
.update
&& devs_found
> 1) {
1291 pr_err("can only update a single array at a time\n");
1294 if (c
.backup_file
&& devs_found
> 1) {
1295 pr_err("can only assemble a single array when providing a backup file.\n");
1298 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1299 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1300 if (array_ident
== NULL
) {
1301 pr_err("%s not identified in config file.\n",
1306 if (array_ident
->autof
== 0)
1307 array_ident
->autof
= c
.autof
;
1308 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1313 pr_err("--update not meaningful with a --scan assembly.\n");
1316 if (c
.backup_file
) {
1317 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1320 rv
= scan_assemble(ss
, &c
, &ident
);
1326 c
.delay
= DEFAULT_BITMAP_DELAY
;
1327 if (s
.write_behind
&& !s
.bitmap_file
) {
1328 pr_err("write-behind mode requires a bitmap.\n");
1332 if (s
.raiddisks
== 0) {
1333 pr_err("no raid-devices specified.\n");
1338 if (s
.bitmap_file
) {
1339 if (strcmp(s
.bitmap_file
, "internal")==0) {
1340 pr_err("'internal' bitmaps not supported with --build\n");
1345 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1349 c
.delay
= DEFAULT_BITMAP_DELAY
;
1350 if (s
.write_behind
&& !s
.bitmap_file
) {
1351 pr_err("write-behind mode requires a bitmap.\n");
1355 if (s
.raiddisks
== 0) {
1356 pr_err("no raid-devices specified.\n");
1361 rv
= Create(ss
, devlist
->devname
,
1362 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1363 devs_found
-1, devlist
->next
,
1364 &s
, &c
, data_offset
);
1367 if (devmode
== 'E') {
1368 if (devlist
== NULL
&& !c
.scan
) {
1369 pr_err("No devices to examine\n");
1372 if (devlist
== NULL
)
1373 devlist
= conf_get_devs();
1374 if (devlist
== NULL
) {
1375 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1378 rv
= Examine(devlist
, &c
, ss
);
1379 } else if (devmode
== DetailPlatform
) {
1380 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1381 c
.verbose
, c
.export
,
1382 devlist
? devlist
->devname
: NULL
);
1383 } else if (devlist
== NULL
) {
1384 if (devmode
== 'S' && c
.scan
)
1385 rv
= stop_scan(c
.verbose
);
1386 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1387 rv
= misc_scan(devmode
, &c
);
1388 else if (devmode
== UdevRules
)
1389 rv
= Write_rules(udev_filename
);
1391 pr_err("No devices given.\n");
1395 rv
= misc_list(devlist
, &ident
, ss
, &c
);
1398 if (!devlist
&& !c
.scan
) {
1399 pr_err("Cannot monitor: need --scan or at least one device\n");
1403 if (pidfile
&& !daemonise
) {
1404 pr_err("Cannot write a pid file when not in daemon mode\n");
1409 if (get_linux_version() > 2006016)
1410 /* mdstat responds to poll */
1417 rv
= Monitor(devlist
, mailaddr
, program
,
1418 &c
, daemonise
, oneshot
,
1419 dosyslog
, pidfile
, increments
,
1424 if (array_size
> 0) {
1425 /* alway impose array size first, independent of
1427 * Do not allow level or raid_disks changes at the
1428 * same time as that can be irreversibly destructive.
1432 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1433 pr_err("cannot change array size in same operation "
1434 "as changing raiddisks or level.\n"
1435 " Change size first, then check that data is still intact.\n");
1439 sysfs_init(&sra
, mdfd
, 0);
1440 if (array_size
== MAX_SIZE
)
1441 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1443 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1446 pr_err("--array-size setting"
1447 " is too large.\n");
1449 pr_err("current kernel does"
1450 " not support setting --array-size\n");
1455 if (devs_found
> 1 && s
.raiddisks
== 0) {
1457 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1458 pr_err("--add cannot be used with "
1459 "other geometry changes in --grow mode\n");
1463 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1464 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1469 } else if (s
.bitmap_file
) {
1470 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1471 s
.layout_str
!= NULL
|| devs_found
> 1) {
1472 pr_err("--bitmap changes cannot be "
1473 "used with other geometry changes "
1474 "in --grow mode\n");
1479 c
.delay
= DEFAULT_BITMAP_DELAY
;
1480 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1481 } else if (grow_continue
)
1482 rv
= Grow_continue_command(devlist
->devname
,
1483 mdfd
, c
.backup_file
,
1485 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1486 || s
.chunk
!= 0 || s
.level
!= UnSet
) {
1487 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1489 data_offset
, &c
, &s
);
1490 } else if (array_size
== 0)
1491 pr_err("no changes to --grow\n");
1498 if (c
.runstop
<= 0) {
1499 pr_err("--incremental --scan meaningless without --run.\n");
1502 if (devmode
== 'f') {
1503 pr_err("--incremental --scan --fail not supported.\n");
1506 rv
= IncrementalScan(c
.verbose
);
1509 if (!rebuild_map
&& !c
.scan
) {
1510 pr_err("--incremental requires a device.\n");
1515 if (devlist
->next
) {
1516 pr_err("--incremental can only handle one device.\n");
1521 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1524 rv
= Incremental(devlist
->devname
, &c
, ss
);
1533 static int scan_assemble(struct supertype
*ss
,
1535 struct mddev_ident
*ident
)
1537 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1538 struct mddev_dev
*devlist
= conf_get_devs();
1539 struct map_ent
*map
= NULL
;
1542 int failures
, successes
;
1544 if (conf_verify_devnames(array_list
)) {
1545 pr_err("Duplicate MD device names in "
1546 "conf file were found.\n");
1549 if (devlist
== NULL
) {
1550 pr_err("No devices listed in conf file were found.\n");
1553 for (a
= array_list
; a
; a
= a
->next
) {
1556 a
->autof
= c
->autof
;
1559 pr_err("%s: failed to get "
1560 "exclusive lock on mapfile\n",
1566 for (a
= array_list
; a
; a
= a
->next
) {
1571 strcasecmp(a
->devname
, "<ignore>") == 0)
1574 r
= Assemble(ss
, a
->devname
,
1584 } while (failures
&& successes
);
1585 if (c
->homehost
&& cnt
== 0) {
1586 /* Maybe we can auto-assemble something.
1587 * Repeatedly call Assemble in auto-assemble mode
1592 ident
->autof
= c
->autof
;
1594 struct mddev_dev
*devlist
= conf_get_devs();
1597 rv2
= Assemble(ss
, NULL
,
1605 /* Incase there are stacked devices, we need to go around again */
1607 if (cnt
== 0 && rv
== 0) {
1608 pr_err("No arrays found in config file or automatically\n");
1612 } else if (cnt
== 0 && rv
== 0) {
1613 pr_err("No arrays found in config file\n");
1620 static int misc_scan(char devmode
, struct context
*c
)
1622 /* apply --detail or --wait-clean to
1623 * all devices in /proc/mdstat
1625 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1626 struct mdstat_ent
*e
;
1627 struct map_ent
*map
= NULL
;
1631 for (members
= 0; members
<= 1; members
++) {
1632 for (e
=ms
; e
; e
=e
->next
) {
1635 int member
= e
->metadata_version
&&
1636 strncmp(e
->metadata_version
,
1637 "external:/", 10) == 0;
1638 if (members
!= member
)
1640 me
= map_by_devnum(&map
, e
->devnum
);
1642 && strcmp(me
->path
, "/unknown") != 0)
1645 name
= get_md_name(e
->devnum
);
1648 pr_err("cannot find device file for %s\n",
1653 rv
|= Detail(name
, c
);
1655 rv
|= WaitClean(name
, -1, c
->verbose
);
1663 static int stop_scan(int verbose
)
1665 /* apply --stop to all devices in /proc/mdstat */
1666 /* Due to possible stacking of devices, repeat until
1667 * nothing more can be stopped
1669 int progress
=1, err
;
1673 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1674 struct mdstat_ent
*e
;
1676 if (!progress
) last
= 1;
1677 progress
= 0; err
= 0;
1678 for (e
=ms
; e
; e
=e
->next
) {
1679 char *name
= get_md_name(e
->devnum
);
1683 pr_err("cannot find device file for %s\n",
1687 mdfd
= open_mddev(name
, 1);
1689 if (Manage_runstop(name
, mdfd
, -1, verbose
, !last
))
1699 } while (!last
&& err
);
1705 static int misc_list(struct mddev_dev
*devlist
,
1706 struct mddev_ident
*ident
,
1707 struct supertype
*ss
, struct context
*c
)
1709 struct mddev_dev
*dv
;
1712 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1715 switch(dv
->disposition
) {
1717 rv
|= Detail(dv
->devname
, c
);
1719 case KillOpt
: /* Zero superblock */
1721 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1725 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1732 rv
|= Query(dv
->devname
); continue;
1734 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1737 rv
|= Wait(dv
->devname
); continue;
1739 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1741 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1743 case UpdateSubarray
:
1744 if (c
->update
== NULL
) {
1745 pr_err("-U/--update must be specified with --update-subarray\n");
1749 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1750 c
->update
, ident
, c
->verbose
);
1753 mdfd
= open_mddev(dv
->devname
, 1);
1755 switch(dv
->disposition
) {
1757 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, c
->verbose
, 0); break;
1759 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, c
->verbose
, 0); break;
1761 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1763 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;