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