]>
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
;
49 if (md_get_version(fd
) < 9000) {
50 fprintf(stderr
, Name
": need md driver version 0.90.0 or later\n");
54 /* If this is an externally-manage array, we need to modify the
55 * metadata_version so that mdmon doesn't undo our change.
57 mdi
= sysfs_read(fd
, -1, GET_LEVEL
|GET_VERSION
);
59 mdi
->array
.major_version
== -1 &&
60 is_subarray(mdi
->text_version
)) {
62 strcpy(vers
, "external:");
63 strcat(vers
, mdi
->text_version
);
66 /* We set readonly ourselves. */
68 sysfs_set_str(mdi
, NULL
, "metadata_version", vers
);
71 rv
= sysfs_set_str(mdi
, NULL
, "array_state", "readonly");
74 fprintf(stderr
, Name
": failed to set readonly for %s: %s\n",
75 devname
, strerror(errno
));
77 vers
[9] = mdi
->text_version
[0];
78 sysfs_set_str(mdi
, NULL
, "metadata_version", vers
);
84 /* We cannot set read/write - must signal mdmon */
86 sysfs_set_str(mdi
, NULL
, "metadata_version", vers
);
88 cp
= strchr(vers
+10, '/');
91 ping_monitor(vers
+10);
92 if (mdi
->array
.level
<= 0)
93 sysfs_set_str(mdi
, NULL
, "array_state", "active");
98 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)) {
99 fprintf(stderr
, Name
": %s does not appear to be active.\n",
106 if (ioctl(fd
, STOP_ARRAY_RO
, NULL
)) {
107 fprintf(stderr
, Name
": failed to set readonly for %s: %s\n",
108 devname
, strerror(errno
));
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
));
130 static void remove_devices(int devnum
, char *path
)
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.
150 sprintf(base
, "/dev/md%d", devnum
);
152 sprintf(base
, "/dev/md_d%d", -1-devnum
);
153 be
= base
+ strlen(base
);
155 path2
= malloc(strlen(path
)+20);
157 pe
= path2
+ strlen(path2
);
159 for (part
= 0; part
< 16; part
++) {
161 sprintf(be
, "p%d", part
);
164 sprintf(pe
, "p%d", part
);
166 sprintf(pe
, "%d", part
);
168 n
= readlink(path2
, link
, sizeof(link
));
169 if (n
> 0 && (int)strlen(base
) == n
&&
170 strncmp(link
, base
, n
) == 0)
177 int Manage_runstop(char *devname
, int fd
, int runstop
, int quiet
)
179 /* Run or stop the array. array must already be configured
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.
185 mdu_param_t param
; /* unused */
188 if (runstop
== -1 && md_get_version(fd
) < 9000) {
189 if (ioctl(fd
, STOP_MD
, 0)) {
190 if (quiet
== 0) fprintf(stderr
,
191 Name
": stopping device %s "
193 devname
, strerror(errno
));
198 if (md_get_version(fd
) < 9000) {
199 fprintf(stderr
, Name
": need md driver version 0.90.0 or later\n");
203 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
204 fprintf(stderr, Name ": %s does not appear to be active.\n",
210 if (ioctl(fd
, RUN_ARRAY
, ¶m
)) {
211 fprintf(stderr
, Name
": failed to run array %s: %s\n",
212 devname
, strerror(errno
));
216 fprintf(stderr
, Name
": started %s\n", devname
);
217 } else if (runstop
< 0){
218 struct map_ent
*map
= NULL
;
224 /* If this is an mdmon managed array, just write 'inactive'
225 * to the array state and let mdmon clear up.
227 devnum
= fd2devnum(fd
);
228 /* Get EXCL access first. If this fails, then attempting
229 * to stop is probably a bad idea.
232 fd
= open(devname
, O_RDONLY
|O_EXCL
);
233 if (fd
< 0 || fd2devnum(fd
) != devnum
) {
237 Name
": Cannot get exclusive access to %s:"
239 "process, mounted filesystem "
240 "or active volume group?\n",
244 mdi
= sysfs_read(fd
, -1, GET_LEVEL
|GET_VERSION
);
246 mdi
->array
.level
> 0 &&
247 is_subarray(mdi
->text_version
)) {
249 /* This is mdmon managed. */
254 (err
= sysfs_set_str(mdi
, NULL
,
263 ": failed to stop array %s: %s\n",
264 devname
, strerror(errno
));
269 /* Give monitor a chance to act */
270 ping_monitor(mdi
->text_version
);
272 fd
= open_dev_excl(devnum
);
275 ": failed to completely stop %s"
276 ": Device is busy\n",
282 mdi
->array
.major_version
== -1 &&
283 mdi
->array
.minor_version
== -2 &&
284 !is_subarray(mdi
->text_version
)) {
285 struct mdstat_ent
*mds
, *m
;
286 /* container, possibly mdmon-managed.
287 * Make sure mdmon isn't opening it, which
288 * would interfere with the 'stop'
290 ping_monitor(mdi
->sys_name
);
292 /* now check that there are no existing arrays
293 * which are members of this array
295 mds
= mdstat_read(0, 0);
296 for (m
=mds
; m
; m
=m
->next
)
297 if (m
->metadata_version
&&
298 strncmp(m
->metadata_version
, "external:", 9)==0 &&
299 is_subarray(m
->metadata_version
+9) &&
300 devname2devnum(m
->metadata_version
+10) == devnum
) {
303 ": Cannot stop container %s: "
304 "member %s still active\n",
312 /* As we have an O_EXCL open, any use of the device
313 * which blocks STOP_ARRAY is probably a transient use,
314 * so it is reasonable to retry for a while - 5 seconds.
317 while (count
&& fd
>= 0
318 && (err
= ioctl(fd
, STOP_ARRAY
, NULL
)) < 0
323 if (fd
>= 0 && err
) {
326 ": failed to stop array %s: %s\n",
327 devname
, strerror(errno
));
329 fprintf(stderr
, "Perhaps a running "
330 "process, mounted filesystem "
331 "or active volume group?\n");
336 /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
337 * was stopped, so We'll do it here just to be sure. Drop any
338 * partitions as well...
341 ioctl(fd
, BLKRRPART
, 0);
343 sysfs_uevent(mdi
, "change");
346 if (devnum
!= NoMdDev
&&
347 (stat("/dev/.udev", &stb
) != 0 ||
348 check_env("MDADM_NO_UDEV"))) {
349 struct map_ent
*mp
= map_by_devnum(&map
, devnum
);
350 remove_devices(devnum
, mp
? mp
->path
: NULL
);
355 fprintf(stderr
, Name
": stopped %s\n", devname
);
357 map_remove(&map
, devnum
);
366 int Manage_resize(char *devname
, int fd
, long long size
, int raid_disks
)
368 mdu_array_info_t info
;
369 if (ioctl(fd
, GET_ARRAY_INFO
, &info
) != 0) {
370 fprintf(stderr
, Name
": Cannot get array information for %s: %s\n",
371 devname
, strerror(errno
));
377 info
.raid_disks
= raid_disks
;
378 if (ioctl(fd
, SET_ARRAY_INFO
, &info
) != 0) {
379 fprintf(stderr
, Name
": Cannot set device size/shape for %s: %s\n",
380 devname
, strerror(errno
));
386 int Manage_subdevs(char *devname
, int fd
,
387 struct mddev_dev
*devlist
, int verbose
, int test
,
388 char *update
, int force
)
390 /* do something to each dev.
392 * 'a' - add the device
394 * If that fails EINVAL, try ADD_NEW_DISK
395 * 'r' - remove the device HOT_REMOVE_DISK
396 * device can be 'faulty' or 'detached' in which case all
397 * matching devices are removed.
398 * 'f' - set the device faulty SET_DISK_FAULTY
399 * device can be 'detached' in which case any device that
400 * is inaccessible will be marked faulty.
401 * For 'f' and 'r', the device can also be a kernel-internal
402 * name such as 'sdb'.
404 struct mddev_dev
*add_devlist
= NULL
;
405 mdu_array_info_t array
;
406 mdu_disk_info_t disc
;
407 unsigned long long array_size
;
408 struct mddev_dev
*dv
, *next
= NULL
;
412 struct supertype
*st
, *tst
;
413 char *subarray
= NULL
;
418 int count
= 0; /* number of actions taken */
422 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)) {
423 fprintf(stderr
, Name
": cannot get array info for %s\n",
427 sysfs_init(&info
, fd
, 0);
429 /* array.size is only 32 bit and may be truncated.
430 * So read from sysfs if possible, and record number of sectors
433 array_size
= get_component_size(fd
);
435 array_size
= array
.size
* 2;
437 tst
= super_by_fd(fd
, &subarray
);
439 fprintf(stderr
, Name
": unsupport array - version %d.%d\n",
440 array
.major_version
, array
.minor_version
);
445 for (dv
= devlist
, j
=0 ; dv
; dv
= next
, j
= jnext
) {
446 unsigned long long ldsize
;
448 char *dnprintable
= dv
->devname
;
449 char *add_dev
= dv
->devname
;
456 if (strcmp(dv
->devname
, "failed")==0 ||
457 strcmp(dv
->devname
, "faulty")==0) {
458 int remaining_disks
= array
.nr_disks
;
459 if (dv
->disposition
!= 'r') {
460 fprintf(stderr
, Name
": %s only meaningful "
461 "with -r, not -%c\n",
462 dv
->devname
, dv
->disposition
);
465 for (; j
< MAX_DISKS
&& remaining_disks
> 0; j
++) {
468 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
470 if (disc
.major
== 0 && disc
.minor
== 0)
473 if ((disc
.state
& 1) == 0) /* faulty */
475 dev
= makedev(disc
.major
, disc
.minor
);
476 if (stb
.st_rdev
== dev
)
477 /* already did that one */
481 /* same slot again next time - things might
484 sprintf(dvname
,"%d:%d", disc
.major
, disc
.minor
);
485 dnprintable
= dvname
;
490 } else if (strcmp(dv
->devname
, "detached") == 0) {
491 int remaining_disks
= array
.nr_disks
;
492 if (dv
->disposition
!= 'r' && dv
->disposition
!= 'f') {
493 fprintf(stderr
, Name
": %s only meaningful "
494 "with -r of -f, not -%c\n",
495 dv
->devname
, dv
->disposition
);
498 for (; j
< MAX_DISKS
&& remaining_disks
> 0; j
++) {
502 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
504 if (disc
.major
== 0 && disc
.minor
== 0)
507 sprintf(dvname
,"%d:%d", disc
.major
, disc
.minor
);
508 sfd
= dev_open(dvname
, O_RDONLY
);
513 if (dv
->disposition
== 'f' &&
514 (disc
.state
& 1) == 1) /* already faulty */
518 dev
= makedev(disc
.major
, disc
.minor
);
519 if (stb
.st_rdev
== dev
)
520 /* already did that one */
524 /* same slot again next time - things might
527 dnprintable
= dvname
;
532 } else if (strcmp(dv
->devname
, "missing") == 0) {
533 if (dv
->disposition
!= 'a' || dv
->re_add
== 0) {
534 fprintf(stderr
, Name
": 'missing' only meaningful "
538 if (add_devlist
== NULL
)
539 add_devlist
= conf_get_devs();
540 if (add_devlist
== NULL
) {
541 fprintf(stderr
, Name
": no devices to scan for missing members.");
544 add_dev
= add_devlist
->devname
;
545 add_devlist
= add_devlist
->next
;
546 if (add_devlist
!= NULL
)
548 if (stat(add_dev
, &stb
) < 0)
550 } else if (strchr(dv
->devname
, '/') == NULL
&&
551 strchr(dv
->devname
, ':') == NULL
&&
552 strlen(dv
->devname
) < 50) {
553 /* Assume this is a kernel-internal name like 'sda1' */
556 if (dv
->disposition
!= 'r' && dv
->disposition
!= 'f') {
557 fprintf(stderr
, Name
": %s only meaningful "
558 "with -r or -f, not -%c\n",
559 dv
->devname
, dv
->disposition
);
563 sprintf(dname
, "dev-%s", dv
->devname
);
564 sysfd
= sysfs_open(fd2devnum(fd
), dname
, "block/dev");
568 if (sysfs_fd_get_str(sysfd
, dn
, 20) > 0 &&
569 sscanf(dn
, "%d:%d", &mj
,&mn
) == 2) {
570 stb
.st_rdev
= makedev(mj
,mn
);
577 sysfd
= sysfs_open(fd2devnum(fd
), dname
, "state");
579 fprintf(stderr
, Name
": %s does not appear "
580 "to be a component of %s\n",
581 dv
->devname
, devname
);
588 tfd
= dev_open(dv
->devname
, O_RDONLY
);
589 if (tfd
< 0 && dv
->disposition
== 'r' &&
590 lstat(dv
->devname
, &stb
) == 0)
591 /* Be happy, the lstat worked, that is
592 * enough for --remove
596 if (tfd
< 0 || fstat(tfd
, &stb
) != 0) {
597 fprintf(stderr
, Name
": cannot find %s: %s\n",
598 dv
->devname
, strerror(errno
));
606 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
607 fprintf(stderr
, Name
": %s is not a "
613 switch(dv
->disposition
){
615 fprintf(stderr
, Name
": internal error - devmode[%s]=%d\n",
616 dv
->devname
, dv
->disposition
);
621 fprintf(stderr
, Name
": Cannot add disks to a"
622 " \'member\' array, perform this"
623 " operation on the parent container\n");
626 /* Make sure it isn't in use (in 2.6 or later) */
627 tfd
= dev_open(add_dev
, O_RDONLY
|O_EXCL
|O_DIRECT
);
628 if (tfd
< 0 && add_dev
!= dv
->devname
)
631 fprintf(stderr
, Name
": Cannot open %s: %s\n",
632 dv
->devname
, strerror(errno
));
636 if (sysfs_freeze_array(&info
) == 1)
644 if (array
.not_persistent
==0)
645 st
->ss
->load_super(st
, tfd
, NULL
);
647 if (add_dev
== dv
->devname
) {
648 if (!get_dev_size(tfd
, dv
->devname
, &ldsize
)) {
649 st
->ss
->free_super(st
);
653 } else if (!get_dev_size(tfd
, NULL
, &ldsize
)) {
654 st
->ss
->free_super(st
);
660 if (tst
->ss
->validate_geometry(
661 tst
, array
.level
, array
.layout
,
662 array
.raid_disks
, NULL
,
663 ldsize
>> 9, NULL
, NULL
, 0) == 0) {
666 ": %s is larger than %s can "
668 " Add --force is you "
669 "really want to add this device.\n",
671 st
->ss
->free_super(st
);
676 ": %s is larger than %s can "
678 " Adding anyway as --force "
682 if (!tst
->ss
->external
&&
683 array
.major_version
== 0 &&
684 md_get_version(fd
)%100 < 2) {
686 st
->ss
->free_super(st
);
688 if (ioctl(fd
, HOT_ADD_DISK
,
689 (unsigned long)stb
.st_rdev
)==0) {
691 fprintf(stderr
, Name
": hot added %s\n",
696 fprintf(stderr
, Name
": hot add failed for %s: %s\n",
697 add_dev
, strerror(errno
));
701 if (array
.not_persistent
== 0 || tst
->ss
->external
) {
703 /* need to find a sample superblock to copy, and
704 * a spare slot to use.
705 * For 'external' array (well, container based),
706 * We can just load the metadata for the array.
709 /* already loaded */;
710 else if (tst
->ss
->external
) {
711 tst
->ss
->load_container(tst
, fd
, NULL
);
712 } else for (j
= 0; j
< tst
->max_devs
; j
++) {
716 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
718 if (disc
.major
==0 && disc
.minor
==0)
720 if ((disc
.state
& 4)==0) continue; /* sync */
721 /* Looks like a good device to try */
722 dev
= map_dev(disc
.major
, disc
.minor
, 1);
724 dfd
= dev_open(dev
, O_RDONLY
);
725 if (dfd
< 0) continue;
726 if (tst
->ss
->load_super(tst
, dfd
,
734 /* FIXME this is a bad test to be using */
737 /* we are re-adding a device to a
738 * completely dead array - have to depend
741 } else if (!tst
->sb
) {
743 st
->ss
->free_super(st
);
744 fprintf(stderr
, Name
": cannot load array metadata from %s\n", devname
);
748 /* Make sure device is large enough */
749 if (tst
->ss
->avail_size(tst
, ldsize
/512) <
753 st
->ss
->free_super(st
);
754 if (add_dev
!= dv
->devname
)
756 fprintf(stderr
, Name
": %s not large enough to join array\n",
761 /* Possibly this device was recently part of the array
762 * and was temporarily removed, and is now being re-added.
763 * If so, we can simply re-add it.
768 st
->ss
->getinfo_super(st
, &mdi
, NULL
);
769 st
->ss
->uuid_from_super(st
, ouuid
);
771 tst
->ss
->uuid_from_super(tst
, duuid
);
773 /* Assume uuid matches: kernel will check */
774 memcpy(duuid
, ouuid
, sizeof(ouuid
));
775 if ((mdi
.disk
.state
& (1<<MD_DISK_ACTIVE
)) &&
776 !(mdi
.disk
.state
& (1<<MD_DISK_FAULTY
)) &&
777 memcmp(duuid
, ouuid
, sizeof(ouuid
))==0) {
778 /* look like it is worth a try. Need to
779 * make sure kernel will accept it though.
781 /* re-add doesn't work for version-1 superblocks
784 if (array
.major_version
== 1 &&
785 get_linux_version() <= 2006018)
787 disc
.number
= mdi
.disk
.number
;
788 if (ioctl(fd
, GET_DISK_INFO
, &disc
) != 0
789 || disc
.major
!= 0 || disc
.minor
!= 0
792 disc
.major
= major(stb
.st_rdev
);
793 disc
.minor
= minor(stb
.st_rdev
);
794 disc
.number
= mdi
.disk
.number
;
795 disc
.raid_disk
= mdi
.disk
.raid_disk
;
796 disc
.state
= mdi
.disk
.state
;
797 if (dv
->writemostly
== 1)
798 disc
.state
|= 1 << MD_DISK_WRITEMOSTLY
;
799 if (dv
->writemostly
== 2)
800 disc
.state
&= ~(1 << MD_DISK_WRITEMOSTLY
);
801 remove_partitions(tfd
);
804 if (update
|| dv
->writemostly
> 0) {
806 tfd
= dev_open(dv
->devname
, O_RDWR
);
808 fprintf(stderr
, Name
": failed to open %s for"
809 " superblock update during re-add\n", dv
->devname
);
810 st
->ss
->free_super(st
);
814 if (dv
->writemostly
== 1)
815 rv
= st
->ss
->update_super(
816 st
, NULL
, "writemostly",
817 devname
, verbose
, 0, NULL
);
818 if (dv
->writemostly
== 2)
819 rv
= st
->ss
->update_super(
820 st
, NULL
, "readwrite",
821 devname
, verbose
, 0, NULL
);
823 rv
= st
->ss
->update_super(
825 devname
, verbose
, 0, NULL
);
827 rv
= st
->ss
->store_super(st
, tfd
);
831 fprintf(stderr
, Name
": failed to update"
832 " superblock during re-add\n");
833 st
->ss
->free_super(st
);
837 /* don't even try if disk is marked as faulty */
839 if (ioctl(fd
, ADD_NEW_DISK
, &disc
) == 0) {
841 fprintf(stderr
, Name
": re-added %s\n", add_dev
);
843 st
->ss
->free_super(st
);
846 if (errno
== ENOMEM
|| errno
== EROFS
) {
847 fprintf(stderr
, Name
": add new device failed for %s: %s\n",
848 add_dev
, strerror(errno
));
849 st
->ss
->free_super(st
);
850 if (add_dev
!= dv
->devname
)
856 st
->ss
->free_super(st
);
858 if (add_dev
!= dv
->devname
) {
861 ": --re-add for %s to %s is not possible\n",
873 ": --re-add for %s to %s is not possible\n",
874 dv
->devname
, devname
);
877 if (array
.active_disks
< array
.raid_disks
) {
878 char *avail
= calloc(array
.raid_disks
, 1);
882 for (d
= 0; d
< MAX_DISKS
&& found
< array
.active_disks
; d
++) {
884 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
886 if (disc
.major
== 0 && disc
.minor
== 0)
888 if (!(disc
.state
& (1<<MD_DISK_SYNC
)))
890 avail
[disc
.raid_disk
] = 1;
893 array_failed
= !enough(array
.level
, array
.raid_disks
,
894 array
.layout
, 1, avail
);
898 fprintf(stderr
, Name
": %s has failed so using --add cannot work and might destroy\n",
900 fprintf(stderr
, Name
": data on %s. You should stop the array and re-assemble it.\n",
907 /* non-persistent. Must ensure that new drive
908 * is at least array.size big.
910 if (ldsize
/512 < array_size
) {
911 fprintf(stderr
, Name
": %s not large enough to join array\n",
918 /* committed to really trying this device now*/
920 remove_partitions(tfd
);
924 /* in 2.6.17 and earlier, version-1 superblocks won't
925 * use the number we write, but will choose a free number.
926 * we must choose the same free number, which requires
927 * starting at 'raid_disks' and counting up
929 for (j
= array
.raid_disks
; j
< tst
->max_devs
; j
++) {
931 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
933 if (disc
.major
==0 && disc
.minor
==0)
935 if (disc
.state
& 8) /* removed */
938 disc
.major
= major(stb
.st_rdev
);
939 disc
.minor
= minor(stb
.st_rdev
);
942 if (array
.not_persistent
==0) {
944 if (dv
->writemostly
== 1)
945 disc
.state
|= 1 << MD_DISK_WRITEMOSTLY
;
946 dfd
= dev_open(dv
->devname
, O_RDWR
| O_EXCL
|O_DIRECT
);
947 if (tst
->ss
->add_to_super(tst
, &disc
, dfd
,
952 if (tst
->ss
->write_init_super(tst
)) {
956 } else if (dv
->re_add
) {
957 /* this had better be raid1.
958 * As we are "--re-add"ing we must find a spare slot
961 char *used
= malloc(array
.raid_disks
);
962 memset(used
, 0, array
.raid_disks
);
963 for (j
=0; j
< tst
->max_devs
; j
++) {
964 mdu_disk_info_t disc2
;
966 if (ioctl(fd
, GET_DISK_INFO
, &disc2
))
968 if (disc2
.major
==0 && disc2
.minor
==0)
970 if (disc2
.state
& 8) /* removed */
972 if (disc2
.raid_disk
< 0)
974 if (disc2
.raid_disk
> array
.raid_disks
)
976 used
[disc2
.raid_disk
] = 1;
978 for (j
=0 ; j
<array
.raid_disks
; j
++)
981 disc
.state
|= (1<<MD_DISK_SYNC
);
986 if (dv
->writemostly
== 1)
987 disc
.state
|= (1 << MD_DISK_WRITEMOSTLY
);
988 if (tst
->ss
->external
) {
990 * to an external metadata container */
991 struct mdinfo new_mdi
;
994 int devnum
= fd2devnum(fd
);
997 container_fd
= open_dev_excl(devnum
);
998 if (container_fd
< 0) {
999 fprintf(stderr
, Name
": add failed for %s:"
1000 " could not get exclusive access to container\n",
1002 tst
->ss
->free_super(tst
);
1006 dfd
= dev_open(dv
->devname
, O_RDWR
| O_EXCL
|O_DIRECT
);
1007 if (mdmon_running(tst
->container_dev
))
1008 tst
->update_tail
= &tst
->updates
;
1009 if (tst
->ss
->add_to_super(tst
, &disc
, dfd
,
1012 close(container_fd
);
1015 if (tst
->update_tail
)
1016 flush_metadata_updates(tst
);
1018 tst
->ss
->sync_metadata(tst
);
1020 sra
= sysfs_read(container_fd
, -1, 0);
1022 fprintf(stderr
, Name
": add failed for %s: sysfs_read failed\n",
1024 close(container_fd
);
1025 tst
->ss
->free_super(tst
);
1028 sra
->array
.level
= LEVEL_CONTAINER
;
1029 /* Need to set data_offset and component_size */
1030 tst
->ss
->getinfo_super(tst
, &new_mdi
, NULL
);
1031 new_mdi
.disk
.major
= disc
.major
;
1032 new_mdi
.disk
.minor
= disc
.minor
;
1033 new_mdi
.recovery_start
= 0;
1034 /* Make sure fds are closed as they are O_EXCL which
1035 * would block add_disk */
1036 tst
->ss
->free_super(tst
);
1037 if (sysfs_add_disk(sra
, &new_mdi
, 0) != 0) {
1038 fprintf(stderr
, Name
": add new device to external metadata"
1039 " failed for %s\n", dv
->devname
);
1040 close(container_fd
);
1044 ping_monitor_by_id(devnum
);
1046 close(container_fd
);
1048 tst
->ss
->free_super(tst
);
1049 if (ioctl(fd
, ADD_NEW_DISK
, &disc
)) {
1050 fprintf(stderr
, Name
": add new device failed for %s as %d: %s\n",
1051 dv
->devname
, j
, strerror(errno
));
1056 fprintf(stderr
, Name
": added %s\n", dv
->devname
);
1062 fprintf(stderr
, Name
": Cannot remove disks from a"
1063 " \'member\' array, perform this"
1064 " operation on the parent container\n");
1069 if (tst
->ss
->external
) {
1070 /* To remove a device from a container, we must
1071 * check that it isn't in use in an array.
1072 * This involves looking in the 'holders'
1073 * directory - there must be just one entry,
1075 * To ensure that it doesn't get used as a
1076 * hold spare while we are checking, we
1077 * get an O_EXCL open on the container
1079 int dnum
= fd2devnum(fd
);
1080 lfd
= open_dev_excl(dnum
);
1082 fprintf(stderr
, Name
1083 ": Cannot get exclusive access "
1084 " to container - odd\n");
1089 /* in the detached case it is not possible to
1090 * check if we are the unique holder, so just
1091 * rely on the 'detached' checks
1093 if (strcmp(dv
->devname
, "detached") == 0 ||
1095 sysfs_unique_holder(dnum
, stb
.st_rdev
))
1098 fprintf(stderr
, Name
1099 ": %s is %s, cannot remove.\n",
1101 errno
== EEXIST
? "still in use":
1107 /* FIXME check that it is a current member */
1109 /* device has been removed and we don't know
1110 * the major:minor number
1112 int n
= write(sysfd
, "remove", 6);
1120 err
= ioctl(fd
, HOT_REMOVE_DISK
, (unsigned long)stb
.st_rdev
);
1121 if (err
&& errno
== ENODEV
) {
1122 /* Old kernels rejected this if no personality
1124 struct mdinfo
*sra
= sysfs_read(fd
, 0, GET_DEVS
);
1125 struct mdinfo
*dv
= NULL
;
1128 for ( ; dv
; dv
=dv
->next
)
1129 if (dv
->disk
.major
== (int)major(stb
.st_rdev
) &&
1130 dv
->disk
.minor
== (int)minor(stb
.st_rdev
))
1133 err
= sysfs_set_str(sra
, dv
,
1142 fprintf(stderr
, Name
": hot remove failed "
1143 "for %s: %s\n", dnprintable
,
1149 if (tst
->ss
->external
) {
1151 * Before dropping our exclusive open we make an
1152 * attempt at preventing mdmon from seeing an
1153 * 'add' event before reconciling this 'remove'
1156 char *name
= devnum2devname(fd2devnum(fd
));
1159 fprintf(stderr
, Name
": unable to get container name\n");
1170 fprintf(stderr
, Name
": hot removed %s from %s\n",
1171 dnprintable
, devname
);
1174 case 'f': /* set faulty */
1175 /* FIXME check current member */
1176 if ((sysfd
>= 0 && write(sysfd
, "faulty", 6) != 6) ||
1177 (sysfd
< 0 && ioctl(fd
, SET_DISK_FAULTY
,
1178 (unsigned long) stb
.st_rdev
))) {
1179 fprintf(stderr
, Name
": set device faulty failed for %s: %s\n",
1180 dnprintable
, strerror(errno
));
1190 fprintf(stderr
, Name
": set %s faulty in %s\n",
1191 dnprintable
, devname
);
1196 sysfs_set_str(&info
, NULL
, "sync_action","idle");
1197 if (test
&& count
== 0)
1203 sysfs_set_str(&info
, NULL
, "sync_action","idle");
1207 int autodetect(void)
1209 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1211 int fd
= dev_open("9:0", O_RDONLY
);
1213 if (ioctl(fd
, RAID_AUTORUN
, 0) == 0)
1220 int Update_subarray(char *dev
, char *subarray
, char *update
, struct mddev_ident
*ident
, int quiet
)
1222 struct supertype supertype
, *st
= &supertype
;
1225 memset(st
, 0, sizeof(*st
));
1227 fd
= open_subarray(dev
, subarray
, st
, quiet
);
1231 if (!st
->ss
->update_subarray
) {
1234 Name
": Operation not supported for %s metadata\n",
1239 if (mdmon_running(st
->devnum
))
1240 st
->update_tail
= &st
->updates
;
1242 rv
= st
->ss
->update_subarray(st
, subarray
, update
, ident
);
1246 fprintf(stderr
, Name
": Failed to update %s of subarray-%s in %s\n",
1247 update
, subarray
, dev
);
1248 } else if (st
->update_tail
)
1249 flush_metadata_updates(st
);
1251 st
->ss
->sync_metadata(st
);
1253 if (rv
== 0 && strcmp(update
, "name") == 0 && !quiet
)
1255 Name
": Updated subarray-%s name from %s, UUIDs may have changed\n",
1259 st
->ss
->free_super(st
);
1265 /* Move spare from one array to another
1266 * If adding to destination array fails
1267 * add back to original array
1268 * Returns 1 on success, 0 on failure */
1269 int move_spare(char *from_devname
, char *to_devname
, dev_t devid
)
1271 struct mddev_dev devlist
;
1274 /* try to remove and add */
1275 int fd1
= open(to_devname
, O_RDONLY
);
1276 int fd2
= open(from_devname
, O_RDONLY
);
1278 if (fd1
< 0 || fd2
< 0) {
1279 if (fd1
>=0) close(fd1
);
1280 if (fd2
>=0) close(fd2
);
1284 devlist
.next
= NULL
;
1287 devlist
.writemostly
= 0;
1288 devlist
.devname
= devname
;
1289 sprintf(devname
, "%d:%d", major(devid
), minor(devid
));
1291 devlist
.disposition
= 'r';
1292 if (Manage_subdevs(from_devname
, fd2
, &devlist
, -1, 0, NULL
, 0) == 0) {
1293 devlist
.disposition
= 'a';
1294 if (Manage_subdevs(to_devname
, fd1
, &devlist
, -1, 0, NULL
, 0) == 0) {
1295 /* make sure manager is aware of changes */
1296 ping_manager(to_devname
);
1297 ping_manager(from_devname
);
1302 else Manage_subdevs(from_devname
, fd2
, &devlist
, -1, 0, NULL
, 0);