]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdadm.c
sysfs: Avoid if and return on the same line
[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 ||
147 mode == CREATE || mode == GROW ||
148 mode == INCREMENTAL || 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 || s.size < 8) {
432 pr_err("invalid size: %s\n", optarg);
433 exit(2);
434 }
435 /* convert sectors to K */
436 s.size /= 2;
437 }
438 continue;
439
440 case O(GROW,'Z'): /* array size */
441 if (array_size > 0) {
442 pr_err("array-size may only be specified once. Second value is %s.\n", optarg);
443 exit(2);
444 }
445 if (strcmp(optarg, "max") == 0)
446 array_size = MAX_SIZE;
447 else {
448 array_size = parse_size(optarg);
449 if (array_size == 0 ||
450 array_size == INVALID_SECTORS) {
451 pr_err("invalid array size: %s\n",
452 optarg);
453 exit(2);
454 }
455 }
456 continue;
457
458 case O(CREATE,DataOffset):
459 case O(GROW,DataOffset):
460 if (data_offset != INVALID_SECTORS) {
461 pr_err("data-offset may only be specified one. Second value is %s.\n", optarg);
462 exit(2);
463 }
464 if (mode == CREATE && strcmp(optarg, "variable") == 0)
465 data_offset = VARIABLE_OFFSET;
466 else
467 data_offset = parse_size(optarg);
468 if (data_offset == INVALID_SECTORS) {
469 pr_err("invalid data-offset: %s\n",
470 optarg);
471 exit(2);
472 }
473 continue;
474
475 case O(GROW,'l'):
476 case O(CREATE,'l'):
477 case O(BUILD,'l'): /* set raid level*/
478 if (s.level != UnSet) {
479 pr_err("raid level may only be set once. Second value is %s.\n", optarg);
480 exit(2);
481 }
482 s.level = map_name(pers, optarg);
483 if (s.level == UnSet) {
484 pr_err("invalid raid level: %s\n",
485 optarg);
486 exit(2);
487 }
488 if (s.level != 0 && s.level != LEVEL_LINEAR &&
489 s.level != 1 && s.level != LEVEL_MULTIPATH &&
490 s.level != LEVEL_FAULTY && s.level != 10 &&
491 mode == BUILD) {
492 pr_err("Raid level %s not permitted with --build.\n",
493 optarg);
494 exit(2);
495 }
496 if (s.sparedisks > 0 && s.level < 1 && s.level >= -1) {
497 pr_err("raid level %s is incompatible with spare-devices setting.\n",
498 optarg);
499 exit(2);
500 }
501 ident.level = s.level;
502 continue;
503
504 case O(GROW, 'p'): /* new layout */
505 case O(GROW, Layout):
506 if (s.layout_str) {
507 pr_err("layout may only be sent once. Second value was %s\n", optarg);
508 exit(2);
509 }
510 s.layout_str = optarg;
511 /* 'Grow' will parse the value */
512 continue;
513
514 case O(CREATE,'p'): /* raid5 layout */
515 case O(CREATE,Layout):
516 case O(BUILD,'p'): /* faulty layout */
517 case O(BUILD,Layout):
518 if (s.layout != UnSet) {
519 pr_err("layout may only be sent once. Second value was %s\n", optarg);
520 exit(2);
521 }
522 switch(s.level) {
523 default:
524 pr_err("layout not meaningful for %s arrays.\n",
525 map_num(pers, s.level));
526 exit(2);
527 case UnSet:
528 pr_err("raid level must be given before layout.\n");
529 exit(2);
530
531 case 5:
532 s.layout = map_name(r5layout, optarg);
533 if (s.layout==UnSet) {
534 pr_err("layout %s not understood for raid5.\n",
535 optarg);
536 exit(2);
537 }
538 break;
539 case 6:
540 s.layout = map_name(r6layout, optarg);
541 if (s.layout==UnSet) {
542 pr_err("layout %s not understood for raid6.\n",
543 optarg);
544 exit(2);
545 }
546 break;
547
548 case 10:
549 s.layout = parse_layout_10(optarg);
550 if (s.layout < 0) {
551 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg);
552 exit(2);
553 }
554 break;
555 case LEVEL_FAULTY:
556 /* Faulty
557 * modeNNN
558 */
559 s.layout = parse_layout_faulty(optarg);
560 if (s.layout == -1) {
561 pr_err("layout %s not understood for faulty.\n",
562 optarg);
563 exit(2);
564 }
565 break;
566 }
567 continue;
568
569 case O(CREATE,AssumeClean):
570 case O(BUILD,AssumeClean): /* assume clean */
571 case O(GROW,AssumeClean):
572 s.assume_clean = 1;
573 continue;
574
575 case O(GROW,'n'):
576 case O(CREATE,'n'):
577 case O(BUILD,'n'): /* number of raid disks */
578 if (s.raiddisks) {
579 pr_err("raid-devices set twice: %d and %s\n",
580 s.raiddisks, optarg);
581 exit(2);
582 }
583 s.raiddisks = parse_num(optarg);
584 if (s.raiddisks <= 0) {
585 pr_err("invalid number of raid devices: %s\n",
586 optarg);
587 exit(2);
588 }
589 ident.raid_disks = s.raiddisks;
590 continue;
591 case O(ASSEMBLE, Nodes):
592 case O(GROW, Nodes):
593 case O(CREATE, Nodes):
594 c.nodes = parse_num(optarg);
595 if (c.nodes <= 0) {
596 pr_err("invalid number for the number of cluster nodes: %s\n",
597 optarg);
598 exit(2);
599 }
600 continue;
601 case O(CREATE, ClusterName):
602 case O(ASSEMBLE, ClusterName):
603 c.homecluster = optarg;
604 if (strlen(c.homecluster) > 64) {
605 pr_err("Cluster name too big.\n");
606 exit(ERANGE);
607 }
608 continue;
609 case O(CREATE,'x'): /* number of spare (eXtra) disks */
610 if (s.sparedisks) {
611 pr_err("spare-devices set twice: %d and %s\n",
612 s.sparedisks, optarg);
613 exit(2);
614 }
615 if (s.level != UnSet && s.level <= 0 && s.level >= -1) {
616 pr_err("spare-devices setting is incompatible with raid level %d\n",
617 s.level);
618 exit(2);
619 }
620 s.sparedisks = parse_num(optarg);
621 if (s.sparedisks < 0) {
622 pr_err("invalid number of spare-devices: %s\n",
623 optarg);
624 exit(2);
625 }
626 continue;
627
628 case O(CREATE,'a'):
629 case O(CREATE,Auto):
630 case O(BUILD,'a'):
631 case O(BUILD,Auto):
632 case O(INCREMENTAL,'a'):
633 case O(INCREMENTAL,Auto):
634 case O(ASSEMBLE,'a'):
635 case O(ASSEMBLE,Auto): /* auto-creation of device node */
636 c.autof = parse_auto(optarg, "--auto flag", 0);
637 continue;
638
639 case O(CREATE,Symlinks):
640 case O(BUILD,Symlinks):
641 case O(ASSEMBLE,Symlinks): /* auto creation of symlinks in /dev to /dev/md */
642 symlinks = optarg;
643 continue;
644
645 case O(BUILD,'f'): /* force honouring '-n 1' */
646 case O(BUILD,Force): /* force honouring '-n 1' */
647 case O(GROW,'f'): /* ditto */
648 case O(GROW,Force): /* ditto */
649 case O(CREATE,'f'): /* force honouring of device list */
650 case O(CREATE,Force): /* force honouring of device list */
651 case O(ASSEMBLE,'f'): /* force assembly */
652 case O(ASSEMBLE,Force): /* force assembly */
653 case O(MISC,'f'): /* force zero */
654 case O(MISC,Force): /* force zero */
655 case O(MANAGE,Force): /* add device which is too large */
656 c.force=1;
657 continue;
658 /* now for the Assemble options */
659 case O(ASSEMBLE, FreezeReshape): /* Freeze reshape during
660 * initrd phase */
661 case O(INCREMENTAL, FreezeReshape):
662 c.freeze_reshape = 1;
663 continue;
664 case O(CREATE,'u'): /* uuid of array */
665 case O(ASSEMBLE,'u'): /* uuid of array */
666 if (ident.uuid_set) {
667 pr_err("uuid cannot be set twice. Second value %s.\n", optarg);
668 exit(2);
669 }
670 if (parse_uuid(optarg, ident.uuid))
671 ident.uuid_set = 1;
672 else {
673 pr_err("Bad uuid: %s\n", optarg);
674 exit(2);
675 }
676 continue;
677
678 case O(CREATE,'N'):
679 case O(ASSEMBLE,'N'):
680 case O(MISC,'N'):
681 if (ident.name[0]) {
682 pr_err("name cannot be set twice. Second value %s.\n", optarg);
683 exit(2);
684 }
685 if (mode == MISC && !c.subarray) {
686 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
687 exit(2);
688 }
689 if (strlen(optarg) > 32) {
690 pr_err("name '%s' is too long, 32 chars max.\n",
691 optarg);
692 exit(2);
693 }
694 strcpy(ident.name, optarg);
695 continue;
696
697 case O(ASSEMBLE,'m'): /* super-minor for array */
698 case O(ASSEMBLE,SuperMinor):
699 if (ident.super_minor != UnSet) {
700 pr_err("super-minor cannot be set twice. Second value: %s.\n", optarg);
701 exit(2);
702 }
703 if (strcmp(optarg, "dev") == 0)
704 ident.super_minor = -2;
705 else {
706 ident.super_minor = parse_num(optarg);
707 if (ident.super_minor < 0) {
708 pr_err("Bad super-minor number: %s.\n", optarg);
709 exit(2);
710 }
711 }
712 continue;
713
714 case O(ASSEMBLE,'o'):
715 case O(MANAGE,'o'):
716 case O(CREATE,'o'):
717 c.readonly = 1;
718 continue;
719
720 case O(ASSEMBLE,'U'): /* update the superblock */
721 case O(MISC,'U'):
722 if (c.update) {
723 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
724 c.update, optarg);
725 exit(2);
726 }
727 if (mode == MISC && !c.subarray) {
728 pr_err("Only subarrays can be updated in misc mode\n");
729 exit(2);
730 }
731 c.update = optarg;
732 if (strcmp(c.update, "sparc2.2") == 0)
733 continue;
734 if (strcmp(c.update, "super-minor") == 0)
735 continue;
736 if (strcmp(c.update, "summaries") == 0)
737 continue;
738 if (strcmp(c.update, "resync") == 0)
739 continue;
740 if (strcmp(c.update, "uuid") == 0)
741 continue;
742 if (strcmp(c.update, "name") == 0)
743 continue;
744 if (strcmp(c.update, "homehost") == 0)
745 continue;
746 if (strcmp(c.update, "home-cluster") == 0)
747 continue;
748 if (strcmp(c.update, "nodes") == 0)
749 continue;
750 if (strcmp(c.update, "devicesize") == 0)
751 continue;
752 if (strcmp(c.update, "no-bitmap") == 0)
753 continue;
754 if (strcmp(c.update, "bbl") == 0)
755 continue;
756 if (strcmp(c.update, "no-bbl") == 0)
757 continue;
758 if (strcmp(c.update, "force-no-bbl") == 0)
759 continue;
760 if (strcmp(c.update, "metadata") == 0)
761 continue;
762 if (strcmp(c.update, "revert-reshape") == 0)
763 continue;
764 if (strcmp(c.update, "byteorder")==0) {
765 if (ss) {
766 pr_err("must not set metadata type with --update=byteorder.\n");
767 exit(2);
768 }
769 for(i=0; !ss && superlist[i]; i++)
770 ss = superlist[i]->match_metadata_desc(
771 "0.swap");
772 if (!ss) {
773 pr_err("INTERNAL ERROR cannot find 0.swap\n");
774 exit(2);
775 }
776
777 continue;
778 }
779 if (strcmp(c.update,"?") == 0 ||
780 strcmp(c.update, "help") == 0) {
781 outf = stdout;
782 fprintf(outf, "%s: ", Name);
783 } else {
784 outf = stderr;
785 fprintf(outf,
786 "%s: '--update=%s' is invalid. ",
787 Name, c.update);
788 }
789 fprintf(outf, "Valid --update options are:\n"
790 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'nodes', 'resync',\n"
791 " 'summaries', 'homehost', 'home-cluster', 'byteorder', 'devicesize',\n"
792 " 'no-bitmap', 'metadata', 'revert-reshape'\n"
793 " 'bbl', 'no-bbl', 'force-no-bbl'\n"
794 );
795 exit(outf == stdout ? 0 : 2);
796
797 case O(MANAGE,'U'):
798 /* update=devicesize is allowed with --re-add */
799 if (devmode != 'A') {
800 pr_err("--update in Manage mode only allowed with --re-add.\n");
801 exit(1);
802 }
803 if (c.update) {
804 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
805 c.update, optarg);
806 exit(2);
807 }
808 c.update = optarg;
809 if (strcmp(c.update, "devicesize") != 0 &&
810 strcmp(c.update, "bbl") != 0 &&
811 strcmp(c.update, "force-no-bbl") != 0 &&
812 strcmp(c.update, "no-bbl") != 0) {
813 pr_err("only 'devicesize', 'bbl', 'no-bbl', and 'force-no-bbl' can be updated with --re-add\n");
814 exit(2);
815 }
816 continue;
817
818 case O(INCREMENTAL,NoDegraded):
819 pr_err("--no-degraded is deprecated in Incremental mode\n");
820 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
821 c.runstop = -1; /* --stop isn't allowed for --assemble,
822 * so we overload slightly */
823 continue;
824
825 case O(ASSEMBLE,'c'):
826 case O(ASSEMBLE,ConfigFile):
827 case O(INCREMENTAL, 'c'):
828 case O(INCREMENTAL, ConfigFile):
829 case O(MISC, 'c'):
830 case O(MISC, ConfigFile):
831 case O(MONITOR,'c'):
832 case O(MONITOR,ConfigFile):
833 case O(CREATE,ConfigFile):
834 if (configfile) {
835 pr_err("configfile cannot be set twice. Second value is %s.\n", optarg);
836 exit(2);
837 }
838 configfile = optarg;
839 set_conffile(configfile);
840 /* FIXME possibly check that config file exists. Even parse it */
841 continue;
842 case O(ASSEMBLE,'s'): /* scan */
843 case O(MISC,'s'):
844 case O(MONITOR,'s'):
845 case O(INCREMENTAL,'s'):
846 c.scan = 1;
847 continue;
848
849 case O(MONITOR,'m'): /* mail address */
850 case O(MONITOR,EMail):
851 if (mailaddr)
852 pr_err("only specify one mailaddress. %s ignored.\n",
853 optarg);
854 else
855 mailaddr = optarg;
856 continue;
857
858 case O(MONITOR,'p'): /* alert program */
859 case O(MONITOR,ProgramOpt): /* alert program */
860 if (program)
861 pr_err("only specify one alter program. %s ignored.\n",
862 optarg);
863 else
864 program = optarg;
865 continue;
866
867 case O(MONITOR,'r'): /* rebuild increments */
868 case O(MONITOR,Increment):
869 increments = atoi(optarg);
870 if (increments > 99 || increments < 1) {
871 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
872 exit(2);
873 }
874 continue;
875
876 case O(MONITOR,'d'): /* delay in seconds */
877 case O(GROW, 'd'):
878 case O(BUILD,'d'): /* delay for bitmap updates */
879 case O(CREATE,'d'):
880 if (c.delay)
881 pr_err("only specify delay once. %s ignored.\n",
882 optarg);
883 else {
884 c.delay = parse_num(optarg);
885 if (c.delay < 1) {
886 pr_err("invalid delay: %s\n",
887 optarg);
888 exit(2);
889 }
890 }
891 continue;
892 case O(MONITOR,'f'): /* daemonise */
893 case O(MONITOR,Fork):
894 daemonise = 1;
895 continue;
896 case O(MONITOR,'i'): /* pid */
897 if (pidfile)
898 pr_err("only specify one pid file. %s ignored.\n",
899 optarg);
900 else
901 pidfile = optarg;
902 continue;
903 case O(MONITOR,'1'): /* oneshot */
904 oneshot = 1;
905 spare_sharing = 0;
906 continue;
907 case O(MONITOR,'t'): /* test */
908 c.test = 1;
909 continue;
910 case O(MONITOR,'y'): /* log messages to syslog */
911 openlog("mdadm", LOG_PID, SYSLOG_FACILITY);
912 dosyslog = 1;
913 continue;
914 case O(MONITOR, NoSharing):
915 spare_sharing = 0;
916 continue;
917
918 /* now the general management options. Some are applicable
919 * to other modes. None have arguments.
920 */
921 case O(GROW,'a'):
922 case O(GROW,Add):
923 case O(MANAGE,'a'):
924 case O(MANAGE,Add): /* add a drive */
925 devmode = 'a';
926 continue;
927 case O(MANAGE,AddSpare): /* add drive - never re-add */
928 devmode = 'S';
929 continue;
930 case O(MANAGE,AddJournal): /* add journal */
931 if (s.journaldisks && (s.level < 4 || s.level > 6)) {
932 pr_err("--add-journal is only supported for RAID level 4/5/6.\n");
933 exit(2);
934 }
935 devmode = 'j';
936 continue;
937 case O(MANAGE,ReAdd):
938 devmode = 'A';
939 continue;
940 case O(MANAGE,'r'): /* remove a drive */
941 case O(MANAGE,Remove):
942 devmode = 'r';
943 continue;
944 case O(MANAGE,'f'): /* set faulty */
945 case O(MANAGE,Fail):
946 case O(INCREMENTAL,'f'):
947 case O(INCREMENTAL,Remove):
948 case O(INCREMENTAL,Fail): /* r for incremental is taken, use f
949 * even though we will both fail and
950 * remove the device */
951 devmode = 'f';
952 continue;
953 case O(MANAGE, ClusterConfirm):
954 devmode = 'c';
955 continue;
956 case O(MANAGE,Replace):
957 /* Mark these devices for replacement */
958 devmode = 'R';
959 continue;
960 case O(MANAGE,With):
961 /* These are the replacements to use */
962 if (devmode != 'R') {
963 pr_err("--with must follow --replace\n");
964 exit(2);
965 }
966 devmode = 'W';
967 continue;
968 case O(INCREMENTAL,'R'):
969 case O(MANAGE,'R'):
970 case O(ASSEMBLE,'R'):
971 case O(BUILD,'R'):
972 case O(CREATE,'R'): /* Run the array */
973 if (c.runstop < 0) {
974 pr_err("Cannot both Stop and Run an array\n");
975 exit(2);
976 }
977 c.runstop = 1;
978 continue;
979 case O(MANAGE,'S'):
980 if (c.runstop > 0) {
981 pr_err("Cannot both Run and Stop an array\n");
982 exit(2);
983 }
984 c.runstop = -1;
985 continue;
986 case O(MANAGE,'t'):
987 c.test = 1;
988 continue;
989
990 case O(MISC,'Q'):
991 case O(MISC,'D'):
992 case O(MISC,'E'):
993 case O(MISC,KillOpt):
994 case O(MISC,'R'):
995 case O(MISC,'S'):
996 case O(MISC,'X'):
997 case O(MISC, ExamineBB):
998 case O(MISC,'o'):
999 case O(MISC,'w'):
1000 case O(MISC,'W'):
1001 case O(MISC, WaitOpt):
1002 case O(MISC, Waitclean):
1003 case O(MISC, DetailPlatform):
1004 case O(MISC, KillSubarray):
1005 case O(MISC, UpdateSubarray):
1006 case O(MISC, Dump):
1007 case O(MISC, Restore):
1008 case O(MISC ,Action):
1009 if (opt == KillSubarray || opt == UpdateSubarray) {
1010 if (c.subarray) {
1011 pr_err("subarray can only be specified once\n");
1012 exit(2);
1013 }
1014 c.subarray = optarg;
1015 }
1016 if (opt == Action) {
1017 if (c.action) {
1018 pr_err("Only one --action can be specified\n");
1019 exit(2);
1020 }
1021 if (strcmp(optarg, "idle") == 0 ||
1022 strcmp(optarg, "frozen") == 0 ||
1023 strcmp(optarg, "check") == 0 ||
1024 strcmp(optarg, "repair") == 0)
1025 c.action = optarg;
1026 else {
1027 pr_err("action must be one of idle, frozen, check, repair\n");
1028 exit(2);
1029 }
1030 }
1031 if (devmode && devmode != opt &&
1032 (devmode == 'E' ||
1033 (opt == 'E' && devmode != 'Q'))) {
1034 pr_err("--examine/-E cannot be given with ");
1035 if (devmode == 'E') {
1036 if (option_index >= 0)
1037 fprintf(stderr, "--%s\n",
1038 long_options[option_index].name);
1039 else
1040 fprintf(stderr, "-%c\n", opt);
1041 } else if (isalpha(devmode))
1042 fprintf(stderr, "-%c\n", devmode);
1043 else
1044 fprintf(stderr, "previous option\n");
1045 exit(2);
1046 }
1047 devmode = opt;
1048 if (opt == Dump || opt == Restore) {
1049 if (dump_directory != NULL) {
1050 pr_err("dump/restore directory specified twice: %s and %s\n",
1051 dump_directory, optarg);
1052 exit(2);
1053 }
1054 dump_directory = optarg;
1055 }
1056 continue;
1057 case O(MISC, UdevRules):
1058 if (devmode && devmode != opt) {
1059 pr_err("--udev-rules must be the only option.\n");
1060 } else {
1061 if (udev_filename)
1062 pr_err("only specify one udev rule filename. %s ignored.\n",
1063 optarg);
1064 else
1065 udev_filename = optarg;
1066 }
1067 devmode = opt;
1068 continue;
1069 case O(MISC,'t'):
1070 c.test = 1;
1071 continue;
1072
1073 case O(MISC, Sparc22):
1074 if (devmode != 'E') {
1075 pr_err("--sparc2.2 only allowed with --examine\n");
1076 exit(2);
1077 }
1078 c.SparcAdjust = 1;
1079 continue;
1080
1081 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
1082 case O(ASSEMBLE,Bitmap):
1083 if (!optarg) {
1084 pr_err("bitmap file needed with -b in --assemble mode\n");
1085 exit(2);
1086 }
1087 if (strcmp(optarg, "internal") == 0) {
1088 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1089 continue;
1090 }
1091 bitmap_fd = open(optarg, O_RDWR);
1092 if (!*optarg || bitmap_fd < 0) {
1093 pr_err("cannot open bitmap file %s: %s\n", optarg, strerror(errno));
1094 exit(2);
1095 }
1096 ident.bitmap_fd = bitmap_fd; /* for Assemble */
1097 continue;
1098
1099 case O(ASSEMBLE, BackupFile):
1100 case O(GROW, BackupFile):
1101 /* Specify a file into which grow might place a backup,
1102 * or from which assemble might recover a backup
1103 */
1104 if (c.backup_file) {
1105 pr_err("backup file already specified, rejecting %s\n", optarg);
1106 exit(2);
1107 }
1108 c.backup_file = optarg;
1109 continue;
1110
1111 case O(GROW, Continue):
1112 /* Continue interrupted grow
1113 */
1114 grow_continue = 1;
1115 continue;
1116 case O(ASSEMBLE, InvalidBackup):
1117 /* Acknowledge that the backupfile is invalid, but ask
1118 * to continue anyway
1119 */
1120 c.invalid_backup = 1;
1121 continue;
1122
1123 case O(BUILD,'b'):
1124 case O(BUILD,Bitmap):
1125 case O(CREATE,'b'):
1126 case O(CREATE,Bitmap): /* here we create the bitmap */
1127 case O(GROW,'b'):
1128 case O(GROW,Bitmap):
1129 if (strcmp(optarg, "internal") == 0 ||
1130 strcmp(optarg, "none") == 0 ||
1131 strchr(optarg, '/') != NULL) {
1132 s.bitmap_file = optarg;
1133 continue;
1134 }
1135 if (strcmp(optarg, "clustered") == 0) {
1136 s.bitmap_file = optarg;
1137 /* Set the default number of cluster nodes
1138 * to 4 if not already set by user
1139 */
1140 if (c.nodes < 1)
1141 c.nodes = 4;
1142 continue;
1143 }
1144 /* probable typo */
1145 pr_err("bitmap file must contain a '/', or be 'internal', or be 'clustered', or 'none'\n"
1146 " not '%s'\n", optarg);
1147 exit(2);
1148
1149 case O(GROW,BitmapChunk):
1150 case O(BUILD,BitmapChunk):
1151 case O(CREATE,BitmapChunk): /* bitmap chunksize */
1152 s.bitmap_chunk = parse_size(optarg);
1153 if (s.bitmap_chunk == 0 ||
1154 s.bitmap_chunk == INVALID_SECTORS ||
1155 s.bitmap_chunk & (s.bitmap_chunk - 1)) {
1156 pr_err("invalid bitmap chunksize: %s\n",
1157 optarg);
1158 exit(2);
1159 }
1160 s.bitmap_chunk = s.bitmap_chunk * 512;
1161 continue;
1162
1163 case O(GROW, WriteBehind):
1164 case O(BUILD, WriteBehind):
1165 case O(CREATE, WriteBehind): /* write-behind mode */
1166 s.write_behind = DEFAULT_MAX_WRITE_BEHIND;
1167 if (optarg) {
1168 s.write_behind = parse_num(optarg);
1169 if (s.write_behind < 0 ||
1170 s.write_behind > 16383) {
1171 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
1172 exit(2);
1173 }
1174 }
1175 continue;
1176
1177 case O(INCREMENTAL, 'r'):
1178 case O(INCREMENTAL, RebuildMapOpt):
1179 rebuild_map = 1;
1180 continue;
1181 case O(INCREMENTAL, IncrementalPath):
1182 remove_path = optarg;
1183 continue;
1184 case O(CREATE, WriteJournal):
1185 if (s.journaldisks) {
1186 pr_err("Please specify only one journal device for the array.\n");
1187 pr_err("Ignoring --write-journal %s...\n", optarg);
1188 continue;
1189 }
1190 dv = xmalloc(sizeof(*dv));
1191 dv->devname = optarg;
1192 dv->disposition = 'j'; /* WriteJournal */
1193 dv->used = 0;
1194 dv->next = NULL;
1195 *devlistend = dv;
1196 devlistend = &dv->next;
1197 devs_found++;
1198
1199 s.journaldisks = 1;
1200 continue;
1201 }
1202 /* We have now processed all the valid options. Anything else is
1203 * an error
1204 */
1205 if (option_index > 0)
1206 pr_err(":option --%s not valid in %s mode\n",
1207 long_options[option_index].name,
1208 map_num(modes, mode));
1209 else
1210 pr_err("option -%c not valid in %s mode\n",
1211 opt, map_num(modes, mode));
1212 exit(2);
1213
1214 }
1215
1216 if (print_help) {
1217 char *help_text;
1218 if (print_help == 2)
1219 help_text = OptionHelp;
1220 else
1221 help_text = mode_help[mode];
1222 if (help_text == NULL)
1223 help_text = Help;
1224 fputs(help_text,stdout);
1225 exit(0);
1226 }
1227
1228 if (s.journaldisks && (s.level < 4 || s.level > 6)) {
1229 pr_err("--write-journal is only supported for RAID level 4/5/6.\n");
1230 exit(2);
1231 }
1232
1233 if (!mode && devs_found) {
1234 mode = MISC;
1235 devmode = 'Q';
1236 if (devlist->disposition == 0)
1237 devlist->disposition = devmode;
1238 }
1239 if (!mode) {
1240 fputs(Usage, stderr);
1241 exit(2);
1242 }
1243
1244 if (symlinks) {
1245 struct createinfo *ci = conf_get_create_info();
1246
1247 if (strcasecmp(symlinks, "yes") == 0)
1248 ci->symlinks = 1;
1249 else if (strcasecmp(symlinks, "no") == 0)
1250 ci->symlinks = 0;
1251 else {
1252 pr_err("option --symlinks must be 'no' or 'yes'\n");
1253 exit(2);
1254 }
1255 }
1256 /* Ok, got the option parsing out of the way
1257 * hopefully it's mostly right but there might be some stuff
1258 * missing
1259 *
1260 * That is mosty checked in the per-mode stuff but...
1261 *
1262 * For @,B,C and A without -s, the first device listed must be
1263 * an md device. We check that here and open it.
1264 */
1265
1266 if (mode == MANAGE || mode == BUILD || mode == CREATE ||
1267 mode == GROW || (mode == ASSEMBLE && ! c.scan)) {
1268 if (devs_found < 1) {
1269 pr_err("an md device must be given in this mode\n");
1270 exit(2);
1271 }
1272 if ((int)ident.super_minor == -2 && c.autof) {
1273 pr_err("--super-minor=dev is incompatible with --auto\n");
1274 exit(2);
1275 }
1276 if (mode == MANAGE || mode == GROW) {
1277 mdfd = open_mddev(devlist->devname, 1);
1278 if (mdfd < 0)
1279 exit(1);
1280 } else
1281 /* non-existent device is OK */
1282 mdfd = open_mddev(devlist->devname, 0);
1283 if (mdfd == -2) {
1284 pr_err("device %s exists but is not an md array.\n", devlist->devname);
1285 exit(1);
1286 }
1287 if ((int)ident.super_minor == -2) {
1288 struct stat stb;
1289 if (mdfd < 0) {
1290 pr_err("--super-minor=dev given, and listed device %s doesn't exist.\n",
1291 devlist->devname);
1292 exit(1);
1293 }
1294 fstat(mdfd, &stb);
1295 ident.super_minor = minor(stb.st_rdev);
1296 }
1297 if (mdfd >= 0 && mode != MANAGE && mode != GROW) {
1298 /* We don't really want this open yet, we just might
1299 * have wanted to check some things
1300 */
1301 close(mdfd);
1302 mdfd = -1;
1303 }
1304 }
1305
1306 if (s.raiddisks) {
1307 if (s.raiddisks == 1 && !c.force && s.level != LEVEL_FAULTY) {
1308 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1309 " a mistake. If you really mean it you will need to specify --force before\n"
1310 " setting the number of drives.\n");
1311 exit(2);
1312 }
1313 }
1314
1315 if (c.homehost == NULL && c.require_homehost)
1316 c.homehost = conf_get_homehost(&c.require_homehost);
1317 if (c.homehost == NULL || strcasecmp(c.homehost, "<system>") == 0) {
1318 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
1319 sys_hostname[sizeof(sys_hostname)-1] = 0;
1320 c.homehost = sys_hostname;
1321 }
1322 }
1323 if (c.homehost &&
1324 (!c.homehost[0] || strcasecmp(c.homehost, "<none>") == 0)) {
1325 c.homehost = NULL;
1326 c.require_homehost = 0;
1327 }
1328
1329 rv = 0;
1330
1331 set_hooks(); /* set hooks from libs */
1332
1333 if (c.homecluster == NULL && (c.nodes > 0)) {
1334 c.homecluster = conf_get_homecluster();
1335 if (c.homecluster == NULL)
1336 rv = get_cluster_name(&c.homecluster);
1337 if (rv) {
1338 pr_err("The md can't get cluster name\n");
1339 exit(1);
1340 }
1341 }
1342
1343 if (c.backup_file && data_offset != INVALID_SECTORS) {
1344 pr_err("--backup-file and --data-offset are incompatible\n");
1345 exit(2);
1346 }
1347
1348 if ((mode == MISC && devmode == 'E') ||
1349 (mode == MONITOR && spare_sharing == 0))
1350 /* Anyone may try this */;
1351 else if (geteuid() != 0) {
1352 pr_err("must be super-user to perform this action\n");
1353 exit(1);
1354 }
1355
1356 ident.autof = c.autof;
1357
1358 if (c.scan && c.verbose < 2)
1359 /* --scan implied --brief unless -vv */
1360 c.brief = 1;
1361
1362 switch(mode) {
1363 case MANAGE:
1364 /* readonly, add/remove, readwrite, runstop */
1365 if (c.readonly > 0)
1366 rv = Manage_ro(devlist->devname, mdfd, c.readonly);
1367 if (!rv && devs_found>1)
1368 rv = Manage_subdevs(devlist->devname, mdfd,
1369 devlist->next, c.verbose, c.test,
1370 c.update, c.force);
1371 if (!rv && c.readonly < 0)
1372 rv = Manage_ro(devlist->devname, mdfd, c.readonly);
1373 if (!rv && c.runstop > 0)
1374 rv = Manage_run(devlist->devname, mdfd, &c);
1375 if (!rv && c.runstop < 0)
1376 rv = Manage_stop(devlist->devname, mdfd, c.verbose, 0);
1377 break;
1378 case ASSEMBLE:
1379 if (devs_found == 1 && ident.uuid_set == 0 &&
1380 ident.super_minor == UnSet && ident.name[0] == 0 &&
1381 !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 ||
1464 strcmp(s.bitmap_file, "clustered") != 0) {
1465 pr_err("--nodes argument only compatible with --bitmap=clustered\n");
1466 rv = 1;
1467 break;
1468 }
1469
1470 if (s.level != 1) {
1471 pr_err("--bitmap=clustered is currently supported with RAID mirror only\n");
1472 rv = 1;
1473 break;
1474 }
1475 }
1476
1477 if (s.write_behind && !s.bitmap_file) {
1478 pr_err("write-behind mode requires a bitmap.\n");
1479 rv = 1;
1480 break;
1481 }
1482 if (s.raiddisks == 0) {
1483 pr_err("no raid-devices specified.\n");
1484 rv = 1;
1485 break;
1486 }
1487
1488 rv = Create(ss, devlist->devname,
1489 ident.name, ident.uuid_set ? ident.uuid : NULL,
1490 devs_found-1, devlist->next,
1491 &s, &c, data_offset);
1492 break;
1493 case MISC:
1494 if (devmode == 'E') {
1495 if (devlist == NULL && !c.scan) {
1496 pr_err("No devices to examine\n");
1497 exit(2);
1498 }
1499 if (devlist == NULL)
1500 devlist = conf_get_devs();
1501 if (devlist == NULL) {
1502 pr_err("No devices listed in %s\n", configfile?configfile:DefaultConfFile);
1503 exit(1);
1504 }
1505 rv = Examine(devlist, &c, ss);
1506 } else if (devmode == DetailPlatform) {
1507 rv = Detail_Platform(ss ? ss->ss : NULL, ss ? c.scan : 1,
1508 c.verbose, c.export,
1509 devlist ? devlist->devname : NULL);
1510 } else if (devlist == NULL) {
1511 if (devmode == 'S' && c.scan)
1512 rv = stop_scan(c.verbose);
1513 else if ((devmode == 'D' || devmode == Waitclean) &&
1514 c.scan)
1515 rv = misc_scan(devmode, &c);
1516 else if (devmode == UdevRules)
1517 rv = Write_rules(udev_filename);
1518 else {
1519 pr_err("No devices given.\n");
1520 exit(2);
1521 }
1522 } else
1523 rv = misc_list(devlist, &ident, dump_directory, ss, &c);
1524 break;
1525 case MONITOR:
1526 if (!devlist && !c.scan) {
1527 pr_err("Cannot monitor: need --scan or at least one device\n");
1528 rv = 1;
1529 break;
1530 }
1531 if (pidfile && !daemonise) {
1532 pr_err("Cannot write a pid file when not in daemon mode\n");
1533 rv = 1;
1534 break;
1535 }
1536 if (c.delay == 0) {
1537 if (get_linux_version() > 2006016)
1538 /* mdstat responds to poll */
1539 c.delay = 1000;
1540 else
1541 c.delay = 60;
1542 }
1543 rv= Monitor(devlist, mailaddr, program,
1544 &c, daemonise, oneshot,
1545 dosyslog, pidfile, increments,
1546 spare_sharing);
1547 break;
1548
1549 case GROW:
1550 if (array_size > 0) {
1551 /* alway impose array size first, independent of
1552 * anything else
1553 * Do not allow level or raid_disks changes at the
1554 * same time as that can be irreversibly destructive.
1555 */
1556 struct mdinfo sra;
1557 int err;
1558 if (s.raiddisks || s.level != UnSet) {
1559 pr_err("cannot change array size in same operation as changing raiddisks or level.\n"
1560 " Change size first, then check that data is still intact.\n");
1561 rv = 1;
1562 break;
1563 }
1564 sysfs_init(&sra, mdfd, NULL);
1565 if (array_size == MAX_SIZE)
1566 err = sysfs_set_str(&sra, NULL, "array_size", "default");
1567 else
1568 err = sysfs_set_num(&sra, NULL, "array_size", array_size / 2);
1569 if (err < 0) {
1570 if (errno == E2BIG)
1571 pr_err("--array-size setting is too large.\n");
1572 else
1573 pr_err("current kernel does not support setting --array-size\n");
1574 rv = 1;
1575 break;
1576 }
1577 }
1578 if (devs_found > 1 && s.raiddisks == 0 && s.level == UnSet) {
1579 /* must be '-a'. */
1580 if (s.size > 0 || s.chunk ||
1581 s.layout_str || s.bitmap_file) {
1582 pr_err("--add cannot be used with other geometry changes in --grow mode\n");
1583 rv = 1;
1584 break;
1585 }
1586 for (dv=devlist->next; dv ; dv=dv->next) {
1587 rv = Grow_Add_device(devlist->devname, mdfd,
1588 dv->devname);
1589 if (rv)
1590 break;
1591 }
1592 } else if (s.bitmap_file) {
1593 if (s.size > 0 || s.raiddisks || s.chunk ||
1594 s.layout_str || devs_found > 1) {
1595 pr_err("--bitmap changes cannot be used with other geometry changes in --grow mode\n");
1596 rv = 1;
1597 break;
1598 }
1599 if (c.delay == 0)
1600 c.delay = DEFAULT_BITMAP_DELAY;
1601 rv = Grow_addbitmap(devlist->devname, mdfd, &c, &s);
1602 } else if (grow_continue)
1603 rv = Grow_continue_command(devlist->devname,
1604 mdfd, c.backup_file,
1605 c.verbose);
1606 else if (s.size > 0 || s.raiddisks || s.layout_str ||
1607 s.chunk != 0 || s.level != UnSet ||
1608 data_offset != INVALID_SECTORS) {
1609 rv = Grow_reshape(devlist->devname, mdfd,
1610 devlist->next,
1611 data_offset, &c, &s);
1612 } else if (array_size == 0)
1613 pr_err("no changes to --grow\n");
1614 break;
1615 case INCREMENTAL:
1616 if (rebuild_map) {
1617 RebuildMap();
1618 }
1619 if (c.scan) {
1620 rv = 1;
1621 if (devlist) {
1622 pr_err("In --incremental mode, a device cannot be given with --scan.\n");
1623 break;
1624 }
1625 if (c.runstop <= 0) {
1626 pr_err("--incremental --scan meaningless without --run.\n");
1627 break;
1628 }
1629 if (devmode == 'f') {
1630 pr_err("--incremental --scan --fail not supported.\n");
1631 break;
1632 }
1633 rv = IncrementalScan(&c, NULL);
1634 }
1635 if (!devlist) {
1636 if (!rebuild_map && !c.scan) {
1637 pr_err("--incremental requires a device.\n");
1638 rv = 1;
1639 }
1640 break;
1641 }
1642 if (devmode == 'f') {
1643 if (devlist->next) {
1644 pr_err("'--incremental --fail' can only handle one device.\n");
1645 rv = 1;
1646 break;
1647 }
1648 rv = IncrementalRemove(devlist->devname, remove_path,
1649 c.verbose);
1650 } else
1651 rv = Incremental(devlist, &c, ss);
1652 break;
1653 case AUTODETECT:
1654 autodetect();
1655 break;
1656 }
1657 exit(rv);
1658 }
1659
1660 static int scan_assemble(struct supertype *ss,
1661 struct context *c,
1662 struct mddev_ident *ident)
1663 {
1664 struct mddev_ident *a, *array_list = conf_get_ident(NULL);
1665 struct mddev_dev *devlist = conf_get_devs();
1666 struct map_ent *map = NULL;
1667 int cnt = 0;
1668 int rv = 0;
1669 int failures, successes;
1670
1671 if (conf_verify_devnames(array_list)) {
1672 pr_err("Duplicate MD device names in conf file were found.\n");
1673 return 1;
1674 }
1675 if (devlist == NULL) {
1676 pr_err("No devices listed in conf file were found.\n");
1677 return 1;
1678 }
1679 for (a = array_list; a ; a = a->next) {
1680 a->assembled = 0;
1681 if (a->autof == 0)
1682 a->autof = c->autof;
1683 }
1684 if (map_lock(&map))
1685 pr_err("failed to get exclusive lock on mapfile\n");
1686 do {
1687 failures = 0;
1688 successes = 0;
1689 rv = 0;
1690 for (a = array_list; a ; a = a->next) {
1691 int r;
1692 if (a->assembled)
1693 continue;
1694 if (a->devname &&
1695 strcasecmp(a->devname, "<ignore>") == 0)
1696 continue;
1697
1698 r = Assemble(ss, a->devname,
1699 a, NULL, c);
1700 if (r == 0) {
1701 a->assembled = 1;
1702 successes++;
1703 } else
1704 failures++;
1705 rv |= r;
1706 cnt++;
1707 }
1708 } while (failures && successes);
1709 if (c->homehost && cnt == 0) {
1710 /* Maybe we can auto-assemble something.
1711 * Repeatedly call Assemble in auto-assemble mode
1712 * until it fails
1713 */
1714 int rv2;
1715 int acnt;
1716 ident->autof = c->autof;
1717 do {
1718 struct mddev_dev *devlist = conf_get_devs();
1719 acnt = 0;
1720 do {
1721 rv2 = Assemble(ss, NULL,
1722 ident,
1723 devlist, c);
1724 if (rv2 == 0) {
1725 cnt++;
1726 acnt++;
1727 }
1728 } while (rv2 != 2);
1729 /* Incase there are stacked devices, we need to go around again */
1730 } while (acnt);
1731 if (cnt == 0 && rv == 0) {
1732 pr_err("No arrays found in config file or automatically\n");
1733 rv = 1;
1734 } else if (cnt)
1735 rv = 0;
1736 } else if (cnt == 0 && rv == 0) {
1737 pr_err("No arrays found in config file\n");
1738 rv = 1;
1739 }
1740 map_unlock(&map);
1741 return rv;
1742 }
1743
1744 static int misc_scan(char devmode, struct context *c)
1745 {
1746 /* apply --detail or --wait-clean to
1747 * all devices in /proc/mdstat
1748 */
1749 struct mdstat_ent *ms = mdstat_read(0, 1);
1750 struct mdstat_ent *e;
1751 struct map_ent *map = NULL;
1752 int members;
1753 int rv = 0;
1754
1755 for (members = 0; members <= 1; members++) {
1756 for (e=ms ; e ; e=e->next) {
1757 char *name = NULL;
1758 struct map_ent *me;
1759 struct stat stb;
1760 int member = e->metadata_version &&
1761 strncmp(e->metadata_version,
1762 "external:/", 10) == 0;
1763 if (members != member)
1764 continue;
1765 me = map_by_devnm(&map, e->devnm);
1766 if (me && me->path
1767 && strcmp(me->path, "/unknown") != 0)
1768 name = me->path;
1769 if (name == NULL || stat(name, &stb) != 0)
1770 name = get_md_name(e->devnm);
1771
1772 if (!name) {
1773 pr_err("cannot find device file for %s\n",
1774 e->devnm);
1775 continue;
1776 }
1777 if (devmode == 'D')
1778 rv |= Detail(name, c);
1779 else
1780 rv |= WaitClean(name, -1, c->verbose);
1781 put_md_name(name);
1782 }
1783 }
1784 free_mdstat(ms);
1785 return rv;
1786 }
1787
1788 static int stop_scan(int verbose)
1789 {
1790 /* apply --stop to all devices in /proc/mdstat */
1791 /* Due to possible stacking of devices, repeat until
1792 * nothing more can be stopped
1793 */
1794 int progress=1, err;
1795 int last = 0;
1796 int rv = 0;
1797 do {
1798 struct mdstat_ent *ms = mdstat_read(0, 0);
1799 struct mdstat_ent *e;
1800
1801 if (!progress) last = 1;
1802 progress = 0; err = 0;
1803 for (e=ms ; e ; e=e->next) {
1804 char *name = get_md_name(e->devnm);
1805 int mdfd;
1806
1807 if (!name) {
1808 pr_err("cannot find device file for %s\n",
1809 e->devnm);
1810 continue;
1811 }
1812 mdfd = open_mddev(name, 1);
1813 if (mdfd >= 0) {
1814 if (Manage_stop(name, mdfd, verbose, !last))
1815 err = 1;
1816 else
1817 progress = 1;
1818 close(mdfd);
1819 }
1820
1821 put_md_name(name);
1822 }
1823 free_mdstat(ms);
1824 } while (!last && err);
1825 if (err)
1826 rv |= 1;
1827 return rv;
1828 }
1829
1830 static int misc_list(struct mddev_dev *devlist,
1831 struct mddev_ident *ident,
1832 char *dump_directory,
1833 struct supertype *ss, struct context *c)
1834 {
1835 struct mddev_dev *dv;
1836 int rv = 0;
1837
1838 for (dv=devlist ; dv; dv=(rv & 16) ? NULL : dv->next) {
1839 int mdfd;
1840
1841 switch(dv->disposition) {
1842 case 'D':
1843 rv |= Detail(dv->devname, c);
1844 continue;
1845 case KillOpt: /* Zero superblock */
1846 if (ss)
1847 rv |= Kill(dv->devname, ss, c->force, c->verbose,0);
1848 else {
1849 int v = c->verbose;
1850 do {
1851 rv |= Kill(dv->devname, NULL, c->force, v, 0);
1852 v = -1;
1853 } while (rv == 0);
1854 rv &= ~2;
1855 }
1856 continue;
1857 case 'Q':
1858 rv |= Query(dv->devname); continue;
1859 case 'X':
1860 rv |= ExamineBitmap(dv->devname, c->brief, ss); continue;
1861 case ExamineBB:
1862 rv |= ExamineBadblocks(dv->devname, c->brief, ss); continue;
1863 case 'W':
1864 case WaitOpt:
1865 rv |= Wait(dv->devname); continue;
1866 case Waitclean:
1867 rv |= WaitClean(dv->devname, -1, c->verbose); continue;
1868 case KillSubarray:
1869 rv |= Kill_subarray(dv->devname, c->subarray, c->verbose);
1870 continue;
1871 case UpdateSubarray:
1872 if (c->update == NULL) {
1873 pr_err("-U/--update must be specified with --update-subarray\n");
1874 rv |= 1;
1875 continue;
1876 }
1877 rv |= Update_subarray(dv->devname, c->subarray,
1878 c->update, ident, c->verbose);
1879 continue;
1880 case Dump:
1881 rv |= Dump_metadata(dv->devname, dump_directory, c, ss);
1882 continue;
1883 case Restore:
1884 rv |= Restore_metadata(dv->devname, dump_directory, c, ss,
1885 (dv == devlist && dv->next == NULL));
1886 continue;
1887 case Action:
1888 rv |= SetAction(dv->devname, c->action);
1889 continue;
1890 }
1891 if (dv->devname[0] == '/')
1892 mdfd = open_mddev(dv->devname, 1);
1893 else {
1894 mdfd = open_dev(dv->devname);
1895 if (mdfd < 0)
1896 pr_err("Cannot open %s\n", dv->devname);
1897 }
1898 if (mdfd>=0) {
1899 switch(dv->disposition) {
1900 case 'R':
1901 c->runstop = 1;
1902 rv |= Manage_run(dv->devname, mdfd, c); break;
1903 case 'S':
1904 rv |= Manage_stop(dv->devname, mdfd, c->verbose, 0); break;
1905 case 'o':
1906 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1907 case 'w':
1908 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1909 }
1910 close(mdfd);
1911 } else
1912 rv |= 1;
1913 }
1914 return rv;
1915 }
1916
1917 int SetAction(char *dev, char *action)
1918 {
1919 int fd = open(dev, O_RDONLY);
1920 struct mdinfo mdi;
1921 if (fd < 0) {
1922 pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
1923 return 1;
1924 }
1925 sysfs_init(&mdi, fd, NULL);
1926 close(fd);
1927 if (!mdi.sys_name[0]) {
1928 pr_err("%s is no an md array\n", dev);
1929 return 1;
1930 }
1931
1932 if (sysfs_set_str(&mdi, NULL, "sync_action", action) < 0) {
1933 pr_err("Count not set action for %s to %s: %s\n",
1934 dev, action, strerror(errno));
1935 return 1;
1936 }
1937 return 0;
1938 }