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