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