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