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