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