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