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