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