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