]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdadm.c
mdadm: add the ability to change cluster name
[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 case O(ASSEMBLE, ClusterName):
602 c.homecluster = optarg;
603 if (strlen(c.homecluster) > 64) {
604 pr_err("Cluster name too big.\n");
605 exit(ERANGE);
606 }
607 continue;
608 case O(CREATE,'x'): /* number of spare (eXtra) disks */
609 if (s.sparedisks) {
610 pr_err("spare-devices set twice: %d and %s\n",
611 s.sparedisks, optarg);
612 exit(2);
613 }
614 if (s.level != UnSet && s.level <= 0 && s.level >= -1) {
615 pr_err("spare-devices setting is incompatible with raid level %d\n",
616 s.level);
617 exit(2);
618 }
619 s.sparedisks = parse_num(optarg);
620 if (s.sparedisks < 0) {
621 pr_err("invalid number of spare-devices: %s\n",
622 optarg);
623 exit(2);
624 }
625 continue;
626
627 case O(CREATE,'a'):
628 case O(CREATE,Auto):
629 case O(BUILD,'a'):
630 case O(BUILD,Auto):
631 case O(INCREMENTAL,'a'):
632 case O(INCREMENTAL,Auto):
633 case O(ASSEMBLE,'a'):
634 case O(ASSEMBLE,Auto): /* auto-creation of device node */
635 c.autof = parse_auto(optarg, "--auto flag", 0);
636 continue;
637
638 case O(CREATE,Symlinks):
639 case O(BUILD,Symlinks):
640 case O(ASSEMBLE,Symlinks): /* auto creation of symlinks in /dev to /dev/md */
641 symlinks = optarg;
642 continue;
643
644 case O(BUILD,'f'): /* force honouring '-n 1' */
645 case O(BUILD,Force): /* force honouring '-n 1' */
646 case O(GROW,'f'): /* ditto */
647 case O(GROW,Force): /* ditto */
648 case O(CREATE,'f'): /* force honouring of device list */
649 case O(CREATE,Force): /* force honouring of device list */
650 case O(ASSEMBLE,'f'): /* force assembly */
651 case O(ASSEMBLE,Force): /* force assembly */
652 case O(MISC,'f'): /* force zero */
653 case O(MISC,Force): /* force zero */
654 case O(MANAGE,Force): /* add device which is too large */
655 c.force=1;
656 continue;
657 /* now for the Assemble options */
658 case O(ASSEMBLE, FreezeReshape): /* Freeze reshape during
659 * initrd phase */
660 case O(INCREMENTAL, FreezeReshape):
661 c.freeze_reshape = 1;
662 continue;
663 case O(CREATE,'u'): /* uuid of array */
664 case O(ASSEMBLE,'u'): /* uuid of array */
665 if (ident.uuid_set) {
666 pr_err("uuid cannot be set twice. Second value %s.\n", optarg);
667 exit(2);
668 }
669 if (parse_uuid(optarg, ident.uuid))
670 ident.uuid_set = 1;
671 else {
672 pr_err("Bad uuid: %s\n", optarg);
673 exit(2);
674 }
675 continue;
676
677 case O(CREATE,'N'):
678 case O(ASSEMBLE,'N'):
679 case O(MISC,'N'):
680 if (ident.name[0]) {
681 pr_err("name cannot be set twice. Second value %s.\n", optarg);
682 exit(2);
683 }
684 if (mode == MISC && !c.subarray) {
685 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
686 exit(2);
687 }
688 if (strlen(optarg) > 32) {
689 pr_err("name '%s' is too long, 32 chars max.\n",
690 optarg);
691 exit(2);
692 }
693 strcpy(ident.name, optarg);
694 continue;
695
696 case O(ASSEMBLE,'m'): /* super-minor for array */
697 case O(ASSEMBLE,SuperMinor):
698 if (ident.super_minor != UnSet) {
699 pr_err("super-minor cannot be set twice. Second value: %s.\n", optarg);
700 exit(2);
701 }
702 if (strcmp(optarg, "dev")==0)
703 ident.super_minor = -2;
704 else {
705 ident.super_minor = parse_num(optarg);
706 if (ident.super_minor < 0) {
707 pr_err("Bad super-minor number: %s.\n", optarg);
708 exit(2);
709 }
710 }
711 continue;
712
713 case O(ASSEMBLE,'o'):
714 case O(MANAGE,'o'):
715 case O(CREATE,'o'):
716 c.readonly = 1;
717 continue;
718
719 case O(ASSEMBLE,'U'): /* update the superblock */
720 case O(MISC,'U'):
721 if (c.update) {
722 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
723 c.update, optarg);
724 exit(2);
725 }
726 if (mode == MISC && !c.subarray) {
727 pr_err("Only subarrays can be updated in misc mode\n");
728 exit(2);
729 }
730 c.update = optarg;
731 if (strcmp(c.update, "sparc2.2")==0)
732 continue;
733 if (strcmp(c.update, "super-minor") == 0)
734 continue;
735 if (strcmp(c.update, "summaries")==0)
736 continue;
737 if (strcmp(c.update, "resync")==0)
738 continue;
739 if (strcmp(c.update, "uuid")==0)
740 continue;
741 if (strcmp(c.update, "name")==0)
742 continue;
743 if (strcmp(c.update, "homehost")==0)
744 continue;
745 if (strcmp(c.update, "home-cluster")==0)
746 continue;
747 if (strcmp(c.update, "devicesize")==0)
748 continue;
749 if (strcmp(c.update, "no-bitmap")==0)
750 continue;
751 if (strcmp(c.update, "bbl") == 0)
752 continue;
753 if (strcmp(c.update, "no-bbl") == 0)
754 continue;
755 if (strcmp(c.update, "metadata") == 0)
756 continue;
757 if (strcmp(c.update, "revert-reshape") == 0)
758 continue;
759 if (strcmp(c.update, "byteorder")==0) {
760 if (ss) {
761 pr_err("must not set metadata type with --update=byteorder.\n");
762 exit(2);
763 }
764 for(i=0; !ss && superlist[i]; i++)
765 ss = superlist[i]->match_metadata_desc(
766 "0.swap");
767 if (!ss) {
768 pr_err("INTERNAL ERROR cannot find 0.swap\n");
769 exit(2);
770 }
771
772 continue;
773 }
774 if (strcmp(c.update,"?") == 0 ||
775 strcmp(c.update, "help") == 0) {
776 outf = stdout;
777 fprintf(outf, "%s: ", Name);
778 } else {
779 outf = stderr;
780 fprintf(outf,
781 "%s: '--update=%s' is invalid. ",
782 Name, c.update);
783 }
784 fprintf(outf, "Valid --update options are:\n"
785 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
786 " 'summaries', 'homehost', 'home-cluster', 'byteorder', 'devicesize',\n"
787 " 'no-bitmap', 'metadata', 'revert-reshape'\n"
788 " 'bbl', 'no-bbl'\n"
789 );
790 exit(outf == stdout ? 0 : 2);
791
792 case O(MANAGE,'U'):
793 /* update=devicesize is allowed with --re-add */
794 if (devmode != 'A') {
795 pr_err("--update in Manage mode only allowed with --re-add.\n");
796 exit(1);
797 }
798 if (c.update) {
799 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
800 c.update, optarg);
801 exit(2);
802 }
803 c.update = optarg;
804 if (strcmp(c.update, "devicesize") != 0 &&
805 strcmp(c.update, "bbl") != 0 &&
806 strcmp(c.update, "no-bbl") != 0) {
807 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be updated with --re-add\n");
808 exit(2);
809 }
810 continue;
811
812 case O(INCREMENTAL,NoDegraded):
813 pr_err("--no-degraded is deprecated in Incremental mode\n");
814 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
815 c.runstop = -1; /* --stop isn't allowed for --assemble,
816 * so we overload slightly */
817 continue;
818
819 case O(ASSEMBLE,'c'):
820 case O(ASSEMBLE,ConfigFile):
821 case O(INCREMENTAL, 'c'):
822 case O(INCREMENTAL, ConfigFile):
823 case O(MISC, 'c'):
824 case O(MISC, ConfigFile):
825 case O(MONITOR,'c'):
826 case O(MONITOR,ConfigFile):
827 case O(CREATE,ConfigFile):
828 if (configfile) {
829 pr_err("configfile cannot be set twice. Second value is %s.\n", optarg);
830 exit(2);
831 }
832 configfile = optarg;
833 set_conffile(configfile);
834 /* FIXME possibly check that config file exists. Even parse it */
835 continue;
836 case O(ASSEMBLE,'s'): /* scan */
837 case O(MISC,'s'):
838 case O(MONITOR,'s'):
839 case O(INCREMENTAL,'s'):
840 c.scan = 1;
841 continue;
842
843 case O(MONITOR,'m'): /* mail address */
844 case O(MONITOR,EMail):
845 if (mailaddr)
846 pr_err("only specify one mailaddress. %s ignored.\n",
847 optarg);
848 else
849 mailaddr = optarg;
850 continue;
851
852 case O(MONITOR,'p'): /* alert program */
853 case O(MONITOR,ProgramOpt): /* alert program */
854 if (program)
855 pr_err("only specify one alter program. %s ignored.\n",
856 optarg);
857 else
858 program = optarg;
859 continue;
860
861 case O(MONITOR,'r'): /* rebuild increments */
862 case O(MONITOR,Increment):
863 increments = atoi(optarg);
864 if (increments > 99 || increments < 1) {
865 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
866 exit(2);
867 }
868 continue;
869
870 case O(MONITOR,'d'): /* delay in seconds */
871 case O(GROW, 'd'):
872 case O(BUILD,'d'): /* delay for bitmap updates */
873 case O(CREATE,'d'):
874 if (c.delay)
875 pr_err("only specify delay once. %s ignored.\n",
876 optarg);
877 else {
878 c.delay = parse_num(optarg);
879 if (c.delay < 1) {
880 pr_err("invalid delay: %s\n",
881 optarg);
882 exit(2);
883 }
884 }
885 continue;
886 case O(MONITOR,'f'): /* daemonise */
887 case O(MONITOR,Fork):
888 daemonise = 1;
889 continue;
890 case O(MONITOR,'i'): /* pid */
891 if (pidfile)
892 pr_err("only specify one pid file. %s ignored.\n",
893 optarg);
894 else
895 pidfile = optarg;
896 continue;
897 case O(MONITOR,'1'): /* oneshot */
898 oneshot = 1;
899 spare_sharing = 0;
900 continue;
901 case O(MONITOR,'t'): /* test */
902 c.test = 1;
903 continue;
904 case O(MONITOR,'y'): /* log messages to syslog */
905 openlog("mdadm", LOG_PID, SYSLOG_FACILITY);
906 dosyslog = 1;
907 continue;
908 case O(MONITOR, NoSharing):
909 spare_sharing = 0;
910 continue;
911
912 /* now the general management options. Some are applicable
913 * to other modes. None have arguments.
914 */
915 case O(GROW,'a'):
916 case O(GROW,Add):
917 case O(MANAGE,'a'):
918 case O(MANAGE,Add): /* add a drive */
919 devmode = 'a';
920 continue;
921 case O(MANAGE,AddSpare): /* add drive - never re-add */
922 devmode = 'S';
923 continue;
924 case O(MANAGE,ReAdd):
925 devmode = 'A';
926 continue;
927 case O(MANAGE,'r'): /* remove a drive */
928 case O(MANAGE,Remove):
929 devmode = 'r';
930 continue;
931 case O(MANAGE,'f'): /* set faulty */
932 case O(MANAGE,Fail):
933 case O(INCREMENTAL,'f'):
934 case O(INCREMENTAL,Remove):
935 case O(INCREMENTAL,Fail): /* r for incremental is taken, use f
936 * even though we will both fail and
937 * remove the device */
938 devmode = 'f';
939 continue;
940 case O(MANAGE, ClusterConfirm):
941 devmode = 'c';
942 continue;
943 case O(MANAGE,Replace):
944 /* Mark these devices for replacement */
945 devmode = 'R';
946 continue;
947 case O(MANAGE,With):
948 /* These are the replacements to use */
949 if (devmode != 'R') {
950 pr_err("--with must follow --replace\n");
951 exit(2);
952 }
953 devmode = 'W';
954 continue;
955 case O(INCREMENTAL,'R'):
956 case O(MANAGE,'R'):
957 case O(ASSEMBLE,'R'):
958 case O(BUILD,'R'):
959 case O(CREATE,'R'): /* Run the array */
960 if (c.runstop < 0) {
961 pr_err("Cannot both Stop and Run an array\n");
962 exit(2);
963 }
964 c.runstop = 1;
965 continue;
966 case O(MANAGE,'S'):
967 if (c.runstop > 0) {
968 pr_err("Cannot both Run and Stop an array\n");
969 exit(2);
970 }
971 c.runstop = -1;
972 continue;
973 case O(MANAGE,'t'):
974 c.test = 1;
975 continue;
976
977 case O(MISC,'Q'):
978 case O(MISC,'D'):
979 case O(MISC,'E'):
980 case O(MISC,KillOpt):
981 case O(MISC,'R'):
982 case O(MISC,'S'):
983 case O(MISC,'X'):
984 case O(MISC, ExamineBB):
985 case O(MISC,'o'):
986 case O(MISC,'w'):
987 case O(MISC,'W'):
988 case O(MISC, WaitOpt):
989 case O(MISC, Waitclean):
990 case O(MISC, DetailPlatform):
991 case O(MISC, KillSubarray):
992 case O(MISC, UpdateSubarray):
993 case O(MISC, Dump):
994 case O(MISC, Restore):
995 case O(MISC ,Action):
996 if (opt == KillSubarray || opt == UpdateSubarray) {
997 if (c.subarray) {
998 pr_err("subarray can only be specified once\n");
999 exit(2);
1000 }
1001 c.subarray = optarg;
1002 }
1003 if (opt == Action) {
1004 if (c.action) {
1005 pr_err("Only one --action can be specified\n");
1006 exit(2);
1007 }
1008 if (strcmp(optarg, "idle") == 0 ||
1009 strcmp(optarg, "frozen") == 0 ||
1010 strcmp(optarg, "check") == 0 ||
1011 strcmp(optarg, "repair") == 0)
1012 c.action = optarg;
1013 else {
1014 pr_err("action must be one of idle, frozen, check, repair\n");
1015 exit(2);
1016 }
1017 }
1018 if (devmode && devmode != opt &&
1019 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
1020 pr_err("--examine/-E cannot be given with ");
1021 if (devmode == 'E') {
1022 if (option_index >= 0)
1023 fprintf(stderr, "--%s\n",
1024 long_options[option_index].name);
1025 else
1026 fprintf(stderr, "-%c\n", opt);
1027 } else if (isalpha(devmode))
1028 fprintf(stderr, "-%c\n", devmode);
1029 else
1030 fprintf(stderr, "previous option\n");
1031 exit(2);
1032 }
1033 devmode = opt;
1034 if (opt == Dump || opt == Restore) {
1035 if (dump_directory != NULL) {
1036 pr_err("dump/restore directory specified twice: %s and %s\n",
1037 dump_directory, optarg);
1038 exit(2);
1039 }
1040 dump_directory = optarg;
1041 }
1042 continue;
1043 case O(MISC, UdevRules):
1044 if (devmode && devmode != opt) {
1045 pr_err("--udev-rules must be the only option.\n");
1046 } else {
1047 if (udev_filename)
1048 pr_err("only specify one udev rule filename. %s ignored.\n",
1049 optarg);
1050 else
1051 udev_filename = optarg;
1052 }
1053 devmode = opt;
1054 continue;
1055 case O(MISC,'t'):
1056 c.test = 1;
1057 continue;
1058
1059 case O(MISC, Sparc22):
1060 if (devmode != 'E') {
1061 pr_err("--sparc2.2 only allowed with --examine\n");
1062 exit(2);
1063 }
1064 c.SparcAdjust = 1;
1065 continue;
1066
1067 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
1068 case O(ASSEMBLE,Bitmap):
1069 if (!optarg) {
1070 pr_err("bitmap file needed with -b in --assemble mode\n");
1071 exit(2);
1072 }
1073 if (strcmp(optarg, "internal")==0) {
1074 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1075 continue;
1076 }
1077 bitmap_fd = open(optarg, O_RDWR);
1078 if (!*optarg || bitmap_fd < 0) {
1079 pr_err("cannot open bitmap file %s: %s\n", optarg, strerror(errno));
1080 exit(2);
1081 }
1082 ident.bitmap_fd = bitmap_fd; /* for Assemble */
1083 continue;
1084
1085 case O(ASSEMBLE, BackupFile):
1086 case O(GROW, BackupFile):
1087 /* Specify a file into which grow might place a backup,
1088 * or from which assemble might recover a backup
1089 */
1090 if (c.backup_file) {
1091 pr_err("backup file already specified, rejecting %s\n", optarg);
1092 exit(2);
1093 }
1094 c.backup_file = optarg;
1095 continue;
1096
1097 case O(GROW, Continue):
1098 /* Continue interrupted grow
1099 */
1100 grow_continue = 1;
1101 continue;
1102 case O(ASSEMBLE, InvalidBackup):
1103 /* Acknowledge that the backupfile is invalid, but ask
1104 * to continue anyway
1105 */
1106 c.invalid_backup = 1;
1107 continue;
1108
1109 case O(BUILD,'b'):
1110 case O(BUILD,Bitmap):
1111 case O(CREATE,'b'):
1112 case O(CREATE,Bitmap): /* here we create the bitmap */
1113 case O(GROW,'b'):
1114 case O(GROW,Bitmap):
1115 if (strcmp(optarg, "internal")== 0 ||
1116 strcmp(optarg, "none")== 0 ||
1117 strchr(optarg, '/') != NULL) {
1118 s.bitmap_file = optarg;
1119 continue;
1120 }
1121 if (strcmp(optarg, "clustered")== 0) {
1122 s.bitmap_file = optarg;
1123 /* Set the default number of cluster nodes
1124 * to 4 if not already set by user
1125 */
1126 if (c.nodes < 1)
1127 c.nodes = 4;
1128 continue;
1129 }
1130 /* probable typo */
1131 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1132 " not '%s'\n", optarg);
1133 exit(2);
1134
1135 case O(GROW,BitmapChunk):
1136 case O(BUILD,BitmapChunk):
1137 case O(CREATE,BitmapChunk): /* bitmap chunksize */
1138 s.bitmap_chunk = parse_size(optarg);
1139 if (s.bitmap_chunk == 0 ||
1140 s.bitmap_chunk == INVALID_SECTORS ||
1141 s.bitmap_chunk & (s.bitmap_chunk - 1)) {
1142 pr_err("invalid bitmap chunksize: %s\n",
1143 optarg);
1144 exit(2);
1145 }
1146 s.bitmap_chunk = s.bitmap_chunk * 512;
1147 continue;
1148
1149 case O(GROW, WriteBehind):
1150 case O(BUILD, WriteBehind):
1151 case O(CREATE, WriteBehind): /* write-behind mode */
1152 s.write_behind = DEFAULT_MAX_WRITE_BEHIND;
1153 if (optarg) {
1154 s.write_behind = parse_num(optarg);
1155 if (s.write_behind < 0 ||
1156 s.write_behind > 16383) {
1157 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
1158 exit(2);
1159 }
1160 }
1161 continue;
1162
1163 case O(INCREMENTAL, 'r'):
1164 case O(INCREMENTAL, RebuildMapOpt):
1165 rebuild_map = 1;
1166 continue;
1167 case O(INCREMENTAL, IncrementalPath):
1168 remove_path = optarg;
1169 continue;
1170 }
1171 /* We have now processed all the valid options. Anything else is
1172 * an error
1173 */
1174 if (option_index > 0)
1175 pr_err(":option --%s not valid in %s mode\n",
1176 long_options[option_index].name,
1177 map_num(modes, mode));
1178 else
1179 pr_err("option -%c not valid in %s mode\n",
1180 opt, map_num(modes, mode));
1181 exit(2);
1182
1183 }
1184
1185 if (print_help) {
1186 char *help_text;
1187 if (print_help == 2)
1188 help_text = OptionHelp;
1189 else
1190 help_text = mode_help[mode];
1191 if (help_text == NULL)
1192 help_text = Help;
1193 fputs(help_text,stdout);
1194 exit(0);
1195 }
1196
1197 if (!mode && devs_found) {
1198 mode = MISC;
1199 devmode = 'Q';
1200 if (devlist->disposition == 0)
1201 devlist->disposition = devmode;
1202 }
1203 if (!mode) {
1204 fputs(Usage, stderr);
1205 exit(2);
1206 }
1207
1208 if (symlinks) {
1209 struct createinfo *ci = conf_get_create_info();
1210
1211 if (strcasecmp(symlinks, "yes") == 0)
1212 ci->symlinks = 1;
1213 else if (strcasecmp(symlinks, "no") == 0)
1214 ci->symlinks = 0;
1215 else {
1216 pr_err("option --symlinks must be 'no' or 'yes'\n");
1217 exit(2);
1218 }
1219 }
1220 /* Ok, got the option parsing out of the way
1221 * hopefully it's mostly right but there might be some stuff
1222 * missing
1223 *
1224 * That is mosty checked in the per-mode stuff but...
1225 *
1226 * For @,B,C and A without -s, the first device listed must be
1227 * an md device. We check that here and open it.
1228 */
1229
1230 if (mode == MANAGE || mode == BUILD || mode == CREATE
1231 || mode == GROW
1232 || (mode == ASSEMBLE && ! c.scan)) {
1233 if (devs_found < 1) {
1234 pr_err("an md device must be given in this mode\n");
1235 exit(2);
1236 }
1237 if ((int)ident.super_minor == -2 && c.autof) {
1238 pr_err("--super-minor=dev is incompatible with --auto\n");
1239 exit(2);
1240 }
1241 if (mode == MANAGE || mode == GROW) {
1242 mdfd = open_mddev(devlist->devname, 1);
1243 if (mdfd < 0)
1244 exit(1);
1245 } else
1246 /* non-existent device is OK */
1247 mdfd = open_mddev(devlist->devname, 0);
1248 if (mdfd == -2) {
1249 pr_err("device %s exists but is not an md array.\n", devlist->devname);
1250 exit(1);
1251 }
1252 if ((int)ident.super_minor == -2) {
1253 struct stat stb;
1254 if (mdfd < 0) {
1255 pr_err("--super-minor=dev given, and listed device %s doesn't exist.\n",
1256 devlist->devname);
1257 exit(1);
1258 }
1259 fstat(mdfd, &stb);
1260 ident.super_minor = minor(stb.st_rdev);
1261 }
1262 if (mdfd >= 0 && mode != MANAGE && mode != GROW) {
1263 /* We don't really want this open yet, we just might
1264 * have wanted to check some things
1265 */
1266 close(mdfd);
1267 mdfd = -1;
1268 }
1269 }
1270
1271 if (s.raiddisks) {
1272 if (s.raiddisks == 1 && !c.force && s.level != LEVEL_FAULTY) {
1273 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1274 " a mistake. If you really mean it you will need to specify --force before\n"
1275 " setting the number of drives.\n");
1276 exit(2);
1277 }
1278 }
1279
1280 if (c.homehost == NULL)
1281 c.homehost = conf_get_homehost(&c.require_homehost);
1282 if (c.homehost == NULL || strcasecmp(c.homehost, "<system>")==0) {
1283 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
1284 sys_hostname[sizeof(sys_hostname)-1] = 0;
1285 c.homehost = sys_hostname;
1286 }
1287 }
1288 if (c.homehost && (!c.homehost[0] || strcasecmp(c.homehost, "<none>") == 0)) {
1289 c.homehost = NULL;
1290 c.require_homehost = 0;
1291 }
1292
1293 if (c.homecluster == NULL && (c.nodes > 0)) {
1294 c.homecluster = conf_get_homecluster();
1295 if (c.homecluster == NULL)
1296 rv = get_cluster_name(&c.homecluster);
1297 if (rv != 0) {
1298 pr_err("The md can't get cluster name\n");
1299 exit(1);
1300 }
1301 }
1302
1303 if (c.backup_file && data_offset != INVALID_SECTORS) {
1304 pr_err("--backup-file and --data-offset are incompatible\n");
1305 exit(2);
1306 }
1307
1308 if ((mode == MISC && devmode == 'E')
1309 || (mode == MONITOR && spare_sharing == 0))
1310 /* Anyone may try this */;
1311 else if (geteuid() != 0) {
1312 pr_err("must be super-user to perform this action\n");
1313 exit(1);
1314 }
1315
1316 ident.autof = c.autof;
1317
1318 if (c.scan && c.verbose < 2)
1319 /* --scan implied --brief unless -vv */
1320 c.brief = 1;
1321
1322 rv = 0;
1323 switch(mode) {
1324 case MANAGE:
1325 /* readonly, add/remove, readwrite, runstop */
1326 if (c.readonly > 0)
1327 rv = Manage_ro(devlist->devname, mdfd, c.readonly);
1328 if (!rv && devs_found>1)
1329 rv = Manage_subdevs(devlist->devname, mdfd,
1330 devlist->next, c.verbose, c.test,
1331 c.update, c.force);
1332 if (!rv && c.readonly < 0)
1333 rv = Manage_ro(devlist->devname, mdfd, c.readonly);
1334 if (!rv && c.runstop > 0)
1335 rv = Manage_run(devlist->devname, mdfd, &c);
1336 if (!rv && c.runstop < 0)
1337 rv = Manage_stop(devlist->devname, mdfd, c.verbose, 0);
1338 break;
1339 case ASSEMBLE:
1340 if (devs_found == 1 && ident.uuid_set == 0 &&
1341 ident.super_minor == UnSet && ident.name[0] == 0 && !c.scan ) {
1342 /* Only a device has been given, so get details from config file */
1343 struct mddev_ident *array_ident = conf_get_ident(devlist->devname);
1344 if (array_ident == NULL) {
1345 pr_err("%s not identified in config file.\n",
1346 devlist->devname);
1347 rv |= 1;
1348 if (mdfd >= 0)
1349 close(mdfd);
1350 } else {
1351 if (array_ident->autof == 0)
1352 array_ident->autof = c.autof;
1353 rv |= Assemble(ss, devlist->devname, array_ident,
1354 NULL, &c);
1355 }
1356 } else if (!c.scan)
1357 rv = Assemble(ss, devlist->devname, &ident,
1358 devlist->next, &c);
1359 else if (devs_found > 0) {
1360 if (c.update && devs_found > 1) {
1361 pr_err("can only update a single array at a time\n");
1362 exit(1);
1363 }
1364 if (c.backup_file && devs_found > 1) {
1365 pr_err("can only assemble a single array when providing a backup file.\n");
1366 exit(1);
1367 }
1368 for (dv = devlist ; dv ; dv=dv->next) {
1369 struct mddev_ident *array_ident = conf_get_ident(dv->devname);
1370 if (array_ident == NULL) {
1371 pr_err("%s not identified in config file.\n",
1372 dv->devname);
1373 rv |= 1;
1374 continue;
1375 }
1376 if (array_ident->autof == 0)
1377 array_ident->autof = c.autof;
1378 rv |= Assemble(ss, dv->devname, array_ident,
1379 NULL, &c);
1380 }
1381 } else {
1382 if (c.update) {
1383 pr_err("--update not meaningful with a --scan assembly.\n");
1384 exit(1);
1385 }
1386 if (c.backup_file) {
1387 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1388 exit(1);
1389 }
1390 rv = scan_assemble(ss, &c, &ident);
1391 }
1392
1393 break;
1394 case BUILD:
1395 if (c.delay == 0)
1396 c.delay = DEFAULT_BITMAP_DELAY;
1397 if (s.write_behind && !s.bitmap_file) {
1398 pr_err("write-behind mode requires a bitmap.\n");
1399 rv = 1;
1400 break;
1401 }
1402 if (s.raiddisks == 0) {
1403 pr_err("no raid-devices specified.\n");
1404 rv = 1;
1405 break;
1406 }
1407
1408 if (s.bitmap_file) {
1409 if (strcmp(s.bitmap_file, "internal")==0) {
1410 pr_err("'internal' bitmaps not supported with --build\n");
1411 rv |= 1;
1412 break;
1413 }
1414 }
1415 rv = Build(devlist->devname, devlist->next, &s, &c);
1416 break;
1417 case CREATE:
1418 if (c.delay == 0)
1419 c.delay = DEFAULT_BITMAP_DELAY;
1420
1421 if (c.nodes) {
1422 if (!s.bitmap_file || strcmp(s.bitmap_file, "clustered") != 0) {
1423 pr_err("--nodes argument only compatible with --bitmap=clustered\n");
1424 rv = 1;
1425 break;
1426 }
1427
1428 if (s.level != 1) {
1429 pr_err("--bitmap=clustered is currently supported with RAID mirror only\n");
1430 rv = 1;
1431 break;
1432 }
1433 }
1434
1435 if (s.write_behind && !s.bitmap_file) {
1436 pr_err("write-behind mode requires a bitmap.\n");
1437 rv = 1;
1438 break;
1439 }
1440 if (s.raiddisks == 0) {
1441 pr_err("no raid-devices specified.\n");
1442 rv = 1;
1443 break;
1444 }
1445
1446 rv = Create(ss, devlist->devname,
1447 ident.name, ident.uuid_set ? ident.uuid : NULL,
1448 devs_found-1, devlist->next,
1449 &s, &c, data_offset);
1450 break;
1451 case MISC:
1452 if (devmode == 'E') {
1453 if (devlist == NULL && !c.scan) {
1454 pr_err("No devices to examine\n");
1455 exit(2);
1456 }
1457 if (devlist == NULL)
1458 devlist = conf_get_devs();
1459 if (devlist == NULL) {
1460 pr_err("No devices listed in %s\n", configfile?configfile:DefaultConfFile);
1461 exit(1);
1462 }
1463 rv = Examine(devlist, &c, ss);
1464 } else if (devmode == DetailPlatform) {
1465 rv = Detail_Platform(ss ? ss->ss : NULL, ss ? c.scan : 1,
1466 c.verbose, c.export,
1467 devlist ? devlist->devname : NULL);
1468 } else if (devlist == NULL) {
1469 if (devmode == 'S' && c.scan)
1470 rv = stop_scan(c.verbose);
1471 else if ((devmode == 'D' || devmode == Waitclean) && c.scan)
1472 rv = misc_scan(devmode, &c);
1473 else if (devmode == UdevRules)
1474 rv = Write_rules(udev_filename);
1475 else {
1476 pr_err("No devices given.\n");
1477 exit(2);
1478 }
1479 } else
1480 rv = misc_list(devlist, &ident, dump_directory, ss, &c);
1481 break;
1482 case MONITOR:
1483 if (!devlist && !c.scan) {
1484 pr_err("Cannot monitor: need --scan or at least one device\n");
1485 rv = 1;
1486 break;
1487 }
1488 if (pidfile && !daemonise) {
1489 pr_err("Cannot write a pid file when not in daemon mode\n");
1490 rv = 1;
1491 break;
1492 }
1493 if (c.delay == 0) {
1494 if (get_linux_version() > 2006016)
1495 /* mdstat responds to poll */
1496 c.delay = 1000;
1497 else
1498 c.delay = 60;
1499 }
1500 if (c.delay == 0)
1501 c.delay = 60;
1502 rv= Monitor(devlist, mailaddr, program,
1503 &c, daemonise, oneshot,
1504 dosyslog, pidfile, increments,
1505 spare_sharing);
1506 break;
1507
1508 case GROW:
1509 if (array_size > 0) {
1510 /* alway impose array size first, independent of
1511 * anything else
1512 * Do not allow level or raid_disks changes at the
1513 * same time as that can be irreversibly destructive.
1514 */
1515 struct mdinfo sra;
1516 int err;
1517 if (s.raiddisks || s.level != UnSet) {
1518 pr_err("cannot change array size in same operation as changing raiddisks or level.\n"
1519 " Change size first, then check that data is still intact.\n");
1520 rv = 1;
1521 break;
1522 }
1523 sysfs_init(&sra, mdfd, NULL);
1524 if (array_size == MAX_SIZE)
1525 err = sysfs_set_str(&sra, NULL, "array_size", "default");
1526 else
1527 err = sysfs_set_num(&sra, NULL, "array_size", array_size / 2);
1528 if (err < 0) {
1529 if (errno == E2BIG)
1530 pr_err("--array-size setting is too large.\n");
1531 else
1532 pr_err("current kernel does not support setting --array-size\n");
1533 rv = 1;
1534 break;
1535 }
1536 }
1537 if (devs_found > 1 && s.raiddisks == 0 && s.level == UnSet) {
1538 /* must be '-a'. */
1539 if (s.size > 0 || s.chunk || s.layout_str != NULL || s.bitmap_file) {
1540 pr_err("--add cannot be used with other geometry changes in --grow mode\n");
1541 rv = 1;
1542 break;
1543 }
1544 for (dv=devlist->next; dv ; dv=dv->next) {
1545 rv = Grow_Add_device(devlist->devname, mdfd,
1546 dv->devname);
1547 if (rv)
1548 break;
1549 }
1550 } else if (s.bitmap_file) {
1551 if (s.size > 0 || s.raiddisks || s.chunk ||
1552 s.layout_str != NULL || devs_found > 1) {
1553 pr_err("--bitmap changes cannot be used with other geometry changes in --grow mode\n");
1554 rv = 1;
1555 break;
1556 }
1557 if (c.delay == 0)
1558 c.delay = DEFAULT_BITMAP_DELAY;
1559 rv = Grow_addbitmap(devlist->devname, mdfd, &c, &s);
1560 } else if (grow_continue)
1561 rv = Grow_continue_command(devlist->devname,
1562 mdfd, c.backup_file,
1563 c.verbose);
1564 else if (s.size > 0 || s.raiddisks || s.layout_str != NULL
1565 || s.chunk != 0 || s.level != UnSet
1566 || data_offset != INVALID_SECTORS) {
1567 rv = Grow_reshape(devlist->devname, mdfd,
1568 devlist->next,
1569 data_offset, &c, &s);
1570 } else if (array_size == 0)
1571 pr_err("no changes to --grow\n");
1572 break;
1573 case INCREMENTAL:
1574 if (rebuild_map) {
1575 RebuildMap();
1576 }
1577 if (c.scan) {
1578 rv = 1;
1579 if (devlist) {
1580 pr_err("In --incremental mode, a device cannot be given with --scan.\n");
1581 break;
1582 }
1583 if (c.runstop <= 0) {
1584 pr_err("--incremental --scan meaningless without --run.\n");
1585 break;
1586 }
1587 if (devmode == 'f') {
1588 pr_err("--incremental --scan --fail not supported.\n");
1589 break;
1590 }
1591 rv = IncrementalScan(&c, NULL);
1592 }
1593 if (!devlist) {
1594 if (!rebuild_map && !c.scan) {
1595 pr_err("--incremental requires a device.\n");
1596 rv = 1;
1597 }
1598 break;
1599 }
1600 if (devmode == 'f') {
1601 if (devlist->next) {
1602 pr_err("'--incremental --fail' can only handle one device.\n");
1603 rv = 1;
1604 break;
1605 }
1606 rv = IncrementalRemove(devlist->devname, remove_path,
1607 c.verbose);
1608 } else
1609 rv = Incremental(devlist, &c, ss);
1610 break;
1611 case AUTODETECT:
1612 autodetect();
1613 break;
1614 }
1615 exit(rv);
1616 }
1617
1618 static int scan_assemble(struct supertype *ss,
1619 struct context *c,
1620 struct mddev_ident *ident)
1621 {
1622 struct mddev_ident *a, *array_list = conf_get_ident(NULL);
1623 struct mddev_dev *devlist = conf_get_devs();
1624 struct map_ent *map = NULL;
1625 int cnt = 0;
1626 int rv = 0;
1627 int failures, successes;
1628
1629 if (conf_verify_devnames(array_list)) {
1630 pr_err("Duplicate MD device names in conf file were found.\n");
1631 return 1;
1632 }
1633 if (devlist == NULL) {
1634 pr_err("No devices listed in conf file were found.\n");
1635 return 1;
1636 }
1637 for (a = array_list; a ; a = a->next) {
1638 a->assembled = 0;
1639 if (a->autof == 0)
1640 a->autof = c->autof;
1641 }
1642 if (map_lock(&map))
1643 pr_err("failed to get exclusive lock on mapfile\n");
1644 do {
1645 failures = 0;
1646 successes = 0;
1647 rv = 0;
1648 for (a = array_list; a ; a = a->next) {
1649 int r;
1650 if (a->assembled)
1651 continue;
1652 if (a->devname &&
1653 strcasecmp(a->devname, "<ignore>") == 0)
1654 continue;
1655
1656 r = Assemble(ss, a->devname,
1657 a, NULL, c);
1658 if (r == 0) {
1659 a->assembled = 1;
1660 successes++;
1661 } else
1662 failures++;
1663 rv |= r;
1664 cnt++;
1665 }
1666 } while (failures && successes);
1667 if (c->homehost && cnt == 0) {
1668 /* Maybe we can auto-assemble something.
1669 * Repeatedly call Assemble in auto-assemble mode
1670 * until it fails
1671 */
1672 int rv2;
1673 int acnt;
1674 ident->autof = c->autof;
1675 do {
1676 struct mddev_dev *devlist = conf_get_devs();
1677 acnt = 0;
1678 do {
1679 rv2 = Assemble(ss, NULL,
1680 ident,
1681 devlist, c);
1682 if (rv2==0) {
1683 cnt++;
1684 acnt++;
1685 }
1686 } while (rv2!=2);
1687 /* Incase there are stacked devices, we need to go around again */
1688 } while (acnt);
1689 if (cnt == 0 && rv == 0) {
1690 pr_err("No arrays found in config file or automatically\n");
1691 rv = 1;
1692 } else if (cnt)
1693 rv = 0;
1694 } else if (cnt == 0 && rv == 0) {
1695 pr_err("No arrays found in config file\n");
1696 rv = 1;
1697 }
1698 map_unlock(&map);
1699 return rv;
1700 }
1701
1702 static int misc_scan(char devmode, struct context *c)
1703 {
1704 /* apply --detail or --wait-clean to
1705 * all devices in /proc/mdstat
1706 */
1707 struct mdstat_ent *ms = mdstat_read(0, 1);
1708 struct mdstat_ent *e;
1709 struct map_ent *map = NULL;
1710 int members;
1711 int rv = 0;
1712
1713 for (members = 0; members <= 1; members++) {
1714 for (e=ms ; e ; e=e->next) {
1715 char *name = NULL;
1716 struct map_ent *me;
1717 struct stat stb;
1718 int member = e->metadata_version &&
1719 strncmp(e->metadata_version,
1720 "external:/", 10) == 0;
1721 if (members != member)
1722 continue;
1723 me = map_by_devnm(&map, e->devnm);
1724 if (me && me->path
1725 && strcmp(me->path, "/unknown") != 0)
1726 name = me->path;
1727 if (name == NULL ||
1728 stat(name, &stb) != 0)
1729 name = get_md_name(e->devnm);
1730
1731 if (!name) {
1732 pr_err("cannot find device file for %s\n",
1733 e->dev);
1734 continue;
1735 }
1736 if (devmode == 'D')
1737 rv |= Detail(name, c);
1738 else
1739 rv |= WaitClean(name, -1, c->verbose);
1740 put_md_name(name);
1741 }
1742 }
1743 free_mdstat(ms);
1744 return rv;
1745 }
1746
1747 static int stop_scan(int verbose)
1748 {
1749 /* apply --stop to all devices in /proc/mdstat */
1750 /* Due to possible stacking of devices, repeat until
1751 * nothing more can be stopped
1752 */
1753 int progress=1, err;
1754 int last = 0;
1755 int rv = 0;
1756 do {
1757 struct mdstat_ent *ms = mdstat_read(0, 0);
1758 struct mdstat_ent *e;
1759
1760 if (!progress) last = 1;
1761 progress = 0; err = 0;
1762 for (e=ms ; e ; e=e->next) {
1763 char *name = get_md_name(e->devnm);
1764 int mdfd;
1765
1766 if (!name) {
1767 pr_err("cannot find device file for %s\n",
1768 e->dev);
1769 continue;
1770 }
1771 mdfd = open_mddev(name, 1);
1772 if (mdfd >= 0) {
1773 if (Manage_stop(name, mdfd, verbose, !last))
1774 err = 1;
1775 else
1776 progress = 1;
1777 close(mdfd);
1778 }
1779
1780 put_md_name(name);
1781 }
1782 free_mdstat(ms);
1783 } while (!last && err);
1784 if (err)
1785 rv |= 1;
1786 return rv;
1787 }
1788
1789 static int misc_list(struct mddev_dev *devlist,
1790 struct mddev_ident *ident,
1791 char *dump_directory,
1792 struct supertype *ss, struct context *c)
1793 {
1794 struct mddev_dev *dv;
1795 int rv = 0;
1796
1797 for (dv=devlist ; dv; dv=(rv & 16) ? NULL : dv->next) {
1798 int mdfd;
1799
1800 switch(dv->disposition) {
1801 case 'D':
1802 rv |= Detail(dv->devname, c);
1803 continue;
1804 case KillOpt: /* Zero superblock */
1805 if (ss)
1806 rv |= Kill(dv->devname, ss, c->force, c->verbose,0);
1807 else {
1808 int v = c->verbose;
1809 do {
1810 rv |= Kill(dv->devname, NULL, c->force, v, 0);
1811 v = -1;
1812 } while (rv == 0);
1813 rv &= ~2;
1814 }
1815 continue;
1816 case 'Q':
1817 rv |= Query(dv->devname); continue;
1818 case 'X':
1819 rv |= ExamineBitmap(dv->devname, c->brief, ss); continue;
1820 case ExamineBB:
1821 rv |= ExamineBadblocks(dv->devname, c->brief, ss); continue;
1822 case 'W':
1823 case WaitOpt:
1824 rv |= Wait(dv->devname); continue;
1825 case Waitclean:
1826 rv |= WaitClean(dv->devname, -1, c->verbose); continue;
1827 case KillSubarray:
1828 rv |= Kill_subarray(dv->devname, c->subarray, c->verbose);
1829 continue;
1830 case UpdateSubarray:
1831 if (c->update == NULL) {
1832 pr_err("-U/--update must be specified with --update-subarray\n");
1833 rv |= 1;
1834 continue;
1835 }
1836 rv |= Update_subarray(dv->devname, c->subarray,
1837 c->update, ident, c->verbose);
1838 continue;
1839 case Dump:
1840 rv |= Dump_metadata(dv->devname, dump_directory, c, ss);
1841 continue;
1842 case Restore:
1843 rv |= Restore_metadata(dv->devname, dump_directory, c, ss,
1844 (dv == devlist && dv->next == NULL));
1845 continue;
1846 case Action:
1847 rv |= SetAction(dv->devname, c->action);
1848 continue;
1849 }
1850 if (dv->devname[0] == '/')
1851 mdfd = open_mddev(dv->devname, 1);
1852 else {
1853 mdfd = open_dev(dv->devname);
1854 if (mdfd < 0)
1855 pr_err("Cannot open %s\n", dv->devname);
1856 }
1857 if (mdfd>=0) {
1858 switch(dv->disposition) {
1859 case 'R':
1860 c->runstop = 1;
1861 rv |= Manage_run(dv->devname, mdfd, c); break;
1862 case 'S':
1863 rv |= Manage_stop(dv->devname, mdfd, c->verbose, 0); break;
1864 case 'o':
1865 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1866 case 'w':
1867 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1868 }
1869 close(mdfd);
1870 } else
1871 rv |= 1;
1872 }
1873 return rv;
1874 }
1875
1876 int SetAction(char *dev, char *action)
1877 {
1878 int fd = open(dev, O_RDONLY);
1879 struct mdinfo mdi;
1880 if (fd < 0) {
1881 pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
1882 return 1;
1883 }
1884 sysfs_init(&mdi, fd, NULL);
1885 close(fd);
1886 if (!mdi.sys_name[0]) {
1887 pr_err("%s is no an md array\n", dev);
1888 return 1;
1889 }
1890
1891 if (sysfs_set_str(&mdi, NULL, "sync_action", action) < 0) {
1892 pr_err("Count not set action for %s to %s: %s\n",
1893 dev, action, strerror(errno));
1894 return 1;
1895 }
1896 return 0;
1897 }