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