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