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