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