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