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