Update tests and add linear-add
[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 }
8382f19b
NB
109 if (quiet <= 0)
110 fprintf(stderr, Name ": started %s\n", devname);
682c7051 111 } else if (runstop < 0){
8382f19b
NB
112 struct map_ent *map = NULL;
113 struct stat stb;
682c7051 114 if (ioctl(fd, STOP_ARRAY, NULL)) {
ab56093f 115 if (quiet==0)
91f068bf
NB
116 fprintf(stderr, Name ": fail to stop array %s: %s\n",
117 devname, strerror(errno));
682c7051
NB
118 return 1;
119 }
ab56093f
NB
120 if (quiet <= 0)
121 fprintf(stderr, Name ": stopped %s\n", devname);
8382f19b
NB
122 if (fstat(fd, &stb) == 0) {
123 int devnum;
124 if (major(stb.st_rdev) == MD_MAJOR)
125 devnum = minor(stb.st_rdev);
126 else
127 devnum = -1-(minor(stb.st_rdev)>>6);
128 map_delete(&map, devnum);
129 map_write(map);
130 map_free(map);
131 }
682c7051
NB
132 }
133 return 0;
64c4757e
NB
134}
135
dd0781e5
NB
136int Manage_resize(char *devname, int fd, long long size, int raid_disks)
137{
138 mdu_array_info_t info;
139 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
140 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
141 devname, strerror(errno));
142 return 1;
143 }
144 if (size >= 0)
145 info.size = size;
146 if (raid_disks > 0)
147 info.raid_disks = raid_disks;
148 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
149 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
150 devname, strerror(errno));
151 return 1;
152 }
153 return 0;
154}
155
b5e64645
NB
156int Manage_reconfig(char *devname, int fd, int layout)
157{
158 mdu_array_info_t info;
159 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
160 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
161 devname, strerror(errno));
162 return 1;
163 }
164 info.layout = layout;
165 printf("layout set to %d\n", info.layout);
166 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
167 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
168 devname, strerror(errno));
169 return 1;
170 }
171 return 0;
172}
dd0781e5 173
64c4757e 174int Manage_subdevs(char *devname, int fd,
dab6685f 175 mddev_dev_t devlist, int verbose)
cd29a5c8 176{
682c7051
NB
177 /* do something to each dev.
178 * devmode can be
179 * 'a' - add the device
180 * try HOT_ADD_DISK
181 * If that fails EINVAL, try ADD_NEW_DISK
182 * 'r' - remove the device HOT_REMOVE_DISK
b80da661
NB
183 * device can be 'faulty' or 'detached' in which case all
184 * matching devices are removed.
682c7051 185 * 'f' - set the device faulty SET_DISK_FAULTY
b80da661
NB
186 * device can be 'detached' in which case any device that
187 * is inaccessible will be marked faulty.
682c7051
NB
188 */
189 mdu_array_info_t array;
190 mdu_disk_info_t disc;
b80da661 191 mddev_dev_t dv, next = NULL;
682c7051 192 struct stat stb;
b80da661 193 int j, jnext = 0;
0fbf459d 194 int tfd;
892debc8
NB
195 struct supertype *st;
196 void *dsuper = NULL;
fe80f49b
NB
197 void *osuper = NULL; /* original super */
198 int duuid[4];
199 int ouuid[4];
682c7051
NB
200
201 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
202 fprintf(stderr, Name ": cannot get array info for %s\n",
203 devname);
204 return 1;
205 }
b80da661 206 for (dv = devlist, j=0 ; dv; dv = next, j = jnext) {
4a39c6f2 207 unsigned long long ldsize;
b80da661
NB
208 char dvname[20];
209 char *dnprintable = dv->devname;
4a39c6f2 210
b80da661
NB
211 next = dv->next;
212 jnext = 0;
213
214 if (strcmp(dv->devname, "failed")==0 ||
215 strcmp(dv->devname, "faulty")==0) {
216 if (dv->disposition != 'r') {
217 fprintf(stderr, Name ": %s only meaningful "
218 "with -r, not -%c\n",
219 dv->devname, dv->disposition);
220 return 1;
221 }
222 for (; j < array.raid_disks + array.nr_disks ; j++) {
223 disc.number = j;
224 if (ioctl(fd, GET_DISK_INFO, &disc))
225 continue;
226 if (disc.major == 0 && disc.minor == 0)
227 continue;
228 if ((disc.state & 1) == 0) /* faulty */
229 continue;
230 stb.st_rdev = makedev(disc.major, disc.minor);
231 next = dv;
232 jnext = j+1;
233 sprintf(dvname,"%d:%d", disc.major, disc.minor);
234 dnprintable = dvname;
235 break;
236 }
237 if (jnext == 0)
238 continue;
239 } else if (strcmp(dv->devname, "detached") == 0) {
240 if (dv->disposition != 'r' && dv->disposition != 'f') {
241 fprintf(stderr, Name ": %s only meaningful "
242 "with -r of -f, not -%c\n",
243 dv->devname, dv->disposition);
244 return 1;
245 }
246 for (; j < array.raid_disks + array.nr_disks; j++) {
247 int sfd;
248 disc.number = j;
249 if (ioctl(fd, GET_DISK_INFO, &disc))
250 continue;
251 if (disc.major == 0 && disc.minor == 0)
252 continue;
253 sprintf(dvname,"%d:%d", disc.major, disc.minor);
254 sfd = dev_open(dvname, O_RDONLY);
255 if (sfd >= 0) {
256 close(sfd);
257 continue;
258 }
259 if (dv->disposition == 'f' &&
260 (disc.state & 1) == 1) /* already faulty */
261 continue;
262 if (errno != ENXIO)
263 continue;
264 stb.st_rdev = makedev(disc.major, disc.minor);
265 next = dv;
266 jnext = j+1;
267 dnprintable = dvname;
268 break;
269 }
270 if (jnext == 0)
271 continue;
272 } else {
273 j = 0;
274
275 if (stat(dv->devname, &stb)) {
276 fprintf(stderr, Name ": cannot find %s: %s\n",
277 dv->devname, strerror(errno));
278 return 1;
279 }
280 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
281 fprintf(stderr, Name ": %s is not a "
282 "block device.\n",
283 dv->devname);
284 return 1;
285 }
682c7051 286 }
cd29a5c8 287 switch(dv->disposition){
682c7051 288 default:
c913b90e
NB
289 fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
290 dv->devname, dv->disposition);
682c7051
NB
291 return 1;
292 case 'a':
4a39c6f2 293 /* add the device */
fe80f49b
NB
294 st = super_by_version(array.major_version,
295 array.minor_version);
296 if (!st) {
297 fprintf(stderr, Name ": unsupport array - version %d.%d\n",
298 array.major_version, array.minor_version);
299 return 1;
300 }
301
f277ce36 302 /* Make sure it isn't in use (in 2.6 or later) */
0fbf459d
NB
303 tfd = open(dv->devname, O_RDONLY|O_EXCL);
304 if (tfd < 0) {
d7eaf49f
NB
305 fprintf(stderr, Name ": Cannot open %s: %s\n",
306 dv->devname, strerror(errno));
307 return 1;
308 }
fe80f49b
NB
309 if (array.not_persistent==0)
310 st->ss->load_super(st, tfd, &osuper, NULL);
311 /* will use osuper later */
beae1dfe 312 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
4a39c6f2
NB
313 close(tfd);
314 return 1;
4a39c6f2 315 }
0fbf459d 316 close(tfd);
fe80f49b 317
60d9a174
NB
318 if (array.major_version == 0 &&
319 md_get_version(fd)%100 < 2) {
892debc8
NB
320 if (ioctl(fd, HOT_ADD_DISK,
321 (unsigned long)stb.st_rdev)==0) {
dab6685f
NB
322 if (verbose >= 0)
323 fprintf(stderr, Name ": hot added %s\n",
324 dv->devname);
892debc8
NB
325 continue;
326 }
327
cd29a5c8 328 fprintf(stderr, Name ": hot add failed for %s: %s\n",
892debc8 329 dv->devname, strerror(errno));
cd29a5c8
NB
330 return 1;
331 }
892debc8 332
fe80f49b
NB
333 if (array.not_persistent == 0) {
334
4a39c6f2
NB
335 /* Make sure device is large enough */
336 if (st->ss->avail_size(st, ldsize/512) <
337 array.size) {
338 fprintf(stderr, Name ": %s not large enough to join array\n",
339 dv->devname);
340 return 1;
341 }
342
fe80f49b
NB
343 /* need to find a sample superblock to copy, and
344 * a spare slot to use
345 */
346 for (j=0; j<st->max_devs; j++) {
347 char *dev;
348 int dfd;
349 disc.number = j;
350 if (ioctl(fd, GET_DISK_INFO, &disc))
351 continue;
352 if (disc.major==0 && disc.minor==0)
353 continue;
354 if ((disc.state & 4)==0) continue; /* sync */
355 /* Looks like a good device to try */
16c6fa80 356 dev = map_dev(disc.major, disc.minor, 1);
fe80f49b 357 if (!dev) continue;
16c6fa80 358 dfd = dev_open(dev, O_RDONLY);
fe80f49b
NB
359 if (dfd < 0) continue;
360 if (st->ss->load_super(st, dfd, &dsuper, NULL)) {
361 close(dfd);
362 continue;
363 }
0430ed48 364 remove_partitions(dfd);
892debc8 365 close(dfd);
fe80f49b
NB
366 break;
367 }
368 if (!dsuper) {
369 fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
370 return 1;
371 }
372 /* Possibly this device was recently part of the array
373 * and was temporarily removed, and is now being re-added.
374 * If so, we can simply re-add it.
375 */
376 st->ss->uuid_from_super(duuid, dsuper);
7eae7080
NB
377
378 /* re-add doesn't work for version-1 superblocks
379 * before 2.6.18 :-(
380 */
381 if (array.major_version == 1 &&
382 get_linux_version() <= 2006018)
383 ;
384 else if (osuper) {
fe80f49b
NB
385 st->ss->uuid_from_super(ouuid, osuper);
386 if (memcmp(duuid, ouuid, sizeof(ouuid))==0) {
387 /* look close enough for now. Kernel
388 * will worry about where a bitmap
389 * based reconstruct is possible
390 */
391 struct mdinfo mdi;
31317663 392 st->ss->getinfo_super(&mdi, osuper);
fe80f49b
NB
393 disc.major = major(stb.st_rdev);
394 disc.minor = minor(stb.st_rdev);
395 disc.number = mdi.disk.number;
396 disc.raid_disk = mdi.disk.raid_disk;
397 disc.state = mdi.disk.state;
398 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
399 if (verbose >= 0)
400 fprintf(stderr, Name ": re-added %s\n", dv->devname);
308e1801 401 continue;
fe80f49b
NB
402 }
403 /* fall back on normal-add */
404 }
892debc8 405 }
4a39c6f2
NB
406 } else {
407 /* non-persistent. Must ensure that new drive
408 * is at least array.size big.
409 */
410 if (ldsize/512 < array.size) {
411 fprintf(stderr, Name ": %s not large enough to join array\n",
412 dv->devname);
413 return 1;
414 }
892debc8 415 }
7eae7080
NB
416 /* in 2.6.17 and earlier, version-1 superblocks won't
417 * use the number we write, but will choose a free number.
418 * we must choose the same free number, which requires
419 * starting at 'raid_disks' and counting up
420 */
421 for (j = array.raid_disks; j< st->max_devs; j++) {
aa88f531 422 disc.number = j;
682c7051
NB
423 if (ioctl(fd, GET_DISK_INFO, &disc))
424 break;
425 if (disc.major==0 && disc.minor==0)
426 break;
427 if (disc.state & 8) /* removed */
428 break;
429 }
0df46c2a
NB
430 disc.major = major(stb.st_rdev);
431 disc.minor = minor(stb.st_rdev);
892debc8
NB
432 disc.number =j;
433 disc.state = 0;
fe80f49b
NB
434 if (array.not_persistent==0) {
435 if (dv->writemostly)
436 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
437 st->ss->add_to_super(dsuper, &disc);
1bf4e2d9 438 if (st->ss->write_init_super(st, dsuper, &disc, dv->devname))
fe80f49b
NB
439 return 1;
440 } else if (dv->re_add) {
441 /* this had better be raid1.
442 * As we are "--re-add"ing we must find a spare slot
443 * to fill.
444 */
445 char *used = malloc(array.raid_disks);
446 memset(used, 0, array.raid_disks);
447 for (j=0; j< st->max_devs; j++) {
448 mdu_disk_info_t disc2;
449 disc2.number = j;
450 if (ioctl(fd, GET_DISK_INFO, &disc2))
451 continue;
452 if (disc2.major==0 && disc2.minor==0)
453 continue;
454 if (disc2.state & 8) /* removed */
455 continue;
456 if (disc2.raid_disk < 0)
457 continue;
458 if (disc2.raid_disk > array.raid_disks)
459 continue;
460 used[disc2.raid_disk] = 1;
461 }
462 for (j=0 ; j<array.raid_disks; j++)
463 if (!used[j]) {
464 disc.raid_disk = j;
465 disc.state |= (1<<MD_DISK_SYNC);
466 break;
467 }
468 }
d8def61c
NB
469 if (dv->writemostly)
470 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
682c7051 471 if (ioctl(fd,ADD_NEW_DISK, &disc)) {
f277ce36
NB
472 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
473 dv->devname, j, strerror(errno));
682c7051
NB
474 return 1;
475 }
dab6685f
NB
476 if (verbose >= 0)
477 fprintf(stderr, Name ": added %s\n", dv->devname);
682c7051
NB
478 break;
479
480 case 'r':
481 /* hot remove */
82b27616 482 /* FIXME check that it is a current member */
cd29a5c8 483 if (ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev)) {
b80da661
NB
484 fprintf(stderr, Name ": hot remove failed "
485 "for %s: %s\n", dnprintable,
486 strerror(errno));
682c7051
NB
487 return 1;
488 }
dab6685f 489 if (verbose >= 0)
b80da661
NB
490 fprintf(stderr, Name ": hot removed %s\n",
491 dnprintable);
682c7051
NB
492 break;
493
494 case 'f': /* set faulty */
495 /* FIXME check current member */
cd29a5c8 496 if (ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev)) {
b83d95f3 497 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
b80da661 498 dnprintable, strerror(errno));
682c7051
NB
499 return 1;
500 }
dab6685f
NB
501 if (verbose >= 0)
502 fprintf(stderr, Name ": set %s faulty in %s\n",
b80da661 503 dnprintable, devname);
682c7051
NB
504 break;
505 }
506 }
507 return 0;
508
64c4757e 509}
435d4ebb 510#endif