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