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