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