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