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