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