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