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
,
41 struct supertype
*ss
, struct context
*c
);
44 int main(int argc
, char *argv
[])
52 unsigned long long array_size
= 0;
53 unsigned long long data_offset
= INVALID_SECTORS
;
54 struct mddev_ident ident
;
55 char *configfile
= NULL
;
58 struct mddev_dev
*devlist
= NULL
;
59 struct mddev_dev
**devlistend
= & devlist
;
62 char *symlinks
= NULL
;
63 int grow_continue
= 0;
64 /* autof indicates whether and how to create device node.
65 * bottom 3 bits are style. Rest (when shifted) are number of parts
67 * 1 - don't create (no)
68 * 2 - if is_standard, then create (yes)
69 * 3 - create as 'md' - reject is_standard mdp (md)
70 * 4 - create as 'mdp' - reject is_standard md (mdp)
71 * 5 - default to md if not is_standard (md in config file)
72 * 6 - default to mdp if not is_standard (part, or mdp in config file)
75 .require_homehost
= 1,
80 .bitmap_chunk
= UnSet
,
83 char sys_hostname
[256];
84 char *mailaddr
= NULL
;
90 int spare_sharing
= 1;
91 struct supertype
*ss
= NULL
;
93 char *shortopt
= short_options
;
96 char *remove_path
= NULL
;
97 char *udev_filename
= NULL
;
98 char *dump_directory
= NULL
;
105 srandom(time(0) ^ getpid());
109 ident
.raid_disks
= UnSet
;
110 ident
.super_minor
= UnSet
;
112 ident
.spare_group
= NULL
;
115 ident
.bitmap_fd
= -1;
116 ident
.bitmap_file
= NULL
;
118 ident
.container
= NULL
;
122 * set first char of argv[0] to @. This is used by
123 * systemd to signal that the task was launched from
124 * initrd/initramfs and should be preserved during shutdown
128 while ((option_index
= -1) ,
129 (opt
=getopt_long(argc
, argv
,
130 shortopt
, long_options
,
131 &option_index
)) != -1) {
133 /* firstly, some mode-independent options */
143 fputs(Version
, stderr
);
146 case 'v': c
.verbose
++;
149 case 'q': c
.verbose
--;
153 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
154 || mode
== GROW
|| mode
== INCREMENTAL
156 break; /* b means bitmap */
161 case 'Y': c
.export
++;
165 if (strcasecmp(optarg
, "<ignore>") == 0)
166 c
.require_homehost
= 0;
172 /* Silently ignore old option */
178 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
184 fputs(Usage
, stderr
);
187 /* second, figure out the mode.
188 * Some options force the mode. Others
189 * set the mode if it isn't already
195 shortopt
= short_bitmap_options
;
205 case ReAdd
: /* re-add */
208 shortopt
= short_bitmap_options
;
212 case 'A': newmode
= ASSEMBLE
;
213 shortopt
= short_bitmap_auto_options
;
215 case 'B': newmode
= BUILD
;
216 shortopt
= short_bitmap_auto_options
;
218 case 'C': newmode
= CREATE
;
219 shortopt
= short_bitmap_auto_options
;
221 case 'F': newmode
= MONITOR
;
223 case 'G': newmode
= GROW
;
224 shortopt
= short_bitmap_options
;
226 case 'I': newmode
= INCREMENTAL
;
227 shortopt
= short_bitmap_auto_options
;
230 newmode
= AUTODETECT
;
264 if (mode
&& newmode
== mode
) {
265 /* everybody happy ! */
266 } else if (mode
&& newmode
!= mode
) {
269 if (option_index
>= 0)
270 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
272 fprintf(stderr
, "-%c", opt
);
273 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
274 map_num(modes
, newmode
),
275 map_num(modes
, mode
));
277 } else if (!mode
&& newmode
) {
279 if (mode
== MISC
&& devs_found
) {
280 pr_err("No action given for %s in --misc mode\n",
282 cont_err("Action options must come before device names\n");
286 /* special case of -c --help */
287 if ((opt
== 'c' || opt
== ConfigFile
) &&
288 ( strncmp(optarg
, "--h", 3)==0 ||
289 strncmp(optarg
, "-h", 2)==0)) {
290 fputs(Help_config
, stdout
);
294 /* If first option is a device, don't force the mode yet */
296 if (devs_found
== 0) {
297 dv
= xmalloc(sizeof(*dv
));
298 dv
->devname
= optarg
;
299 dv
->disposition
= devmode
;
300 dv
->writemostly
= writemostly
;
304 devlistend
= &dv
->next
;
309 /* No mode yet, and this is the second device ... */
310 pr_err("An option must be given to set the mode before a second device\n"
311 " (%s) is listed\n", optarg
);
314 if (option_index
>= 0)
315 pr_err("--%s", long_options
[option_index
].name
);
318 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
322 /* if we just set the mode, then done */
336 /* an undecorated option - must be a device name.
339 if (devs_found
> 0 && devmode
== DetailPlatform
) {
340 pr_err("controller may only be specified once. %s ignored\n",
345 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
346 pr_err("Must give one of -a/-r/-f"
347 " for subsequent devices at %s\n", optarg
);
350 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
351 pr_err("Must give -a/--add for"
352 " devices to add: %s\n", optarg
);
355 dv
= xmalloc(sizeof(*dv
));
356 dv
->devname
= optarg
;
357 dv
->disposition
= devmode
;
358 dv
->writemostly
= writemostly
;
362 devlistend
= &dv
->next
;
368 /* We've got a mode, and opt is now something else which
369 * could depend on the mode */
370 #define O(a,b) ((a<<16)|b)
371 switch (O(mode
,opt
)) {
373 case O(GROW
,ChunkSize
):
375 case O(CREATE
,ChunkSize
):
376 case O(BUILD
,'c'): /* chunk or rounding */
377 case O(BUILD
,ChunkSize
): /* chunk or rounding */
379 pr_err("chunk/rounding may only be specified once. "
380 "Second value is %s.\n", optarg
);
383 s
.chunk
= parse_size(optarg
);
384 if (s
.chunk
== INVALID_SECTORS
||
385 s
.chunk
< 8 || (s
.chunk
&1)) {
386 pr_err("invalid chunk/rounding value: %s\n",
390 /* Convert sectors to K */
394 case O(INCREMENTAL
, 'e'):
396 case O(ASSEMBLE
,'e'):
397 case O(MISC
,'e'): /* set metadata (superblock) information */
399 pr_err("metadata information already given\n");
402 for(i
=0; !ss
&& superlist
[i
]; i
++)
403 ss
= superlist
[i
]->match_metadata_desc(optarg
);
406 pr_err("unrecognised metadata identifier: %s\n", optarg
);
412 case O(MANAGE
,WriteMostly
):
414 case O(BUILD
,WriteMostly
):
416 case O(CREATE
,WriteMostly
):
417 /* set write-mostly for following devices */
422 /* clear write-mostly for following devices */
429 case O(BUILD
,'z'): /* size */
431 pr_err("size may only be specified once. "
432 "Second value is %s.\n", optarg
);
435 if (strcmp(optarg
, "max")==0)
438 s
.size
= parse_size(optarg
);
439 if (s
.size
== INVALID_SECTORS
||
441 pr_err("invalid size: %s\n",
445 /* convert sectors to K */
450 case O(GROW
,'Z'): /* array size */
451 if (array_size
> 0) {
452 pr_err("array-size may only be specified once. "
453 "Second value is %s.\n", optarg
);
456 if (strcmp(optarg
, "max") == 0)
457 array_size
= MAX_SIZE
;
459 array_size
= parse_size(optarg
);
460 if (array_size
== 0 ||
461 array_size
== INVALID_SECTORS
) {
462 pr_err("invalid array size: %s\n",
469 case O(CREATE
,DataOffset
):
470 case O(GROW
,DataOffset
):
471 if (data_offset
!= INVALID_SECTORS
) {
472 pr_err("data-offset may only be specified one. "
473 "Second value is %s.\n", optarg
);
476 if (mode
== CREATE
&&
477 strcmp(optarg
, "variable") == 0)
478 data_offset
= VARIABLE_OFFSET
;
480 data_offset
= parse_size(optarg
);
481 if (data_offset
== INVALID_SECTORS
) {
482 pr_err("invalid data-offset: %s\n",
490 case O(BUILD
,'l'): /* set raid level*/
491 if (s
.level
!= UnSet
) {
492 pr_err("raid level may only be set once. "
493 "Second value is %s.\n", optarg
);
496 s
.level
= map_name(pers
, optarg
);
497 if (s
.level
== UnSet
) {
498 pr_err("invalid raid level: %s\n",
502 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
503 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
506 pr_err("Raid level %s not permitted with --build.\n",
510 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
511 pr_err("raid level %s is incompatible with spare-devices setting.\n",
515 ident
.level
= s
.level
;
518 case O(GROW
, 'p'): /* new layout */
519 case O(GROW
, Layout
):
521 pr_err("layout may only be sent once. "
522 "Second value was %s\n", optarg
);
525 s
.layout_str
= optarg
;
526 /* 'Grow' will parse the value */
529 case O(CREATE
,'p'): /* raid5 layout */
530 case O(CREATE
,Layout
):
531 case O(BUILD
,'p'): /* faulty layout */
532 case O(BUILD
,Layout
):
533 if (s
.layout
!= UnSet
) {
534 pr_err("layout may only be sent once. "
535 "Second value was %s\n", optarg
);
540 pr_err("layout not meaningful for %s arrays.\n",
541 map_num(pers
, s
.level
));
544 pr_err("raid level must be given before layout.\n");
548 s
.layout
= map_name(r5layout
, optarg
);
549 if (s
.layout
==UnSet
) {
550 pr_err("layout %s not understood for raid5.\n",
556 s
.layout
= map_name(r6layout
, optarg
);
557 if (s
.layout
==UnSet
) {
558 pr_err("layout %s not understood for raid6.\n",
565 s
.layout
= parse_layout_10(optarg
);
567 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
575 s
.layout
= parse_layout_faulty(optarg
);
576 if (s
.layout
== -1) {
577 pr_err("layout %s not understood for faulty.\n",
585 case O(CREATE
,AssumeClean
):
586 case O(BUILD
,AssumeClean
): /* assume clean */
587 case O(GROW
,AssumeClean
):
593 case O(BUILD
,'n'): /* number of raid disks */
595 pr_err("raid-devices set twice: %d and %s\n",
596 s
.raiddisks
, optarg
);
599 s
.raiddisks
= parse_num(optarg
);
600 if (s
.raiddisks
<= 0) {
601 pr_err("invalid number of raid devices: %s\n",
605 ident
.raid_disks
= s
.raiddisks
;
608 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
610 pr_err("spare-devices set twice: %d and %s\n",
611 s
.sparedisks
, optarg
);
614 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
615 pr_err("spare-devices setting is incompatible with raid level %d\n",
619 s
.sparedisks
= parse_num(optarg
);
620 if (s
.sparedisks
< 0) {
621 pr_err("invalid number of spare-devices: %s\n",
631 case O(INCREMENTAL
,'a'):
632 case O(INCREMENTAL
,Auto
):
633 case O(ASSEMBLE
,'a'):
634 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
635 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
638 case O(CREATE
,Symlinks
):
639 case O(BUILD
,Symlinks
):
640 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
644 case O(BUILD
,'f'): /* force honouring '-n 1' */
645 case O(BUILD
,Force
): /* force honouring '-n 1' */
646 case O(GROW
,'f'): /* ditto */
647 case O(GROW
,Force
): /* ditto */
648 case O(CREATE
,'f'): /* force honouring of device list */
649 case O(CREATE
,Force
): /* force honouring of device list */
650 case O(ASSEMBLE
,'f'): /* force assembly */
651 case O(ASSEMBLE
,Force
): /* force assembly */
652 case O(MISC
,'f'): /* force zero */
653 case O(MISC
,Force
): /* force zero */
654 case O(MANAGE
,Force
): /* add device which is too large */
657 /* now for the Assemble options */
658 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
660 case O(INCREMENTAL
, FreezeReshape
):
661 c
.freeze_reshape
= 1;
663 case O(CREATE
,'u'): /* uuid of array */
664 case O(ASSEMBLE
,'u'): /* uuid of array */
665 if (ident
.uuid_set
) {
666 pr_err("uuid cannot be set twice. "
667 "Second value %s.\n", optarg
);
670 if (parse_uuid(optarg
, ident
.uuid
))
673 pr_err("Bad uuid: %s\n", optarg
);
679 case O(ASSEMBLE
,'N'):
682 pr_err("name cannot be set twice. "
683 "Second value %s.\n", optarg
);
686 if (mode
== MISC
&& !c
.subarray
) {
687 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
690 if (strlen(optarg
) > 32) {
691 pr_err("name '%s' is too long, 32 chars max.\n",
695 strcpy(ident
.name
, optarg
);
698 case O(ASSEMBLE
,'m'): /* super-minor for array */
699 case O(ASSEMBLE
,SuperMinor
):
700 if (ident
.super_minor
!= UnSet
) {
701 pr_err("super-minor cannot be set twice. "
702 "Second value: %s.\n", optarg
);
705 if (strcmp(optarg
, "dev")==0)
706 ident
.super_minor
= -2;
708 ident
.super_minor
= parse_num(optarg
);
709 if (ident
.super_minor
< 0) {
710 pr_err("Bad super-minor number: %s.\n", optarg
);
716 case O(ASSEMBLE
,'o'):
722 case O(ASSEMBLE
,'U'): /* update the superblock */
725 pr_err("Can only update one aspect"
726 " of superblock, both %s and %s given.\n",
730 if (mode
== MISC
&& !c
.subarray
) {
731 pr_err("Only subarrays can be"
732 " updated in misc mode\n");
736 if (strcmp(c
.update
, "sparc2.2")==0)
738 if (strcmp(c
.update
, "super-minor") == 0)
740 if (strcmp(c
.update
, "summaries")==0)
742 if (strcmp(c
.update
, "resync")==0)
744 if (strcmp(c
.update
, "uuid")==0)
746 if (strcmp(c
.update
, "name")==0)
748 if (strcmp(c
.update
, "homehost")==0)
750 if (strcmp(c
.update
, "devicesize")==0)
752 if (strcmp(c
.update
, "no-bitmap")==0)
754 if (strcmp(c
.update
, "bbl") == 0)
756 if (strcmp(c
.update
, "no-bbl") == 0)
758 if (strcmp(c
.update
, "metadata") == 0)
760 if (strcmp(c
.update
, "revert-reshape") == 0)
762 if (strcmp(c
.update
, "byteorder")==0) {
764 pr_err("must not set metadata"
765 " type with --update=byteorder.\n");
768 for(i
=0; !ss
&& superlist
[i
]; i
++)
769 ss
= superlist
[i
]->match_metadata_desc(
772 pr_err("INTERNAL ERROR"
773 " cannot find 0.swap\n");
779 if (strcmp(c
.update
,"?") == 0 ||
780 strcmp(c
.update
, "help") == 0) {
782 fprintf(outf
, Name
": ");
786 Name
": '--update=%s' is invalid. ",
789 fprintf(outf
, "Valid --update options are:\n"
790 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
791 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
792 " 'no-bitmap', 'metadata', 'revert-reshape'\n");
793 exit(outf
== stdout
? 0 : 2);
796 /* update=devicesize is allowed with --re-add */
797 if (devmode
!= 'A') {
798 pr_err("--update in Manage mode only"
799 " allowed with --re-add.\n");
803 pr_err("Can only update one aspect"
804 " of superblock, both %s and %s given.\n",
809 if (strcmp(c
.update
, "devicesize") != 0 &&
810 strcmp(c
.update
, "bbl") != 0 &&
811 strcmp(c
.update
, "no-bbl") != 0) {
812 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be"
813 " updated with --re-add\n");
818 case O(INCREMENTAL
,NoDegraded
):
819 pr_err("--no-degraded is deprecated in Incremental mode\n");
820 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
821 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
822 * so we overload slightly */
825 case O(ASSEMBLE
,'c'):
826 case O(ASSEMBLE
,ConfigFile
):
827 case O(INCREMENTAL
, 'c'):
828 case O(INCREMENTAL
, ConfigFile
):
830 case O(MISC
, ConfigFile
):
832 case O(MONITOR
,ConfigFile
):
834 pr_err("configfile cannot be set twice. "
835 "Second value is %s.\n", optarg
);
839 set_conffile(configfile
);
840 /* FIXME possibly check that config file exists. Even parse it */
842 case O(ASSEMBLE
,'s'): /* scan */
845 case O(INCREMENTAL
,'s'):
849 case O(MONITOR
,'m'): /* mail address */
850 case O(MONITOR
,EMail
):
852 pr_err("only specify one mailaddress. %s ignored.\n",
858 case O(MONITOR
,'p'): /* alert program */
859 case O(MONITOR
,ProgramOpt
): /* alert program */
861 pr_err("only specify one alter program. %s ignored.\n",
867 case O(MONITOR
,'r'): /* rebuild increments */
868 case O(MONITOR
,Increment
):
869 increments
= atoi(optarg
);
870 if (increments
> 99 || increments
< 1) {
871 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
876 case O(MONITOR
,'d'): /* delay in seconds */
878 case O(BUILD
,'d'): /* delay for bitmap updates */
881 pr_err("only specify delay once. %s ignored.\n",
884 c
.delay
= parse_num(optarg
);
886 pr_err("invalid delay: %s\n",
892 case O(MONITOR
,'f'): /* daemonise */
893 case O(MONITOR
,Fork
):
896 case O(MONITOR
,'i'): /* pid */
898 pr_err("only specify one pid file. %s ignored.\n",
903 case O(MONITOR
,'1'): /* oneshot */
907 case O(MONITOR
,'t'): /* test */
910 case O(MONITOR
,'y'): /* log messages to syslog */
911 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
914 case O(MONITOR
, NoSharing
):
918 /* now the general management options. Some are applicable
919 * to other modes. None have arguments.
924 case O(MANAGE
,Add
): /* add a drive */
927 case O(MANAGE
,ReAdd
):
930 case O(MANAGE
,'r'): /* remove a drive */
931 case O(MANAGE
,Remove
):
934 case O(MANAGE
,'f'): /* set faulty */
936 case O(INCREMENTAL
,'f'):
937 case O(INCREMENTAL
,Remove
):
938 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
939 * even though we will both fail and
940 * remove the device */
943 case O(MANAGE
,Replace
):
944 /* Mark these devices for replacement */
948 /* These are the replacements to use */
949 if (devmode
!= 'R') {
950 pr_err("--with must follow --replace\n");
955 case O(INCREMENTAL
,'R'):
957 case O(ASSEMBLE
,'R'):
959 case O(CREATE
,'R'): /* Run the array */
961 pr_err("Cannot both Stop and Run an array\n");
968 pr_err("Cannot both Run and Stop an array\n");
980 case O(MISC
,KillOpt
):
984 case O(MISC
, ExamineBB
):
988 case O(MISC
, WaitOpt
):
989 case O(MISC
, Waitclean
):
990 case O(MISC
, DetailPlatform
):
991 case O(MISC
, KillSubarray
):
992 case O(MISC
, UpdateSubarray
):
994 case O(MISC
, Restore
):
995 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
997 pr_err("subarray can only"
998 " be specified once\n");
1001 c
.subarray
= optarg
;
1003 if (devmode
&& devmode
!= opt
&&
1004 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
1005 pr_err("--examine/-E cannot be given with ");
1006 if (devmode
== 'E') {
1007 if (option_index
>= 0)
1008 fprintf(stderr
, "--%s\n",
1009 long_options
[option_index
].name
);
1011 fprintf(stderr
, "-%c\n", opt
);
1012 } else if (isalpha(devmode
))
1013 fprintf(stderr
, "-%c\n", devmode
);
1015 fprintf(stderr
, "previous option\n");
1019 if (opt
== Dump
|| opt
== Restore
) {
1020 if (dump_directory
!= NULL
) {
1021 pr_err("dump/restore directory specified twice: %s and %s\n",
1022 dump_directory
, optarg
);
1025 dump_directory
= optarg
;
1028 case O(MISC
, UdevRules
):
1029 if (devmode
&& devmode
!= opt
) {
1030 pr_err("--udev-rules must"
1031 " be the only option.\n");
1034 pr_err("only specify one udev "
1035 "rule filename. %s ignored.\n",
1038 udev_filename
= optarg
;
1046 case O(MISC
, Sparc22
):
1047 if (devmode
!= 'E') {
1048 pr_err("--sparc2.2 only allowed with --examine\n");
1054 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1055 case O(ASSEMBLE
,Bitmap
):
1057 pr_err("bitmap file needed with -b in --assemble mode\n");
1060 if (strcmp(optarg
, "internal")==0) {
1061 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1064 bitmap_fd
= open(optarg
, O_RDWR
);
1065 if (!*optarg
|| bitmap_fd
< 0) {
1066 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1069 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1072 case O(ASSEMBLE
, BackupFile
):
1073 case O(GROW
, BackupFile
):
1074 /* Specify a file into which grow might place a backup,
1075 * or from which assemble might recover a backup
1077 if (c
.backup_file
) {
1078 pr_err("backup file already specified, rejecting %s\n", optarg
);
1081 c
.backup_file
= optarg
;
1084 case O(GROW
, Continue
):
1085 /* Continue interrupted grow
1089 case O(ASSEMBLE
, InvalidBackup
):
1090 /* Acknowledge that the backupfile is invalid, but ask
1091 * to continue anyway
1093 c
.invalid_backup
= 1;
1097 case O(BUILD
,Bitmap
):
1099 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1101 case O(GROW
,Bitmap
):
1102 if (strcmp(optarg
, "internal")== 0 ||
1103 strcmp(optarg
, "none")== 0 ||
1104 strchr(optarg
, '/') != NULL
) {
1105 s
.bitmap_file
= optarg
;
1109 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1110 " not '%s'\n", optarg
);
1113 case O(GROW
,BitmapChunk
):
1114 case O(BUILD
,BitmapChunk
):
1115 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1116 s
.bitmap_chunk
= parse_size(optarg
);
1117 if (s
.bitmap_chunk
== 0 ||
1118 s
.bitmap_chunk
== INVALID_SECTORS
||
1119 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1120 pr_err("invalid bitmap chunksize: %s\n",
1124 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1127 case O(GROW
, WriteBehind
):
1128 case O(BUILD
, WriteBehind
):
1129 case O(CREATE
, WriteBehind
): /* write-behind mode */
1130 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1132 s
.write_behind
= parse_num(optarg
);
1133 if (s
.write_behind
< 0 ||
1134 s
.write_behind
> 16383) {
1135 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1141 case O(INCREMENTAL
, 'r'):
1142 case O(INCREMENTAL
, RebuildMapOpt
):
1145 case O(INCREMENTAL
, IncrementalPath
):
1146 remove_path
= optarg
;
1149 /* We have now processed all the valid options. Anything else is
1152 if (option_index
> 0)
1153 pr_err(":option --%s not valid in %s mode\n",
1154 long_options
[option_index
].name
,
1155 map_num(modes
, mode
));
1157 pr_err("option -%c not valid in %s mode\n",
1158 opt
, map_num(modes
, mode
));
1165 if (print_help
== 2)
1166 help_text
= OptionHelp
;
1168 help_text
= mode_help
[mode
];
1169 if (help_text
== NULL
)
1171 fputs(help_text
,stdout
);
1175 if (!mode
&& devs_found
) {
1178 if (devlist
->disposition
== 0)
1179 devlist
->disposition
= devmode
;
1182 fputs(Usage
, stderr
);
1187 struct createinfo
*ci
= conf_get_create_info();
1189 if (strcasecmp(symlinks
, "yes") == 0)
1191 else if (strcasecmp(symlinks
, "no") == 0)
1194 pr_err("option --symlinks must be 'no' or 'yes'\n");
1198 /* Ok, got the option parsing out of the way
1199 * hopefully it's mostly right but there might be some stuff
1202 * That is mosty checked in the per-mode stuff but...
1204 * For @,B,C and A without -s, the first device listed must be
1205 * an md device. We check that here and open it.
1208 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1210 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1211 if (devs_found
< 1) {
1212 pr_err("an md device must be given in this mode\n");
1215 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1216 pr_err("--super-minor=dev is incompatible with --auto\n");
1219 if (mode
== MANAGE
|| mode
== GROW
) {
1220 mdfd
= open_mddev(devlist
->devname
, 1);
1224 /* non-existent device is OK */
1225 mdfd
= open_mddev(devlist
->devname
, 0);
1227 pr_err("device %s exists but is not an "
1228 "md array.\n", devlist
->devname
);
1231 if ((int)ident
.super_minor
== -2) {
1234 pr_err("--super-minor=dev given, and "
1235 "listed device %s doesn't exist.\n",
1240 ident
.super_minor
= minor(stb
.st_rdev
);
1242 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1243 /* We don't really want this open yet, we just might
1244 * have wanted to check some things
1252 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1253 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1254 " a mistake. If you really mean it you will need to specify --force before\n"
1255 " setting the number of drives.\n");
1260 if (c
.homehost
== NULL
)
1261 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1262 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1263 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1264 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1265 c
.homehost
= sys_hostname
;
1268 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1270 c
.require_homehost
= 0;
1273 if (c
.backup_file
&& data_offset
!= INVALID_SECTORS
) {
1274 pr_err("--backup-file and --data-offset are incompatible\n");
1278 if ((mode
== MISC
&& devmode
== 'E')
1279 || (mode
== MONITOR
&& spare_sharing
== 0))
1280 /* Anyone may try this */;
1281 else if (geteuid() != 0) {
1282 pr_err("must be super-user to perform this action\n");
1286 ident
.autof
= c
.autof
;
1288 if (c
.scan
&& c
.verbose
< 2)
1289 /* --scan implied --brief unless -vv */
1295 /* readonly, add/remove, readwrite, runstop */
1297 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1298 if (!rv
&& devs_found
>1)
1299 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1300 devlist
->next
, c
.verbose
, c
.test
,
1302 if (!rv
&& c
.readonly
< 0)
1303 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1304 if (!rv
&& c
.runstop
> 0)
1305 rv
= Manage_run(devlist
->devname
, mdfd
, c
.verbose
);
1306 if (!rv
&& c
.runstop
< 0)
1307 rv
= Manage_stop(devlist
->devname
, mdfd
, c
.verbose
, 0);
1310 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1311 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1312 /* Only a device has been given, so get details from config file */
1313 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1314 if (array_ident
== NULL
) {
1315 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
, devlist
->devname
, array_ident
,
1327 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1329 else if (devs_found
> 0) {
1330 if (c
.update
&& devs_found
> 1) {
1331 pr_err("can only update a single array at a time\n");
1334 if (c
.backup_file
&& devs_found
> 1) {
1335 pr_err("can only assemble a single array when providing a backup file.\n");
1338 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1339 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1340 if (array_ident
== NULL
) {
1341 pr_err("%s not identified in config file.\n",
1346 if (array_ident
->autof
== 0)
1347 array_ident
->autof
= c
.autof
;
1348 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1353 pr_err("--update not meaningful with a --scan assembly.\n");
1356 if (c
.backup_file
) {
1357 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1360 rv
= scan_assemble(ss
, &c
, &ident
);
1366 c
.delay
= DEFAULT_BITMAP_DELAY
;
1367 if (s
.write_behind
&& !s
.bitmap_file
) {
1368 pr_err("write-behind mode requires a bitmap.\n");
1372 if (s
.raiddisks
== 0) {
1373 pr_err("no raid-devices specified.\n");
1378 if (s
.bitmap_file
) {
1379 if (strcmp(s
.bitmap_file
, "internal")==0) {
1380 pr_err("'internal' bitmaps not supported with --build\n");
1385 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1389 c
.delay
= DEFAULT_BITMAP_DELAY
;
1390 if (s
.write_behind
&& !s
.bitmap_file
) {
1391 pr_err("write-behind mode requires a bitmap.\n");
1395 if (s
.raiddisks
== 0) {
1396 pr_err("no raid-devices specified.\n");
1401 rv
= Create(ss
, devlist
->devname
,
1402 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1403 devs_found
-1, devlist
->next
,
1404 &s
, &c
, data_offset
);
1407 if (devmode
== 'E') {
1408 if (devlist
== NULL
&& !c
.scan
) {
1409 pr_err("No devices to examine\n");
1412 if (devlist
== NULL
)
1413 devlist
= conf_get_devs();
1414 if (devlist
== NULL
) {
1415 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1418 rv
= Examine(devlist
, &c
, ss
);
1419 } else if (devmode
== DetailPlatform
) {
1420 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1421 c
.verbose
, c
.export
,
1422 devlist
? devlist
->devname
: NULL
);
1423 } else if (devlist
== NULL
) {
1424 if (devmode
== 'S' && c
.scan
)
1425 rv
= stop_scan(c
.verbose
);
1426 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1427 rv
= misc_scan(devmode
, &c
);
1428 else if (devmode
== UdevRules
)
1429 rv
= Write_rules(udev_filename
);
1431 pr_err("No devices given.\n");
1435 rv
= misc_list(devlist
, &ident
, dump_directory
, ss
, &c
);
1438 if (!devlist
&& !c
.scan
) {
1439 pr_err("Cannot monitor: need --scan or at least one device\n");
1443 if (pidfile
&& !daemonise
) {
1444 pr_err("Cannot write a pid file when not in daemon mode\n");
1449 if (get_linux_version() > 2006016)
1450 /* mdstat responds to poll */
1457 rv
= Monitor(devlist
, mailaddr
, program
,
1458 &c
, daemonise
, oneshot
,
1459 dosyslog
, pidfile
, increments
,
1464 if (array_size
> 0) {
1465 /* alway impose array size first, independent of
1467 * Do not allow level or raid_disks changes at the
1468 * same time as that can be irreversibly destructive.
1472 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1473 pr_err("cannot change array size in same operation "
1474 "as changing raiddisks or level.\n"
1475 " Change size first, then check that data is still intact.\n");
1479 sysfs_init(&sra
, mdfd
, NULL
);
1480 if (array_size
== MAX_SIZE
)
1481 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1483 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1486 pr_err("--array-size setting"
1487 " is too large.\n");
1489 pr_err("current kernel does"
1490 " not support setting --array-size\n");
1495 if (devs_found
> 1 && s
.raiddisks
== 0) {
1497 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1498 pr_err("--add cannot be used with "
1499 "other geometry changes in --grow mode\n");
1503 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1504 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1509 } else if (s
.bitmap_file
) {
1510 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1511 s
.layout_str
!= NULL
|| devs_found
> 1) {
1512 pr_err("--bitmap changes cannot be "
1513 "used with other geometry changes "
1514 "in --grow mode\n");
1519 c
.delay
= DEFAULT_BITMAP_DELAY
;
1520 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1521 } else if (grow_continue
)
1522 rv
= Grow_continue_command(devlist
->devname
,
1523 mdfd
, c
.backup_file
,
1525 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1526 || s
.chunk
!= 0 || s
.level
!= UnSet
1527 || data_offset
!= INVALID_SECTORS
) {
1528 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1530 data_offset
, &c
, &s
);
1531 } else if (array_size
== 0)
1532 pr_err("no changes to --grow\n");
1539 if (c
.runstop
<= 0) {
1540 pr_err("--incremental --scan meaningless without --run.\n");
1543 if (devmode
== 'f') {
1544 pr_err("--incremental --scan --fail not supported.\n");
1547 rv
= IncrementalScan(c
.verbose
);
1550 if (!rebuild_map
&& !c
.scan
) {
1551 pr_err("--incremental requires a device.\n");
1556 if (devlist
->next
) {
1557 pr_err("--incremental can only handle one device.\n");
1562 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1565 rv
= Incremental(devlist
->devname
, &c
, ss
);
1574 static int scan_assemble(struct supertype
*ss
,
1576 struct mddev_ident
*ident
)
1578 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1579 struct mddev_dev
*devlist
= conf_get_devs();
1580 struct map_ent
*map
= NULL
;
1583 int failures
, successes
;
1585 if (conf_verify_devnames(array_list
)) {
1586 pr_err("Duplicate MD device names in "
1587 "conf file were found.\n");
1590 if (devlist
== NULL
) {
1591 pr_err("No devices listed in conf file were found.\n");
1594 for (a
= array_list
; a
; a
= a
->next
) {
1597 a
->autof
= c
->autof
;
1600 pr_err("%s: failed to get "
1601 "exclusive lock on mapfile\n",
1607 for (a
= array_list
; a
; a
= a
->next
) {
1612 strcasecmp(a
->devname
, "<ignore>") == 0)
1615 r
= Assemble(ss
, a
->devname
,
1625 } while (failures
&& successes
);
1626 if (c
->homehost
&& cnt
== 0) {
1627 /* Maybe we can auto-assemble something.
1628 * Repeatedly call Assemble in auto-assemble mode
1633 ident
->autof
= c
->autof
;
1635 struct mddev_dev
*devlist
= conf_get_devs();
1638 rv2
= Assemble(ss
, NULL
,
1646 /* Incase there are stacked devices, we need to go around again */
1648 if (cnt
== 0 && rv
== 0) {
1649 pr_err("No arrays found in config file or automatically\n");
1653 } else if (cnt
== 0 && rv
== 0) {
1654 pr_err("No arrays found in config file\n");
1661 static int misc_scan(char devmode
, struct context
*c
)
1663 /* apply --detail or --wait-clean to
1664 * all devices in /proc/mdstat
1666 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1667 struct mdstat_ent
*e
;
1668 struct map_ent
*map
= NULL
;
1672 for (members
= 0; members
<= 1; members
++) {
1673 for (e
=ms
; e
; e
=e
->next
) {
1677 int member
= e
->metadata_version
&&
1678 strncmp(e
->metadata_version
,
1679 "external:/", 10) == 0;
1680 if (members
!= member
)
1682 me
= map_by_devnm(&map
, e
->devnm
);
1684 && strcmp(me
->path
, "/unknown") != 0)
1687 stat(name
, &stb
) != 0)
1688 name
= get_md_name(e
->devnm
);
1691 pr_err("cannot find device file for %s\n",
1696 rv
|= Detail(name
, c
);
1698 rv
|= WaitClean(name
, -1, c
->verbose
);
1706 static int stop_scan(int verbose
)
1708 /* apply --stop to all devices in /proc/mdstat */
1709 /* Due to possible stacking of devices, repeat until
1710 * nothing more can be stopped
1712 int progress
=1, err
;
1716 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1717 struct mdstat_ent
*e
;
1719 if (!progress
) last
= 1;
1720 progress
= 0; err
= 0;
1721 for (e
=ms
; e
; e
=e
->next
) {
1722 char *name
= get_md_name(e
->devnm
);
1726 pr_err("cannot find device file for %s\n",
1730 mdfd
= open_mddev(name
, 1);
1732 if (Manage_stop(name
, mdfd
, verbose
, !last
))
1742 } while (!last
&& err
);
1748 static int misc_list(struct mddev_dev
*devlist
,
1749 struct mddev_ident
*ident
,
1750 char *dump_directory
,
1751 struct supertype
*ss
, struct context
*c
)
1753 struct mddev_dev
*dv
;
1756 for (dv
=devlist
; dv
; dv
=(rv
& 16) ? NULL
: dv
->next
) {
1759 switch(dv
->disposition
) {
1761 rv
|= Detail(dv
->devname
, c
);
1763 case KillOpt
: /* Zero superblock */
1765 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1769 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1776 rv
|= Query(dv
->devname
); continue;
1778 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1780 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1783 rv
|= Wait(dv
->devname
); continue;
1785 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1787 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1789 case UpdateSubarray
:
1790 if (c
->update
== NULL
) {
1791 pr_err("-U/--update must be specified with --update-subarray\n");
1795 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1796 c
->update
, ident
, c
->verbose
);
1799 rv
|= Dump_metadata(dv
->devname
, dump_directory
, c
, ss
);
1802 rv
|= Restore_metadata(dv
->devname
, dump_directory
, c
, ss
,
1803 (dv
== devlist
&& dv
->next
== NULL
));
1806 mdfd
= open_mddev(dv
->devname
, 1);
1808 switch(dv
->disposition
) {
1810 rv
|= Manage_run(dv
->devname
, mdfd
, c
->verbose
); break;
1812 rv
|= Manage_stop(dv
->devname
, mdfd
, c
->verbose
, 0); break;
1814 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1816 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;