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>
27 #include <sys/socket.h>
28 #include <sys/utsname.h>
31 #include <sys/resource.h>
34 #include <linux/magic.h>
43 * following taken from linux/blkpg.h because they aren't
44 * anywhere else and it isn't safe to #include linux/ * stuff.
47 #define BLKPG _IO(0x12,105)
49 /* The argument structure */
50 struct blkpg_ioctl_arg
{
57 /* The subfunctions (for the op field) */
58 #define BLKPG_ADD_PARTITION 1
59 #define BLKPG_DEL_PARTITION 2
61 /* Sizes of name fields. Unused at present. */
62 #define BLKPG_DEVNAMELTH 64
63 #define BLKPG_VOLNAMELTH 64
65 /* The data structure for ADD_PARTITION and DEL_PARTITION */
66 struct blkpg_partition
{
67 long long start
; /* starting offset in bytes */
68 long long length
; /* length in bytes */
69 int pno
; /* partition number */
70 char devname
[BLKPG_DEVNAMELTH
]; /* partition name, like sda5 or c0d1p2,
71 to be used in kernel messages */
72 char volname
[BLKPG_VOLNAMELTH
]; /* volume label */
77 /* Force a compilation error if condition is true */
78 #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
80 /* Force a compilation error if condition is true, but also produce a
81 result (of value 0 and type size_t), so the expression can be used
82 e.g. in a structure initializer (or where-ever else comma expressions
84 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
86 static int is_dlm_hooks_ready
= 0;
88 int dlm_funs_ready(void)
90 return is_dlm_hooks_ready
? 1 : 0;
93 static struct dlm_hooks
*dlm_hooks
= NULL
;
94 struct dlm_lock_resource
*dlm_lock_res
= NULL
;
95 static int ast_called
= 0;
97 struct dlm_lock_resource
{
102 /* Using poll(2) to wait for and dispatch ASTs */
103 static int poll_for_ast(dlm_lshandle_t ls
)
107 pfd
.fd
= dlm_hooks
->ls_get_fd(ls
);
112 if (poll(&pfd
, 1, 0) < 0)
117 dlm_hooks
->dispatch(dlm_hooks
->ls_get_fd(ls
));
124 static void dlm_ast(void *arg
)
129 static char *cluster_name
= NULL
;
130 /* Create the lockspace, take bitmapXXX locks on all the bitmaps. */
131 int cluster_get_dlmlock(void)
135 int flags
= LKF_NOQUEUE
;
138 if (!dlm_funs_ready()) {
139 pr_err("Something wrong with dlm library\n");
143 ret
= get_cluster_name(&cluster_name
);
145 pr_err("The md can't get cluster name\n");
149 dlm_lock_res
= xmalloc(sizeof(struct dlm_lock_resource
));
150 dlm_lock_res
->ls
= dlm_hooks
->open_lockspace(cluster_name
);
151 if (!dlm_lock_res
->ls
) {
152 dlm_lock_res
->ls
= dlm_hooks
->create_lockspace(cluster_name
, O_RDWR
);
153 if (!dlm_lock_res
->ls
) {
154 pr_err("%s failed to create lockspace\n", cluster_name
);
158 pr_err("open existed %s lockspace\n", cluster_name
);
161 snprintf(str
, 64, "bitmap%s", cluster_name
);
163 ret
= dlm_hooks
->ls_lock(dlm_lock_res
->ls
, LKM_PWMODE
,
164 &dlm_lock_res
->lksb
, flags
, str
, strlen(str
),
165 0, dlm_ast
, dlm_lock_res
, NULL
, NULL
);
167 pr_err("error %d when get PW mode on lock %s\n", errno
, str
);
168 /* let's try several times if EAGAIN happened */
169 if (dlm_lock_res
->lksb
.sb_status
== EAGAIN
&& retry_count
< 10) {
174 dlm_hooks
->release_lockspace(cluster_name
, dlm_lock_res
->ls
, 1);
178 /* Wait for it to complete */
179 poll_for_ast(dlm_lock_res
->ls
);
181 if (dlm_lock_res
->lksb
.sb_status
) {
182 pr_err("failed to lock cluster\n");
188 int cluster_release_dlmlock(void)
195 if (!dlm_lock_res
->lksb
.sb_lkid
)
198 ret
= dlm_hooks
->ls_unlock_wait(dlm_lock_res
->ls
,
199 dlm_lock_res
->lksb
.sb_lkid
, 0,
200 &dlm_lock_res
->lksb
);
202 pr_err("error %d happened when unlock\n", errno
);
203 /* XXX make sure the lock is unlocked eventually */
207 /* Wait for it to complete */
208 poll_for_ast(dlm_lock_res
->ls
);
210 errno
= dlm_lock_res
->lksb
.sb_status
;
211 if (errno
!= EUNLOCK
) {
212 pr_err("error %d happened in ast when unlock lockspace\n",
214 /* XXX make sure the lockspace is unlocked eventually */
218 ret
= dlm_hooks
->release_lockspace(cluster_name
, dlm_lock_res
->ls
, 1);
220 pr_err("error %d happened when release lockspace\n", errno
);
221 /* XXX make sure the lockspace is released eventually */
230 int md_array_valid(int fd
)
235 sra
= sysfs_read(fd
, NULL
, GET_ARRAY_STATE
);
237 if (sra
->array_state
!= ARRAY_UNKNOWN_STATE
)
245 * GET_ARRAY_INFO doesn't provide access to the proper state
246 * information, so fallback to a basic check for raid_disks != 0
248 ret
= ioctl(fd
, RAID_VERSION
);
254 int md_array_active(int fd
)
257 struct mdu_array_info_s array
;
260 sra
= sysfs_read(fd
, NULL
, GET_ARRAY_STATE
);
262 if (!md_array_is_active(sra
))
268 * GET_ARRAY_INFO doesn't provide access to the proper state
269 * information, so fallback to a basic check for raid_disks != 0
271 ret
= ioctl(fd
, GET_ARRAY_INFO
, &array
);
277 int md_array_is_active(struct mdinfo
*info
)
279 return (info
->array_state
!= ARRAY_CLEAR
&&
280 info
->array_state
!= ARRAY_INACTIVE
&&
281 info
->array_state
!= ARRAY_UNKNOWN_STATE
);
285 * Get array info from the kernel. Longer term we want to deprecate the
286 * ioctl and get it from sysfs.
288 int md_get_array_info(int fd
, struct mdu_array_info_s
*array
)
290 return ioctl(fd
, GET_ARRAY_INFO
, array
);
296 int md_set_array_info(int fd
, struct mdu_array_info_s
*array
)
298 return ioctl(fd
, SET_ARRAY_INFO
, array
);
302 * Get disk info from the kernel.
304 int md_get_disk_info(int fd
, struct mdu_disk_info_s
*disk
)
306 return ioctl(fd
, GET_DISK_INFO
, disk
);
310 * Parse a 128 bit uuid in 4 integers
311 * format is 32 hexx nibbles with options :.<space> separator
312 * If not exactly 32 hex digits are found, return 0
315 int parse_uuid(char *str
, int uuid
[4])
317 int hit
= 0; /* number of Hex digIT */
320 for (i
= 0; i
< 4; i
++)
323 while ((c
= *str
++) != 0) {
325 if (c
>= '0' && c
<= '9')
327 else if (c
>= 'a' && c
<= 'f')
329 else if (c
>= 'A' && c
<= 'F')
331 else if (strchr(":. -", c
))
346 int get_linux_version()
350 int a
= 0, b
= 0,c
= 0;
355 a
= strtoul(cp
, &cp
, 10);
357 b
= strtoul(cp
+1, &cp
, 10);
359 c
= strtoul(cp
+1, &cp
, 10);
361 return (a
*1000000)+(b
*1000)+c
;
364 int mdadm_version(char *version
)
372 cp
= strchr(version
, '-');
373 if (!cp
|| *(cp
+1) != ' ' || *(cp
+2) != 'v')
376 a
= strtoul(cp
, &cp
, 10);
379 b
= strtoul(cp
+1, &cp
, 10);
381 c
= strtoul(cp
+1, &cp
, 10);
384 if (*cp
!= ' ' && *cp
!= '-')
386 return (a
*1000000)+(b
*1000)+c
;
389 unsigned long long parse_size(char *size
)
391 /* parse 'size' which should be a number optionally
392 * followed by 'K', 'M', or 'G'.
393 * Without a suffix, K is assumed.
394 * Number returned is in sectors (half-K)
395 * INVALID_SECTORS returned on error.
398 long long s
= strtoll(size
, &c
, 10);
412 s
*= 1024 * 1024 * 2;
414 case 's': /* sectors */
425 int is_near_layout_10(int layout
)
429 fc
= (layout
>> 8) & 255;
430 fo
= layout
& (1 << 16);
431 if (fc
> 1 || fo
> 0)
436 int parse_layout_10(char *layout
)
440 /* Parse the layout string for raid10 */
441 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
442 if ((layout
[0] != 'n' && layout
[0] != 'f' && layout
[0] != 'o') ||
443 (copies
= strtoul(layout
+1, &cp
, 10)) < 1 ||
447 if (layout
[0] == 'n')
449 else if (layout
[0] == 'o')
450 rv
= 0x10000 + (copies
<<8) + 1;
452 rv
= 1 + (copies
<<8);
456 int parse_layout_faulty(char *layout
)
458 /* Parse the layout string for 'faulty' */
459 int ln
= strcspn(layout
, "0123456789");
460 char *m
= xstrdup(layout
);
463 mode
= map_name(faultylayout
, m
);
467 return mode
| (atoi(layout
+ln
)<< ModeShift
);
470 long parse_num(char *num
)
472 /* Either return a valid number, or -1 */
474 long rv
= strtol(num
, &c
, 10);
475 if (rv
< 0 || *c
|| !num
[0])
481 int parse_cluster_confirm_arg(char *input
, char **devname
, int *slot
)
484 *slot
= strtoul(input
, &dev
, 10);
485 if (dev
== input
|| dev
[0] != ':')
491 void remove_partitions(int fd
)
493 /* remove partitions from this block devices.
494 * This is used for components added to an array
496 #ifdef BLKPG_DEL_PARTITION
497 struct blkpg_ioctl_arg a
;
498 struct blkpg_partition p
;
500 a
.op
= BLKPG_DEL_PARTITION
;
502 a
.datalen
= sizeof(p
);
504 memset(a
.data
, 0, a
.datalen
);
505 for (p
.pno
= 0; p
.pno
< 16; p
.pno
++)
506 ioctl(fd
, BLKPG
, &a
);
510 int test_partition(int fd
)
512 /* Check if fd is a whole-disk or a partition.
513 * BLKPG will return EINVAL on a partition, and BLKPG_DEL_PARTITION
514 * will return ENXIO on an invalid partition number.
516 struct blkpg_ioctl_arg a
;
517 struct blkpg_partition p
;
518 a
.op
= BLKPG_DEL_PARTITION
;
520 a
.datalen
= sizeof(p
);
522 memset(a
.data
, 0, a
.datalen
);
524 if (ioctl(fd
, BLKPG
, &a
) == 0)
525 /* Very unlikely, but not a partition */
527 if (errno
== ENXIO
|| errno
== ENOTTY
)
528 /* not a partition */
534 int test_partition_from_id(dev_t id
)
539 sprintf(buf
, "%d:%d", major(id
), minor(id
));
540 fd
= dev_open(buf
, O_RDONLY
);
543 rv
= test_partition(fd
);
548 int enough(int level
, int raid_disks
, int layout
, int clean
, char *avail
)
554 for (i
= 0; i
< raid_disks
; i
++)
555 avail_disks
+= !!avail
[i
];
559 /* This is the tricky one - we need to check
560 * which actual disks are present.
562 copies
= (layout
&255)* ((layout
>>8) & 255);
565 /* there must be one of the 'copies' form 'first' */
572 this = (this+1) % raid_disks
;
576 first
= (first
+(layout
&255)) % raid_disks
;
577 } while (first
!= 0);
580 case LEVEL_MULTIPATH
:
581 return avail_disks
>= 1;
584 return avail_disks
== raid_disks
;
586 return avail_disks
>= 1;
588 if (avail_disks
== raid_disks
- 1 &&
589 !avail
[raid_disks
- 1])
590 /* If just the parity device is missing, then we
591 * have enough, even if not clean
597 return avail_disks
>= raid_disks
-1;
599 return avail_disks
>= raid_disks
;
602 return avail_disks
>= raid_disks
-2;
604 return avail_disks
>= raid_disks
;
610 const int uuid_zero
[4] = { 0, 0, 0, 0 };
612 int same_uuid(int a
[4], int b
[4], int swapuuid
)
615 /* parse uuids are hostendian.
616 * uuid's from some superblocks are big-ending
617 * if there is a difference, we need to swap..
619 unsigned char *ac
= (unsigned char *)a
;
620 unsigned char *bc
= (unsigned char *)b
;
622 for (i
= 0; i
< 16; i
+= 4) {
623 if (ac
[i
+0] != bc
[i
+3] ||
624 ac
[i
+1] != bc
[i
+2] ||
625 ac
[i
+2] != bc
[i
+1] ||
640 void copy_uuid(void *a
, int b
[4], int swapuuid
)
643 /* parse uuids are hostendian.
644 * uuid's from some superblocks are big-ending
645 * if there is a difference, we need to swap..
647 unsigned char *ac
= (unsigned char *)a
;
648 unsigned char *bc
= (unsigned char *)b
;
650 for (i
= 0; i
< 16; i
+= 4) {
660 char *__fname_from_uuid(int id
[4], int swap
, char *buf
, char sep
)
667 copy_uuid(uuid
, id
, swap
);
668 for (i
= 0; i
< 4; i
++) {
671 for (j
= 3; j
>= 0; j
--) {
672 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
680 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
,
683 // dirty hack to work around an issue with super1 superblocks...
684 // super1 superblocks need swapuuid set in order for assembly to
685 // work, but can't have it set if we want this printout to match
686 // all the other uuid printouts in super1.c, so we force swapuuid
687 // to 1 to make our printout match the rest of super1
688 #if __BYTE_ORDER == BIG_ENDIAN
689 return __fname_from_uuid(info
->uuid
, 1, buf
, sep
);
691 return __fname_from_uuid(info
->uuid
, (st
->ss
== &super1
) ? 1 :
692 st
->ss
->swapuuid
, buf
, sep
);
696 int check_ext2(int fd
, char *name
)
699 * Check for an ext2fs file system.
700 * Superblock is always 1K at 1K offset
702 * s_magic is le16 at 56 == 0xEF53
703 * report mtime - le32 at 44
705 * logblksize - le32 at 24
707 unsigned char sb
[1024];
709 unsigned long long size
;
711 if (lseek(fd
, 1024,0)!= 1024)
713 if (read(fd
, sb
, 1024)!= 1024)
715 if (sb
[56] != 0x53 || sb
[57] != 0xef)
718 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
719 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
720 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
722 pr_err("%s appears to contain an ext2fs file system\n",
724 cont_err("size=%lluK mtime=%s", size
, ctime(&mtime
));
728 int check_reiser(int fd
, char *name
)
731 * superblock is at 64K
733 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
736 unsigned char sb
[1024];
737 unsigned long long size
;
738 if (lseek(fd
, 64*1024, 0) != 64*1024)
740 if (read(fd
, sb
, 1024) != 1024)
742 if (strncmp((char*)sb
+52, "ReIsErFs",8) != 0 &&
743 strncmp((char*)sb
+52, "ReIsEr2Fs",9) != 0)
745 pr_err("%s appears to contain a reiserfs file system\n",name
);
746 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
747 cont_err("size = %lluK\n", size
*4);
752 int check_raid(int fd
, char *name
)
757 struct supertype
*st
= guess_super(fd
);
761 if (st
->ss
->add_to_super
!= NULL
) {
762 st
->ss
->load_super(st
, fd
, name
);
763 /* Looks like a raid array .. */
764 pr_err("%s appears to be part of a raid array:\n", name
);
765 st
->ss
->getinfo_super(st
, &info
, NULL
);
766 st
->ss
->free_super(st
);
767 crtime
= info
.array
.ctime
;
768 level
= map_num(pers
, info
.array
.level
);
771 cont_err("level=%s devices=%d ctime=%s",
772 level
, info
.array
.raid_disks
, ctime(&crtime
));
774 /* Looks like GPT or MBR */
775 pr_err("partition table exists on %s\n", name
);
780 int fstat_is_blkdev(int fd
, char *devname
, dev_t
*rdev
)
784 if (fstat(fd
, &stb
) != 0) {
785 pr_err("fstat failed for %s: %s\n", devname
, strerror(errno
));
788 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
) {
789 pr_err("%s is not a block device.\n", devname
);
797 int stat_is_blkdev(char *devname
, dev_t
*rdev
)
801 if (stat(devname
, &stb
) != 0) {
802 pr_err("stat failed for %s: %s\n", devname
, strerror(errno
));
805 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
) {
806 pr_err("%s is not a block device.\n", devname
);
818 for (i
= 0; i
< 5; i
++) {
820 fprintf(stderr
, "%s%s", mesg
, add
);
822 if (fgets(buf
, 100, stdin
)==NULL
)
824 if (buf
[0]=='y' || buf
[0]=='Y')
826 if (buf
[0]=='n' || buf
[0]=='N')
830 pr_err("assuming 'no'\n");
834 int is_standard(char *dev
, int *nump
)
836 /* tests if dev is a "standard" md dev name.
837 * i.e if the last component is "/dNN" or "/mdNN",
838 * where NN is a string of digits
839 * Returns 1 if a partitionable standard,
840 * -1 if non-partitonable,
841 * 0 if not a standard name.
843 char *d
= strrchr(dev
, '/');
848 if (strncmp(d
, "/d",2) == 0)
849 d
+= 2, type
= 1; /* /dev/md/dN{pM} */
850 else if (strncmp(d
, "/md_d", 5) == 0)
851 d
+= 5, type
= 1; /* /dev/md_dN{pM} */
852 else if (strncmp(d
, "/md", 3) == 0)
853 d
+= 3, type
= -1; /* /dev/mdN */
854 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3) == 0)
855 d
+= 1, type
= -1; /* /dev/md/N */
865 if (nump
) *nump
= num
;
870 unsigned long calc_csum(void *super
, int bytes
)
872 unsigned long long newcsum
= 0;
875 unsigned int *superc
= (unsigned int*) super
;
877 for(i
= 0; i
< bytes
/4; i
++)
878 newcsum
+= superc
[i
];
879 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
881 /* The in-kernel checksum calculation is always 16bit on
882 * the alpha, though it is 32 bit on i386...
883 * I wonder what it is elsewhere... (it uses an API in
884 * a way that it shouldn't).
886 csum
= (csum
& 0xffff) + (csum
>> 16);
887 csum
= (csum
& 0xffff) + (csum
>> 16);
892 char *human_size(long long bytes
)
896 /* We convert bytes to either centi-M{ega,ibi}bytes or
897 * centi-G{igi,ibi}bytes, with appropriate rounding,
898 * and then print 1/100th of those as a decimal.
899 * We allow upto 2048Megabytes before converting to
900 * gigabytes, as that shows more precision and isn't
901 * too large a number.
902 * Terabytes are not yet handled.
905 if (bytes
< 5000*1024)
907 else if (bytes
< 2*1024LL*1024LL*1024LL) {
908 long cMiB
= (bytes
* 200LL / (1LL<<20) + 1) / 2;
909 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
910 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
911 cMiB
/100, cMiB
% 100, cMB
/100, cMB
% 100);
913 long cGiB
= (bytes
* 200LL / (1LL<<30) +1) / 2;
914 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
915 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
916 cGiB
/100, cGiB
% 100, cGB
/100, cGB
% 100);
921 char *human_size_brief(long long bytes
, int prefix
)
925 /* We convert bytes to either centi-M{ega,ibi}bytes or
926 * centi-G{igi,ibi}bytes, with appropriate rounding,
927 * and then print 1/100th of those as a decimal.
928 * We allow upto 2048Megabytes before converting to
929 * gigabytes, as that shows more precision and isn't
930 * too large a number.
931 * Terabytes are not yet handled.
933 * If prefix == IEC, we mean prefixes like kibi,mebi,gibi etc.
934 * If prefix == JEDEC, we mean prefixes like kilo,mega,giga etc.
937 if (bytes
< 5000*1024)
939 else if (prefix
== IEC
) {
940 if (bytes
< 2*1024LL*1024LL*1024LL) {
941 long cMiB
= (bytes
* 200LL / (1LL<<20) +1) /2;
942 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
943 cMiB
/100, cMiB
% 100);
945 long cGiB
= (bytes
* 200LL / (1LL<<30) +1) /2;
946 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
947 cGiB
/100, cGiB
% 100);
950 else if (prefix
== JEDEC
) {
951 if (bytes
< 2*1024LL*1024LL*1024LL) {
952 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
953 snprintf(buf
, sizeof(buf
), "%ld.%02ldMB",
956 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
957 snprintf(buf
, sizeof(buf
), "%ld.%02ldGB",
967 void print_r10_layout(int layout
)
969 int near
= layout
& 255;
970 int far
= (layout
>> 8) & 255;
971 int offset
= (layout
&0x10000);
975 printf("%s near=%d", sep
, near
);
979 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
981 printf("NO REDUNDANCY");
984 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
985 int chunksize
, unsigned long long devsize
)
989 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
990 return get_data_disks(level
, layout
, raid_disks
) * devsize
;
993 int get_data_disks(int level
, int layout
, int raid_disks
)
997 case 0: data_disks
= raid_disks
;
999 case 1: data_disks
= 1;
1002 case 5: data_disks
= raid_disks
- 1;
1004 case 6: data_disks
= raid_disks
- 2;
1006 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
1013 dev_t
devnm2devid(char *devnm
)
1015 /* First look in /sys/block/$DEVNM/dev for %d:%d
1016 * If that fails, try parsing out a number
1023 sprintf(path
, "/sys/block/%s/dev", devnm
);
1024 fd
= open(path
, O_RDONLY
);
1027 int n
= read(fd
, buf
, sizeof(buf
));
1031 if (n
> 0 && sscanf(buf
, "%d:%d\n", &mjr
, &mnr
) == 2)
1032 return makedev(mjr
, mnr
);
1034 if (strncmp(devnm
, "md_d", 4) == 0 &&
1035 isdigit(devnm
[4]) &&
1036 (mnr
= strtoul(devnm
+4, &ep
, 10)) >= 0 &&
1037 ep
> devnm
&& *ep
== 0)
1038 return makedev(get_mdp_major(), mnr
<< MdpMinorShift
);
1040 if (strncmp(devnm
, "md", 2) == 0 &&
1041 isdigit(devnm
[2]) &&
1042 (mnr
= strtoul(devnm
+2, &ep
, 10)) >= 0 &&
1043 ep
> devnm
&& *ep
== 0)
1044 return makedev(MD_MAJOR
, mnr
);
1049 char *get_md_name(char *devnm
)
1051 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
1052 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
1054 static char devname
[50];
1056 dev_t rdev
= devnm2devid(devnm
);
1061 if (strncmp(devnm
, "md_", 3) == 0) {
1062 snprintf(devname
, sizeof(devname
), "/dev/md/%s",
1064 if (stat(devname
, &stb
) == 0 &&
1065 (S_IFMT
&stb
.st_mode
) == S_IFBLK
&& (stb
.st_rdev
== rdev
))
1068 snprintf(devname
, sizeof(devname
), "/dev/%s", devnm
);
1069 if (stat(devname
, &stb
) == 0 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
&&
1070 (stb
.st_rdev
== rdev
))
1073 snprintf(devname
, sizeof(devname
), "/dev/md/%s", devnm
+2);
1074 if (stat(devname
, &stb
) == 0 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
&&
1075 (stb
.st_rdev
== rdev
))
1078 dn
= map_dev(major(rdev
), minor(rdev
), 0);
1081 snprintf(devname
, sizeof(devname
), "/dev/.tmp.%s", devnm
);
1082 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
1083 if (errno
!= EEXIST
)
1086 if (stat(devname
, &stb
) == 0 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
&&
1087 (stb
.st_rdev
== rdev
))
1093 void put_md_name(char *name
)
1095 if (strncmp(name
, "/dev/.tmp.md", 12) == 0)
1099 int get_maj_min(char *dev
, int *major
, int *minor
)
1102 *major
= strtoul(dev
, &e
, 0);
1103 return (e
> dev
&& *e
== ':' && e
[1] &&
1104 (*minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
1108 int dev_open(char *dev
, int flags
)
1110 /* like 'open', but if 'dev' matches %d:%d, create a temp
1111 * block device and open that
1122 if (get_maj_min(dev
, &major
, &minor
)) {
1123 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
1124 (int)getpid(), major
, minor
);
1125 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
)) == 0) {
1126 fd
= open(devname
, flags
);
1130 /* Try /tmp as /dev appear to be read-only */
1131 snprintf(devname
, sizeof(devname
),
1132 "/tmp/.tmp.md.%d:%d:%d",
1133 (int)getpid(), major
, minor
);
1134 if (mknod(devname
, S_IFBLK
|0600,
1135 makedev(major
, minor
)) == 0) {
1136 fd
= open(devname
, flags
);
1141 fd
= open(dev
, flags
);
1145 int open_dev_flags(char *devnm
, int flags
)
1150 devid
= devnm2devid(devnm
);
1151 sprintf(buf
, "%d:%d", major(devid
), minor(devid
));
1152 return dev_open(buf
, flags
);
1155 int open_dev(char *devnm
)
1157 return open_dev_flags(devnm
, O_RDONLY
);
1160 int open_dev_excl(char *devnm
)
1165 dev_t devid
= devnm2devid(devnm
);
1168 sprintf(buf
, "%d:%d", major(devid
), minor(devid
));
1169 for (i
= 0; i
< 25; i
++) {
1170 int fd
= dev_open(buf
, flags
|O_EXCL
);
1173 if (errno
== EACCES
&& flags
== O_RDWR
) {
1186 int same_dev(char *one
, char *two
)
1188 struct stat st1
, st2
;
1189 if (stat(one
, &st1
) != 0)
1191 if (stat(two
, &st2
) != 0)
1193 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
1195 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
1197 return st1
.st_rdev
== st2
.st_rdev
;
1200 void wait_for(char *dev
, int fd
)
1203 struct stat stb_want
;
1206 if (fstat(fd
, &stb_want
) != 0 ||
1207 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
1210 for (i
= 0; i
< 25; i
++) {
1212 if (stat(dev
, &stb
) == 0 &&
1213 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
1214 (stb
.st_rdev
== stb_want
.st_rdev
))
1221 pr_err("timeout waiting for %s\n", dev
);
1224 struct superswitch
*superlist
[] =
1227 &super_ddf
, &super_imsm
,
1232 struct supertype
*super_by_fd(int fd
, char **subarrayp
)
1234 mdu_array_info_t array
;
1237 struct supertype
*st
= NULL
;
1242 char *subarray
= NULL
;
1243 char container
[32] = "";
1245 sra
= sysfs_read(fd
, NULL
, GET_VERSION
);
1248 vers
= sra
->array
.major_version
;
1249 minor
= sra
->array
.minor_version
;
1250 verstr
= sra
->text_version
;
1252 if (md_get_array_info(fd
, &array
))
1253 array
.major_version
= array
.minor_version
= 0;
1254 vers
= array
.major_version
;
1255 minor
= array
.minor_version
;
1260 sprintf(version
, "%d.%d", vers
, minor
);
1263 if (minor
== -2 && is_subarray(verstr
)) {
1264 char *dev
= verstr
+1;
1266 subarray
= strchr(dev
, '/');
1269 subarray
= xstrdup(subarray
);
1271 strcpy(container
, dev
);
1273 sra
= sysfs_read(-1, container
, GET_VERSION
);
1274 if (sra
&& sra
->text_version
[0])
1275 verstr
= sra
->text_version
;
1277 verstr
= "-no-metadata-";
1280 for (i
= 0; st
== NULL
&& superlist
[i
]; i
++)
1281 st
= superlist
[i
]->match_metadata_desc(verstr
);
1287 *subarrayp
= subarray
;
1288 strcpy(st
->container_devnm
, container
);
1289 strcpy(st
->devnm
, fd2devnm(fd
));
1296 int dev_size_from_id(dev_t id
, unsigned long long *size
)
1301 sprintf(buf
, "%d:%d", major(id
), minor(id
));
1302 fd
= dev_open(buf
, O_RDONLY
);
1305 if (get_dev_size(fd
, NULL
, size
)) {
1313 int dev_sector_size_from_id(dev_t id
, unsigned int *size
)
1318 sprintf(buf
, "%d:%d", major(id
), minor(id
));
1319 fd
= dev_open(buf
, O_RDONLY
);
1322 if (get_dev_sector_size(fd
, NULL
, size
)) {
1330 struct supertype
*dup_super(struct supertype
*orig
)
1332 struct supertype
*st
;
1336 st
= xcalloc(1, sizeof(*st
));
1338 st
->max_devs
= orig
->max_devs
;
1339 st
->minor_version
= orig
->minor_version
;
1340 st
->ignore_hw_compat
= orig
->ignore_hw_compat
;
1341 st
->data_offset
= orig
->data_offset
;
1347 struct supertype
*guess_super_type(int fd
, enum guess_types guess_type
)
1349 /* try each load_super to find the best match,
1350 * and return the best superswitch
1352 struct superswitch
*ss
;
1353 struct supertype
*st
;
1354 unsigned int besttime
= 0;
1358 st
= xcalloc(1, sizeof(*st
));
1359 st
->container_devnm
[0] = 0;
1361 for (i
= 0; superlist
[i
]; i
++) {
1364 if (guess_type
== guess_array
&& ss
->add_to_super
== NULL
)
1366 if (guess_type
== guess_partitions
&& ss
->add_to_super
!= NULL
)
1368 memset(st
, 0, sizeof(*st
));
1369 st
->ignore_hw_compat
= 1;
1370 rv
= ss
->load_super(st
, fd
, NULL
);
1373 st
->ss
->getinfo_super(st
, &info
, NULL
);
1374 if (bestsuper
== -1 ||
1375 besttime
< info
.array
.ctime
) {
1377 besttime
= info
.array
.ctime
;
1382 if (bestsuper
!= -1) {
1384 memset(st
, 0, sizeof(*st
));
1385 st
->ignore_hw_compat
= 1;
1386 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1388 superlist
[bestsuper
]->free_super(st
);
1396 /* Return size of device in bytes */
1397 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1399 unsigned long long ldsize
;
1402 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1403 ldsize
= (unsigned long long)st
.st_size
;
1406 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1409 unsigned long dsize
;
1410 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1415 pr_err("Cannot get size of %s: %s\n",
1416 dname
, strerror(errno
));
1424 /* Return sector size of device in bytes */
1425 int get_dev_sector_size(int fd
, char *dname
, unsigned int *sectsizep
)
1427 unsigned int sectsize
;
1429 if (ioctl(fd
, BLKSSZGET
, §size
) != 0) {
1431 pr_err("Cannot get sector size of %s: %s\n",
1432 dname
, strerror(errno
));
1436 *sectsizep
= sectsize
;
1440 /* Return true if this can only be a container, not a member device.
1441 * i.e. is and md device and size is zero
1443 int must_be_container(int fd
)
1446 unsigned long long size
;
1448 mdi
= sysfs_read(fd
, NULL
, GET_VERSION
);
1453 if (get_dev_size(fd
, NULL
, &size
) == 0)
1460 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1461 * Returns: 1 if successful
1462 * -1 for unknown partition type
1463 * 0 for other errors
1465 static int get_gpt_last_partition_end(int fd
, unsigned long long *endofpart
)
1468 unsigned char empty_gpt_entry
[16]= {0};
1469 struct GPT_part_entry
*part
;
1471 unsigned long long curr_part_end
;
1472 unsigned all_partitions
, entry_size
;
1474 unsigned int sector_size
= 0;
1478 BUILD_BUG_ON(sizeof(gpt
) != 512);
1479 /* skip protective MBR */
1480 if (!get_dev_sector_size(fd
, NULL
, §or_size
))
1482 lseek(fd
, sector_size
, SEEK_SET
);
1483 /* read GPT header */
1484 if (read(fd
, &gpt
, 512) != 512)
1487 /* get the number of partition entries and the entry size */
1488 all_partitions
= __le32_to_cpu(gpt
.part_cnt
);
1489 entry_size
= __le32_to_cpu(gpt
.part_size
);
1491 /* Check GPT signature*/
1492 if (gpt
.magic
!= GPT_SIGNATURE_MAGIC
)
1496 if (all_partitions
> 1024 ||
1497 entry_size
> sizeof(buf
))
1500 part
= (struct GPT_part_entry
*)buf
;
1502 /* set offset to third block (GPT entries) */
1503 lseek(fd
, sector_size
*2, SEEK_SET
);
1504 for (part_nr
= 0; part_nr
< all_partitions
; part_nr
++) {
1505 /* read partition entry */
1506 if (read(fd
, buf
, entry_size
) != (ssize_t
)entry_size
)
1509 /* is this valid partition? */
1510 if (memcmp(part
->type_guid
, empty_gpt_entry
, 16) != 0) {
1511 /* check the last lba for the current partition */
1512 curr_part_end
= __le64_to_cpu(part
->ending_lba
);
1513 if (curr_part_end
> *endofpart
)
1514 *endofpart
= curr_part_end
;
1521 /* Sets endofpart parameter to the last block used by the last partition on the device.
1522 * Returns: 1 if successful
1523 * -1 for unknown partition type
1524 * 0 for other errors
1526 static int get_last_partition_end(int fd
, unsigned long long *endofpart
)
1528 struct MBR boot_sect
;
1529 unsigned long long curr_part_end
;
1531 unsigned int sector_size
;
1536 BUILD_BUG_ON(sizeof(boot_sect
) != 512);
1539 if (read(fd
, &boot_sect
, 512) != 512)
1542 /* check MBP signature */
1543 if (boot_sect
.magic
== MBR_SIGNATURE_MAGIC
) {
1545 /* found the correct signature */
1547 for (part_nr
= 0; part_nr
< MBR_PARTITIONS
; part_nr
++) {
1549 * Have to make every access through boot_sect rather
1550 * than using a pointer to the partition table (or an
1551 * entry), since the entries are not properly aligned.
1554 /* check for GPT type */
1555 if (boot_sect
.parts
[part_nr
].part_type
==
1556 MBR_GPT_PARTITION_TYPE
) {
1557 retval
= get_gpt_last_partition_end(fd
, endofpart
);
1560 /* check the last used lba for the current partition */
1562 __le32_to_cpu(boot_sect
.parts
[part_nr
].first_sect_lba
) +
1563 __le32_to_cpu(boot_sect
.parts
[part_nr
].blocks_num
);
1564 if (curr_part_end
> *endofpart
)
1565 *endofpart
= curr_part_end
;
1568 /* Unknown partition table */
1571 /* calculate number of 512-byte blocks */
1572 if (get_dev_sector_size(fd
, NULL
, §or_size
))
1573 *endofpart
*= (sector_size
/ 512);
1578 int check_partitions(int fd
, char *dname
, unsigned long long freesize
,
1579 unsigned long long size
)
1582 * Check where the last partition ends
1584 unsigned long long endofpart
;
1586 if (get_last_partition_end(fd
, &endofpart
) > 0) {
1587 /* There appears to be a partition table here */
1588 if (freesize
== 0) {
1589 /* partitions will not be visible in new device */
1590 pr_err("partition table exists on %s but will be lost or\n"
1591 " meaningless after creating array\n",
1594 } else if (endofpart
> freesize
) {
1595 /* last partition overlaps metadata */
1596 pr_err("metadata will over-write last partition on %s.\n",
1599 } else if (size
&& endofpart
> size
) {
1600 /* partitions will be truncated in new device */
1601 pr_err("array size is too small to cover all partitions on %s.\n",
1609 int open_container(int fd
)
1611 /* 'fd' is a block device. Find out if it is in use
1612 * by a container, and return an open fd on that container.
1623 if (fstat(fd
, &st
) != 0)
1625 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1626 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1627 e
= path
+ strlen(path
);
1629 dir
= opendir(path
);
1632 while ((de
= readdir(dir
))) {
1635 if (de
->d_name
[0] == '.')
1637 /* Need to make sure it is a container and not a volume */
1638 sprintf(e
, "/%s/md/metadata_version", de
->d_name
);
1639 dfd
= open(path
, O_RDONLY
);
1642 n
= read(dfd
, buf
, sizeof(buf
));
1644 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1647 if (strncmp(buf
, "external", 8) != 0 ||
1651 sprintf(e
, "/%s/dev", de
->d_name
);
1652 dfd
= open(path
, O_RDONLY
);
1655 n
= read(dfd
, buf
, sizeof(buf
));
1657 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1660 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1662 sprintf(buf
, "%d:%d", major
, minor
);
1663 dfd
= dev_open(buf
, O_RDONLY
);
1673 struct superswitch
*version_to_superswitch(char *vers
)
1677 for (i
= 0; superlist
[i
]; i
++) {
1678 struct superswitch
*ss
= superlist
[i
];
1680 if (strcmp(vers
, ss
->name
) == 0)
1687 int metadata_container_matches(char *metadata
, char *devnm
)
1689 /* Check if 'devnm' is the container named in 'metadata'
1691 * /containername/componentname or
1692 * -containername/componentname
1695 if (*metadata
!= '/' && *metadata
!= '-')
1698 if (strncmp(metadata
+1, devnm
, l
) != 0)
1700 if (metadata
[l
+1] != '/')
1705 int metadata_subdev_matches(char *metadata
, char *devnm
)
1707 /* Check if 'devnm' is the subdev named in 'metadata'
1709 * /containername/subdev or
1710 * -containername/subdev
1713 if (*metadata
!= '/' && *metadata
!= '-')
1715 sl
= strchr(metadata
+1, '/');
1718 if (strcmp(sl
+1, devnm
) == 0)
1723 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
1725 if (mdstat
->metadata_version
== NULL
||
1726 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
1727 !metadata_container_matches(mdstat
->metadata_version
+9, container
))
1733 int is_subarray_active(char *subarray
, char *container
)
1735 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1736 struct mdstat_ent
*ent
;
1738 for (ent
= mdstat
; ent
; ent
= ent
->next
)
1739 if (is_container_member(ent
, container
))
1740 if (strcmp(to_subarray(ent
, container
), subarray
) == 0)
1743 free_mdstat(mdstat
);
1748 /* open_subarray - opens a subarray in a container
1749 * @dev: container device name
1750 * @st: empty supertype
1751 * @quiet: block reporting errors flag
1753 * On success returns an fd to a container and fills in *st
1755 int open_subarray(char *dev
, char *subarray
, struct supertype
*st
, int quiet
)
1758 struct mdinfo
*info
;
1762 fd
= open(dev
, O_RDWR
|O_EXCL
);
1765 pr_err("Couldn't open %s, aborting\n",
1770 _devnm
= fd2devnm(fd
);
1771 if (_devnm
== NULL
) {
1773 pr_err("Failed to determine device number for %s\n",
1777 strcpy(st
->devnm
, _devnm
);
1779 mdi
= sysfs_read(fd
, st
->devnm
, GET_VERSION
|GET_LEVEL
);
1782 pr_err("Failed to read sysfs for %s\n",
1787 if (mdi
->array
.level
!= UnSet
) {
1789 pr_err("%s is not a container\n", dev
);
1793 st
->ss
= version_to_superswitch(mdi
->text_version
);
1796 pr_err("Operation not supported for %s metadata\n",
1801 if (st
->devnm
[0] == 0) {
1803 pr_err("Failed to allocate device name\n");
1807 if (!st
->ss
->load_container
) {
1809 pr_err("%s is not a container\n", dev
);
1813 if (st
->ss
->load_container(st
, fd
, NULL
)) {
1815 pr_err("Failed to load metadata for %s\n",
1820 info
= st
->ss
->container_content(st
, subarray
);
1823 pr_err("Failed to find subarray-%s in %s\n",
1833 st
->ss
->free_super(st
);
1846 int add_disk(int mdfd
, struct supertype
*st
,
1847 struct mdinfo
*sra
, struct mdinfo
*info
)
1849 /* Add a device to an array, in one of 2 ways. */
1852 if (st
->ss
->external
) {
1853 if (info
->disk
.state
& (1<<MD_DISK_SYNC
))
1854 info
->recovery_start
= MaxSector
;
1856 info
->recovery_start
= 0;
1857 rv
= sysfs_add_disk(sra
, info
, 0);
1860 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1864 sd2
= xmalloc(sizeof(*sd2
));
1866 sd2
->next
= sra
->devs
;
1871 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1875 int remove_disk(int mdfd
, struct supertype
*st
,
1876 struct mdinfo
*sra
, struct mdinfo
*info
)
1880 /* Remove the disk given by 'info' from the array */
1881 if (st
->ss
->external
)
1882 rv
= sysfs_set_str(sra
, info
, "slot", "none");
1884 rv
= ioctl(mdfd
, HOT_REMOVE_DISK
, makedev(info
->disk
.major
,
1889 int hot_remove_disk(int mdfd
, unsigned long dev
, int force
)
1891 int cnt
= force
? 500 : 5;
1894 /* HOT_REMOVE_DISK can fail with EBUSY if there are
1895 * outstanding IO requests to the device.
1896 * In this case, it can be helpful to wait a little while,
1897 * up to 5 seconds if 'force' is set, or 50 msec if not.
1899 while ((ret
= ioctl(mdfd
, HOT_REMOVE_DISK
, dev
)) == -1 &&
1907 int sys_hot_remove_disk(int statefd
, int force
)
1909 int cnt
= force
? 500 : 5;
1912 while ((ret
= write(statefd
, "remove", 6)) == -1 &&
1916 return ret
== 6 ? 0 : -1;
1919 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1921 /* Initialise kernel's knowledge of array.
1922 * This varies between externally managed arrays
1925 mdu_array_info_t inf
;
1928 if (st
->ss
->external
)
1929 return sysfs_set_array(info
, 9003);
1931 memset(&inf
, 0, sizeof(inf
));
1932 inf
.major_version
= info
->array
.major_version
;
1933 inf
.minor_version
= info
->array
.minor_version
;
1934 rv
= md_set_array_info(mdfd
, &inf
);
1939 unsigned long long min_recovery_start(struct mdinfo
*array
)
1941 /* find the minimum recovery_start in an array for metadata
1942 * formats that only record per-array recovery progress instead
1945 unsigned long long recovery_start
= MaxSector
;
1948 for (d
= array
->devs
; d
; d
= d
->next
)
1949 recovery_start
= min(recovery_start
, d
->recovery_start
);
1951 return recovery_start
;
1954 int mdmon_pid(char *devnm
)
1961 sprintf(path
, "%s/%s.pid", MDMON_DIR
, devnm
);
1963 fd
= open(path
, O_RDONLY
| O_NOATIME
, 0);
1967 n
= read(fd
, pid
, 9);
1974 int mdmon_running(char *devnm
)
1976 int pid
= mdmon_pid(devnm
);
1979 if (kill(pid
, 0) == 0)
1984 int start_mdmon(char *devnm
)
1998 if (check_env("MDADM_NO_MDMON"))
2001 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
)-1);
2005 sl
= strrchr(pathbuf
, '/');
2010 strcpy(sl
, "mdmon");
2014 /* First try to run systemctl */
2015 if (!check_env("MDADM_NO_SYSTEMCTL"))
2018 /* FIXME yuk. CLOSE_EXEC?? */
2020 for (i
= 3; skipped
< 20; i
++)
2026 /* Don't want to see error messages from
2027 * systemctl. If the service doesn't exist,
2028 * we start mdmon ourselves.
2031 open("/dev/null", O_WRONLY
);
2032 snprintf(pathbuf
, sizeof(pathbuf
), "mdmon@%s.service",
2034 status
= execl("/usr/bin/systemctl", "systemctl",
2037 status
= execl("/bin/systemctl", "systemctl", "start",
2040 case -1: pr_err("cannot run mdmon. Array remains readonly\n");
2042 default: /* parent - good */
2043 pid
= wait(&status
);
2044 if (pid
>= 0 && status
== 0)
2048 /* That failed, try running mdmon directly */
2051 /* FIXME yuk. CLOSE_EXEC?? */
2053 for (i
= 3; skipped
< 20; i
++)
2059 for (i
= 0; paths
[i
]; i
++)
2061 execl(paths
[i
], paths
[i
],
2065 case -1: pr_err("cannot run mdmon. Array remains readonly\n");
2067 default: /* parent - good */
2068 pid
= wait(&status
);
2069 if (pid
< 0 || status
!= 0) {
2070 pr_err("failed to launch mdmon. Array remains readonly\n");
2077 __u32
random32(void)
2080 int rfd
= open("/dev/urandom", O_RDONLY
);
2081 if (rfd
< 0 || read(rfd
, &rv
, 4) != 4)
2088 void random_uuid(__u8
*buf
)
2093 fd
= open("/dev/urandom", O_RDONLY
);
2096 len
= read(fd
, buf
, 16);
2104 for (i
= 0; i
< 4; i
++)
2109 int flush_metadata_updates(struct supertype
*st
)
2113 st
->update_tail
= NULL
;
2117 sfd
= connect_monitor(st
->container_devnm
);
2121 while (st
->updates
) {
2122 struct metadata_update
*mu
= st
->updates
;
2123 st
->updates
= mu
->next
;
2125 send_message(sfd
, mu
, 0);
2133 st
->update_tail
= NULL
;
2137 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
2140 struct metadata_update
*mu
= xmalloc(sizeof(*mu
));
2145 mu
->space_list
= NULL
;
2147 *st
->update_tail
= mu
;
2148 st
->update_tail
= &mu
->next
;
2152 /* tinyc doesn't optimize this check in ioctl.h out ... */
2153 unsigned int __invalid_size_argument_for_IOC
= 0;
2156 /* Pick all spares matching given criteria from a container
2157 * if min_size == 0 do not check size
2158 * if domlist == NULL do not check domains
2159 * if spare_group given add it to domains of each spare
2160 * metadata allows to test domains using metadata of destination array */
2161 struct mdinfo
*container_choose_spares(struct supertype
*st
,
2162 struct spare_criteria
*criteria
,
2163 struct domainlist
*domlist
,
2165 const char *metadata
, int get_one
)
2167 struct mdinfo
*d
, **dp
, *disks
= NULL
;
2169 /* get list of all disks in container */
2170 if (st
->ss
->getinfo_super_disks
)
2171 disks
= st
->ss
->getinfo_super_disks(st
);
2175 /* find spare devices on the list */
2177 disks
->array
.spare_disks
= 0;
2181 if (d
->disk
.state
== 0) {
2182 /* check if size is acceptable */
2183 unsigned long long dev_size
;
2184 unsigned int dev_sector_size
;
2186 int sector_size_valid
= 0;
2188 dev_t dev
= makedev(d
->disk
.major
,d
->disk
.minor
);
2190 if (!criteria
->min_size
||
2191 (dev_size_from_id(dev
, &dev_size
) &&
2192 dev_size
>= criteria
->min_size
))
2195 if (!criteria
->sector_size
||
2196 (dev_sector_size_from_id(dev
, &dev_sector_size
) &&
2197 criteria
->sector_size
== dev_sector_size
))
2198 sector_size_valid
= 1;
2200 found
= size_valid
&& sector_size_valid
;
2202 /* check if domain matches */
2203 if (found
&& domlist
) {
2204 struct dev_policy
*pol
= devid_policy(dev
);
2206 pol_add(&pol
, pol_domain
,
2208 if (domain_test(domlist
, pol
, metadata
) != 1)
2210 dev_policy_free(pol
);
2215 disks
->array
.spare_disks
++;
2229 /* Checks if paths point to the same device
2230 * Returns 0 if they do.
2231 * Returns 1 if they don't.
2232 * Returns -1 if something went wrong,
2233 * e.g. paths are empty or the files
2234 * they point to don't exist */
2235 int compare_paths (char* path1
, char* path2
)
2237 struct stat st1
,st2
;
2239 if (path1
== NULL
|| path2
== NULL
)
2241 if (stat(path1
,&st1
) != 0)
2243 if (stat(path2
,&st2
) != 0)
2245 if ((st1
.st_ino
== st2
.st_ino
) && (st1
.st_dev
== st2
.st_dev
))
2250 /* Make sure we can open as many devices as needed */
2251 void enable_fds(int devices
)
2253 unsigned int fds
= 20 + devices
;
2255 if (getrlimit(RLIMIT_NOFILE
, &lim
) != 0 || lim
.rlim_cur
>= fds
)
2257 if (lim
.rlim_max
< fds
)
2260 setrlimit(RLIMIT_NOFILE
, &lim
);
2265 /* This is based on similar function in systemd. */
2267 /* statfs.f_type is signed long on s390x and MIPS, causing all
2268 sorts of sign extension problems with RAMFS_MAGIC being
2269 defined as 0x858458f6 */
2270 return statfs("/", &s
) >= 0 &&
2271 ((unsigned long)s
.f_type
== TMPFS_MAGIC
||
2272 ((unsigned long)s
.f_type
& 0xFFFFFFFFUL
) ==
2273 ((unsigned long)RAMFS_MAGIC
& 0xFFFFFFFFUL
));
2276 void reopen_mddev(int mdfd
)
2278 /* Re-open without any O_EXCL, but keep
2283 devnm
= fd2devnm(mdfd
);
2285 fd
= open_dev(devnm
);
2286 if (fd
>= 0 && fd
!= mdfd
)
2290 static struct cmap_hooks
*cmap_hooks
= NULL
;
2291 static int is_cmap_hooks_ready
= 0;
2293 void set_cmap_hooks(void)
2295 cmap_hooks
= xmalloc(sizeof(struct cmap_hooks
));
2296 cmap_hooks
->cmap_handle
= dlopen("libcmap.so.4", RTLD_NOW
| RTLD_LOCAL
);
2297 if (!cmap_hooks
->cmap_handle
)
2300 cmap_hooks
->initialize
=
2301 dlsym(cmap_hooks
->cmap_handle
, "cmap_initialize");
2302 cmap_hooks
->get_string
=
2303 dlsym(cmap_hooks
->cmap_handle
, "cmap_get_string");
2304 cmap_hooks
->finalize
= dlsym(cmap_hooks
->cmap_handle
, "cmap_finalize");
2306 if (!cmap_hooks
->initialize
|| !cmap_hooks
->get_string
||
2307 !cmap_hooks
->finalize
)
2308 dlclose(cmap_hooks
->cmap_handle
);
2310 is_cmap_hooks_ready
= 1;
2313 int get_cluster_name(char **cluster_name
)
2316 cmap_handle_t handle
;
2318 if (!is_cmap_hooks_ready
)
2321 rv
= cmap_hooks
->initialize(&handle
);
2325 rv
= cmap_hooks
->get_string(handle
, "totem.cluster_name", cluster_name
);
2327 free(*cluster_name
);
2334 cmap_hooks
->finalize(handle
);
2339 void set_dlm_hooks(void)
2341 dlm_hooks
= xmalloc(sizeof(struct dlm_hooks
));
2342 dlm_hooks
->dlm_handle
= dlopen("libdlm_lt.so.3", RTLD_NOW
| RTLD_LOCAL
);
2343 if (!dlm_hooks
->dlm_handle
)
2346 dlm_hooks
->open_lockspace
=
2347 dlsym(dlm_hooks
->dlm_handle
, "dlm_open_lockspace");
2348 dlm_hooks
->create_lockspace
=
2349 dlsym(dlm_hooks
->dlm_handle
, "dlm_create_lockspace");
2350 dlm_hooks
->release_lockspace
=
2351 dlsym(dlm_hooks
->dlm_handle
, "dlm_release_lockspace");
2352 dlm_hooks
->ls_lock
= dlsym(dlm_hooks
->dlm_handle
, "dlm_ls_lock");
2353 dlm_hooks
->ls_unlock_wait
=
2354 dlsym(dlm_hooks
->dlm_handle
, "dlm_ls_unlock_wait");
2355 dlm_hooks
->ls_get_fd
= dlsym(dlm_hooks
->dlm_handle
, "dlm_ls_get_fd");
2356 dlm_hooks
->dispatch
= dlsym(dlm_hooks
->dlm_handle
, "dlm_dispatch");
2358 if (!dlm_hooks
->open_lockspace
|| !dlm_hooks
->create_lockspace
||
2359 !dlm_hooks
->ls_lock
|| !dlm_hooks
->ls_unlock_wait
||
2360 !dlm_hooks
->release_lockspace
|| !dlm_hooks
->ls_get_fd
||
2361 !dlm_hooks
->dispatch
)
2362 dlclose(dlm_hooks
->dlm_handle
);
2364 is_dlm_hooks_ready
= 1;
2367 void set_hooks(void)
2373 int zero_disk_range(int fd
, unsigned long long sector
, size_t count
)
2379 size_t len
= count
* 512;
2382 fd_zero
= open("/dev/zero", O_RDONLY
);
2384 pr_err("Cannot open /dev/zero\n");
2388 if (lseek64(fd
, sector
* 512, SEEK_SET
) < 0) {
2390 pr_err("Failed to seek offset for zeroing\n");
2394 addr
= mmap(NULL
, len
, PROT_READ
, MAP_PRIVATE
, fd_zero
, 0);
2396 if (addr
== MAP_FAILED
) {
2398 pr_err("Mapping /dev/zero failed\n");
2403 n
= write(fd
, addr
+ written
, len
- written
);
2408 pr_err("Zeroing disk range failed\n");
2412 } while (written
!= len
);