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