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