]> git.ipfire.org Git - thirdparty/mdadm.git/blame_incremental - mdadm.c
mdmonitor: use MAILFROM to set sendmail envelope sender address
[thirdparty/mdadm.git] / mdadm.c
... / ...
CommitLineData
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
5 *
6 *
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.
11 *
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.
16 *
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
20 *
21 * Author: Neil Brown
22 * Email: <neilb@suse.de>
23 *
24 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
25 * Paul Clements, SteelEye Technology, Inc.
26 */
27
28#include "mdadm.h"
29#include "md_p.h"
30#include "xmalloc.h"
31
32#include <ctype.h>
33
34/**
35 * set_bitmap_value() - set bitmap value.
36 * @s: Shape.
37 * @c: Context.
38 * @val: value to set.
39 *
40 * Validate and set bitmap. Context is needed for setting nodes for clustered bitmap.
41 */
42static mdadm_status_t set_bitmap_value(struct shape *s, struct context *c, char *val)
43{
44 if (s->btype != BitmapUnknown) {
45 pr_err("--bitmap cannot be set twice. Second value: \"%s\".\n", val);
46 return MDADM_STATUS_ERROR;
47 }
48
49 if (strcmp(optarg, STR_COMMON_NONE) == 0) {
50 s->btype = BitmapNone;
51 return MDADM_STATUS_SUCCESS;
52 }
53
54 if (strcmp(val, "internal") == 0) {
55 s->btype = BitmapInternal;
56 return MDADM_STATUS_SUCCESS;
57 }
58
59 if (strcmp(val, "clustered") == 0) {
60 s->btype = BitmapCluster;
61 /* Set the default number of cluster nodes
62 * to 4 if not already set by user
63 */
64 if (c->nodes < 1)
65 c->nodes = 4;
66 return MDADM_STATUS_SUCCESS;
67 }
68
69 if (strchr(val, '/')) {
70 pr_err("Custom write-intent bitmap file option is not supported.\n");
71 return MDADM_STATUS_ERROR;
72 }
73
74 pr_err("--bitmap value must be 'internal', 'clustered' or 'none'\n");
75 pr_err("Current value is \"%s\"\n", val);
76 return MDADM_STATUS_ERROR;
77}
78
79static int scan_assemble(struct supertype *ss,
80 struct context *c,
81 struct mddev_ident *ident);
82static int misc_scan(char devmode, struct context *c);
83static int stop_scan(int verbose);
84static int misc_list(struct mddev_dev *devlist,
85 struct mddev_ident *ident,
86 char *dump_directory,
87 struct supertype *ss, struct context *c);
88const char Name[] = "mdadm";
89
90int main(int argc, char *argv[])
91{
92 int mode = 0;
93 int opt;
94 int option_index;
95 int rv;
96 int i;
97
98 unsigned long long array_size = 0;
99 struct mddev_ident ident;
100 char *configfile = NULL;
101 int devmode = 0;
102 struct mddev_dev *devlist = NULL;
103 struct mddev_dev **devlistend = & devlist;
104 struct mddev_dev *dv;
105 mdu_array_info_t array;
106 int devs_found = 0;
107 int grow_continue = 0;
108 struct context c = {
109 .require_homehost = 1,
110 };
111 struct shape s = {
112 .journaldisks = 0,
113 .level = UnSet,
114 .layout = UnSet,
115 .bitmap_chunk = UnSet,
116 .consistency_policy = CONSISTENCY_POLICY_UNKNOWN,
117 .data_offset = INVALID_SECTORS,
118 .btype = BitmapUnknown,
119 };
120
121 char sys_hostname[256];
122 char *mailaddr = NULL;
123 char *program = NULL;
124 int increments = 20;
125 int daemonise = 0;
126 char *pidfile = NULL;
127 int oneshot = 0;
128 int spare_sharing = 1;
129 struct supertype *ss = NULL;
130 enum flag_mode writemostly = FlagDefault;
131 enum flag_mode failfast = FlagDefault;
132 char *shortopt = short_opts;
133 int dosyslog = 0;
134 int rebuild_map = 0;
135 char *remove_path = NULL;
136 char *udev_filename = NULL;
137 char *dump_directory = NULL;
138
139 int print_help = 0;
140 FILE *outf = NULL;
141
142 int mdfd = -1;
143 int locked = 0;
144
145 srandom(time(0) ^ getpid());
146
147 if (get_linux_version() < 2006032) {
148 pr_err("This version of mdadm does not support kernels older than 2.6.32\n");
149 exit(1);
150 }
151
152 ident_init(&ident);
153
154 while ((option_index = -1),
155 (opt = getopt_long(argc, argv, shortopt, long_options,
156 &option_index)) != -1) {
157 int newmode = mode;
158 /* firstly, some mode-independent options */
159 switch(opt) {
160 case HelpOptions:
161 print_help = 2;
162 continue;
163 case 'h':
164 print_help = 1;
165 continue;
166
167 case 'V':
168 fputs(Version, stderr);
169 exit(0);
170
171 case 'v': c.verbose++;
172 continue;
173
174 case 'q': c.verbose--;
175 continue;
176
177 case 'b':
178 if (mode == ASSEMBLE || mode == BUILD ||
179 mode == CREATE || mode == GROW ||
180 mode == INCREMENTAL || mode == MANAGE)
181 break; /* b means bitmap */
182 case Brief:
183 c.brief = 1;
184 continue;
185
186 case NoDevices:
187 c.no_devices = 1;
188 continue;
189
190 case 'Y': c.export++;
191 continue;
192
193 case HomeHost:
194 if (is_devname_ignore(optarg) == true)
195 c.require_homehost = 0;
196 else
197 c.homehost = optarg;
198 continue;
199
200 case OffRootOpt:
201 /* Silently ignore old option */
202 continue;
203
204 case Prefer:
205 if (c.prefer)
206 free(c.prefer);
207 if (asprintf(&c.prefer, "/%s/", optarg) <= 0)
208 c.prefer = NULL;
209 continue;
210
211 case ':':
212 case '?':
213 fputs(Usage, stderr);
214 exit(2);
215 }
216 /* second, figure out the mode.
217 * Some options force the mode. Others
218 * set the mode if it isn't already
219 */
220
221 switch (opt) {
222 case ManageOpt:
223 newmode = MANAGE;
224 shortopt = short_bitmap_opts;
225 break;
226 case 'a':
227 case Add:
228 case AddSpare:
229 case AddJournal:
230 case 'r':
231 case Remove:
232 case Replace:
233 case With:
234 case 'f':
235 case Fail:
236 case ReAdd: /* re-add */
237 case ClusterConfirm:
238 if (!mode) {
239 newmode = MANAGE;
240 shortopt = short_bitmap_opts;
241 }
242 break;
243
244 case 'A':
245 newmode = ASSEMBLE;
246 shortopt = short_bitmap_auto_opts;
247 break;
248 case 'B':
249 newmode = BUILD;
250 shortopt = short_bitmap_auto_opts;
251 break;
252 case 'C':
253 newmode = CREATE;
254 shortopt = short_bitmap_auto_opts;
255 break;
256 case 'F':
257 newmode = MONITOR;
258 shortopt = short_monitor_opts;
259 break;
260 case 'G':
261 newmode = GROW;
262 shortopt = short_bitmap_opts;
263 break;
264 case 'I':
265 newmode = INCREMENTAL;
266 shortopt = short_bitmap_auto_opts;
267 break;
268 case AutoDetect:
269 newmode = AUTODETECT;
270 break;
271
272 case MiscOpt:
273 case 'D':
274 case 'E':
275 case 'X':
276 case 'Q':
277 case ExamineBB:
278 case Dump:
279 case Restore:
280 case Action:
281 newmode = MISC;
282 break;
283
284 case 'R':
285 case 'S':
286 case 'o':
287 case 'w':
288 case 'W':
289 case WaitOpt:
290 case Waitclean:
291 case DetailPlatform:
292 case KillSubarray:
293 case UpdateSubarray:
294 case UdevRules:
295 case KillOpt:
296 if (!mode)
297 newmode = MISC;
298 break;
299
300 case NoSharing:
301 newmode = MONITOR;
302 break;
303 }
304 if (mode && newmode == mode) {
305 /* everybody happy ! */
306 } else if (mode && newmode != mode) {
307 /* not allowed.. */
308 pr_err("");
309 if (option_index >= 0)
310 fprintf(stderr, "--%s", long_options[option_index].name);
311 else
312 fprintf(stderr, "-%c", opt);
313 fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
314 map_num_s(modes, newmode),
315 map_num_s(modes, mode));
316 exit(2);
317 } else if (!mode && newmode) {
318 mode = newmode;
319 if (mode == MISC && devs_found) {
320 pr_err("No action given for %s in --misc mode\n",
321 devlist->devname);
322 cont_err("Action options must come before device names\n");
323 exit(2);
324 }
325 } else {
326 /* special case of -c --help */
327 if ((opt == 'c' || opt == ConfigFile) &&
328 (strncmp(optarg, "--h", 3) == 0 ||
329 strncmp(optarg, "-h", 2) == 0)) {
330 fputs(Help_config, stdout);
331 exit(0);
332 }
333
334 /* If first option is a device, don't force the mode yet */
335 if (opt == 1) {
336 if (devs_found == 0) {
337 dv = xmalloc(sizeof(*dv));
338 dv->devname = optarg;
339 dv->disposition = devmode;
340 dv->writemostly = writemostly;
341 dv->failfast = failfast;
342 dv->used = 0;
343 dv->next = NULL;
344 *devlistend = dv;
345 devlistend = &dv->next;
346
347 devs_found++;
348 continue;
349 }
350 /* No mode yet, and this is the second device ... */
351 pr_err("An option must be given to set the mode before a second device\n"
352 " (%s) is listed\n", optarg);
353 exit(2);
354 }
355 if (option_index >= 0)
356 pr_err("--%s", long_options[option_index].name);
357 else
358 pr_err("-%c", opt);
359 fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
360 exit(2);
361 }
362
363 /* if we just set the mode, then done */
364 switch(opt) {
365 case ManageOpt:
366 case MiscOpt:
367 case 'A':
368 case 'B':
369 case 'C':
370 case 'F':
371 case 'G':
372 case 'I':
373 case AutoDetect:
374 continue;
375 }
376 if (opt == 1) {
377 /* an undecorated option - must be a device name.
378 */
379
380 if (devs_found > 0 && devmode == DetailPlatform) {
381 pr_err("controller may only be specified once. %s ignored\n",
382 optarg);
383 continue;
384 }
385
386 if (devs_found > 0 && mode == MANAGE && !devmode) {
387 pr_err("Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
388 exit(2);
389 }
390 if (devs_found > 0 && mode == GROW && !devmode) {
391 pr_err("Must give -a/--add for devices to add: %s\n", optarg);
392 exit(2);
393 }
394 dv = xmalloc(sizeof(*dv));
395 dv->devname = optarg;
396 dv->disposition = devmode;
397 dv->writemostly = writemostly;
398 dv->failfast = failfast;
399 dv->used = 0;
400 dv->next = NULL;
401 *devlistend = dv;
402 devlistend = &dv->next;
403
404 devs_found++;
405 continue;
406 }
407
408 /* We've got a mode, and opt is now something else which
409 * could depend on the mode */
410#define O(a,b) ((a<<16)|b)
411 switch (O(mode,opt)) {
412 case O(GROW,'c'):
413 case O(GROW,ChunkSize):
414 case O(CREATE,'c'):
415 case O(CREATE,ChunkSize):
416 case O(BUILD,'c'): /* chunk or rounding */
417 case O(BUILD,ChunkSize): /* chunk or rounding */
418 if (s.chunk) {
419 pr_err("chunk/rounding may only be specified once. Second value is %s.\n", optarg);
420 exit(2);
421 }
422 s.chunk = parse_size(optarg);
423 if (s.chunk == INVALID_SECTORS ||
424 s.chunk < 8 || (s.chunk&1)) {
425 pr_err("invalid chunk/rounding value: %s\n",
426 optarg);
427 exit(2);
428 }
429 /* Convert sectors to K */
430 s.chunk /= 2;
431 continue;
432
433 case O(INCREMENTAL, 'e'):
434 case O(CREATE,'e'):
435 case O(ASSEMBLE,'e'):
436 case O(MISC,'e'): /* set metadata (superblock) information */
437 if (ss) {
438 pr_err("metadata information already given\n");
439 exit(2);
440 }
441 for(i = 0; !ss && superlist[i]; i++)
442 ss = superlist[i]->match_metadata_desc(optarg);
443
444 if (!ss) {
445 pr_err("unrecognised metadata identifier: %s\n", optarg);
446 exit(2);
447 }
448 continue;
449
450 case O(MANAGE,'W'):
451 case O(MANAGE,WriteMostly):
452 case O(BUILD,'W'):
453 case O(BUILD,WriteMostly):
454 case O(CREATE,'W'):
455 case O(CREATE,WriteMostly):
456 /* set write-mostly for following devices */
457 writemostly = FlagSet;
458 continue;
459
460 case O(MANAGE,'w'):
461 /* clear write-mostly for following devices */
462 writemostly = FlagClear;
463 continue;
464
465 case O(MANAGE,FailFast):
466 case O(CREATE,FailFast):
467 failfast = FlagSet;
468 continue;
469 case O(MANAGE,NoFailFast):
470 failfast = FlagClear;
471 continue;
472
473 case O(GROW,'z'):
474 case O(CREATE,'z'):
475 case O(BUILD,'z'): /* size */
476 if (s.size > 0) {
477 pr_err("size may only be specified once. Second value is %s.\n", optarg);
478 exit(2);
479 }
480 if (strcmp(optarg, "max") == 0)
481 s.size = MAX_SIZE;
482 else {
483 s.size = parse_size(optarg);
484 if (s.size == INVALID_SECTORS || s.size < 8) {
485 pr_err("invalid size: %s\n", optarg);
486 exit(2);
487 }
488 /* convert sectors to K */
489 s.size /= 2;
490 }
491 continue;
492
493 case O(GROW,'Z'): /* array size */
494 if (array_size > 0) {
495 pr_err("array-size may only be specified once. Second value is %s.\n", optarg);
496 exit(2);
497 }
498 if (strcmp(optarg, "max") == 0)
499 array_size = MAX_SIZE;
500 else {
501 array_size = parse_size(optarg);
502 if (array_size == 0 ||
503 array_size == INVALID_SECTORS) {
504 pr_err("invalid array size: %s\n",
505 optarg);
506 exit(2);
507 }
508 }
509 continue;
510
511 case O(CREATE,DataOffset):
512 case O(GROW,DataOffset):
513 if (s.data_offset != INVALID_SECTORS) {
514 pr_err("data-offset may only be specified one. Second value is %s.\n", optarg);
515 exit(2);
516 }
517 if (mode == CREATE && strcmp(optarg, "variable") == 0)
518 s.data_offset = VARIABLE_OFFSET;
519 else
520 s.data_offset = parse_size(optarg);
521 if (s.data_offset == INVALID_SECTORS) {
522 pr_err("invalid data-offset: %s\n",
523 optarg);
524 exit(2);
525 }
526 continue;
527
528 case O(GROW,'l'):
529 case O(CREATE,'l'):
530 case O(BUILD,'l'): /* set raid level*/
531 if (s.level != UnSet) {
532 pr_err("raid level may only be set once. Second value is %s.\n", optarg);
533 exit(2);
534 }
535 s.level = map_name(pers, optarg);
536 if (s.level == UnSet) {
537 pr_err("invalid raid level: %s\n",
538 optarg);
539 exit(2);
540 }
541 if (s.level != 0 && s.level != LEVEL_LINEAR &&
542 s.level != 1 && s.level != LEVEL_MULTIPATH &&
543 s.level != LEVEL_FAULTY && s.level != 10 &&
544 mode == BUILD) {
545 pr_err("Raid level %s not permitted with --build.\n",
546 optarg);
547 exit(2);
548 }
549 if (s.sparedisks > 0 && s.level < 1 && s.level >= -1) {
550 pr_err("raid level %s is incompatible with spare-devices setting.\n",
551 optarg);
552 exit(2);
553 }
554 ident.level = s.level;
555 continue;
556
557 case O(GROW, 'p'): /* new layout */
558 case O(GROW, Layout):
559 if (s.layout_str) {
560 pr_err("layout may only be sent once. Second value was %s\n", optarg);
561 exit(2);
562 }
563 s.layout_str = optarg;
564 /* 'Grow' will parse the value */
565 continue;
566
567 case O(CREATE,'p'): /* raid5 layout */
568 case O(CREATE,Layout):
569 case O(BUILD,'p'): /* faulty layout */
570 case O(BUILD,Layout):
571 if (s.layout != UnSet) {
572 pr_err("layout may only be sent once. Second value was %s\n", optarg);
573 exit(2);
574 }
575 switch(s.level) {
576 default:
577 pr_err("layout not meaningful for %s arrays.\n",
578 map_num_s(pers, s.level));
579 exit(2);
580 case UnSet:
581 pr_err("raid level must be given before layout.\n");
582 exit(2);
583
584 case 0:
585 s.layout = map_name(r0layout, optarg);
586 if (s.layout == UnSet) {
587 pr_err("layout %s not understood for raid0.\n",
588 optarg);
589 exit(2);
590 }
591 break;
592 case 5:
593 s.layout = map_name(r5layout, optarg);
594 if (s.layout == UnSet) {
595 pr_err("layout %s not understood for raid5.\n",
596 optarg);
597 exit(2);
598 }
599 break;
600 case 6:
601 s.layout = map_name(r6layout, optarg);
602 if (s.layout == UnSet) {
603 pr_err("layout %s not understood for raid6.\n",
604 optarg);
605 exit(2);
606 }
607 break;
608
609 case 10:
610 s.layout = parse_layout_10(optarg);
611 if (s.layout < 0) {
612 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg);
613 exit(2);
614 }
615 break;
616 case LEVEL_FAULTY:
617 /* Faulty
618 * modeNNN
619 */
620 s.layout = parse_layout_faulty(optarg);
621 if (s.layout == -1) {
622 pr_err("layout %s not understood for faulty.\n",
623 optarg);
624 exit(2);
625 }
626 break;
627 }
628 continue;
629
630 case O(CREATE,AssumeClean):
631 case O(BUILD,AssumeClean): /* assume clean */
632 case O(GROW,AssumeClean):
633 s.assume_clean = 1;
634 continue;
635
636 case O(CREATE, WriteZeroes):
637 s.write_zeroes = 1;
638 continue;
639
640 case O(GROW,'n'):
641 case O(CREATE,'n'):
642 case O(BUILD,'n'): /* number of raid disks */
643 if (s.raiddisks) {
644 pr_err("raid-devices set twice: %d and %s\n",
645 s.raiddisks, optarg);
646 exit(2);
647 }
648 if (parse_num(&s.raiddisks, optarg) != 0 || s.raiddisks <= 0) {
649 pr_err("invalid number of raid devices: %s\n",
650 optarg);
651 exit(2);
652 }
653 ident.raid_disks = s.raiddisks;
654 continue;
655 case O(ASSEMBLE, Nodes):
656 case O(GROW, Nodes):
657 case O(CREATE, Nodes):
658 if (parse_num(&c.nodes, optarg) != 0 || c.nodes < 2) {
659 pr_err("clustered array needs two nodes at least: %s\n",
660 optarg);
661 exit(2);
662 }
663 continue;
664 case O(CREATE, ClusterName):
665 case O(ASSEMBLE, ClusterName):
666 c.homecluster = optarg;
667 if (strlen(c.homecluster) > 64) {
668 pr_err("Cluster name too big.\n");
669 exit(2);
670 }
671 continue;
672 case O(CREATE,'x'): /* number of spare (eXtra) disks */
673 if (s.sparedisks) {
674 pr_err("spare-devices set twice: %d and %s\n",
675 s.sparedisks, optarg);
676 exit(2);
677 }
678 if (s.level != UnSet && s.level <= 0 && s.level >= -1) {
679 pr_err("spare-devices setting is incompatible with raid level %d\n",
680 s.level);
681 exit(2);
682 }
683 if (parse_num(&s.sparedisks, optarg) != 0 || s.sparedisks < 0) {
684 pr_err("invalid number of spare-devices: %s\n",
685 optarg);
686 exit(2);
687 }
688 continue;
689
690 case O(CREATE,'a'):
691 case O(CREATE,Auto):
692 case O(BUILD,'a'):
693 case O(BUILD,Auto):
694 case O(INCREMENTAL,'a'):
695 case O(INCREMENTAL,Auto):
696 case O(ASSEMBLE,'a'):
697 case O(ASSEMBLE, Auto): /* auto-creation of device node - deprecated */
698 pr_info("--auto is deprecated and will be removed in future releases.\n");
699 continue;
700 case O(BUILD,'f'): /* force honouring '-n 1' */
701 case O(BUILD,Force): /* force honouring '-n 1' */
702 case O(GROW,'f'): /* ditto */
703 case O(GROW,Force): /* ditto */
704 case O(CREATE,'f'): /* force honouring of device list */
705 case O(CREATE,Force): /* force honouring of device list */
706 case O(ASSEMBLE,'f'): /* force assembly */
707 case O(ASSEMBLE,Force): /* force assembly */
708 case O(MISC,'f'): /* force zero */
709 case O(MISC,Force): /* force zero */
710 case O(MANAGE,Force): /* add device which is too large */
711 c.force = 1;
712 continue;
713 case O(CREATE,'u'): /* uuid of array */
714 case O(ASSEMBLE,'u'): /* uuid of array */
715 if (ident.uuid_set) {
716 pr_err("uuid cannot be set twice. Second value %s.\n", optarg);
717 exit(2);
718 }
719 if (parse_uuid(optarg, ident.uuid))
720 ident.uuid_set = 1;
721 else {
722 pr_err("Bad uuid: %s\n", optarg);
723 exit(2);
724 }
725 continue;
726
727 case O(CREATE,'N'):
728 case O(ASSEMBLE,'N'):
729 case O(MISC,'N'):
730 if (mode == MISC && !c.subarray) {
731 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
732 exit(2);
733 }
734
735 if (ident_set_name(&ident, optarg) != MDADM_STATUS_SUCCESS)
736 exit(2);
737
738 continue;
739
740 case O(ASSEMBLE,'m'): /* super-minor for array */
741 case O(ASSEMBLE,SuperMinor):
742 if (ident.super_minor != UnSet) {
743 pr_err("super-minor cannot be set twice. Second value: %s.\n", optarg);
744 exit(2);
745 }
746 if (strcmp(optarg, "dev") == 0)
747 ident.super_minor = -2;
748 else if (parse_num(&ident.super_minor, optarg) != 0 || ident.super_minor < 0) {
749 pr_err("Bad super-minor number: %s.\n", optarg);
750 exit(2);
751 }
752 continue;
753
754 case O(ASSEMBLE,'o'):
755 case O(MANAGE,'o'):
756 case O(CREATE,'o'):
757 c.readonly = 1;
758 continue;
759
760 case O(ASSEMBLE,'U'): /* update the superblock */
761 case O(MISC,'U'): {
762 enum update_opt print_mode = UOPT_HELP;
763 const char *error_addon = "update option";
764
765 if (c.update) {
766 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
767 map_num(update_options, c.update), optarg);
768 exit(2);
769 }
770 if (mode == MISC && !c.subarray) {
771 pr_err("Only subarrays can be updated in misc mode\n");
772 exit(2);
773 }
774
775 c.update = map_name(update_options, optarg);
776
777 if (devmode == UpdateSubarray) {
778 print_mode = UOPT_SUBARRAY_ONLY;
779 error_addon = "update-subarray option";
780
781 if (c.update > UOPT_SUBARRAY_ONLY && c.update < UOPT_HELP)
782 c.update = UOPT_UNDEFINED;
783 }
784
785 switch (c.update) {
786 case UOPT_UNDEFINED:
787 pr_err("'--update=%s' is invalid %s. ",
788 optarg, error_addon);
789 outf = stderr;
790 case UOPT_HELP:
791 if (!outf)
792 outf = stdout;
793 fprint_update_options(outf, print_mode);
794 exit(outf == stdout ? 0 : 2);
795 case UOPT_BYTEORDER:
796 if (ss) {
797 pr_err("must not set metadata type with --update=byteorder.\n");
798 exit(2);
799 }
800 default:
801 break;
802 }
803 continue;
804 }
805 case O(MANAGE,'U'):
806 /* update=devicesize is allowed with --re-add */
807 if (devmode != 'A') {
808 pr_err("--update in Manage mode only allowed with --re-add.\n");
809 exit(1);
810 }
811 if (c.update) {
812 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
813 map_num(update_options, c.update), optarg);
814 exit(2);
815 }
816 c.update = map_name(update_options, optarg);
817 if (c.update != UOPT_DEVICESIZE &&
818 c.update != UOPT_BBL &&
819 c.update != UOPT_NO_BBL &&
820 c.update != UOPT_FORCE_NO_BBL) {
821 pr_err("only 'devicesize', 'bbl', 'no-bbl', and 'force-no-bbl' can be updated with --re-add\n");
822 exit(2);
823 }
824 continue;
825
826 case O(INCREMENTAL,NoDegraded):
827 pr_err("--no-degraded is deprecated in Incremental mode\n");
828 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
829 c.runstop = -1; /* --stop isn't allowed for --assemble,
830 * so we overload slightly */
831 continue;
832
833 case O(ASSEMBLE,'c'):
834 case O(ASSEMBLE,ConfigFile):
835 case O(INCREMENTAL, 'c'):
836 case O(INCREMENTAL, ConfigFile):
837 case O(MISC, 'c'):
838 case O(MISC, ConfigFile):
839 case O(MONITOR,'c'):
840 case O(MONITOR,ConfigFile):
841 case O(CREATE,ConfigFile):
842 if (configfile) {
843 pr_err("configfile cannot be set twice. Second value is %s.\n", optarg);
844 exit(2);
845 }
846 configfile = optarg;
847 set_conffile(configfile);
848 /* FIXME possibly check that config file exists. Even parse it */
849 continue;
850 case O(ASSEMBLE,'s'): /* scan */
851 case O(MISC,'s'):
852 case O(MONITOR,'s'):
853 case O(INCREMENTAL,'s'):
854 c.scan = 1;
855 continue;
856
857 case O(MONITOR,'m'): /* mail address */
858 case O(MONITOR,EMail):
859 if (mailaddr)
860 pr_err("only specify one mailaddress. %s ignored.\n",
861 optarg);
862 else
863 mailaddr = optarg;
864 continue;
865
866 case O(MONITOR,'p'): /* alert program */
867 case O(MONITOR,ProgramOpt): /* alert program */
868 if (program)
869 pr_err("only specify one alter program. %s ignored.\n",
870 optarg);
871 else
872 program = optarg;
873 continue;
874
875 case O(MONITOR,'r'): /* rebuild increments */
876 case O(MONITOR,Increment):
877 if (parse_num(&increments, optarg) != 0
878 || increments > 99 || increments < 1) {
879 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
880 exit(2);
881 }
882 continue;
883
884 case O(MONITOR,'d'): /* delay in seconds */
885 case O(GROW, 'd'):
886 case O(BUILD,'d'): /* delay for bitmap updates */
887 case O(CREATE,'d'):
888 if (c.delay)
889 pr_err("only specify delay once. %s ignored.\n", optarg);
890 else if (parse_num(&c.delay, optarg) != 0 || c.delay < 1) {
891 pr_err("invalid delay: %s\n", optarg);
892 exit(2);
893 }
894 continue;
895 case O(MONITOR,'f'): /* daemonise */
896 case O(MONITOR,Fork):
897 daemonise = 1;
898 continue;
899 case O(MONITOR,'i'): /* pid */
900 if (pidfile)
901 pr_err("only specify one pid file. %s ignored.\n",
902 optarg);
903 else
904 pidfile = optarg;
905 continue;
906 case O(MONITOR,'1'): /* oneshot */
907 oneshot = 1;
908 spare_sharing = 0;
909 continue;
910 case O(MONITOR,'t'): /* test */
911 c.test = 1;
912 continue;
913 case O(MONITOR,'y'): /* log messages to syslog */
914 openlog("mdadm", LOG_PID, SYSLOG_FACILITY);
915 dosyslog = 1;
916 continue;
917 case O(MONITOR, NoSharing):
918 spare_sharing = 0;
919 continue;
920
921 /* now the general management options. Some are applicable
922 * to other modes. None have arguments.
923 */
924 case O(GROW,'a'):
925 case O(GROW,Add):
926 case O(MANAGE,'a'):
927 case O(MANAGE,Add): /* add a drive */
928 devmode = 'a';
929 continue;
930 case O(MANAGE,AddSpare): /* add drive - never re-add */
931 devmode = 'S';
932 continue;
933 case O(MANAGE,AddJournal): /* add journal */
934 if (s.journaldisks && (s.level < 4 || s.level > 6)) {
935 pr_err("--add-journal is only supported for RAID level 4/5/6.\n");
936 exit(2);
937 }
938 devmode = 'j';
939 continue;
940 case O(MANAGE,ReAdd):
941 devmode = 'A';
942 continue;
943 case O(MANAGE,'r'): /* remove a drive */
944 case O(MANAGE,Remove):
945 devmode = 'r';
946 continue;
947 case O(MANAGE,'f'): /* set faulty */
948 case O(MANAGE,Fail):
949 case O(INCREMENTAL,'f'):
950 case O(INCREMENTAL,Remove):
951 case O(INCREMENTAL,Fail): /* r for incremental is taken, use f
952 * even though we will both fail and
953 * remove the device */
954 devmode = 'f';
955 continue;
956 case O(MANAGE, ClusterConfirm):
957 devmode = 'c';
958 continue;
959 case O(MANAGE,Replace):
960 /* Mark these devices for replacement */
961 devmode = 'R';
962 continue;
963 case O(MANAGE,With):
964 /* These are the replacements to use */
965 if (devmode != 'R') {
966 pr_err("--with must follow --replace\n");
967 exit(2);
968 }
969 devmode = 'W';
970 continue;
971 case O(INCREMENTAL,'R'):
972 case O(MANAGE,'R'):
973 case O(ASSEMBLE,'R'):
974 case O(BUILD,'R'):
975 case O(CREATE,'R'): /* Run the array */
976 if (c.runstop < 0) {
977 pr_err("Cannot both Stop and Run an array\n");
978 exit(2);
979 }
980 c.runstop = 1;
981 continue;
982 case O(MANAGE,'S'):
983 if (c.runstop > 0) {
984 pr_err("Cannot both Run and Stop an array\n");
985 exit(2);
986 }
987 c.runstop = -1;
988 continue;
989 case O(MANAGE,'t'):
990 c.test = 1;
991 continue;
992
993 case O(MISC,'Q'):
994 case O(MISC,'D'):
995 case O(MISC,'E'):
996 case O(MISC,KillOpt):
997 case O(MISC,'R'):
998 case O(MISC,'S'):
999 case O(MISC,'X'):
1000 case O(MISC, ExamineBB):
1001 case O(MISC,'o'):
1002 case O(MISC,'w'):
1003 case O(MISC,'W'):
1004 case O(MISC, WaitOpt):
1005 case O(MISC, Waitclean):
1006 case O(MISC, DetailPlatform):
1007 case O(MISC, KillSubarray):
1008 case O(MISC, UpdateSubarray):
1009 case O(MISC, Dump):
1010 case O(MISC, Restore):
1011 case O(MISC ,Action):
1012 if (opt == KillSubarray || opt == UpdateSubarray) {
1013 if (c.subarray) {
1014 pr_err("subarray can only be specified once\n");
1015 exit(2);
1016 }
1017 c.subarray = optarg;
1018 }
1019 if (opt == Action) {
1020 if (c.action) {
1021 pr_err("Only one --action can be specified\n");
1022 exit(2);
1023 }
1024 if (strcmp(optarg, "idle") == 0 ||
1025 strcmp(optarg, "frozen") == 0 ||
1026 strcmp(optarg, "check") == 0 ||
1027 strcmp(optarg, "repair") == 0)
1028 c.action = optarg;
1029 else {
1030 pr_err("action must be one of idle, frozen, check, repair\n");
1031 exit(2);
1032 }
1033 }
1034 if (devmode && devmode != opt &&
1035 (devmode == 'E' ||
1036 (opt == 'E' && devmode != 'Q'))) {
1037 pr_err("--examine/-E cannot be given with ");
1038 if (devmode == 'E') {
1039 if (option_index >= 0)
1040 fprintf(stderr, "--%s\n",
1041 long_options[option_index].name);
1042 else
1043 fprintf(stderr, "-%c\n", opt);
1044 } else if (isalpha(devmode))
1045 fprintf(stderr, "-%c\n", devmode);
1046 else
1047 fprintf(stderr, "previous option\n");
1048 exit(2);
1049 }
1050 devmode = opt;
1051 if (opt == Dump || opt == Restore) {
1052 if (dump_directory != NULL) {
1053 pr_err("dump/restore directory specified twice: %s and %s\n",
1054 dump_directory, optarg);
1055 exit(2);
1056 }
1057 dump_directory = optarg;
1058 }
1059 continue;
1060 case O(MISC, UdevRules):
1061 if (devmode && devmode != opt) {
1062 pr_err("--udev-rules must be the only option.\n");
1063 } else {
1064 if (udev_filename)
1065 pr_err("only specify one udev rule filename. %s ignored.\n",
1066 optarg);
1067 else
1068 udev_filename = optarg;
1069 }
1070 devmode = opt;
1071 continue;
1072 case O(MISC,'t'):
1073 c.test = 1;
1074 continue;
1075
1076 case O(MISC, Sparc22):
1077 if (devmode != 'E') {
1078 pr_err("--sparc2.2 only allowed with --examine\n");
1079 exit(2);
1080 }
1081 c.SparcAdjust = 1;
1082 continue;
1083
1084 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
1085 case O(ASSEMBLE,Bitmap):
1086 if (optarg && (strcmp(optarg, "internal") == 0 ||
1087 strcmp(optarg, "clustered")) == 0) {
1088 pr_err("no need to specify --bitmap when assembling"
1089 " arrays with internal or clustered bitmap\n");
1090 continue;
1091 }
1092
1093 pr_err("bitmap file is not supported %s\n", optarg);
1094 exit(2);
1095 case O(ASSEMBLE, BackupFile):
1096 case O(GROW, BackupFile):
1097 /* Specify a file into which grow might place a backup,
1098 * or from which assemble might recover a backup
1099 */
1100 if (c.backup_file) {
1101 pr_err("backup file already specified, rejecting %s\n", optarg);
1102 exit(2);
1103 }
1104 c.backup_file = optarg;
1105 continue;
1106
1107 case O(GROW, Continue):
1108 /* Continue interrupted grow
1109 */
1110 grow_continue = 1;
1111 continue;
1112 case O(ASSEMBLE, InvalidBackup):
1113 /* Acknowledge that the backupfile is invalid, but ask
1114 * to continue anyway
1115 */
1116 c.invalid_backup = 1;
1117 continue;
1118
1119 case O(BUILD,'b'):
1120 case O(BUILD,Bitmap):
1121 case O(CREATE,'b'):
1122 case O(CREATE,Bitmap): /* here we create the bitmap */
1123 case O(GROW,'b'):
1124 case O(GROW,Bitmap):
1125 if (set_bitmap_value(&s, &c, optarg))
1126 exit(2);
1127 continue;
1128 case O(GROW,BitmapChunk):
1129 case O(BUILD,BitmapChunk):
1130 case O(CREATE,BitmapChunk): /* bitmap chunksize */
1131 s.bitmap_chunk = parse_size(optarg);
1132 if (s.bitmap_chunk == 0 ||
1133 s.bitmap_chunk == INVALID_SECTORS ||
1134 s.bitmap_chunk & (s.bitmap_chunk - 1)) {
1135 pr_err("invalid bitmap chunksize: %s\n",
1136 optarg);
1137 exit(2);
1138 }
1139 s.bitmap_chunk = s.bitmap_chunk * 512;
1140 continue;
1141
1142 case O(GROW, WriteBehind):
1143 case O(BUILD, WriteBehind):
1144 case O(CREATE, WriteBehind):
1145 s.write_behind = DEFAULT_MAX_WRITE_BEHIND;
1146 if (optarg &&
1147 (parse_num(&s.write_behind, optarg) != 0 ||
1148 s.write_behind < 0 || s.write_behind > 16383)) {
1149 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n",
1150 optarg);
1151 exit(2);
1152 }
1153 continue;
1154 case O(INCREMENTAL, 'r'):
1155 case O(INCREMENTAL, RebuildMapOpt):
1156 rebuild_map = 1;
1157 continue;
1158 case O(INCREMENTAL, IncrementalPath):
1159 remove_path = optarg;
1160 continue;
1161 case O(CREATE, WriteJournal):
1162 if (s.journaldisks) {
1163 pr_err("Please specify only one journal device for the array.\n");
1164 pr_err("Ignoring --write-journal %s...\n", optarg);
1165 continue;
1166 }
1167 dv = xmalloc(sizeof(*dv));
1168 dv->devname = optarg;
1169 dv->disposition = 'j'; /* WriteJournal */
1170 dv->used = 0;
1171 dv->next = NULL;
1172 *devlistend = dv;
1173 devlistend = &dv->next;
1174 devs_found++;
1175
1176 s.journaldisks = 1;
1177 continue;
1178 case O(CREATE, 'k'):
1179 case O(GROW, 'k'):
1180 s.consistency_policy = map_name(consistency_policies,
1181 optarg);
1182 if (s.consistency_policy < CONSISTENCY_POLICY_RESYNC) {
1183 pr_err("Invalid consistency policy: %s\n",
1184 optarg);
1185 exit(2);
1186 }
1187 continue;
1188 }
1189 /* We have now processed all the valid options. Anything else is
1190 * an error
1191 */
1192 if (option_index > 0)
1193 pr_err(":option --%s not valid in %s mode\n",
1194 long_options[option_index].name,
1195 map_num_s(modes, mode));
1196 else
1197 pr_err("option -%c not valid in %s mode\n",
1198 opt, map_num_s(modes, mode));
1199 exit(2);
1200
1201 }
1202
1203 if (print_help) {
1204 char *help_text;
1205 if (print_help == 2)
1206 help_text = OptionHelp;
1207 else
1208 help_text = mode_help[mode];
1209 if (help_text == NULL)
1210 help_text = Help;
1211 fputs(help_text,stdout);
1212 exit(0);
1213 }
1214
1215 if (s.journaldisks) {
1216 if (s.level < 4 || s.level > 6) {
1217 pr_err("--write-journal is only supported for RAID level 4/5/6.\n");
1218 exit(2);
1219 }
1220 if (s.consistency_policy != CONSISTENCY_POLICY_UNKNOWN &&
1221 s.consistency_policy != CONSISTENCY_POLICY_JOURNAL) {
1222 pr_err("--write-journal is not supported with consistency policy: %s\n",
1223 map_num_s(consistency_policies, s.consistency_policy));
1224 exit(2);
1225 }
1226 }
1227
1228 if (mode == CREATE &&
1229 s.consistency_policy != CONSISTENCY_POLICY_UNKNOWN) {
1230 if (s.level <= 0) {
1231 pr_err("--consistency-policy not meaningful with level %s.\n",
1232 map_num_s(pers, s.level));
1233 exit(2);
1234 } else if (s.consistency_policy == CONSISTENCY_POLICY_JOURNAL &&
1235 !s.journaldisks) {
1236 pr_err("--write-journal is required for consistency policy: %s\n",
1237 map_num_s(consistency_policies, s.consistency_policy));
1238 exit(2);
1239 } else if (s.consistency_policy == CONSISTENCY_POLICY_PPL &&
1240 s.level != 5) {
1241 pr_err("PPL consistency policy is only supported for RAID level 5.\n");
1242 exit(2);
1243 } else if (s.consistency_policy == CONSISTENCY_POLICY_BITMAP &&
1244 s.btype == BitmapNone) {
1245 pr_err("--bitmap is required for consistency policy: %s\n",
1246 map_num_s(consistency_policies, s.consistency_policy));
1247 exit(2);
1248 } else if ((s.btype == BitmapInternal || s.btype == BitmapCluster) &&
1249 s.consistency_policy != CONSISTENCY_POLICY_BITMAP &&
1250 s.consistency_policy != CONSISTENCY_POLICY_JOURNAL) {
1251 pr_err("--bitmap is not compatible with consistency policy: %s\n",
1252 map_num_s(consistency_policies, s.consistency_policy));
1253 exit(2);
1254 }
1255 }
1256
1257 if (s.write_zeroes && !s.assume_clean) {
1258 pr_info("Disk zeroing requested, setting --assume-clean to skip resync\n");
1259 s.assume_clean = 1;
1260 }
1261
1262 if (!mode && devs_found) {
1263 mode = MISC;
1264 devmode = 'Q';
1265 if (devlist->disposition == 0)
1266 devlist->disposition = devmode;
1267 }
1268 if (!mode) {
1269 fputs(Usage, stderr);
1270 exit(2);
1271 }
1272
1273 /* Ok, got the option parsing out of the way
1274 * hopefully it's mostly right but there might be some stuff
1275 * missing
1276 *
1277 * That is mostly checked in the per-mode stuff but...
1278 *
1279 * For @,B,C and A without -s, the first device listed must be
1280 * an md device. We check that here and open it.
1281 */
1282
1283 if (mode == MANAGE || mode == BUILD || mode == CREATE ||
1284 mode == GROW || (mode == ASSEMBLE && ! c.scan)) {
1285 struct stat stb;
1286 int ret;
1287
1288 if (devs_found < 1) {
1289 pr_err("an md device must be given in this mode\n");
1290 exit(2);
1291 }
1292 if (ident_set_devname(&ident, devlist->devname) != MDADM_STATUS_SUCCESS)
1293 exit(1);
1294
1295 if (mode == MANAGE || mode == GROW) {
1296 mdfd = open_mddev(ident.devname, 1);
1297 if (mdfd < 0)
1298 exit(1);
1299
1300 ret = fstat(mdfd, &stb);
1301 if (ret) {
1302 pr_err("fstat failed on %s.\n", ident.devname);
1303 exit(1);
1304 }
1305 } else {
1306 ret = stat(ident.devname, &stb);
1307 if (ident.super_minor == -2 && ret != 0) {
1308 pr_err("--super-minor=dev given, and listed device %s doesn't exist.\n",
1309 ident.devname);
1310 exit(1);
1311 }
1312
1313 if (!ret && !stat_is_md_dev(&stb)) {
1314 pr_err("device %s exists but is not an md array.\n", ident.devname);
1315 exit(1);
1316 }
1317 }
1318 if (ident.super_minor == -2)
1319 ident.super_minor = minor(stb.st_rdev);
1320 }
1321
1322 if (s.raiddisks) {
1323 if (s.raiddisks == 1 && !c.force && s.level != LEVEL_FAULTY) {
1324 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1325 " a mistake. If you really mean it you will need to specify --force before\n"
1326 " setting the number of drives.\n");
1327 exit(2);
1328 }
1329 }
1330
1331 if (c.homehost == NULL && c.require_homehost)
1332 c.homehost = conf_get_homehost(&c.require_homehost);
1333 if (c.homehost == NULL || strcasecmp(c.homehost, "<system>") == 0) {
1334 if (s_gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
1335 c.homehost = sys_hostname;
1336 }
1337 }
1338 if (c.homehost &&
1339 (!c.homehost[0] || strcasecmp(c.homehost, "<none>") == 0)) {
1340 c.homehost = NULL;
1341 c.require_homehost = 0;
1342 }
1343
1344 rv = 0;
1345
1346 set_hooks(); /* set hooks from libs */
1347
1348 if (c.homecluster == NULL && (c.nodes > 0)) {
1349 c.homecluster = conf_get_homecluster();
1350 if (c.homecluster == NULL)
1351 rv = get_cluster_name(&c.homecluster);
1352 if (rv) {
1353 pr_err("The md can't get cluster name\n");
1354 exit(1);
1355 }
1356 }
1357
1358 if (c.update && c.update == UOPT_NODES && c.nodes == 0) {
1359 pr_err("Please specify nodes number with --nodes\n");
1360 exit(1);
1361 }
1362
1363 if (c.backup_file && s.data_offset != INVALID_SECTORS) {
1364 pr_err("--backup-file and --data-offset are incompatible\n");
1365 exit(2);
1366 }
1367
1368 if ((mode == MISC && devmode == 'E') ||
1369 (mode == MONITOR && spare_sharing == 0))
1370 /* Anyone may try this */;
1371 else if (geteuid() != 0) {
1372 pr_err("must be super-user to perform this action\n");
1373 exit(1);
1374 }
1375
1376 if (c.scan && c.verbose < 2)
1377 /* --scan implied --brief unless -vv */
1378 c.brief = 1;
1379
1380 if (mode == CREATE) {
1381 if (s.btype == BitmapCluster) {
1382 locked = cluster_get_dlmlock();
1383 if (locked != 1)
1384 exit(1);
1385 }
1386 } else if (mode == MANAGE || mode == GROW || mode == INCREMENTAL) {
1387 if (!md_get_array_info(mdfd, &array) && (devmode != 'c')) {
1388 if (array.state & (1 << MD_SB_CLUSTERED)) {
1389 locked = cluster_get_dlmlock();
1390 if (locked != 1)
1391 exit(1);
1392 }
1393 }
1394 }
1395
1396 switch(mode) {
1397 case MANAGE:
1398 /* readonly, add/remove, readwrite, runstop */
1399 if (c.readonly > 0)
1400 rv = Manage_ro(ident.devname, mdfd, c.readonly);
1401 if (!rv && devs_found > 1)
1402 rv = Manage_subdevs(ident.devname, mdfd,
1403 devlist->next, c.verbose,
1404 c.test, c.update, c.force);
1405 if (!rv && c.readonly < 0)
1406 rv = Manage_ro(ident.devname, mdfd, c.readonly);
1407 if (!rv && c.runstop > 0)
1408 rv = Manage_run(ident.devname, mdfd, &c);
1409 if (!rv && c.runstop < 0)
1410 rv = Manage_stop(ident.devname, mdfd, c.verbose, 0);
1411 break;
1412 case ASSEMBLE:
1413 if (!c.scan && c.runstop == -1) {
1414 pr_err("--no-degraded not meaningful without a --scan assembly.\n");
1415 exit(1);
1416 } else if (devs_found == 1 && ident.uuid_set == 0 &&
1417 ident.super_minor == UnSet && ident.name[0] == 0 &&
1418 !c.scan) {
1419 /* Only a device has been given, so get details from config file */
1420 struct mddev_ident *array_ident = conf_get_ident(ident.devname);
1421 if (array_ident == NULL) {
1422 pr_err("%s not identified in config file.\n", ident.devname);
1423 rv |= 1;
1424 if (mdfd >= 0)
1425 close(mdfd);
1426 } else {
1427 rv |= Assemble(ss, ident.devname, array_ident, NULL, &c);
1428 }
1429 } else if (!c.scan)
1430 rv = Assemble(ss, ident.devname, &ident, devlist->next, &c);
1431 else if (devs_found > 0) {
1432 if (c.update && devs_found > 1) {
1433 pr_err("can only update a single array at a time\n");
1434 exit(1);
1435 }
1436 if (c.backup_file && devs_found > 1) {
1437 pr_err("can only assemble a single array when providing a backup file.\n");
1438 exit(1);
1439 }
1440 for (dv = devlist; dv; dv = dv->next) {
1441 struct mddev_ident *array_ident = conf_get_ident(dv->devname);
1442 if (array_ident == NULL) {
1443 pr_err("%s not identified in config file.\n",
1444 dv->devname);
1445 rv |= 1;
1446 continue;
1447 }
1448 rv |= Assemble(ss, dv->devname, array_ident, NULL, &c);
1449 }
1450 } else {
1451 if (c.update) {
1452 pr_err("--update not meaningful with a --scan assembly.\n");
1453 exit(1);
1454 }
1455 if (c.backup_file) {
1456 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1457 exit(1);
1458 }
1459 rv = scan_assemble(ss, &c, &ident);
1460 }
1461
1462 break;
1463 case BUILD:
1464 if (c.delay == 0)
1465 c.delay = DEFAULT_BITMAP_DELAY;
1466
1467 if (s.btype == BitmapUnknown)
1468 s.btype = BitmapNone;
1469
1470 if (s.btype != BitmapNone) {
1471 pr_err("--build argument only compatible with --bitmap=none\n");
1472 rv |= 1;
1473 break;
1474 }
1475
1476 if (s.write_behind) {
1477 pr_err("write-behind mode requires a bitmap.\n");
1478 rv = 1;
1479 break;
1480 }
1481 if (s.raiddisks == 0) {
1482 pr_err("no raid-devices specified.\n");
1483 rv = 1;
1484 break;
1485 }
1486
1487 rv = Build(&ident, devlist->next, &s, &c);
1488 break;
1489 case CREATE:
1490 if (c.delay == 0)
1491 c.delay = DEFAULT_BITMAP_DELAY;
1492
1493 if (c.nodes) {
1494 if (s.btype != BitmapCluster) {
1495 pr_err("--nodes argument only compatible with --bitmap=clustered\n");
1496 rv = 1;
1497 break;
1498 }
1499
1500 if (s.level != 1 && s.level != 10) {
1501 pr_err("--bitmap=clustered is currently supported with raid1/10 only\n");
1502 rv = 1;
1503 break;
1504 }
1505 if (s.level == 10 && !(is_near_layout_10(s.layout) || s.layout == UnSet)) {
1506 pr_err("only near layout is supported with clustered raid10\n");
1507 rv = 1;
1508 break;
1509 }
1510 }
1511
1512 if (s.write_behind && s.btype == BitmapNone) {
1513 pr_err("write-behind mode requires a bitmap.\n");
1514 rv = 1;
1515 break;
1516 }
1517 if (s.raiddisks == 0) {
1518 pr_err("no raid-devices specified.\n");
1519 rv = 1;
1520 break;
1521 }
1522
1523 if (s.btype == BitmapUnknown) {
1524 if (c.runstop != 1 && s.level >= 1 &&
1525 ask("To optimize recovery speed, it is recommended to enable write-intent bitmap, do you want to enable it now?"))
1526 s.btype = BitmapInternal;
1527 else
1528 s.btype = BitmapNone;
1529 }
1530
1531 rv = Create(ss, &ident, devs_found - 1, devlist->next, &s, &c);
1532 break;
1533 case MISC:
1534 if (devmode == 'E') {
1535 if (devlist == NULL && !c.scan) {
1536 pr_err("No devices to examine\n");
1537 exit(2);
1538 }
1539 if (devlist == NULL)
1540 devlist = conf_get_devs();
1541 if (devlist == NULL) {
1542 pr_err("No devices listed in %s\n", configfile?configfile:DefaultConfFile);
1543 exit(1);
1544 }
1545 rv = Examine(devlist, &c, ss);
1546 } else if (devmode == DetailPlatform) {
1547 rv = Detail_Platform(ss ? ss->ss : NULL, ss ? c.scan : 1,
1548 c.verbose, c.export,
1549 devlist ? devlist->devname : NULL);
1550 } else if (devlist == NULL) {
1551 if (devmode == 'S' && c.scan)
1552 rv = stop_scan(c.verbose);
1553 else if ((devmode == 'D' || devmode == Waitclean) &&
1554 c.scan)
1555 rv = misc_scan(devmode, &c);
1556 else if (devmode == UdevRules)
1557 rv = Write_rules(udev_filename);
1558 else {
1559 pr_err("No devices given.\n");
1560 exit(2);
1561 }
1562 } else
1563 rv = misc_list(devlist, &ident, dump_directory, ss, &c);
1564 break;
1565 case MONITOR:
1566 if (!devlist && !c.scan) {
1567 pr_err("Cannot monitor: need --scan or at least one device\n");
1568 rv = 1;
1569 break;
1570 }
1571 if (pidfile && !daemonise) {
1572 pr_err("Cannot write a pid file when not in daemon mode\n");
1573 rv = 1;
1574 break;
1575 }
1576 if (c.delay == 0) {
1577 c.delay = conf_get_monitor_delay();
1578 if (!c.delay)
1579 c.delay = 60;
1580 }
1581 rv = Monitor(devlist, mailaddr, program,
1582 &c, daemonise, oneshot,
1583 dosyslog, pidfile, increments,
1584 spare_sharing);
1585 break;
1586
1587 case GROW:
1588 if (array_size > 0) {
1589 /* alway impose array size first, independent of
1590 * anything else
1591 * Do not allow level or raid_disks changes at the
1592 * same time as that can be irreversibly destructive.
1593 */
1594 struct mdinfo sra;
1595 int err;
1596 if (s.raiddisks || s.level != UnSet) {
1597 pr_err("cannot change array size in same operation as changing raiddisks or level.\n"
1598 " Change size first, then check that data is still intact.\n");
1599 rv = 1;
1600 break;
1601 }
1602 if (sysfs_init(&sra, mdfd, NULL)) {
1603 rv = 1;
1604 break;
1605 }
1606 if (array_size == MAX_SIZE)
1607 err = sysfs_set_str(&sra, NULL, "array_size", "default");
1608 else
1609 err = sysfs_set_num(&sra, NULL, "array_size", array_size / 2);
1610 if (err < 0) {
1611 if (errno == E2BIG)
1612 pr_err("--array-size setting is too large.\n");
1613 else
1614 pr_err("current kernel does not support setting --array-size\n");
1615 rv = 1;
1616 break;
1617 }
1618 }
1619 if (devs_found > 1 && s.raiddisks == 0 && s.level == UnSet) {
1620 /* must be '-a'. */
1621 if (s.size > 0 || s.chunk ||
1622 s.layout_str || s.btype != BitmapUnknown) {
1623 pr_err("--add cannot be used with other geometry changes in --grow mode\n");
1624 rv = 1;
1625 break;
1626 }
1627 for (dv = devlist->next; dv; dv = dv->next) {
1628 rv = Grow_Add_device(ident.devname, mdfd, dv->devname);
1629 if (rv)
1630 break;
1631 }
1632 } else if (s.btype != BitmapUnknown) {
1633 if (s.size > 0 || s.raiddisks || s.chunk ||
1634 s.layout_str || devs_found > 1) {
1635 pr_err("--bitmap changes cannot be used with other geometry changes in --grow mode\n");
1636 rv = 1;
1637 break;
1638 }
1639 if (c.delay == 0)
1640 c.delay = DEFAULT_BITMAP_DELAY;
1641 rv = Grow_addbitmap(ident.devname, mdfd, &c, &s);
1642 } else if (grow_continue)
1643 rv = Grow_continue_command(ident.devname, mdfd, &c);
1644 else if (s.size > 0 || s.raiddisks || s.layout_str ||
1645 s.chunk != 0 || s.level != UnSet ||
1646 s.data_offset != INVALID_SECTORS) {
1647 rv = Grow_reshape(ident.devname, mdfd, devlist->next, &c, &s);
1648 } else if (s.consistency_policy != CONSISTENCY_POLICY_UNKNOWN) {
1649 rv = Grow_consistency_policy(ident.devname, mdfd, &c, &s);
1650 } else if (array_size == 0)
1651 pr_err("no changes to --grow\n");
1652 break;
1653 case INCREMENTAL:
1654 if (rebuild_map) {
1655 RebuildMap();
1656 }
1657 if (c.scan) {
1658 rv = 1;
1659 if (devlist) {
1660 pr_err("In --incremental mode, a device cannot be given with --scan.\n");
1661 break;
1662 }
1663 if (c.runstop <= 0) {
1664 pr_err("--incremental --scan meaningless without --run.\n");
1665 break;
1666 }
1667 if (devmode == 'f') {
1668 pr_err("--incremental --scan --fail not supported.\n");
1669 break;
1670 }
1671 rv = IncrementalScan(&c, NULL);
1672 }
1673 if (!devlist) {
1674 if (!rebuild_map && !c.scan) {
1675 pr_err("--incremental requires a device.\n");
1676 rv = 1;
1677 }
1678 break;
1679 }
1680 if (devmode == 'f') {
1681 if (devlist->next) {
1682 pr_err("'--incremental --fail' can only handle one device.\n");
1683 rv = 1;
1684 break;
1685 }
1686 rv = Incremental_remove(devlist->devname, remove_path, c.verbose);
1687 } else
1688 rv = Incremental(devlist, &c, ss);
1689 break;
1690 case AUTODETECT:
1691 autodetect();
1692 break;
1693 }
1694 if (ss) {
1695 ss->ss->free_super(ss);
1696 free(ss);
1697 }
1698 if (locked)
1699 cluster_release_dlmlock();
1700 close_fd(&mdfd);
1701 exit(rv);
1702}
1703
1704static int scan_assemble(struct supertype *ss,
1705 struct context *c,
1706 struct mddev_ident *ident)
1707{
1708 struct mddev_ident *a, *array_list = conf_get_ident(NULL);
1709 struct mddev_dev *devlist = conf_get_devs();
1710 struct map_ent *map = NULL;
1711 int cnt = 0;
1712 int rv = 0;
1713 int failures, successes;
1714
1715 if (conf_verify_devnames(array_list)) {
1716 pr_err("Duplicate MD device names in conf file were found.\n");
1717 return 1;
1718 }
1719 if (devlist == NULL) {
1720 pr_err("No devices listed in conf file were found.\n");
1721 return 1;
1722 }
1723
1724 for (a = array_list; a; a = a->next)
1725 a->assembled = 0;
1726
1727 if (map_lock(&map))
1728 pr_err("failed to get exclusive lock on mapfile\n");
1729 do {
1730 failures = 0;
1731 successes = 0;
1732 rv = 0;
1733 for (a = array_list; a; a = a->next) {
1734 int r;
1735 if (a->assembled)
1736 continue;
1737 if (a->devname && is_devname_ignore(a->devname) == true)
1738 continue;
1739
1740 r = Assemble(ss, a->devname,
1741 a, NULL, c);
1742 if (r == 0) {
1743 a->assembled = 1;
1744 successes++;
1745 } else
1746 failures++;
1747 rv |= r;
1748 cnt++;
1749 }
1750 } while (failures && successes);
1751 if (c->homehost && cnt == 0) {
1752 /* Maybe we can auto-assemble something.
1753 * Repeatedly call Assemble in auto-assemble mode
1754 * until it fails
1755 */
1756 int rv2;
1757 int acnt;
1758
1759 do {
1760 struct mddev_dev *devlist = conf_get_devs();
1761 acnt = 0;
1762 do {
1763 rv2 = Assemble(ss, NULL,
1764 ident,
1765 devlist, c);
1766 if (rv2 == 0) {
1767 cnt++;
1768 acnt++;
1769 }
1770 } while (rv2 != 2);
1771 /* Incase there are stacked devices, we need to go around again */
1772 } while (acnt);
1773 if (cnt == 0 && rv == 0) {
1774 pr_err("No arrays found in config file or automatically\n");
1775 rv = 1;
1776 } else if (cnt)
1777 rv = 0;
1778 } else if (cnt == 0 && rv == 0) {
1779 pr_err("No arrays found in config file\n");
1780 rv = 1;
1781 }
1782 map_unlock(&map);
1783 return rv;
1784}
1785
1786static int misc_scan(char devmode, struct context *c)
1787{
1788 /* apply --detail or --wait-clean to
1789 * all devices in /proc/mdstat
1790 */
1791 struct mdstat_ent *ms = mdstat_read(0, 1);
1792 struct mdstat_ent *e;
1793 struct map_ent *map = NULL;
1794 int members;
1795 int rv = 0;
1796
1797 for (members = 0; members <= 1; members++) {
1798 for (e = ms; e; e = e->next) {
1799 char *name = NULL;
1800 struct map_ent *me;
1801 struct stat stb;
1802 int member = e->metadata_version &&
1803 strncmp(e->metadata_version,
1804 "external:/", 10) == 0;
1805 if (members != member)
1806 continue;
1807 me = map_by_devnm(&map, e->devnm);
1808 if (me && me->path && strcmp(me->path, "/unknown") != 0)
1809 name = me->path;
1810 if (name == NULL || stat(name, &stb) != 0)
1811 name = get_md_name(e->devnm);
1812
1813 if (!name) {
1814 pr_err("cannot find device file for %s\n",
1815 e->devnm);
1816 continue;
1817 }
1818 if (devmode == 'D')
1819 rv |= Detail(name, c);
1820 else
1821 rv |= WaitClean(name, c->verbose);
1822 put_md_name(name);
1823 map_free(map);
1824 map = NULL;
1825 }
1826 }
1827 free_mdstat(ms);
1828 return rv;
1829}
1830
1831static int stop_scan(int verbose)
1832{
1833 /* apply --stop to all devices in /proc/mdstat */
1834 /* Due to possible stacking of devices, repeat until
1835 * nothing more can be stopped
1836 */
1837 int progress = 1, err;
1838 int last = 0;
1839 int rv = 0;
1840 do {
1841 struct mdstat_ent *ms = mdstat_read(0, 0);
1842 struct mdstat_ent *e;
1843
1844 if (!progress) last = 1;
1845 progress = 0; err = 0;
1846 for (e = ms; e; e = e->next) {
1847 char *name = get_md_name(e->devnm);
1848 int mdfd;
1849
1850 if (!name) {
1851 pr_err("cannot find device file for %s\n",
1852 e->devnm);
1853 continue;
1854 }
1855 mdfd = open_mddev(name, 1);
1856 if (mdfd >= 0) {
1857 if (Manage_stop(name, mdfd, verbose, !last))
1858 err = 1;
1859 else
1860 progress = 1;
1861 close(mdfd);
1862 }
1863
1864 put_md_name(name);
1865 }
1866 free_mdstat(ms);
1867 } while (!last && err);
1868 if (err)
1869 rv |= 1;
1870 return rv;
1871}
1872
1873static int misc_list(struct mddev_dev *devlist,
1874 struct mddev_ident *ident,
1875 char *dump_directory,
1876 struct supertype *ss, struct context *c)
1877{
1878 struct mddev_dev *dv;
1879 int rv = 0;
1880
1881 for (dv = devlist; dv; dv = (rv & 16) ? NULL : dv->next) {
1882 int mdfd = -1;
1883
1884 switch(dv->disposition) {
1885 case 'D':
1886 rv |= Detail(dv->devname, c);
1887 continue;
1888 case KillOpt: /* Zero superblock */
1889 if (ss)
1890 rv |= Kill(dv->devname, ss, c->force, c->verbose,0);
1891 else {
1892 int v = c->verbose;
1893 do {
1894 rv |= Kill(dv->devname, NULL, c->force, v, 0);
1895 v = -1;
1896 } while (rv == 0);
1897 rv &= ~4;
1898 }
1899 continue;
1900 case 'Q':
1901 rv |= Query(dv->devname);
1902 continue;
1903 case 'X':
1904 rv |= ExamineBitmap(dv->devname, c->brief, ss);
1905 continue;
1906 case ExamineBB:
1907 rv |= ExamineBadblocks(dv->devname, c->brief, ss);
1908 continue;
1909 case 'W':
1910 case WaitOpt:
1911 rv |= Wait(dv->devname);
1912 continue;
1913 case Waitclean:
1914 rv |= WaitClean(dv->devname, c->verbose);
1915 continue;
1916 case KillSubarray:
1917 rv |= Kill_subarray(dv->devname, c->subarray, c->verbose);
1918 continue;
1919 case UpdateSubarray:
1920 if (!c->update) {
1921 pr_err("-U/--update must be specified with --update-subarray\n");
1922 rv |= 1;
1923 continue;
1924 }
1925 rv |= Update_subarray(dv->devname, c->subarray,
1926 c->update, ident, c->verbose);
1927 continue;
1928 case Dump:
1929 rv |= Dump_metadata(dv->devname, dump_directory, c, ss);
1930 continue;
1931 case Restore:
1932 rv |= Restore_metadata(dv->devname, dump_directory, c, ss,
1933 (dv == devlist && dv->next == NULL));
1934 continue;
1935 case Action:
1936 rv |= SetAction(dv->devname, c->action);
1937 continue;
1938 }
1939
1940 if (dv->devname[0] != '/')
1941 mdfd = open_dev(dv->devname);
1942 if (dv->devname[0] == '/' || mdfd < 0)
1943 mdfd = open_mddev(dv->devname, 1);
1944
1945 if (mdfd >= 0) {
1946 switch(dv->disposition) {
1947 case 'R':
1948 c->runstop = 1;
1949 rv |= Manage_run(dv->devname, mdfd, c);
1950 break;
1951 case 'S':
1952 if (c->scan) {
1953 pr_err("--stop not meaningful with both a --scan assembly and a device name.\n");
1954 rv |= 1;
1955 break;
1956 }
1957 rv |= Manage_stop(dv->devname, mdfd, c->verbose, 0);
1958 break;
1959 case 'o':
1960 rv |= Manage_ro(dv->devname, mdfd, 1);
1961 break;
1962 case 'w':
1963 rv |= Manage_ro(dv->devname, mdfd, -1);
1964 break;
1965 }
1966 close(mdfd);
1967 } else
1968 rv |= 1;
1969 }
1970 return rv;
1971}
1972
1973int SetAction(char *dev, char *action)
1974{
1975 int fd = open(dev, O_RDONLY);
1976 struct mdinfo mdi;
1977 int retval;
1978
1979 if (fd < 0) {
1980 pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
1981 return 1;
1982 }
1983 retval = sysfs_init(&mdi, fd, NULL);
1984 close(fd);
1985 if (retval) {
1986 pr_err("%s is no an md array\n", dev);
1987 return 1;
1988 }
1989
1990 if (sysfs_set_str(&mdi, NULL, "sync_action", action) < 0) {
1991 pr_err("Count not set action for %s to %s: %s\n",
1992 dev, action, strerror(errno));
1993 return 1;
1994 }
1995 return 0;
1996}