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