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