]>
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 unsigned long long rd1
, rd2
;
212 if (will_retry
&& verbose
== 0)
215 if (md_get_version(fd
) < 9000) {
216 if (ioctl(fd
, STOP_MD
, 0) == 0)
218 pr_err("stopping device %s "
220 devname
, strerror(errno
));
224 /* If this is an mdmon managed array, just write 'inactive'
225 * to the array state and let mdmon clear up.
227 strcpy(devnm
, fd2devnm(fd
));
228 /* Get EXCL access first. If this fails, then attempting
229 * to stop is probably a bad idea.
231 mdi
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_COMPONENT
|GET_VERSION
);
232 if (mdi
&& is_subarray(mdi
->text_version
)) {
234 strncpy(container
, mdi
->text_version
+1, sizeof(container
));
235 container
[sizeof(container
)-1] = 0;
236 sl
= strchr(container
, '/');
243 while (((fd
= ((devnm
[0] == '/')
244 ?open(devname
, O_RDONLY
|O_EXCL
)
245 :open_dev_flags(devnm
, O_RDONLY
|O_EXCL
))) < 0
246 || strcmp(fd2devnm(fd
), devnm
) != 0)
248 && mdmon_running(container
)
252 flush_mdmon(container
);
255 if (fd
< 0 || strcmp(fd2devnm(fd
), devnm
) != 0) {
259 pr_err("Cannot get exclusive access to %s:"
261 "process, mounted filesystem "
262 "or active volume group?\n",
267 mdi
->array
.level
> 0 &&
268 is_subarray(mdi
->text_version
)) {
270 /* This is mdmon managed. */
273 /* As we have an O_EXCL open, any use of the device
274 * which blocks STOP_ARRAY is probably a transient use,
275 * so it is reasonable to retry for a while - 5 seconds.
279 (err
= sysfs_set_str(mdi
, NULL
,
288 pr_err("failed to stop array %s: %s\n",
289 devname
, strerror(errno
));
294 /* Give monitor a chance to act */
295 ping_monitor(mdi
->text_version
);
297 fd
= open_dev_excl(devnm
);
300 pr_err("failed to completely stop %s"
301 ": Device is busy\n",
307 mdi
->array
.major_version
== -1 &&
308 mdi
->array
.minor_version
== -2 &&
309 !is_subarray(mdi
->text_version
)) {
310 struct mdstat_ent
*mds
, *m
;
311 /* container, possibly mdmon-managed.
312 * Make sure mdmon isn't opening it, which
313 * would interfere with the 'stop'
315 ping_monitor(mdi
->sys_name
);
317 /* now check that there are no existing arrays
318 * which are members of this array
320 mds
= mdstat_read(0, 0);
321 for (m
= mds
; m
; m
= m
->next
)
322 if (m
->metadata_version
&&
323 strncmp(m
->metadata_version
, "external:", 9)==0 &&
324 metadata_container_matches(m
->metadata_version
+9,
327 pr_err("Cannot stop container %s: "
328 "member %s still active\n",
336 /* If the array is undergoing a reshape which changes the number
337 * of devices, then it would be nice to stop it at a point where
338 * it has completed a full number of stripes in both old and
339 * new layouts as this will allow the reshape to be reverted.
340 * So if 'sync_action' is "reshape" and 'raid_disks' shows two
341 * different numbers, then
343 * - set sync_max to next multiple of both data_disks and
344 * chunk sizes (or next but one)
346 * - wait on 'sync_completed' for that point to be reached.
348 if (mdi
&& (mdi
->array
.level
>= 4 && mdi
->array
.level
<= 6) &&
349 sysfs_attribute_available(mdi
, NULL
, "sync_action") &&
350 sysfs_attribute_available(mdi
, NULL
, "reshape_direction") &&
351 sysfs_get_str(mdi
, NULL
, "sync_action", buf
, 20) > 0 &&
352 strcmp(buf
, "reshape\n") == 0 &&
353 sysfs_get_two(mdi
, NULL
, "raid_disks", &rd1
, &rd2
) == 2 &&
354 sysfs_set_str(mdi
, NULL
, "sync_action", "frozen") == 0) {
355 /* Array is frozen */
356 unsigned long long position
, curr
;
357 unsigned long long chunk1
, chunk2
;
358 unsigned long long rddiv
, chunkdiv
;
359 unsigned long long sectors
;
360 unsigned long long sync_max
, old_sync_max
;
361 unsigned long long completed
;
366 rd1
-= mdi
->array
.level
== 6 ? 2 : 1;
367 rd2
-= mdi
->array
.level
== 6 ? 2 : 1;
368 sysfs_get_str(mdi
, NULL
, "reshape_direction", buf
, sizeof(buf
));
369 if (strncmp(buf
, "back", 4) == 0)
371 sysfs_get_ll(mdi
, NULL
, "reshape_position", &position
);
372 sysfs_get_two(mdi
, NULL
, "chunk_size", &chunk1
, &chunk2
);
375 rddiv
= GCD(rd1
, rd2
);
376 chunkdiv
= GCD(chunk1
, chunk2
);
377 sectors
= (chunk1
/chunkdiv
) * chunk2
* (rd1
/rddiv
) * rd2
;
380 /* Need to subtract 'reshape_position' from
381 * array size to get equivalent of sync_max.
382 * Size calculation based on raid5_size in kernel.
384 unsigned long long size
= mdi
->component_size
;
387 /* rd1 must be smaller */
388 position
= (position
/ sectors
- 1) * sectors
;
389 sync_max
= size
- position
/rd1
;
391 position
= (position
/ sectors
+ 2) * sectors
;
392 sync_max
= position
/rd1
;
394 if (sysfs_get_ll(mdi
, NULL
, "sync_max", &old_sync_max
) < 0)
395 old_sync_max
= mdi
->component_size
;
396 /* Must not advance sync_max as that could confuse
397 * the reshape monitor */
398 if (sync_max
< old_sync_max
)
399 sysfs_set_num(mdi
, NULL
, "sync_max", sync_max
);
400 sysfs_set_str(mdi
, NULL
, "sync_action", "idle");
402 /* That should have set things going again. Now we
403 * wait a little while (1 second max) for sync_completed
404 * to reach the target.
407 scfd
= sysfs_open(mdi
->sys_name
, NULL
, "sync_completed");
408 while (scfd
>= 0 && delay
> 0) {
409 sysfs_get_ll(mdi
, NULL
, "reshape_position", &curr
);
410 sysfs_fd_get_str(scfd
, buf
, sizeof(buf
));
411 if (strncmp(buf
, "none", 4) == 0)
414 if (sysfs_fd_get_ll(scfd
, &completed
) == 0 &&
415 (completed
> sync_max
||
416 (completed
== sync_max
&& curr
!= position
))) {
417 while (completed
> sync_max
) {
418 sync_max
+= sectors
/ rd1
;
424 if (sync_max
< old_sync_max
)
425 sysfs_set_num(mdi
, NULL
, "sync_max", sync_max
);
428 if (!backwards
&& curr
>= position
)
430 if (backwards
&& curr
<= position
)
432 sysfs_wait(scfd
, &delay
);
439 /* As we have an O_EXCL open, any use of the device
440 * which blocks STOP_ARRAY is probably a transient use,
441 * so it is reasonable to retry for a while - 5 seconds.
444 while (count
&& fd
>= 0
445 && (err
= ioctl(fd
, STOP_ARRAY
, NULL
)) < 0
450 if (fd
>= 0 && err
) {
452 pr_err("failed to stop array %s: %s\n",
453 devname
, strerror(errno
));
455 cont_err("Perhaps a running "
456 "process, mounted filesystem "
457 "or active volume group?\n");
462 /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
463 * was stopped, so We'll do it here just to be sure. Drop any
464 * partitions as well...
467 ioctl(fd
, BLKRRPART
, 0);
469 sysfs_uevent(mdi
, "change");
471 if (devnm
[0] && use_udev()) {
472 struct map_ent
*mp
= map_by_devnm(&map
, devnm
);
473 remove_devices(devnm
, mp
? mp
->path
: NULL
);
477 pr_err("stopped %s\n", devname
);
479 map_remove(&map
, devnm
);
488 static struct mddev_dev
*add_one(struct mddev_dev
*dv
, char *name
, char disp
)
490 struct mddev_dev
*new;
491 new = xmalloc(sizeof(*new));
492 memset(new, 0, sizeof(*new));
493 new->devname
= xstrdup(name
);
494 new->disposition
= disp
;
495 new->next
= dv
->next
;
500 static void add_faulty(struct mddev_dev
*dv
, int fd
, char disp
)
502 mdu_array_info_t array
;
503 mdu_disk_info_t disk
;
507 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0)
510 remaining_disks
= array
.nr_disks
;
511 for (i
= 0; i
< MAX_DISKS
&& remaining_disks
> 0; i
++) {
514 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
516 if (disk
.major
== 0 && disk
.minor
== 0)
519 if ((disk
.state
& 1) == 0) /* not faulty */
521 sprintf(buf
, "%d:%d", disk
.major
, disk
.minor
);
522 dv
= add_one(dv
, buf
, disp
);
526 static void add_detached(struct mddev_dev
*dv
, int fd
, char disp
)
528 mdu_array_info_t array
;
529 mdu_disk_info_t disk
;
533 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0)
536 remaining_disks
= array
.nr_disks
;
537 for (i
= 0; i
< MAX_DISKS
&& remaining_disks
> 0; i
++) {
541 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
543 if (disk
.major
== 0 && disk
.minor
== 0)
546 if (disp
== 'f' && (disk
.state
& 1) != 0) /* already faulty */
548 sprintf(buf
, "%d:%d", disk
.major
, disk
.minor
);
549 sfd
= dev_open(buf
, O_RDONLY
);
556 /* Probably not detached */
558 dv
= add_one(dv
, buf
, disp
);
562 static void add_set(struct mddev_dev
*dv
, int fd
, char set_char
)
564 mdu_array_info_t array
;
565 mdu_disk_info_t disk
;
570 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0)
572 if (array
.level
!= 10)
574 copies
= ((array
.layout
& 0xff) *
575 ((array
.layout
>> 8) & 0xff));
576 if (array
.raid_disks
% copies
)
579 remaining_disks
= array
.nr_disks
;
580 for (i
= 0; i
< MAX_DISKS
&& remaining_disks
> 0; i
++) {
583 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
585 if (disk
.major
== 0 && disk
.minor
== 0)
588 set
= disk
.raid_disk
% copies
;
589 if (set_char
!= set
+ 'A')
591 sprintf(buf
, "%d:%d", disk
.major
, disk
.minor
);
592 dv
= add_one(dv
, buf
, dv
->disposition
);
596 int attempt_re_add(int fd
, int tfd
, struct mddev_dev
*dv
,
597 struct supertype
*dev_st
, struct supertype
*tst
,
599 char *update
, char *devname
, int verbose
,
600 mdu_array_info_t
*array
)
606 dev_st
->ss
->getinfo_super(dev_st
, &mdi
, NULL
);
607 dev_st
->ss
->uuid_from_super(dev_st
, ouuid
);
609 tst
->ss
->uuid_from_super(tst
, duuid
);
611 /* Assume uuid matches: kernel will check */
612 memcpy(duuid
, ouuid
, sizeof(ouuid
));
613 if ((mdi
.disk
.state
& (1<<MD_DISK_ACTIVE
)) &&
614 !(mdi
.disk
.state
& (1<<MD_DISK_FAULTY
)) &&
615 memcmp(duuid
, ouuid
, sizeof(ouuid
))==0) {
616 /* Looks like it is worth a
617 * try. Need to make sure
618 * kernel will accept it
621 mdu_disk_info_t disc
;
622 /* re-add doesn't work for version-1 superblocks
625 if (array
->major_version
== 1 &&
626 get_linux_version() <= 2006018)
628 disc
.number
= mdi
.disk
.number
;
629 if (ioctl(fd
, GET_DISK_INFO
, &disc
) != 0
630 || disc
.major
!= 0 || disc
.minor
!= 0
633 disc
.major
= major(rdev
);
634 disc
.minor
= minor(rdev
);
635 disc
.number
= mdi
.disk
.number
;
636 disc
.raid_disk
= mdi
.disk
.raid_disk
;
637 disc
.state
= mdi
.disk
.state
;
638 if (dv
->writemostly
== 1)
639 disc
.state
|= 1 << MD_DISK_WRITEMOSTLY
;
640 if (dv
->writemostly
== 2)
641 disc
.state
&= ~(1 << MD_DISK_WRITEMOSTLY
);
642 remove_partitions(tfd
);
643 if (update
|| dv
->writemostly
> 0) {
645 tfd
= dev_open(dv
->devname
, O_RDWR
);
647 pr_err("failed to open %s for"
648 " superblock update during re-add\n", dv
->devname
);
652 if (dv
->writemostly
== 1)
653 rv
= dev_st
->ss
->update_super(
654 dev_st
, NULL
, "writemostly",
655 devname
, verbose
, 0, NULL
);
656 if (dv
->writemostly
== 2)
657 rv
= dev_st
->ss
->update_super(
658 dev_st
, NULL
, "readwrite",
659 devname
, verbose
, 0, NULL
);
661 rv
= dev_st
->ss
->update_super(
662 dev_st
, NULL
, update
,
663 devname
, verbose
, 0, NULL
);
665 rv
= dev_st
->ss
->store_super(dev_st
, tfd
);
668 pr_err("failed to update"
669 " superblock during re-add\n");
673 /* don't even try if disk is marked as faulty */
675 if (ioctl(fd
, ADD_NEW_DISK
, &disc
) == 0) {
677 pr_err("re-added %s\n", dv
->devname
);
680 if (errno
== ENOMEM
|| errno
== EROFS
) {
681 pr_err("add new device failed for %s: %s\n",
682 dv
->devname
, strerror(errno
));
683 if (dv
->disposition
== 'M')
692 int Manage_add(int fd
, int tfd
, struct mddev_dev
*dv
,
693 struct supertype
*tst
, mdu_array_info_t
*array
,
694 int force
, int verbose
, char *devname
,
695 char *update
, unsigned long rdev
, unsigned long long array_size
)
697 unsigned long long ldsize
;
698 struct supertype
*dev_st
= NULL
;
700 mdu_disk_info_t disc
;
702 if (!get_dev_size(tfd
, dv
->devname
, &ldsize
)) {
703 if (dv
->disposition
== 'M')
709 if (tst
->ss
->validate_geometry(
710 tst
, array
->level
, array
->layout
,
711 array
->raid_disks
, NULL
,
712 ldsize
>> 9, INVALID_SECTORS
, NULL
, NULL
, 0) == 0) {
714 pr_err("%s is larger than %s can "
716 " Add --force is you "
717 "really want to add this device.\n",
718 dv
->devname
, devname
);
721 pr_err("%s is larger than %s can "
723 " Adding anyway as --force "
725 dv
->devname
, devname
);
727 if (!tst
->ss
->external
&&
728 array
->major_version
== 0 &&
729 md_get_version(fd
)%100 < 2) {
730 if (ioctl(fd
, HOT_ADD_DISK
, rdev
)==0) {
732 pr_err("hot added %s\n",
737 pr_err("hot add failed for %s: %s\n",
738 dv
->devname
, strerror(errno
));
742 if (array
->not_persistent
== 0 || tst
->ss
->external
) {
744 /* need to find a sample superblock to copy, and
745 * a spare slot to use.
746 * For 'external' array (well, container based),
747 * We can just load the metadata for the array->
751 /* already loaded */;
752 else if (tst
->ss
->external
) {
753 tst
->ss
->load_container(tst
, fd
, NULL
);
754 } else for (j
= 0; j
< tst
->max_devs
; j
++) {
758 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
760 if (disc
.major
==0 && disc
.minor
==0)
762 if ((disc
.state
& 4)==0) /* sync */
764 /* Looks like a good device to try */
765 dev
= map_dev(disc
.major
, disc
.minor
, 1);
768 dfd
= dev_open(dev
, O_RDONLY
);
771 if (tst
->ss
->load_super(tst
, dfd
,
779 /* FIXME this is a bad test to be using */
780 if (!tst
->sb
&& dv
->disposition
!= 'a') {
781 /* we are re-adding a device to a
782 * completely dead array - have to depend
785 } else if (!tst
->sb
) {
786 pr_err("cannot load array metadata from %s\n", devname
);
790 /* Make sure device is large enough */
791 if (tst
->ss
->avail_size(tst
, ldsize
/512, INVALID_SECTORS
) <
793 if (dv
->disposition
== 'M')
795 pr_err("%s not large enough to join array\n",
800 /* Possibly this device was recently part of
801 * the array and was temporarily removed, and
802 * is now being re-added. If so, we can
806 if (array
->not_persistent
==0) {
807 dev_st
= dup_super(tst
);
808 dev_st
->ss
->load_super(dev_st
, tfd
, NULL
);
810 if (dev_st
&& dev_st
->sb
) {
811 int rv
= attempt_re_add(fd
, tfd
, dv
,
817 dev_st
->ss
->free_super(dev_st
);
821 if (dv
->disposition
== 'M') {
823 pr_err("--re-add for %s to %s is not possible\n",
824 dv
->devname
, devname
);
827 if (dv
->disposition
== 'A') {
828 pr_err("--re-add for %s to %s is not possible\n",
829 dv
->devname
, devname
);
832 if (array
->active_disks
< array
->raid_disks
) {
833 char *avail
= xcalloc(array
->raid_disks
, 1);
837 for (d
= 0; d
< MAX_DISKS
&& found
< array
->active_disks
; d
++) {
839 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
841 if (disc
.major
== 0 && disc
.minor
== 0)
843 if (!(disc
.state
& (1<<MD_DISK_SYNC
)))
845 avail
[disc
.raid_disk
] = 1;
848 array_failed
= !enough(array
->level
, array
->raid_disks
,
849 array
->layout
, 1, avail
);
853 pr_err("%s has failed so using --add cannot work and might destroy\n",
855 pr_err("data on %s. You should stop the array and re-assemble it.\n",
860 /* non-persistent. Must ensure that new drive
861 * is at least array->size big.
863 if (ldsize
/512 < array_size
) {
864 pr_err("%s not large enough to join array\n",
869 /* committed to really trying this device now*/
870 remove_partitions(tfd
);
872 /* in 2.6.17 and earlier, version-1 superblocks won't
873 * use the number we write, but will choose a free number.
874 * we must choose the same free number, which requires
875 * starting at 'raid_disks' and counting up
877 for (j
= array
->raid_disks
; j
< tst
->max_devs
; j
++) {
879 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
881 if (disc
.major
==0 && disc
.minor
==0)
883 if (disc
.state
& 8) /* removed */
886 disc
.major
= major(rdev
);
887 disc
.minor
= minor(rdev
);
890 if (array
->not_persistent
==0) {
892 if (dv
->writemostly
== 1)
893 disc
.state
|= 1 << MD_DISK_WRITEMOSTLY
;
894 dfd
= dev_open(dv
->devname
, O_RDWR
| O_EXCL
|O_DIRECT
);
895 if (tst
->ss
->add_to_super(tst
, &disc
, dfd
,
896 dv
->devname
, INVALID_SECTORS
))
898 if (tst
->ss
->write_init_super(tst
))
900 } else if (dv
->disposition
== 'A') {
901 /* this had better be raid1.
902 * As we are "--re-add"ing we must find a spare slot
905 char *used
= xcalloc(array
->raid_disks
, 1);
906 for (j
= 0; j
< tst
->max_devs
; j
++) {
907 mdu_disk_info_t disc2
;
909 if (ioctl(fd
, GET_DISK_INFO
, &disc2
))
911 if (disc2
.major
==0 && disc2
.minor
==0)
913 if (disc2
.state
& 8) /* removed */
915 if (disc2
.raid_disk
< 0)
917 if (disc2
.raid_disk
> array
->raid_disks
)
919 used
[disc2
.raid_disk
] = 1;
921 for (j
= 0 ; j
< array
->raid_disks
; j
++)
924 disc
.state
|= (1<<MD_DISK_SYNC
);
929 if (dv
->writemostly
== 1)
930 disc
.state
|= (1 << MD_DISK_WRITEMOSTLY
);
931 if (tst
->ss
->external
) {
933 * to an external metadata container */
934 struct mdinfo new_mdi
;
940 strcpy(devnm
, fd2devnm(fd
));
942 container_fd
= open_dev_excl(devnm
);
943 if (container_fd
< 0) {
944 pr_err("add failed for %s:"
945 " could not get exclusive access to container\n",
947 tst
->ss
->free_super(tst
);
951 Kill(dv
->devname
, NULL
, 0, -1, 0);
952 dfd
= dev_open(dv
->devname
, O_RDWR
| O_EXCL
|O_DIRECT
);
953 if (mdmon_running(tst
->container_devnm
))
954 tst
->update_tail
= &tst
->updates
;
955 if (tst
->ss
->add_to_super(tst
, &disc
, dfd
,
956 dv
->devname
, INVALID_SECTORS
)) {
961 if (tst
->update_tail
)
962 flush_metadata_updates(tst
);
964 tst
->ss
->sync_metadata(tst
);
966 sra
= sysfs_read(container_fd
, NULL
, 0);
968 pr_err("add failed for %s: sysfs_read failed\n",
971 tst
->ss
->free_super(tst
);
974 sra
->array
.level
= LEVEL_CONTAINER
;
975 /* Need to set data_offset and component_size */
976 tst
->ss
->getinfo_super(tst
, &new_mdi
, NULL
);
977 new_mdi
.disk
.major
= disc
.major
;
978 new_mdi
.disk
.minor
= disc
.minor
;
979 new_mdi
.recovery_start
= 0;
980 /* Make sure fds are closed as they are O_EXCL which
981 * would block add_disk */
982 tst
->ss
->free_super(tst
);
983 if (sysfs_add_disk(sra
, &new_mdi
, 0) != 0) {
984 pr_err("add new device to external metadata"
985 " failed for %s\n", dv
->devname
);
994 tst
->ss
->free_super(tst
);
995 if (ioctl(fd
, ADD_NEW_DISK
, &disc
)) {
996 pr_err("add new device failed for %s as %d: %s\n",
997 dv
->devname
, j
, strerror(errno
));
1002 pr_err("added %s\n", dv
->devname
);
1006 int Manage_remove(struct supertype
*tst
, int fd
, struct mddev_dev
*dv
,
1007 int sysfd
, unsigned long rdev
, int verbose
, char *devname
)
1012 if (tst
->ss
->external
) {
1013 /* To remove a device from a container, we must
1014 * check that it isn't in use in an array.
1015 * This involves looking in the 'holders'
1016 * directory - there must be just one entry,
1018 * To ensure that it doesn't get used as a
1019 * hot spare while we are checking, we
1020 * get an O_EXCL open on the container
1024 strcpy(devnm
, fd2devnm(fd
));
1025 lfd
= open_dev_excl(devnm
);
1027 pr_err("Cannot get exclusive access "
1028 " to container - odd\n");
1031 /* We may not be able to check on holders in
1032 * sysfs, either because we don't have the dev num
1033 * (rdev == 0) or because the device has been detached
1034 * and the 'holders' directory no longer exists
1035 * (ret == -1). In that case, assume it is OK to
1041 ret
= sysfs_unique_holder(devnm
, rdev
);
1043 pr_err("%s is not a member, cannot remove.\n",
1049 pr_err("%s is still in use, cannot remove.\n",
1055 /* FIXME check that it is a current member */
1057 /* device has been removed and we don't know
1058 * the major:minor number
1060 int n
= write(sysfd
, "remove", 6);
1066 err
= ioctl(fd
, HOT_REMOVE_DISK
, rdev
);
1067 if (err
&& errno
== ENODEV
) {
1068 /* Old kernels rejected this if no personality
1070 struct mdinfo
*sra
= sysfs_read(fd
, NULL
, GET_DEVS
);
1071 struct mdinfo
*dv
= NULL
;
1074 for ( ; dv
; dv
=dv
->next
)
1075 if (dv
->disk
.major
== (int)major(rdev
) &&
1076 dv
->disk
.minor
== (int)minor(rdev
))
1079 err
= sysfs_set_str(sra
, dv
,
1088 pr_err("hot remove failed "
1089 "for %s: %s\n", dv
->devname
,
1095 if (tst
->ss
->external
) {
1097 * Before dropping our exclusive open we make an
1098 * attempt at preventing mdmon from seeing an
1099 * 'add' event before reconciling this 'remove'
1102 char *devnm
= fd2devnm(fd
);
1105 pr_err("unable to get container name\n");
1109 ping_manager(devnm
);
1114 pr_err("hot removed %s from %s\n",
1115 dv
->devname
, devname
);
1119 int Manage_replace(struct supertype
*tst
, int fd
, struct mddev_dev
*dv
,
1120 unsigned long rdev
, int verbose
, char *devname
)
1122 struct mdinfo
*mdi
, *di
;
1123 if (tst
->ss
->external
) {
1124 pr_err("--replace only supported for native metadata (0.90 or 1.x)\n");
1127 /* Need to find the device in sysfs and add 'want_replacement' to the
1130 mdi
= sysfs_read(fd
, NULL
, GET_DEVS
);
1131 if (!mdi
|| !mdi
->devs
) {
1132 pr_err("Cannot find status of %s to enable replacement - strange\n",
1136 for (di
= mdi
->devs
; di
; di
= di
->next
)
1137 if (di
->disk
.major
== (int)major(rdev
) &&
1138 di
->disk
.minor
== (int)minor(rdev
))
1142 if (di
->disk
.raid_disk
< 0) {
1143 pr_err("%s is not active and so cannot be replaced.\n",
1148 rv
= sysfs_set_str(mdi
, di
,
1149 "state", "want_replacement");
1152 pr_err("Failed to request replacement for %s\n",
1157 pr_err("Marked %s (device %d in %s) for replacement\n",
1158 dv
->devname
, di
->disk
.raid_disk
, devname
);
1159 /* If there is a matching 'with', we need to tell it which
1162 while (dv
&& dv
->disposition
!= 'W')
1165 dv
->disposition
= 'w';
1166 dv
->used
= di
->disk
.raid_disk
;
1171 pr_err("%s not found in %s so cannot --replace it\n",
1172 dv
->devname
, devname
);
1176 int Manage_with(struct supertype
*tst
, int fd
, struct mddev_dev
*dv
,
1177 unsigned long rdev
, int verbose
, char *devname
)
1179 struct mdinfo
*mdi
, *di
;
1180 /* try to set 'slot' for 'rdev' in 'fd' to 'dv->used' */
1181 mdi
= sysfs_read(fd
, NULL
, GET_DEVS
|GET_STATE
);
1182 if (!mdi
|| !mdi
->devs
) {
1183 pr_err("Cannot find status of %s to enable replacement - strange\n",
1187 for (di
= mdi
->devs
; di
; di
= di
->next
)
1188 if (di
->disk
.major
== (int)major(rdev
) &&
1189 di
->disk
.minor
== (int)minor(rdev
))
1193 if (di
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
1194 pr_err("%s is faulty and cannot be a replacement\n",
1199 if (di
->disk
.raid_disk
>= 0) {
1200 pr_err("%s is active and cannot be a replacement\n",
1205 rv
= sysfs_set_num(mdi
, di
,
1209 pr_err("Failed to %s as preferred replacement.\n",
1214 pr_err("Marked %s in %s as replacement for device %d\n",
1215 dv
->devname
, devname
, dv
->used
);
1219 pr_err("%s not found in %s so cannot make it preferred replacement\n",
1220 dv
->devname
, devname
);
1224 int Manage_subdevs(char *devname
, int fd
,
1225 struct mddev_dev
*devlist
, int verbose
, int test
,
1226 char *update
, int force
)
1228 /* Do something to each dev.
1230 * 'a' - add the device
1232 * If that fails EINVAL, try ADD_NEW_DISK
1233 * 'A' - re-add the device
1234 * 'r' - remove the device: HOT_REMOVE_DISK
1235 * device can be 'faulty' or 'detached' in which case all
1236 * matching devices are removed.
1237 * 'f' - set the device faulty SET_DISK_FAULTY
1238 * device can be 'detached' in which case any device that
1239 * is inaccessible will be marked faulty.
1240 * 'R' - mark this device as wanting replacement.
1241 * 'W' - this device is added if necessary and activated as
1242 * a replacement for a previous 'R' device.
1244 * 'w' - 'W' will be changed to 'w' when it is paired with
1245 * a 'R' device. If a 'W' is found while walking the list
1246 * it must be unpaired, and is an error.
1247 * 'M' - this is created by a 'missing' target. It is a slight
1249 * 'F' - Another variant of 'A', where the device was faulty
1250 * so must be removed from the array first.
1252 * For 'f' and 'r', the device can also be a kernel-internal
1253 * name such as 'sdb'.
1255 mdu_array_info_t array
;
1256 unsigned long long array_size
;
1257 struct mddev_dev
*dv
;
1260 struct supertype
*tst
;
1261 char *subarray
= NULL
;
1263 int count
= 0; /* number of actions taken */
1268 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)) {
1269 pr_err("Cannot get array info for %s\n",
1273 sysfs_init(&info
, fd
, NULL
);
1275 /* array.size is only 32 bits and may be truncated.
1276 * So read from sysfs if possible, and record number of sectors
1279 array_size
= get_component_size(fd
);
1280 if (array_size
<= 0)
1281 array_size
= array
.size
* 2;
1283 tst
= super_by_fd(fd
, &subarray
);
1285 pr_err("unsupport array - version %d.%d\n",
1286 array
.major_version
, array
.minor_version
);
1291 for (dv
= devlist
; dv
; dv
= dv
->next
) {
1294 if (strcmp(dv
->devname
, "failed") == 0 ||
1295 strcmp(dv
->devname
, "faulty") == 0) {
1296 if (dv
->disposition
!= 'A'
1297 && dv
->disposition
!= 'r') {
1298 pr_err("%s only meaningful "
1299 "with -r or --re-add, not -%c\n",
1300 dv
->devname
, dv
->disposition
);
1303 add_faulty(dv
, fd
, (dv
->disposition
== 'A'
1307 if (strcmp(dv
->devname
, "detached") == 0) {
1308 if (dv
->disposition
!= 'r' && dv
->disposition
!= 'f') {
1309 pr_err("%s only meaningful "
1310 "with -r of -f, not -%c\n",
1311 dv
->devname
, dv
->disposition
);
1314 add_detached(dv
, fd
, dv
->disposition
);
1318 if (strcmp(dv
->devname
, "missing") == 0) {
1319 struct mddev_dev
*add_devlist
= NULL
;
1320 struct mddev_dev
**dp
;
1321 if (dv
->disposition
!= 'A') {
1322 pr_err("'missing' only meaningful "
1326 add_devlist
= conf_get_devs();
1327 if (add_devlist
== NULL
) {
1328 pr_err("no devices to scan for missing members.");
1331 for (dp
= &add_devlist
; *dp
; dp
= & (*dp
)->next
)
1332 /* 'M' (for 'missing') is like 'A' without errors */
1333 (*dp
)->disposition
= 'M';
1335 dv
->next
= add_devlist
;
1339 if (strncmp(dv
->devname
, "set-", 4) == 0 &&
1340 strlen(dv
->devname
) == 5) {
1343 if (dv
->disposition
!= 'r' &&
1344 dv
->disposition
!= 'f') {
1345 pr_err("'%s' only meaningful with -r or -f\n",
1349 if (array
.level
!= 10) {
1350 pr_err("'%s' only meaningful with RAID10 arrays\n",
1354 copies
= ((array
.layout
& 0xff) *
1355 ((array
.layout
>> 8) & 0xff));
1356 if (array
.raid_disks
% copies
!= 0 ||
1357 dv
->devname
[4] < 'A' ||
1358 dv
->devname
[4] >= 'A' + copies
||
1360 pr_err("'%s' not meaningful with this array\n",
1364 add_set(dv
, fd
, dv
->devname
[4]);
1368 if (strchr(dv
->devname
, '/') == NULL
&&
1369 strchr(dv
->devname
, ':') == NULL
&&
1370 strlen(dv
->devname
) < 50) {
1371 /* Assume this is a kernel-internal name like 'sda1' */
1374 if (dv
->disposition
!= 'r' && dv
->disposition
!= 'f') {
1375 pr_err("%s only meaningful "
1376 "with -r or -f, not -%c\n",
1377 dv
->devname
, dv
->disposition
);
1381 sprintf(dname
, "dev-%s", dv
->devname
);
1382 sysfd
= sysfs_open(fd2devnm(fd
), dname
, "block/dev");
1386 if (sysfs_fd_get_str(sysfd
, dn
, 20) > 0 &&
1387 sscanf(dn
, "%d:%d", &mj
,&mn
) == 2) {
1388 stb
.st_rdev
= makedev(mj
,mn
);
1395 sysfd
= sysfs_open(fd2devnm(fd
), dname
, "state");
1397 pr_err("%s does not appear "
1398 "to be a component of %s\n",
1399 dv
->devname
, devname
);
1404 tfd
= dev_open(dv
->devname
, O_RDONLY
);
1408 int open_err
= errno
;
1409 if (stat(dv
->devname
, &stb
) != 0) {
1410 pr_err("Cannot find %s: %s\n",
1411 dv
->devname
, strerror(errno
));
1414 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
1415 if (dv
->disposition
== 'M')
1416 /* non-fatal. Also improbable */
1418 pr_err("%s is not a block device.\n",
1422 if (dv
->disposition
== 'r')
1423 /* Be happy, the stat worked, that is
1424 * enough for --remove
1428 if (dv
->disposition
== 'M')
1431 pr_err("Cannot open %s: %s\n",
1432 dv
->devname
, strerror(open_err
));
1437 switch(dv
->disposition
){
1439 pr_err("internal error - devmode[%s]=%d\n",
1440 dv
->devname
, dv
->disposition
);
1444 case 'M': /* --re-add missing */
1445 case 'F': /* --re-add faulty */
1446 /* add the device */
1448 pr_err("Cannot add disks to a"
1449 " \'member\' array, perform this"
1450 " operation on the parent container\n");
1453 if (dv
->disposition
== 'F')
1454 /* Need to remove first */
1455 ioctl(fd
, HOT_REMOVE_DISK
,
1456 (unsigned long)stb
.st_rdev
);
1457 /* Make sure it isn't in use (in 2.6 or later) */
1458 tfd
= dev_open(dv
->devname
, O_RDONLY
|O_EXCL
);
1460 /* We know no-one else is using it. We'll
1461 * need non-exclusive access to add it, so
1465 tfd
= dev_open(dv
->devname
, O_RDONLY
);
1468 if (dv
->disposition
== 'M')
1470 pr_err("Cannot open %s: %s\n",
1471 dv
->devname
, strerror(errno
));
1475 if (sysfs_freeze_array(&info
) == 1)
1480 rv
= Manage_add(fd
, tfd
, dv
, tst
, &array
,
1481 force
, verbose
, devname
, update
,
1482 stb
.st_rdev
, array_size
);
1494 pr_err("Cannot remove disks from a"
1495 " \'member\' array, perform this"
1496 " operation on the parent container\n");
1499 rv
= Manage_remove(tst
, fd
, dv
, sysfd
,
1500 stb
.st_rdev
, verbose
,
1511 case 'f': /* set faulty */
1512 /* FIXME check current member */
1513 if ((sysfd
>= 0 && write(sysfd
, "faulty", 6) != 6) ||
1514 (sysfd
< 0 && ioctl(fd
, SET_DISK_FAULTY
,
1515 (unsigned long) stb
.st_rdev
))) {
1518 pr_err("set device faulty failed for %s: %s\n",
1519 dv
->devname
, strerror(errno
));
1529 pr_err("set %s faulty in %s\n",
1530 dv
->devname
, devname
);
1532 case 'R': /* Mark as replaceable */
1534 pr_err("Cannot replace disks in a"
1535 " \'member\' array, perform this"
1536 " operation on the parent container\n");
1540 if (sysfs_freeze_array(&info
) == 1)
1545 rv
= Manage_replace(tst
, fd
, dv
,
1546 stb
.st_rdev
, verbose
,
1554 case 'W': /* --with device that doesn't match */
1555 pr_err("No matching --replace device for --with %s\n",
1558 case 'w': /* --with device which was matched */
1559 rv
= Manage_with(tst
, fd
, dv
,
1560 stb
.st_rdev
, verbose
, devname
);
1567 sysfs_set_str(&info
, NULL
, "sync_action","idle");
1568 if (test
&& count
== 0)
1574 sysfs_set_str(&info
, NULL
, "sync_action","idle");
1575 return !test
&& busy
? 2 : 1;
1578 int autodetect(void)
1580 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1582 int fd
= dev_open("9:0", O_RDONLY
);
1584 if (ioctl(fd
, RAID_AUTORUN
, 0) == 0)
1591 int Update_subarray(char *dev
, char *subarray
, char *update
, struct mddev_ident
*ident
, int verbose
)
1593 struct supertype supertype
, *st
= &supertype
;
1596 memset(st
, 0, sizeof(*st
));
1598 fd
= open_subarray(dev
, subarray
, st
, verbose
< 0);
1602 if (!st
->ss
->update_subarray
) {
1604 pr_err("Operation not supported for %s metadata\n",
1609 if (mdmon_running(st
->devnm
))
1610 st
->update_tail
= &st
->updates
;
1612 rv
= st
->ss
->update_subarray(st
, subarray
, update
, ident
);
1616 pr_err("Failed to update %s of subarray-%s in %s\n",
1617 update
, subarray
, dev
);
1618 } else if (st
->update_tail
)
1619 flush_metadata_updates(st
);
1621 st
->ss
->sync_metadata(st
);
1623 if (rv
== 0 && strcmp(update
, "name") == 0 && verbose
>= 0)
1624 pr_err("Updated subarray-%s name from %s, UUIDs may have changed\n",
1628 st
->ss
->free_super(st
);
1634 /* Move spare from one array to another If adding to destination array fails
1635 * add back to original array.
1636 * Returns 1 on success, 0 on failure */
1637 int move_spare(char *from_devname
, char *to_devname
, dev_t devid
)
1639 struct mddev_dev devlist
;
1642 /* try to remove and add */
1643 int fd1
= open(to_devname
, O_RDONLY
);
1644 int fd2
= open(from_devname
, O_RDONLY
);
1646 if (fd1
< 0 || fd2
< 0) {
1647 if (fd1
>=0) close(fd1
);
1648 if (fd2
>=0) close(fd2
);
1652 devlist
.next
= NULL
;
1654 devlist
.writemostly
= 0;
1655 devlist
.devname
= devname
;
1656 sprintf(devname
, "%d:%d", major(devid
), minor(devid
));
1658 devlist
.disposition
= 'r';
1659 if (Manage_subdevs(from_devname
, fd2
, &devlist
, -1, 0, NULL
, 0) == 0) {
1660 devlist
.disposition
= 'a';
1661 if (Manage_subdevs(to_devname
, fd1
, &devlist
, -1, 0, NULL
, 0) == 0) {
1662 /* make sure manager is aware of changes */
1663 ping_manager(to_devname
);
1664 ping_manager(from_devname
);
1669 else Manage_subdevs(from_devname
, fd2
, &devlist
, -1, 0, NULL
, 0);