mdadm-1.2.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
NB
31#include "md_p.h"
32
52826846 33int open_mddev(char *dev)
64c4757e 34{
52826846
NB
35 int mdfd = open(dev, O_RDWR, 0);
36 if (mdfd < 0)
e0d19036 37 fprintf(stderr, Name ": error opening %s: %s\n",
52826846
NB
38 dev, strerror(errno));
39 else if (md_get_version(mdfd) <= 0) {
40 fprintf(stderr, Name ": %s does not appear to be an md device\n",
41 dev);
42 close(mdfd);
43 mdfd = -1;
64c4757e 44 }
52826846
NB
45 return mdfd;
46}
64c4757e 47
64c4757e 48
52826846
NB
49
50int main(int argc, char *argv[])
51{
e0d19036 52 int mode = 0;
52826846 53 int opt;
e0d19036 54 int option_index;
52826846
NB
55 char *help_text;
56 char *c;
57 int rv;
52826846
NB
58
59 int chunk = 0;
60 int size = 0;
61 int level = -10;
62 int layout = -1;
63 int raiddisks = 0;
64 int sparedisks = 0;
65 struct mddev_ident_s ident;
66 char *configfile = NULL;
cd29a5c8 67 char *cp;
5787fa49 68 char *update = NULL;
52826846
NB
69 int scan = 0;
70 char devmode = 0;
71 int runstop = 0;
72 int readonly = 0;
bd526cee 73 int SparcAdjust = 0;
cd29a5c8
NB
74 mddev_dev_t devlist = NULL;
75 mddev_dev_t *devlistend = & devlist;
76 mddev_dev_t dv;
52826846
NB
77 int devs_found = 0;
78 int verbose = 0;
cd29a5c8 79 int brief = 0;
52826846
NB
80 int force = 0;
81
82 char *mailaddr = NULL;
83 char *program = NULL;
84 int delay = 0;
d013a55e 85 int daemonise = 0;
52826846
NB
86
87 int mdfd = -1;
88
89 ident.uuid_set=0;
cd29a5c8
NB
90 ident.level = -10;
91 ident.raid_disks = -1;
52826846
NB
92 ident.super_minor= -1;
93 ident.devices=0;
94
e0d19036
NB
95 while ((option_index = -1) ,
96 (opt=getopt_long(argc, argv,
52826846 97 short_options, long_options,
e0d19036
NB
98 &option_index)) != -1) {
99 int newmode = mode;
100 /* firstly, so mode-independant options */
52826846 101 switch(opt) {
52826846
NB
102 case 'h':
103 help_text = Help;
56eedc1a
NB
104 if (option_index > 0 &&
105 strcmp(long_options[option_index].name, "help-options")==0)
106 help_text = OptionHelp;
107 else
108 switch (mode) {
109 case ASSEMBLE : help_text = Help_assemble; break;
110 case BUILD : help_text = Help_build; break;
111 case CREATE : help_text = Help_create; break;
112 case MANAGE : help_text = Help_manage; break;
113 case MISC : help_text = Help_misc; break;
114 case MONITOR : help_text = Help_monitor; break;
115 }
52826846
NB
116 fputs(help_text,stderr);
117 exit(0);
118
119 case 'V':
120 fputs(Version, stderr);
121 exit(0);
122
123 case 'v': verbose = 1;
124 continue;
125
cd29a5c8
NB
126 case 'b': brief = 1;
127 continue;
128
e0d19036
NB
129 case ':':
130 case '?':
131 fputs(Usage, stderr);
132 exit(2);
133 }
134 /* second, figure out the mode.
135 * Some options force the mode. Others
136 * set the mode if it isn't already
137 */
138
139 switch(opt) {
140 case '@': /* just incase they say --manage */
141 newmode = MANAGE; break;
142 case 'a':
143 case 'r':
144 case 'f':
145 case 1 : if (!mode) newmode = MANAGE; break;
146
147 case 'A': newmode = ASSEMBLE; break;
148 case 'B': newmode = BUILD; break;
149 case 'C': newmode = CREATE; break;
150 case 'F': newmode = MONITOR;break;
151
152 case '#':
153 case 'D':
154 case 'E':
155 case 'Q': newmode = MISC; break;
156 case 'R':
157 case 'S':
158 case 'o':
159 case 'w':
160 case 'K': if (!mode) newmode = MISC; break;
161 }
162 if (mode && newmode == mode) {
163 /* everybody happy ! */
164 } else if (mode && newmode != mode) {
165 /* not allowed.. */
166 fprintf(stderr, Name ": ");
167 if (option_index >= 0)
168 fprintf(stderr, "--%s", long_options[option_index].name);
169 else
170 fprintf(stderr, "-%c", opt);
171 fprintf(stderr, " would set mode to %s, but it is already %s.\n",
172 map_num(modes, newmode),
173 map_num(modes, mode));
174 exit(2);
175 } else if (!mode && newmode) {
176 mode = newmode;
177 } else {
178 /* special case of -c --help */
179 if (opt == 'c' &&
180 ( strncmp(optarg, "--h", 3)==0 ||
181 strncmp(optarg, "-h", 2)==0)) {
182 fputs(Help_config, stderr);
183 exit(0);
cd29a5c8 184 }
e0d19036
NB
185 if (option_index >= 0)
186 fprintf(stderr, "--%s", long_options[option_index].name);
187 else
188 fprintf(stderr, "-%c", opt);
189 fprintf(stderr, " does not set the mode, and so cannot be first.\n");
190 exit(2);
191 }
192
193 /* if we just set the mode, then done */
194 switch(opt) {
195 case '@':
196 case '#':
197 case 'A':
198 case 'B':
199 case 'C':
200 case 'F':
201 continue;
202 }
203 if (opt == 1) {
204 /* an undecorated option - must be a device name.
205 */
cd29a5c8
NB
206 if (devs_found > 0 && mode == '@' && !devmode) {
207 fprintf(stderr, Name ": Must give on of -a/-r/-f for subsequent devices at %s\n", optarg);
52826846
NB
208 exit(2);
209 }
cd29a5c8
NB
210 dv = malloc(sizeof(*dv));
211 if (dv == NULL) {
212 fprintf(stderr, Name ": malloc failed\n");
213 exit(3);
214 }
215 dv->devname = optarg;
216 dv->disposition = devmode;
217 dv->next = NULL;
218 *devlistend = dv;
219 devlistend = &dv->next;
220
52826846
NB
221 devs_found++;
222 continue;
682c7051 223 }
682c7051 224
52826846
NB
225 /* We've got a mode, and opt is now something else which
226 * could depend on the mode */
227#define O(a,b) ((a<<8)|b)
228 switch (O(mode,opt)) {
e0d19036
NB
229 case O(CREATE,'c'):
230 case O(BUILD,'c'): /* chunk or rounding */
52826846
NB
231 if (chunk) {
232 fprintf(stderr, Name ": chunk/rounding may only be specified once. "
233 "Second value is %s.\n", optarg);
234 exit(2);
235 }
236 chunk = strtol(optarg, &c, 10);
237 if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) {
238 fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
239 optarg);
240 exit(2);
241 }
242 continue;
64c4757e 243
e0d19036 244 case O(CREATE,'z'): /* size */
52826846
NB
245 if (size) {
246 fprintf(stderr, Name ": size may only be specified once. "
247 "Second value is %s.\n", optarg);
248 exit(2);
249 }
250 size = strtol(optarg, &c, 10);
251 if (!optarg[0] || *c || size < 4) {
252 fprintf(stderr, Name ": invalid size: %s\n",
253 optarg);
254 exit(2);
255 }
256 continue;
64c4757e 257
e0d19036
NB
258 case O(CREATE,'l'):
259 case O(BUILD,'l'): /* set raid level*/
52826846
NB
260 if (level != -10) {
261 fprintf(stderr, Name ": raid level may only be set once. "
262 "Second value is %s.\n", optarg);
263 exit(2);
264 }
265 level = map_name(pers, optarg);
266 if (level == -10) {
267 fprintf(stderr, Name ": invalid raid level: %s\n",
268 optarg);
269 exit(2);
270 }
e0d19036 271 if (level != 0 && level != -1 && mode == BUILD) {
52826846
NB
272 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
273 optarg);
274 exit(2);
275 }
e0d19036 276 if (sparedisks > 0 && level < 1 && level >= -1) {
b83d95f3 277 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
52826846
NB
278 optarg);
279 exit(2);
280 }
cd29a5c8 281 ident.level = level;
52826846 282 continue;
64c4757e 283
e0d19036 284 case O(CREATE,'p'): /* raid5 layout */
52826846
NB
285 if (layout >= 0) {
286 fprintf(stderr,Name ": layout may only be sent once. "
287 "Second value was %s\n", optarg);
288 exit(2);
289 }
290 switch(level) {
291 default:
56eb10c0 292 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
52826846
NB
293 map_num(pers, level));
294 exit(2);
295 case -10:
296 fprintf(stderr, Name ": raid level must be given before layout.\n");
297 exit(2);
298
299 case 5:
300 layout = map_name(r5layout, optarg);
301 if (layout==-10) {
302 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
303 optarg);
304 exit(2);
305 }
306 break;
307 }
308 continue;
309
e0d19036
NB
310 case O(CREATE,'n'):
311 case O(BUILD,'n'): /* number of raid disks */
52826846 312 if (raiddisks) {
b83d95f3 313 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
52826846
NB
314 raiddisks, optarg);
315 exit(2);
316 }
317 raiddisks = strtol(optarg, &c, 10);
318 if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) {
b83d95f3 319 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
52826846
NB
320 optarg);
321 exit(2);
322 }
cd29a5c8 323 ident.raid_disks = raiddisks;
52826846
NB
324 continue;
325
e0d19036 326 case O(CREATE,'x'): /* number of spare (eXtra) discs */
52826846 327 if (sparedisks) {
b83d95f3 328 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
52826846
NB
329 sparedisks, optarg);
330 exit(2);
331 }
e0d19036 332 if (level > -10 && level <= 0 && level >= -1) {
b83d95f3 333 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
52826846
NB
334 level);
335 exit(2);
336 }
337 sparedisks = strtol(optarg, &c, 10);
338 if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) {
b83d95f3 339 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
52826846
NB
340 optarg);
341 exit(2);
342 }
343 continue;
e0d19036
NB
344 case O(CREATE,'f'): /* force honouring of device list */
345 case O(ASSEMBLE,'f'): /* force assembly */
346 case O(MISC,'f'): /* force zero */
52826846
NB
347 force=1;
348 continue;
349
350 /* now for the Assemble options */
e0d19036 351 case O(ASSEMBLE,'u'): /* uuid of array */
52826846 352 if (ident.uuid_set) {
cd29a5c8 353 fprintf(stderr, Name ": uuid cannot be set twice. "
52826846
NB
354 "Second value %s.\n", optarg);
355 exit(2);
356 }
357 if (parse_uuid(optarg, ident.uuid))
358 ident.uuid_set = 1;
359 else {
360 fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
361 exit(2);
362 }
363 continue;
364
e0d19036 365 case O(ASSEMBLE,'m'): /* super-minor for array */
d013a55e 366 if (ident.super_minor != -1) {
cd29a5c8
NB
367 fprintf(stderr, Name ": super-minor cannot be set twice. "
368 "Second value: %s.\n", optarg);
369 exit(2);
370 }
d013a55e
NB
371 if (strcmp(optarg, "dev")==0)
372 ident.super_minor = -2;
373 else {
374 ident.super_minor = strtoul(optarg, &cp, 10);
375 if (!optarg[0] || *cp) {
376 fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
377 exit(2);
378 }
cd29a5c8
NB
379 }
380 continue;
381
5787fa49
NB
382 case O(ASSEMBLE,'U'): /* update the superblock */
383 if (update) {
384 fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n",
385 update, optarg);
386 exit(2);
387 }
388 update = optarg;
389 if (strcmp(update, "sparc2.2")==0) continue;
390 if (strcmp(update, "super-minor") == 0)
391 continue;
392 fprintf(stderr, Name ": '--update %s' invalid. Only 'sparc2.2' or 'super-minor' supported\n",update);
393 exit(2);
394
e0d19036 395 case O(ASSEMBLE,'c'): /* config file */
2d465520 396 case O(MISC, 'c'):
e0d19036 397 case O(MONITOR,'c'):
52826846
NB
398 if (configfile) {
399 fprintf(stderr, Name ": configfile cannot be set twice. "
400 "Second value is %s.\n", optarg);
401 exit(2);
402 }
403 configfile = optarg;
404 /* FIXME possibly check that config file exists. Even parse it */
405 continue;
e0d19036
NB
406 case O(ASSEMBLE,'s'): /* scan */
407 case O(MISC,'s'):
408 case O(MONITOR,'s'):
52826846
NB
409 scan = 1;
410 continue;
411
e0d19036 412 case O(MONITOR,'m'): /* mail address */
52826846
NB
413 if (mailaddr)
414 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
415 optarg);
416 else
417 mailaddr = optarg;
418 continue;
419
e0d19036 420 case O(MONITOR,'p'): /* alert program */
52826846
NB
421 if (program)
422 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
423 optarg);
424 else
425 program = optarg;
426 continue;
64c4757e 427
e0d19036 428 case O(MONITOR,'d'): /* delay in seconds */
52826846
NB
429 if (delay)
430 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
431 optarg);
432 else {
433 delay = strtol(optarg, &c, 10);
434 if (!optarg[0] || *c || delay<1) {
435 fprintf(stderr, Name ": invalid delay: %s\n",
436 optarg);
437 exit(2);
438 }
439 }
440 continue;
d013a55e
NB
441 case O(MONITOR,'f'): /* daemonise */
442 daemonise = 1;
443 continue;
52826846
NB
444
445
446 /* now the general management options. Some are applicable
447 * to other modes. None have arguments.
448 */
e0d19036
NB
449 case O(MANAGE,'a'):
450 case O(CREATE,'a'):
451 case O(BUILD,'a'):
452 case O(ASSEMBLE,'a'): /* add a drive */
52826846
NB
453 devmode = 'a';
454 continue;
e0d19036 455 case O(MANAGE,'r'): /* remove a drive */
52826846
NB
456 devmode = 'r';
457 continue;
e0d19036 458 case O(MANAGE,'f'): /* set faulty */
52826846
NB
459 devmode = 'f';
460 continue;
e0d19036
NB
461 case O(MANAGE,'R'):
462 case O(ASSEMBLE,'R'):
463 case O(BUILD,'R'):
464 case O(CREATE,'R'): /* Run the array */
52826846
NB
465 if (runstop < 0) {
466 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
467 exit(2);
468 }
469 runstop = 1;
470 continue;
e0d19036 471 case O(MANAGE,'S'):
52826846
NB
472 if (runstop > 0) {
473 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
474 exit(2);
475 }
476 runstop = -1;
477 continue;
478
e0d19036 479 case O(MANAGE,'o'):
52826846
NB
480 if (readonly < 0) {
481 fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
482 exit(2);
483 }
484 readonly = 1;
485 continue;
e0d19036 486 case O(MANAGE,'w'):
52826846 487 if (readonly > 0) {
e0d19036 488 fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n");
52826846
NB
489 exit(2);
490 }
491 readonly = -1;
492 continue;
e0d19036
NB
493
494 case O(MISC,'Q'):
495 case O(MISC,'D'):
496 case O(MISC,'E'):
497 case O(MISC,'K'):
498 case O(MISC,'R'):
499 case O(MISC,'S'):
500 case O(MISC,'o'):
501 case O(MISC,'w'):
502 if (devmode && devmode != opt &&
503 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
504 fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n",
505 devmode =='E'?opt:devmode);
506 exit(2);
507 }
508 devmode = opt;
509 continue;
510
bd526cee
NB
511 case O(MISC, 22):
512 if (devmode != 'E') {
513 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
514 exit(2);
515 }
516 SparcAdjust = 1;
517 continue;
52826846
NB
518 }
519 /* We have now processed all the valid options. Anything else is
520 * an error
521 */
e0d19036
NB
522 fprintf(stderr, Name ": option %c not valid in %s mode\n",
523 opt, map_num(modes, mode));
64c4757e 524 exit(2);
52826846
NB
525
526 }
527
528 if (!mode) {
529 fputs(Usage, stderr);
64c4757e 530 exit(2);
64c4757e 531 }
52826846
NB
532 /* Ok, got the option parsing out of the way
533 * hopefully it's mostly right but there might be some stuff
534 * missing
535 *
e0d19036 536 * That is mosty checked in the per-mode stuff but...
52826846
NB
537 *
538 * For @,B,C and A without -s, the first device listed must be an md device
539 * we check that here and open it.
64c4757e 540 */
52826846 541
e0d19036 542 if (mode==MANAGE || mode == BUILD || mode == CREATE || (mode == ASSEMBLE && ! scan)) {
52826846
NB
543 if (devs_found < 1) {
544 fprintf(stderr, Name ": an md device must be given in this mode\n");
545 exit(2);
546 }
cd29a5c8 547 mdfd = open_mddev(devlist->devname);
52826846
NB
548 if (mdfd < 0)
549 exit(1);
d013a55e
NB
550 if (ident.super_minor == -2) {
551 struct stat stb;
552 fstat(mdfd, &stb);
553 ident.super_minor = MINOR(stb.st_rdev);
554 }
64c4757e 555 }
52826846 556
52826846
NB
557 rv = 0;
558 switch(mode) {
e0d19036 559 case MANAGE:
52826846
NB
560 /* readonly, add/remove, readwrite, runstop */
561 if (readonly>0)
cd29a5c8 562 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 563 if (!rv && devs_found>1)
cd29a5c8
NB
564 rv = Manage_subdevs(devlist->devname, mdfd,
565 devlist->next);
52826846 566 if (!rv && readonly < 0)
cd29a5c8 567 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 568 if (!rv && runstop)
cd29a5c8 569 rv = Manage_runstop(devlist->devname, mdfd, runstop);
52826846 570 break;
e0d19036 571 case ASSEMBLE:
d013a55e
NB
572 if (devs_found == 1 && ident.uuid_set == 0 &&
573 ident.super_minor == -1 && !scan ) {
574 /* Only a device has been given, so get details from config file */
575 mddev_ident_t array_ident = conf_get_ident(configfile, devlist->devname);
576 mdfd = open_mddev(devlist->devname);
577 if (mdfd < 0)
578 rv |= 1;
579 else {
580 if (array_ident == NULL) {
581 fprintf(stderr, Name ": %s not identified in config file.\n",
582 devlist->devname);
583 rv |= 1;
584 }
585 else
586 rv |= Assemble(devlist->devname, mdfd, array_ident, configfile,
587 NULL,
588 readonly, runstop, update, verbose, force);
589 }
590 } else if (!scan)
cd29a5c8
NB
591 rv = Assemble(devlist->devname, mdfd, &ident, configfile,
592 devlist->next,
5787fa49
NB
593 readonly, runstop, update, verbose, force);
594 else if (devs_found>0) {
595 if (update && devs_found > 1) {
596 fprintf(stderr, Name ": can only update a single array at a time\n");
597 exit(1);
598 }
cd29a5c8
NB
599 for (dv = devlist ; dv ; dv=dv->next) {
600 mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname);
601 mdfd = open_mddev(dv->devname);
52826846
NB
602 if (mdfd < 0) {
603 rv |= 1;
604 continue;
605 }
606 if (array_ident == NULL) {
607 fprintf(stderr, Name ": %s not identified in config file.\n",
cd29a5c8 608 dv->devname);
52826846
NB
609 rv |= 1;
610 continue;
611 }
cd29a5c8
NB
612 rv |= Assemble(dv->devname, mdfd, array_ident, configfile,
613 NULL,
5787fa49 614 readonly, runstop, update, verbose, force);
52826846 615 }
5787fa49 616 } else {
52826846
NB
617 mddev_ident_t array_list = conf_get_ident(configfile, NULL);
618 if (!array_list) {
619 fprintf(stderr, Name ": No arrays found in config file\n");
620 rv = 1;
621 } else
622 for (; array_list; array_list = array_list->next) {
cd29a5c8 623 mdu_array_info_t array;
52826846
NB
624 mdfd = open_mddev(array_list->devname);
625 if (mdfd < 0) {
626 rv |= 1;
627 continue;
628 }
cd29a5c8
NB
629 if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
630 /* already assembled, skip */
631 continue;
52826846
NB
632 rv |= Assemble(array_list->devname, mdfd,
633 array_list, configfile,
cd29a5c8 634 NULL,
5787fa49 635 readonly, runstop, NULL, verbose, force);
52826846
NB
636 }
637 }
638 break;
e0d19036 639 case BUILD:
cd29a5c8 640 rv = Build(devlist->devname, mdfd, chunk, level, raiddisks, devlist->next);
52826846 641 break;
e0d19036 642 case CREATE:
cd29a5c8 643 rv = Create(devlist->devname, mdfd, chunk, level, layout, size,
52826846 644 raiddisks, sparedisks,
cd29a5c8 645 devs_found-1, devlist->next, runstop, verbose, force);
52826846 646 break;
e0d19036
NB
647 case MISC:
648
649 if (devmode == 'E') {
650 if (devlist == NULL && !scan) {
651 fprintf(stderr, Name ": No devices to examine\n");
652 exit(2);
653 }
654 if (devlist == NULL)
655 devlist = conf_get_devs(configfile);
656 if (devlist == NULL) {
c913b90e 657 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
e0d19036
NB
658 exit(1);
659 }
bd526cee 660 rv = Examine(devlist, scan?!verbose:brief, scan, SparcAdjust);
e0d19036
NB
661 } else {
662 if (devlist == NULL) {
663 if ((devmode == 'S' ||devmode=='D') && scan) {
664 /* apply to all devices in /proc/mdstat */
665 struct mdstat_ent *ms = mdstat_read();
666 struct mdstat_ent *e;
667 for (e=ms ; e ; e=e->next) {
668 char *name = get_md_name(e->devnum);
669
670 if (!name) {
671 fprintf(stderr, Name ": cannot find device file for %s\n",
672 e->dev);
673 continue;
674 }
675 if (devmode == 'D')
676 rv |= Detail(name, !verbose);
677 else if (devmode=='S') {
678 mdfd = open_mddev(name);
679 if (mdfd >= 0)
680 rv |= Manage_runstop(name, mdfd, -1);
681 }
682 put_md_name(name);
683 }
684 } else {
685 fprintf(stderr, Name ": No devices given.\n");
686 exit(2);
687 }
688 }
689 for (dv=devlist ; dv; dv=dv->next) {
690 switch(dv->disposition) {
691 case 'D':
692 rv |= Detail(dv->devname, brief); continue;
693 case 'K': /* Zero superblock */
694 rv |= Kill(dv->devname, force); continue;
695 case 'Q':
696 rv |= Query(dv->devname); continue;
697 }
698 mdfd = open_mddev(dv->devname);
699 if (mdfd>=0)
700 switch(dv->disposition) {
701 case 'R':
702 rv |= Manage_runstop(dv->devname, mdfd, 1); break;
703 case 'S':
704 rv |= Manage_runstop(dv->devname, mdfd, -1); break;
705 case 'o':
706 rv |= Manage_ro(dv->devname, mdfd, 1); break;
707 case 'w':
708 rv |= Manage_ro(dv->devname, mdfd, -1); break;
709 }
710 }
cd29a5c8 711 }
9a9dab36 712 break;
e0d19036 713 case MONITOR:
e0d19036
NB
714 if (!devlist && !scan) {
715 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
716 rv = 1;
717 break;
718 }
d013a55e
NB
719 rv= Monitor(devlist, mailaddr, program,
720 delay?delay:60, daemonise, scan, configfile);
9a9dab36 721 break;
64c4757e 722 }
52826846 723 exit(rv);
64c4757e 724}