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