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