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