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