Add a test.
[thirdparty/mdadm.git] / Manage.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 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
9a9dab36 30#include "mdadm.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
91f068bf 75int Manage_runstop(char *devname, int fd, int runstop, int quiet)
64c4757e 76{
682c7051
NB
77 /* Run or stop the array. array must already be configured
78 * required >= 0.90.0
79 */
682c7051 80 mdu_param_t param; /* unused */
82b27616
NB
81
82 if (runstop == -1 && md_get_version(fd) < 9000) {
83 if (ioctl(fd, STOP_MD, 0)) {
91f068bf
NB
84 if (!quiet) fprintf(stderr, Name ": stopping device %s failed: %s\n",
85 devname, strerror(errno));
82b27616
NB
86 return 1;
87 }
88 }
682c7051
NB
89
90 if (md_get_version(fd) < 9000) {
91 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
92 return 1;
93 }
82b27616 94 /*
682c7051
NB
95 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
96 fprintf(stderr, Name ": %s does not appear to be active.\n",
97 devname);
98 return 1;
99 }
82b27616 100 */
682c7051
NB
101 if (runstop>0) {
102 if (ioctl(fd, RUN_ARRAY, &param)) {
103 fprintf(stderr, Name ": failed to run array %s: %s\n",
104 devname, strerror(errno));
105 return 1;
106 }
107 } else if (runstop < 0){
108 if (ioctl(fd, STOP_ARRAY, NULL)) {
91f068bf
NB
109 if (!quiet)
110 fprintf(stderr, Name ": fail to stop array %s: %s\n",
111 devname, strerror(errno));
682c7051
NB
112 return 1;
113 }
114 }
115 return 0;
64c4757e
NB
116}
117
dd0781e5
NB
118int Manage_resize(char *devname, int fd, long long size, int raid_disks)
119{
120 mdu_array_info_t info;
121 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
122 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
123 devname, strerror(errno));
124 return 1;
125 }
126 if (size >= 0)
127 info.size = size;
128 if (raid_disks > 0)
129 info.raid_disks = raid_disks;
130 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
131 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
132 devname, strerror(errno));
133 return 1;
134 }
135 return 0;
136}
137
b5e64645
NB
138int Manage_reconfig(char *devname, int fd, int layout)
139{
140 mdu_array_info_t info;
141 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
142 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
143 devname, strerror(errno));
144 return 1;
145 }
146 info.layout = layout;
147 printf("layout set to %d\n", info.layout);
148 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
149 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
150 devname, strerror(errno));
151 return 1;
152 }
153 return 0;
154}
dd0781e5 155
64c4757e 156int Manage_subdevs(char *devname, int fd,
cd29a5c8
NB
157 mddev_dev_t devlist)
158{
682c7051
NB
159 /* do something to each dev.
160 * devmode can be
161 * 'a' - add the device
162 * try HOT_ADD_DISK
163 * If that fails EINVAL, try ADD_NEW_DISK
164 * 'r' - remove the device HOT_REMOVE_DISK
165 * 'f' - set the device faulty SET_DISK_FAULTY
166 */
167 mdu_array_info_t array;
168 mdu_disk_info_t disc;
cd29a5c8 169 mddev_dev_t dv;
682c7051 170 struct stat stb;
c913b90e 171 int j;
0fbf459d 172 int tfd;
892debc8
NB
173 struct supertype *st;
174 void *dsuper = NULL;
682c7051
NB
175
176 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
177 fprintf(stderr, Name ": cannot get array info for %s\n",
178 devname);
179 return 1;
180 }
cd29a5c8
NB
181 for (dv = devlist ; dv; dv=dv->next) {
182 if (stat(dv->devname, &stb)) {
682c7051 183 fprintf(stderr, Name ": cannot find %s: %s\n",
cd29a5c8 184 dv->devname, strerror(errno));
682c7051
NB
185 return 1;
186 }
187 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
188 fprintf(stderr, Name ": %s is not a block device.\n",
cd29a5c8 189 dv->devname);
682c7051
NB
190 return 1;
191 }
cd29a5c8 192 switch(dv->disposition){
682c7051 193 default:
c913b90e
NB
194 fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
195 dv->devname, dv->disposition);
682c7051
NB
196 return 1;
197 case 'a':
198 /* add the device - hot or cold */
f277ce36 199 /* Make sure it isn't in use (in 2.6 or later) */
0fbf459d
NB
200 tfd = open(dv->devname, O_RDONLY|O_EXCL);
201 if (tfd < 0) {
d7eaf49f
NB
202 fprintf(stderr, Name ": Cannot open %s: %s\n",
203 dv->devname, strerror(errno));
204 return 1;
205 }
0fbf459d 206 close(tfd);
60d9a174
NB
207 if (array.major_version == 0 &&
208 md_get_version(fd)%100 < 2) {
892debc8
NB
209 if (ioctl(fd, HOT_ADD_DISK,
210 (unsigned long)stb.st_rdev)==0) {
211 fprintf(stderr, Name ": hot added %s\n",
212 dv->devname);
213 continue;
214 }
215
cd29a5c8 216 fprintf(stderr, Name ": hot add failed for %s: %s\n",
892debc8 217 dv->devname, strerror(errno));
cd29a5c8
NB
218 return 1;
219 }
892debc8
NB
220
221 /* need to find a sample superblock to copy, and
222 * a spare slot to use
682c7051 223 */
892debc8
NB
224 st = super_by_version(array.major_version,
225 array.minor_version);
226 if (!st) {
227 fprintf(stderr, Name ": unsupport array - version %d.%d\n",
228 array.major_version, array.minor_version);
229 return 1;
230 }
f277ce36 231 for (j=0; j<st->max_devs; j++) {
892debc8
NB
232 char *dev;
233 int dfd;
234 disc.number = j;
235 if (ioctl(fd, GET_DISK_INFO, &disc))
236 continue;
237 if (disc.major==0 && disc.minor==0)
238 continue;
239 if ((disc.state & 4)==0) continue; /* sync */
240 /* Looks like a good device to try */
241 dev = map_dev(disc.major, disc.minor);
242 if (!dev) continue;
243 dfd = open(dev, O_RDONLY);
244 if (dfd < 0) continue;
245 if (st->ss->load_super(st, dfd, &dsuper, NULL)) {
246 close(dfd);
247 continue;
248 }
249 close(dfd);
250 break;
251 }
252 if (!dsuper) {
253 fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
254 return 1;
255 }
f277ce36 256 for (j=0; j< st->max_devs; j++) {
aa88f531 257 disc.number = j;
682c7051
NB
258 if (ioctl(fd, GET_DISK_INFO, &disc))
259 break;
260 if (disc.major==0 && disc.minor==0)
261 break;
262 if (disc.state & 8) /* removed */
263 break;
264 }
0df46c2a
NB
265 disc.major = major(stb.st_rdev);
266 disc.minor = minor(stb.st_rdev);
892debc8
NB
267 disc.number =j;
268 disc.state = 0;
dfd4d8ee
NB
269 if (dv->writemostly)
270 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
f277ce36 271 st->ss->add_to_super(dsuper, &disc);
892debc8
NB
272 if (st->ss->write_init_super(st, dsuper, &disc, dv->devname))
273 return 1;
682c7051 274 if (ioctl(fd,ADD_NEW_DISK, &disc)) {
f277ce36
NB
275 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
276 dv->devname, j, strerror(errno));
682c7051
NB
277 return 1;
278 }
cd29a5c8 279 fprintf(stderr, Name ": added %s\n", dv->devname);
682c7051
NB
280 break;
281
282 case 'r':
283 /* hot remove */
82b27616 284 /* FIXME check that it is a current member */
cd29a5c8 285 if (ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev)) {
682c7051 286 fprintf(stderr, Name ": hot remove failed for %s: %s\n",
cd29a5c8 287 dv->devname, strerror(errno));
682c7051
NB
288 return 1;
289 }
cd29a5c8 290 fprintf(stderr, Name ": hot removed %s\n", dv->devname);
682c7051
NB
291 break;
292
293 case 'f': /* set faulty */
294 /* FIXME check current member */
cd29a5c8 295 if (ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev)) {
b83d95f3 296 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
cd29a5c8 297 dv->devname, strerror(errno));
682c7051
NB
298 return 1;
299 }
300 fprintf(stderr, Name ": set %s faulty in %s\n",
cd29a5c8 301 dv->devname, devname);
682c7051
NB
302 break;
303 }
304 }
305 return 0;
306
64c4757e 307}