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
);
42 int main(int argc
, char *argv
[])
50 unsigned long long array_size
= 0;
51 unsigned long long data_offset
= INVALID_SECTORS
;
52 struct mddev_ident ident
;
53 char *configfile
= NULL
;
56 struct mddev_dev
*devlist
= NULL
;
57 struct mddev_dev
**devlistend
= & devlist
;
60 char *symlinks
= NULL
;
61 int grow_continue
= 0;
62 /* autof indicates whether and how to create device node.
63 * bottom 3 bits are style. Rest (when shifted) are number of parts
65 * 1 - don't create (no)
66 * 2 - if is_standard, then create (yes)
67 * 3 - create as 'md' - reject is_standard mdp (md)
68 * 4 - create as 'mdp' - reject is_standard md (mdp)
69 * 5 - default to md if not is_standard (md in config file)
70 * 6 - default to mdp if not is_standard (part, or mdp in config file)
73 .require_homehost
= 1,
78 .bitmap_chunk
= UnSet
,
81 char sys_hostname
[256];
82 char *mailaddr
= NULL
;
88 int spare_sharing
= 1;
89 struct supertype
*ss
= NULL
;
91 char *shortopt
= short_options
;
94 char *remove_path
= NULL
;
95 char *udev_filename
= NULL
;
96 char *dump_directory
= NULL
;
103 srandom(time(0) ^ getpid());
107 ident
.raid_disks
= UnSet
;
108 ident
.super_minor
= UnSet
;
110 ident
.spare_group
= NULL
;
113 ident
.bitmap_fd
= -1;
114 ident
.bitmap_file
= NULL
;
116 ident
.container
= NULL
;
119 while ((option_index
= -1) ,
120 (opt
=getopt_long(argc
, argv
,
121 shortopt
, long_options
,
122 &option_index
)) != -1) {
124 /* firstly, some mode-independent options */
134 fputs(Version
, stderr
);
137 case 'v': c
.verbose
++;
140 case 'q': c
.verbose
--;
144 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
145 || mode
== GROW
|| mode
== INCREMENTAL
147 break; /* b means bitmap */
152 case 'Y': c
.export
++;
156 if (strcasecmp(optarg
, "<ignore>") == 0)
157 c
.require_homehost
= 0;
163 /* Silently ignore old option */
169 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
175 fputs(Usage
, stderr
);
178 /* second, figure out the mode.
179 * Some options force the mode. Others
180 * set the mode if it isn't already
186 shortopt
= short_bitmap_options
;
197 case ReAdd
: /* re-add */
200 shortopt
= short_bitmap_options
;
204 case 'A': newmode
= ASSEMBLE
;
205 shortopt
= short_bitmap_auto_options
;
207 case 'B': newmode
= BUILD
;
208 shortopt
= short_bitmap_auto_options
;
210 case 'C': newmode
= CREATE
;
211 shortopt
= short_bitmap_auto_options
;
213 case 'F': newmode
= MONITOR
;
215 case 'G': newmode
= GROW
;
216 shortopt
= short_bitmap_options
;
218 case 'I': newmode
= INCREMENTAL
;
219 shortopt
= short_bitmap_auto_options
;
222 newmode
= AUTODETECT
;
256 if (mode
&& newmode
== mode
) {
257 /* everybody happy ! */
258 } else if (mode
&& newmode
!= mode
) {
261 if (option_index
>= 0)
262 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
264 fprintf(stderr
, "-%c", opt
);
265 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
266 map_num(modes
, newmode
),
267 map_num(modes
, mode
));
269 } else if (!mode
&& newmode
) {
271 if (mode
== MISC
&& devs_found
) {
272 pr_err("No action given for %s in --misc mode\n",
274 cont_err("Action options must come before device names\n");
278 /* special case of -c --help */
279 if ((opt
== 'c' || opt
== ConfigFile
) &&
280 ( strncmp(optarg
, "--h", 3)==0 ||
281 strncmp(optarg
, "-h", 2)==0)) {
282 fputs(Help_config
, stdout
);
286 /* If first option is a device, don't force the mode yet */
288 if (devs_found
== 0) {
289 dv
= xmalloc(sizeof(*dv
));
290 dv
->devname
= optarg
;
291 dv
->disposition
= devmode
;
292 dv
->writemostly
= writemostly
;
296 devlistend
= &dv
->next
;
301 /* No mode yet, and this is the second device ... */
302 pr_err("An option must be given to set the mode before a second device\n"
303 " (%s) is listed\n", optarg
);
306 if (option_index
>= 0)
307 pr_err("--%s", long_options
[option_index
].name
);
310 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
314 /* if we just set the mode, then done */
328 /* an undecorated option - must be a device name.
331 if (devs_found
> 0 && devmode
== DetailPlatform
) {
332 pr_err("controller may only be specified once. %s ignored\n",
337 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
338 pr_err("Must give one of -a/-r/-f"
339 " for subsequent devices at %s\n", optarg
);
342 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
343 pr_err("Must give -a/--add for"
344 " devices to add: %s\n", optarg
);
347 dv
= xmalloc(sizeof(*dv
));
348 dv
->devname
= optarg
;
349 dv
->disposition
= devmode
;
350 dv
->writemostly
= writemostly
;
354 devlistend
= &dv
->next
;
360 /* We've got a mode, and opt is now something else which
361 * could depend on the mode */
362 #define O(a,b) ((a<<16)|b)
363 switch (O(mode
,opt
)) {
365 case O(GROW
,ChunkSize
):
367 case O(CREATE
,ChunkSize
):
368 case O(BUILD
,'c'): /* chunk or rounding */
369 case O(BUILD
,ChunkSize
): /* chunk or rounding */
371 pr_err("chunk/rounding may only be specified once. "
372 "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. "
423 "Second value is %s.\n", optarg
);
426 if (strcmp(optarg
, "max")==0)
429 s
.size
= parse_size(optarg
);
430 if (s
.size
== INVALID_SECTORS
||
432 pr_err("invalid size: %s\n",
436 /* convert sectors to K */
441 case O(GROW
,'Z'): /* array size */
442 if (array_size
> 0) {
443 pr_err("array-size may only be specified once. "
444 "Second value is %s.\n", optarg
);
447 if (strcmp(optarg
, "max") == 0)
448 array_size
= MAX_SIZE
;
450 array_size
= parse_size(optarg
);
451 if (array_size
== 0 ||
452 array_size
== INVALID_SECTORS
) {
453 pr_err("invalid array size: %s\n",
460 case O(CREATE
,DataOffset
):
461 case O(GROW
,DataOffset
):
462 if (data_offset
!= INVALID_SECTORS
) {
463 pr_err("data-offset may only be specified one. "
464 "Second value is %s.\n", optarg
);
467 if (mode
== CREATE
&&
468 strcmp(optarg
, "variable") == 0)
469 data_offset
= VARIABLE_OFFSET
;
471 data_offset
= parse_size(optarg
);
472 if (data_offset
== INVALID_SECTORS
) {
473 pr_err("invalid data-offset: %s\n",
481 case O(BUILD
,'l'): /* set raid level*/
482 if (s
.level
!= UnSet
) {
483 pr_err("raid level may only be set once. "
484 "Second value is %s.\n", optarg
);
487 s
.level
= map_name(pers
, optarg
);
488 if (s
.level
== UnSet
) {
489 pr_err("invalid raid level: %s\n",
493 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
494 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
497 pr_err("Raid level %s not permitted with --build.\n",
501 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
502 pr_err("raid level %s is incompatible with spare-devices setting.\n",
506 ident
.level
= s
.level
;
509 case O(GROW
, 'p'): /* new layout */
510 case O(GROW
, Layout
):
512 pr_err("layout may only be sent once. "
513 "Second value was %s\n", optarg
);
516 s
.layout_str
= optarg
;
517 /* 'Grow' will parse the value */
520 case O(CREATE
,'p'): /* raid5 layout */
521 case O(CREATE
,Layout
):
522 case O(BUILD
,'p'): /* faulty layout */
523 case O(BUILD
,Layout
):
524 if (s
.layout
!= UnSet
) {
525 pr_err("layout may only be sent once. "
526 "Second value was %s\n", optarg
);
531 pr_err("layout not meaningful for %s arrays.\n",
532 map_num(pers
, s
.level
));
535 pr_err("raid level must be given before layout.\n");
539 s
.layout
= map_name(r5layout
, optarg
);
540 if (s
.layout
==UnSet
) {
541 pr_err("layout %s not understood for raid5.\n",
547 s
.layout
= map_name(r6layout
, optarg
);
548 if (s
.layout
==UnSet
) {
549 pr_err("layout %s not understood for raid6.\n",
556 s
.layout
= parse_layout_10(optarg
);
558 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
566 s
.layout
= parse_layout_faulty(optarg
);
567 if (s
.layout
== -1) {
568 pr_err("layout %s not understood for faulty.\n",
576 case O(CREATE
,AssumeClean
):
577 case O(BUILD
,AssumeClean
): /* assume clean */
578 case O(GROW
,AssumeClean
):
584 case O(BUILD
,'n'): /* number of raid disks */
586 pr_err("raid-devices set twice: %d and %s\n",
587 s
.raiddisks
, optarg
);
590 s
.raiddisks
= parse_num(optarg
);
591 if (s
.raiddisks
<= 0) {
592 pr_err("invalid number of raid devices: %s\n",
596 ident
.raid_disks
= s
.raiddisks
;
599 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
601 pr_err("spare-devices set twice: %d and %s\n",
602 s
.sparedisks
, optarg
);
605 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
606 pr_err("spare-devices setting is incompatible with raid level %d\n",
610 s
.sparedisks
= parse_num(optarg
);
611 if (s
.sparedisks
< 0) {
612 pr_err("invalid number of spare-devices: %s\n",
622 case O(INCREMENTAL
,'a'):
623 case O(INCREMENTAL
,Auto
):
624 case O(ASSEMBLE
,'a'):
625 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
626 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
629 case O(CREATE
,Symlinks
):
630 case O(BUILD
,Symlinks
):
631 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
635 case O(BUILD
,'f'): /* force honouring '-n 1' */
636 case O(BUILD
,Force
): /* force honouring '-n 1' */
637 case O(GROW
,'f'): /* ditto */
638 case O(GROW
,Force
): /* ditto */
639 case O(CREATE
,'f'): /* force honouring of device list */
640 case O(CREATE
,Force
): /* force honouring of device list */
641 case O(ASSEMBLE
,'f'): /* force assembly */
642 case O(ASSEMBLE
,Force
): /* force assembly */
643 case O(MISC
,'f'): /* force zero */
644 case O(MISC
,Force
): /* force zero */
645 case O(MANAGE
,Force
): /* add device which is too large */
648 /* now for the Assemble options */
649 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
651 case O(INCREMENTAL
, FreezeReshape
):
652 c
.freeze_reshape
= 1;
654 case O(CREATE
,'u'): /* uuid of array */
655 case O(ASSEMBLE
,'u'): /* uuid of array */
656 if (ident
.uuid_set
) {
657 pr_err("uuid cannot be set twice. "
658 "Second value %s.\n", optarg
);
661 if (parse_uuid(optarg
, ident
.uuid
))
664 pr_err("Bad uuid: %s\n", optarg
);
670 case O(ASSEMBLE
,'N'):
673 pr_err("name cannot be set twice. "
674 "Second value %s.\n", optarg
);
677 if (mode
== MISC
&& !c
.subarray
) {
678 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
681 if (strlen(optarg
) > 32) {
682 pr_err("name '%s' is too long, 32 chars max.\n",
686 strcpy(ident
.name
, optarg
);
689 case O(ASSEMBLE
,'m'): /* super-minor for array */
690 case O(ASSEMBLE
,SuperMinor
):
691 if (ident
.super_minor
!= UnSet
) {
692 pr_err("super-minor cannot be set twice. "
693 "Second value: %s.\n", optarg
);
696 if (strcmp(optarg
, "dev")==0)
697 ident
.super_minor
= -2;
699 ident
.super_minor
= parse_num(optarg
);
700 if (ident
.super_minor
< 0) {
701 pr_err("Bad super-minor number: %s.\n", optarg
);
707 case O(ASSEMBLE
,'o'):
713 case O(ASSEMBLE
,'U'): /* update the superblock */
716 pr_err("Can only update one aspect"
717 " of superblock, both %s and %s given.\n",
721 if (mode
== MISC
&& !c
.subarray
) {
722 pr_err("Only subarrays can be"
723 " updated in misc mode\n");
727 if (strcmp(c
.update
, "sparc2.2")==0)
729 if (strcmp(c
.update
, "super-minor") == 0)
731 if (strcmp(c
.update
, "summaries")==0)
733 if (strcmp(c
.update
, "resync")==0)
735 if (strcmp(c
.update
, "uuid")==0)
737 if (strcmp(c
.update
, "name")==0)
739 if (strcmp(c
.update
, "homehost")==0)
741 if (strcmp(c
.update
, "devicesize")==0)
743 if (strcmp(c
.update
, "no-bitmap")==0)
745 if (strcmp(c
.update
, "bbl") == 0)
747 if (strcmp(c
.update
, "no-bbl") == 0)
749 if (strcmp(c
.update
, "metadata") == 0)
751 if (strcmp(c
.update
, "revert-reshape") == 0)
753 if (strcmp(c
.update
, "byteorder")==0) {
755 pr_err("must not set metadata"
756 " type with --update=byteorder.\n");
759 for(i
=0; !ss
&& superlist
[i
]; i
++)
760 ss
= superlist
[i
]->match_metadata_desc(
763 pr_err("INTERNAL ERROR"
764 " cannot find 0.swap\n");
770 if (strcmp(c
.update
,"?") == 0 ||
771 strcmp(c
.update
, "help") == 0) {
773 fprintf(outf
, Name
": ");
777 Name
": '--update=%s' is invalid. ",
780 fprintf(outf
, "Valid --update options are:\n"
781 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
782 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
783 " 'no-bitmap', 'metadata', 'revert-reshape'\n");
784 exit(outf
== stdout
? 0 : 2);
787 /* update=devicesize is allowed with --re-add */
788 if (devmode
!= 'A') {
789 pr_err("--update in Manage mode only"
790 " allowed with --re-add.\n");
794 pr_err("Can only update one aspect"
795 " of superblock, both %s and %s given.\n",
800 if (strcmp(c
.update
, "devicesize") != 0 &&
801 strcmp(c
.update
, "bbl") != 0 &&
802 strcmp(c
.update
, "no-bbl") != 0) {
803 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be"
804 " 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. "
827 "Second value is %s.\n", optarg
);
831 set_conffile(configfile
);
832 /* FIXME possibly check that config file exists. Even parse it */
834 case O(ASSEMBLE
,'s'): /* scan */
837 case O(INCREMENTAL
,'s'):
841 case O(MONITOR
,'m'): /* mail address */
842 case O(MONITOR
,EMail
):
844 pr_err("only specify one mailaddress. %s ignored.\n",
850 case O(MONITOR
,'p'): /* alert program */
851 case O(MONITOR
,ProgramOpt
): /* alert program */
853 pr_err("only specify one alter program. %s ignored.\n",
859 case O(MONITOR
,'r'): /* rebuild increments */
860 case O(MONITOR
,Increment
):
861 increments
= atoi(optarg
);
862 if (increments
> 99 || increments
< 1) {
863 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
868 case O(MONITOR
,'d'): /* delay in seconds */
870 case O(BUILD
,'d'): /* delay for bitmap updates */
873 pr_err("only specify delay once. %s ignored.\n",
876 c
.delay
= parse_num(optarg
);
878 pr_err("invalid delay: %s\n",
884 case O(MONITOR
,'f'): /* daemonise */
885 case O(MONITOR
,Fork
):
888 case O(MONITOR
,'i'): /* pid */
890 pr_err("only specify one pid file. %s ignored.\n",
895 case O(MONITOR
,'1'): /* oneshot */
899 case O(MONITOR
,'t'): /* test */
902 case O(MONITOR
,'y'): /* log messages to syslog */
903 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
906 case O(MONITOR
, NoSharing
):
910 /* now the general management options. Some are applicable
911 * to other modes. None have arguments.
916 case O(MANAGE
,Add
): /* add a drive */
919 case O(MANAGE
,AddSpare
): /* add drive - never re-add */
922 case O(MANAGE
,ReAdd
):
925 case O(MANAGE
,'r'): /* remove a drive */
926 case O(MANAGE
,Remove
):
929 case O(MANAGE
,'f'): /* set faulty */
931 case O(INCREMENTAL
,'f'):
932 case O(INCREMENTAL
,Remove
):
933 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
934 * even though we will both fail and
935 * remove the device */
938 case O(MANAGE
,Replace
):
939 /* Mark these devices for replacement */
943 /* These are the replacements to use */
944 if (devmode
!= 'R') {
945 pr_err("--with must follow --replace\n");
950 case O(INCREMENTAL
,'R'):
952 case O(ASSEMBLE
,'R'):
954 case O(CREATE
,'R'): /* Run the array */
956 pr_err("Cannot both Stop and Run an array\n");
963 pr_err("Cannot both Run and Stop an array\n");
975 case O(MISC
,KillOpt
):
979 case O(MISC
, ExamineBB
):
983 case O(MISC
, WaitOpt
):
984 case O(MISC
, Waitclean
):
985 case O(MISC
, DetailPlatform
):
986 case O(MISC
, KillSubarray
):
987 case O(MISC
, UpdateSubarray
):
989 case O(MISC
, Restore
):
990 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
992 pr_err("subarray can only"
993 " be specified once\n");
998 if (devmode
&& devmode
!= opt
&&
999 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
1000 pr_err("--examine/-E cannot be given with ");
1001 if (devmode
== 'E') {
1002 if (option_index
>= 0)
1003 fprintf(stderr
, "--%s\n",
1004 long_options
[option_index
].name
);
1006 fprintf(stderr
, "-%c\n", opt
);
1007 } else if (isalpha(devmode
))
1008 fprintf(stderr
, "-%c\n", devmode
);
1010 fprintf(stderr
, "previous option\n");
1014 if (opt
== Dump
|| opt
== Restore
) {
1015 if (dump_directory
!= NULL
) {
1016 pr_err("dump/restore directory specified twice: %s and %s\n",
1017 dump_directory
, optarg
);
1020 dump_directory
= optarg
;
1023 case O(MISC
, UdevRules
):
1024 if (devmode
&& devmode
!= opt
) {
1025 pr_err("--udev-rules must"
1026 " be the only option.\n");
1029 pr_err("only specify one udev "
1030 "rule filename. %s ignored.\n",
1033 udev_filename
= optarg
;
1041 case O(MISC
, Sparc22
):
1042 if (devmode
!= 'E') {
1043 pr_err("--sparc2.2 only allowed with --examine\n");
1049 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1050 case O(ASSEMBLE
,Bitmap
):
1052 pr_err("bitmap file needed with -b in --assemble mode\n");
1055 if (strcmp(optarg
, "internal")==0) {
1056 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1059 bitmap_fd
= open(optarg
, O_RDWR
);
1060 if (!*optarg
|| bitmap_fd
< 0) {
1061 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1064 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1067 case O(ASSEMBLE
, BackupFile
):
1068 case O(GROW
, BackupFile
):
1069 /* Specify a file into which grow might place a backup,
1070 * or from which assemble might recover a backup
1072 if (c
.backup_file
) {
1073 pr_err("backup file already specified, rejecting %s\n", optarg
);
1076 c
.backup_file
= optarg
;
1079 case O(GROW
, Continue
):
1080 /* Continue interrupted grow
1084 case O(ASSEMBLE
, InvalidBackup
):
1085 /* Acknowledge that the backupfile is invalid, but ask
1086 * to continue anyway
1088 c
.invalid_backup
= 1;
1092 case O(BUILD
,Bitmap
):
1094 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1096 case O(GROW
,Bitmap
):
1097 if (strcmp(optarg
, "internal")== 0 ||
1098 strcmp(optarg
, "none")== 0 ||
1099 strchr(optarg
, '/') != NULL
) {
1100 s
.bitmap_file
= optarg
;
1104 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1105 " not '%s'\n", optarg
);
1108 case O(GROW
,BitmapChunk
):
1109 case O(BUILD
,BitmapChunk
):
1110 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1111 s
.bitmap_chunk
= parse_size(optarg
);
1112 if (s
.bitmap_chunk
== 0 ||
1113 s
.bitmap_chunk
== INVALID_SECTORS
||
1114 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1115 pr_err("invalid bitmap chunksize: %s\n",
1119 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1122 case O(GROW
, WriteBehind
):
1123 case O(BUILD
, WriteBehind
):
1124 case O(CREATE
, WriteBehind
): /* write-behind mode */
1125 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1127 s
.write_behind
= parse_num(optarg
);
1128 if (s
.write_behind
< 0 ||
1129 s
.write_behind
> 16383) {
1130 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1136 case O(INCREMENTAL
, 'r'):
1137 case O(INCREMENTAL
, RebuildMapOpt
):
1140 case O(INCREMENTAL
, IncrementalPath
):
1141 remove_path
= optarg
;
1144 /* We have now processed all the valid options. Anything else is
1147 if (option_index
> 0)
1148 pr_err(":option --%s not valid in %s mode\n",
1149 long_options
[option_index
].name
,
1150 map_num(modes
, mode
));
1152 pr_err("option -%c not valid in %s mode\n",
1153 opt
, map_num(modes
, mode
));
1160 if (print_help
== 2)
1161 help_text
= OptionHelp
;
1163 help_text
= mode_help
[mode
];
1164 if (help_text
== NULL
)
1166 fputs(help_text
,stdout
);
1170 if (!mode
&& devs_found
) {
1173 if (devlist
->disposition
== 0)
1174 devlist
->disposition
= devmode
;
1177 fputs(Usage
, stderr
);
1182 struct createinfo
*ci
= conf_get_create_info();
1184 if (strcasecmp(symlinks
, "yes") == 0)
1186 else if (strcasecmp(symlinks
, "no") == 0)
1189 pr_err("option --symlinks must be 'no' or 'yes'\n");
1193 /* Ok, got the option parsing out of the way
1194 * hopefully it's mostly right but there might be some stuff
1197 * That is mosty checked in the per-mode stuff but...
1199 * For @,B,C and A without -s, the first device listed must be
1200 * an md device. We check that here and open it.
1203 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1205 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1206 if (devs_found
< 1) {
1207 pr_err("an md device must be given in this mode\n");
1210 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1211 pr_err("--super-minor=dev is incompatible with --auto\n");
1214 if (mode
== MANAGE
|| mode
== GROW
) {
1215 mdfd
= open_mddev(devlist
->devname
, 1);
1219 /* non-existent device is OK */
1220 mdfd
= open_mddev(devlist
->devname
, 0);
1222 pr_err("device %s exists but is not an "
1223 "md array.\n", devlist
->devname
);
1226 if ((int)ident
.super_minor
== -2) {
1229 pr_err("--super-minor=dev given, and "
1230 "listed device %s doesn't exist.\n",
1235 ident
.super_minor
= minor(stb
.st_rdev
);
1237 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1238 /* We don't really want this open yet, we just might
1239 * have wanted to check some things
1247 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1248 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1249 " a mistake. If you really mean it you will need to specify --force before\n"
1250 " setting the number of drives.\n");
1255 if (c
.homehost
== NULL
)
1256 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1257 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1258 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1259 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1260 c
.homehost
= sys_hostname
;
1263 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1265 c
.require_homehost
= 0;
1268 if (c
.backup_file
&& data_offset
!= INVALID_SECTORS
) {
1269 pr_err("--backup-file and --data-offset are incompatible\n");
1273 if ((mode
== MISC
&& devmode
== 'E')
1274 || (mode
== MONITOR
&& spare_sharing
== 0))
1275 /* Anyone may try this */;
1276 else if (geteuid() != 0) {
1277 pr_err("must be super-user to perform this action\n");
1281 ident
.autof
= c
.autof
;
1283 if (c
.scan
&& c
.verbose
< 2)
1284 /* --scan implied --brief unless -vv */
1290 /* readonly, add/remove, readwrite, runstop */
1292 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1293 if (!rv
&& devs_found
>1)
1294 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1295 devlist
->next
, c
.verbose
, c
.test
,
1297 if (!rv
&& c
.readonly
< 0)
1298 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1299 if (!rv
&& c
.runstop
> 0)
1300 rv
= Manage_run(devlist
->devname
, mdfd
, &c
);
1301 if (!rv
&& c
.runstop
< 0)
1302 rv
= Manage_stop(devlist
->devname
, mdfd
, c
.verbose
, 0);
1305 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1306 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1307 /* Only a device has been given, so get details from config file */
1308 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1309 if (array_ident
== NULL
) {
1310 pr_err("%s not identified in config file.\n",
1316 if (array_ident
->autof
== 0)
1317 array_ident
->autof
= c
.autof
;
1318 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1322 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1324 else if (devs_found
> 0) {
1325 if (c
.update
&& devs_found
> 1) {
1326 pr_err("can only update a single array at a time\n");
1329 if (c
.backup_file
&& devs_found
> 1) {
1330 pr_err("can only assemble a single array when providing a backup file.\n");
1333 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1334 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1335 if (array_ident
== NULL
) {
1336 pr_err("%s not identified in config file.\n",
1341 if (array_ident
->autof
== 0)
1342 array_ident
->autof
= c
.autof
;
1343 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1348 pr_err("--update not meaningful with a --scan assembly.\n");
1351 if (c
.backup_file
) {
1352 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1355 rv
= scan_assemble(ss
, &c
, &ident
);
1361 c
.delay
= DEFAULT_BITMAP_DELAY
;
1362 if (s
.write_behind
&& !s
.bitmap_file
) {
1363 pr_err("write-behind mode requires a bitmap.\n");
1367 if (s
.raiddisks
== 0) {
1368 pr_err("no raid-devices specified.\n");
1373 if (s
.bitmap_file
) {
1374 if (strcmp(s
.bitmap_file
, "internal")==0) {
1375 pr_err("'internal' bitmaps not supported with --build\n");
1380 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1384 c
.delay
= DEFAULT_BITMAP_DELAY
;
1385 if (s
.write_behind
&& !s
.bitmap_file
) {
1386 pr_err("write-behind mode requires a bitmap.\n");
1390 if (s
.raiddisks
== 0) {
1391 pr_err("no raid-devices specified.\n");
1396 rv
= Create(ss
, devlist
->devname
,
1397 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1398 devs_found
-1, devlist
->next
,
1399 &s
, &c
, data_offset
);
1402 if (devmode
== 'E') {
1403 if (devlist
== NULL
&& !c
.scan
) {
1404 pr_err("No devices to examine\n");
1407 if (devlist
== NULL
)
1408 devlist
= conf_get_devs();
1409 if (devlist
== NULL
) {
1410 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1413 rv
= Examine(devlist
, &c
, ss
);
1414 } else if (devmode
== DetailPlatform
) {
1415 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1416 c
.verbose
, c
.export
,
1417 devlist
? devlist
->devname
: NULL
);
1418 } else if (devlist
== NULL
) {
1419 if (devmode
== 'S' && c
.scan
)
1420 rv
= stop_scan(c
.verbose
);
1421 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1422 rv
= misc_scan(devmode
, &c
);
1423 else if (devmode
== UdevRules
)
1424 rv
= Write_rules(udev_filename
);
1426 pr_err("No devices given.\n");
1430 rv
= misc_list(devlist
, &ident
, dump_directory
, ss
, &c
);
1433 if (!devlist
&& !c
.scan
) {
1434 pr_err("Cannot monitor: need --scan or at least one device\n");
1438 if (pidfile
&& !daemonise
) {
1439 pr_err("Cannot write a pid file when not in daemon mode\n");
1444 if (get_linux_version() > 2006016)
1445 /* mdstat responds to poll */
1452 rv
= Monitor(devlist
, mailaddr
, program
,
1453 &c
, daemonise
, oneshot
,
1454 dosyslog
, pidfile
, increments
,
1459 if (array_size
> 0) {
1460 /* alway impose array size first, independent of
1462 * Do not allow level or raid_disks changes at the
1463 * same time as that can be irreversibly destructive.
1467 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1468 pr_err("cannot change array size in same operation "
1469 "as changing raiddisks or level.\n"
1470 " Change size first, then check that data is still intact.\n");
1474 sysfs_init(&sra
, mdfd
, NULL
);
1475 if (array_size
== MAX_SIZE
)
1476 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1478 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1481 pr_err("--array-size setting"
1482 " is too large.\n");
1484 pr_err("current kernel does"
1485 " not support setting --array-size\n");
1490 if (devs_found
> 1 && s
.raiddisks
== 0 && s
.level
== UnSet
) {
1492 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1493 pr_err("--add cannot be used with "
1494 "other geometry changes in --grow mode\n");
1498 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1499 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1504 } else if (s
.bitmap_file
) {
1505 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1506 s
.layout_str
!= NULL
|| devs_found
> 1) {
1507 pr_err("--bitmap changes cannot be "
1508 "used with other geometry changes "
1509 "in --grow mode\n");
1514 c
.delay
= DEFAULT_BITMAP_DELAY
;
1515 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1516 } else if (grow_continue
)
1517 rv
= Grow_continue_command(devlist
->devname
,
1518 mdfd
, c
.backup_file
,
1520 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1521 || s
.chunk
!= 0 || s
.level
!= UnSet
1522 || data_offset
!= INVALID_SECTORS
) {
1523 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1525 data_offset
, &c
, &s
);
1526 } else if (array_size
== 0)
1527 pr_err("no changes to --grow\n");
1536 pr_err("In --incremental mode, a device cannot be given with --scan.\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
, NULL
);
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 if (dv
->devname
[0] == '/')
1807 mdfd
= open_mddev(dv
->devname
, 1);
1809 mdfd
= open_dev(dv
->devname
);
1811 pr_err("Cannot open %s\n", dv
->devname
);
1814 switch(dv
->disposition
) {
1817 rv
|= Manage_run(dv
->devname
, mdfd
, c
); break;
1819 rv
|= Manage_stop(dv
->devname
, mdfd
, c
->verbose
, 0); break;
1821 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1823 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;