]> git.ipfire.org Git - thirdparty/mdadm.git/blame - mdadm.c
Support fail/remove using kernel name
[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
997aed5d 674 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
0047d254
NB
675 runstop = -1; /* --stop isn't allowed for --assemble,
676 * so we overload slightly */
b8a8ccf9
NB
677 continue;
678
e0d19036 679 case O(ASSEMBLE,'c'): /* config file */
7b187ed7 680 case O(INCREMENTAL, 'c'):
2d465520 681 case O(MISC, 'c'):
e0d19036 682 case O(MONITOR,'c'):
52826846
NB
683 if (configfile) {
684 fprintf(stderr, Name ": configfile cannot be set twice. "
685 "Second value is %s.\n", optarg);
686 exit(2);
687 }
688 configfile = optarg;
8aec876d 689 set_conffile(configfile);
52826846
NB
690 /* FIXME possibly check that config file exists. Even parse it */
691 continue;
e0d19036
NB
692 case O(ASSEMBLE,'s'): /* scan */
693 case O(MISC,'s'):
694 case O(MONITOR,'s'):
8382f19b 695 case O(INCREMENTAL,'s'):
52826846
NB
696 scan = 1;
697 continue;
698
e0d19036 699 case O(MONITOR,'m'): /* mail address */
52826846
NB
700 if (mailaddr)
701 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
702 optarg);
703 else
704 mailaddr = optarg;
705 continue;
706
e0d19036 707 case O(MONITOR,'p'): /* alert program */
52826846
NB
708 if (program)
709 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
710 optarg);
711 else
712 program = optarg;
713 continue;
64c4757e 714
9a36a9b7
ZB
715 case O(MONITOR,'r'): /* rebuild increments */
716 increments = atoi(optarg);
717 if (increments>99 || increments<1) {
718 fprintf(stderr, Name ": please specify positive integer between 1 and 99 as rebuild increments.\n");
719 exit(2);
720 }
721 continue;
722
e0d19036 723 case O(MONITOR,'d'): /* delay in seconds */
f5e166fe 724 case O(GROW, 'd'):
c82f047c
NB
725 case O(BUILD,'d'): /* delay for bitmap updates */
726 case O(CREATE,'d'):
52826846
NB
727 if (delay)
728 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
729 optarg);
730 else {
731 delay = strtol(optarg, &c, 10);
732 if (!optarg[0] || *c || delay<1) {
733 fprintf(stderr, Name ": invalid delay: %s\n",
734 optarg);
735 exit(2);
736 }
737 }
738 continue;
d013a55e
NB
739 case O(MONITOR,'f'): /* daemonise */
740 daemonise = 1;
741 continue;
b5e64645
NB
742 case O(MONITOR,'i'): /* pid */
743 if (pidfile)
744 fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
745 optarg);
746 else
747 pidfile = optarg;
748 continue;
aa88f531
NB
749 case O(MONITOR,'1'): /* oneshot */
750 oneshot = 1;
751 continue;
98c6faba
NB
752 case O(MONITOR,'t'): /* test */
753 test = 1;
754 continue;
773135f5 755 case O(MONITOR,'y'): /* log messages to syslog */
6ac8aac2 756 openlog("mdadm", LOG_PID, SYSLOG_FACILITY);
773135f5
NB
757 dosyslog = 1;
758 continue;
52826846
NB
759
760 /* now the general management options. Some are applicable
761 * to other modes. None have arguments.
762 */
e5329c37 763 case O(GROW,'a'):
dd0781e5 764 case O(MANAGE,'a'): /* add a drive */
52826846 765 devmode = 'a';
fe80f49b
NB
766 re_add = 0;
767 continue;
997aed5d 768 case O(MANAGE,ReAdd):
fe80f49b
NB
769 devmode = 'a';
770 re_add = 1;
52826846 771 continue;
e0d19036 772 case O(MANAGE,'r'): /* remove a drive */
52826846
NB
773 devmode = 'r';
774 continue;
e0d19036 775 case O(MANAGE,'f'): /* set faulty */
52826846
NB
776 devmode = 'f';
777 continue;
8382f19b 778 case O(INCREMENTAL,'R'):
e0d19036
NB
779 case O(MANAGE,'R'):
780 case O(ASSEMBLE,'R'):
781 case O(BUILD,'R'):
782 case O(CREATE,'R'): /* Run the array */
52826846
NB
783 if (runstop < 0) {
784 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
785 exit(2);
786 }
787 runstop = 1;
788 continue;
e0d19036 789 case O(MANAGE,'S'):
52826846
NB
790 if (runstop > 0) {
791 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
792 exit(2);
793 }
794 runstop = -1;
795 continue;
796
e0d19036
NB
797 case O(MISC,'Q'):
798 case O(MISC,'D'):
799 case O(MISC,'E'):
800 case O(MISC,'K'):
801 case O(MISC,'R'):
802 case O(MISC,'S'):
c82f047c 803 case O(MISC,'X'):
e0d19036
NB
804 case O(MISC,'o'):
805 case O(MISC,'w'):
b90c0e9a 806 case O(MISC,'W'):
1770662b 807 case O(MISC, Waitclean):
4cce4069 808 case O(MISC, DetailPlatform):
e0d19036
NB
809 if (devmode && devmode != opt &&
810 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
811 fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n",
812 devmode =='E'?opt:devmode);
813 exit(2);
814 }
815 devmode = opt;
816 continue;
feb716e9
NB
817 case O(MISC,'t'):
818 test = 1;
819 continue;
e0d19036 820
997aed5d 821 case O(MISC, Sparc22):
bd526cee
NB
822 if (devmode != 'E') {
823 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
824 exit(2);
825 }
826 SparcAdjust = 1;
827 continue;
c82f047c
NB
828
829 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
830 if (!optarg) {
831 fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n");
832 exit(2);
833 }
55935d51
NB
834 if (strcmp(optarg, "internal")==0) {
835 fprintf(stderr, Name ": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
836 continue;
837 }
c82f047c
NB
838 bitmap_fd = open(optarg, O_RDWR);
839 if (!*optarg || bitmap_fd < 0) {
840 fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno));
841 exit(2);
842 }
843 ident.bitmap_fd = bitmap_fd; /* for Assemble */
844 continue;
f5e166fe 845
997aed5d
NB
846 case O(ASSEMBLE, BackupFile):
847 case O(GROW, BackupFile):
06b0d786
NB
848 /* Specify a file into which grow might place a backup,
849 * or from which assemble might recover a backup
850 */
851 if (backup_file) {
852 fprintf(stderr, Name ": backup file already specified, rejecting %s\n", optarg);
853 exit(2);
854 }
855 backup_file = optarg;
856 continue;
857
c82f047c
NB
858 case O(BUILD,'b'):
859 case O(CREATE,'b'): /* here we create the bitmap */
85375d6d
NB
860 if (strcmp(optarg, "none") == 0) {
861 fprintf(stderr, Name ": '--bitmap none' only"
862 " support for --grow\n");
863 exit(2);
864 }
865 /* FALL THROUGH */
866 case O(GROW,'b'):
1e0d770c
NB
867 if (strcmp(optarg, "internal")== 0 ||
868 strcmp(optarg, "none")== 0 ||
869 strchr(optarg, '/') != NULL) {
870 bitmap_file = optarg;
871 continue;
872 }
873 /* probable typo */
a1331cc4
N
874 fprintf(stderr, Name ": bitmap file must contain a '/', or be 'internal', or 'none'\n"
875 " not '%s'\n", optarg);
1e0d770c 876 exit(2);
c82f047c 877
997aed5d
NB
878 case O(GROW,BitmapChunk):
879 case O(BUILD,BitmapChunk):
880 case O(CREATE,BitmapChunk): /* bitmap chunksize */
c82f047c
NB
881 bitmap_chunk = strtol(optarg, &c, 10);
882 if (!optarg[0] || *c || bitmap_chunk < 0 ||
883 bitmap_chunk & (bitmap_chunk - 1)) {
884 fprintf(stderr, Name ": invalid bitmap chunksize: %s\n",
885 optarg);
886 exit(2);
887 }
888 /* convert K to B, chunk of 0K means 512B */
889 bitmap_chunk = bitmap_chunk ? bitmap_chunk * 1024 : 512;
890 continue;
dfd4d8ee 891
7d19ad0d 892 case O(GROW, WriteBehind):
997aed5d
NB
893 case O(BUILD, WriteBehind):
894 case O(CREATE, WriteBehind): /* write-behind mode */
dfd4d8ee
NB
895 write_behind = DEFAULT_MAX_WRITE_BEHIND;
896 if (optarg) {
897 write_behind = strtol(optarg, &c, 10);
898 if (write_behind < 0 || *c ||
899 write_behind > 16383) {
900 fprintf(stderr, Name ": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
901 exit(2);
902 }
903 }
904 continue;
8382f19b
NB
905
906 case O(INCREMENTAL, 'r'):
907 rebuild_map = 1;
908 continue;
52826846
NB
909 }
910 /* We have now processed all the valid options. Anything else is
911 * an error
912 */
06b0d786
NB
913 if (option_index > 0)
914 fprintf(stderr, Name ":option --%s not valid in %s mode\n",
915 long_options[option_index].name,
916 map_num(modes, mode));
917 else
918 fprintf(stderr, Name ": option -%c not valid in %s mode\n",
919 opt, map_num(modes, mode));
64c4757e 920 exit(2);
52826846
NB
921
922 }
923
3d4064cc
NB
924 if (print_help) {
925 char *help_text = Help;
926 if (print_help == 2)
927 help_text = OptionHelp;
928 else
929 switch (mode) {
930 case ASSEMBLE : help_text = Help_assemble; break;
931 case BUILD : help_text = Help_build; break;
932 case CREATE : help_text = Help_create; break;
933 case MANAGE : help_text = Help_manage; break;
934 case MISC : help_text = Help_misc; break;
935 case MONITOR : help_text = Help_monitor; break;
936 case GROW : help_text = Help_grow; break;
8382f19b 937 case INCREMENTAL:help_text= Help_incr; break;
3d4064cc 938 }
0047d254 939 fputs(help_text,stdout);
3d4064cc
NB
940 exit(0);
941 }
942
0320ea45
NB
943 if (!mode && devs_found) {
944 mode = MISC;
945 devmode = 'Q';
946 if (devlist->disposition == 0)
947 devlist->disposition = devmode;
948 }
52826846
NB
949 if (!mode) {
950 fputs(Usage, stderr);
64c4757e 951 exit(2);
64c4757e 952 }
38098016
NB
953
954 if (symlinks) {
955 struct createinfo *ci = conf_get_create_info();
956
957 if (strcasecmp(symlinks, "yes") == 0)
958 ci->symlinks = 1;
959 else if (strcasecmp(symlinks, "no") == 0)
960 ci->symlinks = 0;
961 else {
962 fprintf(stderr, Name ": option --symlinks must be 'no' or 'yes'\n");
963 exit(2);
964 }
965 }
52826846
NB
966 /* Ok, got the option parsing out of the way
967 * hopefully it's mostly right but there might be some stuff
968 * missing
969 *
e0d19036 970 * That is mosty checked in the per-mode stuff but...
52826846
NB
971 *
972 * For @,B,C and A without -s, the first device listed must be an md device
973 * we check that here and open it.
64c4757e 974 */
52826846 975
dd0781e5
NB
976 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
977 (mode == ASSEMBLE && ! scan)) {
52826846
NB
978 if (devs_found < 1) {
979 fprintf(stderr, Name ": an md device must be given in this mode\n");
980 exit(2);
981 }
dd0781e5 982 if ((int)ident.super_minor == -2 && autof) {
aba69144 983 fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
dd0781e5
NB
984 exit(2);
985 }
7f91af49 986 if (mode == MANAGE || mode == GROW) {
6be1d39d 987 mdfd = open_mddev(devlist->devname, 1);
7f91af49
N
988 if (mdfd < 0)
989 exit(1);
990 } else
991 /* non-existent device is OK */
992 mdfd = open_mddev(devlist->devname, 0);
993 if (mdfd == -2) {
994 fprintf(stderr, Name ": device %s exists but is not an "
995 "md array.\n", devlist->devname);
52826846 996 exit(1);
7f91af49 997 }
98c6faba 998 if ((int)ident.super_minor == -2) {
d013a55e 999 struct stat stb;
7f91af49
N
1000 if (mdfd < 0) {
1001 fprintf(stderr, Name ": --super-minor=dev given, and "
1002 "listed device %s doesn't exist.\n",
1003 devlist->devname);
1004 exit(1);
1005 }
d013a55e 1006 fstat(mdfd, &stb);
0df46c2a 1007 ident.super_minor = minor(stb.st_rdev);
d013a55e 1008 }
7f91af49
N
1009 if (mdfd >= 0 && mode != MANAGE && mode != GROW) {
1010 /* We don't really want this open yet, we just might
1011 * have wanted to check some things
1012 */
1013 close(mdfd);
1014 mdfd = -1;
1015 }
64c4757e 1016 }
52826846 1017
e4c4352e
NB
1018 if (raiddisks) {
1019 if (raiddisks > max_disks) {
6fbba4c9
NB
1020 fprintf(stderr, Name ": invalid number of raid devices: %d\n",
1021 raiddisks);
e4c4352e
NB
1022 exit(2);
1023 }
1024 if (raiddisks == 1 && !force && level != -5) {
1025 fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
1026 " a mistake. If you really mean it you will need to specify --force before\n"
1027 " setting the number of drives.\n");
1028 exit(2);
1029 }
1030 }
1031 if (sparedisks) {
1032 if ( sparedisks > max_disks - raiddisks) {
6fbba4c9
NB
1033 fprintf(stderr, Name ": invalid number of spare-devices: %d\n",
1034 sparedisks);
e4c4352e
NB
1035 exit(2);
1036 }
1037 }
c82f047c 1038
997aed5d 1039 if (homehost == NULL)
0ac91628 1040 homehost = conf_get_homehost(&require_homehost);
9180c81b 1041 if (homehost == NULL || strcmp(homehost, "<system>")==0) {
05697ec1
NB
1042 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
1043 sys_hostname[sizeof(sys_hostname)-1] = 0;
1044 homehost = sys_hostname;
1045 }
1046 }
997aed5d 1047
ac5678dd
N
1048 if ((mode != MISC || devmode != 'E') &&
1049 geteuid() != 0) {
1050 fprintf(stderr, Name ": must be super-user to perform this action\n");
1051 exit(1);
1052 }
1053
7f91af49
N
1054 ident.autof = autof;
1055
52826846
NB
1056 rv = 0;
1057 switch(mode) {
e0d19036 1058 case MANAGE:
52826846
NB
1059 /* readonly, add/remove, readwrite, runstop */
1060 if (readonly>0)
cd29a5c8 1061 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 1062 if (!rv && devs_found>1)
cd29a5c8 1063 rv = Manage_subdevs(devlist->devname, mdfd,
dab6685f 1064 devlist->next, verbose-quiet);
52826846 1065 if (!rv && readonly < 0)
cd29a5c8 1066 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 1067 if (!rv && runstop)
ab56093f 1068 rv = Manage_runstop(devlist->devname, mdfd, runstop, quiet);
52826846 1069 break;
e0d19036 1070 case ASSEMBLE:
d013a55e 1071 if (devs_found == 1 && ident.uuid_set == 0 &&
947fd4dd 1072 ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) {
d013a55e 1073 /* Only a device has been given, so get details from config file */
8aec876d 1074 mddev_ident_t array_ident = conf_get_ident(devlist->devname);
b5e64645
NB
1075 if (array_ident == NULL) {
1076 fprintf(stderr, Name ": %s not identified in config file.\n",
1077 devlist->devname);
d013a55e 1078 rv |= 1;
7f91af49
N
1079 if (mdfd >= 0)
1080 close(mdfd);
b5e64645 1081 } else {
7f91af49
N
1082 if (array_ident->autof == 0)
1083 array_ident->autof = autof;
1084 rv |= Assemble(ss, devlist->devname, array_ident,
1085 NULL, backup_file,
0ac91628
N
1086 readonly, runstop, update,
1087 homehost, require_homehost,
1088 verbose-quiet, force);
d013a55e
NB
1089 }
1090 } else if (!scan)
7f91af49 1091 rv = Assemble(ss, devlist->devname, &ident,
06b0d786 1092 devlist->next, backup_file,
0ac91628
N
1093 readonly, runstop, update,
1094 homehost, require_homehost,
1095 verbose-quiet, force);
5787fa49
NB
1096 else if (devs_found>0) {
1097 if (update && devs_found > 1) {
1098 fprintf(stderr, Name ": can only update a single array at a time\n");
1099 exit(1);
1100 }
06b0d786
NB
1101 if (backup_file && devs_found > 1) {
1102 fprintf(stderr, Name ": can only assemble a single array when providing a backup file.\n");
1103 exit(1);
1104 }
cd29a5c8 1105 for (dv = devlist ; dv ; dv=dv->next) {
8aec876d 1106 mddev_ident_t array_ident = conf_get_ident(dv->devname);
52826846
NB
1107 if (array_ident == NULL) {
1108 fprintf(stderr, Name ": %s not identified in config file.\n",
cd29a5c8 1109 dv->devname);
52826846
NB
1110 rv |= 1;
1111 continue;
1112 }
7f91af49
N
1113 if (array_ident->autof == 0)
1114 array_ident->autof = autof;
1115 rv |= Assemble(ss, dv->devname, array_ident,
06b0d786 1116 NULL, backup_file,
0ac91628
N
1117 readonly, runstop, update,
1118 homehost, require_homehost,
1119 verbose-quiet, force);
52826846 1120 }
5787fa49 1121 } else {
b179246f 1122 mddev_ident_t a, array_list = conf_get_ident(NULL);
8aec876d 1123 mddev_dev_t devlist = conf_get_devs();
da6b5ca9 1124 int cnt = 0;
b179246f 1125 int failures, successes;
da6b5ca9
NB
1126 if (devlist == NULL) {
1127 fprintf(stderr, Name ": No devices listed in conf file were found.\n");
1128 exit(1);
1129 }
1130 if (update) {
1131 fprintf(stderr, Name ": --update not meaningful with a --scan assembly.\n");
1132 exit(1);
1133 }
1134 if (backup_file) {
1135 fprintf(stderr, Name ": --backup_file not meaningful with a --scan assembly.\n");
1136 exit(1);
1137 }
b179246f
N
1138 for (a = array_list; a ; a = a->next) {
1139 a->assembled = 0;
1140 if (a->autof == 0)
1141 a->autof = autof;
da6b5ca9 1142 }
b179246f
N
1143 do {
1144 failures = 0;
1145 successes = 0;
1146 rv = 0;
1147 for (a = array_list; a ; a = a->next) {
1148 int r;
1149 if (a->assembled)
1150 continue;
1151 if (a->devname &&
1152 strcasecmp(a->devname, "<ignore>") == 0)
1153 continue;
1154
1155 r = Assemble(ss, a->devname,
1156 a,
1157 NULL, NULL,
1158 readonly, runstop, NULL,
1159 homehost, require_homehost,
1160 verbose-quiet, force);
1161 if (r == 0) {
1162 a->assembled = 1;
1163 successes++;
1164 } else
1165 failures++;
1166 rv |= r;
1167 cnt++;
1168 }
1169 } while (failures && successes);
a30b2ecd 1170 if (homehost && cnt == 0) {
da6b5ca9 1171 /* Maybe we can auto-assemble something.
7f91af49 1172 * Repeatedly call Assemble in auto-assemble mode
da6b5ca9
NB
1173 * until it fails
1174 */
1175 int rv2;
d55e3aef
NB
1176 int acnt;
1177 ident.autof = autof;
da6b5ca9 1178 do {
1c203a4b 1179 mddev_dev_t devlist = conf_get_devs();
d55e3aef
NB
1180 acnt = 0;
1181 do {
7f91af49 1182 rv2 = Assemble(ss, NULL,
8aec876d 1183 &ident,
1c203a4b 1184 devlist, NULL,
0ac91628
N
1185 readonly, runstop, NULL,
1186 homehost, require_homehost,
1187 verbose-quiet, force);
d55e3aef
NB
1188 if (rv2==0) {
1189 cnt++;
1190 acnt++;
1191 }
589395d6
NB
1192 if (rv2 == 1)
1193 /* found something so even though assembly failed we
1194 * want to avoid auto-updates
1195 */
1196 auto_update_home = 0;
d55e3aef
NB
1197 } while (rv2!=2);
1198 /* Incase there are stacked devices, we need to go around again */
d55e3aef 1199 } while (acnt);
e0fe762a 1200#if 0
589395d6
NB
1201 if (cnt == 0 && auto_update_home && homehost) {
1202 /* Nothing found, maybe we need to bootstrap homehost info */
1203 do {
1204 acnt = 0;
1205 do {
7f91af49 1206 rv2 = Assemble(ss, NULL,
8aec876d 1207 &ident,
43f2372a 1208 NULL, NULL,
0ac91628
N
1209 readonly, runstop, "homehost",
1210 homehost, require_homehost,
1211 verbose-quiet, force);
589395d6
NB
1212 if (rv2==0) {
1213 cnt++;
1214 acnt++;
1215 }
1216 } while (rv2!=2);
1217 /* Incase there are stacked devices, we need to go around again */
589395d6
NB
1218 } while (acnt);
1219 }
e0fe762a 1220#endif
da6b5ca9
NB
1221 if (cnt == 0 && rv == 0) {
1222 fprintf(stderr, Name ": No arrays found in config file or automatically\n");
1223 rv = 1;
1c203a4b
NB
1224 } else if (cnt)
1225 rv = 0;
da6b5ca9
NB
1226 } else if (cnt == 0 && rv == 0) {
1227 fprintf(stderr, Name ": No arrays found in config file\n");
1228 rv = 1;
1229 }
52826846
NB
1230 }
1231 break;
e0d19036 1232 case BUILD:
c82f047c 1233 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
dfd4d8ee
NB
1234 if (write_behind && !bitmap_file) {
1235 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1236 rv = 1;
1237 break;
1238 }
5b28bd56 1239 if (raiddisks == 0) {
e5669f40 1240 fprintf(stderr, Name ": no raid-devices specified.\n");
5b28bd56
NB
1241 rv = 1;
1242 break;
1243 }
dfd4d8ee 1244
c82f047c 1245 if (bitmap_file) {
55935d51
NB
1246 if (strcmp(bitmap_file, "internal")==0) {
1247 fprintf(stderr, Name ": 'internal' bitmaps not supported with --build\n");
1248 rv |= 1;
1249 break;
1250 }
c82f047c 1251 }
7f91af49 1252 rv = Build(devlist->devname, chunk, level, layout,
c82f047c 1253 raiddisks, devlist->next, assume_clean,
7f91af49 1254 bitmap_file, bitmap_chunk, write_behind,
83208785 1255 delay, verbose-quiet, autof, size);
52826846 1256 break;
e0d19036 1257 case CREATE:
c82f047c 1258 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
dfd4d8ee
NB
1259 if (write_behind && !bitmap_file) {
1260 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1261 rv = 1;
1262 break;
1263 }
5b28bd56 1264 if (raiddisks == 0) {
e5669f40 1265 fprintf(stderr, Name ": no raid-devices specified.\n");
5b28bd56
NB
1266 rv = 1;
1267 break;
1268 }
f9ce90ba 1269
7f91af49 1270 rv = Create(ss, devlist->devname, chunk, level, layout, size<0 ? 0 : size,
05697ec1 1271 raiddisks, sparedisks, ident.name, homehost,
3d3dd91e 1272 ident.uuid_set ? ident.uuid : NULL,
47d79ef8 1273 devs_found-1, devlist->next, runstop, verbose-quiet, force, assume_clean,
7f91af49 1274 bitmap_file, bitmap_chunk, write_behind, delay, autof);
52826846 1275 break;
e0d19036 1276 case MISC:
e0d19036
NB
1277 if (devmode == 'E') {
1278 if (devlist == NULL && !scan) {
1279 fprintf(stderr, Name ": No devices to examine\n");
1280 exit(2);
1281 }
1282 if (devlist == NULL)
8aec876d 1283 devlist = conf_get_devs();
e0d19036 1284 if (devlist == NULL) {
c913b90e 1285 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
e0d19036
NB
1286 exit(1);
1287 }
e843d5d7
NB
1288 if (brief && verbose)
1289 brief = 2;
0d726f17
KS
1290 rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief,
1291 export, scan,
1292 SparcAdjust, ss, homehost);
4cce4069
DW
1293 } else if (devmode == DetailPlatform) {
1294 rv = Detail_Platform(ss ? ss->ss : NULL, ss ? scan : 1, verbose);
e0d19036
NB
1295 } else {
1296 if (devlist == NULL) {
fabbfd48
DW
1297 if ((devmode=='D' || devmode == Waitclean) && scan) {
1298 /* apply --detail or --wait-clean to
1299 * all devices in /proc/mdstat
1300 */
22a88995 1301 struct mdstat_ent *ms = mdstat_read(0, 1);
e0d19036 1302 struct mdstat_ent *e;
35904960 1303 struct map_ent *map = NULL;
8f1b2bbb 1304 int members;
fabbfd48
DW
1305 int v = verbose>1?0:verbose+1;
1306
8f1b2bbb 1307 for (members = 0; members <= 1; members++) {
e0d19036 1308 for (e=ms ; e ; e=e->next) {
35904960
N
1309 char *name;
1310 struct map_ent *me;
8f1b2bbb
N
1311 int member = e->metadata_version &&
1312 strncmp(e->metadata_version,
1313 "external:/", 10) == 0;
1314 if (members != member)
1315 continue;
35904960 1316 me = map_by_devnum(&map, e->devnum);
8615dcff
N
1317 if (me && me->path
1318 && strcmp(me->path, "/unknown") != 0)
35904960
N
1319 name = me->path;
1320 else
1321 name = get_md_name(e->devnum);
e0d19036
NB
1322
1323 if (!name) {
1324 fprintf(stderr, Name ": cannot find device file for %s\n",
1325 e->dev);
1326 continue;
1327 }
fabbfd48
DW
1328 if (devmode == 'D')
1329 rv |= Detail(name, v,
1330 export, test,
1331 homehost);
1332 else
9f1da824 1333 rv |= WaitClean(name, -1, v);
91f068bf
NB
1334 put_md_name(name);
1335 }
8f1b2bbb 1336 }
89a10d84 1337 free_mdstat(ms);
91f068bf
NB
1338 } else if (devmode == 'S' && scan) {
1339 /* apply --stop to all devices in /proc/mdstat */
1340 /* Due to possible stacking of devices, repeat until
1341 * nothing more can be stopped
1342 */
1343 int progress=1, err;
1344 int last = 0;
1345 do {
22a88995 1346 struct mdstat_ent *ms = mdstat_read(0, 0);
91f068bf
NB
1347 struct mdstat_ent *e;
1348
1349 if (!progress) last = 1;
1350 progress = 0; err = 0;
1351 for (e=ms ; e ; e=e->next) {
1352 char *name = get_md_name(e->devnum);
1353
1354 if (!name) {
1355 fprintf(stderr, Name ": cannot find device file for %s\n",
1356 e->dev);
1357 continue;
1358 }
6be1d39d 1359 mdfd = open_mddev(name, 1);
43fc1676 1360 if (mdfd >= 0) {
ab56093f 1361 if (Manage_runstop(name, mdfd, -1, quiet?1:last?0:-1))
91f068bf
NB
1362 err = 1;
1363 else
1364 progress = 1;
43fc1676
NB
1365 close(mdfd);
1366 }
91f068bf
NB
1367
1368 put_md_name(name);
e0d19036 1369 }
89a10d84 1370 free_mdstat(ms);
91f068bf 1371 } while (!last && err);
f9c25f1d 1372 if (err) rv |= 1;
91f068bf 1373 } else {
e0d19036
NB
1374 fprintf(stderr, Name ": No devices given.\n");
1375 exit(2);
1376 }
1377 }
1378 for (dv=devlist ; dv; dv=dv->next) {
1379 switch(dv->disposition) {
1380 case 'D':
54bad364
KS
1381 rv |= Detail(dv->devname,
1382 brief?1+verbose:0,
1383 export, test, homehost);
1384 continue;
e0d19036 1385 case 'K': /* Zero superblock */
9277cc77
N
1386 if (ss)
1387 rv |= Kill(dv->devname, ss, force, quiet,0);
1388 else {
1389 int q = quiet;
1390 do {
1391 rv |= Kill(dv->devname, NULL, force, q, 0);
1392 q = 1;
4c619b29 1393 } while (rv == 0);
9277cc77
N
1394 rv &= ~2;
1395 }
111d01fc 1396 continue;
e0d19036
NB
1397 case 'Q':
1398 rv |= Query(dv->devname); continue;
c82f047c 1399 case 'X':
55935d51 1400 rv |= ExamineBitmap(dv->devname, brief, ss); continue;
b90c0e9a
NB
1401 case 'W':
1402 rv |= Wait(dv->devname); continue;
1770662b 1403 case Waitclean:
9f1da824 1404 rv |= WaitClean(dv->devname, -1, verbose-quiet); continue;
e0d19036 1405 }
6be1d39d 1406 mdfd = open_mddev(dv->devname, 1);
43fc1676 1407 if (mdfd>=0) {
e0d19036
NB
1408 switch(dv->disposition) {
1409 case 'R':
ab56093f 1410 rv |= Manage_runstop(dv->devname, mdfd, 1, quiet); break;
e0d19036 1411 case 'S':
ab56093f 1412 rv |= Manage_runstop(dv->devname, mdfd, -1, quiet); break;
e0d19036
NB
1413 case 'o':
1414 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1415 case 'w':
1416 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1417 }
43fc1676 1418 close(mdfd);
7f48e210
NB
1419 } else
1420 rv |= 1;
e0d19036 1421 }
cd29a5c8 1422 }
9a9dab36 1423 break;
e0d19036 1424 case MONITOR:
e0d19036
NB
1425 if (!devlist && !scan) {
1426 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
1427 rv = 1;
1428 break;
1429 }
b5e64645
NB
1430 if (pidfile && !daemonise) {
1431 fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
1432 rv = 1;
1433 break;
1434 }
ddc7201f
N
1435 if (delay == 0) {
1436 if (get_linux_version() > 20616)
1437 /* mdstat responds to poll */
1438 delay = 1000;
1439 else
1440 delay = 60;
1441 }
d013a55e 1442 rv= Monitor(devlist, mailaddr, program,
773135f5 1443 delay?delay:60, daemonise, scan, oneshot,
9a36a9b7 1444 dosyslog, test, pidfile, increments);
9a9dab36 1445 break;
dd0781e5
NB
1446
1447 case GROW:
84e11361
N
1448 if (array_size >= 0) {
1449 /* alway impose array size first, independent of
1450 * anything else
9ce510be
N
1451 * Do not allow level or raid_disks changes at the
1452 * same time as that can be irreversibly destructive.
84e11361
N
1453 */
1454 struct mdinfo sra;
1455 int err;
9ce510be
N
1456 if (raiddisks || level != UnSet) {
1457 fprintf(stderr, Name ": cannot change array size in same operation "
1458 "as changing raiddisks or level.\n"
1459 " Change size first, then check that data is still intact.\n");
1460 rv = 1;
1461 break;
1462 }
84e11361
N
1463 sysfs_init(&sra, mdfd, 0);
1464 if (array_size == 0)
1465 err = sysfs_set_str(&sra, NULL, "array_size", "default");
1466 else
1467 err = sysfs_set_num(&sra, NULL, "array_size", array_size / 2);
1468 if (err < 0) {
1469 if (errno == E2BIG)
1470 fprintf(stderr, Name ": --array-size setting"
1471 " is too large.\n");
1472 else
1473 fprintf(stderr, Name ": current kernel does"
1474 " not support setting --array-size\n");
1475 rv = 1;
1476 break;
1477 }
1478 }
dd0781e5 1479 if (devs_found > 1) {
aba69144 1480
e5329c37 1481 /* must be '-a'. */
7236ee7a
N
1482 if (size >= 0 || raiddisks || chunk || layout_str != NULL || bitmap_file) {
1483 fprintf(stderr, Name ": --add cannot be used with other geometry changes in --grow mode\n");
e5329c37
NB
1484 rv = 1;
1485 break;
1486 }
1487 for (dv=devlist->next; dv ; dv=dv->next) {
1488 rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
1489 if (rv)
1490 break;
1491 }
7236ee7a
N
1492 } else if (bitmap_file) {
1493 if (size >= 0 || raiddisks || chunk || layout_str != NULL) {
1494 fprintf(stderr, Name ": --bitmap changes cannot be used with other geometry changes in --grow mode\n");
1495 rv = 1;
1496 break;
1497 }
1498 if (delay == 0)
1499 delay = DEFAULT_BITMAP_DELAY;
f5e166fe 1500 rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file,
8fac0577 1501 bitmap_chunk, delay, write_behind, force);
7236ee7a
N
1502 } else if (size >= 0 || raiddisks != 0 || layout_str != NULL
1503 || chunk != 0 || level != UnSet) {
1504 rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file,
1505 size, level, layout_str, chunk, raiddisks);
84e11361 1506 } else if (array_size < 0)
b5e64645 1507 fprintf(stderr, Name ": no changes to --grow\n");
dd0781e5 1508 break;
8382f19b
NB
1509 case INCREMENTAL:
1510 if (rebuild_map) {
1511 RebuildMap();
1512 }
1513 if (scan) {
1514 if (runstop <= 0) {
1515 fprintf(stderr, Name
1516 ": --incremental --scan meaningless without --run.\n");
1517 break;
1518 }
1519 rv = IncrementalScan(verbose);
1520 }
1521 if (!devlist) {
1522 if (!rebuild_map && !scan) {
1523 fprintf(stderr, Name
1524 ": --incremental requires a device.\n");
1525 rv = 1;
1526 }
1527 break;
1528 }
1529 if (devlist->next) {
1530 fprintf(stderr, Name
1531 ": --incremental can only handle one device.\n");
1532 rv = 1;
1533 break;
1534 }
1535 rv = Incremental(devlist->devname, verbose-quiet, runstop,
0ac91628 1536 ss, homehost, require_homehost, autof);
1f48664b
NB
1537 break;
1538 case AUTODETECT:
1539 autodetect();
1540 break;
64c4757e 1541 }
52826846 1542 exit(rv);
64c4757e 1543}