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