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