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