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