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