]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Manage.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2012 Neil Brown <neilb@suse.de>
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.
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.
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
22 * Email: <neilb@suse.de>
30 #define REGISTER_DEV _IO (MD_MAJOR, 1)
31 #define START_MD _IO (MD_MAJOR, 2)
32 #define STOP_MD _IO (MD_MAJOR, 3)
34 int Manage_ro(char *devname
, int fd
, int readonly
)
36 /* switch to readonly or rw
39 * first check that array is runing
40 * use RESTART_ARRAY_RW or STOP_ARRAY_RO
43 mdu_array_info_t array
;
49 if (md_get_version(fd
) < 9000) {
50 pr_err("need md driver version 0.90.0 or later\n");
54 /* If this is an externally-managed array, we need to modify the
55 * metadata_version so that mdmon doesn't undo our change.
57 mdi
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
);
59 mdi
->array
.major_version
== -1 &&
60 is_subarray(mdi
->text_version
)) {
62 strcpy(vers
, "external:");
63 strcat(vers
, mdi
->text_version
);
66 /* We set readonly ourselves. */
68 sysfs_set_str(mdi
, NULL
, "metadata_version", vers
);
71 rv
= sysfs_set_str(mdi
, NULL
, "array_state", "readonly");
74 pr_err("failed to set readonly for %s: %s\n",
75 devname
, strerror(errno
));
77 vers
[9] = mdi
->text_version
[0];
78 sysfs_set_str(mdi
, NULL
, "metadata_version", vers
);
84 /* We cannot set read/write - must signal mdmon */
86 sysfs_set_str(mdi
, NULL
, "metadata_version", vers
);
88 cp
= strchr(vers
+10, '/');
91 ping_monitor(vers
+10);
92 if (mdi
->array
.level
<= 0)
93 sysfs_set_str(mdi
, NULL
, "array_state", "active");
98 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)) {
99 pr_err("%s does not appear to be active.\n",
106 if (ioctl(fd
, STOP_ARRAY_RO
, NULL
)) {
107 pr_err("failed to set readonly for %s: %s\n",
108 devname
, strerror(errno
));
112 } else if (readonly
< 0) {
113 if (ioctl(fd
, RESTART_ARRAY_RW
, NULL
)) {
114 pr_err("failed to set writable for %s: %s\n",
115 devname
, strerror(errno
));
130 static void remove_devices(char *devnm
, char *path
)
133 * Remove names at 'path' - possibly with
134 * partition suffixes - which link to the 'standard'
135 * name for devnm. These were probably created
136 * by mdadm when the array was assembled.
149 sprintf(base
, "/dev/%s", devnm
);
150 be
= base
+ strlen(base
);
152 path2
= xmalloc(strlen(path
)+20);
154 pe
= path2
+ strlen(path2
);
156 for (part
= 0; part
< 16; part
++) {
158 sprintf(be
, "p%d", part
);
161 sprintf(pe
, "p%d", part
);
163 sprintf(pe
, "%d", part
);
165 n
= readlink(path2
, link
, sizeof(link
));
166 if (n
> 0 && (int)strlen(base
) == n
&&
167 strncmp(link
, base
, n
) == 0)
173 int Manage_run(char *devname
, int fd
, int verbose
)
175 /* Run the array. Array must already be configured
178 mdu_param_t param
; /* unused */
181 if (md_get_version(fd
) < 9000) {
182 pr_err("need md driver version 0.90.0 or later\n");
186 if (ioctl(fd
, RUN_ARRAY
, ¶m
)) {
188 pr_err("failed to run array %s: %s\n",
189 devname
, strerror(errno
));
193 pr_err("started %s\n", devname
);
197 int Manage_stop(char *devname
, int fd
, int verbose
, int will_retry
)
199 /* Stop the array. Array must already be configured
200 * 'will_retry' means that error messages are not wanted.
203 struct map_ent
*map
= NULL
;
210 if (will_retry
&& verbose
== 0)
213 if (md_get_version(fd
) < 9000) {
214 if (ioctl(fd
, STOP_MD
, 0) == 0)
216 pr_err("stopping device %s "
218 devname
, strerror(errno
));
222 /* If this is an mdmon managed array, just write 'inactive'
223 * to the array state and let mdmon clear up.
225 strcpy(devnm
, fd2devnm(fd
));
226 /* Get EXCL access first. If this fails, then attempting
227 * to stop is probably a bad idea.
229 mdi
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
);
230 if (mdi
&& is_subarray(mdi
->text_version
)) {
232 strncpy(container
, mdi
->text_version
+1, sizeof(container
));
233 container
[sizeof(container
)-1] = 0;
234 sl
= strchr(container
, '/');
241 while (((fd
= ((devnm
[0] == '/')
242 ?open(devname
, O_RDONLY
|O_EXCL
)
243 :open_dev_flags(devnm
, O_RDONLY
|O_EXCL
))) < 0
244 || strcmp(fd2devnm(fd
), devnm
) != 0)
246 && mdmon_running(container
)
250 flush_mdmon(container
);
253 if (fd
< 0 || strcmp(fd2devnm(fd
), devnm
) != 0) {
257 pr_err("Cannot get exclusive access to %s:"
259 "process, mounted filesystem "
260 "or active volume group?\n",
265 mdi
->array
.level
> 0 &&
266 is_subarray(mdi
->text_version
)) {
268 /* This is mdmon managed. */
271 /* As we have an O_EXCL open, any use of the device
272 * which blocks STOP_ARRAY is probably a transient use,
273 * so it is reasonable to retry for a while - 5 seconds.
277 (err
= sysfs_set_str(mdi
, NULL
,
286 pr_err("failed to stop array %s: %s\n",
287 devname
, strerror(errno
));
292 /* Give monitor a chance to act */
293 ping_monitor(mdi
->text_version
);
295 fd
= open_dev_excl(devnm
);
298 pr_err("failed to completely stop %s"
299 ": Device is busy\n",
305 mdi
->array
.major_version
== -1 &&
306 mdi
->array
.minor_version
== -2 &&
307 !is_subarray(mdi
->text_version
)) {
308 struct mdstat_ent
*mds
, *m
;
309 /* container, possibly mdmon-managed.
310 * Make sure mdmon isn't opening it, which
311 * would interfere with the 'stop'
313 ping_monitor(mdi
->sys_name
);
315 /* now check that there are no existing arrays
316 * which are members of this array
318 mds
= mdstat_read(0, 0);
319 for (m
= mds
; m
; m
= m
->next
)
320 if (m
->metadata_version
&&
321 strncmp(m
->metadata_version
, "external:", 9)==0 &&
322 metadata_container_matches(m
->metadata_version
+9,
325 pr_err("Cannot stop container %s: "
326 "member %s still active\n",
334 /* As we have an O_EXCL open, any use of the device
335 * which blocks STOP_ARRAY is probably a transient use,
336 * so it is reasonable to retry for a while - 5 seconds.
339 while (count
&& fd
>= 0
340 && (err
= ioctl(fd
, STOP_ARRAY
, NULL
)) < 0
345 if (fd
>= 0 && err
) {
347 pr_err("failed to stop array %s: %s\n",
348 devname
, strerror(errno
));
350 cont_err("Perhaps a running "
351 "process, mounted filesystem "
352 "or active volume group?\n");
357 /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
358 * was stopped, so We'll do it here just to be sure. Drop any
359 * partitions as well...
362 ioctl(fd
, BLKRRPART
, 0);
364 sysfs_uevent(mdi
, "change");
366 if (devnm
[0] && use_udev()) {
367 struct map_ent
*mp
= map_by_devnm(&map
, devnm
);
368 remove_devices(devnm
, mp
? mp
->path
: NULL
);
372 pr_err("stopped %s\n", devname
);
374 map_remove(&map
, devnm
);
383 static struct mddev_dev
*add_one(struct mddev_dev
*dv
, char *name
, char disp
)
385 struct mddev_dev
*new;
386 new = xmalloc(sizeof(*new));
387 memset(new, 0, sizeof(*new));
388 new->devname
= xstrdup(name
);
389 new->disposition
= disp
;
390 new->next
= dv
->next
;
395 static void add_faulty(struct mddev_dev
*dv
, int fd
, char disp
)
397 mdu_array_info_t array
;
398 mdu_disk_info_t disk
;
402 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0)
405 remaining_disks
= array
.nr_disks
;
406 for (i
= 0; i
< MAX_DISKS
&& remaining_disks
> 0; i
++) {
409 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
411 if (disk
.major
== 0 && disk
.minor
== 0)
414 if ((disk
.state
& 1) == 0) /* not faulty */
416 sprintf(buf
, "%d:%d", disk
.major
, disk
.minor
);
417 dv
= add_one(dv
, buf
, disp
);
421 static void add_detached(struct mddev_dev
*dv
, int fd
, char disp
)
423 mdu_array_info_t array
;
424 mdu_disk_info_t disk
;
428 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0)
431 remaining_disks
= array
.nr_disks
;
432 for (i
= 0; i
< MAX_DISKS
&& remaining_disks
> 0; i
++) {
436 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
438 if (disk
.major
== 0 && disk
.minor
== 0)
441 if (disp
== 'f' && (disk
.state
& 1) != 0) /* already faulty */
443 sprintf(buf
, "%d:%d", disk
.major
, disk
.minor
);
444 sfd
= dev_open(buf
, O_RDONLY
);
451 /* Probably not detached */
453 dv
= add_one(dv
, buf
, disp
);
457 static void add_set(struct mddev_dev
*dv
, int fd
, char set_char
)
459 mdu_array_info_t array
;
460 mdu_disk_info_t disk
;
465 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0)
467 if (array
.level
!= 10)
469 copies
= ((array
.layout
& 0xff) *
470 ((array
.layout
>> 8) & 0xff));
471 if (array
.raid_disks
% copies
)
474 remaining_disks
= array
.nr_disks
;
475 for (i
= 0; i
< MAX_DISKS
&& remaining_disks
> 0; i
++) {
478 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
480 if (disk
.major
== 0 && disk
.minor
== 0)
483 set
= disk
.raid_disk
% copies
;
484 if (set_char
!= set
+ 'A')
486 sprintf(buf
, "%d:%d", disk
.major
, disk
.minor
);
487 dv
= add_one(dv
, buf
, dv
->disposition
);
491 int attempt_re_add(int fd
, int tfd
, struct mddev_dev
*dv
,
492 struct supertype
*dev_st
, struct supertype
*tst
,
494 char *update
, char *devname
, int verbose
,
495 mdu_array_info_t
*array
)
501 dev_st
->ss
->getinfo_super(dev_st
, &mdi
, NULL
);
502 dev_st
->ss
->uuid_from_super(dev_st
, ouuid
);
504 tst
->ss
->uuid_from_super(tst
, duuid
);
506 /* Assume uuid matches: kernel will check */
507 memcpy(duuid
, ouuid
, sizeof(ouuid
));
508 if ((mdi
.disk
.state
& (1<<MD_DISK_ACTIVE
)) &&
509 !(mdi
.disk
.state
& (1<<MD_DISK_FAULTY
)) &&
510 memcmp(duuid
, ouuid
, sizeof(ouuid
))==0) {
511 /* Looks like it is worth a
512 * try. Need to make sure
513 * kernel will accept it
516 mdu_disk_info_t disc
;
517 /* re-add doesn't work for version-1 superblocks
520 if (array
->major_version
== 1 &&
521 get_linux_version() <= 2006018)
523 disc
.number
= mdi
.disk
.number
;
524 if (ioctl(fd
, GET_DISK_INFO
, &disc
) != 0
525 || disc
.major
!= 0 || disc
.minor
!= 0
528 disc
.major
= major(rdev
);
529 disc
.minor
= minor(rdev
);
530 disc
.number
= mdi
.disk
.number
;
531 disc
.raid_disk
= mdi
.disk
.raid_disk
;
532 disc
.state
= mdi
.disk
.state
;
533 if (dv
->writemostly
== 1)
534 disc
.state
|= 1 << MD_DISK_WRITEMOSTLY
;
535 if (dv
->writemostly
== 2)
536 disc
.state
&= ~(1 << MD_DISK_WRITEMOSTLY
);
537 remove_partitions(tfd
);
538 if (update
|| dv
->writemostly
> 0) {
540 tfd
= dev_open(dv
->devname
, O_RDWR
);
542 pr_err("failed to open %s for"
543 " superblock update during re-add\n", dv
->devname
);
547 if (dv
->writemostly
== 1)
548 rv
= dev_st
->ss
->update_super(
549 dev_st
, NULL
, "writemostly",
550 devname
, verbose
, 0, NULL
);
551 if (dv
->writemostly
== 2)
552 rv
= dev_st
->ss
->update_super(
553 dev_st
, NULL
, "readwrite",
554 devname
, verbose
, 0, NULL
);
556 rv
= dev_st
->ss
->update_super(
557 dev_st
, NULL
, update
,
558 devname
, verbose
, 0, NULL
);
560 rv
= dev_st
->ss
->store_super(dev_st
, tfd
);
563 pr_err("failed to update"
564 " superblock during re-add\n");
568 /* don't even try if disk is marked as faulty */
570 if (ioctl(fd
, ADD_NEW_DISK
, &disc
) == 0) {
572 pr_err("re-added %s\n", dv
->devname
);
575 if (errno
== ENOMEM
|| errno
== EROFS
) {
576 pr_err("add new device failed for %s: %s\n",
577 dv
->devname
, strerror(errno
));
578 if (dv
->disposition
== 'M')
587 int Manage_add(int fd
, int tfd
, struct mddev_dev
*dv
,
588 struct supertype
*tst
, mdu_array_info_t
*array
,
589 int force
, int verbose
, char *devname
,
590 char *update
, unsigned long rdev
, unsigned long long array_size
)
592 unsigned long long ldsize
;
593 struct supertype
*dev_st
= NULL
;
595 mdu_disk_info_t disc
;
597 if (!get_dev_size(tfd
, dv
->devname
, &ldsize
)) {
598 if (dv
->disposition
== 'M')
604 if (tst
->ss
->validate_geometry(
605 tst
, array
->level
, array
->layout
,
606 array
->raid_disks
, NULL
,
607 ldsize
>> 9, INVALID_SECTORS
, NULL
, NULL
, 0) == 0) {
609 pr_err("%s is larger than %s can "
611 " Add --force is you "
612 "really want to add this device.\n",
613 dv
->devname
, devname
);
616 pr_err("%s is larger than %s can "
618 " Adding anyway as --force "
620 dv
->devname
, devname
);
622 if (!tst
->ss
->external
&&
623 array
->major_version
== 0 &&
624 md_get_version(fd
)%100 < 2) {
625 if (ioctl(fd
, HOT_ADD_DISK
, rdev
)==0) {
627 pr_err("hot added %s\n",
632 pr_err("hot add failed for %s: %s\n",
633 dv
->devname
, strerror(errno
));
637 if (array
->not_persistent
== 0 || tst
->ss
->external
) {
639 /* need to find a sample superblock to copy, and
640 * a spare slot to use.
641 * For 'external' array (well, container based),
642 * We can just load the metadata for the array->
646 /* already loaded */;
647 else if (tst
->ss
->external
) {
648 tst
->ss
->load_container(tst
, fd
, NULL
);
649 } else for (j
= 0; j
< tst
->max_devs
; j
++) {
653 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
655 if (disc
.major
==0 && disc
.minor
==0)
657 if ((disc
.state
& 4)==0) /* sync */
659 /* Looks like a good device to try */
660 dev
= map_dev(disc
.major
, disc
.minor
, 1);
663 dfd
= dev_open(dev
, O_RDONLY
);
666 if (tst
->ss
->load_super(tst
, dfd
,
674 /* FIXME this is a bad test to be using */
675 if (!tst
->sb
&& dv
->disposition
!= 'a') {
676 /* we are re-adding a device to a
677 * completely dead array - have to depend
680 } else if (!tst
->sb
) {
681 pr_err("cannot load array metadata from %s\n", devname
);
685 /* Make sure device is large enough */
686 if (tst
->ss
->avail_size(tst
, ldsize
/512, INVALID_SECTORS
) <
688 if (dv
->disposition
== 'M')
690 pr_err("%s not large enough to join array\n",
695 /* Possibly this device was recently part of
696 * the array and was temporarily removed, and
697 * is now being re-added. If so, we can
701 if (array
->not_persistent
==0) {
702 dev_st
= dup_super(tst
);
703 dev_st
->ss
->load_super(dev_st
, tfd
, NULL
);
705 if (dev_st
&& dev_st
->sb
) {
706 int rv
= attempt_re_add(fd
, tfd
, dv
,
712 dev_st
->ss
->free_super(dev_st
);
716 if (dv
->disposition
== 'M') {
718 pr_err("--re-add for %s to %s is not possible\n",
719 dv
->devname
, devname
);
722 if (dv
->disposition
== 'A') {
723 pr_err("--re-add for %s to %s is not possible\n",
724 dv
->devname
, devname
);
727 if (array
->active_disks
< array
->raid_disks
) {
728 char *avail
= xcalloc(array
->raid_disks
, 1);
732 for (d
= 0; d
< MAX_DISKS
&& found
< array
->active_disks
; d
++) {
734 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
736 if (disc
.major
== 0 && disc
.minor
== 0)
738 if (!(disc
.state
& (1<<MD_DISK_SYNC
)))
740 avail
[disc
.raid_disk
] = 1;
743 array_failed
= !enough(array
->level
, array
->raid_disks
,
744 array
->layout
, 1, avail
);
748 pr_err("%s has failed so using --add cannot work and might destroy\n",
750 pr_err("data on %s. You should stop the array and re-assemble it.\n",
755 /* non-persistent. Must ensure that new drive
756 * is at least array->size big.
758 if (ldsize
/512 < array_size
) {
759 pr_err("%s not large enough to join array\n",
764 /* committed to really trying this device now*/
765 remove_partitions(tfd
);
767 /* in 2.6.17 and earlier, version-1 superblocks won't
768 * use the number we write, but will choose a free number.
769 * we must choose the same free number, which requires
770 * starting at 'raid_disks' and counting up
772 for (j
= array
->raid_disks
; j
< tst
->max_devs
; j
++) {
774 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
776 if (disc
.major
==0 && disc
.minor
==0)
778 if (disc
.state
& 8) /* removed */
781 disc
.major
= major(rdev
);
782 disc
.minor
= minor(rdev
);
785 if (array
->not_persistent
==0) {
787 if (dv
->writemostly
== 1)
788 disc
.state
|= 1 << MD_DISK_WRITEMOSTLY
;
789 dfd
= dev_open(dv
->devname
, O_RDWR
| O_EXCL
|O_DIRECT
);
790 if (tst
->ss
->add_to_super(tst
, &disc
, dfd
,
791 dv
->devname
, INVALID_SECTORS
))
793 if (tst
->ss
->write_init_super(tst
))
795 } else if (dv
->disposition
== 'A') {
796 /* this had better be raid1.
797 * As we are "--re-add"ing we must find a spare slot
800 char *used
= xcalloc(array
->raid_disks
, 1);
801 for (j
= 0; j
< tst
->max_devs
; j
++) {
802 mdu_disk_info_t disc2
;
804 if (ioctl(fd
, GET_DISK_INFO
, &disc2
))
806 if (disc2
.major
==0 && disc2
.minor
==0)
808 if (disc2
.state
& 8) /* removed */
810 if (disc2
.raid_disk
< 0)
812 if (disc2
.raid_disk
> array
->raid_disks
)
814 used
[disc2
.raid_disk
] = 1;
816 for (j
= 0 ; j
< array
->raid_disks
; j
++)
819 disc
.state
|= (1<<MD_DISK_SYNC
);
824 if (dv
->writemostly
== 1)
825 disc
.state
|= (1 << MD_DISK_WRITEMOSTLY
);
826 if (tst
->ss
->external
) {
828 * to an external metadata container */
829 struct mdinfo new_mdi
;
835 strcpy(devnm
, fd2devnm(fd
));
837 container_fd
= open_dev_excl(devnm
);
838 if (container_fd
< 0) {
839 pr_err("add failed for %s:"
840 " could not get exclusive access to container\n",
842 tst
->ss
->free_super(tst
);
846 Kill(dv
->devname
, NULL
, 0, -1, 0);
847 dfd
= dev_open(dv
->devname
, O_RDWR
| O_EXCL
|O_DIRECT
);
848 if (mdmon_running(tst
->container_devnm
))
849 tst
->update_tail
= &tst
->updates
;
850 if (tst
->ss
->add_to_super(tst
, &disc
, dfd
,
851 dv
->devname
, INVALID_SECTORS
)) {
856 if (tst
->update_tail
)
857 flush_metadata_updates(tst
);
859 tst
->ss
->sync_metadata(tst
);
861 sra
= sysfs_read(container_fd
, NULL
, 0);
863 pr_err("add failed for %s: sysfs_read failed\n",
866 tst
->ss
->free_super(tst
);
869 sra
->array
.level
= LEVEL_CONTAINER
;
870 /* Need to set data_offset and component_size */
871 tst
->ss
->getinfo_super(tst
, &new_mdi
, NULL
);
872 new_mdi
.disk
.major
= disc
.major
;
873 new_mdi
.disk
.minor
= disc
.minor
;
874 new_mdi
.recovery_start
= 0;
875 /* Make sure fds are closed as they are O_EXCL which
876 * would block add_disk */
877 tst
->ss
->free_super(tst
);
878 if (sysfs_add_disk(sra
, &new_mdi
, 0) != 0) {
879 pr_err("add new device to external metadata"
880 " failed for %s\n", dv
->devname
);
889 tst
->ss
->free_super(tst
);
890 if (ioctl(fd
, ADD_NEW_DISK
, &disc
)) {
891 pr_err("add new device failed for %s as %d: %s\n",
892 dv
->devname
, j
, strerror(errno
));
897 pr_err("added %s\n", dv
->devname
);
901 int Manage_remove(struct supertype
*tst
, int fd
, struct mddev_dev
*dv
,
902 int sysfd
, unsigned long rdev
, int verbose
, char *devname
)
907 if (tst
->ss
->external
) {
908 /* To remove a device from a container, we must
909 * check that it isn't in use in an array.
910 * This involves looking in the 'holders'
911 * directory - there must be just one entry,
913 * To ensure that it doesn't get used as a
914 * hot spare while we are checking, we
915 * get an O_EXCL open on the container
919 strcpy(devnm
, fd2devnm(fd
));
920 lfd
= open_dev_excl(devnm
);
922 pr_err("Cannot get exclusive access "
923 " to container - odd\n");
926 /* We may not be able to check on holders in
927 * sysfs, either because we don't have the dev num
928 * (rdev == 0) or because the device has been detached
929 * and the 'holders' directory no longer exists
930 * (ret == -1). In that case, assume it is OK to
936 ret
= sysfs_unique_holder(devnm
, rdev
);
938 pr_err("%s is not a member, cannot remove.\n",
944 pr_err("%s is still in use, cannot remove.\n",
950 /* FIXME check that it is a current member */
952 /* device has been removed and we don't know
953 * the major:minor number
955 int n
= write(sysfd
, "remove", 6);
961 err
= ioctl(fd
, HOT_REMOVE_DISK
, rdev
);
962 if (err
&& errno
== ENODEV
) {
963 /* Old kernels rejected this if no personality
965 struct mdinfo
*sra
= sysfs_read(fd
, NULL
, GET_DEVS
);
966 struct mdinfo
*dv
= NULL
;
969 for ( ; dv
; dv
=dv
->next
)
970 if (dv
->disk
.major
== (int)major(rdev
) &&
971 dv
->disk
.minor
== (int)minor(rdev
))
974 err
= sysfs_set_str(sra
, dv
,
983 pr_err("hot remove failed "
984 "for %s: %s\n", dv
->devname
,
990 if (tst
->ss
->external
) {
992 * Before dropping our exclusive open we make an
993 * attempt at preventing mdmon from seeing an
994 * 'add' event before reconciling this 'remove'
997 char *devnm
= fd2devnm(fd
);
1000 pr_err("unable to get container name\n");
1004 ping_manager(devnm
);
1009 pr_err("hot removed %s from %s\n",
1010 dv
->devname
, devname
);
1014 int Manage_replace(struct supertype
*tst
, int fd
, struct mddev_dev
*dv
,
1015 unsigned long rdev
, int verbose
, char *devname
)
1017 struct mdinfo
*mdi
, *di
;
1018 if (tst
->ss
->external
) {
1019 pr_err("--replace only supported for native metadata (0.90 or 1.x)\n");
1022 /* Need to find the device in sysfs and add 'want_replacement' to the
1025 mdi
= sysfs_read(fd
, NULL
, GET_DEVS
);
1026 if (!mdi
|| !mdi
->devs
) {
1027 pr_err("Cannot find status of %s to enable replacement - strange\n",
1031 for (di
= mdi
->devs
; di
; di
= di
->next
)
1032 if (di
->disk
.major
== (int)major(rdev
) &&
1033 di
->disk
.minor
== (int)minor(rdev
))
1037 if (di
->disk
.raid_disk
< 0) {
1038 pr_err("%s is not active and so cannot be replaced.\n",
1043 rv
= sysfs_set_str(mdi
, di
,
1044 "state", "want_replacement");
1047 pr_err("Failed to request replacement for %s\n",
1052 pr_err("Marked %s (device %d in %s) for replacement\n",
1053 dv
->devname
, di
->disk
.raid_disk
, devname
);
1054 /* If there is a matching 'with', we need to tell it which
1057 while (dv
&& dv
->disposition
!= 'W')
1060 dv
->disposition
= 'w';
1061 dv
->used
= di
->disk
.raid_disk
;
1066 pr_err("%s not found in %s so cannot --replace it\n",
1067 dv
->devname
, devname
);
1071 int Manage_with(struct supertype
*tst
, int fd
, struct mddev_dev
*dv
,
1072 unsigned long rdev
, int verbose
, char *devname
)
1074 struct mdinfo
*mdi
, *di
;
1075 /* try to set 'slot' for 'rdev' in 'fd' to 'dv->used' */
1076 mdi
= sysfs_read(fd
, NULL
, GET_DEVS
|GET_STATE
);
1077 if (!mdi
|| !mdi
->devs
) {
1078 pr_err("Cannot find status of %s to enable replacement - strange\n",
1082 for (di
= mdi
->devs
; di
; di
= di
->next
)
1083 if (di
->disk
.major
== (int)major(rdev
) &&
1084 di
->disk
.minor
== (int)minor(rdev
))
1088 if (di
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
1089 pr_err("%s is faulty and cannot be a replacement\n",
1094 if (di
->disk
.raid_disk
>= 0) {
1095 pr_err("%s is active and cannot be a replacement\n",
1100 rv
= sysfs_set_num(mdi
, di
,
1104 pr_err("Failed to %s as preferred replacement.\n",
1109 pr_err("Marked %s in %s as replacement for device %d\n",
1110 dv
->devname
, devname
, dv
->used
);
1114 pr_err("%s not found in %s so cannot make it preferred replacement\n",
1115 dv
->devname
, devname
);
1119 int Manage_subdevs(char *devname
, int fd
,
1120 struct mddev_dev
*devlist
, int verbose
, int test
,
1121 char *update
, int force
)
1123 /* Do something to each dev.
1125 * 'a' - add the device
1127 * If that fails EINVAL, try ADD_NEW_DISK
1128 * 'A' - re-add the device
1129 * 'r' - remove the device: HOT_REMOVE_DISK
1130 * device can be 'faulty' or 'detached' in which case all
1131 * matching devices are removed.
1132 * 'f' - set the device faulty SET_DISK_FAULTY
1133 * device can be 'detached' in which case any device that
1134 * is inaccessible will be marked faulty.
1135 * 'R' - mark this device as wanting replacement.
1136 * 'W' - this device is added if necessary and activated as
1137 * a replacement for a previous 'R' device.
1139 * 'w' - 'W' will be changed to 'w' when it is paired with
1140 * a 'R' device. If a 'W' is found while walking the list
1141 * it must be unpaired, and is an error.
1142 * 'M' - this is created by a 'missing' target. It is a slight
1144 * 'F' - Another variant of 'A', where the device was faulty
1145 * so must be removed from the array first.
1147 * For 'f' and 'r', the device can also be a kernel-internal
1148 * name such as 'sdb'.
1150 mdu_array_info_t array
;
1151 unsigned long long array_size
;
1152 struct mddev_dev
*dv
;
1155 struct supertype
*tst
;
1156 char *subarray
= NULL
;
1158 int count
= 0; /* number of actions taken */
1163 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)) {
1164 pr_err("Cannot get array info for %s\n",
1168 sysfs_init(&info
, fd
, NULL
);
1170 /* array.size is only 32 bits and may be truncated.
1171 * So read from sysfs if possible, and record number of sectors
1174 array_size
= get_component_size(fd
);
1175 if (array_size
<= 0)
1176 array_size
= array
.size
* 2;
1178 tst
= super_by_fd(fd
, &subarray
);
1180 pr_err("unsupport array - version %d.%d\n",
1181 array
.major_version
, array
.minor_version
);
1186 for (dv
= devlist
; dv
; dv
= dv
->next
) {
1189 if (strcmp(dv
->devname
, "failed") == 0 ||
1190 strcmp(dv
->devname
, "faulty") == 0) {
1191 if (dv
->disposition
!= 'A'
1192 && dv
->disposition
!= 'r') {
1193 pr_err("%s only meaningful "
1194 "with -r or --re-add, not -%c\n",
1195 dv
->devname
, dv
->disposition
);
1198 add_faulty(dv
, fd
, (dv
->disposition
== 'A'
1202 if (strcmp(dv
->devname
, "detached") == 0) {
1203 if (dv
->disposition
!= 'r' && dv
->disposition
!= 'f') {
1204 pr_err("%s only meaningful "
1205 "with -r of -f, not -%c\n",
1206 dv
->devname
, dv
->disposition
);
1209 add_detached(dv
, fd
, dv
->disposition
);
1213 if (strcmp(dv
->devname
, "missing") == 0) {
1214 struct mddev_dev
*add_devlist
= NULL
;
1215 struct mddev_dev
**dp
;
1216 if (dv
->disposition
!= 'A') {
1217 pr_err("'missing' only meaningful "
1221 add_devlist
= conf_get_devs();
1222 if (add_devlist
== NULL
) {
1223 pr_err("no devices to scan for missing members.");
1226 for (dp
= &add_devlist
; *dp
; dp
= & (*dp
)->next
)
1227 /* 'M' (for 'missing') is like 'A' without errors */
1228 (*dp
)->disposition
= 'M';
1230 dv
->next
= add_devlist
;
1234 if (strncmp(dv
->devname
, "set-", 4) == 0 &&
1235 strlen(dv
->devname
) == 5) {
1238 if (dv
->disposition
!= 'r' &&
1239 dv
->disposition
!= 'f') {
1240 pr_err("'%s' only meaningful with -r or -f\n",
1244 if (array
.level
!= 10) {
1245 pr_err("'%s' only meaningful with RAID10 arrays\n",
1249 copies
= ((array
.layout
& 0xff) *
1250 ((array
.layout
>> 8) & 0xff));
1251 if (array
.raid_disks
% copies
!= 0 ||
1252 dv
->devname
[4] < 'A' ||
1253 dv
->devname
[4] >= 'A' + copies
||
1255 pr_err("'%s' not meaningful with this array\n",
1259 add_set(dv
, fd
, dv
->devname
[4]);
1263 if (strchr(dv
->devname
, '/') == NULL
&&
1264 strchr(dv
->devname
, ':') == NULL
&&
1265 strlen(dv
->devname
) < 50) {
1266 /* Assume this is a kernel-internal name like 'sda1' */
1269 if (dv
->disposition
!= 'r' && dv
->disposition
!= 'f') {
1270 pr_err("%s only meaningful "
1271 "with -r or -f, not -%c\n",
1272 dv
->devname
, dv
->disposition
);
1276 sprintf(dname
, "dev-%s", dv
->devname
);
1277 sysfd
= sysfs_open(fd2devnm(fd
), dname
, "block/dev");
1281 if (sysfs_fd_get_str(sysfd
, dn
, 20) > 0 &&
1282 sscanf(dn
, "%d:%d", &mj
,&mn
) == 2) {
1283 stb
.st_rdev
= makedev(mj
,mn
);
1290 sysfd
= sysfs_open(fd2devnm(fd
), dname
, "state");
1292 pr_err("%s does not appear "
1293 "to be a component of %s\n",
1294 dv
->devname
, devname
);
1299 tfd
= dev_open(dv
->devname
, O_RDONLY
);
1303 int open_err
= errno
;
1304 if (stat(dv
->devname
, &stb
) != 0) {
1305 pr_err("Cannot find %s: %s\n",
1306 dv
->devname
, strerror(errno
));
1309 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
1310 if (dv
->disposition
== 'M')
1311 /* non-fatal. Also improbable */
1313 pr_err("%s is not a block device.\n",
1317 if (dv
->disposition
== 'r')
1318 /* Be happy, the stat worked, that is
1319 * enough for --remove
1323 if (dv
->disposition
== 'M')
1326 pr_err("Cannot open %s: %s\n",
1327 dv
->devname
, strerror(open_err
));
1332 switch(dv
->disposition
){
1334 pr_err("internal error - devmode[%s]=%d\n",
1335 dv
->devname
, dv
->disposition
);
1339 case 'M': /* --re-add missing */
1340 case 'F': /* --re-add faulty */
1341 /* add the device */
1343 pr_err("Cannot add disks to a"
1344 " \'member\' array, perform this"
1345 " operation on the parent container\n");
1348 if (dv
->disposition
== 'F')
1349 /* Need to remove first */
1350 ioctl(fd
, HOT_REMOVE_DISK
,
1351 (unsigned long)stb
.st_rdev
);
1352 /* Make sure it isn't in use (in 2.6 or later) */
1353 tfd
= dev_open(dv
->devname
, O_RDONLY
|O_EXCL
);
1355 /* We know no-one else is using it. We'll
1356 * need non-exclusive access to add it, so
1360 tfd
= dev_open(dv
->devname
, O_RDONLY
);
1363 if (dv
->disposition
== 'M')
1365 pr_err("Cannot open %s: %s\n",
1366 dv
->devname
, strerror(errno
));
1370 if (sysfs_freeze_array(&info
) == 1)
1375 rv
= Manage_add(fd
, tfd
, dv
, tst
, &array
,
1376 force
, verbose
, devname
, update
,
1377 stb
.st_rdev
, array_size
);
1389 pr_err("Cannot remove disks from a"
1390 " \'member\' array, perform this"
1391 " operation on the parent container\n");
1394 rv
= Manage_remove(tst
, fd
, dv
, sysfd
,
1395 stb
.st_rdev
, verbose
,
1406 case 'f': /* set faulty */
1407 /* FIXME check current member */
1408 if ((sysfd
>= 0 && write(sysfd
, "faulty", 6) != 6) ||
1409 (sysfd
< 0 && ioctl(fd
, SET_DISK_FAULTY
,
1410 (unsigned long) stb
.st_rdev
))) {
1413 pr_err("set device faulty failed for %s: %s\n",
1414 dv
->devname
, strerror(errno
));
1424 pr_err("set %s faulty in %s\n",
1425 dv
->devname
, devname
);
1427 case 'R': /* Mark as replaceable */
1429 pr_err("Cannot replace disks in a"
1430 " \'member\' array, perform this"
1431 " operation on the parent container\n");
1435 if (sysfs_freeze_array(&info
) == 1)
1440 rv
= Manage_replace(tst
, fd
, dv
,
1441 stb
.st_rdev
, verbose
,
1449 case 'W': /* --with device that doesn't match */
1450 pr_err("No matching --replace device for --with %s\n",
1453 case 'w': /* --with device which was matched */
1454 rv
= Manage_with(tst
, fd
, dv
,
1455 stb
.st_rdev
, verbose
, devname
);
1462 sysfs_set_str(&info
, NULL
, "sync_action","idle");
1463 if (test
&& count
== 0)
1469 sysfs_set_str(&info
, NULL
, "sync_action","idle");
1470 return !test
&& busy
? 2 : 1;
1473 int autodetect(void)
1475 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1477 int fd
= dev_open("9:0", O_RDONLY
);
1479 if (ioctl(fd
, RAID_AUTORUN
, 0) == 0)
1486 int Update_subarray(char *dev
, char *subarray
, char *update
, struct mddev_ident
*ident
, int verbose
)
1488 struct supertype supertype
, *st
= &supertype
;
1491 memset(st
, 0, sizeof(*st
));
1493 fd
= open_subarray(dev
, subarray
, st
, verbose
< 0);
1497 if (!st
->ss
->update_subarray
) {
1499 pr_err("Operation not supported for %s metadata\n",
1504 if (mdmon_running(st
->devnm
))
1505 st
->update_tail
= &st
->updates
;
1507 rv
= st
->ss
->update_subarray(st
, subarray
, update
, ident
);
1511 pr_err("Failed to update %s of subarray-%s in %s\n",
1512 update
, subarray
, dev
);
1513 } else if (st
->update_tail
)
1514 flush_metadata_updates(st
);
1516 st
->ss
->sync_metadata(st
);
1518 if (rv
== 0 && strcmp(update
, "name") == 0 && verbose
>= 0)
1519 pr_err("Updated subarray-%s name from %s, UUIDs may have changed\n",
1523 st
->ss
->free_super(st
);
1529 /* Move spare from one array to another If adding to destination array fails
1530 * add back to original array.
1531 * Returns 1 on success, 0 on failure */
1532 int move_spare(char *from_devname
, char *to_devname
, dev_t devid
)
1534 struct mddev_dev devlist
;
1537 /* try to remove and add */
1538 int fd1
= open(to_devname
, O_RDONLY
);
1539 int fd2
= open(from_devname
, O_RDONLY
);
1541 if (fd1
< 0 || fd2
< 0) {
1542 if (fd1
>=0) close(fd1
);
1543 if (fd2
>=0) close(fd2
);
1547 devlist
.next
= NULL
;
1549 devlist
.writemostly
= 0;
1550 devlist
.devname
= devname
;
1551 sprintf(devname
, "%d:%d", major(devid
), minor(devid
));
1553 devlist
.disposition
= 'r';
1554 if (Manage_subdevs(from_devname
, fd2
, &devlist
, -1, 0, NULL
, 0) == 0) {
1555 devlist
.disposition
= 'a';
1556 if (Manage_subdevs(to_devname
, fd1
, &devlist
, -1, 0, NULL
, 0) == 0) {
1557 /* make sure manager is aware of changes */
1558 ping_manager(to_devname
);
1559 ping_manager(from_devname
);
1564 else Manage_subdevs(from_devname
, fd2
, &devlist
, -1, 0, NULL
, 0);