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