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