]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Manage.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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.
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.
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
22 * Email: <neilb@suse.de>
30 #define REGISTER_DEV _IO (MD_MAJOR, 1)
31 #define START_MD _IO (MD_MAJOR, 2)
32 #define STOP_MD _IO (MD_MAJOR, 3)
34 int Manage_ro(char *devname
, int fd
, int readonly
)
36 /* switch to readonly or rw
39 * first check that array is runing
40 * use RESTART_ARRAY_RW or STOP_ARRAY_RO
43 mdu_array_info_t array
;
48 if (md_get_version(fd
) < 9000) {
49 fprintf(stderr
, Name
": need md driver version 0.90.0 or later\n");
53 /* If this is an externally-manage array, we need to modify the
54 * metadata_version so that mdmon doesn't undo our change.
56 mdi
= sysfs_read(fd
, -1, GET_LEVEL
|GET_VERSION
);
58 mdi
->array
.major_version
== -1 &&
59 is_subarray(mdi
->text_version
)) {
61 strcpy(vers
, "external:");
62 strcat(vers
, mdi
->text_version
);
65 /* We set readonly ourselves. */
67 sysfs_set_str(mdi
, NULL
, "metadata_version", vers
);
70 rv
= sysfs_set_str(mdi
, NULL
, "array_state", "readonly");
73 fprintf(stderr
, Name
": failed to set readonly for %s: %s\n",
74 devname
, strerror(errno
));
76 vers
[9] = mdi
->text_version
[0];
77 sysfs_set_str(mdi
, NULL
, "metadata_version", vers
);
82 /* We cannot set read/write - must signal mdmon */
84 sysfs_set_str(mdi
, NULL
, "metadata_version", vers
);
86 cp
= strchr(vers
+10, '/');
89 ping_monitor(vers
+10);
90 if (mdi
->array
.level
<= 0)
91 sysfs_set_str(mdi
, NULL
, "array_state", "active");
96 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)) {
97 fprintf(stderr
, Name
": %s does not appear to be active.\n",
103 if (ioctl(fd
, STOP_ARRAY_RO
, NULL
)) {
104 fprintf(stderr
, Name
": failed to set readonly for %s: %s\n",
105 devname
, strerror(errno
));
108 } else if (readonly
< 0) {
109 if (ioctl(fd
, RESTART_ARRAY_RW
, NULL
)) {
110 fprintf(stderr
, Name
": failed to set writable for %s: %s\n",
111 devname
, strerror(errno
));
120 static void remove_devices(int devnum
, char *path
)
123 * Remove names at 'path' - possibly with
124 * partition suffixes - which link to the 'standard'
125 * name for devnum. These were probably created
126 * by mdadm when the array was assembled.
140 sprintf(base
, "/dev/md%d", devnum
);
142 sprintf(base
, "/dev/md_d%d", -1-devnum
);
143 be
= base
+ strlen(base
);
145 path2
= malloc(strlen(path
)+20);
147 pe
= path2
+ strlen(path2
);
149 for (part
= 0; part
< 16; part
++) {
151 sprintf(be
, "p%d", part
);
154 sprintf(pe
, "p%d", part
);
156 sprintf(pe
, "%d", part
);
158 n
= readlink(path2
, link
, sizeof(link
));
159 if (n
&& (int)strlen(base
) == n
&&
160 strncmp(link
, base
, n
) == 0)
167 int Manage_runstop(char *devname
, int fd
, int runstop
, int quiet
)
169 /* Run or stop the array. array must already be configured
171 * Only print failure messages if quiet == 0;
172 * quiet > 0 means really be quiet
173 * quiet < 0 means we will try again if it fails.
175 mdu_param_t param
; /* unused */
177 if (runstop
== -1 && md_get_version(fd
) < 9000) {
178 if (ioctl(fd
, STOP_MD
, 0)) {
179 if (quiet
== 0) fprintf(stderr
,
180 Name
": stopping device %s "
182 devname
, strerror(errno
));
187 if (md_get_version(fd
) < 9000) {
188 fprintf(stderr
, Name
": need md driver version 0.90.0 or later\n");
192 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
193 fprintf(stderr, Name ": %s does not appear to be active.\n",
199 if (ioctl(fd
, RUN_ARRAY
, ¶m
)) {
200 fprintf(stderr
, Name
": failed to run array %s: %s\n",
201 devname
, strerror(errno
));
205 fprintf(stderr
, Name
": started %s\n", devname
);
206 } else if (runstop
< 0){
207 struct map_ent
*map
= NULL
;
211 /* If this is an mdmon managed array, just write 'inactive'
212 * to the array state and let mdmon clear up.
214 devnum
= fd2devnum(fd
);
215 mdi
= sysfs_read(fd
, -1, GET_LEVEL
|GET_VERSION
);
217 mdi
->array
.level
> 0 &&
218 is_subarray(mdi
->text_version
)) {
219 /* This is mdmon managed. */
221 if (sysfs_set_str(mdi
, NULL
,
222 "array_state", "inactive") < 0) {
225 ": failed to stop array %s: %s\n",
226 devname
, strerror(errno
));
230 /* Give monitor a chance to act */
231 ping_monitor(mdi
->text_version
);
233 fd
= open(devname
, O_RDONLY
);
235 mdi
->array
.major_version
== -1 &&
236 mdi
->array
.minor_version
== -2 &&
237 !is_subarray(mdi
->text_version
)) {
238 struct mdstat_ent
*mds
, *m
;
239 /* container, possibly mdmon-managed.
240 * Make sure mdmon isn't opening it, which
241 * would interfere with the 'stop'
243 ping_monitor(mdi
->sys_name
);
245 /* now check that there are no existing arrays
246 * which are members of this array
248 mds
= mdstat_read(0, 0);
249 for (m
=mds
; m
; m
=m
->next
)
250 if (m
->metadata_version
&&
251 strncmp(m
->metadata_version
, "external:", 9)==0 &&
252 is_subarray(m
->metadata_version
+9) &&
253 devname2devnum(m
->metadata_version
+10) == devnum
) {
256 ": Cannot stop container %s: "
257 "member %s still active\n",
266 if (fd
>= 0 && ioctl(fd
, STOP_ARRAY
, NULL
)) {
269 ": failed to stop array %s: %s\n",
270 devname
, strerror(errno
));
272 fprintf(stderr
, "Perhaps a running "
273 "process, mounted filesystem "
274 "or active volume group?\n");
280 /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
281 * was stopped, so We'll do it here just to be sure. Drop any
282 * partitions as well...
285 ioctl(fd
, BLKRRPART
, 0);
287 sysfs_uevent(mdi
, "change");
290 if (devnum
!= NoMdDev
&&
291 (stat("/dev/.udev", &stb
) != 0 ||
292 check_env("MDADM_NO_UDEV"))) {
293 struct map_ent
*mp
= map_by_devnum(&map
, devnum
);
294 remove_devices(devnum
, mp
? mp
->path
: NULL
);
299 fprintf(stderr
, Name
": stopped %s\n", devname
);
301 map_remove(&map
, devnum
);
307 int Manage_resize(char *devname
, int fd
, long long size
, int raid_disks
)
309 mdu_array_info_t info
;
310 if (ioctl(fd
, GET_ARRAY_INFO
, &info
) != 0) {
311 fprintf(stderr
, Name
": Cannot get array information for %s: %s\n",
312 devname
, strerror(errno
));
318 info
.raid_disks
= raid_disks
;
319 if (ioctl(fd
, SET_ARRAY_INFO
, &info
) != 0) {
320 fprintf(stderr
, Name
": Cannot set device size/shape for %s: %s\n",
321 devname
, strerror(errno
));
327 int Manage_subdevs(char *devname
, int fd
,
328 struct mddev_dev
*devlist
, int verbose
, int test
,
331 /* do something to each dev.
333 * 'a' - add the device
335 * If that fails EINVAL, try ADD_NEW_DISK
336 * 'r' - remove the device HOT_REMOVE_DISK
337 * device can be 'faulty' or 'detached' in which case all
338 * matching devices are removed.
339 * 'f' - set the device faulty SET_DISK_FAULTY
340 * device can be 'detached' in which case any device that
341 * is inaccessible will be marked faulty.
342 * For 'f' and 'r', the device can also be a kernel-internal
343 * name such as 'sdb'.
345 struct mddev_dev
*add_devlist
= NULL
;
346 mdu_array_info_t array
;
347 mdu_disk_info_t disc
;
348 unsigned long long array_size
;
349 struct mddev_dev
*dv
, *next
= NULL
;
353 struct supertype
*st
, *tst
;
354 char *subarray
= NULL
;
359 int count
= 0; /* number of actions taken */
361 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)) {
362 fprintf(stderr
, Name
": cannot get array info for %s\n",
367 /* array.size is only 32 bit and may be truncated.
368 * So read from sysfs if possible, and record number of sectors
371 array_size
= get_component_size(fd
);
373 array_size
= array
.size
* 2;
375 tst
= super_by_fd(fd
, &subarray
);
377 fprintf(stderr
, Name
": unsupport array - version %d.%d\n",
378 array
.major_version
, array
.minor_version
);
383 for (dv
= devlist
, j
=0 ; dv
; dv
= next
, j
= jnext
) {
384 unsigned long long ldsize
;
386 char *dnprintable
= dv
->devname
;
387 char *add_dev
= dv
->devname
;
389 int re_add_failed
= 0;
394 if (strcmp(dv
->devname
, "failed")==0 ||
395 strcmp(dv
->devname
, "faulty")==0) {
396 if (dv
->disposition
!= 'r') {
397 fprintf(stderr
, Name
": %s only meaningful "
398 "with -r, not -%c\n",
399 dv
->devname
, dv
->disposition
);
402 for (; j
< array
.raid_disks
+ array
.nr_disks
; j
++) {
405 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
407 if (disc
.major
== 0 && disc
.minor
== 0)
409 if ((disc
.state
& 1) == 0) /* faulty */
411 dev
= makedev(disc
.major
, disc
.minor
);
412 if (stb
.st_rdev
== dev
)
413 /* already did that one */
417 /* same slot again next time - things might
420 sprintf(dvname
,"%d:%d", disc
.major
, disc
.minor
);
421 dnprintable
= dvname
;
426 } else if (strcmp(dv
->devname
, "detached") == 0) {
427 if (dv
->disposition
!= 'r' && dv
->disposition
!= 'f') {
428 fprintf(stderr
, Name
": %s only meaningful "
429 "with -r of -f, not -%c\n",
430 dv
->devname
, dv
->disposition
);
433 for (; j
< array
.raid_disks
+ array
.nr_disks
; j
++) {
437 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
439 if (disc
.major
== 0 && disc
.minor
== 0)
441 sprintf(dvname
,"%d:%d", disc
.major
, disc
.minor
);
442 sfd
= dev_open(dvname
, O_RDONLY
);
447 if (dv
->disposition
== 'f' &&
448 (disc
.state
& 1) == 1) /* already faulty */
452 dev
= makedev(disc
.major
, disc
.minor
);
453 if (stb
.st_rdev
== dev
)
454 /* already did that one */
458 /* same slot again next time - things might
461 dnprintable
= dvname
;
466 } else if (strcmp(dv
->devname
, "missing") == 0) {
467 if (dv
->disposition
!= 'a' || dv
->re_add
== 0) {
468 fprintf(stderr
, Name
": 'missing' only meaningful "
472 if (add_devlist
== NULL
)
473 add_devlist
= conf_get_devs();
474 if (add_devlist
== NULL
) {
475 fprintf(stderr
, Name
": no devices to scan for missing members.");
478 add_dev
= add_devlist
->devname
;
479 add_devlist
= add_devlist
->next
;
480 if (add_devlist
!= NULL
)
482 if (stat(add_dev
, &stb
) < 0)
484 } else if (strchr(dv
->devname
, '/') == NULL
&&
485 strchr(dv
->devname
, ':') == NULL
&&
486 strlen(dv
->devname
) < 50) {
487 /* Assume this is a kernel-internal name like 'sda1' */
490 if (dv
->disposition
!= 'r' && dv
->disposition
!= 'f') {
491 fprintf(stderr
, Name
": %s only meaningful "
492 "with -r or -f, not -%c\n",
493 dv
->devname
, dv
->disposition
);
497 sprintf(dname
, "dev-%s", dv
->devname
);
498 sysfd
= sysfs_open(fd2devnum(fd
), dname
, "block/dev");
502 if (sysfs_fd_get_str(sysfd
, dn
, 20) > 0 &&
503 sscanf(dn
, "%d:%d", &mj
,&mn
) == 2) {
504 stb
.st_rdev
= makedev(mj
,mn
);
511 sysfd
= sysfs_open(fd2devnum(fd
), dname
, "state");
513 fprintf(stderr
, Name
": %s does not appear "
514 "to be a component of %s\n",
515 dv
->devname
, devname
);
522 tfd
= dev_open(dv
->devname
, O_RDONLY
);
523 if (tfd
< 0 && dv
->disposition
== 'r' &&
524 lstat(dv
->devname
, &stb
) == 0)
525 /* Be happy, the lstat worked, that is
526 * enough for --remove
530 if (tfd
< 0 || fstat(tfd
, &stb
) != 0) {
531 fprintf(stderr
, Name
": cannot find %s: %s\n",
532 dv
->devname
, strerror(errno
));
540 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
541 fprintf(stderr
, Name
": %s is not a "
547 switch(dv
->disposition
){
549 fprintf(stderr
, Name
": internal error - devmode[%s]=%d\n",
550 dv
->devname
, dv
->disposition
);
555 fprintf(stderr
, Name
": Cannot add disks to a"
556 " \'member\' array, perform this"
557 " operation on the parent container\n");
560 /* Make sure it isn't in use (in 2.6 or later) */
561 tfd
= dev_open(add_dev
, O_RDONLY
|O_EXCL
|O_DIRECT
);
562 if (tfd
< 0 && add_dev
!= dv
->devname
)
565 fprintf(stderr
, Name
": Cannot open %s: %s\n",
566 dv
->devname
, strerror(errno
));
572 if (array
.not_persistent
==0)
573 st
->ss
->load_super(st
, tfd
, NULL
);
575 if (add_dev
== dv
->devname
) {
576 if (!get_dev_size(tfd
, dv
->devname
, &ldsize
)) {
580 } else if (!get_dev_size(tfd
, NULL
, &ldsize
)) {
586 if (!tst
->ss
->external
&&
587 array
.major_version
== 0 &&
588 md_get_version(fd
)%100 < 2) {
591 if (ioctl(fd
, HOT_ADD_DISK
,
592 (unsigned long)stb
.st_rdev
)==0) {
594 fprintf(stderr
, Name
": hot added %s\n",
599 fprintf(stderr
, Name
": hot add failed for %s: %s\n",
600 add_dev
, strerror(errno
));
604 if (array
.not_persistent
== 0 || tst
->ss
->external
) {
606 /* need to find a sample superblock to copy, and
607 * a spare slot to use.
608 * For 'external' array (well, container based),
609 * We can just load the metadata for the array.
612 /* already loaded */;
613 else if (tst
->ss
->external
) {
614 tst
->ss
->load_container(tst
, fd
, NULL
);
615 } else for (j
= 0; j
< tst
->max_devs
; j
++) {
619 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
621 if (disc
.major
==0 && disc
.minor
==0)
623 if ((disc
.state
& 4)==0) continue; /* sync */
624 /* Looks like a good device to try */
625 dev
= map_dev(disc
.major
, disc
.minor
, 1);
627 dfd
= dev_open(dev
, O_RDONLY
);
628 if (dfd
< 0) continue;
629 if (tst
->ss
->load_super(tst
, dfd
,
637 /* FIXME this is a bad test to be using */
640 fprintf(stderr
, Name
": cannot find valid superblock in this array - HELP\n");
644 /* Make sure device is large enough */
645 if (tst
->ss
->avail_size(tst
, ldsize
/512) <
649 if (add_dev
!= dv
->devname
)
651 fprintf(stderr
, Name
": %s not large enough to join array\n",
656 /* Possibly this device was recently part of the array
657 * and was temporarily removed, and is now being re-added.
658 * If so, we can simply re-add it.
660 tst
->ss
->uuid_from_super(tst
, duuid
);
662 /* re-add doesn't work for version-1 superblocks
665 if (array
.major_version
== 1 &&
666 get_linux_version() <= 2006018)
670 st
->ss
->getinfo_super(st
, &mdi
, NULL
);
671 st
->ss
->uuid_from_super(st
, ouuid
);
672 if ((mdi
.disk
.state
& (1<<MD_DISK_ACTIVE
)) &&
673 !(mdi
.disk
.state
& (1<<MD_DISK_FAULTY
)) &&
674 memcmp(duuid
, ouuid
, sizeof(ouuid
))==0) {
675 /* look like it is worth a try. Need to
676 * make sure kernel will accept it though.
678 disc
.number
= mdi
.disk
.number
;
679 if (ioctl(fd
, GET_DISK_INFO
, &disc
) != 0
680 || disc
.major
!= 0 || disc
.minor
!= 0
683 disc
.major
= major(stb
.st_rdev
);
684 disc
.minor
= minor(stb
.st_rdev
);
685 disc
.number
= mdi
.disk
.number
;
686 disc
.raid_disk
= mdi
.disk
.raid_disk
;
687 disc
.state
= mdi
.disk
.state
;
688 if (dv
->writemostly
== 1)
689 disc
.state
|= 1 << MD_DISK_WRITEMOSTLY
;
690 if (dv
->writemostly
== 2)
691 disc
.state
&= ~(1 << MD_DISK_WRITEMOSTLY
);
692 remove_partitions(tfd
);
697 tfd
= dev_open(dv
->devname
, O_RDWR
);
700 rv
= st
->ss
->update_super(
702 devname
, verbose
, 0, NULL
);
704 rv
= tst
->ss
->store_super(st
, tfd
);
708 fprintf(stderr
, Name
": failed to update"
709 " superblock during re-add\n");
713 /* don't even try if disk is marked as faulty */
715 if (ioctl(fd
, ADD_NEW_DISK
, &disc
) == 0) {
717 fprintf(stderr
, Name
": re-added %s\n", add_dev
);
721 if (errno
== ENOMEM
|| errno
== EROFS
) {
722 fprintf(stderr
, Name
": add new device failed for %s: %s\n",
723 add_dev
, strerror(errno
));
724 if (add_dev
!= dv
->devname
)
732 if (add_dev
!= dv
->devname
) {
735 ": --re-add for %s to %s is not possible\n",
747 ": --re-add for %s to %s is not possible\n",
748 dv
->devname
, devname
);
752 fprintf(stderr
, Name
": %s reports being an active member for %s, but a --re-add fails.\n",
753 dv
->devname
, devname
);
754 fprintf(stderr
, Name
": not performing --add as that would convert %s in to a spare.\n",
756 fprintf(stderr
, Name
": To make this a spare, use \"mdadm --zero-superblock %s\" first.\n",
763 /* non-persistent. Must ensure that new drive
764 * is at least array.size big.
766 if (ldsize
/512 < array_size
) {
767 fprintf(stderr
, Name
": %s not large enough to join array\n",
774 /* committed to really trying this device now*/
776 remove_partitions(tfd
);
780 /* in 2.6.17 and earlier, version-1 superblocks won't
781 * use the number we write, but will choose a free number.
782 * we must choose the same free number, which requires
783 * starting at 'raid_disks' and counting up
785 for (j
= array
.raid_disks
; j
< tst
->max_devs
; j
++) {
787 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
789 if (disc
.major
==0 && disc
.minor
==0)
791 if (disc
.state
& 8) /* removed */
794 disc
.major
= major(stb
.st_rdev
);
795 disc
.minor
= minor(stb
.st_rdev
);
798 if (array
.not_persistent
==0 || tst
->ss
->external
) {
800 if (dv
->writemostly
== 1)
801 disc
.state
|= 1 << MD_DISK_WRITEMOSTLY
;
802 dfd
= dev_open(dv
->devname
, O_RDWR
| O_EXCL
|O_DIRECT
);
803 if (tst
->ss
->external
&&
804 mdmon_running(tst
->container_dev
))
805 tst
->update_tail
= &tst
->updates
;
806 if (tst
->ss
->add_to_super(tst
, &disc
, dfd
,
811 /* write_init_super will close 'dfd' */
812 if (tst
->ss
->write_init_super(tst
))
814 } else if (dv
->re_add
) {
815 /* this had better be raid1.
816 * As we are "--re-add"ing we must find a spare slot
819 char *used
= malloc(array
.raid_disks
);
820 memset(used
, 0, array
.raid_disks
);
821 for (j
=0; j
< tst
->max_devs
; j
++) {
822 mdu_disk_info_t disc2
;
824 if (ioctl(fd
, GET_DISK_INFO
, &disc2
))
826 if (disc2
.major
==0 && disc2
.minor
==0)
828 if (disc2
.state
& 8) /* removed */
830 if (disc2
.raid_disk
< 0)
832 if (disc2
.raid_disk
> array
.raid_disks
)
834 used
[disc2
.raid_disk
] = 1;
836 for (j
=0 ; j
<array
.raid_disks
; j
++)
839 disc
.state
|= (1<<MD_DISK_SYNC
);
844 if (dv
->writemostly
== 1)
845 disc
.state
|= (1 << MD_DISK_WRITEMOSTLY
);
846 if (tst
->ss
->external
) {
848 * to an external metadata container */
849 struct mdinfo new_mdi
;
852 int devnum
= fd2devnum(fd
);
854 container_fd
= open_dev_excl(devnum
);
855 if (container_fd
< 0) {
856 fprintf(stderr
, Name
": add failed for %s:"
857 " could not get exclusive access to container\n",
862 sra
= sysfs_read(container_fd
, -1, 0);
864 fprintf(stderr
, Name
": add failed for %s: sysfs_read failed\n",
869 sra
->array
.level
= LEVEL_CONTAINER
;
870 /* Need to set data_offset and component_size */
871 tst
->ss
->getinfo_super(tst
, &new_mdi
, NULL
);
872 new_mdi
.disk
.major
= disc
.major
;
873 new_mdi
.disk
.minor
= disc
.minor
;
874 new_mdi
.recovery_start
= 0;
875 if (sysfs_add_disk(sra
, &new_mdi
, 0) != 0) {
876 fprintf(stderr
, Name
": add new device to external metadata"
877 " failed for %s\n", dv
->devname
);
882 ping_monitor(devnum2devname(devnum
));
885 } else if (ioctl(fd
, ADD_NEW_DISK
, &disc
)) {
886 fprintf(stderr
, Name
": add new device failed for %s as %d: %s\n",
887 dv
->devname
, j
, strerror(errno
));
891 fprintf(stderr
, Name
": added %s\n", dv
->devname
);
897 fprintf(stderr
, Name
": Cannot remove disks from a"
898 " \'member\' array, perform this"
899 " operation on the parent container\n");
904 if (tst
->ss
->external
) {
905 /* To remove a device from a container, we must
906 * check that it isn't in use in an array.
907 * This involves looking in the 'holders'
908 * directory - there must be just one entry,
910 * To ensure that it doesn't get used as a
911 * hold spare while we are checking, we
912 * get an O_EXCL open on the container
914 int dnum
= fd2devnum(fd
);
915 lfd
= open_dev_excl(dnum
);
918 ": Cannot get exclusive access "
919 " to container - odd\n");
924 /* in the detached case it is not possible to
925 * check if we are the unique holder, so just
926 * rely on the 'detached' checks
928 if (strcmp(dv
->devname
, "detached") == 0 ||
930 sysfs_unique_holder(dnum
, stb
.st_rdev
))
934 ": %s is %s, cannot remove.\n",
936 errno
== EEXIST
? "still in use":
942 /* FIXME check that it is a current member */
944 /* device has been removed and we don't know
945 * the major:minor number
947 int n
= write(sysfd
, "remove", 6);
955 err
= ioctl(fd
, HOT_REMOVE_DISK
, (unsigned long)stb
.st_rdev
);
956 if (err
&& errno
== ENODEV
) {
957 /* Old kernels rejected this if no personality
959 struct mdinfo
*sra
= sysfs_read(fd
, 0, GET_DEVS
);
960 struct mdinfo
*dv
= NULL
;
963 for ( ; dv
; dv
=dv
->next
)
964 if (dv
->disk
.major
== (int)major(stb
.st_rdev
) &&
965 dv
->disk
.minor
== (int)minor(stb
.st_rdev
))
968 err
= sysfs_set_str(sra
, dv
,
977 fprintf(stderr
, Name
": hot remove failed "
978 "for %s: %s\n", dnprintable
,
984 if (tst
->ss
->external
) {
986 * Before dropping our exclusive open we make an
987 * attempt at preventing mdmon from seeing an
988 * 'add' event before reconciling this 'remove'
991 char *name
= devnum2devname(fd2devnum(fd
));
994 fprintf(stderr
, Name
": unable to get container name\n");
1005 fprintf(stderr
, Name
": hot removed %s from %s\n",
1006 dnprintable
, devname
);
1009 case 'f': /* set faulty */
1010 /* FIXME check current member */
1011 if ((sysfd
>= 0 && write(sysfd
, "faulty", 6) != 6) ||
1012 (sysfd
< 0 && ioctl(fd
, SET_DISK_FAULTY
,
1013 (unsigned long) stb
.st_rdev
))) {
1014 fprintf(stderr
, Name
": set device faulty failed for %s: %s\n",
1015 dnprintable
, strerror(errno
));
1025 fprintf(stderr
, Name
": set %s faulty in %s\n",
1026 dnprintable
, devname
);
1030 if (test
&& count
== 0)
1035 int autodetect(void)
1037 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1039 int fd
= dev_open("9:0", O_RDONLY
);
1041 if (ioctl(fd
, RAID_AUTORUN
, 0) == 0)
1048 int Update_subarray(char *dev
, char *subarray
, char *update
, struct mddev_ident
*ident
, int quiet
)
1050 struct supertype supertype
, *st
= &supertype
;
1053 memset(st
, 0, sizeof(*st
));
1055 fd
= open_subarray(dev
, subarray
, st
, quiet
);
1059 if (!st
->ss
->update_subarray
) {
1062 Name
": Operation not supported for %s metadata\n",
1067 if (mdmon_running(st
->devnum
))
1068 st
->update_tail
= &st
->updates
;
1070 rv
= st
->ss
->update_subarray(st
, subarray
, update
, ident
);
1074 fprintf(stderr
, Name
": Failed to update %s of subarray-%s in %s\n",
1075 update
, subarray
, dev
);
1076 } else if (st
->update_tail
)
1077 flush_metadata_updates(st
);
1079 st
->ss
->sync_metadata(st
);
1081 if (rv
== 0 && strcmp(update
, "name") == 0 && !quiet
)
1083 Name
": Updated subarray-%s name from %s, UUIDs may have changed\n",
1087 st
->ss
->free_super(st
);
1093 /* Move spare from one array to another
1094 * If adding to destination array fails
1095 * add back to original array
1096 * Returns 1 on success, 0 on failure */
1097 int move_spare(char *from_devname
, char *to_devname
, dev_t devid
)
1099 struct mddev_dev devlist
;
1102 /* try to remove and add */
1103 int fd1
= open(to_devname
, O_RDONLY
);
1104 int fd2
= open(from_devname
, O_RDONLY
);
1106 if (fd1
< 0 || fd2
< 0) {
1107 if (fd1
>=0) close(fd1
);
1108 if (fd2
>=0) close(fd2
);
1112 devlist
.next
= NULL
;
1115 devlist
.writemostly
= 0;
1116 devlist
.devname
= devname
;
1117 sprintf(devname
, "%d:%d", major(devid
), minor(devid
));
1119 devlist
.disposition
= 'r';
1120 if (Manage_subdevs(from_devname
, fd2
, &devlist
, -1, 0, NULL
) == 0) {
1121 devlist
.disposition
= 'a';
1122 if (Manage_subdevs(to_devname
, fd1
, &devlist
, -1, 0, NULL
) == 0) {
1123 /* make sure manager is aware of changes */
1124 ping_manager(to_devname
);
1125 ping_manager(from_devname
);
1130 else Manage_subdevs(from_devname
, fd2
, &devlist
, -1, 0, NULL
);