]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Manage.c
Merge branch 'master' into devel-3.0
[thirdparty/mdadm.git] / Manage.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2006 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@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
30 #include "mdadm.h"
31 #include "md_u.h"
32 #include "md_p.h"
33 #include <ctype.h>
34
35 #define REGISTER_DEV _IO (MD_MAJOR, 1)
36 #define START_MD _IO (MD_MAJOR, 2)
37 #define STOP_MD _IO (MD_MAJOR, 3)
38
39 int Manage_ro(char *devname, int fd, int readonly)
40 {
41 /* switch to readonly or rw
42 *
43 * requires >= 0.90.0
44 * first check that array is runing
45 * use RESTART_ARRAY_RW or STOP_ARRAY_RO
46 *
47 */
48 mdu_array_info_t array;
49 #ifndef MDASSEMBLE
50 struct mdinfo *mdi;
51 #endif
52
53 if (md_get_version(fd) < 9000) {
54 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
55 return 1;
56 }
57 #ifndef MDASSEMBLE
58 /* If this is an externally-manage array, we need to modify the
59 * metadata_version so that mdmon doesn't undo our change.
60 */
61 mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
62 if (mdi &&
63 mdi->array.major_version == -1 &&
64 mdi->array.level > 0 &&
65 is_subarray(mdi->text_version)) {
66 char vers[64];
67 strcpy(vers, "external:");
68 strcat(vers, mdi->text_version);
69 if (readonly > 0) {
70 int rv;
71 /* We set readonly ourselves. */
72 vers[9] = '-';
73 sysfs_set_str(mdi, NULL, "metadata_version", vers);
74
75 close(fd);
76 rv = sysfs_set_str(mdi, NULL, "array_state", "readonly");
77
78 if (rv < 0) {
79 fprintf(stderr, Name ": failed to set readonly for %s: %s\n",
80 devname, strerror(errno));
81
82 vers[9] = mdi->text_version[0];
83 sysfs_set_str(mdi, NULL, "metadata_version", vers);
84 return 1;
85 }
86 } else {
87 char *cp;
88 /* We cannot set read/write - must signal mdmon */
89 vers[9] = '/';
90 sysfs_set_str(mdi, NULL, "metadata_version", vers);
91
92 cp = strchr(vers+10, '/');
93 if (*cp)
94 *cp = 0;
95 ping_monitor(vers+10);
96 }
97 return 0;
98 }
99 #endif
100 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
101 fprintf(stderr, Name ": %s does not appear to be active.\n",
102 devname);
103 return 1;
104 }
105
106 if (readonly>0) {
107 if (ioctl(fd, STOP_ARRAY_RO, NULL)) {
108 fprintf(stderr, Name ": failed to set readonly for %s: %s\n",
109 devname, strerror(errno));
110 return 1;
111 }
112 } else if (readonly < 0) {
113 if (ioctl(fd, RESTART_ARRAY_RW, NULL)) {
114 fprintf(stderr, Name ": failed to set writable for %s: %s\n",
115 devname, strerror(errno));
116 return 1;
117 }
118 }
119 return 0;
120 }
121
122 #ifndef MDASSEMBLE
123
124 static void remove_devices(int devnum, char *path)
125 {
126 /* Remove all 'standard' devices for 'devnum', including
127 * partitions. Also remove names at 'path' - possibly with
128 * partition suffixes - which link to those names.
129 */
130 char base[40];
131 char *path2;
132 char link[1024];
133 int n;
134 int part;
135 char *be;
136 char *pe;
137
138 if (devnum >= 0)
139 sprintf(base, "/dev/md%d", devnum);
140 else
141 sprintf(base, "/dev/md_d%d", -1-devnum);
142 be = base + strlen(base);
143 if (path) {
144 path2 = malloc(strlen(path)+20);
145 strcpy(path2, path);
146 pe = path2 + strlen(path2);
147 } else
148 path = NULL;
149
150 for (part = 0; part < 16; part++) {
151 if (part) {
152 sprintf(be, "p%d", part);
153 if (path) {
154 if (isdigit(pe[-1]))
155 sprintf(pe, "p%d", part);
156 else
157 sprintf(pe, "%d", part);
158 }
159 }
160 /* FIXME test if really is md device ?? */
161 unlink(base);
162 if (path) {
163 n = readlink(path2, link, sizeof(link));
164 if (n && strlen(base) == n &&
165 strncmp(link, base, n) == 0)
166 unlink(path2);
167 }
168 }
169 }
170
171
172 int Manage_runstop(char *devname, int fd, int runstop, int quiet)
173 {
174 /* Run or stop the array. array must already be configured
175 * required >= 0.90.0
176 * Only print failure messages if quiet == 0;
177 * quiet > 0 means really be quiet
178 * quiet < 0 means we will try again if it fails.
179 */
180 mdu_param_t param; /* unused */
181
182 if (runstop == -1 && md_get_version(fd) < 9000) {
183 if (ioctl(fd, STOP_MD, 0)) {
184 if (quiet == 0) fprintf(stderr,
185 Name ": stopping device %s "
186 "failed: %s\n",
187 devname, strerror(errno));
188 return 1;
189 }
190 }
191
192 if (md_get_version(fd) < 9000) {
193 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
194 return 1;
195 }
196 /*
197 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
198 fprintf(stderr, Name ": %s does not appear to be active.\n",
199 devname);
200 return 1;
201 }
202 */
203 if (runstop>0) {
204 if (ioctl(fd, RUN_ARRAY, &param)) {
205 fprintf(stderr, Name ": failed to run array %s: %s\n",
206 devname, strerror(errno));
207 return 1;
208 }
209 if (quiet <= 0)
210 fprintf(stderr, Name ": started %s\n", devname);
211 } else if (runstop < 0){
212 struct map_ent *map = NULL;
213 struct stat stb;
214 struct mdinfo *mdi;
215 int devnum;
216 /* If this is an mdmon managed array, just write 'inactive'
217 * to the array state and let mdmon clear up.
218 */
219 devnum = fd2devnum(fd);
220 mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
221 if (mdi &&
222 mdi->array.level > 0 &&
223 is_subarray(mdi->text_version)) {
224 /* This is mdmon managed. */
225 close(fd);
226 if (sysfs_set_str(mdi, NULL,
227 "array_state", "inactive") < 0) {
228 if (quiet == 0)
229 fprintf(stderr, Name
230 ": failed to stop array %s: %s\n",
231 devname, strerror(errno));
232 return 1;
233 }
234
235 /* Give monitor a chance to act */
236 ping_monitor(mdi->text_version);
237
238 fd = open(devname, O_RDONLY);
239 } else if (mdi &&
240 mdi->array.major_version == -1 &&
241 mdi->array.minor_version == -2 &&
242 !is_subarray(mdi->text_version)) {
243 /* container, possibly mdmon-managed.
244 * Make sure mdmon isn't opening it, which
245 * would interfere with the 'stop'
246 */
247 ping_monitor(mdi->sys_name);
248 }
249
250 if (fd >= 0 && ioctl(fd, STOP_ARRAY, NULL)) {
251 if (quiet == 0) {
252 fprintf(stderr, Name
253 ": failed to stop array %s: %s\n",
254 devname, strerror(errno));
255 if (errno == EBUSY)
256 fprintf(stderr, "Perhaps a running "
257 "process, mounted filesystem "
258 "or active volume group?\n");
259 }
260 if (mdi)
261 sysfs_free(mdi);
262 return 1;
263 }
264 /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
265 * was stopped, so We'll do it here just to be sure. Drop any
266 * partitions as well...
267 */
268 if (fd >= 0)
269 ioctl(fd, BLKRRPART, 0);
270 if (mdi)
271 sysfs_uevent(mdi, "change");
272
273
274 if (devnum != NoMdDev &&
275 (stat("/dev/.udev", &stb) != 0 ||
276 check_env("MDADM_NO_UDEV"))) {
277 struct map_ent *mp = map_by_devnum(&map, devnum);
278 remove_devices(devnum, mp ? mp->path : NULL);
279 }
280
281
282 if (quiet <= 0)
283 fprintf(stderr, Name ": stopped %s\n", devname);
284 if (devnum != NoMdDev) {
285 map_delete(&map, devnum);
286 map_write(map);
287 map_free(map);
288 }
289 }
290 return 0;
291 }
292
293 int Manage_resize(char *devname, int fd, long long size, int raid_disks)
294 {
295 mdu_array_info_t info;
296 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
297 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
298 devname, strerror(errno));
299 return 1;
300 }
301 if (size >= 0)
302 info.size = size;
303 if (raid_disks > 0)
304 info.raid_disks = raid_disks;
305 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
306 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
307 devname, strerror(errno));
308 return 1;
309 }
310 return 0;
311 }
312
313 int Manage_reconfig(char *devname, int fd, int layout)
314 {
315 mdu_array_info_t info;
316 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
317 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
318 devname, strerror(errno));
319 return 1;
320 }
321 info.layout = layout;
322 printf("layout set to %d\n", info.layout);
323 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
324 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
325 devname, strerror(errno));
326 return 1;
327 }
328 return 0;
329 }
330
331 int Manage_subdevs(char *devname, int fd,
332 mddev_dev_t devlist, int verbose)
333 {
334 /* do something to each dev.
335 * devmode can be
336 * 'a' - add the device
337 * try HOT_ADD_DISK
338 * If that fails EINVAL, try ADD_NEW_DISK
339 * 'r' - remove the device HOT_REMOVE_DISK
340 * device can be 'faulty' or 'detached' in which case all
341 * matching devices are removed.
342 * 'f' - set the device faulty SET_DISK_FAULTY
343 * device can be 'detached' in which case any device that
344 * is inaccessible will be marked faulty.
345 */
346 mdu_array_info_t array;
347 mdu_disk_info_t disc;
348 unsigned long long array_size;
349 mddev_dev_t dv, next = NULL;
350 struct stat stb;
351 int j, jnext = 0;
352 int tfd;
353 struct supertype *st, *tst;
354 int duuid[4];
355 int ouuid[4];
356 int lfd = -1;
357
358 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
359 fprintf(stderr, Name ": cannot get array info for %s\n",
360 devname);
361 return 1;
362 }
363
364 /* array.size is only 32 bit and may be truncated.
365 * So read from sysfs if possible, and record number of sectors
366 */
367
368 array_size = get_component_size(fd);
369 if (array_size <= 0)
370 array_size = array.size * 2;
371
372 tst = super_by_fd(fd);
373 if (!tst) {
374 fprintf(stderr, Name ": unsupport array - version %d.%d\n",
375 array.major_version, array.minor_version);
376 return 1;
377 }
378
379 for (dv = devlist, j=0 ; dv; dv = next, j = jnext) {
380 unsigned long long ldsize;
381 char dvname[20];
382 char *dnprintable = dv->devname;
383 int err;
384
385 next = dv->next;
386 jnext = 0;
387
388 if (strcmp(dv->devname, "failed")==0 ||
389 strcmp(dv->devname, "faulty")==0) {
390 if (dv->disposition != 'r') {
391 fprintf(stderr, Name ": %s only meaningful "
392 "with -r, not -%c\n",
393 dv->devname, dv->disposition);
394 return 1;
395 }
396 for (; j < array.raid_disks + array.nr_disks ; j++) {
397 disc.number = j;
398 if (ioctl(fd, GET_DISK_INFO, &disc))
399 continue;
400 if (disc.major == 0 && disc.minor == 0)
401 continue;
402 if ((disc.state & 1) == 0) /* faulty */
403 continue;
404 stb.st_rdev = makedev(disc.major, disc.minor);
405 next = dv;
406 jnext = j+1;
407 sprintf(dvname,"%d:%d", disc.major, disc.minor);
408 dnprintable = dvname;
409 break;
410 }
411 if (jnext == 0)
412 continue;
413 } else if (strcmp(dv->devname, "detached") == 0) {
414 if (dv->disposition != 'r' && dv->disposition != 'f') {
415 fprintf(stderr, Name ": %s only meaningful "
416 "with -r of -f, not -%c\n",
417 dv->devname, dv->disposition);
418 return 1;
419 }
420 for (; j < array.raid_disks + array.nr_disks; j++) {
421 int sfd;
422 disc.number = j;
423 if (ioctl(fd, GET_DISK_INFO, &disc))
424 continue;
425 if (disc.major == 0 && disc.minor == 0)
426 continue;
427 sprintf(dvname,"%d:%d", disc.major, disc.minor);
428 sfd = dev_open(dvname, O_RDONLY);
429 if (sfd >= 0) {
430 close(sfd);
431 continue;
432 }
433 if (dv->disposition == 'f' &&
434 (disc.state & 1) == 1) /* already faulty */
435 continue;
436 if (errno != ENXIO)
437 continue;
438 stb.st_rdev = makedev(disc.major, disc.minor);
439 next = dv;
440 jnext = j+1;
441 dnprintable = dvname;
442 break;
443 }
444 if (jnext == 0)
445 continue;
446 } else {
447 j = 0;
448
449 if (stat(dv->devname, &stb)) {
450 fprintf(stderr, Name ": cannot find %s: %s\n",
451 dv->devname, strerror(errno));
452 return 1;
453 }
454 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
455 fprintf(stderr, Name ": %s is not a "
456 "block device.\n",
457 dv->devname);
458 return 1;
459 }
460 }
461 switch(dv->disposition){
462 default:
463 fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
464 dv->devname, dv->disposition);
465 return 1;
466 case 'a':
467 /* add the device */
468 if (tst->subarray[0]) {
469 fprintf(stderr, Name ": Cannot add disks to a"
470 " \'member\' array, perform this"
471 " operation on the parent container\n");
472 return 1;
473 }
474 /* Make sure it isn't in use (in 2.6 or later) */
475 tfd = open(dv->devname, O_RDONLY|O_EXCL|O_DIRECT);
476 if (tfd < 0) {
477 fprintf(stderr, Name ": Cannot open %s: %s\n",
478 dv->devname, strerror(errno));
479 return 1;
480 }
481 remove_partitions(tfd);
482
483 st = dup_super(tst);
484
485 if (array.not_persistent==0)
486 st->ss->load_super(st, tfd, NULL);
487
488 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
489 close(tfd);
490 return 1;
491 }
492 close(tfd);
493
494
495 if (!tst->ss->external &&
496 array.major_version == 0 &&
497 md_get_version(fd)%100 < 2) {
498 if (ioctl(fd, HOT_ADD_DISK,
499 (unsigned long)stb.st_rdev)==0) {
500 if (verbose >= 0)
501 fprintf(stderr, Name ": hot added %s\n",
502 dv->devname);
503 continue;
504 }
505
506 fprintf(stderr, Name ": hot add failed for %s: %s\n",
507 dv->devname, strerror(errno));
508 return 1;
509 }
510
511 if (array.not_persistent == 0 || tst->ss->external) {
512
513 /* need to find a sample superblock to copy, and
514 * a spare slot to use.
515 * For 'external' array (well, container based),
516 * We can just load the metadata for the array.
517 */
518 if (tst->ss->external) {
519 tst->ss->load_super(tst, fd, NULL);
520 } else for (j = 0; j < tst->max_devs; j++) {
521 char *dev;
522 int dfd;
523 disc.number = j;
524 if (ioctl(fd, GET_DISK_INFO, &disc))
525 continue;
526 if (disc.major==0 && disc.minor==0)
527 continue;
528 if ((disc.state & 4)==0) continue; /* sync */
529 /* Looks like a good device to try */
530 dev = map_dev(disc.major, disc.minor, 1);
531 if (!dev) continue;
532 dfd = dev_open(dev, O_RDONLY);
533 if (dfd < 0) continue;
534 if (tst->ss->load_super(tst, dfd,
535 NULL)) {
536 close(dfd);
537 continue;
538 }
539 close(dfd);
540 break;
541 }
542 /* FIXME this is a bad test to be using */
543 if (!tst->sb) {
544 fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
545 return 1;
546 }
547
548 /* Make sure device is large enough */
549 if (tst->ss->avail_size(tst, ldsize/512) <
550 array_size) {
551 fprintf(stderr, Name ": %s not large enough to join array\n",
552 dv->devname);
553 return 1;
554 }
555
556 /* Possibly this device was recently part of the array
557 * and was temporarily removed, and is now being re-added.
558 * If so, we can simply re-add it.
559 */
560 tst->ss->uuid_from_super(tst, duuid);
561
562 /* re-add doesn't work for version-1 superblocks
563 * before 2.6.18 :-(
564 */
565 if (array.major_version == 1 &&
566 get_linux_version() <= 2006018)
567 ;
568 else if (st->sb) {
569 st->ss->uuid_from_super(st, ouuid);
570 if (memcmp(duuid, ouuid, sizeof(ouuid))==0) {
571 /* looks close enough for now. Kernel
572 * will worry about whether a bitmap
573 * based reconstruction is possible.
574 */
575 struct mdinfo mdi;
576 st->ss->getinfo_super(st, &mdi);
577 disc.major = major(stb.st_rdev);
578 disc.minor = minor(stb.st_rdev);
579 disc.number = mdi.disk.number;
580 disc.raid_disk = mdi.disk.raid_disk;
581 disc.state = mdi.disk.state;
582 if (dv->writemostly == 1)
583 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
584 if (dv->writemostly == 2)
585 disc.state &= ~(1 << MD_DISK_WRITEMOSTLY);
586 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
587 if (verbose >= 0)
588 fprintf(stderr, Name ": re-added %s\n", dv->devname);
589 continue;
590 }
591 /* fall back on normal-add */
592 }
593 }
594 } else {
595 /* non-persistent. Must ensure that new drive
596 * is at least array.size big.
597 */
598 if (ldsize/512 < array_size) {
599 fprintf(stderr, Name ": %s not large enough to join array\n",
600 dv->devname);
601 return 1;
602 }
603 }
604 /* in 2.6.17 and earlier, version-1 superblocks won't
605 * use the number we write, but will choose a free number.
606 * we must choose the same free number, which requires
607 * starting at 'raid_disks' and counting up
608 */
609 for (j = array.raid_disks; j< tst->max_devs; j++) {
610 disc.number = j;
611 if (ioctl(fd, GET_DISK_INFO, &disc))
612 break;
613 if (disc.major==0 && disc.minor==0)
614 break;
615 if (disc.state & 8) /* removed */
616 break;
617 }
618 disc.major = major(stb.st_rdev);
619 disc.minor = minor(stb.st_rdev);
620 disc.number =j;
621 disc.state = 0;
622 if (array.not_persistent==0 || tst->ss->external) {
623 int dfd;
624 if (dv->writemostly == 1)
625 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
626 dfd = open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
627 if (tst->ss->add_to_super(tst, &disc, dfd,
628 dv->devname)) {
629 close(dfd);
630 return 1;
631 }
632 /* write_init_super will close 'dfd' */
633 if (tst->ss->external)
634 /* mdmon will write the metadata */
635 close(dfd);
636 else if (tst->ss->write_init_super(tst))
637 return 1;
638 } else if (dv->re_add) {
639 /* this had better be raid1.
640 * As we are "--re-add"ing we must find a spare slot
641 * to fill.
642 */
643 char *used = malloc(array.raid_disks);
644 memset(used, 0, array.raid_disks);
645 for (j=0; j< tst->max_devs; j++) {
646 mdu_disk_info_t disc2;
647 disc2.number = j;
648 if (ioctl(fd, GET_DISK_INFO, &disc2))
649 continue;
650 if (disc2.major==0 && disc2.minor==0)
651 continue;
652 if (disc2.state & 8) /* removed */
653 continue;
654 if (disc2.raid_disk < 0)
655 continue;
656 if (disc2.raid_disk > array.raid_disks)
657 continue;
658 used[disc2.raid_disk] = 1;
659 }
660 for (j=0 ; j<array.raid_disks; j++)
661 if (!used[j]) {
662 disc.raid_disk = j;
663 disc.state |= (1<<MD_DISK_SYNC);
664 break;
665 }
666 }
667 if (dv->writemostly == 1)
668 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
669 if (tst->ss->external) {
670 /* add a disk to an external metadata container
671 * only if mdmon is around to see it
672 */
673 struct mdinfo new_mdi;
674 struct mdinfo *sra;
675 int container_fd;
676 int devnum = fd2devnum(fd);
677
678 container_fd = open_dev_excl(devnum);
679 if (container_fd < 0) {
680 fprintf(stderr, Name ": add failed for %s:"
681 " could not get exclusive access to container\n",
682 dv->devname);
683 return 1;
684 }
685
686 if (!mdmon_running(devnum)) {
687 fprintf(stderr, Name ": add failed for %s: mdmon not running\n",
688 dv->devname);
689 close(container_fd);
690 return 1;
691 }
692
693 sra = sysfs_read(container_fd, -1, 0);
694 if (!sra) {
695 fprintf(stderr, Name ": add failed for %s: sysfs_read failed\n",
696 dv->devname);
697 close(container_fd);
698 return 1;
699 }
700 sra->array.level = LEVEL_CONTAINER;
701 /* Need to set data_offset and component_size */
702 tst->ss->getinfo_super(tst, &new_mdi);
703 new_mdi.disk.major = disc.major;
704 new_mdi.disk.minor = disc.minor;
705 if (sysfs_add_disk(sra, &new_mdi) != 0) {
706 fprintf(stderr, Name ": add new device to external metadata"
707 " failed for %s\n", dv->devname);
708 close(container_fd);
709 return 1;
710 }
711 ping_monitor(devnum2devname(devnum));
712 sysfs_free(sra);
713 close(container_fd);
714 } else if (ioctl(fd, ADD_NEW_DISK, &disc)) {
715 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
716 dv->devname, j, strerror(errno));
717 return 1;
718 }
719 if (verbose >= 0)
720 fprintf(stderr, Name ": added %s\n", dv->devname);
721 break;
722
723 case 'r':
724 /* hot remove */
725 if (tst->subarray[0]) {
726 fprintf(stderr, Name ": Cannot remove disks from a"
727 " \'member\' array, perform this"
728 " operation on the parent container\n");
729 return 1;
730 }
731 if (tst->ss->external) {
732 /* To remove a device from a container, we must
733 * check that it isn't in use in an array.
734 * This involves looking in the 'holders'
735 * directory - there must be just one entry,
736 * the container.
737 * To ensure that it doesn't get used as a
738 * hold spare while we are checking, we
739 * get an O_EXCL open on the container
740 */
741 int dnum = fd2devnum(fd);
742 lfd = open_dev_excl(dnum);
743 if (lfd < 0) {
744 fprintf(stderr, Name
745 ": Cannot get exclusive access "
746 " to container - odd\n");
747 return 1;
748 }
749 if (!sysfs_unique_holder(dnum, stb.st_rdev)) {
750 fprintf(stderr, Name
751 ": %s is %s, cannot remove.\n",
752 dnprintable,
753 errno == EEXIST ? "still in use":
754 "not a member");
755 close(lfd);
756 return 1;
757 }
758 }
759 /* FIXME check that it is a current member */
760 err = ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev);
761 if (err && errno == ENODEV) {
762 /* Old kernels rejected this if no personality
763 * registered */
764 struct mdinfo *sra = sysfs_read(fd, 0, GET_DEVS);
765 struct mdinfo *dv = NULL;
766 if (sra)
767 dv = sra->devs;
768 for ( ; dv ; dv=dv->next)
769 if (dv->disk.major == major(stb.st_rdev) &&
770 dv->disk.minor == minor(stb.st_rdev))
771 break;
772 if (dv)
773 err = sysfs_set_str(sra, dv,
774 "state", "remove");
775 else
776 err = -1;
777 if (sra)
778 sysfs_free(sra);
779 }
780 if (err) {
781 fprintf(stderr, Name ": hot remove failed "
782 "for %s: %s\n", dnprintable,
783 strerror(errno));
784 if (lfd >= 0)
785 close(lfd);
786 return 1;
787 }
788 if (tst->ss->external) {
789 /*
790 * Before dropping our exclusive open we make an
791 * attempt at preventing mdmon from seeing an
792 * 'add' event before reconciling this 'remove'
793 * event.
794 */
795 char *name = devnum2devname(fd2devnum(fd));
796
797 if (!name) {
798 fprintf(stderr, Name ": unable to get container name\n");
799 return 1;
800 }
801
802 ping_manager(name);
803 free(name);
804 }
805 close(lfd);
806 if (verbose >= 0)
807 fprintf(stderr, Name ": hot removed %s\n",
808 dnprintable);
809 break;
810
811 case 'f': /* set faulty */
812 /* FIXME check current member */
813 if (ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev)) {
814 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
815 dnprintable, strerror(errno));
816 return 1;
817 }
818 if (verbose >= 0)
819 fprintf(stderr, Name ": set %s faulty in %s\n",
820 dnprintable, devname);
821 break;
822 }
823 }
824 return 0;
825
826 }
827
828 int autodetect(void)
829 {
830 /* Open any md device, and issue the RAID_AUTORUN ioctl */
831 int rv = 1;
832 int fd = dev_open("9:0", O_RDONLY);
833 if (fd >= 0) {
834 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
835 rv = 0;
836 close(fd);
837 }
838 return rv;
839 }
840 #endif