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