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