]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Manage.c
Support fail/remove using kernel name
[thirdparty/mdadm.git] / Manage.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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@suse.de>
23 */
24
25 #include "mdadm.h"
26 #include "md_u.h"
27 #include "md_p.h"
28 #include <ctype.h>
29
30 #define REGISTER_DEV _IO (MD_MAJOR, 1)
31 #define START_MD _IO (MD_MAJOR, 2)
32 #define STOP_MD _IO (MD_MAJOR, 3)
33
34 int Manage_ro(char *devname, int fd, int readonly)
35 {
36 /* switch to readonly or rw
37 *
38 * requires >= 0.90.0
39 * first check that array is runing
40 * use RESTART_ARRAY_RW or STOP_ARRAY_RO
41 *
42 */
43 mdu_array_info_t array;
44 #ifndef MDASSEMBLE
45 struct mdinfo *mdi;
46 #endif
47
48 if (md_get_version(fd) < 9000) {
49 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
50 return 1;
51 }
52 #ifndef MDASSEMBLE
53 /* If this is an externally-manage array, we need to modify the
54 * metadata_version so that mdmon doesn't undo our change.
55 */
56 mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
57 if (mdi &&
58 mdi->array.major_version == -1 &&
59 mdi->array.level > 0 &&
60 is_subarray(mdi->text_version)) {
61 char vers[64];
62 strcpy(vers, "external:");
63 strcat(vers, mdi->text_version);
64 if (readonly > 0) {
65 int rv;
66 /* We set readonly ourselves. */
67 vers[9] = '-';
68 sysfs_set_str(mdi, NULL, "metadata_version", vers);
69
70 close(fd);
71 rv = sysfs_set_str(mdi, NULL, "array_state", "readonly");
72
73 if (rv < 0) {
74 fprintf(stderr, Name ": failed to set readonly for %s: %s\n",
75 devname, strerror(errno));
76
77 vers[9] = mdi->text_version[0];
78 sysfs_set_str(mdi, NULL, "metadata_version", vers);
79 return 1;
80 }
81 } else {
82 char *cp;
83 /* We cannot set read/write - must signal mdmon */
84 vers[9] = '/';
85 sysfs_set_str(mdi, NULL, "metadata_version", vers);
86
87 cp = strchr(vers+10, '/');
88 if (*cp)
89 *cp = 0;
90 ping_monitor(vers+10);
91 }
92 return 0;
93 }
94 #endif
95 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
96 fprintf(stderr, Name ": %s does not appear to be active.\n",
97 devname);
98 return 1;
99 }
100
101 if (readonly>0) {
102 if (ioctl(fd, STOP_ARRAY_RO, NULL)) {
103 fprintf(stderr, Name ": failed to set readonly for %s: %s\n",
104 devname, strerror(errno));
105 return 1;
106 }
107 } else if (readonly < 0) {
108 if (ioctl(fd, RESTART_ARRAY_RW, NULL)) {
109 fprintf(stderr, Name ": failed to set writable for %s: %s\n",
110 devname, strerror(errno));
111 return 1;
112 }
113 }
114 return 0;
115 }
116
117 #ifndef MDASSEMBLE
118
119 static void remove_devices(int devnum, char *path)
120 {
121 /* Remove all 'standard' devices for 'devnum', including
122 * partitions. Also remove names at 'path' - possibly with
123 * partition suffixes - which link to those names.
124 */
125 char base[40];
126 char *path2;
127 char link[1024];
128 int n;
129 int part;
130 char *be;
131 char *pe;
132
133 if (devnum >= 0)
134 sprintf(base, "/dev/md%d", devnum);
135 else
136 sprintf(base, "/dev/md_d%d", -1-devnum);
137 be = base + strlen(base);
138 if (path) {
139 path2 = malloc(strlen(path)+20);
140 strcpy(path2, path);
141 pe = path2 + strlen(path2);
142 } else
143 path2 = path = NULL;
144
145 for (part = 0; part < 16; part++) {
146 if (part) {
147 sprintf(be, "p%d", part);
148 if (path) {
149 if (isdigit(pe[-1]))
150 sprintf(pe, "p%d", part);
151 else
152 sprintf(pe, "%d", part);
153 }
154 }
155 /* FIXME test if really is md device ?? */
156 unlink(base);
157 if (path) {
158 n = readlink(path2, link, sizeof(link));
159 if (n && strlen(base) == n &&
160 strncmp(link, base, n) == 0)
161 unlink(path2);
162 }
163 }
164 free(path2);
165 }
166
167
168 int Manage_runstop(char *devname, int fd, int runstop, int quiet)
169 {
170 /* Run or stop the array. array must already be configured
171 * required >= 0.90.0
172 * Only print failure messages if quiet == 0;
173 * quiet > 0 means really be quiet
174 * quiet < 0 means we will try again if it fails.
175 */
176 mdu_param_t param; /* unused */
177
178 if (runstop == -1 && md_get_version(fd) < 9000) {
179 if (ioctl(fd, STOP_MD, 0)) {
180 if (quiet == 0) fprintf(stderr,
181 Name ": stopping device %s "
182 "failed: %s\n",
183 devname, strerror(errno));
184 return 1;
185 }
186 }
187
188 if (md_get_version(fd) < 9000) {
189 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
190 return 1;
191 }
192 /*
193 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
194 fprintf(stderr, Name ": %s does not appear to be active.\n",
195 devname);
196 return 1;
197 }
198 */
199 if (runstop>0) {
200 if (ioctl(fd, RUN_ARRAY, &param)) {
201 fprintf(stderr, Name ": failed to run array %s: %s\n",
202 devname, strerror(errno));
203 return 1;
204 }
205 if (quiet <= 0)
206 fprintf(stderr, Name ": started %s\n", devname);
207 } else if (runstop < 0){
208 struct map_ent *map = NULL;
209 struct stat stb;
210 struct mdinfo *mdi;
211 int devnum;
212 /* If this is an mdmon managed array, just write 'inactive'
213 * to the array state and let mdmon clear up.
214 */
215 devnum = fd2devnum(fd);
216 mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
217 if (mdi &&
218 mdi->array.level > 0 &&
219 is_subarray(mdi->text_version)) {
220 /* This is mdmon managed. */
221 close(fd);
222 if (sysfs_set_str(mdi, NULL,
223 "array_state", "inactive") < 0) {
224 if (quiet == 0)
225 fprintf(stderr, Name
226 ": failed to stop array %s: %s\n",
227 devname, strerror(errno));
228 return 1;
229 }
230
231 /* Give monitor a chance to act */
232 ping_monitor(mdi->text_version);
233
234 fd = open(devname, O_RDONLY);
235 } else if (mdi &&
236 mdi->array.major_version == -1 &&
237 mdi->array.minor_version == -2 &&
238 !is_subarray(mdi->text_version)) {
239 struct mdstat_ent *mds, *m;
240 /* container, possibly mdmon-managed.
241 * Make sure mdmon isn't opening it, which
242 * would interfere with the 'stop'
243 */
244 ping_monitor(mdi->sys_name);
245
246 /* now check that there are no existing arrays
247 * which are members of this array
248 */
249 mds = mdstat_read(0, 0);
250 for (m=mds; m; m=m->next)
251 if (m->metadata_version &&
252 strncmp(m->metadata_version, "external:", 9)==0 &&
253 is_subarray(m->metadata_version+9) &&
254 devname2devnum(m->metadata_version+10) == devnum) {
255 if (!quiet)
256 fprintf(stderr, Name
257 ": Cannot stop container %s: "
258 "member %s still active\n",
259 devname, m->dev);
260 free_mdstat(mds);
261 if (mdi)
262 sysfs_free(mdi);
263 return 1;
264 }
265 }
266
267 if (fd >= 0 && ioctl(fd, STOP_ARRAY, NULL)) {
268 if (quiet == 0) {
269 fprintf(stderr, Name
270 ": failed to stop array %s: %s\n",
271 devname, strerror(errno));
272 if (errno == EBUSY)
273 fprintf(stderr, "Perhaps a running "
274 "process, mounted filesystem "
275 "or active volume group?\n");
276 }
277 if (mdi)
278 sysfs_free(mdi);
279 return 1;
280 }
281 /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
282 * was stopped, so We'll do it here just to be sure. Drop any
283 * partitions as well...
284 */
285 if (fd >= 0)
286 ioctl(fd, BLKRRPART, 0);
287 if (mdi)
288 sysfs_uevent(mdi, "change");
289
290
291 if (devnum != NoMdDev &&
292 (stat("/dev/.udev", &stb) != 0 ||
293 check_env("MDADM_NO_UDEV"))) {
294 struct map_ent *mp = map_by_devnum(&map, devnum);
295 remove_devices(devnum, mp ? mp->path : NULL);
296 }
297
298
299 if (quiet <= 0)
300 fprintf(stderr, Name ": stopped %s\n", devname);
301 map_lock(&map);
302 map_remove(&map, devnum);
303 map_unlock(&map);
304 }
305 return 0;
306 }
307
308 int Manage_resize(char *devname, int fd, long long size, int raid_disks)
309 {
310 mdu_array_info_t info;
311 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
312 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
313 devname, strerror(errno));
314 return 1;
315 }
316 if (size >= 0)
317 info.size = size;
318 if (raid_disks > 0)
319 info.raid_disks = raid_disks;
320 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
321 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
322 devname, strerror(errno));
323 return 1;
324 }
325 return 0;
326 }
327
328 int Manage_subdevs(char *devname, int fd,
329 mddev_dev_t devlist, int verbose)
330 {
331 /* do something to each dev.
332 * devmode can be
333 * 'a' - add the device
334 * try HOT_ADD_DISK
335 * If that fails EINVAL, try ADD_NEW_DISK
336 * 'r' - remove the device HOT_REMOVE_DISK
337 * device can be 'faulty' or 'detached' in which case all
338 * matching devices are removed.
339 * 'f' - set the device faulty SET_DISK_FAULTY
340 * device can be 'detached' in which case any device that
341 * is inaccessible will be marked faulty.
342 * For 'f' and 'r', the device can also be a kernel-internal
343 * name such as 'sdb'.
344 */
345 mdu_array_info_t array;
346 mdu_disk_info_t disc;
347 unsigned long long array_size;
348 mddev_dev_t dv, next = NULL;
349 struct stat stb;
350 int j, jnext = 0;
351 int tfd;
352 struct supertype *st, *tst;
353 int duuid[4];
354 int ouuid[4];
355 int lfd = -1;
356 int sysfd = -1;
357
358 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
359 fprintf(stderr, Name ": cannot get array info for %s\n",
360 devname);
361 return 1;
362 }
363
364 /* array.size is only 32 bit and may be truncated.
365 * So read from sysfs if possible, and record number of sectors
366 */
367
368 array_size = get_component_size(fd);
369 if (array_size <= 0)
370 array_size = array.size * 2;
371
372 tst = super_by_fd(fd);
373 if (!tst) {
374 fprintf(stderr, Name ": unsupport array - version %d.%d\n",
375 array.major_version, array.minor_version);
376 return 1;
377 }
378
379 for (dv = devlist, j=0 ; dv; dv = next, j = jnext) {
380 unsigned long long ldsize;
381 char dvname[20];
382 char *dnprintable = dv->devname;
383 int err;
384
385 next = dv->next;
386 jnext = 0;
387
388 if (strcmp(dv->devname, "failed")==0 ||
389 strcmp(dv->devname, "faulty")==0) {
390 if (dv->disposition != 'r') {
391 fprintf(stderr, Name ": %s only meaningful "
392 "with -r, not -%c\n",
393 dv->devname, dv->disposition);
394 return 1;
395 }
396 for (; j < array.raid_disks + array.nr_disks ; j++) {
397 disc.number = j;
398 if (ioctl(fd, GET_DISK_INFO, &disc))
399 continue;
400 if (disc.major == 0 && disc.minor == 0)
401 continue;
402 if ((disc.state & 1) == 0) /* faulty */
403 continue;
404 stb.st_rdev = makedev(disc.major, disc.minor);
405 next = dv;
406 jnext = j+1;
407 sprintf(dvname,"%d:%d", disc.major, disc.minor);
408 dnprintable = dvname;
409 break;
410 }
411 if (jnext == 0)
412 continue;
413 } else if (strcmp(dv->devname, "detached") == 0) {
414 if (dv->disposition != 'r' && dv->disposition != 'f') {
415 fprintf(stderr, Name ": %s only meaningful "
416 "with -r of -f, not -%c\n",
417 dv->devname, dv->disposition);
418 return 1;
419 }
420 for (; j < array.raid_disks + array.nr_disks; j++) {
421 int sfd;
422 disc.number = j;
423 if (ioctl(fd, GET_DISK_INFO, &disc))
424 continue;
425 if (disc.major == 0 && disc.minor == 0)
426 continue;
427 sprintf(dvname,"%d:%d", disc.major, disc.minor);
428 sfd = dev_open(dvname, O_RDONLY);
429 if (sfd >= 0) {
430 close(sfd);
431 continue;
432 }
433 if (dv->disposition == 'f' &&
434 (disc.state & 1) == 1) /* already faulty */
435 continue;
436 if (errno != ENXIO)
437 continue;
438 stb.st_rdev = makedev(disc.major, disc.minor);
439 next = dv;
440 jnext = j+1;
441 dnprintable = dvname;
442 break;
443 }
444 if (jnext == 0)
445 continue;
446 } else if (strchr(dv->devname, '/') == NULL &&
447 strlen(dv->devname) < 50) {
448 /* Assume this is a kernel-internal name like 'sda1' */
449 int found = 0;
450 char dname[55];
451 if (dv->disposition != 'r' && dv->disposition != 'f') {
452 fprintf(stderr, Name ": %s only meaningful "
453 "with -r of -f, not -%c\n",
454 dv->devname, dv->disposition);
455 return 1;
456 }
457
458 sprintf(dname, "dev-%s", dv->devname);
459 sysfd = sysfs_open(fd2devnum(fd), dname, "block/dev");
460 if (sysfd >= 0) {
461 char dn[20];
462 int mj,mn;
463 if (sysfs_fd_get_str(sysfd, dn, 20) > 0 &&
464 sscanf(dn, "%d:%d", &mj,&mn) == 2) {
465 stb.st_rdev = makedev(mj,mn);
466 found = 1;
467 }
468 close(sysfd);
469 sysfd = -1;
470 }
471 if (!found) {
472 sysfd = sysfs_open(fd2devnum(fd), dname, "state");
473 if (sysfd < 0) {
474 fprintf(stderr, Name ": %s does not appear "
475 "to be a component of %s\n",
476 dv->devname, devname);
477 return 1;
478 }
479 }
480 } else {
481 j = 0;
482
483 tfd = dev_open(dv->devname, O_RDONLY);
484 if (tfd < 0 && dv->disposition == 'r' &&
485 lstat(dv->devname, &stb) == 0)
486 /* Be happy, the lstat worked, that is
487 * enough for --remove
488 */
489 ;
490 else {
491 if (tfd < 0 || fstat(tfd, &stb) != 0) {
492 fprintf(stderr, Name ": cannot find %s: %s\n",
493 dv->devname, strerror(errno));
494 if (tfd >= 0)
495 close(tfd);
496 return 1;
497 }
498 close(tfd);
499 }
500 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
501 fprintf(stderr, Name ": %s is not a "
502 "block device.\n",
503 dv->devname);
504 return 1;
505 }
506 }
507 switch(dv->disposition){
508 default:
509 fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
510 dv->devname, dv->disposition);
511 return 1;
512 case 'a':
513 /* add the device */
514 if (tst->subarray[0]) {
515 fprintf(stderr, Name ": Cannot add disks to a"
516 " \'member\' array, perform this"
517 " operation on the parent container\n");
518 return 1;
519 }
520 /* Make sure it isn't in use (in 2.6 or later) */
521 tfd = dev_open(dv->devname, O_RDONLY|O_EXCL|O_DIRECT);
522 if (tfd < 0) {
523 fprintf(stderr, Name ": Cannot open %s: %s\n",
524 dv->devname, strerror(errno));
525 return 1;
526 }
527 remove_partitions(tfd);
528
529 st = dup_super(tst);
530
531 if (array.not_persistent==0)
532 st->ss->load_super(st, tfd, NULL);
533
534 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
535 close(tfd);
536 return 1;
537 }
538 close(tfd);
539
540
541 if (!tst->ss->external &&
542 array.major_version == 0 &&
543 md_get_version(fd)%100 < 2) {
544 if (ioctl(fd, HOT_ADD_DISK,
545 (unsigned long)stb.st_rdev)==0) {
546 if (verbose >= 0)
547 fprintf(stderr, Name ": hot added %s\n",
548 dv->devname);
549 continue;
550 }
551
552 fprintf(stderr, Name ": hot add failed for %s: %s\n",
553 dv->devname, strerror(errno));
554 return 1;
555 }
556
557 if (array.not_persistent == 0 || tst->ss->external) {
558
559 /* need to find a sample superblock to copy, and
560 * a spare slot to use.
561 * For 'external' array (well, container based),
562 * We can just load the metadata for the array.
563 */
564 if (tst->ss->external) {
565 tst->ss->load_super(tst, fd, NULL);
566 } else for (j = 0; j < tst->max_devs; j++) {
567 char *dev;
568 int dfd;
569 disc.number = j;
570 if (ioctl(fd, GET_DISK_INFO, &disc))
571 continue;
572 if (disc.major==0 && disc.minor==0)
573 continue;
574 if ((disc.state & 4)==0) continue; /* sync */
575 /* Looks like a good device to try */
576 dev = map_dev(disc.major, disc.minor, 1);
577 if (!dev) continue;
578 dfd = dev_open(dev, O_RDONLY);
579 if (dfd < 0) continue;
580 if (tst->ss->load_super(tst, dfd,
581 NULL)) {
582 close(dfd);
583 continue;
584 }
585 close(dfd);
586 break;
587 }
588 /* FIXME this is a bad test to be using */
589 if (!tst->sb) {
590 fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
591 return 1;
592 }
593
594 /* Make sure device is large enough */
595 if (tst->ss->avail_size(tst, ldsize/512) <
596 array_size) {
597 fprintf(stderr, Name ": %s not large enough to join array\n",
598 dv->devname);
599 return 1;
600 }
601
602 /* Possibly this device was recently part of the array
603 * and was temporarily removed, and is now being re-added.
604 * If so, we can simply re-add it.
605 */
606 tst->ss->uuid_from_super(tst, duuid);
607
608 /* re-add doesn't work for version-1 superblocks
609 * before 2.6.18 :-(
610 */
611 if (array.major_version == 1 &&
612 get_linux_version() <= 2006018)
613 ;
614 else if (st->sb) {
615 st->ss->uuid_from_super(st, ouuid);
616 if (memcmp(duuid, ouuid, sizeof(ouuid))==0) {
617 /* looks close enough for now. Kernel
618 * will worry about whether a bitmap
619 * based reconstruction is possible.
620 */
621 struct mdinfo mdi;
622 st->ss->getinfo_super(st, &mdi);
623 disc.major = major(stb.st_rdev);
624 disc.minor = minor(stb.st_rdev);
625 disc.number = mdi.disk.number;
626 disc.raid_disk = mdi.disk.raid_disk;
627 disc.state = mdi.disk.state;
628 if (dv->writemostly == 1)
629 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
630 if (dv->writemostly == 2)
631 disc.state &= ~(1 << MD_DISK_WRITEMOSTLY);
632 /* don't even try if disk is marked as faulty */
633 errno = 0;
634 if ((disc.state & 1) == 0 &&
635 ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
636 if (verbose >= 0)
637 fprintf(stderr, Name ": re-added %s\n", dv->devname);
638 continue;
639 }
640 if (errno == ENOMEM || errno == EROFS) {
641 fprintf(stderr, Name ": add new device failed for %s: %s\n",
642 dv->devname, strerror(errno));
643 return 1;
644 }
645 /* fall back on normal-add */
646 }
647 }
648 if (dv->re_add) {
649 fprintf(stderr, Name
650 ": --re-add for %s to %s is not possible\n",
651 dv->devname, devname);
652 return 1;
653 }
654 } else {
655 /* non-persistent. Must ensure that new drive
656 * is at least array.size big.
657 */
658 if (ldsize/512 < array_size) {
659 fprintf(stderr, Name ": %s not large enough to join array\n",
660 dv->devname);
661 return 1;
662 }
663 }
664 /* in 2.6.17 and earlier, version-1 superblocks won't
665 * use the number we write, but will choose a free number.
666 * we must choose the same free number, which requires
667 * starting at 'raid_disks' and counting up
668 */
669 for (j = array.raid_disks; j< tst->max_devs; j++) {
670 disc.number = j;
671 if (ioctl(fd, GET_DISK_INFO, &disc))
672 break;
673 if (disc.major==0 && disc.minor==0)
674 break;
675 if (disc.state & 8) /* removed */
676 break;
677 }
678 disc.major = major(stb.st_rdev);
679 disc.minor = minor(stb.st_rdev);
680 disc.number =j;
681 disc.state = 0;
682 if (array.not_persistent==0 || tst->ss->external) {
683 int dfd;
684 if (dv->writemostly == 1)
685 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
686 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
687 if (tst->ss->add_to_super(tst, &disc, dfd,
688 dv->devname)) {
689 close(dfd);
690 return 1;
691 }
692 /* write_init_super will close 'dfd' */
693 if (tst->ss->external)
694 /* mdmon will write the metadata */
695 close(dfd);
696 else if (tst->ss->write_init_super(tst))
697 return 1;
698 } else if (dv->re_add) {
699 /* this had better be raid1.
700 * As we are "--re-add"ing we must find a spare slot
701 * to fill.
702 */
703 char *used = malloc(array.raid_disks);
704 memset(used, 0, array.raid_disks);
705 for (j=0; j< tst->max_devs; j++) {
706 mdu_disk_info_t disc2;
707 disc2.number = j;
708 if (ioctl(fd, GET_DISK_INFO, &disc2))
709 continue;
710 if (disc2.major==0 && disc2.minor==0)
711 continue;
712 if (disc2.state & 8) /* removed */
713 continue;
714 if (disc2.raid_disk < 0)
715 continue;
716 if (disc2.raid_disk > array.raid_disks)
717 continue;
718 used[disc2.raid_disk] = 1;
719 }
720 for (j=0 ; j<array.raid_disks; j++)
721 if (!used[j]) {
722 disc.raid_disk = j;
723 disc.state |= (1<<MD_DISK_SYNC);
724 break;
725 }
726 free(used);
727 }
728 if (dv->writemostly == 1)
729 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
730 if (tst->ss->external) {
731 /* add a disk to an external metadata container
732 * only if mdmon is around to see it
733 */
734 struct mdinfo new_mdi;
735 struct mdinfo *sra;
736 int container_fd;
737 int devnum = fd2devnum(fd);
738
739 container_fd = open_dev_excl(devnum);
740 if (container_fd < 0) {
741 fprintf(stderr, Name ": add failed for %s:"
742 " could not get exclusive access to container\n",
743 dv->devname);
744 return 1;
745 }
746
747 if (!mdmon_running(devnum)) {
748 fprintf(stderr, Name ": add failed for %s: mdmon not running\n",
749 dv->devname);
750 close(container_fd);
751 return 1;
752 }
753
754 sra = sysfs_read(container_fd, -1, 0);
755 if (!sra) {
756 fprintf(stderr, Name ": add failed for %s: sysfs_read failed\n",
757 dv->devname);
758 close(container_fd);
759 return 1;
760 }
761 sra->array.level = LEVEL_CONTAINER;
762 /* Need to set data_offset and component_size */
763 tst->ss->getinfo_super(tst, &new_mdi);
764 new_mdi.disk.major = disc.major;
765 new_mdi.disk.minor = disc.minor;
766 new_mdi.recovery_start = 0;
767 if (sysfs_add_disk(sra, &new_mdi, 0) != 0) {
768 fprintf(stderr, Name ": add new device to external metadata"
769 " failed for %s\n", dv->devname);
770 close(container_fd);
771 return 1;
772 }
773 ping_monitor(devnum2devname(devnum));
774 sysfs_free(sra);
775 close(container_fd);
776 } else if (ioctl(fd, ADD_NEW_DISK, &disc)) {
777 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
778 dv->devname, j, strerror(errno));
779 return 1;
780 }
781 if (verbose >= 0)
782 fprintf(stderr, Name ": added %s\n", dv->devname);
783 break;
784
785 case 'r':
786 /* hot remove */
787 if (tst->subarray[0]) {
788 fprintf(stderr, Name ": Cannot remove disks from a"
789 " \'member\' array, perform this"
790 " operation on the parent container\n");
791 if (sysfd >= 0)
792 close(sysfd);
793 return 1;
794 }
795 if (tst->ss->external) {
796 /* To remove a device from a container, we must
797 * check that it isn't in use in an array.
798 * This involves looking in the 'holders'
799 * directory - there must be just one entry,
800 * the container.
801 * To ensure that it doesn't get used as a
802 * hold spare while we are checking, we
803 * get an O_EXCL open on the container
804 */
805 int dnum = fd2devnum(fd);
806 lfd = open_dev_excl(dnum);
807 if (lfd < 0) {
808 fprintf(stderr, Name
809 ": Cannot get exclusive access "
810 " to container - odd\n");
811 if (sysfd >= 0)
812 close(sysfd);
813 return 1;
814 }
815 /* in the detached case it is not possible to
816 * check if we are the unique holder, so just
817 * rely on the 'detached' checks
818 */
819 if (strcmp(dv->devname, "detached") == 0 ||
820 sysfd >= 0 ||
821 sysfs_unique_holder(dnum, stb.st_rdev))
822 /* pass */;
823 else {
824 fprintf(stderr, Name
825 ": %s is %s, cannot remove.\n",
826 dnprintable,
827 errno == EEXIST ? "still in use":
828 "not a member");
829 close(lfd);
830 return 1;
831 }
832 }
833 /* FIXME check that it is a current member */
834 if (sysfd >= 0) {
835 /* device has been removed and we don't know
836 * the major:minor number
837 */
838 int n = write(sysfd, "remove", 6);
839 if (n != 6)
840 err = -1;
841 else
842 err = 0;
843 close(sysfd);
844 sysfd = -1;
845 } else {
846 err = ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev);
847 if (err && errno == ENODEV) {
848 /* Old kernels rejected this if no personality
849 * registered */
850 struct mdinfo *sra = sysfs_read(fd, 0, GET_DEVS);
851 struct mdinfo *dv = NULL;
852 if (sra)
853 dv = sra->devs;
854 for ( ; dv ; dv=dv->next)
855 if (dv->disk.major == major(stb.st_rdev) &&
856 dv->disk.minor == minor(stb.st_rdev))
857 break;
858 if (dv)
859 err = sysfs_set_str(sra, dv,
860 "state", "remove");
861 else
862 err = -1;
863 if (sra)
864 sysfs_free(sra);
865 }
866 }
867 if (err) {
868 fprintf(stderr, Name ": hot remove failed "
869 "for %s: %s\n", dnprintable,
870 strerror(errno));
871 if (lfd >= 0)
872 close(lfd);
873 return 1;
874 }
875 if (tst->ss->external) {
876 /*
877 * Before dropping our exclusive open we make an
878 * attempt at preventing mdmon from seeing an
879 * 'add' event before reconciling this 'remove'
880 * event.
881 */
882 char *name = devnum2devname(fd2devnum(fd));
883
884 if (!name) {
885 fprintf(stderr, Name ": unable to get container name\n");
886 return 1;
887 }
888
889 ping_manager(name);
890 free(name);
891 }
892 if (lfd >= 0)
893 close(lfd);
894 if (verbose >= 0)
895 fprintf(stderr, Name ": hot removed %s\n",
896 dnprintable);
897 break;
898
899 case 'f': /* set faulty */
900 /* FIXME check current member */
901 if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) ||
902 (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY,
903 (unsigned long) stb.st_rdev))) {
904 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
905 dnprintable, strerror(errno));
906 if (sysfd >= 0)
907 close(sysfd);
908 return 1;
909 }
910 if (sysfd >= 0)
911 close(sysfd);
912 sysfd = -1;
913 if (verbose >= 0)
914 fprintf(stderr, Name ": set %s faulty in %s\n",
915 dnprintable, devname);
916 break;
917 }
918 }
919 return 0;
920
921 }
922
923 int autodetect(void)
924 {
925 /* Open any md device, and issue the RAID_AUTORUN ioctl */
926 int rv = 1;
927 int fd = dev_open("9:0", O_RDONLY);
928 if (fd >= 0) {
929 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
930 rv = 0;
931 close(fd);
932 }
933 return rv;
934 }
935 #endif