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