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.
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
;
120 * set first char of argv[0] to @. This is used by
121 * systemd to signal that the task was launched from
122 * initrd/initramfs and should be preserved during shutdown
126 while ((option_index
= -1) ,
127 (opt
=getopt_long(argc
, argv
,
128 shortopt
, long_options
,
129 &option_index
)) != -1) {
131 /* firstly, some mode-independent options */
141 fputs(Version
, stderr
);
144 case 'v': c
.verbose
++;
147 case 'q': c
.verbose
--;
151 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
152 || mode
== GROW
|| mode
== INCREMENTAL
154 break; /* b means bitmap */
159 case 'Y': c
.export
++;
163 if (strcasecmp(optarg
, "<ignore>") == 0)
164 c
.require_homehost
= 0;
170 /* Silently ignore old option */
176 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
182 fputs(Usage
, stderr
);
185 /* second, figure out the mode.
186 * Some options force the mode. Others
187 * set the mode if it isn't already
193 shortopt
= short_bitmap_options
;
203 case ReAdd
: /* re-add */
206 shortopt
= short_bitmap_options
;
210 case 'A': newmode
= ASSEMBLE
;
211 shortopt
= short_bitmap_auto_options
;
213 case 'B': newmode
= BUILD
;
214 shortopt
= short_bitmap_auto_options
;
216 case 'C': newmode
= CREATE
;
217 shortopt
= short_bitmap_auto_options
;
219 case 'F': newmode
= MONITOR
;
221 case 'G': newmode
= GROW
;
222 shortopt
= short_bitmap_options
;
224 case 'I': newmode
= INCREMENTAL
;
225 shortopt
= short_bitmap_auto_options
;
228 newmode
= AUTODETECT
;
262 if (mode
&& newmode
== mode
) {
263 /* everybody happy ! */
264 } else if (mode
&& newmode
!= mode
) {
267 if (option_index
>= 0)
268 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
270 fprintf(stderr
, "-%c", opt
);
271 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
272 map_num(modes
, newmode
),
273 map_num(modes
, mode
));
275 } else if (!mode
&& newmode
) {
277 if (mode
== MISC
&& devs_found
) {
278 pr_err("No action given for %s in --misc mode\n",
280 cont_err("Action options must come before device names\n");
284 /* special case of -c --help */
285 if ((opt
== 'c' || opt
== ConfigFile
) &&
286 ( strncmp(optarg
, "--h", 3)==0 ||
287 strncmp(optarg
, "-h", 2)==0)) {
288 fputs(Help_config
, stdout
);
292 /* If first option is a device, don't force the mode yet */
294 if (devs_found
== 0) {
295 dv
= xmalloc(sizeof(*dv
));
296 dv
->devname
= optarg
;
297 dv
->disposition
= devmode
;
298 dv
->writemostly
= writemostly
;
302 devlistend
= &dv
->next
;
307 /* No mode yet, and this is the second device ... */
308 pr_err("An option must be given to set the mode before a second device\n"
309 " (%s) is listed\n", optarg
);
312 if (option_index
>= 0)
313 pr_err("--%s", long_options
[option_index
].name
);
316 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
320 /* if we just set the mode, then done */
334 /* an undecorated option - must be a device name.
337 if (devs_found
> 0 && devmode
== DetailPlatform
) {
338 pr_err("controller may only be specified once. %s ignored\n",
343 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
344 pr_err("Must give one of -a/-r/-f"
345 " for subsequent devices at %s\n", optarg
);
348 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
349 pr_err("Must give -a/--add for"
350 " devices to add: %s\n", optarg
);
353 dv
= xmalloc(sizeof(*dv
));
354 dv
->devname
= optarg
;
355 dv
->disposition
= devmode
;
356 dv
->writemostly
= writemostly
;
360 devlistend
= &dv
->next
;
366 /* We've got a mode, and opt is now something else which
367 * could depend on the mode */
368 #define O(a,b) ((a<<16)|b)
369 switch (O(mode
,opt
)) {
371 case O(GROW
,ChunkSize
):
373 case O(CREATE
,ChunkSize
):
374 case O(BUILD
,'c'): /* chunk or rounding */
375 case O(BUILD
,ChunkSize
): /* chunk or rounding */
377 pr_err("chunk/rounding may only be specified once. "
378 "Second value is %s.\n", optarg
);
381 s
.chunk
= parse_size(optarg
);
382 if (s
.chunk
== INVALID_SECTORS
||
383 s
.chunk
< 8 || (s
.chunk
&1)) {
384 pr_err("invalid chunk/rounding value: %s\n",
388 /* Convert sectors to K */
392 case O(INCREMENTAL
, 'e'):
394 case O(ASSEMBLE
,'e'):
395 case O(MISC
,'e'): /* set metadata (superblock) information */
397 pr_err("metadata information already given\n");
400 for(i
=0; !ss
&& superlist
[i
]; i
++)
401 ss
= superlist
[i
]->match_metadata_desc(optarg
);
404 pr_err("unrecognised metadata identifier: %s\n", optarg
);
410 case O(MANAGE
,WriteMostly
):
412 case O(BUILD
,WriteMostly
):
414 case O(CREATE
,WriteMostly
):
415 /* set write-mostly for following devices */
420 /* clear write-mostly for following devices */
426 case O(BUILD
,'z'): /* size */
428 pr_err("size may only be specified once. "
429 "Second value is %s.\n", optarg
);
432 if (strcmp(optarg
, "max")==0)
435 s
.size
= parse_size(optarg
);
436 if (s
.size
== INVALID_SECTORS
||
438 pr_err("invalid size: %s\n",
442 /* convert sectors to K */
447 case O(GROW
,'Z'): /* array size */
448 if (array_size
> 0) {
449 pr_err("array-size may only be specified once. "
450 "Second value is %s.\n", optarg
);
453 if (strcmp(optarg
, "max") == 0)
454 array_size
= MAX_SIZE
;
456 array_size
= parse_size(optarg
);
457 if (array_size
== 0 ||
458 array_size
== INVALID_SECTORS
) {
459 pr_err("invalid array size: %s\n",
466 case O(CREATE
,DataOffset
):
467 case O(GROW
,DataOffset
):
468 if (data_offset
!= INVALID_SECTORS
) {
469 pr_err("data-offset may only be specified one. "
470 "Second value is %s.\n", optarg
);
473 if (mode
== CREATE
&&
474 strcmp(optarg
, "variable") == 0)
475 data_offset
= VARIABLE_OFFSET
;
477 data_offset
= parse_size(optarg
);
478 if (data_offset
== INVALID_SECTORS
) {
479 pr_err("invalid data-offset: %s\n",
487 case O(BUILD
,'l'): /* set raid level*/
488 if (s
.level
!= UnSet
) {
489 pr_err("raid level may only be set once. "
490 "Second value is %s.\n", optarg
);
493 s
.level
= map_name(pers
, optarg
);
494 if (s
.level
== UnSet
) {
495 pr_err("invalid raid level: %s\n",
499 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
500 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
503 pr_err("Raid level %s not permitted with --build.\n",
507 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
508 pr_err("raid level %s is incompatible with spare-devices setting.\n",
512 ident
.level
= s
.level
;
515 case O(GROW
, 'p'): /* new layout */
516 case O(GROW
, Layout
):
518 pr_err("layout may only be sent once. "
519 "Second value was %s\n", optarg
);
522 s
.layout_str
= optarg
;
523 /* 'Grow' will parse the value */
526 case O(CREATE
,'p'): /* raid5 layout */
527 case O(CREATE
,Layout
):
528 case O(BUILD
,'p'): /* faulty layout */
529 case O(BUILD
,Layout
):
530 if (s
.layout
!= UnSet
) {
531 pr_err("layout may only be sent once. "
532 "Second value was %s\n", optarg
);
537 pr_err("layout not meaningful for %s arrays.\n",
538 map_num(pers
, s
.level
));
541 pr_err("raid level must be given before layout.\n");
545 s
.layout
= map_name(r5layout
, optarg
);
546 if (s
.layout
==UnSet
) {
547 pr_err("layout %s not understood for raid5.\n",
553 s
.layout
= map_name(r6layout
, optarg
);
554 if (s
.layout
==UnSet
) {
555 pr_err("layout %s not understood for raid6.\n",
562 s
.layout
= parse_layout_10(optarg
);
564 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
572 s
.layout
= parse_layout_faulty(optarg
);
573 if (s
.layout
== -1) {
574 pr_err("layout %s not understood for faulty.\n",
582 case O(CREATE
,AssumeClean
):
583 case O(BUILD
,AssumeClean
): /* assume clean */
584 case O(GROW
,AssumeClean
):
590 case O(BUILD
,'n'): /* number of raid disks */
592 pr_err("raid-devices set twice: %d and %s\n",
593 s
.raiddisks
, optarg
);
596 s
.raiddisks
= parse_num(optarg
);
597 if (s
.raiddisks
<= 0) {
598 pr_err("invalid number of raid devices: %s\n",
602 ident
.raid_disks
= s
.raiddisks
;
605 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
607 pr_err("spare-devices set twice: %d and %s\n",
608 s
.sparedisks
, optarg
);
611 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
612 pr_err("spare-devices setting is incompatible with raid level %d\n",
616 s
.sparedisks
= parse_num(optarg
);
617 if (s
.sparedisks
< 0) {
618 pr_err("invalid number of spare-devices: %s\n",
628 case O(INCREMENTAL
,'a'):
629 case O(INCREMENTAL
,Auto
):
630 case O(ASSEMBLE
,'a'):
631 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
632 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
635 case O(CREATE
,Symlinks
):
636 case O(BUILD
,Symlinks
):
637 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
641 case O(BUILD
,'f'): /* force honouring '-n 1' */
642 case O(BUILD
,Force
): /* force honouring '-n 1' */
643 case O(GROW
,'f'): /* ditto */
644 case O(GROW
,Force
): /* ditto */
645 case O(CREATE
,'f'): /* force honouring of device list */
646 case O(CREATE
,Force
): /* force honouring of device list */
647 case O(ASSEMBLE
,'f'): /* force assembly */
648 case O(ASSEMBLE
,Force
): /* force assembly */
649 case O(MISC
,'f'): /* force zero */
650 case O(MISC
,Force
): /* force zero */
651 case O(MANAGE
,Force
): /* add device which is too large */
654 /* now for the Assemble options */
655 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
657 case O(INCREMENTAL
, FreezeReshape
):
658 c
.freeze_reshape
= 1;
660 case O(CREATE
,'u'): /* uuid of array */
661 case O(ASSEMBLE
,'u'): /* uuid of array */
662 if (ident
.uuid_set
) {
663 pr_err("uuid cannot be set twice. "
664 "Second value %s.\n", optarg
);
667 if (parse_uuid(optarg
, ident
.uuid
))
670 pr_err("Bad uuid: %s\n", optarg
);
676 case O(ASSEMBLE
,'N'):
679 pr_err("name cannot be set twice. "
680 "Second value %s.\n", optarg
);
683 if (mode
== MISC
&& !c
.subarray
) {
684 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
687 if (strlen(optarg
) > 32) {
688 pr_err("name '%s' is too long, 32 chars max.\n",
692 strcpy(ident
.name
, optarg
);
695 case O(ASSEMBLE
,'m'): /* super-minor for array */
696 case O(ASSEMBLE
,SuperMinor
):
697 if (ident
.super_minor
!= UnSet
) {
698 pr_err("super-minor cannot be set twice. "
699 "Second value: %s.\n", optarg
);
702 if (strcmp(optarg
, "dev")==0)
703 ident
.super_minor
= -2;
705 ident
.super_minor
= parse_num(optarg
);
706 if (ident
.super_minor
< 0) {
707 pr_err("Bad super-minor number: %s.\n", optarg
);
713 case O(ASSEMBLE
,'o'):
719 case O(ASSEMBLE
,'U'): /* update the superblock */
722 pr_err("Can only update one aspect"
723 " of superblock, both %s and %s given.\n",
727 if (mode
== MISC
&& !c
.subarray
) {
728 pr_err("Only subarrays can be"
729 " updated in misc mode\n");
733 if (strcmp(c
.update
, "sparc2.2")==0)
735 if (strcmp(c
.update
, "super-minor") == 0)
737 if (strcmp(c
.update
, "summaries")==0)
739 if (strcmp(c
.update
, "resync")==0)
741 if (strcmp(c
.update
, "uuid")==0)
743 if (strcmp(c
.update
, "name")==0)
745 if (strcmp(c
.update
, "homehost")==0)
747 if (strcmp(c
.update
, "devicesize")==0)
749 if (strcmp(c
.update
, "no-bitmap")==0)
751 if (strcmp(c
.update
, "bbl") == 0)
753 if (strcmp(c
.update
, "no-bbl") == 0)
755 if (strcmp(c
.update
, "metadata") == 0)
757 if (strcmp(c
.update
, "revert-reshape") == 0)
759 if (strcmp(c
.update
, "byteorder")==0) {
761 pr_err("must not set metadata"
762 " type with --update=byteorder.\n");
765 for(i
=0; !ss
&& superlist
[i
]; i
++)
766 ss
= superlist
[i
]->match_metadata_desc(
769 pr_err("INTERNAL ERROR"
770 " cannot find 0.swap\n");
776 if (strcmp(c
.update
,"?") == 0 ||
777 strcmp(c
.update
, "help") == 0) {
779 fprintf(outf
, Name
": ");
783 Name
": '--update=%s' is invalid. ",
786 fprintf(outf
, "Valid --update options are:\n"
787 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
788 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
789 " 'no-bitmap', 'metadata', 'revert-reshape'\n");
790 exit(outf
== stdout
? 0 : 2);
793 /* update=devicesize is allowed with --re-add */
794 if (devmode
!= 'A') {
795 pr_err("--update in Manage mode only"
796 " allowed with --re-add.\n");
800 pr_err("Can only update one aspect"
801 " of superblock, both %s and %s given.\n",
806 if (strcmp(c
.update
, "devicesize") != 0 &&
807 strcmp(c
.update
, "bbl") != 0 &&
808 strcmp(c
.update
, "no-bbl") != 0) {
809 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be"
810 " updated with --re-add\n");
815 case O(INCREMENTAL
,NoDegraded
):
816 pr_err("--no-degraded is deprecated in Incremental mode\n");
817 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
818 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
819 * so we overload slightly */
822 case O(ASSEMBLE
,'c'):
823 case O(ASSEMBLE
,ConfigFile
):
824 case O(INCREMENTAL
, 'c'):
825 case O(INCREMENTAL
, ConfigFile
):
827 case O(MISC
, ConfigFile
):
829 case O(MONITOR
,ConfigFile
):
831 pr_err("configfile cannot be set twice. "
832 "Second value is %s.\n", optarg
);
836 set_conffile(configfile
);
837 /* FIXME possibly check that config file exists. Even parse it */
839 case O(ASSEMBLE
,'s'): /* scan */
842 case O(INCREMENTAL
,'s'):
846 case O(MONITOR
,'m'): /* mail address */
847 case O(MONITOR
,EMail
):
849 pr_err("only specify one mailaddress. %s ignored.\n",
855 case O(MONITOR
,'p'): /* alert program */
856 case O(MONITOR
,ProgramOpt
): /* alert program */
858 pr_err("only specify one alter program. %s ignored.\n",
864 case O(MONITOR
,'r'): /* rebuild increments */
865 case O(MONITOR
,Increment
):
866 increments
= atoi(optarg
);
867 if (increments
> 99 || increments
< 1) {
868 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
873 case O(MONITOR
,'d'): /* delay in seconds */
875 case O(BUILD
,'d'): /* delay for bitmap updates */
878 pr_err("only specify delay once. %s ignored.\n",
881 c
.delay
= parse_num(optarg
);
883 pr_err("invalid delay: %s\n",
889 case O(MONITOR
,'f'): /* daemonise */
890 case O(MONITOR
,Fork
):
893 case O(MONITOR
,'i'): /* pid */
895 pr_err("only specify one pid file. %s ignored.\n",
900 case O(MONITOR
,'1'): /* oneshot */
904 case O(MONITOR
,'t'): /* test */
907 case O(MONITOR
,'y'): /* log messages to syslog */
908 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
911 case O(MONITOR
, NoSharing
):
915 /* now the general management options. Some are applicable
916 * to other modes. None have arguments.
921 case O(MANAGE
,Add
): /* add a drive */
924 case O(MANAGE
,ReAdd
):
927 case O(MANAGE
,'r'): /* remove a drive */
928 case O(MANAGE
,Remove
):
931 case O(MANAGE
,'f'): /* set faulty */
933 case O(INCREMENTAL
,'f'):
934 case O(INCREMENTAL
,Remove
):
935 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
936 * even though we will both fail and
937 * remove the device */
940 case O(MANAGE
,Replace
):
941 /* Mark these devices for replacement */
945 /* These are the replacements to use */
946 if (devmode
!= 'R') {
947 pr_err("--with must follow --replace\n");
952 case O(INCREMENTAL
,'R'):
954 case O(ASSEMBLE
,'R'):
956 case O(CREATE
,'R'): /* Run the array */
958 pr_err("Cannot both Stop and Run an array\n");
965 pr_err("Cannot both Run and Stop an array\n");
977 case O(MISC
,KillOpt
):
981 case O(MISC
, ExamineBB
):
985 case O(MISC
, WaitOpt
):
986 case O(MISC
, Waitclean
):
987 case O(MISC
, DetailPlatform
):
988 case O(MISC
, KillSubarray
):
989 case O(MISC
, UpdateSubarray
):
991 case O(MISC
, Restore
):
992 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
994 pr_err("subarray can only"
995 " be specified once\n");
1000 if (devmode
&& devmode
!= opt
&&
1001 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
1002 pr_err("--examine/-E cannot be given with ");
1003 if (devmode
== 'E') {
1004 if (option_index
>= 0)
1005 fprintf(stderr
, "--%s\n",
1006 long_options
[option_index
].name
);
1008 fprintf(stderr
, "-%c\n", opt
);
1009 } else if (isalpha(devmode
))
1010 fprintf(stderr
, "-%c\n", devmode
);
1012 fprintf(stderr
, "previous option\n");
1016 if (opt
== Dump
|| opt
== Restore
) {
1017 if (dump_directory
!= NULL
) {
1018 pr_err("dump/restore directory specified twice: %s and %s\n",
1019 dump_directory
, optarg
);
1022 dump_directory
= optarg
;
1025 case O(MISC
, UdevRules
):
1026 if (devmode
&& devmode
!= opt
) {
1027 pr_err("--udev-rules must"
1028 " be the only option.\n");
1031 pr_err("only specify one udev "
1032 "rule filename. %s ignored.\n",
1035 udev_filename
= optarg
;
1043 case O(MISC
, Sparc22
):
1044 if (devmode
!= 'E') {
1045 pr_err("--sparc2.2 only allowed with --examine\n");
1051 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1052 case O(ASSEMBLE
,Bitmap
):
1054 pr_err("bitmap file needed with -b in --assemble mode\n");
1057 if (strcmp(optarg
, "internal")==0) {
1058 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1061 bitmap_fd
= open(optarg
, O_RDWR
);
1062 if (!*optarg
|| bitmap_fd
< 0) {
1063 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1066 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1069 case O(ASSEMBLE
, BackupFile
):
1070 case O(GROW
, BackupFile
):
1071 /* Specify a file into which grow might place a backup,
1072 * or from which assemble might recover a backup
1074 if (c
.backup_file
) {
1075 pr_err("backup file already specified, rejecting %s\n", optarg
);
1078 c
.backup_file
= optarg
;
1081 case O(GROW
, Continue
):
1082 /* Continue interrupted grow
1086 case O(ASSEMBLE
, InvalidBackup
):
1087 /* Acknowledge that the backupfile is invalid, but ask
1088 * to continue anyway
1090 c
.invalid_backup
= 1;
1094 case O(BUILD
,Bitmap
):
1096 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1098 case O(GROW
,Bitmap
):
1099 if (strcmp(optarg
, "internal")== 0 ||
1100 strcmp(optarg
, "none")== 0 ||
1101 strchr(optarg
, '/') != NULL
) {
1102 s
.bitmap_file
= optarg
;
1106 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1107 " not '%s'\n", optarg
);
1110 case O(GROW
,BitmapChunk
):
1111 case O(BUILD
,BitmapChunk
):
1112 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1113 s
.bitmap_chunk
= parse_size(optarg
);
1114 if (s
.bitmap_chunk
== 0 ||
1115 s
.bitmap_chunk
== INVALID_SECTORS
||
1116 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1117 pr_err("invalid bitmap chunksize: %s\n",
1121 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1124 case O(GROW
, WriteBehind
):
1125 case O(BUILD
, WriteBehind
):
1126 case O(CREATE
, WriteBehind
): /* write-behind mode */
1127 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1129 s
.write_behind
= parse_num(optarg
);
1130 if (s
.write_behind
< 0 ||
1131 s
.write_behind
> 16383) {
1132 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1138 case O(INCREMENTAL
, 'r'):
1139 case O(INCREMENTAL
, RebuildMapOpt
):
1142 case O(INCREMENTAL
, IncrementalPath
):
1143 remove_path
= optarg
;
1146 /* We have now processed all the valid options. Anything else is
1149 if (option_index
> 0)
1150 pr_err(":option --%s not valid in %s mode\n",
1151 long_options
[option_index
].name
,
1152 map_num(modes
, mode
));
1154 pr_err("option -%c not valid in %s mode\n",
1155 opt
, map_num(modes
, mode
));
1162 if (print_help
== 2)
1163 help_text
= OptionHelp
;
1165 help_text
= mode_help
[mode
];
1166 if (help_text
== NULL
)
1168 fputs(help_text
,stdout
);
1172 if (!mode
&& devs_found
) {
1175 if (devlist
->disposition
== 0)
1176 devlist
->disposition
= devmode
;
1179 fputs(Usage
, stderr
);
1184 struct createinfo
*ci
= conf_get_create_info();
1186 if (strcasecmp(symlinks
, "yes") == 0)
1188 else if (strcasecmp(symlinks
, "no") == 0)
1191 pr_err("option --symlinks must be 'no' or 'yes'\n");
1195 /* Ok, got the option parsing out of the way
1196 * hopefully it's mostly right but there might be some stuff
1199 * That is mosty checked in the per-mode stuff but...
1201 * For @,B,C and A without -s, the first device listed must be
1202 * an md device. We check that here and open it.
1205 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1207 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1208 if (devs_found
< 1) {
1209 pr_err("an md device must be given in this mode\n");
1212 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1213 pr_err("--super-minor=dev is incompatible with --auto\n");
1216 if (mode
== MANAGE
|| mode
== GROW
) {
1217 mdfd
= open_mddev(devlist
->devname
, 1);
1221 /* non-existent device is OK */
1222 mdfd
= open_mddev(devlist
->devname
, 0);
1224 pr_err("device %s exists but is not an "
1225 "md array.\n", devlist
->devname
);
1228 if ((int)ident
.super_minor
== -2) {
1231 pr_err("--super-minor=dev given, and "
1232 "listed device %s doesn't exist.\n",
1237 ident
.super_minor
= minor(stb
.st_rdev
);
1239 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1240 /* We don't really want this open yet, we just might
1241 * have wanted to check some things
1249 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1250 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1251 " a mistake. If you really mean it you will need to specify --force before\n"
1252 " setting the number of drives.\n");
1257 if (c
.homehost
== NULL
)
1258 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1259 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1260 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1261 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1262 c
.homehost
= sys_hostname
;
1265 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1267 c
.require_homehost
= 0;
1270 if (c
.backup_file
&& data_offset
!= INVALID_SECTORS
) {
1271 pr_err("--backup-file and --data-offset are incompatible\n");
1275 if ((mode
== MISC
&& devmode
== 'E')
1276 || (mode
== MONITOR
&& spare_sharing
== 0))
1277 /* Anyone may try this */;
1278 else if (geteuid() != 0) {
1279 pr_err("must be super-user to perform this action\n");
1283 ident
.autof
= c
.autof
;
1285 if (c
.scan
&& c
.verbose
< 2)
1286 /* --scan implied --brief unless -vv */
1292 /* readonly, add/remove, readwrite, runstop */
1294 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1295 if (!rv
&& devs_found
>1)
1296 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1297 devlist
->next
, c
.verbose
, c
.test
,
1299 if (!rv
&& c
.readonly
< 0)
1300 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1301 if (!rv
&& c
.runstop
> 0)
1302 rv
= Manage_run(devlist
->devname
, mdfd
, c
.verbose
);
1303 if (!rv
&& c
.runstop
< 0)
1304 rv
= Manage_stop(devlist
->devname
, mdfd
, c
.verbose
, 0);
1307 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1308 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1309 /* Only a device has been given, so get details from config file */
1310 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1311 if (array_ident
== NULL
) {
1312 pr_err("%s not identified in config file.\n",
1318 if (array_ident
->autof
== 0)
1319 array_ident
->autof
= c
.autof
;
1320 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1324 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1326 else if (devs_found
> 0) {
1327 if (c
.update
&& devs_found
> 1) {
1328 pr_err("can only update a single array at a time\n");
1331 if (c
.backup_file
&& devs_found
> 1) {
1332 pr_err("can only assemble a single array when providing a backup file.\n");
1335 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1336 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1337 if (array_ident
== NULL
) {
1338 pr_err("%s not identified in config file.\n",
1343 if (array_ident
->autof
== 0)
1344 array_ident
->autof
= c
.autof
;
1345 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1350 pr_err("--update not meaningful with a --scan assembly.\n");
1353 if (c
.backup_file
) {
1354 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1357 rv
= scan_assemble(ss
, &c
, &ident
);
1363 c
.delay
= DEFAULT_BITMAP_DELAY
;
1364 if (s
.write_behind
&& !s
.bitmap_file
) {
1365 pr_err("write-behind mode requires a bitmap.\n");
1369 if (s
.raiddisks
== 0) {
1370 pr_err("no raid-devices specified.\n");
1375 if (s
.bitmap_file
) {
1376 if (strcmp(s
.bitmap_file
, "internal")==0) {
1377 pr_err("'internal' bitmaps not supported with --build\n");
1382 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1386 c
.delay
= DEFAULT_BITMAP_DELAY
;
1387 if (s
.write_behind
&& !s
.bitmap_file
) {
1388 pr_err("write-behind mode requires a bitmap.\n");
1392 if (s
.raiddisks
== 0) {
1393 pr_err("no raid-devices specified.\n");
1398 rv
= Create(ss
, devlist
->devname
,
1399 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1400 devs_found
-1, devlist
->next
,
1401 &s
, &c
, data_offset
);
1404 if (devmode
== 'E') {
1405 if (devlist
== NULL
&& !c
.scan
) {
1406 pr_err("No devices to examine\n");
1409 if (devlist
== NULL
)
1410 devlist
= conf_get_devs();
1411 if (devlist
== NULL
) {
1412 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1415 rv
= Examine(devlist
, &c
, ss
);
1416 } else if (devmode
== DetailPlatform
) {
1417 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1418 c
.verbose
, c
.export
,
1419 devlist
? devlist
->devname
: NULL
);
1420 } else if (devlist
== NULL
) {
1421 if (devmode
== 'S' && c
.scan
)
1422 rv
= stop_scan(c
.verbose
);
1423 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1424 rv
= misc_scan(devmode
, &c
);
1425 else if (devmode
== UdevRules
)
1426 rv
= Write_rules(udev_filename
);
1428 pr_err("No devices given.\n");
1432 rv
= misc_list(devlist
, &ident
, dump_directory
, ss
, &c
);
1435 if (!devlist
&& !c
.scan
) {
1436 pr_err("Cannot monitor: need --scan or at least one device\n");
1440 if (pidfile
&& !daemonise
) {
1441 pr_err("Cannot write a pid file when not in daemon mode\n");
1446 if (get_linux_version() > 2006016)
1447 /* mdstat responds to poll */
1454 rv
= Monitor(devlist
, mailaddr
, program
,
1455 &c
, daemonise
, oneshot
,
1456 dosyslog
, pidfile
, increments
,
1461 if (array_size
> 0) {
1462 /* alway impose array size first, independent of
1464 * Do not allow level or raid_disks changes at the
1465 * same time as that can be irreversibly destructive.
1469 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1470 pr_err("cannot change array size in same operation "
1471 "as changing raiddisks or level.\n"
1472 " Change size first, then check that data is still intact.\n");
1476 sysfs_init(&sra
, mdfd
, NULL
);
1477 if (array_size
== MAX_SIZE
)
1478 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1480 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1483 pr_err("--array-size setting"
1484 " is too large.\n");
1486 pr_err("current kernel does"
1487 " not support setting --array-size\n");
1492 if (devs_found
> 1 && s
.raiddisks
== 0) {
1494 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1495 pr_err("--add cannot be used with "
1496 "other geometry changes in --grow mode\n");
1500 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1501 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1506 } else if (s
.bitmap_file
) {
1507 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1508 s
.layout_str
!= NULL
|| devs_found
> 1) {
1509 pr_err("--bitmap changes cannot be "
1510 "used with other geometry changes "
1511 "in --grow mode\n");
1516 c
.delay
= DEFAULT_BITMAP_DELAY
;
1517 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1518 } else if (grow_continue
)
1519 rv
= Grow_continue_command(devlist
->devname
,
1520 mdfd
, c
.backup_file
,
1522 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1523 || s
.chunk
!= 0 || s
.level
!= UnSet
1524 || data_offset
!= INVALID_SECTORS
) {
1525 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1527 data_offset
, &c
, &s
);
1528 } else if (array_size
== 0)
1529 pr_err("no changes to --grow\n");
1536 if (c
.runstop
<= 0) {
1537 pr_err("--incremental --scan meaningless without --run.\n");
1540 if (devmode
== 'f') {
1541 pr_err("--incremental --scan --fail not supported.\n");
1544 rv
= IncrementalScan(c
.verbose
);
1547 if (!rebuild_map
&& !c
.scan
) {
1548 pr_err("--incremental requires a device.\n");
1553 if (devlist
->next
) {
1554 pr_err("--incremental can only handle one device.\n");
1559 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1562 rv
= Incremental(devlist
->devname
, &c
, ss
);
1571 static int scan_assemble(struct supertype
*ss
,
1573 struct mddev_ident
*ident
)
1575 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1576 struct mddev_dev
*devlist
= conf_get_devs();
1577 struct map_ent
*map
= NULL
;
1580 int failures
, successes
;
1582 if (conf_verify_devnames(array_list
)) {
1583 pr_err("Duplicate MD device names in "
1584 "conf file were found.\n");
1587 if (devlist
== NULL
) {
1588 pr_err("No devices listed in conf file were found.\n");
1591 for (a
= array_list
; a
; a
= a
->next
) {
1594 a
->autof
= c
->autof
;
1597 pr_err("%s: failed to get "
1598 "exclusive lock on mapfile\n",
1604 for (a
= array_list
; a
; a
= a
->next
) {
1609 strcasecmp(a
->devname
, "<ignore>") == 0)
1612 r
= Assemble(ss
, a
->devname
,
1622 } while (failures
&& successes
);
1623 if (c
->homehost
&& cnt
== 0) {
1624 /* Maybe we can auto-assemble something.
1625 * Repeatedly call Assemble in auto-assemble mode
1630 ident
->autof
= c
->autof
;
1632 struct mddev_dev
*devlist
= conf_get_devs();
1635 rv2
= Assemble(ss
, NULL
,
1643 /* Incase there are stacked devices, we need to go around again */
1645 if (cnt
== 0 && rv
== 0) {
1646 pr_err("No arrays found in config file or automatically\n");
1650 } else if (cnt
== 0 && rv
== 0) {
1651 pr_err("No arrays found in config file\n");
1658 static int misc_scan(char devmode
, struct context
*c
)
1660 /* apply --detail or --wait-clean to
1661 * all devices in /proc/mdstat
1663 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1664 struct mdstat_ent
*e
;
1665 struct map_ent
*map
= NULL
;
1669 for (members
= 0; members
<= 1; members
++) {
1670 for (e
=ms
; e
; e
=e
->next
) {
1674 int member
= e
->metadata_version
&&
1675 strncmp(e
->metadata_version
,
1676 "external:/", 10) == 0;
1677 if (members
!= member
)
1679 me
= map_by_devnm(&map
, e
->devnm
);
1681 && strcmp(me
->path
, "/unknown") != 0)
1684 stat(name
, &stb
) != 0)
1685 name
= get_md_name(e
->devnm
);
1688 pr_err("cannot find device file for %s\n",
1693 rv
|= Detail(name
, c
);
1695 rv
|= WaitClean(name
, -1, c
->verbose
);
1703 static int stop_scan(int verbose
)
1705 /* apply --stop to all devices in /proc/mdstat */
1706 /* Due to possible stacking of devices, repeat until
1707 * nothing more can be stopped
1709 int progress
=1, err
;
1713 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1714 struct mdstat_ent
*e
;
1716 if (!progress
) last
= 1;
1717 progress
= 0; err
= 0;
1718 for (e
=ms
; e
; e
=e
->next
) {
1719 char *name
= get_md_name(e
->devnm
);
1723 pr_err("cannot find device file for %s\n",
1727 mdfd
= open_mddev(name
, 1);
1729 if (Manage_stop(name
, mdfd
, verbose
, !last
))
1739 } while (!last
&& err
);
1745 static int misc_list(struct mddev_dev
*devlist
,
1746 struct mddev_ident
*ident
,
1747 char *dump_directory
,
1748 struct supertype
*ss
, struct context
*c
)
1750 struct mddev_dev
*dv
;
1753 for (dv
=devlist
; dv
; dv
=(rv
& 16) ? NULL
: dv
->next
) {
1756 switch(dv
->disposition
) {
1758 rv
|= Detail(dv
->devname
, c
);
1760 case KillOpt
: /* Zero superblock */
1762 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1766 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1773 rv
|= Query(dv
->devname
); continue;
1775 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1777 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1780 rv
|= Wait(dv
->devname
); continue;
1782 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1784 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1786 case UpdateSubarray
:
1787 if (c
->update
== NULL
) {
1788 pr_err("-U/--update must be specified with --update-subarray\n");
1792 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1793 c
->update
, ident
, c
->verbose
);
1796 rv
|= Dump_metadata(dv
->devname
, dump_directory
, c
, ss
);
1799 rv
|= Restore_metadata(dv
->devname
, dump_directory
, c
, ss
,
1800 (dv
== devlist
&& dv
->next
== NULL
));
1803 if (dv
->devname
[0] == '/')
1804 mdfd
= open_mddev(dv
->devname
, 1);
1806 mdfd
= open_dev(dv
->devname
);
1808 pr_err("Cannot open %s\n", dv
->devname
);
1811 switch(dv
->disposition
) {
1813 rv
|= Manage_run(dv
->devname
, mdfd
, c
->verbose
); break;
1815 rv
|= Manage_stop(dv
->devname
, mdfd
, c
->verbose
, 0); break;
1817 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1819 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;