Add 'Array Slot' line to --examine for version-1 superblocks
[thirdparty/mdadm.git] / Manage.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
4f589ad0 4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
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)) {
ab56093f 109 if (quiet==0)
91f068bf
NB
110 fprintf(stderr, Name ": fail to stop array %s: %s\n",
111 devname, strerror(errno));
682c7051
NB
112 return 1;
113 }
ab56093f
NB
114 if (quiet <= 0)
115 fprintf(stderr, Name ": stopped %s\n", devname);
682c7051
NB
116 }
117 return 0;
64c4757e
NB
118}
119
dd0781e5
NB
120int Manage_resize(char *devname, int fd, long long size, int raid_disks)
121{
122 mdu_array_info_t info;
123 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
124 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
125 devname, strerror(errno));
126 return 1;
127 }
128 if (size >= 0)
129 info.size = size;
130 if (raid_disks > 0)
131 info.raid_disks = raid_disks;
132 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
133 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
134 devname, strerror(errno));
135 return 1;
136 }
137 return 0;
138}
139
b5e64645
NB
140int Manage_reconfig(char *devname, int fd, int layout)
141{
142 mdu_array_info_t info;
143 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
144 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
145 devname, strerror(errno));
146 return 1;
147 }
148 info.layout = layout;
149 printf("layout set to %d\n", info.layout);
150 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
151 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
152 devname, strerror(errno));
153 return 1;
154 }
155 return 0;
156}
dd0781e5 157
64c4757e 158int Manage_subdevs(char *devname, int fd,
dab6685f 159 mddev_dev_t devlist, int verbose)
cd29a5c8 160{
682c7051
NB
161 /* do something to each dev.
162 * devmode can be
163 * 'a' - add the device
164 * try HOT_ADD_DISK
165 * If that fails EINVAL, try ADD_NEW_DISK
166 * 'r' - remove the device HOT_REMOVE_DISK
167 * 'f' - set the device faulty SET_DISK_FAULTY
168 */
169 mdu_array_info_t array;
170 mdu_disk_info_t disc;
cd29a5c8 171 mddev_dev_t dv;
682c7051 172 struct stat stb;
c913b90e 173 int j;
0fbf459d 174 int tfd;
892debc8
NB
175 struct supertype *st;
176 void *dsuper = NULL;
fe80f49b
NB
177 void *osuper = NULL; /* original super */
178 int duuid[4];
179 int ouuid[4];
682c7051
NB
180
181 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
182 fprintf(stderr, Name ": cannot get array info for %s\n",
183 devname);
184 return 1;
185 }
cd29a5c8
NB
186 for (dv = devlist ; dv; dv=dv->next) {
187 if (stat(dv->devname, &stb)) {
682c7051 188 fprintf(stderr, Name ": cannot find %s: %s\n",
cd29a5c8 189 dv->devname, strerror(errno));
682c7051
NB
190 return 1;
191 }
192 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
193 fprintf(stderr, Name ": %s is not a block device.\n",
cd29a5c8 194 dv->devname);
682c7051
NB
195 return 1;
196 }
cd29a5c8 197 switch(dv->disposition){
682c7051 198 default:
c913b90e
NB
199 fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
200 dv->devname, dv->disposition);
682c7051
NB
201 return 1;
202 case 'a':
203 /* add the device - hot or cold */
fe80f49b
NB
204 st = super_by_version(array.major_version,
205 array.minor_version);
206 if (!st) {
207 fprintf(stderr, Name ": unsupport array - version %d.%d\n",
208 array.major_version, array.minor_version);
209 return 1;
210 }
211
f277ce36 212 /* Make sure it isn't in use (in 2.6 or later) */
0fbf459d
NB
213 tfd = open(dv->devname, O_RDONLY|O_EXCL);
214 if (tfd < 0) {
d7eaf49f
NB
215 fprintf(stderr, Name ": Cannot open %s: %s\n",
216 dv->devname, strerror(errno));
217 return 1;
218 }
fe80f49b
NB
219 if (array.not_persistent==0)
220 st->ss->load_super(st, tfd, &osuper, NULL);
221 /* will use osuper later */
0fbf459d 222 close(tfd);
fe80f49b 223
60d9a174
NB
224 if (array.major_version == 0 &&
225 md_get_version(fd)%100 < 2) {
892debc8
NB
226 if (ioctl(fd, HOT_ADD_DISK,
227 (unsigned long)stb.st_rdev)==0) {
dab6685f
NB
228 if (verbose >= 0)
229 fprintf(stderr, Name ": hot added %s\n",
230 dv->devname);
892debc8
NB
231 continue;
232 }
233
cd29a5c8 234 fprintf(stderr, Name ": hot add failed for %s: %s\n",
892debc8 235 dv->devname, strerror(errno));
cd29a5c8
NB
236 return 1;
237 }
892debc8 238
fe80f49b
NB
239 if (array.not_persistent == 0) {
240
241 /* need to find a sample superblock to copy, and
242 * a spare slot to use
243 */
244 for (j=0; j<st->max_devs; j++) {
245 char *dev;
246 int dfd;
247 disc.number = j;
248 if (ioctl(fd, GET_DISK_INFO, &disc))
249 continue;
250 if (disc.major==0 && disc.minor==0)
251 continue;
252 if ((disc.state & 4)==0) continue; /* sync */
253 /* Looks like a good device to try */
16c6fa80 254 dev = map_dev(disc.major, disc.minor, 1);
fe80f49b 255 if (!dev) continue;
16c6fa80 256 dfd = dev_open(dev, O_RDONLY);
fe80f49b
NB
257 if (dfd < 0) continue;
258 if (st->ss->load_super(st, dfd, &dsuper, NULL)) {
259 close(dfd);
260 continue;
261 }
892debc8 262 close(dfd);
fe80f49b
NB
263 break;
264 }
265 if (!dsuper) {
266 fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
267 return 1;
268 }
269 /* Possibly this device was recently part of the array
270 * and was temporarily removed, and is now being re-added.
271 * If so, we can simply re-add it.
272 */
273 st->ss->uuid_from_super(duuid, dsuper);
274
275 if (osuper) {
276 st->ss->uuid_from_super(ouuid, osuper);
277 if (memcmp(duuid, ouuid, sizeof(ouuid))==0) {
278 /* look close enough for now. Kernel
279 * will worry about where a bitmap
280 * based reconstruct is possible
281 */
282 struct mdinfo mdi;
31317663 283 st->ss->getinfo_super(&mdi, osuper);
fe80f49b
NB
284 disc.major = major(stb.st_rdev);
285 disc.minor = minor(stb.st_rdev);
286 disc.number = mdi.disk.number;
287 disc.raid_disk = mdi.disk.raid_disk;
288 disc.state = mdi.disk.state;
289 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
290 if (verbose >= 0)
291 fprintf(stderr, Name ": re-added %s\n", dv->devname);
308e1801 292 continue;
fe80f49b
NB
293 }
294 /* fall back on normal-add */
295 }
892debc8 296 }
892debc8 297 }
f277ce36 298 for (j=0; j< st->max_devs; j++) {
aa88f531 299 disc.number = j;
682c7051
NB
300 if (ioctl(fd, GET_DISK_INFO, &disc))
301 break;
302 if (disc.major==0 && disc.minor==0)
303 break;
304 if (disc.state & 8) /* removed */
305 break;
306 }
0df46c2a
NB
307 disc.major = major(stb.st_rdev);
308 disc.minor = minor(stb.st_rdev);
892debc8
NB
309 disc.number =j;
310 disc.state = 0;
fe80f49b
NB
311 if (array.not_persistent==0) {
312 if (dv->writemostly)
313 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
314 st->ss->add_to_super(dsuper, &disc);
1bf4e2d9 315 if (st->ss->write_init_super(st, dsuper, &disc, dv->devname))
fe80f49b
NB
316 return 1;
317 } else if (dv->re_add) {
318 /* this had better be raid1.
319 * As we are "--re-add"ing we must find a spare slot
320 * to fill.
321 */
322 char *used = malloc(array.raid_disks);
323 memset(used, 0, array.raid_disks);
324 for (j=0; j< st->max_devs; j++) {
325 mdu_disk_info_t disc2;
326 disc2.number = j;
327 if (ioctl(fd, GET_DISK_INFO, &disc2))
328 continue;
329 if (disc2.major==0 && disc2.minor==0)
330 continue;
331 if (disc2.state & 8) /* removed */
332 continue;
333 if (disc2.raid_disk < 0)
334 continue;
335 if (disc2.raid_disk > array.raid_disks)
336 continue;
337 used[disc2.raid_disk] = 1;
338 }
339 for (j=0 ; j<array.raid_disks; j++)
340 if (!used[j]) {
341 disc.raid_disk = j;
342 disc.state |= (1<<MD_DISK_SYNC);
343 break;
344 }
345 }
d8def61c
NB
346 if (dv->writemostly)
347 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
682c7051 348 if (ioctl(fd,ADD_NEW_DISK, &disc)) {
f277ce36
NB
349 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
350 dv->devname, j, strerror(errno));
682c7051
NB
351 return 1;
352 }
dab6685f
NB
353 if (verbose >= 0)
354 fprintf(stderr, Name ": added %s\n", dv->devname);
682c7051
NB
355 break;
356
357 case 'r':
358 /* hot remove */
82b27616 359 /* FIXME check that it is a current member */
cd29a5c8 360 if (ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev)) {
682c7051 361 fprintf(stderr, Name ": hot remove failed for %s: %s\n",
cd29a5c8 362 dv->devname, strerror(errno));
682c7051
NB
363 return 1;
364 }
dab6685f
NB
365 if (verbose >= 0)
366 fprintf(stderr, Name ": hot removed %s\n", dv->devname);
682c7051
NB
367 break;
368
369 case 'f': /* set faulty */
370 /* FIXME check current member */
cd29a5c8 371 if (ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev)) {
b83d95f3 372 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
cd29a5c8 373 dv->devname, strerror(errno));
682c7051
NB
374 return 1;
375 }
dab6685f
NB
376 if (verbose >= 0)
377 fprintf(stderr, Name ": set %s faulty in %s\n",
378 dv->devname, devname);
682c7051
NB
379 break;
380 }
381 }
382 return 0;
383
64c4757e 384}