]> git.ipfire.org Git - thirdparty/mdadm.git/blame - mdadm.c
"--export" segfaults with non-persistent super blocks
[thirdparty/mdadm.git] / mdadm.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
4f589ad0 4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
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 28 *
dfd4d8ee 29 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
c82f047c 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 *c;
44 int rv;
f9ce90ba 45 int i;
52826846
NB
46
47 int chunk = 0;
5dd497ee 48 long long size = -1;
98c6faba
NB
49 int level = UnSet;
50 int layout = UnSet;
52826846 51 int raiddisks = 0;
e27d562b 52 int max_disks = MD_SB_DISKS; /* just a default */
52826846
NB
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;
dfd4d8ee 62 int write_behind = 0;
c82f047c
NB
63 int bitmap_fd = -1;
64 char *bitmap_file = NULL;
06b0d786 65 char *backup_file = NULL;
c82f047c 66 int bitmap_chunk = UnSet;
bd526cee 67 int SparcAdjust = 0;
cd29a5c8
NB
68 mddev_dev_t devlist = NULL;
69 mddev_dev_t *devlistend = & devlist;
70 mddev_dev_t dv;
52826846
NB
71 int devs_found = 0;
72 int verbose = 0;
dab6685f 73 int quiet = 0;
cd29a5c8 74 int brief = 0;
52826846 75 int force = 0;
feb716e9 76 int test = 0;
54bad364 77 int export = 0;
dd0781e5 78 int assume_clean = 0;
38098016 79 char *symlinks = NULL;
f1ae21c4
NB
80 /* autof indicates whether and how to create device node.
81 * bottom 3 bits are style. Rest (when shifted) are number of parts
82 * 0 - unset
83 * 1 - don't create (no)
84 * 2 - if is_standard, then create (yes)
85 * 3 - create as 'md' - reject is_standard mdp (md)
86 * 4 - create as 'mdp' - reject is_standard md (mdp)
87 * 5 - default to md if not is_standard (md in config file)
88 * 6 - default to mdp if not is_standard (part, or mdp in config file)
89 */
90 int autof = 0;
52826846 91
997aed5d 92 char *homehost = NULL;
05697ec1 93 char sys_hostname[256];
52826846
NB
94 char *mailaddr = NULL;
95 char *program = NULL;
96 int delay = 0;
d013a55e 97 int daemonise = 0;
b5e64645 98 char *pidfile = NULL;
aa88f531 99 int oneshot = 0;
82d9eba6 100 struct supertype *ss = NULL;
dfd4d8ee 101 int writemostly = 0;
fe80f49b 102 int re_add = 0;
c06487ce 103 char *shortopt = short_options;
773135f5 104 int dosyslog = 0;
8382f19b 105 int rebuild_map = 0;
589395d6 106 int auto_update_home = 0;
52826846 107
e5329c37 108 int copies;
3d4064cc 109 int print_help = 0;
0047d254 110 FILE *outf;
e5329c37 111
52826846
NB
112 int mdfd = -1;
113
dfe47e00
NB
114 srandom(time(0) ^ getpid());
115
52826846 116 ident.uuid_set=0;
98c6faba
NB
117 ident.level = UnSet;
118 ident.raid_disks = UnSet;
119 ident.super_minor= UnSet;
52826846 120 ident.devices=0;
a75f2fbc
NB
121 ident.spare_group = NULL;
122 ident.autof = 0;
a825febc 123 ident.st = NULL;
c82f047c 124 ident.bitmap_fd = -1;
7ef02d01 125 ident.bitmap_file = NULL;
947fd4dd 126 ident.name[0] = 0;
52826846 127
e0d19036
NB
128 while ((option_index = -1) ,
129 (opt=getopt_long(argc, argv,
c06487ce 130 shortopt, long_options,
e0d19036
NB
131 &option_index)) != -1) {
132 int newmode = mode;
dab6685f 133 /* firstly, some mode-independant options */
52826846 134 switch(opt) {
52826846 135 case 'h':
56eedc1a
NB
136 if (option_index > 0 &&
137 strcmp(long_options[option_index].name, "help-options")==0)
3d4064cc 138 print_help = 2;
56eedc1a 139 else
3d4064cc
NB
140 print_help = 1;
141 continue;
52826846
NB
142
143 case 'V':
144 fputs(Version, stderr);
145 exit(0);
146
22892d56 147 case 'v': verbose++;
52826846
NB
148 continue;
149
dab6685f
NB
150 case 'q': quiet++;
151 continue;
152
c82f047c 153 case 'b':
f5e166fe 154 if (mode == ASSEMBLE || mode == BUILD || mode == CREATE || mode == GROW)
c82f047c
NB
155 break; /* b means bitmap */
156 brief = 1;
c06487ce
NB
157 if (optarg) {
158 fprintf(stderr, Name ": -b cannot have any extra immediately after it, sorry.\n");
159 exit(2);
160 }
cd29a5c8
NB
161 continue;
162
54bad364
KS
163 case 'Y': export++;
164 continue;
165
997aed5d
NB
166 case HomeHost:
167 homehost = optarg;
168 continue;
169
e0d19036
NB
170 case ':':
171 case '?':
172 fputs(Usage, stderr);
173 exit(2);
174 }
175 /* second, figure out the mode.
176 * Some options force the mode. Others
177 * set the mode if it isn't already
178 */
179
180 switch(opt) {
181 case '@': /* just incase they say --manage */
c06487ce 182 newmode = MANAGE;
024768c4 183 shortopt = short_bitmap_options;
c06487ce 184 break;
e0d19036
NB
185 case 'a':
186 case 'r':
187 case 'f':
41a3b72a 188 case ReAdd: /* re-add */
c06487ce
NB
189 if (!mode) {
190 newmode = MANAGE;
024768c4 191 shortopt = short_bitmap_options;
c06487ce 192 }
0320ea45 193 break;
e0d19036 194
c06487ce
NB
195 case 'A': newmode = ASSEMBLE; shortopt = short_bitmap_auto_options; break;
196 case 'B': newmode = BUILD; shortopt = short_bitmap_auto_options; break;
197 case 'C': newmode = CREATE; shortopt = short_bitmap_auto_options; break;
e0d19036 198 case 'F': newmode = MONITOR;break;
024768c4
DL
199 case 'G': newmode = GROW;
200 shortopt = short_bitmap_options;
201 break;
8382f19b 202 case 'I': newmode = INCREMENTAL; break;
1f48664b
NB
203 case AutoDetect:
204 newmode = AUTODETECT; break;
e0d19036
NB
205
206 case '#':
207 case 'D':
208 case 'E':
c82f047c 209 case 'X':
e0d19036
NB
210 case 'Q': newmode = MISC; break;
211 case 'R':
212 case 'S':
213 case 'o':
214 case 'w':
b90c0e9a 215 case 'W':
e0d19036
NB
216 case 'K': if (!mode) newmode = MISC; break;
217 }
218 if (mode && newmode == mode) {
219 /* everybody happy ! */
220 } else if (mode && newmode != mode) {
221 /* not allowed.. */
222 fprintf(stderr, Name ": ");
223 if (option_index >= 0)
224 fprintf(stderr, "--%s", long_options[option_index].name);
225 else
226 fprintf(stderr, "-%c", opt);
e6b64cd0 227 fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
e0d19036
NB
228 map_num(modes, newmode),
229 map_num(modes, mode));
230 exit(2);
231 } else if (!mode && newmode) {
232 mode = newmode;
233 } else {
234 /* special case of -c --help */
235 if (opt == 'c' &&
236 ( strncmp(optarg, "--h", 3)==0 ||
237 strncmp(optarg, "-h", 2)==0)) {
0047d254 238 fputs(Help_config, stdout);
e0d19036 239 exit(0);
cd29a5c8 240 }
0320ea45
NB
241
242 /* If first option is a device, don't force the mode yet */
243 if (opt == 1) {
244 if (devs_found == 0) {
245 dv = malloc(sizeof(*dv));
246 if (dv == NULL) {
247 fprintf(stderr, Name ": malloc failed\n");
248 exit(3);
249 }
250 dv->devname = optarg;
251 dv->disposition = devmode;
dfd4d8ee 252 dv->writemostly = writemostly;
fe80f49b 253 dv->re_add = re_add;
da6b5ca9 254 dv->used = 0;
0320ea45
NB
255 dv->next = NULL;
256 *devlistend = dv;
257 devlistend = &dv->next;
258
259 devs_found++;
260 continue;
261 }
262 /* No mode yet, and this is the second device ... */
263 fprintf(stderr, Name ": An option must be given to set the mode before a second device is listed\n");
264 exit(2);
265 }
e0d19036 266 if (option_index >= 0)
0320ea45 267 fprintf(stderr, Name ": --%s", long_options[option_index].name);
e0d19036 268 else
0320ea45
NB
269 fprintf(stderr, Name ": -%c", opt);
270 fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
e0d19036
NB
271 exit(2);
272 }
273
274 /* if we just set the mode, then done */
275 switch(opt) {
276 case '@':
277 case '#':
278 case 'A':
279 case 'B':
280 case 'C':
281 case 'F':
dd0781e5 282 case 'G':
8382f19b 283 case 'I':
1f48664b 284 case AutoDetect:
e0d19036
NB
285 continue;
286 }
287 if (opt == 1) {
288 /* an undecorated option - must be a device name.
289 */
cd29a5c8 290 if (devs_found > 0 && mode == '@' && !devmode) {
dd0781e5 291 fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
52826846
NB
292 exit(2);
293 }
e5329c37
NB
294 if (devs_found > 0 && mode == 'G' && !devmode) {
295 fprintf(stderr, Name ": Must give one of -a for devices do add: %s\n", optarg);
296 exit(2);
297 }
cd29a5c8
NB
298 dv = malloc(sizeof(*dv));
299 if (dv == NULL) {
300 fprintf(stderr, Name ": malloc failed\n");
301 exit(3);
302 }
303 dv->devname = optarg;
304 dv->disposition = devmode;
dfd4d8ee 305 dv->writemostly = writemostly;
fe80f49b 306 dv->re_add = re_add;
cd29a5c8
NB
307 dv->next = NULL;
308 *devlistend = dv;
309 devlistend = &dv->next;
310
52826846
NB
311 devs_found++;
312 continue;
682c7051 313 }
682c7051 314
52826846
NB
315 /* We've got a mode, and opt is now something else which
316 * could depend on the mode */
317#define O(a,b) ((a<<8)|b)
318 switch (O(mode,opt)) {
e0d19036
NB
319 case O(CREATE,'c'):
320 case O(BUILD,'c'): /* chunk or rounding */
52826846
NB
321 if (chunk) {
322 fprintf(stderr, Name ": chunk/rounding may only be specified once. "
323 "Second value is %s.\n", optarg);
324 exit(2);
325 }
326 chunk = strtol(optarg, &c, 10);
327 if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) {
328 fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
329 optarg);
330 exit(2);
331 }
332 continue;
64c4757e 333
589395d6
NB
334 case O(ASSEMBLE,AutoHomeHost):
335 auto_update_home = 1;
336 continue;
8382f19b 337 case O(INCREMENTAL, 'e'):
f9ce90ba
NB
338 case O(CREATE,'e'):
339 case O(ASSEMBLE,'e'):
340 case O(MISC,'e'): /* set metadata (superblock) information */
341 if (ss) {
342 fprintf(stderr, Name ": metadata information already given\n");
343 exit(2);
344 }
82d9eba6
NB
345 for(i=0; !ss && superlist[i]; i++)
346 ss = superlist[i]->match_metadata_desc(optarg);
347
f9ce90ba
NB
348 if (!ss) {
349 fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg);
350 exit(2);
351 }
ea329559 352 max_disks = ss->max_devs;
f9ce90ba
NB
353 continue;
354
dfd4d8ee
NB
355 case O(MANAGE,'W'):
356 case O(BUILD,'W'):
357 case O(CREATE,'W'):
358 /* set write-mostly for following devices */
359 writemostly = 1;
360 continue;
361
dd0781e5 362 case O(GROW,'z'):
e0d19036 363 case O(CREATE,'z'): /* size */
dd0781e5 364 if (size >= 0) {
52826846
NB
365 fprintf(stderr, Name ": size may only be specified once. "
366 "Second value is %s.\n", optarg);
367 exit(2);
368 }
dd0781e5
NB
369 if (strcmp(optarg, "max")==0)
370 size = 0;
371 else {
5dd497ee 372 size = strtoll(optarg, &c, 10);
dd0781e5
NB
373 if (!optarg[0] || *c || size < 4) {
374 fprintf(stderr, Name ": invalid size: %s\n",
375 optarg);
376 exit(2);
377 }
52826846
NB
378 }
379 continue;
64c4757e 380
b5e64645 381 case O(GROW,'l'): /* hack - needed to understand layout */
e0d19036
NB
382 case O(CREATE,'l'):
383 case O(BUILD,'l'): /* set raid level*/
98c6faba 384 if (level != UnSet) {
52826846
NB
385 fprintf(stderr, Name ": raid level may only be set once. "
386 "Second value is %s.\n", optarg);
387 exit(2);
388 }
389 level = map_name(pers, optarg);
98c6faba 390 if (level == UnSet) {
52826846
NB
391 fprintf(stderr, Name ": invalid raid level: %s\n",
392 optarg);
393 exit(2);
394 }
b5e64645 395 if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) {
52826846
NB
396 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
397 optarg);
398 exit(2);
399 }
e0d19036 400 if (sparedisks > 0 && level < 1 && level >= -1) {
b83d95f3 401 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
52826846
NB
402 optarg);
403 exit(2);
404 }
cd29a5c8 405 ident.level = level;
52826846 406 continue;
64c4757e 407
e0d19036 408 case O(CREATE,'p'): /* raid5 layout */
b5e64645
NB
409 case O(BUILD,'p'): /* faulty layout */
410 case O(GROW, 'p'): /* faulty reconfig */
e5329c37 411 if (layout != UnSet) {
52826846
NB
412 fprintf(stderr,Name ": layout may only be sent once. "
413 "Second value was %s\n", optarg);
414 exit(2);
415 }
416 switch(level) {
417 default:
56eb10c0 418 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
52826846
NB
419 map_num(pers, level));
420 exit(2);
98c6faba 421 case UnSet:
52826846
NB
422 fprintf(stderr, Name ": raid level must be given before layout.\n");
423 exit(2);
424
425 case 5:
98c6faba 426 case 6:
52826846 427 layout = map_name(r5layout, optarg);
98c6faba 428 if (layout==UnSet) {
52826846
NB
429 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
430 optarg);
431 exit(2);
432 }
433 break;
e5329c37
NB
434
435 case 10:
b578481c
NB
436 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
437 if ((optarg[0] != 'n' && optarg[0] != 'f' && optarg[0] != 'o') ||
e5329c37
NB
438 (copies = strtoul(optarg+1, &cp, 10)) < 1 ||
439 copies > 200 ||
440 *cp) {
b578481c 441 fprintf(stderr, Name ": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg);
e5329c37
NB
442 exit(2);
443 }
444 if (optarg[0] == 'n')
445 layout = 256 + copies;
b578481c
NB
446 else if (optarg[0] == 'o')
447 layout = 0x10000 + (copies<<8) + 1;
e5329c37
NB
448 else
449 layout = 1 + (copies<<8);
450 break;
b5e64645
NB
451 case -5: /* Faulty
452 * modeNNN
453 */
454
455 {
456 int ln = strcspn(optarg, "0123456789");
457 char *m = strdup(optarg);
458 int mode;
459 m[ln] = 0;
460 mode = map_name(faultylayout, m);
461 if (mode == UnSet) {
462 fprintf(stderr, Name ": layout %s not understood for faulty.\n",
463 optarg);
464 exit(2);
465 }
466 layout = mode | (atoi(optarg+ln)<< ModeShift);
467 }
52826846
NB
468 }
469 continue;
470
997aed5d
NB
471 case O(CREATE,AssumeClean):
472 case O(BUILD,AssumeClean): /* assume clean */
dd0781e5
NB
473 assume_clean = 1;
474 continue;
475
476 case O(GROW,'n'):
e0d19036
NB
477 case O(CREATE,'n'):
478 case O(BUILD,'n'): /* number of raid disks */
52826846 479 if (raiddisks) {
b83d95f3 480 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
52826846
NB
481 raiddisks, optarg);
482 exit(2);
483 }
484 raiddisks = strtol(optarg, &c, 10);
e4c4352e 485 if (!optarg[0] || *c || raiddisks<=0) {
b83d95f3 486 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
52826846
NB
487 optarg);
488 exit(2);
489 }
cd29a5c8 490 ident.raid_disks = raiddisks;
52826846
NB
491 continue;
492
e0d19036 493 case O(CREATE,'x'): /* number of spare (eXtra) discs */
52826846 494 if (sparedisks) {
b83d95f3 495 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
52826846
NB
496 sparedisks, optarg);
497 exit(2);
498 }
98c6faba 499 if (level != UnSet && level <= 0 && level >= -1) {
b83d95f3 500 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
52826846
NB
501 level);
502 exit(2);
503 }
504 sparedisks = strtol(optarg, &c, 10);
e4c4352e 505 if (!optarg[0] || *c || sparedisks < 0) {
b83d95f3 506 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
52826846
NB
507 optarg);
508 exit(2);
509 }
510 continue;
dd0781e5
NB
511
512 case O(CREATE,'a'):
513 case O(BUILD,'a'):
514 case O(ASSEMBLE,'a'): /* auto-creation of device node */
f1ae21c4 515 autof = parse_auto(optarg, "--auto flag", 0);
dd0781e5
NB
516 continue;
517
38098016
NB
518 case O(CREATE,Symlinks):
519 case O(BUILD,Symlinks):
520 case O(ASSEMBLE,Symlinks): /* auto creation of symlinks in /dev to /dev/md */
521 symlinks = optarg;
522 continue;
523
aa88f531 524 case O(BUILD,'f'): /* force honouring '-n 1' */
bd72c2b2 525 case O(GROW,'f'): /* ditto */
e0d19036
NB
526 case O(CREATE,'f'): /* force honouring of device list */
527 case O(ASSEMBLE,'f'): /* force assembly */
528 case O(MISC,'f'): /* force zero */
52826846
NB
529 force=1;
530 continue;
531
532 /* now for the Assemble options */
3d3dd91e 533 case O(CREATE,'u'): /* uuid of array */
e0d19036 534 case O(ASSEMBLE,'u'): /* uuid of array */
52826846 535 if (ident.uuid_set) {
cd29a5c8 536 fprintf(stderr, Name ": uuid cannot be set twice. "
52826846
NB
537 "Second value %s.\n", optarg);
538 exit(2);
539 }
540 if (parse_uuid(optarg, ident.uuid))
541 ident.uuid_set = 1;
542 else {
543 fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
544 exit(2);
545 }
546 continue;
547
947fd4dd
NB
548 case O(CREATE,'N'):
549 case O(ASSEMBLE,'N'):
550 if (ident.name[0]) {
551 fprintf(stderr, Name ": name cannot be set twice. "
552 "Second value %s.\n", optarg);
553 exit(2);
554 }
555 if (strlen(optarg) > 32) {
556 fprintf(stderr, Name ": name '%s' is too long, 32 chars max.\n",
557 optarg);
558 exit(2);
559 }
560 strcpy(ident.name, optarg);
561 continue;
562
e0d19036 563 case O(ASSEMBLE,'m'): /* super-minor for array */
98c6faba 564 if (ident.super_minor != UnSet) {
cd29a5c8
NB
565 fprintf(stderr, Name ": super-minor cannot be set twice. "
566 "Second value: %s.\n", optarg);
567 exit(2);
568 }
d013a55e
NB
569 if (strcmp(optarg, "dev")==0)
570 ident.super_minor = -2;
571 else {
572 ident.super_minor = strtoul(optarg, &cp, 10);
573 if (!optarg[0] || *cp) {
574 fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
575 exit(2);
576 }
cd29a5c8
NB
577 }
578 continue;
579
5787fa49
NB
580 case O(ASSEMBLE,'U'): /* update the superblock */
581 if (update) {
582 fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n",
583 update, optarg);
584 exit(2);
585 }
586 update = optarg;
e5329c37
NB
587 if (strcmp(update, "sparc2.2")==0)
588 continue;
5787fa49
NB
589 if (strcmp(update, "super-minor") == 0)
590 continue;
feb716e9
NB
591 if (strcmp(update, "summaries")==0)
592 continue;
e5329c37
NB
593 if (strcmp(update, "resync")==0)
594 continue;
7d99579f
NB
595 if (strcmp(update, "uuid")==0)
596 continue;
c4f12c13
NB
597 if (strcmp(update, "name")==0)
598 continue;
0237e0ca
NB
599 if (strcmp(update, "homehost")==0)
600 continue;
bee8ec56
NB
601 if (strcmp(update, "devicesize")==0)
602 continue;
586ed405
NB
603 if (strcmp(update, "byteorder")==0) {
604 if (ss) {
605 fprintf(stderr, Name ": must not set metadata type with --update=byteorder.\n");
606 exit(2);
607 }
608 for(i=0; !ss && superlist[i]; i++)
609 ss = superlist[i]->match_metadata_desc("0.swap");
610 if (!ss) {
611 fprintf(stderr, Name ": INTERNAL ERROR cannot find 0.swap\n");
612 exit(2);
613 }
614
615 continue;
616 }
0047d254
NB
617 if (strcmp(update,"?") == 0 ||
618 strcmp(update, "help") == 0) {
619 outf = stdout;
620 fprintf(outf, Name ": ");
621 } else {
622 outf = stderr;
623 fprintf(outf,
624 Name ": '--update=%s' is invalid. ",
625 update);
626 }
627 fprintf(outf, "Valid --update options are:\n"
bee8ec56
NB
628 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
629 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
0047d254 630 exit(outf == stdout ? 0 : 2);
5787fa49 631
997aed5d 632 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
0047d254
NB
633 runstop = -1; /* --stop isn't allowed for --assemble,
634 * so we overload slightly */
b8a8ccf9
NB
635 continue;
636
e0d19036 637 case O(ASSEMBLE,'c'): /* config file */
2d465520 638 case O(MISC, 'c'):
e0d19036 639 case O(MONITOR,'c'):
52826846
NB
640 if (configfile) {
641 fprintf(stderr, Name ": configfile cannot be set twice. "
642 "Second value is %s.\n", optarg);
643 exit(2);
644 }
645 configfile = optarg;
8aec876d 646 set_conffile(configfile);
52826846
NB
647 /* FIXME possibly check that config file exists. Even parse it */
648 continue;
e0d19036
NB
649 case O(ASSEMBLE,'s'): /* scan */
650 case O(MISC,'s'):
651 case O(MONITOR,'s'):
8382f19b 652 case O(INCREMENTAL,'s'):
52826846
NB
653 scan = 1;
654 continue;
655
e0d19036 656 case O(MONITOR,'m'): /* mail address */
52826846
NB
657 if (mailaddr)
658 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
659 optarg);
660 else
661 mailaddr = optarg;
662 continue;
663
e0d19036 664 case O(MONITOR,'p'): /* alert program */
52826846
NB
665 if (program)
666 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
667 optarg);
668 else
669 program = optarg;
670 continue;
64c4757e 671
e0d19036 672 case O(MONITOR,'d'): /* delay in seconds */
f5e166fe 673 case O(GROW, 'd'):
c82f047c
NB
674 case O(BUILD,'d'): /* delay for bitmap updates */
675 case O(CREATE,'d'):
52826846
NB
676 if (delay)
677 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
678 optarg);
679 else {
680 delay = strtol(optarg, &c, 10);
681 if (!optarg[0] || *c || delay<1) {
682 fprintf(stderr, Name ": invalid delay: %s\n",
683 optarg);
684 exit(2);
685 }
686 }
687 continue;
d013a55e
NB
688 case O(MONITOR,'f'): /* daemonise */
689 daemonise = 1;
690 continue;
b5e64645
NB
691 case O(MONITOR,'i'): /* pid */
692 if (pidfile)
693 fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
694 optarg);
695 else
696 pidfile = optarg;
697 continue;
aa88f531
NB
698 case O(MONITOR,'1'): /* oneshot */
699 oneshot = 1;
700 continue;
98c6faba
NB
701 case O(MONITOR,'t'): /* test */
702 test = 1;
703 continue;
773135f5
NB
704 case O(MONITOR,'y'): /* log messages to syslog */
705 openlog("mdadm", 0, SYSLOG_FACILITY);
706 dosyslog = 1;
707 continue;
52826846
NB
708
709 /* now the general management options. Some are applicable
710 * to other modes. None have arguments.
711 */
e5329c37 712 case O(GROW,'a'):
dd0781e5 713 case O(MANAGE,'a'): /* add a drive */
52826846 714 devmode = 'a';
fe80f49b
NB
715 re_add = 0;
716 continue;
997aed5d 717 case O(MANAGE,ReAdd):
fe80f49b
NB
718 devmode = 'a';
719 re_add = 1;
52826846 720 continue;
e0d19036 721 case O(MANAGE,'r'): /* remove a drive */
52826846
NB
722 devmode = 'r';
723 continue;
e0d19036 724 case O(MANAGE,'f'): /* set faulty */
52826846
NB
725 devmode = 'f';
726 continue;
8382f19b 727 case O(INCREMENTAL,'R'):
e0d19036
NB
728 case O(MANAGE,'R'):
729 case O(ASSEMBLE,'R'):
730 case O(BUILD,'R'):
731 case O(CREATE,'R'): /* Run the array */
52826846
NB
732 if (runstop < 0) {
733 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
734 exit(2);
735 }
736 runstop = 1;
737 continue;
e0d19036 738 case O(MANAGE,'S'):
52826846
NB
739 if (runstop > 0) {
740 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
741 exit(2);
742 }
743 runstop = -1;
744 continue;
745
e0d19036 746 case O(MANAGE,'o'):
52826846
NB
747 if (readonly < 0) {
748 fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
749 exit(2);
750 }
751 readonly = 1;
752 continue;
e0d19036 753 case O(MANAGE,'w'):
52826846 754 if (readonly > 0) {
e0d19036 755 fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n");
52826846
NB
756 exit(2);
757 }
758 readonly = -1;
759 continue;
e0d19036
NB
760
761 case O(MISC,'Q'):
762 case O(MISC,'D'):
763 case O(MISC,'E'):
764 case O(MISC,'K'):
765 case O(MISC,'R'):
766 case O(MISC,'S'):
c82f047c 767 case O(MISC,'X'):
e0d19036
NB
768 case O(MISC,'o'):
769 case O(MISC,'w'):
b90c0e9a 770 case O(MISC,'W'):
e0d19036
NB
771 if (devmode && devmode != opt &&
772 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
773 fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n",
774 devmode =='E'?opt:devmode);
775 exit(2);
776 }
777 devmode = opt;
778 continue;
feb716e9
NB
779 case O(MISC,'t'):
780 test = 1;
781 continue;
e0d19036 782
997aed5d 783 case O(MISC, Sparc22):
bd526cee
NB
784 if (devmode != 'E') {
785 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
786 exit(2);
787 }
788 SparcAdjust = 1;
789 continue;
c82f047c
NB
790
791 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
792 if (!optarg) {
793 fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n");
794 exit(2);
795 }
55935d51
NB
796 if (strcmp(optarg, "internal")==0) {
797 fprintf(stderr, Name ": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
798 continue;
799 }
c82f047c
NB
800 bitmap_fd = open(optarg, O_RDWR);
801 if (!*optarg || bitmap_fd < 0) {
802 fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno));
803 exit(2);
804 }
805 ident.bitmap_fd = bitmap_fd; /* for Assemble */
806 continue;
f5e166fe 807
997aed5d
NB
808 case O(ASSEMBLE, BackupFile):
809 case O(GROW, BackupFile):
06b0d786
NB
810 /* Specify a file into which grow might place a backup,
811 * or from which assemble might recover a backup
812 */
813 if (backup_file) {
814 fprintf(stderr, Name ": backup file already specified, rejecting %s\n", optarg);
815 exit(2);
816 }
817 backup_file = optarg;
818 continue;
819
f5e166fe 820 case O(GROW,'b'):
c82f047c
NB
821 case O(BUILD,'b'):
822 case O(CREATE,'b'): /* here we create the bitmap */
1e0d770c
NB
823 if (strcmp(optarg, "internal")== 0 ||
824 strcmp(optarg, "none")== 0 ||
825 strchr(optarg, '/') != NULL) {
826 bitmap_file = optarg;
827 continue;
828 }
829 /* probable typo */
830 fprintf(stderr, Name ": bitmap file must contain a '/', or be 'internal', or 'none'\n");
831 exit(2);
c82f047c 832
997aed5d
NB
833 case O(GROW,BitmapChunk):
834 case O(BUILD,BitmapChunk):
835 case O(CREATE,BitmapChunk): /* bitmap chunksize */
c82f047c
NB
836 bitmap_chunk = strtol(optarg, &c, 10);
837 if (!optarg[0] || *c || bitmap_chunk < 0 ||
838 bitmap_chunk & (bitmap_chunk - 1)) {
839 fprintf(stderr, Name ": invalid bitmap chunksize: %s\n",
840 optarg);
841 exit(2);
842 }
843 /* convert K to B, chunk of 0K means 512B */
844 bitmap_chunk = bitmap_chunk ? bitmap_chunk * 1024 : 512;
845 continue;
dfd4d8ee 846
7d19ad0d 847 case O(GROW, WriteBehind):
997aed5d
NB
848 case O(BUILD, WriteBehind):
849 case O(CREATE, WriteBehind): /* write-behind mode */
dfd4d8ee
NB
850 write_behind = DEFAULT_MAX_WRITE_BEHIND;
851 if (optarg) {
852 write_behind = strtol(optarg, &c, 10);
853 if (write_behind < 0 || *c ||
854 write_behind > 16383) {
855 fprintf(stderr, Name ": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
856 exit(2);
857 }
858 }
859 continue;
8382f19b
NB
860
861 case O(INCREMENTAL, 'r'):
862 rebuild_map = 1;
863 continue;
52826846
NB
864 }
865 /* We have now processed all the valid options. Anything else is
866 * an error
867 */
06b0d786
NB
868 if (option_index > 0)
869 fprintf(stderr, Name ":option --%s not valid in %s mode\n",
870 long_options[option_index].name,
871 map_num(modes, mode));
872 else
873 fprintf(stderr, Name ": option -%c not valid in %s mode\n",
874 opt, map_num(modes, mode));
64c4757e 875 exit(2);
52826846
NB
876
877 }
878
3d4064cc
NB
879 if (print_help) {
880 char *help_text = Help;
881 if (print_help == 2)
882 help_text = OptionHelp;
883 else
884 switch (mode) {
885 case ASSEMBLE : help_text = Help_assemble; break;
886 case BUILD : help_text = Help_build; break;
887 case CREATE : help_text = Help_create; break;
888 case MANAGE : help_text = Help_manage; break;
889 case MISC : help_text = Help_misc; break;
890 case MONITOR : help_text = Help_monitor; break;
891 case GROW : help_text = Help_grow; break;
8382f19b 892 case INCREMENTAL:help_text= Help_incr; break;
3d4064cc 893 }
0047d254 894 fputs(help_text,stdout);
3d4064cc
NB
895 exit(0);
896 }
897
0320ea45
NB
898 if (!mode && devs_found) {
899 mode = MISC;
900 devmode = 'Q';
901 if (devlist->disposition == 0)
902 devlist->disposition = devmode;
903 }
52826846
NB
904 if (!mode) {
905 fputs(Usage, stderr);
64c4757e 906 exit(2);
64c4757e 907 }
38098016
NB
908
909 if (symlinks) {
910 struct createinfo *ci = conf_get_create_info();
911
912 if (strcasecmp(symlinks, "yes") == 0)
913 ci->symlinks = 1;
914 else if (strcasecmp(symlinks, "no") == 0)
915 ci->symlinks = 0;
916 else {
917 fprintf(stderr, Name ": option --symlinks must be 'no' or 'yes'\n");
918 exit(2);
919 }
920 }
52826846
NB
921 /* Ok, got the option parsing out of the way
922 * hopefully it's mostly right but there might be some stuff
923 * missing
924 *
e0d19036 925 * That is mosty checked in the per-mode stuff but...
52826846
NB
926 *
927 * For @,B,C and A without -s, the first device listed must be an md device
928 * we check that here and open it.
64c4757e 929 */
52826846 930
dd0781e5
NB
931 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
932 (mode == ASSEMBLE && ! scan)) {
52826846
NB
933 if (devs_found < 1) {
934 fprintf(stderr, Name ": an md device must be given in this mode\n");
935 exit(2);
936 }
dd0781e5
NB
937 if ((int)ident.super_minor == -2 && autof) {
938 fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
939 exit(2);
940 }
1d1e104b 941 if (mode == MANAGE || mode == GROW)
d55e3aef 942 autof=1; /* Don't create */
dd0781e5 943 mdfd = open_mddev(devlist->devname, autof);
52826846
NB
944 if (mdfd < 0)
945 exit(1);
98c6faba 946 if ((int)ident.super_minor == -2) {
d013a55e
NB
947 struct stat stb;
948 fstat(mdfd, &stb);
0df46c2a 949 ident.super_minor = minor(stb.st_rdev);
d013a55e 950 }
64c4757e 951 }
52826846 952
e4c4352e
NB
953 if (raiddisks) {
954 if (raiddisks > max_disks) {
6fbba4c9
NB
955 fprintf(stderr, Name ": invalid number of raid devices: %d\n",
956 raiddisks);
e4c4352e
NB
957 exit(2);
958 }
959 if (raiddisks == 1 && !force && level != -5) {
960 fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
961 " a mistake. If you really mean it you will need to specify --force before\n"
962 " setting the number of drives.\n");
963 exit(2);
964 }
965 }
966 if (sparedisks) {
967 if ( sparedisks > max_disks - raiddisks) {
6fbba4c9
NB
968 fprintf(stderr, Name ": invalid number of spare-devices: %d\n",
969 sparedisks);
e4c4352e
NB
970 exit(2);
971 }
972 }
c82f047c 973
997aed5d 974 if (homehost == NULL)
8aec876d 975 homehost = conf_get_homehost();
05697ec1
NB
976 if (homehost && strcmp(homehost, "<system>")==0) {
977 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
978 sys_hostname[sizeof(sys_hostname)-1] = 0;
979 homehost = sys_hostname;
980 }
981 }
997aed5d 982
52826846
NB
983 rv = 0;
984 switch(mode) {
e0d19036 985 case MANAGE:
52826846
NB
986 /* readonly, add/remove, readwrite, runstop */
987 if (readonly>0)
cd29a5c8 988 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 989 if (!rv && devs_found>1)
cd29a5c8 990 rv = Manage_subdevs(devlist->devname, mdfd,
dab6685f 991 devlist->next, verbose-quiet);
52826846 992 if (!rv && readonly < 0)
cd29a5c8 993 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 994 if (!rv && runstop)
ab56093f 995 rv = Manage_runstop(devlist->devname, mdfd, runstop, quiet);
52826846 996 break;
e0d19036 997 case ASSEMBLE:
d013a55e 998 if (devs_found == 1 && ident.uuid_set == 0 &&
947fd4dd 999 ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) {
d013a55e 1000 /* Only a device has been given, so get details from config file */
8aec876d 1001 mddev_ident_t array_ident = conf_get_ident(devlist->devname);
b5e64645
NB
1002 if (array_ident == NULL) {
1003 fprintf(stderr, Name ": %s not identified in config file.\n",
1004 devlist->devname);
d013a55e 1005 rv |= 1;
b5e64645 1006 } else {
1337546d
NB
1007 mdfd = open_mddev(devlist->devname,
1008 array_ident->autof ? array_ident->autof : autof);
b5e64645 1009 if (mdfd < 0)
d013a55e 1010 rv |= 1;
b5e64645 1011 else {
8aec876d 1012 rv |= Assemble(ss, devlist->devname, mdfd, array_ident,
06b0d786 1013 NULL, backup_file,
e5eac01f 1014 readonly, runstop, update, homehost, verbose-quiet, force);
b5e64645
NB
1015 close(mdfd);
1016 }
d013a55e
NB
1017 }
1018 } else if (!scan)
8aec876d 1019 rv = Assemble(ss, devlist->devname, mdfd, &ident,
06b0d786 1020 devlist->next, backup_file,
e5eac01f 1021 readonly, runstop, update, homehost, verbose-quiet, force);
5787fa49
NB
1022 else if (devs_found>0) {
1023 if (update && devs_found > 1) {
1024 fprintf(stderr, Name ": can only update a single array at a time\n");
1025 exit(1);
1026 }
06b0d786
NB
1027 if (backup_file && devs_found > 1) {
1028 fprintf(stderr, Name ": can only assemble a single array when providing a backup file.\n");
1029 exit(1);
1030 }
cd29a5c8 1031 for (dv = devlist ; dv ; dv=dv->next) {
8aec876d 1032 mddev_ident_t array_ident = conf_get_ident(dv->devname);
52826846
NB
1033 if (array_ident == NULL) {
1034 fprintf(stderr, Name ": %s not identified in config file.\n",
cd29a5c8 1035 dv->devname);
52826846
NB
1036 rv |= 1;
1037 continue;
1038 }
1337546d
NB
1039 mdfd = open_mddev(dv->devname,
1040 array_ident->autof ?array_ident->autof : autof);
b5e64645
NB
1041 if (mdfd < 0) {
1042 rv |= 1;
1043 continue;
1044 }
8aec876d 1045 rv |= Assemble(ss, dv->devname, mdfd, array_ident,
06b0d786 1046 NULL, backup_file,
e5eac01f 1047 readonly, runstop, update, homehost, verbose-quiet, force);
b5e64645 1048 close(mdfd);
52826846 1049 }
5787fa49 1050 } else {
8aec876d
NB
1051 mddev_ident_t array_list = conf_get_ident(NULL);
1052 mddev_dev_t devlist = conf_get_devs();
da6b5ca9
NB
1053 int cnt = 0;
1054 if (devlist == NULL) {
1055 fprintf(stderr, Name ": No devices listed in conf file were found.\n");
1056 exit(1);
1057 }
1058 if (update) {
1059 fprintf(stderr, Name ": --update not meaningful with a --scan assembly.\n");
1060 exit(1);
1061 }
1062 if (backup_file) {
1063 fprintf(stderr, Name ": --backup_file not meaningful with a --scan assembly.\n");
1064 exit(1);
1065 }
1066 for (; array_list; array_list = array_list->next) {
1067 mdu_array_info_t array;
1068 mdfd = open_mddev(array_list->devname,
1069 array_list->autof ? array_list->autof : autof);
1070 if (mdfd < 0) {
1071 rv |= 1;
1072 continue;
06b0d786 1073 }
da6b5ca9
NB
1074 if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
1075 /* already assembled, skip */
1cac4c1b 1076 cnt++;
da6b5ca9
NB
1077 else {
1078 rv |= Assemble(ss, array_list->devname, mdfd,
8aec876d 1079 array_list,
43f2372a 1080 NULL, NULL,
da6b5ca9
NB
1081 readonly, runstop, NULL, homehost, verbose-quiet, force);
1082 if (rv == 0) cnt++;
06b0d786 1083 }
da6b5ca9
NB
1084 close(mdfd);
1085 }
1086 if (homehost) {
1087 /* Maybe we can auto-assemble something.
1088 * Repeatedly call Assemble in auto-assmble mode
1089 * until it fails
1090 */
1091 int rv2;
d55e3aef
NB
1092 int acnt;
1093 ident.autof = autof;
da6b5ca9 1094 do {
d55e3aef
NB
1095 acnt = 0;
1096 do {
1097 rv2 = Assemble(ss, NULL, -1,
8aec876d 1098 &ident,
43f2372a 1099 NULL, NULL,
d55e3aef
NB
1100 readonly, runstop, NULL, homehost, verbose-quiet, force);
1101 if (rv2==0) {
1102 cnt++;
1103 acnt++;
1104 }
589395d6
NB
1105 if (rv2 == 1)
1106 /* found something so even though assembly failed we
1107 * want to avoid auto-updates
1108 */
1109 auto_update_home = 0;
d55e3aef
NB
1110 } while (rv2!=2);
1111 /* Incase there are stacked devices, we need to go around again */
d55e3aef 1112 } while (acnt);
589395d6
NB
1113 if (cnt == 0 && auto_update_home && homehost) {
1114 /* Nothing found, maybe we need to bootstrap homehost info */
1115 do {
1116 acnt = 0;
1117 do {
1118 rv2 = Assemble(ss, NULL, -1,
8aec876d 1119 &ident,
43f2372a 1120 NULL, NULL,
589395d6
NB
1121 readonly, runstop, "homehost", homehost, verbose-quiet, force);
1122 if (rv2==0) {
1123 cnt++;
1124 acnt++;
1125 }
1126 } while (rv2!=2);
1127 /* Incase there are stacked devices, we need to go around again */
589395d6
NB
1128 } while (acnt);
1129 }
da6b5ca9
NB
1130 if (cnt == 0 && rv == 0) {
1131 fprintf(stderr, Name ": No arrays found in config file or automatically\n");
1132 rv = 1;
52826846 1133 }
da6b5ca9
NB
1134 } else if (cnt == 0 && rv == 0) {
1135 fprintf(stderr, Name ": No arrays found in config file\n");
1136 rv = 1;
1137 }
52826846
NB
1138 }
1139 break;
e0d19036 1140 case BUILD:
c82f047c 1141 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
dfd4d8ee
NB
1142 if (write_behind && !bitmap_file) {
1143 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1144 rv = 1;
1145 break;
1146 }
5b28bd56
NB
1147 if (raiddisks == 0) {
1148 fprintf(stderr, Name ": no raid-disks specified.\n");
1149 rv = 1;
1150 break;
1151 }
dfd4d8ee 1152
c82f047c 1153 if (bitmap_file) {
55935d51
NB
1154 if (strcmp(bitmap_file, "internal")==0) {
1155 fprintf(stderr, Name ": 'internal' bitmaps not supported with --build\n");
1156 rv |= 1;
1157 break;
1158 }
c82f047c
NB
1159 }
1160 rv = Build(devlist->devname, mdfd, chunk, level, layout,
1161 raiddisks, devlist->next, assume_clean,
dab6685f 1162 bitmap_file, bitmap_chunk, write_behind, delay, verbose-quiet);
52826846 1163 break;
e0d19036 1164 case CREATE:
c82f047c 1165 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
dfd4d8ee
NB
1166 if (write_behind && !bitmap_file) {
1167 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1168 rv = 1;
1169 break;
1170 }
5b28bd56
NB
1171 if (raiddisks == 0) {
1172 fprintf(stderr, Name ": no raid-disks specified.\n");
1173 rv = 1;
1174 break;
1175 }
f9ce90ba
NB
1176
1177 rv = Create(ss, devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size,
05697ec1 1178 raiddisks, sparedisks, ident.name, homehost,
3d3dd91e 1179 ident.uuid_set ? ident.uuid : NULL,
47d79ef8 1180 devs_found-1, devlist->next, runstop, verbose-quiet, force, assume_clean,
dfd4d8ee 1181 bitmap_file, bitmap_chunk, write_behind, delay);
52826846 1182 break;
e0d19036 1183 case MISC:
e0d19036
NB
1184 if (devmode == 'E') {
1185 if (devlist == NULL && !scan) {
1186 fprintf(stderr, Name ": No devices to examine\n");
1187 exit(2);
1188 }
1189 if (devlist == NULL)
8aec876d 1190 devlist = conf_get_devs();
e0d19036 1191 if (devlist == NULL) {
c913b90e 1192 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
e0d19036
NB
1193 exit(1);
1194 }
e843d5d7
NB
1195 if (brief && verbose)
1196 brief = 2;
a1cbd7d0 1197 rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief, scan, SparcAdjust, ss, homehost);
e0d19036
NB
1198 } else {
1199 if (devlist == NULL) {
91f068bf
NB
1200 if (devmode=='D' && scan) {
1201 /* apply --detail to all devices in /proc/mdstat */
22a88995 1202 struct mdstat_ent *ms = mdstat_read(0, 1);
e0d19036
NB
1203 struct mdstat_ent *e;
1204 for (e=ms ; e ; e=e->next) {
1205 char *name = get_md_name(e->devnum);
1206
1207 if (!name) {
1208 fprintf(stderr, Name ": cannot find device file for %s\n",
1209 e->dev);
1210 continue;
1211 }
54bad364
KS
1212 rv |= Detail(name, verbose>1?0:verbose+1,
1213 export, test, homehost);
91f068bf
NB
1214 put_md_name(name);
1215 }
1216 } else if (devmode == 'S' && scan) {
1217 /* apply --stop to all devices in /proc/mdstat */
1218 /* Due to possible stacking of devices, repeat until
1219 * nothing more can be stopped
1220 */
1221 int progress=1, err;
1222 int last = 0;
1223 do {
22a88995 1224 struct mdstat_ent *ms = mdstat_read(0, 0);
91f068bf
NB
1225 struct mdstat_ent *e;
1226
1227 if (!progress) last = 1;
1228 progress = 0; err = 0;
1229 for (e=ms ; e ; e=e->next) {
1230 char *name = get_md_name(e->devnum);
1231
1232 if (!name) {
1233 fprintf(stderr, Name ": cannot find device file for %s\n",
1234 e->dev);
1235 continue;
1236 }
d55e3aef 1237 mdfd = open_mddev(name, 1);
43fc1676 1238 if (mdfd >= 0) {
ab56093f 1239 if (Manage_runstop(name, mdfd, -1, quiet?1:last?0:-1))
91f068bf
NB
1240 err = 1;
1241 else
1242 progress = 1;
43fc1676
NB
1243 close(mdfd);
1244 }
91f068bf
NB
1245
1246 put_md_name(name);
e0d19036 1247 }
91f068bf 1248 } while (!last && err);
f9c25f1d 1249 if (err) rv |= 1;
91f068bf 1250 } else {
e0d19036
NB
1251 fprintf(stderr, Name ": No devices given.\n");
1252 exit(2);
1253 }
1254 }
1255 for (dv=devlist ; dv; dv=dv->next) {
1256 switch(dv->disposition) {
1257 case 'D':
54bad364
KS
1258 rv |= Detail(dv->devname,
1259 brief?1+verbose:0,
1260 export, test, homehost);
1261 continue;
e0d19036 1262 case 'K': /* Zero superblock */
6409687b 1263 rv |= Kill(dv->devname, force, quiet); continue;
e0d19036
NB
1264 case 'Q':
1265 rv |= Query(dv->devname); continue;
c82f047c 1266 case 'X':
55935d51 1267 rv |= ExamineBitmap(dv->devname, brief, ss); continue;
b90c0e9a
NB
1268 case 'W':
1269 rv |= Wait(dv->devname); continue;
e0d19036 1270 }
d55e3aef 1271 mdfd = open_mddev(dv->devname, 1);
43fc1676 1272 if (mdfd>=0) {
e0d19036
NB
1273 switch(dv->disposition) {
1274 case 'R':
ab56093f 1275 rv |= Manage_runstop(dv->devname, mdfd, 1, quiet); break;
e0d19036 1276 case 'S':
ab56093f 1277 rv |= Manage_runstop(dv->devname, mdfd, -1, quiet); break;
e0d19036
NB
1278 case 'o':
1279 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1280 case 'w':
1281 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1282 }
43fc1676 1283 close(mdfd);
7f48e210
NB
1284 } else
1285 rv |= 1;
e0d19036 1286 }
cd29a5c8 1287 }
9a9dab36 1288 break;
e0d19036 1289 case MONITOR:
e0d19036
NB
1290 if (!devlist && !scan) {
1291 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
1292 rv = 1;
1293 break;
1294 }
b5e64645
NB
1295 if (pidfile && !daemonise) {
1296 fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
1297 rv = 1;
1298 break;
1299 }
d013a55e 1300 rv= Monitor(devlist, mailaddr, program,
773135f5 1301 delay?delay:60, daemonise, scan, oneshot,
8aec876d 1302 dosyslog, test, pidfile);
9a9dab36 1303 break;
dd0781e5
NB
1304
1305 case GROW:
1306 if (devs_found > 1) {
e5329c37
NB
1307
1308 /* must be '-a'. */
1309 if (size >= 0 || raiddisks) {
1310 fprintf(stderr, Name ": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1311 rv = 1;
1312 break;
1313 }
1314 for (dv=devlist->next; dv ; dv=dv->next) {
1315 rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
1316 if (rv)
1317 break;
1318 }
f5e166fe
NB
1319 } else if ((size >= 0) + (raiddisks != 0) + (layout != UnSet) + (bitmap_file != NULL)> 1) {
1320 fprintf(stderr, Name ": can change at most one of size, raiddisks, bitmap, and layout\n");
dd0781e5
NB
1321 rv = 1;
1322 break;
b5e64645
NB
1323 } else if (layout != UnSet)
1324 rv = Manage_reconfig(devlist->devname, mdfd, layout);
1325 else if (size >= 0 || raiddisks)
06b0d786 1326 rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file,
e86c9dd6 1327 size, level, layout, chunk, raiddisks);
f5e166fe
NB
1328 else if (bitmap_file) {
1329 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1330 rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file,
8fac0577 1331 bitmap_chunk, delay, write_behind, force);
f5e166fe 1332 } else
b5e64645 1333 fprintf(stderr, Name ": no changes to --grow\n");
dd0781e5 1334 break;
8382f19b
NB
1335 case INCREMENTAL:
1336 if (rebuild_map) {
1337 RebuildMap();
1338 }
1339 if (scan) {
1340 if (runstop <= 0) {
1341 fprintf(stderr, Name
1342 ": --incremental --scan meaningless without --run.\n");
1343 break;
1344 }
1345 rv = IncrementalScan(verbose);
1346 }
1347 if (!devlist) {
1348 if (!rebuild_map && !scan) {
1349 fprintf(stderr, Name
1350 ": --incremental requires a device.\n");
1351 rv = 1;
1352 }
1353 break;
1354 }
1355 if (devlist->next) {
1356 fprintf(stderr, Name
1357 ": --incremental can only handle one device.\n");
1358 rv = 1;
1359 break;
1360 }
1361 rv = Incremental(devlist->devname, verbose-quiet, runstop,
1362 ss, homehost, autof);
1f48664b
NB
1363 break;
1364 case AUTODETECT:
1365 autodetect();
1366 break;
64c4757e 1367 }
52826846 1368 exit(rv);
64c4757e 1369}