mdctl-0.6
[thirdparty/mdadm.git] / Manage.c
CommitLineData
64c4757e
NB
1/*
2 * mdctl - manage Linux "md" devices aka RAID arrays.
3 *
cd29a5c8 4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
64c4757e
NB
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 "mdctl.h"
682c7051
NB
31#include "md_u.h"
32#include "md_p.h"
64c4757e 33
82b27616
NB
34#define REGISTER_DEV _IO (MD_MAJOR, 1)
35#define START_MD _IO (MD_MAJOR, 2)
36#define STOP_MD _IO (MD_MAJOR, 3)
37
64c4757e
NB
38int Manage_ro(char *devname, int fd, int readonly)
39{
682c7051
NB
40 /* switch to readonly or rw
41 *
42 * requires >= 0.90.0
43 * first check that array is runing
44 * use RESTART_ARRAY_RW or STOP_ARRAY_RO
45 *
46 */
47 mdu_array_info_t array;
48
49 if (md_get_version(fd) < 9000) {
50 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
51 return 1;
52 }
53 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
54 fprintf(stderr, Name ": %s does not appear to be active.\n",
55 devname);
56 return 1;
57 }
58
59 if (readonly>0) {
60 if (ioctl(fd, STOP_ARRAY_RO, NULL)) {
61 fprintf(stderr, Name ": failed to set readonly for %s: %s\n",
62 devname, strerror(errno));
63 return 1;
64 }
65 } else if (readonly < 0) {
66 if (ioctl(fd, RESTART_ARRAY_RW, NULL)) {
82b27616 67 fprintf(stderr, Name ": failed to set writable for %s: %s\n",
682c7051
NB
68 devname, strerror(errno));
69 return 1;
70 }
71 }
72 return 0;
64c4757e
NB
73}
74
75int Manage_runstop(char *devname, int fd, int runstop)
76{
682c7051
NB
77 /* Run or stop the array. array must already be configured
78 * required >= 0.90.0
79 */
80 mdu_array_info_t array;
81 mdu_param_t param; /* unused */
82b27616
NB
82
83 if (runstop == -1 && md_get_version(fd) < 9000) {
84 if (ioctl(fd, STOP_MD, 0)) {
85 fprintf(stderr, Name ": stopping device %s failed: %s\n",
86 devname, strerror(errno));
87 return 1;
88 }
89 }
682c7051
NB
90
91 if (md_get_version(fd) < 9000) {
92 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
93 return 1;
94 }
82b27616 95 /*
682c7051
NB
96 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
97 fprintf(stderr, Name ": %s does not appear to be active.\n",
98 devname);
99 return 1;
100 }
82b27616 101 */
682c7051
NB
102 if (runstop>0) {
103 if (ioctl(fd, RUN_ARRAY, &param)) {
104 fprintf(stderr, Name ": failed to run array %s: %s\n",
105 devname, strerror(errno));
106 return 1;
107 }
108 } else if (runstop < 0){
109 if (ioctl(fd, STOP_ARRAY, NULL)) {
52826846 110 fprintf(stderr, Name ": fail to stop array %s: %s\n",
682c7051
NB
111 devname, strerror(errno));
112 return 1;
113 }
114 }
115 return 0;
64c4757e
NB
116}
117
118int Manage_subdevs(char *devname, int fd,
cd29a5c8
NB
119 mddev_dev_t devlist)
120{
682c7051
NB
121 /* do something to each dev.
122 * devmode can be
123 * 'a' - add the device
124 * try HOT_ADD_DISK
125 * If that fails EINVAL, try ADD_NEW_DISK
126 * 'r' - remove the device HOT_REMOVE_DISK
127 * 'f' - set the device faulty SET_DISK_FAULTY
128 */
129 mdu_array_info_t array;
130 mdu_disk_info_t disc;
cd29a5c8 131 mddev_dev_t dv;
682c7051
NB
132 struct stat stb;
133 int i,j;
cd29a5c8
NB
134 int save_errno;
135 static buf[4096];
682c7051
NB
136
137 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
138 fprintf(stderr, Name ": cannot get array info for %s\n",
139 devname);
140 return 1;
141 }
cd29a5c8
NB
142 for (dv = devlist ; dv; dv=dv->next) {
143 if (stat(dv->devname, &stb)) {
682c7051 144 fprintf(stderr, Name ": cannot find %s: %s\n",
cd29a5c8 145 dv->devname, strerror(errno));
682c7051
NB
146 return 1;
147 }
148 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
149 fprintf(stderr, Name ": %s is not a block device.\n",
cd29a5c8 150 dv->devname);
682c7051
NB
151 return 1;
152 }
cd29a5c8 153 switch(dv->disposition){
682c7051
NB
154 default:
155 fprintf(stderr, Name ": internal error - devmode[%d]=%d\n",
cd29a5c8 156 i, dv->disposition);
682c7051
NB
157 return 1;
158 case 'a':
159 /* add the device - hot or cold */
cd29a5c8 160 if (ioctl(fd, HOT_ADD_DISK, (unsigned long)stb.st_rdev)==0) {
682c7051 161 fprintf(stderr, Name ": hot added %s\n",
cd29a5c8 162 dv->devname);
682c7051
NB
163 continue;
164 }
cd29a5c8
NB
165 save_errno = errno;
166 if (read(fd, buf, sizeof(buf)) > 0) {
167 /* array is active, so don't try to add.
168 * i.e. something is wrong
169 */
170 fprintf(stderr, Name ": hot add failed for %s: %s\n",
171 dv->devname, strerror(save_errno));
172 return 1;
173 }
682c7051
NB
174 /* try ADD_NEW_DISK.
175 * we might be creating, we might be assembling,
176 * it is hard to tell.
177 * set up number/raid_disk/state just
178 * in case
179 */
180 for (j=0; j<array.nr_disks; j++) {
181 if (ioctl(fd, GET_DISK_INFO, &disc))
182 break;
183 if (disc.major==0 && disc.minor==0)
184 break;
185 if (disc.state & 8) /* removed */
186 break;
187 }
188 disc.number =j;
189 disc.raid_disk = j;
190 disc.state = 0;
191 disc.major = MAJOR(stb.st_rdev);
192 disc.minor = MINOR(stb.st_rdev);
193 if (ioctl(fd,ADD_NEW_DISK, &disc)) {
194 fprintf(stderr, Name ": add new disk failed for %s: %s\n",
cd29a5c8 195 dv->devname, strerror(errno));
682c7051
NB
196 return 1;
197 }
cd29a5c8 198 fprintf(stderr, Name ": added %s\n", dv->devname);
682c7051
NB
199 break;
200
201 case 'r':
202 /* hot remove */
82b27616 203 /* FIXME check that it is a current member */
cd29a5c8 204 if (ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev)) {
682c7051 205 fprintf(stderr, Name ": hot remove failed for %s: %s\n",
cd29a5c8 206 dv->devname, strerror(errno));
682c7051
NB
207 return 1;
208 }
cd29a5c8 209 fprintf(stderr, Name ": hot removed %s\n", dv->devname);
682c7051
NB
210 break;
211
212 case 'f': /* set faulty */
213 /* FIXME check current member */
cd29a5c8 214 if (ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev)) {
682c7051 215 fprintf(stderr, Name ": set disk faulty failed for %s: %s\n",
cd29a5c8 216 dv->devname, strerror(errno));
682c7051
NB
217 return 1;
218 }
219 fprintf(stderr, Name ": set %s faulty in %s\n",
cd29a5c8 220 dv->devname, devname);
682c7051
NB
221 break;
222 }
223 }
224 return 0;
225
64c4757e 226}