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