]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Manage.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
34 #define REGISTER_DEV _IO (MD_MAJOR, 1)
35 #define START_MD _IO (MD_MAJOR, 2)
36 #define STOP_MD _IO (MD_MAJOR, 3)
38 int Manage_ro(char *devname
, int fd
, int readonly
)
40 /* switch to readonly or rw
43 * first check that array is runing
44 * use RESTART_ARRAY_RW or STOP_ARRAY_RO
47 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");
53 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)) {
54 fprintf(stderr
, Name
": %s does not appear to be active.\n",
60 if (ioctl(fd
, STOP_ARRAY_RO
, NULL
)) {
61 fprintf(stderr
, Name
": failed to set readonly for %s: %s\n",
62 devname
, strerror(errno
));
65 } else if (readonly
< 0) {
66 if (ioctl(fd
, RESTART_ARRAY_RW
, NULL
)) {
67 fprintf(stderr
, Name
": failed to set writable for %s: %s\n",
68 devname
, strerror(errno
));
75 int Manage_runstop(char *devname
, int fd
, int runstop
)
77 /* Run or stop the array. array must already be configured
80 mdu_param_t param
; /* unused */
82 if (runstop
== -1 && md_get_version(fd
) < 9000) {
83 if (ioctl(fd
, STOP_MD
, 0)) {
84 fprintf(stderr
, Name
": stopping device %s failed: %s\n",
85 devname
, strerror(errno
));
90 if (md_get_version(fd
) < 9000) {
91 fprintf(stderr
, Name
": need md driver version 0.90.0 or later\n");
95 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
96 fprintf(stderr, Name ": %s does not appear to be active.\n",
102 if (ioctl(fd
, RUN_ARRAY
, ¶m
)) {
103 fprintf(stderr
, Name
": failed to run array %s: %s\n",
104 devname
, strerror(errno
));
107 } else if (runstop
< 0){
108 if (ioctl(fd
, STOP_ARRAY
, NULL
)) {
109 fprintf(stderr
, Name
": fail to stop array %s: %s\n",
110 devname
, strerror(errno
));
117 int Manage_subdevs(char *devname
, int fd
,
120 /* do something to each dev.
122 * 'a' - add the device
124 * If that fails EINVAL, try ADD_NEW_DISK
125 * 'r' - remove the device HOT_REMOVE_DISK
126 * 'f' - set the device faulty SET_DISK_FAULTY
128 mdu_array_info_t array
;
129 mdu_disk_info_t disc
;
134 static char buf
[4096];
136 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)) {
137 fprintf(stderr
, Name
": cannot get array info for %s\n",
141 for (dv
= devlist
; dv
; dv
=dv
->next
) {
142 if (stat(dv
->devname
, &stb
)) {
143 fprintf(stderr
, Name
": cannot find %s: %s\n",
144 dv
->devname
, strerror(errno
));
147 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
148 fprintf(stderr
, Name
": %s is not a block device.\n",
152 switch(dv
->disposition
){
154 fprintf(stderr
, Name
": internal error - devmode[%s]=%d\n",
155 dv
->devname
, dv
->disposition
);
158 /* add the device - hot or cold */
159 if (ioctl(fd
, HOT_ADD_DISK
, (unsigned long)stb
.st_rdev
)==0) {
160 fprintf(stderr
, Name
": hot added %s\n",
165 if (read(fd
, buf
, sizeof(buf
)) > 0) {
166 /* array is active, so don't try to add.
167 * i.e. something is wrong
169 fprintf(stderr
, Name
": hot add failed for %s: %s\n",
170 dv
->devname
, strerror(save_errno
));
174 * we might be creating, we might be assembling,
175 * it is hard to tell.
176 * set up number/raid_disk/state just
179 for (j
=0; j
<array
.nr_disks
; j
++) {
180 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
182 if (disc
.major
==0 && disc
.minor
==0)
184 if (disc
.state
& 8) /* removed */
190 disc
.major
= MAJOR(stb
.st_rdev
);
191 disc
.minor
= MINOR(stb
.st_rdev
);
192 if (ioctl(fd
,ADD_NEW_DISK
, &disc
)) {
193 fprintf(stderr
, Name
": add new device failed for %s: %s\n",
194 dv
->devname
, strerror(errno
));
197 fprintf(stderr
, Name
": added %s\n", dv
->devname
);
202 /* FIXME check that it is a current member */
203 if (ioctl(fd
, HOT_REMOVE_DISK
, (unsigned long)stb
.st_rdev
)) {
204 fprintf(stderr
, Name
": hot remove failed for %s: %s\n",
205 dv
->devname
, strerror(errno
));
208 fprintf(stderr
, Name
": hot removed %s\n", dv
->devname
);
211 case 'f': /* set faulty */
212 /* FIXME check current member */
213 if (ioctl(fd
, SET_DISK_FAULTY
, (unsigned long) stb
.st_rdev
)) {
214 fprintf(stderr
, Name
": set device faulty failed for %s: %s\n",
215 dv
->devname
, strerror(errno
));
218 fprintf(stderr
, Name
": set %s faulty in %s\n",
219 dv
->devname
, devname
);