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