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