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
;
120 * set first char of argv[0] to @. This is used by
121 * systemd to signal that the task was launched from
122 * initrd/initramfs and should be preserved during shutdown
126 while ((option_index
= -1) ,
127 (opt
=getopt_long(argc
, argv
,
128 shortopt
, long_options
,
129 &option_index
)) != -1) {
131 /* firstly, some mode-independent options */
141 fputs(Version
, stderr
);
144 case 'v': c
.verbose
++;
147 case 'q': c
.verbose
--;
151 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
152 || mode
== GROW
|| mode
== INCREMENTAL
154 break; /* b means bitmap */
159 case 'Y': c
.export
++;
163 if (strcasecmp(optarg
, "<ignore>") == 0)
164 c
.require_homehost
= 0;
170 /* Silently ignore old option */
176 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
182 fputs(Usage
, stderr
);
185 /* second, figure out the mode.
186 * Some options force the mode. Others
187 * set the mode if it isn't already
193 shortopt
= short_bitmap_options
;
203 case ReAdd
: /* re-add */
206 shortopt
= short_bitmap_options
;
210 case 'A': newmode
= ASSEMBLE
;
211 shortopt
= short_bitmap_auto_options
;
213 case 'B': newmode
= BUILD
;
214 shortopt
= short_bitmap_auto_options
;
216 case 'C': newmode
= CREATE
;
217 shortopt
= short_bitmap_auto_options
;
219 case 'F': newmode
= MONITOR
;
221 case 'G': newmode
= GROW
;
222 shortopt
= short_bitmap_options
;
224 case 'I': newmode
= INCREMENTAL
;
225 shortopt
= short_bitmap_auto_options
;
228 newmode
= AUTODETECT
;
260 if (mode
&& newmode
== mode
) {
261 /* everybody happy ! */
262 } else if (mode
&& newmode
!= mode
) {
265 if (option_index
>= 0)
266 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
268 fprintf(stderr
, "-%c", opt
);
269 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
270 map_num(modes
, newmode
),
271 map_num(modes
, mode
));
273 } else if (!mode
&& newmode
) {
275 if (mode
== MISC
&& devs_found
) {
276 pr_err("No action given for %s in --misc mode\n",
278 fprintf(stderr
," Action options must come before device names\n");
282 /* special case of -c --help */
283 if ((opt
== 'c' || opt
== ConfigFile
) &&
284 ( strncmp(optarg
, "--h", 3)==0 ||
285 strncmp(optarg
, "-h", 2)==0)) {
286 fputs(Help_config
, stdout
);
290 /* If first option is a device, don't force the mode yet */
292 if (devs_found
== 0) {
293 dv
= xmalloc(sizeof(*dv
));
294 dv
->devname
= optarg
;
295 dv
->disposition
= devmode
;
296 dv
->writemostly
= writemostly
;
300 devlistend
= &dv
->next
;
305 /* No mode yet, and this is the second device ... */
306 pr_err("An option must be given to set the mode before a second device\n"
307 " (%s) is listed\n", optarg
);
310 if (option_index
>= 0)
311 pr_err("--%s", long_options
[option_index
].name
);
314 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
318 /* if we just set the mode, then done */
332 /* an undecorated option - must be a device name.
335 if (devs_found
> 0 && devmode
== DetailPlatform
) {
336 pr_err("controller may only be specified once. %s ignored\n",
341 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
342 pr_err("Must give one of -a/-r/-f"
343 " for subsequent devices at %s\n", optarg
);
346 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
347 pr_err("Must give -a/--add for"
348 " devices to add: %s\n", optarg
);
351 dv
= xmalloc(sizeof(*dv
));
352 dv
->devname
= optarg
;
353 dv
->disposition
= devmode
;
354 dv
->writemostly
= writemostly
;
358 devlistend
= &dv
->next
;
364 /* We've got a mode, and opt is now something else which
365 * could depend on the mode */
366 #define O(a,b) ((a<<16)|b)
367 switch (O(mode
,opt
)) {
369 case O(GROW
,ChunkSize
):
371 case O(CREATE
,ChunkSize
):
372 case O(BUILD
,'c'): /* chunk or rounding */
373 case O(BUILD
,ChunkSize
): /* chunk or rounding */
375 pr_err("chunk/rounding may only be specified once. "
376 "Second value is %s.\n", optarg
);
379 s
.chunk
= parse_size(optarg
);
380 if (s
.chunk
== INVALID_SECTORS
||
381 s
.chunk
< 8 || (s
.chunk
&1)) {
382 pr_err("invalid chunk/rounding value: %s\n",
386 /* Convert sectors to K */
390 case O(INCREMENTAL
, 'e'):
392 case O(ASSEMBLE
,'e'):
393 case O(MISC
,'e'): /* set metadata (superblock) information */
395 pr_err("metadata information already given\n");
398 for(i
=0; !ss
&& superlist
[i
]; i
++)
399 ss
= superlist
[i
]->match_metadata_desc(optarg
);
402 pr_err("unrecognised metadata identifier: %s\n", optarg
);
408 case O(MANAGE
,WriteMostly
):
410 case O(BUILD
,WriteMostly
):
412 case O(CREATE
,WriteMostly
):
413 /* set write-mostly for following devices */
418 /* clear write-mostly for following devices */
425 case O(BUILD
,'z'): /* size */
427 pr_err("size may only be specified once. "
428 "Second value is %s.\n", optarg
);
431 if (strcmp(optarg
, "max")==0)
434 s
.size
= parse_size(optarg
);
435 if (s
.size
== INVALID_SECTORS
||
437 pr_err("invalid size: %s\n",
441 /* convert sectors to K */
446 case O(GROW
,'Z'): /* array size */
447 if (array_size
> 0) {
448 pr_err("array-size may only be specified once. "
449 "Second value is %s.\n", optarg
);
452 if (strcmp(optarg
, "max") == 0)
453 array_size
= MAX_SIZE
;
455 array_size
= parse_size(optarg
);
456 if (array_size
== 0 ||
457 array_size
== INVALID_SECTORS
) {
458 pr_err("invalid array size: %s\n",
465 case O(CREATE
,DataOffset
):
466 case O(GROW
,DataOffset
):
467 if (data_offset
!= INVALID_SECTORS
) {
468 fprintf(stderr
, Name
": data-offset may only be specified one. "
469 "Second value is %s.\n", optarg
);
472 if (mode
== CREATE
&&
473 strcmp(optarg
, "variable") == 0)
474 data_offset
= VARIABLE_OFFSET
;
476 data_offset
= parse_size(optarg
);
477 if (data_offset
== INVALID_SECTORS
) {
478 fprintf(stderr
, Name
": invalid data-offset: %s\n",
486 case O(BUILD
,'l'): /* set raid level*/
487 if (s
.level
!= UnSet
) {
488 pr_err("raid level may only be set once. "
489 "Second value is %s.\n", optarg
);
492 s
.level
= map_name(pers
, optarg
);
493 if (s
.level
== UnSet
) {
494 pr_err("invalid raid level: %s\n",
498 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
499 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
502 pr_err("Raid level %s not permitted with --build.\n",
506 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
507 pr_err("raid level %s is incompatible with spare-devices setting.\n",
511 ident
.level
= s
.level
;
514 case O(GROW
, 'p'): /* new layout */
515 case O(GROW
, Layout
):
517 pr_err("layout may only be sent once. "
518 "Second value was %s\n", optarg
);
521 s
.layout_str
= optarg
;
522 /* 'Grow' will parse the value */
525 case O(CREATE
,'p'): /* raid5 layout */
526 case O(CREATE
,Layout
):
527 case O(BUILD
,'p'): /* faulty layout */
528 case O(BUILD
,Layout
):
529 if (s
.layout
!= UnSet
) {
530 pr_err("layout may only be sent once. "
531 "Second value was %s\n", optarg
);
536 pr_err("layout not meaningful for %s arrays.\n",
537 map_num(pers
, s
.level
));
540 pr_err("raid level must be given before layout.\n");
544 s
.layout
= map_name(r5layout
, optarg
);
545 if (s
.layout
==UnSet
) {
546 pr_err("layout %s not understood for raid5.\n",
552 s
.layout
= map_name(r6layout
, optarg
);
553 if (s
.layout
==UnSet
) {
554 pr_err("layout %s not understood for raid6.\n",
561 s
.layout
= parse_layout_10(optarg
);
563 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
571 s
.layout
= parse_layout_faulty(optarg
);
572 if (s
.layout
== -1) {
573 pr_err("layout %s not understood for faulty.\n",
581 case O(CREATE
,AssumeClean
):
582 case O(BUILD
,AssumeClean
): /* assume clean */
583 case O(GROW
,AssumeClean
):
589 case O(BUILD
,'n'): /* number of raid disks */
591 pr_err("raid-devices set twice: %d and %s\n",
592 s
.raiddisks
, optarg
);
595 s
.raiddisks
= parse_num(optarg
);
596 if (s
.raiddisks
<= 0) {
597 pr_err("invalid number of raid devices: %s\n",
601 ident
.raid_disks
= s
.raiddisks
;
604 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
606 pr_err("spare-devices set twice: %d and %s\n",
607 s
.sparedisks
, optarg
);
610 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
611 pr_err("spare-devices setting is incompatible with raid level %d\n",
615 s
.sparedisks
= parse_num(optarg
);
616 if (s
.sparedisks
< 0) {
617 pr_err("invalid number of spare-devices: %s\n",
627 case O(INCREMENTAL
,'a'):
628 case O(INCREMENTAL
,Auto
):
629 case O(ASSEMBLE
,'a'):
630 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
631 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
634 case O(CREATE
,Symlinks
):
635 case O(BUILD
,Symlinks
):
636 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
640 case O(BUILD
,'f'): /* force honouring '-n 1' */
641 case O(BUILD
,Force
): /* force honouring '-n 1' */
642 case O(GROW
,'f'): /* ditto */
643 case O(GROW
,Force
): /* ditto */
644 case O(CREATE
,'f'): /* force honouring of device list */
645 case O(CREATE
,Force
): /* force honouring of device list */
646 case O(ASSEMBLE
,'f'): /* force assembly */
647 case O(ASSEMBLE
,Force
): /* force assembly */
648 case O(MISC
,'f'): /* force zero */
649 case O(MISC
,Force
): /* force zero */
650 case O(MANAGE
,Force
): /* add device which is too large */
653 /* now for the Assemble options */
654 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
656 case O(INCREMENTAL
, FreezeReshape
):
657 c
.freeze_reshape
= 1;
659 case O(CREATE
,'u'): /* uuid of array */
660 case O(ASSEMBLE
,'u'): /* uuid of array */
661 if (ident
.uuid_set
) {
662 pr_err("uuid cannot be set twice. "
663 "Second value %s.\n", optarg
);
666 if (parse_uuid(optarg
, ident
.uuid
))
669 pr_err("Bad uuid: %s\n", optarg
);
675 case O(ASSEMBLE
,'N'):
678 pr_err("name cannot be set twice. "
679 "Second value %s.\n", optarg
);
682 if (mode
== MISC
&& !c
.subarray
) {
683 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
686 if (strlen(optarg
) > 32) {
687 pr_err("name '%s' is too long, 32 chars max.\n",
691 strcpy(ident
.name
, optarg
);
694 case O(ASSEMBLE
,'m'): /* super-minor for array */
695 case O(ASSEMBLE
,SuperMinor
):
696 if (ident
.super_minor
!= UnSet
) {
697 pr_err("super-minor cannot be set twice. "
698 "Second value: %s.\n", optarg
);
701 if (strcmp(optarg
, "dev")==0)
702 ident
.super_minor
= -2;
704 ident
.super_minor
= parse_num(optarg
);
705 if (ident
.super_minor
< 0) {
706 pr_err("Bad super-minor number: %s.\n", optarg
);
712 case O(ASSEMBLE
,'o'):
718 case O(ASSEMBLE
,'U'): /* update the superblock */
721 pr_err("Can only update one aspect"
722 " of superblock, both %s and %s given.\n",
726 if (mode
== MISC
&& !c
.subarray
) {
727 pr_err("Only subarrays can be"
728 " updated in misc mode\n");
732 if (strcmp(c
.update
, "sparc2.2")==0)
734 if (strcmp(c
.update
, "super-minor") == 0)
736 if (strcmp(c
.update
, "summaries")==0)
738 if (strcmp(c
.update
, "resync")==0)
740 if (strcmp(c
.update
, "uuid")==0)
742 if (strcmp(c
.update
, "name")==0)
744 if (strcmp(c
.update
, "homehost")==0)
746 if (strcmp(c
.update
, "devicesize")==0)
748 if (strcmp(c
.update
, "no-bitmap")==0)
750 if (strcmp(c
.update
, "bbl") == 0)
752 if (strcmp(c
.update
, "no-bbl") == 0)
754 if (strcmp(c
.update
, "byteorder")==0) {
756 pr_err("must not set metadata"
757 " type with --update=byteorder.\n");
760 for(i
=0; !ss
&& superlist
[i
]; i
++)
761 ss
= superlist
[i
]->match_metadata_desc(
764 pr_err("INTERNAL ERROR"
765 " cannot find 0.swap\n");
771 if (strcmp(c
.update
,"?") == 0 ||
772 strcmp(c
.update
, "help") == 0) {
774 fprintf(outf
, Name
": ");
778 Name
": '--update=%s' is invalid. ",
781 fprintf(outf
, "Valid --update options are:\n"
782 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
783 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
785 exit(outf
== stdout
? 0 : 2);
788 /* update=devicesize is allowed with --re-add */
789 if (devmode
!= 'A') {
790 pr_err("--update in Manage mode only"
791 " allowed with --re-add.\n");
795 pr_err("Can only update one aspect"
796 " of superblock, both %s and %s given.\n",
801 if (strcmp(c
.update
, "devicesize") != 0 &&
802 strcmp(c
.update
, "bbl") != 0 &&
803 strcmp(c
.update
, "no-bbl") != 0) {
804 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be"
805 " updated with --re-add\n");
810 case O(INCREMENTAL
,NoDegraded
):
811 pr_err("--no-degraded is deprecated in Incremental mode\n");
812 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
813 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
814 * so we overload slightly */
817 case O(ASSEMBLE
,'c'):
818 case O(ASSEMBLE
,ConfigFile
):
819 case O(INCREMENTAL
, 'c'):
820 case O(INCREMENTAL
, ConfigFile
):
822 case O(MISC
, ConfigFile
):
824 case O(MONITOR
,ConfigFile
):
826 pr_err("configfile cannot be set twice. "
827 "Second value is %s.\n", optarg
);
831 set_conffile(configfile
);
832 /* FIXME possibly check that config file exists. Even parse it */
834 case O(ASSEMBLE
,'s'): /* scan */
837 case O(INCREMENTAL
,'s'):
841 case O(MONITOR
,'m'): /* mail address */
842 case O(MONITOR
,EMail
):
844 pr_err("only specify one mailaddress. %s ignored.\n",
850 case O(MONITOR
,'p'): /* alert program */
851 case O(MONITOR
,ProgramOpt
): /* alert program */
853 pr_err("only specify one alter program. %s ignored.\n",
859 case O(MONITOR
,'r'): /* rebuild increments */
860 case O(MONITOR
,Increment
):
861 increments
= atoi(optarg
);
862 if (increments
> 99 || increments
< 1) {
863 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
868 case O(MONITOR
,'d'): /* delay in seconds */
870 case O(BUILD
,'d'): /* delay for bitmap updates */
873 pr_err("only specify delay once. %s ignored.\n",
876 c
.delay
= parse_num(optarg
);
878 pr_err("invalid delay: %s\n",
884 case O(MONITOR
,'f'): /* daemonise */
885 case O(MONITOR
,Fork
):
888 case O(MONITOR
,'i'): /* pid */
890 pr_err("only specify one pid file. %s ignored.\n",
895 case O(MONITOR
,'1'): /* oneshot */
899 case O(MONITOR
,'t'): /* test */
902 case O(MONITOR
,'y'): /* log messages to syslog */
903 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
906 case O(MONITOR
, NoSharing
):
910 /* now the general management options. Some are applicable
911 * to other modes. None have arguments.
916 case O(MANAGE
,Add
): /* add a drive */
919 case O(MANAGE
,ReAdd
):
922 case O(MANAGE
,'r'): /* remove a drive */
923 case O(MANAGE
,Remove
):
926 case O(MANAGE
,'f'): /* set faulty */
928 case O(INCREMENTAL
,'f'):
929 case O(INCREMENTAL
,Remove
):
930 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
931 * even though we will both fail and
932 * remove the device */
935 case O(MANAGE
,Replace
):
936 /* Mark these devices for replacement */
940 /* These are the replacements to use */
941 if (devmode
!= 'R') {
942 pr_err("--with must follow --replace\n");
947 case O(INCREMENTAL
,'R'):
949 case O(ASSEMBLE
,'R'):
951 case O(CREATE
,'R'): /* Run the array */
953 pr_err("Cannot both Stop and Run an array\n");
960 pr_err("Cannot both Run and Stop an array\n");
972 case O(MISC
,KillOpt
):
976 case O(MISC
, ExamineBB
):
980 case O(MISC
, WaitOpt
):
981 case O(MISC
, Waitclean
):
982 case O(MISC
, DetailPlatform
):
983 case O(MISC
, KillSubarray
):
984 case O(MISC
, UpdateSubarray
):
985 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
987 pr_err("subarray can only"
988 " be specified once\n");
993 if (devmode
&& devmode
!= opt
&&
994 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
995 pr_err("--examine/-E cannot be given with ");
996 if (devmode
== 'E') {
997 if (option_index
>= 0)
998 fprintf(stderr
, "--%s\n",
999 long_options
[option_index
].name
);
1001 fprintf(stderr
, "-%c\n", opt
);
1002 } else if (isalpha(devmode
))
1003 fprintf(stderr
, "-%c\n", devmode
);
1005 fprintf(stderr
, "previous option\n");
1010 case O(MISC
, UdevRules
):
1011 if (devmode
&& devmode
!= opt
) {
1012 pr_err("--udev-rules must"
1013 " be the only option.\n");
1016 pr_err("only specify one udev "
1017 "rule filename. %s ignored.\n",
1020 udev_filename
= optarg
;
1028 case O(MISC
, Sparc22
):
1029 if (devmode
!= 'E') {
1030 pr_err("--sparc2.2 only allowed with --examine\n");
1036 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1037 case O(ASSEMBLE
,Bitmap
):
1039 pr_err("bitmap file needed with -b in --assemble mode\n");
1042 if (strcmp(optarg
, "internal")==0) {
1043 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1046 bitmap_fd
= open(optarg
, O_RDWR
);
1047 if (!*optarg
|| bitmap_fd
< 0) {
1048 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1051 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1054 case O(ASSEMBLE
, BackupFile
):
1055 case O(GROW
, BackupFile
):
1056 /* Specify a file into which grow might place a backup,
1057 * or from which assemble might recover a backup
1059 if (c
.backup_file
) {
1060 pr_err("backup file already specified, rejecting %s\n", optarg
);
1063 c
.backup_file
= optarg
;
1066 case O(GROW
, Continue
):
1067 /* Continue interrupted grow
1071 case O(ASSEMBLE
, InvalidBackup
):
1072 /* Acknowledge that the backupfile is invalid, but ask
1073 * to continue anyway
1075 c
.invalid_backup
= 1;
1079 case O(BUILD
,Bitmap
):
1081 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1083 case O(GROW
,Bitmap
):
1084 if (strcmp(optarg
, "internal")== 0 ||
1085 strcmp(optarg
, "none")== 0 ||
1086 strchr(optarg
, '/') != NULL
) {
1087 s
.bitmap_file
= optarg
;
1091 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1092 " not '%s'\n", optarg
);
1095 case O(GROW
,BitmapChunk
):
1096 case O(BUILD
,BitmapChunk
):
1097 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1098 s
.bitmap_chunk
= parse_size(optarg
);
1099 if (s
.bitmap_chunk
== 0 ||
1100 s
.bitmap_chunk
== INVALID_SECTORS
||
1101 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1102 pr_err("invalid bitmap chunksize: %s\n",
1106 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1109 case O(GROW
, WriteBehind
):
1110 case O(BUILD
, WriteBehind
):
1111 case O(CREATE
, WriteBehind
): /* write-behind mode */
1112 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1114 s
.write_behind
= parse_num(optarg
);
1115 if (s
.write_behind
< 0 ||
1116 s
.write_behind
> 16383) {
1117 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1123 case O(INCREMENTAL
, 'r'):
1124 case O(INCREMENTAL
, RebuildMapOpt
):
1127 case O(INCREMENTAL
, IncrementalPath
):
1128 remove_path
= optarg
;
1131 /* We have now processed all the valid options. Anything else is
1134 if (option_index
> 0)
1135 pr_err(":option --%s not valid in %s mode\n",
1136 long_options
[option_index
].name
,
1137 map_num(modes
, mode
));
1139 pr_err("option -%c not valid in %s mode\n",
1140 opt
, map_num(modes
, mode
));
1147 if (print_help
== 2)
1148 help_text
= OptionHelp
;
1150 help_text
= mode_help
[mode
];
1151 if (help_text
== NULL
)
1153 fputs(help_text
,stdout
);
1157 if (!mode
&& devs_found
) {
1160 if (devlist
->disposition
== 0)
1161 devlist
->disposition
= devmode
;
1164 fputs(Usage
, stderr
);
1169 struct createinfo
*ci
= conf_get_create_info();
1171 if (strcasecmp(symlinks
, "yes") == 0)
1173 else if (strcasecmp(symlinks
, "no") == 0)
1176 pr_err("option --symlinks must be 'no' or 'yes'\n");
1180 /* Ok, got the option parsing out of the way
1181 * hopefully it's mostly right but there might be some stuff
1184 * That is mosty checked in the per-mode stuff but...
1186 * For @,B,C and A without -s, the first device listed must be
1187 * an md device. We check that here and open it.
1190 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1192 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1193 if (devs_found
< 1) {
1194 pr_err("an md device must be given in this mode\n");
1197 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1198 pr_err("--super-minor=dev is incompatible with --auto\n");
1201 if (mode
== MANAGE
|| mode
== GROW
) {
1202 mdfd
= open_mddev(devlist
->devname
, 1);
1206 /* non-existent device is OK */
1207 mdfd
= open_mddev(devlist
->devname
, 0);
1209 pr_err("device %s exists but is not an "
1210 "md array.\n", devlist
->devname
);
1213 if ((int)ident
.super_minor
== -2) {
1216 pr_err("--super-minor=dev given, and "
1217 "listed device %s doesn't exist.\n",
1222 ident
.super_minor
= minor(stb
.st_rdev
);
1224 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1225 /* We don't really want this open yet, we just might
1226 * have wanted to check some things
1234 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1235 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1236 " a mistake. If you really mean it you will need to specify --force before\n"
1237 " setting the number of drives.\n");
1242 if (c
.homehost
== NULL
)
1243 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1244 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1245 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1246 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1247 c
.homehost
= sys_hostname
;
1250 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1252 c
.require_homehost
= 0;
1255 if ((mode
== MISC
&& devmode
== 'E')
1256 || (mode
== MONITOR
&& spare_sharing
== 0))
1257 /* Anyone may try this */;
1258 else if (geteuid() != 0) {
1259 pr_err("must be super-user to perform this action\n");
1263 ident
.autof
= c
.autof
;
1265 if (c
.scan
&& c
.verbose
< 2)
1266 /* --scan implied --brief unless -vv */
1272 /* readonly, add/remove, readwrite, runstop */
1274 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1275 if (!rv
&& devs_found
>1)
1276 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1277 devlist
->next
, c
.verbose
, c
.test
,
1279 if (!rv
&& c
.readonly
< 0)
1280 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1281 if (!rv
&& c
.runstop
)
1282 rv
= Manage_runstop(devlist
->devname
, mdfd
, c
.runstop
, c
.verbose
, 0);
1285 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1286 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1287 /* Only a device has been given, so get details from config file */
1288 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1289 if (array_ident
== NULL
) {
1290 pr_err("%s not identified in config file.\n",
1296 if (array_ident
->autof
== 0)
1297 array_ident
->autof
= c
.autof
;
1298 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1302 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1304 else if (devs_found
> 0) {
1305 if (c
.update
&& devs_found
> 1) {
1306 pr_err("can only update a single array at a time\n");
1309 if (c
.backup_file
&& devs_found
> 1) {
1310 pr_err("can only assemble a single array when providing a backup file.\n");
1313 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1314 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1315 if (array_ident
== NULL
) {
1316 pr_err("%s not identified in config file.\n",
1321 if (array_ident
->autof
== 0)
1322 array_ident
->autof
= c
.autof
;
1323 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1328 pr_err("--update not meaningful with a --scan assembly.\n");
1331 if (c
.backup_file
) {
1332 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1335 rv
= scan_assemble(ss
, &c
, &ident
);
1341 c
.delay
= DEFAULT_BITMAP_DELAY
;
1342 if (s
.write_behind
&& !s
.bitmap_file
) {
1343 pr_err("write-behind mode requires a bitmap.\n");
1347 if (s
.raiddisks
== 0) {
1348 pr_err("no raid-devices specified.\n");
1353 if (s
.bitmap_file
) {
1354 if (strcmp(s
.bitmap_file
, "internal")==0) {
1355 pr_err("'internal' bitmaps not supported with --build\n");
1360 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1364 c
.delay
= DEFAULT_BITMAP_DELAY
;
1365 if (s
.write_behind
&& !s
.bitmap_file
) {
1366 pr_err("write-behind mode requires a bitmap.\n");
1370 if (s
.raiddisks
== 0) {
1371 pr_err("no raid-devices specified.\n");
1376 rv
= Create(ss
, devlist
->devname
,
1377 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1378 devs_found
-1, devlist
->next
,
1379 &s
, &c
, data_offset
);
1382 if (devmode
== 'E') {
1383 if (devlist
== NULL
&& !c
.scan
) {
1384 pr_err("No devices to examine\n");
1387 if (devlist
== NULL
)
1388 devlist
= conf_get_devs();
1389 if (devlist
== NULL
) {
1390 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1393 rv
= Examine(devlist
, &c
, ss
);
1394 } else if (devmode
== DetailPlatform
) {
1395 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1396 c
.verbose
, c
.export
,
1397 devlist
? devlist
->devname
: NULL
);
1398 } else if (devlist
== NULL
) {
1399 if (devmode
== 'S' && c
.scan
)
1400 rv
= stop_scan(c
.verbose
);
1401 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1402 rv
= misc_scan(devmode
, &c
);
1403 else if (devmode
== UdevRules
)
1404 rv
= Write_rules(udev_filename
);
1406 pr_err("No devices given.\n");
1410 rv
= misc_list(devlist
, &ident
, ss
, &c
);
1413 if (!devlist
&& !c
.scan
) {
1414 pr_err("Cannot monitor: need --scan or at least one device\n");
1418 if (pidfile
&& !daemonise
) {
1419 pr_err("Cannot write a pid file when not in daemon mode\n");
1424 if (get_linux_version() > 2006016)
1425 /* mdstat responds to poll */
1432 rv
= Monitor(devlist
, mailaddr
, program
,
1433 &c
, daemonise
, oneshot
,
1434 dosyslog
, pidfile
, increments
,
1439 if (array_size
> 0) {
1440 /* alway impose array size first, independent of
1442 * Do not allow level or raid_disks changes at the
1443 * same time as that can be irreversibly destructive.
1447 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1448 pr_err("cannot change array size in same operation "
1449 "as changing raiddisks or level.\n"
1450 " Change size first, then check that data is still intact.\n");
1454 sysfs_init(&sra
, mdfd
, NULL
);
1455 if (array_size
== MAX_SIZE
)
1456 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1458 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1461 pr_err("--array-size setting"
1462 " is too large.\n");
1464 pr_err("current kernel does"
1465 " not support setting --array-size\n");
1470 if (devs_found
> 1 && s
.raiddisks
== 0) {
1472 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1473 pr_err("--add cannot be used with "
1474 "other geometry changes in --grow mode\n");
1478 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1479 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1484 } else if (s
.bitmap_file
) {
1485 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1486 s
.layout_str
!= NULL
|| devs_found
> 1) {
1487 pr_err("--bitmap changes cannot be "
1488 "used with other geometry changes "
1489 "in --grow mode\n");
1494 c
.delay
= DEFAULT_BITMAP_DELAY
;
1495 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1496 } else if (grow_continue
)
1497 rv
= Grow_continue_command(devlist
->devname
,
1498 mdfd
, c
.backup_file
,
1500 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1501 || s
.chunk
!= 0 || s
.level
!= UnSet
) {
1502 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1504 data_offset
, &c
, &s
);
1505 } else if (array_size
== 0)
1506 pr_err("no changes to --grow\n");
1513 if (c
.runstop
<= 0) {
1514 pr_err("--incremental --scan meaningless without --run.\n");
1517 if (devmode
== 'f') {
1518 pr_err("--incremental --scan --fail not supported.\n");
1521 rv
= IncrementalScan(c
.verbose
);
1524 if (!rebuild_map
&& !c
.scan
) {
1525 pr_err("--incremental requires a device.\n");
1530 if (devlist
->next
) {
1531 pr_err("--incremental can only handle one device.\n");
1536 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1539 rv
= Incremental(devlist
->devname
, &c
, ss
);
1548 static int scan_assemble(struct supertype
*ss
,
1550 struct mddev_ident
*ident
)
1552 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1553 struct mddev_dev
*devlist
= conf_get_devs();
1554 struct map_ent
*map
= NULL
;
1557 int failures
, successes
;
1559 if (conf_verify_devnames(array_list
)) {
1560 pr_err("Duplicate MD device names in "
1561 "conf file were found.\n");
1564 if (devlist
== NULL
) {
1565 pr_err("No devices listed in conf file were found.\n");
1568 for (a
= array_list
; a
; a
= a
->next
) {
1571 a
->autof
= c
->autof
;
1574 pr_err("%s: failed to get "
1575 "exclusive lock on mapfile\n",
1581 for (a
= array_list
; a
; a
= a
->next
) {
1586 strcasecmp(a
->devname
, "<ignore>") == 0)
1589 r
= Assemble(ss
, a
->devname
,
1599 } while (failures
&& successes
);
1600 if (c
->homehost
&& cnt
== 0) {
1601 /* Maybe we can auto-assemble something.
1602 * Repeatedly call Assemble in auto-assemble mode
1607 ident
->autof
= c
->autof
;
1609 struct mddev_dev
*devlist
= conf_get_devs();
1612 rv2
= Assemble(ss
, NULL
,
1620 /* Incase there are stacked devices, we need to go around again */
1622 if (cnt
== 0 && rv
== 0) {
1623 pr_err("No arrays found in config file or automatically\n");
1627 } else if (cnt
== 0 && rv
== 0) {
1628 pr_err("No arrays found in config file\n");
1635 static int misc_scan(char devmode
, struct context
*c
)
1637 /* apply --detail or --wait-clean to
1638 * all devices in /proc/mdstat
1640 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1641 struct mdstat_ent
*e
;
1642 struct map_ent
*map
= NULL
;
1646 for (members
= 0; members
<= 1; members
++) {
1647 for (e
=ms
; e
; e
=e
->next
) {
1650 int member
= e
->metadata_version
&&
1651 strncmp(e
->metadata_version
,
1652 "external:/", 10) == 0;
1653 if (members
!= member
)
1655 me
= map_by_devnm(&map
, e
->devnm
);
1657 && strcmp(me
->path
, "/unknown") != 0)
1660 name
= get_md_name(e
->devnm
);
1663 pr_err("cannot find device file for %s\n",
1668 rv
|= Detail(name
, c
);
1670 rv
|= WaitClean(name
, -1, c
->verbose
);
1678 static int stop_scan(int verbose
)
1680 /* apply --stop to all devices in /proc/mdstat */
1681 /* Due to possible stacking of devices, repeat until
1682 * nothing more can be stopped
1684 int progress
=1, err
;
1688 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1689 struct mdstat_ent
*e
;
1691 if (!progress
) last
= 1;
1692 progress
= 0; err
= 0;
1693 for (e
=ms
; e
; e
=e
->next
) {
1694 char *name
= get_md_name(e
->devnm
);
1698 pr_err("cannot find device file for %s\n",
1702 mdfd
= open_mddev(name
, 1);
1704 if (Manage_runstop(name
, mdfd
, -1, verbose
, !last
))
1714 } while (!last
&& err
);
1720 static int misc_list(struct mddev_dev
*devlist
,
1721 struct mddev_ident
*ident
,
1722 struct supertype
*ss
, struct context
*c
)
1724 struct mddev_dev
*dv
;
1727 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1730 switch(dv
->disposition
) {
1732 rv
|= Detail(dv
->devname
, c
);
1734 case KillOpt
: /* Zero superblock */
1736 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1740 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1747 rv
|= Query(dv
->devname
); continue;
1749 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1751 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1754 rv
|= Wait(dv
->devname
); continue;
1756 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1758 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1760 case UpdateSubarray
:
1761 if (c
->update
== NULL
) {
1762 pr_err("-U/--update must be specified with --update-subarray\n");
1766 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1767 c
->update
, ident
, c
->verbose
);
1770 mdfd
= open_mddev(dv
->devname
, 1);
1772 switch(dv
->disposition
) {
1774 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, c
->verbose
, 0); break;
1776 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, c
->verbose
, 0); break;
1778 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1780 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;