]> git.ipfire.org Git - thirdparty/mdadm.git/blame - mdadm.c
Improve code for recognising config lines.
[thirdparty/mdadm.git] / mdadm.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
4f589ad0 4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
64c4757e
NB
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
c82f047c 28 *
dfd4d8ee 29 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
c82f047c 30 * Paul Clements, SteelEye Technology, Inc.
64c4757e
NB
31 */
32
9a9dab36 33#include "mdadm.h"
64c4757e 34#include "md_p.h"
dd0781e5 35#include <ctype.h>
64c4757e 36
dd0781e5 37
52826846
NB
38int main(int argc, char *argv[])
39{
e0d19036 40 int mode = 0;
52826846 41 int opt;
e0d19036 42 int option_index;
52826846
NB
43 char *c;
44 int rv;
f9ce90ba 45 int i;
52826846
NB
46
47 int chunk = 0;
5dd497ee 48 long long size = -1;
98c6faba
NB
49 int level = UnSet;
50 int layout = UnSet;
52826846 51 int raiddisks = 0;
e27d562b 52 int max_disks = MD_SB_DISKS; /* just a default */
52826846
NB
53 int sparedisks = 0;
54 struct mddev_ident_s ident;
55 char *configfile = NULL;
cd29a5c8 56 char *cp;
5787fa49 57 char *update = NULL;
52826846
NB
58 int scan = 0;
59 char devmode = 0;
60 int runstop = 0;
61 int readonly = 0;
dfd4d8ee 62 int write_behind = 0;
c82f047c
NB
63 int bitmap_fd = -1;
64 char *bitmap_file = NULL;
06b0d786 65 char *backup_file = NULL;
c82f047c 66 int bitmap_chunk = UnSet;
bd526cee 67 int SparcAdjust = 0;
cd29a5c8
NB
68 mddev_dev_t devlist = NULL;
69 mddev_dev_t *devlistend = & devlist;
70 mddev_dev_t dv;
52826846
NB
71 int devs_found = 0;
72 int verbose = 0;
dab6685f 73 int quiet = 0;
cd29a5c8 74 int brief = 0;
52826846 75 int force = 0;
feb716e9 76 int test = 0;
dd0781e5 77 int assume_clean = 0;
f1ae21c4
NB
78 /* autof indicates whether and how to create device node.
79 * bottom 3 bits are style. Rest (when shifted) are number of parts
80 * 0 - unset
81 * 1 - don't create (no)
82 * 2 - if is_standard, then create (yes)
83 * 3 - create as 'md' - reject is_standard mdp (md)
84 * 4 - create as 'mdp' - reject is_standard md (mdp)
85 * 5 - default to md if not is_standard (md in config file)
86 * 6 - default to mdp if not is_standard (part, or mdp in config file)
87 */
88 int autof = 0;
52826846
NB
89
90 char *mailaddr = NULL;
91 char *program = NULL;
92 int delay = 0;
d013a55e 93 int daemonise = 0;
b5e64645 94 char *pidfile = NULL;
aa88f531 95 int oneshot = 0;
82d9eba6 96 struct supertype *ss = NULL;
dfd4d8ee 97 int writemostly = 0;
fe80f49b 98 int re_add = 0;
c06487ce 99 char *shortopt = short_options;
773135f5 100 int dosyslog = 0;
52826846 101
e5329c37 102 int copies;
3d4064cc 103 int print_help = 0;
e5329c37 104
52826846
NB
105 int mdfd = -1;
106
dfe47e00
NB
107 srandom(time(0) ^ getpid());
108
52826846 109 ident.uuid_set=0;
98c6faba
NB
110 ident.level = UnSet;
111 ident.raid_disks = UnSet;
112 ident.super_minor= UnSet;
52826846 113 ident.devices=0;
a75f2fbc
NB
114 ident.spare_group = NULL;
115 ident.autof = 0;
a825febc 116 ident.st = NULL;
c82f047c 117 ident.bitmap_fd = -1;
7ef02d01 118 ident.bitmap_file = NULL;
947fd4dd 119 ident.name[0] = 0;
52826846 120
e0d19036
NB
121 while ((option_index = -1) ,
122 (opt=getopt_long(argc, argv,
c06487ce 123 shortopt, long_options,
e0d19036
NB
124 &option_index)) != -1) {
125 int newmode = mode;
dab6685f 126 /* firstly, some mode-independant options */
52826846 127 switch(opt) {
52826846 128 case 'h':
56eedc1a
NB
129 if (option_index > 0 &&
130 strcmp(long_options[option_index].name, "help-options")==0)
3d4064cc 131 print_help = 2;
56eedc1a 132 else
3d4064cc
NB
133 print_help = 1;
134 continue;
52826846
NB
135
136 case 'V':
137 fputs(Version, stderr);
138 exit(0);
139
22892d56 140 case 'v': verbose++;
52826846
NB
141 continue;
142
dab6685f
NB
143 case 'q': quiet++;
144 continue;
145
c82f047c 146 case 'b':
f5e166fe 147 if (mode == ASSEMBLE || mode == BUILD || mode == CREATE || mode == GROW)
c82f047c
NB
148 break; /* b means bitmap */
149 brief = 1;
c06487ce
NB
150 if (optarg) {
151 fprintf(stderr, Name ": -b cannot have any extra immediately after it, sorry.\n");
152 exit(2);
153 }
cd29a5c8
NB
154 continue;
155
e0d19036
NB
156 case ':':
157 case '?':
158 fputs(Usage, stderr);
159 exit(2);
160 }
161 /* second, figure out the mode.
162 * Some options force the mode. Others
163 * set the mode if it isn't already
164 */
165
166 switch(opt) {
167 case '@': /* just incase they say --manage */
c06487ce
NB
168 newmode = MANAGE;
169 shortopt = short_bitmap_auto_options;
170 break;
e0d19036
NB
171 case 'a':
172 case 'r':
173 case 'f':
fe80f49b 174 case 6: /* re-add */
c06487ce
NB
175 if (!mode) {
176 newmode = MANAGE;
177 shortopt = short_bitmap_auto_options;
178 }
0320ea45 179 break;
e0d19036 180
c06487ce
NB
181 case 'A': newmode = ASSEMBLE; shortopt = short_bitmap_auto_options; break;
182 case 'B': newmode = BUILD; shortopt = short_bitmap_auto_options; break;
183 case 'C': newmode = CREATE; shortopt = short_bitmap_auto_options; break;
e0d19036 184 case 'F': newmode = MONITOR;break;
c06487ce 185 case 'G': newmode = GROW; shortopt = short_bitmap_auto_options; break;
e0d19036
NB
186
187 case '#':
188 case 'D':
189 case 'E':
c82f047c 190 case 'X':
e0d19036
NB
191 case 'Q': newmode = MISC; break;
192 case 'R':
193 case 'S':
194 case 'o':
195 case 'w':
196 case 'K': if (!mode) newmode = MISC; break;
197 }
198 if (mode && newmode == mode) {
199 /* everybody happy ! */
200 } else if (mode && newmode != mode) {
201 /* not allowed.. */
202 fprintf(stderr, Name ": ");
203 if (option_index >= 0)
204 fprintf(stderr, "--%s", long_options[option_index].name);
205 else
206 fprintf(stderr, "-%c", opt);
e6b64cd0 207 fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
e0d19036
NB
208 map_num(modes, newmode),
209 map_num(modes, mode));
210 exit(2);
211 } else if (!mode && newmode) {
212 mode = newmode;
213 } else {
214 /* special case of -c --help */
215 if (opt == 'c' &&
216 ( strncmp(optarg, "--h", 3)==0 ||
217 strncmp(optarg, "-h", 2)==0)) {
218 fputs(Help_config, stderr);
219 exit(0);
cd29a5c8 220 }
0320ea45
NB
221
222 /* If first option is a device, don't force the mode yet */
223 if (opt == 1) {
224 if (devs_found == 0) {
225 dv = malloc(sizeof(*dv));
226 if (dv == NULL) {
227 fprintf(stderr, Name ": malloc failed\n");
228 exit(3);
229 }
230 dv->devname = optarg;
231 dv->disposition = devmode;
dfd4d8ee 232 dv->writemostly = writemostly;
fe80f49b 233 dv->re_add = re_add;
0320ea45
NB
234 dv->next = NULL;
235 *devlistend = dv;
236 devlistend = &dv->next;
237
238 devs_found++;
239 continue;
240 }
241 /* No mode yet, and this is the second device ... */
242 fprintf(stderr, Name ": An option must be given to set the mode before a second device is listed\n");
243 exit(2);
244 }
e0d19036 245 if (option_index >= 0)
0320ea45 246 fprintf(stderr, Name ": --%s", long_options[option_index].name);
e0d19036 247 else
0320ea45
NB
248 fprintf(stderr, Name ": -%c", opt);
249 fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
e0d19036
NB
250 exit(2);
251 }
252
253 /* if we just set the mode, then done */
254 switch(opt) {
255 case '@':
256 case '#':
257 case 'A':
258 case 'B':
259 case 'C':
260 case 'F':
dd0781e5 261 case 'G':
e0d19036
NB
262 continue;
263 }
264 if (opt == 1) {
265 /* an undecorated option - must be a device name.
266 */
cd29a5c8 267 if (devs_found > 0 && mode == '@' && !devmode) {
dd0781e5 268 fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
52826846
NB
269 exit(2);
270 }
e5329c37
NB
271 if (devs_found > 0 && mode == 'G' && !devmode) {
272 fprintf(stderr, Name ": Must give one of -a for devices do add: %s\n", optarg);
273 exit(2);
274 }
cd29a5c8
NB
275 dv = malloc(sizeof(*dv));
276 if (dv == NULL) {
277 fprintf(stderr, Name ": malloc failed\n");
278 exit(3);
279 }
280 dv->devname = optarg;
281 dv->disposition = devmode;
dfd4d8ee 282 dv->writemostly = writemostly;
fe80f49b 283 dv->re_add = re_add;
cd29a5c8
NB
284 dv->next = NULL;
285 *devlistend = dv;
286 devlistend = &dv->next;
287
52826846
NB
288 devs_found++;
289 continue;
682c7051 290 }
682c7051 291
52826846
NB
292 /* We've got a mode, and opt is now something else which
293 * could depend on the mode */
294#define O(a,b) ((a<<8)|b)
295 switch (O(mode,opt)) {
e0d19036
NB
296 case O(CREATE,'c'):
297 case O(BUILD,'c'): /* chunk or rounding */
52826846
NB
298 if (chunk) {
299 fprintf(stderr, Name ": chunk/rounding may only be specified once. "
300 "Second value is %s.\n", optarg);
301 exit(2);
302 }
303 chunk = strtol(optarg, &c, 10);
304 if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) {
305 fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
306 optarg);
307 exit(2);
308 }
309 continue;
64c4757e 310
f9ce90ba
NB
311 case O(CREATE,'e'):
312 case O(ASSEMBLE,'e'):
313 case O(MISC,'e'): /* set metadata (superblock) information */
314 if (ss) {
315 fprintf(stderr, Name ": metadata information already given\n");
316 exit(2);
317 }
82d9eba6
NB
318 for(i=0; !ss && superlist[i]; i++)
319 ss = superlist[i]->match_metadata_desc(optarg);
320
f9ce90ba
NB
321 if (!ss) {
322 fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg);
323 exit(2);
324 }
ea329559 325 max_disks = ss->max_devs;
f9ce90ba
NB
326 continue;
327
dfd4d8ee
NB
328 case O(MANAGE,'W'):
329 case O(BUILD,'W'):
330 case O(CREATE,'W'):
331 /* set write-mostly for following devices */
332 writemostly = 1;
333 continue;
334
dd0781e5 335 case O(GROW,'z'):
e0d19036 336 case O(CREATE,'z'): /* size */
dd0781e5 337 if (size >= 0) {
52826846
NB
338 fprintf(stderr, Name ": size may only be specified once. "
339 "Second value is %s.\n", optarg);
340 exit(2);
341 }
dd0781e5
NB
342 if (strcmp(optarg, "max")==0)
343 size = 0;
344 else {
5dd497ee 345 size = strtoll(optarg, &c, 10);
dd0781e5
NB
346 if (!optarg[0] || *c || size < 4) {
347 fprintf(stderr, Name ": invalid size: %s\n",
348 optarg);
349 exit(2);
350 }
52826846
NB
351 }
352 continue;
64c4757e 353
b5e64645 354 case O(GROW,'l'): /* hack - needed to understand layout */
e0d19036
NB
355 case O(CREATE,'l'):
356 case O(BUILD,'l'): /* set raid level*/
98c6faba 357 if (level != UnSet) {
52826846
NB
358 fprintf(stderr, Name ": raid level may only be set once. "
359 "Second value is %s.\n", optarg);
360 exit(2);
361 }
362 level = map_name(pers, optarg);
98c6faba 363 if (level == UnSet) {
52826846
NB
364 fprintf(stderr, Name ": invalid raid level: %s\n",
365 optarg);
366 exit(2);
367 }
b5e64645 368 if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) {
52826846
NB
369 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
370 optarg);
371 exit(2);
372 }
e0d19036 373 if (sparedisks > 0 && level < 1 && level >= -1) {
b83d95f3 374 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
52826846
NB
375 optarg);
376 exit(2);
377 }
cd29a5c8 378 ident.level = level;
52826846 379 continue;
64c4757e 380
e0d19036 381 case O(CREATE,'p'): /* raid5 layout */
b5e64645
NB
382 case O(BUILD,'p'): /* faulty layout */
383 case O(GROW, 'p'): /* faulty reconfig */
e5329c37 384 if (layout != UnSet) {
52826846
NB
385 fprintf(stderr,Name ": layout may only be sent once. "
386 "Second value was %s\n", optarg);
387 exit(2);
388 }
389 switch(level) {
390 default:
56eb10c0 391 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
52826846
NB
392 map_num(pers, level));
393 exit(2);
98c6faba 394 case UnSet:
52826846
NB
395 fprintf(stderr, Name ": raid level must be given before layout.\n");
396 exit(2);
397
398 case 5:
98c6faba 399 case 6:
52826846 400 layout = map_name(r5layout, optarg);
98c6faba 401 if (layout==UnSet) {
52826846
NB
402 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
403 optarg);
404 exit(2);
405 }
406 break;
e5329c37
NB
407
408 case 10:
b578481c
NB
409 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
410 if ((optarg[0] != 'n' && optarg[0] != 'f' && optarg[0] != 'o') ||
e5329c37
NB
411 (copies = strtoul(optarg+1, &cp, 10)) < 1 ||
412 copies > 200 ||
413 *cp) {
b578481c 414 fprintf(stderr, Name ": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg);
e5329c37
NB
415 exit(2);
416 }
417 if (optarg[0] == 'n')
418 layout = 256 + copies;
b578481c
NB
419 else if (optarg[0] == 'o')
420 layout = 0x10000 + (copies<<8) + 1;
e5329c37
NB
421 else
422 layout = 1 + (copies<<8);
423 break;
b5e64645
NB
424 case -5: /* Faulty
425 * modeNNN
426 */
427
428 {
429 int ln = strcspn(optarg, "0123456789");
430 char *m = strdup(optarg);
431 int mode;
432 m[ln] = 0;
433 mode = map_name(faultylayout, m);
434 if (mode == UnSet) {
435 fprintf(stderr, Name ": layout %s not understood for faulty.\n",
436 optarg);
437 exit(2);
438 }
439 layout = mode | (atoi(optarg+ln)<< ModeShift);
440 }
52826846
NB
441 }
442 continue;
443
dd0781e5
NB
444 case O(CREATE,3):
445 case O(BUILD,3): /* assume clean */
446 assume_clean = 1;
447 continue;
448
449 case O(GROW,'n'):
e0d19036
NB
450 case O(CREATE,'n'):
451 case O(BUILD,'n'): /* number of raid disks */
52826846 452 if (raiddisks) {
b83d95f3 453 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
52826846
NB
454 raiddisks, optarg);
455 exit(2);
456 }
457 raiddisks = strtol(optarg, &c, 10);
e4c4352e 458 if (!optarg[0] || *c || raiddisks<=0) {
b83d95f3 459 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
52826846
NB
460 optarg);
461 exit(2);
462 }
cd29a5c8 463 ident.raid_disks = raiddisks;
52826846
NB
464 continue;
465
e0d19036 466 case O(CREATE,'x'): /* number of spare (eXtra) discs */
52826846 467 if (sparedisks) {
b83d95f3 468 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
52826846
NB
469 sparedisks, optarg);
470 exit(2);
471 }
98c6faba 472 if (level != UnSet && level <= 0 && level >= -1) {
b83d95f3 473 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
52826846
NB
474 level);
475 exit(2);
476 }
477 sparedisks = strtol(optarg, &c, 10);
e4c4352e 478 if (!optarg[0] || *c || sparedisks < 0) {
b83d95f3 479 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
52826846
NB
480 optarg);
481 exit(2);
482 }
483 continue;
dd0781e5
NB
484
485 case O(CREATE,'a'):
486 case O(BUILD,'a'):
487 case O(ASSEMBLE,'a'): /* auto-creation of device node */
f1ae21c4 488 autof = parse_auto(optarg, "--auto flag", 0);
dd0781e5
NB
489 continue;
490
aa88f531 491 case O(BUILD,'f'): /* force honouring '-n 1' */
bd72c2b2 492 case O(GROW,'f'): /* ditto */
e0d19036
NB
493 case O(CREATE,'f'): /* force honouring of device list */
494 case O(ASSEMBLE,'f'): /* force assembly */
495 case O(MISC,'f'): /* force zero */
52826846
NB
496 force=1;
497 continue;
498
499 /* now for the Assemble options */
e0d19036 500 case O(ASSEMBLE,'u'): /* uuid of array */
52826846 501 if (ident.uuid_set) {
cd29a5c8 502 fprintf(stderr, Name ": uuid cannot be set twice. "
52826846
NB
503 "Second value %s.\n", optarg);
504 exit(2);
505 }
506 if (parse_uuid(optarg, ident.uuid))
507 ident.uuid_set = 1;
508 else {
509 fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
510 exit(2);
511 }
512 continue;
513
947fd4dd
NB
514 case O(CREATE,'N'):
515 case O(ASSEMBLE,'N'):
516 if (ident.name[0]) {
517 fprintf(stderr, Name ": name cannot be set twice. "
518 "Second value %s.\n", optarg);
519 exit(2);
520 }
521 if (strlen(optarg) > 32) {
522 fprintf(stderr, Name ": name '%s' is too long, 32 chars max.\n",
523 optarg);
524 exit(2);
525 }
526 strcpy(ident.name, optarg);
527 continue;
528
e0d19036 529 case O(ASSEMBLE,'m'): /* super-minor for array */
98c6faba 530 if (ident.super_minor != UnSet) {
cd29a5c8
NB
531 fprintf(stderr, Name ": super-minor cannot be set twice. "
532 "Second value: %s.\n", optarg);
533 exit(2);
534 }
d013a55e
NB
535 if (strcmp(optarg, "dev")==0)
536 ident.super_minor = -2;
537 else {
538 ident.super_minor = strtoul(optarg, &cp, 10);
539 if (!optarg[0] || *cp) {
540 fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
541 exit(2);
542 }
cd29a5c8
NB
543 }
544 continue;
545
5787fa49
NB
546 case O(ASSEMBLE,'U'): /* update the superblock */
547 if (update) {
548 fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n",
549 update, optarg);
550 exit(2);
551 }
552 update = optarg;
e5329c37
NB
553 if (strcmp(update, "sparc2.2")==0)
554 continue;
5787fa49
NB
555 if (strcmp(update, "super-minor") == 0)
556 continue;
feb716e9
NB
557 if (strcmp(update, "summaries")==0)
558 continue;
e5329c37
NB
559 if (strcmp(update, "resync")==0)
560 continue;
7d99579f
NB
561 if (strcmp(update, "uuid")==0)
562 continue;
586ed405
NB
563 if (strcmp(update, "byteorder")==0) {
564 if (ss) {
565 fprintf(stderr, Name ": must not set metadata type with --update=byteorder.\n");
566 exit(2);
567 }
568 for(i=0; !ss && superlist[i]; i++)
569 ss = superlist[i]->match_metadata_desc("0.swap");
570 if (!ss) {
571 fprintf(stderr, Name ": INTERNAL ERROR cannot find 0.swap\n");
572 exit(2);
573 }
574
575 continue;
576 }
7d99579f 577 fprintf(stderr, Name ": '--update %s' invalid. Only 'sparc2.2', 'super-minor', 'uuid', 'resync' or 'summaries' supported\n",update);
5787fa49
NB
578 exit(2);
579
b8a8ccf9
NB
580 case O(ASSEMBLE,4): /* --no-degraded */
581 runstop = -1; /* --stop isn't allowed for --assemble, so we overload slightly */
582 continue;
583
e0d19036 584 case O(ASSEMBLE,'c'): /* config file */
2d465520 585 case O(MISC, 'c'):
e0d19036 586 case O(MONITOR,'c'):
52826846
NB
587 if (configfile) {
588 fprintf(stderr, Name ": configfile cannot be set twice. "
589 "Second value is %s.\n", optarg);
590 exit(2);
591 }
592 configfile = optarg;
593 /* FIXME possibly check that config file exists. Even parse it */
594 continue;
e0d19036
NB
595 case O(ASSEMBLE,'s'): /* scan */
596 case O(MISC,'s'):
597 case O(MONITOR,'s'):
52826846
NB
598 scan = 1;
599 continue;
600
e0d19036 601 case O(MONITOR,'m'): /* mail address */
52826846
NB
602 if (mailaddr)
603 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
604 optarg);
605 else
606 mailaddr = optarg;
607 continue;
608
e0d19036 609 case O(MONITOR,'p'): /* alert program */
52826846
NB
610 if (program)
611 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
612 optarg);
613 else
614 program = optarg;
615 continue;
64c4757e 616
e0d19036 617 case O(MONITOR,'d'): /* delay in seconds */
f5e166fe 618 case O(GROW, 'd'):
c82f047c
NB
619 case O(BUILD,'d'): /* delay for bitmap updates */
620 case O(CREATE,'d'):
52826846
NB
621 if (delay)
622 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
623 optarg);
624 else {
625 delay = strtol(optarg, &c, 10);
626 if (!optarg[0] || *c || delay<1) {
627 fprintf(stderr, Name ": invalid delay: %s\n",
628 optarg);
629 exit(2);
630 }
631 }
632 continue;
d013a55e
NB
633 case O(MONITOR,'f'): /* daemonise */
634 daemonise = 1;
635 continue;
b5e64645
NB
636 case O(MONITOR,'i'): /* pid */
637 if (pidfile)
638 fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
639 optarg);
640 else
641 pidfile = optarg;
642 continue;
aa88f531
NB
643 case O(MONITOR,'1'): /* oneshot */
644 oneshot = 1;
645 continue;
98c6faba
NB
646 case O(MONITOR,'t'): /* test */
647 test = 1;
648 continue;
773135f5
NB
649 case O(MONITOR,'y'): /* log messages to syslog */
650 openlog("mdadm", 0, SYSLOG_FACILITY);
651 dosyslog = 1;
652 continue;
52826846
NB
653
654 /* now the general management options. Some are applicable
655 * to other modes. None have arguments.
656 */
e5329c37 657 case O(GROW,'a'):
dd0781e5 658 case O(MANAGE,'a'): /* add a drive */
52826846 659 devmode = 'a';
fe80f49b
NB
660 re_add = 0;
661 continue;
662 case O(MANAGE,6):
663 devmode = 'a';
664 re_add = 1;
52826846 665 continue;
e0d19036 666 case O(MANAGE,'r'): /* remove a drive */
52826846
NB
667 devmode = 'r';
668 continue;
e0d19036 669 case O(MANAGE,'f'): /* set faulty */
52826846
NB
670 devmode = 'f';
671 continue;
e0d19036
NB
672 case O(MANAGE,'R'):
673 case O(ASSEMBLE,'R'):
674 case O(BUILD,'R'):
675 case O(CREATE,'R'): /* Run the array */
52826846
NB
676 if (runstop < 0) {
677 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
678 exit(2);
679 }
680 runstop = 1;
681 continue;
e0d19036 682 case O(MANAGE,'S'):
52826846
NB
683 if (runstop > 0) {
684 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
685 exit(2);
686 }
687 runstop = -1;
688 continue;
689
e0d19036 690 case O(MANAGE,'o'):
52826846
NB
691 if (readonly < 0) {
692 fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
693 exit(2);
694 }
695 readonly = 1;
696 continue;
e0d19036 697 case O(MANAGE,'w'):
52826846 698 if (readonly > 0) {
e0d19036 699 fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n");
52826846
NB
700 exit(2);
701 }
702 readonly = -1;
703 continue;
e0d19036
NB
704
705 case O(MISC,'Q'):
706 case O(MISC,'D'):
707 case O(MISC,'E'):
708 case O(MISC,'K'):
709 case O(MISC,'R'):
710 case O(MISC,'S'):
c82f047c 711 case O(MISC,'X'):
e0d19036
NB
712 case O(MISC,'o'):
713 case O(MISC,'w'):
714 if (devmode && devmode != opt &&
715 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
716 fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n",
717 devmode =='E'?opt:devmode);
718 exit(2);
719 }
720 devmode = opt;
721 continue;
feb716e9
NB
722 case O(MISC,'t'):
723 test = 1;
724 continue;
e0d19036 725
bd526cee
NB
726 case O(MISC, 22):
727 if (devmode != 'E') {
728 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
729 exit(2);
730 }
731 SparcAdjust = 1;
732 continue;
c82f047c
NB
733
734 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
735 if (!optarg) {
736 fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n");
737 exit(2);
738 }
55935d51
NB
739 if (strcmp(optarg, "internal")==0) {
740 fprintf(stderr, Name ": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
741 continue;
742 }
c82f047c
NB
743 bitmap_fd = open(optarg, O_RDWR);
744 if (!*optarg || bitmap_fd < 0) {
745 fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno));
746 exit(2);
747 }
748 ident.bitmap_fd = bitmap_fd; /* for Assemble */
749 continue;
f5e166fe 750
06b0d786
NB
751 case O(ASSEMBLE, 7):
752 case O(GROW, 7):
753 /* Specify a file into which grow might place a backup,
754 * or from which assemble might recover a backup
755 */
756 if (backup_file) {
757 fprintf(stderr, Name ": backup file already specified, rejecting %s\n", optarg);
758 exit(2);
759 }
760 backup_file = optarg;
761 continue;
762
f5e166fe 763 case O(GROW,'b'):
c82f047c
NB
764 case O(BUILD,'b'):
765 case O(CREATE,'b'): /* here we create the bitmap */
1e0d770c
NB
766 if (strcmp(optarg, "internal")== 0 ||
767 strcmp(optarg, "none")== 0 ||
768 strchr(optarg, '/') != NULL) {
769 bitmap_file = optarg;
770 continue;
771 }
772 /* probable typo */
773 fprintf(stderr, Name ": bitmap file must contain a '/', or be 'internal', or 'none'\n");
774 exit(2);
c82f047c 775
f5e166fe 776 case O(GROW,4):
c82f047c
NB
777 case O(BUILD,4):
778 case O(CREATE,4): /* bitmap chunksize */
779 bitmap_chunk = strtol(optarg, &c, 10);
780 if (!optarg[0] || *c || bitmap_chunk < 0 ||
781 bitmap_chunk & (bitmap_chunk - 1)) {
782 fprintf(stderr, Name ": invalid bitmap chunksize: %s\n",
783 optarg);
784 exit(2);
785 }
786 /* convert K to B, chunk of 0K means 512B */
787 bitmap_chunk = bitmap_chunk ? bitmap_chunk * 1024 : 512;
788 continue;
dfd4d8ee
NB
789
790 case O(BUILD, 5):
791 case O(CREATE, 5): /* write-behind mode */
792 write_behind = DEFAULT_MAX_WRITE_BEHIND;
793 if (optarg) {
794 write_behind = strtol(optarg, &c, 10);
795 if (write_behind < 0 || *c ||
796 write_behind > 16383) {
797 fprintf(stderr, Name ": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
798 exit(2);
799 }
800 }
801 continue;
52826846
NB
802 }
803 /* We have now processed all the valid options. Anything else is
804 * an error
805 */
06b0d786
NB
806 if (option_index > 0)
807 fprintf(stderr, Name ":option --%s not valid in %s mode\n",
808 long_options[option_index].name,
809 map_num(modes, mode));
810 else
811 fprintf(stderr, Name ": option -%c not valid in %s mode\n",
812 opt, map_num(modes, mode));
64c4757e 813 exit(2);
52826846
NB
814
815 }
816
3d4064cc
NB
817 if (print_help) {
818 char *help_text = Help;
819 if (print_help == 2)
820 help_text = OptionHelp;
821 else
822 switch (mode) {
823 case ASSEMBLE : help_text = Help_assemble; break;
824 case BUILD : help_text = Help_build; break;
825 case CREATE : help_text = Help_create; break;
826 case MANAGE : help_text = Help_manage; break;
827 case MISC : help_text = Help_misc; break;
828 case MONITOR : help_text = Help_monitor; break;
829 case GROW : help_text = Help_grow; break;
830 }
831 fputs(help_text,stderr);
832 exit(0);
833 }
834
0320ea45
NB
835 if (!mode && devs_found) {
836 mode = MISC;
837 devmode = 'Q';
838 if (devlist->disposition == 0)
839 devlist->disposition = devmode;
840 }
52826846
NB
841 if (!mode) {
842 fputs(Usage, stderr);
64c4757e 843 exit(2);
64c4757e 844 }
52826846
NB
845 /* Ok, got the option parsing out of the way
846 * hopefully it's mostly right but there might be some stuff
847 * missing
848 *
e0d19036 849 * That is mosty checked in the per-mode stuff but...
52826846
NB
850 *
851 * For @,B,C and A without -s, the first device listed must be an md device
852 * we check that here and open it.
64c4757e 853 */
52826846 854
dd0781e5
NB
855 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
856 (mode == ASSEMBLE && ! scan)) {
52826846
NB
857 if (devs_found < 1) {
858 fprintf(stderr, Name ": an md device must be given in this mode\n");
859 exit(2);
860 }
dd0781e5
NB
861 if ((int)ident.super_minor == -2 && autof) {
862 fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
863 exit(2);
864 }
865 mdfd = open_mddev(devlist->devname, autof);
52826846
NB
866 if (mdfd < 0)
867 exit(1);
98c6faba 868 if ((int)ident.super_minor == -2) {
d013a55e
NB
869 struct stat stb;
870 fstat(mdfd, &stb);
0df46c2a 871 ident.super_minor = minor(stb.st_rdev);
d013a55e 872 }
64c4757e 873 }
52826846 874
e4c4352e
NB
875 if (raiddisks) {
876 if (raiddisks > max_disks) {
6fbba4c9
NB
877 fprintf(stderr, Name ": invalid number of raid devices: %d\n",
878 raiddisks);
e4c4352e
NB
879 exit(2);
880 }
881 if (raiddisks == 1 && !force && level != -5) {
882 fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
883 " a mistake. If you really mean it you will need to specify --force before\n"
884 " setting the number of drives.\n");
885 exit(2);
886 }
887 }
888 if (sparedisks) {
889 if ( sparedisks > max_disks - raiddisks) {
6fbba4c9
NB
890 fprintf(stderr, Name ": invalid number of spare-devices: %d\n",
891 sparedisks);
e4c4352e
NB
892 exit(2);
893 }
894 }
c82f047c 895
52826846
NB
896 rv = 0;
897 switch(mode) {
e0d19036 898 case MANAGE:
52826846
NB
899 /* readonly, add/remove, readwrite, runstop */
900 if (readonly>0)
cd29a5c8 901 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 902 if (!rv && devs_found>1)
cd29a5c8 903 rv = Manage_subdevs(devlist->devname, mdfd,
dab6685f 904 devlist->next, verbose-quiet);
52826846 905 if (!rv && readonly < 0)
cd29a5c8 906 rv = Manage_ro(devlist->devname, mdfd, readonly);
52826846 907 if (!rv && runstop)
91f068bf 908 rv = Manage_runstop(devlist->devname, mdfd, runstop, 0);
52826846 909 break;
e0d19036 910 case ASSEMBLE:
d013a55e 911 if (devs_found == 1 && ident.uuid_set == 0 &&
947fd4dd 912 ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) {
d013a55e
NB
913 /* Only a device has been given, so get details from config file */
914 mddev_ident_t array_ident = conf_get_ident(configfile, devlist->devname);
b5e64645
NB
915 if (array_ident == NULL) {
916 fprintf(stderr, Name ": %s not identified in config file.\n",
917 devlist->devname);
d013a55e 918 rv |= 1;
b5e64645 919 } else {
1337546d
NB
920 mdfd = open_mddev(devlist->devname,
921 array_ident->autof ? array_ident->autof : autof);
b5e64645 922 if (mdfd < 0)
d013a55e 923 rv |= 1;
b5e64645 924 else {
f9ce90ba 925 rv |= Assemble(ss, devlist->devname, mdfd, array_ident, configfile,
06b0d786 926 NULL, backup_file,
dab6685f 927 readonly, runstop, update, verbose-quiet, force);
b5e64645
NB
928 close(mdfd);
929 }
d013a55e
NB
930 }
931 } else if (!scan)
f9ce90ba 932 rv = Assemble(ss, devlist->devname, mdfd, &ident, configfile,
06b0d786 933 devlist->next, backup_file,
dab6685f 934 readonly, runstop, update, verbose-quiet, force);
5787fa49
NB
935 else if (devs_found>0) {
936 if (update && devs_found > 1) {
937 fprintf(stderr, Name ": can only update a single array at a time\n");
938 exit(1);
939 }
06b0d786
NB
940 if (backup_file && devs_found > 1) {
941 fprintf(stderr, Name ": can only assemble a single array when providing a backup file.\n");
942 exit(1);
943 }
cd29a5c8
NB
944 for (dv = devlist ; dv ; dv=dv->next) {
945 mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname);
52826846
NB
946 if (array_ident == NULL) {
947 fprintf(stderr, Name ": %s not identified in config file.\n",
cd29a5c8 948 dv->devname);
52826846
NB
949 rv |= 1;
950 continue;
951 }
1337546d
NB
952 mdfd = open_mddev(dv->devname,
953 array_ident->autof ?array_ident->autof : autof);
b5e64645
NB
954 if (mdfd < 0) {
955 rv |= 1;
956 continue;
957 }
f9ce90ba 958 rv |= Assemble(ss, dv->devname, mdfd, array_ident, configfile,
06b0d786 959 NULL, backup_file,
dab6685f 960 readonly, runstop, update, verbose-quiet, force);
b5e64645 961 close(mdfd);
52826846 962 }
5787fa49 963 } else {
52826846
NB
964 mddev_ident_t array_list = conf_get_ident(configfile, NULL);
965 if (!array_list) {
966 fprintf(stderr, Name ": No arrays found in config file\n");
967 rv = 1;
968 } else
06b0d786
NB
969 if (update) {
970 fprintf(stderr, Name ": --update not meaningful with a --scan assembly.\n");
971 exit(1);
972 }
973 if (backup_file) {
974 fprintf(stderr, Name ": --backup_file not meaningful with a --scan assembly.\n");
975 exit(1);
976 }
52826846 977 for (; array_list; array_list = array_list->next) {
cd29a5c8 978 mdu_array_info_t array;
1337546d
NB
979 mdfd = open_mddev(array_list->devname,
980 array_list->autof ? array_list->autof : autof);
52826846
NB
981 if (mdfd < 0) {
982 rv |= 1;
983 continue;
984 }
cd29a5c8
NB
985 if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
986 /* already assembled, skip */
43fc1676
NB
987 ;
988 else
f9ce90ba 989 rv |= Assemble(ss, array_list->devname, mdfd,
43fc1676 990 array_list, configfile,
06b0d786 991 NULL, NULL,
dab6685f 992 readonly, runstop, NULL, verbose-quiet, force);
43fc1676 993 close(mdfd);
52826846
NB
994 }
995 }
996 break;
e0d19036 997 case BUILD:
c82f047c 998 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
dfd4d8ee
NB
999 if (write_behind && !bitmap_file) {
1000 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1001 rv = 1;
1002 break;
1003 }
1004
c82f047c 1005 if (bitmap_file) {
55935d51
NB
1006 if (strcmp(bitmap_file, "internal")==0) {
1007 fprintf(stderr, Name ": 'internal' bitmaps not supported with --build\n");
1008 rv |= 1;
1009 break;
1010 }
c82f047c
NB
1011 }
1012 rv = Build(devlist->devname, mdfd, chunk, level, layout,
1013 raiddisks, devlist->next, assume_clean,
dab6685f 1014 bitmap_file, bitmap_chunk, write_behind, delay, verbose-quiet);
52826846 1015 break;
e0d19036 1016 case CREATE:
c82f047c 1017 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
dfd4d8ee
NB
1018 if (write_behind && !bitmap_file) {
1019 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1020 rv = 1;
1021 break;
1022 }
f9ce90ba
NB
1023
1024 rv = Create(ss, devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size,
947fd4dd 1025 raiddisks, sparedisks, ident.name,
47d79ef8 1026 devs_found-1, devlist->next, runstop, verbose-quiet, force, assume_clean,
dfd4d8ee 1027 bitmap_file, bitmap_chunk, write_behind, delay);
52826846 1028 break;
e0d19036
NB
1029 case MISC:
1030
1031 if (devmode == 'E') {
1032 if (devlist == NULL && !scan) {
1033 fprintf(stderr, Name ": No devices to examine\n");
1034 exit(2);
1035 }
1036 if (devlist == NULL)
1037 devlist = conf_get_devs(configfile);
1038 if (devlist == NULL) {
c913b90e 1039 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
e0d19036
NB
1040 exit(1);
1041 }
22892d56 1042 rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief, scan, SparcAdjust, ss);
e0d19036
NB
1043 } else {
1044 if (devlist == NULL) {
91f068bf
NB
1045 if (devmode=='D' && scan) {
1046 /* apply --detail to all devices in /proc/mdstat */
22a88995 1047 struct mdstat_ent *ms = mdstat_read(0, 1);
e0d19036
NB
1048 struct mdstat_ent *e;
1049 for (e=ms ; e ; e=e->next) {
1050 char *name = get_md_name(e->devnum);
1051
1052 if (!name) {
1053 fprintf(stderr, Name ": cannot find device file for %s\n",
1054 e->dev);
1055 continue;
1056 }
22892d56 1057 rv |= Detail(name, verbose>1?0:verbose+1, test);
91f068bf
NB
1058 put_md_name(name);
1059 }
1060 } else if (devmode == 'S' && scan) {
1061 /* apply --stop to all devices in /proc/mdstat */
1062 /* Due to possible stacking of devices, repeat until
1063 * nothing more can be stopped
1064 */
1065 int progress=1, err;
1066 int last = 0;
1067 do {
22a88995 1068 struct mdstat_ent *ms = mdstat_read(0, 0);
91f068bf
NB
1069 struct mdstat_ent *e;
1070
1071 if (!progress) last = 1;
1072 progress = 0; err = 0;
1073 for (e=ms ; e ; e=e->next) {
1074 char *name = get_md_name(e->devnum);
1075
1076 if (!name) {
1077 fprintf(stderr, Name ": cannot find device file for %s\n",
1078 e->dev);
1079 continue;
1080 }
dd0781e5 1081 mdfd = open_mddev(name, 0);
43fc1676 1082 if (mdfd >= 0) {
91f068bf
NB
1083 if (Manage_runstop(name, mdfd, -1, !last))
1084 err = 1;
1085 else
1086 progress = 1;
43fc1676
NB
1087 close(mdfd);
1088 }
91f068bf
NB
1089
1090 put_md_name(name);
e0d19036 1091 }
91f068bf 1092 } while (!last && err);
f9c25f1d 1093 if (err) rv |= 1;
91f068bf 1094 } else {
e0d19036
NB
1095 fprintf(stderr, Name ": No devices given.\n");
1096 exit(2);
1097 }
1098 }
1099 for (dv=devlist ; dv; dv=dv->next) {
1100 switch(dv->disposition) {
1101 case 'D':
22892d56 1102 rv |= Detail(dv->devname, brief?1+verbose:0, test); continue;
e0d19036 1103 case 'K': /* Zero superblock */
6409687b 1104 rv |= Kill(dv->devname, force, quiet); continue;
e0d19036
NB
1105 case 'Q':
1106 rv |= Query(dv->devname); continue;
c82f047c 1107 case 'X':
55935d51 1108 rv |= ExamineBitmap(dv->devname, brief, ss); continue;
e0d19036 1109 }
dd0781e5 1110 mdfd = open_mddev(dv->devname, 0);
43fc1676 1111 if (mdfd>=0) {
e0d19036
NB
1112 switch(dv->disposition) {
1113 case 'R':
91f068bf 1114 rv |= Manage_runstop(dv->devname, mdfd, 1, 0); break;
e0d19036 1115 case 'S':
91f068bf 1116 rv |= Manage_runstop(dv->devname, mdfd, -1, 0); break;
e0d19036
NB
1117 case 'o':
1118 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1119 case 'w':
1120 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1121 }
43fc1676 1122 close(mdfd);
7f48e210
NB
1123 } else
1124 rv |= 1;
e0d19036 1125 }
cd29a5c8 1126 }
9a9dab36 1127 break;
e0d19036 1128 case MONITOR:
e0d19036
NB
1129 if (!devlist && !scan) {
1130 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
1131 rv = 1;
1132 break;
1133 }
b5e64645
NB
1134 if (pidfile && !daemonise) {
1135 fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
1136 rv = 1;
1137 break;
1138 }
d013a55e 1139 rv= Monitor(devlist, mailaddr, program,
773135f5
NB
1140 delay?delay:60, daemonise, scan, oneshot,
1141 dosyslog, configfile, test, pidfile);
9a9dab36 1142 break;
dd0781e5
NB
1143
1144 case GROW:
1145 if (devs_found > 1) {
e5329c37
NB
1146
1147 /* must be '-a'. */
1148 if (size >= 0 || raiddisks) {
1149 fprintf(stderr, Name ": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1150 rv = 1;
1151 break;
1152 }
1153 for (dv=devlist->next; dv ; dv=dv->next) {
1154 rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
1155 if (rv)
1156 break;
1157 }
f5e166fe
NB
1158 } else if ((size >= 0) + (raiddisks != 0) + (layout != UnSet) + (bitmap_file != NULL)> 1) {
1159 fprintf(stderr, Name ": can change at most one of size, raiddisks, bitmap, and layout\n");
dd0781e5
NB
1160 rv = 1;
1161 break;
b5e64645
NB
1162 } else if (layout != UnSet)
1163 rv = Manage_reconfig(devlist->devname, mdfd, layout);
1164 else if (size >= 0 || raiddisks)
06b0d786 1165 rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file,
e86c9dd6 1166 size, level, layout, chunk, raiddisks);
f5e166fe
NB
1167 else if (bitmap_file) {
1168 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1169 rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file,
8fac0577 1170 bitmap_chunk, delay, write_behind, force);
f5e166fe 1171 } else
b5e64645 1172 fprintf(stderr, Name ": no changes to --grow\n");
dd0781e5 1173 break;
64c4757e 1174 }
52826846 1175 exit(rv);
64c4757e 1176}