mdadm-1.7.0
[thirdparty/mdadm.git] / mdadm.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
cd29a5c8 4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
64c4757e
NB
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
9a9dab36 30#include "mdadm.h"
64c4757e 31#include "md_p.h"
dd0781e5 32#include <ctype.h>
64c4757e 33
dd0781e5
NB
34
35void make_parts(char *dev, int cnt)
64c4757e 36{
dd0781e5
NB
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);
52826846 183 if (mdfd < 0)
e0d19036 184 fprintf(stderr, Name ": error opening %s: %s\n",
52826846
NB
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;
64c4757e 191 }
52826846
NB
192 return mdfd;
193}
64c4757e 194
64c4757e 195
52826846
NB
196
197int main(int argc, char *argv[])
198{
e0d19036 199 int mode = 0;
52826846 200 int opt;
e0d19036 201 int option_index;
52826846
NB
202 char *help_text;
203 char *c;
204 int rv;
52826846
NB
205
206 int chunk = 0;
dd0781e5 207 int size = -1;
98c6faba
NB
208 int level = UnSet;
209 int layout = UnSet;
52826846
NB
210 int raiddisks = 0;
211 int sparedisks = 0;
212 struct mddev_ident_s ident;
213 char *configfile = NULL;
cd29a5c8 214 char *cp;
5787fa49 215 char *update = NULL;
52826846
NB
216 int scan = 0;
217 char devmode = 0;
218 int runstop = 0;
219 int readonly = 0;
bd526cee 220 int SparcAdjust = 0;
cd29a5c8
NB
221 mddev_dev_t devlist = NULL;
222 mddev_dev_t *devlistend = & devlist;
223 mddev_dev_t dv;
52826846
NB
224 int devs_found = 0;
225 int verbose = 0;
cd29a5c8 226 int brief = 0;
52826846 227 int force = 0;
feb716e9 228 int test = 0;
dd0781e5
NB
229 int assume_clean = 0;
230 int autof = 0; /* -1 for non-partitions, 1 or more to create partitions */
52826846
NB
231
232 char *mailaddr = NULL;
233 char *program = NULL;
234 int delay = 0;
d013a55e 235 int daemonise = 0;
aa88f531 236 int oneshot = 0;
52826846 237
e5329c37
NB
238 int copies;
239
52826846
NB
240 int mdfd = -1;
241
242 ident.uuid_set=0;
98c6faba
NB
243 ident.level = UnSet;
244 ident.raid_disks = UnSet;
245 ident.super_minor= UnSet;
52826846
NB
246 ident.devices=0;
247
e0d19036
NB
248 while ((option_index = -1) ,
249 (opt=getopt_long(argc, argv,
52826846 250 short_options, long_options,
e0d19036
NB
251 &option_index)) != -1) {
252 int newmode = mode;
253 /* firstly, so mode-independant options */
52826846 254 switch(opt) {
52826846
NB
255 case 'h':
256 help_text = Help;
56eedc1a
NB
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;
dd0781e5 268 case GROW : help_text = Help_grow; break;
56eedc1a 269 }
52826846
NB
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
cd29a5c8
NB
280 case 'b': brief = 1;
281 continue;
282
e0d19036
NB
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;
dd0781e5 305 case 'G': newmode = GROW; break;
e0d19036
NB
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);
cd29a5c8 339 }
e0d19036
NB
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':
dd0781e5 356 case 'G':
e0d19036
NB
357 continue;
358 }
359 if (opt == 1) {
360 /* an undecorated option - must be a device name.
361 */
cd29a5c8 362 if (devs_found > 0 && mode == '@' && !devmode) {
dd0781e5 363 fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
52826846
NB
364 exit(2);
365 }
e5329c37
NB
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 }
cd29a5c8
NB
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
52826846
NB
381 devs_found++;
382 continue;
682c7051 383 }
682c7051 384
52826846
NB
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)) {
e0d19036
NB
389 case O(CREATE,'c'):
390 case O(BUILD,'c'): /* chunk or rounding */
52826846
NB
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;
64c4757e 403
dd0781e5 404 case O(GROW,'z'):
e0d19036 405 case O(CREATE,'z'): /* size */
dd0781e5 406 if (size >= 0) {
52826846
NB
407 fprintf(stderr, Name ": size may only be specified once. "
408 "Second value is %s.\n", optarg);
409 exit(2);
410 }
dd0781e5
NB
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 }
52826846
NB
420 }
421 continue;
64c4757e 422
e0d19036
NB
423 case O(CREATE,'l'):
424 case O(BUILD,'l'): /* set raid level*/
98c6faba 425 if (level != UnSet) {
52826846
NB
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);
98c6faba 431 if (level == UnSet) {
52826846
NB
432 fprintf(stderr, Name ": invalid raid level: %s\n",
433 optarg);
434 exit(2);
435 }
dd0781e5 436 if (level != 0 && level != -1 && level != 1 && level != -4 && mode == BUILD) {
52826846
NB
437 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
438 optarg);
439 exit(2);
440 }
e0d19036 441 if (sparedisks > 0 && level < 1 && level >= -1) {
b83d95f3 442 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
52826846
NB
443 optarg);
444 exit(2);
445 }
cd29a5c8 446 ident.level = level;
52826846 447 continue;
64c4757e 448
e0d19036 449 case O(CREATE,'p'): /* raid5 layout */
e5329c37 450 if (layout != UnSet) {
52826846
NB
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:
56eb10c0 457 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
52826846
NB
458 map_num(pers, level));
459 exit(2);
98c6faba 460 case UnSet:
52826846
NB
461 fprintf(stderr, Name ": raid level must be given before layout.\n");
462 exit(2);
463
464 case 5:
98c6faba 465 case 6:
52826846 466 layout = map_name(r5layout, optarg);
98c6faba 467 if (layout==UnSet) {
52826846
NB
468 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
469 optarg);
470 exit(2);
471 }
472 break;
e5329c37
NB
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;
52826846
NB
488 }
489 continue;
490
dd0781e5
NB
491 case O(CREATE,3):
492 case O(BUILD,3): /* assume clean */
493 assume_clean = 1;
494 continue;
495
496 case O(GROW,'n'):
e0d19036
NB
497 case O(CREATE,'n'):
498 case O(BUILD,'n'): /* number of raid disks */
52826846 499 if (raiddisks) {
b83d95f3 500 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
52826846
NB
501 raiddisks, optarg);
502 exit(2);
503 }
504 raiddisks = strtol(optarg, &c, 10);
505 if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) {
b83d95f3 506 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
52826846
NB
507 optarg);
508 exit(2);
509 }
aa88f531
NB
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 }
cd29a5c8 516 ident.raid_disks = raiddisks;
52826846
NB
517 continue;
518
e0d19036 519 case O(CREATE,'x'): /* number of spare (eXtra) discs */
52826846 520 if (sparedisks) {
b83d95f3 521 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
52826846
NB
522 sparedisks, optarg);
523 exit(2);
524 }
98c6faba 525 if (level != UnSet && level <= 0 && level >= -1) {
b83d95f3 526 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
52826846
NB
527 level);
528 exit(2);
529 }
530 sparedisks = strtol(optarg, &c, 10);
531 if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) {
b83d95f3 532 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
52826846
NB
533 optarg);
534 exit(2);
535 }
536 continue;
dd0781e5
NB
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
aa88f531 574 case O(BUILD,'f'): /* force honouring '-n 1' */
e0d19036
NB
575 case O(CREATE,'f'): /* force honouring of device list */
576 case O(ASSEMBLE,'f'): /* force assembly */
577 case O(MISC,'f'): /* force zero */
52826846
NB
578 force=1;
579 continue;
580
581 /* now for the Assemble options */
e0d19036 582 case O(ASSEMBLE,'u'): /* uuid of array */
52826846 583 if (ident.uuid_set) {
cd29a5c8 584 fprintf(stderr, Name ": uuid cannot be set twice. "
52826846
NB
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
e0d19036 596 case O(ASSEMBLE,'m'): /* super-minor for array */
98c6faba 597 if (ident.super_minor != UnSet) {
cd29a5c8
NB
598 fprintf(stderr, Name ": super-minor cannot be set twice. "
599 "Second value: %s.\n", optarg);
600 exit(2);
601 }
d013a55e
NB
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 }
cd29a5c8
NB
610 }
611 continue;
612
5787fa49
NB
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;
e5329c37
NB
620 if (strcmp(update, "sparc2.2")==0)
621 continue;
5787fa49
NB
622 if (strcmp(update, "super-minor") == 0)
623 continue;
feb716e9
NB
624 if (strcmp(update, "summaries")==0)
625 continue;
e5329c37
NB
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);
5787fa49
NB
629 exit(2);
630
e0d19036 631 case O(ASSEMBLE,'c'): /* config file */
2d465520 632 case O(MISC, 'c'):
e0d19036 633 case O(MONITOR,'c'):
52826846
NB
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;
e0d19036
NB
642 case O(ASSEMBLE,'s'): /* scan */
643 case O(MISC,'s'):
644 case O(MONITOR,'s'):
52826846
NB
645 scan = 1;
646 continue;
647
e0d19036 648 case O(MONITOR,'m'): /* mail address */
52826846
NB
649 if (mailaddr)
650 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
651 optarg);
652 else
653 mailaddr = optarg;
654 continue;
655
e0d19036 656 case O(MONITOR,'p'): /* alert program */
52826846
NB
657 if (program)
658 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
659 optarg);
660 else
661 program = optarg;
662 continue;
64c4757e 663
e0d19036 664 case O(MONITOR,'d'): /* delay in seconds */
52826846
NB
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;
d013a55e
NB
677 case O(MONITOR,'f'): /* daemonise */
678 daemonise = 1;
679 continue;
aa88f531
NB
680 case O(MONITOR,'1'): /* oneshot */
681 oneshot = 1;
682 continue;
98c6faba
NB
683 case O(MONITOR,'t'): /* test */
684 test = 1;
685 continue;
52826846
NB
686
687 /* now the general management options. Some are applicable
688 * to other modes. None have arguments.
689 */
e5329c37 690 case O(GROW,'a'):
dd0781e5 691 case O(MANAGE,'a'): /* add a drive */
52826846
NB
692 devmode = 'a';
693 continue;
e0d19036 694 case O(MANAGE,'r'): /* remove a drive */
52826846
NB
695 devmode = 'r';
696 continue;
e0d19036 697 case O(MANAGE,'f'): /* set faulty */
52826846
NB
698 devmode = 'f';
699 continue;
e0d19036
NB
700 case O(MANAGE,'R'):
701 case O(ASSEMBLE,'R'):
702 case O(BUILD,'R'):
703 case O(CREATE,'R'): /* Run the array */
52826846
NB
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;
e0d19036 710 case O(MANAGE,'S'):
52826846
NB
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
e0d19036 718 case O(MANAGE,'o'):
52826846
NB
719 if (readonly < 0) {
720 fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
721 exit(2);
722 }
723 readonly = 1;
724 continue;
e0d19036 725 case O(MANAGE,'w'):
52826846 726 if (readonly > 0) {
e0d19036 727 fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n");
52826846
NB
728 exit(2);
729 }
730 readonly = -1;
731 continue;
e0d19036
NB
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;
feb716e9
NB
749 case O(MISC,'t'):
750 test = 1;
751 continue;
e0d19036 752
bd526cee
NB
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;
52826846
NB
760 }
761 /* We have now processed all the valid options. Anything else is
762 * an error
763 */
e0d19036
NB
764 fprintf(stderr, Name ": option %c not valid in %s mode\n",
765 opt, map_num(modes, mode));
64c4757e 766 exit(2);
52826846
NB
767
768 }
769
770 if (!mode) {
771 fputs(Usage, stderr);
64c4757e 772 exit(2);
64c4757e 773 }
52826846
NB
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 *
e0d19036 778 * That is mosty checked in the per-mode stuff but...
52826846
NB
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.
64c4757e 782 */
52826846 783
dd0781e5
NB
784 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
785 (mode == ASSEMBLE && ! scan)) {
52826846
NB
786 if (devs_found < 1) {
787 fprintf(stderr, Name ": an md device must be given in this mode\n");
788 exit(2);
789 }
dd0781e5
NB
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);
52826846
NB
795 if (mdfd < 0)
796 exit(1);
98c6faba 797 if ((int)ident.super_minor == -2) {
d013a55e
NB
798 struct stat stb;
799 fstat(mdfd, &stb);
800 ident.super_minor = MINOR(stb.st_rdev);
801 }
64c4757e 802 }
52826846 803
52826846
NB
804 rv = 0;
805 switch(mode) {
e0d19036 806 case MANAGE:
52826846
NB
807 /* readonly, add/remove, readwrite, runstop */
808 if (readonly>0)
cd29a5c8 809 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 810 if (!rv && devs_found>1)
cd29a5c8
NB
811 rv = Manage_subdevs(devlist->devname, mdfd,
812 devlist->next);
52826846 813 if (!rv && readonly < 0)
cd29a5c8 814 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 815 if (!rv && runstop)
cd29a5c8 816 rv = Manage_runstop(devlist->devname, mdfd, runstop);
52826846 817 break;
e0d19036 818 case ASSEMBLE:
d013a55e 819 if (devs_found == 1 && ident.uuid_set == 0 &&
98c6faba 820 ident.super_minor == UnSet && !scan ) {
d013a55e
NB
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);
dd0781e5 823 mdfd = open_mddev(devlist->devname, array_ident->autof);
d013a55e
NB
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)
cd29a5c8
NB
838 rv = Assemble(devlist->devname, mdfd, &ident, configfile,
839 devlist->next,
5787fa49
NB
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 }
cd29a5c8
NB
846 for (dv = devlist ; dv ; dv=dv->next) {
847 mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname);
dd0781e5 848 mdfd = open_mddev(dv->devname, array_ident->autof);
52826846
NB
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",
cd29a5c8 855 dv->devname);
52826846
NB
856 rv |= 1;
857 continue;
858 }
cd29a5c8
NB
859 rv |= Assemble(dv->devname, mdfd, array_ident, configfile,
860 NULL,
5787fa49 861 readonly, runstop, update, verbose, force);
52826846 862 }
5787fa49 863 } else {
52826846
NB
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) {
cd29a5c8 870 mdu_array_info_t array;
dd0781e5 871 mdfd = open_mddev(array_list->devname, array_list->autof);
52826846
NB
872 if (mdfd < 0) {
873 rv |= 1;
874 continue;
875 }
cd29a5c8
NB
876 if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
877 /* already assembled, skip */
878 continue;
52826846
NB
879 rv |= Assemble(array_list->devname, mdfd,
880 array_list, configfile,
cd29a5c8 881 NULL,
5787fa49 882 readonly, runstop, NULL, verbose, force);
52826846
NB
883 }
884 }
885 break;
e0d19036 886 case BUILD:
dd0781e5 887 rv = Build(devlist->devname, mdfd, chunk, level, raiddisks, devlist->next, assume_clean);
52826846 888 break;
e0d19036 889 case CREATE:
dd0781e5 890 rv = Create(devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size,
52826846 891 raiddisks, sparedisks,
cd29a5c8 892 devs_found-1, devlist->next, runstop, verbose, force);
52826846 893 break;
e0d19036
NB
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) {
c913b90e 904 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
e0d19036
NB
905 exit(1);
906 }
bd526cee 907 rv = Examine(devlist, scan?!verbose:brief, scan, SparcAdjust);
e0d19036
NB
908 } else {
909 if (devlist == NULL) {
910 if ((devmode == 'S' ||devmode=='D') && scan) {
911 /* apply to all devices in /proc/mdstat */
dd0781e5 912 struct mdstat_ent *ms = mdstat_read(0);
e0d19036
NB
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')
feb716e9 923 rv |= Detail(name, !verbose, test);
e0d19036 924 else if (devmode=='S') {
dd0781e5 925 mdfd = open_mddev(name, 0);
e0d19036
NB
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':
feb716e9 939 rv |= Detail(dv->devname, brief, test); continue;
e0d19036
NB
940 case 'K': /* Zero superblock */
941 rv |= Kill(dv->devname, force); continue;
942 case 'Q':
943 rv |= Query(dv->devname); continue;
944 }
dd0781e5 945 mdfd = open_mddev(dv->devname, 0);
e0d19036
NB
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 }
cd29a5c8 958 }
9a9dab36 959 break;
e0d19036 960 case MONITOR:
e0d19036
NB
961 if (!devlist && !scan) {
962 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
963 rv = 1;
964 break;
965 }
d013a55e 966 rv= Monitor(devlist, mailaddr, program,
98c6faba 967 delay?delay:60, daemonise, scan, oneshot, configfile, test);
9a9dab36 968 break;
dd0781e5
NB
969
970 case GROW:
971 if (devs_found > 1) {
e5329c37
NB
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) {
dd0781e5
NB
985 fprintf(stderr, Name ": can only grow size OR raiddisks, not both\n");
986 rv = 1;
987 break;
e5329c37
NB
988 } else
989 rv = Manage_resize(devlist->devname, mdfd, size, raiddisks);
dd0781e5 990 break;
64c4757e 991 }
52826846 992 exit(rv);
64c4757e 993}