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