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