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