]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdadm.c
Increase raid456 stripe cache size if needed to --grow the array.
[thirdparty/mdadm.git] / mdadm.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2006 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@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 *
29 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
30 * Paul Clements, SteelEye Technology, Inc.
31 */
32
33 #include "mdadm.h"
34 #include "md_p.h"
35 #include <ctype.h>
36
37
38 int main(int argc, char *argv[])
39 {
40 int mode = 0;
41 int opt;
42 int option_index;
43 char *c;
44 int rv;
45 int i;
46
47 int chunk = 0;
48 long long size = -1;
49 int level = UnSet;
50 int layout = UnSet;
51 int raiddisks = 0;
52 int max_disks = MD_SB_DISKS; /* just a default */
53 int sparedisks = 0;
54 struct mddev_ident_s ident;
55 char *configfile = NULL;
56 char *cp;
57 char *update = NULL;
58 int scan = 0;
59 char devmode = 0;
60 int runstop = 0;
61 int readonly = 0;
62 int write_behind = 0;
63 int bitmap_fd = -1;
64 char *bitmap_file = NULL;
65 char *backup_file = NULL;
66 int bitmap_chunk = UnSet;
67 int SparcAdjust = 0;
68 mddev_dev_t devlist = NULL;
69 mddev_dev_t *devlistend = & devlist;
70 mddev_dev_t dv;
71 int devs_found = 0;
72 int verbose = 0;
73 int quiet = 0;
74 int brief = 0;
75 int force = 0;
76 int test = 0;
77 int assume_clean = 0;
78 char *symlinks = NULL;
79 /* autof indicates whether and how to create device node.
80 * bottom 3 bits are style. Rest (when shifted) are number of parts
81 * 0 - unset
82 * 1 - don't create (no)
83 * 2 - if is_standard, then create (yes)
84 * 3 - create as 'md' - reject is_standard mdp (md)
85 * 4 - create as 'mdp' - reject is_standard md (mdp)
86 * 5 - default to md if not is_standard (md in config file)
87 * 6 - default to mdp if not is_standard (part, or mdp in config file)
88 */
89 int autof = 0;
90
91 char *homehost = NULL;
92 char sys_hostname[256];
93 char *mailaddr = NULL;
94 char *program = NULL;
95 int delay = 0;
96 int daemonise = 0;
97 char *pidfile = NULL;
98 int oneshot = 0;
99 struct supertype *ss = NULL;
100 int writemostly = 0;
101 int re_add = 0;
102 char *shortopt = short_options;
103 int dosyslog = 0;
104 int auto_update_home = 0;
105
106 int copies;
107 int print_help = 0;
108
109 int mdfd = -1;
110
111 srandom(time(0) ^ getpid());
112
113 ident.uuid_set=0;
114 ident.level = UnSet;
115 ident.raid_disks = UnSet;
116 ident.super_minor= UnSet;
117 ident.devices=0;
118 ident.spare_group = NULL;
119 ident.autof = 0;
120 ident.st = NULL;
121 ident.bitmap_fd = -1;
122 ident.bitmap_file = NULL;
123 ident.name[0] = 0;
124
125 while ((option_index = -1) ,
126 (opt=getopt_long(argc, argv,
127 shortopt, long_options,
128 &option_index)) != -1) {
129 int newmode = mode;
130 /* firstly, some mode-independant options */
131 switch(opt) {
132 case 'h':
133 if (option_index > 0 &&
134 strcmp(long_options[option_index].name, "help-options")==0)
135 print_help = 2;
136 else
137 print_help = 1;
138 continue;
139
140 case 'V':
141 fputs(Version, stderr);
142 exit(0);
143
144 case 'v': verbose++;
145 continue;
146
147 case 'q': quiet++;
148 continue;
149
150 case 'b':
151 if (mode == ASSEMBLE || mode == BUILD || mode == CREATE || mode == GROW)
152 break; /* b means bitmap */
153 brief = 1;
154 if (optarg) {
155 fprintf(stderr, Name ": -b cannot have any extra immediately after it, sorry.\n");
156 exit(2);
157 }
158 continue;
159
160 case HomeHost:
161 homehost = optarg;
162 continue;
163
164 case ':':
165 case '?':
166 fputs(Usage, stderr);
167 exit(2);
168 }
169 /* second, figure out the mode.
170 * Some options force the mode. Others
171 * set the mode if it isn't already
172 */
173
174 switch(opt) {
175 case '@': /* just incase they say --manage */
176 newmode = MANAGE;
177 shortopt = short_bitmap_auto_options;
178 break;
179 case 'a':
180 case 'r':
181 case 'f':
182 case ReAdd: /* re-add */
183 if (!mode) {
184 newmode = MANAGE;
185 shortopt = short_bitmap_auto_options;
186 }
187 break;
188
189 case 'A': newmode = ASSEMBLE; shortopt = short_bitmap_auto_options; break;
190 case 'B': newmode = BUILD; shortopt = short_bitmap_auto_options; break;
191 case 'C': newmode = CREATE; shortopt = short_bitmap_auto_options; break;
192 case 'F': newmode = MONITOR;break;
193 case 'G': newmode = GROW; shortopt = short_bitmap_auto_options; break;
194
195 case '#':
196 case 'D':
197 case 'E':
198 case 'X':
199 case 'Q': newmode = MISC; break;
200 case 'R':
201 case 'S':
202 case 'o':
203 case 'w':
204 case 'K': if (!mode) newmode = MISC; break;
205 }
206 if (mode && newmode == mode) {
207 /* everybody happy ! */
208 } else if (mode && newmode != mode) {
209 /* not allowed.. */
210 fprintf(stderr, Name ": ");
211 if (option_index >= 0)
212 fprintf(stderr, "--%s", long_options[option_index].name);
213 else
214 fprintf(stderr, "-%c", opt);
215 fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
216 map_num(modes, newmode),
217 map_num(modes, mode));
218 exit(2);
219 } else if (!mode && newmode) {
220 mode = newmode;
221 } else {
222 /* special case of -c --help */
223 if (opt == 'c' &&
224 ( strncmp(optarg, "--h", 3)==0 ||
225 strncmp(optarg, "-h", 2)==0)) {
226 fputs(Help_config, stderr);
227 exit(0);
228 }
229
230 /* If first option is a device, don't force the mode yet */
231 if (opt == 1) {
232 if (devs_found == 0) {
233 dv = malloc(sizeof(*dv));
234 if (dv == NULL) {
235 fprintf(stderr, Name ": malloc failed\n");
236 exit(3);
237 }
238 dv->devname = optarg;
239 dv->disposition = devmode;
240 dv->writemostly = writemostly;
241 dv->re_add = re_add;
242 dv->used = 0;
243 dv->next = NULL;
244 *devlistend = dv;
245 devlistend = &dv->next;
246
247 devs_found++;
248 continue;
249 }
250 /* No mode yet, and this is the second device ... */
251 fprintf(stderr, Name ": An option must be given to set the mode before a second device is listed\n");
252 exit(2);
253 }
254 if (option_index >= 0)
255 fprintf(stderr, Name ": --%s", long_options[option_index].name);
256 else
257 fprintf(stderr, Name ": -%c", opt);
258 fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
259 exit(2);
260 }
261
262 /* if we just set the mode, then done */
263 switch(opt) {
264 case '@':
265 case '#':
266 case 'A':
267 case 'B':
268 case 'C':
269 case 'F':
270 case 'G':
271 continue;
272 }
273 if (opt == 1) {
274 /* an undecorated option - must be a device name.
275 */
276 if (devs_found > 0 && mode == '@' && !devmode) {
277 fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
278 exit(2);
279 }
280 if (devs_found > 0 && mode == 'G' && !devmode) {
281 fprintf(stderr, Name ": Must give one of -a for devices do add: %s\n", optarg);
282 exit(2);
283 }
284 dv = malloc(sizeof(*dv));
285 if (dv == NULL) {
286 fprintf(stderr, Name ": malloc failed\n");
287 exit(3);
288 }
289 dv->devname = optarg;
290 dv->disposition = devmode;
291 dv->writemostly = writemostly;
292 dv->re_add = re_add;
293 dv->next = NULL;
294 *devlistend = dv;
295 devlistend = &dv->next;
296
297 devs_found++;
298 continue;
299 }
300
301 /* We've got a mode, and opt is now something else which
302 * could depend on the mode */
303 #define O(a,b) ((a<<8)|b)
304 switch (O(mode,opt)) {
305 case O(CREATE,'c'):
306 case O(BUILD,'c'): /* chunk or rounding */
307 if (chunk) {
308 fprintf(stderr, Name ": chunk/rounding may only be specified once. "
309 "Second value is %s.\n", optarg);
310 exit(2);
311 }
312 chunk = strtol(optarg, &c, 10);
313 if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) {
314 fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
315 optarg);
316 exit(2);
317 }
318 continue;
319
320 case O(ASSEMBLE,AutoHomeHost):
321 auto_update_home = 1;
322 continue;
323 case O(CREATE,'e'):
324 case O(ASSEMBLE,'e'):
325 case O(MISC,'e'): /* set metadata (superblock) information */
326 if (ss) {
327 fprintf(stderr, Name ": metadata information already given\n");
328 exit(2);
329 }
330 for(i=0; !ss && superlist[i]; i++)
331 ss = superlist[i]->match_metadata_desc(optarg);
332
333 if (!ss) {
334 fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg);
335 exit(2);
336 }
337 max_disks = ss->max_devs;
338 continue;
339
340 case O(MANAGE,'W'):
341 case O(BUILD,'W'):
342 case O(CREATE,'W'):
343 /* set write-mostly for following devices */
344 writemostly = 1;
345 continue;
346
347 case O(GROW,'z'):
348 case O(CREATE,'z'): /* size */
349 if (size >= 0) {
350 fprintf(stderr, Name ": size may only be specified once. "
351 "Second value is %s.\n", optarg);
352 exit(2);
353 }
354 if (strcmp(optarg, "max")==0)
355 size = 0;
356 else {
357 size = strtoll(optarg, &c, 10);
358 if (!optarg[0] || *c || size < 4) {
359 fprintf(stderr, Name ": invalid size: %s\n",
360 optarg);
361 exit(2);
362 }
363 }
364 continue;
365
366 case O(GROW,'l'): /* hack - needed to understand layout */
367 case O(CREATE,'l'):
368 case O(BUILD,'l'): /* set raid level*/
369 if (level != UnSet) {
370 fprintf(stderr, Name ": raid level may only be set once. "
371 "Second value is %s.\n", optarg);
372 exit(2);
373 }
374 level = map_name(pers, optarg);
375 if (level == UnSet) {
376 fprintf(stderr, Name ": invalid raid level: %s\n",
377 optarg);
378 exit(2);
379 }
380 if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) {
381 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
382 optarg);
383 exit(2);
384 }
385 if (sparedisks > 0 && level < 1 && level >= -1) {
386 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
387 optarg);
388 exit(2);
389 }
390 ident.level = level;
391 continue;
392
393 case O(CREATE,'p'): /* raid5 layout */
394 case O(BUILD,'p'): /* faulty layout */
395 case O(GROW, 'p'): /* faulty reconfig */
396 if (layout != UnSet) {
397 fprintf(stderr,Name ": layout may only be sent once. "
398 "Second value was %s\n", optarg);
399 exit(2);
400 }
401 switch(level) {
402 default:
403 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
404 map_num(pers, level));
405 exit(2);
406 case UnSet:
407 fprintf(stderr, Name ": raid level must be given before layout.\n");
408 exit(2);
409
410 case 5:
411 case 6:
412 layout = map_name(r5layout, optarg);
413 if (layout==UnSet) {
414 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
415 optarg);
416 exit(2);
417 }
418 break;
419
420 case 10:
421 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
422 if ((optarg[0] != 'n' && optarg[0] != 'f' && optarg[0] != 'o') ||
423 (copies = strtoul(optarg+1, &cp, 10)) < 1 ||
424 copies > 200 ||
425 *cp) {
426 fprintf(stderr, Name ": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg);
427 exit(2);
428 }
429 if (optarg[0] == 'n')
430 layout = 256 + copies;
431 else if (optarg[0] == 'o')
432 layout = 0x10000 + (copies<<8) + 1;
433 else
434 layout = 1 + (copies<<8);
435 break;
436 case -5: /* Faulty
437 * modeNNN
438 */
439
440 {
441 int ln = strcspn(optarg, "0123456789");
442 char *m = strdup(optarg);
443 int mode;
444 m[ln] = 0;
445 mode = map_name(faultylayout, m);
446 if (mode == UnSet) {
447 fprintf(stderr, Name ": layout %s not understood for faulty.\n",
448 optarg);
449 exit(2);
450 }
451 layout = mode | (atoi(optarg+ln)<< ModeShift);
452 }
453 }
454 continue;
455
456 case O(CREATE,AssumeClean):
457 case O(BUILD,AssumeClean): /* assume clean */
458 assume_clean = 1;
459 continue;
460
461 case O(GROW,'n'):
462 case O(CREATE,'n'):
463 case O(BUILD,'n'): /* number of raid disks */
464 if (raiddisks) {
465 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
466 raiddisks, optarg);
467 exit(2);
468 }
469 raiddisks = strtol(optarg, &c, 10);
470 if (!optarg[0] || *c || raiddisks<=0) {
471 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
472 optarg);
473 exit(2);
474 }
475 ident.raid_disks = raiddisks;
476 continue;
477
478 case O(CREATE,'x'): /* number of spare (eXtra) discs */
479 if (sparedisks) {
480 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
481 sparedisks, optarg);
482 exit(2);
483 }
484 if (level != UnSet && level <= 0 && level >= -1) {
485 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
486 level);
487 exit(2);
488 }
489 sparedisks = strtol(optarg, &c, 10);
490 if (!optarg[0] || *c || sparedisks < 0) {
491 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
492 optarg);
493 exit(2);
494 }
495 continue;
496
497 case O(CREATE,'a'):
498 case O(BUILD,'a'):
499 case O(ASSEMBLE,'a'): /* auto-creation of device node */
500 autof = parse_auto(optarg, "--auto flag", 0);
501 continue;
502
503 case O(CREATE,Symlinks):
504 case O(BUILD,Symlinks):
505 case O(ASSEMBLE,Symlinks): /* auto creation of symlinks in /dev to /dev/md */
506 symlinks = optarg;
507 continue;
508
509 case O(BUILD,'f'): /* force honouring '-n 1' */
510 case O(GROW,'f'): /* ditto */
511 case O(CREATE,'f'): /* force honouring of device list */
512 case O(ASSEMBLE,'f'): /* force assembly */
513 case O(MISC,'f'): /* force zero */
514 force=1;
515 continue;
516
517 /* now for the Assemble options */
518 case O(ASSEMBLE,'u'): /* uuid of array */
519 if (ident.uuid_set) {
520 fprintf(stderr, Name ": uuid cannot be set twice. "
521 "Second value %s.\n", optarg);
522 exit(2);
523 }
524 if (parse_uuid(optarg, ident.uuid))
525 ident.uuid_set = 1;
526 else {
527 fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
528 exit(2);
529 }
530 continue;
531
532 case O(CREATE,'N'):
533 case O(ASSEMBLE,'N'):
534 if (ident.name[0]) {
535 fprintf(stderr, Name ": name cannot be set twice. "
536 "Second value %s.\n", optarg);
537 exit(2);
538 }
539 if (strlen(optarg) > 32) {
540 fprintf(stderr, Name ": name '%s' is too long, 32 chars max.\n",
541 optarg);
542 exit(2);
543 }
544 strcpy(ident.name, optarg);
545 continue;
546
547 case O(ASSEMBLE,'m'): /* super-minor for array */
548 if (ident.super_minor != UnSet) {
549 fprintf(stderr, Name ": super-minor cannot be set twice. "
550 "Second value: %s.\n", optarg);
551 exit(2);
552 }
553 if (strcmp(optarg, "dev")==0)
554 ident.super_minor = -2;
555 else {
556 ident.super_minor = strtoul(optarg, &cp, 10);
557 if (!optarg[0] || *cp) {
558 fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
559 exit(2);
560 }
561 }
562 continue;
563
564 case O(ASSEMBLE,'U'): /* update the superblock */
565 if (update) {
566 fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n",
567 update, optarg);
568 exit(2);
569 }
570 update = optarg;
571 if (strcmp(update, "sparc2.2")==0)
572 continue;
573 if (strcmp(update, "super-minor") == 0)
574 continue;
575 if (strcmp(update, "summaries")==0)
576 continue;
577 if (strcmp(update, "resync")==0)
578 continue;
579 if (strcmp(update, "uuid")==0)
580 continue;
581 if (strcmp(update, "name")==0)
582 continue;
583 if (strcmp(update, "homehost")==0)
584 continue;
585 if (strcmp(update, "devicesize")==0)
586 continue;
587 if (strcmp(update, "byteorder")==0) {
588 if (ss) {
589 fprintf(stderr, Name ": must not set metadata type with --update=byteorder.\n");
590 exit(2);
591 }
592 for(i=0; !ss && superlist[i]; i++)
593 ss = superlist[i]->match_metadata_desc("0.swap");
594 if (!ss) {
595 fprintf(stderr, Name ": INTERNAL ERROR cannot find 0.swap\n");
596 exit(2);
597 }
598
599 continue;
600 }
601 if (strcmp(update,"?") == 0 || strcmp(update, "help") == 0)
602 fprintf(stderr, Name ": ");
603 else
604 fprintf(stderr, Name ": '--update=%s' is invalid. ", update);
605 fprintf(stderr, "Valid --update options are:\n"
606 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
607 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
608 exit(2);
609
610 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
611 runstop = -1; /* --stop isn't allowed for --assemble, so we overload slightly */
612 continue;
613
614 case O(ASSEMBLE,'c'): /* config file */
615 case O(MISC, 'c'):
616 case O(MONITOR,'c'):
617 if (configfile) {
618 fprintf(stderr, Name ": configfile cannot be set twice. "
619 "Second value is %s.\n", optarg);
620 exit(2);
621 }
622 configfile = optarg;
623 set_conffile(configfile);
624 /* FIXME possibly check that config file exists. Even parse it */
625 continue;
626 case O(ASSEMBLE,'s'): /* scan */
627 case O(MISC,'s'):
628 case O(MONITOR,'s'):
629 scan = 1;
630 continue;
631
632 case O(MONITOR,'m'): /* mail address */
633 if (mailaddr)
634 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
635 optarg);
636 else
637 mailaddr = optarg;
638 continue;
639
640 case O(MONITOR,'p'): /* alert program */
641 if (program)
642 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
643 optarg);
644 else
645 program = optarg;
646 continue;
647
648 case O(MONITOR,'d'): /* delay in seconds */
649 case O(GROW, 'd'):
650 case O(BUILD,'d'): /* delay for bitmap updates */
651 case O(CREATE,'d'):
652 if (delay)
653 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
654 optarg);
655 else {
656 delay = strtol(optarg, &c, 10);
657 if (!optarg[0] || *c || delay<1) {
658 fprintf(stderr, Name ": invalid delay: %s\n",
659 optarg);
660 exit(2);
661 }
662 }
663 continue;
664 case O(MONITOR,'f'): /* daemonise */
665 daemonise = 1;
666 continue;
667 case O(MONITOR,'i'): /* pid */
668 if (pidfile)
669 fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
670 optarg);
671 else
672 pidfile = optarg;
673 continue;
674 case O(MONITOR,'1'): /* oneshot */
675 oneshot = 1;
676 continue;
677 case O(MONITOR,'t'): /* test */
678 test = 1;
679 continue;
680 case O(MONITOR,'y'): /* log messages to syslog */
681 openlog("mdadm", 0, SYSLOG_FACILITY);
682 dosyslog = 1;
683 continue;
684
685 /* now the general management options. Some are applicable
686 * to other modes. None have arguments.
687 */
688 case O(GROW,'a'):
689 case O(MANAGE,'a'): /* add a drive */
690 devmode = 'a';
691 re_add = 0;
692 continue;
693 case O(MANAGE,ReAdd):
694 devmode = 'a';
695 re_add = 1;
696 continue;
697 case O(MANAGE,'r'): /* remove a drive */
698 devmode = 'r';
699 continue;
700 case O(MANAGE,'f'): /* set faulty */
701 devmode = 'f';
702 continue;
703 case O(MANAGE,'R'):
704 case O(ASSEMBLE,'R'):
705 case O(BUILD,'R'):
706 case O(CREATE,'R'): /* Run the array */
707 if (runstop < 0) {
708 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
709 exit(2);
710 }
711 runstop = 1;
712 continue;
713 case O(MANAGE,'S'):
714 if (runstop > 0) {
715 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
716 exit(2);
717 }
718 runstop = -1;
719 continue;
720
721 case O(MANAGE,'o'):
722 if (readonly < 0) {
723 fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
724 exit(2);
725 }
726 readonly = 1;
727 continue;
728 case O(MANAGE,'w'):
729 if (readonly > 0) {
730 fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n");
731 exit(2);
732 }
733 readonly = -1;
734 continue;
735
736 case O(MISC,'Q'):
737 case O(MISC,'D'):
738 case O(MISC,'E'):
739 case O(MISC,'K'):
740 case O(MISC,'R'):
741 case O(MISC,'S'):
742 case O(MISC,'X'):
743 case O(MISC,'o'):
744 case O(MISC,'w'):
745 if (devmode && devmode != opt &&
746 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
747 fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n",
748 devmode =='E'?opt:devmode);
749 exit(2);
750 }
751 devmode = opt;
752 continue;
753 case O(MISC,'t'):
754 test = 1;
755 continue;
756
757 case O(MISC, Sparc22):
758 if (devmode != 'E') {
759 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
760 exit(2);
761 }
762 SparcAdjust = 1;
763 continue;
764
765 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
766 if (!optarg) {
767 fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n");
768 exit(2);
769 }
770 if (strcmp(optarg, "internal")==0) {
771 fprintf(stderr, Name ": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
772 continue;
773 }
774 bitmap_fd = open(optarg, O_RDWR);
775 if (!*optarg || bitmap_fd < 0) {
776 fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno));
777 exit(2);
778 }
779 ident.bitmap_fd = bitmap_fd; /* for Assemble */
780 continue;
781
782 case O(ASSEMBLE, BackupFile):
783 case O(GROW, BackupFile):
784 /* Specify a file into which grow might place a backup,
785 * or from which assemble might recover a backup
786 */
787 if (backup_file) {
788 fprintf(stderr, Name ": backup file already specified, rejecting %s\n", optarg);
789 exit(2);
790 }
791 backup_file = optarg;
792 continue;
793
794 case O(GROW,'b'):
795 case O(BUILD,'b'):
796 case O(CREATE,'b'): /* here we create the bitmap */
797 if (strcmp(optarg, "internal")== 0 ||
798 strcmp(optarg, "none")== 0 ||
799 strchr(optarg, '/') != NULL) {
800 bitmap_file = optarg;
801 continue;
802 }
803 /* probable typo */
804 fprintf(stderr, Name ": bitmap file must contain a '/', or be 'internal', or 'none'\n");
805 exit(2);
806
807 case O(GROW,BitmapChunk):
808 case O(BUILD,BitmapChunk):
809 case O(CREATE,BitmapChunk): /* bitmap chunksize */
810 bitmap_chunk = strtol(optarg, &c, 10);
811 if (!optarg[0] || *c || bitmap_chunk < 0 ||
812 bitmap_chunk & (bitmap_chunk - 1)) {
813 fprintf(stderr, Name ": invalid bitmap chunksize: %s\n",
814 optarg);
815 exit(2);
816 }
817 /* convert K to B, chunk of 0K means 512B */
818 bitmap_chunk = bitmap_chunk ? bitmap_chunk * 1024 : 512;
819 continue;
820
821 case O(BUILD, WriteBehind):
822 case O(CREATE, WriteBehind): /* write-behind mode */
823 write_behind = DEFAULT_MAX_WRITE_BEHIND;
824 if (optarg) {
825 write_behind = strtol(optarg, &c, 10);
826 if (write_behind < 0 || *c ||
827 write_behind > 16383) {
828 fprintf(stderr, Name ": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
829 exit(2);
830 }
831 }
832 continue;
833 }
834 /* We have now processed all the valid options. Anything else is
835 * an error
836 */
837 if (option_index > 0)
838 fprintf(stderr, Name ":option --%s not valid in %s mode\n",
839 long_options[option_index].name,
840 map_num(modes, mode));
841 else
842 fprintf(stderr, Name ": option -%c not valid in %s mode\n",
843 opt, map_num(modes, mode));
844 exit(2);
845
846 }
847
848 if (print_help) {
849 char *help_text = Help;
850 if (print_help == 2)
851 help_text = OptionHelp;
852 else
853 switch (mode) {
854 case ASSEMBLE : help_text = Help_assemble; break;
855 case BUILD : help_text = Help_build; break;
856 case CREATE : help_text = Help_create; break;
857 case MANAGE : help_text = Help_manage; break;
858 case MISC : help_text = Help_misc; break;
859 case MONITOR : help_text = Help_monitor; break;
860 case GROW : help_text = Help_grow; break;
861 }
862 fputs(help_text,stderr);
863 exit(0);
864 }
865
866 if (!mode && devs_found) {
867 mode = MISC;
868 devmode = 'Q';
869 if (devlist->disposition == 0)
870 devlist->disposition = devmode;
871 }
872 if (!mode) {
873 fputs(Usage, stderr);
874 exit(2);
875 }
876
877 if (symlinks) {
878 struct createinfo *ci = conf_get_create_info();
879
880 if (strcasecmp(symlinks, "yes") == 0)
881 ci->symlinks = 1;
882 else if (strcasecmp(symlinks, "no") == 0)
883 ci->symlinks = 0;
884 else {
885 fprintf(stderr, Name ": option --symlinks must be 'no' or 'yes'\n");
886 exit(2);
887 }
888 }
889 /* Ok, got the option parsing out of the way
890 * hopefully it's mostly right but there might be some stuff
891 * missing
892 *
893 * That is mosty checked in the per-mode stuff but...
894 *
895 * For @,B,C and A without -s, the first device listed must be an md device
896 * we check that here and open it.
897 */
898
899 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
900 (mode == ASSEMBLE && ! scan)) {
901 if (devs_found < 1) {
902 fprintf(stderr, Name ": an md device must be given in this mode\n");
903 exit(2);
904 }
905 if ((int)ident.super_minor == -2 && autof) {
906 fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
907 exit(2);
908 }
909 if (mode == MANAGE || mode == GROW)
910 autof=1; /* Don't create */
911 mdfd = open_mddev(devlist->devname, autof);
912 if (mdfd < 0)
913 exit(1);
914 if ((int)ident.super_minor == -2) {
915 struct stat stb;
916 fstat(mdfd, &stb);
917 ident.super_minor = minor(stb.st_rdev);
918 }
919 }
920
921 if (raiddisks) {
922 if (raiddisks > max_disks) {
923 fprintf(stderr, Name ": invalid number of raid devices: %d\n",
924 raiddisks);
925 exit(2);
926 }
927 if (raiddisks == 1 && !force && level != -5) {
928 fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
929 " a mistake. If you really mean it you will need to specify --force before\n"
930 " setting the number of drives.\n");
931 exit(2);
932 }
933 }
934 if (sparedisks) {
935 if ( sparedisks > max_disks - raiddisks) {
936 fprintf(stderr, Name ": invalid number of spare-devices: %d\n",
937 sparedisks);
938 exit(2);
939 }
940 }
941
942 if (homehost == NULL)
943 homehost = conf_get_homehost();
944 if (homehost && strcmp(homehost, "<system>")==0) {
945 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
946 sys_hostname[sizeof(sys_hostname)-1] = 0;
947 homehost = sys_hostname;
948 }
949 }
950
951 rv = 0;
952 switch(mode) {
953 case MANAGE:
954 /* readonly, add/remove, readwrite, runstop */
955 if (readonly>0)
956 rv = Manage_ro(devlist->devname, mdfd, readonly);
957 if (!rv && devs_found>1)
958 rv = Manage_subdevs(devlist->devname, mdfd,
959 devlist->next, verbose-quiet);
960 if (!rv && readonly < 0)
961 rv = Manage_ro(devlist->devname, mdfd, readonly);
962 if (!rv && runstop)
963 rv = Manage_runstop(devlist->devname, mdfd, runstop, quiet);
964 break;
965 case ASSEMBLE:
966 if (devs_found == 1 && ident.uuid_set == 0 &&
967 ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) {
968 /* Only a device has been given, so get details from config file */
969 mddev_ident_t array_ident = conf_get_ident(devlist->devname);
970 if (array_ident == NULL) {
971 fprintf(stderr, Name ": %s not identified in config file.\n",
972 devlist->devname);
973 rv |= 1;
974 } else {
975 mdfd = open_mddev(devlist->devname,
976 array_ident->autof ? array_ident->autof : autof);
977 if (mdfd < 0)
978 rv |= 1;
979 else {
980 rv |= Assemble(ss, devlist->devname, mdfd, array_ident,
981 NULL, backup_file,
982 readonly, runstop, update, homehost, verbose-quiet, force);
983 close(mdfd);
984 }
985 }
986 } else if (!scan)
987 rv = Assemble(ss, devlist->devname, mdfd, &ident,
988 devlist->next, backup_file,
989 readonly, runstop, update, homehost, verbose-quiet, force);
990 else if (devs_found>0) {
991 if (update && devs_found > 1) {
992 fprintf(stderr, Name ": can only update a single array at a time\n");
993 exit(1);
994 }
995 if (backup_file && devs_found > 1) {
996 fprintf(stderr, Name ": can only assemble a single array when providing a backup file.\n");
997 exit(1);
998 }
999 for (dv = devlist ; dv ; dv=dv->next) {
1000 mddev_ident_t array_ident = conf_get_ident(dv->devname);
1001 if (array_ident == NULL) {
1002 fprintf(stderr, Name ": %s not identified in config file.\n",
1003 dv->devname);
1004 rv |= 1;
1005 continue;
1006 }
1007 mdfd = open_mddev(dv->devname,
1008 array_ident->autof ?array_ident->autof : autof);
1009 if (mdfd < 0) {
1010 rv |= 1;
1011 continue;
1012 }
1013 rv |= Assemble(ss, dv->devname, mdfd, array_ident,
1014 NULL, backup_file,
1015 readonly, runstop, update, homehost, verbose-quiet, force);
1016 close(mdfd);
1017 }
1018 } else {
1019 mddev_ident_t array_list = conf_get_ident(NULL);
1020 mddev_dev_t devlist = conf_get_devs();
1021 int cnt = 0;
1022 if (devlist == NULL) {
1023 fprintf(stderr, Name ": No devices listed in conf file were found.\n");
1024 exit(1);
1025 }
1026 if (update) {
1027 fprintf(stderr, Name ": --update not meaningful with a --scan assembly.\n");
1028 exit(1);
1029 }
1030 if (backup_file) {
1031 fprintf(stderr, Name ": --backup_file not meaningful with a --scan assembly.\n");
1032 exit(1);
1033 }
1034 for (; array_list; array_list = array_list->next) {
1035 mdu_array_info_t array;
1036 mdfd = open_mddev(array_list->devname,
1037 array_list->autof ? array_list->autof : autof);
1038 if (mdfd < 0) {
1039 rv |= 1;
1040 continue;
1041 }
1042 if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
1043 /* already assembled, skip */
1044 cnt++;
1045 else {
1046 rv |= Assemble(ss, array_list->devname, mdfd,
1047 array_list,
1048 NULL, NULL,
1049 readonly, runstop, NULL, homehost, verbose-quiet, force);
1050 if (rv == 0) cnt++;
1051 }
1052 close(mdfd);
1053 }
1054 if (homehost) {
1055 /* Maybe we can auto-assemble something.
1056 * Repeatedly call Assemble in auto-assmble mode
1057 * until it fails
1058 */
1059 int rv2;
1060 int acnt;
1061 ident.autof = autof;
1062 do {
1063 acnt = 0;
1064 do {
1065 rv2 = Assemble(ss, NULL, -1,
1066 &ident,
1067 NULL, NULL,
1068 readonly, runstop, NULL, homehost, verbose-quiet, force);
1069 if (rv2==0) {
1070 cnt++;
1071 acnt++;
1072 }
1073 if (rv2 == 1)
1074 /* found something so even though assembly failed we
1075 * want to avoid auto-updates
1076 */
1077 auto_update_home = 0;
1078 } while (rv2!=2);
1079 /* Incase there are stacked devices, we need to go around again */
1080 } while (acnt);
1081 if (cnt == 0 && auto_update_home && homehost) {
1082 /* Nothing found, maybe we need to bootstrap homehost info */
1083 do {
1084 acnt = 0;
1085 do {
1086 rv2 = Assemble(ss, NULL, -1,
1087 &ident,
1088 NULL, NULL,
1089 readonly, runstop, "homehost", homehost, verbose-quiet, force);
1090 if (rv2==0) {
1091 cnt++;
1092 acnt++;
1093 }
1094 } while (rv2!=2);
1095 /* Incase there are stacked devices, we need to go around again */
1096 } while (acnt);
1097 }
1098 if (cnt == 0 && rv == 0) {
1099 fprintf(stderr, Name ": No arrays found in config file or automatically\n");
1100 rv = 1;
1101 }
1102 } else if (cnt == 0 && rv == 0) {
1103 fprintf(stderr, Name ": No arrays found in config file\n");
1104 rv = 1;
1105 }
1106 }
1107 break;
1108 case BUILD:
1109 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1110 if (write_behind && !bitmap_file) {
1111 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1112 rv = 1;
1113 break;
1114 }
1115
1116 if (bitmap_file) {
1117 if (strcmp(bitmap_file, "internal")==0) {
1118 fprintf(stderr, Name ": 'internal' bitmaps not supported with --build\n");
1119 rv |= 1;
1120 break;
1121 }
1122 }
1123 rv = Build(devlist->devname, mdfd, chunk, level, layout,
1124 raiddisks, devlist->next, assume_clean,
1125 bitmap_file, bitmap_chunk, write_behind, delay, verbose-quiet);
1126 break;
1127 case CREATE:
1128 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1129 if (write_behind && !bitmap_file) {
1130 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1131 rv = 1;
1132 break;
1133 }
1134
1135 rv = Create(ss, devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size,
1136 raiddisks, sparedisks, ident.name, homehost,
1137 devs_found-1, devlist->next, runstop, verbose-quiet, force, assume_clean,
1138 bitmap_file, bitmap_chunk, write_behind, delay);
1139 break;
1140 case MISC:
1141
1142 if (devmode == 'E') {
1143 if (devlist == NULL && !scan) {
1144 fprintf(stderr, Name ": No devices to examine\n");
1145 exit(2);
1146 }
1147 if (devlist == NULL)
1148 devlist = conf_get_devs();
1149 if (devlist == NULL) {
1150 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
1151 exit(1);
1152 }
1153 if (brief && verbose)
1154 brief = 2;
1155 rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief, scan, SparcAdjust, ss, homehost);
1156 } else {
1157 if (devlist == NULL) {
1158 if (devmode=='D' && scan) {
1159 /* apply --detail to all devices in /proc/mdstat */
1160 struct mdstat_ent *ms = mdstat_read(0, 1);
1161 struct mdstat_ent *e;
1162 for (e=ms ; e ; e=e->next) {
1163 char *name = get_md_name(e->devnum);
1164
1165 if (!name) {
1166 fprintf(stderr, Name ": cannot find device file for %s\n",
1167 e->dev);
1168 continue;
1169 }
1170 rv |= Detail(name, verbose>1?0:verbose+1, test, homehost);
1171 put_md_name(name);
1172 }
1173 } else if (devmode == 'S' && scan) {
1174 /* apply --stop to all devices in /proc/mdstat */
1175 /* Due to possible stacking of devices, repeat until
1176 * nothing more can be stopped
1177 */
1178 int progress=1, err;
1179 int last = 0;
1180 do {
1181 struct mdstat_ent *ms = mdstat_read(0, 0);
1182 struct mdstat_ent *e;
1183
1184 if (!progress) last = 1;
1185 progress = 0; err = 0;
1186 for (e=ms ; e ; e=e->next) {
1187 char *name = get_md_name(e->devnum);
1188
1189 if (!name) {
1190 fprintf(stderr, Name ": cannot find device file for %s\n",
1191 e->dev);
1192 continue;
1193 }
1194 mdfd = open_mddev(name, 1);
1195 if (mdfd >= 0) {
1196 if (Manage_runstop(name, mdfd, -1, quiet?1:last?0:-1))
1197 err = 1;
1198 else
1199 progress = 1;
1200 close(mdfd);
1201 }
1202
1203 put_md_name(name);
1204 }
1205 } while (!last && err);
1206 if (err) rv |= 1;
1207 } else {
1208 fprintf(stderr, Name ": No devices given.\n");
1209 exit(2);
1210 }
1211 }
1212 for (dv=devlist ; dv; dv=dv->next) {
1213 switch(dv->disposition) {
1214 case 'D':
1215 rv |= Detail(dv->devname, brief?1+verbose:0, test, homehost); continue;
1216 case 'K': /* Zero superblock */
1217 rv |= Kill(dv->devname, force, quiet); continue;
1218 case 'Q':
1219 rv |= Query(dv->devname); continue;
1220 case 'X':
1221 rv |= ExamineBitmap(dv->devname, brief, ss); continue;
1222 }
1223 mdfd = open_mddev(dv->devname, 1);
1224 if (mdfd>=0) {
1225 switch(dv->disposition) {
1226 case 'R':
1227 rv |= Manage_runstop(dv->devname, mdfd, 1, quiet); break;
1228 case 'S':
1229 rv |= Manage_runstop(dv->devname, mdfd, -1, quiet); break;
1230 case 'o':
1231 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1232 case 'w':
1233 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1234 }
1235 close(mdfd);
1236 } else
1237 rv |= 1;
1238 }
1239 }
1240 break;
1241 case MONITOR:
1242 if (!devlist && !scan) {
1243 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
1244 rv = 1;
1245 break;
1246 }
1247 if (pidfile && !daemonise) {
1248 fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
1249 rv = 1;
1250 break;
1251 }
1252 rv= Monitor(devlist, mailaddr, program,
1253 delay?delay:60, daemonise, scan, oneshot,
1254 dosyslog, test, pidfile);
1255 break;
1256
1257 case GROW:
1258 if (devs_found > 1) {
1259
1260 /* must be '-a'. */
1261 if (size >= 0 || raiddisks) {
1262 fprintf(stderr, Name ": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1263 rv = 1;
1264 break;
1265 }
1266 for (dv=devlist->next; dv ; dv=dv->next) {
1267 rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
1268 if (rv)
1269 break;
1270 }
1271 } else if ((size >= 0) + (raiddisks != 0) + (layout != UnSet) + (bitmap_file != NULL)> 1) {
1272 fprintf(stderr, Name ": can change at most one of size, raiddisks, bitmap, and layout\n");
1273 rv = 1;
1274 break;
1275 } else if (layout != UnSet)
1276 rv = Manage_reconfig(devlist->devname, mdfd, layout);
1277 else if (size >= 0 || raiddisks)
1278 rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file,
1279 size, level, layout, chunk, raiddisks);
1280 else if (bitmap_file) {
1281 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1282 rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file,
1283 bitmap_chunk, delay, write_behind, force);
1284 } else
1285 fprintf(stderr, Name ": no changes to --grow\n");
1286 break;
1287 }
1288 exit(rv);
1289 }