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