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