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