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