]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdadm.c
Assemble: --update=metadata converts v0.90 to v1.0
[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 cont_err("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 pr_err("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 pr_err("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, "metadata") == 0)
759 continue;
760 if (strcmp(c.update, "byteorder")==0) {
761 if (ss) {
762 pr_err("must not set metadata"
763 " type with --update=byteorder.\n");
764 exit(2);
765 }
766 for(i=0; !ss && superlist[i]; i++)
767 ss = superlist[i]->match_metadata_desc(
768 "0.swap");
769 if (!ss) {
770 pr_err("INTERNAL ERROR"
771 " cannot find 0.swap\n");
772 exit(2);
773 }
774
775 continue;
776 }
777 if (strcmp(c.update,"?") == 0 ||
778 strcmp(c.update, "help") == 0) {
779 outf = stdout;
780 fprintf(outf, Name ": ");
781 } else {
782 outf = stderr;
783 fprintf(outf,
784 Name ": '--update=%s' is invalid. ",
785 c.update);
786 }
787 fprintf(outf, "Valid --update options are:\n"
788 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
789 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
790 " 'no-bitmap', 'metadata'\n");
791 exit(outf == stdout ? 0 : 2);
792
793 case O(MANAGE,'U'):
794 /* update=devicesize is allowed with --re-add */
795 if (devmode != 'A') {
796 pr_err("--update in Manage mode only"
797 " allowed with --re-add.\n");
798 exit(1);
799 }
800 if (c.update) {
801 pr_err("Can only update one aspect"
802 " of superblock, both %s and %s given.\n",
803 c.update, optarg);
804 exit(2);
805 }
806 c.update = optarg;
807 if (strcmp(c.update, "devicesize") != 0 &&
808 strcmp(c.update, "bbl") != 0 &&
809 strcmp(c.update, "no-bbl") != 0) {
810 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be"
811 " updated with --re-add\n");
812 exit(2);
813 }
814 continue;
815
816 case O(INCREMENTAL,NoDegraded):
817 pr_err("--no-degraded is deprecated in Incremental mode\n");
818 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
819 c.runstop = -1; /* --stop isn't allowed for --assemble,
820 * so we overload slightly */
821 continue;
822
823 case O(ASSEMBLE,'c'):
824 case O(ASSEMBLE,ConfigFile):
825 case O(INCREMENTAL, 'c'):
826 case O(INCREMENTAL, ConfigFile):
827 case O(MISC, 'c'):
828 case O(MISC, ConfigFile):
829 case O(MONITOR,'c'):
830 case O(MONITOR,ConfigFile):
831 if (configfile) {
832 pr_err("configfile cannot be set twice. "
833 "Second value is %s.\n", optarg);
834 exit(2);
835 }
836 configfile = optarg;
837 set_conffile(configfile);
838 /* FIXME possibly check that config file exists. Even parse it */
839 continue;
840 case O(ASSEMBLE,'s'): /* scan */
841 case O(MISC,'s'):
842 case O(MONITOR,'s'):
843 case O(INCREMENTAL,'s'):
844 c.scan = 1;
845 continue;
846
847 case O(MONITOR,'m'): /* mail address */
848 case O(MONITOR,EMail):
849 if (mailaddr)
850 pr_err("only specify one mailaddress. %s ignored.\n",
851 optarg);
852 else
853 mailaddr = optarg;
854 continue;
855
856 case O(MONITOR,'p'): /* alert program */
857 case O(MONITOR,ProgramOpt): /* alert program */
858 if (program)
859 pr_err("only specify one alter program. %s ignored.\n",
860 optarg);
861 else
862 program = optarg;
863 continue;
864
865 case O(MONITOR,'r'): /* rebuild increments */
866 case O(MONITOR,Increment):
867 increments = atoi(optarg);
868 if (increments > 99 || increments < 1) {
869 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
870 exit(2);
871 }
872 continue;
873
874 case O(MONITOR,'d'): /* delay in seconds */
875 case O(GROW, 'd'):
876 case O(BUILD,'d'): /* delay for bitmap updates */
877 case O(CREATE,'d'):
878 if (c.delay)
879 pr_err("only specify delay once. %s ignored.\n",
880 optarg);
881 else {
882 c.delay = parse_num(optarg);
883 if (c.delay < 1) {
884 pr_err("invalid delay: %s\n",
885 optarg);
886 exit(2);
887 }
888 }
889 continue;
890 case O(MONITOR,'f'): /* daemonise */
891 case O(MONITOR,Fork):
892 daemonise = 1;
893 continue;
894 case O(MONITOR,'i'): /* pid */
895 if (pidfile)
896 pr_err("only specify one pid file. %s ignored.\n",
897 optarg);
898 else
899 pidfile = optarg;
900 continue;
901 case O(MONITOR,'1'): /* oneshot */
902 oneshot = 1;
903 spare_sharing = 0;
904 continue;
905 case O(MONITOR,'t'): /* test */
906 c.test = 1;
907 continue;
908 case O(MONITOR,'y'): /* log messages to syslog */
909 openlog("mdadm", LOG_PID, SYSLOG_FACILITY);
910 dosyslog = 1;
911 continue;
912 case O(MONITOR, NoSharing):
913 spare_sharing = 0;
914 continue;
915
916 /* now the general management options. Some are applicable
917 * to other modes. None have arguments.
918 */
919 case O(GROW,'a'):
920 case O(GROW,Add):
921 case O(MANAGE,'a'):
922 case O(MANAGE,Add): /* add a drive */
923 devmode = 'a';
924 continue;
925 case O(MANAGE,ReAdd):
926 devmode = 'A';
927 continue;
928 case O(MANAGE,'r'): /* remove a drive */
929 case O(MANAGE,Remove):
930 devmode = 'r';
931 continue;
932 case O(MANAGE,'f'): /* set faulty */
933 case O(MANAGE,Fail):
934 case O(INCREMENTAL,'f'):
935 case O(INCREMENTAL,Remove):
936 case O(INCREMENTAL,Fail): /* r for incremental is taken, use f
937 * even though we will both fail and
938 * remove the device */
939 devmode = 'f';
940 continue;
941 case O(MANAGE,Replace):
942 /* Mark these devices for replacement */
943 devmode = 'R';
944 continue;
945 case O(MANAGE,With):
946 /* These are the replacements to use */
947 if (devmode != 'R') {
948 pr_err("--with must follow --replace\n");
949 exit(2);
950 }
951 devmode = 'W';
952 continue;
953 case O(INCREMENTAL,'R'):
954 case O(MANAGE,'R'):
955 case O(ASSEMBLE,'R'):
956 case O(BUILD,'R'):
957 case O(CREATE,'R'): /* Run the array */
958 if (c.runstop < 0) {
959 pr_err("Cannot both Stop and Run an array\n");
960 exit(2);
961 }
962 c.runstop = 1;
963 continue;
964 case O(MANAGE,'S'):
965 if (c.runstop > 0) {
966 pr_err("Cannot both Run and Stop an array\n");
967 exit(2);
968 }
969 c.runstop = -1;
970 continue;
971 case O(MANAGE,'t'):
972 c.test = 1;
973 continue;
974
975 case O(MISC,'Q'):
976 case O(MISC,'D'):
977 case O(MISC,'E'):
978 case O(MISC,KillOpt):
979 case O(MISC,'R'):
980 case O(MISC,'S'):
981 case O(MISC,'X'):
982 case O(MISC, ExamineBB):
983 case O(MISC,'o'):
984 case O(MISC,'w'):
985 case O(MISC,'W'):
986 case O(MISC, WaitOpt):
987 case O(MISC, Waitclean):
988 case O(MISC, DetailPlatform):
989 case O(MISC, KillSubarray):
990 case O(MISC, UpdateSubarray):
991 case O(MISC, Dump):
992 case O(MISC, Restore):
993 if (opt == KillSubarray || opt == UpdateSubarray) {
994 if (c.subarray) {
995 pr_err("subarray can only"
996 " be specified once\n");
997 exit(2);
998 }
999 c.subarray = optarg;
1000 }
1001 if (devmode && devmode != opt &&
1002 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
1003 pr_err("--examine/-E cannot be given with ");
1004 if (devmode == 'E') {
1005 if (option_index >= 0)
1006 fprintf(stderr, "--%s\n",
1007 long_options[option_index].name);
1008 else
1009 fprintf(stderr, "-%c\n", opt);
1010 } else if (isalpha(devmode))
1011 fprintf(stderr, "-%c\n", devmode);
1012 else
1013 fprintf(stderr, "previous option\n");
1014 exit(2);
1015 }
1016 devmode = opt;
1017 if (opt == Dump || opt == Restore) {
1018 if (dump_directory != NULL) {
1019 pr_err("dump/restore directory specified twice: %s and %s\n",
1020 dump_directory, optarg);
1021 exit(2);
1022 }
1023 dump_directory = optarg;
1024 }
1025 continue;
1026 case O(MISC, UdevRules):
1027 if (devmode && devmode != opt) {
1028 pr_err("--udev-rules must"
1029 " be the only option.\n");
1030 } else {
1031 if (udev_filename)
1032 pr_err("only specify one udev "
1033 "rule filename. %s ignored.\n",
1034 optarg);
1035 else
1036 udev_filename = optarg;
1037 }
1038 devmode = opt;
1039 continue;
1040 case O(MISC,'t'):
1041 c.test = 1;
1042 continue;
1043
1044 case O(MISC, Sparc22):
1045 if (devmode != 'E') {
1046 pr_err("--sparc2.2 only allowed with --examine\n");
1047 exit(2);
1048 }
1049 c.SparcAdjust = 1;
1050 continue;
1051
1052 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
1053 case O(ASSEMBLE,Bitmap):
1054 if (!optarg) {
1055 pr_err("bitmap file needed with -b in --assemble mode\n");
1056 exit(2);
1057 }
1058 if (strcmp(optarg, "internal")==0) {
1059 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1060 continue;
1061 }
1062 bitmap_fd = open(optarg, O_RDWR);
1063 if (!*optarg || bitmap_fd < 0) {
1064 pr_err("cannot open bitmap file %s: %s\n", optarg, strerror(errno));
1065 exit(2);
1066 }
1067 ident.bitmap_fd = bitmap_fd; /* for Assemble */
1068 continue;
1069
1070 case O(ASSEMBLE, BackupFile):
1071 case O(GROW, BackupFile):
1072 /* Specify a file into which grow might place a backup,
1073 * or from which assemble might recover a backup
1074 */
1075 if (c.backup_file) {
1076 pr_err("backup file already specified, rejecting %s\n", optarg);
1077 exit(2);
1078 }
1079 c.backup_file = optarg;
1080 continue;
1081
1082 case O(GROW, Continue):
1083 /* Continue interrupted grow
1084 */
1085 grow_continue = 1;
1086 continue;
1087 case O(ASSEMBLE, InvalidBackup):
1088 /* Acknowledge that the backupfile is invalid, but ask
1089 * to continue anyway
1090 */
1091 c.invalid_backup = 1;
1092 continue;
1093
1094 case O(BUILD,'b'):
1095 case O(BUILD,Bitmap):
1096 case O(CREATE,'b'):
1097 case O(CREATE,Bitmap): /* here we create the bitmap */
1098 case O(GROW,'b'):
1099 case O(GROW,Bitmap):
1100 if (strcmp(optarg, "internal")== 0 ||
1101 strcmp(optarg, "none")== 0 ||
1102 strchr(optarg, '/') != NULL) {
1103 s.bitmap_file = optarg;
1104 continue;
1105 }
1106 /* probable typo */
1107 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1108 " not '%s'\n", optarg);
1109 exit(2);
1110
1111 case O(GROW,BitmapChunk):
1112 case O(BUILD,BitmapChunk):
1113 case O(CREATE,BitmapChunk): /* bitmap chunksize */
1114 s.bitmap_chunk = parse_size(optarg);
1115 if (s.bitmap_chunk == 0 ||
1116 s.bitmap_chunk == INVALID_SECTORS ||
1117 s.bitmap_chunk & (s.bitmap_chunk - 1)) {
1118 pr_err("invalid bitmap chunksize: %s\n",
1119 optarg);
1120 exit(2);
1121 }
1122 s.bitmap_chunk = s.bitmap_chunk * 512;
1123 continue;
1124
1125 case O(GROW, WriteBehind):
1126 case O(BUILD, WriteBehind):
1127 case O(CREATE, WriteBehind): /* write-behind mode */
1128 s.write_behind = DEFAULT_MAX_WRITE_BEHIND;
1129 if (optarg) {
1130 s.write_behind = parse_num(optarg);
1131 if (s.write_behind < 0 ||
1132 s.write_behind > 16383) {
1133 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
1134 exit(2);
1135 }
1136 }
1137 continue;
1138
1139 case O(INCREMENTAL, 'r'):
1140 case O(INCREMENTAL, RebuildMapOpt):
1141 rebuild_map = 1;
1142 continue;
1143 case O(INCREMENTAL, IncrementalPath):
1144 remove_path = optarg;
1145 continue;
1146 }
1147 /* We have now processed all the valid options. Anything else is
1148 * an error
1149 */
1150 if (option_index > 0)
1151 pr_err(":option --%s not valid in %s mode\n",
1152 long_options[option_index].name,
1153 map_num(modes, mode));
1154 else
1155 pr_err("option -%c not valid in %s mode\n",
1156 opt, map_num(modes, mode));
1157 exit(2);
1158
1159 }
1160
1161 if (print_help) {
1162 char *help_text;
1163 if (print_help == 2)
1164 help_text = OptionHelp;
1165 else
1166 help_text = mode_help[mode];
1167 if (help_text == NULL)
1168 help_text = Help;
1169 fputs(help_text,stdout);
1170 exit(0);
1171 }
1172
1173 if (!mode && devs_found) {
1174 mode = MISC;
1175 devmode = 'Q';
1176 if (devlist->disposition == 0)
1177 devlist->disposition = devmode;
1178 }
1179 if (!mode) {
1180 fputs(Usage, stderr);
1181 exit(2);
1182 }
1183
1184 if (symlinks) {
1185 struct createinfo *ci = conf_get_create_info();
1186
1187 if (strcasecmp(symlinks, "yes") == 0)
1188 ci->symlinks = 1;
1189 else if (strcasecmp(symlinks, "no") == 0)
1190 ci->symlinks = 0;
1191 else {
1192 pr_err("option --symlinks must be 'no' or 'yes'\n");
1193 exit(2);
1194 }
1195 }
1196 /* Ok, got the option parsing out of the way
1197 * hopefully it's mostly right but there might be some stuff
1198 * missing
1199 *
1200 * That is mosty checked in the per-mode stuff but...
1201 *
1202 * For @,B,C and A without -s, the first device listed must be
1203 * an md device. We check that here and open it.
1204 */
1205
1206 if (mode == MANAGE || mode == BUILD || mode == CREATE
1207 || mode == GROW
1208 || (mode == ASSEMBLE && ! c.scan)) {
1209 if (devs_found < 1) {
1210 pr_err("an md device must be given in this mode\n");
1211 exit(2);
1212 }
1213 if ((int)ident.super_minor == -2 && c.autof) {
1214 pr_err("--super-minor=dev is incompatible with --auto\n");
1215 exit(2);
1216 }
1217 if (mode == MANAGE || mode == GROW) {
1218 mdfd = open_mddev(devlist->devname, 1);
1219 if (mdfd < 0)
1220 exit(1);
1221 } else
1222 /* non-existent device is OK */
1223 mdfd = open_mddev(devlist->devname, 0);
1224 if (mdfd == -2) {
1225 pr_err("device %s exists but is not an "
1226 "md array.\n", devlist->devname);
1227 exit(1);
1228 }
1229 if ((int)ident.super_minor == -2) {
1230 struct stat stb;
1231 if (mdfd < 0) {
1232 pr_err("--super-minor=dev given, and "
1233 "listed device %s doesn't exist.\n",
1234 devlist->devname);
1235 exit(1);
1236 }
1237 fstat(mdfd, &stb);
1238 ident.super_minor = minor(stb.st_rdev);
1239 }
1240 if (mdfd >= 0 && mode != MANAGE && mode != GROW) {
1241 /* We don't really want this open yet, we just might
1242 * have wanted to check some things
1243 */
1244 close(mdfd);
1245 mdfd = -1;
1246 }
1247 }
1248
1249 if (s.raiddisks) {
1250 if (s.raiddisks == 1 && !c.force && s.level != LEVEL_FAULTY) {
1251 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1252 " a mistake. If you really mean it you will need to specify --force before\n"
1253 " setting the number of drives.\n");
1254 exit(2);
1255 }
1256 }
1257
1258 if (c.homehost == NULL)
1259 c.homehost = conf_get_homehost(&c.require_homehost);
1260 if (c.homehost == NULL || strcasecmp(c.homehost, "<system>")==0) {
1261 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
1262 sys_hostname[sizeof(sys_hostname)-1] = 0;
1263 c.homehost = sys_hostname;
1264 }
1265 }
1266 if (c.homehost && (!c.homehost[0] || strcasecmp(c.homehost, "<none>") == 0)) {
1267 c.homehost = NULL;
1268 c.require_homehost = 0;
1269 }
1270
1271 if (c.backup_file && data_offset != INVALID_SECTORS) {
1272 pr_err("--backup-file and --data-offset are incompatible\n");
1273 exit(2);
1274 }
1275
1276 if ((mode == MISC && devmode == 'E')
1277 || (mode == MONITOR && spare_sharing == 0))
1278 /* Anyone may try this */;
1279 else if (geteuid() != 0) {
1280 pr_err("must be super-user to perform this action\n");
1281 exit(1);
1282 }
1283
1284 ident.autof = c.autof;
1285
1286 if (c.scan && c.verbose < 2)
1287 /* --scan implied --brief unless -vv */
1288 c.brief = 1;
1289
1290 rv = 0;
1291 switch(mode) {
1292 case MANAGE:
1293 /* readonly, add/remove, readwrite, runstop */
1294 if (c.readonly > 0)
1295 rv = Manage_ro(devlist->devname, mdfd, c.readonly);
1296 if (!rv && devs_found>1)
1297 rv = Manage_subdevs(devlist->devname, mdfd,
1298 devlist->next, c.verbose, c.test,
1299 c.update, c.force);
1300 if (!rv && c.readonly < 0)
1301 rv = Manage_ro(devlist->devname, mdfd, c.readonly);
1302 if (!rv && c.runstop)
1303 rv = Manage_runstop(devlist->devname, mdfd, c.runstop, c.verbose, 0);
1304 break;
1305 case ASSEMBLE:
1306 if (devs_found == 1 && ident.uuid_set == 0 &&
1307 ident.super_minor == UnSet && ident.name[0] == 0 && !c.scan ) {
1308 /* Only a device has been given, so get details from config file */
1309 struct mddev_ident *array_ident = conf_get_ident(devlist->devname);
1310 if (array_ident == NULL) {
1311 pr_err("%s not identified in config file.\n",
1312 devlist->devname);
1313 rv |= 1;
1314 if (mdfd >= 0)
1315 close(mdfd);
1316 } else {
1317 if (array_ident->autof == 0)
1318 array_ident->autof = c.autof;
1319 rv |= Assemble(ss, devlist->devname, array_ident,
1320 NULL, &c);
1321 }
1322 } else if (!c.scan)
1323 rv = Assemble(ss, devlist->devname, &ident,
1324 devlist->next, &c);
1325 else if (devs_found > 0) {
1326 if (c.update && devs_found > 1) {
1327 pr_err("can only update a single array at a time\n");
1328 exit(1);
1329 }
1330 if (c.backup_file && devs_found > 1) {
1331 pr_err("can only assemble a single array when providing a backup file.\n");
1332 exit(1);
1333 }
1334 for (dv = devlist ; dv ; dv=dv->next) {
1335 struct mddev_ident *array_ident = conf_get_ident(dv->devname);
1336 if (array_ident == NULL) {
1337 pr_err("%s not identified in config file.\n",
1338 dv->devname);
1339 rv |= 1;
1340 continue;
1341 }
1342 if (array_ident->autof == 0)
1343 array_ident->autof = c.autof;
1344 rv |= Assemble(ss, dv->devname, array_ident,
1345 NULL, &c);
1346 }
1347 } else {
1348 if (c.update) {
1349 pr_err("--update not meaningful with a --scan assembly.\n");
1350 exit(1);
1351 }
1352 if (c.backup_file) {
1353 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1354 exit(1);
1355 }
1356 rv = scan_assemble(ss, &c, &ident);
1357 }
1358
1359 break;
1360 case BUILD:
1361 if (c.delay == 0)
1362 c.delay = DEFAULT_BITMAP_DELAY;
1363 if (s.write_behind && !s.bitmap_file) {
1364 pr_err("write-behind mode requires a bitmap.\n");
1365 rv = 1;
1366 break;
1367 }
1368 if (s.raiddisks == 0) {
1369 pr_err("no raid-devices specified.\n");
1370 rv = 1;
1371 break;
1372 }
1373
1374 if (s.bitmap_file) {
1375 if (strcmp(s.bitmap_file, "internal")==0) {
1376 pr_err("'internal' bitmaps not supported with --build\n");
1377 rv |= 1;
1378 break;
1379 }
1380 }
1381 rv = Build(devlist->devname, devlist->next, &s, &c);
1382 break;
1383 case CREATE:
1384 if (c.delay == 0)
1385 c.delay = DEFAULT_BITMAP_DELAY;
1386 if (s.write_behind && !s.bitmap_file) {
1387 pr_err("write-behind mode requires a bitmap.\n");
1388 rv = 1;
1389 break;
1390 }
1391 if (s.raiddisks == 0) {
1392 pr_err("no raid-devices specified.\n");
1393 rv = 1;
1394 break;
1395 }
1396
1397 rv = Create(ss, devlist->devname,
1398 ident.name, ident.uuid_set ? ident.uuid : NULL,
1399 devs_found-1, devlist->next,
1400 &s, &c, data_offset);
1401 break;
1402 case MISC:
1403 if (devmode == 'E') {
1404 if (devlist == NULL && !c.scan) {
1405 pr_err("No devices to examine\n");
1406 exit(2);
1407 }
1408 if (devlist == NULL)
1409 devlist = conf_get_devs();
1410 if (devlist == NULL) {
1411 pr_err("No devices listed in %s\n", configfile?configfile:DefaultConfFile);
1412 exit(1);
1413 }
1414 rv = Examine(devlist, &c, ss);
1415 } else if (devmode == DetailPlatform) {
1416 rv = Detail_Platform(ss ? ss->ss : NULL, ss ? c.scan : 1,
1417 c.verbose, c.export,
1418 devlist ? devlist->devname : NULL);
1419 } else if (devlist == NULL) {
1420 if (devmode == 'S' && c.scan)
1421 rv = stop_scan(c.verbose);
1422 else if ((devmode == 'D' || devmode == Waitclean) && c.scan)
1423 rv = misc_scan(devmode, &c);
1424 else if (devmode == UdevRules)
1425 rv = Write_rules(udev_filename);
1426 else {
1427 pr_err("No devices given.\n");
1428 exit(2);
1429 }
1430 } else
1431 rv = misc_list(devlist, &ident, dump_directory, ss, &c);
1432 break;
1433 case MONITOR:
1434 if (!devlist && !c.scan) {
1435 pr_err("Cannot monitor: need --scan or at least one device\n");
1436 rv = 1;
1437 break;
1438 }
1439 if (pidfile && !daemonise) {
1440 pr_err("Cannot write a pid file when not in daemon mode\n");
1441 rv = 1;
1442 break;
1443 }
1444 if (c.delay == 0) {
1445 if (get_linux_version() > 2006016)
1446 /* mdstat responds to poll */
1447 c.delay = 1000;
1448 else
1449 c.delay = 60;
1450 }
1451 if (c.delay == 0)
1452 c.delay = 60;
1453 rv= Monitor(devlist, mailaddr, program,
1454 &c, daemonise, oneshot,
1455 dosyslog, pidfile, increments,
1456 spare_sharing);
1457 break;
1458
1459 case GROW:
1460 if (array_size > 0) {
1461 /* alway impose array size first, independent of
1462 * anything else
1463 * Do not allow level or raid_disks changes at the
1464 * same time as that can be irreversibly destructive.
1465 */
1466 struct mdinfo sra;
1467 int err;
1468 if (s.raiddisks || s.level != UnSet) {
1469 pr_err("cannot change array size in same operation "
1470 "as changing raiddisks or level.\n"
1471 " Change size first, then check that data is still intact.\n");
1472 rv = 1;
1473 break;
1474 }
1475 sysfs_init(&sra, mdfd, NULL);
1476 if (array_size == MAX_SIZE)
1477 err = sysfs_set_str(&sra, NULL, "array_size", "default");
1478 else
1479 err = sysfs_set_num(&sra, NULL, "array_size", array_size / 2);
1480 if (err < 0) {
1481 if (errno == E2BIG)
1482 pr_err("--array-size setting"
1483 " is too large.\n");
1484 else
1485 pr_err("current kernel does"
1486 " not support setting --array-size\n");
1487 rv = 1;
1488 break;
1489 }
1490 }
1491 if (devs_found > 1 && s.raiddisks == 0) {
1492 /* must be '-a'. */
1493 if (s.size > 0 || s.chunk || s.layout_str != NULL || s.bitmap_file) {
1494 pr_err("--add cannot be used with "
1495 "other geometry changes in --grow mode\n");
1496 rv = 1;
1497 break;
1498 }
1499 for (dv=devlist->next; dv ; dv=dv->next) {
1500 rv = Grow_Add_device(devlist->devname, mdfd,
1501 dv->devname);
1502 if (rv)
1503 break;
1504 }
1505 } else if (s.bitmap_file) {
1506 if (s.size > 0 || s.raiddisks || s.chunk ||
1507 s.layout_str != NULL || devs_found > 1) {
1508 pr_err("--bitmap changes cannot be "
1509 "used with other geometry changes "
1510 "in --grow mode\n");
1511 rv = 1;
1512 break;
1513 }
1514 if (c.delay == 0)
1515 c.delay = DEFAULT_BITMAP_DELAY;
1516 rv = Grow_addbitmap(devlist->devname, mdfd, &c, &s);
1517 } else if (grow_continue)
1518 rv = Grow_continue_command(devlist->devname,
1519 mdfd, c.backup_file,
1520 c.verbose);
1521 else if (s.size > 0 || s.raiddisks || s.layout_str != NULL
1522 || s.chunk != 0 || s.level != UnSet
1523 || data_offset != INVALID_SECTORS) {
1524 rv = Grow_reshape(devlist->devname, mdfd,
1525 devlist->next,
1526 data_offset, &c, &s);
1527 } else if (array_size == 0)
1528 pr_err("no changes to --grow\n");
1529 break;
1530 case INCREMENTAL:
1531 if (rebuild_map) {
1532 RebuildMap();
1533 }
1534 if (c.scan) {
1535 if (c.runstop <= 0) {
1536 pr_err("--incremental --scan meaningless without --run.\n");
1537 break;
1538 }
1539 if (devmode == 'f') {
1540 pr_err("--incremental --scan --fail not supported.\n");
1541 break;
1542 }
1543 rv = IncrementalScan(c.verbose);
1544 }
1545 if (!devlist) {
1546 if (!rebuild_map && !c.scan) {
1547 pr_err("--incremental requires a device.\n");
1548 rv = 1;
1549 }
1550 break;
1551 }
1552 if (devlist->next) {
1553 pr_err("--incremental can only handle one device.\n");
1554 rv = 1;
1555 break;
1556 }
1557 if (devmode == 'f')
1558 rv = IncrementalRemove(devlist->devname, remove_path,
1559 c.verbose);
1560 else
1561 rv = Incremental(devlist->devname, &c, ss);
1562 break;
1563 case AUTODETECT:
1564 autodetect();
1565 break;
1566 }
1567 exit(rv);
1568 }
1569
1570 static int scan_assemble(struct supertype *ss,
1571 struct context *c,
1572 struct mddev_ident *ident)
1573 {
1574 struct mddev_ident *a, *array_list = conf_get_ident(NULL);
1575 struct mddev_dev *devlist = conf_get_devs();
1576 struct map_ent *map = NULL;
1577 int cnt = 0;
1578 int rv = 0;
1579 int failures, successes;
1580
1581 if (conf_verify_devnames(array_list)) {
1582 pr_err("Duplicate MD device names in "
1583 "conf file were found.\n");
1584 return 1;
1585 }
1586 if (devlist == NULL) {
1587 pr_err("No devices listed in conf file were found.\n");
1588 return 1;
1589 }
1590 for (a = array_list; a ; a = a->next) {
1591 a->assembled = 0;
1592 if (a->autof == 0)
1593 a->autof = c->autof;
1594 }
1595 if (map_lock(&map))
1596 pr_err("%s: failed to get "
1597 "exclusive lock on mapfile\n",
1598 __func__);
1599 do {
1600 failures = 0;
1601 successes = 0;
1602 rv = 0;
1603 for (a = array_list; a ; a = a->next) {
1604 int r;
1605 if (a->assembled)
1606 continue;
1607 if (a->devname &&
1608 strcasecmp(a->devname, "<ignore>") == 0)
1609 continue;
1610
1611 r = Assemble(ss, a->devname,
1612 a, NULL, c);
1613 if (r == 0) {
1614 a->assembled = 1;
1615 successes++;
1616 } else
1617 failures++;
1618 rv |= r;
1619 cnt++;
1620 }
1621 } while (failures && successes);
1622 if (c->homehost && cnt == 0) {
1623 /* Maybe we can auto-assemble something.
1624 * Repeatedly call Assemble in auto-assemble mode
1625 * until it fails
1626 */
1627 int rv2;
1628 int acnt;
1629 ident->autof = c->autof;
1630 do {
1631 struct mddev_dev *devlist = conf_get_devs();
1632 acnt = 0;
1633 do {
1634 rv2 = Assemble(ss, NULL,
1635 ident,
1636 devlist, c);
1637 if (rv2==0) {
1638 cnt++;
1639 acnt++;
1640 }
1641 } while (rv2!=2);
1642 /* Incase there are stacked devices, we need to go around again */
1643 } while (acnt);
1644 if (cnt == 0 && rv == 0) {
1645 pr_err("No arrays found in config file or automatically\n");
1646 rv = 1;
1647 } else if (cnt)
1648 rv = 0;
1649 } else if (cnt == 0 && rv == 0) {
1650 pr_err("No arrays found in config file\n");
1651 rv = 1;
1652 }
1653 map_unlock(&map);
1654 return rv;
1655 }
1656
1657 static int misc_scan(char devmode, struct context *c)
1658 {
1659 /* apply --detail or --wait-clean to
1660 * all devices in /proc/mdstat
1661 */
1662 struct mdstat_ent *ms = mdstat_read(0, 1);
1663 struct mdstat_ent *e;
1664 struct map_ent *map = NULL;
1665 int members;
1666 int rv = 0;
1667
1668 for (members = 0; members <= 1; members++) {
1669 for (e=ms ; e ; e=e->next) {
1670 char *name = NULL;
1671 struct map_ent *me;
1672 struct stat stb;
1673 int member = e->metadata_version &&
1674 strncmp(e->metadata_version,
1675 "external:/", 10) == 0;
1676 if (members != member)
1677 continue;
1678 me = map_by_devnm(&map, e->devnm);
1679 if (me && me->path
1680 && strcmp(me->path, "/unknown") != 0)
1681 name = me->path;
1682 if (name == NULL ||
1683 stat(name, &stb) != 0)
1684 name = get_md_name(e->devnm);
1685
1686 if (!name) {
1687 pr_err("cannot find device file for %s\n",
1688 e->dev);
1689 continue;
1690 }
1691 if (devmode == 'D')
1692 rv |= Detail(name, c);
1693 else
1694 rv |= WaitClean(name, -1, c->verbose);
1695 put_md_name(name);
1696 }
1697 }
1698 free_mdstat(ms);
1699 return rv;
1700 }
1701
1702 static int stop_scan(int verbose)
1703 {
1704 /* apply --stop to all devices in /proc/mdstat */
1705 /* Due to possible stacking of devices, repeat until
1706 * nothing more can be stopped
1707 */
1708 int progress=1, err;
1709 int last = 0;
1710 int rv = 0;
1711 do {
1712 struct mdstat_ent *ms = mdstat_read(0, 0);
1713 struct mdstat_ent *e;
1714
1715 if (!progress) last = 1;
1716 progress = 0; err = 0;
1717 for (e=ms ; e ; e=e->next) {
1718 char *name = get_md_name(e->devnm);
1719 int mdfd;
1720
1721 if (!name) {
1722 pr_err("cannot find device file for %s\n",
1723 e->dev);
1724 continue;
1725 }
1726 mdfd = open_mddev(name, 1);
1727 if (mdfd >= 0) {
1728 if (Manage_runstop(name, mdfd, -1, verbose, !last))
1729 err = 1;
1730 else
1731 progress = 1;
1732 close(mdfd);
1733 }
1734
1735 put_md_name(name);
1736 }
1737 free_mdstat(ms);
1738 } while (!last && err);
1739 if (err)
1740 rv |= 1;
1741 return rv;
1742 }
1743
1744 static int misc_list(struct mddev_dev *devlist,
1745 struct mddev_ident *ident,
1746 char *dump_directory,
1747 struct supertype *ss, struct context *c)
1748 {
1749 struct mddev_dev *dv;
1750 int rv = 0;
1751
1752 for (dv=devlist ; dv; dv=(rv & 16) ? NULL : dv->next) {
1753 int mdfd;
1754
1755 switch(dv->disposition) {
1756 case 'D':
1757 rv |= Detail(dv->devname, c);
1758 continue;
1759 case KillOpt: /* Zero superblock */
1760 if (ss)
1761 rv |= Kill(dv->devname, ss, c->force, c->verbose,0);
1762 else {
1763 int v = c->verbose;
1764 do {
1765 rv |= Kill(dv->devname, NULL, c->force, v, 0);
1766 v = -1;
1767 } while (rv == 0);
1768 rv &= ~2;
1769 }
1770 continue;
1771 case 'Q':
1772 rv |= Query(dv->devname); continue;
1773 case 'X':
1774 rv |= ExamineBitmap(dv->devname, c->brief, ss); continue;
1775 case ExamineBB:
1776 rv |= ExamineBadblocks(dv->devname, c->brief, ss); continue;
1777 case 'W':
1778 case WaitOpt:
1779 rv |= Wait(dv->devname); continue;
1780 case Waitclean:
1781 rv |= WaitClean(dv->devname, -1, c->verbose); continue;
1782 case KillSubarray:
1783 rv |= Kill_subarray(dv->devname, c->subarray, c->verbose);
1784 continue;
1785 case UpdateSubarray:
1786 if (c->update == NULL) {
1787 pr_err("-U/--update must be specified with --update-subarray\n");
1788 rv |= 1;
1789 continue;
1790 }
1791 rv |= Update_subarray(dv->devname, c->subarray,
1792 c->update, ident, c->verbose);
1793 continue;
1794 case Dump:
1795 rv |= Dump_metadata(dv->devname, dump_directory, c, ss);
1796 continue;
1797 case Restore:
1798 rv |= Restore_metadata(dv->devname, dump_directory, c, ss,
1799 (dv == devlist && dv->next == NULL));
1800 continue;
1801 }
1802 mdfd = open_mddev(dv->devname, 1);
1803 if (mdfd>=0) {
1804 switch(dv->disposition) {
1805 case 'R':
1806 rv |= Manage_runstop(dv->devname, mdfd, 1, c->verbose, 0); break;
1807 case 'S':
1808 rv |= Manage_runstop(dv->devname, mdfd, -1, c->verbose, 0); break;
1809 case 'o':
1810 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1811 case 'w':
1812 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1813 }
1814 close(mdfd);
1815 } else
1816 rv |= 1;
1817 }
1818 return rv;
1819 }