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