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