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