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