Factor out test for subarray version string.
[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
4b9d39fc
N
81 * Only print failure messages if quiet == 0;
82 * quiet > 0 means really be quiet
83 * quiet < 0 means we will try again if it fails.
682c7051 84 */
682c7051 85 mdu_param_t param; /* unused */
82b27616
NB
86
87 if (runstop == -1 && md_get_version(fd) < 9000) {
88 if (ioctl(fd, STOP_MD, 0)) {
4b9d39fc
N
89 if (quiet == 0) fprintf(stderr,
90 Name ": stopping device %s "
91 "failed: %s\n",
92 devname, strerror(errno));
82b27616
NB
93 return 1;
94 }
95 }
aba69144 96
682c7051
NB
97 if (md_get_version(fd) < 9000) {
98 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
99 return 1;
100 }
82b27616 101 /*
682c7051
NB
102 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
103 fprintf(stderr, Name ": %s does not appear to be active.\n",
104 devname);
105 return 1;
106 }
82b27616 107 */
682c7051
NB
108 if (runstop>0) {
109 if (ioctl(fd, RUN_ARRAY, &param)) {
110 fprintf(stderr, Name ": failed to run array %s: %s\n",
111 devname, strerror(errno));
112 return 1;
113 }
8382f19b
NB
114 if (quiet <= 0)
115 fprintf(stderr, Name ": started %s\n", devname);
682c7051 116 } else if (runstop < 0){
8382f19b
NB
117 struct map_ent *map = NULL;
118 struct stat stb;
daf7a3ce
NB
119 struct mdinfo *mdi;
120 /* If this is an mdmon managed array, just write 'inactive'
121 * to the array state and let mdmon clear up.
122 */
123 mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
124 if (mdi &&
125 mdi->array.level > 0 &&
3c558363 126 is_subarray(mdi->text_version)) {
daf7a3ce
NB
127 char *cp;
128
129 /* This is mdmon managed. */
130 close(fd);
131 if (sysfs_set_str(mdi, NULL,
132 "array_state", "inactive") < 0) {
4b9d39fc 133 if (quiet == 0)
daf7a3ce 134 fprintf(stderr, Name
4b9d39fc 135 ": failed to stop array %s: %s\n",
daf7a3ce
NB
136 devname, strerror(errno));
137 return 1;
138 }
139
140 /* Give monitor a chance to act */
141 cp = strchr(mdi->text_version+1, '/');
142 if (*cp)
143 *cp = 0;
144 ping_monitor(mdi->text_version+1);
145
146 fd = open(devname, O_RDONLY);
ada6c239
N
147 } else if (mdi &&
148 mdi->array.major_version == -1 &&
149 mdi->array.minor_version == -2 &&
3c558363 150 !is_subarray(mdi->text_version)) {
ada6c239
N
151 /* container, possibly mdmon-managed.
152 * Make sure mdmon isn't opening it, which
153 * would interfere with the 'stop'
154 */
155 ping_monitor(mdi->sys_name);
daf7a3ce
NB
156 }
157 if (mdi)
158 sysfs_free(mdi);
159
160 if (fd >= 0 && ioctl(fd, STOP_ARRAY, NULL)) {
37ea3936 161 if (quiet == 0) {
daf7a3ce 162 fprintf(stderr, Name
4b9d39fc 163 ": failed to stop array %s: %s\n",
91f068bf 164 devname, strerror(errno));
d927f3c4
NB
165 if (errno == EBUSY)
166 fprintf(stderr, "Perhaps a running "
167 "process, mounted filesystem "
168 "or active volume group?\n");
169 }
682c7051
NB
170 return 1;
171 }
daf7a3ce 172
ab56093f
NB
173 if (quiet <= 0)
174 fprintf(stderr, Name ": stopped %s\n", devname);
daf7a3ce 175 if (fd >= 0 && fstat(fd, &stb) == 0) {
8382f19b
NB
176 int devnum;
177 if (major(stb.st_rdev) == MD_MAJOR)
178 devnum = minor(stb.st_rdev);
179 else
180 devnum = -1-(minor(stb.st_rdev)>>6);
181 map_delete(&map, devnum);
182 map_write(map);
183 map_free(map);
184 }
682c7051
NB
185 }
186 return 0;
64c4757e
NB
187}
188
dd0781e5
NB
189int Manage_resize(char *devname, int fd, long long size, int raid_disks)
190{
191 mdu_array_info_t info;
192 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
193 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
194 devname, strerror(errno));
195 return 1;
196 }
197 if (size >= 0)
198 info.size = size;
199 if (raid_disks > 0)
200 info.raid_disks = raid_disks;
201 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
202 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
203 devname, strerror(errno));
204 return 1;
205 }
206 return 0;
207}
208
b5e64645
NB
209int Manage_reconfig(char *devname, int fd, int layout)
210{
211 mdu_array_info_t info;
212 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
213 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
214 devname, strerror(errno));
215 return 1;
216 }
217 info.layout = layout;
218 printf("layout set to %d\n", info.layout);
219 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
220 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
221 devname, strerror(errno));
222 return 1;
223 }
224 return 0;
225}
dd0781e5 226
64c4757e 227int Manage_subdevs(char *devname, int fd,
dab6685f 228 mddev_dev_t devlist, int verbose)
cd29a5c8 229{
682c7051
NB
230 /* do something to each dev.
231 * devmode can be
232 * 'a' - add the device
233 * try HOT_ADD_DISK
234 * If that fails EINVAL, try ADD_NEW_DISK
235 * 'r' - remove the device HOT_REMOVE_DISK
b80da661
NB
236 * device can be 'faulty' or 'detached' in which case all
237 * matching devices are removed.
682c7051 238 * 'f' - set the device faulty SET_DISK_FAULTY
b80da661
NB
239 * device can be 'detached' in which case any device that
240 * is inaccessible will be marked faulty.
682c7051
NB
241 */
242 mdu_array_info_t array;
243 mdu_disk_info_t disc;
7a3be72f 244 unsigned long long array_size;
b80da661 245 mddev_dev_t dv, next = NULL;
682c7051 246 struct stat stb;
b80da661 247 int j, jnext = 0;
0fbf459d 248 int tfd;
3da92f27 249 struct supertype *st, *tst;
fe80f49b
NB
250 int duuid[4];
251 int ouuid[4];
f94d52f4 252 int lfd = -1;
682c7051
NB
253
254 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
255 fprintf(stderr, Name ": cannot get array info for %s\n",
256 devname);
257 return 1;
258 }
3da92f27 259
7a3be72f
NB
260 /* array.size is only 32 bit and may be truncated.
261 * So read from sysfs if possible, and record number of sectors
262 */
263
264 array_size = get_component_size(fd);
265 if (array_size <= 0)
266 array_size = array.size * 2;
267
1686dc25 268 tst = super_by_fd(fd);
3da92f27
NB
269 if (!tst) {
270 fprintf(stderr, Name ": unsupport array - version %d.%d\n",
271 array.major_version, array.minor_version);
272 return 1;
273 }
274
b80da661 275 for (dv = devlist, j=0 ; dv; dv = next, j = jnext) {
4a39c6f2 276 unsigned long long ldsize;
b80da661
NB
277 char dvname[20];
278 char *dnprintable = dv->devname;
f94d52f4 279 int err;
4a39c6f2 280
b80da661
NB
281 next = dv->next;
282 jnext = 0;
283
284 if (strcmp(dv->devname, "failed")==0 ||
285 strcmp(dv->devname, "faulty")==0) {
286 if (dv->disposition != 'r') {
287 fprintf(stderr, Name ": %s only meaningful "
288 "with -r, not -%c\n",
289 dv->devname, dv->disposition);
290 return 1;
291 }
292 for (; j < array.raid_disks + array.nr_disks ; j++) {
293 disc.number = j;
294 if (ioctl(fd, GET_DISK_INFO, &disc))
295 continue;
296 if (disc.major == 0 && disc.minor == 0)
297 continue;
298 if ((disc.state & 1) == 0) /* faulty */
299 continue;
300 stb.st_rdev = makedev(disc.major, disc.minor);
301 next = dv;
302 jnext = j+1;
303 sprintf(dvname,"%d:%d", disc.major, disc.minor);
304 dnprintable = dvname;
305 break;
306 }
307 if (jnext == 0)
308 continue;
309 } else if (strcmp(dv->devname, "detached") == 0) {
310 if (dv->disposition != 'r' && dv->disposition != 'f') {
311 fprintf(stderr, Name ": %s only meaningful "
312 "with -r of -f, not -%c\n",
313 dv->devname, dv->disposition);
314 return 1;
315 }
316 for (; j < array.raid_disks + array.nr_disks; j++) {
317 int sfd;
318 disc.number = j;
319 if (ioctl(fd, GET_DISK_INFO, &disc))
320 continue;
321 if (disc.major == 0 && disc.minor == 0)
322 continue;
323 sprintf(dvname,"%d:%d", disc.major, disc.minor);
324 sfd = dev_open(dvname, O_RDONLY);
325 if (sfd >= 0) {
326 close(sfd);
327 continue;
328 }
329 if (dv->disposition == 'f' &&
330 (disc.state & 1) == 1) /* already faulty */
331 continue;
332 if (errno != ENXIO)
333 continue;
334 stb.st_rdev = makedev(disc.major, disc.minor);
335 next = dv;
336 jnext = j+1;
337 dnprintable = dvname;
338 break;
339 }
340 if (jnext == 0)
341 continue;
342 } else {
343 j = 0;
344
345 if (stat(dv->devname, &stb)) {
346 fprintf(stderr, Name ": cannot find %s: %s\n",
347 dv->devname, strerror(errno));
348 return 1;
349 }
350 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
351 fprintf(stderr, Name ": %s is not a "
352 "block device.\n",
353 dv->devname);
354 return 1;
355 }
682c7051 356 }
cd29a5c8 357 switch(dv->disposition){
682c7051 358 default:
c913b90e
NB
359 fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
360 dv->devname, dv->disposition);
682c7051
NB
361 return 1;
362 case 'a':
4a39c6f2 363 /* add the device */
f7e7067b 364 if (tst->subarray[0]) {
f7dd881f
DW
365 fprintf(stderr, Name ": Cannot add disks to a"
366 " \'member\' array, perform this"
367 " operation on the parent container\n");
368 return 1;
f94d52f4 369 }
f277ce36 370 /* Make sure it isn't in use (in 2.6 or later) */
6416d527 371 tfd = open(dv->devname, O_RDONLY|O_EXCL|O_DIRECT);
0fbf459d 372 if (tfd < 0) {
d7eaf49f
NB
373 fprintf(stderr, Name ": Cannot open %s: %s\n",
374 dv->devname, strerror(errno));
375 return 1;
376 }
25037aa8 377 remove_partitions(tfd);
3da92f27 378
1686dc25 379 st = dup_super(tst);
3da92f27 380
fe80f49b 381 if (array.not_persistent==0)
3da92f27
NB
382 st->ss->load_super(st, tfd, NULL);
383
beae1dfe 384 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
4a39c6f2
NB
385 close(tfd);
386 return 1;
4a39c6f2 387 }
0fbf459d 388 close(tfd);
fe80f49b 389
f94d52f4
NB
390
391 if (!tst->ss->external &&
392 array.major_version == 0 &&
60d9a174 393 md_get_version(fd)%100 < 2) {
892debc8
NB
394 if (ioctl(fd, HOT_ADD_DISK,
395 (unsigned long)stb.st_rdev)==0) {
dab6685f
NB
396 if (verbose >= 0)
397 fprintf(stderr, Name ": hot added %s\n",
398 dv->devname);
892debc8
NB
399 continue;
400 }
401
cd29a5c8 402 fprintf(stderr, Name ": hot add failed for %s: %s\n",
892debc8 403 dv->devname, strerror(errno));
cd29a5c8
NB
404 return 1;
405 }
892debc8 406
43dad3d6 407 if (array.not_persistent == 0 || tst->ss->external) {
4a39c6f2 408 /* Make sure device is large enough */
3da92f27 409 if (tst->ss->avail_size(tst, ldsize/512) <
7a3be72f 410 array_size) {
4a39c6f2
NB
411 fprintf(stderr, Name ": %s not large enough to join array\n",
412 dv->devname);
413 return 1;
414 }
415
fe80f49b 416 /* need to find a sample superblock to copy, and
43dad3d6
DW
417 * a spare slot to use.
418 * For 'external' array (well, container based),
419 * We can just load the metadata for the array.
fe80f49b 420 */
43dad3d6
DW
421 if (tst->ss->external) {
422 tst->ss->load_super(tst, fd, NULL);
423 } else for (j = 0; j < tst->max_devs; j++) {
fe80f49b
NB
424 char *dev;
425 int dfd;
426 disc.number = j;
427 if (ioctl(fd, GET_DISK_INFO, &disc))
428 continue;
429 if (disc.major==0 && disc.minor==0)
430 continue;
431 if ((disc.state & 4)==0) continue; /* sync */
432 /* Looks like a good device to try */
16c6fa80 433 dev = map_dev(disc.major, disc.minor, 1);
fe80f49b 434 if (!dev) continue;
16c6fa80 435 dfd = dev_open(dev, O_RDONLY);
fe80f49b 436 if (dfd < 0) continue;
3da92f27
NB
437 if (tst->ss->load_super(tst, dfd,
438 NULL)) {
fe80f49b
NB
439 close(dfd);
440 continue;
441 }
892debc8 442 close(dfd);
fe80f49b
NB
443 break;
444 }
43dad3d6 445 /* FIXME this is a bad test to be using */
3da92f27 446 if (!tst->sb) {
fe80f49b
NB
447 fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
448 return 1;
449 }
450 /* Possibly this device was recently part of the array
451 * and was temporarily removed, and is now being re-added.
452 * If so, we can simply re-add it.
453 */
3da92f27 454 tst->ss->uuid_from_super(tst, duuid);
7eae7080
NB
455
456 /* re-add doesn't work for version-1 superblocks
457 * before 2.6.18 :-(
458 */
459 if (array.major_version == 1 &&
460 get_linux_version() <= 2006018)
461 ;
3da92f27
NB
462 else if (st->sb) {
463 st->ss->uuid_from_super(st, ouuid);
fe80f49b 464 if (memcmp(duuid, ouuid, sizeof(ouuid))==0) {
4d20d744
NB
465 /* looks close enough for now. Kernel
466 * will worry about whether a bitmap
467 * based reconstruction is possible.
fe80f49b
NB
468 */
469 struct mdinfo mdi;
3da92f27 470 st->ss->getinfo_super(st, &mdi);
fe80f49b
NB
471 disc.major = major(stb.st_rdev);
472 disc.minor = minor(stb.st_rdev);
473 disc.number = mdi.disk.number;
474 disc.raid_disk = mdi.disk.raid_disk;
475 disc.state = mdi.disk.state;
4d20d744
NB
476 if (dv->writemostly)
477 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
fe80f49b
NB
478 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
479 if (verbose >= 0)
480 fprintf(stderr, Name ": re-added %s\n", dv->devname);
308e1801 481 continue;
fe80f49b
NB
482 }
483 /* fall back on normal-add */
484 }
892debc8 485 }
4a39c6f2
NB
486 } else {
487 /* non-persistent. Must ensure that new drive
488 * is at least array.size big.
489 */
7a3be72f 490 if (ldsize/512 < array_size) {
4a39c6f2
NB
491 fprintf(stderr, Name ": %s not large enough to join array\n",
492 dv->devname);
493 return 1;
494 }
892debc8 495 }
7eae7080
NB
496 /* in 2.6.17 and earlier, version-1 superblocks won't
497 * use the number we write, but will choose a free number.
498 * we must choose the same free number, which requires
499 * starting at 'raid_disks' and counting up
500 */
3da92f27 501 for (j = array.raid_disks; j< tst->max_devs; j++) {
aa88f531 502 disc.number = j;
682c7051
NB
503 if (ioctl(fd, GET_DISK_INFO, &disc))
504 break;
505 if (disc.major==0 && disc.minor==0)
506 break;
507 if (disc.state & 8) /* removed */
508 break;
509 }
0df46c2a
NB
510 disc.major = major(stb.st_rdev);
511 disc.minor = minor(stb.st_rdev);
892debc8
NB
512 disc.number =j;
513 disc.state = 0;
43dad3d6 514 if (array.not_persistent==0 || tst->ss->external) {
111d01fc 515 int dfd;
fe80f49b
NB
516 if (dv->writemostly)
517 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
6416d527 518 dfd = open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
111d01fc
NB
519 tst->ss->add_to_super(tst, &disc, dfd,
520 dv->devname);
521 /* write_init_super will close 'dfd' */
43dad3d6
DW
522 if (tst->ss->external)
523 /* mdmon will write the metadata */
524 close(dfd);
525 else if (tst->ss->write_init_super(tst))
fe80f49b
NB
526 return 1;
527 } else if (dv->re_add) {
528 /* this had better be raid1.
529 * As we are "--re-add"ing we must find a spare slot
530 * to fill.
531 */
532 char *used = malloc(array.raid_disks);
533 memset(used, 0, array.raid_disks);
3da92f27 534 for (j=0; j< tst->max_devs; j++) {
fe80f49b
NB
535 mdu_disk_info_t disc2;
536 disc2.number = j;
537 if (ioctl(fd, GET_DISK_INFO, &disc2))
538 continue;
539 if (disc2.major==0 && disc2.minor==0)
540 continue;
541 if (disc2.state & 8) /* removed */
542 continue;
543 if (disc2.raid_disk < 0)
544 continue;
545 if (disc2.raid_disk > array.raid_disks)
546 continue;
547 used[disc2.raid_disk] = 1;
548 }
549 for (j=0 ; j<array.raid_disks; j++)
550 if (!used[j]) {
551 disc.raid_disk = j;
552 disc.state |= (1<<MD_DISK_SYNC);
553 break;
554 }
555 }
d8def61c
NB
556 if (dv->writemostly)
557 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
43dad3d6
DW
558 if (tst->ss->external) {
559 /* add a disk to an external metadata container
560 * only if mdmon is around to see it
561 */
562 struct mdinfo new_mdi;
563 struct mdinfo *sra;
564 int container_fd;
565 int devnum = fd2devnum(fd);
566
567 container_fd = open_dev_excl(devnum);
568 if (container_fd < 0) {
569 fprintf(stderr, Name ": add failed for %s:"
570 " could not get exclusive access to container\n",
571 dv->devname);
572 return 1;
573 }
574
575 if (!mdmon_running(devnum)) {
576 fprintf(stderr, Name ": add failed for %s: mdmon not running\n",
577 dv->devname);
578 close(container_fd);
579 return 1;
580 }
581
582 sra = sysfs_read(container_fd, -1, 0);
583 if (!sra) {
584 fprintf(stderr, Name ": add failed for %s: sysfs_read failed\n",
585 dv->devname);
586 close(container_fd);
587 return 1;
588 }
589 sra->array.level = LEVEL_CONTAINER;
590 /* Need to set data_offset and component_size */
591 tst->ss->getinfo_super(tst, &new_mdi);
592 new_mdi.disk.major = disc.major;
593 new_mdi.disk.minor = disc.minor;
594 if (sysfs_add_disk(sra, &new_mdi) != 0) {
595 fprintf(stderr, Name ": add new device to external metadata"
596 " failed for %s\n", dv->devname);
597 close(container_fd);
598 return 1;
599 }
600 ping_monitor(devnum2devname(devnum));
601 sysfs_free(sra);
602 close(container_fd);
603 } else if (ioctl(fd, ADD_NEW_DISK, &disc)) {
f277ce36
NB
604 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
605 dv->devname, j, strerror(errno));
682c7051
NB
606 return 1;
607 }
dab6685f
NB
608 if (verbose >= 0)
609 fprintf(stderr, Name ": added %s\n", dv->devname);
682c7051
NB
610 break;
611
612 case 'r':
613 /* hot remove */
f7e7067b 614 if (tst->subarray[0]) {
f7dd881f
DW
615 fprintf(stderr, Name ": Cannot remove disks from a"
616 " \'member\' array, perform this"
617 " operation on the parent container\n");
618 return 1;
f94d52f4
NB
619 }
620 if (tst->ss->external) {
621 /* To remove a device from a container, we must
622 * check that it isn't in use in an array.
623 * This involves looking in the 'holders'
624 * directory - there must be just one entry,
625 * the container.
626 * To ensure that it doesn't get used as a
627 * hold spare while we are checking, we
628 * get an O_EXCL open on the container
629 */
630 int dnum = fd2devnum(fd);
631 lfd = open_dev_excl(dnum);
632 if (lfd < 0) {
633 fprintf(stderr, Name
634 ": Cannot get exclusive access "
635 " to container - odd\n");
636 return 1;
637 }
638 if (!sysfs_unique_holder(dnum, stb.st_rdev)) {
639 fprintf(stderr, Name
640 ": %s is %s, cannot remove.\n",
641 dnprintable,
642 errno == EEXIST ? "still in use":
643 "not a member");
644 close(lfd);
645 return 1;
646 }
647 }
82b27616 648 /* FIXME check that it is a current member */
f94d52f4
NB
649 err = ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev);
650 if (err && errno == ENODEV) {
651 /* Old kernels rejected this if no personality
652 * registered */
653 struct mdinfo *sra = sysfs_read(fd, 0, GET_DEVS);
654 struct mdinfo *dv = NULL;
655 if (sra)
656 dv = sra->devs;
657 for ( ; dv ; dv=dv->next)
658 if (dv->disk.major == major(stb.st_rdev) &&
659 dv->disk.minor == minor(stb.st_rdev))
660 break;
661 if (dv)
662 err = sysfs_set_str(sra, dv,
663 "state", "remove");
664 else
665 err = -1;
666 if (sra)
667 sysfs_free(sra);
668 }
669 if (err) {
b80da661
NB
670 fprintf(stderr, Name ": hot remove failed "
671 "for %s: %s\n", dnprintable,
672 strerror(errno));
f94d52f4
NB
673 if (lfd >= 0)
674 close(lfd);
682c7051
NB
675 return 1;
676 }
f94d52f4 677 close(lfd);
dab6685f 678 if (verbose >= 0)
b80da661
NB
679 fprintf(stderr, Name ": hot removed %s\n",
680 dnprintable);
682c7051
NB
681 break;
682
683 case 'f': /* set faulty */
684 /* FIXME check current member */
cd29a5c8 685 if (ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev)) {
b83d95f3 686 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
b80da661 687 dnprintable, strerror(errno));
682c7051
NB
688 return 1;
689 }
dab6685f
NB
690 if (verbose >= 0)
691 fprintf(stderr, Name ": set %s faulty in %s\n",
b80da661 692 dnprintable, devname);
682c7051
NB
693 break;
694 }
695 }
696 return 0;
aba69144 697
64c4757e 698}
1f48664b
NB
699
700int autodetect(void)
701{
702 /* Open any md device, and issue the RAID_AUTORUN ioctl */
703 int rv = 1;
704 int fd = dev_open("9:0", O_RDONLY);
705 if (fd >= 0) {
706 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
707 rv = 0;
708 close(fd);
709 }
710 return rv;
711}
435d4ebb 712#endif