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