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