]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de> | |
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 | |
22 | * Email: <neilb@suse.de> | |
23 | */ | |
24 | ||
25 | #include "mdadm.h" | |
26 | #include "md_u.h" | |
27 | #include "md_p.h" | |
28 | #include <ctype.h> | |
29 | ||
30 | #define REGISTER_DEV _IO (MD_MAJOR, 1) | |
31 | #define START_MD _IO (MD_MAJOR, 2) | |
32 | #define STOP_MD _IO (MD_MAJOR, 3) | |
33 | ||
34 | int Manage_ro(char *devname, int fd, int readonly) | |
35 | { | |
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; | |
44 | #ifndef MDASSEMBLE | |
45 | struct mdinfo *mdi; | |
46 | #endif | |
47 | int rv = 0; | |
48 | ||
49 | if (md_get_version(fd) < 9000) { | |
50 | pr_err("need md driver version 0.90.0 or later\n"); | |
51 | return 1; | |
52 | } | |
53 | #ifndef MDASSEMBLE | |
54 | /* If this is an externally-manage array, we need to modify the | |
55 | * metadata_version so that mdmon doesn't undo our change. | |
56 | */ | |
57 | mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION); | |
58 | if (mdi && | |
59 | mdi->array.major_version == -1 && | |
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) { | |
74 | pr_err("failed to set readonly for %s: %s\n", | |
75 | devname, strerror(errno)); | |
76 | ||
77 | vers[9] = mdi->text_version[0]; | |
78 | sysfs_set_str(mdi, NULL, "metadata_version", vers); | |
79 | rv = 1; | |
80 | goto out; | |
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, '/'); | |
89 | if (cp) | |
90 | *cp = 0; | |
91 | ping_monitor(vers+10); | |
92 | if (mdi->array.level <= 0) | |
93 | sysfs_set_str(mdi, NULL, "array_state", "active"); | |
94 | } | |
95 | goto out; | |
96 | } | |
97 | #endif | |
98 | if (ioctl(fd, GET_ARRAY_INFO, &array)) { | |
99 | pr_err("%s does not appear to be active.\n", | |
100 | devname); | |
101 | rv = 1; | |
102 | goto out; | |
103 | } | |
104 | ||
105 | if (readonly>0) { | |
106 | if (ioctl(fd, STOP_ARRAY_RO, NULL)) { | |
107 | pr_err("failed to set readonly for %s: %s\n", | |
108 | devname, strerror(errno)); | |
109 | rv = 1; | |
110 | goto out; | |
111 | } | |
112 | } else if (readonly < 0) { | |
113 | if (ioctl(fd, RESTART_ARRAY_RW, NULL)) { | |
114 | pr_err("failed to set writable for %s: %s\n", | |
115 | devname, strerror(errno)); | |
116 | rv = 1; | |
117 | goto out; | |
118 | } | |
119 | } | |
120 | out: | |
121 | #ifndef MDASSEMBLE | |
122 | if (mdi) | |
123 | sysfs_free(mdi); | |
124 | #endif | |
125 | return rv; | |
126 | } | |
127 | ||
128 | #ifndef MDASSEMBLE | |
129 | ||
130 | static void remove_devices(int devnum, char *path) | |
131 | { | |
132 | /* | |
133 | * Remove names at 'path' - possibly with | |
134 | * partition suffixes - which link to the 'standard' | |
135 | * name for devnum. These were probably created | |
136 | * by mdadm when the array was assembled. | |
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 | ||
146 | if (!path) | |
147 | return; | |
148 | ||
149 | if (devnum >= 0) | |
150 | sprintf(base, "/dev/md%d", devnum); | |
151 | else | |
152 | sprintf(base, "/dev/md_d%d", -1-devnum); | |
153 | be = base + strlen(base); | |
154 | ||
155 | path2 = xmalloc(strlen(path)+20); | |
156 | strcpy(path2, path); | |
157 | pe = path2 + strlen(path2); | |
158 | ||
159 | for (part = 0; part < 16; part++) { | |
160 | if (part) { | |
161 | sprintf(be, "p%d", part); | |
162 | ||
163 | if (isdigit(pe[-1])) | |
164 | sprintf(pe, "p%d", part); | |
165 | else | |
166 | sprintf(pe, "%d", part); | |
167 | } | |
168 | n = readlink(path2, link, sizeof(link)); | |
169 | if (n > 0 && (int)strlen(base) == n && | |
170 | strncmp(link, base, n) == 0) | |
171 | unlink(path2); | |
172 | } | |
173 | free(path2); | |
174 | } | |
175 | ||
176 | ||
177 | int Manage_runstop(char *devname, int fd, int runstop, int quiet) | |
178 | { | |
179 | /* Run or stop the array. array must already be configured | |
180 | * required >= 0.90.0 | |
181 | * Only print failure messages if quiet == 0; | |
182 | * quiet > 0 means really be quiet | |
183 | * quiet < 0 means we will try again if it fails. | |
184 | */ | |
185 | mdu_param_t param; /* unused */ | |
186 | int rv = 0; | |
187 | ||
188 | if (runstop == -1 && md_get_version(fd) < 9000) { | |
189 | if (ioctl(fd, STOP_MD, 0)) { | |
190 | pr_err("stopping device %s " | |
191 | "failed: %s\n", | |
192 | devname, strerror(errno)); | |
193 | return 1; | |
194 | } | |
195 | } | |
196 | ||
197 | if (md_get_version(fd) < 9000) { | |
198 | pr_err("need md driver version 0.90.0 or later\n"); | |
199 | return 1; | |
200 | } | |
201 | /* | |
202 | if (ioctl(fd, GET_ARRAY_INFO, &array)) { | |
203 | pr_err("%s does not appear to be active.\n", | |
204 | devname); | |
205 | return 1; | |
206 | } | |
207 | */ | |
208 | if (runstop>0) { | |
209 | if (ioctl(fd, RUN_ARRAY, ¶m)) { | |
210 | pr_err("failed to run array %s: %s\n", | |
211 | devname, strerror(errno)); | |
212 | return 1; | |
213 | } | |
214 | if (quiet <= 0) | |
215 | pr_err("started %s\n", devname); | |
216 | } else if (runstop < 0){ | |
217 | struct map_ent *map = NULL; | |
218 | struct stat stb; | |
219 | struct mdinfo *mdi; | |
220 | int devnum; | |
221 | int err; | |
222 | int count; | |
223 | /* If this is an mdmon managed array, just write 'inactive' | |
224 | * to the array state and let mdmon clear up. | |
225 | */ | |
226 | devnum = fd2devnum(fd); | |
227 | /* Get EXCL access first. If this fails, then attempting | |
228 | * to stop is probably a bad idea. | |
229 | */ | |
230 | close(fd); | |
231 | fd = open(devname, O_RDONLY|O_EXCL); | |
232 | if (fd < 0 || fd2devnum(fd) != devnum) { | |
233 | if (fd >= 0) | |
234 | close(fd); | |
235 | pr_err("Cannot get exclusive access to %s:" | |
236 | "Perhaps a running " | |
237 | "process, mounted filesystem " | |
238 | "or active volume group?\n", | |
239 | devname); | |
240 | return 1; | |
241 | } | |
242 | mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION); | |
243 | if (mdi && | |
244 | mdi->array.level > 0 && | |
245 | is_subarray(mdi->text_version)) { | |
246 | int err; | |
247 | /* This is mdmon managed. */ | |
248 | close(fd); | |
249 | ||
250 | count = 25; | |
251 | while (count && | |
252 | (err = sysfs_set_str(mdi, NULL, | |
253 | "array_state", | |
254 | "inactive")) < 0 | |
255 | && errno == EBUSY) { | |
256 | usleep(200000); | |
257 | count--; | |
258 | } | |
259 | if (err && !quiet) { | |
260 | pr_err("failed to stop array %s: %s\n", | |
261 | devname, strerror(errno)); | |
262 | rv = 1; | |
263 | goto out; | |
264 | } | |
265 | ||
266 | /* Give monitor a chance to act */ | |
267 | ping_monitor(mdi->text_version); | |
268 | ||
269 | fd = open_dev_excl(devnum); | |
270 | if (fd < 0) { | |
271 | pr_err("failed to completely stop %s" | |
272 | ": Device is busy\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' | |
285 | */ | |
286 | ping_monitor(mdi->sys_name); | |
287 | ||
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 | is_subarray(m->metadata_version+9) && | |
296 | devname2devnum(m->metadata_version+10) == devnum) { | |
297 | if (!quiet) | |
298 | pr_err("Cannot stop container %s: " | |
299 | "member %s still active\n", | |
300 | devname, m->dev); | |
301 | free_mdstat(mds); | |
302 | rv = 1; | |
303 | goto out; | |
304 | } | |
305 | } | |
306 | ||
307 | /* As we have an O_EXCL open, any use of the device | |
308 | * which blocks STOP_ARRAY is probably a transient use, | |
309 | * so it is reasonable to retry for a while - 5 seconds. | |
310 | */ | |
311 | count = 25; err = 0; | |
312 | while (count && fd >= 0 | |
313 | && (err = ioctl(fd, STOP_ARRAY, NULL)) < 0 | |
314 | && errno == EBUSY) { | |
315 | usleep(200000); | |
316 | count --; | |
317 | } | |
318 | if (fd >= 0 && err) { | |
319 | if (quiet == 0) { | |
320 | pr_err("failed to stop array %s: %s\n", | |
321 | devname, strerror(errno)); | |
322 | if (errno == EBUSY) | |
323 | fprintf(stderr, "Perhaps a running " | |
324 | "process, mounted filesystem " | |
325 | "or active volume group?\n"); | |
326 | } | |
327 | rv = 1; | |
328 | goto out; | |
329 | } | |
330 | /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array | |
331 | * was stopped, so We'll do it here just to be sure. Drop any | |
332 | * partitions as well... | |
333 | */ | |
334 | if (fd >= 0) | |
335 | ioctl(fd, BLKRRPART, 0); | |
336 | if (mdi) | |
337 | sysfs_uevent(mdi, "change"); | |
338 | ||
339 | ||
340 | if (devnum != NoMdDev && | |
341 | (stat("/dev/.udev", &stb) != 0 || | |
342 | check_env("MDADM_NO_UDEV"))) { | |
343 | struct map_ent *mp = map_by_devnum(&map, devnum); | |
344 | remove_devices(devnum, mp ? mp->path : NULL); | |
345 | } | |
346 | ||
347 | ||
348 | if (quiet <= 0) | |
349 | pr_err("stopped %s\n", devname); | |
350 | map_lock(&map); | |
351 | map_remove(&map, devnum); | |
352 | map_unlock(&map); | |
353 | out: | |
354 | if (mdi) | |
355 | sysfs_free(mdi); | |
356 | } | |
357 | return rv; | |
358 | } | |
359 | ||
360 | int Manage_resize(char *devname, int fd, long long size, int raid_disks) | |
361 | { | |
362 | mdu_array_info_t info; | |
363 | if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) { | |
364 | pr_err("Cannot get array information for %s: %s\n", | |
365 | devname, strerror(errno)); | |
366 | return 1; | |
367 | } | |
368 | if (size >= 0) | |
369 | info.size = size; | |
370 | if (raid_disks > 0) | |
371 | info.raid_disks = raid_disks; | |
372 | if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) { | |
373 | pr_err("Cannot set device size/shape for %s: %s\n", | |
374 | devname, strerror(errno)); | |
375 | return 1; | |
376 | } | |
377 | return 0; | |
378 | } | |
379 | ||
380 | int Manage_subdevs(char *devname, int fd, | |
381 | struct mddev_dev *devlist, int verbose, int test, | |
382 | char *update, int force) | |
383 | { | |
384 | /* do something to each dev. | |
385 | * devmode can be | |
386 | * 'a' - add the device | |
387 | * try HOT_ADD_DISK | |
388 | * If that fails EINVAL, try ADD_NEW_DISK | |
389 | * 'r' - remove the device HOT_REMOVE_DISK | |
390 | * device can be 'faulty' or 'detached' in which case all | |
391 | * matching devices are removed. | |
392 | * 'f' - set the device faulty SET_DISK_FAULTY | |
393 | * device can be 'detached' in which case any device that | |
394 | * is inaccessible will be marked faulty. | |
395 | * For 'f' and 'r', the device can also be a kernel-internal | |
396 | * name such as 'sdb'. | |
397 | */ | |
398 | struct mddev_dev *add_devlist = NULL; | |
399 | mdu_array_info_t array; | |
400 | mdu_disk_info_t disc; | |
401 | unsigned long long array_size; | |
402 | struct mddev_dev *dv, *next = NULL; | |
403 | struct stat stb; | |
404 | int j, jnext = 0; | |
405 | int tfd = -1; | |
406 | struct supertype *st, *tst; | |
407 | char *subarray = NULL; | |
408 | int duuid[4]; | |
409 | int ouuid[4]; | |
410 | int lfd = -1; | |
411 | int sysfd = -1; | |
412 | int count = 0; /* number of actions taken */ | |
413 | struct mdinfo info; | |
414 | int frozen = 0; | |
415 | ||
416 | if (ioctl(fd, GET_ARRAY_INFO, &array)) { | |
417 | pr_err("cannot get array info for %s\n", | |
418 | devname); | |
419 | goto abort; | |
420 | } | |
421 | sysfs_init(&info, fd, 0); | |
422 | ||
423 | /* array.size is only 32 bit and may be truncated. | |
424 | * So read from sysfs if possible, and record number of sectors | |
425 | */ | |
426 | ||
427 | array_size = get_component_size(fd); | |
428 | if (array_size <= 0) | |
429 | array_size = array.size * 2; | |
430 | ||
431 | tst = super_by_fd(fd, &subarray); | |
432 | if (!tst) { | |
433 | pr_err("unsupport array - version %d.%d\n", | |
434 | array.major_version, array.minor_version); | |
435 | goto abort; | |
436 | } | |
437 | ||
438 | stb.st_rdev = 0; | |
439 | for (dv = devlist, j=0 ; dv; dv = next, j = jnext) { | |
440 | unsigned long long ldsize; | |
441 | char dvname[20]; | |
442 | char *dnprintable = dv->devname; | |
443 | char *add_dev = dv->devname; | |
444 | int err; | |
445 | int array_failed; | |
446 | ||
447 | next = dv->next; | |
448 | jnext = 0; | |
449 | ||
450 | if (strcmp(dv->devname, "failed")==0 || | |
451 | strcmp(dv->devname, "faulty")==0) { | |
452 | int remaining_disks = array.nr_disks; | |
453 | if (dv->disposition != 'r') { | |
454 | pr_err("%s only meaningful " | |
455 | "with -r, not -%c\n", | |
456 | dv->devname, dv->disposition); | |
457 | goto abort; | |
458 | } | |
459 | for (; j < MAX_DISKS && remaining_disks > 0; j++) { | |
460 | unsigned dev; | |
461 | disc.number = j; | |
462 | if (ioctl(fd, GET_DISK_INFO, &disc)) | |
463 | continue; | |
464 | if (disc.major == 0 && disc.minor == 0) | |
465 | continue; | |
466 | remaining_disks --; | |
467 | if ((disc.state & 1) == 0) /* faulty */ | |
468 | continue; | |
469 | dev = makedev(disc.major, disc.minor); | |
470 | if (stb.st_rdev == dev) | |
471 | /* already did that one */ | |
472 | continue; | |
473 | stb.st_rdev = dev; | |
474 | next = dv; | |
475 | /* same slot again next time - things might | |
476 | * have reshuffled */ | |
477 | jnext = j; | |
478 | sprintf(dvname,"%d:%d", disc.major, disc.minor); | |
479 | dnprintable = dvname; | |
480 | break; | |
481 | } | |
482 | if (next != dv) | |
483 | continue; | |
484 | } else if (strcmp(dv->devname, "detached") == 0) { | |
485 | int remaining_disks = array.nr_disks; | |
486 | if (dv->disposition != 'r' && dv->disposition != 'f') { | |
487 | pr_err("%s only meaningful " | |
488 | "with -r of -f, not -%c\n", | |
489 | dv->devname, dv->disposition); | |
490 | goto abort; | |
491 | } | |
492 | for (; j < MAX_DISKS && remaining_disks > 0; j++) { | |
493 | int sfd; | |
494 | unsigned dev; | |
495 | disc.number = j; | |
496 | if (ioctl(fd, GET_DISK_INFO, &disc)) | |
497 | continue; | |
498 | if (disc.major == 0 && disc.minor == 0) | |
499 | continue; | |
500 | remaining_disks --; | |
501 | sprintf(dvname,"%d:%d", disc.major, disc.minor); | |
502 | sfd = dev_open(dvname, O_RDONLY); | |
503 | if (sfd >= 0) { | |
504 | close(sfd); | |
505 | continue; | |
506 | } | |
507 | if (dv->disposition == 'f' && | |
508 | (disc.state & 1) == 1) /* already faulty */ | |
509 | continue; | |
510 | if (errno != ENXIO) | |
511 | continue; | |
512 | dev = makedev(disc.major, disc.minor); | |
513 | if (stb.st_rdev == dev) | |
514 | /* already did that one */ | |
515 | continue; | |
516 | stb.st_rdev = dev; | |
517 | next = dv; | |
518 | /* same slot again next time - things might | |
519 | * have reshuffled */ | |
520 | jnext = j; | |
521 | dnprintable = dvname; | |
522 | break; | |
523 | } | |
524 | if (next != dv) | |
525 | continue; | |
526 | } else if (strcmp(dv->devname, "missing") == 0) { | |
527 | if (dv->disposition != 'A') { | |
528 | pr_err("'missing' only meaningful " | |
529 | "with --re-add\n"); | |
530 | goto abort; | |
531 | } | |
532 | if (add_devlist == NULL) | |
533 | add_devlist = conf_get_devs(); | |
534 | if (add_devlist == NULL) { | |
535 | pr_err("no devices to scan for missing members."); | |
536 | continue; | |
537 | } | |
538 | add_dev = add_devlist->devname; | |
539 | add_devlist = add_devlist->next; | |
540 | if (add_devlist != NULL) | |
541 | next = dv; | |
542 | if (stat(add_dev, &stb) < 0) | |
543 | continue; | |
544 | } else if (strchr(dv->devname, '/') == NULL && | |
545 | strchr(dv->devname, ':') == NULL && | |
546 | strlen(dv->devname) < 50) { | |
547 | /* Assume this is a kernel-internal name like 'sda1' */ | |
548 | int found = 0; | |
549 | char dname[55]; | |
550 | if (dv->disposition != 'r' && dv->disposition != 'f') { | |
551 | pr_err("%s only meaningful " | |
552 | "with -r or -f, not -%c\n", | |
553 | dv->devname, dv->disposition); | |
554 | goto abort; | |
555 | } | |
556 | ||
557 | sprintf(dname, "dev-%s", dv->devname); | |
558 | sysfd = sysfs_open(fd2devnum(fd), dname, "block/dev"); | |
559 | if (sysfd >= 0) { | |
560 | char dn[20]; | |
561 | int mj,mn; | |
562 | if (sysfs_fd_get_str(sysfd, dn, 20) > 0 && | |
563 | sscanf(dn, "%d:%d", &mj,&mn) == 2) { | |
564 | stb.st_rdev = makedev(mj,mn); | |
565 | found = 1; | |
566 | } | |
567 | close(sysfd); | |
568 | sysfd = -1; | |
569 | } | |
570 | if (!found) { | |
571 | sysfd = sysfs_open(fd2devnum(fd), dname, "state"); | |
572 | if (sysfd < 0) { | |
573 | pr_err("%s does not appear " | |
574 | "to be a component of %s\n", | |
575 | dv->devname, devname); | |
576 | goto abort; | |
577 | } | |
578 | } | |
579 | } else { | |
580 | j = 0; | |
581 | ||
582 | tfd = dev_open(dv->devname, O_RDONLY); | |
583 | if (tfd < 0 && dv->disposition == 'r' && | |
584 | lstat(dv->devname, &stb) == 0) | |
585 | /* Be happy, the lstat worked, that is | |
586 | * enough for --remove | |
587 | */ | |
588 | ; | |
589 | else { | |
590 | if (tfd < 0 || fstat(tfd, &stb) != 0) { | |
591 | pr_err("cannot find %s: %s\n", | |
592 | dv->devname, strerror(errno)); | |
593 | if (tfd >= 0) | |
594 | close(tfd); | |
595 | goto abort; | |
596 | } | |
597 | close(tfd); | |
598 | tfd = -1; | |
599 | } | |
600 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
601 | pr_err("%s is not a " | |
602 | "block device.\n", | |
603 | dv->devname); | |
604 | goto abort; | |
605 | } | |
606 | } | |
607 | switch(dv->disposition){ | |
608 | default: | |
609 | pr_err("internal error - devmode[%s]=%d\n", | |
610 | dv->devname, dv->disposition); | |
611 | goto abort; | |
612 | case 'a': | |
613 | case 'A': | |
614 | /* add the device */ | |
615 | if (subarray) { | |
616 | pr_err("Cannot add disks to a" | |
617 | " \'member\' array, perform this" | |
618 | " operation on the parent container\n"); | |
619 | goto abort; | |
620 | } | |
621 | /* Make sure it isn't in use (in 2.6 or later) */ | |
622 | tfd = dev_open(add_dev, O_RDONLY|O_EXCL|O_DIRECT); | |
623 | if (tfd < 0 && add_dev != dv->devname) | |
624 | continue; | |
625 | if (tfd < 0) { | |
626 | pr_err("Cannot open %s: %s\n", | |
627 | dv->devname, strerror(errno)); | |
628 | goto abort; | |
629 | } | |
630 | if (!frozen) { | |
631 | if (sysfs_freeze_array(&info) == 1) | |
632 | frozen = 1; | |
633 | else | |
634 | frozen = -1; | |
635 | } | |
636 | ||
637 | st = dup_super(tst); | |
638 | ||
639 | if (array.not_persistent==0) | |
640 | st->ss->load_super(st, tfd, NULL); | |
641 | ||
642 | if (add_dev == dv->devname) { | |
643 | if (!get_dev_size(tfd, dv->devname, &ldsize)) { | |
644 | st->ss->free_super(st); | |
645 | close(tfd); | |
646 | goto abort; | |
647 | } | |
648 | } else if (!get_dev_size(tfd, NULL, &ldsize)) { | |
649 | st->ss->free_super(st); | |
650 | close(tfd); | |
651 | tfd = -1; | |
652 | continue; | |
653 | } | |
654 | ||
655 | if (tst->ss->validate_geometry( | |
656 | tst, array.level, array.layout, | |
657 | array.raid_disks, NULL, | |
658 | ldsize >> 9, NULL, NULL, 0) == 0) { | |
659 | if (!force) { | |
660 | pr_err("%s is larger than %s can " | |
661 | "effectively use.\n" | |
662 | " Add --force is you " | |
663 | "really want to add this device.\n", | |
664 | add_dev, devname); | |
665 | st->ss->free_super(st); | |
666 | close(tfd); | |
667 | goto abort; | |
668 | } | |
669 | pr_err("%s is larger than %s can " | |
670 | "effectively use.\n" | |
671 | " Adding anyway as --force " | |
672 | "was given.\n", | |
673 | add_dev, devname); | |
674 | } | |
675 | if (!tst->ss->external && | |
676 | array.major_version == 0 && | |
677 | md_get_version(fd)%100 < 2) { | |
678 | close(tfd); | |
679 | st->ss->free_super(st); | |
680 | tfd = -1; | |
681 | if (ioctl(fd, HOT_ADD_DISK, | |
682 | (unsigned long)stb.st_rdev)==0) { | |
683 | if (verbose >= 0) | |
684 | pr_err("hot added %s\n", | |
685 | add_dev); | |
686 | continue; | |
687 | } | |
688 | ||
689 | pr_err("hot add failed for %s: %s\n", | |
690 | add_dev, strerror(errno)); | |
691 | goto abort; | |
692 | } | |
693 | ||
694 | if (array.not_persistent == 0 || tst->ss->external) { | |
695 | ||
696 | /* need to find a sample superblock to copy, and | |
697 | * a spare slot to use. | |
698 | * For 'external' array (well, container based), | |
699 | * We can just load the metadata for the array. | |
700 | */ | |
701 | if (tst->sb) | |
702 | /* already loaded */; | |
703 | else if (tst->ss->external) { | |
704 | tst->ss->load_container(tst, fd, NULL); | |
705 | } else for (j = 0; j < tst->max_devs; j++) { | |
706 | char *dev; | |
707 | int dfd; | |
708 | disc.number = j; | |
709 | if (ioctl(fd, GET_DISK_INFO, &disc)) | |
710 | continue; | |
711 | if (disc.major==0 && disc.minor==0) | |
712 | continue; | |
713 | if ((disc.state & 4)==0) continue; /* sync */ | |
714 | /* Looks like a good device to try */ | |
715 | dev = map_dev(disc.major, disc.minor, 1); | |
716 | if (!dev) continue; | |
717 | dfd = dev_open(dev, O_RDONLY); | |
718 | if (dfd < 0) continue; | |
719 | if (tst->ss->load_super(tst, dfd, | |
720 | NULL)) { | |
721 | close(dfd); | |
722 | continue; | |
723 | } | |
724 | close(dfd); | |
725 | break; | |
726 | } | |
727 | /* FIXME this is a bad test to be using */ | |
728 | if (!tst->sb && dv->disposition == 'A') { | |
729 | /* we are re-adding a device to a | |
730 | * completely dead array - have to depend | |
731 | * on kernel to check | |
732 | */ | |
733 | } else if (!tst->sb) { | |
734 | close(tfd); | |
735 | st->ss->free_super(st); | |
736 | pr_err("cannot load array metadata from %s\n", devname); | |
737 | goto abort; | |
738 | } | |
739 | ||
740 | /* Make sure device is large enough */ | |
741 | if (tst->ss->avail_size(tst, ldsize/512) < | |
742 | array_size) { | |
743 | close(tfd); | |
744 | tfd = -1; | |
745 | st->ss->free_super(st); | |
746 | if (add_dev != dv->devname) | |
747 | continue; | |
748 | pr_err("%s not large enough to join array\n", | |
749 | dv->devname); | |
750 | goto abort; | |
751 | } | |
752 | ||
753 | /* Possibly this device was recently part of the array | |
754 | * and was temporarily removed, and is now being re-added. | |
755 | * If so, we can simply re-add it. | |
756 | */ | |
757 | ||
758 | if (st->sb) { | |
759 | struct mdinfo mdi; | |
760 | st->ss->getinfo_super(st, &mdi, NULL); | |
761 | st->ss->uuid_from_super(st, ouuid); | |
762 | if (tst->sb) | |
763 | tst->ss->uuid_from_super(tst, duuid); | |
764 | else | |
765 | /* Assume uuid matches: kernel will check */ | |
766 | memcpy(duuid, ouuid, sizeof(ouuid)); | |
767 | if ((mdi.disk.state & (1<<MD_DISK_ACTIVE)) && | |
768 | !(mdi.disk.state & (1<<MD_DISK_FAULTY)) && | |
769 | memcmp(duuid, ouuid, sizeof(ouuid))==0) { | |
770 | /* look like it is worth a try. Need to | |
771 | * make sure kernel will accept it though. | |
772 | */ | |
773 | /* re-add doesn't work for version-1 superblocks | |
774 | * before 2.6.18 :-( | |
775 | */ | |
776 | if (array.major_version == 1 && | |
777 | get_linux_version() <= 2006018) | |
778 | goto skip_re_add; | |
779 | disc.number = mdi.disk.number; | |
780 | if (ioctl(fd, GET_DISK_INFO, &disc) != 0 | |
781 | || disc.major != 0 || disc.minor != 0 | |
782 | ) | |
783 | goto skip_re_add; | |
784 | disc.major = major(stb.st_rdev); | |
785 | disc.minor = minor(stb.st_rdev); | |
786 | disc.number = mdi.disk.number; | |
787 | disc.raid_disk = mdi.disk.raid_disk; | |
788 | disc.state = mdi.disk.state; | |
789 | if (dv->writemostly == 1) | |
790 | disc.state |= 1 << MD_DISK_WRITEMOSTLY; | |
791 | if (dv->writemostly == 2) | |
792 | disc.state &= ~(1 << MD_DISK_WRITEMOSTLY); | |
793 | remove_partitions(tfd); | |
794 | close(tfd); | |
795 | tfd = -1; | |
796 | if (update || dv->writemostly > 0) { | |
797 | int rv = -1; | |
798 | tfd = dev_open(dv->devname, O_RDWR); | |
799 | if (tfd < 0) { | |
800 | pr_err("failed to open %s for" | |
801 | " superblock update during re-add\n", dv->devname); | |
802 | st->ss->free_super(st); | |
803 | goto abort; | |
804 | } | |
805 | ||
806 | if (dv->writemostly == 1) | |
807 | rv = st->ss->update_super( | |
808 | st, NULL, "writemostly", | |
809 | devname, verbose, 0, NULL); | |
810 | if (dv->writemostly == 2) | |
811 | rv = st->ss->update_super( | |
812 | st, NULL, "readwrite", | |
813 | devname, verbose, 0, NULL); | |
814 | if (update) | |
815 | rv = st->ss->update_super( | |
816 | st, NULL, update, | |
817 | devname, verbose, 0, NULL); | |
818 | if (rv == 0) | |
819 | rv = st->ss->store_super(st, tfd); | |
820 | close(tfd); | |
821 | tfd = -1; | |
822 | if (rv != 0) { | |
823 | pr_err("failed to update" | |
824 | " superblock during re-add\n"); | |
825 | st->ss->free_super(st); | |
826 | goto abort; | |
827 | } | |
828 | } | |
829 | /* don't even try if disk is marked as faulty */ | |
830 | errno = 0; | |
831 | if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) { | |
832 | if (verbose >= 0) | |
833 | pr_err("re-added %s\n", add_dev); | |
834 | count++; | |
835 | st->ss->free_super(st); | |
836 | continue; | |
837 | } | |
838 | if (errno == ENOMEM || errno == EROFS) { | |
839 | pr_err("add new device failed for %s: %s\n", | |
840 | add_dev, strerror(errno)); | |
841 | st->ss->free_super(st); | |
842 | if (add_dev != dv->devname) | |
843 | continue; | |
844 | goto abort; | |
845 | } | |
846 | } | |
847 | skip_re_add: | |
848 | st->ss->free_super(st); | |
849 | } | |
850 | if (add_dev != dv->devname) { | |
851 | if (verbose > 0) | |
852 | pr_err("--re-add for %s to %s is not possible\n", | |
853 | add_dev, devname); | |
854 | if (tfd >= 0) { | |
855 | close(tfd); | |
856 | tfd = -1; | |
857 | } | |
858 | continue; | |
859 | } | |
860 | if (dv->disposition == 'A') { | |
861 | if (tfd >= 0) | |
862 | close(tfd); | |
863 | pr_err("--re-add for %s to %s is not possible\n", | |
864 | dv->devname, devname); | |
865 | goto abort; | |
866 | } | |
867 | if (array.active_disks < array.raid_disks) { | |
868 | char *avail = xcalloc(array.raid_disks, 1); | |
869 | int d; | |
870 | int found = 0; | |
871 | ||
872 | for (d = 0; d < MAX_DISKS && found < array.active_disks; d++) { | |
873 | disc.number = d; | |
874 | if (ioctl(fd, GET_DISK_INFO, &disc)) | |
875 | continue; | |
876 | if (disc.major == 0 && disc.minor == 0) | |
877 | continue; | |
878 | if (!(disc.state & (1<<MD_DISK_SYNC))) | |
879 | continue; | |
880 | avail[disc.raid_disk] = 1; | |
881 | found++; | |
882 | } | |
883 | array_failed = !enough(array.level, array.raid_disks, | |
884 | array.layout, 1, avail); | |
885 | } else | |
886 | array_failed = 0; | |
887 | if (array_failed) { | |
888 | pr_err("%s has failed so using --add cannot work and might destroy\n", | |
889 | devname); | |
890 | pr_err("data on %s. You should stop the array and re-assemble it.\n", | |
891 | dv->devname); | |
892 | if (tfd >= 0) | |
893 | close(tfd); | |
894 | goto abort; | |
895 | } | |
896 | } else { | |
897 | /* non-persistent. Must ensure that new drive | |
898 | * is at least array.size big. | |
899 | */ | |
900 | if (ldsize/512 < array_size) { | |
901 | pr_err("%s not large enough to join array\n", | |
902 | dv->devname); | |
903 | if (tfd >= 0) | |
904 | close(tfd); | |
905 | goto abort; | |
906 | } | |
907 | } | |
908 | /* committed to really trying this device now*/ | |
909 | if (tfd >= 0) { | |
910 | remove_partitions(tfd); | |
911 | close(tfd); | |
912 | tfd = -1; | |
913 | } | |
914 | /* in 2.6.17 and earlier, version-1 superblocks won't | |
915 | * use the number we write, but will choose a free number. | |
916 | * we must choose the same free number, which requires | |
917 | * starting at 'raid_disks' and counting up | |
918 | */ | |
919 | for (j = array.raid_disks; j< tst->max_devs; j++) { | |
920 | disc.number = j; | |
921 | if (ioctl(fd, GET_DISK_INFO, &disc)) | |
922 | break; | |
923 | if (disc.major==0 && disc.minor==0) | |
924 | break; | |
925 | if (disc.state & 8) /* removed */ | |
926 | break; | |
927 | } | |
928 | disc.major = major(stb.st_rdev); | |
929 | disc.minor = minor(stb.st_rdev); | |
930 | disc.number =j; | |
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, | |
938 | dv->devname)) { | |
939 | close(dfd); | |
940 | goto abort; | |
941 | } | |
942 | if (tst->ss->write_init_super(tst)) { | |
943 | close(dfd); | |
944 | goto abort; | |
945 | } | |
946 | } else if (dv->disposition == 'A') { | |
947 | /* this had better be raid1. | |
948 | * As we are "--re-add"ing we must find a spare slot | |
949 | * to fill. | |
950 | */ | |
951 | char *used = xcalloc(array.raid_disks, 1); | |
952 | for (j=0; j< tst->max_devs; j++) { | |
953 | mdu_disk_info_t disc2; | |
954 | disc2.number = j; | |
955 | if (ioctl(fd, GET_DISK_INFO, &disc2)) | |
956 | continue; | |
957 | if (disc2.major==0 && disc2.minor==0) | |
958 | continue; | |
959 | if (disc2.state & 8) /* removed */ | |
960 | continue; | |
961 | if (disc2.raid_disk < 0) | |
962 | continue; | |
963 | if (disc2.raid_disk > array.raid_disks) | |
964 | continue; | |
965 | used[disc2.raid_disk] = 1; | |
966 | } | |
967 | for (j=0 ; j<array.raid_disks; j++) | |
968 | if (!used[j]) { | |
969 | disc.raid_disk = j; | |
970 | disc.state |= (1<<MD_DISK_SYNC); | |
971 | break; | |
972 | } | |
973 | free(used); | |
974 | } | |
975 | if (dv->writemostly == 1) | |
976 | disc.state |= (1 << MD_DISK_WRITEMOSTLY); | |
977 | if (tst->ss->external) { | |
978 | /* add a disk | |
979 | * to an external metadata container */ | |
980 | struct mdinfo new_mdi; | |
981 | struct mdinfo *sra; | |
982 | int container_fd; | |
983 | int devnum = fd2devnum(fd); | |
984 | int dfd; | |
985 | ||
986 | container_fd = open_dev_excl(devnum); | |
987 | if (container_fd < 0) { | |
988 | pr_err("add failed for %s:" | |
989 | " could not get exclusive access to container\n", | |
990 | dv->devname); | |
991 | tst->ss->free_super(tst); | |
992 | goto abort; | |
993 | } | |
994 | ||
995 | dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT); | |
996 | if (mdmon_running(tst->container_dev)) | |
997 | tst->update_tail = &tst->updates; | |
998 | if (tst->ss->add_to_super(tst, &disc, dfd, | |
999 | dv->devname)) { | |
1000 | close(dfd); | |
1001 | close(container_fd); | |
1002 | goto abort; | |
1003 | } | |
1004 | if (tst->update_tail) | |
1005 | flush_metadata_updates(tst); | |
1006 | else | |
1007 | tst->ss->sync_metadata(tst); | |
1008 | ||
1009 | sra = sysfs_read(container_fd, -1, 0); | |
1010 | if (!sra) { | |
1011 | pr_err("add failed for %s: sysfs_read failed\n", | |
1012 | dv->devname); | |
1013 | close(container_fd); | |
1014 | tst->ss->free_super(tst); | |
1015 | goto abort; | |
1016 | } | |
1017 | sra->array.level = LEVEL_CONTAINER; | |
1018 | /* Need to set data_offset and component_size */ | |
1019 | tst->ss->getinfo_super(tst, &new_mdi, NULL); | |
1020 | new_mdi.disk.major = disc.major; | |
1021 | new_mdi.disk.minor = disc.minor; | |
1022 | new_mdi.recovery_start = 0; | |
1023 | /* Make sure fds are closed as they are O_EXCL which | |
1024 | * would block add_disk */ | |
1025 | tst->ss->free_super(tst); | |
1026 | if (sysfs_add_disk(sra, &new_mdi, 0) != 0) { | |
1027 | pr_err("add new device to external metadata" | |
1028 | " failed for %s\n", dv->devname); | |
1029 | close(container_fd); | |
1030 | sysfs_free(sra); | |
1031 | goto abort; | |
1032 | } | |
1033 | ping_monitor_by_id(devnum); | |
1034 | sysfs_free(sra); | |
1035 | close(container_fd); | |
1036 | } else { | |
1037 | tst->ss->free_super(tst); | |
1038 | if (ioctl(fd, ADD_NEW_DISK, &disc)) { | |
1039 | pr_err("add new device failed for %s as %d: %s\n", | |
1040 | dv->devname, j, strerror(errno)); | |
1041 | goto abort; | |
1042 | } | |
1043 | } | |
1044 | if (verbose >= 0) | |
1045 | pr_err("added %s\n", dv->devname); | |
1046 | break; | |
1047 | ||
1048 | case 'r': | |
1049 | /* hot remove */ | |
1050 | if (subarray) { | |
1051 | pr_err("Cannot remove disks from a" | |
1052 | " \'member\' array, perform this" | |
1053 | " operation on the parent container\n"); | |
1054 | if (sysfd >= 0) | |
1055 | close(sysfd); | |
1056 | goto abort; | |
1057 | } | |
1058 | if (tst->ss->external) { | |
1059 | /* To remove a device from a container, we must | |
1060 | * check that it isn't in use in an array. | |
1061 | * This involves looking in the 'holders' | |
1062 | * directory - there must be just one entry, | |
1063 | * the container. | |
1064 | * To ensure that it doesn't get used as a | |
1065 | * hold spare while we are checking, we | |
1066 | * get an O_EXCL open on the container | |
1067 | */ | |
1068 | int dnum = fd2devnum(fd); | |
1069 | lfd = open_dev_excl(dnum); | |
1070 | if (lfd < 0) { | |
1071 | pr_err("Cannot get exclusive access " | |
1072 | " to container - odd\n"); | |
1073 | if (sysfd >= 0) | |
1074 | close(sysfd); | |
1075 | goto abort; | |
1076 | } | |
1077 | /* in the detached case it is not possible to | |
1078 | * check if we are the unique holder, so just | |
1079 | * rely on the 'detached' checks | |
1080 | */ | |
1081 | if (strcmp(dv->devname, "detached") == 0 || | |
1082 | sysfd >= 0 || | |
1083 | sysfs_unique_holder(dnum, stb.st_rdev)) | |
1084 | /* pass */; | |
1085 | else { | |
1086 | pr_err("%s is %s, cannot remove.\n", | |
1087 | dnprintable, | |
1088 | errno == EEXIST ? "still in use": | |
1089 | "not a member"); | |
1090 | close(lfd); | |
1091 | goto abort; | |
1092 | } | |
1093 | } | |
1094 | /* FIXME check that it is a current member */ | |
1095 | if (sysfd >= 0) { | |
1096 | /* device has been removed and we don't know | |
1097 | * the major:minor number | |
1098 | */ | |
1099 | int n = write(sysfd, "remove", 6); | |
1100 | if (n != 6) | |
1101 | err = -1; | |
1102 | else | |
1103 | err = 0; | |
1104 | close(sysfd); | |
1105 | sysfd = -1; | |
1106 | } else { | |
1107 | err = ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev); | |
1108 | if (err && errno == ENODEV) { | |
1109 | /* Old kernels rejected this if no personality | |
1110 | * registered */ | |
1111 | struct mdinfo *sra = sysfs_read(fd, 0, GET_DEVS); | |
1112 | struct mdinfo *dv = NULL; | |
1113 | if (sra) | |
1114 | dv = sra->devs; | |
1115 | for ( ; dv ; dv=dv->next) | |
1116 | if (dv->disk.major == (int)major(stb.st_rdev) && | |
1117 | dv->disk.minor == (int)minor(stb.st_rdev)) | |
1118 | break; | |
1119 | if (dv) | |
1120 | err = sysfs_set_str(sra, dv, | |
1121 | "state", "remove"); | |
1122 | else | |
1123 | err = -1; | |
1124 | if (sra) | |
1125 | sysfs_free(sra); | |
1126 | } | |
1127 | } | |
1128 | if (err) { | |
1129 | pr_err("hot remove failed " | |
1130 | "for %s: %s\n", dnprintable, | |
1131 | strerror(errno)); | |
1132 | if (lfd >= 0) | |
1133 | close(lfd); | |
1134 | goto abort; | |
1135 | } | |
1136 | if (tst->ss->external) { | |
1137 | /* | |
1138 | * Before dropping our exclusive open we make an | |
1139 | * attempt at preventing mdmon from seeing an | |
1140 | * 'add' event before reconciling this 'remove' | |
1141 | * event. | |
1142 | */ | |
1143 | char *name = devnum2devname(fd2devnum(fd)); | |
1144 | ||
1145 | if (!name) { | |
1146 | pr_err("unable to get container name\n"); | |
1147 | goto abort; | |
1148 | } | |
1149 | ||
1150 | ping_manager(name); | |
1151 | free(name); | |
1152 | } | |
1153 | if (lfd >= 0) | |
1154 | close(lfd); | |
1155 | count++; | |
1156 | if (verbose >= 0) | |
1157 | pr_err("hot removed %s from %s\n", | |
1158 | dnprintable, devname); | |
1159 | break; | |
1160 | ||
1161 | case 'f': /* set faulty */ | |
1162 | /* FIXME check current member */ | |
1163 | if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) || | |
1164 | (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY, | |
1165 | (unsigned long) stb.st_rdev))) { | |
1166 | pr_err("set device faulty failed for %s: %s\n", | |
1167 | dnprintable, strerror(errno)); | |
1168 | if (sysfd >= 0) | |
1169 | close(sysfd); | |
1170 | goto abort; | |
1171 | } | |
1172 | if (sysfd >= 0) | |
1173 | close(sysfd); | |
1174 | sysfd = -1; | |
1175 | count++; | |
1176 | if (verbose >= 0) | |
1177 | pr_err("set %s faulty in %s\n", | |
1178 | dnprintable, devname); | |
1179 | break; | |
1180 | } | |
1181 | } | |
1182 | if (frozen > 0) | |
1183 | sysfs_set_str(&info, NULL, "sync_action","idle"); | |
1184 | if (test && count == 0) | |
1185 | return 2; | |
1186 | return 0; | |
1187 | ||
1188 | abort: | |
1189 | if (frozen > 0) | |
1190 | sysfs_set_str(&info, NULL, "sync_action","idle"); | |
1191 | return 1; | |
1192 | } | |
1193 | ||
1194 | int autodetect(void) | |
1195 | { | |
1196 | /* Open any md device, and issue the RAID_AUTORUN ioctl */ | |
1197 | int rv = 1; | |
1198 | int fd = dev_open("9:0", O_RDONLY); | |
1199 | if (fd >= 0) { | |
1200 | if (ioctl(fd, RAID_AUTORUN, 0) == 0) | |
1201 | rv = 0; | |
1202 | close(fd); | |
1203 | } | |
1204 | return rv; | |
1205 | } | |
1206 | ||
1207 | int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident *ident, int quiet) | |
1208 | { | |
1209 | struct supertype supertype, *st = &supertype; | |
1210 | int fd, rv = 2; | |
1211 | ||
1212 | memset(st, 0, sizeof(*st)); | |
1213 | ||
1214 | fd = open_subarray(dev, subarray, st, quiet); | |
1215 | if (fd < 0) | |
1216 | return 2; | |
1217 | ||
1218 | if (!st->ss->update_subarray) { | |
1219 | if (!quiet) | |
1220 | pr_err("Operation not supported for %s metadata\n", | |
1221 | st->ss->name); | |
1222 | goto free_super; | |
1223 | } | |
1224 | ||
1225 | if (mdmon_running(st->devnum)) | |
1226 | st->update_tail = &st->updates; | |
1227 | ||
1228 | rv = st->ss->update_subarray(st, subarray, update, ident); | |
1229 | ||
1230 | if (rv) { | |
1231 | if (!quiet) | |
1232 | pr_err("Failed to update %s of subarray-%s in %s\n", | |
1233 | update, subarray, dev); | |
1234 | } else if (st->update_tail) | |
1235 | flush_metadata_updates(st); | |
1236 | else | |
1237 | st->ss->sync_metadata(st); | |
1238 | ||
1239 | if (rv == 0 && strcmp(update, "name") == 0 && !quiet) | |
1240 | pr_err("Updated subarray-%s name from %s, UUIDs may have changed\n", | |
1241 | subarray, dev); | |
1242 | ||
1243 | free_super: | |
1244 | st->ss->free_super(st); | |
1245 | close(fd); | |
1246 | ||
1247 | return rv; | |
1248 | } | |
1249 | ||
1250 | /* Move spare from one array to another | |
1251 | * If adding to destination array fails | |
1252 | * add back to original array | |
1253 | * Returns 1 on success, 0 on failure */ | |
1254 | int move_spare(char *from_devname, char *to_devname, dev_t devid) | |
1255 | { | |
1256 | struct mddev_dev devlist; | |
1257 | char devname[20]; | |
1258 | ||
1259 | /* try to remove and add */ | |
1260 | int fd1 = open(to_devname, O_RDONLY); | |
1261 | int fd2 = open(from_devname, O_RDONLY); | |
1262 | ||
1263 | if (fd1 < 0 || fd2 < 0) { | |
1264 | if (fd1>=0) close(fd1); | |
1265 | if (fd2>=0) close(fd2); | |
1266 | return 0; | |
1267 | } | |
1268 | ||
1269 | devlist.next = NULL; | |
1270 | devlist.used = 0; | |
1271 | devlist.writemostly = 0; | |
1272 | devlist.devname = devname; | |
1273 | sprintf(devname, "%d:%d", major(devid), minor(devid)); | |
1274 | ||
1275 | devlist.disposition = 'r'; | |
1276 | if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0) == 0) { | |
1277 | devlist.disposition = 'a'; | |
1278 | if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0, NULL, 0) == 0) { | |
1279 | /* make sure manager is aware of changes */ | |
1280 | ping_manager(to_devname); | |
1281 | ping_manager(from_devname); | |
1282 | close(fd1); | |
1283 | close(fd2); | |
1284 | return 1; | |
1285 | } | |
1286 | else Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0); | |
1287 | } | |
1288 | close(fd1); | |
1289 | close(fd2); | |
1290 | return 0; | |
1291 | } | |
1292 | #endif |