2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2013 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.
32 static int scan_assemble(struct supertype
*ss
,
34 struct mddev_ident
*ident
);
35 static int misc_scan(char devmode
, struct context
*c
);
36 static int stop_scan(int verbose
);
37 static int misc_list(struct mddev_dev
*devlist
,
38 struct mddev_ident
*ident
,
40 struct supertype
*ss
, struct context
*c
);
41 const char Name
[] = "mdadm";
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
;
97 char *dump_directory
= NULL
;
104 srandom(time(0) ^ getpid());
108 ident
.raid_disks
= UnSet
;
109 ident
.super_minor
= UnSet
;
111 ident
.spare_group
= NULL
;
114 ident
.bitmap_fd
= -1;
115 ident
.bitmap_file
= NULL
;
117 ident
.container
= NULL
;
120 while ((option_index
= -1) ,
121 (opt
=getopt_long(argc
, argv
,
122 shortopt
, long_options
,
123 &option_index
)) != -1) {
125 /* firstly, some mode-independent options */
135 fputs(Version
, stderr
);
138 case 'v': c
.verbose
++;
141 case 'q': c
.verbose
--;
145 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
146 || mode
== GROW
|| mode
== INCREMENTAL
148 break; /* b means bitmap */
153 case 'Y': c
.export
++;
157 if (strcasecmp(optarg
, "<ignore>") == 0)
158 c
.require_homehost
= 0;
164 /* Silently ignore old option */
170 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
176 fputs(Usage
, stderr
);
179 /* second, figure out the mode.
180 * Some options force the mode. Others
181 * set the mode if it isn't already
187 shortopt
= short_bitmap_options
;
198 case ReAdd
: /* re-add */
202 shortopt
= short_bitmap_options
;
206 case 'A': newmode
= ASSEMBLE
;
207 shortopt
= short_bitmap_auto_options
;
209 case 'B': newmode
= BUILD
;
210 shortopt
= short_bitmap_auto_options
;
212 case 'C': newmode
= CREATE
;
213 shortopt
= short_bitmap_auto_options
;
215 case 'F': newmode
= MONITOR
;
217 case 'G': newmode
= GROW
;
218 shortopt
= short_bitmap_options
;
220 case 'I': newmode
= INCREMENTAL
;
221 shortopt
= short_bitmap_auto_options
;
224 newmode
= AUTODETECT
;
259 if (mode
&& newmode
== mode
) {
260 /* everybody happy ! */
261 } else if (mode
&& newmode
!= mode
) {
264 if (option_index
>= 0)
265 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
267 fprintf(stderr
, "-%c", opt
);
268 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
269 map_num(modes
, newmode
),
270 map_num(modes
, mode
));
272 } else if (!mode
&& newmode
) {
274 if (mode
== MISC
&& devs_found
) {
275 pr_err("No action given for %s in --misc mode\n",
277 cont_err("Action options must come before device names\n");
281 /* special case of -c --help */
282 if ((opt
== 'c' || opt
== ConfigFile
) &&
283 ( strncmp(optarg
, "--h", 3)==0 ||
284 strncmp(optarg
, "-h", 2)==0)) {
285 fputs(Help_config
, stdout
);
289 /* If first option is a device, don't force the mode yet */
291 if (devs_found
== 0) {
292 dv
= xmalloc(sizeof(*dv
));
293 dv
->devname
= optarg
;
294 dv
->disposition
= devmode
;
295 dv
->writemostly
= writemostly
;
299 devlistend
= &dv
->next
;
304 /* No mode yet, and this is the second device ... */
305 pr_err("An option must be given to set the mode before a second device\n"
306 " (%s) is listed\n", optarg
);
309 if (option_index
>= 0)
310 pr_err("--%s", long_options
[option_index
].name
);
313 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
317 /* if we just set the mode, then done */
331 /* an undecorated option - must be a device name.
334 if (devs_found
> 0 && devmode
== DetailPlatform
) {
335 pr_err("controller may only be specified once. %s ignored\n",
340 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
341 pr_err("Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
344 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
345 pr_err("Must give -a/--add for devices to add: %s\n", optarg
);
348 dv
= xmalloc(sizeof(*dv
));
349 dv
->devname
= optarg
;
350 dv
->disposition
= devmode
;
351 dv
->writemostly
= writemostly
;
355 devlistend
= &dv
->next
;
361 /* We've got a mode, and opt is now something else which
362 * could depend on the mode */
363 #define O(a,b) ((a<<16)|b)
364 switch (O(mode
,opt
)) {
366 case O(GROW
,ChunkSize
):
368 case O(CREATE
,ChunkSize
):
369 case O(BUILD
,'c'): /* chunk or rounding */
370 case O(BUILD
,ChunkSize
): /* chunk or rounding */
372 pr_err("chunk/rounding may only be specified once. Second value is %s.\n", optarg
);
375 s
.chunk
= parse_size(optarg
);
376 if (s
.chunk
== INVALID_SECTORS
||
377 s
.chunk
< 8 || (s
.chunk
&1)) {
378 pr_err("invalid chunk/rounding value: %s\n",
382 /* Convert sectors to K */
386 case O(INCREMENTAL
, 'e'):
388 case O(ASSEMBLE
,'e'):
389 case O(MISC
,'e'): /* set metadata (superblock) information */
391 pr_err("metadata information already given\n");
394 for(i
=0; !ss
&& superlist
[i
]; i
++)
395 ss
= superlist
[i
]->match_metadata_desc(optarg
);
398 pr_err("unrecognised metadata identifier: %s\n", optarg
);
404 case O(MANAGE
,WriteMostly
):
406 case O(BUILD
,WriteMostly
):
408 case O(CREATE
,WriteMostly
):
409 /* set write-mostly for following devices */
414 /* clear write-mostly for following devices */
420 case O(BUILD
,'z'): /* size */
422 pr_err("size may only be specified once. Second value is %s.\n", optarg
);
425 if (strcmp(optarg
, "max")==0)
428 s
.size
= parse_size(optarg
);
429 if (s
.size
== INVALID_SECTORS
||
431 pr_err("invalid size: %s\n",
435 /* convert sectors to K */
440 case O(GROW
,'Z'): /* array size */
441 if (array_size
> 0) {
442 pr_err("array-size may only be specified once. Second value is %s.\n", optarg
);
445 if (strcmp(optarg
, "max") == 0)
446 array_size
= MAX_SIZE
;
448 array_size
= parse_size(optarg
);
449 if (array_size
== 0 ||
450 array_size
== INVALID_SECTORS
) {
451 pr_err("invalid array size: %s\n",
458 case O(CREATE
,DataOffset
):
459 case O(GROW
,DataOffset
):
460 if (data_offset
!= INVALID_SECTORS
) {
461 pr_err("data-offset may only be specified one. Second value is %s.\n", optarg
);
464 if (mode
== CREATE
&&
465 strcmp(optarg
, "variable") == 0)
466 data_offset
= VARIABLE_OFFSET
;
468 data_offset
= parse_size(optarg
);
469 if (data_offset
== INVALID_SECTORS
) {
470 pr_err("invalid data-offset: %s\n",
478 case O(BUILD
,'l'): /* set raid level*/
479 if (s
.level
!= UnSet
) {
480 pr_err("raid level may only be set once. Second value is %s.\n", optarg
);
483 s
.level
= map_name(pers
, optarg
);
484 if (s
.level
== UnSet
) {
485 pr_err("invalid raid level: %s\n",
489 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
490 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
493 pr_err("Raid level %s not permitted with --build.\n",
497 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
498 pr_err("raid level %s is incompatible with spare-devices setting.\n",
502 ident
.level
= s
.level
;
505 case O(GROW
, 'p'): /* new layout */
506 case O(GROW
, Layout
):
508 pr_err("layout may only be sent once. Second value was %s\n", optarg
);
511 s
.layout_str
= optarg
;
512 /* 'Grow' will parse the value */
515 case O(CREATE
,'p'): /* raid5 layout */
516 case O(CREATE
,Layout
):
517 case O(BUILD
,'p'): /* faulty layout */
518 case O(BUILD
,Layout
):
519 if (s
.layout
!= UnSet
) {
520 pr_err("layout may only be sent once. Second value was %s\n", optarg
);
525 pr_err("layout not meaningful for %s arrays.\n",
526 map_num(pers
, s
.level
));
529 pr_err("raid level must be given before layout.\n");
533 s
.layout
= map_name(r5layout
, optarg
);
534 if (s
.layout
==UnSet
) {
535 pr_err("layout %s not understood for raid5.\n",
541 s
.layout
= map_name(r6layout
, optarg
);
542 if (s
.layout
==UnSet
) {
543 pr_err("layout %s not understood for raid6.\n",
550 s
.layout
= parse_layout_10(optarg
);
552 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
560 s
.layout
= parse_layout_faulty(optarg
);
561 if (s
.layout
== -1) {
562 pr_err("layout %s not understood for faulty.\n",
570 case O(CREATE
,AssumeClean
):
571 case O(BUILD
,AssumeClean
): /* assume clean */
572 case O(GROW
,AssumeClean
):
578 case O(BUILD
,'n'): /* number of raid disks */
580 pr_err("raid-devices set twice: %d and %s\n",
581 s
.raiddisks
, optarg
);
584 s
.raiddisks
= parse_num(optarg
);
585 if (s
.raiddisks
<= 0) {
586 pr_err("invalid number of raid devices: %s\n",
590 ident
.raid_disks
= s
.raiddisks
;
592 case O(CREATE
, Nodes
):
593 c
.nodes
= parse_num(optarg
);
595 pr_err("invalid number for the number of cluster nodes: %s\n",
600 case O(CREATE
, ClusterName
):
601 c
.homecluster
= optarg
;
602 if (strlen(c
.homecluster
) > 64) {
603 pr_err("Cluster name too big.\n");
607 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
609 pr_err("spare-devices set twice: %d and %s\n",
610 s
.sparedisks
, optarg
);
613 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
614 pr_err("spare-devices setting is incompatible with raid level %d\n",
618 s
.sparedisks
= parse_num(optarg
);
619 if (s
.sparedisks
< 0) {
620 pr_err("invalid number of spare-devices: %s\n",
630 case O(INCREMENTAL
,'a'):
631 case O(INCREMENTAL
,Auto
):
632 case O(ASSEMBLE
,'a'):
633 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
634 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
637 case O(CREATE
,Symlinks
):
638 case O(BUILD
,Symlinks
):
639 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
643 case O(BUILD
,'f'): /* force honouring '-n 1' */
644 case O(BUILD
,Force
): /* force honouring '-n 1' */
645 case O(GROW
,'f'): /* ditto */
646 case O(GROW
,Force
): /* ditto */
647 case O(CREATE
,'f'): /* force honouring of device list */
648 case O(CREATE
,Force
): /* force honouring of device list */
649 case O(ASSEMBLE
,'f'): /* force assembly */
650 case O(ASSEMBLE
,Force
): /* force assembly */
651 case O(MISC
,'f'): /* force zero */
652 case O(MISC
,Force
): /* force zero */
653 case O(MANAGE
,Force
): /* add device which is too large */
656 /* now for the Assemble options */
657 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
659 case O(INCREMENTAL
, FreezeReshape
):
660 c
.freeze_reshape
= 1;
662 case O(CREATE
,'u'): /* uuid of array */
663 case O(ASSEMBLE
,'u'): /* uuid of array */
664 if (ident
.uuid_set
) {
665 pr_err("uuid cannot be set twice. Second value %s.\n", optarg
);
668 if (parse_uuid(optarg
, ident
.uuid
))
671 pr_err("Bad uuid: %s\n", optarg
);
677 case O(ASSEMBLE
,'N'):
680 pr_err("name cannot be set twice. Second value %s.\n", optarg
);
683 if (mode
== MISC
&& !c
.subarray
) {
684 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
687 if (strlen(optarg
) > 32) {
688 pr_err("name '%s' is too long, 32 chars max.\n",
692 strcpy(ident
.name
, optarg
);
695 case O(ASSEMBLE
,'m'): /* super-minor for array */
696 case O(ASSEMBLE
,SuperMinor
):
697 if (ident
.super_minor
!= UnSet
) {
698 pr_err("super-minor cannot be set twice. 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 of superblock, both %s and %s given.\n",
725 if (mode
== MISC
&& !c
.subarray
) {
726 pr_err("Only subarrays can be updated in misc mode\n");
730 if (strcmp(c
.update
, "sparc2.2")==0)
732 if (strcmp(c
.update
, "super-minor") == 0)
734 if (strcmp(c
.update
, "summaries")==0)
736 if (strcmp(c
.update
, "resync")==0)
738 if (strcmp(c
.update
, "uuid")==0)
740 if (strcmp(c
.update
, "name")==0)
742 if (strcmp(c
.update
, "homehost")==0)
744 if (strcmp(c
.update
, "devicesize")==0)
746 if (strcmp(c
.update
, "no-bitmap")==0)
748 if (strcmp(c
.update
, "bbl") == 0)
750 if (strcmp(c
.update
, "no-bbl") == 0)
752 if (strcmp(c
.update
, "metadata") == 0)
754 if (strcmp(c
.update
, "revert-reshape") == 0)
756 if (strcmp(c
.update
, "byteorder")==0) {
758 pr_err("must not set metadata type with --update=byteorder.\n");
761 for(i
=0; !ss
&& superlist
[i
]; i
++)
762 ss
= superlist
[i
]->match_metadata_desc(
765 pr_err("INTERNAL ERROR cannot find 0.swap\n");
771 if (strcmp(c
.update
,"?") == 0 ||
772 strcmp(c
.update
, "help") == 0) {
774 fprintf(outf
, "%s: ", Name
);
778 "%s: '--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"
784 " 'no-bitmap', 'metadata', 'revert-reshape'\n"
787 exit(outf
== stdout
? 0 : 2);
790 /* update=devicesize is allowed with --re-add */
791 if (devmode
!= 'A') {
792 pr_err("--update in Manage mode only allowed with --re-add.\n");
796 pr_err("Can only update one aspect 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 updated with --re-add\n");
809 case O(INCREMENTAL
,NoDegraded
):
810 pr_err("--no-degraded is deprecated in Incremental mode\n");
811 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
812 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
813 * so we overload slightly */
816 case O(ASSEMBLE
,'c'):
817 case O(ASSEMBLE
,ConfigFile
):
818 case O(INCREMENTAL
, 'c'):
819 case O(INCREMENTAL
, ConfigFile
):
821 case O(MISC
, ConfigFile
):
823 case O(MONITOR
,ConfigFile
):
824 case O(CREATE
,ConfigFile
):
826 pr_err("configfile cannot be set twice. Second value is %s.\n", optarg
);
830 set_conffile(configfile
);
831 /* FIXME possibly check that config file exists. Even parse it */
833 case O(ASSEMBLE
,'s'): /* scan */
836 case O(INCREMENTAL
,'s'):
840 case O(MONITOR
,'m'): /* mail address */
841 case O(MONITOR
,EMail
):
843 pr_err("only specify one mailaddress. %s ignored.\n",
849 case O(MONITOR
,'p'): /* alert program */
850 case O(MONITOR
,ProgramOpt
): /* alert program */
852 pr_err("only specify one alter program. %s ignored.\n",
858 case O(MONITOR
,'r'): /* rebuild increments */
859 case O(MONITOR
,Increment
):
860 increments
= atoi(optarg
);
861 if (increments
> 99 || increments
< 1) {
862 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
867 case O(MONITOR
,'d'): /* delay in seconds */
869 case O(BUILD
,'d'): /* delay for bitmap updates */
872 pr_err("only specify delay once. %s ignored.\n",
875 c
.delay
= parse_num(optarg
);
877 pr_err("invalid delay: %s\n",
883 case O(MONITOR
,'f'): /* daemonise */
884 case O(MONITOR
,Fork
):
887 case O(MONITOR
,'i'): /* pid */
889 pr_err("only specify one pid file. %s ignored.\n",
894 case O(MONITOR
,'1'): /* oneshot */
898 case O(MONITOR
,'t'): /* test */
901 case O(MONITOR
,'y'): /* log messages to syslog */
902 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
905 case O(MONITOR
, NoSharing
):
909 /* now the general management options. Some are applicable
910 * to other modes. None have arguments.
915 case O(MANAGE
,Add
): /* add a drive */
918 case O(MANAGE
,AddSpare
): /* add drive - never re-add */
921 case O(MANAGE
,ReAdd
):
924 case O(MANAGE
,'r'): /* remove a drive */
925 case O(MANAGE
,Remove
):
928 case O(MANAGE
,'f'): /* set faulty */
930 case O(INCREMENTAL
,'f'):
931 case O(INCREMENTAL
,Remove
):
932 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
933 * even though we will both fail and
934 * remove the device */
937 case O(MANAGE
, ClusterConfirm
):
940 case O(MANAGE
,Replace
):
941 /* Mark these devices for replacement */
945 /* These are the replacements to use */
946 if (devmode
!= 'R') {
947 pr_err("--with must follow --replace\n");
952 case O(INCREMENTAL
,'R'):
954 case O(ASSEMBLE
,'R'):
956 case O(CREATE
,'R'): /* Run the array */
958 pr_err("Cannot both Stop and Run an array\n");
965 pr_err("Cannot both Run and Stop an array\n");
977 case O(MISC
,KillOpt
):
981 case O(MISC
, ExamineBB
):
985 case O(MISC
, WaitOpt
):
986 case O(MISC
, Waitclean
):
987 case O(MISC
, DetailPlatform
):
988 case O(MISC
, KillSubarray
):
989 case O(MISC
, UpdateSubarray
):
991 case O(MISC
, Restore
):
992 case O(MISC
,Action
):
993 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
995 pr_err("subarray can only be specified once\n");
1000 if (opt
== Action
) {
1002 pr_err("Only one --action can be specified\n");
1005 if (strcmp(optarg
, "idle") == 0 ||
1006 strcmp(optarg
, "frozen") == 0 ||
1007 strcmp(optarg
, "check") == 0 ||
1008 strcmp(optarg
, "repair") == 0)
1011 pr_err("action must be one of idle, frozen, check, repair\n");
1015 if (devmode
&& devmode
!= opt
&&
1016 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
1017 pr_err("--examine/-E cannot be given with ");
1018 if (devmode
== 'E') {
1019 if (option_index
>= 0)
1020 fprintf(stderr
, "--%s\n",
1021 long_options
[option_index
].name
);
1023 fprintf(stderr
, "-%c\n", opt
);
1024 } else if (isalpha(devmode
))
1025 fprintf(stderr
, "-%c\n", devmode
);
1027 fprintf(stderr
, "previous option\n");
1031 if (opt
== Dump
|| opt
== Restore
) {
1032 if (dump_directory
!= NULL
) {
1033 pr_err("dump/restore directory specified twice: %s and %s\n",
1034 dump_directory
, optarg
);
1037 dump_directory
= optarg
;
1040 case O(MISC
, UdevRules
):
1041 if (devmode
&& devmode
!= opt
) {
1042 pr_err("--udev-rules must be the only option.\n");
1045 pr_err("only specify one udev rule filename. %s ignored.\n",
1048 udev_filename
= optarg
;
1056 case O(MISC
, Sparc22
):
1057 if (devmode
!= 'E') {
1058 pr_err("--sparc2.2 only allowed with --examine\n");
1064 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1065 case O(ASSEMBLE
,Bitmap
):
1067 pr_err("bitmap file needed with -b in --assemble mode\n");
1070 if (strcmp(optarg
, "internal")==0) {
1071 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1074 bitmap_fd
= open(optarg
, O_RDWR
);
1075 if (!*optarg
|| bitmap_fd
< 0) {
1076 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1079 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1082 case O(ASSEMBLE
, BackupFile
):
1083 case O(GROW
, BackupFile
):
1084 /* Specify a file into which grow might place a backup,
1085 * or from which assemble might recover a backup
1087 if (c
.backup_file
) {
1088 pr_err("backup file already specified, rejecting %s\n", optarg
);
1091 c
.backup_file
= optarg
;
1094 case O(GROW
, Continue
):
1095 /* Continue interrupted grow
1099 case O(ASSEMBLE
, InvalidBackup
):
1100 /* Acknowledge that the backupfile is invalid, but ask
1101 * to continue anyway
1103 c
.invalid_backup
= 1;
1107 case O(BUILD
,Bitmap
):
1109 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1111 case O(GROW
,Bitmap
):
1112 if (strcmp(optarg
, "internal")== 0 ||
1113 strcmp(optarg
, "none")== 0 ||
1114 strchr(optarg
, '/') != NULL
) {
1115 s
.bitmap_file
= optarg
;
1118 if (strcmp(optarg
, "clustered")== 0) {
1119 s
.bitmap_file
= optarg
;
1120 /* Set the default number of cluster nodes
1121 * to 4 if not already set by user
1128 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1129 " not '%s'\n", optarg
);
1132 case O(GROW
,BitmapChunk
):
1133 case O(BUILD
,BitmapChunk
):
1134 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1135 s
.bitmap_chunk
= parse_size(optarg
);
1136 if (s
.bitmap_chunk
== 0 ||
1137 s
.bitmap_chunk
== INVALID_SECTORS
||
1138 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1139 pr_err("invalid bitmap chunksize: %s\n",
1143 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1146 case O(GROW
, WriteBehind
):
1147 case O(BUILD
, WriteBehind
):
1148 case O(CREATE
, WriteBehind
): /* write-behind mode */
1149 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1151 s
.write_behind
= parse_num(optarg
);
1152 if (s
.write_behind
< 0 ||
1153 s
.write_behind
> 16383) {
1154 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1160 case O(INCREMENTAL
, 'r'):
1161 case O(INCREMENTAL
, RebuildMapOpt
):
1164 case O(INCREMENTAL
, IncrementalPath
):
1165 remove_path
= optarg
;
1168 /* We have now processed all the valid options. Anything else is
1171 if (option_index
> 0)
1172 pr_err(":option --%s not valid in %s mode\n",
1173 long_options
[option_index
].name
,
1174 map_num(modes
, mode
));
1176 pr_err("option -%c not valid in %s mode\n",
1177 opt
, map_num(modes
, mode
));
1184 if (print_help
== 2)
1185 help_text
= OptionHelp
;
1187 help_text
= mode_help
[mode
];
1188 if (help_text
== NULL
)
1190 fputs(help_text
,stdout
);
1194 if (!mode
&& devs_found
) {
1197 if (devlist
->disposition
== 0)
1198 devlist
->disposition
= devmode
;
1201 fputs(Usage
, stderr
);
1206 struct createinfo
*ci
= conf_get_create_info();
1208 if (strcasecmp(symlinks
, "yes") == 0)
1210 else if (strcasecmp(symlinks
, "no") == 0)
1213 pr_err("option --symlinks must be 'no' or 'yes'\n");
1217 /* Ok, got the option parsing out of the way
1218 * hopefully it's mostly right but there might be some stuff
1221 * That is mosty checked in the per-mode stuff but...
1223 * For @,B,C and A without -s, the first device listed must be
1224 * an md device. We check that here and open it.
1227 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1229 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1230 if (devs_found
< 1) {
1231 pr_err("an md device must be given in this mode\n");
1234 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1235 pr_err("--super-minor=dev is incompatible with --auto\n");
1238 if (mode
== MANAGE
|| mode
== GROW
) {
1239 mdfd
= open_mddev(devlist
->devname
, 1);
1243 /* non-existent device is OK */
1244 mdfd
= open_mddev(devlist
->devname
, 0);
1246 pr_err("device %s exists but is not an md array.\n", devlist
->devname
);
1249 if ((int)ident
.super_minor
== -2) {
1252 pr_err("--super-minor=dev given, and listed device %s doesn't exist.\n",
1257 ident
.super_minor
= minor(stb
.st_rdev
);
1259 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1260 /* We don't really want this open yet, we just might
1261 * have wanted to check some things
1269 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1270 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1271 " a mistake. If you really mean it you will need to specify --force before\n"
1272 " setting the number of drives.\n");
1277 if (c
.homehost
== NULL
)
1278 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1279 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1280 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1281 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1282 c
.homehost
= sys_hostname
;
1285 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1287 c
.require_homehost
= 0;
1290 if (c
.homecluster
== NULL
&& (c
.nodes
> 0)) {
1291 c
.homecluster
= conf_get_homecluster();
1292 if (c
.homecluster
== NULL
)
1293 rv
= get_cluster_name(&c
.homecluster
);
1295 pr_err("The md can't get cluster name\n");
1300 if (c
.backup_file
&& data_offset
!= INVALID_SECTORS
) {
1301 pr_err("--backup-file and --data-offset are incompatible\n");
1305 if ((mode
== MISC
&& devmode
== 'E')
1306 || (mode
== MONITOR
&& spare_sharing
== 0))
1307 /* Anyone may try this */;
1308 else if (geteuid() != 0) {
1309 pr_err("must be super-user to perform this action\n");
1313 ident
.autof
= c
.autof
;
1315 if (c
.scan
&& c
.verbose
< 2)
1316 /* --scan implied --brief unless -vv */
1322 /* readonly, add/remove, readwrite, runstop */
1324 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1325 if (!rv
&& devs_found
>1)
1326 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1327 devlist
->next
, c
.verbose
, c
.test
,
1329 if (!rv
&& c
.readonly
< 0)
1330 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1331 if (!rv
&& c
.runstop
> 0)
1332 rv
= Manage_run(devlist
->devname
, mdfd
, &c
);
1333 if (!rv
&& c
.runstop
< 0)
1334 rv
= Manage_stop(devlist
->devname
, mdfd
, c
.verbose
, 0);
1337 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1338 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1339 /* Only a device has been given, so get details from config file */
1340 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1341 if (array_ident
== NULL
) {
1342 pr_err("%s not identified in config file.\n",
1348 if (array_ident
->autof
== 0)
1349 array_ident
->autof
= c
.autof
;
1350 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1354 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1356 else if (devs_found
> 0) {
1357 if (c
.update
&& devs_found
> 1) {
1358 pr_err("can only update a single array at a time\n");
1361 if (c
.backup_file
&& devs_found
> 1) {
1362 pr_err("can only assemble a single array when providing a backup file.\n");
1365 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1366 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1367 if (array_ident
== NULL
) {
1368 pr_err("%s not identified in config file.\n",
1373 if (array_ident
->autof
== 0)
1374 array_ident
->autof
= c
.autof
;
1375 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1380 pr_err("--update not meaningful with a --scan assembly.\n");
1383 if (c
.backup_file
) {
1384 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1387 rv
= scan_assemble(ss
, &c
, &ident
);
1393 c
.delay
= DEFAULT_BITMAP_DELAY
;
1394 if (s
.write_behind
&& !s
.bitmap_file
) {
1395 pr_err("write-behind mode requires a bitmap.\n");
1399 if (s
.raiddisks
== 0) {
1400 pr_err("no raid-devices specified.\n");
1405 if (s
.bitmap_file
) {
1406 if (strcmp(s
.bitmap_file
, "internal")==0) {
1407 pr_err("'internal' bitmaps not supported with --build\n");
1412 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1416 c
.delay
= DEFAULT_BITMAP_DELAY
;
1419 if (!s
.bitmap_file
|| strcmp(s
.bitmap_file
, "clustered") != 0) {
1420 pr_err("--nodes argument only compatible with --bitmap=clustered\n");
1426 pr_err("--bitmap=clustered is currently supported with RAID mirror only\n");
1432 if (s
.write_behind
&& !s
.bitmap_file
) {
1433 pr_err("write-behind mode requires a bitmap.\n");
1437 if (s
.raiddisks
== 0) {
1438 pr_err("no raid-devices specified.\n");
1443 rv
= Create(ss
, devlist
->devname
,
1444 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1445 devs_found
-1, devlist
->next
,
1446 &s
, &c
, data_offset
);
1449 if (devmode
== 'E') {
1450 if (devlist
== NULL
&& !c
.scan
) {
1451 pr_err("No devices to examine\n");
1454 if (devlist
== NULL
)
1455 devlist
= conf_get_devs();
1456 if (devlist
== NULL
) {
1457 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1460 rv
= Examine(devlist
, &c
, ss
);
1461 } else if (devmode
== DetailPlatform
) {
1462 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1463 c
.verbose
, c
.export
,
1464 devlist
? devlist
->devname
: NULL
);
1465 } else if (devlist
== NULL
) {
1466 if (devmode
== 'S' && c
.scan
)
1467 rv
= stop_scan(c
.verbose
);
1468 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1469 rv
= misc_scan(devmode
, &c
);
1470 else if (devmode
== UdevRules
)
1471 rv
= Write_rules(udev_filename
);
1473 pr_err("No devices given.\n");
1477 rv
= misc_list(devlist
, &ident
, dump_directory
, ss
, &c
);
1480 if (!devlist
&& !c
.scan
) {
1481 pr_err("Cannot monitor: need --scan or at least one device\n");
1485 if (pidfile
&& !daemonise
) {
1486 pr_err("Cannot write a pid file when not in daemon mode\n");
1491 if (get_linux_version() > 2006016)
1492 /* mdstat responds to poll */
1499 rv
= Monitor(devlist
, mailaddr
, program
,
1500 &c
, daemonise
, oneshot
,
1501 dosyslog
, pidfile
, increments
,
1506 if (array_size
> 0) {
1507 /* alway impose array size first, independent of
1509 * Do not allow level or raid_disks changes at the
1510 * same time as that can be irreversibly destructive.
1514 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1515 pr_err("cannot change array size in same operation as changing raiddisks or level.\n"
1516 " Change size first, then check that data is still intact.\n");
1520 sysfs_init(&sra
, mdfd
, NULL
);
1521 if (array_size
== MAX_SIZE
)
1522 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1524 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1527 pr_err("--array-size setting is too large.\n");
1529 pr_err("current kernel does not support setting --array-size\n");
1534 if (devs_found
> 1 && s
.raiddisks
== 0 && s
.level
== UnSet
) {
1536 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1537 pr_err("--add cannot be used with other geometry changes in --grow mode\n");
1541 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1542 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1547 } else if (s
.bitmap_file
) {
1548 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1549 s
.layout_str
!= NULL
|| devs_found
> 1) {
1550 pr_err("--bitmap changes cannot be used with other geometry changes in --grow mode\n");
1555 c
.delay
= DEFAULT_BITMAP_DELAY
;
1556 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1557 } else if (grow_continue
)
1558 rv
= Grow_continue_command(devlist
->devname
,
1559 mdfd
, c
.backup_file
,
1561 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1562 || s
.chunk
!= 0 || s
.level
!= UnSet
1563 || data_offset
!= INVALID_SECTORS
) {
1564 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1566 data_offset
, &c
, &s
);
1567 } else if (array_size
== 0)
1568 pr_err("no changes to --grow\n");
1577 pr_err("In --incremental mode, a device cannot be given with --scan.\n");
1580 if (c
.runstop
<= 0) {
1581 pr_err("--incremental --scan meaningless without --run.\n");
1584 if (devmode
== 'f') {
1585 pr_err("--incremental --scan --fail not supported.\n");
1588 rv
= IncrementalScan(&c
, NULL
);
1591 if (!rebuild_map
&& !c
.scan
) {
1592 pr_err("--incremental requires a device.\n");
1597 if (devmode
== 'f') {
1598 if (devlist
->next
) {
1599 pr_err("'--incremental --fail' can only handle one device.\n");
1603 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1606 rv
= Incremental(devlist
, &c
, ss
);
1615 static int scan_assemble(struct supertype
*ss
,
1617 struct mddev_ident
*ident
)
1619 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1620 struct mddev_dev
*devlist
= conf_get_devs();
1621 struct map_ent
*map
= NULL
;
1624 int failures
, successes
;
1626 if (conf_verify_devnames(array_list
)) {
1627 pr_err("Duplicate MD device names in conf file were found.\n");
1630 if (devlist
== NULL
) {
1631 pr_err("No devices listed in conf file were found.\n");
1634 for (a
= array_list
; a
; a
= a
->next
) {
1637 a
->autof
= c
->autof
;
1640 pr_err("failed to get exclusive lock on mapfile\n");
1645 for (a
= array_list
; a
; a
= a
->next
) {
1650 strcasecmp(a
->devname
, "<ignore>") == 0)
1653 r
= Assemble(ss
, a
->devname
,
1663 } while (failures
&& successes
);
1664 if (c
->homehost
&& cnt
== 0) {
1665 /* Maybe we can auto-assemble something.
1666 * Repeatedly call Assemble in auto-assemble mode
1671 ident
->autof
= c
->autof
;
1673 struct mddev_dev
*devlist
= conf_get_devs();
1676 rv2
= Assemble(ss
, NULL
,
1684 /* Incase there are stacked devices, we need to go around again */
1686 if (cnt
== 0 && rv
== 0) {
1687 pr_err("No arrays found in config file or automatically\n");
1691 } else if (cnt
== 0 && rv
== 0) {
1692 pr_err("No arrays found in config file\n");
1699 static int misc_scan(char devmode
, struct context
*c
)
1701 /* apply --detail or --wait-clean to
1702 * all devices in /proc/mdstat
1704 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1705 struct mdstat_ent
*e
;
1706 struct map_ent
*map
= NULL
;
1710 for (members
= 0; members
<= 1; members
++) {
1711 for (e
=ms
; e
; e
=e
->next
) {
1715 int member
= e
->metadata_version
&&
1716 strncmp(e
->metadata_version
,
1717 "external:/", 10) == 0;
1718 if (members
!= member
)
1720 me
= map_by_devnm(&map
, e
->devnm
);
1722 && strcmp(me
->path
, "/unknown") != 0)
1725 stat(name
, &stb
) != 0)
1726 name
= get_md_name(e
->devnm
);
1729 pr_err("cannot find device file for %s\n",
1734 rv
|= Detail(name
, c
);
1736 rv
|= WaitClean(name
, -1, c
->verbose
);
1744 static int stop_scan(int verbose
)
1746 /* apply --stop to all devices in /proc/mdstat */
1747 /* Due to possible stacking of devices, repeat until
1748 * nothing more can be stopped
1750 int progress
=1, err
;
1754 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1755 struct mdstat_ent
*e
;
1757 if (!progress
) last
= 1;
1758 progress
= 0; err
= 0;
1759 for (e
=ms
; e
; e
=e
->next
) {
1760 char *name
= get_md_name(e
->devnm
);
1764 pr_err("cannot find device file for %s\n",
1768 mdfd
= open_mddev(name
, 1);
1770 if (Manage_stop(name
, mdfd
, verbose
, !last
))
1780 } while (!last
&& err
);
1786 static int misc_list(struct mddev_dev
*devlist
,
1787 struct mddev_ident
*ident
,
1788 char *dump_directory
,
1789 struct supertype
*ss
, struct context
*c
)
1791 struct mddev_dev
*dv
;
1794 for (dv
=devlist
; dv
; dv
=(rv
& 16) ? NULL
: dv
->next
) {
1797 switch(dv
->disposition
) {
1799 rv
|= Detail(dv
->devname
, c
);
1801 case KillOpt
: /* Zero superblock */
1803 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1807 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1814 rv
|= Query(dv
->devname
); continue;
1816 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1818 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1821 rv
|= Wait(dv
->devname
); continue;
1823 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1825 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1827 case UpdateSubarray
:
1828 if (c
->update
== NULL
) {
1829 pr_err("-U/--update must be specified with --update-subarray\n");
1833 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1834 c
->update
, ident
, c
->verbose
);
1837 rv
|= Dump_metadata(dv
->devname
, dump_directory
, c
, ss
);
1840 rv
|= Restore_metadata(dv
->devname
, dump_directory
, c
, ss
,
1841 (dv
== devlist
&& dv
->next
== NULL
));
1844 rv
|= SetAction(dv
->devname
, c
->action
);
1847 if (dv
->devname
[0] == '/')
1848 mdfd
= open_mddev(dv
->devname
, 1);
1850 mdfd
= open_dev(dv
->devname
);
1852 pr_err("Cannot open %s\n", dv
->devname
);
1855 switch(dv
->disposition
) {
1858 rv
|= Manage_run(dv
->devname
, mdfd
, c
); break;
1860 rv
|= Manage_stop(dv
->devname
, mdfd
, c
->verbose
, 0); break;
1862 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1864 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1873 int SetAction(char *dev
, char *action
)
1875 int fd
= open(dev
, O_RDONLY
);
1878 pr_err("Couldn't open %s: %s\n", dev
, strerror(errno
));
1881 sysfs_init(&mdi
, fd
, NULL
);
1883 if (!mdi
.sys_name
[0]) {
1884 pr_err("%s is no an md array\n", dev
);
1888 if (sysfs_set_str(&mdi
, NULL
, "sync_action", action
) < 0) {
1889 pr_err("Count not set action for %s to %s: %s\n",
1890 dev
, action
, strerror(errno
));