2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2012 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
24 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
25 * Paul Clements, SteelEye Technology, Inc.
33 static int scan_assemble(struct supertype
*ss
,
35 struct mddev_ident
*ident
);
36 static int misc_scan(char devmode
, struct context
*c
);
37 static int stop_scan(int verbose
);
38 static int misc_list(struct mddev_dev
*devlist
,
39 struct mddev_ident
*ident
,
40 struct supertype
*ss
, struct context
*c
);
43 int main(int argc
, char *argv
[])
51 unsigned long long array_size
= 0;
52 unsigned long long data_offset
= INVALID_SECTORS
;
53 struct mddev_ident ident
;
54 char *configfile
= NULL
;
57 struct mddev_dev
*devlist
= NULL
;
58 struct mddev_dev
**devlistend
= & devlist
;
61 char *symlinks
= NULL
;
62 int grow_continue
= 0;
63 /* autof indicates whether and how to create device node.
64 * bottom 3 bits are style. Rest (when shifted) are number of parts
66 * 1 - don't create (no)
67 * 2 - if is_standard, then create (yes)
68 * 3 - create as 'md' - reject is_standard mdp (md)
69 * 4 - create as 'mdp' - reject is_standard md (mdp)
70 * 5 - default to md if not is_standard (md in config file)
71 * 6 - default to mdp if not is_standard (part, or mdp in config file)
74 .require_homehost
= 1,
79 .bitmap_chunk
= UnSet
,
82 char sys_hostname
[256];
83 char *mailaddr
= NULL
;
89 int spare_sharing
= 1;
90 struct supertype
*ss
= NULL
;
92 char *shortopt
= short_options
;
95 char *remove_path
= NULL
;
96 char *udev_filename
= NULL
;
103 srandom(time(0) ^ getpid());
107 ident
.raid_disks
= UnSet
;
108 ident
.super_minor
= UnSet
;
110 ident
.spare_group
= NULL
;
113 ident
.bitmap_fd
= -1;
114 ident
.bitmap_file
= NULL
;
116 ident
.container
= NULL
;
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 * --offroot sets first char of argv[0] to @. This is used
164 * by systemd to signal that the task was launched from
165 * initrd/initramfs and should be preserved during shutdown
175 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
181 fputs(Usage
, stderr
);
184 /* second, figure out the mode.
185 * Some options force the mode. Others
186 * set the mode if it isn't already
192 shortopt
= short_bitmap_options
;
202 case ReAdd
: /* re-add */
205 shortopt
= short_bitmap_options
;
209 case 'A': newmode
= ASSEMBLE
;
210 shortopt
= short_bitmap_auto_options
;
212 case 'B': newmode
= BUILD
;
213 shortopt
= short_bitmap_auto_options
;
215 case 'C': newmode
= CREATE
;
216 shortopt
= short_bitmap_auto_options
;
218 case 'F': newmode
= MONITOR
;
220 case 'G': newmode
= GROW
;
221 shortopt
= short_bitmap_options
;
223 case 'I': newmode
= INCREMENTAL
;
224 shortopt
= short_bitmap_auto_options
;
227 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 fprintf(stderr
," 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"
342 " for subsequent devices at %s\n", optarg
);
345 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
346 pr_err("Must give -a/--add for"
347 " devices to add: %s\n", optarg
);
350 dv
= xmalloc(sizeof(*dv
));
351 dv
->devname
= optarg
;
352 dv
->disposition
= devmode
;
353 dv
->writemostly
= writemostly
;
357 devlistend
= &dv
->next
;
363 /* We've got a mode, and opt is now something else which
364 * could depend on the mode */
365 #define O(a,b) ((a<<16)|b)
366 switch (O(mode
,opt
)) {
368 case O(GROW
,ChunkSize
):
370 case O(CREATE
,ChunkSize
):
371 case O(BUILD
,'c'): /* chunk or rounding */
372 case O(BUILD
,ChunkSize
): /* chunk or rounding */
374 pr_err("chunk/rounding may only be specified once. "
375 "Second value is %s.\n", optarg
);
378 s
.chunk
= parse_size(optarg
);
379 if (s
.chunk
== INVALID_SECTORS
||
380 s
.chunk
< 8 || (s
.chunk
&1)) {
381 pr_err("invalid chunk/rounding value: %s\n",
385 /* Convert sectors to K */
389 case O(INCREMENTAL
, 'e'):
391 case O(ASSEMBLE
,'e'):
392 case O(MISC
,'e'): /* set metadata (superblock) information */
394 pr_err("metadata information already given\n");
397 for(i
=0; !ss
&& superlist
[i
]; i
++)
398 ss
= superlist
[i
]->match_metadata_desc(optarg
);
401 pr_err("unrecognised metadata identifier: %s\n", optarg
);
407 case O(MANAGE
,WriteMostly
):
409 case O(BUILD
,WriteMostly
):
411 case O(CREATE
,WriteMostly
):
412 /* set write-mostly for following devices */
417 /* clear write-mostly for following devices */
424 case O(BUILD
,'z'): /* size */
426 pr_err("size may only be specified once. "
427 "Second value is %s.\n", optarg
);
430 if (strcmp(optarg
, "max")==0)
433 s
.size
= parse_size(optarg
);
434 if (s
.size
== INVALID_SECTORS
||
436 pr_err("invalid size: %s\n",
440 /* convert sectors to K */
445 case O(GROW
,'Z'): /* array size */
446 if (array_size
> 0) {
447 pr_err("array-size may only be specified once. "
448 "Second value is %s.\n", optarg
);
451 if (strcmp(optarg
, "max") == 0)
452 array_size
= MAX_SIZE
;
454 array_size
= parse_size(optarg
);
455 if (array_size
== 0 ||
456 array_size
== INVALID_SECTORS
) {
457 pr_err("invalid array size: %s\n",
464 case O(CREATE
,DataOffset
):
465 case O(GROW
,DataOffset
):
466 if (data_offset
!= INVALID_SECTORS
) {
467 fprintf(stderr
, Name
": data-offset may only be specified one. "
468 "Second value is %s.\n", optarg
);
471 if (mode
== CREATE
&&
472 strcmp(optarg
, "variable") == 0)
473 data_offset
= VARIABLE_OFFSET
;
475 data_offset
= parse_size(optarg
);
476 if (data_offset
== INVALID_SECTORS
) {
477 fprintf(stderr
, Name
": invalid data-offset: %s\n",
485 case O(BUILD
,'l'): /* set raid level*/
486 if (s
.level
!= UnSet
) {
487 pr_err("raid level may only be set once. "
488 "Second value is %s.\n", optarg
);
491 s
.level
= map_name(pers
, optarg
);
492 if (s
.level
== UnSet
) {
493 pr_err("invalid raid level: %s\n",
497 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
498 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
501 pr_err("Raid level %s not permitted with --build.\n",
505 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
506 pr_err("raid level %s is incompatible with spare-devices setting.\n",
510 ident
.level
= s
.level
;
513 case O(GROW
, 'p'): /* new layout */
514 case O(GROW
, Layout
):
516 pr_err("layout may only be sent once. "
517 "Second value was %s\n", optarg
);
520 s
.layout_str
= optarg
;
521 /* 'Grow' will parse the value */
524 case O(CREATE
,'p'): /* raid5 layout */
525 case O(CREATE
,Layout
):
526 case O(BUILD
,'p'): /* faulty layout */
527 case O(BUILD
,Layout
):
528 if (s
.layout
!= UnSet
) {
529 pr_err("layout may only be sent once. "
530 "Second value was %s\n", optarg
);
535 pr_err("layout not meaningful for %s arrays.\n",
536 map_num(pers
, s
.level
));
539 pr_err("raid level must be given before layout.\n");
543 s
.layout
= map_name(r5layout
, optarg
);
544 if (s
.layout
==UnSet
) {
545 pr_err("layout %s not understood for raid5.\n",
551 s
.layout
= map_name(r6layout
, optarg
);
552 if (s
.layout
==UnSet
) {
553 pr_err("layout %s not understood for raid6.\n",
560 s
.layout
= parse_layout_10(optarg
);
562 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
570 s
.layout
= parse_layout_faulty(optarg
);
571 if (s
.layout
== -1) {
572 pr_err("layout %s not understood for faulty.\n",
580 case O(CREATE
,AssumeClean
):
581 case O(BUILD
,AssumeClean
): /* assume clean */
582 case O(GROW
,AssumeClean
):
588 case O(BUILD
,'n'): /* number of raid disks */
590 pr_err("raid-devices set twice: %d and %s\n",
591 s
.raiddisks
, optarg
);
594 s
.raiddisks
= parse_num(optarg
);
595 if (s
.raiddisks
<= 0) {
596 pr_err("invalid number of raid devices: %s\n",
600 ident
.raid_disks
= s
.raiddisks
;
603 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
605 pr_err("spare-devices set twice: %d and %s\n",
606 s
.sparedisks
, optarg
);
609 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
610 pr_err("spare-devices setting is incompatible with raid level %d\n",
614 s
.sparedisks
= parse_num(optarg
);
615 if (s
.sparedisks
< 0) {
616 pr_err("invalid number of spare-devices: %s\n",
626 case O(INCREMENTAL
,'a'):
627 case O(INCREMENTAL
,Auto
):
628 case O(ASSEMBLE
,'a'):
629 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
630 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
633 case O(CREATE
,Symlinks
):
634 case O(BUILD
,Symlinks
):
635 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
639 case O(BUILD
,'f'): /* force honouring '-n 1' */
640 case O(BUILD
,Force
): /* force honouring '-n 1' */
641 case O(GROW
,'f'): /* ditto */
642 case O(GROW
,Force
): /* ditto */
643 case O(CREATE
,'f'): /* force honouring of device list */
644 case O(CREATE
,Force
): /* force honouring of device list */
645 case O(ASSEMBLE
,'f'): /* force assembly */
646 case O(ASSEMBLE
,Force
): /* force assembly */
647 case O(MISC
,'f'): /* force zero */
648 case O(MISC
,Force
): /* force zero */
649 case O(MANAGE
,Force
): /* add device which is too large */
652 /* now for the Assemble options */
653 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
655 case O(INCREMENTAL
, FreezeReshape
):
656 c
.freeze_reshape
= 1;
658 case O(CREATE
,'u'): /* uuid of array */
659 case O(ASSEMBLE
,'u'): /* uuid of array */
660 if (ident
.uuid_set
) {
661 pr_err("uuid cannot be set twice. "
662 "Second value %s.\n", optarg
);
665 if (parse_uuid(optarg
, ident
.uuid
))
668 pr_err("Bad uuid: %s\n", optarg
);
674 case O(ASSEMBLE
,'N'):
677 pr_err("name cannot be set twice. "
678 "Second value %s.\n", optarg
);
681 if (mode
== MISC
&& !c
.subarray
) {
682 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
685 if (strlen(optarg
) > 32) {
686 pr_err("name '%s' is too long, 32 chars max.\n",
690 strcpy(ident
.name
, optarg
);
693 case O(ASSEMBLE
,'m'): /* super-minor for array */
694 case O(ASSEMBLE
,SuperMinor
):
695 if (ident
.super_minor
!= UnSet
) {
696 pr_err("super-minor cannot be set twice. "
697 "Second value: %s.\n", optarg
);
700 if (strcmp(optarg
, "dev")==0)
701 ident
.super_minor
= -2;
703 ident
.super_minor
= parse_num(optarg
);
704 if (ident
.super_minor
< 0) {
705 pr_err("Bad super-minor number: %s.\n", optarg
);
711 case O(ASSEMBLE
,'o'):
717 case O(ASSEMBLE
,'U'): /* update the superblock */
720 pr_err("Can only update one aspect"
721 " of superblock, both %s and %s given.\n",
725 if (mode
== MISC
&& !c
.subarray
) {
726 pr_err("Only subarrays can be"
727 " updated in misc mode\n");
731 if (strcmp(c
.update
, "sparc2.2")==0)
733 if (strcmp(c
.update
, "super-minor") == 0)
735 if (strcmp(c
.update
, "summaries")==0)
737 if (strcmp(c
.update
, "resync")==0)
739 if (strcmp(c
.update
, "uuid")==0)
741 if (strcmp(c
.update
, "name")==0)
743 if (strcmp(c
.update
, "homehost")==0)
745 if (strcmp(c
.update
, "devicesize")==0)
747 if (strcmp(c
.update
, "no-bitmap")==0)
749 if (strcmp(c
.update
, "bbl") == 0)
751 if (strcmp(c
.update
, "no-bbl") == 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"
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
):
825 pr_err("configfile cannot be set twice. "
826 "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
,ReAdd
):
921 case O(MANAGE
,'r'): /* remove a drive */
922 case O(MANAGE
,Remove
):
925 case O(MANAGE
,'f'): /* set faulty */
927 case O(INCREMENTAL
,'f'):
928 case O(INCREMENTAL
,Remove
):
929 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
930 * even though we will both fail and
931 * remove the device */
934 case O(MANAGE
,Replace
):
935 /* Mark these devices for replacement */
939 /* These are the replacements to use */
940 if (devmode
!= 'R') {
941 pr_err("--with must follow --replace\n");
946 case O(INCREMENTAL
,'R'):
948 case O(ASSEMBLE
,'R'):
950 case O(CREATE
,'R'): /* Run the array */
952 pr_err("Cannot both Stop and Run an array\n");
959 pr_err("Cannot both Run and Stop an array\n");
971 case O(MISC
,KillOpt
):
975 case O(MISC
, ExamineBB
):
979 case O(MISC
, WaitOpt
):
980 case O(MISC
, Waitclean
):
981 case O(MISC
, DetailPlatform
):
982 case O(MISC
, KillSubarray
):
983 case O(MISC
, UpdateSubarray
):
984 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
986 pr_err("subarray can only"
987 " be specified once\n");
992 if (devmode
&& devmode
!= opt
&&
993 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
994 pr_err("--examine/-E cannot be given with ");
995 if (devmode
== 'E') {
996 if (option_index
>= 0)
997 fprintf(stderr
, "--%s\n",
998 long_options
[option_index
].name
);
1000 fprintf(stderr
, "-%c\n", opt
);
1001 } else if (isalpha(devmode
))
1002 fprintf(stderr
, "-%c\n", devmode
);
1004 fprintf(stderr
, "previous option\n");
1009 case O(MISC
, UdevRules
):
1010 if (devmode
&& devmode
!= opt
) {
1011 pr_err("--udev-rules must"
1012 " be the only option.\n");
1015 pr_err("only specify one udev "
1016 "rule filename. %s ignored.\n",
1019 udev_filename
= optarg
;
1027 case O(MISC
, Sparc22
):
1028 if (devmode
!= 'E') {
1029 pr_err("--sparc2.2 only allowed with --examine\n");
1035 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1036 case O(ASSEMBLE
,Bitmap
):
1038 pr_err("bitmap file needed with -b in --assemble mode\n");
1041 if (strcmp(optarg
, "internal")==0) {
1042 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1045 bitmap_fd
= open(optarg
, O_RDWR
);
1046 if (!*optarg
|| bitmap_fd
< 0) {
1047 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1050 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1053 case O(ASSEMBLE
, BackupFile
):
1054 case O(GROW
, BackupFile
):
1055 /* Specify a file into which grow might place a backup,
1056 * or from which assemble might recover a backup
1058 if (c
.backup_file
) {
1059 pr_err("backup file already specified, rejecting %s\n", optarg
);
1062 c
.backup_file
= optarg
;
1065 case O(GROW
, Continue
):
1066 /* Continue interrupted grow
1070 case O(ASSEMBLE
, InvalidBackup
):
1071 /* Acknowledge that the backupfile is invalid, but ask
1072 * to continue anyway
1074 c
.invalid_backup
= 1;
1078 case O(BUILD
,Bitmap
):
1080 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1081 if (strcmp(optarg
, "none") == 0) {
1082 pr_err("'--bitmap none' only"
1083 " supported for --grow\n");
1088 case O(GROW
,Bitmap
):
1089 if (strcmp(optarg
, "internal")== 0 ||
1090 strcmp(optarg
, "none")== 0 ||
1091 strchr(optarg
, '/') != NULL
) {
1092 s
.bitmap_file
= optarg
;
1096 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1097 " not '%s'\n", optarg
);
1100 case O(GROW
,BitmapChunk
):
1101 case O(BUILD
,BitmapChunk
):
1102 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1103 s
.bitmap_chunk
= parse_size(optarg
);
1104 if (s
.bitmap_chunk
== 0 ||
1105 s
.bitmap_chunk
== INVALID_SECTORS
||
1106 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1107 pr_err("invalid bitmap chunksize: %s\n",
1111 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1114 case O(GROW
, WriteBehind
):
1115 case O(BUILD
, WriteBehind
):
1116 case O(CREATE
, WriteBehind
): /* write-behind mode */
1117 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1119 s
.write_behind
= parse_num(optarg
);
1120 if (s
.write_behind
< 0 ||
1121 s
.write_behind
> 16383) {
1122 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1128 case O(INCREMENTAL
, 'r'):
1129 case O(INCREMENTAL
, RebuildMapOpt
):
1132 case O(INCREMENTAL
, IncrementalPath
):
1133 remove_path
= optarg
;
1136 /* We have now processed all the valid options. Anything else is
1139 if (option_index
> 0)
1140 pr_err(":option --%s not valid in %s mode\n",
1141 long_options
[option_index
].name
,
1142 map_num(modes
, mode
));
1144 pr_err("option -%c not valid in %s mode\n",
1145 opt
, map_num(modes
, mode
));
1152 if (print_help
== 2)
1153 help_text
= OptionHelp
;
1155 help_text
= mode_help
[mode
];
1156 if (help_text
== NULL
)
1158 fputs(help_text
,stdout
);
1162 if (!mode
&& devs_found
) {
1165 if (devlist
->disposition
== 0)
1166 devlist
->disposition
= devmode
;
1169 fputs(Usage
, stderr
);
1174 struct createinfo
*ci
= conf_get_create_info();
1176 if (strcasecmp(symlinks
, "yes") == 0)
1178 else if (strcasecmp(symlinks
, "no") == 0)
1181 pr_err("option --symlinks must be 'no' or 'yes'\n");
1185 /* Ok, got the option parsing out of the way
1186 * hopefully it's mostly right but there might be some stuff
1189 * That is mosty checked in the per-mode stuff but...
1191 * For @,B,C and A without -s, the first device listed must be
1192 * an md device. We check that here and open it.
1195 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1197 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1198 if (devs_found
< 1) {
1199 pr_err("an md device must be given in this mode\n");
1202 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1203 pr_err("--super-minor=dev is incompatible with --auto\n");
1206 if (mode
== MANAGE
|| mode
== GROW
) {
1207 mdfd
= open_mddev(devlist
->devname
, 1);
1211 /* non-existent device is OK */
1212 mdfd
= open_mddev(devlist
->devname
, 0);
1214 pr_err("device %s exists but is not an "
1215 "md array.\n", devlist
->devname
);
1218 if ((int)ident
.super_minor
== -2) {
1221 pr_err("--super-minor=dev given, and "
1222 "listed device %s doesn't exist.\n",
1227 ident
.super_minor
= minor(stb
.st_rdev
);
1229 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1230 /* We don't really want this open yet, we just might
1231 * have wanted to check some things
1239 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1240 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1241 " a mistake. If you really mean it you will need to specify --force before\n"
1242 " setting the number of drives.\n");
1247 if (c
.homehost
== NULL
)
1248 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1249 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1250 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1251 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1252 c
.homehost
= sys_hostname
;
1255 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1257 c
.require_homehost
= 0;
1260 if ((mode
== MISC
&& devmode
== 'E')
1261 || (mode
== MONITOR
&& spare_sharing
== 0))
1262 /* Anyone may try this */;
1263 else if (geteuid() != 0) {
1264 pr_err("must be super-user to perform this action\n");
1268 ident
.autof
= c
.autof
;
1270 if (c
.scan
&& c
.verbose
< 2)
1271 /* --scan implied --brief unless -vv */
1277 /* readonly, add/remove, readwrite, runstop */
1279 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1280 if (!rv
&& devs_found
>1)
1281 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1282 devlist
->next
, c
.verbose
, c
.test
,
1284 if (!rv
&& c
.readonly
< 0)
1285 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1286 if (!rv
&& c
.runstop
)
1287 rv
= Manage_runstop(devlist
->devname
, mdfd
, c
.runstop
, c
.verbose
, 0);
1290 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1291 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1292 /* Only a device has been given, so get details from config file */
1293 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1294 if (array_ident
== NULL
) {
1295 pr_err("%s not identified in config file.\n",
1301 if (array_ident
->autof
== 0)
1302 array_ident
->autof
= c
.autof
;
1303 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1307 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1309 else if (devs_found
> 0) {
1310 if (c
.update
&& devs_found
> 1) {
1311 pr_err("can only update a single array at a time\n");
1314 if (c
.backup_file
&& devs_found
> 1) {
1315 pr_err("can only assemble a single array when providing a backup file.\n");
1318 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1319 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1320 if (array_ident
== NULL
) {
1321 pr_err("%s not identified in config file.\n",
1326 if (array_ident
->autof
== 0)
1327 array_ident
->autof
= c
.autof
;
1328 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1333 pr_err("--update not meaningful with a --scan assembly.\n");
1336 if (c
.backup_file
) {
1337 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1340 rv
= scan_assemble(ss
, &c
, &ident
);
1346 c
.delay
= DEFAULT_BITMAP_DELAY
;
1347 if (s
.write_behind
&& !s
.bitmap_file
) {
1348 pr_err("write-behind mode requires a bitmap.\n");
1352 if (s
.raiddisks
== 0) {
1353 pr_err("no raid-devices specified.\n");
1358 if (s
.bitmap_file
) {
1359 if (strcmp(s
.bitmap_file
, "internal")==0) {
1360 pr_err("'internal' bitmaps not supported with --build\n");
1365 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1369 c
.delay
= DEFAULT_BITMAP_DELAY
;
1370 if (s
.write_behind
&& !s
.bitmap_file
) {
1371 pr_err("write-behind mode requires a bitmap.\n");
1375 if (s
.raiddisks
== 0) {
1376 pr_err("no raid-devices specified.\n");
1381 rv
= Create(ss
, devlist
->devname
,
1382 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1383 devs_found
-1, devlist
->next
,
1384 &s
, &c
, data_offset
);
1387 if (devmode
== 'E') {
1388 if (devlist
== NULL
&& !c
.scan
) {
1389 pr_err("No devices to examine\n");
1392 if (devlist
== NULL
)
1393 devlist
= conf_get_devs();
1394 if (devlist
== NULL
) {
1395 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1398 rv
= Examine(devlist
, &c
, ss
);
1399 } else if (devmode
== DetailPlatform
) {
1400 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1401 c
.verbose
, c
.export
,
1402 devlist
? devlist
->devname
: NULL
);
1403 } else if (devlist
== NULL
) {
1404 if (devmode
== 'S' && c
.scan
)
1405 rv
= stop_scan(c
.verbose
);
1406 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1407 rv
= misc_scan(devmode
, &c
);
1408 else if (devmode
== UdevRules
)
1409 rv
= Write_rules(udev_filename
);
1411 pr_err("No devices given.\n");
1415 rv
= misc_list(devlist
, &ident
, ss
, &c
);
1418 if (!devlist
&& !c
.scan
) {
1419 pr_err("Cannot monitor: need --scan or at least one device\n");
1423 if (pidfile
&& !daemonise
) {
1424 pr_err("Cannot write a pid file when not in daemon mode\n");
1429 if (get_linux_version() > 2006016)
1430 /* mdstat responds to poll */
1437 rv
= Monitor(devlist
, mailaddr
, program
,
1438 &c
, daemonise
, oneshot
,
1439 dosyslog
, pidfile
, increments
,
1444 if (array_size
> 0) {
1445 /* alway impose array size first, independent of
1447 * Do not allow level or raid_disks changes at the
1448 * same time as that can be irreversibly destructive.
1452 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1453 pr_err("cannot change array size in same operation "
1454 "as changing raiddisks or level.\n"
1455 " Change size first, then check that data is still intact.\n");
1459 sysfs_init(&sra
, mdfd
, 0);
1460 if (array_size
== MAX_SIZE
)
1461 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1463 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1466 pr_err("--array-size setting"
1467 " is too large.\n");
1469 pr_err("current kernel does"
1470 " not support setting --array-size\n");
1475 if (devs_found
> 1 && s
.raiddisks
== 0) {
1477 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1478 pr_err("--add cannot be used with "
1479 "other geometry changes in --grow mode\n");
1483 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1484 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1489 } else if (s
.bitmap_file
) {
1490 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1491 s
.layout_str
!= NULL
|| devs_found
> 1) {
1492 pr_err("--bitmap changes cannot be "
1493 "used with other geometry changes "
1494 "in --grow mode\n");
1499 c
.delay
= DEFAULT_BITMAP_DELAY
;
1500 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1501 } else if (grow_continue
)
1502 rv
= Grow_continue_command(devlist
->devname
,
1503 mdfd
, c
.backup_file
,
1505 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1506 || s
.chunk
!= 0 || s
.level
!= UnSet
) {
1507 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1509 data_offset
, &c
, &s
);
1510 } else if (array_size
== 0)
1511 pr_err("no changes to --grow\n");
1518 if (c
.runstop
<= 0) {
1519 pr_err("--incremental --scan meaningless without --run.\n");
1522 if (devmode
== 'f') {
1523 pr_err("--incremental --scan --fail not supported.\n");
1526 rv
= IncrementalScan(c
.verbose
);
1529 if (!rebuild_map
&& !c
.scan
) {
1530 pr_err("--incremental requires a device.\n");
1535 if (devlist
->next
) {
1536 pr_err("--incremental can only handle one device.\n");
1541 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1544 rv
= Incremental(devlist
->devname
, &c
, ss
);
1553 static int scan_assemble(struct supertype
*ss
,
1555 struct mddev_ident
*ident
)
1557 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1558 struct mddev_dev
*devlist
= conf_get_devs();
1559 struct map_ent
*map
= NULL
;
1562 int failures
, successes
;
1564 if (conf_verify_devnames(array_list
)) {
1565 pr_err("Duplicate MD device names in "
1566 "conf file were found.\n");
1569 if (devlist
== NULL
) {
1570 pr_err("No devices listed in conf file were found.\n");
1573 for (a
= array_list
; a
; a
= a
->next
) {
1576 a
->autof
= c
->autof
;
1579 pr_err("%s: failed to get "
1580 "exclusive lock on mapfile\n",
1586 for (a
= array_list
; a
; a
= a
->next
) {
1591 strcasecmp(a
->devname
, "<ignore>") == 0)
1594 r
= Assemble(ss
, a
->devname
,
1604 } while (failures
&& successes
);
1605 if (c
->homehost
&& cnt
== 0) {
1606 /* Maybe we can auto-assemble something.
1607 * Repeatedly call Assemble in auto-assemble mode
1612 ident
->autof
= c
->autof
;
1614 struct mddev_dev
*devlist
= conf_get_devs();
1617 rv2
= Assemble(ss
, NULL
,
1625 /* Incase there are stacked devices, we need to go around again */
1627 if (cnt
== 0 && rv
== 0) {
1628 pr_err("No arrays found in config file or automatically\n");
1632 } else if (cnt
== 0 && rv
== 0) {
1633 pr_err("No arrays found in config file\n");
1640 static int misc_scan(char devmode
, struct context
*c
)
1642 /* apply --detail or --wait-clean to
1643 * all devices in /proc/mdstat
1645 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1646 struct mdstat_ent
*e
;
1647 struct map_ent
*map
= NULL
;
1651 for (members
= 0; members
<= 1; members
++) {
1652 for (e
=ms
; e
; e
=e
->next
) {
1655 int member
= e
->metadata_version
&&
1656 strncmp(e
->metadata_version
,
1657 "external:/", 10) == 0;
1658 if (members
!= member
)
1660 me
= map_by_devnum(&map
, e
->devnum
);
1662 && strcmp(me
->path
, "/unknown") != 0)
1665 name
= get_md_name(e
->devnum
);
1668 pr_err("cannot find device file for %s\n",
1673 rv
|= Detail(name
, c
);
1675 rv
|= WaitClean(name
, -1, c
->verbose
);
1683 static int stop_scan(int verbose
)
1685 /* apply --stop to all devices in /proc/mdstat */
1686 /* Due to possible stacking of devices, repeat until
1687 * nothing more can be stopped
1689 int progress
=1, err
;
1693 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1694 struct mdstat_ent
*e
;
1696 if (!progress
) last
= 1;
1697 progress
= 0; err
= 0;
1698 for (e
=ms
; e
; e
=e
->next
) {
1699 char *name
= get_md_name(e
->devnum
);
1703 pr_err("cannot find device file for %s\n",
1707 mdfd
= open_mddev(name
, 1);
1709 if (Manage_runstop(name
, mdfd
, -1, verbose
, !last
))
1719 } while (!last
&& err
);
1725 static int misc_list(struct mddev_dev
*devlist
,
1726 struct mddev_ident
*ident
,
1727 struct supertype
*ss
, struct context
*c
)
1729 struct mddev_dev
*dv
;
1732 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1735 switch(dv
->disposition
) {
1737 rv
|= Detail(dv
->devname
, c
);
1739 case KillOpt
: /* Zero superblock */
1741 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1745 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1752 rv
|= Query(dv
->devname
); continue;
1754 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1756 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1759 rv
|= Wait(dv
->devname
); continue;
1761 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1763 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1765 case UpdateSubarray
:
1766 if (c
->update
== NULL
) {
1767 pr_err("-U/--update must be specified with --update-subarray\n");
1771 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1772 c
->update
, ident
, c
->verbose
);
1775 mdfd
= open_mddev(dv
->devname
, 1);
1777 switch(dv
->disposition
) {
1779 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, c
->verbose
, 0); break;
1781 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, c
->verbose
, 0); break;
1783 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1785 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;