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