]>
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
;
451 int re_add_failed
= 0;
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
< 1024 && 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
< 1024 && 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 wan 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
)
857 st
->ss
->free_super(st
);
859 if (add_dev
!= dv
->devname
) {
862 ": --re-add for %s to %s is not possible\n",
874 ": --re-add for %s to %s is not possible\n",
875 dv
->devname
, devname
);
879 fprintf(stderr
, Name
": %s reports being an active member for %s, but a --re-add fails.\n",
880 dv
->devname
, devname
);
881 fprintf(stderr
, Name
": not performing --add as that would convert %s in to a spare.\n",
883 fprintf(stderr
, Name
": To make this a spare, use \"mdadm --zero-superblock %s\" first.\n",
890 /* non-persistent. Must ensure that new drive
891 * is at least array.size big.
893 if (ldsize
/512 < array_size
) {
894 fprintf(stderr
, Name
": %s not large enough to join array\n",
901 /* committed to really trying this device now*/
903 remove_partitions(tfd
);
907 /* in 2.6.17 and earlier, version-1 superblocks won't
908 * use the number we write, but will choose a free number.
909 * we must choose the same free number, which requires
910 * starting at 'raid_disks' and counting up
912 for (j
= array
.raid_disks
; j
< tst
->max_devs
; j
++) {
914 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
916 if (disc
.major
==0 && disc
.minor
==0)
918 if (disc
.state
& 8) /* removed */
921 disc
.major
= major(stb
.st_rdev
);
922 disc
.minor
= minor(stb
.st_rdev
);
925 if (array
.not_persistent
==0) {
927 if (dv
->writemostly
== 1)
928 disc
.state
|= 1 << MD_DISK_WRITEMOSTLY
;
929 dfd
= dev_open(dv
->devname
, O_RDWR
| O_EXCL
|O_DIRECT
);
930 if (tst
->ss
->add_to_super(tst
, &disc
, dfd
,
935 if (tst
->ss
->write_init_super(tst
)) {
939 } else if (dv
->re_add
) {
940 /* this had better be raid1.
941 * As we are "--re-add"ing we must find a spare slot
944 char *used
= malloc(array
.raid_disks
);
945 memset(used
, 0, array
.raid_disks
);
946 for (j
=0; j
< tst
->max_devs
; j
++) {
947 mdu_disk_info_t disc2
;
949 if (ioctl(fd
, GET_DISK_INFO
, &disc2
))
951 if (disc2
.major
==0 && disc2
.minor
==0)
953 if (disc2
.state
& 8) /* removed */
955 if (disc2
.raid_disk
< 0)
957 if (disc2
.raid_disk
> array
.raid_disks
)
959 used
[disc2
.raid_disk
] = 1;
961 for (j
=0 ; j
<array
.raid_disks
; j
++)
964 disc
.state
|= (1<<MD_DISK_SYNC
);
969 if (dv
->writemostly
== 1)
970 disc
.state
|= (1 << MD_DISK_WRITEMOSTLY
);
971 if (tst
->ss
->external
) {
973 * to an external metadata container */
974 struct mdinfo new_mdi
;
977 int devnum
= fd2devnum(fd
);
980 container_fd
= open_dev_excl(devnum
);
981 if (container_fd
< 0) {
982 fprintf(stderr
, Name
": add failed for %s:"
983 " could not get exclusive access to container\n",
985 tst
->ss
->free_super(tst
);
989 dfd
= dev_open(dv
->devname
, O_RDWR
| O_EXCL
|O_DIRECT
);
990 if (mdmon_running(tst
->container_dev
))
991 tst
->update_tail
= &tst
->updates
;
992 if (tst
->ss
->add_to_super(tst
, &disc
, dfd
,
998 if (tst
->update_tail
)
999 flush_metadata_updates(tst
);
1001 tst
->ss
->sync_metadata(tst
);
1003 sra
= sysfs_read(container_fd
, -1, 0);
1005 fprintf(stderr
, Name
": add failed for %s: sysfs_read failed\n",
1007 close(container_fd
);
1008 tst
->ss
->free_super(tst
);
1011 sra
->array
.level
= LEVEL_CONTAINER
;
1012 /* Need to set data_offset and component_size */
1013 tst
->ss
->getinfo_super(tst
, &new_mdi
, NULL
);
1014 new_mdi
.disk
.major
= disc
.major
;
1015 new_mdi
.disk
.minor
= disc
.minor
;
1016 new_mdi
.recovery_start
= 0;
1017 /* Make sure fds are closed as they are O_EXCL which
1018 * would block add_disk */
1019 tst
->ss
->free_super(tst
);
1020 if (sysfs_add_disk(sra
, &new_mdi
, 0) != 0) {
1021 fprintf(stderr
, Name
": add new device to external metadata"
1022 " failed for %s\n", dv
->devname
);
1023 close(container_fd
);
1027 ping_monitor_by_id(devnum
);
1029 close(container_fd
);
1031 tst
->ss
->free_super(tst
);
1032 if (ioctl(fd
, ADD_NEW_DISK
, &disc
)) {
1033 fprintf(stderr
, Name
": add new device failed for %s as %d: %s\n",
1034 dv
->devname
, j
, strerror(errno
));
1039 fprintf(stderr
, Name
": added %s\n", dv
->devname
);
1045 fprintf(stderr
, Name
": Cannot remove disks from a"
1046 " \'member\' array, perform this"
1047 " operation on the parent container\n");
1052 if (tst
->ss
->external
) {
1053 /* To remove a device from a container, we must
1054 * check that it isn't in use in an array.
1055 * This involves looking in the 'holders'
1056 * directory - there must be just one entry,
1058 * To ensure that it doesn't get used as a
1059 * hold spare while we are checking, we
1060 * get an O_EXCL open on the container
1062 int dnum
= fd2devnum(fd
);
1063 lfd
= open_dev_excl(dnum
);
1065 fprintf(stderr
, Name
1066 ": Cannot get exclusive access "
1067 " to container - odd\n");
1072 /* in the detached case it is not possible to
1073 * check if we are the unique holder, so just
1074 * rely on the 'detached' checks
1076 if (strcmp(dv
->devname
, "detached") == 0 ||
1078 sysfs_unique_holder(dnum
, stb
.st_rdev
))
1081 fprintf(stderr
, Name
1082 ": %s is %s, cannot remove.\n",
1084 errno
== EEXIST
? "still in use":
1090 /* FIXME check that it is a current member */
1092 /* device has been removed and we don't know
1093 * the major:minor number
1095 int n
= write(sysfd
, "remove", 6);
1103 err
= ioctl(fd
, HOT_REMOVE_DISK
, (unsigned long)stb
.st_rdev
);
1104 if (err
&& errno
== ENODEV
) {
1105 /* Old kernels rejected this if no personality
1107 struct mdinfo
*sra
= sysfs_read(fd
, 0, GET_DEVS
);
1108 struct mdinfo
*dv
= NULL
;
1111 for ( ; dv
; dv
=dv
->next
)
1112 if (dv
->disk
.major
== (int)major(stb
.st_rdev
) &&
1113 dv
->disk
.minor
== (int)minor(stb
.st_rdev
))
1116 err
= sysfs_set_str(sra
, dv
,
1125 fprintf(stderr
, Name
": hot remove failed "
1126 "for %s: %s\n", dnprintable
,
1132 if (tst
->ss
->external
) {
1134 * Before dropping our exclusive open we make an
1135 * attempt at preventing mdmon from seeing an
1136 * 'add' event before reconciling this 'remove'
1139 char *name
= devnum2devname(fd2devnum(fd
));
1142 fprintf(stderr
, Name
": unable to get container name\n");
1153 fprintf(stderr
, Name
": hot removed %s from %s\n",
1154 dnprintable
, devname
);
1157 case 'f': /* set faulty */
1158 /* FIXME check current member */
1159 if ((sysfd
>= 0 && write(sysfd
, "faulty", 6) != 6) ||
1160 (sysfd
< 0 && ioctl(fd
, SET_DISK_FAULTY
,
1161 (unsigned long) stb
.st_rdev
))) {
1162 fprintf(stderr
, Name
": set device faulty failed for %s: %s\n",
1163 dnprintable
, strerror(errno
));
1173 fprintf(stderr
, Name
": set %s faulty in %s\n",
1174 dnprintable
, devname
);
1179 sysfs_set_str(&info
, NULL
, "sync_action","idle");
1180 if (test
&& count
== 0)
1186 sysfs_set_str(&info
, NULL
, "sync_action","idle");
1190 int autodetect(void)
1192 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1194 int fd
= dev_open("9:0", O_RDONLY
);
1196 if (ioctl(fd
, RAID_AUTORUN
, 0) == 0)
1203 int Update_subarray(char *dev
, char *subarray
, char *update
, struct mddev_ident
*ident
, int quiet
)
1205 struct supertype supertype
, *st
= &supertype
;
1208 memset(st
, 0, sizeof(*st
));
1210 fd
= open_subarray(dev
, subarray
, st
, quiet
);
1214 if (!st
->ss
->update_subarray
) {
1217 Name
": Operation not supported for %s metadata\n",
1222 if (mdmon_running(st
->devnum
))
1223 st
->update_tail
= &st
->updates
;
1225 rv
= st
->ss
->update_subarray(st
, subarray
, update
, ident
);
1229 fprintf(stderr
, Name
": Failed to update %s of subarray-%s in %s\n",
1230 update
, subarray
, dev
);
1231 } else if (st
->update_tail
)
1232 flush_metadata_updates(st
);
1234 st
->ss
->sync_metadata(st
);
1236 if (rv
== 0 && strcmp(update
, "name") == 0 && !quiet
)
1238 Name
": Updated subarray-%s name from %s, UUIDs may have changed\n",
1242 st
->ss
->free_super(st
);
1248 /* Move spare from one array to another
1249 * If adding to destination array fails
1250 * add back to original array
1251 * Returns 1 on success, 0 on failure */
1252 int move_spare(char *from_devname
, char *to_devname
, dev_t devid
)
1254 struct mddev_dev devlist
;
1257 /* try to remove and add */
1258 int fd1
= open(to_devname
, O_RDONLY
);
1259 int fd2
= open(from_devname
, O_RDONLY
);
1261 if (fd1
< 0 || fd2
< 0) {
1262 if (fd1
>=0) close(fd1
);
1263 if (fd2
>=0) close(fd2
);
1267 devlist
.next
= NULL
;
1270 devlist
.writemostly
= 0;
1271 devlist
.devname
= devname
;
1272 sprintf(devname
, "%d:%d", major(devid
), minor(devid
));
1274 devlist
.disposition
= 'r';
1275 if (Manage_subdevs(from_devname
, fd2
, &devlist
, -1, 0, NULL
, 0) == 0) {
1276 devlist
.disposition
= 'a';
1277 if (Manage_subdevs(to_devname
, fd1
, &devlist
, -1, 0, NULL
, 0) == 0) {
1278 /* make sure manager is aware of changes */
1279 ping_manager(to_devname
);
1280 ping_manager(from_devname
);
1285 else Manage_subdevs(from_devname
, fd2
, &devlist
, -1, 0, NULL
, 0);