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