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