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