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