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