2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
24 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
25 * Paul Clements, SteelEye Technology, Inc.
32 static int scan_assemble(struct supertype
*ss
,
34 struct mddev_ident
*ident
);
35 static int misc_scan(char devmode
, struct context
*c
);
36 static int stop_scan(int verbose
);
37 static int misc_list(struct mddev_dev
*devlist
,
38 struct mddev_ident
*ident
,
40 struct supertype
*ss
, struct context
*c
);
41 const char Name
[] = "mdadm";
43 int main(int argc
, char *argv
[])
51 unsigned long long array_size
= 0;
52 unsigned long long data_offset
= INVALID_SECTORS
;
53 struct mddev_ident ident
;
54 char *configfile
= NULL
;
57 struct mddev_dev
*devlist
= NULL
;
58 struct mddev_dev
**devlistend
= & devlist
;
61 char *symlinks
= NULL
;
62 int grow_continue
= 0;
63 /* autof indicates whether and how to create device node.
64 * bottom 3 bits are style. Rest (when shifted) are number of parts
66 * 1 - don't create (no)
67 * 2 - if is_standard, then create (yes)
68 * 3 - create as 'md' - reject is_standard mdp (md)
69 * 4 - create as 'mdp' - reject is_standard md (mdp)
70 * 5 - default to md if not is_standard (md in config file)
71 * 6 - default to mdp if not is_standard (part, or mdp in config file)
74 .require_homehost
= 1,
80 .bitmap_chunk
= UnSet
,
83 char sys_hostname
[256];
84 char *mailaddr
= NULL
;
90 int spare_sharing
= 1;
91 struct supertype
*ss
= NULL
;
93 char *shortopt
= short_options
;
96 char *remove_path
= NULL
;
97 char *udev_filename
= NULL
;
98 char *dump_directory
= NULL
;
105 srandom(time(0) ^ getpid());
109 ident
.raid_disks
= UnSet
;
110 ident
.super_minor
= UnSet
;
112 ident
.spare_group
= NULL
;
115 ident
.bitmap_fd
= -1;
116 ident
.bitmap_file
= NULL
;
118 ident
.container
= NULL
;
121 while ((option_index
= -1) ,
122 (opt
=getopt_long(argc
, argv
,
123 shortopt
, long_options
,
124 &option_index
)) != -1) {
126 /* firstly, some mode-independent options */
136 fputs(Version
, stderr
);
139 case 'v': c
.verbose
++;
142 case 'q': c
.verbose
--;
146 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
147 || mode
== GROW
|| mode
== INCREMENTAL
149 break; /* b means bitmap */
154 case 'Y': c
.export
++;
158 if (strcasecmp(optarg
, "<ignore>") == 0)
159 c
.require_homehost
= 0;
165 /* Silently ignore old option */
171 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
177 fputs(Usage
, stderr
);
180 /* second, figure out the mode.
181 * Some options force the mode. Others
182 * set the mode if it isn't already
188 shortopt
= short_bitmap_options
;
200 case ReAdd
: /* re-add */
204 shortopt
= short_bitmap_options
;
208 case 'A': newmode
= ASSEMBLE
;
209 shortopt
= short_bitmap_auto_options
;
211 case 'B': newmode
= BUILD
;
212 shortopt
= short_bitmap_auto_options
;
214 case 'C': newmode
= CREATE
;
215 shortopt
= short_bitmap_auto_options
;
217 case 'F': newmode
= MONITOR
;
219 case 'G': newmode
= GROW
;
220 shortopt
= short_bitmap_options
;
222 case 'I': newmode
= INCREMENTAL
;
223 shortopt
= short_bitmap_auto_options
;
226 newmode
= AUTODETECT
;
261 if (mode
&& newmode
== mode
) {
262 /* everybody happy ! */
263 } else if (mode
&& newmode
!= mode
) {
266 if (option_index
>= 0)
267 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
269 fprintf(stderr
, "-%c", opt
);
270 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
271 map_num(modes
, newmode
),
272 map_num(modes
, mode
));
274 } else if (!mode
&& newmode
) {
276 if (mode
== MISC
&& devs_found
) {
277 pr_err("No action given for %s in --misc mode\n",
279 cont_err("Action options must come before device names\n");
283 /* special case of -c --help */
284 if ((opt
== 'c' || opt
== ConfigFile
) &&
285 ( strncmp(optarg
, "--h", 3)==0 ||
286 strncmp(optarg
, "-h", 2)==0)) {
287 fputs(Help_config
, stdout
);
291 /* If first option is a device, don't force the mode yet */
293 if (devs_found
== 0) {
294 dv
= xmalloc(sizeof(*dv
));
295 dv
->devname
= optarg
;
296 dv
->disposition
= devmode
;
297 dv
->writemostly
= writemostly
;
301 devlistend
= &dv
->next
;
306 /* No mode yet, and this is the second device ... */
307 pr_err("An option must be given to set the mode before a second device\n"
308 " (%s) is listed\n", optarg
);
311 if (option_index
>= 0)
312 pr_err("--%s", long_options
[option_index
].name
);
315 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
319 /* if we just set the mode, then done */
333 /* an undecorated option - must be a device name.
336 if (devs_found
> 0 && devmode
== DetailPlatform
) {
337 pr_err("controller may only be specified once. %s ignored\n",
342 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
343 pr_err("Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
346 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
347 pr_err("Must give -a/--add for 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. Second value is %s.\n", optarg
);
377 s
.chunk
= parse_size(optarg
);
378 if (s
.chunk
== INVALID_SECTORS
||
379 s
.chunk
< 8 || (s
.chunk
&1)) {
380 pr_err("invalid chunk/rounding value: %s\n",
384 /* Convert sectors to K */
388 case O(INCREMENTAL
, 'e'):
390 case O(ASSEMBLE
,'e'):
391 case O(MISC
,'e'): /* set metadata (superblock) information */
393 pr_err("metadata information already given\n");
396 for(i
=0; !ss
&& superlist
[i
]; i
++)
397 ss
= superlist
[i
]->match_metadata_desc(optarg
);
400 pr_err("unrecognised metadata identifier: %s\n", optarg
);
406 case O(MANAGE
,WriteMostly
):
408 case O(BUILD
,WriteMostly
):
410 case O(CREATE
,WriteMostly
):
411 /* set write-mostly for following devices */
416 /* clear write-mostly for following devices */
422 case O(BUILD
,'z'): /* size */
424 pr_err("size may only be specified once. Second value is %s.\n", optarg
);
427 if (strcmp(optarg
, "max")==0)
430 s
.size
= parse_size(optarg
);
431 if (s
.size
== INVALID_SECTORS
||
433 pr_err("invalid size: %s\n",
437 /* convert sectors to K */
442 case O(GROW
,'Z'): /* array size */
443 if (array_size
> 0) {
444 pr_err("array-size may only be specified once. 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. Second value is %s.\n", optarg
);
466 if (mode
== CREATE
&&
467 strcmp(optarg
, "variable") == 0)
468 data_offset
= VARIABLE_OFFSET
;
470 data_offset
= parse_size(optarg
);
471 if (data_offset
== INVALID_SECTORS
) {
472 pr_err("invalid data-offset: %s\n",
480 case O(BUILD
,'l'): /* set raid level*/
481 if (s
.level
!= UnSet
) {
482 pr_err("raid level may only be set once. Second value is %s.\n", optarg
);
485 s
.level
= map_name(pers
, optarg
);
486 if (s
.level
== UnSet
) {
487 pr_err("invalid raid level: %s\n",
491 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
492 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
495 pr_err("Raid level %s not permitted with --build.\n",
499 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
500 pr_err("raid level %s is incompatible with spare-devices setting.\n",
504 ident
.level
= s
.level
;
507 case O(GROW
, 'p'): /* new layout */
508 case O(GROW
, Layout
):
510 pr_err("layout may only be sent once. Second value was %s\n", optarg
);
513 s
.layout_str
= optarg
;
514 /* 'Grow' will parse the value */
517 case O(CREATE
,'p'): /* raid5 layout */
518 case O(CREATE
,Layout
):
519 case O(BUILD
,'p'): /* faulty layout */
520 case O(BUILD
,Layout
):
521 if (s
.layout
!= UnSet
) {
522 pr_err("layout may only be sent once. Second value was %s\n", optarg
);
527 pr_err("layout not meaningful for %s arrays.\n",
528 map_num(pers
, s
.level
));
531 pr_err("raid level must be given before layout.\n");
535 s
.layout
= map_name(r5layout
, optarg
);
536 if (s
.layout
==UnSet
) {
537 pr_err("layout %s not understood for raid5.\n",
543 s
.layout
= map_name(r6layout
, optarg
);
544 if (s
.layout
==UnSet
) {
545 pr_err("layout %s not understood for raid6.\n",
552 s
.layout
= parse_layout_10(optarg
);
554 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
562 s
.layout
= parse_layout_faulty(optarg
);
563 if (s
.layout
== -1) {
564 pr_err("layout %s not understood for faulty.\n",
572 case O(CREATE
,AssumeClean
):
573 case O(BUILD
,AssumeClean
): /* assume clean */
574 case O(GROW
,AssumeClean
):
580 case O(BUILD
,'n'): /* number of raid disks */
582 pr_err("raid-devices set twice: %d and %s\n",
583 s
.raiddisks
, optarg
);
586 s
.raiddisks
= parse_num(optarg
);
587 if (s
.raiddisks
<= 0) {
588 pr_err("invalid number of raid devices: %s\n",
592 ident
.raid_disks
= s
.raiddisks
;
594 case O(ASSEMBLE
, Nodes
):
595 case O(CREATE
, Nodes
):
596 c
.nodes
= parse_num(optarg
);
598 pr_err("invalid number for the number of cluster nodes: %s\n",
603 case O(CREATE
, ClusterName
):
604 case O(ASSEMBLE
, ClusterName
):
605 c
.homecluster
= optarg
;
606 if (strlen(c
.homecluster
) > 64) {
607 pr_err("Cluster name too big.\n");
611 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
613 pr_err("spare-devices set twice: %d and %s\n",
614 s
.sparedisks
, optarg
);
617 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
618 pr_err("spare-devices setting is incompatible with raid level %d\n",
622 s
.sparedisks
= parse_num(optarg
);
623 if (s
.sparedisks
< 0) {
624 pr_err("invalid number of spare-devices: %s\n",
634 case O(INCREMENTAL
,'a'):
635 case O(INCREMENTAL
,Auto
):
636 case O(ASSEMBLE
,'a'):
637 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
638 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
641 case O(CREATE
,Symlinks
):
642 case O(BUILD
,Symlinks
):
643 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
647 case O(BUILD
,'f'): /* force honouring '-n 1' */
648 case O(BUILD
,Force
): /* force honouring '-n 1' */
649 case O(GROW
,'f'): /* ditto */
650 case O(GROW
,Force
): /* ditto */
651 case O(CREATE
,'f'): /* force honouring of device list */
652 case O(CREATE
,Force
): /* force honouring of device list */
653 case O(ASSEMBLE
,'f'): /* force assembly */
654 case O(ASSEMBLE
,Force
): /* force assembly */
655 case O(MISC
,'f'): /* force zero */
656 case O(MISC
,Force
): /* force zero */
657 case O(MANAGE
,Force
): /* add device which is too large */
660 /* now for the Assemble options */
661 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
663 case O(INCREMENTAL
, FreezeReshape
):
664 c
.freeze_reshape
= 1;
666 case O(CREATE
,'u'): /* uuid of array */
667 case O(ASSEMBLE
,'u'): /* uuid of array */
668 if (ident
.uuid_set
) {
669 pr_err("uuid cannot be set twice. Second value %s.\n", optarg
);
672 if (parse_uuid(optarg
, ident
.uuid
))
675 pr_err("Bad uuid: %s\n", optarg
);
681 case O(ASSEMBLE
,'N'):
684 pr_err("name cannot be set twice. Second value %s.\n", optarg
);
687 if (mode
== MISC
&& !c
.subarray
) {
688 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
691 if (strlen(optarg
) > 32) {
692 pr_err("name '%s' is too long, 32 chars max.\n",
696 strcpy(ident
.name
, optarg
);
699 case O(ASSEMBLE
,'m'): /* super-minor for array */
700 case O(ASSEMBLE
,SuperMinor
):
701 if (ident
.super_minor
!= UnSet
) {
702 pr_err("super-minor cannot be set twice. Second value: %s.\n", optarg
);
705 if (strcmp(optarg
, "dev")==0)
706 ident
.super_minor
= -2;
708 ident
.super_minor
= parse_num(optarg
);
709 if (ident
.super_minor
< 0) {
710 pr_err("Bad super-minor number: %s.\n", optarg
);
716 case O(ASSEMBLE
,'o'):
722 case O(ASSEMBLE
,'U'): /* update the superblock */
725 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
729 if (mode
== MISC
&& !c
.subarray
) {
730 pr_err("Only subarrays can be updated in misc mode\n");
734 if (strcmp(c
.update
, "sparc2.2")==0)
736 if (strcmp(c
.update
, "super-minor") == 0)
738 if (strcmp(c
.update
, "summaries")==0)
740 if (strcmp(c
.update
, "resync")==0)
742 if (strcmp(c
.update
, "uuid")==0)
744 if (strcmp(c
.update
, "name")==0)
746 if (strcmp(c
.update
, "homehost")==0)
748 if (strcmp(c
.update
, "home-cluster")==0)
750 if (strcmp(c
.update
, "nodes")==0)
752 if (strcmp(c
.update
, "devicesize")==0)
754 if (strcmp(c
.update
, "no-bitmap")==0)
756 if (strcmp(c
.update
, "bbl") == 0)
758 if (strcmp(c
.update
, "no-bbl") == 0)
760 if (strcmp(c
.update
, "metadata") == 0)
762 if (strcmp(c
.update
, "revert-reshape") == 0)
764 if (strcmp(c
.update
, "byteorder")==0) {
766 pr_err("must not set metadata type with --update=byteorder.\n");
769 for(i
=0; !ss
&& superlist
[i
]; i
++)
770 ss
= superlist
[i
]->match_metadata_desc(
773 pr_err("INTERNAL ERROR cannot find 0.swap\n");
779 if (strcmp(c
.update
,"?") == 0 ||
780 strcmp(c
.update
, "help") == 0) {
782 fprintf(outf
, "%s: ", Name
);
786 "%s: '--update=%s' is invalid. ",
789 fprintf(outf
, "Valid --update options are:\n"
790 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'nodes', 'resync',\n"
791 " 'summaries', 'homehost', 'home-cluster', 'byteorder', 'devicesize',\n"
792 " 'no-bitmap', 'metadata', 'revert-reshape'\n"
795 exit(outf
== stdout
? 0 : 2);
798 /* update=devicesize is allowed with --re-add */
799 if (devmode
!= 'A') {
800 pr_err("--update in Manage mode only allowed with --re-add.\n");
804 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
809 if (strcmp(c
.update
, "devicesize") != 0 &&
810 strcmp(c
.update
, "bbl") != 0 &&
811 strcmp(c
.update
, "no-bbl") != 0) {
812 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be updated with --re-add\n");
817 case O(INCREMENTAL
,NoDegraded
):
818 pr_err("--no-degraded is deprecated in Incremental mode\n");
819 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
820 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
821 * so we overload slightly */
824 case O(ASSEMBLE
,'c'):
825 case O(ASSEMBLE
,ConfigFile
):
826 case O(INCREMENTAL
, 'c'):
827 case O(INCREMENTAL
, ConfigFile
):
829 case O(MISC
, ConfigFile
):
831 case O(MONITOR
,ConfigFile
):
832 case O(CREATE
,ConfigFile
):
834 pr_err("configfile cannot be set twice. Second value is %s.\n", optarg
);
838 set_conffile(configfile
);
839 /* FIXME possibly check that config file exists. Even parse it */
841 case O(ASSEMBLE
,'s'): /* scan */
844 case O(INCREMENTAL
,'s'):
848 case O(MONITOR
,'m'): /* mail address */
849 case O(MONITOR
,EMail
):
851 pr_err("only specify one mailaddress. %s ignored.\n",
857 case O(MONITOR
,'p'): /* alert program */
858 case O(MONITOR
,ProgramOpt
): /* alert program */
860 pr_err("only specify one alter program. %s ignored.\n",
866 case O(MONITOR
,'r'): /* rebuild increments */
867 case O(MONITOR
,Increment
):
868 increments
= atoi(optarg
);
869 if (increments
> 99 || increments
< 1) {
870 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
875 case O(MONITOR
,'d'): /* delay in seconds */
877 case O(BUILD
,'d'): /* delay for bitmap updates */
880 pr_err("only specify delay once. %s ignored.\n",
883 c
.delay
= parse_num(optarg
);
885 pr_err("invalid delay: %s\n",
891 case O(MONITOR
,'f'): /* daemonise */
892 case O(MONITOR
,Fork
):
895 case O(MONITOR
,'i'): /* pid */
897 pr_err("only specify one pid file. %s ignored.\n",
902 case O(MONITOR
,'1'): /* oneshot */
906 case O(MONITOR
,'t'): /* test */
909 case O(MONITOR
,'y'): /* log messages to syslog */
910 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
913 case O(MONITOR
, NoSharing
):
917 /* now the general management options. Some are applicable
918 * to other modes. None have arguments.
923 case O(MANAGE
,Add
): /* add a drive */
926 case O(MANAGE
,AddSpare
): /* add drive - never re-add */
929 case O(MANAGE
,AddJournal
): /* add journal */
930 if (s
.journaldisks
&& (s
.level
< 4 || s
.level
> 6)) {
931 pr_err("--add-journal is only supported for RAID level 4/5/6.\n");
936 case O(MANAGE
,ReAdd
):
939 case O(MANAGE
,'r'): /* remove a drive */
940 case O(MANAGE
,Remove
):
943 case O(MANAGE
,'f'): /* set faulty */
945 case O(INCREMENTAL
,'f'):
946 case O(INCREMENTAL
,Remove
):
947 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
948 * even though we will both fail and
949 * remove the device */
952 case O(MANAGE
, ClusterConfirm
):
955 case O(MANAGE
,Replace
):
956 /* Mark these devices for replacement */
960 /* These are the replacements to use */
961 if (devmode
!= 'R') {
962 pr_err("--with must follow --replace\n");
967 case O(INCREMENTAL
,'R'):
969 case O(ASSEMBLE
,'R'):
971 case O(CREATE
,'R'): /* Run the array */
973 pr_err("Cannot both Stop and Run an array\n");
980 pr_err("Cannot both Run and Stop an array\n");
992 case O(MISC
,KillOpt
):
996 case O(MISC
, ExamineBB
):
1000 case O(MISC
, WaitOpt
):
1001 case O(MISC
, Waitclean
):
1002 case O(MISC
, DetailPlatform
):
1003 case O(MISC
, KillSubarray
):
1004 case O(MISC
, UpdateSubarray
):
1006 case O(MISC
, Restore
):
1007 case O(MISC
,Action
):
1008 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
1010 pr_err("subarray can only be specified once\n");
1013 c
.subarray
= optarg
;
1015 if (opt
== Action
) {
1017 pr_err("Only one --action can be specified\n");
1020 if (strcmp(optarg
, "idle") == 0 ||
1021 strcmp(optarg
, "frozen") == 0 ||
1022 strcmp(optarg
, "check") == 0 ||
1023 strcmp(optarg
, "repair") == 0)
1026 pr_err("action must be one of idle, frozen, check, repair\n");
1030 if (devmode
&& devmode
!= opt
&&
1031 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
1032 pr_err("--examine/-E cannot be given with ");
1033 if (devmode
== 'E') {
1034 if (option_index
>= 0)
1035 fprintf(stderr
, "--%s\n",
1036 long_options
[option_index
].name
);
1038 fprintf(stderr
, "-%c\n", opt
);
1039 } else if (isalpha(devmode
))
1040 fprintf(stderr
, "-%c\n", devmode
);
1042 fprintf(stderr
, "previous option\n");
1046 if (opt
== Dump
|| opt
== Restore
) {
1047 if (dump_directory
!= NULL
) {
1048 pr_err("dump/restore directory specified twice: %s and %s\n",
1049 dump_directory
, optarg
);
1052 dump_directory
= optarg
;
1055 case O(MISC
, UdevRules
):
1056 if (devmode
&& devmode
!= opt
) {
1057 pr_err("--udev-rules must be the only option.\n");
1060 pr_err("only specify one udev rule filename. %s ignored.\n",
1063 udev_filename
= optarg
;
1071 case O(MISC
, Sparc22
):
1072 if (devmode
!= 'E') {
1073 pr_err("--sparc2.2 only allowed with --examine\n");
1079 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1080 case O(ASSEMBLE
,Bitmap
):
1082 pr_err("bitmap file needed with -b in --assemble mode\n");
1085 if (strcmp(optarg
, "internal")==0) {
1086 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1089 bitmap_fd
= open(optarg
, O_RDWR
);
1090 if (!*optarg
|| bitmap_fd
< 0) {
1091 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1094 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1097 case O(ASSEMBLE
, BackupFile
):
1098 case O(GROW
, BackupFile
):
1099 /* Specify a file into which grow might place a backup,
1100 * or from which assemble might recover a backup
1102 if (c
.backup_file
) {
1103 pr_err("backup file already specified, rejecting %s\n", optarg
);
1106 c
.backup_file
= optarg
;
1109 case O(GROW
, Continue
):
1110 /* Continue interrupted grow
1114 case O(ASSEMBLE
, InvalidBackup
):
1115 /* Acknowledge that the backupfile is invalid, but ask
1116 * to continue anyway
1118 c
.invalid_backup
= 1;
1122 case O(BUILD
,Bitmap
):
1124 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1126 case O(GROW
,Bitmap
):
1127 if (strcmp(optarg
, "internal")== 0 ||
1128 strcmp(optarg
, "none")== 0 ||
1129 strchr(optarg
, '/') != NULL
) {
1130 s
.bitmap_file
= optarg
;
1133 if (strcmp(optarg
, "clustered")== 0) {
1134 s
.bitmap_file
= optarg
;
1135 /* Set the default number of cluster nodes
1136 * to 4 if not already set by user
1143 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1144 " not '%s'\n", optarg
);
1147 case O(GROW
,BitmapChunk
):
1148 case O(BUILD
,BitmapChunk
):
1149 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1150 s
.bitmap_chunk
= parse_size(optarg
);
1151 if (s
.bitmap_chunk
== 0 ||
1152 s
.bitmap_chunk
== INVALID_SECTORS
||
1153 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1154 pr_err("invalid bitmap chunksize: %s\n",
1158 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1161 case O(GROW
, WriteBehind
):
1162 case O(BUILD
, WriteBehind
):
1163 case O(CREATE
, WriteBehind
): /* write-behind mode */
1164 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1166 s
.write_behind
= parse_num(optarg
);
1167 if (s
.write_behind
< 0 ||
1168 s
.write_behind
> 16383) {
1169 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1175 case O(INCREMENTAL
, 'r'):
1176 case O(INCREMENTAL
, RebuildMapOpt
):
1179 case O(INCREMENTAL
, IncrementalPath
):
1180 remove_path
= optarg
;
1182 case O(CREATE
, WriteJournal
):
1183 if (s
.journaldisks
) {
1184 pr_err("Please specify only one journal device for the array.\n");
1185 pr_err("Ignoring --write-journal %s...\n", optarg
);
1188 dv
= xmalloc(sizeof(*dv
));
1189 dv
->devname
= optarg
;
1190 dv
->disposition
= 'j'; /* WriteJournal */
1194 devlistend
= &dv
->next
;
1200 /* We have now processed all the valid options. Anything else is
1203 if (option_index
> 0)
1204 pr_err(":option --%s not valid in %s mode\n",
1205 long_options
[option_index
].name
,
1206 map_num(modes
, mode
));
1208 pr_err("option -%c not valid in %s mode\n",
1209 opt
, map_num(modes
, mode
));
1216 if (print_help
== 2)
1217 help_text
= OptionHelp
;
1219 help_text
= mode_help
[mode
];
1220 if (help_text
== NULL
)
1222 fputs(help_text
,stdout
);
1226 if (s
.journaldisks
&& (s
.level
< 4 || s
.level
> 6)) {
1227 pr_err("--write-journal is only supported for RAID level 4/5/6.\n");
1231 if (!mode
&& devs_found
) {
1234 if (devlist
->disposition
== 0)
1235 devlist
->disposition
= devmode
;
1238 fputs(Usage
, stderr
);
1243 struct createinfo
*ci
= conf_get_create_info();
1245 if (strcasecmp(symlinks
, "yes") == 0)
1247 else if (strcasecmp(symlinks
, "no") == 0)
1250 pr_err("option --symlinks must be 'no' or 'yes'\n");
1254 /* Ok, got the option parsing out of the way
1255 * hopefully it's mostly right but there might be some stuff
1258 * That is mosty checked in the per-mode stuff but...
1260 * For @,B,C and A without -s, the first device listed must be
1261 * an md device. We check that here and open it.
1264 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1266 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1267 if (devs_found
< 1) {
1268 pr_err("an md device must be given in this mode\n");
1271 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1272 pr_err("--super-minor=dev is incompatible with --auto\n");
1275 if (mode
== MANAGE
|| mode
== GROW
) {
1276 mdfd
= open_mddev(devlist
->devname
, 1);
1280 /* non-existent device is OK */
1281 mdfd
= open_mddev(devlist
->devname
, 0);
1283 pr_err("device %s exists but is not an md array.\n", devlist
->devname
);
1286 if ((int)ident
.super_minor
== -2) {
1289 pr_err("--super-minor=dev given, and listed device %s doesn't exist.\n",
1294 ident
.super_minor
= minor(stb
.st_rdev
);
1296 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1297 /* We don't really want this open yet, we just might
1298 * have wanted to check some things
1306 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1307 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1308 " a mistake. If you really mean it you will need to specify --force before\n"
1309 " setting the number of drives.\n");
1314 if (c
.homehost
== NULL
&& c
.require_homehost
)
1315 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1316 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1317 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1318 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1319 c
.homehost
= sys_hostname
;
1322 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1324 c
.require_homehost
= 0;
1329 set_hooks(); /* set hooks from libs */
1331 if (c
.homecluster
== NULL
&& (c
.nodes
> 0)) {
1332 c
.homecluster
= conf_get_homecluster();
1333 if (c
.homecluster
== NULL
)
1334 rv
= get_cluster_name(&c
.homecluster
);
1336 pr_err("The md can't get cluster name\n");
1341 if (c
.backup_file
&& data_offset
!= INVALID_SECTORS
) {
1342 pr_err("--backup-file and --data-offset are incompatible\n");
1346 if ((mode
== MISC
&& devmode
== 'E')
1347 || (mode
== MONITOR
&& spare_sharing
== 0))
1348 /* Anyone may try this */;
1349 else if (geteuid() != 0) {
1350 pr_err("must be super-user to perform this action\n");
1354 ident
.autof
= c
.autof
;
1356 if (c
.scan
&& c
.verbose
< 2)
1357 /* --scan implied --brief unless -vv */
1362 /* readonly, add/remove, readwrite, runstop */
1364 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1365 if (!rv
&& devs_found
>1)
1366 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1367 devlist
->next
, c
.verbose
, c
.test
,
1369 if (!rv
&& c
.readonly
< 0)
1370 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1371 if (!rv
&& c
.runstop
> 0)
1372 rv
= Manage_run(devlist
->devname
, mdfd
, &c
);
1373 if (!rv
&& c
.runstop
< 0)
1374 rv
= Manage_stop(devlist
->devname
, mdfd
, c
.verbose
, 0);
1377 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1378 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1379 /* Only a device has been given, so get details from config file */
1380 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1381 if (array_ident
== NULL
) {
1382 pr_err("%s not identified in config file.\n",
1388 if (array_ident
->autof
== 0)
1389 array_ident
->autof
= c
.autof
;
1390 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1394 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1396 else if (devs_found
> 0) {
1397 if (c
.update
&& devs_found
> 1) {
1398 pr_err("can only update a single array at a time\n");
1401 if (c
.backup_file
&& devs_found
> 1) {
1402 pr_err("can only assemble a single array when providing a backup file.\n");
1405 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1406 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1407 if (array_ident
== NULL
) {
1408 pr_err("%s not identified in config file.\n",
1413 if (array_ident
->autof
== 0)
1414 array_ident
->autof
= c
.autof
;
1415 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1420 pr_err("--update not meaningful with a --scan assembly.\n");
1423 if (c
.backup_file
) {
1424 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1427 rv
= scan_assemble(ss
, &c
, &ident
);
1433 c
.delay
= DEFAULT_BITMAP_DELAY
;
1434 if (s
.write_behind
&& !s
.bitmap_file
) {
1435 pr_err("write-behind mode requires a bitmap.\n");
1439 if (s
.raiddisks
== 0) {
1440 pr_err("no raid-devices specified.\n");
1445 if (s
.bitmap_file
) {
1446 if (strcmp(s
.bitmap_file
, "internal")==0 ||
1447 strcmp(s
.bitmap_file
, "clustered") == 0) {
1448 pr_err("'internal' and 'clustered' bitmaps not supported with --build\n");
1453 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1457 c
.delay
= DEFAULT_BITMAP_DELAY
;
1460 if (!s
.bitmap_file
|| strcmp(s
.bitmap_file
, "clustered") != 0) {
1461 pr_err("--nodes argument only compatible with --bitmap=clustered\n");
1467 pr_err("--bitmap=clustered is currently supported with RAID mirror only\n");
1473 if (s
.write_behind
&& !s
.bitmap_file
) {
1474 pr_err("write-behind mode requires a bitmap.\n");
1478 if (s
.raiddisks
== 0) {
1479 pr_err("no raid-devices specified.\n");
1484 rv
= Create(ss
, devlist
->devname
,
1485 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1486 devs_found
-1, devlist
->next
,
1487 &s
, &c
, data_offset
);
1490 if (devmode
== 'E') {
1491 if (devlist
== NULL
&& !c
.scan
) {
1492 pr_err("No devices to examine\n");
1495 if (devlist
== NULL
)
1496 devlist
= conf_get_devs();
1497 if (devlist
== NULL
) {
1498 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1501 rv
= Examine(devlist
, &c
, ss
);
1502 } else if (devmode
== DetailPlatform
) {
1503 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1504 c
.verbose
, c
.export
,
1505 devlist
? devlist
->devname
: NULL
);
1506 } else if (devlist
== NULL
) {
1507 if (devmode
== 'S' && c
.scan
)
1508 rv
= stop_scan(c
.verbose
);
1509 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1510 rv
= misc_scan(devmode
, &c
);
1511 else if (devmode
== UdevRules
)
1512 rv
= Write_rules(udev_filename
);
1514 pr_err("No devices given.\n");
1518 rv
= misc_list(devlist
, &ident
, dump_directory
, ss
, &c
);
1521 if (!devlist
&& !c
.scan
) {
1522 pr_err("Cannot monitor: need --scan or at least one device\n");
1526 if (pidfile
&& !daemonise
) {
1527 pr_err("Cannot write a pid file when not in daemon mode\n");
1532 if (get_linux_version() > 2006016)
1533 /* mdstat responds to poll */
1538 rv
= Monitor(devlist
, mailaddr
, program
,
1539 &c
, daemonise
, oneshot
,
1540 dosyslog
, pidfile
, increments
,
1545 if (array_size
> 0) {
1546 /* alway impose array size first, independent of
1548 * Do not allow level or raid_disks changes at the
1549 * same time as that can be irreversibly destructive.
1553 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1554 pr_err("cannot change array size in same operation as changing raiddisks or level.\n"
1555 " Change size first, then check that data is still intact.\n");
1559 sysfs_init(&sra
, mdfd
, NULL
);
1560 if (array_size
== MAX_SIZE
)
1561 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1563 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1566 pr_err("--array-size setting is too large.\n");
1568 pr_err("current kernel does not support setting --array-size\n");
1573 if (devs_found
> 1 && s
.raiddisks
== 0 && s
.level
== UnSet
) {
1575 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1576 pr_err("--add cannot be used with other geometry changes in --grow mode\n");
1580 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1581 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1586 } else if (s
.bitmap_file
) {
1587 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1588 s
.layout_str
!= NULL
|| devs_found
> 1) {
1589 pr_err("--bitmap changes cannot be used with other geometry changes in --grow mode\n");
1594 c
.delay
= DEFAULT_BITMAP_DELAY
;
1595 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1596 } else if (grow_continue
)
1597 rv
= Grow_continue_command(devlist
->devname
,
1598 mdfd
, c
.backup_file
,
1600 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1601 || s
.chunk
!= 0 || s
.level
!= UnSet
1602 || data_offset
!= INVALID_SECTORS
) {
1603 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1605 data_offset
, &c
, &s
);
1606 } else if (array_size
== 0)
1607 pr_err("no changes to --grow\n");
1616 pr_err("In --incremental mode, a device cannot be given with --scan.\n");
1619 if (c
.runstop
<= 0) {
1620 pr_err("--incremental --scan meaningless without --run.\n");
1623 if (devmode
== 'f') {
1624 pr_err("--incremental --scan --fail not supported.\n");
1627 rv
= IncrementalScan(&c
, NULL
);
1630 if (!rebuild_map
&& !c
.scan
) {
1631 pr_err("--incremental requires a device.\n");
1636 if (devmode
== 'f') {
1637 if (devlist
->next
) {
1638 pr_err("'--incremental --fail' can only handle one device.\n");
1642 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1645 rv
= Incremental(devlist
, &c
, ss
);
1654 static int scan_assemble(struct supertype
*ss
,
1656 struct mddev_ident
*ident
)
1658 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1659 struct mddev_dev
*devlist
= conf_get_devs();
1660 struct map_ent
*map
= NULL
;
1663 int failures
, successes
;
1665 if (conf_verify_devnames(array_list
)) {
1666 pr_err("Duplicate MD device names in conf file were found.\n");
1669 if (devlist
== NULL
) {
1670 pr_err("No devices listed in conf file were found.\n");
1673 for (a
= array_list
; a
; a
= a
->next
) {
1676 a
->autof
= c
->autof
;
1679 pr_err("failed to get exclusive lock on mapfile\n");
1684 for (a
= array_list
; a
; a
= a
->next
) {
1689 strcasecmp(a
->devname
, "<ignore>") == 0)
1692 r
= Assemble(ss
, a
->devname
,
1702 } while (failures
&& successes
);
1703 if (c
->homehost
&& cnt
== 0) {
1704 /* Maybe we can auto-assemble something.
1705 * Repeatedly call Assemble in auto-assemble mode
1710 ident
->autof
= c
->autof
;
1712 struct mddev_dev
*devlist
= conf_get_devs();
1715 rv2
= Assemble(ss
, NULL
,
1723 /* Incase there are stacked devices, we need to go around again */
1725 if (cnt
== 0 && rv
== 0) {
1726 pr_err("No arrays found in config file or automatically\n");
1730 } else if (cnt
== 0 && rv
== 0) {
1731 pr_err("No arrays found in config file\n");
1738 static int misc_scan(char devmode
, struct context
*c
)
1740 /* apply --detail or --wait-clean to
1741 * all devices in /proc/mdstat
1743 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1744 struct mdstat_ent
*e
;
1745 struct map_ent
*map
= NULL
;
1749 for (members
= 0; members
<= 1; members
++) {
1750 for (e
=ms
; e
; e
=e
->next
) {
1754 int member
= e
->metadata_version
&&
1755 strncmp(e
->metadata_version
,
1756 "external:/", 10) == 0;
1757 if (members
!= member
)
1759 me
= map_by_devnm(&map
, e
->devnm
);
1761 && strcmp(me
->path
, "/unknown") != 0)
1764 stat(name
, &stb
) != 0)
1765 name
= get_md_name(e
->devnm
);
1768 pr_err("cannot find device file for %s\n",
1773 rv
|= Detail(name
, c
);
1775 rv
|= WaitClean(name
, -1, c
->verbose
);
1783 static int stop_scan(int verbose
)
1785 /* apply --stop to all devices in /proc/mdstat */
1786 /* Due to possible stacking of devices, repeat until
1787 * nothing more can be stopped
1789 int progress
=1, err
;
1793 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1794 struct mdstat_ent
*e
;
1796 if (!progress
) last
= 1;
1797 progress
= 0; err
= 0;
1798 for (e
=ms
; e
; e
=e
->next
) {
1799 char *name
= get_md_name(e
->devnm
);
1803 pr_err("cannot find device file for %s\n",
1807 mdfd
= open_mddev(name
, 1);
1809 if (Manage_stop(name
, mdfd
, verbose
, !last
))
1819 } while (!last
&& err
);
1825 static int misc_list(struct mddev_dev
*devlist
,
1826 struct mddev_ident
*ident
,
1827 char *dump_directory
,
1828 struct supertype
*ss
, struct context
*c
)
1830 struct mddev_dev
*dv
;
1833 for (dv
=devlist
; dv
; dv
=(rv
& 16) ? NULL
: dv
->next
) {
1836 switch(dv
->disposition
) {
1838 rv
|= Detail(dv
->devname
, c
);
1840 case KillOpt
: /* Zero superblock */
1842 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1846 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1853 rv
|= Query(dv
->devname
); continue;
1855 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1857 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1860 rv
|= Wait(dv
->devname
); continue;
1862 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1864 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1866 case UpdateSubarray
:
1867 if (c
->update
== NULL
) {
1868 pr_err("-U/--update must be specified with --update-subarray\n");
1872 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1873 c
->update
, ident
, c
->verbose
);
1876 rv
|= Dump_metadata(dv
->devname
, dump_directory
, c
, ss
);
1879 rv
|= Restore_metadata(dv
->devname
, dump_directory
, c
, ss
,
1880 (dv
== devlist
&& dv
->next
== NULL
));
1883 rv
|= SetAction(dv
->devname
, c
->action
);
1886 if (dv
->devname
[0] == '/')
1887 mdfd
= open_mddev(dv
->devname
, 1);
1889 mdfd
= open_dev(dv
->devname
);
1891 pr_err("Cannot open %s\n", dv
->devname
);
1894 switch(dv
->disposition
) {
1897 rv
|= Manage_run(dv
->devname
, mdfd
, c
); break;
1899 rv
|= Manage_stop(dv
->devname
, mdfd
, c
->verbose
, 0); break;
1901 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1903 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1912 int SetAction(char *dev
, char *action
)
1914 int fd
= open(dev
, O_RDONLY
);
1917 pr_err("Couldn't open %s: %s\n", dev
, strerror(errno
));
1920 sysfs_init(&mdi
, fd
, NULL
);
1922 if (!mdi
.sys_name
[0]) {
1923 pr_err("%s is no an md array\n", dev
);
1927 if (sysfs_set_str(&mdi
, NULL
, "sync_action", action
) < 0) {
1928 pr_err("Count not set action for %s to %s: %s\n",
1929 dev
, action
, strerror(errno
));