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