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