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