]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdadm.c
Hold the map lock while performing Assemble to avoid races with udev
[thirdparty/mdadm.git] / mdadm.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 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@suse.de>
23 *
24 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
25 * Paul Clements, SteelEye Technology, Inc.
26 */
27
28 #include "mdadm.h"
29 #include "md_p.h"
30 #include <ctype.h>
31
32
33 int main(int argc, char *argv[])
34 {
35 int mode = 0;
36 int opt;
37 int option_index;
38 char *c;
39 int rv;
40 int i;
41
42 int chunk = 0;
43 long long size = -1;
44 long long array_size = -1;
45 int level = UnSet;
46 int layout = UnSet;
47 char *layout_str = NULL;
48 int raiddisks = 0;
49 int sparedisks = 0;
50 struct mddev_ident ident;
51 char *configfile = NULL;
52 char *cp;
53 char *update = NULL;
54 int scan = 0;
55 int devmode = 0;
56 int runstop = 0;
57 int readonly = 0;
58 int write_behind = 0;
59 int bitmap_fd = -1;
60 char *bitmap_file = NULL;
61 char *backup_file = NULL;
62 int invalid_backup = 0;
63 int bitmap_chunk = UnSet;
64 int SparcAdjust = 0;
65 struct mddev_dev *devlist = NULL;
66 struct mddev_dev **devlistend = & devlist;
67 struct mddev_dev *dv;
68 int devs_found = 0;
69 int verbose = 0;
70 int quiet = 0;
71 int brief = 0;
72 int force = 0;
73 int test = 0;
74 int export = 0;
75 int assume_clean = 0;
76 char *symlinks = NULL;
77 int grow_continue = 0;
78 /* autof indicates whether and how to create device node.
79 * bottom 3 bits are style. Rest (when shifted) are number of parts
80 * 0 - unset
81 * 1 - don't create (no)
82 * 2 - if is_standard, then create (yes)
83 * 3 - create as 'md' - reject is_standard mdp (md)
84 * 4 - create as 'mdp' - reject is_standard md (mdp)
85 * 5 - default to md if not is_standard (md in config file)
86 * 6 - default to mdp if not is_standard (part, or mdp in config file)
87 */
88 int autof = 0;
89
90 char *homehost = NULL;
91 char sys_hostname[256];
92 int require_homehost = 1;
93 char *mailaddr = NULL;
94 char *program = NULL;
95 int increments = 20;
96 int delay = 0;
97 int daemonise = 0;
98 char *pidfile = NULL;
99 int oneshot = 0;
100 int spare_sharing = 1;
101 struct supertype *ss = NULL;
102 int writemostly = 0;
103 int re_add = 0;
104 char *shortopt = short_options;
105 int dosyslog = 0;
106 int rebuild_map = 0;
107 char *subarray = NULL;
108 char *remove_path = NULL;
109 char *udev_filename = NULL;
110
111 int print_help = 0;
112 FILE *outf;
113
114 int mdfd = -1;
115
116 int freeze_reshape = 0;
117
118 srandom(time(0) ^ getpid());
119
120 ident.uuid_set=0;
121 ident.level = UnSet;
122 ident.raid_disks = UnSet;
123 ident.super_minor= UnSet;
124 ident.devices=0;
125 ident.spare_group = NULL;
126 ident.autof = 0;
127 ident.st = NULL;
128 ident.bitmap_fd = -1;
129 ident.bitmap_file = NULL;
130 ident.name[0] = 0;
131 ident.container = NULL;
132 ident.member = NULL;
133
134 while ((option_index = -1) ,
135 (opt=getopt_long(argc, argv,
136 shortopt, long_options,
137 &option_index)) != -1) {
138 int newmode = mode;
139 /* firstly, some mode-independent options */
140 switch(opt) {
141 case HelpOptions:
142 print_help = 2;
143 continue;
144 case 'h':
145 print_help = 1;
146 continue;
147
148 case 'V':
149 fputs(Version, stderr);
150 exit(0);
151
152 case 'v': verbose++;
153 continue;
154
155 case 'q': quiet++;
156 continue;
157
158 case 'b':
159 if (mode == ASSEMBLE || mode == BUILD || mode == CREATE
160 || mode == GROW || mode == INCREMENTAL
161 || mode == MANAGE)
162 break; /* b means bitmap */
163 case Brief:
164 brief = 1;
165 continue;
166
167 case 'Y': export++;
168 continue;
169
170 case HomeHost:
171 if (strcasecmp(optarg, "<ignore>") == 0)
172 require_homehost = 0;
173 else
174 homehost = optarg;
175 continue;
176
177 case ':':
178 case '?':
179 fputs(Usage, stderr);
180 exit(2);
181 }
182 /* second, figure out the mode.
183 * Some options force the mode. Others
184 * set the mode if it isn't already
185 */
186
187 switch(opt) {
188 case ManageOpt:
189 newmode = MANAGE;
190 shortopt = short_bitmap_options;
191 break;
192 case 'a':
193 case Add:
194 case 'r':
195 case Remove:
196 case 'f':
197 case Fail:
198 case ReAdd: /* re-add */
199 if (!mode) {
200 newmode = MANAGE;
201 shortopt = short_bitmap_options;
202 }
203 break;
204
205 case 'A': newmode = ASSEMBLE; shortopt = short_bitmap_auto_options; break;
206 case 'B': newmode = BUILD; shortopt = short_bitmap_auto_options; break;
207 case 'C': newmode = CREATE; shortopt = short_bitmap_auto_options; break;
208 case 'F': newmode = MONITOR;break;
209 case 'G': newmode = GROW;
210 shortopt = short_bitmap_options;
211 break;
212 case 'I': newmode = INCREMENTAL;
213 shortopt = short_bitmap_auto_options; break;
214 case AutoDetect:
215 newmode = AUTODETECT; break;
216
217 case MiscOpt:
218 case 'D':
219 case 'E':
220 case 'X':
221 case 'Q': newmode = MISC; break;
222 case 'R':
223 case 'S':
224 case 'o':
225 case 'w':
226 case 'W':
227 case WaitOpt:
228 case Waitclean:
229 case DetailPlatform:
230 case KillSubarray:
231 case UpdateSubarray:
232 if (opt == KillSubarray || opt == UpdateSubarray) {
233 if (subarray) {
234 fprintf(stderr, Name ": subarray can only"
235 " be specified once\n");
236 exit(2);
237 }
238 subarray = optarg;
239 }
240 case UdevRules:
241 case 'K': if (!mode) newmode = MISC; break;
242 case NoSharing: newmode = MONITOR; break;
243 }
244 if (mode && newmode == mode) {
245 /* everybody happy ! */
246 } else if (mode && newmode != mode) {
247 /* not allowed.. */
248 fprintf(stderr, Name ": ");
249 if (option_index >= 0)
250 fprintf(stderr, "--%s", long_options[option_index].name);
251 else
252 fprintf(stderr, "-%c", opt);
253 fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
254 map_num(modes, newmode),
255 map_num(modes, mode));
256 exit(2);
257 } else if (!mode && newmode) {
258 mode = newmode;
259 if (mode == MISC && devs_found) {
260 fprintf(stderr, Name ": No action given for %s in --misc mode\n",
261 devlist->devname);
262 fprintf(stderr," Action options must come before device names\n");
263 exit(2);
264 }
265 } else {
266 /* special case of -c --help */
267 if ((opt == 'c' || opt == ConfigFile) &&
268 ( strncmp(optarg, "--h", 3)==0 ||
269 strncmp(optarg, "-h", 2)==0)) {
270 fputs(Help_config, stdout);
271 exit(0);
272 }
273
274 /* If first option is a device, don't force the mode yet */
275 if (opt == 1) {
276 if (devs_found == 0) {
277 dv = malloc(sizeof(*dv));
278 if (dv == NULL) {
279 fprintf(stderr, Name ": malloc failed\n");
280 exit(3);
281 }
282 dv->devname = optarg;
283 dv->disposition = devmode;
284 dv->writemostly = writemostly;
285 dv->re_add = re_add;
286 dv->used = 0;
287 dv->next = NULL;
288 *devlistend = dv;
289 devlistend = &dv->next;
290
291 devs_found++;
292 continue;
293 }
294 /* No mode yet, and this is the second device ... */
295 fprintf(stderr, Name ": An option must be given to set the mode before a second device\n"
296 " (%s) is listed\n", optarg);
297 exit(2);
298 }
299 if (option_index >= 0)
300 fprintf(stderr, Name ": --%s", long_options[option_index].name);
301 else
302 fprintf(stderr, Name ": -%c", opt);
303 fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
304 exit(2);
305 }
306
307 /* if we just set the mode, then done */
308 switch(opt) {
309 case ManageOpt:
310 case MiscOpt:
311 case 'A':
312 case 'B':
313 case 'C':
314 case 'F':
315 case 'G':
316 case 'I':
317 case AutoDetect:
318 continue;
319 }
320 if (opt == 1) {
321 /* an undecorated option - must be a device name.
322 */
323 if (devs_found > 0 && mode == MANAGE && !devmode) {
324 fprintf(stderr, Name ": Must give one of -a/-r/-f"
325 " for subsequent devices at %s\n", optarg);
326 exit(2);
327 }
328 if (devs_found > 0 && mode == GROW && !devmode) {
329 fprintf(stderr, Name ": Must give -a/--add for"
330 " devices to add: %s\n", optarg);
331 exit(2);
332 }
333 dv = malloc(sizeof(*dv));
334 if (dv == NULL) {
335 fprintf(stderr, Name ": malloc failed\n");
336 exit(3);
337 }
338 dv->devname = optarg;
339 dv->disposition = devmode;
340 dv->writemostly = writemostly;
341 dv->re_add = re_add;
342 dv->used = 0;
343 dv->next = NULL;
344 *devlistend = dv;
345 devlistend = &dv->next;
346
347 devs_found++;
348 continue;
349 }
350
351 /* We've got a mode, and opt is now something else which
352 * could depend on the mode */
353 #define O(a,b) ((a<<16)|b)
354 switch (O(mode,opt)) {
355 case O(GROW,'c'):
356 case O(GROW,ChunkSize):
357 case O(CREATE,'c'):
358 case O(CREATE,ChunkSize):
359 case O(BUILD,'c'): /* chunk or rounding */
360 case O(BUILD,ChunkSize): /* chunk or rounding */
361 if (chunk) {
362 fprintf(stderr, Name ": chunk/rounding may only be specified once. "
363 "Second value is %s.\n", optarg);
364 exit(2);
365 }
366 chunk = parse_size(optarg);
367 if (chunk < 8 || (chunk&1)) {
368 fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
369 optarg);
370 exit(2);
371 }
372 /* Convert sectors to K */
373 chunk /= 2;
374 continue;
375
376 case O(INCREMENTAL, 'e'):
377 case O(CREATE,'e'):
378 case O(ASSEMBLE,'e'):
379 case O(MISC,'e'): /* set metadata (superblock) information */
380 if (ss) {
381 fprintf(stderr, Name ": metadata information already given\n");
382 exit(2);
383 }
384 for(i=0; !ss && superlist[i]; i++)
385 ss = superlist[i]->match_metadata_desc(optarg);
386
387 if (!ss) {
388 fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg);
389 exit(2);
390 }
391 continue;
392
393 case O(MANAGE,'W'):
394 case O(MANAGE,WriteMostly):
395 case O(BUILD,'W'):
396 case O(BUILD,WriteMostly):
397 case O(CREATE,'W'):
398 case O(CREATE,WriteMostly):
399 /* set write-mostly for following devices */
400 writemostly = 1;
401 continue;
402
403 case O(MANAGE,'w'):
404 /* clear write-mostly for following devices */
405 writemostly = 2;
406 continue;
407
408
409 case O(GROW,'z'):
410 case O(CREATE,'z'):
411 case O(BUILD,'z'): /* size */
412 if (size >= 0) {
413 fprintf(stderr, Name ": size may only be specified once. "
414 "Second value is %s.\n", optarg);
415 exit(2);
416 }
417 if (strcmp(optarg, "max")==0)
418 size = 0;
419 else {
420 size = parse_size(optarg);
421 if (size < 8) {
422 fprintf(stderr, Name ": invalid size: %s\n",
423 optarg);
424 exit(2);
425 }
426 /* convert sectors to K */
427 size /= 2;
428 }
429 continue;
430
431 case O(GROW,'Z'): /* array size */
432 if (array_size >= 0) {
433 fprintf(stderr, Name ": array-size may only be specified once. "
434 "Second value is %s.\n", optarg);
435 exit(2);
436 }
437 if (strcmp(optarg, "max") == 0)
438 array_size = 0;
439 else {
440 array_size = parse_size(optarg);
441 if (array_size <= 0) {
442 fprintf(stderr, Name ": invalid array size: %s\n",
443 optarg);
444 exit(2);
445 }
446 }
447 continue;
448
449 case O(GROW,'l'):
450 case O(CREATE,'l'):
451 case O(BUILD,'l'): /* set raid level*/
452 if (level != UnSet) {
453 fprintf(stderr, Name ": raid level may only be set once. "
454 "Second value is %s.\n", optarg);
455 exit(2);
456 }
457 level = map_name(pers, optarg);
458 if (level == UnSet) {
459 fprintf(stderr, Name ": invalid raid level: %s\n",
460 optarg);
461 exit(2);
462 }
463 if (level != 0 && level != LEVEL_LINEAR && level != 1 &&
464 level != LEVEL_MULTIPATH && level != LEVEL_FAULTY &&
465 level != 10 &&
466 mode == BUILD) {
467 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
468 optarg);
469 exit(2);
470 }
471 if (sparedisks > 0 && level < 1 && level >= -1) {
472 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
473 optarg);
474 exit(2);
475 }
476 ident.level = level;
477 continue;
478
479 case O(GROW, 'p'): /* new layout */
480 case O(GROW, Layout):
481 if (layout_str) {
482 fprintf(stderr,Name ": layout may only be sent once. "
483 "Second value was %s\n", optarg);
484 exit(2);
485 }
486 layout_str = optarg;
487 /* 'Grow' will parse the value */
488 continue;
489
490 case O(CREATE,'p'): /* raid5 layout */
491 case O(CREATE,Layout):
492 case O(BUILD,'p'): /* faulty layout */
493 case O(BUILD,Layout):
494 if (layout != UnSet) {
495 fprintf(stderr,Name ": layout may only be sent once. "
496 "Second value was %s\n", optarg);
497 exit(2);
498 }
499 switch(level) {
500 default:
501 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
502 map_num(pers, level));
503 exit(2);
504 case UnSet:
505 fprintf(stderr, Name ": raid level must be given before layout.\n");
506 exit(2);
507
508 case 5:
509 layout = map_name(r5layout, optarg);
510 if (layout==UnSet) {
511 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
512 optarg);
513 exit(2);
514 }
515 break;
516 case 6:
517 layout = map_name(r6layout, optarg);
518 if (layout==UnSet) {
519 fprintf(stderr, Name ": layout %s not understood for raid6.\n",
520 optarg);
521 exit(2);
522 }
523 break;
524
525 case 10:
526 layout = parse_layout_10(optarg);
527 if (layout < 0) {
528 fprintf(stderr, Name ": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg);
529 exit(2);
530 }
531 break;
532 case LEVEL_FAULTY:
533 /* Faulty
534 * modeNNN
535 */
536 layout = parse_layout_faulty(optarg);
537 if (layout == -1) {
538 fprintf(stderr, Name ": layout %s not understood for faulty.\n",
539 optarg);
540 exit(2);
541 }
542 break;
543 }
544 continue;
545
546 case O(CREATE,AssumeClean):
547 case O(BUILD,AssumeClean): /* assume clean */
548 case O(GROW,AssumeClean):
549 assume_clean = 1;
550 continue;
551
552 case O(GROW,'n'):
553 case O(CREATE,'n'):
554 case O(BUILD,'n'): /* number of raid disks */
555 if (raiddisks) {
556 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
557 raiddisks, optarg);
558 exit(2);
559 }
560 raiddisks = strtol(optarg, &c, 10);
561 if (!optarg[0] || *c || raiddisks<=0) {
562 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
563 optarg);
564 exit(2);
565 }
566 ident.raid_disks = raiddisks;
567 continue;
568
569 case O(CREATE,'x'): /* number of spare (eXtra) discs */
570 if (sparedisks) {
571 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
572 sparedisks, optarg);
573 exit(2);
574 }
575 if (level != UnSet && level <= 0 && level >= -1) {
576 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
577 level);
578 exit(2);
579 }
580 sparedisks = strtol(optarg, &c, 10);
581 if (!optarg[0] || *c || sparedisks < 0) {
582 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
583 optarg);
584 exit(2);
585 }
586 continue;
587
588 case O(CREATE,'a'):
589 case O(CREATE,Auto):
590 case O(BUILD,'a'):
591 case O(BUILD,Auto):
592 case O(INCREMENTAL,'a'):
593 case O(INCREMENTAL,Auto):
594 case O(ASSEMBLE,'a'):
595 case O(ASSEMBLE,Auto): /* auto-creation of device node */
596 autof = parse_auto(optarg, "--auto flag", 0);
597 continue;
598
599 case O(CREATE,Symlinks):
600 case O(BUILD,Symlinks):
601 case O(ASSEMBLE,Symlinks): /* auto creation of symlinks in /dev to /dev/md */
602 symlinks = optarg;
603 continue;
604
605 case O(BUILD,'f'): /* force honouring '-n 1' */
606 case O(BUILD,Force): /* force honouring '-n 1' */
607 case O(GROW,'f'): /* ditto */
608 case O(GROW,Force): /* ditto */
609 case O(CREATE,'f'): /* force honouring of device list */
610 case O(CREATE,Force): /* force honouring of device list */
611 case O(ASSEMBLE,'f'): /* force assembly */
612 case O(ASSEMBLE,Force): /* force assembly */
613 case O(MISC,'f'): /* force zero */
614 case O(MISC,Force): /* force zero */
615 case O(MANAGE,Force): /* add device which is too large */
616 force=1;
617 continue;
618 /* now for the Assemble options */
619 case O(ASSEMBLE, FreezeReshape): /* Freeze reshape during
620 * initrd phase */
621 case O(INCREMENTAL, FreezeReshape):
622 freeze_reshape = 1;
623 continue;
624 case O(CREATE,'u'): /* uuid of array */
625 case O(ASSEMBLE,'u'): /* uuid of array */
626 if (ident.uuid_set) {
627 fprintf(stderr, Name ": uuid cannot be set twice. "
628 "Second value %s.\n", optarg);
629 exit(2);
630 }
631 if (parse_uuid(optarg, ident.uuid))
632 ident.uuid_set = 1;
633 else {
634 fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
635 exit(2);
636 }
637 continue;
638
639 case O(CREATE,'N'):
640 case O(ASSEMBLE,'N'):
641 case O(MISC,'N'):
642 if (ident.name[0]) {
643 fprintf(stderr, Name ": name cannot be set twice. "
644 "Second value %s.\n", optarg);
645 exit(2);
646 }
647 if (mode == MISC && !subarray) {
648 fprintf(stderr, Name ": -N/--name only valid with --update-subarray in misc mode\n");
649 exit(2);
650 }
651 if (strlen(optarg) > 32) {
652 fprintf(stderr, Name ": name '%s' is too long, 32 chars max.\n",
653 optarg);
654 exit(2);
655 }
656 strcpy(ident.name, optarg);
657 continue;
658
659 case O(ASSEMBLE,'m'): /* super-minor for array */
660 case O(ASSEMBLE,SuperMinor):
661 if (ident.super_minor != UnSet) {
662 fprintf(stderr, Name ": super-minor cannot be set twice. "
663 "Second value: %s.\n", optarg);
664 exit(2);
665 }
666 if (strcmp(optarg, "dev")==0)
667 ident.super_minor = -2;
668 else {
669 ident.super_minor = strtoul(optarg, &cp, 10);
670 if (!optarg[0] || *cp) {
671 fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
672 exit(2);
673 }
674 }
675 continue;
676
677 case O(ASSEMBLE,'U'): /* update the superblock */
678 case O(MISC,'U'):
679 if (update) {
680 fprintf(stderr, Name ": Can only update one aspect"
681 " of superblock, both %s and %s given.\n",
682 update, optarg);
683 exit(2);
684 }
685 if (mode == MISC && !subarray) {
686 fprintf(stderr, Name ": Only subarrays can be"
687 " updated in misc mode\n");
688 exit(2);
689 }
690 update = optarg;
691 if (strcmp(update, "sparc2.2")==0)
692 continue;
693 if (strcmp(update, "super-minor") == 0)
694 continue;
695 if (strcmp(update, "summaries")==0)
696 continue;
697 if (strcmp(update, "resync")==0)
698 continue;
699 if (strcmp(update, "uuid")==0)
700 continue;
701 if (strcmp(update, "name")==0)
702 continue;
703 if (strcmp(update, "homehost")==0)
704 continue;
705 if (strcmp(update, "devicesize")==0)
706 continue;
707 if (strcmp(update, "no-bitmap")==0)
708 continue;
709 if (strcmp(update, "byteorder")==0) {
710 if (ss) {
711 fprintf(stderr,
712 Name ": must not set metadata"
713 " type with --update=byteorder.\n");
714 exit(2);
715 }
716 for(i=0; !ss && superlist[i]; i++)
717 ss = superlist[i]->match_metadata_desc(
718 "0.swap");
719 if (!ss) {
720 fprintf(stderr, Name ": INTERNAL ERROR"
721 " cannot find 0.swap\n");
722 exit(2);
723 }
724
725 continue;
726 }
727 if (strcmp(update,"?") == 0 ||
728 strcmp(update, "help") == 0) {
729 outf = stdout;
730 fprintf(outf, Name ": ");
731 } else {
732 outf = stderr;
733 fprintf(outf,
734 Name ": '--update=%s' is invalid. ",
735 update);
736 }
737 fprintf(outf, "Valid --update options are:\n"
738 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
739 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
740 " 'no-bitmap'\n");
741 exit(outf == stdout ? 0 : 2);
742
743 case O(MANAGE,'U'):
744 /* update=devicesize is allowed with --re-add */
745 if (devmode != 'a' || re_add != 1) {
746 fprintf(stderr, Name "--update in Manage mode only"
747 " allowed with --re-add.\n");
748 exit(1);
749 }
750 if (update) {
751 fprintf(stderr, Name ": Can only update one aspect"
752 " of superblock, both %s and %s given.\n",
753 update, optarg);
754 exit(2);
755 }
756 update = optarg;
757 if (strcmp(update, "devicesize") != 0) {
758 fprintf(stderr, Name ": only 'devicesize' can be"
759 " updated with --re-add\n");
760 exit(2);
761 }
762 continue;
763
764 case O(INCREMENTAL,NoDegraded):
765 fprintf(stderr, Name ": --no-degraded is deprecated in Incremental mode\n");
766 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
767 runstop = -1; /* --stop isn't allowed for --assemble,
768 * so we overload slightly */
769 continue;
770
771 case O(ASSEMBLE,'c'):
772 case O(ASSEMBLE,ConfigFile):
773 case O(INCREMENTAL, 'c'):
774 case O(INCREMENTAL, ConfigFile):
775 case O(MISC, 'c'):
776 case O(MISC, ConfigFile):
777 case O(MONITOR,'c'):
778 case O(MONITOR,ConfigFile):
779 if (configfile) {
780 fprintf(stderr, Name ": configfile cannot be set twice. "
781 "Second value is %s.\n", optarg);
782 exit(2);
783 }
784 configfile = optarg;
785 set_conffile(configfile);
786 /* FIXME possibly check that config file exists. Even parse it */
787 continue;
788 case O(ASSEMBLE,'s'): /* scan */
789 case O(MISC,'s'):
790 case O(MONITOR,'s'):
791 case O(INCREMENTAL,'s'):
792 scan = 1;
793 continue;
794
795 case O(MONITOR,'m'): /* mail address */
796 case O(MONITOR,EMail):
797 if (mailaddr)
798 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
799 optarg);
800 else
801 mailaddr = optarg;
802 continue;
803
804 case O(MONITOR,'p'): /* alert program */
805 case O(MONITOR,ProgramOpt): /* alert program */
806 if (program)
807 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
808 optarg);
809 else
810 program = optarg;
811 continue;
812
813 case O(MONITOR,'r'): /* rebuild increments */
814 case O(MONITOR,Increment):
815 increments = atoi(optarg);
816 if (increments>99 || increments<1) {
817 fprintf(stderr, Name ": please specify positive integer between 1 and 99 as rebuild increments.\n");
818 exit(2);
819 }
820 continue;
821
822 case O(MONITOR,'d'): /* delay in seconds */
823 case O(GROW, 'd'):
824 case O(BUILD,'d'): /* delay for bitmap updates */
825 case O(CREATE,'d'):
826 if (delay)
827 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
828 optarg);
829 else {
830 delay = strtol(optarg, &c, 10);
831 if (!optarg[0] || *c || delay<1) {
832 fprintf(stderr, Name ": invalid delay: %s\n",
833 optarg);
834 exit(2);
835 }
836 }
837 continue;
838 case O(MONITOR,'f'): /* daemonise */
839 case O(MONITOR,Fork):
840 daemonise = 1;
841 continue;
842 case O(MONITOR,'i'): /* pid */
843 if (pidfile)
844 fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
845 optarg);
846 else
847 pidfile = optarg;
848 continue;
849 case O(MONITOR,'1'): /* oneshot */
850 oneshot = 1;
851 spare_sharing = 0;
852 continue;
853 case O(MONITOR,'t'): /* test */
854 test = 1;
855 continue;
856 case O(MONITOR,'y'): /* log messages to syslog */
857 openlog("mdadm", LOG_PID, SYSLOG_FACILITY);
858 dosyslog = 1;
859 continue;
860 case O(MONITOR, NoSharing):
861 spare_sharing = 0;
862 continue;
863 /* now the general management options. Some are applicable
864 * to other modes. None have arguments.
865 */
866 case O(GROW,'a'):
867 case O(GROW,Add):
868 case O(MANAGE,'a'):
869 case O(MANAGE,Add): /* add a drive */
870 devmode = 'a';
871 re_add = 0;
872 continue;
873 case O(MANAGE,ReAdd):
874 devmode = 'a';
875 re_add = 1;
876 continue;
877 case O(MANAGE,'r'): /* remove a drive */
878 case O(MANAGE,Remove):
879 devmode = 'r';
880 continue;
881 case O(MANAGE,'f'): /* set faulty */
882 case O(MANAGE,Fail):
883 case O(INCREMENTAL,'f'):
884 case O(INCREMENTAL,Remove):
885 case O(INCREMENTAL,Fail): /* r for incremental is taken, use f
886 * even though we will both fail and
887 * remove the device */
888 devmode = 'f';
889 continue;
890 case O(INCREMENTAL,'R'):
891 case O(MANAGE,'R'):
892 case O(ASSEMBLE,'R'):
893 case O(BUILD,'R'):
894 case O(CREATE,'R'): /* Run the array */
895 if (runstop < 0) {
896 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
897 exit(2);
898 }
899 runstop = 1;
900 continue;
901 case O(MANAGE,'S'):
902 if (runstop > 0) {
903 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
904 exit(2);
905 }
906 runstop = -1;
907 continue;
908 case O(MANAGE,'t'):
909 test = 1;
910 continue;
911
912 case O(MISC,'Q'):
913 case O(MISC,'D'):
914 case O(MISC,'E'):
915 case O(MISC,'K'):
916 case O(MISC,'R'):
917 case O(MISC,'S'):
918 case O(MISC,'X'):
919 case O(MISC,'o'):
920 case O(MISC,'w'):
921 case O(MISC,'W'):
922 case O(MISC, WaitOpt):
923 case O(MISC, Waitclean):
924 case O(MISC, DetailPlatform):
925 case O(MISC, KillSubarray):
926 case O(MISC, UpdateSubarray):
927 if (devmode && devmode != opt &&
928 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
929 fprintf(stderr, Name ": --examine/-E cannot be given with ");
930 if (devmode == 'E') {
931 if (option_index >= 0)
932 fprintf(stderr, "--%s\n",
933 long_options[option_index].name);
934 else
935 fprintf(stderr, "-%c\n", opt);
936 } else if (isalpha(devmode))
937 fprintf(stderr, "-%c\n", devmode);
938 else
939 fprintf(stderr, "previous option\n");
940 exit(2);
941 }
942 devmode = opt;
943 continue;
944 case O(MISC, UdevRules):
945 if (devmode && devmode != opt) {
946 fprintf(stderr, Name ": --udev-rules must"
947 " be the only option.\n");
948 } else {
949 if (udev_filename)
950 fprintf(stderr, Name ": only specify one udev "
951 "rule filename. %s ignored.\n",
952 optarg);
953 else
954 udev_filename = optarg;
955 }
956 devmode = opt;
957 continue;
958 case O(MISC,'t'):
959 test = 1;
960 continue;
961
962 case O(MISC, Sparc22):
963 if (devmode != 'E') {
964 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
965 exit(2);
966 }
967 SparcAdjust = 1;
968 continue;
969
970 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
971 case O(ASSEMBLE,Bitmap):
972 if (!optarg) {
973 fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n");
974 exit(2);
975 }
976 if (strcmp(optarg, "internal")==0) {
977 fprintf(stderr, Name ": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
978 continue;
979 }
980 bitmap_fd = open(optarg, O_RDWR);
981 if (!*optarg || bitmap_fd < 0) {
982 fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno));
983 exit(2);
984 }
985 ident.bitmap_fd = bitmap_fd; /* for Assemble */
986 continue;
987
988 case O(ASSEMBLE, BackupFile):
989 case O(GROW, BackupFile):
990 /* Specify a file into which grow might place a backup,
991 * or from which assemble might recover a backup
992 */
993 if (backup_file) {
994 fprintf(stderr, Name ": backup file already specified, rejecting %s\n", optarg);
995 exit(2);
996 }
997 backup_file = optarg;
998 continue;
999
1000 case O(GROW, Continue):
1001 /* Continue interrupted grow
1002 */
1003 grow_continue = 1;
1004 continue;
1005 case O(ASSEMBLE, InvalidBackup):
1006 /* Acknowledge that the backupfile is invalid, but ask
1007 * to continue anyway
1008 */
1009 invalid_backup = 1;
1010 continue;
1011
1012 case O(BUILD,'b'):
1013 case O(BUILD,Bitmap):
1014 case O(CREATE,'b'):
1015 case O(CREATE,Bitmap): /* here we create the bitmap */
1016 if (strcmp(optarg, "none") == 0) {
1017 fprintf(stderr, Name ": '--bitmap none' only"
1018 " support for --grow\n");
1019 exit(2);
1020 }
1021 /* FALL THROUGH */
1022 case O(GROW,'b'):
1023 case O(GROW,Bitmap):
1024 if (strcmp(optarg, "internal")== 0 ||
1025 strcmp(optarg, "none")== 0 ||
1026 strchr(optarg, '/') != NULL) {
1027 bitmap_file = optarg;
1028 continue;
1029 }
1030 /* probable typo */
1031 fprintf(stderr, Name ": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1032 " not '%s'\n", optarg);
1033 exit(2);
1034
1035 case O(GROW,BitmapChunk):
1036 case O(BUILD,BitmapChunk):
1037 case O(CREATE,BitmapChunk): /* bitmap chunksize */
1038 bitmap_chunk = parse_size(optarg);
1039 if (bitmap_chunk < 0 ||
1040 bitmap_chunk & (bitmap_chunk - 1)) {
1041 fprintf(stderr,
1042 Name ": invalid bitmap chunksize: %s\n",
1043 optarg);
1044 exit(2);
1045 }
1046 /* convert sectors to B, chunk of 0 means 512B */
1047 bitmap_chunk = bitmap_chunk ? bitmap_chunk * 512 : 512;
1048 continue;
1049
1050 case O(GROW, WriteBehind):
1051 case O(BUILD, WriteBehind):
1052 case O(CREATE, WriteBehind): /* write-behind mode */
1053 write_behind = DEFAULT_MAX_WRITE_BEHIND;
1054 if (optarg) {
1055 write_behind = strtol(optarg, &c, 10);
1056 if (write_behind < 0 || *c ||
1057 write_behind > 16383) {
1058 fprintf(stderr, Name ": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
1059 exit(2);
1060 }
1061 }
1062 continue;
1063
1064 case O(INCREMENTAL, 'r'):
1065 case O(INCREMENTAL, RebuildMapOpt):
1066 rebuild_map = 1;
1067 continue;
1068 case O(INCREMENTAL, IncrementalPath):
1069 remove_path = optarg;
1070 continue;
1071 }
1072 /* We have now processed all the valid options. Anything else is
1073 * an error
1074 */
1075 if (option_index > 0)
1076 fprintf(stderr, Name ":option --%s not valid in %s mode\n",
1077 long_options[option_index].name,
1078 map_num(modes, mode));
1079 else
1080 fprintf(stderr, Name ": option -%c not valid in %s mode\n",
1081 opt, map_num(modes, mode));
1082 exit(2);
1083
1084 }
1085
1086 if (print_help) {
1087 char *help_text = Help;
1088 if (print_help == 2)
1089 help_text = OptionHelp;
1090 else
1091 switch (mode) {
1092 case ASSEMBLE : help_text = Help_assemble; break;
1093 case BUILD : help_text = Help_build; break;
1094 case CREATE : help_text = Help_create; break;
1095 case MANAGE : help_text = Help_manage; break;
1096 case MISC : help_text = Help_misc; break;
1097 case MONITOR : help_text = Help_monitor; break;
1098 case GROW : help_text = Help_grow; break;
1099 case INCREMENTAL:help_text= Help_incr; break;
1100 }
1101 fputs(help_text,stdout);
1102 exit(0);
1103 }
1104
1105 if (!mode && devs_found) {
1106 mode = MISC;
1107 devmode = 'Q';
1108 if (devlist->disposition == 0)
1109 devlist->disposition = devmode;
1110 }
1111 if (!mode) {
1112 fputs(Usage, stderr);
1113 exit(2);
1114 }
1115
1116 if (symlinks) {
1117 struct createinfo *ci = conf_get_create_info();
1118
1119 if (strcasecmp(symlinks, "yes") == 0)
1120 ci->symlinks = 1;
1121 else if (strcasecmp(symlinks, "no") == 0)
1122 ci->symlinks = 0;
1123 else {
1124 fprintf(stderr, Name ": option --symlinks must be 'no' or 'yes'\n");
1125 exit(2);
1126 }
1127 }
1128 /* Ok, got the option parsing out of the way
1129 * hopefully it's mostly right but there might be some stuff
1130 * missing
1131 *
1132 * That is mosty checked in the per-mode stuff but...
1133 *
1134 * For @,B,C and A without -s, the first device listed must be an md device
1135 * we check that here and open it.
1136 */
1137
1138 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
1139 (mode == ASSEMBLE && ! scan)) {
1140 if (devs_found < 1) {
1141 fprintf(stderr, Name ": an md device must be given in this mode\n");
1142 exit(2);
1143 }
1144 if ((int)ident.super_minor == -2 && autof) {
1145 fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
1146 exit(2);
1147 }
1148 if (mode == MANAGE || mode == GROW) {
1149 mdfd = open_mddev(devlist->devname, 1);
1150 if (mdfd < 0)
1151 exit(1);
1152 } else
1153 /* non-existent device is OK */
1154 mdfd = open_mddev(devlist->devname, 0);
1155 if (mdfd == -2) {
1156 fprintf(stderr, Name ": device %s exists but is not an "
1157 "md array.\n", devlist->devname);
1158 exit(1);
1159 }
1160 if ((int)ident.super_minor == -2) {
1161 struct stat stb;
1162 if (mdfd < 0) {
1163 fprintf(stderr, Name ": --super-minor=dev given, and "
1164 "listed device %s doesn't exist.\n",
1165 devlist->devname);
1166 exit(1);
1167 }
1168 fstat(mdfd, &stb);
1169 ident.super_minor = minor(stb.st_rdev);
1170 }
1171 if (mdfd >= 0 && mode != MANAGE && mode != GROW) {
1172 /* We don't really want this open yet, we just might
1173 * have wanted to check some things
1174 */
1175 close(mdfd);
1176 mdfd = -1;
1177 }
1178 }
1179
1180 if (raiddisks) {
1181 if (raiddisks == 1 && !force && level != -5) {
1182 fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
1183 " a mistake. If you really mean it you will need to specify --force before\n"
1184 " setting the number of drives.\n");
1185 exit(2);
1186 }
1187 }
1188
1189 if (homehost == NULL)
1190 homehost = conf_get_homehost(&require_homehost);
1191 if (homehost == NULL || strcasecmp(homehost, "<system>")==0) {
1192 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
1193 sys_hostname[sizeof(sys_hostname)-1] = 0;
1194 homehost = sys_hostname;
1195 }
1196 }
1197 if (homehost && (!homehost[0] || strcasecmp(homehost, "<none>") == 0)) {
1198 homehost = NULL;
1199 require_homehost = 0;
1200 }
1201
1202 if ((mode != MISC || devmode != 'E') &&
1203 geteuid() != 0) {
1204 fprintf(stderr, Name ": must be super-user to perform this action\n");
1205 exit(1);
1206 }
1207
1208 ident.autof = autof;
1209
1210 rv = 0;
1211 switch(mode) {
1212 case MANAGE:
1213 /* readonly, add/remove, readwrite, runstop */
1214 if (readonly>0)
1215 rv = Manage_ro(devlist->devname, mdfd, readonly);
1216 if (!rv && devs_found>1)
1217 rv = Manage_subdevs(devlist->devname, mdfd,
1218 devlist->next, verbose-quiet, test,
1219 update, force);
1220 if (!rv && readonly < 0)
1221 rv = Manage_ro(devlist->devname, mdfd, readonly);
1222 if (!rv && runstop)
1223 rv = Manage_runstop(devlist->devname, mdfd, runstop, quiet);
1224 break;
1225 case ASSEMBLE:
1226 if (devs_found == 1 && ident.uuid_set == 0 &&
1227 ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) {
1228 /* Only a device has been given, so get details from config file */
1229 struct mddev_ident *array_ident = conf_get_ident(devlist->devname);
1230 if (array_ident == NULL) {
1231 fprintf(stderr, Name ": %s not identified in config file.\n",
1232 devlist->devname);
1233 rv |= 1;
1234 if (mdfd >= 0)
1235 close(mdfd);
1236 } else {
1237 if (array_ident->autof == 0)
1238 array_ident->autof = autof;
1239 rv |= Assemble(ss, devlist->devname, array_ident,
1240 NULL, backup_file, invalid_backup,
1241 readonly, runstop, update,
1242 homehost, require_homehost,
1243 verbose-quiet, force,
1244 freeze_reshape);
1245 }
1246 } else if (!scan)
1247 rv = Assemble(ss, devlist->devname, &ident,
1248 devlist->next, backup_file, invalid_backup,
1249 readonly, runstop, update,
1250 homehost, require_homehost,
1251 verbose-quiet, force,
1252 freeze_reshape);
1253 else if (devs_found>0) {
1254 if (update && devs_found > 1) {
1255 fprintf(stderr, Name ": can only update a single array at a time\n");
1256 exit(1);
1257 }
1258 if (backup_file && devs_found > 1) {
1259 fprintf(stderr, Name ": can only assemble a single array when providing a backup file.\n");
1260 exit(1);
1261 }
1262 for (dv = devlist ; dv ; dv=dv->next) {
1263 struct mddev_ident *array_ident = conf_get_ident(dv->devname);
1264 if (array_ident == NULL) {
1265 fprintf(stderr, Name ": %s not identified in config file.\n",
1266 dv->devname);
1267 rv |= 1;
1268 continue;
1269 }
1270 if (array_ident->autof == 0)
1271 array_ident->autof = autof;
1272 rv |= Assemble(ss, dv->devname, array_ident,
1273 NULL, backup_file, invalid_backup,
1274 readonly, runstop, update,
1275 homehost, require_homehost,
1276 verbose-quiet, force,
1277 freeze_reshape);
1278 }
1279 } else {
1280 struct mddev_ident *a, *array_list = conf_get_ident(NULL);
1281 struct mddev_dev *devlist = conf_get_devs();
1282 struct map_ent *map = NULL;
1283 int cnt = 0;
1284 int failures, successes;
1285 if (devlist == NULL) {
1286 fprintf(stderr, Name ": No devices listed in conf file were found.\n");
1287 exit(1);
1288 }
1289 if (update) {
1290 fprintf(stderr, Name ": --update not meaningful with a --scan assembly.\n");
1291 exit(1);
1292 }
1293 if (backup_file) {
1294 fprintf(stderr, Name ": --backup_file not meaningful with a --scan assembly.\n");
1295 exit(1);
1296 }
1297 for (a = array_list; a ; a = a->next) {
1298 a->assembled = 0;
1299 if (a->autof == 0)
1300 a->autof = autof;
1301 }
1302 if (map_lock(&map))
1303 fprintf(stderr, Name " %s: failed to get "
1304 "exclusive lock on mapfile\n",
1305 __func__);
1306 do {
1307 failures = 0;
1308 successes = 0;
1309 rv = 0;
1310 for (a = array_list; a ; a = a->next) {
1311 int r;
1312 if (a->assembled)
1313 continue;
1314 if (a->devname &&
1315 strcasecmp(a->devname, "<ignore>") == 0)
1316 continue;
1317
1318 r = Assemble(ss, a->devname,
1319 a,
1320 NULL, NULL, 0,
1321 readonly, runstop, NULL,
1322 homehost, require_homehost,
1323 verbose-quiet, force,
1324 freeze_reshape);
1325 if (r == 0) {
1326 a->assembled = 1;
1327 successes++;
1328 } else
1329 failures++;
1330 rv |= r;
1331 cnt++;
1332 }
1333 } while (failures && successes);
1334 if (homehost && cnt == 0) {
1335 /* Maybe we can auto-assemble something.
1336 * Repeatedly call Assemble in auto-assemble mode
1337 * until it fails
1338 */
1339 int rv2;
1340 int acnt;
1341 ident.autof = autof;
1342 do {
1343 struct mddev_dev *devlist = conf_get_devs();
1344 acnt = 0;
1345 do {
1346 rv2 = Assemble(ss, NULL,
1347 &ident,
1348 devlist, NULL, 0,
1349 readonly,
1350 runstop, NULL,
1351 homehost,
1352 require_homehost,
1353 verbose-quiet,
1354 force,
1355 freeze_reshape);
1356 if (rv2==0) {
1357 cnt++;
1358 acnt++;
1359 }
1360 } while (rv2!=2);
1361 /* Incase there are stacked devices, we need to go around again */
1362 } while (acnt);
1363 if (cnt == 0 && rv == 0) {
1364 fprintf(stderr, Name ": No arrays found in config file or automatically\n");
1365 rv = 1;
1366 } else if (cnt)
1367 rv = 0;
1368 } else if (cnt == 0 && rv == 0) {
1369 fprintf(stderr, Name ": No arrays found in config file\n");
1370 rv = 1;
1371 }
1372 map_unlock(&map);
1373 }
1374 break;
1375 case BUILD:
1376 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1377 if (write_behind && !bitmap_file) {
1378 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1379 rv = 1;
1380 break;
1381 }
1382 if (raiddisks == 0) {
1383 fprintf(stderr, Name ": no raid-devices specified.\n");
1384 rv = 1;
1385 break;
1386 }
1387
1388 if (bitmap_file) {
1389 if (strcmp(bitmap_file, "internal")==0) {
1390 fprintf(stderr, Name ": 'internal' bitmaps not supported with --build\n");
1391 rv |= 1;
1392 break;
1393 }
1394 }
1395 rv = Build(devlist->devname, chunk, level, layout,
1396 raiddisks, devlist->next, assume_clean,
1397 bitmap_file, bitmap_chunk, write_behind,
1398 delay, verbose-quiet, autof, size);
1399 break;
1400 case CREATE:
1401 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1402 if (write_behind && !bitmap_file) {
1403 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1404 rv = 1;
1405 break;
1406 }
1407 if (raiddisks == 0) {
1408 fprintf(stderr, Name ": no raid-devices specified.\n");
1409 rv = 1;
1410 break;
1411 }
1412
1413 rv = Create(ss, devlist->devname, chunk, level, layout, size<0 ? 0 : size,
1414 raiddisks, sparedisks, ident.name, homehost,
1415 ident.uuid_set ? ident.uuid : NULL,
1416 devs_found-1, devlist->next, runstop, verbose-quiet, force, assume_clean,
1417 bitmap_file, bitmap_chunk, write_behind, delay, autof);
1418 break;
1419 case MISC:
1420 if (devmode == 'E') {
1421 if (devlist == NULL && !scan) {
1422 fprintf(stderr, Name ": No devices to examine\n");
1423 exit(2);
1424 }
1425 if (devlist == NULL)
1426 devlist = conf_get_devs();
1427 if (devlist == NULL) {
1428 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
1429 exit(1);
1430 }
1431 if (brief && verbose)
1432 brief = 2;
1433 rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief,
1434 export, scan,
1435 SparcAdjust, ss, homehost);
1436 } else if (devmode == DetailPlatform) {
1437 rv = Detail_Platform(ss ? ss->ss : NULL, ss ? scan : 1, verbose);
1438 } else {
1439 if (devlist == NULL) {
1440 if ((devmode=='D' || devmode == Waitclean) && scan) {
1441 /* apply --detail or --wait-clean to
1442 * all devices in /proc/mdstat
1443 */
1444 struct mdstat_ent *ms = mdstat_read(0, 1);
1445 struct mdstat_ent *e;
1446 struct map_ent *map = NULL;
1447 int members;
1448 int v = verbose>1?0:verbose+1;
1449
1450 for (members = 0; members <= 1; members++) {
1451 for (e=ms ; e ; e=e->next) {
1452 char *name;
1453 struct map_ent *me;
1454 int member = e->metadata_version &&
1455 strncmp(e->metadata_version,
1456 "external:/", 10) == 0;
1457 if (members != member)
1458 continue;
1459 me = map_by_devnum(&map, e->devnum);
1460 if (me && me->path
1461 && strcmp(me->path, "/unknown") != 0)
1462 name = me->path;
1463 else
1464 name = get_md_name(e->devnum);
1465
1466 if (!name) {
1467 fprintf(stderr, Name ": cannot find device file for %s\n",
1468 e->dev);
1469 continue;
1470 }
1471 if (devmode == 'D')
1472 rv |= Detail(name, v,
1473 export, test,
1474 homehost);
1475 else
1476 rv |= WaitClean(name, -1, v);
1477 put_md_name(name);
1478 }
1479 }
1480 free_mdstat(ms);
1481 } else if (devmode == 'S' && scan) {
1482 /* apply --stop to all devices in /proc/mdstat */
1483 /* Due to possible stacking of devices, repeat until
1484 * nothing more can be stopped
1485 */
1486 int progress=1, err;
1487 int last = 0;
1488 do {
1489 struct mdstat_ent *ms = mdstat_read(0, 0);
1490 struct mdstat_ent *e;
1491
1492 if (!progress) last = 1;
1493 progress = 0; err = 0;
1494 for (e=ms ; e ; e=e->next) {
1495 char *name = get_md_name(e->devnum);
1496
1497 if (!name) {
1498 fprintf(stderr, Name ": cannot find device file for %s\n",
1499 e->dev);
1500 continue;
1501 }
1502 mdfd = open_mddev(name, 1);
1503 if (mdfd >= 0) {
1504 if (Manage_runstop(name, mdfd, -1, quiet?1:last?0:-1))
1505 err = 1;
1506 else
1507 progress = 1;
1508 close(mdfd);
1509 }
1510
1511 put_md_name(name);
1512 }
1513 free_mdstat(ms);
1514 } while (!last && err);
1515 if (err) rv |= 1;
1516 } else if (devmode == UdevRules) {
1517 rv = Write_rules(udev_filename);
1518 } else {
1519 fprintf(stderr, Name ": No devices given.\n");
1520 exit(2);
1521 }
1522 }
1523 for (dv=devlist ; dv; dv=dv->next) {
1524 switch(dv->disposition) {
1525 case 'D':
1526 rv |= Detail(dv->devname,
1527 brief?1+verbose:0,
1528 export, test, homehost);
1529 continue;
1530 case 'K': /* Zero superblock */
1531 if (ss)
1532 rv |= Kill(dv->devname, ss, force, quiet,0);
1533 else {
1534 int q = quiet;
1535 do {
1536 rv |= Kill(dv->devname, NULL, force, q, 0);
1537 q = 1;
1538 } while (rv == 0);
1539 rv &= ~2;
1540 }
1541 continue;
1542 case 'Q':
1543 rv |= Query(dv->devname); continue;
1544 case 'X':
1545 rv |= ExamineBitmap(dv->devname, brief, ss); continue;
1546 case 'W':
1547 case WaitOpt:
1548 rv |= Wait(dv->devname); continue;
1549 case Waitclean:
1550 rv |= WaitClean(dv->devname, -1, verbose-quiet); continue;
1551 case KillSubarray:
1552 rv |= Kill_subarray(dv->devname, subarray, quiet);
1553 continue;
1554 case UpdateSubarray:
1555 if (update == NULL) {
1556 fprintf(stderr,
1557 Name ": -U/--update must be specified with --update-subarray\n");
1558 rv |= 1;
1559 continue;
1560 }
1561 rv |= Update_subarray(dv->devname, subarray, update, &ident, quiet);
1562 continue;
1563 }
1564 mdfd = open_mddev(dv->devname, 1);
1565 if (mdfd>=0) {
1566 switch(dv->disposition) {
1567 case 'R':
1568 rv |= Manage_runstop(dv->devname, mdfd, 1, quiet); break;
1569 case 'S':
1570 rv |= Manage_runstop(dv->devname, mdfd, -1, quiet); break;
1571 case 'o':
1572 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1573 case 'w':
1574 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1575 }
1576 close(mdfd);
1577 } else
1578 rv |= 1;
1579 }
1580 }
1581 break;
1582 case MONITOR:
1583 if (!devlist && !scan) {
1584 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
1585 rv = 1;
1586 break;
1587 }
1588 if (pidfile && !daemonise) {
1589 fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
1590 rv = 1;
1591 break;
1592 }
1593 if (delay == 0) {
1594 if (get_linux_version() > 2006016)
1595 /* mdstat responds to poll */
1596 delay = 1000;
1597 else
1598 delay = 60;
1599 }
1600 rv= Monitor(devlist, mailaddr, program,
1601 delay?delay:60, daemonise, scan, oneshot,
1602 dosyslog, test, pidfile, increments, spare_sharing);
1603 break;
1604
1605 case GROW:
1606 if (array_size >= 0) {
1607 /* alway impose array size first, independent of
1608 * anything else
1609 * Do not allow level or raid_disks changes at the
1610 * same time as that can be irreversibly destructive.
1611 */
1612 struct mdinfo sra;
1613 int err;
1614 if (raiddisks || level != UnSet) {
1615 fprintf(stderr, Name ": cannot change array size in same operation "
1616 "as changing raiddisks or level.\n"
1617 " Change size first, then check that data is still intact.\n");
1618 rv = 1;
1619 break;
1620 }
1621 sysfs_init(&sra, mdfd, 0);
1622 if (array_size == 0)
1623 err = sysfs_set_str(&sra, NULL, "array_size", "default");
1624 else
1625 err = sysfs_set_num(&sra, NULL, "array_size", array_size / 2);
1626 if (err < 0) {
1627 if (errno == E2BIG)
1628 fprintf(stderr, Name ": --array-size setting"
1629 " is too large.\n");
1630 else
1631 fprintf(stderr, Name ": current kernel does"
1632 " not support setting --array-size\n");
1633 rv = 1;
1634 break;
1635 }
1636 }
1637 if (devs_found > 1 && raiddisks == 0) {
1638 /* must be '-a'. */
1639 if (size >= 0 || chunk || layout_str != NULL || bitmap_file) {
1640 fprintf(stderr, Name ": --add cannot be used with "
1641 "other geometry changes in --grow mode\n");
1642 rv = 1;
1643 break;
1644 }
1645 for (dv=devlist->next; dv ; dv=dv->next) {
1646 rv = Grow_Add_device(devlist->devname, mdfd,
1647 dv->devname);
1648 if (rv)
1649 break;
1650 }
1651 } else if (bitmap_file) {
1652 if (size >= 0 || raiddisks || chunk ||
1653 layout_str != NULL || devs_found > 1) {
1654 fprintf(stderr, Name ": --bitmap changes cannot be "
1655 "used with other geometry changes "
1656 "in --grow mode\n");
1657 rv = 1;
1658 break;
1659 }
1660 if (delay == 0)
1661 delay = DEFAULT_BITMAP_DELAY;
1662 rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file,
1663 bitmap_chunk, delay, write_behind, force);
1664 } else if (grow_continue)
1665 rv = Grow_continue_command(devlist->devname,
1666 mdfd, backup_file,
1667 verbose);
1668 else if (size >= 0 || raiddisks != 0 || layout_str != NULL
1669 || chunk != 0 || level != UnSet) {
1670 rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file,
1671 size, level, layout_str, chunk, raiddisks,
1672 devlist->next,
1673 assume_clean, force);
1674 } else if (array_size < 0)
1675 fprintf(stderr, Name ": no changes to --grow\n");
1676 break;
1677 case INCREMENTAL:
1678 if (rebuild_map) {
1679 RebuildMap();
1680 }
1681 if (scan) {
1682 if (runstop <= 0) {
1683 fprintf(stderr, Name
1684 ": --incremental --scan meaningless without --run.\n");
1685 break;
1686 }
1687 if (devmode == 'f') {
1688 fprintf(stderr, Name
1689 ": --incremental --scan --fail not supported.\n");
1690 break;
1691 }
1692 rv = IncrementalScan(verbose);
1693 }
1694 if (!devlist) {
1695 if (!rebuild_map && !scan) {
1696 fprintf(stderr, Name
1697 ": --incremental requires a device.\n");
1698 rv = 1;
1699 }
1700 break;
1701 }
1702 if (devlist->next) {
1703 fprintf(stderr, Name
1704 ": --incremental can only handle one device.\n");
1705 rv = 1;
1706 break;
1707 }
1708 if (devmode == 'f')
1709 rv = IncrementalRemove(devlist->devname, remove_path,
1710 verbose-quiet);
1711 else
1712 rv = Incremental(devlist->devname, verbose-quiet,
1713 runstop, ss, homehost,
1714 require_homehost, autof,
1715 freeze_reshape);
1716 break;
1717 case AUTODETECT:
1718 autodetect();
1719 break;
1720 }
1721 exit(rv);
1722 }