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