]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Manage.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
35 #define REGISTER_DEV _IO (MD_MAJOR, 1)
36 #define START_MD _IO (MD_MAJOR, 2)
37 #define STOP_MD _IO (MD_MAJOR, 3)
39 int Manage_ro(char *devname
, int fd
, int readonly
)
41 /* switch to readonly or rw
44 * first check that array is runing
45 * use RESTART_ARRAY_RW or STOP_ARRAY_RO
48 mdu_array_info_t array
;
53 if (md_get_version(fd
) < 9000) {
54 fprintf(stderr
, Name
": need md driver version 0.90.0 or later\n");
58 /* If this is an externally-manage array, we need to modify the
59 * metadata_version so that mdmon doesn't undo our change.
61 mdi
= sysfs_read(fd
, -1, GET_LEVEL
|GET_VERSION
);
63 mdi
->array
.major_version
== -1 &&
64 mdi
->array
.level
> 0 &&
65 is_subarray(mdi
->text_version
)) {
67 strcpy(vers
, "external:");
68 strcat(vers
, mdi
->text_version
);
71 /* We set readonly ourselves. */
73 sysfs_set_str(mdi
, NULL
, "metadata_version", vers
);
76 rv
= sysfs_set_str(mdi
, NULL
, "array_state", "readonly");
79 fprintf(stderr
, Name
": failed to set readonly for %s: %s\n",
80 devname
, strerror(errno
));
82 vers
[9] = mdi
->text_version
[0];
83 sysfs_set_str(mdi
, NULL
, "metadata_version", vers
);
88 /* We cannot set read/write - must signal mdmon */
90 sysfs_set_str(mdi
, NULL
, "metadata_version", vers
);
92 cp
= strchr(vers
+10, '/');
95 ping_monitor(vers
+10);
100 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)) {
101 fprintf(stderr
, Name
": %s does not appear to be active.\n",
107 if (ioctl(fd
, STOP_ARRAY_RO
, NULL
)) {
108 fprintf(stderr
, Name
": failed to set readonly for %s: %s\n",
109 devname
, strerror(errno
));
112 } else if (readonly
< 0) {
113 if (ioctl(fd
, RESTART_ARRAY_RW
, NULL
)) {
114 fprintf(stderr
, Name
": failed to set writable for %s: %s\n",
115 devname
, strerror(errno
));
124 static void remove_devices(int devnum
, char *path
)
126 /* Remove all 'standard' devices for 'devnum', including
127 * partitions. Also remove names at 'path' - possibly with
128 * partition suffixes - which link to those names.
139 sprintf(base
, "/dev/md%d", devnum
);
141 sprintf(base
, "/dev/md_d%d", -1-devnum
);
142 be
= base
+ strlen(base
);
144 path2
= malloc(strlen(path
)+20);
146 pe
= path2
+ strlen(path2
);
150 for (part
= 0; part
< 16; part
++) {
152 sprintf(be
, "p%d", part
);
155 sprintf(pe
, "p%d", part
);
157 sprintf(pe
, "%d", part
);
160 /* FIXME test if really is md device ?? */
163 n
= readlink(path2
, link
, sizeof(link
));
164 if (n
&& strlen(base
) == n
&&
165 strncmp(link
, base
, n
) == 0)
172 int Manage_runstop(char *devname
, int fd
, int runstop
, int quiet
)
174 /* Run or stop the array. array must already be configured
176 * Only print failure messages if quiet == 0;
177 * quiet > 0 means really be quiet
178 * quiet < 0 means we will try again if it fails.
180 mdu_param_t param
; /* unused */
182 if (runstop
== -1 && md_get_version(fd
) < 9000) {
183 if (ioctl(fd
, STOP_MD
, 0)) {
184 if (quiet
== 0) fprintf(stderr
,
185 Name
": stopping device %s "
187 devname
, strerror(errno
));
192 if (md_get_version(fd
) < 9000) {
193 fprintf(stderr
, Name
": need md driver version 0.90.0 or later\n");
197 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
198 fprintf(stderr, Name ": %s does not appear to be active.\n",
204 if (ioctl(fd
, RUN_ARRAY
, ¶m
)) {
205 fprintf(stderr
, Name
": failed to run array %s: %s\n",
206 devname
, strerror(errno
));
210 fprintf(stderr
, Name
": started %s\n", devname
);
211 } else if (runstop
< 0){
212 struct map_ent
*map
= NULL
;
216 /* If this is an mdmon managed array, just write 'inactive'
217 * to the array state and let mdmon clear up.
219 devnum
= fd2devnum(fd
);
220 mdi
= sysfs_read(fd
, -1, GET_LEVEL
|GET_VERSION
);
222 mdi
->array
.level
> 0 &&
223 is_subarray(mdi
->text_version
)) {
224 /* This is mdmon managed. */
226 if (sysfs_set_str(mdi
, NULL
,
227 "array_state", "inactive") < 0) {
230 ": failed to stop array %s: %s\n",
231 devname
, strerror(errno
));
235 /* Give monitor a chance to act */
236 ping_monitor(mdi
->text_version
);
238 fd
= open(devname
, O_RDONLY
);
240 mdi
->array
.major_version
== -1 &&
241 mdi
->array
.minor_version
== -2 &&
242 !is_subarray(mdi
->text_version
)) {
243 /* container, possibly mdmon-managed.
244 * Make sure mdmon isn't opening it, which
245 * would interfere with the 'stop'
247 ping_monitor(mdi
->sys_name
);
250 if (fd
>= 0 && ioctl(fd
, STOP_ARRAY
, NULL
)) {
253 ": failed to stop array %s: %s\n",
254 devname
, strerror(errno
));
256 fprintf(stderr
, "Perhaps a running "
257 "process, mounted filesystem "
258 "or active volume group?\n");
264 /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
265 * was stopped, so We'll do it here just to be sure. Drop any
266 * partitions as well...
269 ioctl(fd
, BLKRRPART
, 0);
271 sysfs_uevent(mdi
, "change");
274 if (devnum
!= NoMdDev
&&
275 (stat("/dev/.udev", &stb
) != 0 ||
276 check_env("MDADM_NO_UDEV"))) {
277 struct map_ent
*mp
= map_by_devnum(&map
, devnum
);
278 remove_devices(devnum
, mp
? mp
->path
: NULL
);
283 fprintf(stderr
, Name
": stopped %s\n", devname
);
284 if (devnum
!= NoMdDev
) {
285 map_delete(&map
, devnum
);
293 int Manage_resize(char *devname
, int fd
, long long size
, int raid_disks
)
295 mdu_array_info_t info
;
296 if (ioctl(fd
, GET_ARRAY_INFO
, &info
) != 0) {
297 fprintf(stderr
, Name
": Cannot get array information for %s: %s\n",
298 devname
, strerror(errno
));
304 info
.raid_disks
= raid_disks
;
305 if (ioctl(fd
, SET_ARRAY_INFO
, &info
) != 0) {
306 fprintf(stderr
, Name
": Cannot set device size/shape for %s: %s\n",
307 devname
, strerror(errno
));
313 int Manage_reconfig(char *devname
, int fd
, int layout
)
315 mdu_array_info_t info
;
316 if (ioctl(fd
, GET_ARRAY_INFO
, &info
) != 0) {
317 fprintf(stderr
, Name
": Cannot get array information for %s: %s\n",
318 devname
, strerror(errno
));
321 info
.layout
= layout
;
322 printf("layout set to %d\n", info
.layout
);
323 if (ioctl(fd
, SET_ARRAY_INFO
, &info
) != 0) {
324 fprintf(stderr
, Name
": Cannot set layout for %s: %s\n",
325 devname
, strerror(errno
));
331 int Manage_subdevs(char *devname
, int fd
,
332 mddev_dev_t devlist
, int verbose
)
334 /* do something to each dev.
336 * 'a' - add the device
338 * If that fails EINVAL, try ADD_NEW_DISK
339 * 'r' - remove the device HOT_REMOVE_DISK
340 * device can be 'faulty' or 'detached' in which case all
341 * matching devices are removed.
342 * 'f' - set the device faulty SET_DISK_FAULTY
343 * device can be 'detached' in which case any device that
344 * is inaccessible will be marked faulty.
346 mdu_array_info_t array
;
347 mdu_disk_info_t disc
;
348 unsigned long long array_size
;
349 mddev_dev_t dv
, next
= NULL
;
353 struct supertype
*st
, *tst
;
358 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)) {
359 fprintf(stderr
, Name
": cannot get array info for %s\n",
364 /* array.size is only 32 bit and may be truncated.
365 * So read from sysfs if possible, and record number of sectors
368 array_size
= get_component_size(fd
);
370 array_size
= array
.size
* 2;
372 tst
= super_by_fd(fd
);
374 fprintf(stderr
, Name
": unsupport array - version %d.%d\n",
375 array
.major_version
, array
.minor_version
);
379 for (dv
= devlist
, j
=0 ; dv
; dv
= next
, j
= jnext
) {
380 unsigned long long ldsize
;
382 char *dnprintable
= dv
->devname
;
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
);
396 for (; j
< array
.raid_disks
+ array
.nr_disks
; j
++) {
398 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
400 if (disc
.major
== 0 && disc
.minor
== 0)
402 if ((disc
.state
& 1) == 0) /* faulty */
404 stb
.st_rdev
= makedev(disc
.major
, disc
.minor
);
407 sprintf(dvname
,"%d:%d", disc
.major
, disc
.minor
);
408 dnprintable
= dvname
;
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
);
420 for (; j
< array
.raid_disks
+ array
.nr_disks
; j
++) {
423 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
425 if (disc
.major
== 0 && disc
.minor
== 0)
427 sprintf(dvname
,"%d:%d", disc
.major
, disc
.minor
);
428 sfd
= dev_open(dvname
, O_RDONLY
);
433 if (dv
->disposition
== 'f' &&
434 (disc
.state
& 1) == 1) /* already faulty */
438 stb
.st_rdev
= makedev(disc
.major
, disc
.minor
);
441 dnprintable
= dvname
;
449 if (stat(dv
->devname
, &stb
)) {
450 fprintf(stderr
, Name
": cannot find %s: %s\n",
451 dv
->devname
, strerror(errno
));
454 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
455 fprintf(stderr
, Name
": %s is not a "
461 switch(dv
->disposition
){
463 fprintf(stderr
, Name
": internal error - devmode[%s]=%d\n",
464 dv
->devname
, dv
->disposition
);
468 if (tst
->subarray
[0]) {
469 fprintf(stderr
, Name
": Cannot add disks to a"
470 " \'member\' array, perform this"
471 " operation on the parent container\n");
474 /* Make sure it isn't in use (in 2.6 or later) */
475 tfd
= open(dv
->devname
, O_RDONLY
|O_EXCL
|O_DIRECT
);
477 fprintf(stderr
, Name
": Cannot open %s: %s\n",
478 dv
->devname
, strerror(errno
));
481 remove_partitions(tfd
);
485 if (array
.not_persistent
==0)
486 st
->ss
->load_super(st
, tfd
, NULL
);
488 if (!get_dev_size(tfd
, dv
->devname
, &ldsize
)) {
495 if (!tst
->ss
->external
&&
496 array
.major_version
== 0 &&
497 md_get_version(fd
)%100 < 2) {
498 if (ioctl(fd
, HOT_ADD_DISK
,
499 (unsigned long)stb
.st_rdev
)==0) {
501 fprintf(stderr
, Name
": hot added %s\n",
506 fprintf(stderr
, Name
": hot add failed for %s: %s\n",
507 dv
->devname
, strerror(errno
));
511 if (array
.not_persistent
== 0 || tst
->ss
->external
) {
513 /* need to find a sample superblock to copy, and
514 * a spare slot to use.
515 * For 'external' array (well, container based),
516 * We can just load the metadata for the array.
518 if (tst
->ss
->external
) {
519 tst
->ss
->load_super(tst
, fd
, NULL
);
520 } else for (j
= 0; j
< tst
->max_devs
; j
++) {
524 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
526 if (disc
.major
==0 && disc
.minor
==0)
528 if ((disc
.state
& 4)==0) continue; /* sync */
529 /* Looks like a good device to try */
530 dev
= map_dev(disc
.major
, disc
.minor
, 1);
532 dfd
= dev_open(dev
, O_RDONLY
);
533 if (dfd
< 0) continue;
534 if (tst
->ss
->load_super(tst
, dfd
,
542 /* FIXME this is a bad test to be using */
544 fprintf(stderr
, Name
": cannot find valid superblock in this array - HELP\n");
548 /* Make sure device is large enough */
549 if (tst
->ss
->avail_size(tst
, ldsize
/512) <
551 fprintf(stderr
, Name
": %s not large enough to join array\n",
556 /* Possibly this device was recently part of the array
557 * and was temporarily removed, and is now being re-added.
558 * If so, we can simply re-add it.
560 tst
->ss
->uuid_from_super(tst
, duuid
);
562 /* re-add doesn't work for version-1 superblocks
565 if (array
.major_version
== 1 &&
566 get_linux_version() <= 2006018)
569 st
->ss
->uuid_from_super(st
, ouuid
);
570 if (memcmp(duuid
, ouuid
, sizeof(ouuid
))==0) {
571 /* looks close enough for now. Kernel
572 * will worry about whether a bitmap
573 * based reconstruction is possible.
576 st
->ss
->getinfo_super(st
, &mdi
);
577 disc
.major
= major(stb
.st_rdev
);
578 disc
.minor
= minor(stb
.st_rdev
);
579 disc
.number
= mdi
.disk
.number
;
580 disc
.raid_disk
= mdi
.disk
.raid_disk
;
581 disc
.state
= mdi
.disk
.state
;
582 if (dv
->writemostly
== 1)
583 disc
.state
|= 1 << MD_DISK_WRITEMOSTLY
;
584 if (dv
->writemostly
== 2)
585 disc
.state
&= ~(1 << MD_DISK_WRITEMOSTLY
);
586 if (ioctl(fd
, ADD_NEW_DISK
, &disc
) == 0) {
588 fprintf(stderr
, Name
": re-added %s\n", dv
->devname
);
591 /* fall back on normal-add */
595 /* non-persistent. Must ensure that new drive
596 * is at least array.size big.
598 if (ldsize
/512 < array_size
) {
599 fprintf(stderr
, Name
": %s not large enough to join array\n",
604 /* in 2.6.17 and earlier, version-1 superblocks won't
605 * use the number we write, but will choose a free number.
606 * we must choose the same free number, which requires
607 * starting at 'raid_disks' and counting up
609 for (j
= array
.raid_disks
; j
< tst
->max_devs
; j
++) {
611 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
613 if (disc
.major
==0 && disc
.minor
==0)
615 if (disc
.state
& 8) /* removed */
618 disc
.major
= major(stb
.st_rdev
);
619 disc
.minor
= minor(stb
.st_rdev
);
622 if (array
.not_persistent
==0 || tst
->ss
->external
) {
624 if (dv
->writemostly
== 1)
625 disc
.state
|= 1 << MD_DISK_WRITEMOSTLY
;
626 dfd
= open(dv
->devname
, O_RDWR
| O_EXCL
|O_DIRECT
);
627 if (tst
->ss
->add_to_super(tst
, &disc
, dfd
,
632 /* write_init_super will close 'dfd' */
633 if (tst
->ss
->external
)
634 /* mdmon will write the metadata */
636 else if (tst
->ss
->write_init_super(tst
))
638 } else if (dv
->re_add
) {
639 /* this had better be raid1.
640 * As we are "--re-add"ing we must find a spare slot
643 char *used
= malloc(array
.raid_disks
);
644 memset(used
, 0, array
.raid_disks
);
645 for (j
=0; j
< tst
->max_devs
; j
++) {
646 mdu_disk_info_t disc2
;
648 if (ioctl(fd
, GET_DISK_INFO
, &disc2
))
650 if (disc2
.major
==0 && disc2
.minor
==0)
652 if (disc2
.state
& 8) /* removed */
654 if (disc2
.raid_disk
< 0)
656 if (disc2
.raid_disk
> array
.raid_disks
)
658 used
[disc2
.raid_disk
] = 1;
660 for (j
=0 ; j
<array
.raid_disks
; j
++)
663 disc
.state
|= (1<<MD_DISK_SYNC
);
667 if (dv
->writemostly
== 1)
668 disc
.state
|= (1 << MD_DISK_WRITEMOSTLY
);
669 if (tst
->ss
->external
) {
670 /* add a disk to an external metadata container
671 * only if mdmon is around to see it
673 struct mdinfo new_mdi
;
676 int devnum
= fd2devnum(fd
);
678 container_fd
= open_dev_excl(devnum
);
679 if (container_fd
< 0) {
680 fprintf(stderr
, Name
": add failed for %s:"
681 " could not get exclusive access to container\n",
686 if (!mdmon_running(devnum
)) {
687 fprintf(stderr
, Name
": add failed for %s: mdmon not running\n",
693 sra
= sysfs_read(container_fd
, -1, 0);
695 fprintf(stderr
, Name
": add failed for %s: sysfs_read failed\n",
700 sra
->array
.level
= LEVEL_CONTAINER
;
701 /* Need to set data_offset and component_size */
702 tst
->ss
->getinfo_super(tst
, &new_mdi
);
703 new_mdi
.disk
.major
= disc
.major
;
704 new_mdi
.disk
.minor
= disc
.minor
;
705 if (sysfs_add_disk(sra
, &new_mdi
) != 0) {
706 fprintf(stderr
, Name
": add new device to external metadata"
707 " failed for %s\n", dv
->devname
);
711 ping_monitor(devnum2devname(devnum
));
714 } else if (ioctl(fd
, ADD_NEW_DISK
, &disc
)) {
715 fprintf(stderr
, Name
": add new device failed for %s as %d: %s\n",
716 dv
->devname
, j
, strerror(errno
));
720 fprintf(stderr
, Name
": added %s\n", dv
->devname
);
725 if (tst
->subarray
[0]) {
726 fprintf(stderr
, Name
": Cannot remove disks from a"
727 " \'member\' array, perform this"
728 " operation on the parent container\n");
731 if (tst
->ss
->external
) {
732 /* To remove a device from a container, we must
733 * check that it isn't in use in an array.
734 * This involves looking in the 'holders'
735 * directory - there must be just one entry,
737 * To ensure that it doesn't get used as a
738 * hold spare while we are checking, we
739 * get an O_EXCL open on the container
741 int dnum
= fd2devnum(fd
);
742 lfd
= open_dev_excl(dnum
);
745 ": Cannot get exclusive access "
746 " to container - odd\n");
749 if (!sysfs_unique_holder(dnum
, stb
.st_rdev
)) {
751 ": %s is %s, cannot remove.\n",
753 errno
== EEXIST
? "still in use":
759 /* FIXME check that it is a current member */
760 err
= ioctl(fd
, HOT_REMOVE_DISK
, (unsigned long)stb
.st_rdev
);
761 if (err
&& errno
== ENODEV
) {
762 /* Old kernels rejected this if no personality
764 struct mdinfo
*sra
= sysfs_read(fd
, 0, GET_DEVS
);
765 struct mdinfo
*dv
= NULL
;
768 for ( ; dv
; dv
=dv
->next
)
769 if (dv
->disk
.major
== major(stb
.st_rdev
) &&
770 dv
->disk
.minor
== minor(stb
.st_rdev
))
773 err
= sysfs_set_str(sra
, dv
,
781 fprintf(stderr
, Name
": hot remove failed "
782 "for %s: %s\n", dnprintable
,
788 if (tst
->ss
->external
) {
790 * Before dropping our exclusive open we make an
791 * attempt at preventing mdmon from seeing an
792 * 'add' event before reconciling this 'remove'
795 char *name
= devnum2devname(fd2devnum(fd
));
798 fprintf(stderr
, Name
": unable to get container name\n");
807 fprintf(stderr
, Name
": hot removed %s\n",
811 case 'f': /* set faulty */
812 /* FIXME check current member */
813 if (ioctl(fd
, SET_DISK_FAULTY
, (unsigned long) stb
.st_rdev
)) {
814 fprintf(stderr
, Name
": set device faulty failed for %s: %s\n",
815 dnprintable
, strerror(errno
));
819 fprintf(stderr
, Name
": set %s faulty in %s\n",
820 dnprintable
, devname
);
830 /* Open any md device, and issue the RAID_AUTORUN ioctl */
832 int fd
= dev_open("9:0", O_RDONLY
);
834 if (ioctl(fd
, RAID_AUTORUN
, 0) == 0)