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