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