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 */
1082 if (strcmp(optarg
, "none") == 0) {
1083 pr_err("'--bitmap none' only"
1084 " supported for --grow\n");
1089 case O(GROW
,Bitmap
):
1090 if (strcmp(optarg
, "internal")== 0 ||
1091 strcmp(optarg
, "none")== 0 ||
1092 strchr(optarg
, '/') != NULL
) {
1093 s
.bitmap_file
= optarg
;
1097 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1098 " not '%s'\n", optarg
);
1101 case O(GROW
,BitmapChunk
):
1102 case O(BUILD
,BitmapChunk
):
1103 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1104 s
.bitmap_chunk
= parse_size(optarg
);
1105 if (s
.bitmap_chunk
== 0 ||
1106 s
.bitmap_chunk
== INVALID_SECTORS
||
1107 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1108 pr_err("invalid bitmap chunksize: %s\n",
1112 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1115 case O(GROW
, WriteBehind
):
1116 case O(BUILD
, WriteBehind
):
1117 case O(CREATE
, WriteBehind
): /* write-behind mode */
1118 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1120 s
.write_behind
= parse_num(optarg
);
1121 if (s
.write_behind
< 0 ||
1122 s
.write_behind
> 16383) {
1123 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1129 case O(INCREMENTAL
, 'r'):
1130 case O(INCREMENTAL
, RebuildMapOpt
):
1133 case O(INCREMENTAL
, IncrementalPath
):
1134 remove_path
= optarg
;
1137 /* We have now processed all the valid options. Anything else is
1140 if (option_index
> 0)
1141 pr_err(":option --%s not valid in %s mode\n",
1142 long_options
[option_index
].name
,
1143 map_num(modes
, mode
));
1145 pr_err("option -%c not valid in %s mode\n",
1146 opt
, map_num(modes
, mode
));
1153 if (print_help
== 2)
1154 help_text
= OptionHelp
;
1156 help_text
= mode_help
[mode
];
1157 if (help_text
== NULL
)
1159 fputs(help_text
,stdout
);
1163 if (!mode
&& devs_found
) {
1166 if (devlist
->disposition
== 0)
1167 devlist
->disposition
= devmode
;
1170 fputs(Usage
, stderr
);
1175 struct createinfo
*ci
= conf_get_create_info();
1177 if (strcasecmp(symlinks
, "yes") == 0)
1179 else if (strcasecmp(symlinks
, "no") == 0)
1182 pr_err("option --symlinks must be 'no' or 'yes'\n");
1186 /* Ok, got the option parsing out of the way
1187 * hopefully it's mostly right but there might be some stuff
1190 * That is mosty checked in the per-mode stuff but...
1192 * For @,B,C and A without -s, the first device listed must be
1193 * an md device. We check that here and open it.
1196 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1198 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1199 if (devs_found
< 1) {
1200 pr_err("an md device must be given in this mode\n");
1203 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1204 pr_err("--super-minor=dev is incompatible with --auto\n");
1207 if (mode
== MANAGE
|| mode
== GROW
) {
1208 mdfd
= open_mddev(devlist
->devname
, 1);
1212 /* non-existent device is OK */
1213 mdfd
= open_mddev(devlist
->devname
, 0);
1215 pr_err("device %s exists but is not an "
1216 "md array.\n", devlist
->devname
);
1219 if ((int)ident
.super_minor
== -2) {
1222 pr_err("--super-minor=dev given, and "
1223 "listed device %s doesn't exist.\n",
1228 ident
.super_minor
= minor(stb
.st_rdev
);
1230 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1231 /* We don't really want this open yet, we just might
1232 * have wanted to check some things
1240 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1241 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1242 " a mistake. If you really mean it you will need to specify --force before\n"
1243 " setting the number of drives.\n");
1248 if (c
.homehost
== NULL
)
1249 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1250 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1251 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1252 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1253 c
.homehost
= sys_hostname
;
1256 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1258 c
.require_homehost
= 0;
1261 if ((mode
== MISC
&& devmode
== 'E')
1262 || (mode
== MONITOR
&& spare_sharing
== 0))
1263 /* Anyone may try this */;
1264 else if (geteuid() != 0) {
1265 pr_err("must be super-user to perform this action\n");
1269 ident
.autof
= c
.autof
;
1271 if (c
.scan
&& c
.verbose
< 2)
1272 /* --scan implied --brief unless -vv */
1278 /* readonly, add/remove, readwrite, runstop */
1280 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1281 if (!rv
&& devs_found
>1)
1282 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1283 devlist
->next
, c
.verbose
, c
.test
,
1285 if (!rv
&& c
.readonly
< 0)
1286 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1287 if (!rv
&& c
.runstop
)
1288 rv
= Manage_runstop(devlist
->devname
, mdfd
, c
.runstop
, c
.verbose
, 0);
1291 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1292 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1293 /* Only a device has been given, so get details from config file */
1294 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1295 if (array_ident
== NULL
) {
1296 pr_err("%s not identified in config file.\n",
1302 if (array_ident
->autof
== 0)
1303 array_ident
->autof
= c
.autof
;
1304 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1308 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1310 else if (devs_found
> 0) {
1311 if (c
.update
&& devs_found
> 1) {
1312 pr_err("can only update a single array at a time\n");
1315 if (c
.backup_file
&& devs_found
> 1) {
1316 pr_err("can only assemble a single array when providing a backup file.\n");
1319 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1320 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1321 if (array_ident
== NULL
) {
1322 pr_err("%s not identified in config file.\n",
1327 if (array_ident
->autof
== 0)
1328 array_ident
->autof
= c
.autof
;
1329 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1334 pr_err("--update not meaningful with a --scan assembly.\n");
1337 if (c
.backup_file
) {
1338 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1341 rv
= scan_assemble(ss
, &c
, &ident
);
1347 c
.delay
= DEFAULT_BITMAP_DELAY
;
1348 if (s
.write_behind
&& !s
.bitmap_file
) {
1349 pr_err("write-behind mode requires a bitmap.\n");
1353 if (s
.raiddisks
== 0) {
1354 pr_err("no raid-devices specified.\n");
1359 if (s
.bitmap_file
) {
1360 if (strcmp(s
.bitmap_file
, "internal")==0) {
1361 pr_err("'internal' bitmaps not supported with --build\n");
1366 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1370 c
.delay
= DEFAULT_BITMAP_DELAY
;
1371 if (s
.write_behind
&& !s
.bitmap_file
) {
1372 pr_err("write-behind mode requires a bitmap.\n");
1376 if (s
.raiddisks
== 0) {
1377 pr_err("no raid-devices specified.\n");
1382 rv
= Create(ss
, devlist
->devname
,
1383 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1384 devs_found
-1, devlist
->next
,
1385 &s
, &c
, data_offset
);
1388 if (devmode
== 'E') {
1389 if (devlist
== NULL
&& !c
.scan
) {
1390 pr_err("No devices to examine\n");
1393 if (devlist
== NULL
)
1394 devlist
= conf_get_devs();
1395 if (devlist
== NULL
) {
1396 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1399 rv
= Examine(devlist
, &c
, ss
);
1400 } else if (devmode
== DetailPlatform
) {
1401 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1402 c
.verbose
, c
.export
,
1403 devlist
? devlist
->devname
: NULL
);
1404 } else if (devlist
== NULL
) {
1405 if (devmode
== 'S' && c
.scan
)
1406 rv
= stop_scan(c
.verbose
);
1407 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1408 rv
= misc_scan(devmode
, &c
);
1409 else if (devmode
== UdevRules
)
1410 rv
= Write_rules(udev_filename
);
1412 pr_err("No devices given.\n");
1416 rv
= misc_list(devlist
, &ident
, ss
, &c
);
1419 if (!devlist
&& !c
.scan
) {
1420 pr_err("Cannot monitor: need --scan or at least one device\n");
1424 if (pidfile
&& !daemonise
) {
1425 pr_err("Cannot write a pid file when not in daemon mode\n");
1430 if (get_linux_version() > 2006016)
1431 /* mdstat responds to poll */
1438 rv
= Monitor(devlist
, mailaddr
, program
,
1439 &c
, daemonise
, oneshot
,
1440 dosyslog
, pidfile
, increments
,
1445 if (array_size
> 0) {
1446 /* alway impose array size first, independent of
1448 * Do not allow level or raid_disks changes at the
1449 * same time as that can be irreversibly destructive.
1453 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1454 pr_err("cannot change array size in same operation "
1455 "as changing raiddisks or level.\n"
1456 " Change size first, then check that data is still intact.\n");
1460 sysfs_init(&sra
, mdfd
, NULL
);
1461 if (array_size
== MAX_SIZE
)
1462 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1464 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1467 pr_err("--array-size setting"
1468 " is too large.\n");
1470 pr_err("current kernel does"
1471 " not support setting --array-size\n");
1476 if (devs_found
> 1 && s
.raiddisks
== 0) {
1478 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1479 pr_err("--add cannot be used with "
1480 "other geometry changes in --grow mode\n");
1484 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1485 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1490 } else if (s
.bitmap_file
) {
1491 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1492 s
.layout_str
!= NULL
|| devs_found
> 1) {
1493 pr_err("--bitmap changes cannot be "
1494 "used with other geometry changes "
1495 "in --grow mode\n");
1500 c
.delay
= DEFAULT_BITMAP_DELAY
;
1501 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1502 } else if (grow_continue
)
1503 rv
= Grow_continue_command(devlist
->devname
,
1504 mdfd
, c
.backup_file
,
1506 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1507 || s
.chunk
!= 0 || s
.level
!= UnSet
) {
1508 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1510 data_offset
, &c
, &s
);
1511 } else if (array_size
== 0)
1512 pr_err("no changes to --grow\n");
1519 if (c
.runstop
<= 0) {
1520 pr_err("--incremental --scan meaningless without --run.\n");
1523 if (devmode
== 'f') {
1524 pr_err("--incremental --scan --fail not supported.\n");
1527 rv
= IncrementalScan(c
.verbose
);
1530 if (!rebuild_map
&& !c
.scan
) {
1531 pr_err("--incremental requires a device.\n");
1536 if (devlist
->next
) {
1537 pr_err("--incremental can only handle one device.\n");
1542 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1545 rv
= Incremental(devlist
->devname
, &c
, ss
);
1554 static int scan_assemble(struct supertype
*ss
,
1556 struct mddev_ident
*ident
)
1558 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1559 struct mddev_dev
*devlist
= conf_get_devs();
1560 struct map_ent
*map
= NULL
;
1563 int failures
, successes
;
1565 if (conf_verify_devnames(array_list
)) {
1566 pr_err("Duplicate MD device names in "
1567 "conf file were found.\n");
1570 if (devlist
== NULL
) {
1571 pr_err("No devices listed in conf file were found.\n");
1574 for (a
= array_list
; a
; a
= a
->next
) {
1577 a
->autof
= c
->autof
;
1580 pr_err("%s: failed to get "
1581 "exclusive lock on mapfile\n",
1587 for (a
= array_list
; a
; a
= a
->next
) {
1592 strcasecmp(a
->devname
, "<ignore>") == 0)
1595 r
= Assemble(ss
, a
->devname
,
1605 } while (failures
&& successes
);
1606 if (c
->homehost
&& cnt
== 0) {
1607 /* Maybe we can auto-assemble something.
1608 * Repeatedly call Assemble in auto-assemble mode
1613 ident
->autof
= c
->autof
;
1615 struct mddev_dev
*devlist
= conf_get_devs();
1618 rv2
= Assemble(ss
, NULL
,
1626 /* Incase there are stacked devices, we need to go around again */
1628 if (cnt
== 0 && rv
== 0) {
1629 pr_err("No arrays found in config file or automatically\n");
1633 } else if (cnt
== 0 && rv
== 0) {
1634 pr_err("No arrays found in config file\n");
1641 static int misc_scan(char devmode
, struct context
*c
)
1643 /* apply --detail or --wait-clean to
1644 * all devices in /proc/mdstat
1646 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1647 struct mdstat_ent
*e
;
1648 struct map_ent
*map
= NULL
;
1652 for (members
= 0; members
<= 1; members
++) {
1653 for (e
=ms
; e
; e
=e
->next
) {
1656 int member
= e
->metadata_version
&&
1657 strncmp(e
->metadata_version
,
1658 "external:/", 10) == 0;
1659 if (members
!= member
)
1661 me
= map_by_devnm(&map
, e
->devnm
);
1663 && strcmp(me
->path
, "/unknown") != 0)
1666 name
= get_md_name(e
->devnm
);
1669 pr_err("cannot find device file for %s\n",
1674 rv
|= Detail(name
, c
);
1676 rv
|= WaitClean(name
, -1, c
->verbose
);
1684 static int stop_scan(int verbose
)
1686 /* apply --stop to all devices in /proc/mdstat */
1687 /* Due to possible stacking of devices, repeat until
1688 * nothing more can be stopped
1690 int progress
=1, err
;
1694 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1695 struct mdstat_ent
*e
;
1697 if (!progress
) last
= 1;
1698 progress
= 0; err
= 0;
1699 for (e
=ms
; e
; e
=e
->next
) {
1700 char *name
= get_md_name(e
->devnm
);
1704 pr_err("cannot find device file for %s\n",
1708 mdfd
= open_mddev(name
, 1);
1710 if (Manage_runstop(name
, mdfd
, -1, verbose
, !last
))
1720 } while (!last
&& err
);
1726 static int misc_list(struct mddev_dev
*devlist
,
1727 struct mddev_ident
*ident
,
1728 struct supertype
*ss
, struct context
*c
)
1730 struct mddev_dev
*dv
;
1733 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1736 switch(dv
->disposition
) {
1738 rv
|= Detail(dv
->devname
, c
);
1740 case KillOpt
: /* Zero superblock */
1742 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1746 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1753 rv
|= Query(dv
->devname
); continue;
1755 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1757 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1760 rv
|= Wait(dv
->devname
); continue;
1762 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1764 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1766 case UpdateSubarray
:
1767 if (c
->update
== NULL
) {
1768 pr_err("-U/--update must be specified with --update-subarray\n");
1772 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1773 c
->update
, ident
, c
->verbose
);
1776 mdfd
= open_mddev(dv
->devname
, 1);
1778 switch(dv
->disposition
) {
1780 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, c
->verbose
, 0); break;
1782 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, c
->verbose
, 0); break;
1784 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1786 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;