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