]>
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> |
64c4757e NB |
23 | */ |
24 | ||
9a9dab36 | 25 | #include "mdadm.h" |
682c7051 NB |
26 | #include "md_u.h" |
27 | #include "md_p.h" | |
4ccad7b1 | 28 | #include <ctype.h> |
64c4757e | 29 | |
1011e834 N |
30 | #define REGISTER_DEV _IO (MD_MAJOR, 1) |
31 | #define START_MD _IO (MD_MAJOR, 2) | |
32 | #define STOP_MD _IO (MD_MAJOR, 3) | |
82b27616 | 33 | |
64c4757e NB |
34 | int Manage_ro(char *devname, int fd, int readonly) |
35 | { | |
682c7051 NB |
36 | /* switch to readonly or rw |
37 | * | |
38 | * requires >= 0.90.0 | |
39 | * first check that array is runing | |
40 | * use RESTART_ARRAY_RW or STOP_ARRAY_RO | |
41 | * | |
42 | */ | |
43 | mdu_array_info_t array; | |
0e600426 | 44 | #ifndef MDASSEMBLE |
e9dd1598 | 45 | struct mdinfo *mdi; |
0e600426 | 46 | #endif |
b73e45ae | 47 | int rv = 0; |
aba69144 | 48 | |
0e600426 | 49 | #ifndef MDASSEMBLE |
7bd04da9 | 50 | /* If this is an externally-managed array, we need to modify the |
e9dd1598 N |
51 | * metadata_version so that mdmon doesn't undo our change. |
52 | */ | |
4dd2df09 | 53 | mdi = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION); |
e9dd1598 N |
54 | if (mdi && |
55 | mdi->array.major_version == -1 && | |
e9dd1598 N |
56 | is_subarray(mdi->text_version)) { |
57 | char vers[64]; | |
58 | strcpy(vers, "external:"); | |
59 | strcat(vers, mdi->text_version); | |
60 | if (readonly > 0) { | |
61 | int rv; | |
62 | /* We set readonly ourselves. */ | |
63 | vers[9] = '-'; | |
64 | sysfs_set_str(mdi, NULL, "metadata_version", vers); | |
65 | ||
66 | close(fd); | |
67 | rv = sysfs_set_str(mdi, NULL, "array_state", "readonly"); | |
68 | ||
69 | if (rv < 0) { | |
e7b84f9d | 70 | pr_err("failed to set readonly for %s: %s\n", |
e9dd1598 N |
71 | devname, strerror(errno)); |
72 | ||
73 | vers[9] = mdi->text_version[0]; | |
74 | sysfs_set_str(mdi, NULL, "metadata_version", vers); | |
b73e45ae JS |
75 | rv = 1; |
76 | goto out; | |
e9dd1598 N |
77 | } |
78 | } else { | |
79 | char *cp; | |
80 | /* We cannot set read/write - must signal mdmon */ | |
81 | vers[9] = '/'; | |
82 | sysfs_set_str(mdi, NULL, "metadata_version", vers); | |
83 | ||
84 | cp = strchr(vers+10, '/'); | |
1471b8b1 | 85 | if (cp) |
e9dd1598 N |
86 | *cp = 0; |
87 | ping_monitor(vers+10); | |
9ea5a252 DW |
88 | if (mdi->array.level <= 0) |
89 | sysfs_set_str(mdi, NULL, "array_state", "active"); | |
e9dd1598 | 90 | } |
b73e45ae | 91 | goto out; |
e9dd1598 | 92 | } |
0e600426 | 93 | #endif |
9cd39f01 | 94 | if (md_get_array_info(fd, &array)) { |
e7b84f9d | 95 | pr_err("%s does not appear to be active.\n", |
682c7051 | 96 | devname); |
b73e45ae JS |
97 | rv = 1; |
98 | goto out; | |
682c7051 | 99 | } |
aba69144 | 100 | |
7bd04da9 | 101 | if (readonly > 0) { |
682c7051 | 102 | if (ioctl(fd, STOP_ARRAY_RO, NULL)) { |
e7b84f9d | 103 | pr_err("failed to set readonly for %s: %s\n", |
682c7051 | 104 | devname, strerror(errno)); |
b73e45ae JS |
105 | rv = 1; |
106 | goto out; | |
682c7051 NB |
107 | } |
108 | } else if (readonly < 0) { | |
109 | if (ioctl(fd, RESTART_ARRAY_RW, NULL)) { | |
e7b84f9d | 110 | pr_err("failed to set writable for %s: %s\n", |
682c7051 | 111 | devname, strerror(errno)); |
b73e45ae JS |
112 | rv = 1; |
113 | goto out; | |
682c7051 NB |
114 | } |
115 | } | |
b73e45ae JS |
116 | out: |
117 | #ifndef MDASSEMBLE | |
6e8d27e7 | 118 | sysfs_free(mdi); |
b73e45ae JS |
119 | #endif |
120 | return rv; | |
64c4757e NB |
121 | } |
122 | ||
435d4ebb LB |
123 | #ifndef MDASSEMBLE |
124 | ||
4dd2df09 | 125 | static void remove_devices(char *devnm, char *path) |
4ccad7b1 | 126 | { |
7bd04da9 | 127 | /* |
b1702f48 N |
128 | * Remove names at 'path' - possibly with |
129 | * partition suffixes - which link to the 'standard' | |
4dd2df09 | 130 | * name for devnm. These were probably created |
b1702f48 | 131 | * by mdadm when the array was assembled. |
4ccad7b1 N |
132 | */ |
133 | char base[40]; | |
134 | char *path2; | |
135 | char link[1024]; | |
136 | int n; | |
137 | int part; | |
138 | char *be; | |
139 | char *pe; | |
140 | ||
b1702f48 N |
141 | if (!path) |
142 | return; | |
143 | ||
4dd2df09 | 144 | sprintf(base, "/dev/%s", devnm); |
4ccad7b1 | 145 | be = base + strlen(base); |
b1702f48 | 146 | |
503975b9 | 147 | path2 = xmalloc(strlen(path)+20); |
b1702f48 N |
148 | strcpy(path2, path); |
149 | pe = path2 + strlen(path2); | |
7bd04da9 | 150 | |
4ccad7b1 N |
151 | for (part = 0; part < 16; part++) { |
152 | if (part) { | |
153 | sprintf(be, "p%d", part); | |
b1702f48 N |
154 | |
155 | if (isdigit(pe[-1])) | |
156 | sprintf(pe, "p%d", part); | |
157 | else | |
158 | sprintf(pe, "%d", part); | |
4ccad7b1 | 159 | } |
b1702f48 | 160 | n = readlink(path2, link, sizeof(link)); |
d9ca03e9 | 161 | if (n > 0 && (int)strlen(base) == n && |
b1702f48 N |
162 | strncmp(link, base, n) == 0) |
163 | unlink(path2); | |
4ccad7b1 | 164 | } |
0eb26465 | 165 | free(path2); |
4ccad7b1 | 166 | } |
4ccad7b1 | 167 | |
d5a40416 | 168 | int Manage_run(char *devname, int fd, struct context *c) |
64c4757e | 169 | { |
fe7e0e64 N |
170 | /* Run the array. Array must already be configured |
171 | * Requires >= 0.90.0 | |
682c7051 | 172 | */ |
d3786cdc | 173 | char nm[32], *nmp; |
82b27616 | 174 | |
d3786cdc N |
175 | nmp = fd2devnm(fd); |
176 | if (!nmp) { | |
177 | pr_err("Cannot find %s in sysfs!!\n", devname); | |
fe7e0e64 N |
178 | return 1; |
179 | } | |
d3786cdc | 180 | strcpy(nm, nmp); |
d5a40416 | 181 | return IncrementalScan(c, nm); |
fe7e0e64 N |
182 | } |
183 | ||
184 | int Manage_stop(char *devname, int fd, int verbose, int will_retry) | |
185 | { | |
186 | /* Stop the array. Array must already be configured | |
187 | * 'will_retry' means that error messages are not wanted. | |
188 | */ | |
189 | int rv = 0; | |
190 | struct map_ent *map = NULL; | |
191 | struct mdinfo *mdi; | |
192 | char devnm[32]; | |
193 | char container[32]; | |
194 | int err; | |
195 | int count; | |
2eba8496 N |
196 | char buf[32]; |
197 | unsigned long long rd1, rd2; | |
fe7e0e64 | 198 | |
ba728be7 N |
199 | if (will_retry && verbose == 0) |
200 | verbose = -1; | |
201 | ||
fe7e0e64 N |
202 | strcpy(devnm, fd2devnm(fd)); |
203 | /* Get EXCL access first. If this fails, then attempting | |
204 | * to stop is probably a bad idea. | |
205 | */ | |
2eba8496 | 206 | mdi = sysfs_read(fd, NULL, GET_LEVEL|GET_COMPONENT|GET_VERSION); |
fe7e0e64 N |
207 | if (mdi && is_subarray(mdi->text_version)) { |
208 | char *sl; | |
209 | strncpy(container, mdi->text_version+1, sizeof(container)); | |
210 | container[sizeof(container)-1] = 0; | |
211 | sl = strchr(container, '/'); | |
212 | if (sl) | |
213 | *sl = 0; | |
214 | } else | |
215 | container[0] = 0; | |
216 | close(fd); | |
217 | count = 5; | |
52b6ccad | 218 | while (((fd = ((devname[0] == '/') |
fe7e0e64 N |
219 | ?open(devname, O_RDONLY|O_EXCL) |
220 | :open_dev_flags(devnm, O_RDONLY|O_EXCL))) < 0 | |
221 | || strcmp(fd2devnm(fd), devnm) != 0) | |
222 | && container[0] | |
223 | && mdmon_running(container) | |
224 | && count) { | |
932be627 N |
225 | /* Can't open, so something might be wrong. However it |
226 | * is a container, so we might be racing with mdmon, so | |
227 | * retry for a bit. | |
228 | */ | |
fe7e0e64 N |
229 | if (fd >= 0) |
230 | close(fd); | |
231 | flush_mdmon(container); | |
232 | count--; | |
682c7051 | 233 | } |
fe7e0e64 N |
234 | if (fd < 0 || strcmp(fd2devnm(fd), devnm) != 0) { |
235 | if (fd >= 0) | |
236 | close(fd); | |
ba728be7 | 237 | if (verbose >= 0) |
7a862a02 | 238 | pr_err("Cannot get exclusive access to %s:Perhaps a running process, mounted filesystem or active volume group?\n", |
fe7e0e64 N |
239 | devname); |
240 | return 1; | |
241 | } | |
932be627 N |
242 | /* If this is an mdmon managed array, just write 'inactive' |
243 | * to the array state and let mdmon clear up. | |
244 | */ | |
fe7e0e64 N |
245 | if (mdi && |
246 | mdi->array.level > 0 && | |
247 | is_subarray(mdi->text_version)) { | |
eb0af526 | 248 | int err; |
fe7e0e64 | 249 | /* This is mdmon managed. */ |
eb0af526 | 250 | close(fd); |
daf7a3ce | 251 | |
932be627 | 252 | /* As we had an O_EXCL open, any use of the device |
eb0af526 N |
253 | * which blocks STOP_ARRAY is probably a transient use, |
254 | * so it is reasonable to retry for a while - 5 seconds. | |
255 | */ | |
fe7e0e64 N |
256 | count = 25; |
257 | while (count && | |
258 | (err = sysfs_set_str(mdi, NULL, | |
259 | "array_state", | |
260 | "inactive")) < 0 | |
eb0af526 N |
261 | && errno == EBUSY) { |
262 | usleep(200000); | |
fe7e0e64 | 263 | count--; |
eb0af526 | 264 | } |
fe7e0e64 N |
265 | if (err) { |
266 | if (verbose >= 0) | |
e7b84f9d N |
267 | pr_err("failed to stop array %s: %s\n", |
268 | devname, strerror(errno)); | |
bccd8153 JS |
269 | rv = 1; |
270 | goto out; | |
682c7051 | 271 | } |
fe7e0e64 N |
272 | |
273 | /* Give monitor a chance to act */ | |
274 | ping_monitor(mdi->text_version); | |
275 | ||
276 | fd = open_dev_excl(devnm); | |
277 | if (fd < 0) { | |
278 | if (verbose >= 0) | |
7a862a02 | 279 | pr_err("failed to completely stop %s: Device is busy\n", |
fe7e0e64 N |
280 | devname); |
281 | rv = 1; | |
282 | goto out; | |
283 | } | |
284 | } else if (mdi && | |
285 | mdi->array.major_version == -1 && | |
286 | mdi->array.minor_version == -2 && | |
287 | !is_subarray(mdi->text_version)) { | |
288 | struct mdstat_ent *mds, *m; | |
289 | /* container, possibly mdmon-managed. | |
290 | * Make sure mdmon isn't opening it, which | |
291 | * would interfere with the 'stop' | |
97590376 | 292 | */ |
fe7e0e64 | 293 | ping_monitor(mdi->sys_name); |
daf7a3ce | 294 | |
fe7e0e64 N |
295 | /* now check that there are no existing arrays |
296 | * which are members of this array | |
297 | */ | |
298 | mds = mdstat_read(0, 0); | |
299 | for (m = mds; m; m = m->next) | |
300 | if (m->metadata_version && | |
301 | strncmp(m->metadata_version, "external:", 9)==0 && | |
302 | metadata_container_matches(m->metadata_version+9, | |
303 | devnm)) { | |
304 | if (verbose >= 0) | |
7a862a02 | 305 | pr_err("Cannot stop container %s: member %s still active\n", |
9581efb1 | 306 | devname, m->devnm); |
fe7e0e64 N |
307 | free_mdstat(mds); |
308 | rv = 1; | |
309 | goto out; | |
310 | } | |
311 | } | |
312 | ||
2eba8496 N |
313 | /* If the array is undergoing a reshape which changes the number |
314 | * of devices, then it would be nice to stop it at a point where | |
315 | * it has completed a full number of stripes in both old and | |
316 | * new layouts as this will allow the reshape to be reverted. | |
317 | * So if 'sync_action' is "reshape" and 'raid_disks' shows two | |
318 | * different numbers, then | |
319 | * - freeze reshape | |
320 | * - set sync_max to next multiple of both data_disks and | |
321 | * chunk sizes (or next but one) | |
322 | * - unfreeze reshape | |
323 | * - wait on 'sync_completed' for that point to be reached. | |
324 | */ | |
325 | if (mdi && (mdi->array.level >= 4 && mdi->array.level <= 6) && | |
326 | sysfs_attribute_available(mdi, NULL, "sync_action") && | |
327 | sysfs_attribute_available(mdi, NULL, "reshape_direction") && | |
328 | sysfs_get_str(mdi, NULL, "sync_action", buf, 20) > 0 && | |
329 | strcmp(buf, "reshape\n") == 0 && | |
e3e0d0a8 | 330 | sysfs_get_two(mdi, NULL, "raid_disks", &rd1, &rd2) == 2) { |
2eba8496 N |
331 | unsigned long long position, curr; |
332 | unsigned long long chunk1, chunk2; | |
333 | unsigned long long rddiv, chunkdiv; | |
334 | unsigned long long sectors; | |
5509dc44 N |
335 | unsigned long long sync_max, old_sync_max; |
336 | unsigned long long completed; | |
2eba8496 N |
337 | int backwards = 0; |
338 | int delay; | |
339 | int scfd; | |
340 | ||
e3e0d0a8 N |
341 | delay = 40; |
342 | while (rd1 > rd2 && delay > 0 && | |
343 | sysfs_get_ll(mdi, NULL, "sync_max", &old_sync_max) == 0) { | |
344 | /* must be in the critical section - wait a bit */ | |
345 | delay -= 1; | |
346 | usleep(100000); | |
347 | } | |
348 | ||
349 | if (sysfs_set_str(mdi, NULL, "sync_action", "frozen") != 0) | |
350 | goto done; | |
351 | /* Array is frozen */ | |
352 | ||
2eba8496 N |
353 | rd1 -= mdi->array.level == 6 ? 2 : 1; |
354 | rd2 -= mdi->array.level == 6 ? 2 : 1; | |
355 | sysfs_get_str(mdi, NULL, "reshape_direction", buf, sizeof(buf)); | |
356 | if (strncmp(buf, "back", 4) == 0) | |
357 | backwards = 1; | |
e3e0d0a8 N |
358 | if (sysfs_get_ll(mdi, NULL, "reshape_position", &position) != 0) { |
359 | /* reshape must have finished now */ | |
360 | sysfs_set_str(mdi, NULL, "sync_action", "idle"); | |
361 | goto done; | |
362 | } | |
2eba8496 N |
363 | sysfs_get_two(mdi, NULL, "chunk_size", &chunk1, &chunk2); |
364 | chunk1 /= 512; | |
365 | chunk2 /= 512; | |
366 | rddiv = GCD(rd1, rd2); | |
367 | chunkdiv = GCD(chunk1, chunk2); | |
368 | sectors = (chunk1/chunkdiv) * chunk2 * (rd1/rddiv) * rd2; | |
369 | ||
370 | if (backwards) { | |
371 | /* Need to subtract 'reshape_position' from | |
372 | * array size to get equivalent of sync_max. | |
373 | * Size calculation based on raid5_size in kernel. | |
374 | */ | |
375 | unsigned long long size = mdi->component_size; | |
376 | size &= ~(chunk1-1); | |
377 | size &= ~(chunk2-1); | |
378 | /* rd1 must be smaller */ | |
932be627 N |
379 | /* Reshape may have progressed further backwards than |
380 | * recorded, so target even further back (hence "-1") | |
381 | */ | |
5509dc44 | 382 | position = (position / sectors - 1) * sectors; |
932be627 N |
383 | /* rd1 is always the conversion factor between 'sync' |
384 | * position and 'reshape' position. | |
385 | * We read 1 "new" stripe worth of data from where-ever, | |
386 | * and when write out that full stripe. | |
387 | */ | |
5509dc44 | 388 | sync_max = size - position/rd1; |
2eba8496 | 389 | } else { |
932be627 N |
390 | /* Reshape will very likely be beyond position, and it may |
391 | * be too late to stop at '+1', so aim for '+2' | |
392 | */ | |
5509dc44 N |
393 | position = (position / sectors + 2) * sectors; |
394 | sync_max = position/rd1; | |
2eba8496 | 395 | } |
5509dc44 N |
396 | if (sysfs_get_ll(mdi, NULL, "sync_max", &old_sync_max) < 0) |
397 | old_sync_max = mdi->component_size; | |
398 | /* Must not advance sync_max as that could confuse | |
399 | * the reshape monitor */ | |
400 | if (sync_max < old_sync_max) | |
401 | sysfs_set_num(mdi, NULL, "sync_max", sync_max); | |
2eba8496 N |
402 | sysfs_set_str(mdi, NULL, "sync_action", "idle"); |
403 | ||
404 | /* That should have set things going again. Now we | |
3afaff93 | 405 | * wait a little while (3 second max) for sync_completed |
2eba8496 | 406 | * to reach the target. |
3afaff93 N |
407 | * The reshape process can block for 500msec if |
408 | * the sync speed limit is hit, so we need to wait | |
409 | * a lot longer than that. 1 second is usually | |
410 | * enough. 3 is safe. | |
2eba8496 | 411 | */ |
3afaff93 | 412 | delay = 3000; |
2eba8496 | 413 | scfd = sysfs_open(mdi->sys_name, NULL, "sync_completed"); |
3afaff93 | 414 | while (scfd >= 0 && delay > 0 && old_sync_max > 0) { |
30ddba7d | 415 | unsigned long long max_completed; |
5509dc44 | 416 | sysfs_get_ll(mdi, NULL, "reshape_position", &curr); |
2eba8496 | 417 | sysfs_fd_get_str(scfd, buf, sizeof(buf)); |
3afaff93 N |
418 | if (strncmp(buf, "none", 4) == 0) { |
419 | /* Either reshape has aborted, or hasn't | |
420 | * quite started yet. Wait a bit and | |
421 | * check 'sync_action' to see. | |
422 | */ | |
423 | usleep(10000); | |
424 | sysfs_get_str(mdi, NULL, "sync_action", buf, sizeof(buf)); | |
425 | if (strncmp(buf, "reshape", 7) != 0) | |
426 | break; | |
427 | } | |
5509dc44 | 428 | |
30ddba7d N |
429 | if (sysfs_fd_get_two(scfd, &completed, |
430 | &max_completed) == 2 && | |
431 | /* 'completed' sometimes reads as max-uulong */ | |
432 | completed < max_completed && | |
5509dc44 N |
433 | (completed > sync_max || |
434 | (completed == sync_max && curr != position))) { | |
435 | while (completed > sync_max) { | |
436 | sync_max += sectors / rd1; | |
437 | if (backwards) | |
438 | position -= sectors; | |
439 | else | |
440 | position += sectors; | |
441 | } | |
442 | if (sync_max < old_sync_max) | |
443 | sysfs_set_num(mdi, NULL, "sync_max", sync_max); | |
444 | } | |
445 | ||
2eba8496 N |
446 | if (!backwards && curr >= position) |
447 | break; | |
448 | if (backwards && curr <= position) | |
449 | break; | |
450 | sysfs_wait(scfd, &delay); | |
451 | } | |
452 | if (scfd >= 0) | |
453 | close(scfd); | |
454 | ||
455 | } | |
e3e0d0a8 | 456 | done: |
2eba8496 | 457 | |
fe7e0e64 N |
458 | /* As we have an O_EXCL open, any use of the device |
459 | * which blocks STOP_ARRAY is probably a transient use, | |
460 | * so it is reasonable to retry for a while - 5 seconds. | |
461 | */ | |
462 | count = 25; err = 0; | |
463 | while (count && fd >= 0 | |
464 | && (err = ioctl(fd, STOP_ARRAY, NULL)) < 0 | |
465 | && errno == EBUSY) { | |
466 | usleep(200000); | |
467 | count --; | |
468 | } | |
469 | if (fd >= 0 && err) { | |
470 | if (verbose >= 0) { | |
471 | pr_err("failed to stop array %s: %s\n", | |
472 | devname, strerror(errno)); | |
473 | if (errno == EBUSY) | |
7a862a02 | 474 | cont_err("Perhaps a running process, mounted filesystem or active volume group?\n"); |
4ccad7b1 | 475 | } |
fe7e0e64 N |
476 | rv = 1; |
477 | goto out; | |
478 | } | |
229e66cb JS |
479 | |
480 | if (get_linux_version() < 2006028) { | |
481 | /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array | |
482 | * was stopped, so We'll do it here just to be sure. Drop any | |
483 | * partitions as well... | |
484 | */ | |
485 | if (fd >= 0) | |
486 | ioctl(fd, BLKRRPART, 0); | |
487 | if (mdi) | |
488 | sysfs_uevent(mdi, "change"); | |
489 | } | |
4ccad7b1 | 490 | |
fe7e0e64 N |
491 | if (devnm[0] && use_udev()) { |
492 | struct map_ent *mp = map_by_devnm(&map, devnm); | |
493 | remove_devices(devnm, mp ? mp->path : NULL); | |
682c7051 | 494 | } |
fe7e0e64 N |
495 | |
496 | if (verbose >= 0) | |
497 | pr_err("stopped %s\n", devname); | |
498 | map_lock(&map); | |
499 | map_remove(&map, devnm); | |
500 | map_unlock(&map); | |
501 | out: | |
6e8d27e7 | 502 | sysfs_free(mdi); |
fe7e0e64 | 503 | |
bccd8153 | 504 | return rv; |
64c4757e NB |
505 | } |
506 | ||
5e73b024 N |
507 | static struct mddev_dev *add_one(struct mddev_dev *dv, char *name, char disp) |
508 | { | |
509 | struct mddev_dev *new; | |
510 | new = xmalloc(sizeof(*new)); | |
511 | memset(new, 0, sizeof(*new)); | |
512 | new->devname = xstrdup(name); | |
513 | new->disposition = disp; | |
514 | new->next = dv->next; | |
515 | dv->next = new; | |
516 | return new; | |
517 | } | |
518 | ||
1d997643 N |
519 | static void add_faulty(struct mddev_dev *dv, int fd, char disp) |
520 | { | |
521 | mdu_array_info_t array; | |
522 | mdu_disk_info_t disk; | |
523 | int remaining_disks; | |
524 | int i; | |
525 | ||
9cd39f01 | 526 | if (md_get_array_info(fd, &array) != 0) |
1d997643 N |
527 | return; |
528 | ||
529 | remaining_disks = array.nr_disks; | |
530 | for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) { | |
1d997643 N |
531 | char buf[40]; |
532 | disk.number = i; | |
d97572f5 | 533 | if (md_get_disk_info(fd, &disk) != 0) |
1d997643 N |
534 | continue; |
535 | if (disk.major == 0 && disk.minor == 0) | |
536 | continue; | |
537 | remaining_disks--; | |
538 | if ((disk.state & 1) == 0) /* not faulty */ | |
539 | continue; | |
540 | sprintf(buf, "%d:%d", disk.major, disk.minor); | |
5e73b024 | 541 | dv = add_one(dv, buf, disp); |
1d997643 N |
542 | } |
543 | } | |
544 | ||
545 | static void add_detached(struct mddev_dev *dv, int fd, char disp) | |
546 | { | |
547 | mdu_array_info_t array; | |
548 | mdu_disk_info_t disk; | |
549 | int remaining_disks; | |
550 | int i; | |
551 | ||
9cd39f01 | 552 | if (md_get_array_info(fd, &array) != 0) |
1d997643 N |
553 | return; |
554 | ||
555 | remaining_disks = array.nr_disks; | |
556 | for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) { | |
1d997643 N |
557 | char buf[40]; |
558 | int sfd; | |
559 | disk.number = i; | |
d97572f5 | 560 | if (md_get_disk_info(fd, &disk) != 0) |
1d997643 N |
561 | continue; |
562 | if (disk.major == 0 && disk.minor == 0) | |
563 | continue; | |
564 | remaining_disks--; | |
565 | if (disp == 'f' && (disk.state & 1) != 0) /* already faulty */ | |
566 | continue; | |
567 | sprintf(buf, "%d:%d", disk.major, disk.minor); | |
568 | sfd = dev_open(buf, O_RDONLY); | |
569 | if (sfd >= 0) { | |
570 | /* Not detached */ | |
571 | close(sfd); | |
572 | continue; | |
573 | } | |
574 | if (errno != ENXIO) | |
575 | /* Probably not detached */ | |
576 | continue; | |
5e73b024 | 577 | dv = add_one(dv, buf, disp); |
1d997643 N |
578 | } |
579 | } | |
580 | ||
64a78416 N |
581 | static void add_set(struct mddev_dev *dv, int fd, char set_char) |
582 | { | |
583 | mdu_array_info_t array; | |
584 | mdu_disk_info_t disk; | |
585 | int remaining_disks; | |
586 | int copies, set; | |
587 | int i; | |
588 | ||
9cd39f01 | 589 | if (md_get_array_info(fd, &array) != 0) |
64a78416 N |
590 | return; |
591 | if (array.level != 10) | |
592 | return; | |
593 | copies = ((array.layout & 0xff) * | |
594 | ((array.layout >> 8) & 0xff)); | |
595 | if (array.raid_disks % copies) | |
596 | return; | |
597 | ||
598 | remaining_disks = array.nr_disks; | |
599 | for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) { | |
600 | char buf[40]; | |
601 | disk.number = i; | |
d97572f5 | 602 | if (md_get_disk_info(fd, &disk) != 0) |
64a78416 N |
603 | continue; |
604 | if (disk.major == 0 && disk.minor == 0) | |
605 | continue; | |
606 | remaining_disks--; | |
607 | set = disk.raid_disk % copies; | |
608 | if (set_char != set + 'A') | |
609 | continue; | |
610 | sprintf(buf, "%d:%d", disk.major, disk.minor); | |
611 | dv = add_one(dv, buf, dv->disposition); | |
612 | } | |
613 | } | |
614 | ||
abe94694 N |
615 | int attempt_re_add(int fd, int tfd, struct mddev_dev *dv, |
616 | struct supertype *dev_st, struct supertype *tst, | |
617 | unsigned long rdev, | |
618 | char *update, char *devname, int verbose, | |
619 | mdu_array_info_t *array) | |
620 | { | |
621 | struct mdinfo mdi; | |
622 | int duuid[4]; | |
623 | int ouuid[4]; | |
624 | ||
625 | dev_st->ss->getinfo_super(dev_st, &mdi, NULL); | |
626 | dev_st->ss->uuid_from_super(dev_st, ouuid); | |
627 | if (tst->sb) | |
628 | tst->ss->uuid_from_super(tst, duuid); | |
629 | else | |
630 | /* Assume uuid matches: kernel will check */ | |
631 | memcpy(duuid, ouuid, sizeof(ouuid)); | |
632 | if ((mdi.disk.state & (1<<MD_DISK_ACTIVE)) && | |
633 | !(mdi.disk.state & (1<<MD_DISK_FAULTY)) && | |
634 | memcmp(duuid, ouuid, sizeof(ouuid))==0) { | |
635 | /* Looks like it is worth a | |
636 | * try. Need to make sure | |
637 | * kernel will accept it | |
638 | * though. | |
639 | */ | |
640 | mdu_disk_info_t disc; | |
641 | /* re-add doesn't work for version-1 superblocks | |
642 | * before 2.6.18 :-( | |
643 | */ | |
644 | if (array->major_version == 1 && | |
645 | get_linux_version() <= 2006018) | |
646 | goto skip_re_add; | |
647 | disc.number = mdi.disk.number; | |
d97572f5 JS |
648 | if (md_get_disk_info(fd, &disc) != 0 || |
649 | disc.major != 0 || disc.minor != 0) | |
abe94694 N |
650 | goto skip_re_add; |
651 | disc.major = major(rdev); | |
652 | disc.minor = minor(rdev); | |
653 | disc.number = mdi.disk.number; | |
654 | disc.raid_disk = mdi.disk.raid_disk; | |
655 | disc.state = mdi.disk.state; | |
bff96f73 GJ |
656 | if (array->state & (1 << MD_SB_CLUSTERED)) { |
657 | /* extra flags are needed when adding to a cluster as | |
658 | * there are two cases to distinguish | |
659 | */ | |
660 | if (dv->disposition == 'c') | |
661 | disc.state |= (1 << MD_DISK_CANDIDATE); | |
662 | else | |
663 | disc.state |= (1 << MD_DISK_CLUSTER_ADD); | |
664 | } | |
e22fe3ae | 665 | if (dv->writemostly == FlagSet) |
abe94694 | 666 | disc.state |= 1 << MD_DISK_WRITEMOSTLY; |
e22fe3ae | 667 | if (dv->writemostly == FlagClear) |
abe94694 | 668 | disc.state &= ~(1 << MD_DISK_WRITEMOSTLY); |
e22fe3ae | 669 | if (dv->failfast == FlagSet) |
71574efb | 670 | disc.state |= 1 << MD_DISK_FAILFAST; |
e22fe3ae | 671 | if (dv->failfast == FlagClear) |
71574efb | 672 | disc.state &= ~(1 << MD_DISK_FAILFAST); |
abe94694 | 673 | remove_partitions(tfd); |
e22fe3ae N |
674 | if (update || dv->writemostly != FlagDefault |
675 | || dv->failfast != FlagDefault) { | |
abe94694 N |
676 | int rv = -1; |
677 | tfd = dev_open(dv->devname, O_RDWR); | |
678 | if (tfd < 0) { | |
7a862a02 | 679 | pr_err("failed to open %s for superblock update during re-add\n", dv->devname); |
abe94694 N |
680 | return -1; |
681 | } | |
682 | ||
e22fe3ae | 683 | if (dv->writemostly == FlagSet) |
abe94694 N |
684 | rv = dev_st->ss->update_super( |
685 | dev_st, NULL, "writemostly", | |
686 | devname, verbose, 0, NULL); | |
e22fe3ae | 687 | if (dv->writemostly == FlagClear) |
abe94694 N |
688 | rv = dev_st->ss->update_super( |
689 | dev_st, NULL, "readwrite", | |
690 | devname, verbose, 0, NULL); | |
e22fe3ae | 691 | if (dv->failfast == FlagSet) |
71574efb N |
692 | rv = dev_st->ss->update_super( |
693 | dev_st, NULL, "failfast", | |
694 | devname, verbose, 0, NULL); | |
e22fe3ae | 695 | if (dv->failfast == FlagClear) |
71574efb N |
696 | rv = dev_st->ss->update_super( |
697 | dev_st, NULL, "nofailfast", | |
698 | devname, verbose, 0, NULL); | |
abe94694 N |
699 | if (update) |
700 | rv = dev_st->ss->update_super( | |
701 | dev_st, NULL, update, | |
702 | devname, verbose, 0, NULL); | |
703 | if (rv == 0) | |
704 | rv = dev_st->ss->store_super(dev_st, tfd); | |
705 | close(tfd); | |
706 | if (rv != 0) { | |
7a862a02 | 707 | pr_err("failed to update superblock during re-add\n"); |
abe94694 N |
708 | return -1; |
709 | } | |
710 | } | |
711 | /* don't even try if disk is marked as faulty */ | |
712 | errno = 0; | |
713 | if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) { | |
714 | if (verbose >= 0) | |
715 | pr_err("re-added %s\n", dv->devname); | |
716 | return 1; | |
717 | } | |
718 | if (errno == ENOMEM || errno == EROFS) { | |
719 | pr_err("add new device failed for %s: %s\n", | |
720 | dv->devname, strerror(errno)); | |
721 | if (dv->disposition == 'M') | |
722 | return 0; | |
723 | return -1; | |
724 | } | |
725 | } | |
726 | skip_re_add: | |
727 | return 0; | |
728 | } | |
729 | ||
38aeaf3a N |
730 | int Manage_add(int fd, int tfd, struct mddev_dev *dv, |
731 | struct supertype *tst, mdu_array_info_t *array, | |
732 | int force, int verbose, char *devname, | |
4de90913 GJ |
733 | char *update, unsigned long rdev, unsigned long long array_size, |
734 | int raid_slot) | |
38aeaf3a N |
735 | { |
736 | unsigned long long ldsize; | |
fbd3e15c | 737 | struct supertype *dev_st; |
38aeaf3a N |
738 | int j; |
739 | mdu_disk_info_t disc; | |
740 | ||
741 | if (!get_dev_size(tfd, dv->devname, &ldsize)) { | |
742 | if (dv->disposition == 'M') | |
743 | return 0; | |
744 | else | |
745 | return -1; | |
746 | } | |
747 | ||
632dc30c | 748 | if (tst->ss == &super0 && ldsize > 4ULL*1024*1024*1024*1024) { |
7ccc4cc4 | 749 | /* More than 4TB is wasted on v0.90 */ |
38aeaf3a | 750 | if (!force) { |
7a862a02 N |
751 | pr_err("%s is larger than %s can effectively use.\n" |
752 | " Add --force is you really want to add this device.\n", | |
38aeaf3a N |
753 | dv->devname, devname); |
754 | return -1; | |
755 | } | |
7a862a02 N |
756 | pr_err("%s is larger than %s can effectively use.\n" |
757 | " Adding anyway as --force was given.\n", | |
38aeaf3a N |
758 | dv->devname, devname); |
759 | } | |
091e8e6e | 760 | if (!tst->ss->external && array->major_version == 0) { |
38aeaf3a N |
761 | if (ioctl(fd, HOT_ADD_DISK, rdev)==0) { |
762 | if (verbose >= 0) | |
763 | pr_err("hot added %s\n", | |
764 | dv->devname); | |
765 | return 1; | |
766 | } | |
767 | ||
768 | pr_err("hot add failed for %s: %s\n", | |
769 | dv->devname, strerror(errno)); | |
770 | return -1; | |
771 | } | |
772 | ||
773 | if (array->not_persistent == 0 || tst->ss->external) { | |
774 | ||
775 | /* need to find a sample superblock to copy, and | |
776 | * a spare slot to use. | |
777 | * For 'external' array (well, container based), | |
778 | * We can just load the metadata for the array-> | |
779 | */ | |
780 | int array_failed; | |
781 | if (tst->sb) | |
782 | /* already loaded */; | |
783 | else if (tst->ss->external) { | |
784 | tst->ss->load_container(tst, fd, NULL); | |
785 | } else for (j = 0; j < tst->max_devs; j++) { | |
786 | char *dev; | |
787 | int dfd; | |
788 | disc.number = j; | |
d97572f5 | 789 | if (md_get_disk_info(fd, &disc)) |
38aeaf3a N |
790 | continue; |
791 | if (disc.major==0 && disc.minor==0) | |
792 | continue; | |
793 | if ((disc.state & 4)==0) /* sync */ | |
794 | continue; | |
795 | /* Looks like a good device to try */ | |
796 | dev = map_dev(disc.major, disc.minor, 1); | |
797 | if (!dev) | |
798 | continue; | |
799 | dfd = dev_open(dev, O_RDONLY); | |
800 | if (dfd < 0) | |
801 | continue; | |
802 | if (tst->ss->load_super(tst, dfd, | |
803 | NULL)) { | |
804 | close(dfd); | |
805 | continue; | |
806 | } | |
807 | close(dfd); | |
808 | break; | |
809 | } | |
810 | /* FIXME this is a bad test to be using */ | |
f33a71f1 N |
811 | if (!tst->sb && (dv->disposition != 'a' |
812 | && dv->disposition != 'S')) { | |
38aeaf3a N |
813 | /* we are re-adding a device to a |
814 | * completely dead array - have to depend | |
815 | * on kernel to check | |
816 | */ | |
817 | } else if (!tst->sb) { | |
818 | pr_err("cannot load array metadata from %s\n", devname); | |
819 | return -1; | |
820 | } | |
821 | ||
822 | /* Make sure device is large enough */ | |
01290056 SL |
823 | if (dv->disposition != 'j' && /* skip size check for Journal */ |
824 | tst->sb && | |
2609f339 | 825 | tst->ss->avail_size(tst, ldsize/512, INVALID_SECTORS) < |
38aeaf3a N |
826 | array_size) { |
827 | if (dv->disposition == 'M') | |
828 | return 0; | |
829 | pr_err("%s not large enough to join array\n", | |
830 | dv->devname); | |
831 | return -1; | |
832 | } | |
833 | ||
834 | /* Possibly this device was recently part of | |
835 | * the array and was temporarily removed, and | |
836 | * is now being re-added. If so, we can | |
837 | * simply re-add it. | |
838 | */ | |
839 | ||
fbd3e15c | 840 | if (array->not_persistent == 0) { |
38aeaf3a N |
841 | dev_st = dup_super(tst); |
842 | dev_st->ss->load_super(dev_st, tfd, NULL); | |
fbd3e15c JS |
843 | if (dev_st->sb && dv->disposition != 'S') { |
844 | int rv; | |
845 | ||
846 | rv = attempt_re_add(fd, tfd, dv, dev_st, tst, | |
847 | rdev, update, devname, | |
848 | verbose, array); | |
849 | dev_st->ss->free_super(dev_st); | |
850 | if (rv) | |
851 | return rv; | |
852 | } | |
38aeaf3a N |
853 | } |
854 | if (dv->disposition == 'M') { | |
855 | if (verbose > 0) | |
856 | pr_err("--re-add for %s to %s is not possible\n", | |
857 | dv->devname, devname); | |
858 | return 0; | |
859 | } | |
860 | if (dv->disposition == 'A') { | |
861 | pr_err("--re-add for %s to %s is not possible\n", | |
862 | dv->devname, devname); | |
863 | return -1; | |
864 | } | |
865 | if (array->active_disks < array->raid_disks) { | |
866 | char *avail = xcalloc(array->raid_disks, 1); | |
867 | int d; | |
868 | int found = 0; | |
869 | ||
d180d2aa | 870 | for (d = 0; d < MAX_DISKS && found < array->nr_disks; d++) { |
38aeaf3a | 871 | disc.number = d; |
d97572f5 | 872 | if (md_get_disk_info(fd, &disc)) |
38aeaf3a N |
873 | continue; |
874 | if (disc.major == 0 && disc.minor == 0) | |
875 | continue; | |
876 | if (!(disc.state & (1<<MD_DISK_SYNC))) | |
877 | continue; | |
878 | avail[disc.raid_disk] = 1; | |
d31d0f52 | 879 | found++; |
38aeaf3a N |
880 | } |
881 | array_failed = !enough(array->level, array->raid_disks, | |
882 | array->layout, 1, avail); | |
6157951f | 883 | free(avail); |
38aeaf3a N |
884 | } else |
885 | array_failed = 0; | |
886 | if (array_failed) { | |
887 | pr_err("%s has failed so using --add cannot work and might destroy\n", | |
888 | devname); | |
889 | pr_err("data on %s. You should stop the array and re-assemble it.\n", | |
890 | dv->devname); | |
891 | return -1; | |
892 | } | |
893 | } else { | |
894 | /* non-persistent. Must ensure that new drive | |
895 | * is at least array->size big. | |
896 | */ | |
897 | if (ldsize/512 < array_size) { | |
898 | pr_err("%s not large enough to join array\n", | |
899 | dv->devname); | |
900 | return -1; | |
901 | } | |
902 | } | |
903 | /* committed to really trying this device now*/ | |
904 | remove_partitions(tfd); | |
905 | ||
906 | /* in 2.6.17 and earlier, version-1 superblocks won't | |
907 | * use the number we write, but will choose a free number. | |
908 | * we must choose the same free number, which requires | |
909 | * starting at 'raid_disks' and counting up | |
910 | */ | |
911 | for (j = array->raid_disks; j < tst->max_devs; j++) { | |
912 | disc.number = j; | |
d97572f5 | 913 | if (md_get_disk_info(fd, &disc)) |
38aeaf3a N |
914 | break; |
915 | if (disc.major==0 && disc.minor==0) | |
916 | break; | |
917 | if (disc.state & 8) /* removed */ | |
918 | break; | |
919 | } | |
920 | disc.major = major(rdev); | |
921 | disc.minor = minor(rdev); | |
4de90913 GJ |
922 | if (raid_slot < 0) |
923 | disc.number = j; | |
924 | else | |
925 | disc.number = raid_slot; | |
38aeaf3a | 926 | disc.state = 0; |
01290056 SL |
927 | |
928 | /* only add journal to array that supports journaling */ | |
929 | if (dv->disposition == 'j') { | |
930 | struct mdinfo mdi; | |
931 | struct mdinfo *mdp; | |
932 | ||
933 | mdp = sysfs_read(fd, NULL, GET_ARRAY_STATE); | |
2a1990c0 JS |
934 | if (!mdp) { |
935 | pr_err("%s unable to read array state.\n", devname); | |
936 | return -1; | |
937 | } | |
01290056 SL |
938 | |
939 | if (strncmp(mdp->sysfs_array_state, "readonly", 8) != 0) { | |
d209181d | 940 | sysfs_free(mdp); |
01290056 SL |
941 | pr_err("%s is not readonly, cannot add journal.\n", devname); |
942 | return -1; | |
943 | } | |
944 | ||
d209181d JS |
945 | sysfs_free(mdp); |
946 | ||
01290056 SL |
947 | tst->ss->getinfo_super(tst, &mdi, NULL); |
948 | if (mdi.journal_device_required == 0) { | |
949 | pr_err("%s does not support journal device.\n", devname); | |
950 | return -1; | |
951 | } | |
38c2e05b | 952 | disc.raid_disk = 0; |
01290056 SL |
953 | } |
954 | ||
38aeaf3a N |
955 | if (array->not_persistent==0) { |
956 | int dfd; | |
01290056 SL |
957 | if (dv->disposition == 'j') |
958 | disc.state |= (1 << MD_DISK_JOURNAL) | (1 << MD_DISK_SYNC); | |
e22fe3ae | 959 | if (dv->writemostly == FlagSet) |
38aeaf3a | 960 | disc.state |= 1 << MD_DISK_WRITEMOSTLY; |
e22fe3ae | 961 | if (dv->failfast == FlagSet) |
71574efb | 962 | disc.state |= 1 << MD_DISK_FAILFAST; |
38aeaf3a N |
963 | dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT); |
964 | if (tst->ss->add_to_super(tst, &disc, dfd, | |
72ca9bcf | 965 | dv->devname, INVALID_SECTORS)) |
38aeaf3a N |
966 | return -1; |
967 | if (tst->ss->write_init_super(tst)) | |
968 | return -1; | |
969 | } else if (dv->disposition == 'A') { | |
970 | /* this had better be raid1. | |
971 | * As we are "--re-add"ing we must find a spare slot | |
972 | * to fill. | |
973 | */ | |
974 | char *used = xcalloc(array->raid_disks, 1); | |
975 | for (j = 0; j < tst->max_devs; j++) { | |
976 | mdu_disk_info_t disc2; | |
977 | disc2.number = j; | |
d97572f5 | 978 | if (md_get_disk_info(fd, &disc2)) |
38aeaf3a N |
979 | continue; |
980 | if (disc2.major==0 && disc2.minor==0) | |
981 | continue; | |
982 | if (disc2.state & 8) /* removed */ | |
983 | continue; | |
984 | if (disc2.raid_disk < 0) | |
985 | continue; | |
986 | if (disc2.raid_disk > array->raid_disks) | |
987 | continue; | |
988 | used[disc2.raid_disk] = 1; | |
989 | } | |
990 | for (j = 0 ; j < array->raid_disks; j++) | |
991 | if (!used[j]) { | |
992 | disc.raid_disk = j; | |
993 | disc.state |= (1<<MD_DISK_SYNC); | |
994 | break; | |
995 | } | |
996 | free(used); | |
997 | } | |
4de90913 GJ |
998 | |
999 | if (array->state & (1 << MD_SB_CLUSTERED)) { | |
1000 | if (dv->disposition == 'c') | |
1001 | disc.state |= (1 << MD_DISK_CANDIDATE); | |
1002 | else | |
1003 | disc.state |= (1 << MD_DISK_CLUSTER_ADD); | |
1004 | } | |
1005 | ||
e22fe3ae | 1006 | if (dv->writemostly == FlagSet) |
38aeaf3a | 1007 | disc.state |= (1 << MD_DISK_WRITEMOSTLY); |
e22fe3ae | 1008 | if (dv->failfast == FlagSet) |
71574efb | 1009 | disc.state |= (1 << MD_DISK_FAILFAST); |
38aeaf3a N |
1010 | if (tst->ss->external) { |
1011 | /* add a disk | |
1012 | * to an external metadata container */ | |
1013 | struct mdinfo new_mdi; | |
1014 | struct mdinfo *sra; | |
1015 | int container_fd; | |
4dd2df09 | 1016 | char devnm[32]; |
38aeaf3a N |
1017 | int dfd; |
1018 | ||
4dd2df09 N |
1019 | strcpy(devnm, fd2devnm(fd)); |
1020 | ||
1021 | container_fd = open_dev_excl(devnm); | |
38aeaf3a | 1022 | if (container_fd < 0) { |
7a862a02 | 1023 | pr_err("add failed for %s: could not get exclusive access to container\n", |
38aeaf3a N |
1024 | dv->devname); |
1025 | tst->ss->free_super(tst); | |
1026 | return -1; | |
1027 | } | |
1028 | ||
9cf9a1de | 1029 | Kill(dv->devname, NULL, 0, -1, 0); |
38aeaf3a | 1030 | dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT); |
4dd2df09 | 1031 | if (mdmon_running(tst->container_devnm)) |
38aeaf3a N |
1032 | tst->update_tail = &tst->updates; |
1033 | if (tst->ss->add_to_super(tst, &disc, dfd, | |
72ca9bcf | 1034 | dv->devname, INVALID_SECTORS)) { |
38aeaf3a N |
1035 | close(dfd); |
1036 | close(container_fd); | |
1037 | return -1; | |
1038 | } | |
1039 | if (tst->update_tail) | |
1040 | flush_metadata_updates(tst); | |
1041 | else | |
1042 | tst->ss->sync_metadata(tst); | |
1043 | ||
4dd2df09 | 1044 | sra = sysfs_read(container_fd, NULL, 0); |
38aeaf3a N |
1045 | if (!sra) { |
1046 | pr_err("add failed for %s: sysfs_read failed\n", | |
1047 | dv->devname); | |
1048 | close(container_fd); | |
1049 | tst->ss->free_super(tst); | |
1050 | return -1; | |
1051 | } | |
1052 | sra->array.level = LEVEL_CONTAINER; | |
1053 | /* Need to set data_offset and component_size */ | |
1054 | tst->ss->getinfo_super(tst, &new_mdi, NULL); | |
1055 | new_mdi.disk.major = disc.major; | |
1056 | new_mdi.disk.minor = disc.minor; | |
1057 | new_mdi.recovery_start = 0; | |
1058 | /* Make sure fds are closed as they are O_EXCL which | |
1059 | * would block add_disk */ | |
1060 | tst->ss->free_super(tst); | |
1061 | if (sysfs_add_disk(sra, &new_mdi, 0) != 0) { | |
7a862a02 | 1062 | pr_err("add new device to external metadata failed for %s\n", dv->devname); |
38aeaf3a N |
1063 | close(container_fd); |
1064 | sysfs_free(sra); | |
1065 | return -1; | |
1066 | } | |
4dd2df09 | 1067 | ping_monitor(devnm); |
38aeaf3a N |
1068 | sysfs_free(sra); |
1069 | close(container_fd); | |
1070 | } else { | |
1071 | tst->ss->free_super(tst); | |
1072 | if (ioctl(fd, ADD_NEW_DISK, &disc)) { | |
01290056 SL |
1073 | if (dv->disposition == 'j') |
1074 | pr_err("Failed to hot add %s as journal, " | |
1075 | "please try restart %s.\n", dv->devname, devname); | |
1076 | else | |
1077 | pr_err("add new device failed for %s as %d: %s\n", | |
1078 | dv->devname, j, strerror(errno)); | |
38aeaf3a N |
1079 | return -1; |
1080 | } | |
01290056 SL |
1081 | if (dv->disposition == 'j') { |
1082 | pr_err("Journal added successfully, making %s read-write\n", devname); | |
1083 | if (Manage_ro(devname, fd, -1)) | |
1084 | pr_err("Failed to make %s read-write\n", devname); | |
1085 | } | |
1086 | ||
38aeaf3a N |
1087 | } |
1088 | if (verbose >= 0) | |
1089 | pr_err("added %s\n", dv->devname); | |
1090 | return 1; | |
1091 | } | |
1092 | ||
d070235d | 1093 | int Manage_remove(struct supertype *tst, int fd, struct mddev_dev *dv, |
1ab9ed2a | 1094 | int sysfd, unsigned long rdev, int force, int verbose, char *devname) |
d070235d N |
1095 | { |
1096 | int lfd = -1; | |
1097 | int err; | |
1098 | ||
1099 | if (tst->ss->external) { | |
1100 | /* To remove a device from a container, we must | |
1101 | * check that it isn't in use in an array. | |
1102 | * This involves looking in the 'holders' | |
1103 | * directory - there must be just one entry, | |
1104 | * the container. | |
1105 | * To ensure that it doesn't get used as a | |
1106 | * hot spare while we are checking, we | |
1107 | * get an O_EXCL open on the container | |
1108 | */ | |
aab15415 | 1109 | int ret; |
4dd2df09 N |
1110 | char devnm[32]; |
1111 | strcpy(devnm, fd2devnm(fd)); | |
1112 | lfd = open_dev_excl(devnm); | |
d070235d | 1113 | if (lfd < 0) { |
7a862a02 | 1114 | pr_err("Cannot get exclusive access to container - odd\n"); |
d070235d N |
1115 | return -1; |
1116 | } | |
aab15415 N |
1117 | /* We may not be able to check on holders in |
1118 | * sysfs, either because we don't have the dev num | |
1119 | * (rdev == 0) or because the device has been detached | |
1120 | * and the 'holders' directory no longer exists | |
1121 | * (ret == -1). In that case, assume it is OK to | |
1122 | * remove. | |
d070235d | 1123 | */ |
aab15415 N |
1124 | if (rdev == 0) |
1125 | ret = -1; | |
c922221e TM |
1126 | else { |
1127 | /* | |
1128 | * The drive has already been set to 'faulty', however | |
1129 | * monitor might not have had time to process it and the | |
1130 | * drive might still have an entry in the 'holders' | |
1131 | * directory. Try a few times to avoid a false error | |
1132 | */ | |
1133 | int count = 20; | |
1134 | ||
1135 | do { | |
1136 | ret = sysfs_unique_holder(devnm, rdev); | |
1137 | if (ret < 2) | |
1138 | break; | |
1139 | usleep(100 * 1000); /* 100ms */ | |
1140 | } while (--count > 0); | |
1141 | ||
1142 | if (ret == 0) { | |
1143 | pr_err("%s is not a member, cannot remove.\n", | |
1144 | dv->devname); | |
1145 | close(lfd); | |
1146 | return -1; | |
1147 | } | |
1148 | if (ret >= 2) { | |
1149 | pr_err("%s is still in use, cannot remove.\n", | |
1150 | dv->devname); | |
1151 | close(lfd); | |
1152 | return -1; | |
1153 | } | |
d070235d N |
1154 | } |
1155 | } | |
1156 | /* FIXME check that it is a current member */ | |
1157 | if (sysfd >= 0) { | |
1158 | /* device has been removed and we don't know | |
1159 | * the major:minor number | |
1160 | */ | |
1ab9ed2a | 1161 | err = sys_hot_remove_disk(sysfd, force); |
d070235d | 1162 | } else { |
1ab9ed2a | 1163 | err = hot_remove_disk(fd, rdev, force); |
d070235d N |
1164 | if (err && errno == ENODEV) { |
1165 | /* Old kernels rejected this if no personality | |
1166 | * is registered */ | |
4dd2df09 | 1167 | struct mdinfo *sra = sysfs_read(fd, NULL, GET_DEVS); |
d070235d N |
1168 | struct mdinfo *dv = NULL; |
1169 | if (sra) | |
1170 | dv = sra->devs; | |
1171 | for ( ; dv ; dv=dv->next) | |
1172 | if (dv->disk.major == (int)major(rdev) && | |
1173 | dv->disk.minor == (int)minor(rdev)) | |
1174 | break; | |
1175 | if (dv) | |
1176 | err = sysfs_set_str(sra, dv, | |
1177 | "state", "remove"); | |
1178 | else | |
1179 | err = -1; | |
6e8d27e7 | 1180 | sysfs_free(sra); |
d070235d N |
1181 | } |
1182 | } | |
1183 | if (err) { | |
7a862a02 | 1184 | pr_err("hot remove failed for %s: %s\n", dv->devname, |
d070235d N |
1185 | strerror(errno)); |
1186 | if (lfd >= 0) | |
1187 | close(lfd); | |
1188 | return -1; | |
1189 | } | |
1190 | if (tst->ss->external) { | |
1191 | /* | |
1192 | * Before dropping our exclusive open we make an | |
1193 | * attempt at preventing mdmon from seeing an | |
1194 | * 'add' event before reconciling this 'remove' | |
1195 | * event. | |
1196 | */ | |
4dd2df09 | 1197 | char *devnm = fd2devnm(fd); |
d070235d | 1198 | |
4dd2df09 | 1199 | if (!devnm) { |
d070235d N |
1200 | pr_err("unable to get container name\n"); |
1201 | return -1; | |
1202 | } | |
1203 | ||
4dd2df09 | 1204 | ping_manager(devnm); |
d070235d N |
1205 | } |
1206 | if (lfd >= 0) | |
1207 | close(lfd); | |
1208 | if (verbose >= 0) | |
1209 | pr_err("hot removed %s from %s\n", | |
1210 | dv->devname, devname); | |
1211 | return 1; | |
1212 | } | |
1213 | ||
70c55e36 N |
1214 | int Manage_replace(struct supertype *tst, int fd, struct mddev_dev *dv, |
1215 | unsigned long rdev, int verbose, char *devname) | |
1216 | { | |
1217 | struct mdinfo *mdi, *di; | |
1218 | if (tst->ss->external) { | |
1219 | pr_err("--replace only supported for native metadata (0.90 or 1.x)\n"); | |
1220 | return -1; | |
1221 | } | |
1222 | /* Need to find the device in sysfs and add 'want_replacement' to the | |
1223 | * status. | |
1224 | */ | |
4dd2df09 | 1225 | mdi = sysfs_read(fd, NULL, GET_DEVS); |
70c55e36 N |
1226 | if (!mdi || !mdi->devs) { |
1227 | pr_err("Cannot find status of %s to enable replacement - strange\n", | |
1228 | devname); | |
1229 | return -1; | |
1230 | } | |
1231 | for (di = mdi->devs; di; di = di->next) | |
1232 | if (di->disk.major == (int)major(rdev) && | |
1233 | di->disk.minor == (int)minor(rdev)) | |
1234 | break; | |
1235 | if (di) { | |
1236 | int rv; | |
1237 | if (di->disk.raid_disk < 0) { | |
1238 | pr_err("%s is not active and so cannot be replaced.\n", | |
1239 | dv->devname); | |
1240 | sysfs_free(mdi); | |
1241 | return -1; | |
1242 | } | |
1243 | rv = sysfs_set_str(mdi, di, | |
1244 | "state", "want_replacement"); | |
1245 | if (rv) { | |
1246 | sysfs_free(mdi); | |
1247 | pr_err("Failed to request replacement for %s\n", | |
1248 | dv->devname); | |
1249 | return -1; | |
1250 | } | |
1251 | if (verbose >= 0) | |
1252 | pr_err("Marked %s (device %d in %s) for replacement\n", | |
1253 | dv->devname, di->disk.raid_disk, devname); | |
1254 | /* If there is a matching 'with', we need to tell it which | |
1255 | * raid disk | |
1256 | */ | |
1257 | while (dv && dv->disposition != 'W') | |
1258 | dv = dv->next; | |
1259 | if (dv) { | |
1260 | dv->disposition = 'w'; | |
1261 | dv->used = di->disk.raid_disk; | |
1262 | } | |
1263 | return 1; | |
1264 | } | |
1265 | sysfs_free(mdi); | |
1266 | pr_err("%s not found in %s so cannot --replace it\n", | |
1267 | dv->devname, devname); | |
1268 | return -1; | |
1269 | } | |
1270 | ||
1271 | int Manage_with(struct supertype *tst, int fd, struct mddev_dev *dv, | |
1272 | unsigned long rdev, int verbose, char *devname) | |
1273 | { | |
1274 | struct mdinfo *mdi, *di; | |
1275 | /* try to set 'slot' for 'rdev' in 'fd' to 'dv->used' */ | |
4dd2df09 | 1276 | mdi = sysfs_read(fd, NULL, GET_DEVS|GET_STATE); |
70c55e36 N |
1277 | if (!mdi || !mdi->devs) { |
1278 | pr_err("Cannot find status of %s to enable replacement - strange\n", | |
1279 | devname); | |
1280 | return -1; | |
1281 | } | |
1282 | for (di = mdi->devs; di; di = di->next) | |
1283 | if (di->disk.major == (int)major(rdev) && | |
1284 | di->disk.minor == (int)minor(rdev)) | |
1285 | break; | |
1286 | if (di) { | |
1287 | int rv; | |
1288 | if (di->disk.state & (1<<MD_DISK_FAULTY)) { | |
1289 | pr_err("%s is faulty and cannot be a replacement\n", | |
1290 | dv->devname); | |
1291 | sysfs_free(mdi); | |
1292 | return -1; | |
1293 | } | |
1294 | if (di->disk.raid_disk >= 0) { | |
1295 | pr_err("%s is active and cannot be a replacement\n", | |
1296 | dv->devname); | |
1297 | sysfs_free(mdi); | |
1298 | return -1; | |
1299 | } | |
1300 | rv = sysfs_set_num(mdi, di, | |
1301 | "slot", dv->used); | |
1302 | if (rv) { | |
1303 | sysfs_free(mdi); | |
51425978 | 1304 | pr_err("Failed to set %s as preferred replacement.\n", |
70c55e36 N |
1305 | dv->devname); |
1306 | return -1; | |
1307 | } | |
1308 | if (verbose >= 0) | |
1309 | pr_err("Marked %s in %s as replacement for device %d\n", | |
1310 | dv->devname, devname, dv->used); | |
1311 | return 1; | |
1312 | } | |
1313 | sysfs_free(mdi); | |
1314 | pr_err("%s not found in %s so cannot make it preferred replacement\n", | |
1315 | dv->devname, devname); | |
1316 | return -1; | |
1317 | } | |
1318 | ||
64c4757e | 1319 | int Manage_subdevs(char *devname, int fd, |
833bb0f8 | 1320 | struct mddev_dev *devlist, int verbose, int test, |
11b391ec | 1321 | char *update, int force) |
cd29a5c8 | 1322 | { |
7bd04da9 | 1323 | /* Do something to each dev. |
682c7051 NB |
1324 | * devmode can be |
1325 | * 'a' - add the device | |
1326 | * try HOT_ADD_DISK | |
1327 | * If that fails EINVAL, try ADD_NEW_DISK | |
f33a71f1 | 1328 | * 'S' - add the device as a spare - don't try re-add |
01290056 | 1329 | * 'j' - add the device as a journal device |
7bd04da9 N |
1330 | * 'A' - re-add the device |
1331 | * 'r' - remove the device: HOT_REMOVE_DISK | |
b80da661 NB |
1332 | * device can be 'faulty' or 'detached' in which case all |
1333 | * matching devices are removed. | |
682c7051 | 1334 | * 'f' - set the device faulty SET_DISK_FAULTY |
b80da661 NB |
1335 | * device can be 'detached' in which case any device that |
1336 | * is inaccessible will be marked faulty. | |
70c55e36 N |
1337 | * 'R' - mark this device as wanting replacement. |
1338 | * 'W' - this device is added if necessary and activated as | |
1339 | * a replacement for a previous 'R' device. | |
1340 | * ----- | |
1341 | * 'w' - 'W' will be changed to 'w' when it is paired with | |
1342 | * a 'R' device. If a 'W' is found while walking the list | |
1343 | * it must be unpaired, and is an error. | |
1344 | * 'M' - this is created by a 'missing' target. It is a slight | |
1345 | * variant on 'A' | |
262e3b7f N |
1346 | * 'F' - Another variant of 'A', where the device was faulty |
1347 | * so must be removed from the array first. | |
4de90913 | 1348 | * 'c' - confirm the device as found (for clustered environments) |
70c55e36 | 1349 | * |
98d27e39 N |
1350 | * For 'f' and 'r', the device can also be a kernel-internal |
1351 | * name such as 'sdb'. | |
682c7051 NB |
1352 | */ |
1353 | mdu_array_info_t array; | |
7a3be72f | 1354 | unsigned long long array_size; |
1d997643 | 1355 | struct mddev_dev *dv; |
cfad27a9 | 1356 | int tfd = -1; |
38aeaf3a | 1357 | struct supertype *tst; |
4725bc31 | 1358 | char *subarray = NULL; |
98d27e39 | 1359 | int sysfd = -1; |
7d2e6486 | 1360 | int count = 0; /* number of actions taken */ |
9f584691 | 1361 | struct mdinfo info; |
9465f170 | 1362 | struct mdinfo devinfo; |
9f584691 | 1363 | int frozen = 0; |
8af530b0 | 1364 | int busy = 0; |
4de90913 | 1365 | int raid_slot = -1; |
682c7051 | 1366 | |
dae13137 JS |
1367 | if (sysfs_init(&info, fd, NULL)) { |
1368 | pr_err("sysfs not availabile for %s\n", devname); | |
1369 | goto abort; | |
1370 | } | |
1371 | ||
9cd39f01 JS |
1372 | if (md_get_array_info(fd, &array)) { |
1373 | pr_err("Cannot get array info for %s\n", devname); | |
bcbb3112 | 1374 | goto abort; |
682c7051 | 1375 | } |
7bd04da9 | 1376 | /* array.size is only 32 bits and may be truncated. |
7a3be72f NB |
1377 | * So read from sysfs if possible, and record number of sectors |
1378 | */ | |
1379 | ||
1380 | array_size = get_component_size(fd); | |
1381 | if (array_size <= 0) | |
1382 | array_size = array.size * 2; | |
1383 | ||
4725bc31 | 1384 | tst = super_by_fd(fd, &subarray); |
3da92f27 | 1385 | if (!tst) { |
e7b84f9d | 1386 | pr_err("unsupport array - version %d.%d\n", |
3da92f27 | 1387 | array.major_version, array.minor_version); |
bcbb3112 | 1388 | goto abort; |
3da92f27 NB |
1389 | } |
1390 | ||
38aeaf3a | 1391 | for (dv = devlist; dv; dv = dv->next) { |
5dffd09d | 1392 | unsigned long rdev = 0; /* device to add/remove etc */ |
38aeaf3a | 1393 | int rv; |
b47024f1 | 1394 | int mj,mn; |
4a39c6f2 | 1395 | |
4de90913 GJ |
1396 | raid_slot = -1; |
1397 | if (dv->disposition == 'c') { | |
1398 | rv = parse_cluster_confirm_arg(dv->devname, | |
1399 | &dv->devname, | |
1400 | &raid_slot); | |
d7a49369 | 1401 | if (rv) { |
4de90913 GJ |
1402 | pr_err("Could not get the devname of cluster\n"); |
1403 | goto abort; | |
1404 | } | |
1405 | } | |
1406 | ||
1d997643 N |
1407 | if (strcmp(dv->devname, "failed") == 0 || |
1408 | strcmp(dv->devname, "faulty") == 0) { | |
262e3b7f N |
1409 | if (dv->disposition != 'A' |
1410 | && dv->disposition != 'r') { | |
7a862a02 | 1411 | pr_err("%s only meaningful with -r or --re-add, not -%c\n", |
b80da661 | 1412 | dv->devname, dv->disposition); |
bcbb3112 | 1413 | goto abort; |
b80da661 | 1414 | } |
262e3b7f N |
1415 | add_faulty(dv, fd, (dv->disposition == 'A' |
1416 | ? 'F' : 'r')); | |
1d997643 N |
1417 | continue; |
1418 | } | |
1419 | if (strcmp(dv->devname, "detached") == 0) { | |
b80da661 | 1420 | if (dv->disposition != 'r' && dv->disposition != 'f') { |
7a862a02 | 1421 | pr_err("%s only meaningful with -r of -f, not -%c\n", |
b80da661 | 1422 | dv->devname, dv->disposition); |
bcbb3112 | 1423 | goto abort; |
b80da661 | 1424 | } |
1d997643 N |
1425 | add_detached(dv, fd, dv->disposition); |
1426 | continue; | |
1427 | } | |
1428 | ||
1429 | if (strcmp(dv->devname, "missing") == 0) { | |
e9ddbb2b | 1430 | struct mddev_dev *add_devlist; |
1d997643 | 1431 | struct mddev_dev **dp; |
4de90913 GJ |
1432 | if (dv->disposition == 'c') { |
1433 | rv = ioctl(fd, CLUSTERED_DISK_NACK, NULL); | |
1434 | break; | |
1435 | } | |
1436 | ||
c8e1a230 | 1437 | if (dv->disposition != 'A') { |
7a862a02 | 1438 | pr_err("'missing' only meaningful with --re-add\n"); |
bcbb3112 | 1439 | goto abort; |
a4e13010 | 1440 | } |
1d997643 | 1441 | add_devlist = conf_get_devs(); |
a4e13010 | 1442 | if (add_devlist == NULL) { |
e7b84f9d | 1443 | pr_err("no devices to scan for missing members."); |
a4e13010 N |
1444 | continue; |
1445 | } | |
1d997643 | 1446 | for (dp = &add_devlist; *dp; dp = & (*dp)->next) |
7bd04da9 | 1447 | /* 'M' (for 'missing') is like 'A' without errors */ |
1d997643 N |
1448 | (*dp)->disposition = 'M'; |
1449 | *dp = dv->next; | |
1450 | dv->next = add_devlist; | |
1451 | continue; | |
1452 | } | |
1453 | ||
64a78416 N |
1454 | if (strncmp(dv->devname, "set-", 4) == 0 && |
1455 | strlen(dv->devname) == 5) { | |
1456 | int copies; | |
1457 | ||
1458 | if (dv->disposition != 'r' && | |
1459 | dv->disposition != 'f') { | |
1460 | pr_err("'%s' only meaningful with -r or -f\n", | |
1461 | dv->devname); | |
1462 | goto abort; | |
1463 | } | |
1464 | if (array.level != 10) { | |
1465 | pr_err("'%s' only meaningful with RAID10 arrays\n", | |
1466 | dv->devname); | |
1467 | goto abort; | |
1468 | } | |
1469 | copies = ((array.layout & 0xff) * | |
1470 | ((array.layout >> 8) & 0xff)); | |
1471 | if (array.raid_disks % copies != 0 || | |
1472 | dv->devname[4] < 'A' || | |
1473 | dv->devname[4] >= 'A' + copies || | |
1474 | copies > 26) { | |
1475 | pr_err("'%s' not meaningful with this array\n", | |
1476 | dv->devname); | |
1477 | goto abort; | |
1478 | } | |
1479 | add_set(dv, fd, dv->devname[4]); | |
1480 | continue; | |
1481 | } | |
1482 | ||
1d997643 | 1483 | if (strchr(dv->devname, '/') == NULL && |
aab15415 N |
1484 | strchr(dv->devname, ':') == NULL && |
1485 | strlen(dv->devname) < 50) { | |
98d27e39 N |
1486 | /* Assume this is a kernel-internal name like 'sda1' */ |
1487 | int found = 0; | |
1488 | char dname[55]; | |
1489 | if (dv->disposition != 'r' && dv->disposition != 'f') { | |
7a862a02 | 1490 | pr_err("%s only meaningful with -r or -f, not -%c\n", |
98d27e39 | 1491 | dv->devname, dv->disposition); |
bcbb3112 | 1492 | goto abort; |
98d27e39 N |
1493 | } |
1494 | ||
1495 | sprintf(dname, "dev-%s", dv->devname); | |
4dd2df09 | 1496 | sysfd = sysfs_open(fd2devnm(fd), dname, "block/dev"); |
98d27e39 N |
1497 | if (sysfd >= 0) { |
1498 | char dn[20]; | |
98d27e39 N |
1499 | if (sysfs_fd_get_str(sysfd, dn, 20) > 0 && |
1500 | sscanf(dn, "%d:%d", &mj,&mn) == 2) { | |
5dffd09d | 1501 | rdev = makedev(mj,mn); |
98d27e39 N |
1502 | found = 1; |
1503 | } | |
1504 | close(sysfd); | |
1505 | sysfd = -1; | |
1506 | } | |
1507 | if (!found) { | |
4dd2df09 | 1508 | sysfd = sysfs_open(fd2devnm(fd), dname, "state"); |
98d27e39 | 1509 | if (sysfd < 0) { |
7a862a02 | 1510 | pr_err("%s does not appear to be a component of %s\n", |
98d27e39 | 1511 | dv->devname, devname); |
bcbb3112 | 1512 | goto abort; |
98d27e39 N |
1513 | } |
1514 | } | |
b47024f1 N |
1515 | } else if ((dv->disposition == 'r' || dv->disposition == 'f') |
1516 | && get_maj_min(dv->devname, &mj, &mn)) { | |
1517 | /* for 'fail' and 'remove', the device might | |
1518 | * not exist. | |
1519 | */ | |
1520 | rdev = makedev(mj, mn); | |
b80da661 | 1521 | } else { |
5dffd09d | 1522 | struct stat stb; |
c7b47447 | 1523 | tfd = dev_open(dv->devname, O_RDONLY); |
cc5083d1 | 1524 | if (tfd >= 0) { |
5fe7f5f7 | 1525 | fstat(tfd, &stb); |
cc5083d1 JS |
1526 | close(tfd); |
1527 | } else { | |
5fe7f5f7 N |
1528 | int open_err = errno; |
1529 | if (stat(dv->devname, &stb) != 0) { | |
1530 | pr_err("Cannot find %s: %s\n", | |
1531 | dv->devname, strerror(errno)); | |
1532 | goto abort; | |
1533 | } | |
1534 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
1535 | if (dv->disposition == 'M') | |
1536 | /* non-fatal. Also improbable */ | |
1537 | continue; | |
1538 | pr_err("%s is not a block device.\n", | |
1539 | dv->devname); | |
1540 | goto abort; | |
1541 | } | |
1542 | if (dv->disposition == 'r') | |
1543 | /* Be happy, the stat worked, that is | |
1544 | * enough for --remove | |
1545 | */ | |
1546 | ; | |
1547 | else { | |
1d997643 N |
1548 | if (dv->disposition == 'M') |
1549 | /* non-fatal */ | |
1550 | continue; | |
839f27a3 | 1551 | pr_err("Cannot open %s: %s\n", |
5fe7f5f7 | 1552 | dv->devname, strerror(open_err)); |
bcbb3112 | 1553 | goto abort; |
5a9de8db | 1554 | } |
b80da661 | 1555 | } |
5dffd09d | 1556 | rdev = stb.st_rdev; |
682c7051 | 1557 | } |
cd29a5c8 | 1558 | switch(dv->disposition){ |
682c7051 | 1559 | default: |
e7b84f9d | 1560 | pr_err("internal error - devmode[%s]=%d\n", |
c913b90e | 1561 | dv->devname, dv->disposition); |
bcbb3112 | 1562 | goto abort; |
682c7051 | 1563 | case 'a': |
f33a71f1 | 1564 | case 'S': /* --add-spare */ |
01290056 | 1565 | case 'j': /* --add-journal */ |
c8e1a230 | 1566 | case 'A': |
262e3b7f N |
1567 | case 'M': /* --re-add missing */ |
1568 | case 'F': /* --re-add faulty */ | |
4de90913 | 1569 | case 'c': /* --cluster-confirm */ |
4a39c6f2 | 1570 | /* add the device */ |
4725bc31 | 1571 | if (subarray) { |
7a862a02 | 1572 | pr_err("Cannot add disks to a \'member\' array, perform this operation on the parent container\n"); |
bcbb3112 | 1573 | goto abort; |
f94d52f4 | 1574 | } |
9465f170 GJ |
1575 | |
1576 | /* Let's first try to write re-add to sysfs */ | |
1577 | if (rdev != 0 && | |
1578 | (dv->disposition == 'A' || dv->disposition == 'F')) { | |
1579 | sysfs_init_dev(&devinfo, rdev); | |
1580 | if (sysfs_set_str(&info, &devinfo, "state", "re-add") == 0) { | |
1581 | pr_err("re-add %s to %s succeed\n", | |
1582 | dv->devname, info.sys_name); | |
1583 | break; | |
1584 | } | |
1585 | } | |
1586 | ||
262e3b7f N |
1587 | if (dv->disposition == 'F') |
1588 | /* Need to remove first */ | |
1ab9ed2a | 1589 | hot_remove_disk(fd, rdev, force); |
f277ce36 | 1590 | /* Make sure it isn't in use (in 2.6 or later) */ |
1d997643 | 1591 | tfd = dev_open(dv->devname, O_RDONLY|O_EXCL); |
38aeaf3a N |
1592 | if (tfd >= 0) { |
1593 | /* We know no-one else is using it. We'll | |
1594 | * need non-exclusive access to add it, so | |
1595 | * do that now. | |
1596 | */ | |
1597 | close(tfd); | |
1598 | tfd = dev_open(dv->devname, O_RDONLY); | |
1011e834 | 1599 | } |
0fbf459d | 1600 | if (tfd < 0) { |
1d997643 N |
1601 | if (dv->disposition == 'M') |
1602 | continue; | |
e7b84f9d | 1603 | pr_err("Cannot open %s: %s\n", |
d7eaf49f | 1604 | dv->devname, strerror(errno)); |
bcbb3112 | 1605 | goto abort; |
d7eaf49f | 1606 | } |
9f584691 N |
1607 | if (!frozen) { |
1608 | if (sysfs_freeze_array(&info) == 1) | |
1609 | frozen = 1; | |
1610 | else | |
1611 | frozen = -1; | |
1612 | } | |
38aeaf3a N |
1613 | rv = Manage_add(fd, tfd, dv, tst, &array, |
1614 | force, verbose, devname, update, | |
4de90913 | 1615 | rdev, array_size, raid_slot); |
38aeaf3a N |
1616 | close(tfd); |
1617 | tfd = -1; | |
1618 | if (rv < 0) | |
bcbb3112 | 1619 | goto abort; |
38aeaf3a N |
1620 | if (rv > 0) |
1621 | count++; | |
682c7051 NB |
1622 | break; |
1623 | ||
1624 | case 'r': | |
1625 | /* hot remove */ | |
4725bc31 | 1626 | if (subarray) { |
7a862a02 | 1627 | pr_err("Cannot remove disks from a \'member\' array, perform this operation on the parent container\n"); |
d070235d N |
1628 | rv = -1; |
1629 | } else | |
1630 | rv = Manage_remove(tst, fd, dv, sysfd, | |
1ab9ed2a | 1631 | rdev, verbose, force, |
d070235d N |
1632 | devname); |
1633 | if (sysfd >= 0) | |
98d27e39 | 1634 | close(sysfd); |
d070235d N |
1635 | sysfd = -1; |
1636 | if (rv < 0) | |
bcbb3112 | 1637 | goto abort; |
d070235d N |
1638 | if (rv > 0) |
1639 | count++; | |
682c7051 NB |
1640 | break; |
1641 | ||
1642 | case 'f': /* set faulty */ | |
1643 | /* FIXME check current member */ | |
98d27e39 N |
1644 | if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) || |
1645 | (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY, | |
5dffd09d | 1646 | rdev))) { |
8af530b0 N |
1647 | if (errno == EBUSY) |
1648 | busy = 1; | |
e7b84f9d | 1649 | pr_err("set device faulty failed for %s: %s\n", |
1d997643 | 1650 | dv->devname, strerror(errno)); |
98d27e39 N |
1651 | if (sysfd >= 0) |
1652 | close(sysfd); | |
bcbb3112 | 1653 | goto abort; |
682c7051 | 1654 | } |
98d27e39 N |
1655 | if (sysfd >= 0) |
1656 | close(sysfd); | |
1657 | sysfd = -1; | |
7d2e6486 | 1658 | count++; |
dab6685f | 1659 | if (verbose >= 0) |
e7b84f9d | 1660 | pr_err("set %s faulty in %s\n", |
1d997643 | 1661 | dv->devname, devname); |
682c7051 | 1662 | break; |
70c55e36 N |
1663 | case 'R': /* Mark as replaceable */ |
1664 | if (subarray) { | |
7a862a02 | 1665 | pr_err("Cannot replace disks in a \'member\' array, perform this operation on the parent container\n"); |
70c55e36 N |
1666 | rv = -1; |
1667 | } else { | |
1668 | if (!frozen) { | |
1669 | if (sysfs_freeze_array(&info) == 1) | |
1670 | frozen = 1; | |
1671 | else | |
1672 | frozen = -1; | |
1673 | } | |
1674 | rv = Manage_replace(tst, fd, dv, | |
5dffd09d | 1675 | rdev, verbose, |
70c55e36 N |
1676 | devname); |
1677 | } | |
1678 | if (rv < 0) | |
1679 | goto abort; | |
1680 | if (rv > 0) | |
1681 | count++; | |
1682 | break; | |
1683 | case 'W': /* --with device that doesn't match */ | |
1684 | pr_err("No matching --replace device for --with %s\n", | |
1685 | dv->devname); | |
1686 | goto abort; | |
1687 | case 'w': /* --with device which was matched */ | |
1688 | rv = Manage_with(tst, fd, dv, | |
5dffd09d | 1689 | rdev, verbose, devname); |
70c55e36 N |
1690 | if (rv < 0) |
1691 | goto abort; | |
1692 | break; | |
682c7051 NB |
1693 | } |
1694 | } | |
9f584691 N |
1695 | if (frozen > 0) |
1696 | sysfs_set_str(&info, NULL, "sync_action","idle"); | |
7d2e6486 N |
1697 | if (test && count == 0) |
1698 | return 2; | |
682c7051 | 1699 | return 0; |
bcbb3112 N |
1700 | |
1701 | abort: | |
9f584691 N |
1702 | if (frozen > 0) |
1703 | sysfs_set_str(&info, NULL, "sync_action","idle"); | |
8af530b0 | 1704 | return !test && busy ? 2 : 1; |
64c4757e | 1705 | } |
1f48664b NB |
1706 | |
1707 | int autodetect(void) | |
1708 | { | |
1709 | /* Open any md device, and issue the RAID_AUTORUN ioctl */ | |
1710 | int rv = 1; | |
1711 | int fd = dev_open("9:0", O_RDONLY); | |
1712 | if (fd >= 0) { | |
1713 | if (ioctl(fd, RAID_AUTORUN, 0) == 0) | |
1714 | rv = 0; | |
1715 | close(fd); | |
1716 | } | |
1717 | return rv; | |
1718 | } | |
aa534678 | 1719 | |
ba728be7 | 1720 | int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident *ident, int verbose) |
aa534678 DW |
1721 | { |
1722 | struct supertype supertype, *st = &supertype; | |
1723 | int fd, rv = 2; | |
1724 | ||
1725 | memset(st, 0, sizeof(*st)); | |
aa534678 | 1726 | |
ba728be7 | 1727 | fd = open_subarray(dev, subarray, st, verbose < 0); |
aa534678 DW |
1728 | if (fd < 0) |
1729 | return 2; | |
1730 | ||
1731 | if (!st->ss->update_subarray) { | |
ba728be7 | 1732 | if (verbose >= 0) |
e7b84f9d N |
1733 | pr_err("Operation not supported for %s metadata\n", |
1734 | st->ss->name); | |
aa534678 DW |
1735 | goto free_super; |
1736 | } | |
1737 | ||
4dd2df09 | 1738 | if (mdmon_running(st->devnm)) |
aa534678 DW |
1739 | st->update_tail = &st->updates; |
1740 | ||
a951a4f7 | 1741 | rv = st->ss->update_subarray(st, subarray, update, ident); |
aa534678 DW |
1742 | |
1743 | if (rv) { | |
ba728be7 | 1744 | if (verbose >= 0) |
e7b84f9d | 1745 | pr_err("Failed to update %s of subarray-%s in %s\n", |
aa534678 DW |
1746 | update, subarray, dev); |
1747 | } else if (st->update_tail) | |
1748 | flush_metadata_updates(st); | |
1749 | else | |
1750 | st->ss->sync_metadata(st); | |
1751 | ||
ba728be7 | 1752 | if (rv == 0 && strcmp(update, "name") == 0 && verbose >= 0) |
e7b84f9d N |
1753 | pr_err("Updated subarray-%s name from %s, UUIDs may have changed\n", |
1754 | subarray, dev); | |
aa534678 DW |
1755 | |
1756 | free_super: | |
1757 | st->ss->free_super(st); | |
1758 | close(fd); | |
1759 | ||
1760 | return rv; | |
1761 | } | |
d52bb542 | 1762 | |
7bd04da9 N |
1763 | /* Move spare from one array to another If adding to destination array fails |
1764 | * add back to original array. | |
d52bb542 AC |
1765 | * Returns 1 on success, 0 on failure */ |
1766 | int move_spare(char *from_devname, char *to_devname, dev_t devid) | |
1767 | { | |
1768 | struct mddev_dev devlist; | |
1769 | char devname[20]; | |
1770 | ||
1771 | /* try to remove and add */ | |
1772 | int fd1 = open(to_devname, O_RDONLY); | |
1773 | int fd2 = open(from_devname, O_RDONLY); | |
1774 | ||
1775 | if (fd1 < 0 || fd2 < 0) { | |
1776 | if (fd1>=0) close(fd1); | |
1777 | if (fd2>=0) close(fd2); | |
1778 | return 0; | |
1779 | } | |
1780 | ||
1781 | devlist.next = NULL; | |
1782 | devlist.used = 0; | |
e22fe3ae N |
1783 | devlist.writemostly = FlagDefault; |
1784 | devlist.failfast = FlagDefault; | |
d52bb542 AC |
1785 | devlist.devname = devname; |
1786 | sprintf(devname, "%d:%d", major(devid), minor(devid)); | |
1787 | ||
1788 | devlist.disposition = 'r'; | |
11b391ec | 1789 | if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0) == 0) { |
d52bb542 | 1790 | devlist.disposition = 'a'; |
11b391ec | 1791 | if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0, NULL, 0) == 0) { |
d52bb542 AC |
1792 | /* make sure manager is aware of changes */ |
1793 | ping_manager(to_devname); | |
1794 | ping_manager(from_devname); | |
1795 | close(fd1); | |
1796 | close(fd2); | |
1797 | return 1; | |
1798 | } | |
11b391ec | 1799 | else Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0); |
d52bb542 AC |
1800 | } |
1801 | close(fd1); | |
1802 | close(fd2); | |
1803 | return 0; | |
1804 | } | |
435d4ebb | 1805 | #endif |