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