2.0-devel-1 release
[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
c82f047c
NB
28 *
29 * Additions for bitmap and async RAID options, Copyright (C) 2003-2004,
30 * Paul Clements, SteelEye Technology, Inc.
64c4757e
NB
31 */
32
9a9dab36 33#include "mdadm.h"
64c4757e 34#include "md_p.h"
dd0781e5 35#include <ctype.h>
64c4757e 36
dd0781e5 37
52826846
NB
38int main(int argc, char *argv[])
39{
e0d19036 40 int mode = 0;
52826846 41 int opt;
e0d19036 42 int option_index;
52826846
NB
43 char *help_text;
44 char *c;
45 int rv;
f9ce90ba 46 int i;
52826846
NB
47
48 int chunk = 0;
dd0781e5 49 int size = -1;
98c6faba
NB
50 int level = UnSet;
51 int layout = UnSet;
52826846
NB
52 int raiddisks = 0;
53 int sparedisks = 0;
54 struct mddev_ident_s ident;
55 char *configfile = NULL;
cd29a5c8 56 char *cp;
5787fa49 57 char *update = NULL;
52826846
NB
58 int scan = 0;
59 char devmode = 0;
60 int runstop = 0;
61 int readonly = 0;
c82f047c
NB
62 int bitmap_fd = -1;
63 char *bitmap_file = NULL;
64 int bitmap_chunk = UnSet;
bd526cee 65 int SparcAdjust = 0;
cd29a5c8
NB
66 mddev_dev_t devlist = NULL;
67 mddev_dev_t *devlistend = & devlist;
68 mddev_dev_t dv;
52826846
NB
69 int devs_found = 0;
70 int verbose = 0;
cd29a5c8 71 int brief = 0;
52826846 72 int force = 0;
feb716e9 73 int test = 0;
dd0781e5 74 int assume_clean = 0;
8d80900b
NB
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 */
52826846
NB
83
84 char *mailaddr = NULL;
85 char *program = NULL;
86 int delay = 0;
d013a55e 87 int daemonise = 0;
b5e64645 88 char *pidfile = NULL;
aa88f531 89 int oneshot = 0;
82d9eba6 90 struct supertype *ss = NULL;
52826846 91
e5329c37
NB
92 int copies;
93
52826846
NB
94 int mdfd = -1;
95
dfe47e00
NB
96 srandom(time(0) ^ getpid());
97
52826846 98 ident.uuid_set=0;
98c6faba
NB
99 ident.level = UnSet;
100 ident.raid_disks = UnSet;
101 ident.super_minor= UnSet;
52826846 102 ident.devices=0;
a75f2fbc
NB
103 ident.spare_group = NULL;
104 ident.autof = 0;
a825febc 105 ident.st = NULL;
c82f047c 106 ident.bitmap_fd = -1;
52826846 107
e0d19036
NB
108 while ((option_index = -1) ,
109 (opt=getopt_long(argc, argv,
52826846 110 short_options, long_options,
e0d19036
NB
111 &option_index)) != -1) {
112 int newmode = mode;
113 /* firstly, so mode-independant options */
52826846 114 switch(opt) {
52826846
NB
115 case 'h':
116 help_text = Help;
56eedc1a
NB
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;
dd0781e5 128 case GROW : help_text = Help_grow; break;
56eedc1a 129 }
52826846
NB
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
c82f047c
NB
140 case 'b':
141 if (mode == ASSEMBLE || mode == BUILD || mode == CREATE)
142 break; /* b means bitmap */
143 brief = 1;
cd29a5c8
NB
144 continue;
145
e0d19036
NB
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':
0320ea45
NB
162 if (!mode) newmode = MANAGE;
163 break;
e0d19036
NB
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;
dd0781e5 169 case 'G': newmode = GROW; break;
e0d19036
NB
170
171 case '#':
172 case 'D':
173 case 'E':
c82f047c 174 case 'X':
e0d19036
NB
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);
e6b64cd0 191 fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
e0d19036
NB
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);
cd29a5c8 204 }
0320ea45
NB
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 }
e0d19036 227 if (option_index >= 0)
0320ea45 228 fprintf(stderr, Name ": --%s", long_options[option_index].name);
e0d19036 229 else
0320ea45
NB
230 fprintf(stderr, Name ": -%c", opt);
231 fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
e0d19036
NB
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':
dd0781e5 243 case 'G':
e0d19036
NB
244 continue;
245 }
246 if (opt == 1) {
247 /* an undecorated option - must be a device name.
248 */
cd29a5c8 249 if (devs_found > 0 && mode == '@' && !devmode) {
dd0781e5 250 fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
52826846
NB
251 exit(2);
252 }
e5329c37
NB
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 }
cd29a5c8
NB
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
52826846
NB
268 devs_found++;
269 continue;
682c7051 270 }
682c7051 271
52826846
NB
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)) {
e0d19036
NB
276 case O(CREATE,'c'):
277 case O(BUILD,'c'): /* chunk or rounding */
52826846
NB
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;
64c4757e 290
f9ce90ba
NB
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 }
82d9eba6
NB
298 for(i=0; !ss && superlist[i]; i++)
299 ss = superlist[i]->match_metadata_desc(optarg);
300
f9ce90ba
NB
301 if (!ss) {
302 fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg);
303 exit(2);
304 }
305 continue;
306
dd0781e5 307 case O(GROW,'z'):
e0d19036 308 case O(CREATE,'z'): /* size */
dd0781e5 309 if (size >= 0) {
52826846
NB
310 fprintf(stderr, Name ": size may only be specified once. "
311 "Second value is %s.\n", optarg);
312 exit(2);
313 }
dd0781e5
NB
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 }
52826846
NB
323 }
324 continue;
64c4757e 325
b5e64645 326 case O(GROW,'l'): /* hack - needed to understand layout */
e0d19036
NB
327 case O(CREATE,'l'):
328 case O(BUILD,'l'): /* set raid level*/
98c6faba 329 if (level != UnSet) {
52826846
NB
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);
98c6faba 335 if (level == UnSet) {
52826846
NB
336 fprintf(stderr, Name ": invalid raid level: %s\n",
337 optarg);
338 exit(2);
339 }
b5e64645 340 if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) {
52826846
NB
341 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
342 optarg);
343 exit(2);
344 }
e0d19036 345 if (sparedisks > 0 && level < 1 && level >= -1) {
b83d95f3 346 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
52826846
NB
347 optarg);
348 exit(2);
349 }
cd29a5c8 350 ident.level = level;
52826846 351 continue;
64c4757e 352
e0d19036 353 case O(CREATE,'p'): /* raid5 layout */
b5e64645
NB
354 case O(BUILD,'p'): /* faulty layout */
355 case O(GROW, 'p'): /* faulty reconfig */
e5329c37 356 if (layout != UnSet) {
52826846
NB
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:
56eb10c0 363 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
52826846
NB
364 map_num(pers, level));
365 exit(2);
98c6faba 366 case UnSet:
52826846
NB
367 fprintf(stderr, Name ": raid level must be given before layout.\n");
368 exit(2);
369
370 case 5:
98c6faba 371 case 6:
52826846 372 layout = map_name(r5layout, optarg);
98c6faba 373 if (layout==UnSet) {
52826846
NB
374 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
375 optarg);
376 exit(2);
377 }
378 break;
e5329c37
NB
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;
b5e64645
NB
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 }
52826846
NB
411 }
412 continue;
413
dd0781e5
NB
414 case O(CREATE,3):
415 case O(BUILD,3): /* assume clean */
416 assume_clean = 1;
417 continue;
418
419 case O(GROW,'n'):
e0d19036
NB
420 case O(CREATE,'n'):
421 case O(BUILD,'n'): /* number of raid disks */
52826846 422 if (raiddisks) {
b83d95f3 423 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
52826846
NB
424 raiddisks, optarg);
425 exit(2);
426 }
427 raiddisks = strtol(optarg, &c, 10);
428 if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) {
b83d95f3 429 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
52826846
NB
430 optarg);
431 exit(2);
432 }
b5e64645 433 if (raiddisks == 1 && !force && level != -5) {
aa88f531
NB
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 }
cd29a5c8 439 ident.raid_disks = raiddisks;
52826846
NB
440 continue;
441
e0d19036 442 case O(CREATE,'x'): /* number of spare (eXtra) discs */
52826846 443 if (sparedisks) {
b83d95f3 444 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
52826846
NB
445 sparedisks, optarg);
446 exit(2);
447 }
98c6faba 448 if (level != UnSet && level <= 0 && level >= -1) {
b83d95f3 449 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
52826846
NB
450 level);
451 exit(2);
452 }
453 sparedisks = strtol(optarg, &c, 10);
454 if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) {
b83d95f3 455 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
52826846
NB
456 optarg);
457 exit(2);
458 }
459 continue;
dd0781e5
NB
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)
8d80900b 465 autof = -2;
dd0781e5
NB
466 else if (strcasecmp(optarg,"no")==0)
467 autof = 0;
8d80900b
NB
468 else if (strcasecmp(optarg,"yes")==0)
469 autof = -2;
470 else if (strcasecmp(optarg,"md")==0)
dd0781e5
NB
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
aa88f531 499 case O(BUILD,'f'): /* force honouring '-n 1' */
e0d19036
NB
500 case O(CREATE,'f'): /* force honouring of device list */
501 case O(ASSEMBLE,'f'): /* force assembly */
502 case O(MISC,'f'): /* force zero */
52826846
NB
503 force=1;
504 continue;
505
506 /* now for the Assemble options */
e0d19036 507 case O(ASSEMBLE,'u'): /* uuid of array */
52826846 508 if (ident.uuid_set) {
cd29a5c8 509 fprintf(stderr, Name ": uuid cannot be set twice. "
52826846
NB
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
e0d19036 521 case O(ASSEMBLE,'m'): /* super-minor for array */
98c6faba 522 if (ident.super_minor != UnSet) {
cd29a5c8
NB
523 fprintf(stderr, Name ": super-minor cannot be set twice. "
524 "Second value: %s.\n", optarg);
525 exit(2);
526 }
d013a55e
NB
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 }
cd29a5c8
NB
535 }
536 continue;
537
5787fa49
NB
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;
e5329c37
NB
545 if (strcmp(update, "sparc2.2")==0)
546 continue;
5787fa49
NB
547 if (strcmp(update, "super-minor") == 0)
548 continue;
feb716e9
NB
549 if (strcmp(update, "summaries")==0)
550 continue;
e5329c37
NB
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);
5787fa49
NB
554 exit(2);
555
e0d19036 556 case O(ASSEMBLE,'c'): /* config file */
2d465520 557 case O(MISC, 'c'):
e0d19036 558 case O(MONITOR,'c'):
52826846
NB
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;
e0d19036
NB
567 case O(ASSEMBLE,'s'): /* scan */
568 case O(MISC,'s'):
569 case O(MONITOR,'s'):
52826846
NB
570 scan = 1;
571 continue;
572
e0d19036 573 case O(MONITOR,'m'): /* mail address */
52826846
NB
574 if (mailaddr)
575 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
576 optarg);
577 else
578 mailaddr = optarg;
579 continue;
580
e0d19036 581 case O(MONITOR,'p'): /* alert program */
52826846
NB
582 if (program)
583 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
584 optarg);
585 else
586 program = optarg;
587 continue;
64c4757e 588
e0d19036 589 case O(MONITOR,'d'): /* delay in seconds */
c82f047c
NB
590 case O(BUILD,'d'): /* delay for bitmap updates */
591 case O(CREATE,'d'):
52826846
NB
592 if (delay)
593 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
594 optarg);
595 else {
596 delay = strtol(optarg, &c, 10);
597 if (!optarg[0] || *c || delay<1) {
598 fprintf(stderr, Name ": invalid delay: %s\n",
599 optarg);
600 exit(2);
601 }
602 }
603 continue;
d013a55e
NB
604 case O(MONITOR,'f'): /* daemonise */
605 daemonise = 1;
606 continue;
b5e64645
NB
607 case O(MONITOR,'i'): /* pid */
608 if (pidfile)
609 fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
610 optarg);
611 else
612 pidfile = optarg;
613 continue;
aa88f531
NB
614 case O(MONITOR,'1'): /* oneshot */
615 oneshot = 1;
616 continue;
98c6faba
NB
617 case O(MONITOR,'t'): /* test */
618 test = 1;
619 continue;
52826846
NB
620
621 /* now the general management options. Some are applicable
622 * to other modes. None have arguments.
623 */
e5329c37 624 case O(GROW,'a'):
dd0781e5 625 case O(MANAGE,'a'): /* add a drive */
52826846
NB
626 devmode = 'a';
627 continue;
e0d19036 628 case O(MANAGE,'r'): /* remove a drive */
52826846
NB
629 devmode = 'r';
630 continue;
e0d19036 631 case O(MANAGE,'f'): /* set faulty */
52826846
NB
632 devmode = 'f';
633 continue;
e0d19036
NB
634 case O(MANAGE,'R'):
635 case O(ASSEMBLE,'R'):
636 case O(BUILD,'R'):
637 case O(CREATE,'R'): /* Run the array */
52826846
NB
638 if (runstop < 0) {
639 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
640 exit(2);
641 }
642 runstop = 1;
643 continue;
e0d19036 644 case O(MANAGE,'S'):
52826846
NB
645 if (runstop > 0) {
646 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
647 exit(2);
648 }
649 runstop = -1;
650 continue;
651
e0d19036 652 case O(MANAGE,'o'):
52826846
NB
653 if (readonly < 0) {
654 fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
655 exit(2);
656 }
657 readonly = 1;
658 continue;
e0d19036 659 case O(MANAGE,'w'):
52826846 660 if (readonly > 0) {
e0d19036 661 fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n");
52826846
NB
662 exit(2);
663 }
664 readonly = -1;
665 continue;
e0d19036
NB
666
667 case O(MISC,'Q'):
668 case O(MISC,'D'):
669 case O(MISC,'E'):
670 case O(MISC,'K'):
671 case O(MISC,'R'):
672 case O(MISC,'S'):
c82f047c 673 case O(MISC,'X'):
e0d19036
NB
674 case O(MISC,'o'):
675 case O(MISC,'w'):
676 if (devmode && devmode != opt &&
677 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
678 fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n",
679 devmode =='E'?opt:devmode);
680 exit(2);
681 }
682 devmode = opt;
683 continue;
feb716e9
NB
684 case O(MISC,'t'):
685 test = 1;
686 continue;
e0d19036 687
bd526cee
NB
688 case O(MISC, 22):
689 if (devmode != 'E') {
690 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
691 exit(2);
692 }
693 SparcAdjust = 1;
694 continue;
c82f047c
NB
695
696 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
697 if (!optarg) {
698 fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n");
699 exit(2);
700 }
701 bitmap_fd = open(optarg, O_RDWR);
702 if (!*optarg || bitmap_fd < 0) {
703 fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno));
704 exit(2);
705 }
706 ident.bitmap_fd = bitmap_fd; /* for Assemble */
707 continue;
708 case O(BUILD,'b'):
709 case O(CREATE,'b'): /* here we create the bitmap */
710 bitmap_file = optarg;
711 continue;
712
713 case O(BUILD,4):
714 case O(CREATE,4): /* bitmap chunksize */
715 bitmap_chunk = strtol(optarg, &c, 10);
716 if (!optarg[0] || *c || bitmap_chunk < 0 ||
717 bitmap_chunk & (bitmap_chunk - 1)) {
718 fprintf(stderr, Name ": invalid bitmap chunksize: %s\n",
719 optarg);
720 exit(2);
721 }
722 /* convert K to B, chunk of 0K means 512B */
723 bitmap_chunk = bitmap_chunk ? bitmap_chunk * 1024 : 512;
724 continue;
52826846
NB
725 }
726 /* We have now processed all the valid options. Anything else is
727 * an error
728 */
e0d19036
NB
729 fprintf(stderr, Name ": option %c not valid in %s mode\n",
730 opt, map_num(modes, mode));
64c4757e 731 exit(2);
52826846
NB
732
733 }
734
0320ea45
NB
735 if (!mode && devs_found) {
736 mode = MISC;
737 devmode = 'Q';
738 if (devlist->disposition == 0)
739 devlist->disposition = devmode;
740 }
52826846
NB
741 if (!mode) {
742 fputs(Usage, stderr);
64c4757e 743 exit(2);
64c4757e 744 }
52826846
NB
745 /* Ok, got the option parsing out of the way
746 * hopefully it's mostly right but there might be some stuff
747 * missing
748 *
e0d19036 749 * That is mosty checked in the per-mode stuff but...
52826846
NB
750 *
751 * For @,B,C and A without -s, the first device listed must be an md device
752 * we check that here and open it.
64c4757e 753 */
52826846 754
dd0781e5
NB
755 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
756 (mode == ASSEMBLE && ! scan)) {
52826846
NB
757 if (devs_found < 1) {
758 fprintf(stderr, Name ": an md device must be given in this mode\n");
759 exit(2);
760 }
dd0781e5
NB
761 if ((int)ident.super_minor == -2 && autof) {
762 fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
763 exit(2);
764 }
765 mdfd = open_mddev(devlist->devname, autof);
52826846
NB
766 if (mdfd < 0)
767 exit(1);
98c6faba 768 if ((int)ident.super_minor == -2) {
d013a55e
NB
769 struct stat stb;
770 fstat(mdfd, &stb);
0df46c2a 771 ident.super_minor = minor(stb.st_rdev);
d013a55e 772 }
64c4757e 773 }
52826846 774
c82f047c 775
52826846
NB
776 rv = 0;
777 switch(mode) {
e0d19036 778 case MANAGE:
52826846
NB
779 /* readonly, add/remove, readwrite, runstop */
780 if (readonly>0)
cd29a5c8 781 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 782 if (!rv && devs_found>1)
cd29a5c8
NB
783 rv = Manage_subdevs(devlist->devname, mdfd,
784 devlist->next);
52826846 785 if (!rv && readonly < 0)
cd29a5c8 786 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 787 if (!rv && runstop)
cd29a5c8 788 rv = Manage_runstop(devlist->devname, mdfd, runstop);
52826846 789 break;
e0d19036 790 case ASSEMBLE:
d013a55e 791 if (devs_found == 1 && ident.uuid_set == 0 &&
98c6faba 792 ident.super_minor == UnSet && !scan ) {
d013a55e
NB
793 /* Only a device has been given, so get details from config file */
794 mddev_ident_t array_ident = conf_get_ident(configfile, devlist->devname);
b5e64645
NB
795 if (array_ident == NULL) {
796 fprintf(stderr, Name ": %s not identified in config file.\n",
797 devlist->devname);
d013a55e 798 rv |= 1;
b5e64645
NB
799 } else {
800 mdfd = open_mddev(devlist->devname, array_ident->autof);
801 if (mdfd < 0)
d013a55e 802 rv |= 1;
b5e64645 803 else {
f9ce90ba 804 rv |= Assemble(ss, devlist->devname, mdfd, array_ident, configfile,
d013a55e
NB
805 NULL,
806 readonly, runstop, update, verbose, force);
b5e64645
NB
807 close(mdfd);
808 }
d013a55e
NB
809 }
810 } else if (!scan)
f9ce90ba 811 rv = Assemble(ss, devlist->devname, mdfd, &ident, configfile,
cd29a5c8 812 devlist->next,
5787fa49
NB
813 readonly, runstop, update, verbose, force);
814 else if (devs_found>0) {
815 if (update && devs_found > 1) {
816 fprintf(stderr, Name ": can only update a single array at a time\n");
817 exit(1);
818 }
cd29a5c8
NB
819 for (dv = devlist ; dv ; dv=dv->next) {
820 mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname);
52826846
NB
821 if (array_ident == NULL) {
822 fprintf(stderr, Name ": %s not identified in config file.\n",
cd29a5c8 823 dv->devname);
52826846
NB
824 rv |= 1;
825 continue;
826 }
b5e64645
NB
827 mdfd = open_mddev(dv->devname, array_ident->autof);
828 if (mdfd < 0) {
829 rv |= 1;
830 continue;
831 }
f9ce90ba 832 rv |= Assemble(ss, dv->devname, mdfd, array_ident, configfile,
cd29a5c8 833 NULL,
5787fa49 834 readonly, runstop, update, verbose, force);
b5e64645 835 close(mdfd);
52826846 836 }
5787fa49 837 } else {
52826846
NB
838 mddev_ident_t array_list = conf_get_ident(configfile, NULL);
839 if (!array_list) {
840 fprintf(stderr, Name ": No arrays found in config file\n");
841 rv = 1;
842 } else
843 for (; array_list; array_list = array_list->next) {
cd29a5c8 844 mdu_array_info_t array;
dd0781e5 845 mdfd = open_mddev(array_list->devname, array_list->autof);
52826846
NB
846 if (mdfd < 0) {
847 rv |= 1;
848 continue;
849 }
cd29a5c8
NB
850 if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
851 /* already assembled, skip */
43fc1676
NB
852 ;
853 else
f9ce90ba 854 rv |= Assemble(ss, array_list->devname, mdfd,
43fc1676
NB
855 array_list, configfile,
856 NULL,
857 readonly, runstop, NULL, verbose, force);
858 close(mdfd);
52826846
NB
859 }
860 }
861 break;
e0d19036 862 case BUILD:
c82f047c
NB
863 if (bitmap_chunk == UnSet) bitmap_chunk = DEFAULT_BITMAP_CHUNK;
864 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
865 if (bitmap_file) {
866 bitmap_fd = open(bitmap_file, O_RDWR,0);
867 if (bitmap_fd < 0 && errno != ENOENT) {
868 perror(Name ": cannot create bitmap file");
869 rv |= 1;
870 break;
871 }
872 if (bitmap_fd < 0) {
873 bitmap_fd = CreateBitmap(bitmap_file, force, NULL,
874 bitmap_chunk, delay, size);
875 }
876 }
877 rv = Build(devlist->devname, mdfd, chunk, level, layout,
878 raiddisks, devlist->next, assume_clean,
879 bitmap_file, bitmap_chunk, delay);
52826846 880 break;
e0d19036 881 case CREATE:
c82f047c
NB
882 if (bitmap_chunk == UnSet) bitmap_chunk = DEFAULT_BITMAP_CHUNK;
883 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
f9ce90ba 884 if (ss == NULL) {
82d9eba6
NB
885 for(i=0; !ss && superlist[i]; i++)
886 ss = superlist[i]->match_metadata_desc("default");
f9ce90ba
NB
887 }
888 if (!ss) {
889 fprintf(stderr, Name ": internal error - no default metadata style\n");
890 exit(2);
891 }
892
893 rv = Create(ss, devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size,
52826846 894 raiddisks, sparedisks,
c82f047c
NB
895 devs_found-1, devlist->next, runstop, verbose, force,
896 bitmap_file, bitmap_chunk, delay);
52826846 897 break;
e0d19036
NB
898 case MISC:
899
900 if (devmode == 'E') {
901 if (devlist == NULL && !scan) {
902 fprintf(stderr, Name ": No devices to examine\n");
903 exit(2);
904 }
905 if (devlist == NULL)
906 devlist = conf_get_devs(configfile);
907 if (devlist == NULL) {
c913b90e 908 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
e0d19036
NB
909 exit(1);
910 }
82d9eba6 911 rv = Examine(devlist, scan?!verbose:brief, scan, SparcAdjust, ss);
e0d19036
NB
912 } else {
913 if (devlist == NULL) {
914 if ((devmode == 'S' ||devmode=='D') && scan) {
915 /* apply to all devices in /proc/mdstat */
dd0781e5 916 struct mdstat_ent *ms = mdstat_read(0);
e0d19036 917 struct mdstat_ent *e;
d17c1f8c
NB
918 if (devmode == 'S') {
919 /* reverse order so that arrays made of arrays are stopped properly */
920 struct mdstat_ent *sm = NULL;
921 while ((e=ms) != NULL) {
922 ms = e->next;
923 e->next = sm;
924 sm = e;
925 }
926 ms = sm;
927 }
e0d19036
NB
928 for (e=ms ; e ; e=e->next) {
929 char *name = get_md_name(e->devnum);
930
931 if (!name) {
932 fprintf(stderr, Name ": cannot find device file for %s\n",
933 e->dev);
934 continue;
935 }
936 if (devmode == 'D')
feb716e9 937 rv |= Detail(name, !verbose, test);
e0d19036 938 else if (devmode=='S') {
dd0781e5 939 mdfd = open_mddev(name, 0);
43fc1676 940 if (mdfd >= 0) {
e0d19036 941 rv |= Manage_runstop(name, mdfd, -1);
43fc1676
NB
942 close(mdfd);
943 }
e0d19036
NB
944 }
945 put_md_name(name);
946 }
947 } else {
948 fprintf(stderr, Name ": No devices given.\n");
949 exit(2);
950 }
951 }
952 for (dv=devlist ; dv; dv=dv->next) {
953 switch(dv->disposition) {
954 case 'D':
feb716e9 955 rv |= Detail(dv->devname, brief, test); continue;
e0d19036
NB
956 case 'K': /* Zero superblock */
957 rv |= Kill(dv->devname, force); continue;
958 case 'Q':
959 rv |= Query(dv->devname); continue;
c82f047c
NB
960 case 'X':
961 rv |= ExamineBitmap(dv->devname, brief); continue;
e0d19036 962 }
dd0781e5 963 mdfd = open_mddev(dv->devname, 0);
43fc1676 964 if (mdfd>=0) {
e0d19036
NB
965 switch(dv->disposition) {
966 case 'R':
967 rv |= Manage_runstop(dv->devname, mdfd, 1); break;
968 case 'S':
969 rv |= Manage_runstop(dv->devname, mdfd, -1); break;
970 case 'o':
971 rv |= Manage_ro(dv->devname, mdfd, 1); break;
972 case 'w':
973 rv |= Manage_ro(dv->devname, mdfd, -1); break;
974 }
43fc1676
NB
975 close(mdfd);
976 }
e0d19036 977 }
cd29a5c8 978 }
9a9dab36 979 break;
e0d19036 980 case MONITOR:
e0d19036
NB
981 if (!devlist && !scan) {
982 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
983 rv = 1;
984 break;
985 }
b5e64645
NB
986 if (pidfile && !daemonise) {
987 fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
988 rv = 1;
989 break;
990 }
d013a55e 991 rv= Monitor(devlist, mailaddr, program,
b5e64645 992 delay?delay:60, daemonise, scan, oneshot, configfile, test, pidfile);
9a9dab36 993 break;
dd0781e5
NB
994
995 case GROW:
996 if (devs_found > 1) {
e5329c37
NB
997
998 /* must be '-a'. */
999 if (size >= 0 || raiddisks) {
1000 fprintf(stderr, Name ": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1001 rv = 1;
1002 break;
1003 }
1004 for (dv=devlist->next; dv ; dv=dv->next) {
1005 rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
1006 if (rv)
1007 break;
1008 }
b5e64645
NB
1009 } else if ((size >= 0) + (raiddisks != 0) + (layout != UnSet) > 1) {
1010 fprintf(stderr, Name ": can change at most one of size, raiddisks, and layout\n");
dd0781e5
NB
1011 rv = 1;
1012 break;
b5e64645
NB
1013 } else if (layout != UnSet)
1014 rv = Manage_reconfig(devlist->devname, mdfd, layout);
1015 else if (size >= 0 || raiddisks)
e5329c37 1016 rv = Manage_resize(devlist->devname, mdfd, size, raiddisks);
b5e64645
NB
1017 else
1018 fprintf(stderr, Name ": no changes to --grow\n");
dd0781e5 1019 break;
64c4757e 1020 }
52826846 1021 exit(rv);
64c4757e 1022}