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