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