]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Manage.c
e3d46f3b968ec28bc2cfef44e026a624f5ca7084
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2013 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
));
129 static void remove_devices(char *devnm
, char *path
)
132 * Remove names at 'path' - possibly with
133 * partition suffixes - which link to the 'standard'
134 * name for devnm. These were probably created
135 * by mdadm when the array was assembled.
148 sprintf(base
, "/dev/%s", devnm
);
149 be
= base
+ strlen(base
);
151 path2
= xmalloc(strlen(path
)+20);
153 pe
= path2
+ strlen(path2
);
155 for (part
= 0; part
< 16; part
++) {
157 sprintf(be
, "p%d", part
);
160 sprintf(pe
, "p%d", part
);
162 sprintf(pe
, "%d", part
);
164 n
= readlink(path2
, link
, sizeof(link
));
165 if (n
> 0 && (int)strlen(base
) == n
&&
166 strncmp(link
, base
, n
) == 0)
172 int Manage_run(char *devname
, int fd
, struct context
*c
)
174 /* Run the array. Array must already be configured
179 if (md_get_version(fd
) < 9000) {
180 pr_err("need md driver version 0.90.0 or later\n");
185 pr_err("Cannot find %s in sysfs!!\n", devname
);
189 return IncrementalScan(c
, nm
);
192 int Manage_stop(char *devname
, int fd
, int verbose
, int will_retry
)
194 /* Stop the array. Array must already be configured
195 * 'will_retry' means that error messages are not wanted.
198 struct map_ent
*map
= NULL
;
205 unsigned long long rd1
, rd2
;
207 if (will_retry
&& verbose
== 0)
210 if (md_get_version(fd
) < 9000) {
211 if (ioctl(fd
, STOP_MD
, 0) == 0)
213 pr_err("stopping device %s failed: %s\n",
214 devname
, strerror(errno
));
218 strcpy(devnm
, fd2devnm(fd
));
219 /* Get EXCL access first. If this fails, then attempting
220 * to stop is probably a bad idea.
222 mdi
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_COMPONENT
|GET_VERSION
);
223 if (mdi
&& is_subarray(mdi
->text_version
)) {
225 strncpy(container
, mdi
->text_version
+1, sizeof(container
));
226 container
[sizeof(container
)-1] = 0;
227 sl
= strchr(container
, '/');
234 while (((fd
= ((devname
[0] == '/')
235 ?open(devname
, O_RDONLY
|O_EXCL
)
236 :open_dev_flags(devnm
, O_RDONLY
|O_EXCL
))) < 0
237 || strcmp(fd2devnm(fd
), devnm
) != 0)
239 && mdmon_running(container
)
241 /* Can't open, so something might be wrong. However it
242 * is a container, so we might be racing with mdmon, so
247 flush_mdmon(container
);
250 if (fd
< 0 || strcmp(fd2devnm(fd
), devnm
) != 0) {
254 pr_err("Cannot get exclusive access to %s:Perhaps a running process, mounted filesystem or active volume group?\n",
258 /* If this is an mdmon managed array, just write 'inactive'
259 * to the array state and let mdmon clear up.
262 mdi
->array
.level
> 0 &&
263 is_subarray(mdi
->text_version
)) {
265 /* This is mdmon managed. */
268 /* As we had an O_EXCL open, any use of the device
269 * which blocks STOP_ARRAY is probably a transient use,
270 * so it is reasonable to retry for a while - 5 seconds.
274 (err
= sysfs_set_str(mdi
, NULL
,
283 pr_err("failed to stop array %s: %s\n",
284 devname
, strerror(errno
));
289 /* Give monitor a chance to act */
290 ping_monitor(mdi
->text_version
);
292 fd
= open_dev_excl(devnm
);
295 pr_err("failed to completely stop %s: Device is busy\n",
301 mdi
->array
.major_version
== -1 &&
302 mdi
->array
.minor_version
== -2 &&
303 !is_subarray(mdi
->text_version
)) {
304 struct mdstat_ent
*mds
, *m
;
305 /* container, possibly mdmon-managed.
306 * Make sure mdmon isn't opening it, which
307 * would interfere with the 'stop'
309 ping_monitor(mdi
->sys_name
);
311 /* now check that there are no existing arrays
312 * which are members of this array
314 mds
= mdstat_read(0, 0);
315 for (m
= mds
; m
; m
= m
->next
)
316 if (m
->metadata_version
&&
317 strncmp(m
->metadata_version
, "external:", 9)==0 &&
318 metadata_container_matches(m
->metadata_version
+9,
321 pr_err("Cannot stop container %s: member %s still active\n",
329 /* If the array is undergoing a reshape which changes the number
330 * of devices, then it would be nice to stop it at a point where
331 * it has completed a full number of stripes in both old and
332 * new layouts as this will allow the reshape to be reverted.
333 * So if 'sync_action' is "reshape" and 'raid_disks' shows two
334 * different numbers, then
336 * - set sync_max to next multiple of both data_disks and
337 * chunk sizes (or next but one)
339 * - wait on 'sync_completed' for that point to be reached.
341 if (mdi
&& (mdi
->array
.level
>= 4 && mdi
->array
.level
<= 6) &&
342 sysfs_attribute_available(mdi
, NULL
, "sync_action") &&
343 sysfs_attribute_available(mdi
, NULL
, "reshape_direction") &&
344 sysfs_get_str(mdi
, NULL
, "sync_action", buf
, 20) > 0 &&
345 strcmp(buf
, "reshape\n") == 0 &&
346 sysfs_get_two(mdi
, NULL
, "raid_disks", &rd1
, &rd2
) == 2) {
347 unsigned long long position
, curr
;
348 unsigned long long chunk1
, chunk2
;
349 unsigned long long rddiv
, chunkdiv
;
350 unsigned long long sectors
;
351 unsigned long long sync_max
, old_sync_max
;
352 unsigned long long completed
;
358 while (rd1
> rd2
&& delay
> 0 &&
359 sysfs_get_ll(mdi
, NULL
, "sync_max", &old_sync_max
) == 0) {
360 /* must be in the critical section - wait a bit */
365 if (sysfs_set_str(mdi
, NULL
, "sync_action", "frozen") != 0)
367 /* Array is frozen */
369 rd1
-= mdi
->array
.level
== 6 ? 2 : 1;
370 rd2
-= mdi
->array
.level
== 6 ? 2 : 1;
371 sysfs_get_str(mdi
, NULL
, "reshape_direction", buf
, sizeof(buf
));
372 if (strncmp(buf
, "back", 4) == 0)
374 if (sysfs_get_ll(mdi
, NULL
, "reshape_position", &position
) != 0) {
375 /* reshape must have finished now */
376 sysfs_set_str(mdi
, NULL
, "sync_action", "idle");
379 sysfs_get_two(mdi
, NULL
, "chunk_size", &chunk1
, &chunk2
);
382 rddiv
= GCD(rd1
, rd2
);
383 chunkdiv
= GCD(chunk1
, chunk2
);
384 sectors
= (chunk1
/chunkdiv
) * chunk2
* (rd1
/rddiv
) * rd2
;
387 /* Need to subtract 'reshape_position' from
388 * array size to get equivalent of sync_max.
389 * Size calculation based on raid5_size in kernel.
391 unsigned long long size
= mdi
->component_size
;
394 /* rd1 must be smaller */
395 /* Reshape may have progressed further backwards than
396 * recorded, so target even further back (hence "-1")
398 position
= (position
/ sectors
- 1) * sectors
;
399 /* rd1 is always the conversion factor between 'sync'
400 * position and 'reshape' position.
401 * We read 1 "new" stripe worth of data from where-ever,
402 * and when write out that full stripe.
404 sync_max
= size
- position
/rd1
;
406 /* Reshape will very likely be beyond position, and it may
407 * be too late to stop at '+1', so aim for '+2'
409 position
= (position
/ sectors
+ 2) * sectors
;
410 sync_max
= position
/rd1
;
412 if (sysfs_get_ll(mdi
, NULL
, "sync_max", &old_sync_max
) < 0)
413 old_sync_max
= mdi
->component_size
;
414 /* Must not advance sync_max as that could confuse
415 * the reshape monitor */
416 if (sync_max
< old_sync_max
)
417 sysfs_set_num(mdi
, NULL
, "sync_max", sync_max
);
418 sysfs_set_str(mdi
, NULL
, "sync_action", "idle");
420 /* That should have set things going again. Now we
421 * wait a little while (3 second max) for sync_completed
422 * to reach the target.
423 * The reshape process can block for 500msec if
424 * the sync speed limit is hit, so we need to wait
425 * a lot longer than that. 1 second is usually
429 scfd
= sysfs_open(mdi
->sys_name
, NULL
, "sync_completed");
430 while (scfd
>= 0 && delay
> 0 && old_sync_max
> 0) {
431 unsigned long long max_completed
;
432 sysfs_get_ll(mdi
, NULL
, "reshape_position", &curr
);
433 sysfs_fd_get_str(scfd
, buf
, sizeof(buf
));
434 if (strncmp(buf
, "none", 4) == 0) {
435 /* Either reshape has aborted, or hasn't
436 * quite started yet. Wait a bit and
437 * check 'sync_action' to see.
440 sysfs_get_str(mdi
, NULL
, "sync_action", buf
, sizeof(buf
));
441 if (strncmp(buf
, "reshape", 7) != 0)
445 if (sysfs_fd_get_two(scfd
, &completed
,
446 &max_completed
) == 2 &&
447 /* 'completed' sometimes reads as max-uulong */
448 completed
< max_completed
&&
449 (completed
> sync_max
||
450 (completed
== sync_max
&& curr
!= position
))) {
451 while (completed
> sync_max
) {
452 sync_max
+= sectors
/ rd1
;
458 if (sync_max
< old_sync_max
)
459 sysfs_set_num(mdi
, NULL
, "sync_max", sync_max
);
462 if (!backwards
&& curr
>= position
)
464 if (backwards
&& curr
<= position
)
466 sysfs_wait(scfd
, &delay
);
474 /* As we have an O_EXCL open, any use of the device
475 * which blocks STOP_ARRAY is probably a transient use,
476 * so it is reasonable to retry for a while - 5 seconds.
479 while (count
&& fd
>= 0
480 && (err
= ioctl(fd
, STOP_ARRAY
, NULL
)) < 0
485 if (fd
>= 0 && err
) {
487 pr_err("failed to stop array %s: %s\n",
488 devname
, strerror(errno
));
490 cont_err("Perhaps a running process, mounted filesystem or active volume group?\n");
496 if (get_linux_version() < 2006028) {
497 /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
498 * was stopped, so We'll do it here just to be sure. Drop any
499 * partitions as well...
502 ioctl(fd
, BLKRRPART
, 0);
504 sysfs_uevent(mdi
, "change");
507 if (devnm
[0] && use_udev()) {
508 struct map_ent
*mp
= map_by_devnm(&map
, devnm
);
509 remove_devices(devnm
, mp
? mp
->path
: NULL
);
513 pr_err("stopped %s\n", devname
);
515 map_remove(&map
, devnm
);
523 static struct mddev_dev
*add_one(struct mddev_dev
*dv
, char *name
, char disp
)
525 struct mddev_dev
*new;
526 new = xmalloc(sizeof(*new));
527 memset(new, 0, sizeof(*new));
528 new->devname
= xstrdup(name
);
529 new->disposition
= disp
;
530 new->next
= dv
->next
;
535 static void add_faulty(struct mddev_dev
*dv
, int fd
, char disp
)
537 mdu_array_info_t array
;
538 mdu_disk_info_t disk
;
542 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0)
545 remaining_disks
= array
.nr_disks
;
546 for (i
= 0; i
< MAX_DISKS
&& remaining_disks
> 0; i
++) {
549 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
551 if (disk
.major
== 0 && disk
.minor
== 0)
554 if ((disk
.state
& 1) == 0) /* not faulty */
556 sprintf(buf
, "%d:%d", disk
.major
, disk
.minor
);
557 dv
= add_one(dv
, buf
, disp
);
561 static void add_detached(struct mddev_dev
*dv
, int fd
, char disp
)
563 mdu_array_info_t array
;
564 mdu_disk_info_t disk
;
568 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0)
571 remaining_disks
= array
.nr_disks
;
572 for (i
= 0; i
< MAX_DISKS
&& remaining_disks
> 0; i
++) {
576 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
578 if (disk
.major
== 0 && disk
.minor
== 0)
581 if (disp
== 'f' && (disk
.state
& 1) != 0) /* already faulty */
583 sprintf(buf
, "%d:%d", disk
.major
, disk
.minor
);
584 sfd
= dev_open(buf
, O_RDONLY
);
591 /* Probably not detached */
593 dv
= add_one(dv
, buf
, disp
);
597 static void add_set(struct mddev_dev
*dv
, int fd
, char set_char
)
599 mdu_array_info_t array
;
600 mdu_disk_info_t disk
;
605 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0)
607 if (array
.level
!= 10)
609 copies
= ((array
.layout
& 0xff) *
610 ((array
.layout
>> 8) & 0xff));
611 if (array
.raid_disks
% copies
)
614 remaining_disks
= array
.nr_disks
;
615 for (i
= 0; i
< MAX_DISKS
&& remaining_disks
> 0; i
++) {
618 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
620 if (disk
.major
== 0 && disk
.minor
== 0)
623 set
= disk
.raid_disk
% copies
;
624 if (set_char
!= set
+ 'A')
626 sprintf(buf
, "%d:%d", disk
.major
, disk
.minor
);
627 dv
= add_one(dv
, buf
, dv
->disposition
);
631 int attempt_re_add(int fd
, int tfd
, struct mddev_dev
*dv
,
632 struct supertype
*dev_st
, struct supertype
*tst
,
634 char *update
, char *devname
, int verbose
,
635 mdu_array_info_t
*array
)
641 dev_st
->ss
->getinfo_super(dev_st
, &mdi
, NULL
);
642 dev_st
->ss
->uuid_from_super(dev_st
, ouuid
);
644 tst
->ss
->uuid_from_super(tst
, duuid
);
646 /* Assume uuid matches: kernel will check */
647 memcpy(duuid
, ouuid
, sizeof(ouuid
));
648 if ((mdi
.disk
.state
& (1<<MD_DISK_ACTIVE
)) &&
649 !(mdi
.disk
.state
& (1<<MD_DISK_FAULTY
)) &&
650 memcmp(duuid
, ouuid
, sizeof(ouuid
))==0) {
651 /* Looks like it is worth a
652 * try. Need to make sure
653 * kernel will accept it
656 mdu_disk_info_t disc
;
657 /* re-add doesn't work for version-1 superblocks
660 if (array
->major_version
== 1 &&
661 get_linux_version() <= 2006018)
663 disc
.number
= mdi
.disk
.number
;
664 if (ioctl(fd
, GET_DISK_INFO
, &disc
) != 0
665 || disc
.major
!= 0 || disc
.minor
!= 0
668 disc
.major
= major(rdev
);
669 disc
.minor
= minor(rdev
);
670 disc
.number
= mdi
.disk
.number
;
671 disc
.raid_disk
= mdi
.disk
.raid_disk
;
672 disc
.state
= mdi
.disk
.state
;
673 if (array
->state
& (1 << MD_SB_CLUSTERED
)) {
674 /* extra flags are needed when adding to a cluster as
675 * there are two cases to distinguish
677 if (dv
->disposition
== 'c')
678 disc
.state
|= (1 << MD_DISK_CANDIDATE
);
680 disc
.state
|= (1 << MD_DISK_CLUSTER_ADD
);
682 if (dv
->writemostly
== 1)
683 disc
.state
|= 1 << MD_DISK_WRITEMOSTLY
;
684 if (dv
->writemostly
== 2)
685 disc
.state
&= ~(1 << MD_DISK_WRITEMOSTLY
);
686 remove_partitions(tfd
);
687 if (update
|| dv
->writemostly
> 0) {
689 tfd
= dev_open(dv
->devname
, O_RDWR
);
691 pr_err("failed to open %s for superblock update during re-add\n", dv
->devname
);
695 if (dv
->writemostly
== 1)
696 rv
= dev_st
->ss
->update_super(
697 dev_st
, NULL
, "writemostly",
698 devname
, verbose
, 0, NULL
);
699 if (dv
->writemostly
== 2)
700 rv
= dev_st
->ss
->update_super(
701 dev_st
, NULL
, "readwrite",
702 devname
, verbose
, 0, NULL
);
704 rv
= dev_st
->ss
->update_super(
705 dev_st
, NULL
, update
,
706 devname
, verbose
, 0, NULL
);
708 rv
= dev_st
->ss
->store_super(dev_st
, tfd
);
711 pr_err("failed to update superblock during re-add\n");
715 /* don't even try if disk is marked as faulty */
717 if (ioctl(fd
, ADD_NEW_DISK
, &disc
) == 0) {
719 pr_err("re-added %s\n", dv
->devname
);
722 if (errno
== ENOMEM
|| errno
== EROFS
) {
723 pr_err("add new device failed for %s: %s\n",
724 dv
->devname
, strerror(errno
));
725 if (dv
->disposition
== 'M')
734 int Manage_add(int fd
, int tfd
, struct mddev_dev
*dv
,
735 struct supertype
*tst
, mdu_array_info_t
*array
,
736 int force
, int verbose
, char *devname
,
737 char *update
, unsigned long rdev
, unsigned long long array_size
,
740 unsigned long long ldsize
;
741 struct supertype
*dev_st
= NULL
;
743 mdu_disk_info_t disc
;
745 if (!get_dev_size(tfd
, dv
->devname
, &ldsize
)) {
746 if (dv
->disposition
== 'M')
752 if (tst
->ss
== &super0
&& ldsize
> 4ULL*1024*1024*1024*1024) {
753 /* More than 4TB is wasted on v0.90 */
755 pr_err("%s is larger than %s can effectively use.\n"
756 " Add --force is you really want to add this device.\n",
757 dv
->devname
, devname
);
760 pr_err("%s is larger than %s can effectively use.\n"
761 " Adding anyway as --force was given.\n",
762 dv
->devname
, devname
);
764 if (!tst
->ss
->external
&&
765 array
->major_version
== 0 &&
766 md_get_version(fd
)%100 < 2) {
767 if (ioctl(fd
, HOT_ADD_DISK
, rdev
)==0) {
769 pr_err("hot added %s\n",
774 pr_err("hot add failed for %s: %s\n",
775 dv
->devname
, strerror(errno
));
779 if (array
->not_persistent
== 0 || tst
->ss
->external
) {
781 /* need to find a sample superblock to copy, and
782 * a spare slot to use.
783 * For 'external' array (well, container based),
784 * We can just load the metadata for the array->
788 /* already loaded */;
789 else if (tst
->ss
->external
) {
790 tst
->ss
->load_container(tst
, fd
, NULL
);
791 } else for (j
= 0; j
< tst
->max_devs
; j
++) {
795 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
797 if (disc
.major
==0 && disc
.minor
==0)
799 if ((disc
.state
& 4)==0) /* sync */
801 /* Looks like a good device to try */
802 dev
= map_dev(disc
.major
, disc
.minor
, 1);
805 dfd
= dev_open(dev
, O_RDONLY
);
808 if (tst
->ss
->load_super(tst
, dfd
,
816 /* FIXME this is a bad test to be using */
817 if (!tst
->sb
&& (dv
->disposition
!= 'a'
818 && dv
->disposition
!= 'S')) {
819 /* we are re-adding a device to a
820 * completely dead array - have to depend
823 } else if (!tst
->sb
) {
824 pr_err("cannot load array metadata from %s\n", devname
);
828 /* Make sure device is large enough */
829 if (dv
->disposition
!= 'j' && /* skip size check for Journal */
831 tst
->ss
->avail_size(tst
, ldsize
/512, INVALID_SECTORS
) <
833 if (dv
->disposition
== 'M')
835 pr_err("%s not large enough to join array\n",
840 /* Possibly this device was recently part of
841 * the array and was temporarily removed, and
842 * is now being re-added. If so, we can
846 if (array
->not_persistent
==0) {
847 dev_st
= dup_super(tst
);
848 dev_st
->ss
->load_super(dev_st
, tfd
, NULL
);
850 if (dev_st
&& dev_st
->sb
&& dv
->disposition
!= 'S') {
851 int rv
= attempt_re_add(fd
, tfd
, dv
,
857 dev_st
->ss
->free_super(dev_st
);
861 if (dv
->disposition
== 'M') {
863 pr_err("--re-add for %s to %s is not possible\n",
864 dv
->devname
, devname
);
867 if (dv
->disposition
== 'A') {
868 pr_err("--re-add for %s to %s is not possible\n",
869 dv
->devname
, devname
);
872 if (array
->active_disks
< array
->raid_disks
) {
873 char *avail
= xcalloc(array
->raid_disks
, 1);
877 for (d
= 0; d
< MAX_DISKS
&& found
< array
->nr_disks
; d
++) {
879 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
881 if (disc
.major
== 0 && disc
.minor
== 0)
883 if (!(disc
.state
& (1<<MD_DISK_SYNC
)))
885 avail
[disc
.raid_disk
] = 1;
888 array_failed
= !enough(array
->level
, array
->raid_disks
,
889 array
->layout
, 1, avail
);
894 pr_err("%s has failed so using --add cannot work and might destroy\n",
896 pr_err("data on %s. You should stop the array and re-assemble it.\n",
901 /* non-persistent. Must ensure that new drive
902 * is at least array->size big.
904 if (ldsize
/512 < array_size
) {
905 pr_err("%s not large enough to join array\n",
910 /* committed to really trying this device now*/
911 remove_partitions(tfd
);
913 /* in 2.6.17 and earlier, version-1 superblocks won't
914 * use the number we write, but will choose a free number.
915 * we must choose the same free number, which requires
916 * starting at 'raid_disks' and counting up
918 for (j
= array
->raid_disks
; j
< tst
->max_devs
; j
++) {
920 if (ioctl(fd
, GET_DISK_INFO
, &disc
))
922 if (disc
.major
==0 && disc
.minor
==0)
924 if (disc
.state
& 8) /* removed */
927 disc
.major
= major(rdev
);
928 disc
.minor
= minor(rdev
);
932 disc
.number
= raid_slot
;
935 /* only add journal to array that supports journaling */
936 if (dv
->disposition
== 'j') {
940 mdp
= sysfs_read(fd
, NULL
, GET_ARRAY_STATE
);
942 pr_err("%s unable to read array state.\n", devname
);
946 if (strncmp(mdp
->sysfs_array_state
, "readonly", 8) != 0) {
947 pr_err("%s is not readonly, cannot add journal.\n", devname
);
951 tst
->ss
->getinfo_super(tst
, &mdi
, NULL
);
952 if (mdi
.journal_device_required
== 0) {
953 pr_err("%s does not support journal device.\n", devname
);
959 if (array
->not_persistent
==0) {
961 if (dv
->disposition
== 'j')
962 disc
.state
|= (1 << MD_DISK_JOURNAL
) | (1 << MD_DISK_SYNC
);
963 if (dv
->writemostly
== 1)
964 disc
.state
|= 1 << MD_DISK_WRITEMOSTLY
;
965 dfd
= dev_open(dv
->devname
, O_RDWR
| O_EXCL
|O_DIRECT
);
966 if (tst
->ss
->add_to_super(tst
, &disc
, dfd
,
967 dv
->devname
, INVALID_SECTORS
))
969 if (tst
->ss
->write_init_super(tst
))
971 } else if (dv
->disposition
== 'A') {
972 /* this had better be raid1.
973 * As we are "--re-add"ing we must find a spare slot
976 char *used
= xcalloc(array
->raid_disks
, 1);
977 for (j
= 0; j
< tst
->max_devs
; j
++) {
978 mdu_disk_info_t disc2
;
980 if (ioctl(fd
, GET_DISK_INFO
, &disc2
))
982 if (disc2
.major
==0 && disc2
.minor
==0)
984 if (disc2
.state
& 8) /* removed */
986 if (disc2
.raid_disk
< 0)
988 if (disc2
.raid_disk
> array
->raid_disks
)
990 used
[disc2
.raid_disk
] = 1;
992 for (j
= 0 ; j
< array
->raid_disks
; j
++)
995 disc
.state
|= (1<<MD_DISK_SYNC
);
1001 if (array
->state
& (1 << MD_SB_CLUSTERED
)) {
1002 if (dv
->disposition
== 'c')
1003 disc
.state
|= (1 << MD_DISK_CANDIDATE
);
1005 disc
.state
|= (1 << MD_DISK_CLUSTER_ADD
);
1008 if (dv
->writemostly
== 1)
1009 disc
.state
|= (1 << MD_DISK_WRITEMOSTLY
);
1010 if (tst
->ss
->external
) {
1012 * to an external metadata container */
1013 struct mdinfo new_mdi
;
1019 strcpy(devnm
, fd2devnm(fd
));
1021 container_fd
= open_dev_excl(devnm
);
1022 if (container_fd
< 0) {
1023 pr_err("add failed for %s: could not get exclusive access to container\n",
1025 tst
->ss
->free_super(tst
);
1029 Kill(dv
->devname
, NULL
, 0, -1, 0);
1030 dfd
= dev_open(dv
->devname
, O_RDWR
| O_EXCL
|O_DIRECT
);
1031 if (mdmon_running(tst
->container_devnm
))
1032 tst
->update_tail
= &tst
->updates
;
1033 if (tst
->ss
->add_to_super(tst
, &disc
, dfd
,
1034 dv
->devname
, INVALID_SECTORS
)) {
1036 close(container_fd
);
1039 if (tst
->update_tail
)
1040 flush_metadata_updates(tst
);
1042 tst
->ss
->sync_metadata(tst
);
1044 sra
= sysfs_read(container_fd
, NULL
, 0);
1046 pr_err("add failed for %s: sysfs_read failed\n",
1048 close(container_fd
);
1049 tst
->ss
->free_super(tst
);
1052 sra
->array
.level
= LEVEL_CONTAINER
;
1053 /* Need to set data_offset and component_size */
1054 tst
->ss
->getinfo_super(tst
, &new_mdi
, NULL
);
1055 new_mdi
.disk
.major
= disc
.major
;
1056 new_mdi
.disk
.minor
= disc
.minor
;
1057 new_mdi
.recovery_start
= 0;
1058 /* Make sure fds are closed as they are O_EXCL which
1059 * would block add_disk */
1060 tst
->ss
->free_super(tst
);
1061 if (sysfs_add_disk(sra
, &new_mdi
, 0) != 0) {
1062 pr_err("add new device to external metadata failed for %s\n", dv
->devname
);
1063 close(container_fd
);
1067 ping_monitor(devnm
);
1069 close(container_fd
);
1071 tst
->ss
->free_super(tst
);
1072 if (ioctl(fd
, ADD_NEW_DISK
, &disc
)) {
1073 if (dv
->disposition
== 'j')
1074 pr_err("Failed to hot add %s as journal, "
1075 "please try restart %s.\n", dv
->devname
, devname
);
1077 pr_err("add new device failed for %s as %d: %s\n",
1078 dv
->devname
, j
, strerror(errno
));
1081 if (dv
->disposition
== 'j') {
1082 pr_err("Journal added successfully, making %s read-write\n", devname
);
1083 if (Manage_ro(devname
, fd
, -1))
1084 pr_err("Failed to make %s read-write\n", devname
);
1089 pr_err("added %s\n", dv
->devname
);
1093 int Manage_remove(struct supertype
*tst
, int fd
, struct mddev_dev
*dv
,
1094 int sysfd
, unsigned long rdev
, int verbose
, char *devname
)
1099 if (tst
->ss
->external
) {
1100 /* To remove a device from a container, we must
1101 * check that it isn't in use in an array.
1102 * This involves looking in the 'holders'
1103 * directory - there must be just one entry,
1105 * To ensure that it doesn't get used as a
1106 * hot spare while we are checking, we
1107 * get an O_EXCL open on the container
1111 strcpy(devnm
, fd2devnm(fd
));
1112 lfd
= open_dev_excl(devnm
);
1114 pr_err("Cannot get exclusive access to container - odd\n");
1117 /* We may not be able to check on holders in
1118 * sysfs, either because we don't have the dev num
1119 * (rdev == 0) or because the device has been detached
1120 * and the 'holders' directory no longer exists
1121 * (ret == -1). In that case, assume it is OK to
1127 ret
= sysfs_unique_holder(devnm
, rdev
);
1129 pr_err("%s is not a member, cannot remove.\n",
1135 pr_err("%s is still in use, cannot remove.\n",
1141 /* FIXME check that it is a current member */
1143 /* device has been removed and we don't know
1144 * the major:minor number
1146 int n
= write(sysfd
, "remove", 6);
1152 err
= ioctl(fd
, HOT_REMOVE_DISK
, rdev
);
1153 if (err
&& errno
== ENODEV
) {
1154 /* Old kernels rejected this if no personality
1156 struct mdinfo
*sra
= sysfs_read(fd
, NULL
, GET_DEVS
);
1157 struct mdinfo
*dv
= NULL
;
1160 for ( ; dv
; dv
=dv
->next
)
1161 if (dv
->disk
.major
== (int)major(rdev
) &&
1162 dv
->disk
.minor
== (int)minor(rdev
))
1165 err
= sysfs_set_str(sra
, dv
,
1173 pr_err("hot remove failed for %s: %s\n", dv
->devname
,
1179 if (tst
->ss
->external
) {
1181 * Before dropping our exclusive open we make an
1182 * attempt at preventing mdmon from seeing an
1183 * 'add' event before reconciling this 'remove'
1186 char *devnm
= fd2devnm(fd
);
1189 pr_err("unable to get container name\n");
1193 ping_manager(devnm
);
1198 pr_err("hot removed %s from %s\n",
1199 dv
->devname
, devname
);
1203 int Manage_replace(struct supertype
*tst
, int fd
, struct mddev_dev
*dv
,
1204 unsigned long rdev
, int verbose
, char *devname
)
1206 struct mdinfo
*mdi
, *di
;
1207 if (tst
->ss
->external
) {
1208 pr_err("--replace only supported for native metadata (0.90 or 1.x)\n");
1211 /* Need to find the device in sysfs and add 'want_replacement' to the
1214 mdi
= sysfs_read(fd
, NULL
, GET_DEVS
);
1215 if (!mdi
|| !mdi
->devs
) {
1216 pr_err("Cannot find status of %s to enable replacement - strange\n",
1220 for (di
= mdi
->devs
; di
; di
= di
->next
)
1221 if (di
->disk
.major
== (int)major(rdev
) &&
1222 di
->disk
.minor
== (int)minor(rdev
))
1226 if (di
->disk
.raid_disk
< 0) {
1227 pr_err("%s is not active and so cannot be replaced.\n",
1232 rv
= sysfs_set_str(mdi
, di
,
1233 "state", "want_replacement");
1236 pr_err("Failed to request replacement for %s\n",
1241 pr_err("Marked %s (device %d in %s) for replacement\n",
1242 dv
->devname
, di
->disk
.raid_disk
, devname
);
1243 /* If there is a matching 'with', we need to tell it which
1246 while (dv
&& dv
->disposition
!= 'W')
1249 dv
->disposition
= 'w';
1250 dv
->used
= di
->disk
.raid_disk
;
1255 pr_err("%s not found in %s so cannot --replace it\n",
1256 dv
->devname
, devname
);
1260 int Manage_with(struct supertype
*tst
, int fd
, struct mddev_dev
*dv
,
1261 unsigned long rdev
, int verbose
, char *devname
)
1263 struct mdinfo
*mdi
, *di
;
1264 /* try to set 'slot' for 'rdev' in 'fd' to 'dv->used' */
1265 mdi
= sysfs_read(fd
, NULL
, GET_DEVS
|GET_STATE
);
1266 if (!mdi
|| !mdi
->devs
) {
1267 pr_err("Cannot find status of %s to enable replacement - strange\n",
1271 for (di
= mdi
->devs
; di
; di
= di
->next
)
1272 if (di
->disk
.major
== (int)major(rdev
) &&
1273 di
->disk
.minor
== (int)minor(rdev
))
1277 if (di
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
1278 pr_err("%s is faulty and cannot be a replacement\n",
1283 if (di
->disk
.raid_disk
>= 0) {
1284 pr_err("%s is active and cannot be a replacement\n",
1289 rv
= sysfs_set_num(mdi
, di
,
1293 pr_err("Failed to set %s as preferred replacement.\n",
1298 pr_err("Marked %s in %s as replacement for device %d\n",
1299 dv
->devname
, devname
, dv
->used
);
1303 pr_err("%s not found in %s so cannot make it preferred replacement\n",
1304 dv
->devname
, devname
);
1308 int Manage_subdevs(char *devname
, int fd
,
1309 struct mddev_dev
*devlist
, int verbose
, int test
,
1310 char *update
, int force
)
1312 /* Do something to each dev.
1314 * 'a' - add the device
1316 * If that fails EINVAL, try ADD_NEW_DISK
1317 * 'S' - add the device as a spare - don't try re-add
1318 * 'j' - add the device as a journal device
1319 * 'A' - re-add the device
1320 * 'r' - remove the device: HOT_REMOVE_DISK
1321 * device can be 'faulty' or 'detached' in which case all
1322 * matching devices are removed.
1323 * 'f' - set the device faulty SET_DISK_FAULTY
1324 * device can be 'detached' in which case any device that
1325 * is inaccessible will be marked faulty.
1326 * 'R' - mark this device as wanting replacement.
1327 * 'W' - this device is added if necessary and activated as
1328 * a replacement for a previous 'R' device.
1330 * 'w' - 'W' will be changed to 'w' when it is paired with
1331 * a 'R' device. If a 'W' is found while walking the list
1332 * it must be unpaired, and is an error.
1333 * 'M' - this is created by a 'missing' target. It is a slight
1335 * 'F' - Another variant of 'A', where the device was faulty
1336 * so must be removed from the array first.
1337 * 'c' - confirm the device as found (for clustered environments)
1339 * For 'f' and 'r', the device can also be a kernel-internal
1340 * name such as 'sdb'.
1342 mdu_array_info_t array
;
1343 unsigned long long array_size
;
1344 struct mddev_dev
*dv
;
1346 struct supertype
*tst
;
1347 char *subarray
= NULL
;
1349 int count
= 0; /* number of actions taken */
1351 struct mdinfo devinfo
;
1356 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)) {
1357 pr_err("Cannot get array info for %s\n",
1361 sysfs_init(&info
, fd
, NULL
);
1363 /* array.size is only 32 bits and may be truncated.
1364 * So read from sysfs if possible, and record number of sectors
1367 array_size
= get_component_size(fd
);
1368 if (array_size
<= 0)
1369 array_size
= array
.size
* 2;
1371 tst
= super_by_fd(fd
, &subarray
);
1373 pr_err("unsupport array - version %d.%d\n",
1374 array
.major_version
, array
.minor_version
);
1378 for (dv
= devlist
; dv
; dv
= dv
->next
) {
1379 unsigned long rdev
= 0; /* device to add/remove etc */
1384 if (dv
->disposition
== 'c') {
1385 rv
= parse_cluster_confirm_arg(dv
->devname
,
1389 pr_err("Could not get the devname of cluster\n");
1394 if (strcmp(dv
->devname
, "failed") == 0 ||
1395 strcmp(dv
->devname
, "faulty") == 0) {
1396 if (dv
->disposition
!= 'A'
1397 && dv
->disposition
!= 'r') {
1398 pr_err("%s only meaningful with -r or --re-add, not -%c\n",
1399 dv
->devname
, dv
->disposition
);
1402 add_faulty(dv
, fd
, (dv
->disposition
== 'A'
1406 if (strcmp(dv
->devname
, "detached") == 0) {
1407 if (dv
->disposition
!= 'r' && dv
->disposition
!= 'f') {
1408 pr_err("%s only meaningful with -r of -f, not -%c\n",
1409 dv
->devname
, dv
->disposition
);
1412 add_detached(dv
, fd
, dv
->disposition
);
1416 if (strcmp(dv
->devname
, "missing") == 0) {
1417 struct mddev_dev
*add_devlist
= NULL
;
1418 struct mddev_dev
**dp
;
1419 if (dv
->disposition
== 'c') {
1420 rv
= ioctl(fd
, CLUSTERED_DISK_NACK
, NULL
);
1424 if (dv
->disposition
!= 'A') {
1425 pr_err("'missing' only meaningful with --re-add\n");
1428 add_devlist
= conf_get_devs();
1429 if (add_devlist
== NULL
) {
1430 pr_err("no devices to scan for missing members.");
1433 for (dp
= &add_devlist
; *dp
; dp
= & (*dp
)->next
)
1434 /* 'M' (for 'missing') is like 'A' without errors */
1435 (*dp
)->disposition
= 'M';
1437 dv
->next
= add_devlist
;
1441 if (strncmp(dv
->devname
, "set-", 4) == 0 &&
1442 strlen(dv
->devname
) == 5) {
1445 if (dv
->disposition
!= 'r' &&
1446 dv
->disposition
!= 'f') {
1447 pr_err("'%s' only meaningful with -r or -f\n",
1451 if (array
.level
!= 10) {
1452 pr_err("'%s' only meaningful with RAID10 arrays\n",
1456 copies
= ((array
.layout
& 0xff) *
1457 ((array
.layout
>> 8) & 0xff));
1458 if (array
.raid_disks
% copies
!= 0 ||
1459 dv
->devname
[4] < 'A' ||
1460 dv
->devname
[4] >= 'A' + copies
||
1462 pr_err("'%s' not meaningful with this array\n",
1466 add_set(dv
, fd
, dv
->devname
[4]);
1470 if (strchr(dv
->devname
, '/') == NULL
&&
1471 strchr(dv
->devname
, ':') == NULL
&&
1472 strlen(dv
->devname
) < 50) {
1473 /* Assume this is a kernel-internal name like 'sda1' */
1476 if (dv
->disposition
!= 'r' && dv
->disposition
!= 'f') {
1477 pr_err("%s only meaningful with -r or -f, not -%c\n",
1478 dv
->devname
, dv
->disposition
);
1482 sprintf(dname
, "dev-%s", dv
->devname
);
1483 sysfd
= sysfs_open(fd2devnm(fd
), dname
, "block/dev");
1486 if (sysfs_fd_get_str(sysfd
, dn
, 20) > 0 &&
1487 sscanf(dn
, "%d:%d", &mj
,&mn
) == 2) {
1488 rdev
= makedev(mj
,mn
);
1495 sysfd
= sysfs_open(fd2devnm(fd
), dname
, "state");
1497 pr_err("%s does not appear to be a component of %s\n",
1498 dv
->devname
, devname
);
1502 } else if ((dv
->disposition
== 'r' || dv
->disposition
== 'f')
1503 && get_maj_min(dv
->devname
, &mj
, &mn
)) {
1504 /* for 'fail' and 'remove', the device might
1507 rdev
= makedev(mj
, mn
);
1510 tfd
= dev_open(dv
->devname
, O_RDONLY
);
1515 int open_err
= errno
;
1516 if (stat(dv
->devname
, &stb
) != 0) {
1517 pr_err("Cannot find %s: %s\n",
1518 dv
->devname
, strerror(errno
));
1521 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
1522 if (dv
->disposition
== 'M')
1523 /* non-fatal. Also improbable */
1525 pr_err("%s is not a block device.\n",
1529 if (dv
->disposition
== 'r')
1530 /* Be happy, the stat worked, that is
1531 * enough for --remove
1535 if (dv
->disposition
== 'M')
1538 pr_err("Cannot open %s: %s\n",
1539 dv
->devname
, strerror(open_err
));
1545 switch(dv
->disposition
){
1547 pr_err("internal error - devmode[%s]=%d\n",
1548 dv
->devname
, dv
->disposition
);
1551 case 'S': /* --add-spare */
1552 case 'j': /* --add-journal */
1554 case 'M': /* --re-add missing */
1555 case 'F': /* --re-add faulty */
1556 case 'c': /* --cluster-confirm */
1557 /* add the device */
1559 pr_err("Cannot add disks to a \'member\' array, perform this operation on the parent container\n");
1563 /* Let's first try to write re-add to sysfs */
1565 (dv
->disposition
== 'A' || dv
->disposition
== 'F')) {
1566 sysfs_init_dev(&devinfo
, rdev
);
1567 if (sysfs_set_str(&info
, &devinfo
, "state", "re-add") == 0) {
1568 pr_err("re-add %s to %s succeed\n",
1569 dv
->devname
, info
.sys_name
);
1574 if (dv
->disposition
== 'F')
1575 /* Need to remove first */
1576 ioctl(fd
, HOT_REMOVE_DISK
, rdev
);
1577 /* Make sure it isn't in use (in 2.6 or later) */
1578 tfd
= dev_open(dv
->devname
, O_RDONLY
|O_EXCL
);
1580 /* We know no-one else is using it. We'll
1581 * need non-exclusive access to add it, so
1585 tfd
= dev_open(dv
->devname
, O_RDONLY
);
1588 if (dv
->disposition
== 'M')
1590 pr_err("Cannot open %s: %s\n",
1591 dv
->devname
, strerror(errno
));
1595 if (sysfs_freeze_array(&info
) == 1)
1600 rv
= Manage_add(fd
, tfd
, dv
, tst
, &array
,
1601 force
, verbose
, devname
, update
,
1602 rdev
, array_size
, raid_slot
);
1614 pr_err("Cannot remove disks from a \'member\' array, perform this operation on the parent container\n");
1617 rv
= Manage_remove(tst
, fd
, dv
, sysfd
,
1629 case 'f': /* set faulty */
1630 /* FIXME check current member */
1631 if ((sysfd
>= 0 && write(sysfd
, "faulty", 6) != 6) ||
1632 (sysfd
< 0 && ioctl(fd
, SET_DISK_FAULTY
,
1636 pr_err("set device faulty failed for %s: %s\n",
1637 dv
->devname
, strerror(errno
));
1647 pr_err("set %s faulty in %s\n",
1648 dv
->devname
, devname
);
1650 case 'R': /* Mark as replaceable */
1652 pr_err("Cannot replace disks in a \'member\' array, perform this operation on the parent container\n");
1656 if (sysfs_freeze_array(&info
) == 1)
1661 rv
= Manage_replace(tst
, fd
, dv
,
1670 case 'W': /* --with device that doesn't match */
1671 pr_err("No matching --replace device for --with %s\n",
1674 case 'w': /* --with device which was matched */
1675 rv
= Manage_with(tst
, fd
, dv
,
1676 rdev
, verbose
, devname
);
1683 sysfs_set_str(&info
, NULL
, "sync_action","idle");
1684 if (test
&& count
== 0)
1690 sysfs_set_str(&info
, NULL
, "sync_action","idle");
1691 return !test
&& busy
? 2 : 1;
1694 int autodetect(void)
1696 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1698 int fd
= dev_open("9:0", O_RDONLY
);
1700 if (ioctl(fd
, RAID_AUTORUN
, 0) == 0)
1707 int Update_subarray(char *dev
, char *subarray
, char *update
, struct mddev_ident
*ident
, int verbose
)
1709 struct supertype supertype
, *st
= &supertype
;
1712 memset(st
, 0, sizeof(*st
));
1714 fd
= open_subarray(dev
, subarray
, st
, verbose
< 0);
1718 if (!st
->ss
->update_subarray
) {
1720 pr_err("Operation not supported for %s metadata\n",
1725 if (mdmon_running(st
->devnm
))
1726 st
->update_tail
= &st
->updates
;
1728 rv
= st
->ss
->update_subarray(st
, subarray
, update
, ident
);
1732 pr_err("Failed to update %s of subarray-%s in %s\n",
1733 update
, subarray
, dev
);
1734 } else if (st
->update_tail
)
1735 flush_metadata_updates(st
);
1737 st
->ss
->sync_metadata(st
);
1739 if (rv
== 0 && strcmp(update
, "name") == 0 && verbose
>= 0)
1740 pr_err("Updated subarray-%s name from %s, UUIDs may have changed\n",
1744 st
->ss
->free_super(st
);
1750 /* Move spare from one array to another If adding to destination array fails
1751 * add back to original array.
1752 * Returns 1 on success, 0 on failure */
1753 int move_spare(char *from_devname
, char *to_devname
, dev_t devid
)
1755 struct mddev_dev devlist
;
1758 /* try to remove and add */
1759 int fd1
= open(to_devname
, O_RDONLY
);
1760 int fd2
= open(from_devname
, O_RDONLY
);
1762 if (fd1
< 0 || fd2
< 0) {
1763 if (fd1
>=0) close(fd1
);
1764 if (fd2
>=0) close(fd2
);
1768 devlist
.next
= NULL
;
1770 devlist
.writemostly
= 0;
1771 devlist
.devname
= devname
;
1772 sprintf(devname
, "%d:%d", major(devid
), minor(devid
));
1774 devlist
.disposition
= 'r';
1775 if (Manage_subdevs(from_devname
, fd2
, &devlist
, -1, 0, NULL
, 0) == 0) {
1776 devlist
.disposition
= 'a';
1777 if (Manage_subdevs(to_devname
, fd1
, &devlist
, -1, 0, NULL
, 0) == 0) {
1778 /* make sure manager is aware of changes */
1779 ping_manager(to_devname
);
1780 ping_manager(from_devname
);
1785 else Manage_subdevs(from_devname
, fd2
, &devlist
, -1, 0, NULL
, 0);