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