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 */
201 shortopt
= short_bitmap_options
;
205 case 'A': newmode
= ASSEMBLE
;
206 shortopt
= short_bitmap_auto_options
;
208 case 'B': newmode
= BUILD
;
209 shortopt
= short_bitmap_auto_options
;
211 case 'C': newmode
= CREATE
;
212 shortopt
= short_bitmap_auto_options
;
214 case 'F': newmode
= MONITOR
;
216 case 'G': newmode
= GROW
;
217 shortopt
= short_bitmap_options
;
219 case 'I': newmode
= INCREMENTAL
;
220 shortopt
= short_bitmap_auto_options
;
223 newmode
= AUTODETECT
;
258 if (mode
&& newmode
== mode
) {
259 /* everybody happy ! */
260 } else if (mode
&& newmode
!= mode
) {
263 if (option_index
>= 0)
264 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
266 fprintf(stderr
, "-%c", opt
);
267 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
268 map_num(modes
, newmode
),
269 map_num(modes
, mode
));
271 } else if (!mode
&& newmode
) {
273 if (mode
== MISC
&& devs_found
) {
274 pr_err("No action given for %s in --misc mode\n",
276 cont_err("Action options must come before device names\n");
280 /* special case of -c --help */
281 if ((opt
== 'c' || opt
== ConfigFile
) &&
282 ( strncmp(optarg
, "--h", 3)==0 ||
283 strncmp(optarg
, "-h", 2)==0)) {
284 fputs(Help_config
, stdout
);
288 /* If first option is a device, don't force the mode yet */
290 if (devs_found
== 0) {
291 dv
= xmalloc(sizeof(*dv
));
292 dv
->devname
= optarg
;
293 dv
->disposition
= devmode
;
294 dv
->writemostly
= writemostly
;
298 devlistend
= &dv
->next
;
303 /* No mode yet, and this is the second device ... */
304 pr_err("An option must be given to set the mode before a second device\n"
305 " (%s) is listed\n", optarg
);
308 if (option_index
>= 0)
309 pr_err("--%s", long_options
[option_index
].name
);
312 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
316 /* if we just set the mode, then done */
330 /* an undecorated option - must be a device name.
333 if (devs_found
> 0 && devmode
== DetailPlatform
) {
334 pr_err("controller may only be specified once. %s ignored\n",
339 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
340 pr_err("Must give one of -a/-r/-f"
341 " for subsequent devices at %s\n", optarg
);
344 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
345 pr_err("Must give -a/--add for"
346 " devices to add: %s\n", optarg
);
349 dv
= xmalloc(sizeof(*dv
));
350 dv
->devname
= optarg
;
351 dv
->disposition
= devmode
;
352 dv
->writemostly
= writemostly
;
356 devlistend
= &dv
->next
;
362 /* We've got a mode, and opt is now something else which
363 * could depend on the mode */
364 #define O(a,b) ((a<<16)|b)
365 switch (O(mode
,opt
)) {
367 case O(GROW
,ChunkSize
):
369 case O(CREATE
,ChunkSize
):
370 case O(BUILD
,'c'): /* chunk or rounding */
371 case O(BUILD
,ChunkSize
): /* chunk or rounding */
373 pr_err("chunk/rounding may only be specified once. "
374 "Second value is %s.\n", optarg
);
377 s
.chunk
= parse_size(optarg
);
378 if (s
.chunk
== INVALID_SECTORS
||
379 s
.chunk
< 8 || (s
.chunk
&1)) {
380 pr_err("invalid chunk/rounding value: %s\n",
384 /* Convert sectors to K */
388 case O(INCREMENTAL
, 'e'):
390 case O(ASSEMBLE
,'e'):
391 case O(MISC
,'e'): /* set metadata (superblock) information */
393 pr_err("metadata information already given\n");
396 for(i
=0; !ss
&& superlist
[i
]; i
++)
397 ss
= superlist
[i
]->match_metadata_desc(optarg
);
400 pr_err("unrecognised metadata identifier: %s\n", optarg
);
406 case O(MANAGE
,WriteMostly
):
408 case O(BUILD
,WriteMostly
):
410 case O(CREATE
,WriteMostly
):
411 /* set write-mostly for following devices */
416 /* clear write-mostly for following devices */
422 case O(BUILD
,'z'): /* size */
424 pr_err("size may only be specified once. "
425 "Second value is %s.\n", optarg
);
428 if (strcmp(optarg
, "max")==0)
431 s
.size
= parse_size(optarg
);
432 if (s
.size
== INVALID_SECTORS
||
434 pr_err("invalid size: %s\n",
438 /* convert sectors to K */
443 case O(GROW
,'Z'): /* array size */
444 if (array_size
> 0) {
445 pr_err("array-size may only be specified once. "
446 "Second value is %s.\n", optarg
);
449 if (strcmp(optarg
, "max") == 0)
450 array_size
= MAX_SIZE
;
452 array_size
= parse_size(optarg
);
453 if (array_size
== 0 ||
454 array_size
== INVALID_SECTORS
) {
455 pr_err("invalid array size: %s\n",
462 case O(CREATE
,DataOffset
):
463 case O(GROW
,DataOffset
):
464 if (data_offset
!= INVALID_SECTORS
) {
465 pr_err("data-offset may only be specified one. "
466 "Second value is %s.\n", optarg
);
469 if (mode
== CREATE
&&
470 strcmp(optarg
, "variable") == 0)
471 data_offset
= VARIABLE_OFFSET
;
473 data_offset
= parse_size(optarg
);
474 if (data_offset
== INVALID_SECTORS
) {
475 pr_err("invalid data-offset: %s\n",
483 case O(BUILD
,'l'): /* set raid level*/
484 if (s
.level
!= UnSet
) {
485 pr_err("raid level may only be set once. "
486 "Second value is %s.\n", optarg
);
489 s
.level
= map_name(pers
, optarg
);
490 if (s
.level
== UnSet
) {
491 pr_err("invalid raid level: %s\n",
495 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
496 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
499 pr_err("Raid level %s not permitted with --build.\n",
503 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
504 pr_err("raid level %s is incompatible with spare-devices setting.\n",
508 ident
.level
= s
.level
;
511 case O(GROW
, 'p'): /* new layout */
512 case O(GROW
, Layout
):
514 pr_err("layout may only be sent once. "
515 "Second value was %s\n", optarg
);
518 s
.layout_str
= optarg
;
519 /* 'Grow' will parse the value */
522 case O(CREATE
,'p'): /* raid5 layout */
523 case O(CREATE
,Layout
):
524 case O(BUILD
,'p'): /* faulty layout */
525 case O(BUILD
,Layout
):
526 if (s
.layout
!= UnSet
) {
527 pr_err("layout may only be sent once. "
528 "Second value was %s\n", optarg
);
533 pr_err("layout not meaningful for %s arrays.\n",
534 map_num(pers
, s
.level
));
537 pr_err("raid level must be given before layout.\n");
541 s
.layout
= map_name(r5layout
, optarg
);
542 if (s
.layout
==UnSet
) {
543 pr_err("layout %s not understood for raid5.\n",
549 s
.layout
= map_name(r6layout
, optarg
);
550 if (s
.layout
==UnSet
) {
551 pr_err("layout %s not understood for raid6.\n",
558 s
.layout
= parse_layout_10(optarg
);
560 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
568 s
.layout
= parse_layout_faulty(optarg
);
569 if (s
.layout
== -1) {
570 pr_err("layout %s not understood for faulty.\n",
578 case O(CREATE
,AssumeClean
):
579 case O(BUILD
,AssumeClean
): /* assume clean */
580 case O(GROW
,AssumeClean
):
586 case O(BUILD
,'n'): /* number of raid disks */
588 pr_err("raid-devices set twice: %d and %s\n",
589 s
.raiddisks
, optarg
);
592 s
.raiddisks
= parse_num(optarg
);
593 if (s
.raiddisks
<= 0) {
594 pr_err("invalid number of raid devices: %s\n",
598 ident
.raid_disks
= s
.raiddisks
;
601 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
603 pr_err("spare-devices set twice: %d and %s\n",
604 s
.sparedisks
, optarg
);
607 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
608 pr_err("spare-devices setting is incompatible with raid level %d\n",
612 s
.sparedisks
= parse_num(optarg
);
613 if (s
.sparedisks
< 0) {
614 pr_err("invalid number of spare-devices: %s\n",
624 case O(INCREMENTAL
,'a'):
625 case O(INCREMENTAL
,Auto
):
626 case O(ASSEMBLE
,'a'):
627 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
628 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
631 case O(CREATE
,Symlinks
):
632 case O(BUILD
,Symlinks
):
633 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
637 case O(BUILD
,'f'): /* force honouring '-n 1' */
638 case O(BUILD
,Force
): /* force honouring '-n 1' */
639 case O(GROW
,'f'): /* ditto */
640 case O(GROW
,Force
): /* ditto */
641 case O(CREATE
,'f'): /* force honouring of device list */
642 case O(CREATE
,Force
): /* force honouring of device list */
643 case O(ASSEMBLE
,'f'): /* force assembly */
644 case O(ASSEMBLE
,Force
): /* force assembly */
645 case O(MISC
,'f'): /* force zero */
646 case O(MISC
,Force
): /* force zero */
647 case O(MANAGE
,Force
): /* add device which is too large */
650 /* now for the Assemble options */
651 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
653 case O(INCREMENTAL
, FreezeReshape
):
654 c
.freeze_reshape
= 1;
656 case O(CREATE
,'u'): /* uuid of array */
657 case O(ASSEMBLE
,'u'): /* uuid of array */
658 if (ident
.uuid_set
) {
659 pr_err("uuid cannot be set twice. "
660 "Second value %s.\n", optarg
);
663 if (parse_uuid(optarg
, ident
.uuid
))
666 pr_err("Bad uuid: %s\n", optarg
);
672 case O(ASSEMBLE
,'N'):
675 pr_err("name cannot be set twice. "
676 "Second value %s.\n", optarg
);
679 if (mode
== MISC
&& !c
.subarray
) {
680 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
683 if (strlen(optarg
) > 32) {
684 pr_err("name '%s' is too long, 32 chars max.\n",
688 strcpy(ident
.name
, optarg
);
691 case O(ASSEMBLE
,'m'): /* super-minor for array */
692 case O(ASSEMBLE
,SuperMinor
):
693 if (ident
.super_minor
!= UnSet
) {
694 pr_err("super-minor cannot be set twice. "
695 "Second value: %s.\n", optarg
);
698 if (strcmp(optarg
, "dev")==0)
699 ident
.super_minor
= -2;
701 ident
.super_minor
= parse_num(optarg
);
702 if (ident
.super_minor
< 0) {
703 pr_err("Bad super-minor number: %s.\n", optarg
);
709 case O(ASSEMBLE
,'o'):
715 case O(ASSEMBLE
,'U'): /* update the superblock */
718 pr_err("Can only update one aspect"
719 " of superblock, both %s and %s given.\n",
723 if (mode
== MISC
&& !c
.subarray
) {
724 pr_err("Only subarrays can be"
725 " updated in misc mode\n");
729 if (strcmp(c
.update
, "sparc2.2")==0)
731 if (strcmp(c
.update
, "super-minor") == 0)
733 if (strcmp(c
.update
, "summaries")==0)
735 if (strcmp(c
.update
, "resync")==0)
737 if (strcmp(c
.update
, "uuid")==0)
739 if (strcmp(c
.update
, "name")==0)
741 if (strcmp(c
.update
, "homehost")==0)
743 if (strcmp(c
.update
, "devicesize")==0)
745 if (strcmp(c
.update
, "no-bitmap")==0)
747 if (strcmp(c
.update
, "bbl") == 0)
749 if (strcmp(c
.update
, "no-bbl") == 0)
751 if (strcmp(c
.update
, "metadata") == 0)
753 if (strcmp(c
.update
, "revert-reshape") == 0)
755 if (strcmp(c
.update
, "byteorder")==0) {
757 pr_err("must not set metadata"
758 " 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"
766 " cannot find 0.swap\n");
772 if (strcmp(c
.update
,"?") == 0 ||
773 strcmp(c
.update
, "help") == 0) {
775 fprintf(outf
, "%s: ", Name
);
779 "%s: '--update=%s' is invalid. ",
782 fprintf(outf
, "Valid --update options are:\n"
783 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
784 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
785 " 'no-bitmap', 'metadata', 'revert-reshape'\n"
788 exit(outf
== stdout
? 0 : 2);
791 /* update=devicesize is allowed with --re-add */
792 if (devmode
!= 'A') {
793 pr_err("--update in Manage mode only"
794 " allowed with --re-add.\n");
798 pr_err("Can only update one aspect"
799 " of superblock, both %s and %s given.\n",
804 if (strcmp(c
.update
, "devicesize") != 0 &&
805 strcmp(c
.update
, "bbl") != 0 &&
806 strcmp(c
.update
, "no-bbl") != 0) {
807 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be"
808 " updated with --re-add\n");
813 case O(INCREMENTAL
,NoDegraded
):
814 pr_err("--no-degraded is deprecated in Incremental mode\n");
815 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
816 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
817 * so we overload slightly */
820 case O(ASSEMBLE
,'c'):
821 case O(ASSEMBLE
,ConfigFile
):
822 case O(INCREMENTAL
, 'c'):
823 case O(INCREMENTAL
, ConfigFile
):
825 case O(MISC
, ConfigFile
):
827 case O(MONITOR
,ConfigFile
):
828 case O(CREATE
,ConfigFile
):
830 pr_err("configfile cannot be set twice. "
831 "Second value is %s.\n", optarg
);
835 set_conffile(configfile
);
836 /* FIXME possibly check that config file exists. Even parse it */
838 case O(ASSEMBLE
,'s'): /* scan */
841 case O(INCREMENTAL
,'s'):
845 case O(MONITOR
,'m'): /* mail address */
846 case O(MONITOR
,EMail
):
848 pr_err("only specify one mailaddress. %s ignored.\n",
854 case O(MONITOR
,'p'): /* alert program */
855 case O(MONITOR
,ProgramOpt
): /* alert program */
857 pr_err("only specify one alter program. %s ignored.\n",
863 case O(MONITOR
,'r'): /* rebuild increments */
864 case O(MONITOR
,Increment
):
865 increments
= atoi(optarg
);
866 if (increments
> 99 || increments
< 1) {
867 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
872 case O(MONITOR
,'d'): /* delay in seconds */
874 case O(BUILD
,'d'): /* delay for bitmap updates */
877 pr_err("only specify delay once. %s ignored.\n",
880 c
.delay
= parse_num(optarg
);
882 pr_err("invalid delay: %s\n",
888 case O(MONITOR
,'f'): /* daemonise */
889 case O(MONITOR
,Fork
):
892 case O(MONITOR
,'i'): /* pid */
894 pr_err("only specify one pid file. %s ignored.\n",
899 case O(MONITOR
,'1'): /* oneshot */
903 case O(MONITOR
,'t'): /* test */
906 case O(MONITOR
,'y'): /* log messages to syslog */
907 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
910 case O(MONITOR
, NoSharing
):
914 /* now the general management options. Some are applicable
915 * to other modes. None have arguments.
920 case O(MANAGE
,Add
): /* add a drive */
923 case O(MANAGE
,AddSpare
): /* add drive - never re-add */
926 case O(MANAGE
,ReAdd
):
929 case O(MANAGE
,'r'): /* remove a drive */
930 case O(MANAGE
,Remove
):
933 case O(MANAGE
,'f'): /* set faulty */
935 case O(INCREMENTAL
,'f'):
936 case O(INCREMENTAL
,Remove
):
937 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
938 * even though we will both fail and
939 * remove the device */
942 case O(MANAGE
,Replace
):
943 /* Mark these devices for replacement */
947 /* These are the replacements to use */
948 if (devmode
!= 'R') {
949 pr_err("--with must follow --replace\n");
954 case O(INCREMENTAL
,'R'):
956 case O(ASSEMBLE
,'R'):
958 case O(CREATE
,'R'): /* Run the array */
960 pr_err("Cannot both Stop and Run an array\n");
967 pr_err("Cannot both Run and Stop an array\n");
979 case O(MISC
,KillOpt
):
983 case O(MISC
, ExamineBB
):
987 case O(MISC
, WaitOpt
):
988 case O(MISC
, Waitclean
):
989 case O(MISC
, DetailPlatform
):
990 case O(MISC
, KillSubarray
):
991 case O(MISC
, UpdateSubarray
):
993 case O(MISC
, Restore
):
994 case O(MISC
,Action
):
995 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
997 pr_err("subarray can only"
998 " be specified once\n");
1001 c
.subarray
= optarg
;
1003 if (opt
== Action
) {
1005 pr_err("Only one --action can be specified\n");
1008 if (strcmp(optarg
, "idle") == 0 ||
1009 strcmp(optarg
, "frozen") == 0 ||
1010 strcmp(optarg
, "check") == 0 ||
1011 strcmp(optarg
, "repair") == 0)
1014 pr_err("action must be one of idle, frozen, check, repair\n");
1018 if (devmode
&& devmode
!= opt
&&
1019 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
1020 pr_err("--examine/-E cannot be given with ");
1021 if (devmode
== 'E') {
1022 if (option_index
>= 0)
1023 fprintf(stderr
, "--%s\n",
1024 long_options
[option_index
].name
);
1026 fprintf(stderr
, "-%c\n", opt
);
1027 } else if (isalpha(devmode
))
1028 fprintf(stderr
, "-%c\n", devmode
);
1030 fprintf(stderr
, "previous option\n");
1034 if (opt
== Dump
|| opt
== Restore
) {
1035 if (dump_directory
!= NULL
) {
1036 pr_err("dump/restore directory specified twice: %s and %s\n",
1037 dump_directory
, optarg
);
1040 dump_directory
= optarg
;
1043 case O(MISC
, UdevRules
):
1044 if (devmode
&& devmode
!= opt
) {
1045 pr_err("--udev-rules must"
1046 " be the only option.\n");
1049 pr_err("only specify one udev "
1050 "rule filename. %s ignored.\n",
1053 udev_filename
= optarg
;
1061 case O(MISC
, Sparc22
):
1062 if (devmode
!= 'E') {
1063 pr_err("--sparc2.2 only allowed with --examine\n");
1069 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1070 case O(ASSEMBLE
,Bitmap
):
1072 pr_err("bitmap file needed with -b in --assemble mode\n");
1075 if (strcmp(optarg
, "internal")==0) {
1076 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1079 bitmap_fd
= open(optarg
, O_RDWR
);
1080 if (!*optarg
|| bitmap_fd
< 0) {
1081 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1084 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1087 case O(ASSEMBLE
, BackupFile
):
1088 case O(GROW
, BackupFile
):
1089 /* Specify a file into which grow might place a backup,
1090 * or from which assemble might recover a backup
1092 if (c
.backup_file
) {
1093 pr_err("backup file already specified, rejecting %s\n", optarg
);
1096 c
.backup_file
= optarg
;
1099 case O(GROW
, Continue
):
1100 /* Continue interrupted grow
1104 case O(ASSEMBLE
, InvalidBackup
):
1105 /* Acknowledge that the backupfile is invalid, but ask
1106 * to continue anyway
1108 c
.invalid_backup
= 1;
1112 case O(BUILD
,Bitmap
):
1114 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1116 case O(GROW
,Bitmap
):
1117 if (strcmp(optarg
, "internal")== 0 ||
1118 strcmp(optarg
, "none")== 0 ||
1119 strchr(optarg
, '/') != NULL
) {
1120 s
.bitmap_file
= optarg
;
1124 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1125 " not '%s'\n", optarg
);
1128 case O(GROW
,BitmapChunk
):
1129 case O(BUILD
,BitmapChunk
):
1130 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1131 s
.bitmap_chunk
= parse_size(optarg
);
1132 if (s
.bitmap_chunk
== 0 ||
1133 s
.bitmap_chunk
== INVALID_SECTORS
||
1134 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1135 pr_err("invalid bitmap chunksize: %s\n",
1139 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1142 case O(GROW
, WriteBehind
):
1143 case O(BUILD
, WriteBehind
):
1144 case O(CREATE
, WriteBehind
): /* write-behind mode */
1145 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1147 s
.write_behind
= parse_num(optarg
);
1148 if (s
.write_behind
< 0 ||
1149 s
.write_behind
> 16383) {
1150 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1156 case O(INCREMENTAL
, 'r'):
1157 case O(INCREMENTAL
, RebuildMapOpt
):
1160 case O(INCREMENTAL
, IncrementalPath
):
1161 remove_path
= optarg
;
1164 /* We have now processed all the valid options. Anything else is
1167 if (option_index
> 0)
1168 pr_err(":option --%s not valid in %s mode\n",
1169 long_options
[option_index
].name
,
1170 map_num(modes
, mode
));
1172 pr_err("option -%c not valid in %s mode\n",
1173 opt
, map_num(modes
, mode
));
1180 if (print_help
== 2)
1181 help_text
= OptionHelp
;
1183 help_text
= mode_help
[mode
];
1184 if (help_text
== NULL
)
1186 fputs(help_text
,stdout
);
1190 if (!mode
&& devs_found
) {
1193 if (devlist
->disposition
== 0)
1194 devlist
->disposition
= devmode
;
1197 fputs(Usage
, stderr
);
1202 struct createinfo
*ci
= conf_get_create_info();
1204 if (strcasecmp(symlinks
, "yes") == 0)
1206 else if (strcasecmp(symlinks
, "no") == 0)
1209 pr_err("option --symlinks must be 'no' or 'yes'\n");
1213 /* Ok, got the option parsing out of the way
1214 * hopefully it's mostly right but there might be some stuff
1217 * That is mosty checked in the per-mode stuff but...
1219 * For @,B,C and A without -s, the first device listed must be
1220 * an md device. We check that here and open it.
1223 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1225 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1226 if (devs_found
< 1) {
1227 pr_err("an md device must be given in this mode\n");
1230 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1231 pr_err("--super-minor=dev is incompatible with --auto\n");
1234 if (mode
== MANAGE
|| mode
== GROW
) {
1235 mdfd
= open_mddev(devlist
->devname
, 1);
1239 /* non-existent device is OK */
1240 mdfd
= open_mddev(devlist
->devname
, 0);
1242 pr_err("device %s exists but is not an "
1243 "md array.\n", devlist
->devname
);
1246 if ((int)ident
.super_minor
== -2) {
1249 pr_err("--super-minor=dev given, and "
1250 "listed device %s doesn't exist.\n",
1255 ident
.super_minor
= minor(stb
.st_rdev
);
1257 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1258 /* We don't really want this open yet, we just might
1259 * have wanted to check some things
1267 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1268 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1269 " a mistake. If you really mean it you will need to specify --force before\n"
1270 " setting the number of drives.\n");
1275 if (c
.homehost
== NULL
)
1276 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1277 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1278 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1279 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1280 c
.homehost
= sys_hostname
;
1283 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1285 c
.require_homehost
= 0;
1288 if (c
.backup_file
&& data_offset
!= INVALID_SECTORS
) {
1289 pr_err("--backup-file and --data-offset are incompatible\n");
1293 if ((mode
== MISC
&& devmode
== 'E')
1294 || (mode
== MONITOR
&& spare_sharing
== 0))
1295 /* Anyone may try this */;
1296 else if (geteuid() != 0) {
1297 pr_err("must be super-user to perform this action\n");
1301 ident
.autof
= c
.autof
;
1303 if (c
.scan
&& c
.verbose
< 2)
1304 /* --scan implied --brief unless -vv */
1310 /* readonly, add/remove, readwrite, runstop */
1312 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1313 if (!rv
&& devs_found
>1)
1314 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1315 devlist
->next
, c
.verbose
, c
.test
,
1317 if (!rv
&& c
.readonly
< 0)
1318 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1319 if (!rv
&& c
.runstop
> 0)
1320 rv
= Manage_run(devlist
->devname
, mdfd
, &c
);
1321 if (!rv
&& c
.runstop
< 0)
1322 rv
= Manage_stop(devlist
->devname
, mdfd
, c
.verbose
, 0);
1325 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1326 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1327 /* Only a device has been given, so get details from config file */
1328 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1329 if (array_ident
== NULL
) {
1330 pr_err("%s not identified in config file.\n",
1336 if (array_ident
->autof
== 0)
1337 array_ident
->autof
= c
.autof
;
1338 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1342 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1344 else if (devs_found
> 0) {
1345 if (c
.update
&& devs_found
> 1) {
1346 pr_err("can only update a single array at a time\n");
1349 if (c
.backup_file
&& devs_found
> 1) {
1350 pr_err("can only assemble a single array when providing a backup file.\n");
1353 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1354 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1355 if (array_ident
== NULL
) {
1356 pr_err("%s not identified in config file.\n",
1361 if (array_ident
->autof
== 0)
1362 array_ident
->autof
= c
.autof
;
1363 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1368 pr_err("--update not meaningful with a --scan assembly.\n");
1371 if (c
.backup_file
) {
1372 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1375 rv
= scan_assemble(ss
, &c
, &ident
);
1381 c
.delay
= DEFAULT_BITMAP_DELAY
;
1382 if (s
.write_behind
&& !s
.bitmap_file
) {
1383 pr_err("write-behind mode requires a bitmap.\n");
1387 if (s
.raiddisks
== 0) {
1388 pr_err("no raid-devices specified.\n");
1393 if (s
.bitmap_file
) {
1394 if (strcmp(s
.bitmap_file
, "internal")==0) {
1395 pr_err("'internal' bitmaps not supported with --build\n");
1400 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1404 c
.delay
= DEFAULT_BITMAP_DELAY
;
1405 if (s
.write_behind
&& !s
.bitmap_file
) {
1406 pr_err("write-behind mode requires a bitmap.\n");
1410 if (s
.raiddisks
== 0) {
1411 pr_err("no raid-devices specified.\n");
1416 rv
= Create(ss
, devlist
->devname
,
1417 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1418 devs_found
-1, devlist
->next
,
1419 &s
, &c
, data_offset
);
1422 if (devmode
== 'E') {
1423 if (devlist
== NULL
&& !c
.scan
) {
1424 pr_err("No devices to examine\n");
1427 if (devlist
== NULL
)
1428 devlist
= conf_get_devs();
1429 if (devlist
== NULL
) {
1430 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1433 rv
= Examine(devlist
, &c
, ss
);
1434 } else if (devmode
== DetailPlatform
) {
1435 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1436 c
.verbose
, c
.export
,
1437 devlist
? devlist
->devname
: NULL
);
1438 } else if (devlist
== NULL
) {
1439 if (devmode
== 'S' && c
.scan
)
1440 rv
= stop_scan(c
.verbose
);
1441 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1442 rv
= misc_scan(devmode
, &c
);
1443 else if (devmode
== UdevRules
)
1444 rv
= Write_rules(udev_filename
);
1446 pr_err("No devices given.\n");
1450 rv
= misc_list(devlist
, &ident
, dump_directory
, ss
, &c
);
1453 if (!devlist
&& !c
.scan
) {
1454 pr_err("Cannot monitor: need --scan or at least one device\n");
1458 if (pidfile
&& !daemonise
) {
1459 pr_err("Cannot write a pid file when not in daemon mode\n");
1464 if (get_linux_version() > 2006016)
1465 /* mdstat responds to poll */
1472 rv
= Monitor(devlist
, mailaddr
, program
,
1473 &c
, daemonise
, oneshot
,
1474 dosyslog
, pidfile
, increments
,
1479 if (array_size
> 0) {
1480 /* alway impose array size first, independent of
1482 * Do not allow level or raid_disks changes at the
1483 * same time as that can be irreversibly destructive.
1487 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1488 pr_err("cannot change array size in same operation "
1489 "as changing raiddisks or level.\n"
1490 " Change size first, then check that data is still intact.\n");
1494 sysfs_init(&sra
, mdfd
, NULL
);
1495 if (array_size
== MAX_SIZE
)
1496 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1498 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1501 pr_err("--array-size setting"
1502 " is too large.\n");
1504 pr_err("current kernel does"
1505 " not support setting --array-size\n");
1510 if (devs_found
> 1 && s
.raiddisks
== 0 && s
.level
== UnSet
) {
1512 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1513 pr_err("--add cannot be used with "
1514 "other geometry changes in --grow mode\n");
1518 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1519 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1524 } else if (s
.bitmap_file
) {
1525 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1526 s
.layout_str
!= NULL
|| devs_found
> 1) {
1527 pr_err("--bitmap changes cannot be "
1528 "used with other geometry changes "
1529 "in --grow mode\n");
1534 c
.delay
= DEFAULT_BITMAP_DELAY
;
1535 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1536 } else if (grow_continue
)
1537 rv
= Grow_continue_command(devlist
->devname
,
1538 mdfd
, c
.backup_file
,
1540 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1541 || s
.chunk
!= 0 || s
.level
!= UnSet
1542 || data_offset
!= INVALID_SECTORS
) {
1543 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1545 data_offset
, &c
, &s
);
1546 } else if (array_size
== 0)
1547 pr_err("no changes to --grow\n");
1556 pr_err("In --incremental mode, a device cannot be given with --scan.\n");
1559 if (c
.runstop
<= 0) {
1560 pr_err("--incremental --scan meaningless without --run.\n");
1563 if (devmode
== 'f') {
1564 pr_err("--incremental --scan --fail not supported.\n");
1567 rv
= IncrementalScan(&c
, NULL
);
1570 if (!rebuild_map
&& !c
.scan
) {
1571 pr_err("--incremental requires a device.\n");
1576 if (devmode
== 'f') {
1577 if (devlist
->next
) {
1578 pr_err("'--incremental --fail' can only handle one device.\n");
1582 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1585 rv
= Incremental(devlist
, &c
, ss
);
1594 static int scan_assemble(struct supertype
*ss
,
1596 struct mddev_ident
*ident
)
1598 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1599 struct mddev_dev
*devlist
= conf_get_devs();
1600 struct map_ent
*map
= NULL
;
1603 int failures
, successes
;
1605 if (conf_verify_devnames(array_list
)) {
1606 pr_err("Duplicate MD device names in "
1607 "conf file were found.\n");
1610 if (devlist
== NULL
) {
1611 pr_err("No devices listed in conf file were found.\n");
1614 for (a
= array_list
; a
; a
= a
->next
) {
1617 a
->autof
= c
->autof
;
1620 pr_err("failed to get exclusive lock on mapfile\n");
1625 for (a
= array_list
; a
; a
= a
->next
) {
1630 strcasecmp(a
->devname
, "<ignore>") == 0)
1633 r
= Assemble(ss
, a
->devname
,
1643 } while (failures
&& successes
);
1644 if (c
->homehost
&& cnt
== 0) {
1645 /* Maybe we can auto-assemble something.
1646 * Repeatedly call Assemble in auto-assemble mode
1651 ident
->autof
= c
->autof
;
1653 struct mddev_dev
*devlist
= conf_get_devs();
1656 rv2
= Assemble(ss
, NULL
,
1664 /* Incase there are stacked devices, we need to go around again */
1666 if (cnt
== 0 && rv
== 0) {
1667 pr_err("No arrays found in config file or automatically\n");
1671 } else if (cnt
== 0 && rv
== 0) {
1672 pr_err("No arrays found in config file\n");
1679 static int misc_scan(char devmode
, struct context
*c
)
1681 /* apply --detail or --wait-clean to
1682 * all devices in /proc/mdstat
1684 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1685 struct mdstat_ent
*e
;
1686 struct map_ent
*map
= NULL
;
1690 for (members
= 0; members
<= 1; members
++) {
1691 for (e
=ms
; e
; e
=e
->next
) {
1695 int member
= e
->metadata_version
&&
1696 strncmp(e
->metadata_version
,
1697 "external:/", 10) == 0;
1698 if (members
!= member
)
1700 me
= map_by_devnm(&map
, e
->devnm
);
1702 && strcmp(me
->path
, "/unknown") != 0)
1705 stat(name
, &stb
) != 0)
1706 name
= get_md_name(e
->devnm
);
1709 pr_err("cannot find device file for %s\n",
1714 rv
|= Detail(name
, c
);
1716 rv
|= WaitClean(name
, -1, c
->verbose
);
1724 static int stop_scan(int verbose
)
1726 /* apply --stop to all devices in /proc/mdstat */
1727 /* Due to possible stacking of devices, repeat until
1728 * nothing more can be stopped
1730 int progress
=1, err
;
1734 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1735 struct mdstat_ent
*e
;
1737 if (!progress
) last
= 1;
1738 progress
= 0; err
= 0;
1739 for (e
=ms
; e
; e
=e
->next
) {
1740 char *name
= get_md_name(e
->devnm
);
1744 pr_err("cannot find device file for %s\n",
1748 mdfd
= open_mddev(name
, 1);
1750 if (Manage_stop(name
, mdfd
, verbose
, !last
))
1760 } while (!last
&& err
);
1766 static int misc_list(struct mddev_dev
*devlist
,
1767 struct mddev_ident
*ident
,
1768 char *dump_directory
,
1769 struct supertype
*ss
, struct context
*c
)
1771 struct mddev_dev
*dv
;
1774 for (dv
=devlist
; dv
; dv
=(rv
& 16) ? NULL
: dv
->next
) {
1777 switch(dv
->disposition
) {
1779 rv
|= Detail(dv
->devname
, c
);
1781 case KillOpt
: /* Zero superblock */
1783 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1787 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1794 rv
|= Query(dv
->devname
); continue;
1796 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1798 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1801 rv
|= Wait(dv
->devname
); continue;
1803 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1805 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1807 case UpdateSubarray
:
1808 if (c
->update
== NULL
) {
1809 pr_err("-U/--update must be specified with --update-subarray\n");
1813 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1814 c
->update
, ident
, c
->verbose
);
1817 rv
|= Dump_metadata(dv
->devname
, dump_directory
, c
, ss
);
1820 rv
|= Restore_metadata(dv
->devname
, dump_directory
, c
, ss
,
1821 (dv
== devlist
&& dv
->next
== NULL
));
1824 rv
|= SetAction(dv
->devname
, c
->action
);
1827 if (dv
->devname
[0] == '/')
1828 mdfd
= open_mddev(dv
->devname
, 1);
1830 mdfd
= open_dev(dv
->devname
);
1832 pr_err("Cannot open %s\n", dv
->devname
);
1835 switch(dv
->disposition
) {
1838 rv
|= Manage_run(dv
->devname
, mdfd
, c
); break;
1840 rv
|= Manage_stop(dv
->devname
, mdfd
, c
->verbose
, 0); break;
1842 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1844 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1853 int SetAction(char *dev
, char *action
)
1855 int fd
= open(dev
, O_RDONLY
);
1858 pr_err("Couldn't open %s: %s\n", dev
, strerror(errno
));
1861 sysfs_init(&mdi
, fd
, NULL
);
1863 if (!mdi
.sys_name
[0]) {
1864 pr_err("%s is no an md array\n", dev
);
1868 if (sysfs_set_str(&mdi
, NULL
, "sync_action", action
) < 0) {
1869 pr_err("Count not set action for %s to %s: %s\n",
1870 dev
, action
, strerror(errno
));