Fix for segfault when reading /proc/mdstat
[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;
aba69144 48
682c7051
NB
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 }
aba69144 58
682c7051
NB
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 }
aba69144 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 }
aba69144 91
682c7051
NB
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;
3da92f27 195 struct supertype *st, *tst;
fe80f49b
NB
196 int duuid[4];
197 int ouuid[4];
682c7051
NB
198
199 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
200 fprintf(stderr, Name ": cannot get array info for %s\n",
201 devname);
202 return 1;
203 }
3da92f27 204
1686dc25 205 tst = super_by_fd(fd);
3da92f27
NB
206 if (!tst) {
207 fprintf(stderr, Name ": unsupport array - version %d.%d\n",
208 array.major_version, array.minor_version);
209 return 1;
210 }
211
b80da661 212 for (dv = devlist, j=0 ; dv; dv = next, j = jnext) {
4a39c6f2 213 unsigned long long ldsize;
b80da661
NB
214 char dvname[20];
215 char *dnprintable = dv->devname;
4a39c6f2 216
b80da661
NB
217 next = dv->next;
218 jnext = 0;
219
220 if (strcmp(dv->devname, "failed")==0 ||
221 strcmp(dv->devname, "faulty")==0) {
222 if (dv->disposition != 'r') {
223 fprintf(stderr, Name ": %s only meaningful "
224 "with -r, not -%c\n",
225 dv->devname, dv->disposition);
226 return 1;
227 }
228 for (; j < array.raid_disks + array.nr_disks ; j++) {
229 disc.number = j;
230 if (ioctl(fd, GET_DISK_INFO, &disc))
231 continue;
232 if (disc.major == 0 && disc.minor == 0)
233 continue;
234 if ((disc.state & 1) == 0) /* faulty */
235 continue;
236 stb.st_rdev = makedev(disc.major, disc.minor);
237 next = dv;
238 jnext = j+1;
239 sprintf(dvname,"%d:%d", disc.major, disc.minor);
240 dnprintable = dvname;
241 break;
242 }
243 if (jnext == 0)
244 continue;
245 } else if (strcmp(dv->devname, "detached") == 0) {
246 if (dv->disposition != 'r' && dv->disposition != 'f') {
247 fprintf(stderr, Name ": %s only meaningful "
248 "with -r of -f, not -%c\n",
249 dv->devname, dv->disposition);
250 return 1;
251 }
252 for (; j < array.raid_disks + array.nr_disks; j++) {
253 int sfd;
254 disc.number = j;
255 if (ioctl(fd, GET_DISK_INFO, &disc))
256 continue;
257 if (disc.major == 0 && disc.minor == 0)
258 continue;
259 sprintf(dvname,"%d:%d", disc.major, disc.minor);
260 sfd = dev_open(dvname, O_RDONLY);
261 if (sfd >= 0) {
262 close(sfd);
263 continue;
264 }
265 if (dv->disposition == 'f' &&
266 (disc.state & 1) == 1) /* already faulty */
267 continue;
268 if (errno != ENXIO)
269 continue;
270 stb.st_rdev = makedev(disc.major, disc.minor);
271 next = dv;
272 jnext = j+1;
273 dnprintable = dvname;
274 break;
275 }
276 if (jnext == 0)
277 continue;
278 } else {
279 j = 0;
280
281 if (stat(dv->devname, &stb)) {
282 fprintf(stderr, Name ": cannot find %s: %s\n",
283 dv->devname, strerror(errno));
284 return 1;
285 }
286 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
287 fprintf(stderr, Name ": %s is not a "
288 "block device.\n",
289 dv->devname);
290 return 1;
291 }
682c7051 292 }
cd29a5c8 293 switch(dv->disposition){
682c7051 294 default:
c913b90e
NB
295 fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
296 dv->devname, dv->disposition);
682c7051
NB
297 return 1;
298 case 'a':
4a39c6f2 299 /* add the device */
fe80f49b 300
f277ce36 301 /* Make sure it isn't in use (in 2.6 or later) */
0fbf459d
NB
302 tfd = open(dv->devname, O_RDONLY|O_EXCL);
303 if (tfd < 0) {
d7eaf49f
NB
304 fprintf(stderr, Name ": Cannot open %s: %s\n",
305 dv->devname, strerror(errno));
306 return 1;
307 }
25037aa8 308 remove_partitions(tfd);
3da92f27 309
1686dc25 310 st = dup_super(tst);
3da92f27 311
fe80f49b 312 if (array.not_persistent==0)
3da92f27
NB
313 st->ss->load_super(st, tfd, NULL);
314
beae1dfe 315 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
4a39c6f2
NB
316 close(tfd);
317 return 1;
4a39c6f2 318 }
0fbf459d 319 close(tfd);
fe80f49b 320
60d9a174
NB
321 if (array.major_version == 0 &&
322 md_get_version(fd)%100 < 2) {
892debc8
NB
323 if (ioctl(fd, HOT_ADD_DISK,
324 (unsigned long)stb.st_rdev)==0) {
dab6685f
NB
325 if (verbose >= 0)
326 fprintf(stderr, Name ": hot added %s\n",
327 dv->devname);
892debc8
NB
328 continue;
329 }
330
cd29a5c8 331 fprintf(stderr, Name ": hot add failed for %s: %s\n",
892debc8 332 dv->devname, strerror(errno));
cd29a5c8
NB
333 return 1;
334 }
892debc8 335
fe80f49b
NB
336 if (array.not_persistent == 0) {
337
4a39c6f2 338 /* Make sure device is large enough */
3da92f27 339 if (tst->ss->avail_size(tst, ldsize/512) <
4a39c6f2
NB
340 array.size) {
341 fprintf(stderr, Name ": %s not large enough to join array\n",
342 dv->devname);
343 return 1;
344 }
345
fe80f49b 346 /* need to find a sample superblock to copy, and
aba69144 347 * a spare slot to use
fe80f49b 348 */
3da92f27 349 for (j = 0; j < tst->max_devs; j++) {
fe80f49b
NB
350 char *dev;
351 int dfd;
352 disc.number = j;
353 if (ioctl(fd, GET_DISK_INFO, &disc))
354 continue;
355 if (disc.major==0 && disc.minor==0)
356 continue;
357 if ((disc.state & 4)==0) continue; /* sync */
358 /* Looks like a good device to try */
16c6fa80 359 dev = map_dev(disc.major, disc.minor, 1);
fe80f49b 360 if (!dev) continue;
16c6fa80 361 dfd = dev_open(dev, O_RDONLY);
fe80f49b 362 if (dfd < 0) continue;
3da92f27
NB
363 if (tst->ss->load_super(tst, dfd,
364 NULL)) {
fe80f49b
NB
365 close(dfd);
366 continue;
367 }
892debc8 368 close(dfd);
fe80f49b
NB
369 break;
370 }
3da92f27 371 if (!tst->sb) {
fe80f49b
NB
372 fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
373 return 1;
374 }
375 /* Possibly this device was recently part of the array
376 * and was temporarily removed, and is now being re-added.
377 * If so, we can simply re-add it.
378 */
3da92f27 379 tst->ss->uuid_from_super(tst, duuid);
7eae7080
NB
380
381 /* re-add doesn't work for version-1 superblocks
382 * before 2.6.18 :-(
383 */
384 if (array.major_version == 1 &&
385 get_linux_version() <= 2006018)
386 ;
3da92f27
NB
387 else if (st->sb) {
388 st->ss->uuid_from_super(st, ouuid);
fe80f49b 389 if (memcmp(duuid, ouuid, sizeof(ouuid))==0) {
4d20d744
NB
390 /* looks close enough for now. Kernel
391 * will worry about whether a bitmap
392 * based reconstruction is possible.
fe80f49b
NB
393 */
394 struct mdinfo mdi;
3da92f27 395 st->ss->getinfo_super(st, &mdi);
fe80f49b
NB
396 disc.major = major(stb.st_rdev);
397 disc.minor = minor(stb.st_rdev);
398 disc.number = mdi.disk.number;
399 disc.raid_disk = mdi.disk.raid_disk;
400 disc.state = mdi.disk.state;
4d20d744
NB
401 if (dv->writemostly)
402 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
fe80f49b
NB
403 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
404 if (verbose >= 0)
405 fprintf(stderr, Name ": re-added %s\n", dv->devname);
308e1801 406 continue;
fe80f49b
NB
407 }
408 /* fall back on normal-add */
409 }
892debc8 410 }
4a39c6f2
NB
411 } else {
412 /* non-persistent. Must ensure that new drive
413 * is at least array.size big.
414 */
415 if (ldsize/512 < array.size) {
416 fprintf(stderr, Name ": %s not large enough to join array\n",
417 dv->devname);
418 return 1;
419 }
892debc8 420 }
7eae7080
NB
421 /* in 2.6.17 and earlier, version-1 superblocks won't
422 * use the number we write, but will choose a free number.
423 * we must choose the same free number, which requires
424 * starting at 'raid_disks' and counting up
425 */
3da92f27 426 for (j = array.raid_disks; j< tst->max_devs; j++) {
aa88f531 427 disc.number = j;
682c7051
NB
428 if (ioctl(fd, GET_DISK_INFO, &disc))
429 break;
430 if (disc.major==0 && disc.minor==0)
431 break;
432 if (disc.state & 8) /* removed */
433 break;
434 }
0df46c2a
NB
435 disc.major = major(stb.st_rdev);
436 disc.minor = minor(stb.st_rdev);
892debc8
NB
437 disc.number =j;
438 disc.state = 0;
fe80f49b
NB
439 if (array.not_persistent==0) {
440 if (dv->writemostly)
441 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
3da92f27
NB
442 tst->ss->add_to_super(tst, &disc);
443 if (tst->ss->write_init_super(tst, &disc,
444 dv->devname))
fe80f49b
NB
445 return 1;
446 } else if (dv->re_add) {
447 /* this had better be raid1.
448 * As we are "--re-add"ing we must find a spare slot
449 * to fill.
450 */
451 char *used = malloc(array.raid_disks);
452 memset(used, 0, array.raid_disks);
3da92f27 453 for (j=0; j< tst->max_devs; j++) {
fe80f49b
NB
454 mdu_disk_info_t disc2;
455 disc2.number = j;
456 if (ioctl(fd, GET_DISK_INFO, &disc2))
457 continue;
458 if (disc2.major==0 && disc2.minor==0)
459 continue;
460 if (disc2.state & 8) /* removed */
461 continue;
462 if (disc2.raid_disk < 0)
463 continue;
464 if (disc2.raid_disk > array.raid_disks)
465 continue;
466 used[disc2.raid_disk] = 1;
467 }
468 for (j=0 ; j<array.raid_disks; j++)
469 if (!used[j]) {
470 disc.raid_disk = j;
471 disc.state |= (1<<MD_DISK_SYNC);
472 break;
473 }
474 }
d8def61c
NB
475 if (dv->writemostly)
476 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
682c7051 477 if (ioctl(fd,ADD_NEW_DISK, &disc)) {
f277ce36
NB
478 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
479 dv->devname, j, strerror(errno));
682c7051
NB
480 return 1;
481 }
dab6685f
NB
482 if (verbose >= 0)
483 fprintf(stderr, Name ": added %s\n", dv->devname);
682c7051
NB
484 break;
485
486 case 'r':
487 /* hot remove */
82b27616 488 /* FIXME check that it is a current member */
cd29a5c8 489 if (ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev)) {
b80da661
NB
490 fprintf(stderr, Name ": hot remove failed "
491 "for %s: %s\n", dnprintable,
492 strerror(errno));
682c7051
NB
493 return 1;
494 }
dab6685f 495 if (verbose >= 0)
b80da661
NB
496 fprintf(stderr, Name ": hot removed %s\n",
497 dnprintable);
682c7051
NB
498 break;
499
500 case 'f': /* set faulty */
501 /* FIXME check current member */
cd29a5c8 502 if (ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev)) {
b83d95f3 503 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
b80da661 504 dnprintable, strerror(errno));
682c7051
NB
505 return 1;
506 }
dab6685f
NB
507 if (verbose >= 0)
508 fprintf(stderr, Name ": set %s faulty in %s\n",
b80da661 509 dnprintable, devname);
682c7051
NB
510 break;
511 }
512 }
513 return 0;
aba69144 514
64c4757e 515}
1f48664b
NB
516
517int autodetect(void)
518{
519 /* Open any md device, and issue the RAID_AUTORUN ioctl */
520 int rv = 1;
521 int fd = dev_open("9:0", O_RDONLY);
522 if (fd >= 0) {
523 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
524 rv = 0;
525 close(fd);
526 }
527 return rv;
528}
435d4ebb 529#endif